/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 | 4.72k | { |
33 | 4.72k | stream_AXE_state *const ss = (stream_AXE_state *) st; |
34 | | |
35 | 4.72k | return s_AXE_init_inline(ss); |
36 | 4.72k | } |
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 | 811k | { |
43 | 811k | stream_AXE_state *const ss = (stream_AXE_state *) st; |
44 | 811k | const byte *p = pr->ptr; |
45 | 811k | byte *q = pw->ptr; |
46 | 811k | int rcount = pr->limit - p; |
47 | 811k | int wcount = pw->limit - q; |
48 | 811k | int count; |
49 | 811k | int pos = ss->count; |
50 | 811k | const char *hex_digits = "0123456789ABCDEF"; |
51 | 811k | int status = 0; |
52 | | |
53 | 811k | if (last && ss->EndOfData) |
54 | 431k | wcount--; /* leave room for '>' */ |
55 | 811k | wcount -= (wcount + pos * 2) / 64; /* leave room for \n */ |
56 | 811k | wcount >>= 1; /* 2 chars per input byte */ |
57 | 811k | count = (wcount < rcount ? (status = 1, wcount) : rcount); |
58 | 16.1M | while (--count >= 0) { |
59 | 15.3M | *++q = hex_digits[*++p >> 4]; |
60 | 15.3M | *++q = hex_digits[*p & 0xf]; |
61 | 15.3M | if (!(++pos & 31) && (count != 0 || !last)) |
62 | 459k | *++q = '\n'; |
63 | 15.3M | } |
64 | 811k | if (last && status == 0 && ss->EndOfData) |
65 | 430k | *++q = '>'; |
66 | 811k | pr->ptr = p; |
67 | 811k | pw->ptr = q; |
68 | 811k | ss->count = pos & 31; |
69 | 811k | return status; |
70 | 811k | } |
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 | 95 | { |
85 | 95 | stream_AXD_state *const ss = (stream_AXD_state *) st; |
86 | | |
87 | 95 | return s_AXD_init_inline(ss); |
88 | 95 | } |
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 | 317k | { |
95 | 317k | stream_AXD_state *const ss = (stream_AXD_state *) st; |
96 | 317k | int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace); |
97 | | |
98 | 317k | switch (code) { |
99 | 578 | case 0: |
100 | 578 | if (ss->odd >= 0 && last) { |
101 | 20 | if (pw->ptr == pw->limit) |
102 | 0 | return 1; |
103 | 20 | *++(pw->ptr) = ss->odd << 4; |
104 | 20 | ss->odd = -1; |
105 | 20 | } |
106 | | /* falls through */ |
107 | 723 | case 1: |
108 | | /* We still need to read ahead and check for EOD. */ |
109 | 723 | for (; pr->ptr < pr->limit; pr->ptr++) |
110 | 131 | if (scan_char_decoder[pr->ptr[1]] != ctype_space) { |
111 | 131 | if (pr->ptr[1] == '>') { |
112 | 0 | pr->ptr++; |
113 | 0 | goto eod; |
114 | 0 | } |
115 | 131 | return 1; |
116 | 131 | } |
117 | 592 | return 0; /* still need to scan ahead */ |
118 | 0 | default: |
119 | 0 | return code; |
120 | 316k | case ERRC: |
121 | 316k | ; |
122 | 317k | } |
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 | 316k | if (*pr->ptr != '>') { /* EOD */ |
130 | 81 | --(pr->ptr); |
131 | 81 | return ERRC; |
132 | 81 | } |
133 | 316k | eod:if (ss->odd >= 0) { |
134 | 1.14k | if (pw->ptr == pw->limit) |
135 | 0 | return 1; |
136 | 1.14k | *++(pw->ptr) = ss->odd << 4; |
137 | 1.14k | } |
138 | 316k | return EOFC; |
139 | 316k | } |
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 | 2.31M | { |
153 | 2.31M | const byte *p = pr->ptr; |
154 | 2.31M | const byte *rlimit = pr->limit; |
155 | 2.31M | byte *q = pw->ptr; |
156 | 2.31M | byte *wlimit = pw->limit; |
157 | 2.31M | int status = 0; |
158 | | |
159 | | /* This doesn't have to be very efficient. */ |
160 | 7.72M | while (p < rlimit) { |
161 | 5.42M | int c = *++p; |
162 | | |
163 | 5.42M | if (c < 32 || c >= 127) { |
164 | 197k | const char *pesc; |
165 | 197k | const char *const esc = "\n\r\t\b\f"; |
166 | | |
167 | 197k | if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) { |
168 | 32.6k | if (wlimit - q < 2) { |
169 | 547 | --p; |
170 | 547 | status = 1; |
171 | 547 | break; |
172 | 547 | } |
173 | 32.1k | *++q = '\\'; |
174 | 32.1k | *++q = "nrtbf"[pesc - esc]; |
175 | 32.1k | continue; |
176 | 32.6k | } |
177 | 165k | if (wlimit - q < 4) { |
178 | 2.21k | --p; |
179 | 2.21k | status = 1; |
180 | 2.21k | break; |
181 | 2.21k | } |
182 | 162k | q[1] = '\\'; |
183 | 162k | q[2] = (c >> 6) + '0'; |
184 | 162k | q[3] = ((c >> 3) & 7) + '0'; |
185 | 162k | q[4] = (c & 7) + '0'; |
186 | 162k | q += 4; |
187 | 162k | continue; |
188 | 5.22M | } else if (c == '(' || c == ')' || c == '\\') { |
189 | 59.4k | if (wlimit - q < 2) { |
190 | 152 | --p; |
191 | 152 | status = 1; |
192 | 152 | break; |
193 | 152 | } |
194 | 59.2k | *++q = '\\'; |
195 | 5.16M | } else { |
196 | 5.16M | if (q == wlimit) { |
197 | 2.42k | --p; |
198 | 2.42k | status = 1; |
199 | 2.42k | break; |
200 | 2.42k | } |
201 | 5.16M | } |
202 | 5.22M | *++q = c; |
203 | 5.22M | } |
204 | 2.31M | if (last && status == 0) { |
205 | 2.30M | if (q == wlimit) |
206 | 21 | status = 1; |
207 | 2.30M | else |
208 | 2.30M | *++q = ')'; |
209 | 2.30M | } |
210 | 2.31M | pr->ptr = p; |
211 | 2.31M | pw->ptr = q; |
212 | 2.31M | return status; |
213 | 2.31M | } |
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 | 0 | { |
228 | 0 | stream_PSSD_state *const ss = (stream_PSSD_state *) st; |
229 | |
|
230 | 0 | ss->from_string = false; |
231 | 0 | return s_PSSD_partially_init_inline(ss); |
232 | 0 | } |
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 | 10.2M | { |
239 | 10.2M | stream_PSSD_state *const ss = (stream_PSSD_state *) st; |
240 | 10.2M | const byte *p = pr->ptr; |
241 | 10.2M | const byte *rlimit = pr->limit; |
242 | 10.2M | byte *q = pw->ptr; |
243 | 10.2M | byte *wlimit = pw->limit; |
244 | 10.2M | int status = 0; |
245 | 10.2M | int c; |
246 | | |
247 | 10.2M | #define check_p(n)\ |
248 | 10.2M | if ( p == rlimit ) { p -= n; goto out; } |
249 | 10.2M | #define check_q(n)\ |
250 | 154M | if ( q == wlimit ) { p -= n; status = 1; goto out; } |
251 | 165M | while (p < rlimit) { |
252 | 165M | c = *++p; |
253 | 165M | if (c == '\\' && !ss->from_string) { |
254 | 2.48M | check_p(1); |
255 | 2.48M | switch ((c = *++p)) { |
256 | 1.54M | case 'n': |
257 | 1.54M | c = '\n'; |
258 | 1.54M | goto put; |
259 | 10.7k | case 'r': |
260 | 10.7k | c = '\r'; |
261 | 10.7k | goto put; |
262 | 39.2k | case 't': |
263 | 39.2k | c = '\t'; |
264 | 39.2k | goto put; |
265 | 894 | case 'b': |
266 | 894 | c = '\b'; |
267 | 894 | goto put; |
268 | 27.2k | case 'f': |
269 | 27.2k | c = '\f'; |
270 | 27.2k | goto put; |
271 | 425k | default: /* ignore the \ */ |
272 | 2.05M | put:check_q(2); |
273 | 2.05M | *++q = c; |
274 | 2.05M | continue; |
275 | 18.2k | case char_CR: /* ignore, check for following \n */ |
276 | 18.2k | check_p(2); |
277 | 18.2k | if (p[1] == char_EOL) |
278 | 544 | p++; |
279 | 18.2k | continue; |
280 | 14.5k | case char_EOL: /* ignore */ |
281 | 14.5k | continue; |
282 | 148k | case '0': |
283 | 273k | case '1': |
284 | 273k | case '2': |
285 | 396k | case '3': |
286 | 396k | case '4': |
287 | 396k | case '5': |
288 | 396k | case '6': |
289 | 398k | case '7': |
290 | 398k | { |
291 | 398k | int d; |
292 | | |
293 | 398k | check_p(2); |
294 | 397k | d = p[1]; |
295 | 397k | c -= '0'; |
296 | 397k | if (d >= '0' && d <= '7') { |
297 | 269k | if (p + 1 == rlimit) { |
298 | 47 | p -= 2; |
299 | 47 | goto out; |
300 | 47 | } |
301 | 269k | check_q(2); |
302 | 269k | c = (c << 3) + d - '0'; |
303 | 269k | d = p[2]; |
304 | 269k | if (d >= '0' && d <= '7') { |
305 | 267k | c = (c << 3) + d - '0'; |
306 | 267k | p += 2; |
307 | 267k | } else |
308 | 2.16k | p++; |
309 | 269k | } else |
310 | 397k | check_q(2); |
311 | 397k | *++q = c; |
312 | 397k | continue; |
313 | 397k | } |
314 | 2.48M | } |
315 | 2.48M | } else |
316 | 162M | switch (c) { |
317 | 499k | case '(': |
318 | 499k | check_q(1); |
319 | 499k | ss->depth++; |
320 | 499k | break; |
321 | 10.5M | case ')': |
322 | 10.5M | if (ss->depth == 0) { |
323 | 10.2M | status = EOFC; |
324 | 10.2M | goto out; |
325 | 10.2M | } |
326 | 324k | check_q(1); |
327 | 324k | ss->depth--; |
328 | 324k | break; |
329 | 214k | case char_CR: /* convert to \n */ |
330 | 214k | check_p(1); |
331 | 213k | check_q(1); |
332 | 213k | if (p[1] == char_EOL) |
333 | 1.09k | p++; |
334 | 213k | *++q = '\n'; |
335 | 213k | continue; |
336 | 35.6k | case char_EOL: |
337 | 35.6k | c = '\n'; |
338 | | /* fall through */ |
339 | 151M | default: |
340 | 151M | check_q(1); |
341 | 151M | break; |
342 | 162M | } |
343 | 152M | *++q = c; |
344 | 152M | } |
345 | 4.35k | #undef check_p |
346 | 4.35k | #undef check_q |
347 | 10.2M | out:pr->ptr = p; |
348 | 10.2M | pw->ptr = q; |
349 | 10.2M | if (last && status == 0 && p != rlimit) |
350 | 67 | status = ERRC; |
351 | 10.2M | return status; |
352 | 10.2M | } |
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 | 2.51M | { |
377 | 2.51M | const byte *p = pr->ptr; |
378 | 2.51M | const byte *rlimit = pr->limit; |
379 | 2.51M | byte *q = pw->ptr; |
380 | 2.51M | byte *wlimit = pw->limit; |
381 | 2.51M | byte *q0 = q; |
382 | 2.51M | byte val1 = (byte) * odd_digit; |
383 | 2.51M | byte val2; |
384 | 2.51M | uint rcount; |
385 | 2.51M | byte *flimit; |
386 | 2.51M | const byte *const decoder = scan_char_decoder; |
387 | 2.51M | int code = 0; |
388 | | |
389 | 2.51M | if (q >= wlimit) |
390 | 99 | return 1; |
391 | 2.51M | if (val1 <= 0xf) |
392 | 1.29k | goto d2; |
393 | 2.55M | do { |
394 | | /* No digits read */ |
395 | 2.55M | if ((rcount = (rlimit - p) >> 1) != 0) |
396 | 2.55M | { |
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 | 2.55M | flimit = (rcount < wlimit - q ? q + rcount : wlimit); |
400 | 11.7M | while (1) { |
401 | 11.7M | if ((val1 = decoder[p[1]]) <= 0xf && |
402 | 11.7M | (val2 = decoder[p[2]]) <= 0xf) { |
403 | 11.3M | p += 2; |
404 | 11.3M | *++q = (val1 << 4) + val2; |
405 | 11.3M | if (q < flimit) |
406 | 9.20M | continue; |
407 | 2.19M | if (q >= wlimit) |
408 | 2.58k | goto px; |
409 | 2.19M | } |
410 | 2.55M | break; |
411 | 11.7M | } |
412 | 2.55M | } |
413 | | /* About to read the first digit */ |
414 | 2.90M | while (1) { |
415 | 2.90M | if (p >= rlimit) |
416 | 2.18M | goto end1; |
417 | 720k | if ((val1 = decoder[*++p]) > 0xf) { |
418 | 670k | if (val1 == ctype_space) { |
419 | 64.8k | switch (syntax) { |
420 | 18.8k | case hex_ignore_garbage: |
421 | 58.3k | case hex_ignore_whitespace: |
422 | 58.3k | continue; |
423 | 6.32k | case hex_ignore_leading_whitespace: |
424 | 6.32k | if (q == q0 && *odd_digit < 0) |
425 | 3.23k | continue; |
426 | | /* pass through */ |
427 | 3.22k | case hex_break_on_whitespace: |
428 | 3.22k | --p; |
429 | 3.22k | code = 2; |
430 | 3.22k | goto end1; |
431 | 64.8k | } |
432 | 605k | } else if (syntax == hex_ignore_garbage) |
433 | 290k | continue; |
434 | 315k | code = ERRC; |
435 | 315k | goto end1; |
436 | 670k | } |
437 | 50.2k | break; |
438 | 720k | } |
439 | 51.5k | d2: |
440 | | /* About to read the second hex digit of a pair */ |
441 | 332k | while (1) { |
442 | 332k | if (p >= rlimit) { |
443 | 6.72k | *odd_digit = val1; |
444 | 6.72k | goto ended; |
445 | 6.72k | } |
446 | 325k | if ((val2 = decoder[*++p]) > 0xf) { |
447 | 282k | if (val2 == ctype_space) |
448 | 20.8k | switch (syntax) { |
449 | 18.2k | case hex_ignore_garbage: |
450 | 18.6k | case hex_ignore_whitespace: |
451 | 18.6k | continue; |
452 | 2.00k | case hex_ignore_leading_whitespace: |
453 | 2.00k | if (q == q0) |
454 | 1.07k | continue; |
455 | | /* pass through */ |
456 | 1.06k | case hex_break_on_whitespace: |
457 | 1.06k | --p; |
458 | 1.06k | *odd_digit = val1; |
459 | 1.06k | code = 2; |
460 | 1.06k | goto ended; |
461 | 20.8k | } |
462 | 261k | if (syntax == hex_ignore_garbage) |
463 | 260k | continue; |
464 | 1.18k | *odd_digit = val1; |
465 | 1.18k | code = ERRC; |
466 | 1.18k | goto ended; |
467 | 261k | } |
468 | 42.5k | break; |
469 | 325k | } |
470 | 42.5k | *++q = (val1 << 4) + val2; |
471 | 42.5k | } while (q < wlimit); |
472 | 2.58k | px:code = 1; |
473 | 2.50M | end1:*odd_digit = -1; |
474 | 2.51M | ended:pr->ptr = p; |
475 | 2.51M | pw->ptr = q; |
476 | 2.51M | return code; |
477 | 2.50M | } |