/src/ghostpdl/base/sstring.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2023 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* String and hexstring streams (filters) */ |
18 | | #include "stdio_.h" /* includes std.h */ |
19 | | #include "memory_.h" |
20 | | #include "string_.h" |
21 | | #include "strimpl.h" |
22 | | #include "sstring.h" |
23 | | #include "scanchar.h" |
24 | | |
25 | | /* ------ ASCIIHexEncode ------ */ |
26 | | |
27 | | private_st_AXE_state(); |
28 | | |
29 | | /* Initialize the state */ |
30 | | static int |
31 | | s_AXE_init(stream_state * st) |
32 | 13.1k | { |
33 | 13.1k | stream_AXE_state *const ss = (stream_AXE_state *) st; |
34 | | |
35 | 13.1k | return s_AXE_init_inline(ss); |
36 | 13.1k | } |
37 | | |
38 | | /* Process a buffer */ |
39 | | static int |
40 | | s_AXE_process(stream_state * st, stream_cursor_read * pr, |
41 | | stream_cursor_write * pw, bool last) |
42 | 2.30M | { |
43 | 2.30M | stream_AXE_state *const ss = (stream_AXE_state *) st; |
44 | 2.30M | const byte *p = pr->ptr; |
45 | 2.30M | byte *q = pw->ptr; |
46 | 2.30M | int rcount = pr->limit - p; |
47 | 2.30M | int wcount = pw->limit - q; |
48 | 2.30M | int count; |
49 | 2.30M | int pos = ss->count; |
50 | 2.30M | const char *hex_digits = "0123456789ABCDEF"; |
51 | 2.30M | int status = 0; |
52 | | |
53 | 2.30M | if (last && ss->EndOfData) |
54 | 1.28M | wcount--; /* leave room for '>' */ |
55 | 2.30M | wcount -= (wcount + pos * 2) / 64; /* leave room for \n */ |
56 | 2.30M | wcount >>= 1; /* 2 chars per input byte */ |
57 | 2.30M | count = (wcount < rcount ? (status = 1, wcount) : rcount); |
58 | 44.0M | while (--count >= 0) { |
59 | 41.7M | *++q = hex_digits[*++p >> 4]; |
60 | 41.7M | *++q = hex_digits[*p & 0xf]; |
61 | 41.7M | if (!(++pos & 31) && (count != 0 || !last)) |
62 | 1.23M | *++q = '\n'; |
63 | 41.7M | } |
64 | 2.30M | if (last && status == 0 && ss->EndOfData) |
65 | 1.28M | *++q = '>'; |
66 | 2.30M | pr->ptr = p; |
67 | 2.30M | pw->ptr = q; |
68 | 2.30M | ss->count = pos & 31; |
69 | 2.30M | return status; |
70 | 2.30M | } |
71 | | |
72 | | /* Stream template */ |
73 | | const stream_template s_AXE_template = |
74 | | {&st_AXE_state, s_AXE_init, s_AXE_process, 1, 3 |
75 | | }; |
76 | | |
77 | | /* ------ ASCIIHexDecode ------ */ |
78 | | |
79 | | private_st_AXD_state(); |
80 | | |
81 | | /* Initialize the state */ |
82 | | static int |
83 | | s_AXD_init(stream_state * st) |
84 | 1.53k | { |
85 | 1.53k | stream_AXD_state *const ss = (stream_AXD_state *) st; |
86 | | |
87 | 1.53k | return s_AXD_init_inline(ss); |
88 | 1.53k | } |
89 | | |
90 | | /* Process a buffer */ |
91 | | static int |
92 | | s_AXD_process(stream_state * st, stream_cursor_read * pr, |
93 | | stream_cursor_write * pw, bool last) |
94 | 5.36M | { |
95 | 5.36M | stream_AXD_state *const ss = (stream_AXD_state *) st; |
96 | 5.36M | int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace); |
97 | | |
98 | 5.36M | switch (code) { |
99 | 8.60k | case 0: |
100 | 8.60k | if (ss->odd >= 0 && last) { |
101 | 365 | if (pw->ptr == pw->limit) |
102 | 0 | return 1; |
103 | 365 | *++(pw->ptr) = ss->odd << 4; |
104 | 365 | ss->odd = -1; |
105 | 365 | } |
106 | | /* falls through */ |
107 | 10.3k | case 1: |
108 | | /* We still need to read ahead and check for EOD. */ |
109 | 10.4k | for (; pr->ptr < pr->limit; pr->ptr++) |
110 | 1.69k | if (scan_char_decoder[pr->ptr[1]] != ctype_space) { |
111 | 1.62k | if (pr->ptr[1] == '>') { |
112 | 0 | pr->ptr++; |
113 | 0 | goto eod; |
114 | 0 | } |
115 | 1.62k | return 1; |
116 | 1.62k | } |
117 | 8.77k | return 0; /* still need to scan ahead */ |
118 | 0 | default: |
119 | 0 | return code; |
120 | 5.35M | case ERRC: |
121 | 5.35M | ; |
122 | 5.36M | } |
123 | | /* |
124 | | * Check for EOD. ERRC implies at least one more character |
125 | | * was read; we must unread it, since the caller might have |
126 | | * invoked the filter with exactly the right count to read all |
127 | | * the available data, and we might be reading past the end. |
128 | | */ |
129 | 5.35M | if (*pr->ptr != '>') { /* EOD */ |
130 | 1.23k | --(pr->ptr); |
131 | 1.23k | return ERRC; |
132 | 1.23k | } |
133 | 5.35M | eod:if (ss->odd >= 0) { |
134 | 83.2k | if (pw->ptr == pw->limit) |
135 | 0 | return 1; |
136 | 83.2k | *++(pw->ptr) = ss->odd << 4; |
137 | 83.2k | } |
138 | 5.35M | return EOFC; |
139 | 5.35M | } |
140 | | |
141 | | /* Stream template */ |
142 | | const stream_template s_AXD_template = |
143 | | {&st_AXD_state, s_AXD_init, s_AXD_process, 2, 1 |
144 | | }; |
145 | | |
146 | | /* ------ PSStringEncode ------ */ |
147 | | |
148 | | /* Process a buffer */ |
149 | | static int |
150 | | s_PSSE_process(stream_state * st, stream_cursor_read * pr, |
151 | | stream_cursor_write * pw, bool last) |
152 | 23.3M | { |
153 | 23.3M | const byte *p = pr->ptr; |
154 | 23.3M | const byte *rlimit = pr->limit; |
155 | 23.3M | byte *q = pw->ptr; |
156 | 23.3M | byte *wlimit = pw->limit; |
157 | 23.3M | int status = 0; |
158 | | |
159 | | /* This doesn't have to be very efficient. */ |
160 | 440M | while (p < rlimit) { |
161 | 423M | int c = *++p; |
162 | | |
163 | 423M | if (c < 32 || c >= 127) { |
164 | 326M | const char *pesc; |
165 | 326M | const char *const esc = "\n\r\t\b\f"; |
166 | | |
167 | 326M | if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) { |
168 | 1.57M | if (wlimit - q < 2) { |
169 | 7.79k | --p; |
170 | 7.79k | status = 1; |
171 | 7.79k | break; |
172 | 7.79k | } |
173 | 1.56M | *++q = '\\'; |
174 | 1.56M | *++q = "nrtbf"[pesc - esc]; |
175 | 1.56M | continue; |
176 | 1.57M | } |
177 | 324M | if (wlimit - q < 4) { |
178 | 5.95M | --p; |
179 | 5.95M | status = 1; |
180 | 5.95M | break; |
181 | 5.95M | } |
182 | 318M | q[1] = '\\'; |
183 | 318M | q[2] = (c >> 6) + '0'; |
184 | 318M | q[3] = ((c >> 3) & 7) + '0'; |
185 | 318M | q[4] = (c & 7) + '0'; |
186 | 318M | q += 4; |
187 | 318M | continue; |
188 | 324M | } else if (c == '(' || c == ')' || c == '\\') { |
189 | 2.93M | if (wlimit - q < 2) { |
190 | 3.50k | --p; |
191 | 3.50k | status = 1; |
192 | 3.50k | break; |
193 | 3.50k | } |
194 | 2.92M | *++q = '\\'; |
195 | 93.9M | } else { |
196 | 93.9M | if (q == wlimit) { |
197 | 151k | --p; |
198 | 151k | status = 1; |
199 | 151k | break; |
200 | 151k | } |
201 | 93.9M | } |
202 | 96.7M | *++q = c; |
203 | 96.7M | } |
204 | 23.3M | if (last && status == 0) { |
205 | 5.91M | if (q == wlimit) |
206 | 598 | status = 1; |
207 | 5.91M | else |
208 | 5.91M | *++q = ')'; |
209 | 5.91M | } |
210 | 23.3M | pr->ptr = p; |
211 | 23.3M | pw->ptr = q; |
212 | 23.3M | return status; |
213 | 23.3M | } |
214 | | |
215 | | /* Stream template */ |
216 | | const stream_template s_PSSE_template = |
217 | | {&st_stream_state, NULL, s_PSSE_process, 1, 4 |
218 | | }; |
219 | | |
220 | | /* ------ PSStringDecode ------ */ |
221 | | |
222 | | private_st_PSSD_state(); |
223 | | |
224 | | /* Initialize the state */ |
225 | | int |
226 | | s_PSSD_init(stream_state * st) |
227 | 4.68k | { |
228 | 4.68k | stream_PSSD_state *const ss = (stream_PSSD_state *) st; |
229 | | |
230 | 4.68k | ss->from_string = false; |
231 | 4.68k | return s_PSSD_partially_init_inline(ss); |
232 | 4.68k | } |
233 | | |
234 | | /* Process a buffer */ |
235 | | static int |
236 | | s_PSSD_process(stream_state * st, stream_cursor_read * pr, |
237 | | stream_cursor_write * pw, bool last) |
238 | 170M | { |
239 | 170M | stream_PSSD_state *const ss = (stream_PSSD_state *) st; |
240 | 170M | const byte *p = pr->ptr; |
241 | 170M | const byte *rlimit = pr->limit; |
242 | 170M | byte *q = pw->ptr; |
243 | 170M | byte *wlimit = pw->limit; |
244 | 170M | int status = 0; |
245 | 170M | int c; |
246 | | |
247 | 170M | #define check_p(n)\ |
248 | 170M | if ( p == rlimit ) { p -= n; goto out; } |
249 | 170M | #define check_q(n)\ |
250 | 2.58G | if ( q == wlimit ) { p -= n; status = 1; goto out; } |
251 | 2.76G | while (p < rlimit) { |
252 | 2.76G | c = *++p; |
253 | 2.76G | if (c == '\\' && !ss->from_string) { |
254 | 41.0M | check_p(1); |
255 | 41.0M | switch ((c = *++p)) { |
256 | 25.7M | case 'n': |
257 | 25.7M | c = '\n'; |
258 | 25.7M | goto put; |
259 | 193k | case 'r': |
260 | 193k | c = '\r'; |
261 | 193k | goto put; |
262 | 666k | case 't': |
263 | 666k | c = '\t'; |
264 | 666k | goto put; |
265 | 13.2k | case 'b': |
266 | 13.2k | c = '\b'; |
267 | 13.2k | goto put; |
268 | 284k | case 'f': |
269 | 284k | c = '\f'; |
270 | 284k | goto put; |
271 | 9.15M | default: /* ignore the \ */ |
272 | 36.0M | put:check_q(2); |
273 | 36.0M | *++q = c; |
274 | 36.0M | continue; |
275 | 500k | case char_CR: /* ignore, check for following \n */ |
276 | 500k | check_p(2); |
277 | 499k | if (p[1] == char_EOL) |
278 | 39.6k | p++; |
279 | 499k | continue; |
280 | 219k | case char_EOL: /* ignore */ |
281 | 219k | continue; |
282 | 2.54M | case '0': |
283 | 3.41M | case '1': |
284 | 3.42M | case '2': |
285 | 4.25M | case '3': |
286 | 4.25M | case '4': |
287 | 4.26M | case '5': |
288 | 4.27M | case '6': |
289 | 4.27M | case '7': |
290 | 4.27M | { |
291 | 4.27M | int d; |
292 | | |
293 | 4.27M | check_p(2); |
294 | 4.27M | d = p[1]; |
295 | 4.27M | c -= '0'; |
296 | 4.27M | if (d >= '0' && d <= '7') { |
297 | 3.37M | if (p + 1 == rlimit) { |
298 | 586 | p -= 2; |
299 | 586 | goto out; |
300 | 586 | } |
301 | 3.37M | check_q(2); |
302 | 3.37M | c = (c << 3) + d - '0'; |
303 | 3.37M | d = p[2]; |
304 | 3.37M | if (d >= '0' && d <= '7') { |
305 | 3.32M | c = (c << 3) + d - '0'; |
306 | 3.32M | p += 2; |
307 | 3.32M | } else |
308 | 49.0k | p++; |
309 | 3.37M | } else |
310 | 4.27M | check_q(2); |
311 | 4.27M | *++q = c; |
312 | 4.27M | continue; |
313 | 4.27M | } |
314 | 41.0M | } |
315 | 41.0M | } else |
316 | 2.71G | switch (c) { |
317 | 6.93M | case '(': |
318 | 6.93M | check_q(1); |
319 | 6.93M | ss->depth++; |
320 | 6.93M | break; |
321 | 174M | case ')': |
322 | 174M | if (ss->depth == 0) { |
323 | 170M | status = EOFC; |
324 | 170M | goto out; |
325 | 170M | } |
326 | 3.45M | check_q(1); |
327 | 3.45M | ss->depth--; |
328 | 3.45M | break; |
329 | 4.27M | case char_CR: /* convert to \n */ |
330 | 4.27M | check_p(1); |
331 | 4.26M | check_q(1); |
332 | 4.26M | if (p[1] == char_EOL) |
333 | 120k | p++; |
334 | 4.26M | *++q = '\n'; |
335 | 4.26M | continue; |
336 | 2.95M | case char_EOL: |
337 | 2.95M | c = '\n'; |
338 | | /* fall through */ |
339 | 2.53G | default: |
340 | 2.53G | check_q(1); |
341 | 2.53G | break; |
342 | 2.71G | } |
343 | 2.54G | *++q = c; |
344 | 2.54G | } |
345 | 94.4k | #undef check_p |
346 | 94.4k | #undef check_q |
347 | 170M | out:pr->ptr = p; |
348 | 170M | pw->ptr = q; |
349 | 170M | if (last && status == 0 && p != rlimit) |
350 | 956 | status = ERRC; |
351 | 170M | return status; |
352 | 170M | } |
353 | | |
354 | | /* Stream template */ |
355 | | const stream_template s_PSSD_template = |
356 | | {&st_PSSD_state, s_PSSD_init, s_PSSD_process, 4, 1 |
357 | | }; |
358 | | |
359 | | /* ------ Utilities ------ */ |
360 | | |
361 | | /* |
362 | | * Convert hex data to binary. |
363 | | * Return 1 if we filled the string, |
364 | | * 0 if we ran out of input data before filling the string, |
365 | | * 2 if hex_break_on_whitespace is on and we encounrered |
366 | | * a white space. |
367 | | * ERRC on error. |
368 | | * The caller must set *odd_digit to -1 before the first call; |
369 | | * after each call, if an odd number of hex digits has been read (total), |
370 | | * *odd_digit is the odd digit value, otherwise *odd_digit = -1. |
371 | | * See strimpl.h for the definition of syntax. |
372 | | */ |
373 | | int |
374 | | s_hex_process(stream_cursor_read * pr, stream_cursor_write * pw, |
375 | | int *odd_digit, hex_syntax syntax) |
376 | 35.2M | { |
377 | 35.2M | const byte *p = pr->ptr; |
378 | 35.2M | const byte *rlimit = pr->limit; |
379 | 35.2M | byte *q = pw->ptr; |
380 | 35.2M | byte *wlimit = pw->limit; |
381 | 35.2M | byte *q0 = q; |
382 | 35.2M | byte val1 = (byte) * odd_digit; |
383 | 35.2M | byte val2; |
384 | 35.2M | uint rcount; |
385 | 35.2M | byte *flimit; |
386 | 35.2M | const byte *const decoder = scan_char_decoder; |
387 | 35.2M | int code = 0; |
388 | | |
389 | 35.2M | if (q >= wlimit) |
390 | 1.43k | return 1; |
391 | 35.2M | if (val1 <= 0xf) |
392 | 28.5k | goto d2; |
393 | 36.1M | do { |
394 | | /* No digits read */ |
395 | 36.1M | if ((rcount = (rlimit - p) >> 1) != 0) |
396 | 36.1M | { |
397 | | /* Set up a fast end-of-loop check, so we don't have to test */ |
398 | | /* both p and q against their respective limits. */ |
399 | 36.1M | flimit = (rcount < wlimit - q ? q + rcount : wlimit); |
400 | 180M | while (1) { |
401 | 180M | if ((val1 = decoder[p[1]]) <= 0xf && |
402 | 180M | (val2 = decoder[p[2]]) <= 0xf) { |
403 | 174M | p += 2; |
404 | 174M | *++q = (val1 << 4) + val2; |
405 | 174M | if (q < flimit) |
406 | 144M | continue; |
407 | 29.8M | if (q >= wlimit) |
408 | 30.0k | goto px; |
409 | 29.8M | } |
410 | 36.1M | break; |
411 | 180M | } |
412 | 36.1M | } |
413 | | /* About to read the first digit */ |
414 | 41.2M | while (1) { |
415 | 41.2M | if (p >= rlimit) |
416 | 29.7M | goto end1; |
417 | 11.5M | if ((val1 = decoder[*++p]) > 0xf) { |
418 | 10.4M | if (val1 == ctype_space) { |
419 | 1.41M | switch (syntax) { |
420 | 360k | case hex_ignore_garbage: |
421 | 1.35M | case hex_ignore_whitespace: |
422 | 1.35M | continue; |
423 | 48.9k | case hex_ignore_leading_whitespace: |
424 | 48.9k | if (q == q0 && *odd_digit < 0) |
425 | 31.4k | continue; |
426 | | /* pass through */ |
427 | 27.9k | case hex_break_on_whitespace: |
428 | 27.9k | --p; |
429 | 27.9k | code = 2; |
430 | 27.9k | goto end1; |
431 | 1.41M | } |
432 | 9.05M | } else if (syntax == hex_ignore_garbage) |
433 | 3.77M | continue; |
434 | 5.28M | code = ERRC; |
435 | 5.28M | goto end1; |
436 | 10.4M | } |
437 | 1.08M | break; |
438 | 11.5M | } |
439 | 1.10M | d2: |
440 | | /* About to read the second hex digit of a pair */ |
441 | 5.08M | while (1) { |
442 | 5.08M | if (p >= rlimit) { |
443 | 92.1k | *odd_digit = val1; |
444 | 92.1k | goto ended; |
445 | 92.1k | } |
446 | 4.99M | if ((val2 = decoder[*++p]) > 0xf) { |
447 | 4.08M | if (val2 == ctype_space) |
448 | 550k | switch (syntax) { |
449 | 321k | case hex_ignore_garbage: |
450 | 514k | case hex_ignore_whitespace: |
451 | 514k | continue; |
452 | 27.1k | case hex_ignore_leading_whitespace: |
453 | 27.1k | if (q == q0) |
454 | 22.2k | continue; |
455 | | /* pass through */ |
456 | 13.1k | case hex_break_on_whitespace: |
457 | 13.1k | --p; |
458 | 13.1k | *odd_digit = val1; |
459 | 13.1k | code = 2; |
460 | 13.1k | goto ended; |
461 | 550k | } |
462 | 3.53M | if (syntax == hex_ignore_garbage) |
463 | 3.43M | continue; |
464 | 95.0k | *odd_digit = val1; |
465 | 95.0k | code = ERRC; |
466 | 95.0k | goto ended; |
467 | 3.53M | } |
468 | 909k | break; |
469 | 4.99M | } |
470 | 909k | *++q = (val1 << 4) + val2; |
471 | 909k | } while (q < wlimit); |
472 | 30.2k | px:code = 1; |
473 | 35.0M | end1:*odd_digit = -1; |
474 | 35.2M | ended:pr->ptr = p; |
475 | 35.2M | pw->ptr = q; |
476 | 35.2M | return code; |
477 | 35.0M | } |