]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
4f5dd394 | 2 | |
11c3a366 TA |
3 | #include <errno.h> |
4 | #include <stdlib.h> | |
5 | #include <string.h> | |
6 | ||
b5efdb8a | 7 | #include "alloc-util.h" |
e4e73a63 LP |
8 | #include "escape.h" |
9 | #include "hexdecoct.h" | |
11c3a366 | 10 | #include "macro.h" |
4f5dd394 | 11 | #include "utf8.h" |
4f5dd394 | 12 | |
b778252b ZJS |
13 | int cescape_char(char c, char *buf) { |
14 | char *buf_old = buf; | |
4f5dd394 | 15 | |
76a35973 LP |
16 | /* Needs space for 4 characters in the buffer */ |
17 | ||
4f5dd394 LP |
18 | switch (c) { |
19 | ||
20 | case '\a': | |
21 | *(buf++) = '\\'; | |
22 | *(buf++) = 'a'; | |
23 | break; | |
24 | case '\b': | |
25 | *(buf++) = '\\'; | |
26 | *(buf++) = 'b'; | |
27 | break; | |
28 | case '\f': | |
29 | *(buf++) = '\\'; | |
30 | *(buf++) = 'f'; | |
31 | break; | |
32 | case '\n': | |
33 | *(buf++) = '\\'; | |
34 | *(buf++) = 'n'; | |
35 | break; | |
36 | case '\r': | |
37 | *(buf++) = '\\'; | |
38 | *(buf++) = 'r'; | |
39 | break; | |
40 | case '\t': | |
41 | *(buf++) = '\\'; | |
42 | *(buf++) = 't'; | |
43 | break; | |
44 | case '\v': | |
45 | *(buf++) = '\\'; | |
46 | *(buf++) = 'v'; | |
47 | break; | |
48 | case '\\': | |
49 | *(buf++) = '\\'; | |
50 | *(buf++) = '\\'; | |
51 | break; | |
52 | case '"': | |
53 | *(buf++) = '\\'; | |
54 | *(buf++) = '"'; | |
55 | break; | |
56 | case '\'': | |
57 | *(buf++) = '\\'; | |
58 | *(buf++) = '\''; | |
59 | break; | |
60 | ||
61 | default: | |
62 | /* For special chars we prefer octal over | |
63 | * hexadecimal encoding, simply because glib's | |
64 | * g_strescape() does the same */ | |
65 | if ((c < ' ') || (c >= 127)) { | |
66 | *(buf++) = '\\'; | |
67 | *(buf++) = octchar((unsigned char) c >> 6); | |
68 | *(buf++) = octchar((unsigned char) c >> 3); | |
69 | *(buf++) = octchar((unsigned char) c); | |
70 | } else | |
71 | *(buf++) = c; | |
72 | break; | |
73 | } | |
74 | ||
75 | return buf - buf_old; | |
76 | } | |
77 | ||
a5ef3638 | 78 | char *cescape_length(const char *s, size_t n) { |
4f5dd394 | 79 | const char *f; |
a5ef3638 | 80 | char *r, *t; |
4f5dd394 | 81 | |
a5ef3638 | 82 | assert(s || n == 0); |
4f5dd394 LP |
83 | |
84 | /* Does C style string escaping. May be reversed with | |
85 | * cunescape(). */ | |
86 | ||
a5ef3638 | 87 | r = new(char, n*4 + 1); |
4f5dd394 LP |
88 | if (!r) |
89 | return NULL; | |
90 | ||
a5ef3638 | 91 | for (f = s, t = r; f < s + n; f++) |
4f5dd394 LP |
92 | t += cescape_char(*f, t); |
93 | ||
94 | *t = 0; | |
95 | ||
96 | return r; | |
97 | } | |
98 | ||
a5ef3638 LP |
99 | char *cescape(const char *s) { |
100 | assert(s); | |
101 | ||
102 | return cescape_length(s, strlen(s)); | |
103 | } | |
104 | ||
c932fb71 | 105 | int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit) { |
4f5dd394 LP |
106 | int r = 1; |
107 | ||
108 | assert(p); | |
4f5dd394 LP |
109 | assert(ret); |
110 | ||
3565e095 ZJS |
111 | /* Unescapes C style. Returns the unescaped character in ret. |
112 | * Sets *eight_bit to true if the escaped sequence either fits in | |
113 | * one byte in UTF-8 or is a non-unicode literal byte and should | |
114 | * instead be copied directly. | |
115 | */ | |
4f5dd394 LP |
116 | |
117 | if (length != (size_t) -1 && length < 1) | |
118 | return -EINVAL; | |
119 | ||
120 | switch (p[0]) { | |
121 | ||
122 | case 'a': | |
123 | *ret = '\a'; | |
124 | break; | |
125 | case 'b': | |
126 | *ret = '\b'; | |
127 | break; | |
128 | case 'f': | |
129 | *ret = '\f'; | |
130 | break; | |
131 | case 'n': | |
132 | *ret = '\n'; | |
133 | break; | |
134 | case 'r': | |
135 | *ret = '\r'; | |
136 | break; | |
137 | case 't': | |
138 | *ret = '\t'; | |
139 | break; | |
140 | case 'v': | |
141 | *ret = '\v'; | |
142 | break; | |
143 | case '\\': | |
144 | *ret = '\\'; | |
145 | break; | |
146 | case '"': | |
147 | *ret = '"'; | |
148 | break; | |
149 | case '\'': | |
150 | *ret = '\''; | |
151 | break; | |
152 | ||
153 | case 's': | |
154 | /* This is an extension of the XDG syntax files */ | |
155 | *ret = ' '; | |
156 | break; | |
157 | ||
158 | case 'x': { | |
159 | /* hexadecimal encoding */ | |
160 | int a, b; | |
161 | ||
162 | if (length != (size_t) -1 && length < 3) | |
163 | return -EINVAL; | |
164 | ||
165 | a = unhexchar(p[1]); | |
166 | if (a < 0) | |
167 | return -EINVAL; | |
168 | ||
169 | b = unhexchar(p[2]); | |
170 | if (b < 0) | |
171 | return -EINVAL; | |
172 | ||
173 | /* Don't allow NUL bytes */ | |
174 | if (a == 0 && b == 0) | |
175 | return -EINVAL; | |
176 | ||
3565e095 ZJS |
177 | *ret = (a << 4U) | b; |
178 | *eight_bit = true; | |
4f5dd394 LP |
179 | r = 3; |
180 | break; | |
181 | } | |
182 | ||
183 | case 'u': { | |
184 | /* C++11 style 16bit unicode */ | |
185 | ||
186 | int a[4]; | |
da6053d0 | 187 | size_t i; |
4f5dd394 LP |
188 | uint32_t c; |
189 | ||
190 | if (length != (size_t) -1 && length < 5) | |
191 | return -EINVAL; | |
192 | ||
193 | for (i = 0; i < 4; i++) { | |
194 | a[i] = unhexchar(p[1 + i]); | |
195 | if (a[i] < 0) | |
196 | return a[i]; | |
197 | } | |
198 | ||
199 | c = ((uint32_t) a[0] << 12U) | ((uint32_t) a[1] << 8U) | ((uint32_t) a[2] << 4U) | (uint32_t) a[3]; | |
200 | ||
201 | /* Don't allow 0 chars */ | |
202 | if (c == 0) | |
203 | return -EINVAL; | |
204 | ||
3565e095 | 205 | *ret = c; |
4f5dd394 LP |
206 | r = 5; |
207 | break; | |
208 | } | |
209 | ||
210 | case 'U': { | |
211 | /* C++11 style 32bit unicode */ | |
212 | ||
213 | int a[8]; | |
da6053d0 | 214 | size_t i; |
c932fb71 | 215 | char32_t c; |
4f5dd394 LP |
216 | |
217 | if (length != (size_t) -1 && length < 9) | |
218 | return -EINVAL; | |
219 | ||
220 | for (i = 0; i < 8; i++) { | |
221 | a[i] = unhexchar(p[1 + i]); | |
222 | if (a[i] < 0) | |
223 | return a[i]; | |
224 | } | |
225 | ||
dcd12626 LP |
226 | c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) | |
227 | ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7]; | |
4f5dd394 LP |
228 | |
229 | /* Don't allow 0 chars */ | |
230 | if (c == 0) | |
231 | return -EINVAL; | |
232 | ||
233 | /* Don't allow invalid code points */ | |
234 | if (!unichar_is_valid(c)) | |
235 | return -EINVAL; | |
236 | ||
3565e095 | 237 | *ret = c; |
4f5dd394 LP |
238 | r = 9; |
239 | break; | |
240 | } | |
241 | ||
242 | case '0': | |
243 | case '1': | |
244 | case '2': | |
245 | case '3': | |
246 | case '4': | |
247 | case '5': | |
248 | case '6': | |
249 | case '7': { | |
250 | /* octal encoding */ | |
251 | int a, b, c; | |
c932fb71 | 252 | char32_t m; |
4f5dd394 LP |
253 | |
254 | if (length != (size_t) -1 && length < 3) | |
255 | return -EINVAL; | |
256 | ||
257 | a = unoctchar(p[0]); | |
258 | if (a < 0) | |
259 | return -EINVAL; | |
260 | ||
261 | b = unoctchar(p[1]); | |
262 | if (b < 0) | |
263 | return -EINVAL; | |
264 | ||
265 | c = unoctchar(p[2]); | |
266 | if (c < 0) | |
267 | return -EINVAL; | |
268 | ||
269 | /* don't allow NUL bytes */ | |
270 | if (a == 0 && b == 0 && c == 0) | |
271 | return -EINVAL; | |
272 | ||
273 | /* Don't allow bytes above 255 */ | |
dcd12626 | 274 | m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c; |
4f5dd394 LP |
275 | if (m > 255) |
276 | return -EINVAL; | |
277 | ||
278 | *ret = m; | |
3565e095 | 279 | *eight_bit = true; |
4f5dd394 LP |
280 | r = 3; |
281 | break; | |
282 | } | |
283 | ||
284 | default: | |
285 | return -EINVAL; | |
286 | } | |
287 | ||
288 | return r; | |
289 | } | |
290 | ||
291 | int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) { | |
292 | char *r, *t; | |
293 | const char *f; | |
294 | size_t pl; | |
295 | ||
296 | assert(s); | |
297 | assert(ret); | |
298 | ||
299 | /* Undoes C style string escaping, and optionally prefixes it. */ | |
300 | ||
7bf7ce28 | 301 | pl = strlen_ptr(prefix); |
4f5dd394 LP |
302 | |
303 | r = new(char, pl+length+1); | |
304 | if (!r) | |
305 | return -ENOMEM; | |
306 | ||
307 | if (prefix) | |
308 | memcpy(r, prefix, pl); | |
309 | ||
310 | for (f = s, t = r + pl; f < s + length; f++) { | |
311 | size_t remaining; | |
3565e095 | 312 | bool eight_bit = false; |
c932fb71 | 313 | char32_t u; |
4f5dd394 LP |
314 | int k; |
315 | ||
316 | remaining = s + length - f; | |
317 | assert(remaining > 0); | |
318 | ||
319 | if (*f != '\\') { | |
629ff674 | 320 | /* A literal, copy verbatim */ |
4f5dd394 LP |
321 | *(t++) = *f; |
322 | continue; | |
323 | } | |
324 | ||
325 | if (remaining == 1) { | |
326 | if (flags & UNESCAPE_RELAX) { | |
327 | /* A trailing backslash, copy verbatim */ | |
328 | *(t++) = *f; | |
329 | continue; | |
330 | } | |
331 | ||
332 | free(r); | |
333 | return -EINVAL; | |
334 | } | |
335 | ||
3565e095 | 336 | k = cunescape_one(f + 1, remaining - 1, &u, &eight_bit); |
4f5dd394 LP |
337 | if (k < 0) { |
338 | if (flags & UNESCAPE_RELAX) { | |
339 | /* Invalid escape code, let's take it literal then */ | |
340 | *(t++) = '\\'; | |
341 | continue; | |
342 | } | |
343 | ||
344 | free(r); | |
345 | return k; | |
346 | } | |
347 | ||
3565e095 ZJS |
348 | f += k; |
349 | if (eight_bit) | |
350 | /* One byte? Set directly as specified */ | |
351 | *(t++) = u; | |
4f5dd394 | 352 | else |
3565e095 | 353 | /* Otherwise encode as multi-byte UTF-8 */ |
4f5dd394 | 354 | t += utf8_encode_unichar(t, u); |
4f5dd394 LP |
355 | } |
356 | ||
357 | *t = 0; | |
358 | ||
359 | *ret = r; | |
360 | return t - r; | |
361 | } | |
362 | ||
363 | int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) { | |
364 | return cunescape_length_with_prefix(s, length, NULL, flags, ret); | |
365 | } | |
366 | ||
367 | int cunescape(const char *s, UnescapeFlags flags, char **ret) { | |
368 | return cunescape_length(s, strlen(s), flags, ret); | |
369 | } | |
370 | ||
70d55819 ZJS |
371 | char *xescape_full(const char *s, const char *bad, size_t console_width, bool eight_bits) { |
372 | char *ans, *t, *prev, *prev2; | |
4f5dd394 LP |
373 | const char *f; |
374 | ||
70d55819 ZJS |
375 | /* Escapes all chars in bad, in addition to \ and all special chars, in \xFF style escaping. May be |
376 | * reversed with cunescape(). If eight_bits is true, characters >= 127 are let through unchanged. | |
377 | * This corresponds to non-ASCII printable characters in pre-unicode encodings. | |
378 | * | |
379 | * If console_width is reached, output is truncated and "..." is appended. */ | |
4f5dd394 | 380 | |
70d55819 ZJS |
381 | if (console_width == 0) |
382 | return strdup(""); | |
383 | ||
384 | ans = new(char, MIN(strlen(s), console_width) * 4 + 1); | |
385 | if (!ans) | |
4f5dd394 LP |
386 | return NULL; |
387 | ||
70d55819 ZJS |
388 | memset(ans, '_', MIN(strlen(s), console_width) * 4); |
389 | ans[MIN(strlen(s), console_width) * 4] = 0; | |
390 | ||
391 | for (f = s, t = prev = prev2 = ans; ; f++) { | |
392 | char *tmp_t = t; | |
393 | ||
394 | if (!*f) { | |
395 | *t = 0; | |
396 | return ans; | |
397 | } | |
398 | ||
399 | if ((unsigned char) *f < ' ' || (!eight_bits && (unsigned char) *f >= 127) || | |
400 | *f == '\\' || strchr(bad, *f)) { | |
401 | if ((size_t) (t - ans) + 4 > console_width) | |
402 | break; | |
4f5dd394 | 403 | |
4f5dd394 LP |
404 | *(t++) = '\\'; |
405 | *(t++) = 'x'; | |
406 | *(t++) = hexchar(*f >> 4); | |
407 | *(t++) = hexchar(*f); | |
70d55819 ZJS |
408 | } else { |
409 | if ((size_t) (t - ans) + 1 > console_width) | |
410 | break; | |
411 | ||
4f5dd394 | 412 | *(t++) = *f; |
70d55819 | 413 | } |
4f5dd394 | 414 | |
70d55819 ZJS |
415 | /* We might need to go back two cycles to fit three dots, so remember two positions */ |
416 | prev2 = prev; | |
417 | prev = tmp_t; | |
418 | } | |
4f5dd394 | 419 | |
70d55819 ZJS |
420 | /* We can just write where we want, since chars are one-byte */ |
421 | size_t c = MIN(console_width, 3u); /* If the console is too narrow, write fewer dots */ | |
422 | size_t off; | |
423 | if (console_width - c >= (size_t) (t - ans)) | |
424 | off = (size_t) (t - ans); | |
425 | else if (console_width - c >= (size_t) (prev - ans)) | |
426 | off = (size_t) (prev - ans); | |
427 | else if (console_width - c >= (size_t) (prev2 - ans)) | |
428 | off = (size_t) (prev2 - ans); | |
429 | else | |
430 | off = console_width - c; | |
431 | assert(off <= (size_t) (t - ans)); | |
432 | ||
433 | memcpy(ans + off, "...", c); | |
434 | ans[off + c] = '\0'; | |
435 | return ans; | |
4f5dd394 LP |
436 | } |
437 | ||
e3b4efd2 ZJS |
438 | char *escape_non_printable_full(const char *str, size_t console_width, bool eight_bit) { |
439 | if (eight_bit) | |
440 | return xescape_full(str, "", console_width, true); | |
441 | else | |
442 | return utf8_escape_non_printable_full(str, console_width); | |
443 | } | |
444 | ||
95052df3 ZJS |
445 | char *octescape(const char *s, size_t len) { |
446 | char *r, *t; | |
447 | const char *f; | |
448 | ||
449 | /* Escapes all chars in bad, in addition to \ and " chars, | |
450 | * in \nnn style escaping. */ | |
451 | ||
452 | r = new(char, len * 4 + 1); | |
453 | if (!r) | |
454 | return NULL; | |
455 | ||
456 | for (f = s, t = r; f < s + len; f++) { | |
457 | ||
4c701096 | 458 | if (*f < ' ' || *f >= 127 || IN_SET(*f, '\\', '"')) { |
95052df3 ZJS |
459 | *(t++) = '\\'; |
460 | *(t++) = '0' + (*f >> 6); | |
461 | *(t++) = '0' + ((*f >> 3) & 8); | |
462 | *(t++) = '0' + (*f & 8); | |
463 | } else | |
464 | *(t++) = *f; | |
465 | } | |
466 | ||
467 | *t = 0; | |
468 | ||
469 | return r; | |
470 | ||
471 | } | |
472 | ||
804ee07c | 473 | static char *strcpy_backslash_escaped(char *t, const char *s, const char *bad, bool escape_tab_nl) { |
4f5dd394 LP |
474 | assert(bad); |
475 | ||
476 | for (; *s; s++) { | |
804ee07c ZJS |
477 | if (escape_tab_nl && IN_SET(*s, '\n', '\t')) { |
478 | *(t++) = '\\'; | |
479 | *(t++) = *s == '\n' ? 'n' : 't'; | |
480 | continue; | |
481 | } | |
482 | ||
4f5dd394 LP |
483 | if (*s == '\\' || strchr(bad, *s)) |
484 | *(t++) = '\\'; | |
485 | ||
486 | *(t++) = *s; | |
487 | } | |
488 | ||
489 | return t; | |
490 | } | |
491 | ||
492 | char *shell_escape(const char *s, const char *bad) { | |
493 | char *r, *t; | |
494 | ||
495 | r = new(char, strlen(s)*2+1); | |
496 | if (!r) | |
497 | return NULL; | |
498 | ||
804ee07c | 499 | t = strcpy_backslash_escaped(r, s, bad, false); |
4f5dd394 LP |
500 | *t = 0; |
501 | ||
502 | return r; | |
503 | } | |
504 | ||
804ee07c | 505 | char* shell_maybe_quote(const char *s, EscapeStyle style) { |
4f5dd394 LP |
506 | const char *p; |
507 | char *r, *t; | |
508 | ||
509 | assert(s); | |
510 | ||
804ee07c ZJS |
511 | /* Encloses a string in quotes if necessary to make it OK as a shell |
512 | * string. Note that we treat benign UTF-8 characters as needing | |
513 | * escaping too, but that should be OK. */ | |
4f5dd394 LP |
514 | |
515 | for (p = s; *p; p++) | |
516 | if (*p <= ' ' || | |
517 | *p >= 127 || | |
518 | strchr(SHELL_NEED_QUOTES, *p)) | |
519 | break; | |
520 | ||
521 | if (!*p) | |
522 | return strdup(s); | |
523 | ||
804ee07c | 524 | r = new(char, (style == ESCAPE_POSIX) + 1 + strlen(s)*2 + 1 + 1); |
4f5dd394 LP |
525 | if (!r) |
526 | return NULL; | |
527 | ||
528 | t = r; | |
804ee07c ZJS |
529 | if (style == ESCAPE_BACKSLASH) |
530 | *(t++) = '"'; | |
531 | else if (style == ESCAPE_POSIX) { | |
532 | *(t++) = '$'; | |
533 | *(t++) = '\''; | |
534 | } else | |
535 | assert_not_reached("Bad EscapeStyle"); | |
536 | ||
4f5dd394 LP |
537 | t = mempcpy(t, s, p - s); |
538 | ||
804ee07c ZJS |
539 | if (style == ESCAPE_BACKSLASH) |
540 | t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE, false); | |
541 | else | |
542 | t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE_POSIX, true); | |
4f5dd394 | 543 | |
804ee07c ZJS |
544 | if (style == ESCAPE_BACKSLASH) |
545 | *(t++) = '"'; | |
546 | else | |
547 | *(t++) = '\''; | |
4f5dd394 LP |
548 | *t = 0; |
549 | ||
550 | return r; | |
551 | } |