1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
10 #include "alloc-util.h"
12 #include "hexdecoct.h"
16 int cescape_char(char c, char *buf) {
19 /* Needs space for 4 characters in the buffer */
65 /* For special chars we prefer octal over
66 * hexadecimal encoding, simply because glib's
67 * g_strescape() does the same */
68 if ((c < ' ') || (c >= 127)) {
70 *(buf++) = octchar((unsigned char) c >> 6);
71 *(buf++) = octchar((unsigned char) c >> 3);
72 *(buf++) = octchar((unsigned char) c);
81 char *cescape_length(const char *s, size_t n) {
87 /* Does C style string escaping. May be reversed with
90 r = new(char, n*4 + 1);
94 for (f = s, t = r; f < s + n; f++)
95 t += cescape_char(*f, t);
102 char *cescape(const char *s) {
105 return cescape_length(s, strlen(s));
108 int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit) {
115 /* Unescapes C style. Returns the unescaped character in ret.
116 * Sets *eight_bit to true if the escaped sequence either fits in
117 * one byte in UTF-8 or is a non-unicode literal byte and should
118 * instead be copied directly.
121 if (length != (size_t) -1 && length < 1)
158 /* This is an extension of the XDG syntax files */
163 /* hexadecimal encoding */
166 if (length != (size_t) -1 && length < 3)
177 /* Don't allow NUL bytes */
178 if (a == 0 && b == 0)
181 *ret = (a << 4U) | b;
188 /* C++11 style 16bit unicode */
194 if (length != (size_t) -1 && length < 5)
197 for (i = 0; i < 4; i++) {
198 a[i] = unhexchar(p[1 + i]);
203 c = ((uint32_t) a[0] << 12U) | ((uint32_t) a[1] << 8U) | ((uint32_t) a[2] << 4U) | (uint32_t) a[3];
205 /* Don't allow 0 chars */
215 /* C++11 style 32bit unicode */
221 if (length != (size_t) -1 && length < 9)
224 for (i = 0; i < 8; i++) {
225 a[i] = unhexchar(p[1 + i]);
230 c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) |
231 ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7];
233 /* Don't allow 0 chars */
237 /* Don't allow invalid code points */
238 if (!unichar_is_valid(c))
258 if (length != (size_t) -1 && length < 3)
273 /* don't allow NUL bytes */
274 if (a == 0 && b == 0 && c == 0)
277 /* Don't allow bytes above 255 */
278 m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c;
295 int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
303 /* Undoes C style string escaping, and optionally prefixes it. */
305 pl = strlen_ptr(prefix);
307 r = new(char, pl+length+1);
312 memcpy(r, prefix, pl);
314 for (f = s, t = r + pl; f < s + length; f++) {
316 bool eight_bit = false;
320 remaining = s + length - f;
321 assert(remaining > 0);
324 /* A literal, copy verbatim */
329 if (remaining == 1) {
330 if (flags & UNESCAPE_RELAX) {
331 /* A trailing backslash, copy verbatim */
340 k = cunescape_one(f + 1, remaining - 1, &u, &eight_bit);
342 if (flags & UNESCAPE_RELAX) {
343 /* Invalid escape code, let's take it literal then */
354 /* One byte? Set directly as specified */
357 /* Otherwise encode as multi-byte UTF-8 */
358 t += utf8_encode_unichar(t, u);
367 int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
368 return cunescape_length_with_prefix(s, length, NULL, flags, ret);
371 int cunescape(const char *s, UnescapeFlags flags, char **ret) {
372 return cunescape_length(s, strlen(s), flags, ret);
375 char *xescape(const char *s, const char *bad) {
379 /* Escapes all chars in bad, in addition to \ and all special
380 * chars, in \xFF style escaping. May be reversed with
383 r = new(char, strlen(s) * 4 + 1);
387 for (f = s, t = r; *f; f++) {
389 if ((*f < ' ') || (*f >= 127) ||
390 (*f == '\\') || strchr(bad, *f)) {
393 *(t++) = hexchar(*f >> 4);
394 *(t++) = hexchar(*f);
404 #if 0 /// UNNEEDED by elogind
405 char *octescape(const char *s, size_t len) {
409 /* Escapes all chars in bad, in addition to \ and " chars,
410 * in \nnn style escaping. */
412 r = new(char, len * 4 + 1);
416 for (f = s, t = r; f < s + len; f++) {
418 if (*f < ' ' || *f >= 127 || IN_SET(*f, '\\', '"')) {
420 *(t++) = '0' + (*f >> 6);
421 *(t++) = '0' + ((*f >> 3) & 8);
422 *(t++) = '0' + (*f & 8);
433 static char *strcpy_backslash_escaped(char *t, const char *s, const char *bad, bool escape_tab_nl) {
437 if (escape_tab_nl && IN_SET(*s, '\n', '\t')) {
439 *(t++) = *s == '\n' ? 'n' : 't';
443 if (*s == '\\' || strchr(bad, *s))
452 char *shell_escape(const char *s, const char *bad) {
455 r = new(char, strlen(s)*2+1);
459 t = strcpy_backslash_escaped(r, s, bad, false);
465 char* shell_maybe_quote(const char *s, EscapeStyle style) {
471 /* Encloses a string in quotes if necessary to make it OK as a shell
472 * string. Note that we treat benign UTF-8 characters as needing
473 * escaping too, but that should be OK. */
478 strchr(SHELL_NEED_QUOTES, *p))
484 r = new(char, (style == ESCAPE_POSIX) + 1 + strlen(s)*2 + 1 + 1);
489 if (style == ESCAPE_BACKSLASH)
491 else if (style == ESCAPE_POSIX) {
495 assert_not_reached("Bad EscapeStyle");
497 t = mempcpy(t, s, p - s);
499 if (style == ESCAPE_BACKSLASH)
500 t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE, false);
502 t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE_POSIX, true);
504 if (style == ESCAPE_BACKSLASH)