]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/escape.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "alloc-util.h"
27 #include "hexdecoct.h"
31 size_t cescape_char(char c
, char *buf
) {
78 /* For special chars we prefer octal over
79 * hexadecimal encoding, simply because glib's
80 * g_strescape() does the same */
81 if ((c
< ' ') || (c
>= 127)) {
83 *(buf
++) = octchar((unsigned char) c
>> 6);
84 *(buf
++) = octchar((unsigned char) c
>> 3);
85 *(buf
++) = octchar((unsigned char) c
);
94 char *cescape_length(const char *s
, size_t n
) {
100 /* Does C style string escaping. May be reversed with
103 r
= new(char, n
*4 + 1);
107 for (f
= s
, t
= r
; f
< s
+ n
; f
++)
108 t
+= cescape_char(*f
, t
);
115 char *cescape(const char *s
) {
118 return cescape_length(s
, strlen(s
));
121 int cunescape_one(const char *p
, size_t length
, char32_t
*ret
, bool *eight_bit
) {
128 /* Unescapes C style. Returns the unescaped character in ret.
129 * Sets *eight_bit to true if the escaped sequence either fits in
130 * one byte in UTF-8 or is a non-unicode literal byte and should
131 * instead be copied directly.
134 if (length
!= (size_t) -1 && length
< 1)
171 /* This is an extension of the XDG syntax files */
176 /* hexadecimal encoding */
179 if (length
!= (size_t) -1 && length
< 3)
190 /* Don't allow NUL bytes */
191 if (a
== 0 && b
== 0)
194 *ret
= (a
<< 4U) | b
;
201 /* C++11 style 16bit unicode */
207 if (length
!= (size_t) -1 && length
< 5)
210 for (i
= 0; i
< 4; i
++) {
211 a
[i
] = unhexchar(p
[1 + i
]);
216 c
= ((uint32_t) a
[0] << 12U) | ((uint32_t) a
[1] << 8U) | ((uint32_t) a
[2] << 4U) | (uint32_t) a
[3];
218 /* Don't allow 0 chars */
228 /* C++11 style 32bit unicode */
234 if (length
!= (size_t) -1 && length
< 9)
237 for (i
= 0; i
< 8; i
++) {
238 a
[i
] = unhexchar(p
[1 + i
]);
243 c
= ((uint32_t) a
[0] << 28U) | ((uint32_t) a
[1] << 24U) | ((uint32_t) a
[2] << 20U) | ((uint32_t) a
[3] << 16U) |
244 ((uint32_t) a
[4] << 12U) | ((uint32_t) a
[5] << 8U) | ((uint32_t) a
[6] << 4U) | (uint32_t) a
[7];
246 /* Don't allow 0 chars */
250 /* Don't allow invalid code points */
251 if (!unichar_is_valid(c
))
271 if (length
!= (size_t) -1 && length
< 3)
286 /* don't allow NUL bytes */
287 if (a
== 0 && b
== 0 && c
== 0)
290 /* Don't allow bytes above 255 */
291 m
= ((uint32_t) a
<< 6U) | ((uint32_t) b
<< 3U) | (uint32_t) c
;
308 int cunescape_length_with_prefix(const char *s
, size_t length
, const char *prefix
, UnescapeFlags flags
, char **ret
) {
316 /* Undoes C style string escaping, and optionally prefixes it. */
318 pl
= strlen_ptr(prefix
);
320 r
= new(char, pl
+length
+1);
325 memcpy(r
, prefix
, pl
);
327 for (f
= s
, t
= r
+ pl
; f
< s
+ length
; f
++) {
329 bool eight_bit
= false;
333 remaining
= s
+ length
- f
;
334 assert(remaining
> 0);
337 /* A literal, copy verbatim */
342 if (remaining
== 1) {
343 if (flags
& UNESCAPE_RELAX
) {
344 /* A trailing backslash, copy verbatim */
353 k
= cunescape_one(f
+ 1, remaining
- 1, &u
, &eight_bit
);
355 if (flags
& UNESCAPE_RELAX
) {
356 /* Invalid escape code, let's take it literal then */
367 /* One byte? Set directly as specified */
370 /* Otherwise encode as multi-byte UTF-8 */
371 t
+= utf8_encode_unichar(t
, u
);
380 int cunescape_length(const char *s
, size_t length
, UnescapeFlags flags
, char **ret
) {
381 return cunescape_length_with_prefix(s
, length
, NULL
, flags
, ret
);
384 int cunescape(const char *s
, UnescapeFlags flags
, char **ret
) {
385 return cunescape_length(s
, strlen(s
), flags
, ret
);
388 char *xescape(const char *s
, const char *bad
) {
392 /* Escapes all chars in bad, in addition to \ and all special
393 * chars, in \xFF style escaping. May be reversed with
396 r
= new(char, strlen(s
) * 4 + 1);
400 for (f
= s
, t
= r
; *f
; f
++) {
402 if ((*f
< ' ') || (*f
>= 127) ||
403 (*f
== '\\') || strchr(bad
, *f
)) {
406 *(t
++) = hexchar(*f
>> 4);
407 *(t
++) = hexchar(*f
);
417 char *octescape(const char *s
, size_t len
) {
421 /* Escapes all chars in bad, in addition to \ and " chars,
422 * in \nnn style escaping. */
424 r
= new(char, len
* 4 + 1);
428 for (f
= s
, t
= r
; f
< s
+ len
; f
++) {
430 if (*f
< ' ' || *f
>= 127 || IN_SET(*f
, '\\', '"')) {
432 *(t
++) = '0' + (*f
>> 6);
433 *(t
++) = '0' + ((*f
>> 3) & 8);
434 *(t
++) = '0' + (*f
& 8);
445 static char *strcpy_backslash_escaped(char *t
, const char *s
, const char *bad
, bool escape_tab_nl
) {
449 if (escape_tab_nl
&& IN_SET(*s
, '\n', '\t')) {
451 *(t
++) = *s
== '\n' ? 'n' : 't';
455 if (*s
== '\\' || strchr(bad
, *s
))
464 char *shell_escape(const char *s
, const char *bad
) {
467 r
= new(char, strlen(s
)*2+1);
471 t
= strcpy_backslash_escaped(r
, s
, bad
, false);
477 char* shell_maybe_quote(const char *s
, EscapeStyle style
) {
483 /* Encloses a string in quotes if necessary to make it OK as a shell
484 * string. Note that we treat benign UTF-8 characters as needing
485 * escaping too, but that should be OK. */
490 strchr(SHELL_NEED_QUOTES
, *p
))
496 r
= new(char, (style
== ESCAPE_POSIX
) + 1 + strlen(s
)*2 + 1 + 1);
501 if (style
== ESCAPE_BACKSLASH
)
503 else if (style
== ESCAPE_POSIX
) {
507 assert_not_reached("Bad EscapeStyle");
509 t
= mempcpy(t
, s
, p
- s
);
511 if (style
== ESCAPE_BACKSLASH
)
512 t
= strcpy_backslash_escaped(t
, p
, SHELL_NEED_ESCAPE
, false);
514 t
= strcpy_backslash_escaped(t
, p
, SHELL_NEED_ESCAPE_POSIX
, true);
516 if (style
== ESCAPE_BACKSLASH
)