1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
24 #include "hexdecoct.h"
25 #include "string-util.h"
29 size_t cescape_char(char c, char *buf) {
76 /* For special chars we prefer octal over
77 * hexadecimal encoding, simply because glib's
78 * g_strescape() does the same */
79 if ((c < ' ') || (c >= 127)) {
81 *(buf++) = octchar((unsigned char) c >> 6);
82 *(buf++) = octchar((unsigned char) c >> 3);
83 *(buf++) = octchar((unsigned char) c);
92 char *cescape_length(const char *s, size_t n) {
98 /* Does C style string escaping. May be reversed with
101 r = new(char, n*4 + 1);
105 for (f = s, t = r; f < s + n; f++)
106 t += cescape_char(*f, t);
113 char *cescape(const char *s) {
116 return cescape_length(s, strlen(s));
119 int cunescape_one(const char *p, size_t length, char *ret, uint32_t *ret_unicode) {
126 /* Unescapes C style. Returns the unescaped character in ret,
127 * unless we encountered a \u sequence in which case the full
128 * unicode character is returned in ret_unicode, instead. */
130 if (length != (size_t) -1 && length < 1)
167 /* This is an extension of the XDG syntax files */
172 /* hexadecimal encoding */
175 if (length != (size_t) -1 && length < 3)
186 /* Don't allow NUL bytes */
187 if (a == 0 && b == 0)
190 *ret = (char) ((a << 4U) | b);
196 /* C++11 style 16bit unicode */
202 if (length != (size_t) -1 && length < 5)
205 for (i = 0; i < 4; i++) {
206 a[i] = unhexchar(p[1 + i]);
211 c = ((uint32_t) a[0] << 12U) | ((uint32_t) a[1] << 8U) | ((uint32_t) a[2] << 4U) | (uint32_t) a[3];
213 /* Don't allow 0 chars */
232 /* C++11 style 32bit unicode */
238 if (length != (size_t) -1 && length < 9)
241 for (i = 0; i < 8; i++) {
242 a[i] = unhexchar(p[1 + i]);
247 c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) |
248 ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7];
250 /* Don't allow 0 chars */
254 /* Don't allow invalid code points */
255 if (!unichar_is_valid(c))
284 if (length != (size_t) -1 && length < 3)
299 /* don't allow NUL bytes */
300 if (a == 0 && b == 0 && c == 0)
303 /* Don't allow bytes above 255 */
304 m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c;
320 int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
328 /* Undoes C style string escaping, and optionally prefixes it. */
330 pl = prefix ? strlen(prefix) : 0;
332 r = new(char, pl+length+1);
337 memcpy(r, prefix, pl);
339 for (f = s, t = r + pl; f < s + length; f++) {
345 remaining = s + length - f;
346 assert(remaining > 0);
349 /* A literal literal, copy verbatim */
354 if (remaining == 1) {
355 if (flags & UNESCAPE_RELAX) {
356 /* A trailing backslash, copy verbatim */
365 k = cunescape_one(f + 1, remaining - 1, &c, &u);
367 if (flags & UNESCAPE_RELAX) {
368 /* Invalid escape code, let's take it literal then */
378 /* Non-Unicode? Let's encode this directly */
381 /* Unicode? Then let's encode this in UTF-8 */
382 t += utf8_encode_unichar(t, u);
393 int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
394 return cunescape_length_with_prefix(s, length, NULL, flags, ret);
397 int cunescape(const char *s, UnescapeFlags flags, char **ret) {
398 return cunescape_length(s, strlen(s), flags, ret);
401 char *xescape(const char *s, const char *bad) {
405 /* Escapes all chars in bad, in addition to \ and all special
406 * chars, in \xFF style escaping. May be reversed with
409 r = new(char, strlen(s) * 4 + 1);
413 for (f = s, t = r; *f; f++) {
415 if ((*f < ' ') || (*f >= 127) ||
416 (*f == '\\') || strchr(bad, *f)) {
419 *(t++) = hexchar(*f >> 4);
420 *(t++) = hexchar(*f);
430 static char *strcpy_backslash_escaped(char *t, const char *s, const char *bad) {
434 if (*s == '\\' || strchr(bad, *s))
443 char *shell_escape(const char *s, const char *bad) {
446 r = new(char, strlen(s)*2+1);
450 t = strcpy_backslash_escaped(r, s, bad);
456 char *shell_maybe_quote(const char *s) {
462 /* Encloses a string in double quotes if necessary to make it
463 * OK as shell string. */
468 strchr(SHELL_NEED_QUOTES, *p))
474 r = new(char, 1+strlen(s)*2+1+1);
480 t = mempcpy(t, s, p - s);
482 t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE);