]>
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"
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(const char *s
) {
98 /* Does C style string escaping. May be reversed with
101 r
= new(char, strlen(s
)*4 + 1);
105 for (f
= s
, t
= r
; *f
; f
++)
106 t
+= cescape_char(*f
, t
);
113 int cunescape_one(const char *p
, size_t length
, char *ret
, uint32_t *ret_unicode
) {
120 /* Unescapes C style. Returns the unescaped character in ret,
121 * unless we encountered a \u sequence in which case the full
122 * unicode character is returned in ret_unicode, instead. */
124 if (length
!= (size_t) -1 && length
< 1)
161 /* This is an extension of the XDG syntax files */
166 /* hexadecimal encoding */
169 if (length
!= (size_t) -1 && length
< 3)
180 /* Don't allow NUL bytes */
181 if (a
== 0 && b
== 0)
184 *ret
= (char) ((a
<< 4U) | b
);
190 /* C++11 style 16bit unicode */
196 if (length
!= (size_t) -1 && length
< 5)
199 for (i
= 0; i
< 4; i
++) {
200 a
[i
] = unhexchar(p
[1 + i
]);
205 c
= ((uint32_t) a
[0] << 12U) | ((uint32_t) a
[1] << 8U) | ((uint32_t) a
[2] << 4U) | (uint32_t) a
[3];
207 /* 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
))
278 if (length
!= (size_t) -1 && length
< 3)
293 /* don't allow NUL bytes */
294 if (a
== 0 && b
== 0 && c
== 0)
297 /* Don't allow bytes above 255 */
298 m
= ((uint32_t) a
<< 6U) | ((uint32_t) b
<< 3U) | (uint32_t) c
;
314 int cunescape_length_with_prefix(const char *s
, size_t length
, const char *prefix
, UnescapeFlags flags
, char **ret
) {
322 /* Undoes C style string escaping, and optionally prefixes it. */
324 pl
= prefix
? strlen(prefix
) : 0;
326 r
= new(char, pl
+length
+1);
331 memcpy(r
, prefix
, pl
);
333 for (f
= s
, t
= r
+ pl
; f
< s
+ length
; f
++) {
339 remaining
= s
+ length
- f
;
340 assert(remaining
> 0);
343 /* A literal literal, copy verbatim */
348 if (remaining
== 1) {
349 if (flags
& UNESCAPE_RELAX
) {
350 /* A trailing backslash, copy verbatim */
359 k
= cunescape_one(f
+ 1, remaining
- 1, &c
, &u
);
361 if (flags
& UNESCAPE_RELAX
) {
362 /* Invalid escape code, let's take it literal then */
372 /* Non-Unicode? Let's encode this directly */
375 /* Unicode? Then let's encode this in UTF-8 */
376 t
+= utf8_encode_unichar(t
, u
);
387 int cunescape_length(const char *s
, size_t length
, UnescapeFlags flags
, char **ret
) {
388 return cunescape_length_with_prefix(s
, length
, NULL
, flags
, ret
);
391 int cunescape(const char *s
, UnescapeFlags flags
, char **ret
) {
392 return cunescape_length(s
, strlen(s
), flags
, ret
);
395 char *xescape(const char *s
, const char *bad
) {
399 /* Escapes all chars in bad, in addition to \ and all special
400 * chars, in \xFF style escaping. May be reversed with
403 r
= new(char, strlen(s
) * 4 + 1);
407 for (f
= s
, t
= r
; *f
; f
++) {
409 if ((*f
< ' ') || (*f
>= 127) ||
410 (*f
== '\\') || strchr(bad
, *f
)) {
413 *(t
++) = hexchar(*f
>> 4);
414 *(t
++) = hexchar(*f
);
424 static char *strcpy_backslash_escaped(char *t
, const char *s
, const char *bad
) {
428 if (*s
== '\\' || strchr(bad
, *s
))
437 char *shell_escape(const char *s
, const char *bad
) {
440 r
= new(char, strlen(s
)*2+1);
444 t
= strcpy_backslash_escaped(r
, s
, bad
);
450 char *shell_maybe_quote(const char *s
) {
456 /* Encloses a string in double quotes if necessary to make it
457 * OK as shell string. */
462 strchr(SHELL_NEED_QUOTES
, *p
))
468 r
= new(char, 1+strlen(s
)*2+1+1);
474 t
= mempcpy(t
, s
, p
- s
);
476 t
= strcpy_backslash_escaped(t
, p
, SHELL_NEED_ESCAPE
);