]>
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/>.
22 #include "alloc-util.h"
24 #include "hexdecoct.h"
28 size_t cescape_char(char c
, char *buf
) {
75 /* For special chars we prefer octal over
76 * hexadecimal encoding, simply because glib's
77 * g_strescape() does the same */
78 if ((c
< ' ') || (c
>= 127)) {
80 *(buf
++) = octchar((unsigned char) c
>> 6);
81 *(buf
++) = octchar((unsigned char) c
>> 3);
82 *(buf
++) = octchar((unsigned char) c
);
91 char *cescape(const char *s
) {
97 /* Does C style string escaping. May be reversed with
100 r
= new(char, strlen(s
)*4 + 1);
104 for (f
= s
, t
= r
; *f
; f
++)
105 t
+= cescape_char(*f
, t
);
112 int cunescape_one(const char *p
, size_t length
, char *ret
, uint32_t *ret_unicode
) {
119 /* Unescapes C style. Returns the unescaped character in ret,
120 * unless we encountered a \u sequence in which case the full
121 * unicode character is returned in ret_unicode, instead. */
123 if (length
!= (size_t) -1 && length
< 1)
160 /* This is an extension of the XDG syntax files */
165 /* hexadecimal encoding */
168 if (length
!= (size_t) -1 && length
< 3)
179 /* Don't allow NUL bytes */
180 if (a
== 0 && b
== 0)
183 *ret
= (char) ((a
<< 4U) | b
);
189 /* C++11 style 16bit unicode */
195 if (length
!= (size_t) -1 && length
< 5)
198 for (i
= 0; i
< 4; i
++) {
199 a
[i
] = unhexchar(p
[1 + i
]);
204 c
= ((uint32_t) a
[0] << 12U) | ((uint32_t) a
[1] << 8U) | ((uint32_t) a
[2] << 4U) | (uint32_t) a
[3];
206 /* Don't allow 0 chars */
225 /* C++11 style 32bit unicode */
231 if (length
!= (size_t) -1 && length
< 9)
234 for (i
= 0; i
< 8; i
++) {
235 a
[i
] = unhexchar(p
[1 + i
]);
240 c
= ((uint32_t) a
[0] << 28U) | ((uint32_t) a
[1] << 24U) | ((uint32_t) a
[2] << 20U) | ((uint32_t) a
[3] << 16U) |
241 ((uint32_t) a
[4] << 12U) | ((uint32_t) a
[5] << 8U) | ((uint32_t) a
[6] << 4U) | (uint32_t) a
[7];
243 /* Don't allow 0 chars */
247 /* Don't allow invalid code points */
248 if (!unichar_is_valid(c
))
277 if (length
!= (size_t) -1 && length
< 3)
292 /* don't allow NUL bytes */
293 if (a
== 0 && b
== 0 && c
== 0)
296 /* Don't allow bytes above 255 */
297 m
= ((uint32_t) a
<< 6U) | ((uint32_t) b
<< 3U) | (uint32_t) c
;
313 int cunescape_length_with_prefix(const char *s
, size_t length
, const char *prefix
, UnescapeFlags flags
, char **ret
) {
321 /* Undoes C style string escaping, and optionally prefixes it. */
323 pl
= prefix
? strlen(prefix
) : 0;
325 r
= new(char, pl
+length
+1);
330 memcpy(r
, prefix
, pl
);
332 for (f
= s
, t
= r
+ pl
; f
< s
+ length
; f
++) {
338 remaining
= s
+ length
- f
;
339 assert(remaining
> 0);
342 /* A literal literal, copy verbatim */
347 if (remaining
== 1) {
348 if (flags
& UNESCAPE_RELAX
) {
349 /* A trailing backslash, copy verbatim */
358 k
= cunescape_one(f
+ 1, remaining
- 1, &c
, &u
);
360 if (flags
& UNESCAPE_RELAX
) {
361 /* Invalid escape code, let's take it literal then */
371 /* Non-Unicode? Let's encode this directly */
374 /* Unicode? Then let's encode this in UTF-8 */
375 t
+= utf8_encode_unichar(t
, u
);
386 int cunescape_length(const char *s
, size_t length
, UnescapeFlags flags
, char **ret
) {
387 return cunescape_length_with_prefix(s
, length
, NULL
, flags
, ret
);
390 int cunescape(const char *s
, UnescapeFlags flags
, char **ret
) {
391 return cunescape_length(s
, strlen(s
), flags
, ret
);
394 char *xescape(const char *s
, const char *bad
) {
398 /* Escapes all chars in bad, in addition to \ and all special
399 * chars, in \xFF style escaping. May be reversed with
402 r
= new(char, strlen(s
) * 4 + 1);
406 for (f
= s
, t
= r
; *f
; f
++) {
408 if ((*f
< ' ') || (*f
>= 127) ||
409 (*f
== '\\') || strchr(bad
, *f
)) {
412 *(t
++) = hexchar(*f
>> 4);
413 *(t
++) = hexchar(*f
);
423 static char *strcpy_backslash_escaped(char *t
, const char *s
, const char *bad
) {
427 if (*s
== '\\' || strchr(bad
, *s
))
436 char *shell_escape(const char *s
, const char *bad
) {
439 r
= new(char, strlen(s
)*2+1);
443 t
= strcpy_backslash_escaped(r
, s
, bad
);
449 char *shell_maybe_quote(const char *s
) {
455 /* Encloses a string in double quotes if necessary to make it
456 * OK as shell string. */
461 strchr(SHELL_NEED_QUOTES
, *p
))
467 r
= new(char, 1+strlen(s
)*2+1+1);
473 t
= mempcpy(t
, s
, p
- s
);
475 t
= strcpy_backslash_escaped(t
, p
, SHELL_NEED_ESCAPE
);