]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/basic/escape.c
man: expand documentation for systemd-sysctl --verify
[thirdparty/systemd.git] / src / basic / escape.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include <stdlib.h>
4#include <string.h>
5
6#include "alloc-util.h"
7#include "escape.h"
8#include "hexdecoct.h"
9#include "string-util.h"
10#include "strv.h"
11#include "utf8.h"
12
13int cescape_char(char c, char *buf) {
14 char *buf_old = buf;
15
16 /* Needs space for 4 characters in the buffer */
17
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
78char* cescape_length(const char *s, size_t n) {
79 const char *f;
80 char *r, *t;
81
82 /* Does C style string escaping. May be reversed with cunescape(). */
83
84 assert(s || n == 0);
85
86 if (n == SIZE_MAX)
87 n = strlen(s);
88
89 if (n > (SIZE_MAX - 1) / 4)
90 return NULL;
91
92 r = new(char, n*4 + 1);
93 if (!r)
94 return NULL;
95
96 for (f = s, t = r; f < s + n; f++)
97 t += cescape_char(*f, t);
98
99 *t = 0;
100
101 return r;
102}
103
104int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit, bool accept_nul) {
105 int r = 1;
106
107 assert(p);
108 assert(ret);
109 assert(eight_bit);
110
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 */
116
117 if (length != SIZE_MAX && 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_MAX && 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 && !accept_nul)
175 return -EINVAL;
176
177 *ret = (a << 4U) | b;
178 *eight_bit = true;
179 r = 3;
180 break;
181 }
182
183 case 'u': {
184 /* C++11 style 16-bit unicode */
185
186 int a[4];
187 size_t i;
188 uint32_t c;
189
190 if (length != SIZE_MAX && 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 && !accept_nul)
203 return -EINVAL;
204
205 /* Don't allow UTF-16 surrogates, they cannot be encoded as valid UTF-8. Note we
206 * deliberately do *not* use unichar_is_valid() here (unlike the \U case below):
207 * it also rejects noncharacters such as U+FFFE, which callers legitimately round-trip
208 * through \u (e.g. systemd.mount-extra= parsing, see test-fstab-generator). */
209 if (utf16_is_surrogate(c))
210 return -EINVAL;
211
212 *ret = c;
213 r = 5;
214 break;
215 }
216
217 case 'U': {
218 /* C++11 style 32-bit unicode */
219
220 int a[8];
221 size_t i;
222 char32_t c;
223
224 if (length != SIZE_MAX && length < 9)
225 return -EINVAL;
226
227 for (i = 0; i < 8; i++) {
228 a[i] = unhexchar(p[1 + i]);
229 if (a[i] < 0)
230 return a[i];
231 }
232
233 c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) |
234 ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7];
235
236 /* Don't allow 0 chars */
237 if (c == 0 && !accept_nul)
238 return -EINVAL;
239
240 /* Don't allow invalid code points */
241 if (!unichar_is_valid(c))
242 return -EINVAL;
243
244 *ret = c;
245 r = 9;
246 break;
247 }
248
249 case '0':
250 case '1':
251 case '2':
252 case '3':
253 case '4':
254 case '5':
255 case '6':
256 case '7': {
257 /* octal encoding */
258 int a, b, c;
259 char32_t m;
260
261 if (length != SIZE_MAX && length < 3)
262 return -EINVAL;
263
264 a = unoctchar(p[0]);
265 if (a < 0)
266 return -EINVAL;
267
268 b = unoctchar(p[1]);
269 if (b < 0)
270 return -EINVAL;
271
272 c = unoctchar(p[2]);
273 if (c < 0)
274 return -EINVAL;
275
276 /* don't allow NUL bytes */
277 if (a == 0 && b == 0 && c == 0 && !accept_nul)
278 return -EINVAL;
279
280 /* Don't allow bytes above 255 */
281 m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c;
282 if (m > 255)
283 return -EINVAL;
284
285 *ret = m;
286 *eight_bit = true;
287 r = 3;
288 break;
289 }
290
291 default:
292 return -EINVAL;
293 }
294
295 return r;
296}
297
298ssize_t cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
299 _cleanup_free_ char *ans = NULL;
300 char *t;
301 const char *f;
302 size_t pl;
303 int r;
304
305 assert(s);
306 assert(ret);
307
308 /* Undoes C style string escaping, and optionally prefixes it. */
309
310 if (length == SIZE_MAX)
311 length = strlen(s);
312
313 pl = strlen_ptr(prefix);
314
315 ans = new(char, pl+length+1);
316 if (!ans)
317 return -ENOMEM;
318
319 if (prefix)
320 memcpy(ans, prefix, pl);
321
322 for (f = s, t = ans + pl; f < s + length; f++) {
323 size_t remaining;
324 bool eight_bit = false;
325 char32_t u;
326
327 remaining = s + length - f;
328 assert(remaining > 0);
329
330 if (*f != '\\') {
331 /* A literal, copy verbatim */
332 *(t++) = *f;
333 continue;
334 }
335
336 if (remaining == 1) {
337 if (flags & UNESCAPE_RELAX) {
338 /* A trailing backslash, copy verbatim */
339 *(t++) = *f;
340 continue;
341 }
342
343 return -EINVAL;
344 }
345
346 r = cunescape_one(f + 1, remaining - 1, &u, &eight_bit, flags & UNESCAPE_ACCEPT_NUL);
347 if (r < 0) {
348 if (flags & UNESCAPE_RELAX) {
349 /* Invalid escape code, let's take it literal then */
350 *(t++) = '\\';
351 continue;
352 }
353
354 return r;
355 }
356
357 f += r;
358 if (eight_bit)
359 /* One byte? Set directly as specified */
360 *(t++) = u;
361 else
362 /* Otherwise encode as multi-byte UTF-8 */
363 t += utf8_encode_unichar(t, u);
364 }
365
366 *t = 0;
367
368 assert(t >= ans); /* Let static analyzers know that the answer is non-negative. */
369 *ret = TAKE_PTR(ans);
370 return t - *ret;
371}
372
373char* xescape_full(const char *s, const char *bad, size_t console_width, XEscapeFlags flags) {
374 char *ans, *t, *prev, *prev2;
375 const char *f;
376
377 assert(s);
378
379 /* Escapes all chars in bad, in addition to \ and all special chars, in \xFF style escaping. May be
380 * reversed with cunescape(). If XESCAPE_8_BIT is specified, characters >= 127 are let through
381 * unchanged. This corresponds to non-ASCII printable characters in pre-unicode encodings.
382 *
383 * If console_width is reached, or XESCAPE_FORCE_ELLIPSIS is set, output is truncated and "..." is
384 * appended. */
385
386 if (console_width == 0)
387 return strdup("");
388
389 size_t len_forced_ellipsis = FLAGS_SET(flags, XESCAPE_FORCE_ELLIPSIS) ? STRLEN("...") : 0;
390 size_t len_s = strlen(s);
391
392 size_t len_body = MIN(console_width, SIZE_MAX - 1); /* We need room for the NUL byte */
393 if (len_body > len_forced_ellipsis && len_s <= (len_body - len_forced_ellipsis) / 4)
394 len_body = len_s * 4 + len_forced_ellipsis;
395
396 ans = new(char, len_body + 1);
397 if (!ans)
398 return NULL;
399
400 for (f = s, t = prev = prev2 = ans; ; f++) {
401 char *tmp_t = t;
402
403 if (!*f) {
404 if (len_forced_ellipsis != 0)
405 break;
406
407 *t = 0;
408 return ans;
409 }
410
411 if ((unsigned char) *f < ' ' ||
412 (!FLAGS_SET(flags, XESCAPE_8_BIT) && (unsigned char) *f >= 127) ||
413 *f == '\\' || (bad && strchr(bad, *f))) {
414 if ((size_t) (t - ans) + 4 + len_forced_ellipsis > len_body)
415 break;
416
417 *(t++) = '\\';
418 *(t++) = 'x';
419 *(t++) = hexchar(*f >> 4);
420 *(t++) = hexchar(*f);
421 } else {
422 if ((size_t) (t - ans) + 1 + len_forced_ellipsis > len_body)
423 break;
424
425 *(t++) = *f;
426 }
427
428 /* We might need to go back two cycles to fit three dots, so remember two positions */
429 prev2 = prev;
430 prev = tmp_t;
431 }
432
433 /* We can just write where we want, since chars are one-byte */
434 size_t c = MIN(len_body, STRLEN("...")); /* If the console is too narrow, write fewer dots */
435 size_t off;
436 if (len_body - c >= (size_t) (t - ans))
437 off = (size_t) (t - ans);
438 else if (len_body - c >= (size_t) (prev - ans))
439 off = (size_t) (prev - ans);
440 else if (len_body - c >= (size_t) (prev2 - ans))
441 off = (size_t) (prev2 - ans);
442 else
443 off = len_body - c;
444 assert(off <= (size_t) (t - ans));
445
446 memcpy(ans + off, "...", c);
447 ans[off + c] = '\0';
448 return ans;
449}
450
451char* escape_non_printable_full(const char *str, size_t console_width, XEscapeFlags flags) {
452 if (FLAGS_SET(flags, XESCAPE_8_BIT))
453 return xescape_full(str, /* bad= */ NULL, console_width, flags);
454 else
455 return utf8_escape_non_printable_full(str,
456 console_width,
457 FLAGS_SET(flags, XESCAPE_FORCE_ELLIPSIS));
458}
459
460char* octescape_full(const char *s, size_t len, const char *bad) {
461 char *buf, *t;
462
463 /* Escapes all chars in bad, in addition to \ and " chars, in \nnn octal style escaping. May be
464 * reversed with cunescape(). */
465
466 assert(s || len == 0);
467
468 if (len == SIZE_MAX)
469 len = strlen(s);
470
471 if (len > (SIZE_MAX - 1) / 4)
472 return NULL;
473
474 t = buf = new(char, len * 4 + 1);
475 if (!buf)
476 return NULL;
477
478 for (size_t i = 0; i < len; i++) {
479 uint8_t u = (uint8_t) s[i];
480
481 if (u < ' ' || u >= 127 || IN_SET(u, '\\', '"') || (bad && strchr(bad, u))) {
482 *(t++) = '\\';
483 *(t++) = '0' + (u >> 6);
484 *(t++) = '0' + ((u >> 3) & 7);
485 *(t++) = '0' + (u & 7);
486 } else
487 *(t++) = u;
488 }
489
490 *t = 0;
491 return buf;
492}
493
494char* decescape(const char *s, size_t len, const char *bad) {
495 char *buf, *t;
496
497 /* Escapes all chars in bad, in addition to \ and " chars, in \nnn decimal style escaping. */
498
499 assert(s || len == 0);
500
501 if (len == SIZE_MAX)
502 len = strlen(s);
503
504 if (len > (SIZE_MAX - 1) / 4)
505 return NULL;
506
507 t = buf = new(char, len * 4 + 1);
508 if (!buf)
509 return NULL;
510
511 for (size_t i = 0; i < len; i++) {
512 uint8_t u = (uint8_t) s[i];
513
514 if (u < ' ' || u >= 127 || IN_SET(u, '\\', '"') || strchr(bad, u)) {
515 *(t++) = '\\';
516 *(t++) = '0' + (u / 100);
517 *(t++) = '0' + ((u / 10) % 10);
518 *(t++) = '0' + (u % 10);
519 } else
520 *(t++) = u;
521 }
522
523 *t = 0;
524 return buf;
525}
526
527static char* strcpy_backslash_escaped(char *t, const char *s, const char *bad) {
528 assert(bad);
529 assert(t);
530 assert(s);
531
532 while (*s) {
533 int l = utf8_encoded_valid_unichar(s, SIZE_MAX);
534
535 if (char_is_cc(*s) || l < 0)
536 t += cescape_char(*(s++), t);
537 else if (l == 1) {
538 if (*s == '\\' || strchr(bad, *s))
539 *(t++) = '\\';
540 *(t++) = *(s++);
541 } else {
542 t = mempcpy(t, s, l);
543 s += l;
544 }
545 }
546
547 return t;
548}
549
550char* shell_escape(const char *s, const char *bad) {
551 char *buf, *t;
552
553 buf = new(char, strlen(s)*4+1);
554 if (!buf)
555 return NULL;
556
557 t = strcpy_backslash_escaped(buf, s, bad);
558 *t = 0;
559
560 return buf;
561}
562
563char* shell_maybe_quote(const char *s, ShellEscapeFlags flags) {
564 const char *p;
565 char *buf, *t;
566
567 assert(s);
568
569 /* Encloses a string in quotes if necessary to make it OK as a shell string. */
570
571 if (FLAGS_SET(flags, SHELL_ESCAPE_EMPTY) && isempty(s))
572 return strdup("\"\""); /* We don't use $'' here in the POSIX mode. "" is fine too. */
573
574 for (p = s; *p; ) {
575 int l = utf8_encoded_valid_unichar(p, SIZE_MAX);
576
577 if (char_is_cc(*p) || l < 0 ||
578 strchr(WHITESPACE SHELL_NEED_QUOTES, *p))
579 break;
580
581 p += l;
582 }
583
584 if (!*p)
585 return strdup(s);
586
587 buf = new(char, FLAGS_SET(flags, SHELL_ESCAPE_POSIX) + 1 + strlen(s)*4 + 1 + 1);
588 if (!buf)
589 return NULL;
590
591 t = buf;
592 if (FLAGS_SET(flags, SHELL_ESCAPE_POSIX)) {
593 *(t++) = '$';
594 *(t++) = '\'';
595 } else
596 *(t++) = '"';
597
598 t = mempcpy(t, s, p - s);
599
600 t = strcpy_backslash_escaped(t, p,
601 FLAGS_SET(flags, SHELL_ESCAPE_POSIX) ? SHELL_NEED_ESCAPE_POSIX : SHELL_NEED_ESCAPE);
602
603 if (FLAGS_SET(flags, SHELL_ESCAPE_POSIX))
604 *(t++) = '\'';
605 else
606 *(t++) = '"';
607 *t = 0;
608
609 return str_realloc(buf);
610}
611
612char* quote_command_line(char * const *argv, ShellEscapeFlags flags) {
613 _cleanup_free_ char *result = NULL;
614
615 assert(argv);
616
617 STRV_FOREACH(a, argv) {
618 _cleanup_free_ char *t = NULL;
619
620 t = shell_maybe_quote(*a, flags);
621 if (!t)
622 return NULL;
623
624 if (!strextend_with_separator(&result, " ", t))
625 return NULL;
626 }
627
628 return str_realloc(TAKE_PTR(result));
629}