1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
13 int cescape_char(char c, char *buf) {
16 /* Needs space for 4 characters in the buffer */
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)) {
67 *(buf++) = octchar((unsigned char) c >> 6);
68 *(buf++) = octchar((unsigned char) c >> 3);
69 *(buf++) = octchar((unsigned char) c);
78 char *cescape_length(const char *s, size_t n) {
84 /* Does C style string escaping. May be reversed with
87 r = new(char, n*4 + 1);
91 for (f = s, t = r; f < s + n; f++)
92 t += cescape_char(*f, t);
99 char *cescape(const char *s) {
102 return cescape_length(s, strlen(s));
105 int cunescape_one(const char *p, size_t length, char32_t *ret, bool *eight_bit) {
112 /* Unescapes C style. Returns the unescaped character in ret.
113 * Sets *eight_bit to true if the escaped sequence either fits in
114 * one byte in UTF-8 or is a non-unicode literal byte and should
115 * instead be copied directly.
118 if (length != (size_t) -1 && length < 1)
155 /* This is an extension of the XDG syntax files */
160 /* hexadecimal encoding */
163 if (length != (size_t) -1 && length < 3)
174 /* Don't allow NUL bytes */
175 if (a == 0 && b == 0)
178 *ret = (a << 4U) | b;
185 /* C++11 style 16bit unicode */
191 if (length != (size_t) -1 && length < 5)
194 for (i = 0; i < 4; i++) {
195 a[i] = unhexchar(p[1 + i]);
200 c = ((uint32_t) a[0] << 12U) | ((uint32_t) a[1] << 8U) | ((uint32_t) a[2] << 4U) | (uint32_t) a[3];
202 /* Don't allow 0 chars */
212 /* C++11 style 32bit unicode */
218 if (length != (size_t) -1 && length < 9)
221 for (i = 0; i < 8; i++) {
222 a[i] = unhexchar(p[1 + i]);
227 c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) |
228 ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7];
230 /* Don't allow 0 chars */
234 /* Don't allow invalid code points */
235 if (!unichar_is_valid(c))
255 if (length != (size_t) -1 && length < 3)
270 /* don't allow NUL bytes */
271 if (a == 0 && b == 0 && c == 0)
274 /* Don't allow bytes above 255 */
275 m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c;
292 int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
300 /* Undoes C style string escaping, and optionally prefixes it. */
302 pl = strlen_ptr(prefix);
304 r = new(char, pl+length+1);
309 memcpy(r, prefix, pl);
311 for (f = s, t = r + pl; f < s + length; f++) {
313 bool eight_bit = false;
317 remaining = s + length - f;
318 assert(remaining > 0);
321 /* A literal, copy verbatim */
326 if (remaining == 1) {
327 if (flags & UNESCAPE_RELAX) {
328 /* A trailing backslash, copy verbatim */
337 k = cunescape_one(f + 1, remaining - 1, &u, &eight_bit);
339 if (flags & UNESCAPE_RELAX) {
340 /* Invalid escape code, let's take it literal then */
351 /* One byte? Set directly as specified */
354 /* Otherwise encode as multi-byte UTF-8 */
355 t += utf8_encode_unichar(t, u);
364 int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
365 return cunescape_length_with_prefix(s, length, NULL, flags, ret);
368 int cunescape(const char *s, UnescapeFlags flags, char **ret) {
369 return cunescape_length(s, strlen(s), flags, ret);
372 char *xescape(const char *s, const char *bad) {
376 /* Escapes all chars in bad, in addition to \ and all special
377 * chars, in \xFF style escaping. May be reversed with
380 r = new(char, strlen(s) * 4 + 1);
384 for (f = s, t = r; *f; f++) {
386 if ((*f < ' ') || (*f >= 127) ||
387 (*f == '\\') || strchr(bad, *f)) {
390 *(t++) = hexchar(*f >> 4);
391 *(t++) = hexchar(*f);
401 #if 0 /// UNNEEDED by elogind
402 char *octescape(const char *s, size_t len) {
406 /* Escapes all chars in bad, in addition to \ and " chars,
407 * in \nnn style escaping. */
409 r = new(char, len * 4 + 1);
413 for (f = s, t = r; f < s + len; f++) {
415 if (*f < ' ' || *f >= 127 || IN_SET(*f, '\\', '"')) {
417 *(t++) = '0' + (*f >> 6);
418 *(t++) = '0' + ((*f >> 3) & 8);
419 *(t++) = '0' + (*f & 8);
430 static char *strcpy_backslash_escaped(char *t, const char *s, const char *bad, bool escape_tab_nl) {
434 if (escape_tab_nl && IN_SET(*s, '\n', '\t')) {
436 *(t++) = *s == '\n' ? 'n' : 't';
440 if (*s == '\\' || strchr(bad, *s))
449 char *shell_escape(const char *s, const char *bad) {
452 r = new(char, strlen(s)*2+1);
456 t = strcpy_backslash_escaped(r, s, bad, false);
462 char* shell_maybe_quote(const char *s, EscapeStyle style) {
468 /* Encloses a string in quotes if necessary to make it OK as a shell
469 * string. Note that we treat benign UTF-8 characters as needing
470 * escaping too, but that should be OK. */
475 strchr(SHELL_NEED_QUOTES, *p))
481 r = new(char, (style == ESCAPE_POSIX) + 1 + strlen(s)*2 + 1 + 1);
486 if (style == ESCAPE_BACKSLASH)
488 else if (style == ESCAPE_POSIX) {
492 assert_not_reached("Bad EscapeStyle");
494 t = mempcpy(t, s, p - s);
496 if (style == ESCAPE_BACKSLASH)
497 t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE, false);
499 t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE_POSIX, true);
501 if (style == ESCAPE_BACKSLASH)