/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 | 6.13k | { |
33 | 6.13k | stream_AXE_state *const ss = (stream_AXE_state *) st; |
34 | | |
35 | 6.13k | return s_AXE_init_inline(ss); |
36 | 6.13k | } |
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 | 1.09M | { |
43 | 1.09M | stream_AXE_state *const ss = (stream_AXE_state *) st; |
44 | 1.09M | const byte *p = pr->ptr; |
45 | 1.09M | byte *q = pw->ptr; |
46 | 1.09M | int rcount = pr->limit - p; |
47 | 1.09M | int wcount = pw->limit - q; |
48 | 1.09M | int count; |
49 | 1.09M | int pos = ss->count; |
50 | 1.09M | const char *hex_digits = "0123456789ABCDEF"; |
51 | 1.09M | int status = 0; |
52 | | |
53 | 1.09M | if (last && ss->EndOfData) |
54 | 614k | wcount--; /* leave room for '>' */ |
55 | 1.09M | wcount -= (wcount + pos * 2) / 64; /* leave room for \n */ |
56 | 1.09M | wcount >>= 1; /* 2 chars per input byte */ |
57 | 1.09M | count = (wcount < rcount ? (status = 1, wcount) : rcount); |
58 | 20.7M | while (--count >= 0) { |
59 | 19.6M | *++q = hex_digits[*++p >> 4]; |
60 | 19.6M | *++q = hex_digits[*p & 0xf]; |
61 | 19.6M | if (!(++pos & 31) && (count != 0 || !last)) |
62 | 584k | *++q = '\n'; |
63 | 19.6M | } |
64 | 1.09M | if (last && status == 0 && ss->EndOfData) |
65 | 613k | *++q = '>'; |
66 | 1.09M | pr->ptr = p; |
67 | 1.09M | pw->ptr = q; |
68 | 1.09M | ss->count = pos & 31; |
69 | 1.09M | return status; |
70 | 1.09M | } |
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 | 143 | { |
85 | 143 | stream_AXD_state *const ss = (stream_AXD_state *) st; |
86 | | |
87 | 143 | return s_AXD_init_inline(ss); |
88 | 143 | } |
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 | 423k | { |
95 | 423k | stream_AXD_state *const ss = (stream_AXD_state *) st; |
96 | 423k | int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace); |
97 | | |
98 | 423k | switch (code) { |
99 | 737 | case 0: |
100 | 737 | if (ss->odd >= 0 && last) { |
101 | 27 | if (pw->ptr == pw->limit) |
102 | 0 | return 1; |
103 | 27 | *++(pw->ptr) = ss->odd << 4; |
104 | 27 | ss->odd = -1; |
105 | 27 | } |
106 | | /* falls through */ |
107 | 904 | case 1: |
108 | | /* We still need to read ahead and check for EOD. */ |
109 | 906 | for (; pr->ptr < pr->limit; pr->ptr++) |
110 | 152 | if (scan_char_decoder[pr->ptr[1]] != ctype_space) { |
111 | 150 | if (pr->ptr[1] == '>') { |
112 | 0 | pr->ptr++; |
113 | 0 | goto eod; |
114 | 0 | } |
115 | 150 | return 1; |
116 | 150 | } |
117 | 754 | return 0; /* still need to scan ahead */ |
118 | 0 | default: |
119 | 0 | return code; |
120 | 423k | case ERRC: |
121 | 423k | ; |
122 | 423k | } |
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 | 423k | if (*pr->ptr != '>') { /* EOD */ |
130 | 110 | --(pr->ptr); |
131 | 110 | return ERRC; |
132 | 110 | } |
133 | 422k | eod:if (ss->odd >= 0) { |
134 | 6.25k | if (pw->ptr == pw->limit) |
135 | 0 | return 1; |
136 | 6.25k | *++(pw->ptr) = ss->odd << 4; |
137 | 6.25k | } |
138 | 422k | return EOFC; |
139 | 422k | } |
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 | 5.05M | { |
153 | 5.05M | const byte *p = pr->ptr; |
154 | 5.05M | const byte *rlimit = pr->limit; |
155 | 5.05M | byte *q = pw->ptr; |
156 | 5.05M | byte *wlimit = pw->limit; |
157 | 5.05M | int status = 0; |
158 | | |
159 | | /* This doesn't have to be very efficient. */ |
160 | 48.2M | while (p < rlimit) { |
161 | 43.6M | int c = *++p; |
162 | | |
163 | 43.6M | if (c < 32 || c >= 127) { |
164 | 23.8M | const char *pesc; |
165 | 23.8M | const char *const esc = "\n\r\t\b\f"; |
166 | | |
167 | 23.8M | if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) { |
168 | 167k | if (wlimit - q < 2) { |
169 | 1.19k | --p; |
170 | 1.19k | status = 1; |
171 | 1.19k | break; |
172 | 1.19k | } |
173 | 165k | *++q = '\\'; |
174 | 165k | *++q = "nrtbf"[pesc - esc]; |
175 | 165k | continue; |
176 | 167k | } |
177 | 23.6M | if (wlimit - q < 4) { |
178 | 362k | --p; |
179 | 362k | status = 1; |
180 | 362k | break; |
181 | 362k | } |
182 | 23.2M | q[1] = '\\'; |
183 | 23.2M | q[2] = (c >> 6) + '0'; |
184 | 23.2M | q[3] = ((c >> 3) & 7) + '0'; |
185 | 23.2M | q[4] = (c & 7) + '0'; |
186 | 23.2M | q += 4; |
187 | 23.2M | continue; |
188 | 23.6M | } else if (c == '(' || c == ')' || c == '\\') { |
189 | 456k | if (wlimit - q < 2) { |
190 | 376 | --p; |
191 | 376 | status = 1; |
192 | 376 | break; |
193 | 376 | } |
194 | 456k | *++q = '\\'; |
195 | 19.3M | } else { |
196 | 19.3M | if (q == wlimit) { |
197 | 38.4k | --p; |
198 | 38.4k | status = 1; |
199 | 38.4k | break; |
200 | 38.4k | } |
201 | 19.3M | } |
202 | 19.7M | *++q = c; |
203 | 19.7M | } |
204 | 5.05M | if (last && status == 0) { |
205 | 3.01M | if (q == wlimit) |
206 | 572 | status = 1; |
207 | 3.01M | else |
208 | 3.01M | *++q = ')'; |
209 | 3.01M | } |
210 | 5.05M | pr->ptr = p; |
211 | 5.05M | pw->ptr = q; |
212 | 5.05M | return status; |
213 | 5.05M | } |
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 | 13.4M | { |
239 | 13.4M | stream_PSSD_state *const ss = (stream_PSSD_state *) st; |
240 | 13.4M | const byte *p = pr->ptr; |
241 | 13.4M | const byte *rlimit = pr->limit; |
242 | 13.4M | byte *q = pw->ptr; |
243 | 13.4M | byte *wlimit = pw->limit; |
244 | 13.4M | int status = 0; |
245 | 13.4M | int c; |
246 | | |
247 | 13.4M | #define check_p(n)\ |
248 | 13.4M | if ( p == rlimit ) { p -= n; goto out; } |
249 | 13.4M | #define check_q(n)\ |
250 | 202M | if ( q == wlimit ) { p -= n; status = 1; goto out; } |
251 | 216M | while (p < rlimit) { |
252 | 216M | c = *++p; |
253 | 216M | if (c == '\\' && !ss->from_string) { |
254 | 3.43M | check_p(1); |
255 | 3.43M | switch ((c = *++p)) { |
256 | 2.01M | case 'n': |
257 | 2.01M | c = '\n'; |
258 | 2.01M | goto put; |
259 | 14.6k | case 'r': |
260 | 14.6k | c = '\r'; |
261 | 14.6k | goto put; |
262 | 51.5k | case 't': |
263 | 51.5k | c = '\t'; |
264 | 51.5k | goto put; |
265 | 202 | case 'b': |
266 | 202 | c = '\b'; |
267 | 202 | goto put; |
268 | 17.1k | case 'f': |
269 | 17.1k | c = '\f'; |
270 | 17.1k | goto put; |
271 | 668k | default: /* ignore the \ */ |
272 | 2.76M | put:check_q(2); |
273 | 2.76M | *++q = c; |
274 | 2.76M | continue; |
275 | 89.2k | case char_CR: /* ignore, check for following \n */ |
276 | 89.2k | check_p(2); |
277 | 89.1k | if (p[1] == char_EOL) |
278 | 263 | p++; |
279 | 89.1k | continue; |
280 | 12.0k | case char_EOL: /* ignore */ |
281 | 12.0k | continue; |
282 | 198k | case '0': |
283 | 382k | case '1': |
284 | 382k | case '2': |
285 | 563k | case '3': |
286 | 563k | case '4': |
287 | 563k | case '5': |
288 | 565k | case '6': |
289 | 565k | case '7': |
290 | 565k | { |
291 | 565k | int d; |
292 | | |
293 | 565k | check_p(2); |
294 | 563k | d = p[1]; |
295 | 563k | c -= '0'; |
296 | 563k | if (d >= '0' && d <= '7') { |
297 | 376k | if (p + 1 == rlimit) { |
298 | 94 | p -= 2; |
299 | 94 | goto out; |
300 | 94 | } |
301 | 376k | check_q(2); |
302 | 376k | c = (c << 3) + d - '0'; |
303 | 376k | d = p[2]; |
304 | 376k | if (d >= '0' && d <= '7') { |
305 | 369k | c = (c << 3) + d - '0'; |
306 | 369k | p += 2; |
307 | 369k | } else |
308 | 6.46k | p++; |
309 | 376k | } else |
310 | 563k | check_q(2); |
311 | 563k | *++q = c; |
312 | 563k | continue; |
313 | 563k | } |
314 | 3.43M | } |
315 | 3.43M | } else |
316 | 212M | switch (c) { |
317 | 639k | case '(': |
318 | 639k | check_q(1); |
319 | 639k | ss->depth++; |
320 | 639k | break; |
321 | 13.8M | case ')': |
322 | 13.8M | if (ss->depth == 0) { |
323 | 13.3M | status = EOFC; |
324 | 13.3M | goto out; |
325 | 13.3M | } |
326 | 472k | check_q(1); |
327 | 472k | ss->depth--; |
328 | 472k | break; |
329 | 200k | case char_CR: /* convert to \n */ |
330 | 200k | check_p(1); |
331 | 200k | check_q(1); |
332 | 200k | if (p[1] == char_EOL) |
333 | 323 | p++; |
334 | 200k | *++q = '\n'; |
335 | 200k | continue; |
336 | 57.3k | case char_EOL: |
337 | 57.3k | c = '\n'; |
338 | | /* fall through */ |
339 | 198M | default: |
340 | 198M | check_q(1); |
341 | 198M | break; |
342 | 212M | } |
343 | 199M | *++q = c; |
344 | 199M | } |
345 | 6.72k | #undef check_p |
346 | 6.72k | #undef check_q |
347 | 13.4M | out:pr->ptr = p; |
348 | 13.4M | pw->ptr = q; |
349 | 13.4M | if (last && status == 0 && p != rlimit) |
350 | 57 | status = ERRC; |
351 | 13.4M | return status; |
352 | 13.4M | } |
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 | 3.72M | { |
377 | 3.72M | const byte *p = pr->ptr; |
378 | 3.72M | const byte *rlimit = pr->limit; |
379 | 3.72M | byte *q = pw->ptr; |
380 | 3.72M | byte *wlimit = pw->limit; |
381 | 3.72M | byte *q0 = q; |
382 | 3.72M | byte val1 = (byte) * odd_digit; |
383 | 3.72M | byte val2; |
384 | 3.72M | uint rcount; |
385 | 3.72M | byte *flimit; |
386 | 3.72M | const byte *const decoder = scan_char_decoder; |
387 | 3.72M | int code = 0; |
388 | | |
389 | 3.72M | if (q >= wlimit) |
390 | 227 | return 1; |
391 | 3.72M | if (val1 <= 0xf) |
392 | 4.18k | goto d2; |
393 | 3.81M | do { |
394 | | /* No digits read */ |
395 | 3.81M | if ((rcount = (rlimit - p) >> 1) != 0) |
396 | 3.81M | { |
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 | 3.81M | flimit = (rcount < wlimit - q ? q + rcount : wlimit); |
400 | 16.3M | while (1) { |
401 | 16.3M | if ((val1 = decoder[p[1]]) <= 0xf && |
402 | 16.3M | (val2 = decoder[p[2]]) <= 0xf) { |
403 | 15.8M | p += 2; |
404 | 15.8M | *++q = (val1 << 4) + val2; |
405 | 15.8M | if (q < flimit) |
406 | 12.5M | continue; |
407 | 3.28M | if (q >= wlimit) |
408 | 3.57k | goto px; |
409 | 3.28M | } |
410 | 3.81M | break; |
411 | 16.3M | } |
412 | 3.81M | } |
413 | | /* About to read the first digit */ |
414 | 4.31M | while (1) { |
415 | 4.31M | if (p >= rlimit) |
416 | 3.27M | goto end1; |
417 | 1.03M | if ((val1 = decoder[*++p]) > 0xf) { |
418 | 927k | if (val1 == ctype_space) { |
419 | 67.4k | switch (syntax) { |
420 | 39.3k | case hex_ignore_garbage: |
421 | 56.2k | case hex_ignore_whitespace: |
422 | 56.2k | continue; |
423 | 9.45k | case hex_ignore_leading_whitespace: |
424 | 9.45k | if (q == q0 && *odd_digit < 0) |
425 | 6.04k | continue; |
426 | | /* pass through */ |
427 | 5.13k | case hex_break_on_whitespace: |
428 | 5.13k | --p; |
429 | 5.13k | code = 2; |
430 | 5.13k | goto end1; |
431 | 67.4k | } |
432 | 860k | } else if (syntax == hex_ignore_garbage) |
433 | 441k | continue; |
434 | 418k | code = ERRC; |
435 | 418k | goto end1; |
436 | 927k | } |
437 | 112k | break; |
438 | 1.03M | } |
439 | 116k | d2: |
440 | | /* About to read the second hex digit of a pair */ |
441 | 543k | while (1) { |
442 | 543k | if (p >= rlimit) { |
443 | 10.0k | *odd_digit = val1; |
444 | 10.0k | goto ended; |
445 | 10.0k | } |
446 | 533k | if ((val2 = decoder[*++p]) > 0xf) { |
447 | 437k | if (val2 == ctype_space) |
448 | 34.8k | switch (syntax) { |
449 | 24.8k | case hex_ignore_garbage: |
450 | 29.9k | case hex_ignore_whitespace: |
451 | 29.9k | continue; |
452 | 3.49k | case hex_ignore_leading_whitespace: |
453 | 3.49k | if (q == q0) |
454 | 2.84k | continue; |
455 | | /* pass through */ |
456 | 2.06k | case hex_break_on_whitespace: |
457 | 2.06k | --p; |
458 | 2.06k | *odd_digit = val1; |
459 | 2.06k | code = 2; |
460 | 2.06k | goto ended; |
461 | 34.8k | } |
462 | 403k | if (syntax == hex_ignore_garbage) |
463 | 394k | continue; |
464 | 8.14k | *odd_digit = val1; |
465 | 8.14k | code = ERRC; |
466 | 8.14k | goto ended; |
467 | 403k | } |
468 | 96.1k | break; |
469 | 533k | } |
470 | 96.1k | *++q = (val1 << 4) + val2; |
471 | 96.1k | } while (q < wlimit); |
472 | 3.58k | px:code = 1; |
473 | 3.70M | end1:*odd_digit = -1; |
474 | 3.72M | ended:pr->ptr = p; |
475 | 3.72M | pw->ptr = q; |
476 | 3.72M | return code; |
477 | 3.70M | } |