/src/ghostpdl/pdf/pdf_int.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2018-2022 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., 1305 Grant Avenue - Suite 200, Novato, |
13 | | CA 94945, U.S.A., +1(415)492-9861, for further information. |
14 | | */ |
15 | | |
16 | | /* The PDF interpreter written in C */ |
17 | | |
18 | | #include "pdf_int.h" |
19 | | #include "pdf_file.h" |
20 | | #include "strmio.h" |
21 | | #include "stream.h" |
22 | | #include "pdf_misc.h" |
23 | | #include "pdf_path.h" |
24 | | #include "pdf_colour.h" |
25 | | #include "pdf_image.h" |
26 | | #include "pdf_shading.h" |
27 | | #include "pdf_font.h" |
28 | | #include "pdf_font_types.h" |
29 | | #include "pdf_cmap.h" |
30 | | #include "pdf_text.h" |
31 | | #include "pdf_gstate.h" |
32 | | #include "pdf_stack.h" |
33 | | #include "pdf_xref.h" |
34 | | #include "pdf_dict.h" |
35 | | #include "pdf_array.h" |
36 | | #include "pdf_trans.h" |
37 | | #include "pdf_optcontent.h" |
38 | | #include "pdf_sec.h" |
39 | | #include <stdlib.h> |
40 | | |
41 | | #include "gsstate.h" /* for gs_gstate_free */ |
42 | | |
43 | | /* we use -ve returns for error, 0 for success and +ve for 'take an action' */ |
44 | | /* Defining tis return so we do not need to define a new error */ |
45 | 42.3M | #define REPAIRED_KEYWORD 1 |
46 | | |
47 | | /***********************************************************************************/ |
48 | | /* 'token' reading functions. Tokens in this sense are PDF logical objects and the */ |
49 | | /* related keywords. So that's numbers, booleans, names, strings, dictionaries, */ |
50 | | /* arrays, the null object and indirect references. The keywords are obj/endobj */ |
51 | | /* stream/endstream, xref, startxref and trailer. */ |
52 | | |
53 | | /***********************************************************************************/ |
54 | | /* Some simple functions to find white space, delimiters and hex bytes */ |
55 | | static bool iswhite(char c) |
56 | 3.22G | { |
57 | 3.22G | if (c == 0x00 || c == 0x09 || c == 0x0a || c == 0x0c || c == 0x0d || c == 0x20) |
58 | 477M | return true; |
59 | 2.74G | else |
60 | 2.74G | return false; |
61 | 3.22G | } |
62 | | |
63 | | static bool isdelimiter(char c) |
64 | 1.95G | { |
65 | 1.95G | if (c == '/' || c == '(' || c == ')' || c == '[' || c == ']' || c == '<' || c == '>' || c == '{' || c == '}' || c == '%') |
66 | 44.7M | return true; |
67 | 1.90G | else |
68 | 1.90G | return false; |
69 | 1.95G | } |
70 | | |
71 | | static bool ishex(char c) |
72 | 131M | { |
73 | 131M | if (c < 0x30) |
74 | 191k | return false; |
75 | | |
76 | 131M | if (c > 0x39) { |
77 | 24.8M | if (c > 'F') { |
78 | 22.9M | if (c < 'a') |
79 | 31.0k | return false; |
80 | 22.9M | if (c > 'f') |
81 | 119k | return false; |
82 | 22.8M | return true; |
83 | 22.9M | } else { |
84 | 1.88M | if (c < 'A') |
85 | 16.8k | return false; |
86 | 1.86M | return true; |
87 | 1.88M | } |
88 | 24.8M | } else |
89 | 106M | return true; |
90 | 131M | } |
91 | | |
92 | | /* You must ensure the character is a hex character before calling this, no error trapping here */ |
93 | | static int fromhex(char c) |
94 | 130M | { |
95 | 130M | if (c > 0x39) { |
96 | 24.5M | if (c > 'F') { |
97 | 22.7M | return c - 0x57; |
98 | 22.7M | } else { |
99 | 1.78M | return c - 0x37; |
100 | 1.78M | } |
101 | 24.5M | } else |
102 | 105M | return c - 0x30; |
103 | 130M | } |
104 | | |
105 | | /* The 'read' functions all return the newly created object on the context's stack |
106 | | * which means these objects are created with a reference count of 0, and only when |
107 | | * pushed onto the stack does the reference count become 1, indicating the stack is |
108 | | * the only reference. |
109 | | */ |
110 | | int pdfi_skip_white(pdf_context *ctx, pdf_c_stream *s) |
111 | 714M | { |
112 | 714M | int c; |
113 | | |
114 | 822M | do { |
115 | 822M | c = pdfi_read_byte(ctx, s); |
116 | 822M | if (c < 0) |
117 | 200k | return 0; |
118 | 822M | } while (iswhite(c)); |
119 | | |
120 | 714M | pdfi_unread_byte(ctx, s, (byte)c); |
121 | 714M | return 0; |
122 | 714M | } |
123 | | |
124 | | int pdfi_skip_eol(pdf_context *ctx, pdf_c_stream *s) |
125 | 1.68M | { |
126 | 1.68M | int c; |
127 | | |
128 | 1.97M | do { |
129 | 1.97M | c = pdfi_read_byte(ctx, s); |
130 | 1.97M | if (c < 0 || c == 0x0a) |
131 | 416k | return 0; |
132 | 1.97M | } while (c != 0x0d); |
133 | 1.26M | c = pdfi_read_byte(ctx, s); |
134 | 1.26M | if (c == 0x0a) |
135 | 1.26M | return 0; |
136 | 2.53k | if (c >= 0) |
137 | 2.49k | pdfi_unread_byte(ctx, s, (byte)c); |
138 | 2.53k | pdfi_set_warning(ctx, 0, NULL, W_PDF_STREAM_BAD_KEYWORD, "pdfi_skip_eol", NULL); |
139 | 2.53k | return 0; |
140 | 1.26M | } |
141 | | |
142 | | /* Fast(ish) but inaccurate strtof, with Adobe overflow handling, |
143 | | * lifted from MuPDF. */ |
144 | | static float acrobat_compatible_atof(char *s) |
145 | 107M | { |
146 | 107M | int neg = 0; |
147 | 107M | int i = 0; |
148 | | |
149 | 111M | while (*s == '-') { |
150 | 3.92M | neg = 1; |
151 | 3.92M | ++s; |
152 | 3.92M | } |
153 | 107M | while (*s == '+') { |
154 | 1 | ++s; |
155 | 1 | } |
156 | | |
157 | 399M | while (*s >= '0' && *s <= '9') { |
158 | | /* We deliberately ignore overflow here. |
159 | | * Tests show that Acrobat handles * overflows in exactly the same way we do: |
160 | | * 123450000000000000000678 is read as 678. |
161 | | */ |
162 | 292M | i = i * 10 + (*s - '0'); |
163 | 292M | ++s; |
164 | 292M | } |
165 | | |
166 | 107M | if (*s == '.') { |
167 | 107M | float MAX = (MAX_FLOAT-9)/10; |
168 | 107M | float v = (float)i; |
169 | 107M | float n = 0; |
170 | 107M | float d = 1; |
171 | 107M | ++s; |
172 | | /* Bug 705211: Ensure that we don't overflow n here - just ignore any |
173 | | * trailing digits after this. This will be plenty accurate enough. */ |
174 | 435M | while (*s >= '0' && *s <= '9' && n <= MAX) { |
175 | 328M | n = 10 * n + (*s - '0'); |
176 | 328M | d = 10 * d; |
177 | 328M | ++s; |
178 | 328M | } |
179 | 107M | v += n / d; |
180 | 107M | return neg ? -v : v; |
181 | 107M | } else { |
182 | 14.6k | return (float)(neg ? -i : i); |
183 | 14.6k | } |
184 | 107M | } |
185 | | |
186 | | int pdfi_read_bare_int(pdf_context *ctx, pdf_c_stream *s, int *parsed_int) |
187 | 113M | { |
188 | 113M | int index = 0; |
189 | 113M | int int_val = 0; |
190 | 113M | int negative = 0; |
191 | | |
192 | 113M | restart: |
193 | 113M | pdfi_skip_white(ctx, s); |
194 | | |
195 | 494M | do { |
196 | 494M | int c = pdfi_read_byte(ctx, s); |
197 | 494M | if (c == EOFC) |
198 | 1.63k | break; |
199 | | |
200 | 494M | if (c < 0) |
201 | 3.26k | return_error(gs_error_ioerror); |
202 | | |
203 | 494M | if (iswhite(c)) { |
204 | 113M | break; |
205 | 380M | } else if (c == '%' && index == 0) { |
206 | 14.2k | pdfi_skip_comment(ctx, s); |
207 | 14.2k | goto restart; |
208 | 380M | } else if (isdelimiter(c)) { |
209 | 3.95k | pdfi_unread_byte(ctx, s, (byte)c); |
210 | 3.95k | break; |
211 | 3.95k | } |
212 | | |
213 | 380M | if (c >= '0' && c <= '9') { |
214 | 380M | int_val = int_val*10 + c - '0'; |
215 | 380M | } else if (c == '.') { |
216 | 1.05k | goto error; |
217 | 85.2k | } else if (c == 'e' || c == 'E') { |
218 | 873 | pdfi_set_warning(ctx, 0, NULL, W_PDF_NUM_EXPONENT, "pdfi_read_num", NULL); |
219 | 873 | goto error; |
220 | 84.3k | } else if (c == '-') { |
221 | | /* Any - sign not at the start of the string indicates a malformed number. */ |
222 | 281 | if (index != 0 || negative) { |
223 | 60 | pdfi_set_error(ctx, 0, NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", NULL); |
224 | 60 | if (ctx->args.pdfstoponerror) |
225 | 0 | return_error(gs_error_syntaxerror); |
226 | 60 | goto error; |
227 | 60 | } |
228 | 221 | negative = 1; |
229 | 84.0k | } else if (c == '+') { |
230 | 51.4k | if (index == 0) { |
231 | | /* Just drop the + it's pointless, and it'll get in the way |
232 | | * of our negation handling for floats. */ |
233 | 51.4k | continue; |
234 | 51.4k | } else { |
235 | 19 | pdfi_set_error(ctx, 0, NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", NULL); |
236 | 19 | if (ctx->args.pdfstoponerror) |
237 | 0 | return_error(gs_error_syntaxerror); |
238 | 19 | goto error; |
239 | 19 | } |
240 | 51.4k | } else { |
241 | 32.5k | if (index > 0) { |
242 | 3.32k | pdfi_set_error(ctx, 0, NULL, E_PDF_MISSINGWHITESPACE, "pdfi_read_num", (char *)"Ignoring missing white space while parsing number"); |
243 | 3.32k | if (ctx->args.pdfstoponerror) |
244 | 0 | return_error(gs_error_syntaxerror); |
245 | 3.32k | } |
246 | 32.5k | pdfi_unread_byte(ctx, s, (byte)c); |
247 | 32.5k | goto error; |
248 | 32.5k | } |
249 | 380M | if (++index > 255) |
250 | 29 | return_error(gs_error_syntaxerror); |
251 | 380M | } while(1); |
252 | | |
253 | 113M | *parsed_int = negative ? -int_val : int_val; |
254 | 113M | if (ctx->args.pdfdebug) |
255 | 113M | dmprintf1(ctx->memory, " %d", *parsed_int); |
256 | 113M | return (index > 0); |
257 | | |
258 | 34.5k | error: |
259 | 34.5k | *parsed_int = 0; |
260 | 34.5k | return_error(gs_error_syntaxerror); |
261 | 113M | } |
262 | | |
263 | | static int pdfi_read_num(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen) |
264 | 190M | { |
265 | 190M | byte Buffer[256]; |
266 | 190M | unsigned short index = 0; |
267 | 190M | bool real = false; |
268 | 190M | bool has_decimal_point = false; |
269 | 190M | bool has_exponent = false; |
270 | 190M | unsigned short exponent_index = 0; |
271 | 190M | pdf_num *num; |
272 | 190M | int code = 0, malformed = false, doubleneg = false, recovered = false, negative = false, overflowed = false; |
273 | 190M | int int_val = 0, tenth_max_int = max_int / 10; |
274 | | |
275 | 190M | pdfi_skip_white(ctx, s); |
276 | | |
277 | 1.17G | do { |
278 | 1.17G | int c = pdfi_read_byte(ctx, s); |
279 | 1.17G | if (c == EOFC) { |
280 | 4.13k | Buffer[index] = 0x00; |
281 | 4.13k | break; |
282 | 4.13k | } |
283 | | |
284 | 1.17G | if (c < 0) |
285 | 2.15k | return_error(gs_error_ioerror); |
286 | | |
287 | 1.17G | if (iswhite(c)) { |
288 | 169M | Buffer[index] = 0x00; |
289 | 169M | break; |
290 | 1.00G | } else if (isdelimiter(c)) { |
291 | 18.9M | pdfi_unread_byte(ctx, s, (byte)c); |
292 | 18.9M | Buffer[index] = 0x00; |
293 | 18.9M | break; |
294 | 18.9M | } |
295 | 982M | Buffer[index] = (byte)c; |
296 | | |
297 | 982M | if (c >= '0' && c <= '9') { |
298 | 860M | if (!(malformed && recovered) && !overflowed) { |
299 | 834M | if (int_val < tenth_max_int) |
300 | 832M | int_val = int_val*10 + c - '0'; |
301 | 1.93M | else { |
302 | 1.93M | pdfi_set_error(ctx, 0, NULL, E_PDF_NUMBEROVERFLOW, "pdfi_read_num", NULL); |
303 | 1.93M | overflowed = true; |
304 | 1.93M | } |
305 | 834M | } |
306 | 860M | } else if (c == '.') { |
307 | 110M | if (has_decimal_point == true) { |
308 | 1.77M | if (ctx->args.pdfstoponerror) |
309 | 0 | return_error(gs_error_syntaxerror); |
310 | 1.77M | malformed = true; |
311 | 108M | } else { |
312 | 108M | has_decimal_point = true; |
313 | 108M | real = true; |
314 | 108M | } |
315 | 110M | } else if (c == 'e' || c == 'E') { |
316 | | /* TODO: technically scientific notation isn't in PDF spec, |
317 | | * but gs seems to accept it, so we should also? |
318 | | */ |
319 | 73.0k | if (has_exponent == true) { |
320 | 9.36k | if (ctx->args.pdfstoponerror) |
321 | 0 | return_error(gs_error_syntaxerror); |
322 | 9.36k | malformed = true; |
323 | 63.6k | } else { |
324 | 63.6k | pdfi_set_warning(ctx, 0, NULL, W_PDF_NUM_EXPONENT, "pdfi_read_num", NULL); |
325 | 63.6k | has_exponent = true; |
326 | 63.6k | exponent_index = index; |
327 | 63.6k | real = true; |
328 | 63.6k | } |
329 | 11.5M | } else if (c == '-') { |
330 | | /* Any - sign not at the start of the string, or just after an exponent |
331 | | * indicates a malformed number. */ |
332 | 9.76M | if (!(index == 0 || (has_exponent && index == exponent_index+1))) { |
333 | 3.14M | pdfi_set_error(ctx, 0, NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", NULL); |
334 | 3.14M | if (ctx->args.pdfstoponerror) |
335 | 0 | return_error(gs_error_syntaxerror); |
336 | 3.14M | if (Buffer[index - 1] != '-') { |
337 | | /* We are parsing a number line 123-56. We should continue parsing, but |
338 | | * ignore anything from the second -. */ |
339 | 162k | malformed = true; |
340 | 162k | Buffer[index] = 0; |
341 | 162k | recovered = true; |
342 | 162k | } |
343 | 3.14M | } |
344 | 9.76M | if (!has_exponent && !(malformed && recovered)) { |
345 | 9.59M | doubleneg = negative; |
346 | 9.59M | negative = 1; |
347 | 9.59M | } |
348 | 9.76M | } else if (c == '+') { |
349 | 124k | if (index == 0 || (has_exponent && index == exponent_index+1)) { |
350 | | /* Just drop the + it's pointless, and it'll get in the way |
351 | | * of our negation handling for floats. */ |
352 | 119k | index--; |
353 | 119k | } else { |
354 | 4.39k | pdfi_set_error(ctx, 0, NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", NULL); |
355 | 4.39k | if (ctx->args.pdfstoponerror) |
356 | 0 | return_error(gs_error_syntaxerror); |
357 | 4.39k | if (Buffer[index - 1] != '-') { |
358 | | /* We are parsing a number line 123-56. We should continue parsing, but |
359 | | * ignore anything from the second -. */ |
360 | 4.31k | malformed = true; |
361 | 4.31k | Buffer[index] = 0; |
362 | 4.31k | recovered = true; |
363 | 4.31k | } |
364 | 4.39k | } |
365 | 1.67M | } else { |
366 | 1.67M | pdfi_set_error(ctx, 0, NULL, E_PDF_MISSINGWHITESPACE, "pdfi_read_num", (char *)"Ignoring missing white space while parsing number"); |
367 | 1.67M | if (ctx->args.pdfstoponerror) |
368 | 0 | return_error(gs_error_syntaxerror); |
369 | 1.67M | pdfi_unread_byte(ctx, s, (byte)c); |
370 | 1.67M | Buffer[index] = 0x00; |
371 | 1.67M | break; |
372 | 1.67M | } |
373 | 981M | if (++index > 255) |
374 | 39.6k | return_error(gs_error_syntaxerror); |
375 | 981M | } while(1); |
376 | | |
377 | 190M | if (real && (!malformed || (malformed && recovered))) |
378 | 107M | code = pdfi_object_alloc(ctx, PDF_REAL, 0, (pdf_obj **)&num); |
379 | 82.5M | else |
380 | 82.5M | code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&num); |
381 | 190M | if (code < 0) |
382 | 0 | return code; |
383 | | |
384 | 190M | if ((malformed && !recovered) || (!real && doubleneg)) { |
385 | 924k | pdfi_set_error_var(ctx, 0, NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", "Treating malformed number %s as 0", Buffer); |
386 | 924k | num->value.i = 0; |
387 | 189M | } else if (has_exponent) { |
388 | 52.7k | float f, exp; |
389 | 52.7k | char *p = strstr((const char *)Buffer, "e"); |
390 | | |
391 | 52.7k | if (p == NULL) |
392 | 7.95k | p = strstr((const char *)Buffer, "E"); |
393 | | |
394 | 52.7k | if (p == NULL) { |
395 | 640 | pdfi_set_error_var(ctx, 0, NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", "Treating malformed float %s as 0", Buffer); |
396 | 640 | num->value.d = 0; |
397 | 52.1k | } else { |
398 | 52.1k | p++; |
399 | | |
400 | 52.1k | if (sscanf((char *)p, "%g", &exp) != 1 || exp > 38) { |
401 | 44.7k | pdfi_set_error_var(ctx, 0, NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", "Treating malformed float %s as 0", Buffer); |
402 | 44.7k | num->value.d = 0; |
403 | 44.7k | } else { |
404 | 7.37k | if (sscanf((char *)Buffer, "%g", &f) == 1) { |
405 | 7.26k | num->value.d = f; |
406 | 7.26k | } else { |
407 | 110 | pdfi_set_error_var(ctx, 0, NULL, E_PDF_MALFORMEDNUMBER, "pdfi_read_num", "Treating malformed float %s as 0", Buffer); |
408 | 110 | num->value.d = 0; |
409 | 110 | } |
410 | 7.37k | } |
411 | 52.1k | } |
412 | 189M | } else if (real) { |
413 | 107M | num->value.d = acrobat_compatible_atof((char *)Buffer); |
414 | 107M | } else { |
415 | | /* The doubleneg case is taken care of above. */ |
416 | 81.6M | num->value.i = negative ? -int_val : int_val; |
417 | 81.6M | } |
418 | 190M | if (ctx->args.pdfdebug) { |
419 | 0 | if (real) |
420 | 0 | dmprintf1(ctx->memory, " %f", num->value.d); |
421 | 0 | else |
422 | 0 | dmprintf1(ctx->memory, " %"PRIi64, num->value.i); |
423 | 0 | } |
424 | 190M | num->indirect_num = indirect_num; |
425 | 190M | num->indirect_gen = indirect_gen; |
426 | | |
427 | 190M | code = pdfi_push(ctx, (pdf_obj *)num); |
428 | | |
429 | 190M | if (code < 0) |
430 | 947 | pdfi_free_object((pdf_obj *)num); |
431 | | |
432 | 190M | return code; |
433 | 190M | } |
434 | | |
435 | | static int pdfi_read_name(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen) |
436 | 47.8M | { |
437 | 47.8M | char *Buffer, *NewBuf = NULL; |
438 | 47.8M | unsigned short index = 0; |
439 | 47.8M | short bytes = 0; |
440 | 47.8M | uint32_t size = 256; |
441 | 47.8M | pdf_name *name = NULL; |
442 | 47.8M | int code; |
443 | | |
444 | 47.8M | Buffer = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_read_name"); |
445 | 47.8M | if (Buffer == NULL) |
446 | 0 | return_error(gs_error_VMerror); |
447 | | |
448 | 323M | do { |
449 | 323M | int c = pdfi_read_byte(ctx, s); |
450 | 323M | if (c < 0) |
451 | 3.51k | break; |
452 | | |
453 | 323M | if (iswhite((char)c)) { |
454 | 27.2M | Buffer[index] = 0x00; |
455 | 27.2M | break; |
456 | 296M | } else if (isdelimiter((char)c)) { |
457 | 20.6M | pdfi_unread_byte(ctx, s, (char)c); |
458 | 20.6M | Buffer[index] = 0x00; |
459 | 20.6M | break; |
460 | 20.6M | } |
461 | 275M | Buffer[index] = (char)c; |
462 | | |
463 | | /* Check for and convert escaped name characters */ |
464 | 275M | if (c == '#') { |
465 | 114k | byte NumBuf[2]; |
466 | | |
467 | 114k | bytes = pdfi_read_bytes(ctx, (byte *)&NumBuf, 1, 2, s); |
468 | 114k | if (bytes < 2 || (!ishex(NumBuf[0]) || !ishex(NumBuf[1]))) { |
469 | 59.7k | pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_NAME_ESCAPE, "pdfi_read_name", NULL); |
470 | 59.7k | pdfi_unread(ctx, s, (byte *)NumBuf, bytes); |
471 | | /* This leaves the name buffer with a # in it, rather than anything sane! */ |
472 | 59.7k | } |
473 | 55.0k | else |
474 | 55.0k | Buffer[index] = (fromhex(NumBuf[0]) << 4) + fromhex(NumBuf[1]); |
475 | 114k | } |
476 | | |
477 | | /* If we ran out of memory, increase the buffer size */ |
478 | 275M | if (index++ >= size - 1) { |
479 | 18.6k | NewBuf = (char *)gs_alloc_bytes(ctx->memory, size + 256, "pdfi_read_name"); |
480 | 18.6k | if (NewBuf == NULL) { |
481 | 0 | gs_free_object(ctx->memory, Buffer, "pdfi_read_name error"); |
482 | 0 | return_error(gs_error_VMerror); |
483 | 0 | } |
484 | 18.6k | memcpy(NewBuf, Buffer, size); |
485 | 18.6k | gs_free_object(ctx->memory, Buffer, "pdfi_read_name"); |
486 | 18.6k | Buffer = NewBuf; |
487 | 18.6k | size += 256; |
488 | 18.6k | } |
489 | 275M | } while(1); |
490 | | |
491 | 47.8M | code = pdfi_object_alloc(ctx, PDF_NAME, index, (pdf_obj **)&name); |
492 | 47.8M | if (code < 0) { |
493 | 0 | gs_free_object(ctx->memory, Buffer, "pdfi_read_name error"); |
494 | 0 | return code; |
495 | 0 | } |
496 | 47.8M | memcpy(name->data, Buffer, index); |
497 | 47.8M | name->indirect_num = indirect_num; |
498 | 47.8M | name->indirect_gen = indirect_gen; |
499 | | |
500 | 47.8M | if (ctx->args.pdfdebug) |
501 | 47.8M | dmprintf1(ctx->memory, " /%s", Buffer); |
502 | | |
503 | 47.8M | gs_free_object(ctx->memory, Buffer, "pdfi_read_name"); |
504 | | |
505 | 47.8M | code = pdfi_push(ctx, (pdf_obj *)name); |
506 | | |
507 | 47.8M | if (code < 0) |
508 | 0 | pdfi_free_object((pdf_obj *)name); |
509 | | |
510 | 47.8M | return code; |
511 | 47.8M | } |
512 | | |
513 | | static int pdfi_read_hexstring(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen) |
514 | 716k | { |
515 | 716k | char *Buffer, *NewBuf = NULL; |
516 | 716k | unsigned short index = 0; |
517 | 716k | uint32_t size = 256; |
518 | 716k | pdf_string *string = NULL; |
519 | 716k | int code, hex0, hex1; |
520 | | |
521 | 716k | Buffer = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_read_hexstring"); |
522 | 716k | if (Buffer == NULL) |
523 | 0 | return_error(gs_error_VMerror); |
524 | | |
525 | 716k | if (ctx->args.pdfdebug) |
526 | 716k | dmprintf(ctx->memory, " <"); |
527 | | |
528 | 65.7M | do { |
529 | 65.8M | do { |
530 | 65.8M | hex0 = pdfi_read_byte(ctx, s); |
531 | 65.8M | if (hex0 < 0) |
532 | 250 | break; |
533 | 65.8M | } while(iswhite(hex0)); |
534 | 65.7M | if (hex0 < 0) |
535 | 250 | break; |
536 | | |
537 | 65.7M | if (hex0 == '>') |
538 | 644k | break; |
539 | | |
540 | 65.1M | if (ctx->args.pdfdebug) |
541 | 65.1M | dmprintf1(ctx->memory, "%c", (char)hex0); |
542 | | |
543 | 65.1M | do { |
544 | 65.1M | hex1 = pdfi_read_byte(ctx, s); |
545 | 65.1M | if (hex1 < 0) |
546 | 237 | break; |
547 | 65.1M | } while(iswhite(hex1)); |
548 | 65.1M | if (hex1 < 0) |
549 | 237 | break; |
550 | | |
551 | 65.1M | if (hex1 == '>') { |
552 | | /* PDF Reference 1.7 page 56: |
553 | | * "If the final digit of a hexadecimal string is missing that is, |
554 | | * if there is an odd number of digits the final digit is assumed to be 0." |
555 | | */ |
556 | 6.05k | hex1 = 0x30; |
557 | 6.05k | if (!ishex(hex0) || !ishex(hex1)) { |
558 | 345 | code = gs_note_error(gs_error_syntaxerror); |
559 | 345 | goto exit; |
560 | 345 | } |
561 | 5.70k | Buffer[index] = (fromhex(hex0) << 4) + fromhex(hex1); |
562 | 5.70k | if (ctx->args.pdfdebug) |
563 | 5.70k | dmprintf1(ctx->memory, "%c", hex1); |
564 | 5.70k | break; |
565 | 6.05k | } |
566 | | |
567 | 65.1M | if (!ishex(hex0) || !ishex(hex1)) { |
568 | 64.5k | code = gs_note_error(gs_error_syntaxerror); |
569 | 64.5k | goto exit; |
570 | 64.5k | } |
571 | | |
572 | 65.0M | if (ctx->args.pdfdebug) |
573 | 65.0M | dmprintf1(ctx->memory, "%c", (char)hex1); |
574 | | |
575 | 65.0M | Buffer[index] = (fromhex(hex0) << 4) + fromhex(hex1); |
576 | | |
577 | 65.0M | if (index++ >= size - 1) { |
578 | 232k | NewBuf = (char *)gs_alloc_bytes(ctx->memory, size + 256, "pdfi_read_hexstring"); |
579 | 232k | if (NewBuf == NULL) { |
580 | 0 | code = gs_note_error(gs_error_VMerror); |
581 | 0 | goto exit; |
582 | 0 | } |
583 | 232k | memcpy(NewBuf, Buffer, size); |
584 | 232k | gs_free_object(ctx->memory, Buffer, "pdfi_read_hexstring"); |
585 | 232k | Buffer = NewBuf; |
586 | 232k | size += 256; |
587 | 232k | } |
588 | 65.0M | } while(1); |
589 | | |
590 | 651k | if (ctx->args.pdfdebug) |
591 | 651k | dmprintf(ctx->memory, ">"); |
592 | | |
593 | 651k | code = pdfi_object_alloc(ctx, PDF_STRING, index, (pdf_obj **)&string); |
594 | 651k | if (code < 0) |
595 | 0 | goto exit; |
596 | 651k | memcpy(string->data, Buffer, index); |
597 | 651k | string->indirect_num = indirect_num; |
598 | 651k | string->indirect_gen = indirect_gen; |
599 | | |
600 | 651k | if (ctx->encryption.is_encrypted && ctx->encryption.decrypt_strings) { |
601 | 340 | code = pdfi_decrypt_string(ctx, string); |
602 | 340 | if (code < 0) |
603 | 0 | return code; |
604 | 340 | } |
605 | | |
606 | 651k | code = pdfi_push(ctx, (pdf_obj *)string); |
607 | 651k | if (code < 0) |
608 | 0 | pdfi_free_object((pdf_obj *)string); |
609 | | |
610 | 716k | exit: |
611 | 716k | gs_free_object(ctx->memory, Buffer, "pdfi_read_hexstring"); |
612 | 716k | return code; |
613 | 651k | } |
614 | | |
615 | | static int pdfi_read_string(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen) |
616 | 8.67M | { |
617 | 8.67M | char *Buffer, *NewBuf = NULL; |
618 | 8.67M | unsigned short index = 0; |
619 | 8.67M | uint32_t size = 256; |
620 | 8.67M | pdf_string *string = NULL; |
621 | 8.67M | int c, code, nesting = 0; |
622 | 8.67M | bool escape = false, skip_eol = false, exit_loop = false; |
623 | | |
624 | 8.67M | Buffer = (char *)gs_alloc_bytes(ctx->memory, size, "pdfi_read_string"); |
625 | 8.67M | if (Buffer == NULL) |
626 | 0 | return_error(gs_error_VMerror); |
627 | | |
628 | 357M | do { |
629 | 357M | if (index >= size - 1) { |
630 | 767k | NewBuf = (char *)gs_alloc_bytes(ctx->memory, size + 256, "pdfi_read_string"); |
631 | 767k | if (NewBuf == NULL) { |
632 | 0 | gs_free_object(ctx->memory, Buffer, "pdfi_read_string error"); |
633 | 0 | return_error(gs_error_VMerror); |
634 | 0 | } |
635 | 767k | memcpy(NewBuf, Buffer, size); |
636 | 767k | gs_free_object(ctx->memory, Buffer, "pdfi_read_string"); |
637 | 767k | Buffer = NewBuf; |
638 | 767k | size += 256; |
639 | 767k | } |
640 | | |
641 | 357M | c = pdfi_read_byte(ctx, s); |
642 | | |
643 | 357M | if (c < 0) { |
644 | 10.3k | if (nesting > 0) |
645 | 5.53k | pdfi_set_error(ctx, 0, NULL, E_PDF_UNESCAPEDSTRING, "pdfi_read_string", NULL); |
646 | 10.3k | Buffer[index] = 0x00; |
647 | 10.3k | break; |
648 | 10.3k | } |
649 | | |
650 | 357M | if (skip_eol) { |
651 | 4.00M | if (c == 0x0a || c == 0x0d) |
652 | 666k | continue; |
653 | 3.33M | skip_eol = false; |
654 | 3.33M | } |
655 | 356M | Buffer[index] = (char)c; |
656 | | |
657 | 356M | if (escape) { |
658 | 1.45M | escape = false; |
659 | 1.45M | switch (Buffer[index]) { |
660 | 2.71k | case 0x0a: |
661 | 48.0k | case 0x0d: |
662 | 48.0k | skip_eol = true; |
663 | 48.0k | continue; |
664 | 16.4k | case 'n': |
665 | 16.4k | Buffer[index] = 0x0a; |
666 | 16.4k | break; |
667 | 21.2k | case 'r': |
668 | 21.2k | Buffer[index] = 0x0d; |
669 | 21.2k | break; |
670 | 6.19k | case 't': |
671 | 6.19k | Buffer[index] = 0x09; |
672 | 6.19k | break; |
673 | 8.15k | case 'b': |
674 | 8.15k | Buffer[index] = 0x08; |
675 | 8.15k | break; |
676 | 6.51k | case 'f': |
677 | 6.51k | Buffer[index] = 0x0c; |
678 | 6.51k | break; |
679 | 59.7k | case '(': |
680 | 118k | case ')': |
681 | 202k | case '\\': |
682 | 202k | break; |
683 | 282k | case '0': |
684 | 289k | case '1': |
685 | 396k | case '2': |
686 | 484k | case '3': |
687 | 488k | case '4': |
688 | 493k | case '5': |
689 | 497k | case '6': |
690 | 499k | case '7': |
691 | 499k | { |
692 | | /* Octal chars can be 1, 2 or 3 chars in length, terminated either |
693 | | * by being 3 chars long, EOFC, or a non-octal char. We do not allow |
694 | | * line breaks in the middle of octal chars. */ |
695 | 499k | int c1 = pdfi_read_byte(ctx, s); |
696 | 499k | c -= '0'; |
697 | 499k | if (c1 < 0) { |
698 | | /* Nothing to do, or unread */ |
699 | 499k | } else if (c1 < '0' || c1 > '7') { |
700 | 32.0k | pdfi_unread_byte(ctx, s, (char)c1); |
701 | 467k | } else { |
702 | 467k | c = c*8 + c1 - '0'; |
703 | 467k | c1 = pdfi_read_byte(ctx, s); |
704 | 467k | if (c1 < 0) { |
705 | | /* Nothing to do, or unread */ |
706 | 467k | } else if (c1 < '0' || c1 > '7') { |
707 | 7.18k | pdfi_unread_byte(ctx, s, (char)c1); |
708 | 7.18k | } else |
709 | 460k | c = c*8 + c1 - '0'; |
710 | 467k | } |
711 | 499k | Buffer[index] = c; |
712 | 499k | break; |
713 | 497k | } |
714 | 646k | default: |
715 | | /* PDF Reference, literal strings, if the character following a |
716 | | * escape \ character is not recognised, then it is ignored. |
717 | | */ |
718 | 646k | escape = false; |
719 | 646k | index++; |
720 | 646k | continue; |
721 | 1.45M | } |
722 | 355M | } else { |
723 | 355M | switch(Buffer[index]) { |
724 | 1.37M | case 0x0d: |
725 | 1.37M | Buffer[index] = 0x0a; |
726 | | /*fallthrough*/ |
727 | 3.29M | case 0x0a: |
728 | 3.29M | skip_eol = true; |
729 | 3.29M | break; |
730 | 9.63M | case ')': |
731 | 9.63M | if (nesting == 0) { |
732 | 8.66M | Buffer[index] = 0x00; |
733 | 8.66M | exit_loop = true; |
734 | 8.66M | } else |
735 | 970k | nesting--; |
736 | 9.63M | break; |
737 | 1.45M | case '\\': |
738 | 1.45M | escape = true; |
739 | 1.45M | continue; |
740 | 1.07M | case '(': |
741 | 1.07M | nesting++; |
742 | 1.07M | break; |
743 | 339M | default: |
744 | 339M | break; |
745 | 355M | } |
746 | 355M | } |
747 | | |
748 | 354M | if (exit_loop) |
749 | 8.66M | break; |
750 | | |
751 | 346M | index++; |
752 | 348M | } while(1); |
753 | | |
754 | 8.67M | code = pdfi_object_alloc(ctx, PDF_STRING, index, (pdf_obj **)&string); |
755 | 8.67M | if (code < 0) { |
756 | 0 | gs_free_object(ctx->memory, Buffer, "pdfi_read_name error"); |
757 | 0 | return code; |
758 | 0 | } |
759 | 8.67M | memcpy(string->data, Buffer, index); |
760 | 8.67M | string->indirect_num = indirect_num; |
761 | 8.67M | string->indirect_gen = indirect_gen; |
762 | | |
763 | 8.67M | gs_free_object(ctx->memory, Buffer, "pdfi_read_string"); |
764 | | |
765 | 8.67M | if (ctx->encryption.is_encrypted && ctx->encryption.decrypt_strings) { |
766 | 3.08k | code = pdfi_decrypt_string(ctx, string); |
767 | 3.08k | if (code < 0) |
768 | 0 | return code; |
769 | 3.08k | } |
770 | | |
771 | 8.67M | if (ctx->args.pdfdebug) { |
772 | 0 | int i; |
773 | 0 | dmprintf(ctx->memory, " ("); |
774 | 0 | for (i=0;i<string->length;i++) |
775 | 0 | dmprintf1(ctx->memory, "%c", string->data[i]); |
776 | 0 | dmprintf(ctx->memory, ")"); |
777 | 0 | } |
778 | | |
779 | 8.67M | code = pdfi_push(ctx, (pdf_obj *)string); |
780 | 8.67M | if (code < 0) { |
781 | 0 | pdfi_free_object((pdf_obj *)string); |
782 | 0 | pdfi_set_error(ctx, code, NULL, 0, "pdfi_read_string", NULL); |
783 | 0 | } |
784 | | |
785 | 8.67M | return code; |
786 | 8.67M | } |
787 | | |
788 | | int pdfi_read_dict(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen) |
789 | 3.87k | { |
790 | 3.87k | int code, depth; |
791 | | |
792 | 3.87k | code = pdfi_read_token(ctx, s, indirect_num, indirect_gen); |
793 | 3.87k | if (code < 0) |
794 | 3 | return code; |
795 | 3.86k | if (code == 0) |
796 | 0 | return_error(gs_error_syntaxerror); |
797 | | |
798 | 3.86k | if (pdfi_type_of(ctx->stack_top[-1]) != PDF_DICT_MARK) |
799 | 8 | return_error(gs_error_typecheck); |
800 | 3.86k | depth = pdfi_count_stack(ctx); |
801 | | |
802 | 50.6k | do { |
803 | 50.6k | code = pdfi_read_token(ctx, s, indirect_num, indirect_gen); |
804 | 50.6k | if (code < 0) |
805 | 50 | return code; |
806 | 50.6k | if (code == 0) |
807 | 5 | return_error(gs_error_syntaxerror); |
808 | 50.6k | } while(pdfi_count_stack(ctx) > depth); |
809 | 3.80k | return 0; |
810 | 3.86k | } |
811 | | |
812 | | int pdfi_skip_comment(pdf_context *ctx, pdf_c_stream *s) |
813 | 1.03M | { |
814 | 1.03M | int c; |
815 | | |
816 | 1.03M | if (ctx->args.pdfdebug) |
817 | 1.03M | dmprintf (ctx->memory, " %%"); |
818 | | |
819 | 30.5M | do { |
820 | 30.5M | c = pdfi_read_byte(ctx, s); |
821 | 30.5M | if (c < 0) |
822 | 2.26k | break; |
823 | | |
824 | 30.5M | if (ctx->args.pdfdebug) |
825 | 30.5M | dmprintf1 (ctx->memory, " %c", (char)c); |
826 | | |
827 | 30.5M | } while (c != 0x0a && c != 0x0d); |
828 | | |
829 | 0 | return 0; |
830 | 1.03M | } |
831 | | |
832 | | #define PARAM1(A) # A, |
833 | | #define PARAM2(A,B) A, |
834 | | static const char pdf_token_strings[][10] = { |
835 | | #include "pdf_tokens.h" |
836 | | }; |
837 | | |
838 | 64.4M | #define nelems(A) (sizeof(A)/sizeof(A[0])) |
839 | | |
840 | | typedef int (*bsearch_comparator)(const void *, const void *); |
841 | | |
842 | | int pdfi_read_bare_keyword(pdf_context *ctx, pdf_c_stream *s) |
843 | 2.17M | { |
844 | 2.17M | byte Buffer[256]; |
845 | 2.17M | int index = 0; |
846 | 2.17M | int c; |
847 | 2.17M | void *t; |
848 | | |
849 | 2.17M | pdfi_skip_white(ctx, s); |
850 | | |
851 | 14.0M | do { |
852 | 14.0M | c = pdfi_read_byte(ctx, s); |
853 | 14.0M | if (c < 0) |
854 | 13.3k | break; |
855 | | |
856 | 14.0M | if (iswhite(c) || isdelimiter(c)) { |
857 | 2.16M | pdfi_unread_byte(ctx, s, (byte)c); |
858 | 2.16M | break; |
859 | 2.16M | } |
860 | 11.9M | Buffer[index] = (byte)c; |
861 | 11.9M | index++; |
862 | 11.9M | } while (index < 255); |
863 | | |
864 | 2.17M | if (index >= 255 || index == 0) { |
865 | 16.7k | if (ctx->args.pdfstoponerror) |
866 | 0 | return_error(gs_error_syntaxerror); |
867 | 16.7k | return TOKEN_INVALID_KEY; |
868 | 16.7k | } |
869 | | |
870 | 2.15M | Buffer[index] = 0x00; |
871 | 2.15M | t = bsearch((const void *)Buffer, |
872 | 2.15M | (const void *)pdf_token_strings[TOKEN_INVALID_KEY+1], |
873 | 2.15M | nelems(pdf_token_strings)-(TOKEN_INVALID_KEY+1), |
874 | 2.15M | sizeof(pdf_token_strings[0]), |
875 | 2.15M | (bsearch_comparator)&strcmp); |
876 | 2.15M | if (t == NULL) |
877 | 26.7k | return TOKEN_INVALID_KEY; |
878 | | |
879 | 2.13M | if (ctx->args.pdfdebug) |
880 | 2.13M | dmprintf1(ctx->memory, " %s\n", Buffer); |
881 | | |
882 | 2.13M | return (((const char *)t) - pdf_token_strings[0]) / sizeof(pdf_token_strings[0]); |
883 | 2.15M | } |
884 | | |
885 | | static pdf_key lookup_keyword(const byte *Buffer) |
886 | 62.2M | { |
887 | 62.2M | void *t = bsearch((const void *)Buffer, |
888 | 62.2M | (const void *)pdf_token_strings[TOKEN_INVALID_KEY+1], |
889 | 62.2M | nelems(pdf_token_strings)-(TOKEN_INVALID_KEY+1), |
890 | 62.2M | sizeof(pdf_token_strings[0]), |
891 | 62.2M | (bsearch_comparator)&strcmp); |
892 | 62.2M | if (t == NULL) |
893 | 6.11M | return TOKEN_NOT_A_KEYWORD; |
894 | | |
895 | 56.1M | return (pdf_key)((((const char *)t) - pdf_token_strings[0]) / |
896 | 56.1M | sizeof(pdf_token_strings[0])); |
897 | 62.2M | } |
898 | | |
899 | | /* This function is slightly misnamed. We read 'keywords' from |
900 | | * the stream (including null, true, false and R), and will usually |
901 | | * return them directly as TOKENs cast to be pointers. In the event |
902 | | * that we can't match what we parse to a known keyword, we'll |
903 | | * instead return a PDF_KEYWORD object. In the even that we parse |
904 | | * an 'R', we will return a PDF_INDIRECT object. |
905 | | */ |
906 | | static int pdfi_read_keyword(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen) |
907 | 62.2M | { |
908 | 62.2M | byte Buffer[256]; |
909 | 62.2M | unsigned short index = 0; |
910 | 62.2M | int c, code; |
911 | 62.2M | pdf_keyword *keyword; |
912 | 62.2M | pdf_key key; |
913 | | |
914 | 62.2M | pdfi_skip_white(ctx, s); |
915 | | |
916 | 256M | do { |
917 | 256M | c = pdfi_read_byte(ctx, s); |
918 | 256M | if (c < 0) |
919 | 176k | break; |
920 | | |
921 | 255M | if (iswhite(c) || isdelimiter(c)) { |
922 | 61.9M | pdfi_unread_byte(ctx, s, (byte)c); |
923 | 61.9M | break; |
924 | 61.9M | } |
925 | 194M | Buffer[index] = (byte)c; |
926 | 194M | index++; |
927 | 194M | } while (index < 255); |
928 | | |
929 | 62.2M | if (index >= 255 || index == 0) { |
930 | 178k | if (ctx->args.pdfstoponerror) |
931 | 0 | return_error(gs_error_syntaxerror); |
932 | 178k | key = (index >= 255 ? TOKEN_TOO_LONG : TOKEN_INVALID_KEY); |
933 | 178k | index = 0; |
934 | 178k | Buffer[0] = 0; |
935 | 62.1M | } else { |
936 | 62.1M | Buffer[index] = 0x00; |
937 | 62.1M | key = lookup_keyword(Buffer); |
938 | | |
939 | 62.1M | if (ctx->args.pdfdebug) |
940 | 62.1M | dmprintf1(ctx->memory, " %s\n", Buffer); |
941 | | |
942 | 62.1M | switch (key) { |
943 | 6.49M | case TOKEN_R: |
944 | 6.49M | { |
945 | 6.49M | pdf_indirect_ref *o; |
946 | 6.49M | uint64_t obj_num; |
947 | 6.49M | uint32_t gen_num; |
948 | | |
949 | 6.49M | if(pdfi_count_stack(ctx) < 2) { |
950 | 9.37k | pdfi_clearstack(ctx); |
951 | 9.37k | return_error(gs_error_stackunderflow); |
952 | 9.37k | } |
953 | | |
954 | 6.48M | if(pdfi_type_of(ctx->stack_top[-1]) != PDF_INT || pdfi_type_of(ctx->stack_top[-2]) != PDF_INT) { |
955 | 21.0k | pdfi_clearstack(ctx); |
956 | 21.0k | return_error(gs_error_typecheck); |
957 | 21.0k | } |
958 | | |
959 | 6.46M | gen_num = ((pdf_num *)ctx->stack_top[-1])->value.i; |
960 | 6.46M | pdfi_pop(ctx, 1); |
961 | 6.46M | obj_num = ((pdf_num *)ctx->stack_top[-1])->value.i; |
962 | 6.46M | pdfi_pop(ctx, 1); |
963 | | |
964 | 6.46M | code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&o); |
965 | 6.46M | if (code < 0) |
966 | 0 | return code; |
967 | | |
968 | 6.46M | o->ref_generation_num = gen_num; |
969 | 6.46M | o->ref_object_num = obj_num; |
970 | 6.46M | o->indirect_num = indirect_num; |
971 | 6.46M | o->indirect_gen = indirect_gen; |
972 | | |
973 | 6.46M | code = pdfi_push(ctx, (pdf_obj *)o); |
974 | 6.46M | if (code < 0) |
975 | 0 | pdfi_free_object((pdf_obj *)o); |
976 | | |
977 | 6.46M | return code; |
978 | 6.46M | } |
979 | 6.11M | case TOKEN_NOT_A_KEYWORD: |
980 | | /* Unexpected keyword found. We'll allocate an object for the buffer below. */ |
981 | 6.11M | break; |
982 | 1.68M | case TOKEN_STREAM: |
983 | 1.68M | code = pdfi_skip_eol(ctx, s); |
984 | 1.68M | if (code < 0) |
985 | 0 | return code; |
986 | | /* fallthrough */ |
987 | 1.93M | case TOKEN_PDF_TRUE: |
988 | 2.23M | case TOKEN_PDF_FALSE: |
989 | 2.24M | case TOKEN_null: |
990 | 49.5M | default: |
991 | | /* This is the fast, common exit case. We just push the key |
992 | | * onto the stack. No allocation required. No deallocation |
993 | | * in the case of error. */ |
994 | 49.5M | return pdfi_push(ctx, (pdf_obj *)(intptr_t)key); |
995 | 62.1M | } |
996 | 62.1M | } |
997 | | |
998 | | /* Unexpected keyword. We can't handle this with the fast no-allocation case. */ |
999 | 6.29M | code = pdfi_object_alloc(ctx, PDF_KEYWORD, index, (pdf_obj **)&keyword); |
1000 | 6.29M | if (code < 0) |
1001 | 0 | return code; |
1002 | | |
1003 | 6.29M | if (index) |
1004 | 6.11M | memcpy(keyword->data, Buffer, index); |
1005 | | |
1006 | | /* keyword->length set as part of allocation. */ |
1007 | 6.29M | keyword->indirect_num = indirect_num; |
1008 | 6.29M | keyword->indirect_gen = indirect_gen; |
1009 | | |
1010 | 6.29M | if (ctx->args.pdfdebug) |
1011 | 6.29M | dmprintf1(ctx->memory, " %s\n", Buffer); |
1012 | | |
1013 | 6.29M | code = pdfi_push(ctx, (pdf_obj *)keyword); |
1014 | 6.29M | if (code < 0) |
1015 | 30 | pdfi_free_object((pdf_obj *)keyword); |
1016 | | |
1017 | 6.29M | return code; |
1018 | 6.29M | } |
1019 | | |
1020 | | /* This function reads from the given stream, at the current offset in the stream, |
1021 | | * a single PDF 'token' and returns it on the stack. |
1022 | | */ |
1023 | | int pdfi_read_token(pdf_context *ctx, pdf_c_stream *s, uint32_t indirect_num, uint32_t indirect_gen) |
1024 | 342M | { |
1025 | 342M | int c, code; |
1026 | | |
1027 | 344M | rescan: |
1028 | 344M | pdfi_skip_white(ctx, s); |
1029 | | |
1030 | 344M | c = pdfi_read_byte(ctx, s); |
1031 | 344M | if (c == EOFC) |
1032 | 178k | return 0; |
1033 | 344M | if (c < 0) |
1034 | 4.01k | return_error(gs_error_ioerror); |
1035 | | |
1036 | 344M | switch(c) { |
1037 | 32.3M | case 0x30: |
1038 | 55.9M | case 0x31: |
1039 | 81.7M | case 0x32: |
1040 | 116M | case 0x33: |
1041 | 145M | case 0x34: |
1042 | 159M | case 0x35: |
1043 | 172M | case 0x36: |
1044 | 177M | case 0x37: |
1045 | 180M | case 0x38: |
1046 | 182M | case 0x39: |
1047 | 182M | case '+': |
1048 | 189M | case '-': |
1049 | 190M | case '.': |
1050 | 190M | pdfi_unread_byte(ctx, s, (byte)c); |
1051 | 190M | code = pdfi_read_num(ctx, s, indirect_num, indirect_gen); |
1052 | 190M | if (code < 0) |
1053 | 42.7k | return code; |
1054 | 190M | break; |
1055 | 190M | case '/': |
1056 | 47.8M | code = pdfi_read_name(ctx, s, indirect_num, indirect_gen); |
1057 | 47.8M | if (code < 0) |
1058 | 0 | return code; |
1059 | 47.8M | return 1; |
1060 | 0 | break; |
1061 | 7.84M | case '<': |
1062 | 7.84M | c = pdfi_read_byte(ctx, s); |
1063 | 7.84M | if (c < 0) |
1064 | 206 | return (gs_error_ioerror); |
1065 | 7.84M | if (iswhite(c)) { |
1066 | 20.2k | code = pdfi_skip_white(ctx, s); |
1067 | 20.2k | if (code < 0) |
1068 | 0 | return code; |
1069 | 20.2k | c = pdfi_read_byte(ctx, s); |
1070 | 20.2k | } |
1071 | 7.84M | if (c == '<') { |
1072 | 6.89M | if (ctx->args.pdfdebug) |
1073 | 6.89M | dmprintf (ctx->memory, " <<\n"); |
1074 | 6.89M | if (ctx->object_nesting < MAX_NESTING_DEPTH) { |
1075 | 6.52M | ctx->object_nesting++; |
1076 | 6.52M | code = pdfi_mark_stack(ctx, PDF_DICT_MARK); |
1077 | 6.52M | if (code < 0) |
1078 | 1 | return code; |
1079 | 6.52M | }else |
1080 | 369k | pdfi_set_error(ctx, 0, NULL, E_PDF_NESTEDTOODEEP, "pdfi_read_token", NULL); |
1081 | 6.89M | return 1; |
1082 | 6.89M | } else if (c == '>') { |
1083 | 10.8k | pdfi_unread_byte(ctx, s, (byte)c); |
1084 | 10.8k | code = pdfi_read_hexstring(ctx, s, indirect_num, indirect_gen); |
1085 | 10.8k | if (code < 0) |
1086 | 0 | return code; |
1087 | 10.8k | return 1; |
1088 | 938k | } else if (ishex(c)) { |
1089 | 705k | pdfi_unread_byte(ctx, s, (byte)c); |
1090 | 705k | code = pdfi_read_hexstring(ctx, s, indirect_num, indirect_gen); |
1091 | 705k | if (code < 0) |
1092 | 64.9k | return code; |
1093 | 705k | } |
1094 | 233k | else |
1095 | 233k | return_error(gs_error_syntaxerror); |
1096 | 640k | break; |
1097 | 7.27M | case '>': |
1098 | 7.27M | c = pdfi_read_byte(ctx, s); |
1099 | 7.27M | if (c < 0) |
1100 | 210 | return (gs_error_ioerror); |
1101 | 7.27M | if (c == '>') { |
1102 | 6.98M | if (ctx->object_nesting > 0) { |
1103 | 6.39M | ctx->object_nesting--; |
1104 | 6.39M | code = pdfi_dict_from_stack(ctx, indirect_num, indirect_gen, false); |
1105 | 6.39M | if (code < 0) |
1106 | 125k | return code; |
1107 | 6.39M | } else { |
1108 | 594k | pdfi_set_error(ctx, 0, NULL, E_PDF_UNMATCHEDMARK, "pdfi_read_token", NULL); |
1109 | 594k | goto rescan; |
1110 | 594k | } |
1111 | 6.26M | return 1; |
1112 | 6.98M | } else { |
1113 | 284k | pdfi_unread_byte(ctx, s, (byte)c); |
1114 | 284k | return_error(gs_error_syntaxerror); |
1115 | 284k | } |
1116 | 0 | break; |
1117 | 8.67M | case '(': |
1118 | 8.67M | code = pdfi_read_string(ctx, s, indirect_num, indirect_gen); |
1119 | 8.67M | if (code < 0) |
1120 | 0 | return code; |
1121 | 8.67M | return 1; |
1122 | 0 | break; |
1123 | 10.4M | case '[': |
1124 | 10.4M | if (ctx->args.pdfdebug) |
1125 | 10.4M | dmprintf (ctx->memory, "["); |
1126 | 10.4M | if (ctx->object_nesting < MAX_NESTING_DEPTH) { |
1127 | 7.48M | ctx->object_nesting++; |
1128 | 7.48M | code = pdfi_mark_stack(ctx, PDF_ARRAY_MARK); |
1129 | 7.48M | if (code < 0) |
1130 | 0 | return code; |
1131 | 7.48M | } else |
1132 | 2.93M | pdfi_set_error(ctx, 0, NULL, E_PDF_NESTEDTOODEEP, "pdfi_read_token", NULL); |
1133 | 10.4M | return 1; |
1134 | 0 | break; |
1135 | 7.57M | case ']': |
1136 | 7.57M | if (ctx->object_nesting > 0) { |
1137 | 7.36M | ctx->object_nesting--; |
1138 | 7.36M | code = pdfi_array_from_stack(ctx, indirect_num, indirect_gen); |
1139 | 7.36M | if (code < 0) |
1140 | 82.1k | return code; |
1141 | 7.36M | } else { |
1142 | 210k | pdfi_set_error(ctx, 0, NULL, E_PDF_UNMATCHEDMARK, "pdfi_read_token", NULL); |
1143 | 210k | goto rescan; |
1144 | 210k | } |
1145 | 7.28M | break; |
1146 | 7.28M | case '{': |
1147 | 609k | if (ctx->args.pdfdebug) |
1148 | 609k | dmprintf (ctx->memory, "{"); |
1149 | 609k | code = pdfi_mark_stack(ctx, PDF_PROC_MARK); |
1150 | 609k | if (code < 0) |
1151 | 0 | return code; |
1152 | 609k | return 1; |
1153 | 0 | break; |
1154 | 212k | case '}': |
1155 | 212k | pdfi_clear_to_mark(ctx); |
1156 | 212k | goto rescan; |
1157 | 0 | break; |
1158 | 984k | case '%': |
1159 | 984k | pdfi_skip_comment(ctx, s); |
1160 | 984k | goto rescan; |
1161 | 0 | break; |
1162 | 62.5M | default: |
1163 | 62.5M | if (isdelimiter(c)) { |
1164 | 258k | if (ctx->args.pdfstoponerror) |
1165 | 0 | return_error(gs_error_syntaxerror); |
1166 | 258k | goto rescan; |
1167 | 258k | } |
1168 | 62.2M | pdfi_unread_byte(ctx, s, (byte)c); |
1169 | 62.2M | code = pdfi_read_keyword(ctx, s, indirect_num, indirect_gen); |
1170 | 62.2M | if (code < 0) |
1171 | 30.5k | return code; |
1172 | 62.2M | return 1; |
1173 | 0 | break; |
1174 | 344M | } |
1175 | 198M | return 1; |
1176 | 344M | } |
1177 | | |
1178 | | /* In contrast to the 'read' functions, the 'make' functions create an object with a |
1179 | | * reference count of 1. This indicates that the caller holds the reference. Thus the |
1180 | | * caller need not increment the reference count to the object, but must decrement |
1181 | | * it (pdf_countdown) before exiting. |
1182 | | */ |
1183 | | int pdfi_name_alloc(pdf_context *ctx, byte *n, uint32_t size, pdf_obj **o) |
1184 | 66.0M | { |
1185 | 66.0M | int code; |
1186 | 66.0M | *o = NULL; |
1187 | | |
1188 | 66.0M | code = pdfi_object_alloc(ctx, PDF_NAME, size, o); |
1189 | 66.0M | if (code < 0) |
1190 | 0 | return code; |
1191 | | |
1192 | 66.0M | memcpy(((pdf_name *)*o)->data, n, size); |
1193 | | |
1194 | 66.0M | return 0; |
1195 | 66.0M | } |
1196 | | |
1197 | | static char op_table_3[5][3] = { |
1198 | | "BDC", "BMC", "EMC", "SCN", "scn" |
1199 | | }; |
1200 | | |
1201 | | static char op_table_2[39][2] = { |
1202 | | "b*", "BI", "BT", "BX", "cm", "CS", "cs", "EI", "d0", "d1", "Do", "DP", "ET", "EX", "f*", "gs", "ID", "MP", "re", "RG", |
1203 | | "rg", "ri", "SC", "sc", "sh", "T*", "Tc", "Td", "TD", "Tf", "Tj", "TJ", "TL", "Tm", "Tr", "Ts", "Tw", "Tz", "W*", |
1204 | | }; |
1205 | | |
1206 | | static char op_table_1[27][1] = { |
1207 | | "b", "B", "c", "d", "f", "F", "G", "g", "h", "i", "j", "J", "K", "k", "l", "m", "n", "q", "Q", "s", "S", "v", "w", "W", |
1208 | | "y", "'", "\"" |
1209 | | }; |
1210 | | |
1211 | | /* forward definition for the 'split_bogus_operator' function to use */ |
1212 | | static int pdfi_interpret_stream_operator(pdf_context *ctx, pdf_c_stream *source, |
1213 | | pdf_dict *stream_dict, pdf_dict *page_dict); |
1214 | | |
1215 | | static int |
1216 | | make_keyword_obj(pdf_context *ctx, const byte *data, int length, pdf_keyword **pkey) |
1217 | 153k | { |
1218 | 153k | byte Buffer[256]; |
1219 | 153k | pdf_key key; |
1220 | 153k | int code; |
1221 | | |
1222 | 153k | memcpy(Buffer, data, length); |
1223 | 153k | Buffer[length] = 0; |
1224 | 153k | key = lookup_keyword(Buffer); |
1225 | 153k | if (key != TOKEN_INVALID_KEY) { |
1226 | | /* The common case. We've found a real key, just cast the token to |
1227 | | * a pointer, and return that. */ |
1228 | 153k | *pkey = (pdf_keyword *)PDF_TOKEN_AS_OBJ(key); |
1229 | 153k | return 1; |
1230 | 153k | } |
1231 | | /* We still haven't found a real keyword. Allocate a new object and |
1232 | | * return it. */ |
1233 | 0 | code = pdfi_object_alloc(ctx, PDF_KEYWORD, length, (pdf_obj **)pkey); |
1234 | 0 | if (code < 0) |
1235 | 0 | return code; |
1236 | 0 | if (length) |
1237 | 0 | memcpy((*pkey)->data, Buffer, length); |
1238 | 0 | pdfi_countup(*pkey); |
1239 | |
|
1240 | 0 | return 1; |
1241 | 0 | } |
1242 | | |
1243 | | static int search_table_3(pdf_context *ctx, unsigned char *str, pdf_keyword **key) |
1244 | 548k | { |
1245 | 548k | int i; |
1246 | | |
1247 | 3.28M | for (i = 0; i < 5; i++) { |
1248 | 2.74M | if (memcmp(str, op_table_3[i], 3) == 0) |
1249 | 528 | return make_keyword_obj(ctx, str, 3, key); |
1250 | 2.74M | } |
1251 | 547k | return 0; |
1252 | 548k | } |
1253 | | |
1254 | | static int search_table_2(pdf_context *ctx, unsigned char *str, pdf_keyword **key) |
1255 | 329k | { |
1256 | 329k | int i; |
1257 | | |
1258 | 12.1M | for (i = 0; i < 39; i++) { |
1259 | 11.8M | if (memcmp(str, op_table_2[i], 2) == 0) |
1260 | 46.0k | return make_keyword_obj(ctx, str, 2, key); |
1261 | 11.8M | } |
1262 | 283k | return 0; |
1263 | 329k | } |
1264 | | |
1265 | | static int search_table_1(pdf_context *ctx, unsigned char *str, pdf_keyword **key) |
1266 | 159k | { |
1267 | 159k | int i; |
1268 | | |
1269 | 2.68M | for (i = 0; i < 27; i++) { |
1270 | 2.62M | if (memcmp(str, op_table_1[i], 1) == 0) |
1271 | 106k | return make_keyword_obj(ctx, str, 1, key); |
1272 | 2.62M | } |
1273 | 53.1k | return 0; |
1274 | 159k | } |
1275 | | |
1276 | | static int split_bogus_operator(pdf_context *ctx, pdf_c_stream *source, pdf_dict *stream_dict, pdf_dict *page_dict) |
1277 | 1.46M | { |
1278 | 1.46M | int code = 0; |
1279 | 1.46M | pdf_keyword *keyword = (pdf_keyword *)ctx->stack_top[-1], *key1 = NULL, *key2 = NULL; |
1280 | 1.46M | int length = keyword->length - 6; |
1281 | | |
1282 | 1.46M | if (length > 0) { |
1283 | | /* Longer than 2 3-character operators, we only allow for up to two |
1284 | | * operators. Check to see if it includes an endstream or endobj. |
1285 | | */ |
1286 | 1.03M | if (memcmp(&keyword->data[length], "endobj", 6) == 0) { |
1287 | | /* Keyword is "<something>endobj". So make a keyword just from |
1288 | | * <something>, push that, execute it, then push endobj. */ |
1289 | 2 | code = make_keyword_obj(ctx, keyword->data, length, &key1); |
1290 | 2 | if (code < 0) |
1291 | 0 | goto error_exit; |
1292 | 2 | pdfi_pop(ctx, 1); |
1293 | 2 | pdfi_push(ctx, (pdf_obj *)key1); |
1294 | 2 | pdfi_countdown(key1); /* Drop the reference returned by make_keyword_obj. */ |
1295 | 2 | code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict); |
1296 | 2 | if (code < 0) |
1297 | 0 | goto error_exit; |
1298 | 2 | pdfi_push(ctx, PDF_TOKEN_AS_OBJ(TOKEN_ENDOBJ)); |
1299 | 2 | return 0; |
1300 | 1.03M | } else { |
1301 | 1.03M | length = keyword->length - 9; |
1302 | 1.03M | if (length > 0 && memcmp(&keyword->data[length], "endstream", 9) == 0) { |
1303 | | /* Keyword is "<something>endstream". So make a keyword just from |
1304 | | * <something>, push that, execute it, then push endstream. */ |
1305 | 0 | code = make_keyword_obj(ctx, keyword->data, length, &key1); |
1306 | 0 | if (code < 0) |
1307 | 0 | goto error_exit; |
1308 | 0 | pdfi_pop(ctx, 1); |
1309 | 0 | pdfi_push(ctx, (pdf_obj *)key1); |
1310 | 0 | pdfi_countdown(key1); /* Drop the reference returned by make_keyword_obj. */ |
1311 | 0 | code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict); |
1312 | 0 | if (code < 0) |
1313 | 0 | goto error_exit; |
1314 | 0 | pdfi_push(ctx, PDF_TOKEN_AS_OBJ(TOKEN_ENDSTREAM)); |
1315 | 0 | return 0; |
1316 | 1.03M | } else { |
1317 | 1.03M | pdfi_clearstack(ctx); |
1318 | 1.03M | return 0; |
1319 | 1.03M | } |
1320 | 1.03M | } |
1321 | 1.03M | } |
1322 | | |
1323 | 429k | if (keyword->length > 3) { |
1324 | 429k | code = search_table_3(ctx, keyword->data, &key1); |
1325 | 429k | if (code < 0) |
1326 | 0 | goto error_exit; |
1327 | | |
1328 | 429k | if (code > 0) { |
1329 | 316 | switch (keyword->length - 3) { |
1330 | 168 | case 1: |
1331 | 168 | code = search_table_1(ctx, &keyword->data[3], &key2); |
1332 | 168 | break; |
1333 | 85 | case 2: |
1334 | 85 | code = search_table_2(ctx, &keyword->data[3], &key2); |
1335 | 85 | break; |
1336 | 63 | case 3: |
1337 | 63 | code = search_table_3(ctx, &keyword->data[3], &key2); |
1338 | 63 | break; |
1339 | 0 | default: |
1340 | 0 | goto error_exit; |
1341 | 316 | } |
1342 | 316 | } |
1343 | 429k | if (code < 0) |
1344 | 0 | goto error_exit; |
1345 | 429k | if (code > 0) |
1346 | 54 | goto match; |
1347 | 429k | } |
1348 | 428k | pdfi_countdown(key1); |
1349 | 428k | pdfi_countdown(key2); |
1350 | 428k | key1 = NULL; |
1351 | 428k | key2 = NULL; |
1352 | | |
1353 | 428k | if (keyword->length > 5 || keyword->length < 2) |
1354 | 129k | goto error_exit; |
1355 | | |
1356 | 299k | code = search_table_2(ctx, keyword->data, &key1); |
1357 | 299k | if (code < 0) |
1358 | 0 | goto error_exit; |
1359 | | |
1360 | 299k | if (code > 0) { |
1361 | 42.5k | switch(keyword->length - 2) { |
1362 | 0 | case 1: |
1363 | 0 | code = search_table_1(ctx, &keyword->data[2], &key2); |
1364 | 0 | break; |
1365 | 29.7k | case 2: |
1366 | 29.7k | code = search_table_2(ctx, &keyword->data[2], &key2); |
1367 | 29.7k | break; |
1368 | 12.7k | case 3: |
1369 | 12.7k | code = search_table_3(ctx, &keyword->data[2], &key2); |
1370 | 12.7k | break; |
1371 | 0 | default: |
1372 | 0 | goto error_exit; |
1373 | 42.5k | } |
1374 | 42.5k | if (code < 0) |
1375 | 0 | goto error_exit; |
1376 | 42.5k | if (code > 0) |
1377 | 3.48k | goto match; |
1378 | 42.5k | } |
1379 | 296k | pdfi_countdown(key1); |
1380 | 296k | pdfi_countdown(key2); |
1381 | 296k | key1 = NULL; |
1382 | 296k | key2 = NULL; |
1383 | | |
1384 | 296k | if (keyword->length > 4) |
1385 | 136k | goto error_exit; |
1386 | | |
1387 | 159k | code = search_table_1(ctx, keyword->data, &key1); |
1388 | 159k | if (code <= 0) |
1389 | 52.9k | goto error_exit; |
1390 | | |
1391 | 106k | switch(keyword->length - 1) { |
1392 | 0 | case 1: |
1393 | 0 | code = search_table_1(ctx, &keyword->data[1], &key2); |
1394 | 0 | break; |
1395 | 0 | case 2: |
1396 | 0 | code = search_table_2(ctx, &keyword->data[1], &key2); |
1397 | 0 | break; |
1398 | 106k | case 3: |
1399 | 106k | code = search_table_3(ctx, &keyword->data[1], &key2); |
1400 | 106k | break; |
1401 | 0 | default: |
1402 | 0 | goto error_exit; |
1403 | 106k | } |
1404 | 106k | if (code <= 0) |
1405 | 106k | goto error_exit; |
1406 | | |
1407 | 3.74k | match: |
1408 | | /* If we get here, we have two PDF_KEYWORD objects. We push them on the stack |
1409 | | * one at a time, and execute them. |
1410 | | */ |
1411 | 3.74k | pdfi_push(ctx, (pdf_obj *)key1); |
1412 | 3.74k | code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict); |
1413 | 3.74k | if (code < 0) |
1414 | 2.91k | goto error_exit; |
1415 | | |
1416 | 832 | pdfi_push(ctx, (pdf_obj *)key2); |
1417 | 832 | code = pdfi_interpret_stream_operator(ctx, source, stream_dict, page_dict); |
1418 | | |
1419 | 429k | error_exit: |
1420 | 429k | pdfi_set_error(ctx, 0, NULL, E_PDF_TOKENERROR, "split_bogus_operator", NULL); |
1421 | 429k | pdfi_countdown(key1); |
1422 | 429k | pdfi_countdown(key2); |
1423 | 429k | pdfi_clearstack(ctx); |
1424 | 429k | return code; |
1425 | 832 | } |
1426 | | |
1427 | | static int pdfi_interpret_stream_operator(pdf_context *ctx, pdf_c_stream *source, |
1428 | | pdf_dict *stream_dict, pdf_dict *page_dict) |
1429 | 42.3M | { |
1430 | 42.3M | pdf_obj *keyword = ctx->stack_top[-1]; |
1431 | 42.3M | int code = 0; |
1432 | | |
1433 | 42.3M | if (keyword < PDF_TOKEN_AS_OBJ(TOKEN__LAST_KEY)) |
1434 | 39.7M | { |
1435 | 39.7M | switch((uintptr_t)keyword) { |
1436 | 8.32k | case TOKEN_b: /* closepath, fill, stroke */ |
1437 | 8.32k | pdfi_pop(ctx, 1); |
1438 | 8.32k | code = pdfi_b(ctx); |
1439 | 8.32k | break; |
1440 | 8.49k | case TOKEN_B: /* fill, stroke */ |
1441 | 8.49k | pdfi_pop(ctx, 1); |
1442 | 8.49k | code = pdfi_B(ctx); |
1443 | 8.49k | break; |
1444 | 134 | case TOKEN_bstar: /* closepath, eofill, stroke */ |
1445 | 134 | pdfi_pop(ctx, 1); |
1446 | 134 | code = pdfi_b_star(ctx); |
1447 | 134 | break; |
1448 | 253 | case TOKEN_Bstar: /* eofill, stroke */ |
1449 | 253 | pdfi_pop(ctx, 1); |
1450 | 253 | code = pdfi_B_star(ctx); |
1451 | 253 | break; |
1452 | 7.22k | case TOKEN_BI: /* begin inline image */ |
1453 | 7.22k | pdfi_pop(ctx, 1); |
1454 | 7.22k | code = pdfi_BI(ctx); |
1455 | 7.22k | break; |
1456 | 109k | case TOKEN_BDC: /* begin marked content sequence with property list */ |
1457 | 109k | pdfi_pop(ctx, 1); |
1458 | 109k | code = pdfi_op_BDC(ctx, stream_dict, page_dict); |
1459 | 109k | break; |
1460 | 6.37k | case TOKEN_BMC: /* begin marked content sequence */ |
1461 | 6.37k | pdfi_pop(ctx, 1); |
1462 | 6.37k | code = pdfi_op_BMC(ctx); |
1463 | 6.37k | break; |
1464 | 763k | case TOKEN_BT: /* begin text */ |
1465 | 763k | pdfi_pop(ctx, 1); |
1466 | 763k | code = pdfi_BT(ctx); |
1467 | 763k | break; |
1468 | 11.1k | case TOKEN_BX: /* begin compatibility section */ |
1469 | 11.1k | pdfi_pop(ctx, 1); |
1470 | 11.1k | break; |
1471 | 11.2M | case TOKEN_c: /* curveto */ |
1472 | 11.2M | pdfi_pop(ctx, 1); |
1473 | 11.2M | code = pdfi_curveto(ctx); |
1474 | 11.2M | break; |
1475 | 700k | case TOKEN_cm: /* concat */ |
1476 | 700k | pdfi_pop(ctx, 1); |
1477 | 700k | code = pdfi_concat(ctx); |
1478 | 700k | break; |
1479 | 18.7k | case TOKEN_CS: /* set stroke colour space */ |
1480 | 18.7k | pdfi_pop(ctx, 1); |
1481 | 18.7k | code = pdfi_setstrokecolor_space(ctx, stream_dict, page_dict); |
1482 | 18.7k | break; |
1483 | 121k | case TOKEN_cs: /* set non-stroke colour space */ |
1484 | 121k | pdfi_pop(ctx, 1); |
1485 | 121k | code = pdfi_setfillcolor_space(ctx, stream_dict, page_dict); |
1486 | 121k | break; |
1487 | 227k | case TOKEN_d: /* set dash params */ |
1488 | 227k | pdfi_pop(ctx, 1); |
1489 | 227k | code = pdfi_setdash(ctx); |
1490 | 227k | break; |
1491 | 207 | case TOKEN_d0: /* set type 3 font glyph width */ |
1492 | 207 | pdfi_pop(ctx, 1); |
1493 | 207 | code = pdfi_d0(ctx); |
1494 | 207 | break; |
1495 | 7.10k | case TOKEN_d1: /* set type 3 font glyph width and bounding box */ |
1496 | 7.10k | pdfi_pop(ctx, 1); |
1497 | 7.10k | code = pdfi_d1(ctx); |
1498 | 7.10k | break; |
1499 | 117k | case TOKEN_Do: /* invoke named XObject */ |
1500 | 117k | pdfi_pop(ctx, 1); |
1501 | 117k | code = pdfi_Do(ctx, stream_dict, page_dict); |
1502 | 117k | break; |
1503 | 754 | case TOKEN_DP: /* define marked content point with property list */ |
1504 | 754 | pdfi_pop(ctx, 1); |
1505 | 754 | code = pdfi_op_DP(ctx, stream_dict, page_dict); |
1506 | 754 | break; |
1507 | 7.20k | case TOKEN_EI: /* end inline image */ |
1508 | 7.20k | pdfi_pop(ctx, 1); |
1509 | 7.20k | code = pdfi_EI(ctx); |
1510 | 7.20k | break; |
1511 | 761k | case TOKEN_ET: /* end text */ |
1512 | 761k | pdfi_pop(ctx, 1); |
1513 | 761k | code = pdfi_ET(ctx); |
1514 | 761k | break; |
1515 | 114k | case TOKEN_EMC: /* end marked content sequence */ |
1516 | 114k | pdfi_pop(ctx, 1); |
1517 | 114k | code = pdfi_op_EMC(ctx); |
1518 | 114k | break; |
1519 | 9.49k | case TOKEN_EX: /* end compatibility section */ |
1520 | 9.49k | pdfi_pop(ctx, 1); |
1521 | 9.49k | break; |
1522 | 1.95M | case TOKEN_f: /* fill */ |
1523 | 1.95M | pdfi_pop(ctx, 1); |
1524 | 1.95M | code = pdfi_fill(ctx); |
1525 | 1.95M | break; |
1526 | 2.58k | case TOKEN_F: /* fill (obselete operator) */ |
1527 | 2.58k | pdfi_pop(ctx, 1); |
1528 | 2.58k | code = pdfi_fill(ctx); |
1529 | 2.58k | break; |
1530 | 22.7k | case TOKEN_fstar: /* eofill */ |
1531 | 22.7k | pdfi_pop(ctx, 1); |
1532 | 22.7k | code = pdfi_eofill(ctx); |
1533 | 22.7k | break; |
1534 | 140k | case TOKEN_G: /* setgray for stroke */ |
1535 | 140k | pdfi_pop(ctx, 1); |
1536 | 140k | code = pdfi_setgraystroke(ctx); |
1537 | 140k | break; |
1538 | 266k | case TOKEN_g: /* setgray for non-stroke */ |
1539 | 266k | pdfi_pop(ctx, 1); |
1540 | 266k | code = pdfi_setgrayfill(ctx); |
1541 | 266k | break; |
1542 | 168k | case TOKEN_gs: /* set graphics state from dictionary */ |
1543 | 168k | pdfi_pop(ctx, 1); |
1544 | 168k | code = pdfi_setgstate(ctx, stream_dict, page_dict); |
1545 | 168k | break; |
1546 | 130k | case TOKEN_h: /* closepath */ |
1547 | 130k | pdfi_pop(ctx, 1); |
1548 | 130k | code = pdfi_closepath(ctx); |
1549 | 130k | break; |
1550 | 139k | case TOKEN_i: /* setflat */ |
1551 | 139k | pdfi_pop(ctx, 1); |
1552 | 139k | code = pdfi_setflat(ctx); |
1553 | 139k | break; |
1554 | 7.27k | case TOKEN_ID: /* begin inline image data */ |
1555 | 7.27k | pdfi_pop(ctx, 1); |
1556 | 7.27k | code = pdfi_ID(ctx, stream_dict, page_dict, source); |
1557 | 7.27k | break; |
1558 | 182k | case TOKEN_j: /* setlinejoin */ |
1559 | 182k | pdfi_pop(ctx, 1); |
1560 | 182k | code = pdfi_setlinejoin(ctx); |
1561 | 182k | break; |
1562 | 189k | case TOKEN_J: /* setlinecap */ |
1563 | 189k | pdfi_pop(ctx, 1); |
1564 | 189k | code = pdfi_setlinecap(ctx); |
1565 | 189k | break; |
1566 | 33.8k | case TOKEN_K: /* setcmyk for non-stroke */ |
1567 | 33.8k | pdfi_pop(ctx, 1); |
1568 | 33.8k | code = pdfi_setcmykstroke(ctx); |
1569 | 33.8k | break; |
1570 | 148k | case TOKEN_k: /* setcmyk for non-stroke */ |
1571 | 148k | pdfi_pop(ctx, 1); |
1572 | 148k | code = pdfi_setcmykfill(ctx); |
1573 | 148k | break; |
1574 | 8.01M | case TOKEN_l: /* lineto */ |
1575 | 8.01M | pdfi_pop(ctx, 1); |
1576 | 8.01M | code = pdfi_lineto(ctx); |
1577 | 8.01M | break; |
1578 | 2.38M | case TOKEN_m: /* moveto */ |
1579 | 2.38M | pdfi_pop(ctx, 1); |
1580 | 2.38M | code = pdfi_moveto(ctx); |
1581 | 2.38M | break; |
1582 | 20.7k | case TOKEN_M: /* setmiterlimit */ |
1583 | 20.7k | pdfi_pop(ctx, 1); |
1584 | 20.7k | code = pdfi_setmiterlimit(ctx); |
1585 | 20.7k | break; |
1586 | 921 | case TOKEN_MP: /* define marked content point */ |
1587 | 921 | pdfi_pop(ctx, 1); |
1588 | 921 | code = pdfi_op_MP(ctx); |
1589 | 921 | break; |
1590 | 319k | case TOKEN_n: /* newpath */ |
1591 | 319k | pdfi_pop(ctx, 1); |
1592 | 319k | code = pdfi_newpath(ctx); |
1593 | 319k | break; |
1594 | 1.09M | case TOKEN_q: /* gsave */ |
1595 | 1.09M | pdfi_pop(ctx, 1); |
1596 | 1.09M | code = pdfi_op_q(ctx); |
1597 | 1.09M | break; |
1598 | 1.06M | case TOKEN_Q: /* grestore */ |
1599 | 1.06M | pdfi_pop(ctx, 1); |
1600 | 1.06M | code = pdfi_op_Q(ctx); |
1601 | 1.06M | break; |
1602 | 13.2k | case TOKEN_r: /* non-standard set rgb colour for non-stroke */ |
1603 | 13.2k | pdfi_pop(ctx, 1); |
1604 | 13.2k | code = pdfi_setrgbfill_array(ctx); |
1605 | 13.2k | break; |
1606 | 1.75M | case TOKEN_re: /* append rectangle */ |
1607 | 1.75M | pdfi_pop(ctx, 1); |
1608 | 1.75M | code = pdfi_rectpath(ctx); |
1609 | 1.75M | break; |
1610 | 236k | case TOKEN_RG: /* set rgb colour for stroke */ |
1611 | 236k | pdfi_pop(ctx, 1); |
1612 | 236k | code = pdfi_setrgbstroke(ctx); |
1613 | 236k | break; |
1614 | 333k | case TOKEN_rg: /* set rgb colour for non-stroke */ |
1615 | 333k | pdfi_pop(ctx, 1); |
1616 | 333k | code = pdfi_setrgbfill(ctx); |
1617 | 333k | break; |
1618 | 38.3k | case TOKEN_ri: /* set rendering intent */ |
1619 | 38.3k | pdfi_pop(ctx, 1); |
1620 | 38.3k | code = pdfi_ri(ctx); |
1621 | 38.3k | break; |
1622 | 40.4k | case TOKEN_s: /* closepath, stroke */ |
1623 | 40.4k | pdfi_pop(ctx, 1); |
1624 | 40.4k | code = pdfi_closepath_stroke(ctx); |
1625 | 40.4k | break; |
1626 | 781k | case TOKEN_S: /* stroke */ |
1627 | 781k | pdfi_pop(ctx, 1); |
1628 | 781k | code = pdfi_stroke(ctx); |
1629 | 781k | break; |
1630 | 25.4k | case TOKEN_SC: /* set colour for stroke */ |
1631 | 25.4k | pdfi_pop(ctx, 1); |
1632 | 25.4k | code = pdfi_setstrokecolor(ctx); |
1633 | 25.4k | break; |
1634 | 104k | case TOKEN_sc: /* set colour for non-stroke */ |
1635 | 104k | pdfi_pop(ctx, 1); |
1636 | 104k | code = pdfi_setfillcolor(ctx); |
1637 | 104k | break; |
1638 | 5.15k | case TOKEN_SCN: /* set special colour for stroke */ |
1639 | 5.15k | pdfi_pop(ctx, 1); |
1640 | 5.15k | code = pdfi_setcolorN(ctx, stream_dict, page_dict, false); |
1641 | 5.15k | break; |
1642 | 42.4k | case TOKEN_scn: /* set special colour for non-stroke */ |
1643 | 42.4k | pdfi_pop(ctx, 1); |
1644 | 42.4k | code = pdfi_setcolorN(ctx, stream_dict, page_dict, true); |
1645 | 42.4k | break; |
1646 | 57.2k | case TOKEN_sh: /* fill with sahding pattern */ |
1647 | 57.2k | pdfi_pop(ctx, 1); |
1648 | 57.2k | code = pdfi_shading(ctx, stream_dict, page_dict); |
1649 | 57.2k | break; |
1650 | 82.0k | case TOKEN_Tstar: /* Move to start of next text line */ |
1651 | 82.0k | pdfi_pop(ctx, 1); |
1652 | 82.0k | code = pdfi_T_star(ctx); |
1653 | 82.0k | break; |
1654 | 523k | case TOKEN_Tc: /* set character spacing */ |
1655 | 523k | pdfi_pop(ctx, 1); |
1656 | 523k | code = pdfi_Tc(ctx); |
1657 | 523k | break; |
1658 | 547k | case TOKEN_Td: /* move text position */ |
1659 | 547k | pdfi_pop(ctx, 1); |
1660 | 547k | code = pdfi_Td(ctx); |
1661 | 547k | break; |
1662 | 298k | case TOKEN_TD: /* Move text position, set leading */ |
1663 | 298k | pdfi_pop(ctx, 1); |
1664 | 298k | code = pdfi_TD(ctx); |
1665 | 298k | break; |
1666 | 498k | case TOKEN_Tf: /* set font and size */ |
1667 | 498k | pdfi_pop(ctx, 1); |
1668 | 498k | code = pdfi_Tf(ctx, stream_dict, page_dict); |
1669 | 498k | break; |
1670 | 666k | case TOKEN_Tj: /* show text */ |
1671 | 666k | pdfi_pop(ctx, 1); |
1672 | 666k | code = pdfi_Tj(ctx); |
1673 | 666k | break; |
1674 | 950k | case TOKEN_TJ: /* show text with individual glyph positioning */ |
1675 | 950k | pdfi_pop(ctx, 1); |
1676 | 950k | code = pdfi_TJ(ctx); |
1677 | 950k | break; |
1678 | 3.38k | case TOKEN_TL: /* set text leading */ |
1679 | 3.38k | pdfi_pop(ctx, 1); |
1680 | 3.38k | code = pdfi_TL(ctx); |
1681 | 3.38k | break; |
1682 | 700k | case TOKEN_Tm: /* set text matrix */ |
1683 | 700k | pdfi_pop(ctx, 1); |
1684 | 700k | code = pdfi_Tm(ctx); |
1685 | 700k | break; |
1686 | 170k | case TOKEN_Tr: /* set text rendering mode */ |
1687 | 170k | pdfi_pop(ctx, 1); |
1688 | 170k | code = pdfi_Tr(ctx); |
1689 | 170k | break; |
1690 | 1.06k | case TOKEN_Ts: /* set text rise */ |
1691 | 1.06k | pdfi_pop(ctx, 1); |
1692 | 1.06k | code = pdfi_Ts(ctx); |
1693 | 1.06k | break; |
1694 | 124k | case TOKEN_Tw: /* set word spacing */ |
1695 | 124k | pdfi_pop(ctx, 1); |
1696 | 124k | code = pdfi_Tw(ctx); |
1697 | 124k | break; |
1698 | 46.0k | case TOKEN_Tz: /* set text matrix */ |
1699 | 46.0k | pdfi_pop(ctx, 1); |
1700 | 46.0k | code = pdfi_Tz(ctx); |
1701 | 46.0k | break; |
1702 | 150k | case TOKEN_v: /* append curve (initial point replicated) */ |
1703 | 150k | pdfi_pop(ctx, 1); |
1704 | 150k | code = pdfi_v_curveto(ctx); |
1705 | 150k | break; |
1706 | 581k | case TOKEN_w: /* setlinewidth */ |
1707 | 581k | pdfi_pop(ctx, 1); |
1708 | 581k | code = pdfi_setlinewidth(ctx); |
1709 | 581k | break; |
1710 | 269k | case TOKEN_W: /* clip */ |
1711 | 269k | pdfi_pop(ctx, 1); |
1712 | 269k | ctx->clip_active = true; |
1713 | 269k | ctx->do_eoclip = false; |
1714 | 269k | break; |
1715 | 10.4k | case TOKEN_Wstar: /* eoclip */ |
1716 | 10.4k | pdfi_pop(ctx, 1); |
1717 | 10.4k | ctx->clip_active = true; |
1718 | 10.4k | ctx->do_eoclip = true; |
1719 | 10.4k | break; |
1720 | 160k | case TOKEN_y: /* append curve (final point replicated) */ |
1721 | 160k | pdfi_pop(ctx, 1); |
1722 | 160k | code = pdfi_y_curveto(ctx); |
1723 | 160k | break; |
1724 | 3.50k | case TOKEN_APOSTROPHE: /* move to next line and show text */ |
1725 | 3.50k | pdfi_pop(ctx, 1); |
1726 | 3.50k | code = pdfi_singlequote(ctx); |
1727 | 3.50k | break; |
1728 | 2.04k | case TOKEN_QUOTE: /* set word and character spacing, move to next line, show text */ |
1729 | 2.04k | pdfi_pop(ctx, 1); |
1730 | 2.04k | code = pdfi_doublequote(ctx); |
1731 | 2.04k | break; |
1732 | 225 | default: |
1733 | | /* Shouldn't we return an error here? Original code didn't seem to. */ |
1734 | 225 | break; |
1735 | 39.7M | } |
1736 | | /* We use a return value of 1 to indicate a repaired keyword (a pair of operators |
1737 | | * was concatenated, and we split them up). We must not return a value > 0 from here |
1738 | | * to avoid tripping that test. |
1739 | | */ |
1740 | 39.7M | if (code > 0) |
1741 | 0 | code = 0; |
1742 | 39.7M | return code; |
1743 | 39.7M | } else if (((pdf_keyword *)keyword)->length > 3) { |
1744 | 1.46M | if (ctx->args.pdfdebug) { |
1745 | 0 | char Buffer[1024]; |
1746 | 0 | int length; |
1747 | |
|
1748 | 0 | if (((pdf_keyword *)keyword)->length > 1023) |
1749 | 0 | length = 1023; |
1750 | 0 | else |
1751 | 0 | length = ((pdf_keyword *)keyword)->length; |
1752 | |
|
1753 | 0 | memcpy(Buffer, ((pdf_keyword *)keyword)->data, length); |
1754 | 0 | Buffer[length] = 0x00; |
1755 | 0 | dmprintf1(ctx->memory, " %s\n", Buffer); |
1756 | 0 | } |
1757 | | |
1758 | | /* This means we either have a corrupted or illegal operator. The most |
1759 | | * usual corruption is two concatented operators (eg QBT instead of Q BT) |
1760 | | * I plan to tackle this by trying to see if I can make two or more operators |
1761 | | * out of the mangled one. Note this will also be done below in the 'default' |
1762 | | * case where we don't recognise a keyword with 3 or fewer characters. |
1763 | | */ |
1764 | 1.46M | code = split_bogus_operator(ctx, source, stream_dict, page_dict); |
1765 | 1.46M | if (code < 0) |
1766 | 3.68k | return code; |
1767 | 1.46M | if (pdfi_count_stack(ctx) > 0) { |
1768 | 2 | keyword = ctx->stack_top[-1]; |
1769 | 2 | if (keyword != PDF_TOKEN_AS_OBJ(TOKEN_NOT_A_KEYWORD)) |
1770 | 2 | return REPAIRED_KEYWORD; |
1771 | 2 | } |
1772 | 1.46M | } |
1773 | 2.59M | return 0; |
1774 | 42.3M | } |
1775 | | |
1776 | | void local_save_stream_state(pdf_context *ctx, stream_save *local_save) |
1777 | 285k | { |
1778 | | /* copy the 'save_stream' data from the context to a local structure */ |
1779 | 285k | local_save->stream_offset = ctx->current_stream_save.stream_offset; |
1780 | 285k | local_save->gsave_level = ctx->current_stream_save.gsave_level; |
1781 | 285k | local_save->stack_count = ctx->current_stream_save.stack_count; |
1782 | 285k | local_save->group_depth = ctx->current_stream_save.group_depth; |
1783 | 285k | } |
1784 | | |
1785 | | void cleanup_context_interpretation(pdf_context *ctx, stream_save *local_save) |
1786 | 285k | { |
1787 | 285k | pdfi_seek(ctx, ctx->main_stream, ctx->current_stream_save.stream_offset, SEEK_SET); |
1788 | | /* The transparency group implenetation does a gsave, so the end group does a |
1789 | | * grestore. Therefore we need to do this before we check the saved gstate depth |
1790 | | */ |
1791 | 285k | if (ctx->current_stream_save.group_depth != local_save->group_depth) { |
1792 | 0 | pdfi_set_warning(ctx, 0, NULL, W_PDF_GROUPERROR, "pdfi_cleanup_context_interpretation", NULL); |
1793 | 0 | while (ctx->current_stream_save.group_depth > local_save->group_depth) |
1794 | 0 | pdfi_trans_end_group(ctx); |
1795 | 0 | } |
1796 | 285k | if (ctx->pgs->level > ctx->current_stream_save.gsave_level) |
1797 | 6.49k | pdfi_set_warning(ctx, 0, NULL, W_PDF_TOOMANYq, "pdfi_cleanup_context_interpretation", NULL); |
1798 | 285k | if (pdfi_count_stack(ctx) > ctx->current_stream_save.stack_count) |
1799 | 5.31k | pdfi_set_warning(ctx, 0, NULL, W_PDF_STACKGARBAGE, "pdfi_cleanup_context_interpretation", NULL); |
1800 | 380k | while (ctx->pgs->level > ctx->current_stream_save.gsave_level) |
1801 | 94.7k | pdfi_grestore(ctx); |
1802 | 285k | pdfi_clearstack(ctx); |
1803 | 285k | } |
1804 | | |
1805 | | void local_restore_stream_state(pdf_context *ctx, stream_save *local_save) |
1806 | 285k | { |
1807 | | /* Put the entries stored in the context back to what they were on entry |
1808 | | * We shouldn't really need to do this, the cleanup above should mean all the |
1809 | | * entries are properly reset. |
1810 | | */ |
1811 | 285k | ctx->current_stream_save.stream_offset = local_save->stream_offset; |
1812 | 285k | ctx->current_stream_save.gsave_level = local_save->gsave_level; |
1813 | 285k | ctx->current_stream_save.stack_count = local_save->stack_count; |
1814 | 285k | ctx->current_stream_save.group_depth = local_save->group_depth; |
1815 | 285k | } |
1816 | | |
1817 | | void initialise_stream_save(pdf_context *ctx) |
1818 | 285k | { |
1819 | | /* Set up the values in the context to the current values */ |
1820 | 285k | ctx->current_stream_save.stream_offset = pdfi_tell(ctx->main_stream); |
1821 | 285k | ctx->current_stream_save.gsave_level = ctx->pgs->level; |
1822 | 285k | ctx->current_stream_save.stack_count = pdfi_count_total_stack(ctx); |
1823 | 285k | } |
1824 | | |
1825 | | /* Run a stream in a sub-context (saves/restores DefaultQState) */ |
1826 | | int pdfi_run_context(pdf_context *ctx, pdf_stream *stream_obj, |
1827 | | pdf_dict *page_dict, bool stoponerror, const char *desc) |
1828 | 226k | { |
1829 | 226k | int code = 0, code1 = 0; |
1830 | 226k | gs_gstate *DefaultQState = NULL; |
1831 | | /* Save any existing Default* colour spaces */ |
1832 | 226k | gs_color_space *PageDefaultGray = ctx->page.DefaultGray_cs; |
1833 | 226k | gs_color_space *PageDefaultRGB = ctx->page.DefaultRGB_cs; |
1834 | 226k | gs_color_space *PageDefaultCMYK = ctx->page.DefaultCMYK_cs; |
1835 | | |
1836 | 226k | ctx->page.DefaultGray_cs = NULL; |
1837 | 226k | ctx->page.DefaultRGB_cs = NULL; |
1838 | 226k | ctx->page.DefaultCMYK_cs = NULL; |
1839 | | |
1840 | | #if DEBUG_CONTEXT |
1841 | | dbgmprintf(ctx->memory, "pdfi_run_context BEGIN\n"); |
1842 | | #endif |
1843 | | /* If the stream has any Default* colour spaces, replace the page level ones. |
1844 | | * This will derement the reference counts to the current spaces if they are replaced. |
1845 | | */ |
1846 | 226k | code = pdfi_setup_DefaultSpaces(ctx, stream_obj->stream_dict); |
1847 | 226k | if (code < 0) |
1848 | 0 | goto exit; |
1849 | | |
1850 | | /* If no Default* space found, try using the Page level ones (if any) */ |
1851 | 226k | if (ctx->page.DefaultGray_cs == NULL) { |
1852 | 226k | ctx->page.DefaultGray_cs = PageDefaultGray; |
1853 | 226k | rc_increment(PageDefaultGray); |
1854 | 226k | } |
1855 | 226k | if (ctx->page.DefaultRGB_cs == NULL) { |
1856 | 226k | ctx->page.DefaultRGB_cs = PageDefaultRGB; |
1857 | 226k | rc_increment(PageDefaultRGB); |
1858 | 226k | } |
1859 | 226k | if (ctx->page.DefaultCMYK_cs == NULL) { |
1860 | 226k | ctx->page.DefaultCMYK_cs = PageDefaultCMYK; |
1861 | 226k | rc_increment(PageDefaultCMYK); |
1862 | 226k | } |
1863 | | |
1864 | 226k | code = pdfi_copy_DefaultQState(ctx, &DefaultQState); |
1865 | 226k | if (code < 0) |
1866 | 0 | goto exit; |
1867 | | |
1868 | 226k | code = pdfi_set_DefaultQState(ctx, ctx->pgs); |
1869 | 226k | if (code < 0) |
1870 | 0 | goto exit; |
1871 | | |
1872 | 226k | code = pdfi_interpret_inner_content_stream(ctx, stream_obj, page_dict, stoponerror, desc); |
1873 | | |
1874 | 226k | code1 = pdfi_restore_DefaultQState(ctx, &DefaultQState); |
1875 | 226k | if (code >= 0) |
1876 | 226k | code = code1; |
1877 | | |
1878 | 226k | exit: |
1879 | 226k | if (DefaultQState != NULL) { |
1880 | 0 | gs_gstate_free(DefaultQState); |
1881 | 0 | DefaultQState = NULL; |
1882 | 0 | } |
1883 | | |
1884 | | /* Count down any Default* colour spaces */ |
1885 | 226k | rc_decrement(ctx->page.DefaultGray_cs, "pdfi_run_context"); |
1886 | 226k | rc_decrement(ctx->page.DefaultRGB_cs, "pdfi_run_context"); |
1887 | 226k | rc_decrement(ctx->page.DefaultCMYK_cs, "pdfi_run_context"); |
1888 | | |
1889 | | /* And restore the page level ones (if any) */ |
1890 | 226k | ctx->page.DefaultGray_cs = PageDefaultGray; |
1891 | 226k | ctx->page.DefaultRGB_cs = PageDefaultRGB; |
1892 | 226k | ctx->page.DefaultCMYK_cs = PageDefaultCMYK; |
1893 | | |
1894 | | #if DEBUG_CONTEXT |
1895 | | dbgmprintf(ctx->memory, "pdfi_run_context END\n"); |
1896 | | #endif |
1897 | 226k | return code; |
1898 | 226k | } |
1899 | | |
1900 | | |
1901 | | /* Interpret a sub-content stream, with some handling of error recovery, clearing stack, etc. |
1902 | | * This temporarily turns on pdfstoponerror if requested. |
1903 | | * It will make sure the stack is cleared and the gstate is matched. |
1904 | | */ |
1905 | | static int |
1906 | | pdfi_interpret_inner_content(pdf_context *ctx, pdf_c_stream *content_stream, pdf_stream *stream_obj, |
1907 | | pdf_dict *page_dict, bool stoponerror, const char *desc) |
1908 | 242k | { |
1909 | 242k | int code = 0; |
1910 | 242k | bool saved_stoponerror = ctx->args.pdfstoponerror; |
1911 | 242k | stream_save local_entry_save; |
1912 | | |
1913 | 242k | local_save_stream_state(ctx, &local_entry_save); |
1914 | 242k | initialise_stream_save(ctx); |
1915 | | |
1916 | | /* This causes several files to render 'incorrectly', even though they are in some sense |
1917 | | * invalid. It doesn't seem to provide any benefits so I have, for now, removed it. If |
1918 | | * there is a good reason for it we can put it back again. |
1919 | | * FIXME - either restore or remove these lines |
1920 | | * /tests_private/pdf/PDF_2.0_FTS/fts_23_2310.pdf |
1921 | | * /tests_private/pdf/PDF_2.0_FTS/fts_23_2311.pdf |
1922 | | * /tests_private/pdf/PDF_2.0_FTS/fts_23_2312.pdf |
1923 | | * /tests_private/pdf/sumatra/recursive_colorspace.pdf |
1924 | | * /tests_private/pdf/uploads/Bug696410.pdf |
1925 | | * /tests_private/pdf/sumatra/1900_-_cairo_transparency_inefficiency.pdf (with pdfwrite) |
1926 | | */ |
1927 | | #if 0 |
1928 | | /* Stop on error in substream, and also be prepared to clean up the stack */ |
1929 | | if (stoponerror) |
1930 | | ctx->args.pdfstoponerror = true; |
1931 | | #endif |
1932 | | |
1933 | | #if DEBUG_CONTEXT |
1934 | | dbgmprintf1(ctx->memory, "BEGIN %s stream\n", desc); |
1935 | | #endif |
1936 | 242k | code = pdfi_interpret_content_stream(ctx, content_stream, stream_obj, page_dict); |
1937 | | #if DEBUG_CONTEXT |
1938 | | dbgmprintf1(ctx->memory, "END %s stream\n", desc); |
1939 | | #endif |
1940 | | |
1941 | 242k | if (code < 0) |
1942 | 242k | dbgmprintf1(ctx->memory, "ERROR: inner_stream: code %d when rendering stream\n", code); |
1943 | | |
1944 | 242k | ctx->args.pdfstoponerror = saved_stoponerror; |
1945 | | |
1946 | | /* Put our state back the way it was on entry */ |
1947 | | #if PROBE_STREAMS |
1948 | | if (ctx->pgs->level > ctx->current_stream_save.gsave_level || |
1949 | | pdfi_count_stack(ctx) > ctx->current_stream_save.stack_count) |
1950 | | code = ((pdf_context *)0)->first_page; |
1951 | | #endif |
1952 | | |
1953 | 242k | cleanup_context_interpretation(ctx, &local_entry_save); |
1954 | 242k | local_restore_stream_state(ctx, &local_entry_save); |
1955 | 242k | if (!ctx->args.pdfstoponerror) |
1956 | 242k | code = 0; |
1957 | 242k | return code; |
1958 | 242k | } |
1959 | | |
1960 | | /* Interpret inner content from a buffer |
1961 | | */ |
1962 | | int |
1963 | | pdfi_interpret_inner_content_buffer(pdf_context *ctx, byte *content_data, |
1964 | | uint32_t content_length, |
1965 | | pdf_dict *stream_dict, pdf_dict *page_dict, |
1966 | | bool stoponerror, const char *desc) |
1967 | 16.4k | { |
1968 | 16.4k | int code = 0; |
1969 | 16.4k | pdf_c_stream *stream = NULL; |
1970 | 16.4k | pdf_stream *stream_obj = NULL; |
1971 | | |
1972 | 16.4k | code = pdfi_open_memory_stream_from_memory(ctx, content_length, |
1973 | 16.4k | content_data, &stream, true); |
1974 | 16.4k | if (code < 0) |
1975 | 0 | goto exit; |
1976 | | |
1977 | 16.4k | code = pdfi_obj_dict_to_stream(ctx, stream_dict, &stream_obj, false); |
1978 | 16.4k | if (code < 0) |
1979 | 0 | return code; |
1980 | | |
1981 | | /* NOTE: stream gets closed in here */ |
1982 | 16.4k | code = pdfi_interpret_inner_content(ctx, stream, stream_obj, page_dict, stoponerror, desc); |
1983 | 16.4k | pdfi_countdown(stream_obj); |
1984 | 16.4k | exit: |
1985 | 16.4k | return code; |
1986 | 16.4k | } |
1987 | | |
1988 | | /* Interpret inner content from a C string |
1989 | | */ |
1990 | | int |
1991 | | pdfi_interpret_inner_content_c_string(pdf_context *ctx, char *content_string, |
1992 | | pdf_dict *stream_dict, pdf_dict *page_dict, |
1993 | | bool stoponerror, const char *desc) |
1994 | 1.92k | { |
1995 | 1.92k | uint32_t length = (uint32_t)strlen(content_string); |
1996 | 1.92k | bool decrypt_strings; |
1997 | 1.92k | int code; |
1998 | | |
1999 | | |
2000 | | /* Underlying buffer limit is uint32, so handle the extremely unlikely case that |
2001 | | * our string is too big. |
2002 | | */ |
2003 | 1.92k | if (length != strlen(content_string)) |
2004 | 0 | return_error(gs_error_limitcheck); |
2005 | | |
2006 | | /* Since this is a constructed string content, not part of the file, it can never |
2007 | | * be encrypted. So disable decryption during this call. |
2008 | | */ |
2009 | 1.92k | decrypt_strings = ctx->encryption.decrypt_strings; |
2010 | 1.92k | ctx->encryption.decrypt_strings = false; |
2011 | 1.92k | code = pdfi_interpret_inner_content_buffer(ctx, (byte *)content_string, length, |
2012 | 1.92k | stream_dict, page_dict, stoponerror, desc); |
2013 | 1.92k | ctx->encryption.decrypt_strings = decrypt_strings; |
2014 | | |
2015 | 1.92k | return code; |
2016 | 1.92k | } |
2017 | | |
2018 | | /* Interpret inner content from a string |
2019 | | */ |
2020 | | int |
2021 | | pdfi_interpret_inner_content_string(pdf_context *ctx, pdf_string *content_string, |
2022 | | pdf_dict *stream_dict, pdf_dict *page_dict, |
2023 | | bool stoponerror, const char *desc) |
2024 | 14.4k | { |
2025 | 14.4k | return pdfi_interpret_inner_content_buffer(ctx, content_string->data, content_string->length, |
2026 | 14.4k | stream_dict, page_dict, stoponerror, desc); |
2027 | 14.4k | } |
2028 | | |
2029 | | /* Interpret inner content from a stream_dict |
2030 | | */ |
2031 | | int |
2032 | | pdfi_interpret_inner_content_stream(pdf_context *ctx, pdf_stream *stream_obj, |
2033 | | pdf_dict *page_dict, bool stoponerror, const char *desc) |
2034 | 226k | { |
2035 | 226k | return pdfi_interpret_inner_content(ctx, NULL, stream_obj, page_dict, stoponerror, desc); |
2036 | 226k | } |
2037 | | |
2038 | | /* |
2039 | | * Interpret a content stream. |
2040 | | * content_stream -- content to parse. If NULL, get it from the stream_dict |
2041 | | * stream_dict -- dict containing the stream |
2042 | | */ |
2043 | | int |
2044 | | pdfi_interpret_content_stream(pdf_context *ctx, pdf_c_stream *content_stream, |
2045 | | pdf_stream *stream_obj, pdf_dict *page_dict) |
2046 | 306k | { |
2047 | 306k | int code; |
2048 | 306k | pdf_c_stream *stream = NULL, *SubFile_stream = NULL; |
2049 | 306k | pdf_keyword *keyword; |
2050 | 306k | pdf_stream *s = ctx->current_stream; |
2051 | 306k | pdf_obj_type type; |
2052 | 306k | char EODString[] = "endstream"; |
2053 | | |
2054 | | /* Check this stream, and all the streams currently being executed, to see |
2055 | | * if the stream we've been given is already in train. If it is, then we |
2056 | | * have encountered recursion. This can happen if a non-page stream such |
2057 | | * as a Form or Pattern uses a Resource, but does not declare it in it's |
2058 | | * Resources, and instead inherits it from the parent. We cannot detect that |
2059 | | * before the Resource is used, so all we can do is check here. |
2060 | | */ |
2061 | 405k | while (s != NULL && pdfi_type_of(s) == PDF_STREAM) { |
2062 | 98.8k | if (s->object_num > 0) { |
2063 | 98.6k | if (s->object_num == stream_obj->object_num) { |
2064 | 82 | pdf_dict *d = NULL; |
2065 | 82 | bool known = false; |
2066 | | |
2067 | 82 | code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &d); |
2068 | 82 | if (code >= 0) { |
2069 | 82 | code = pdfi_dict_known(ctx, d, "Parent", &known); |
2070 | 82 | if (code >= 0 && known) |
2071 | 4 | (void)pdfi_dict_delete(ctx, d, "Parent"); |
2072 | 82 | } |
2073 | 82 | pdfi_set_error(ctx, 0, NULL, E_PDF_CIRCULARREF, "pdfi_interpret_content_stream", "Aborting stream"); |
2074 | 82 | return_error(gs_error_circular_reference); |
2075 | 82 | } |
2076 | 98.6k | } |
2077 | 98.8k | s = (pdf_stream *)s->parent_obj; |
2078 | 98.8k | } |
2079 | | |
2080 | 306k | if (content_stream != NULL) { |
2081 | 32.7k | stream = content_stream; |
2082 | 273k | } else { |
2083 | 273k | code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, stream_obj), SEEK_SET); |
2084 | 273k | if (code < 0) |
2085 | 0 | return code; |
2086 | | |
2087 | 273k | if (stream_obj->length_valid) |
2088 | 272k | code = pdfi_apply_SubFileDecode_filter(ctx, stream_obj->Length, NULL, ctx->main_stream, &SubFile_stream, false); |
2089 | 1.32k | else |
2090 | 1.32k | code = pdfi_apply_SubFileDecode_filter(ctx, 0, EODString, ctx->main_stream, &SubFile_stream, false); |
2091 | 273k | if (code < 0) |
2092 | 0 | return code; |
2093 | | |
2094 | 273k | code = pdfi_filter(ctx, stream_obj, SubFile_stream, &stream, false); |
2095 | 273k | if (code < 0) { |
2096 | 56 | pdfi_close_file(ctx, SubFile_stream); |
2097 | 56 | return code; |
2098 | 56 | } |
2099 | 273k | } |
2100 | | |
2101 | 306k | pdfi_set_stream_parent(ctx, stream_obj, ctx->current_stream); |
2102 | 306k | ctx->current_stream = stream_obj; |
2103 | | |
2104 | 178M | do { |
2105 | 178M | code = pdfi_read_token(ctx, stream, stream_obj->object_num, stream_obj->generation_num); |
2106 | 178M | if (code < 0) { |
2107 | 127k | if (code == gs_error_ioerror || code == gs_error_VMerror || ctx->args.pdfstoponerror) { |
2108 | 5.32k | if (code == gs_error_ioerror) { |
2109 | 5.32k | pdfi_set_error(ctx, 0, NULL, E_PDF_BADSTREAM, "pdfi_interpret_content_stream", (char *)"**** Error reading a content stream. The page may be incomplete"); |
2110 | 5.32k | } else if (code == gs_error_VMerror) { |
2111 | 0 | pdfi_set_error(ctx, 0, NULL, E_PDF_OUTOFMEMORY, "pdfi_interpret_content_stream", (char *)"**** Error ran out of memory reading a content stream. The page may be incomplete"); |
2112 | 0 | } |
2113 | 5.32k | goto exit; |
2114 | 5.32k | } |
2115 | 121k | continue; |
2116 | 127k | } |
2117 | | |
2118 | 178M | if (pdfi_count_stack(ctx) <= 0) { |
2119 | 117k | if(stream->eof == true) |
2120 | 117k | break; |
2121 | 117k | } |
2122 | | |
2123 | 178M | repaired_keyword: |
2124 | 178M | type = pdfi_type_of(ctx->stack_top[-1]); |
2125 | 178M | if (type == PDF_FAST_KEYWORD) { |
2126 | 39.7M | keyword = (pdf_keyword *)ctx->stack_top[-1]; |
2127 | | |
2128 | 39.7M | switch((uintptr_t)keyword) { |
2129 | 18.7k | case TOKEN_ENDSTREAM: |
2130 | 18.7k | pdfi_pop(ctx,1); |
2131 | 18.7k | goto exit; |
2132 | 0 | break; |
2133 | 11 | case TOKEN_ENDOBJ: |
2134 | 11 | pdfi_clearstack(ctx); |
2135 | 11 | pdfi_set_error(ctx, 0, NULL, E_PDF_MISSINGENDSTREAM, "pdfi_interpret_content_stream", NULL); |
2136 | 11 | if (ctx->args.pdfstoponerror) |
2137 | 0 | code = gs_note_error(gs_error_syntaxerror); |
2138 | 11 | goto exit; |
2139 | 0 | break; |
2140 | 0 | case TOKEN_INVALID_KEY: |
2141 | 0 | pdfi_set_error(ctx, 0, NULL, E_PDF_KEYWORDTOOLONG, "pdfi_interpret_content_stream", NULL); |
2142 | 0 | pdfi_clearstack(ctx); |
2143 | 0 | break; |
2144 | 0 | case TOKEN_TOO_LONG: |
2145 | 0 | pdfi_set_error(ctx, 0, NULL, E_PDF_MISSINGENDSTREAM, "pdfi_interpret_content_stream", NULL); |
2146 | 0 | pdfi_clearstack(ctx); |
2147 | 0 | break; |
2148 | 39.7M | default: |
2149 | 39.7M | goto execute; |
2150 | 39.7M | } |
2151 | 39.7M | } |
2152 | 138M | else if (type == PDF_KEYWORD) |
2153 | 2.59M | { |
2154 | 42.3M | execute: |
2155 | 42.3M | { |
2156 | 42.3M | pdf_dict *stream_dict = NULL; |
2157 | | |
2158 | 42.3M | code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &stream_dict); |
2159 | 42.3M | if (code < 0) |
2160 | 0 | goto exit; |
2161 | | |
2162 | 42.3M | code = pdfi_interpret_stream_operator(ctx, stream, stream_dict, page_dict); |
2163 | 42.3M | if (code == REPAIRED_KEYWORD) |
2164 | 2 | goto repaired_keyword; |
2165 | | |
2166 | 42.3M | if (code < 0) { |
2167 | 1.05M | pdfi_set_error(ctx, code, NULL, E_PDF_TOKENERROR, "pdf_interpret_content_stream", NULL); |
2168 | 1.05M | if (ctx->args.pdfstoponerror) { |
2169 | 0 | pdfi_clearstack(ctx); |
2170 | 0 | goto exit; |
2171 | 0 | } |
2172 | 1.05M | } |
2173 | 42.3M | } |
2174 | 42.3M | } |
2175 | 178M | if(stream->eof == true) |
2176 | 164k | break; |
2177 | 178M | }while(1); |
2178 | | |
2179 | 306k | exit: |
2180 | 306k | ctx->current_stream = pdfi_stream_parent(ctx, stream_obj); |
2181 | 306k | pdfi_clear_stream_parent(ctx, stream_obj); |
2182 | 306k | pdfi_close_file(ctx, stream); |
2183 | 306k | if (SubFile_stream != NULL) |
2184 | 273k | pdfi_close_file(ctx, SubFile_stream); |
2185 | 306k | return code; |
2186 | 306k | } |