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, uint32_t *ret, bool *eight_bit) {
126 /* Unescapes C style. Returns the unescaped character in ret.
127 * Sets *eight_bit to true if the escaped sequence either fits in
128 * one byte in UTF-8 or is a non-unicode literal byte and should
129 * instead be copied directly.
132 if (length != (size_t) -1 && length < 1)
169 /* This is an extension of the XDG syntax files */
174 /* hexadecimal encoding */
177 if (length != (size_t) -1 && length < 3)
188 /* Don't allow NUL bytes */
189 if (a == 0 && b == 0)
192 *ret = (a << 4U) | b;
199 /* C++11 style 16bit unicode */
205 if (length != (size_t) -1 && length < 5)
208 for (i = 0; i < 4; i++) {
209 a[i] = unhexchar(p[1 + i]);
214 c = ((uint32_t) a[0] << 12U) | ((uint32_t) a[1] << 8U) | ((uint32_t) a[2] << 4U) | (uint32_t) a[3];
216 /* Don't allow 0 chars */
226 /* C++11 style 32bit unicode */
232 if (length != (size_t) -1 && length < 9)
235 for (i = 0; i < 8; i++) {
236 a[i] = unhexchar(p[1 + i]);
241 c = ((uint32_t) a[0] << 28U) | ((uint32_t) a[1] << 24U) | ((uint32_t) a[2] << 20U) | ((uint32_t) a[3] << 16U) |
242 ((uint32_t) a[4] << 12U) | ((uint32_t) a[5] << 8U) | ((uint32_t) a[6] << 4U) | (uint32_t) a[7];
244 /* Don't allow 0 chars */
248 /* Don't allow invalid code points */
249 if (!unichar_is_valid(c))
269 if (length != (size_t) -1 && length < 3)
284 /* don't allow NUL bytes */
285 if (a == 0 && b == 0 && c == 0)
288 /* Don't allow bytes above 255 */
289 m = ((uint32_t) a << 6U) | ((uint32_t) b << 3U) | (uint32_t) c;
306 int cunescape_length_with_prefix(const char *s, size_t length, const char *prefix, UnescapeFlags flags, char **ret) {
314 /* Undoes C style string escaping, and optionally prefixes it. */
316 pl = prefix ? strlen(prefix) : 0;
318 r = new(char, pl+length+1);
323 memcpy(r, prefix, pl);
325 for (f = s, t = r + pl; f < s + length; f++) {
328 bool eight_bit = false;
331 remaining = s + length - f;
332 assert(remaining > 0);
335 /* A literal literal, copy verbatim */
340 if (remaining == 1) {
341 if (flags & UNESCAPE_RELAX) {
342 /* A trailing backslash, copy verbatim */
351 k = cunescape_one(f + 1, remaining - 1, &u, &eight_bit);
353 if (flags & UNESCAPE_RELAX) {
354 /* Invalid escape code, let's take it literal then */
365 /* One byte? Set directly as specified */
368 /* Otherwise encode as multi-byte UTF-8 */
369 t += utf8_encode_unichar(t, u);
378 int cunescape_length(const char *s, size_t length, UnescapeFlags flags, char **ret) {
379 return cunescape_length_with_prefix(s, length, NULL, flags, ret);
382 int cunescape(const char *s, UnescapeFlags flags, char **ret) {
383 return cunescape_length(s, strlen(s), flags, ret);
386 char *xescape(const char *s, const char *bad) {
390 /* Escapes all chars in bad, in addition to \ and all special
391 * chars, in \xFF style escaping. May be reversed with
394 r = new(char, strlen(s) * 4 + 1);
398 for (f = s, t = r; *f; f++) {
400 if ((*f < ' ') || (*f >= 127) ||
401 (*f == '\\') || strchr(bad, *f)) {
404 *(t++) = hexchar(*f >> 4);
405 *(t++) = hexchar(*f);
415 static char *strcpy_backslash_escaped(char *t, const char *s, const char *bad) {
419 if (*s == '\\' || strchr(bad, *s))
428 char *shell_escape(const char *s, const char *bad) {
431 r = new(char, strlen(s)*2+1);
435 t = strcpy_backslash_escaped(r, s, bad);
441 char *shell_maybe_quote(const char *s) {
447 /* Encloses a string in double quotes if necessary to make it
448 * OK as shell string. */
453 strchr(SHELL_NEED_QUOTES, *p))
459 r = new(char, 1+strlen(s)*2+1+1);
465 t = mempcpy(t, s, p - s);
467 t = strcpy_backslash_escaped(t, p, SHELL_NEED_ESCAPE);