]>
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/>.
23 #include "hexdecoct.h"
27 size_t cescape_char(char c
, char *buf
) {
74 /* For special chars we prefer octal over
75 * hexadecimal encoding, simply because glib's
76 * g_strescape() does the same */
77 if ((c
< ' ') || (c
>= 127)) {
79 *(buf
++) = octchar((unsigned char) c
>> 6);
80 *(buf
++) = octchar((unsigned char) c
>> 3);
81 *(buf
++) = octchar((unsigned char) c
);
90 char *cescape(const char *s
) {
96 /* Does C style string escaping. May be reversed with
99 r
= new(char, strlen(s
)*4 + 1);
103 for (f
= s
, t
= r
; *f
; f
++)
104 t
+= cescape_char(*f
, t
);
111 int cunescape_one(const char *p
, size_t length
, char *ret
, uint32_t *ret_unicode
) {
118 /* Unescapes C style. Returns the unescaped character in ret,
119 * unless we encountered a \u sequence in which case the full
120 * unicode character is returned in ret_unicode, instead. */
122 if (length
!= (size_t) -1 && length
< 1)
159 /* This is an extension of the XDG syntax files */
164 /* hexadecimal encoding */
167 if (length
!= (size_t) -1 && length
< 3)
178 /* Don't allow NUL bytes */
179 if (a
== 0 && b
== 0)
182 *ret
= (char) ((a
<< 4U) | b
);
188 /* C++11 style 16bit unicode */
194 if (length
!= (size_t) -1 && length
< 5)
197 for (i
= 0; i
< 4; i
++) {
198 a
[i
] = unhexchar(p
[1 + i
]);
203 c
= ((uint32_t) a
[0] << 12U) | ((uint32_t) a
[1] << 8U) | ((uint32_t) a
[2] << 4U) | (uint32_t) a
[3];
205 /* Don't allow 0 chars */
224 /* C++11 style 32bit unicode */
230 if (length
!= (size_t) -1 && length
< 9)
233 for (i
= 0; i
< 8; i
++) {
234 a
[i
] = unhexchar(p
[1 + i
]);
239 c
= ((uint32_t) a
[0] << 28U) | ((uint32_t) a
[1] << 24U) | ((uint32_t) a
[2] << 20U) | ((uint32_t) a
[3] << 16U) |
240 ((uint32_t) a
[4] << 12U) | ((uint32_t) a
[5] << 8U) | ((uint32_t) a
[6] << 4U) | (uint32_t) a
[7];
242 /* Don't allow 0 chars */
246 /* Don't allow invalid code points */
247 if (!unichar_is_valid(c
))
276 if (length
!= (size_t) -1 && length
< 3)
291 /* don't allow NUL bytes */
292 if (a
== 0 && b
== 0 && c
== 0)
295 /* Don't allow bytes above 255 */
296 m
= ((uint32_t) a
<< 6U) | ((uint32_t) b
<< 3U) | (uint32_t) c
;
312 int cunescape_length_with_prefix(const char *s
, size_t length
, const char *prefix
, UnescapeFlags flags
, char **ret
) {
320 /* Undoes C style string escaping, and optionally prefixes it. */
322 pl
= prefix
? strlen(prefix
) : 0;
324 r
= new(char, pl
+length
+1);
329 memcpy(r
, prefix
, pl
);
331 for (f
= s
, t
= r
+ pl
; f
< s
+ length
; f
++) {
337 remaining
= s
+ length
- f
;
338 assert(remaining
> 0);
341 /* A literal literal, copy verbatim */
346 if (remaining
== 1) {
347 if (flags
& UNESCAPE_RELAX
) {
348 /* A trailing backslash, copy verbatim */
357 k
= cunescape_one(f
+ 1, remaining
- 1, &c
, &u
);
359 if (flags
& UNESCAPE_RELAX
) {
360 /* Invalid escape code, let's take it literal then */
370 /* Non-Unicode? Let's encode this directly */
373 /* Unicode? Then let's encode this in UTF-8 */
374 t
+= utf8_encode_unichar(t
, u
);
385 int cunescape_length(const char *s
, size_t length
, UnescapeFlags flags
, char **ret
) {
386 return cunescape_length_with_prefix(s
, length
, NULL
, flags
, ret
);
389 int cunescape(const char *s
, UnescapeFlags flags
, char **ret
) {
390 return cunescape_length(s
, strlen(s
), flags
, ret
);
393 char *xescape(const char *s
, const char *bad
) {
397 /* Escapes all chars in bad, in addition to \ and all special
398 * chars, in \xFF style escaping. May be reversed with
401 r
= new(char, strlen(s
) * 4 + 1);
405 for (f
= s
, t
= r
; *f
; f
++) {
407 if ((*f
< ' ') || (*f
>= 127) ||
408 (*f
== '\\') || strchr(bad
, *f
)) {
411 *(t
++) = hexchar(*f
>> 4);
412 *(t
++) = hexchar(*f
);
422 static char *strcpy_backslash_escaped(char *t
, const char *s
, const char *bad
) {
426 if (*s
== '\\' || strchr(bad
, *s
))
435 char *shell_escape(const char *s
, const char *bad
) {
438 r
= new(char, strlen(s
)*2+1);
442 t
= strcpy_backslash_escaped(r
, s
, bad
);
448 char *shell_maybe_quote(const char *s
) {
454 /* Encloses a string in double quotes if necessary to make it
455 * OK as shell string. */
460 strchr(SHELL_NEED_QUOTES
, *p
))
466 r
= new(char, 1+strlen(s
)*2+1+1);
472 t
= mempcpy(t
, s
, p
- s
);
474 t
= strcpy_backslash_escaped(t
, p
, SHELL_NEED_ESCAPE
);