Debian Codegraph @

src/sd-escape.c

raw symbols SCIP
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */cxx . . $ `<file>/src/sd-escape.c`/
2
3 #include "sd-escape.h"cxx . . $ `<file>/src/sd-escape.h`/
4 #include <errno.h>
5
6 /* The following is copied from these files in systemd with minor
7 * modifications:
8 * - src/basic/escape.c
9 * - src/basic/utf8.c
10 * - src/basic/hexdecoct.c
11 */
12
13 static int unhexcharcxx . . $ unhexchar(784f3405e7e545be).(char c) {
14
15 if (c >= '0' && c <= '9')
16 return c - '0';
17
18 if (c >= 'a' && c <= 'f')
19 return c - 'a' + 10;
20
21 if (c >= 'A' && c <= 'F')
22 return c - 'A' + 10;
23
24 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
25 }
26
27 static gbooleancxx . . $ gboolean#
28 unichar_is_validcxx . . $ unichar_is_valid(4c679297bf94b9eb).(guint32cxx . . $ guint32# ch) {
29
30 if (ch >= 0x110000) /* End of unicode space */
31 return FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!;
32 if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
33 return FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!;
34 if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
35 return FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!;
36 if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
37 return FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!;
38
39 return TRUEcxx . . $ `<external>/12741050725406041920/gmacros.h:971:9`!;
40 }
41
42 static int
43 unoctcharcxx . . $ unoctchar(784f3405e7e545be).(char c) {
44
45 if (c >= '0' && c <= '7')
46 return c - '0';
47
48 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
49 }
50
51 int cunescape_onecxx . . $ cunescape_one(2e8190e4e1b147d8).(const char *p, gsizecxx . . $ gsize# length, guint32cxx . . $ guint32# *ret, gbooleancxx . . $ gboolean# *eight_bit, gbooleancxx . . $ gboolean# accept_nul) {
52 int r = 1;
53
54 g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`!(p);
55 g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`!(ret);
56
57 /* Unescapes C style. Returns the unescaped character in ret.
58 * Sets *eight_bit to true if the escaped sequence either fits in
59 * one byte in UTF-8 or is a non-unicode literal byte and should
60 * instead be copied directly.
61 */
62
63 if (length != G_MAXSIZEcxx . . $ `<external>/10505296616216143549/glibconfig.h:89:9`! && length < 1)
64 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
65
66 switch (p[0]) {
67
68 case 'a':
69 *ret = '\a';
70 break;
71 case 'b':
72 *ret = '\b';
73 break;
74 case 'f':
75 *ret = '\f';
76 break;
77 case 'n':
78 *ret = '\n';
79 break;
80 case 'r':
81 *ret = '\r';
82 break;
83 case 't':
84 *ret = '\t';
85 break;
86 case 'v':
87 *ret = '\v';
88 break;
89 case '\\':
90 *ret = '\\';
91 break;
92 case '"':
93 *ret = '"';
94 break;
95 case '\'':
96 *ret = '\'';
97 break;
98
99 case 's':
100 /* This is an extension of the XDG syntax files */
101 *ret = ' ';
102 break;
103
104 case 'x': {
105 /* hexadecimal encoding */
106 int a, b;
107
108 if (length != G_MAXSIZEcxx . . $ `<external>/10505296616216143549/glibconfig.h:89:9`! && length < 3)
109 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
110
111 a = unhexcharcxx . . $ unhexchar(784f3405e7e545be).(p[1]);
112 if (a < 0)
113 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
114
115 b = unhexcharcxx . . $ unhexchar(784f3405e7e545be).(p[2]);
116 if (b < 0)
117 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
118
119 /* Don't allow NUL bytes */
120 if (a == 0 && b == 0 && !accept_nul)
121 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
122
123 *ret = (a << 4U) | b;
124 *eight_bit = TRUEcxx . . $ `<external>/12741050725406041920/gmacros.h:971:9`!;
125 r = 3;
126 break;
127 }
128
129 case 'u': {
130 /* C++11 style 16bit unicode */
131
132 int a[4];
133 gsizecxx . . $ gsize# i;
134 guint32cxx . . $ guint32# c;
135
136 if (length != G_MAXSIZEcxx . . $ `<external>/10505296616216143549/glibconfig.h:89:9`! && length < 5)
137 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
138
139 for (i = 0; i < 4; i++) {
140 a[i] = unhexcharcxx . . $ unhexchar(784f3405e7e545be).(p[1 + i]);
141 if (a[i] < 0)
142 return a[i];
143 }
144
145 c = ((guint32cxx . . $ guint32#) a[0] << 12U) | ((guint32cxx . . $ guint32#) a[1] << 8U) | ((guint32cxx . . $ guint32#) a[2] << 4U) | (guint32cxx . . $ guint32#) a[3];
146
147 /* Don't allow 0 chars */
148 if (c == 0 && !accept_nul)
149 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
150
151 *ret = c;
152 r = 5;
153 break;
154 }
155
156 case 'U': {
157 /* C++11 style 32bit unicode */
158
159 int a[8];
160 gsizecxx . . $ gsize# i;
161 guint32cxx . . $ guint32# c;
162
163 if (length != G_MAXSIZEcxx . . $ `<external>/10505296616216143549/glibconfig.h:89:9`! && length < 9)
164 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
165
166 for (i = 0; i < 8; i++) {
167 a[i] = unhexcharcxx . . $ unhexchar(784f3405e7e545be).(p[1 + i]);
168 if (a[i] < 0)
169 return a[i];
170 }
171
172 c = ((guint32cxx . . $ guint32#) a[0] << 28U) | ((guint32cxx . . $ guint32#) a[1] << 24U) | ((guint32cxx . . $ guint32#) a[2] << 20U) | ((guint32cxx . . $ guint32#) a[3] << 16U) |
173 ((guint32cxx . . $ guint32#) a[4] << 12U) | ((guint32cxx . . $ guint32#) a[5] << 8U) | ((guint32cxx . . $ guint32#) a[6] << 4U) | (guint32cxx . . $ guint32#) a[7];
174
175 /* Don't allow 0 chars */
176 if (c == 0 && !accept_nul)
177 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
178
179 /* Don't allow invalid code points */
180 if (!unichar_is_validcxx . . $ unichar_is_valid(4c679297bf94b9eb).(c))
181 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
182
183 *ret = c;
184 r = 9;
185 break;
186 }
187
188 case '0':
189 case '1':
190 case '2':
191 case '3':
192 case '4':
193 case '5':
194 case '6':
195 case '7': {
196 /* octal encoding */
197 int a, b, c;
198 guint32cxx . . $ guint32# m;
199
200 if (length != G_MAXSIZEcxx . . $ `<external>/10505296616216143549/glibconfig.h:89:9`! && length < 3)
201 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
202
203 a = unoctcharcxx . . $ unoctchar(784f3405e7e545be).(p[0]);
204 if (a < 0)
205 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
206
207 b = unoctcharcxx . . $ unoctchar(784f3405e7e545be).(p[1]);
208 if (b < 0)
209 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
210
211 c = unoctcharcxx . . $ unoctchar(784f3405e7e545be).(p[2]);
212 if (c < 0)
213 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
214
215 /* don't allow NUL bytes */
216 if (a == 0 && b == 0 && c == 0 && !accept_nul)
217 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
218
219 /* Don't allow bytes above 255 */
220 m = ((guint32cxx . . $ guint32#) a << 6U) | ((guint32cxx . . $ guint32#) b << 3U) | (guint32cxx . . $ guint32#) c;
221 if (m > 255)
222 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
223
224 *ret = m;
225 *eight_bit = TRUEcxx . . $ `<external>/12741050725406041920/gmacros.h:971:9`!;
226 r = 3;
227 break;
228 }
229
230 default:
231 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
232 }
233
234 return r;
235 }
236
237 /**
238 * utf8_encode_unichar: Encode single UCS-4 character as UTF-8
239 * @out_utf8: output buffer of at least 4 bytes or NULL
240 * @g: UCS-4 character to encode
241 *
242 * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8.
243 * The length of the character is returned. It is not zero-terminated! If the
244 * output buffer is NULL, only the length is returned.
245 *
246 * Returns: The length in bytes that the UTF-8 representation does or would
247 * occupy.
248 */
249 static gsizecxx . . $ gsize#
250 utf8_encode_unicharcxx . . $ utf8_encode_unichar(d54821d47c7efe5e).(char *out_utf8, guint32cxx . . $ guint32# g) {
251
252 if (g < (1 << 7)) {
253 if (out_utf8)
254 out_utf8[0] = g & 0x7f;
255 return 1;
256 } else if (g < (1 << 11)) {
257 if (out_utf8) {
258 out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f);
259 out_utf8[1] = 0x80 | (g & 0x3f);
260 }
261 return 2;
262 } else if (g < (1 << 16)) {
263 if (out_utf8) {
264 out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f);
265 out_utf8[1] = 0x80 | ((g >> 6) & 0x3f);
266 out_utf8[2] = 0x80 | (g & 0x3f);
267 }
268 return 3;
269 } else if (g < (1 << 21)) {
270 if (out_utf8) {
271 out_utf8[0] = 0xf0 | ((g >> 18) & 0x07);
272 out_utf8[1] = 0x80 | ((g >> 12) & 0x3f);
273 out_utf8[2] = 0x80 | ((g >> 6) & 0x3f);
274 out_utf8[3] = 0x80 | (g & 0x3f);
275 }
276 return 4;
277 }
278
279 return 0;
280 }
281
282 gssizecxx . . $ gssize# cunescape_length_with_prefixcxx . . $ cunescape_length_with_prefix(86207bd5d8315feb).(const char *s, gsizecxx . . $ gsize# length, const char *prefix, UnescapeFlagscxx . . $ UnescapeFlags# flags, char **ret) {
283 g_autofreecxx . . $ `<external>/12741050725406041920/gmacros.h:1429:9`! char *ans = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
284 char *t;
285 const char *f;
286 gsizecxx . . $ gsize# pl;
287 int r;
288
289 g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`!(s);
290 g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`!(ret);
291
292 /* Undoes C style string escaping, and optionally prefixes it. */
293
294 if (!prefix)
295 pl = 0;
296 else
297 pl = strlencxx . . $ strlen(751346f8406fd082).(prefix);
298
299 ans = g_newcxx . . $ `<external>/1123956534522462559/gmem.h:318:9`! (char, pl+length+1);
300 if (!ans)
301 return -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
302
303 if (prefix)
304 memcpycxx . . $ memcpy(cc817cb175f282d).(ans, prefix, pl);
305
306 for (f = s, t = ans + pl; f < s + length; f++) {
307 gsizecxx . . $ gsize# remaining;
308 gbooleancxx . . $ gboolean# eight_bit = FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!;
309 guint32cxx . . $ guint32# u;
310
311 remaining = s + length - f;
312 g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`!(remaining > 0);
313
314 if (*f != '\\') {
315 /* A literal, copy verbatim */
316 *(t++) = *f;
317 continue;
318 }
319
320 if (remaining == 1) {
321 if (flags & UNESCAPE_RELAXcxx . . $ UNESCAPE_RELAX.) {
322 /* A trailing backslash, copy verbatim */
323 *(t++) = *f;
324 continue;
325 }
326
327 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
328 }
329
330 r = cunescape_onecxx . . $ cunescape_one(2e8190e4e1b147d8).(f + 1, remaining - 1, &u, &eight_bit, flags & UNESCAPE_ACCEPT_NULcxx . . $ UNESCAPE_ACCEPT_NUL.);
331 if (r < 0) {
332 if (flags & UNESCAPE_RELAXcxx . . $ UNESCAPE_RELAX.) {
333 /* Invalid escape code, let's take it literal then */
334 *(t++) = '\\';
335 continue;
336 }
337
338 return r;
339 }
340
341 f += r;
342 if (eight_bit)
343 /* One byte? Set directly as specified */
344 *(t++) = u;
345 else
346 /* Otherwise encode as multi-byte UTF-8 */
347 t += utf8_encode_unicharcxx . . $ utf8_encode_unichar(d54821d47c7efe5e).(t, u);
348 }
349
350 *t = 0;
351
352 g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`!(t >= ans); /* Let static analyzers know that the answer is non-negative. */
353 *ret = g_steal_pointercxx . . $ `<external>/1123956534522462559/gmem.h:250:9`! (&ans);
354 return t - *ret;
355 }
356