]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/escape.c
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/>.
26 #include "alloc-util.h"
28 #include "hexdecoct.h"
32 size_t cescape_char(char c
, char *buf
) {
79 /* For special chars we prefer octal over
80 * hexadecimal encoding, simply because glib's
81 * g_strescape() does the same */
82 if ((c
< ' ') || (c
>= 127)) {
84 *(buf
++) = octchar((unsigned char) c
>> 6);
85 *(buf
++) = octchar((unsigned char) c
>> 3);
86 *(buf
++) = octchar((unsigned char) c
);
95 char *cescape_length(const char *s
, size_t n
) {
101 /* Does C style string escaping. May be reversed with
104 r
= new(char, n
*4 + 1);
108 for (f
= s
, t
= r
; f
< s
+ n
; f
++)
109 t
+= cescape_char(*f
, t
);
116 char *cescape(const char *s
) {
119 return cescape_length(s
, strlen(s
));
122 int cunescape_one(const char *p
, size_t length
, uint32_t *ret
, bool *eight_bit
) {
129 /* Unescapes C style. Returns the unescaped character in ret.
130 * Sets *eight_bit to true if the escaped sequence either fits in
131 * one byte in UTF-8 or is a non-unicode literal byte and should
132 * instead be copied directly.
135 if (length
!= (size_t) -1 && length
< 1)
172 /* This is an extension of the XDG syntax files */
177 /* hexadecimal encoding */
180 if (length
!= (size_t) -1 && length
< 3)
191 /* Don't allow NUL bytes */
192 if (a
== 0 && b
== 0)
195 *ret
= (a
<< 4U) | b
;
202 /* C++11 style 16bit unicode */
208 if (length
!= (size_t) -1 && length
< 5)
211 for (i
= 0; i
< 4; i
++) {
212 a
[i
] = unhexchar(p
[1 + i
]);
217 c
= ((uint32_t) a
[0] << 12U) | ((uint32_t) a
[1] << 8U) | ((uint32_t) a
[2] << 4U) | (uint32_t) a
[3];
219 /* Don't allow 0 chars */
229 /* C++11 style 32bit unicode */
235 if (length
!= (size_t) -1 && length
< 9)
238 for (i
= 0; i
< 8; i
++) {
239 a
[i
] = unhexchar(p
[1 + i
]);
244 c
= ((uint32_t) a
[0] << 28U) | ((uint32_t) a
[1] << 24U) | ((uint32_t) a
[2] << 20U) | ((uint32_t) a
[3] << 16U) |
245 ((uint32_t) a
[4] << 12U) | ((uint32_t) a
[5] << 8U) | ((uint32_t) a
[6] << 4U) | (uint32_t) a
[7];
247 /* Don't allow 0 chars */
251 /* Don't allow invalid code points */
252 if (!unichar_is_valid(c
))
272 if (length
!= (size_t) -1 && length
< 3)
287 /* don't allow NUL bytes */
288 if (a
== 0 && b
== 0 && c
== 0)
291 /* Don't allow bytes above 255 */
292 m
= ((uint32_t) a
<< 6U) | ((uint32_t) b
<< 3U) | (uint32_t) c
;
309 int cunescape_length_with_prefix(const char *s
, size_t length
, const char *prefix
, UnescapeFlags flags
, char **ret
) {
317 /* Undoes C style string escaping, and optionally prefixes it. */
319 pl
= prefix
? strlen(prefix
) : 0;
321 r
= new(char, pl
+length
+1);
326 memcpy(r
, prefix
, pl
);
328 for (f
= s
, t
= r
+ pl
; f
< s
+ length
; f
++) {
331 bool eight_bit
= false;
334 remaining
= s
+ length
- f
;
335 assert(remaining
> 0);
338 /* A literal literal, copy verbatim */
343 if (remaining
== 1) {
344 if (flags
& UNESCAPE_RELAX
) {
345 /* A trailing backslash, copy verbatim */
354 k
= cunescape_one(f
+ 1, remaining
- 1, &u
, &eight_bit
);
356 if (flags
& UNESCAPE_RELAX
) {
357 /* Invalid escape code, let's take it literal then */
368 /* One byte? Set directly as specified */
371 /* Otherwise encode as multi-byte UTF-8 */
372 t
+= utf8_encode_unichar(t
, u
);
381 int cunescape_length(const char *s
, size_t length
, UnescapeFlags flags
, char **ret
) {
382 return cunescape_length_with_prefix(s
, length
, NULL
, flags
, ret
);
385 int cunescape(const char *s
, UnescapeFlags flags
, char **ret
) {
386 return cunescape_length(s
, strlen(s
), flags
, ret
);
389 char *xescape(const char *s
, const char *bad
) {
393 /* Escapes all chars in bad, in addition to \ and all special
394 * chars, in \xFF style escaping. May be reversed with
397 r
= new(char, strlen(s
) * 4 + 1);
401 for (f
= s
, t
= r
; *f
; f
++) {
403 if ((*f
< ' ') || (*f
>= 127) ||
404 (*f
== '\\') || strchr(bad
, *f
)) {
407 *(t
++) = hexchar(*f
>> 4);
408 *(t
++) = hexchar(*f
);
418 static char *strcpy_backslash_escaped(char *t
, const char *s
, const char *bad
) {
422 if (*s
== '\\' || strchr(bad
, *s
))
431 char *shell_escape(const char *s
, const char *bad
) {
434 r
= new(char, strlen(s
)*2+1);
438 t
= strcpy_backslash_escaped(r
, s
, bad
);
444 char *shell_maybe_quote(const char *s
) {
450 /* Encloses a string in double quotes if necessary to make it
451 * OK as shell string. */
456 strchr(SHELL_NEED_QUOTES
, *p
))
462 r
= new(char, 1+strlen(s
)*2+1+1);
468 t
= mempcpy(t
, s
, p
- s
);
470 t
= strcpy_backslash_escaped(t
, p
, SHELL_NEED_ESCAPE
);