/src/ghostpdl/pdf/pdf_file.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2018-2025 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 | | #include "ghostpdf.h" |
17 | | #include "pdf_types.h" |
18 | | #include "pdf_stack.h" |
19 | | #include "pdf_dict.h" |
20 | | #include "pdf_file.h" |
21 | | #include "pdf_int.h" |
22 | | #include "pdf_array.h" |
23 | | #include "pdf_misc.h" |
24 | | #include "pdf_sec.h" |
25 | | #include "pdf_loop_detect.h" |
26 | | #include "stream.h" |
27 | | #include "strimpl.h" |
28 | | #include "strmio.h" |
29 | | #include "gpmisc.h" |
30 | | #include "simscale.h" /* SIMScaleDecode */ |
31 | | #include "szlibx.h" /* Flate */ |
32 | | #include "spngpx.h" /* PNG Predictor */ |
33 | | #include "spdiffx.h" /* Horizontal differencing predictor */ |
34 | | #include "slzwx.h" /* LZW ZLib */ |
35 | | #include "sbrotlix.h" /* Brotli */ |
36 | | #include "sstring.h" /* ASCIIHexDecode */ |
37 | | #include "sa85d.h" /* ASCII85Decode */ |
38 | | #include "scfx.h" /* CCITTFaxDecode */ |
39 | | #include "srlx.h" /* RunLengthDecode */ |
40 | | #include "jpeglib_.h" |
41 | | #include "sdct.h" /* DCTDecode */ |
42 | | #include "sjpeg.h" |
43 | | #include "sfilter.h" /* SubFileDecode and PFBDecode */ |
44 | | #include "sarc4.h" /* Arc4Decode */ |
45 | | #include "saes.h" /* AESDecode */ |
46 | | #include "ssha2.h" /* SHA256Encode */ |
47 | | #include "gxdevsop.h" /* For special ops */ |
48 | | |
49 | | #ifdef USE_LDF_JB2 |
50 | | #include "sjbig2_luratech.h" |
51 | | #else |
52 | | #include "sjbig2.h" |
53 | | #endif |
54 | | #if defined(USE_LWF_JP2) |
55 | | # include "sjpx_luratech.h" |
56 | | #elif defined(USE_OPENJPEG_JP2) |
57 | | # include "sjpx_openjpeg.h" |
58 | | #else |
59 | | # include "sjpx.h" |
60 | | #endif |
61 | | |
62 | | extern const uint file_default_buffer_size; |
63 | | |
64 | | static void pdfi_close_filter_chain(pdf_context *ctx, stream *s, stream *target); |
65 | | |
66 | | /* Utility routine to create a pdf_c_stream object */ |
67 | | static int pdfi_alloc_stream(pdf_context *ctx, stream *source, stream *original, pdf_c_stream **new_stream) |
68 | 5.85M | { |
69 | 5.85M | *new_stream = NULL; |
70 | 5.85M | *new_stream = (pdf_c_stream *)gs_alloc_bytes(ctx->memory, sizeof(pdf_c_stream), "pdfi_alloc_stream"); |
71 | 5.85M | if (*new_stream == NULL) |
72 | 0 | return_error(gs_error_VMerror); |
73 | 5.85M | memset(*new_stream, 0x00, sizeof(pdf_c_stream)); |
74 | 5.85M | (*new_stream)->eof = false; |
75 | 5.85M | ((pdf_c_stream *)(*new_stream))->s = source; |
76 | 5.85M | ((pdf_c_stream *)(*new_stream))->original = original; |
77 | 5.85M | return 0; |
78 | 5.85M | } |
79 | | |
80 | | /***********************************************************************************/ |
81 | | /* Decompression filters. */ |
82 | | |
83 | | static int |
84 | | pdfi_filter_report_error(stream_state * st, const char *str) |
85 | 4.13k | { |
86 | 4.13k | if_debug1m('s', st->memory, "[s]stream error: %s\n", str); |
87 | 4.13k | strncpy(st->error_string, str, STREAM_MAX_ERROR_STRING); |
88 | | /* Ensure null termination. */ |
89 | 4.13k | st->error_string[STREAM_MAX_ERROR_STRING] = 0; |
90 | 4.13k | return 0; |
91 | 4.13k | } |
92 | | |
93 | | /* Open a file stream for a filter. */ |
94 | | static int |
95 | | pdfi_filter_open(uint buffer_size, |
96 | | const stream_procs * procs, const stream_template * templat, |
97 | | const stream_state * st, gs_memory_t *mem, stream **new_stream) |
98 | 5.21M | { |
99 | 5.21M | stream *s; |
100 | 5.21M | uint ssize = gs_struct_type_size(templat->stype); |
101 | 5.21M | stream_state *sst = NULL; |
102 | 5.21M | int code; |
103 | | |
104 | 5.21M | if (templat->stype != &st_stream_state) { |
105 | 5.21M | sst = s_alloc_state(mem, templat->stype, "pdfi_filter_open(stream_state)"); |
106 | 5.21M | if (sst == NULL) |
107 | 0 | return_error(gs_error_VMerror); |
108 | 5.21M | if (templat->set_defaults != NULL) { |
109 | 5.15M | (*templat->set_defaults)(sst); |
110 | 5.15M | } |
111 | 5.21M | } |
112 | 5.21M | if (buffer_size < 128) |
113 | 43.8k | buffer_size = file_default_buffer_size; |
114 | | |
115 | 5.21M | code = file_open_stream((char *)0, 0, "r", buffer_size, &s, |
116 | 5.21M | (gx_io_device *)0, (iodev_proc_fopen_t)0, mem); |
117 | 5.21M | if (code < 0) { |
118 | 0 | gs_free_object(mem, sst, "pdfi_filter_open(stream_state)"); |
119 | 0 | return code; |
120 | 0 | } |
121 | 5.21M | s_std_init(s, s->cbuf, s->bsize, procs, s_mode_read); |
122 | 5.21M | s->procs.process = templat->process; |
123 | 5.21M | s->save_close = s->procs.close; |
124 | 5.21M | s->procs.close = file_close_file; |
125 | 5.21M | s->close_at_eod = 0; |
126 | 5.21M | if (sst == NULL) { |
127 | | /* This stream doesn't have any state of its own. */ |
128 | | /* Hack: use the stream itself as the state. */ |
129 | 0 | sst = (stream_state *) s; |
130 | 5.21M | } else if (st != NULL) /* might not have client parameters */ |
131 | 5.20M | memcpy(sst, st, ssize); |
132 | 5.21M | s->state = sst; |
133 | 5.21M | s_init_state(sst, templat, mem); |
134 | 5.21M | sst->report_error = pdfi_filter_report_error; |
135 | | |
136 | 5.21M | if (templat->init != NULL) { |
137 | 5.19M | code = (*templat->init)(sst); |
138 | 5.19M | if (code < 0) { |
139 | 3 | gs_free_object(mem, sst, "filter_open(stream_state)"); |
140 | 3 | gs_free_object(mem, s->cbuf, "filter_open(buffer)"); |
141 | 3 | gs_free_object(mem, s, "filter_open(stream)"); |
142 | 3 | return code; |
143 | 3 | } |
144 | 5.19M | } |
145 | 5.21M | *new_stream = s; |
146 | 5.21M | return 0; |
147 | 5.21M | } |
148 | | |
149 | | static int pdfi_Predictor_filter(pdf_context *ctx, pdf_dict *d, stream *source, stream **new_stream) |
150 | 46.8k | { |
151 | 46.8k | int code; |
152 | 46.8k | int64_t Predictor, Colors, BPC, Columns; |
153 | 46.8k | uint min_size; |
154 | 46.8k | stream_PNGP_state pps; |
155 | 46.8k | stream_PDiff_state ppds; |
156 | | /* NOTE: 'max_min_left=1' is a horribly named definition from stream.h */ |
157 | | |
158 | 46.8k | code = pdfi_dict_get_int_def(ctx, d, "Predictor", &Predictor, 1); |
159 | 46.8k | if (code < 0) |
160 | 5 | return code; |
161 | | |
162 | | /* Predictor values 0,1 are identity (use the existing stream). |
163 | | * The other values need to be cascaded. |
164 | | */ |
165 | 46.8k | switch(Predictor) { |
166 | 2 | case 0: |
167 | 2 | Predictor = 1; |
168 | 2 | break; |
169 | 2.61k | case 1: |
170 | 2.61k | break; |
171 | 20 | case 2: |
172 | 20 | case 10: |
173 | 28 | case 11: |
174 | 11.7k | case 12: |
175 | 11.7k | case 13: |
176 | 11.7k | case 14: |
177 | 43.9k | case 15: |
178 | | /* grab values common to both kinds of predictors */ |
179 | 43.9k | min_size = s_zlibD_template.min_out_size + max_min_left; |
180 | 43.9k | code = pdfi_dict_get_int_def(ctx, d, "Colors", &Colors, 1); |
181 | 43.9k | if (code < 0) |
182 | 2 | return code; |
183 | 43.9k | if (Colors < 1 || Colors > s_PNG_max_Colors) |
184 | 608 | return_error(gs_error_rangecheck); |
185 | | |
186 | 43.3k | code = pdfi_dict_get_int_def(ctx, d, "BitsPerComponent", &BPC, 8); |
187 | 43.3k | if (code < 0) |
188 | 0 | return code; |
189 | | /* tests for 1-16, powers of 2 */ |
190 | 43.3k | if (BPC < 1 || BPC > 16 || (BPC & (BPC - 1)) != 0) |
191 | 224 | return_error(gs_error_rangecheck); |
192 | | |
193 | 43.1k | code = pdfi_dict_get_int_def(ctx, d, "Columns", &Columns, 1); |
194 | 43.1k | if (code < 0) |
195 | 88 | return code; |
196 | 43.0k | if (Columns < 1) |
197 | 12 | return_error(gs_error_rangecheck); |
198 | 43.0k | break; |
199 | 43.0k | default: |
200 | 208 | return_error(gs_error_rangecheck); |
201 | 46.8k | } |
202 | | |
203 | 45.6k | switch(Predictor) { |
204 | 2.61k | case 1: |
205 | 2.61k | *new_stream = source; |
206 | 2.61k | break; |
207 | 20 | case 2: |
208 | | /* zpd_setup, componentwise horizontal differencing */ |
209 | 20 | ppds.Colors = (int)Colors; |
210 | 20 | ppds.BitsPerComponent = (int)BPC; |
211 | 20 | ppds.Columns = (int)Columns; |
212 | 20 | code = pdfi_filter_open(min_size, &s_filter_read_procs, |
213 | 20 | (const stream_template *)&s_PDiffD_template, |
214 | 20 | (const stream_state *)&ppds, ctx->memory->non_gc_memory, new_stream); |
215 | 20 | if (code < 0) |
216 | 0 | return code; |
217 | | |
218 | 20 | (*new_stream)->strm = source; |
219 | 20 | break; |
220 | 43.0k | default: |
221 | | /* zpp_setup, PNG predictor */ |
222 | 43.0k | pps.Colors = (int)Colors; |
223 | 43.0k | pps.BitsPerComponent = (int)BPC; |
224 | 43.0k | pps.Columns = (uint)Columns; |
225 | 43.0k | pps.Predictor = Predictor; |
226 | 43.0k | code = pdfi_filter_open(min_size, &s_filter_read_procs, |
227 | 43.0k | (const stream_template *)&s_PNGPD_template, |
228 | 43.0k | (const stream_state *)&pps, ctx->memory->non_gc_memory, new_stream); |
229 | 43.0k | if (code < 0) |
230 | 0 | return code; |
231 | | |
232 | 43.0k | (*new_stream)->strm = source; |
233 | 43.0k | break; |
234 | 45.6k | } |
235 | 45.6k | return 0; |
236 | 45.6k | } |
237 | | |
238 | | int pdfi_apply_Arc4_filter(pdf_context *ctx, pdf_string *Key, pdf_c_stream *source, pdf_c_stream **new_stream) |
239 | 16.9k | { |
240 | 16.9k | int code = 0; |
241 | 16.9k | stream_arcfour_state state; |
242 | 16.9k | stream *new_s; |
243 | 16.9k | int min_size = 2048; |
244 | | |
245 | 16.9k | s_arcfour_set_key(&state, (const unsigned char *)Key->data, Key->length); /* lgtm [cpp/weak-cryptographic-algorithm] */ |
246 | | |
247 | 16.9k | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_arcfour_template, (const stream_state *)&state, ctx->memory->non_gc_memory, &new_s); |
248 | 16.9k | if (code < 0) |
249 | 0 | return code; |
250 | | |
251 | 16.9k | code = pdfi_alloc_stream(ctx, new_s, source->s, new_stream); |
252 | 16.9k | if (code >= 0) |
253 | 16.9k | new_s->strm = source->s; |
254 | 16.9k | return code; |
255 | 16.9k | } |
256 | | |
257 | | int pdfi_apply_AES_filter(pdf_context *ctx, pdf_string *Key, bool use_padding, pdf_c_stream *source, pdf_c_stream **new_stream) |
258 | 39.1k | { |
259 | 39.1k | stream_aes_state state; |
260 | 39.1k | uint min_size = 2048; |
261 | 39.1k | int code = 0; |
262 | 39.1k | stream *new_s; |
263 | | |
264 | 39.1k | s_aes_set_key(&state, Key->data, Key->length); |
265 | 39.1k | s_aes_set_padding(&state, use_padding); |
266 | | |
267 | 39.1k | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_aes_template, (const stream_state *)&state, ctx->memory->non_gc_memory, &new_s); |
268 | | |
269 | 39.1k | if (code < 0) |
270 | 0 | return code; |
271 | | |
272 | 39.1k | code = pdfi_alloc_stream(ctx, new_s, source->s, new_stream); |
273 | 39.1k | if (code >= 0) |
274 | 39.1k | new_s->strm = source->s; |
275 | 39.1k | return code; |
276 | 39.1k | } |
277 | | |
278 | | #ifdef UNUSED_FILTER |
279 | | int pdfi_apply_SHA256_filter(pdf_context *ctx, pdf_c_stream *source, pdf_c_stream **new_stream) |
280 | | { |
281 | | stream_SHA256E_state state; |
282 | | uint min_size = 2048; |
283 | | int code = 0; |
284 | | stream *new_s; |
285 | | |
286 | | pSHA256_Init(&state.sha256); |
287 | | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_SHA256E_template, (const stream_state *)&state, ctx->memory->non_gc_memory, &new_s); |
288 | | |
289 | | if (code < 0) |
290 | | return code; |
291 | | |
292 | | code = pdfi_alloc_stream(ctx, new_s, source->s, new_stream); |
293 | | if (code >= 0) |
294 | | new_s->strm = source->s; |
295 | | return code; |
296 | | } |
297 | | #endif |
298 | | |
299 | | int pdfi_apply_imscale_filter(pdf_context *ctx, pdf_string *Key, int width, int height, pdf_c_stream *source, pdf_c_stream **new_stream) |
300 | 0 | { |
301 | 0 | int code = 0; |
302 | 0 | stream_imscale_state state; |
303 | 0 | stream *new_s; |
304 | |
|
305 | 0 | state.params.spp_decode = 1; |
306 | 0 | state.params.spp_interp = 1; |
307 | 0 | state.params.BitsPerComponentIn = 1; |
308 | 0 | state.params.MaxValueIn = 1; |
309 | 0 | state.params.WidthIn = width; |
310 | 0 | state.params.HeightIn = height; |
311 | 0 | state.params.BitsPerComponentOut = 1; |
312 | 0 | state.params.MaxValueOut = 1; |
313 | 0 | state.params.WidthOut = width << 2; |
314 | 0 | state.params.HeightOut = height << 2; |
315 | |
|
316 | 0 | code = pdfi_filter_open(2048, &s_filter_read_procs, (const stream_template *)&s_imscale_template, (const stream_state *)&state, ctx->memory->non_gc_memory, &new_s); |
317 | |
|
318 | 0 | if (code < 0) |
319 | 0 | return code; |
320 | | |
321 | 0 | code = pdfi_alloc_stream(ctx, new_s, source->s, new_stream); |
322 | 0 | if (code >= 0) |
323 | 0 | new_s->strm = source->s; |
324 | 0 | return code; |
325 | 0 | } |
326 | | |
327 | | static int pdfi_Flate_filter(pdf_context *ctx, pdf_dict *d, stream *source, stream **new_stream) |
328 | 2.13M | { |
329 | 2.13M | stream_zlib_state zls; |
330 | 2.13M | uint min_size = 2048; |
331 | 2.13M | int code; |
332 | 2.13M | stream *Flate_source = NULL; |
333 | | |
334 | 2.13M | memset(&zls, 0, sizeof(zls)); |
335 | | |
336 | | /* s_zlibD_template defined in base/szlibd.c */ |
337 | 2.13M | (*s_zlibD_template.set_defaults)((stream_state *)&zls); |
338 | | |
339 | 2.13M | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_zlibD_template, (const stream_state *)&zls, ctx->memory->non_gc_memory, new_stream); |
340 | 2.13M | if (code < 0) |
341 | 0 | return code; |
342 | | |
343 | 2.13M | (*new_stream)->strm = source; |
344 | 2.13M | source = *new_stream; |
345 | | |
346 | 2.13M | if (d && pdfi_type_of(d) == PDF_DICT) { |
347 | 46.8k | Flate_source = (*new_stream)->strm; |
348 | 46.8k | code = pdfi_Predictor_filter(ctx, d, source, new_stream); |
349 | 46.8k | if (code < 0) |
350 | 1.14k | pdfi_close_filter_chain(ctx, source, Flate_source); |
351 | 46.8k | } |
352 | 2.13M | return code; |
353 | 2.13M | } |
354 | | |
355 | | static int pdfi_Brotli_filter(pdf_context *ctx, pdf_dict *d, stream *source, stream **new_stream) |
356 | 0 | { |
357 | 0 | stream_brotlid_state zls; |
358 | 0 | uint min_size = 2048; |
359 | 0 | int code; |
360 | 0 | stream *Brotli_source = NULL; |
361 | |
|
362 | 0 | memset(&zls, 0, sizeof(zls)); |
363 | |
|
364 | 0 | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_brotliD_template, (const stream_state *)&zls, ctx->memory->non_gc_memory, new_stream); |
365 | 0 | if (code < 0) |
366 | 0 | return code; |
367 | | |
368 | 0 | (*new_stream)->strm = source; |
369 | 0 | source = *new_stream; |
370 | |
|
371 | 0 | if (d && pdfi_type_of(d) == PDF_DICT) { |
372 | 0 | Brotli_source = (*new_stream)->strm; |
373 | 0 | code = pdfi_Predictor_filter(ctx, d, source, new_stream); |
374 | 0 | if (code < 0) |
375 | 0 | pdfi_close_filter_chain(ctx, source, Brotli_source); |
376 | 0 | } |
377 | 0 | return code; |
378 | 0 | } |
379 | | |
380 | | static int |
381 | | pdfi_JBIG2Decode_filter(pdf_context *ctx, pdf_dict *dict, pdf_dict *decode, |
382 | | stream *source, stream **new_stream) |
383 | 798 | { |
384 | 798 | stream_jbig2decode_state state; |
385 | 798 | uint min_size = s_jbig2decode_template.min_out_size; |
386 | 798 | int code; |
387 | 798 | pdf_stream *Globals = NULL; |
388 | 798 | byte *buf = NULL; |
389 | 798 | int64_t buflen = 0; |
390 | 798 | void *globalctx; |
391 | | |
392 | 798 | s_jbig2decode_set_global_data((stream_state*)&state, NULL, NULL); |
393 | | |
394 | 798 | if (decode) { |
395 | 0 | code = pdfi_dict_knownget_type(ctx, decode, "JBIG2Globals", PDF_STREAM, |
396 | 0 | (pdf_obj **)&Globals); |
397 | 0 | if (code < 0) { |
398 | 0 | goto cleanupExit; |
399 | 0 | } |
400 | | |
401 | | /* read in the globals from stream */ |
402 | 0 | if (code > 0) { |
403 | 0 | code = pdfi_stream_to_buffer(ctx, Globals, &buf, &buflen); |
404 | 0 | if (code == 0) { |
405 | 0 | code = s_jbig2decode_make_global_data(ctx->memory->non_gc_memory, |
406 | 0 | buf, buflen, &globalctx); |
407 | 0 | if (code < 0) |
408 | 0 | goto cleanupExit; |
409 | | |
410 | 0 | s_jbig2decode_set_global_data((stream_state*)&state, NULL, globalctx); |
411 | 0 | } |
412 | 0 | } |
413 | 0 | } |
414 | | |
415 | 798 | code = pdfi_filter_open(min_size, &s_filter_read_procs, |
416 | 798 | (const stream_template *)&s_jbig2decode_template, |
417 | 798 | (const stream_state *)&state, ctx->memory->non_gc_memory, new_stream); |
418 | 798 | if (code < 0) |
419 | 0 | goto cleanupExit; |
420 | | |
421 | 798 | (*new_stream)->strm = source; |
422 | 798 | code = 0; |
423 | | |
424 | 798 | cleanupExit: |
425 | 798 | gs_free_object(ctx->memory, buf, "pdfi_JBIG2Decode_filter (Globals buf)"); |
426 | 798 | pdfi_countdown(Globals); |
427 | 798 | return code; |
428 | 798 | } |
429 | | |
430 | | static int pdfi_LZW_filter(pdf_context *ctx, pdf_dict *d, stream *source, stream **new_stream) |
431 | 173 | { |
432 | 173 | stream_LZW_state lzs; |
433 | 173 | uint min_size = 2048; |
434 | 173 | int code; |
435 | 173 | int64_t i; |
436 | | |
437 | | /* s_zlibD_template defined in base/szlibd.c */ |
438 | 173 | s_LZW_set_defaults_inline(&lzs); |
439 | | |
440 | 173 | if (d && pdfi_type_of(d) == PDF_DICT) { |
441 | 0 | code = pdfi_dict_get_int(ctx, d, "EarlyChange", &i); |
442 | 0 | if (code < 0 && code != gs_error_undefined) |
443 | 0 | return code; |
444 | 0 | if (code == 0) { |
445 | 0 | if (i == 0) |
446 | 0 | lzs.EarlyChange = false; |
447 | 0 | else |
448 | 0 | lzs.EarlyChange = true; |
449 | 0 | } |
450 | 0 | } |
451 | | |
452 | 173 | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_LZWD_template, (const stream_state *)&lzs, ctx->memory->non_gc_memory, new_stream); |
453 | 173 | if (code < 0) |
454 | 0 | return code; |
455 | 173 | (*new_stream)->strm = source; |
456 | 173 | source = *new_stream; |
457 | | |
458 | 173 | if (d && pdfi_type_of(d) == PDF_DICT) |
459 | 0 | pdfi_Predictor_filter(ctx, d, source, new_stream); |
460 | 173 | return 0; |
461 | 173 | } |
462 | | |
463 | | static int PS_JPXD_PassThrough(void *d, byte *Buffer, int Size) |
464 | 157k | { |
465 | 157k | gx_device *dev = (gx_device *)d; |
466 | | |
467 | 157k | if (Buffer == NULL) { |
468 | 3.73k | if (Size == 0) |
469 | 1.86k | dev_proc(dev, dev_spec_op)(dev, gxdso_JPX_passthrough_end, NULL, 0); |
470 | 1.86k | else |
471 | 1.86k | dev_proc(dev, dev_spec_op)(dev, gxdso_JPX_passthrough_begin, NULL, 0); |
472 | 153k | } else { |
473 | 153k | dev_proc(dev, dev_spec_op)(dev, gxdso_JPX_passthrough_data, Buffer, Size); |
474 | 153k | } |
475 | 157k | return 0; |
476 | 157k | } |
477 | | |
478 | | /* |
479 | | * dict -- the dict that contained the decoder (i.e. the image dict) |
480 | | * decode -- the decoder dict |
481 | | */ |
482 | | static int |
483 | | pdfi_JPX_filter(pdf_context *ctx, pdf_dict *dict, pdf_dict *decode, |
484 | | stream *source, stream **new_stream) |
485 | 13.7k | { |
486 | 13.7k | stream_jpxd_state state; |
487 | 13.7k | uint min_size = s_jpxd_template.min_out_size; |
488 | 13.7k | int code; |
489 | 13.7k | pdf_obj *csobj = NULL; |
490 | 13.7k | pdf_name *csname = NULL; |
491 | 13.7k | bool alpha; |
492 | 13.7k | gx_device *dev = gs_currentdevice(ctx->pgs); |
493 | | |
494 | 13.7k | state.memory = ctx->memory->non_gc_memory; |
495 | 13.7k | if (s_jpxd_template.set_defaults) |
496 | 13.7k | (*s_jpxd_template.set_defaults)((stream_state *)&state); |
497 | | |
498 | | /* Pull some extra params out of the image dict */ |
499 | 13.7k | if (dict) { |
500 | | /* This Alpha is a thing that gs code uses to tell that we |
501 | | * are doing an SMask. It's a bit of a hack, but |
502 | | * I guess we can do the same. |
503 | | */ |
504 | 13.7k | code = pdfi_dict_get_bool(ctx, dict, "Alpha", &alpha); |
505 | 13.7k | if (code == 0) |
506 | 0 | state.alpha = alpha; |
507 | 13.7k | } |
508 | 13.7k | if (dict && pdfi_dict_get(ctx, dict, "ColorSpace", &csobj) == 0) { |
509 | | /* parse the value */ |
510 | 7.24k | switch (pdfi_type_of(csobj)) { |
511 | 5 | case PDF_ARRAY: |
512 | | /* assume it's the first array element */ |
513 | 5 | code = pdfi_array_get(ctx, (pdf_array *)csobj, (uint64_t)0, (pdf_obj **)&csname); |
514 | 5 | if (code < 0) { |
515 | 0 | pdfi_countdown(csobj); |
516 | 0 | return code; |
517 | 0 | } |
518 | 5 | break; |
519 | 7.24k | case PDF_NAME: |
520 | | /* use the name directly */ |
521 | 7.24k | csname = (pdf_name *)csobj; |
522 | 7.24k | csobj = NULL; /* To keep ref counting straight */ |
523 | 7.24k | break; |
524 | 0 | default: |
525 | 0 | code = pdfi_set_error_stop(ctx, gs_note_error(gs_error_undefined), NULL, E_PDF_BAD_JPX_CS, "pdfi_JPX_filter", ""); |
526 | 0 | break; |
527 | 7.24k | } |
528 | 7.24k | if (csname != NULL && pdfi_type_of(csname) == PDF_NAME) { |
529 | | /* request raw index values if the colorspace is /Indexed */ |
530 | 7.24k | if (pdfi_name_is(csname, "Indexed")) |
531 | 5 | state.colorspace = gs_jpx_cs_indexed; |
532 | | /* tell the filter what output we want for other spaces */ |
533 | 7.24k | else if (pdfi_name_is(csname, "DeviceGray")) |
534 | 2.05k | state.colorspace = gs_jpx_cs_gray; |
535 | 5.18k | else if (pdfi_name_is(csname, "DeviceRGB")) |
536 | 5.18k | state.colorspace = gs_jpx_cs_rgb; |
537 | 0 | else if (pdfi_name_is(csname, "DeviceCMYK")) |
538 | 0 | state.colorspace = gs_jpx_cs_cmyk; |
539 | 0 | else if (pdfi_name_is(csname, "ICCBased")) { |
540 | | /* TODO: I don't think this even happens without PS wrapper code? */ |
541 | | #if 0 |
542 | | /* The second array element should be the profile's |
543 | | stream dict */ |
544 | | ref *csdict = csobj->value.refs + 1; |
545 | | ref *nref; |
546 | | ref altname; |
547 | | if (r_is_array(csobj) && (r_size(csobj) > 1) && |
548 | | r_has_type(csdict, t_dictionary)) { |
549 | | check_dict_read(*csdict); |
550 | | /* try to look up the alternate space */ |
551 | | if (dict_find_string(csdict, "Alternate", &nref) > 0) { |
552 | | name_string_ref(imemory, csname, &altname); |
553 | | if (pdfi_name_is(&altname, "DeviceGray")) |
554 | | state.colorspace = gs_jpx_cs_gray; |
555 | | else if (pdfi_name_is(&altname, "DeviceRGB")) |
556 | | state.colorspace = gs_jpx_cs_rgb; |
557 | | else if (pdfi_name_is(&altname, "DeviceCMYK")) |
558 | | state.colorspace = gs_jpx_cs_cmyk; |
559 | | } |
560 | | /* else guess based on the number of components */ |
561 | | if (state.colorspace == gs_jpx_cs_unset && |
562 | | dict_find_string(csdict, "N", &nref) > 0) { |
563 | | if_debug1m('w', imemory, "[w] JPX image has an external %"PRIpsint |
564 | | " channel colorspace\n", nref->value.intval); |
565 | | switch (nref->value.intval) { |
566 | | case 1: state.colorspace = gs_jpx_cs_gray; |
567 | | break; |
568 | | case 3: state.colorspace = gs_jpx_cs_rgb; |
569 | | break; |
570 | | case 4: state.colorspace = gs_jpx_cs_cmyk; |
571 | | break; |
572 | | } |
573 | | } |
574 | | } |
575 | | #endif |
576 | 0 | } |
577 | 7.24k | } |
578 | 7.24k | } |
579 | | |
580 | 13.7k | if (csobj) |
581 | 5 | pdfi_countdown(csobj); |
582 | 13.7k | if (csname) |
583 | 7.24k | pdfi_countdown(csname); |
584 | | |
585 | | |
586 | 13.7k | if (dev_proc(dev, dev_spec_op)(dev, gxdso_JPX_passthrough_query, NULL, 0) > 0) { |
587 | 2.03k | state.StartedPassThrough = 0; |
588 | 2.03k | state.PassThrough = 1; |
589 | 2.03k | state.PassThroughfn = (PS_JPXD_PassThrough); |
590 | 2.03k | state.device = (void *)dev; |
591 | 2.03k | } |
592 | 11.7k | else { |
593 | 11.7k | state.PassThrough = 0; |
594 | 11.7k | state.device = (void *)NULL; |
595 | 11.7k | } |
596 | | |
597 | 13.7k | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_jpxd_template, |
598 | 13.7k | (const stream_state *)&state, ctx->memory->non_gc_memory, new_stream); |
599 | 13.7k | if (code < 0) |
600 | 0 | return code; |
601 | 13.7k | (*new_stream)->strm = source; |
602 | 13.7k | source = *new_stream; |
603 | | |
604 | 13.7k | return 0; |
605 | 13.7k | } |
606 | | |
607 | | private_st_jpeg_decompress_data(); |
608 | | |
609 | | static int PDF_DCTD_PassThrough(void *d, byte *Buffer, int Size) |
610 | 262k | { |
611 | 262k | gx_device *dev = (gx_device *)d; |
612 | | |
613 | 262k | if (Buffer == NULL) { |
614 | 6.02k | if (Size == 0) |
615 | 3.01k | dev_proc(dev, dev_spec_op)(dev, gxdso_JPEG_passthrough_end, NULL, 0); |
616 | 3.01k | else |
617 | 3.01k | dev_proc(dev, dev_spec_op)(dev, gxdso_JPEG_passthrough_begin, NULL, 0); |
618 | 256k | } else { |
619 | 256k | dev_proc(dev, dev_spec_op)(dev, gxdso_JPEG_passthrough_data, Buffer, Size); |
620 | 256k | } |
621 | 262k | return 0; |
622 | 262k | } |
623 | | |
624 | | static int pdfi_DCT_filter(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *decode, |
625 | | stream *source, stream **new_stream) |
626 | 13.3k | { |
627 | 13.3k | stream_DCT_state dcts; |
628 | 13.3k | uint min_size = s_DCTD_template.min_out_size; |
629 | 13.3k | int code; |
630 | 13.3k | int64_t i; |
631 | 13.3k | jpeg_decompress_data *jddp; |
632 | 13.3k | gx_device *dev = gs_currentdevice_inline(ctx->pgs); |
633 | 13.3k | double Height = 0; |
634 | | |
635 | 13.3k | dcts.memory = ctx->memory; |
636 | | /* First allocate space for IJG parameters. */ |
637 | 13.3k | jddp = gs_alloc_struct_immovable(ctx->memory, jpeg_decompress_data, |
638 | 13.3k | &st_jpeg_decompress_data, "pdfi_DCT"); |
639 | 13.3k | if (jddp == 0) |
640 | 0 | return_error(gs_error_VMerror); |
641 | 13.3k | if (s_DCTD_template.set_defaults) |
642 | 13.3k | (*s_DCTD_template.set_defaults) ((stream_state *) & dcts); |
643 | | |
644 | 13.3k | dcts.data.decompress = jddp; |
645 | 13.3k | jddp->memory = dcts.jpeg_memory = ctx->memory; /* set now for allocation */ |
646 | 13.3k | jddp->scanline_buffer = NULL; /* set this early for safe error exit */ |
647 | 13.3k | dcts.report_error = pdfi_filter_report_error; /* in case create fails */ |
648 | 13.3k | if ((code = gs_jpeg_create_decompress(&dcts)) < 0) { |
649 | 0 | gs_jpeg_destroy(&dcts); |
650 | 0 | gs_free_object(ctx->memory, jddp, "zDCTD fail"); |
651 | 0 | return code; |
652 | 0 | } |
653 | | |
654 | 13.3k | if (decode && pdfi_type_of(decode) == PDF_DICT) { |
655 | | /* TODO: Why is this here? 'i' never gets used? */ |
656 | 714 | code = pdfi_dict_get_int(ctx, decode, "ColorTransform", &i); |
657 | 714 | if (code < 0 && code != gs_error_undefined) |
658 | 0 | return code; |
659 | 714 | } |
660 | | |
661 | 13.3k | if (dev_proc(dev, dev_spec_op)(dev, gxdso_JPEG_passthrough_query, NULL, 0) > 0) { |
662 | 3.01k | jddp->StartedPassThrough = 0; |
663 | 3.01k | jddp->PassThrough = 1; |
664 | 3.01k | jddp->PassThroughfn = (PDF_DCTD_PassThrough); |
665 | 3.01k | jddp->device = (void *)dev; |
666 | 3.01k | } |
667 | 10.3k | else { |
668 | 10.3k | jddp->PassThrough = 0; |
669 | 10.3k | jddp->device = (void *)NULL; |
670 | 10.3k | } |
671 | | |
672 | | /* Hack for Bug695112.pdf to grab a height in case it is missing from the JPEG data */ |
673 | 13.3k | code = pdfi_dict_knownget_number(ctx, stream_dict, "Height", &Height); |
674 | 13.3k | if (code < 0) |
675 | 0 | return code; |
676 | 13.3k | jddp->Height = (int)floor(Height); |
677 | | |
678 | 13.3k | jddp->templat = s_DCTD_template; |
679 | | |
680 | 13.3k | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&jddp->templat, (const stream_state *)&dcts, ctx->memory->non_gc_memory, new_stream); |
681 | 13.3k | if (code < 0) |
682 | 0 | return code; |
683 | 13.3k | (*new_stream)->strm = source; |
684 | 13.3k | source = *new_stream; |
685 | | |
686 | 13.3k | return 0; |
687 | 13.3k | } |
688 | | |
689 | | static int pdfi_ASCII85_filter(pdf_context *ctx, pdf_dict *d, stream *source, stream **new_stream) |
690 | 518 | { |
691 | 518 | stream_A85D_state ss; |
692 | 518 | uint min_size = 2048; |
693 | 518 | int code; |
694 | | |
695 | 518 | ss.pdf_rules = true; |
696 | | |
697 | 518 | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_A85D_template, (const stream_state *)&ss, ctx->memory->non_gc_memory, new_stream); |
698 | 518 | if (code < 0) |
699 | 0 | return code; |
700 | | |
701 | 518 | (*new_stream)->strm = source; |
702 | 518 | return 0; |
703 | 518 | } |
704 | | |
705 | | static int pdfi_CCITTFax_filter(pdf_context *ctx, pdf_dict *d, stream *source, stream **new_stream) |
706 | 6.17k | { |
707 | 6.17k | stream_CFD_state ss; |
708 | 6.17k | uint min_size = 2048; |
709 | 6.17k | bool bval; |
710 | 6.17k | int code; |
711 | 6.17k | int64_t i; |
712 | | |
713 | 6.17k | s_CF_set_defaults_inline(&ss); |
714 | | |
715 | 6.17k | if (d && pdfi_type_of(d) == PDF_DICT) { |
716 | 6.13k | code = pdfi_dict_get_int(ctx, d, "K", &i); |
717 | 6.13k | if (code < 0 && code != gs_error_undefined) |
718 | 0 | return code; |
719 | 6.13k | if (code == 0) |
720 | 6.08k | ss.K = i; |
721 | | |
722 | 6.13k | code = pdfi_dict_get_bool(ctx, d, "EndOfLine", &bval); |
723 | 6.13k | if (code < 0 && code != gs_error_undefined) |
724 | 0 | return code; |
725 | 6.13k | if (code == 0) |
726 | 0 | ss.EndOfLine = bval ? 1 : 0; |
727 | | |
728 | 6.13k | code = pdfi_dict_get_bool(ctx, d, "EncodedByteAlign", &bval); |
729 | 6.13k | if (code < 0 && code != gs_error_undefined) |
730 | 0 | return code; |
731 | 6.13k | if (code == 0) |
732 | 0 | ss.EncodedByteAlign = bval ? 1 : 0; |
733 | | |
734 | 6.13k | code = pdfi_dict_get_bool(ctx, d, "EndOfBlock", &bval); |
735 | 6.13k | if (code < 0 && code != gs_error_undefined) |
736 | 0 | return code; |
737 | 6.13k | if (code == 0) |
738 | 652 | ss.EndOfBlock = bval ? 1 : 0; |
739 | | |
740 | 6.13k | code = pdfi_dict_get_bool(ctx, d, "BlackIs1", &bval); |
741 | 6.13k | if (code < 0 && code != gs_error_undefined) |
742 | 0 | return code; |
743 | 6.13k | if (code == 0) |
744 | 105 | ss.BlackIs1 = bval ? 1 : 0; |
745 | | |
746 | 6.13k | code = pdfi_dict_get_int(ctx, d, "Columns", &i); |
747 | 6.13k | if (code < 0 && code != gs_error_undefined) |
748 | 5 | return code; |
749 | 6.12k | if (code == 0) |
750 | 6.09k | ss.Columns = i; |
751 | | |
752 | 6.12k | code = pdfi_dict_get_int(ctx, d, "Rows", &i); |
753 | 6.12k | if (code < 0 && code != gs_error_undefined) |
754 | 0 | return code; |
755 | 6.12k | if (code == 0) |
756 | 1.48k | ss.Rows = i; |
757 | | |
758 | 6.12k | code = pdfi_dict_get_int(ctx, d, "DamagedRowsBeforeError", &i); |
759 | 6.12k | if (code < 0 && code != gs_error_undefined) |
760 | 0 | return code; |
761 | 6.12k | if (code == 0) |
762 | 0 | ss.DamagedRowsBeforeError = i; |
763 | | |
764 | 6.12k | } |
765 | | |
766 | 6.17k | code = pdfi_filter_open(min_size, &s_filter_read_procs, |
767 | 6.17k | (const stream_template *)&s_CFD_template, |
768 | 6.17k | (const stream_state *)&ss, |
769 | 6.17k | ctx->memory->non_gc_memory, new_stream); |
770 | 6.17k | if (code < 0) |
771 | 3 | return code; |
772 | | |
773 | 6.16k | (*new_stream)->strm = source; |
774 | 6.16k | return 0; |
775 | 6.17k | } |
776 | | |
777 | | static int pdfi_RunLength_filter(pdf_context *ctx, pdf_dict *d, stream *source, stream **new_stream) |
778 | 128 | { |
779 | 128 | stream_RLD_state ss; |
780 | 128 | uint min_size = 2048; |
781 | 128 | int code; |
782 | | |
783 | 128 | if (s_RLD_template.set_defaults) |
784 | 128 | (*s_RLD_template.set_defaults) ((stream_state *) & ss); |
785 | | |
786 | 128 | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_RLD_template, (const stream_state *)&ss, ctx->memory->non_gc_memory, new_stream); |
787 | 128 | if (code < 0) |
788 | 0 | return code; |
789 | | |
790 | 128 | (*new_stream)->strm = source; |
791 | 128 | return 0; |
792 | 128 | } |
793 | | |
794 | | static int pdfi_simple_filter(pdf_context *ctx, const stream_template *tmplate, stream *source, stream **new_stream) |
795 | 1.52k | { |
796 | 1.52k | uint min_size = 2048; |
797 | 1.52k | int code; |
798 | | |
799 | 1.52k | code = pdfi_filter_open(min_size, &s_filter_read_procs, tmplate, NULL, ctx->memory->non_gc_memory, new_stream); |
800 | 1.52k | if (code < 0) |
801 | 0 | return code; |
802 | | |
803 | 1.52k | (*new_stream)->strm = source; |
804 | 1.52k | return 0; |
805 | 1.52k | } |
806 | | |
807 | | static int pdfi_apply_filter(pdf_context *ctx, pdf_dict *dict, pdf_name *n, pdf_dict *decode, |
808 | | stream *source, stream **new_stream, bool inline_image) |
809 | 2.17M | { |
810 | 2.17M | int code; |
811 | | |
812 | 2.17M | code = pdfi_loop_detector_mark(ctx); |
813 | 2.17M | if (code < 0) |
814 | 0 | return code; |
815 | | |
816 | 2.17M | if (dict->object_num != 0) { |
817 | 2.11M | code = pdfi_loop_detector_add_object(ctx, dict->object_num); |
818 | 2.11M | if (code < 0) |
819 | 0 | goto cleanupExit; |
820 | 2.11M | } else { |
821 | 60.4k | if (dict->indirect_num != 0) { |
822 | 12.3k | code = pdfi_loop_detector_add_object(ctx, dict->indirect_num); |
823 | 12.3k | if (code < 0) |
824 | 0 | goto cleanupExit; |
825 | 12.3k | } |
826 | 60.4k | } |
827 | | |
828 | 2.17M | if (ctx->args.pdfdebug) |
829 | 0 | { |
830 | 0 | char *str; |
831 | 0 | str = (char *)gs_alloc_bytes(ctx->memory, n->length + 1, "temp string for debug"); |
832 | 0 | if (str == NULL) |
833 | 0 | return_error(gs_error_VMerror); |
834 | 0 | memcpy(str, (const char *)n->data, n->length); |
835 | 0 | str[n->length] = '\0'; |
836 | 0 | outprintf(ctx->memory, "FILTER NAME:%s\n", str); |
837 | 0 | gs_free_object(ctx->memory, str, "temp string for debug"); |
838 | 0 | } |
839 | | |
840 | 2.17M | if (pdfi_name_is(n, "RunLengthDecode")) { |
841 | 128 | code = pdfi_RunLength_filter(ctx, decode, source, new_stream); |
842 | 128 | goto cleanupExit; |
843 | 128 | } |
844 | 2.17M | if (pdfi_name_is(n, "CCITTFaxDecode")) { |
845 | 2.30k | code = pdfi_CCITTFax_filter(ctx, decode, source, new_stream); |
846 | 2.30k | goto cleanupExit; |
847 | 2.30k | } |
848 | 2.17M | if (pdfi_name_is(n, "ASCIIHexDecode")) { |
849 | 686 | code = pdfi_simple_filter(ctx, &s_AXD_template, source, new_stream); |
850 | 686 | goto cleanupExit; |
851 | 686 | } |
852 | 2.17M | if (pdfi_name_is(n, "ASCII85Decode")) { |
853 | 359 | code = pdfi_ASCII85_filter(ctx, decode, source, new_stream); |
854 | 359 | goto cleanupExit; |
855 | 359 | } |
856 | 2.17M | if (pdfi_name_is(n, "FlateDecode")) { |
857 | 2.13M | code = pdfi_Flate_filter(ctx, decode, source, new_stream); |
858 | 2.13M | goto cleanupExit; |
859 | 2.13M | } |
860 | 37.0k | if (pdfi_name_is(n, "JBIG2Decode")) { |
861 | 798 | code = pdfi_JBIG2Decode_filter(ctx, dict, decode, source, new_stream); |
862 | 798 | goto cleanupExit; |
863 | 798 | } |
864 | 36.2k | if (pdfi_name_is(n, "LZWDecode")) { |
865 | 173 | code = pdfi_LZW_filter(ctx, decode, source, new_stream); |
866 | 173 | goto cleanupExit; |
867 | 173 | } |
868 | 36.0k | if (pdfi_name_is(n, "DCTDecode")) { |
869 | 13.2k | code = pdfi_DCT_filter(ctx, dict, decode, source, new_stream); |
870 | 13.2k | goto cleanupExit; |
871 | 13.2k | } |
872 | 22.7k | if (pdfi_name_is(n, "JPXDecode")) { |
873 | 13.7k | code = pdfi_JPX_filter(ctx, dict, decode, source, new_stream); |
874 | 13.7k | goto cleanupExit; |
875 | 13.7k | } |
876 | 9.00k | if (pdfi_name_is(n, "BrotliDecode")) { |
877 | 0 | code = pdfi_Brotli_filter(ctx, decode, source, new_stream); |
878 | 0 | goto cleanupExit; |
879 | 0 | } |
880 | | |
881 | 9.00k | if (pdfi_name_is(n, "AHx")) { |
882 | 837 | if (!inline_image) { |
883 | 91 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINEFILTER, "pdfi_apply_filter", NULL)) < 0) |
884 | 0 | goto cleanupExit; |
885 | 91 | } |
886 | 837 | code = pdfi_simple_filter(ctx, &s_AXD_template, source, new_stream); |
887 | 837 | goto cleanupExit; |
888 | 837 | } |
889 | 8.17k | if (pdfi_name_is(n, "A85")) { |
890 | 159 | if (!inline_image) { |
891 | 159 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINEFILTER, "pdfi_apply_filter", NULL)) < 0) |
892 | 0 | goto cleanupExit; |
893 | 159 | } |
894 | 159 | code = pdfi_ASCII85_filter(ctx, decode, source, new_stream); |
895 | 159 | goto cleanupExit; |
896 | 159 | } |
897 | 8.01k | if (pdfi_name_is(n, "LZW")) { |
898 | 0 | if (!inline_image) { |
899 | 0 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINEFILTER, "pdfi_apply_filter", NULL)) < 0) |
900 | 0 | goto cleanupExit; |
901 | 0 | } |
902 | 0 | code = pdfi_LZW_filter(ctx, decode, source, new_stream); |
903 | 0 | goto cleanupExit; |
904 | 0 | } |
905 | 8.01k | if (pdfi_name_is(n, "CCF")) { |
906 | 3.87k | if (!inline_image) { |
907 | 0 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINEFILTER, "pdfi_apply_filter", NULL)) < 0) |
908 | 0 | goto cleanupExit; |
909 | 0 | } |
910 | 3.87k | code = pdfi_CCITTFax_filter(ctx, decode, source, new_stream); |
911 | 3.87k | goto cleanupExit; |
912 | 3.87k | } |
913 | 4.14k | if (pdfi_name_is(n, "DCT")) { |
914 | 90 | if (!inline_image) { |
915 | 0 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINEFILTER, "pdfi_apply_filter", NULL)) < 0) |
916 | 0 | goto cleanupExit; |
917 | 0 | } |
918 | 90 | code = pdfi_DCT_filter(ctx, dict, decode, source, new_stream); |
919 | 90 | goto cleanupExit; |
920 | 90 | } |
921 | 4.05k | if (pdfi_name_is(n, "Fl")) { |
922 | 207 | if (!inline_image) { |
923 | 15 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINEFILTER, "pdfi_apply_filter", NULL)) < 0) |
924 | 0 | goto cleanupExit; |
925 | 15 | } |
926 | 207 | code = pdfi_Flate_filter(ctx, decode, source, new_stream); |
927 | 207 | goto cleanupExit; |
928 | 207 | } |
929 | 3.84k | if (pdfi_name_is(n, "RL")) { |
930 | 0 | if (!inline_image) { |
931 | 0 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINEFILTER, "pdfi_apply_filter", NULL)) < 0) |
932 | 0 | goto cleanupExit; |
933 | 0 | } |
934 | 0 | code = pdfi_RunLength_filter(ctx, decode, source, new_stream); |
935 | 0 | goto cleanupExit; |
936 | 0 | } |
937 | 3.84k | if (pdfi_name_is(n, "Br")) { |
938 | 0 | if (!inline_image) { |
939 | 0 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_syntaxerror), NULL, W_PDF_BAD_INLINEFILTER, "pdfi_apply_filter", NULL)) < 0) |
940 | 0 | goto cleanupExit; |
941 | 0 | } |
942 | 0 | code = pdfi_Brotli_filter(ctx, decode, source, new_stream); |
943 | 0 | goto cleanupExit; |
944 | 0 | } |
945 | | |
946 | 3.84k | pdfi_set_error(ctx, 0, NULL, E_PDF_UNKNOWNFILTER, "pdfi_apply_filter", NULL); |
947 | 3.84k | code = gs_error_undefined; |
948 | | |
949 | 2.17M | cleanupExit: |
950 | 2.17M | pdfi_loop_detector_cleartomark(ctx); |
951 | 2.17M | return code; |
952 | 3.84k | } |
953 | | |
954 | | int pdfi_filter_no_decryption(pdf_context *ctx, pdf_stream *stream_obj, |
955 | | pdf_c_stream *source, pdf_c_stream **new_stream, bool inline_image) |
956 | 2.59M | { |
957 | 2.59M | pdf_obj *o = NULL, *o1 = NULL; |
958 | 2.59M | pdf_obj *decode = NULL; |
959 | 2.59M | pdf_obj *Filter = NULL; |
960 | 2.59M | pdf_dict *stream_dict = NULL; |
961 | 2.59M | pdf_array *DecodeParams = NULL; |
962 | 2.59M | int code; |
963 | 2.59M | int64_t i, j, duplicates; |
964 | 2.59M | stream *s = source->s, *new_s = NULL; |
965 | | |
966 | 2.59M | *new_stream = NULL; |
967 | | |
968 | 2.59M | if (ctx->args.pdfdebug) { |
969 | 0 | gs_offset_t stream_offset = pdfi_stream_offset(ctx, stream_obj); |
970 | 0 | outprintf(ctx->memory, "Filter: offset %ld(0x%lx)\n", stream_offset, stream_offset); |
971 | 0 | } |
972 | | |
973 | 2.59M | code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &stream_dict); |
974 | 2.59M | if (code < 0) |
975 | 0 | goto exit; |
976 | | |
977 | | /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. */ |
978 | 2.59M | if (inline_image) { |
979 | 224k | code = pdfi_dict_knownget(ctx, stream_dict, "F", &Filter); |
980 | 224k | if (code == 0) |
981 | 219k | code = pdfi_dict_knownget(ctx, stream_dict, "Filter", &Filter); |
982 | 224k | } else |
983 | 2.36M | code = pdfi_dict_knownget(ctx, stream_dict, "Filter", &Filter); |
984 | | |
985 | 2.59M | if (code < 0) |
986 | 0 | goto exit; |
987 | 2.59M | if (code == 0) { |
988 | | /* No filter, just open the stream */ |
989 | 412k | code = pdfi_alloc_stream(ctx, s, source->s, new_stream); |
990 | 412k | goto exit; |
991 | 412k | } |
992 | | |
993 | 2.17M | switch (pdfi_type_of(Filter)) { |
994 | 12 | default: |
995 | 12 | code = gs_note_error(gs_error_typecheck); |
996 | 12 | goto exit; |
997 | 2.13M | case PDF_NAME: |
998 | | /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. */ |
999 | 2.13M | if (inline_image) { |
1000 | 3.99k | code = pdfi_dict_knownget(ctx, stream_dict, "DP", &decode); |
1001 | 3.99k | if (code == 0) |
1002 | 117 | code = pdfi_dict_knownget(ctx, stream_dict, "DecodeParms", &decode); |
1003 | 2.13M | } else { |
1004 | 2.13M | code = pdfi_dict_knownget(ctx, stream_dict, "DecodeParms", &decode); |
1005 | 2.13M | if (code == 0) |
1006 | 2.08M | code = pdfi_dict_knownget(ctx, stream_dict, "DP", &decode); |
1007 | 2.13M | } |
1008 | 2.13M | if (code < 0) |
1009 | 11 | goto exit; |
1010 | | |
1011 | 2.13M | code = pdfi_apply_filter(ctx, stream_dict, (pdf_name *)Filter, |
1012 | 2.13M | (pdf_dict *)decode, s, &new_s, inline_image); |
1013 | 2.13M | if (code < 0) |
1014 | 4.91k | goto exit; |
1015 | | |
1016 | 2.12M | code = pdfi_alloc_stream(ctx, new_s, source->s, new_stream); |
1017 | 2.12M | break; |
1018 | 43.5k | case PDF_ARRAY: |
1019 | 43.5k | { |
1020 | 43.5k | pdf_array *filter_array = (pdf_array *)Filter; |
1021 | | |
1022 | | /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precendence. */ |
1023 | 43.5k | if (inline_image) { |
1024 | 973 | code = pdfi_dict_knownget(ctx, stream_dict, "DP", (pdf_obj **)&DecodeParams); |
1025 | 973 | if (code == 0) |
1026 | 867 | code = pdfi_dict_knownget(ctx, stream_dict, "DecodeParms", (pdf_obj **)&DecodeParams); |
1027 | 42.5k | } else { |
1028 | 42.5k | code = pdfi_dict_knownget(ctx, stream_dict, "DecodeParms", (pdf_obj **)&DecodeParams); |
1029 | 42.5k | if (code == 0) |
1030 | 36.6k | code = pdfi_dict_knownget(ctx, stream_dict, "DP", &decode); |
1031 | 42.5k | } |
1032 | 43.5k | if (code < 0) |
1033 | 0 | goto exit; |
1034 | | |
1035 | 43.5k | if (DecodeParams != NULL) { |
1036 | 5.95k | if (pdfi_array_size(DecodeParams) == 0 || pdfi_array_size(DecodeParams) != pdfi_array_size(filter_array)) { |
1037 | 70 | pdfi_countdown(DecodeParams); |
1038 | 70 | DecodeParams = NULL; |
1039 | 70 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_rangecheck), NULL, W_PDF_STREAM_BAD_DECODEPARMS, "pdfi_filter_no_decryption", NULL)) < 0) |
1040 | 0 | goto exit; |
1041 | 5.88k | } else { |
1042 | 5.88k | if (pdfi_array_size(DecodeParams) != pdfi_array_size(filter_array)) { |
1043 | 0 | code = gs_note_error(gs_error_typecheck); |
1044 | 0 | goto exit; |
1045 | 0 | } |
1046 | 5.88k | } |
1047 | 5.95k | } |
1048 | | |
1049 | | /* Check the Filter array to see if we have any duplicates (to prevent filter bombs) |
1050 | | * For now we will allow one duplicate (in case people do stupid things like ASCIIEncode |
1051 | | * and Flate and ASCIIEncode again or something). |
1052 | | */ |
1053 | 44.5k | for (i = 0; i < (int)pdfi_array_size(filter_array) - 1;i++) { |
1054 | 1.00k | code = pdfi_array_get_type(ctx, filter_array, i, PDF_NAME, &o); |
1055 | 1.00k | if (code < 0) |
1056 | 0 | goto exit; |
1057 | 1.00k | duplicates = 0; |
1058 | | |
1059 | 2.03k | for (j = i + 1; j < pdfi_array_size(filter_array);j++) { |
1060 | 1.03k | code = pdfi_array_get_type(ctx, filter_array, j, PDF_NAME, &o1); |
1061 | 1.03k | if (code < 0) { |
1062 | 0 | goto exit; |
1063 | 0 | } |
1064 | 1.03k | if (((pdf_name *)o)->length == ((pdf_name *)o1)->length) { |
1065 | 24 | if (memcmp(((pdf_name *)o)->data, ((pdf_name *)o1)->data, ((pdf_name *)o)->length) == 0) |
1066 | 11 | duplicates++; |
1067 | 24 | } |
1068 | 1.03k | pdfi_countdown(o1); |
1069 | 1.03k | o1 = NULL; |
1070 | 1.03k | } |
1071 | 1.00k | pdfi_countdown(o); |
1072 | 1.00k | o = NULL; |
1073 | 1.00k | if (duplicates > 2) { |
1074 | 0 | pdfi_set_error(ctx, 0, NULL, E_PDF_BADSTREAM, "pdfi_filter_nodecryption", (char *)"**** ERROR Detected possible filter bomb (duplicate Filters). Aborting processing"); |
1075 | 0 | code = gs_note_error(gs_error_syntaxerror); |
1076 | 0 | goto exit; |
1077 | 0 | } |
1078 | 1.00k | } |
1079 | | |
1080 | 87.8k | for (i = 0; i < pdfi_array_size(filter_array);i++) { |
1081 | 44.4k | code = pdfi_array_get_type(ctx, filter_array, i, PDF_NAME, &o); |
1082 | 44.4k | if (code < 0) |
1083 | 0 | goto error; |
1084 | 44.4k | if (DecodeParams != NULL) { |
1085 | 6.25k | code = pdfi_array_get(ctx, DecodeParams, i, &decode); |
1086 | 6.25k | if (code < 0) { |
1087 | 13 | goto error; |
1088 | 13 | } |
1089 | 6.25k | } |
1090 | 44.4k | if (decode && decode != PDF_NULL_OBJ && pdfi_type_of(decode) != PDF_DICT) { |
1091 | 0 | pdfi_countdown(decode); |
1092 | 0 | decode = NULL; |
1093 | 0 | if ((code = pdfi_set_warning_stop(ctx, gs_note_error(gs_error_typecheck), NULL, W_PDF_STREAM_BAD_DECODEPARMS, "pdfi_filter_no_decryption", NULL)) < 0) |
1094 | 0 | goto error; |
1095 | 0 | } |
1096 | 44.4k | if (decode && decode == PDF_NULL_OBJ) { |
1097 | 478 | pdfi_countdown(decode); |
1098 | 478 | decode = NULL; |
1099 | 478 | } |
1100 | | |
1101 | 44.4k | code = pdfi_apply_filter(ctx, stream_dict, (pdf_name *)o, |
1102 | 44.4k | (pdf_dict *)decode, s, &new_s, inline_image); |
1103 | 44.4k | pdfi_countdown(decode); |
1104 | 44.4k | decode = NULL; |
1105 | 44.4k | pdfi_countdown(o); |
1106 | 44.4k | o = NULL; |
1107 | 44.4k | if (code < 0) |
1108 | 89 | goto error; |
1109 | | |
1110 | 44.3k | s = new_s; |
1111 | 44.3k | } |
1112 | 43.4k | code = pdfi_alloc_stream(ctx, s, source->s, new_stream); |
1113 | 43.4k | } |
1114 | 2.17M | } |
1115 | | |
1116 | 2.59M | exit: |
1117 | 2.59M | pdfi_countdown(o); |
1118 | 2.59M | pdfi_countdown(o1); |
1119 | 2.59M | pdfi_countdown(DecodeParams); |
1120 | 2.59M | pdfi_countdown(decode); |
1121 | 2.59M | pdfi_countdown(Filter); |
1122 | 2.59M | return code; |
1123 | | |
1124 | 102 | error: |
1125 | 102 | if (s) |
1126 | 102 | pdfi_close_filter_chain(ctx, s, source->s); |
1127 | 102 | *new_stream = NULL; |
1128 | 102 | pdfi_countdown(o); |
1129 | 102 | pdfi_countdown(o1); |
1130 | 102 | pdfi_countdown(DecodeParams); |
1131 | 102 | pdfi_countdown(decode); |
1132 | 102 | pdfi_countdown(Filter); |
1133 | 102 | return code; |
1134 | 2.17M | } |
1135 | | |
1136 | | int pdfi_filter(pdf_context *ctx, pdf_stream *stream_obj, pdf_c_stream *source, |
1137 | | pdf_c_stream **new_stream, bool inline_image) |
1138 | 2.57M | { |
1139 | 2.57M | int code; |
1140 | 2.57M | pdf_c_stream *crypt_stream = NULL, *SubFile_stream = NULL; |
1141 | 2.57M | pdf_string *StreamKey = NULL; |
1142 | 2.57M | pdf_dict *stream_dict = NULL; |
1143 | 2.57M | pdf_obj *FileSpec = NULL; |
1144 | 2.57M | pdf_stream *NewStream = NULL; |
1145 | 2.57M | bool known = false; |
1146 | | |
1147 | 2.57M | *new_stream = NULL; |
1148 | | |
1149 | 2.57M | code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &stream_dict); |
1150 | 2.57M | if (code < 0) |
1151 | 0 | goto error; |
1152 | | |
1153 | | /* Horrifyingly, any stream dictionary can contain a file specification, which means that |
1154 | | * instead of using the stream from the PDF file we must use an external file. |
1155 | | * So much for portability! |
1156 | | * Note: We must not do this for inline images as an inline image dictionary can |
1157 | | * contain the abbreviation /F for the Filter, and an inline image is never a |
1158 | | * separate stream, it is (obviously) contained in the current stream. |
1159 | | */ |
1160 | 2.57M | if (!inline_image) { |
1161 | 2.35M | code = pdfi_dict_known(ctx, stream_dict, "F", &known); |
1162 | 2.35M | if (code >= 0 && known) { |
1163 | 8 | pdf_obj *FS = NULL, *o = NULL; |
1164 | 8 | pdf_dict *dict = NULL; |
1165 | 8 | stream *gstream = NULL; |
1166 | 8 | char CFileName[gp_file_name_sizeof]; |
1167 | | |
1168 | 8 | code = pdfi_dict_get(ctx, stream_dict, "F", &FileSpec); |
1169 | 8 | if (code < 0) |
1170 | 0 | goto error; |
1171 | 8 | if (pdfi_type_of(FileSpec) == PDF_DICT) { |
1172 | | /* We don't really support FileSpec dictionaries, partly because we |
1173 | | * don't really know which platform to use. If there is a /F string |
1174 | | * then we will use that, just as if we had been given a string in |
1175 | | * the first place. |
1176 | | */ |
1177 | 0 | code = pdfi_dict_knownget(ctx, (pdf_dict *)FileSpec, "F", &FS); |
1178 | 0 | if (code < 0) { |
1179 | 0 | goto error; |
1180 | 0 | } |
1181 | 0 | pdfi_countdown(FileSpec); |
1182 | 0 | FileSpec = FS; |
1183 | 0 | FS = NULL; |
1184 | 0 | } |
1185 | 8 | if (pdfi_type_of(FileSpec) != PDF_STRING) { |
1186 | 8 | code = gs_note_error(gs_error_typecheck); |
1187 | 8 | goto error; |
1188 | 8 | } |
1189 | | |
1190 | 0 | if (((pdf_string *)FileSpec)->length + 1 > gp_file_name_sizeof) { |
1191 | 0 | code = gs_note_error(gs_error_ioerror); |
1192 | 0 | goto error; |
1193 | 0 | } |
1194 | 0 | memcpy(CFileName, ((pdf_string *)FileSpec)->data, ((pdf_string *)FileSpec)->length); |
1195 | 0 | CFileName[((pdf_string *)FileSpec)->length] = 0x00; |
1196 | | |
1197 | | /* We should now have a string with the filename (or URL). We need |
1198 | | * to open the file and create a stream, if that succeeds. |
1199 | | */ |
1200 | 0 | gstream = sfopen((const char *)CFileName, "r", ctx->memory); |
1201 | 0 | if (gstream == NULL) { |
1202 | 0 | emprintf1(ctx->memory, "Failed to open file %s\n", CFileName); |
1203 | 0 | code = gs_note_error(gs_error_ioerror); |
1204 | 0 | goto error; |
1205 | 0 | } |
1206 | | |
1207 | 0 | source = (pdf_c_stream *)gs_alloc_bytes(ctx->memory, sizeof(pdf_c_stream), "external stream"); |
1208 | 0 | if (source == NULL) { |
1209 | 0 | code = gs_note_error(gs_error_VMerror); |
1210 | 0 | goto error; |
1211 | 0 | } |
1212 | 0 | memset(source, 0x00, sizeof(pdf_c_stream)); |
1213 | 0 | source->s = gstream; |
1214 | |
|
1215 | 0 | code = pdfi_object_alloc(ctx, PDF_STREAM, 0, (pdf_obj **)&NewStream); |
1216 | 0 | if (code < 0) |
1217 | 0 | goto error; |
1218 | 0 | pdfi_countup(NewStream); |
1219 | 0 | code = pdfi_dict_alloc(ctx, 32, &dict); |
1220 | 0 | if (code < 0){ |
1221 | 0 | pdfi_countdown(NewStream); |
1222 | 0 | goto error; |
1223 | 0 | } |
1224 | 0 | pdfi_countup(dict); |
1225 | 0 | NewStream->stream_dict = dict; |
1226 | 0 | code = pdfi_dict_get(ctx, stream_dict, "FFilter", &o); |
1227 | 0 | if (code >= 0) { |
1228 | 0 | code = pdfi_dict_put(ctx, NewStream->stream_dict, "Filter", o); |
1229 | 0 | if (code < 0) { |
1230 | 0 | pdfi_countdown(NewStream); |
1231 | 0 | goto error; |
1232 | 0 | } |
1233 | 0 | } |
1234 | 0 | code = pdfi_dict_get(ctx, stream_dict, "FPredictor", &o); |
1235 | 0 | if (code >= 0) { |
1236 | 0 | code = pdfi_dict_put(ctx, NewStream->stream_dict, "Predictor", o); |
1237 | 0 | if (code < 0) { |
1238 | 0 | pdfi_countdown(NewStream); |
1239 | 0 | goto error; |
1240 | 0 | } |
1241 | 0 | } |
1242 | 0 | pdfi_countup(NewStream->stream_dict); |
1243 | 0 | NewStream->stream_offset = 0; |
1244 | 0 | NewStream->Length = 0; |
1245 | 0 | NewStream->length_valid = 0; |
1246 | 0 | NewStream->stream_written = 0; |
1247 | 0 | NewStream->is_marking = 0; |
1248 | 0 | NewStream->parent_obj = NULL; |
1249 | 0 | stream_obj = NewStream; |
1250 | 0 | stream_dict = NewStream->stream_dict; |
1251 | 0 | } |
1252 | 2.35M | } |
1253 | | |
1254 | | /* If the file isn't encrypted, don't apply encryption. If this is an inline |
1255 | | * image then its in a content stream and will already be decrypted, so don't |
1256 | | * apply decryption again. |
1257 | | */ |
1258 | 2.57M | if (ctx->encryption.is_encrypted && !inline_image) { |
1259 | 29.8k | int64_t Length; |
1260 | | |
1261 | 29.8k | if (ctx->encryption.StmF == CRYPT_IDENTITY) |
1262 | 0 | return pdfi_filter_no_decryption(ctx, stream_obj, source, new_stream, inline_image); |
1263 | | |
1264 | 29.8k | code = pdfi_dict_get_type(ctx, stream_dict, "StreamKey", PDF_STRING, (pdf_obj **)&StreamKey); |
1265 | 29.8k | if (code == gs_error_undefined) { |
1266 | 8.86k | code = pdfi_compute_objkey(ctx, (pdf_obj *)stream_dict, &StreamKey); |
1267 | 8.86k | if (code < 0) |
1268 | 0 | return code; |
1269 | 8.86k | code = pdfi_dict_put(ctx, stream_dict, "StreamKey", (pdf_obj *)StreamKey); |
1270 | 8.86k | if (code < 0) |
1271 | 0 | goto error; |
1272 | 8.86k | } |
1273 | 29.8k | if (code < 0) |
1274 | 0 | return code; |
1275 | | |
1276 | | /* If we are applying a decryption filter we must also apply a SubFileDecode filter. |
1277 | | * This is because the underlying stream may not have a compression filter, if it doesn't |
1278 | | * then we have no way of detecting the end of the data. Normally we would get an 'endstream' |
1279 | | * token but if we have applied a decryption filter then we'll 'decrypt' that token |
1280 | | * and that will corrupt it. So make sure we can't read past the end of the stream |
1281 | | * by applying a SubFileDecode. |
1282 | | * NB applying a SubFileDecode filter with an EODString seems to limit the amount of data |
1283 | | * that the decode filter is prepared to return at any time to the size of the EODString. |
1284 | | * This doesn't play well with other filters (eg the AESDecode filter) which require a |
1285 | | * larger minimum to be returned (16 bytes for AESDecode). So I'm using the filter |
1286 | | * Length here, even though I'd prefer not to..... |
1287 | | */ |
1288 | 29.8k | Length = pdfi_stream_length(ctx, stream_obj); |
1289 | | |
1290 | 29.8k | if (Length <= 0) { |
1291 | | /* Don't treat as an encrypted stream if Length is 0 */ |
1292 | 220 | pdfi_countdown(StreamKey); |
1293 | 220 | return pdfi_filter_no_decryption(ctx, stream_obj, source, new_stream, inline_image); |
1294 | 220 | } |
1295 | | |
1296 | 29.6k | code = pdfi_apply_SubFileDecode_filter(ctx, Length, NULL, source, &SubFile_stream, false); |
1297 | 29.6k | if (code < 0) |
1298 | 0 | goto error; |
1299 | | |
1300 | 29.6k | SubFile_stream->original = source->s; |
1301 | | |
1302 | 29.6k | switch(ctx->encryption.StmF) { |
1303 | 0 | case CRYPT_IDENTITY: |
1304 | | /* Can't happen, handled above */ |
1305 | 0 | break; |
1306 | | /* There are only two possible filters, RC4 or AES, we take care |
1307 | | * of the number of bits in the key by using ctx->Length. |
1308 | | */ |
1309 | 2.54k | case CRYPT_V1: |
1310 | 2.61k | case CRYPT_V2: |
1311 | 2.61k | code = pdfi_apply_Arc4_filter(ctx, StreamKey, SubFile_stream, &crypt_stream); |
1312 | 2.61k | break; |
1313 | 25.9k | case CRYPT_AESV2: |
1314 | 26.9k | case CRYPT_AESV3: |
1315 | 26.9k | code = pdfi_apply_AES_filter(ctx, StreamKey, 1, SubFile_stream, &crypt_stream); |
1316 | 26.9k | break; |
1317 | 0 | default: |
1318 | 0 | code = gs_error_rangecheck; |
1319 | 29.6k | } |
1320 | 29.6k | if (code < 0) { |
1321 | 0 | pdfi_close_file(ctx, SubFile_stream); |
1322 | 0 | goto error; |
1323 | 0 | } |
1324 | | |
1325 | 29.6k | crypt_stream->original = SubFile_stream->original; |
1326 | 29.6k | gs_free_object(ctx->memory, SubFile_stream, "pdfi_filter"); |
1327 | | |
1328 | 29.6k | code = pdfi_filter_no_decryption(ctx, stream_obj, crypt_stream, new_stream, false); |
1329 | 29.6k | if (code < 0) { |
1330 | 683 | pdfi_close_file(ctx, crypt_stream); |
1331 | 683 | goto error; |
1332 | 683 | } |
1333 | | |
1334 | 28.9k | (*new_stream)->original = source->s; |
1335 | 28.9k | gs_free_object(ctx->memory, crypt_stream, "pdfi_filter"); |
1336 | 2.54M | } else { |
1337 | 2.54M | code = pdfi_filter_no_decryption(ctx, stream_obj, source, new_stream, inline_image); |
1338 | 2.54M | } |
1339 | 2.57M | error: |
1340 | 2.57M | pdfi_countdown(NewStream); |
1341 | 2.57M | pdfi_countdown(StreamKey); |
1342 | 2.57M | pdfi_countdown(FileSpec); |
1343 | 2.57M | return code; |
1344 | 2.57M | } |
1345 | | |
1346 | | /* This is just a convenience routine. We could use pdfi_filter() above, but because PDF |
1347 | | * doesn't support the SubFileDecode filter that would mean callers having to manufacture |
1348 | | * a dictionary in order to use it. That's excessively convoluted, so just supply a simple |
1349 | | * means to instantiate a SubFileDecode filter. |
1350 | | * |
1351 | | * NB! The EODString can't be tracked by the stream code. The caller is responsible for |
1352 | | * managing the lifetime of this object. It must remain valid until the filter is closed. |
1353 | | */ |
1354 | | int pdfi_apply_SubFileDecode_filter(pdf_context *ctx, int EODCount, const char *EODString, pdf_c_stream *source, pdf_c_stream **new_stream, bool inline_image) |
1355 | 2.93M | { |
1356 | 2.93M | int code; |
1357 | 2.93M | stream_SFD_state state; |
1358 | 2.93M | stream *new_s = NULL; |
1359 | 2.93M | int min_size = 2048; |
1360 | | |
1361 | 2.93M | *new_stream = NULL; |
1362 | | |
1363 | 2.93M | memset(&state, 0, sizeof(state)); |
1364 | | |
1365 | 2.93M | if (s_SFD_template.set_defaults) |
1366 | 2.93M | s_SFD_template.set_defaults((stream_state *)&state); |
1367 | | |
1368 | 2.93M | if (EODString != NULL) { |
1369 | 573k | state.eod.data = (const byte *)EODString; |
1370 | 573k | state.eod.size = strlen(EODString); |
1371 | 573k | } |
1372 | | |
1373 | 2.93M | if (EODCount > 0) |
1374 | 2.36M | state.count = EODCount - source->unread_size; |
1375 | 573k | else |
1376 | 573k | state.count = EODCount; |
1377 | | |
1378 | 2.93M | code = pdfi_filter_open(min_size, &s_filter_read_procs, (const stream_template *)&s_SFD_template, (const stream_state *)&state, ctx->memory->non_gc_memory, &new_s); |
1379 | 2.93M | if (code < 0) |
1380 | 0 | return code; |
1381 | | |
1382 | 2.93M | code = pdfi_alloc_stream(ctx, new_s, source->s, new_stream); |
1383 | 2.93M | if (code < 0) { |
1384 | 0 | gs_free_object(ctx->memory->non_gc_memory, new_s->state, "pdfi_apply_SubFileDecode_filter"); |
1385 | 0 | gs_free_object(ctx->memory->non_gc_memory, new_s->cbuf, "pdfi_apply_SubFileDecode_filter"); |
1386 | 0 | gs_free_object(ctx->memory->non_gc_memory, new_s, "pdfi_apply_SubFileDecode_filter"); |
1387 | 0 | return code; |
1388 | 0 | } |
1389 | 2.93M | new_s->strm = source->s; |
1390 | 2.93M | if (source->unread_size != 0) { |
1391 | 0 | (*new_stream)->unread_size = source->unread_size; |
1392 | 0 | memcpy((*new_stream)->unget_buffer, source->unget_buffer, source->unread_size); |
1393 | 0 | source->unread_size = 0; |
1394 | 0 | } |
1395 | 2.93M | return code; |
1396 | 2.93M | } |
1397 | | |
1398 | | /* We would really like to use a ReusableStreamDecode filter here, but that filter is defined |
1399 | | * purely in the PostScript interpreter. So instead we make a temporary stream from a |
1400 | | * memory buffer. Its icky (we can end up with the same data in memory multiple times) |
1401 | | * but it works, and is used elsewhere in Ghostscript. |
1402 | | * If retain_ownership is true then the calling function is responsible for buffer pointer lifetime. |
1403 | | * Otherwise the buffer will be freed when the stream is closed. |
1404 | | */ |
1405 | | int pdfi_open_memory_stream_from_stream(pdf_context *ctx, unsigned int size, byte **Buffer, pdf_c_stream *source, pdf_c_stream **new_pdf_stream, bool retain_ownership) |
1406 | 0 | { |
1407 | 0 | stream *new_stream; |
1408 | 0 | int code; |
1409 | |
|
1410 | 0 | new_stream = file_alloc_stream(ctx->memory, "open memory stream(stream)"); |
1411 | 0 | if (new_stream == NULL) |
1412 | 0 | return_error(gs_error_VMerror); |
1413 | | |
1414 | 0 | *Buffer = gs_alloc_bytes(ctx->memory, size, "open memory stream (buffer)"); |
1415 | 0 | if (*Buffer == NULL) { |
1416 | 0 | gs_free_object(ctx->memory, new_stream, "open memory stream(stream)"); |
1417 | 0 | return_error(gs_error_VMerror); |
1418 | 0 | } |
1419 | 0 | code = pdfi_read_bytes(ctx, *Buffer, 1, size, source); |
1420 | 0 | if (code < 0) { |
1421 | 0 | gs_free_object(ctx->memory, *Buffer, "open memory stream(buffer)"); |
1422 | 0 | gs_free_object(ctx->memory, new_stream, "open memory stream(stream)"); |
1423 | 0 | return code; |
1424 | 0 | } |
1425 | | |
1426 | 0 | if (retain_ownership) |
1427 | 0 | sread_string_reusable(new_stream, *Buffer, size); |
1428 | 0 | else |
1429 | 0 | sread_transient_string_reusable(new_stream, ctx->memory, *Buffer, size); |
1430 | |
|
1431 | 0 | code = pdfi_alloc_stream(ctx, new_stream, source->s, new_pdf_stream); |
1432 | 0 | if (code < 0) { |
1433 | 0 | sclose(new_stream); |
1434 | 0 | gs_free_object(ctx->memory, *Buffer, "open memory stream(buffer)"); |
1435 | 0 | gs_free_object(ctx->memory, new_stream, "open memory stream(stream)"); |
1436 | 0 | } |
1437 | |
|
1438 | 0 | return code; |
1439 | 0 | } |
1440 | | |
1441 | | /* |
1442 | | * Like pdfi_open_memory_stream_from_stream (and makes use of it) this is a way to read from a stream into |
1443 | | * memory, and return a stream which reads from that memory. The difference is that this function takes |
1444 | | * any filters into account, decompressing them. We could layer a decompression stream onto the memory |
1445 | | * stream returned by open_memory_stream_from_stream above instead. |
1446 | | * |
1447 | | * This function returns < 0 for an error, and the length of the uncompressed data on success. |
1448 | | */ |
1449 | | int pdfi_open_memory_stream_from_filtered_stream(pdf_context *ctx, pdf_stream *stream_obj, |
1450 | | byte **Buffer, pdf_c_stream **new_pdf_stream, bool retain_ownership) |
1451 | 119k | { |
1452 | 119k | int code; |
1453 | 119k | int64_t bufferlen = 0; |
1454 | | |
1455 | 119k | code = pdfi_stream_to_buffer(ctx, stream_obj, Buffer, &bufferlen); |
1456 | 119k | if (code < 0) { |
1457 | 480 | *Buffer = NULL; |
1458 | 480 | *new_pdf_stream = NULL; |
1459 | 480 | return code; |
1460 | 480 | } |
1461 | 118k | code = pdfi_open_memory_stream_from_memory(ctx, (unsigned int)bufferlen, *Buffer, new_pdf_stream, retain_ownership); |
1462 | 118k | if (code < 0) { |
1463 | 0 | gs_free_object(ctx->memory, *Buffer, "pdfi_open_memory_stream_from_filtered_stream"); |
1464 | 0 | *Buffer = NULL; |
1465 | 0 | *new_pdf_stream = NULL; |
1466 | 0 | } |
1467 | 118k | return (int)bufferlen; |
1468 | 119k | } |
1469 | | |
1470 | | int pdfi_open_memory_stream_from_memory(pdf_context *ctx, unsigned int size, byte *Buffer, pdf_c_stream **new_pdf_stream, bool retain_ownership) |
1471 | 279k | { |
1472 | 279k | int code; |
1473 | 279k | stream *new_stream; |
1474 | | |
1475 | 279k | new_stream = file_alloc_stream(ctx->memory, "open memory stream from memory(stream)"); |
1476 | 279k | if (new_stream == NULL) |
1477 | 0 | return_error(gs_error_VMerror); |
1478 | 279k | new_stream->close_at_eod = false; |
1479 | 279k | if (retain_ownership) |
1480 | 191k | sread_string(new_stream, Buffer, size); |
1481 | 88.4k | else |
1482 | 88.4k | sread_transient_string(new_stream, ctx->memory, Buffer, size); |
1483 | | |
1484 | 279k | code = pdfi_alloc_stream(ctx, new_stream, NULL, new_pdf_stream); |
1485 | 279k | if (code < 0) { |
1486 | 0 | sclose(new_stream); |
1487 | 0 | gs_free_object(ctx->memory, new_stream, "open memory stream from memory(stream)"); |
1488 | 0 | } |
1489 | | |
1490 | 279k | return code; |
1491 | 279k | } |
1492 | | |
1493 | | int pdfi_close_memory_stream(pdf_context *ctx, byte *Buffer, pdf_c_stream *source) |
1494 | 113k | { |
1495 | 113k | gs_free_object(ctx->memory, Buffer, "open memory stream(buffer)"); |
1496 | 113k | if (source != NULL) { |
1497 | 113k | if (source->s != NULL) { |
1498 | 113k | sclose(source->s); |
1499 | 113k | gs_free_object(ctx->memory, source->s, "open memory stream(stream)"); |
1500 | 113k | } |
1501 | 113k | gs_free_object(ctx->memory, source, "open memory stream(pdf_stream)"); |
1502 | 113k | } |
1503 | 113k | return 0; |
1504 | 113k | } |
1505 | | |
1506 | | /***********************************************************************************/ |
1507 | | /* Basic 'file' operations. Because of the need to 'unread' bytes we need our own */ |
1508 | | |
1509 | | static void pdfi_close_filter_chain(pdf_context *ctx, stream *s, stream *target) |
1510 | 5.63M | { |
1511 | 5.63M | stream *next_s = s; |
1512 | | |
1513 | 10.9M | while(next_s && next_s != target){ |
1514 | 5.32M | stream *curr_s = next_s; |
1515 | 5.32M | next_s = next_s->strm; |
1516 | 5.32M | if (curr_s != ctx->main_stream->s) |
1517 | 5.32M | sfclose(curr_s); |
1518 | 5.32M | } |
1519 | 5.63M | } |
1520 | | |
1521 | | void pdfi_close_file(pdf_context *ctx, pdf_c_stream *s) |
1522 | 5.63M | { |
1523 | 5.63M | pdfi_close_filter_chain(ctx, s->s, s->original); |
1524 | | |
1525 | 5.63M | gs_free_object(ctx->memory, s, "closing pdf_file"); |
1526 | 5.63M | } |
1527 | | |
1528 | | int pdfi_seek(pdf_context *ctx, pdf_c_stream *s, gs_offset_t offset, uint32_t origin) |
1529 | 15.7M | { |
1530 | 15.7M | int code = 0; |
1531 | | |
1532 | 15.7M | if (origin == SEEK_CUR && s->unread_size != 0) |
1533 | 1.14k | offset -= s->unread_size; |
1534 | | |
1535 | 15.7M | s->unread_size = 0;; |
1536 | | |
1537 | 15.7M | code = sfseek(s->s, offset, origin); |
1538 | 15.7M | if (s->eof && code >= 0) |
1539 | 133k | s->eof = 0; |
1540 | | |
1541 | 15.7M | return code; |
1542 | 15.7M | } |
1543 | | |
1544 | | /* We use 'stell' sometimes to save the position of the underlying file |
1545 | | * when reading a compressed stream, so that we can return to the same |
1546 | | * point in the underlying file after performing some other operation. This |
1547 | | * allows us (for instance) to load a font while interpreting a content stream. |
1548 | | * However, if we've 'unread' any bytes we need to take that into account. |
1549 | | * NOTE! this is only going to be valid when performed on the main stream |
1550 | | * the original PDF file, not any compressed stream! |
1551 | | */ |
1552 | | gs_offset_t pdfi_unread_tell(pdf_context *ctx) |
1553 | 188M | { |
1554 | 188M | gs_offset_t off = stell(ctx->main_stream->s); |
1555 | | |
1556 | 188M | return (off - ctx->main_stream->unread_size); |
1557 | 188M | } |
1558 | | |
1559 | | gs_offset_t pdfi_tell(pdf_c_stream *s) |
1560 | 1.49M | { |
1561 | 1.49M | return stell(s->s); |
1562 | 1.49M | } |
1563 | | |
1564 | | int pdfi_unread_byte(pdf_context *ctx, pdf_c_stream *s, char c) |
1565 | 1.93G | { |
1566 | 1.93G | if (s->unread_size == UNREAD_BUFFER_SIZE) |
1567 | 0 | return_error(gs_error_ioerror); |
1568 | | |
1569 | 1.93G | s->unget_buffer[s->unread_size++] = c; |
1570 | | |
1571 | 1.93G | return 0; |
1572 | 1.93G | } |
1573 | | |
1574 | | int pdfi_unread(pdf_context *ctx, pdf_c_stream *s, byte *Buffer, uint32_t size) |
1575 | 189k | { |
1576 | 189k | if (size + s->unread_size > UNREAD_BUFFER_SIZE) |
1577 | 12 | return_error(gs_error_ioerror); |
1578 | | |
1579 | 189k | Buffer += size; |
1580 | 756k | while (size) { |
1581 | 567k | s->unget_buffer[s->unread_size++] = *--Buffer; |
1582 | 567k | size--; |
1583 | 567k | } |
1584 | | |
1585 | 189k | return 0; |
1586 | 189k | } |
1587 | | |
1588 | | int pdfi_read_byte(pdf_context *ctx, pdf_c_stream *s) |
1589 | 15.6G | { |
1590 | 15.6G | int32_t code; |
1591 | | |
1592 | 15.6G | if (s->eof && s->unread_size == 0) |
1593 | 406k | return EOFC; |
1594 | | |
1595 | 15.6G | if (s->unread_size) |
1596 | 1.93G | return (byte)s->unget_buffer[--s->unread_size]; |
1597 | | |
1598 | | /* TODO the Ghostscript code uses sbufptr(s) to avoid a memcpy |
1599 | | * at some point we should modify this code to do so as well. |
1600 | | */ |
1601 | 13.7G | code = spgetc(s->s); |
1602 | 13.7G | if (code == EOFC) { |
1603 | 642k | s->eof = true; |
1604 | 642k | return EOFC; |
1605 | 13.7G | } else if (code == gs_error_ioerror) { |
1606 | 222 | pdfi_set_error(ctx, code, "sgets", E_PDF_BADSTREAM, "pdfi_read_bytes", NULL); |
1607 | 222 | s->eof = true; |
1608 | 222 | return EOFC; |
1609 | 13.7G | } else if(code == ERRC) { |
1610 | 148k | return ERRC; |
1611 | 148k | } |
1612 | 13.7G | return (int)code; |
1613 | 13.7G | } |
1614 | | |
1615 | | |
1616 | | int pdfi_read_bytes(pdf_context *ctx, byte *Buffer, uint32_t size, uint32_t count, pdf_c_stream *s) |
1617 | 4.00M | { |
1618 | 4.00M | uint32_t i = 0, total = size * count; |
1619 | 4.00M | uint32_t bytes = 0; |
1620 | 4.00M | int32_t code; |
1621 | | |
1622 | 4.00M | if (s->eof && s->unread_size == 0) |
1623 | 10 | return 0; |
1624 | | |
1625 | 4.00M | if (s->unread_size) { |
1626 | 38.1k | i = s->unread_size; |
1627 | 38.1k | if (i >= total) |
1628 | 0 | i = total; |
1629 | 38.1k | bytes = i; |
1630 | 79.5k | while (bytes) { |
1631 | 41.3k | *Buffer++ = s->unget_buffer[--s->unread_size]; |
1632 | 41.3k | bytes--; |
1633 | 41.3k | } |
1634 | 38.1k | total -= i; |
1635 | 38.1k | if (total == 0 || s->eof) |
1636 | 0 | return i; |
1637 | 38.1k | } |
1638 | | |
1639 | | /* TODO the Ghostscript code uses sbufptr(s) to avoid a memcpy |
1640 | | * at some point we should modify this code to do so as well. |
1641 | | */ |
1642 | 4.00M | code = sgets(s->s, Buffer, total, &bytes); |
1643 | 4.00M | if (code == EOFC) { |
1644 | 625 | s->eof = true; |
1645 | 4.00M | } else if (code == gs_error_ioerror) { |
1646 | 0 | pdfi_set_error(ctx, code, "sgets", E_PDF_BADSTREAM, "pdfi_read_bytes", NULL); |
1647 | 0 | s->eof = true; |
1648 | 4.00M | } else if(code == ERRC) { |
1649 | 199 | bytes = ERRC; |
1650 | 4.00M | } else { |
1651 | 4.00M | bytes = bytes + i; |
1652 | 4.00M | } |
1653 | | |
1654 | 4.00M | return bytes; |
1655 | 4.00M | } |
1656 | | |
1657 | | /* Read bytes from stream object into buffer. |
1658 | | * Handles both plain streams and filtered streams. |
1659 | | * Buffer gets allocated here, and must be freed by caller. |
1660 | | * Preserves the location of the current stream file position. |
1661 | | */ |
1662 | | int |
1663 | | pdfi_stream_to_buffer(pdf_context *ctx, pdf_stream *stream_obj, byte **buf, int64_t *bufferlen) |
1664 | 233k | { |
1665 | 233k | byte *Buffer = NULL; |
1666 | 233k | int code = 0; |
1667 | 233k | uint read = 0, buflen = 0; |
1668 | 233k | int64_t ToRead = *bufferlen; |
1669 | 233k | gs_offset_t savedoffset; |
1670 | 233k | pdf_c_stream *stream = NULL, *SubFileStream = NULL; |
1671 | 233k | bool filtered; |
1672 | 233k | pdf_dict *stream_dict = NULL; |
1673 | | |
1674 | 233k | savedoffset = pdfi_tell(ctx->main_stream); |
1675 | | |
1676 | 233k | pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, stream_obj), SEEK_SET); |
1677 | | |
1678 | 233k | code = pdfi_dict_from_obj(ctx, (pdf_obj *)stream_obj, &stream_dict); |
1679 | 233k | if (code < 0) |
1680 | 0 | goto exit; |
1681 | | |
1682 | | /* See if this is a filtered stream */ |
1683 | 233k | code = pdfi_dict_known(ctx, stream_dict, "Filter", &filtered); |
1684 | 233k | if (code < 0) |
1685 | 0 | goto exit; |
1686 | | |
1687 | 233k | if (!filtered) { |
1688 | 30.9k | code = pdfi_dict_known(ctx, stream_dict, "F", &filtered); |
1689 | 30.9k | if (code < 0) |
1690 | 0 | goto exit; |
1691 | 30.9k | } |
1692 | | |
1693 | 238k | retry: |
1694 | 238k | if (ToRead == 0) { |
1695 | 227k | if (filtered || ctx->encryption.is_encrypted) { |
1696 | 202k | code = pdfi_apply_SubFileDecode_filter(ctx, 0, "endstream", ctx->main_stream, &SubFileStream, false); |
1697 | 202k | if (code < 0) |
1698 | 0 | goto exit; |
1699 | 202k | code = pdfi_filter(ctx, stream_obj, SubFileStream, &stream, false); |
1700 | 202k | if (code < 0) { |
1701 | 736 | pdfi_close_file(ctx, SubFileStream); |
1702 | 736 | goto exit; |
1703 | 736 | } |
1704 | 1.18M | while (seofp(stream->s) != true && serrorp(stream->s) != true) { |
1705 | 979k | s_process_read_buf(stream->s); |
1706 | 979k | buflen += sbufavailable(stream->s); |
1707 | 979k | (void)sbufskip(stream->s, sbufavailable(stream->s)); |
1708 | 979k | } |
1709 | 201k | pdfi_close_file(ctx, stream); |
1710 | 201k | pdfi_close_file(ctx, SubFileStream); |
1711 | 201k | } else { |
1712 | 25.2k | buflen = pdfi_stream_length(ctx, stream_obj); |
1713 | 25.2k | } |
1714 | 227k | } else |
1715 | 10.5k | buflen = *bufferlen; |
1716 | | |
1717 | | /* Alloc buffer */ |
1718 | 237k | Buffer = gs_alloc_bytes(ctx->memory, buflen, "pdfi_stream_to_buffer (Buffer)"); |
1719 | 237k | if (!Buffer) { |
1720 | 5 | code = gs_note_error(gs_error_VMerror); |
1721 | 5 | goto exit; |
1722 | 5 | } |
1723 | | |
1724 | 237k | code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, stream_obj), SEEK_SET); |
1725 | 237k | if (code < 0) { |
1726 | 0 | buflen = 0; |
1727 | 0 | goto exit; |
1728 | 0 | } |
1729 | 237k | if (filtered || ctx->encryption.is_encrypted) { |
1730 | 206k | if (ToRead && stream_obj->length_valid) |
1731 | 4.36k | code = pdfi_apply_SubFileDecode_filter(ctx, stream_obj->Length, NULL, ctx->main_stream, &SubFileStream, false); |
1732 | 202k | else |
1733 | 202k | code = pdfi_apply_SubFileDecode_filter(ctx, 0, "endstream", ctx->main_stream, &SubFileStream, false); |
1734 | 206k | if (code < 0) |
1735 | 0 | goto exit; |
1736 | | |
1737 | 206k | code = pdfi_filter(ctx, stream_obj, SubFileStream, &stream, false); |
1738 | 206k | if (code < 0) { |
1739 | | /* Because we opened the SubFileDecode separately to the filter chain, we need to close it separately too */ |
1740 | 83 | pdfi_close_file(ctx, SubFileStream); |
1741 | 83 | goto exit; |
1742 | 83 | } |
1743 | | |
1744 | 206k | code = sgets(stream->s, Buffer, buflen, (unsigned int *)&read); |
1745 | 206k | if (read < buflen) { |
1746 | 2.68k | memset(Buffer + read, 0x00, buflen - read); |
1747 | 2.68k | } |
1748 | | |
1749 | 206k | pdfi_close_file(ctx, stream); |
1750 | | /* Because we opened the SubFileDecode separately to the filter chain, we need to close it separately too */ |
1751 | 206k | pdfi_close_file(ctx, SubFileStream); |
1752 | 206k | if (code == ERRC || code == EOFC) { |
1753 | 2.71k | code = 0; |
1754 | | /* Error reading the expected number of bytes. If we already calculated the number of |
1755 | | * bytes in the loop above, then ignore the error and carry on. If, however, we were |
1756 | | * told how many bytes to expect, and failed to read that many, go back and do this |
1757 | | * the slow way to determine how many bytes are *really* available. |
1758 | | */ |
1759 | 2.71k | if(ToRead != 0) { |
1760 | 409 | buflen = ToRead = 0; |
1761 | 409 | code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, stream_obj), SEEK_SET); |
1762 | 409 | if (code < 0) |
1763 | 0 | goto exit; |
1764 | 409 | gs_free_object(ctx->memory, Buffer, "pdfi_stream_to_buffer (Buffer)"); |
1765 | 409 | goto retry; |
1766 | 409 | } |
1767 | 2.71k | } |
1768 | 206k | } else { |
1769 | 30.9k | if (ToRead && stream_obj->length_valid) |
1770 | 5.66k | code = pdfi_apply_SubFileDecode_filter(ctx, stream_obj->Length, NULL, ctx->main_stream, &SubFileStream, false); |
1771 | 25.2k | else |
1772 | 25.2k | code = pdfi_apply_SubFileDecode_filter(ctx, ToRead, "endstream", ctx->main_stream, &SubFileStream, false); |
1773 | 30.9k | if (code < 0) |
1774 | 0 | goto exit; |
1775 | | |
1776 | 30.9k | code = sgets(SubFileStream->s, Buffer, buflen, (unsigned int *)&read); |
1777 | 30.9k | if (read < buflen) { |
1778 | 140 | memset(Buffer + read, 0x00, buflen - read); |
1779 | 140 | } |
1780 | | |
1781 | 30.9k | pdfi_close_file(ctx, SubFileStream); |
1782 | 30.9k | if (code == ERRC || code == EOFC) { |
1783 | 4.52k | code = 0; |
1784 | | /* Error reading the expected number of bytes. If we already calculated the number of |
1785 | | * bytes in the loop above, then ignore the error and carry on. If, however, we were |
1786 | | * told how many bytes to expect, and failed to read that many, go back and do this |
1787 | | * the slow way to determine how many bytes are *really* available. |
1788 | | */ |
1789 | 4.52k | buflen = ToRead = 0; |
1790 | | /* Setting filtered to true is a lie, but it forces the code to go through the slow path and check the *real* number of bytes |
1791 | | * in the stream. This will be slow, but it should only happen when we get a file which is invalid. |
1792 | | */ |
1793 | 4.52k | filtered = 1; |
1794 | 4.52k | code = pdfi_seek(ctx, ctx->main_stream, pdfi_stream_offset(ctx, stream_obj), SEEK_SET); |
1795 | 4.52k | if (code < 0) |
1796 | 0 | goto exit; |
1797 | 4.52k | gs_free_object(ctx->memory, Buffer, "pdfi_stream_to_buffer (Buffer)"); |
1798 | 4.52k | goto retry; |
1799 | 4.52k | } |
1800 | 30.9k | } |
1801 | | |
1802 | 233k | exit: |
1803 | 233k | pdfi_seek(ctx, ctx->main_stream, savedoffset, SEEK_SET); |
1804 | 233k | if (Buffer && code < 0) |
1805 | 83 | gs_free_object(ctx->memory, Buffer, "pdfi_stream_to_buffer (Buffer)"); |
1806 | 233k | else |
1807 | 233k | *buf = Buffer; |
1808 | 233k | *bufferlen = buflen; |
1809 | 233k | return code; |
1810 | 237k | } |
1811 | | |
1812 | | static int pdfi_open_resource_file_inner(pdf_context *ctx, const char *fname, const int fnamelen, stream **s) |
1813 | 3.42M | { |
1814 | 3.42M | int code = 0; |
1815 | 3.42M | if (fname == NULL || fnamelen == 0 || fnamelen >= gp_file_name_sizeof) |
1816 | 3 | *s = NULL; |
1817 | 3.42M | else if (gp_file_name_is_absolute(fname, fnamelen) || fname[0] == '%') { |
1818 | 0 | char CFileName[gp_file_name_sizeof]; |
1819 | |
|
1820 | 0 | if (fnamelen + 1 > gp_file_name_sizeof) |
1821 | 0 | return_error(gs_error_ioerror); |
1822 | 0 | memcpy(CFileName, fname, fnamelen); |
1823 | 0 | CFileName[fnamelen] = 0x00; |
1824 | | /* If it's an absolute path or an explicit PS style device, just try to open it */ |
1825 | 0 | *s = sfopen(CFileName, "r", ctx->memory); |
1826 | 0 | } |
1827 | 3.42M | else { |
1828 | 3.42M | char fnametotry[gp_file_name_sizeof]; |
1829 | 3.42M | uint fnlen; |
1830 | 3.42M | gs_parsed_file_name_t pname; |
1831 | 3.42M | gp_file_name_combine_result r; |
1832 | 3.42M | int i, total; |
1833 | | |
1834 | 3.42M | *s = NULL; |
1835 | 3.42M | i = 0; |
1836 | 3.42M | total = ctx->search_paths.num_resource_paths - ctx->search_paths.num_init_resource_paths - 1; |
1837 | 6.74M | retry: |
1838 | 50.6M | for (; i < total; i++) { |
1839 | 43.9M | gs_param_string *ss = &ctx->search_paths.resource_paths[i]; |
1840 | | |
1841 | 43.9M | if (ss->data[0] == '%') { |
1842 | 6.80M | code = gs_parse_file_name(&pname, (char *)ss->data, ss->size, ctx->memory); |
1843 | 6.80M | if (code < 0 || (pname.len + fnamelen >= gp_file_name_sizeof)) { |
1844 | 0 | continue; |
1845 | 0 | } |
1846 | 6.80M | memcpy(fnametotry, pname.fname, pname.len); |
1847 | 6.80M | memcpy(fnametotry + pname.len, fname, fnamelen); |
1848 | 6.80M | code = pname.iodev->procs.open_file(pname.iodev, fnametotry, pname.len + fnamelen, "r", s, ctx->memory); |
1849 | 6.80M | if (code < 0) { |
1850 | 6.76M | continue; |
1851 | 6.76M | } |
1852 | 45.5k | break; |
1853 | 6.80M | } |
1854 | 37.1M | else { |
1855 | 37.1M | fnlen = gp_file_name_sizeof; |
1856 | 37.1M | r = gp_file_name_combine((char *)ss->data, ss->size, fname, fnamelen, false, fnametotry, &fnlen); |
1857 | 37.1M | if (r != gp_combine_success || fnlen > gp_file_name_sizeof - 1) |
1858 | 0 | continue; |
1859 | 37.1M | fnametotry[fnlen] = '\0'; |
1860 | 37.1M | *s = sfopen(fnametotry, "r", ctx->memory); |
1861 | 37.1M | if (*s != NULL) |
1862 | 0 | break; |
1863 | 37.1M | } |
1864 | 43.9M | } |
1865 | 6.74M | if (*s == NULL && i < ctx->search_paths.num_resource_paths) { |
1866 | 3.38M | gs_param_string *ss = &ctx->search_paths.genericresourcedir; |
1867 | 3.38M | fnlen = gp_file_name_sizeof; |
1868 | 3.38M | r = gp_file_name_combine((char *)ss->data, ss->size, fname, fnamelen, false, fnametotry, &fnlen); |
1869 | 3.38M | if (r == gp_combine_success || fnlen < gp_file_name_sizeof) { |
1870 | 3.38M | fnametotry[fnlen] = '\0'; |
1871 | 3.38M | *s = sfopen(fnametotry, "r", ctx->memory); |
1872 | 3.38M | } |
1873 | 3.38M | } |
1874 | 6.74M | if (*s == NULL && i < ctx->search_paths.num_resource_paths) { |
1875 | 3.32M | total = ctx->search_paths.num_resource_paths; |
1876 | 3.32M | goto retry; |
1877 | 3.32M | } |
1878 | 6.74M | } |
1879 | 3.42M | if (*s == NULL) |
1880 | 3.32M | return_error(gs_error_invalidfileaccess); |
1881 | | |
1882 | 102k | return 0; |
1883 | 3.42M | } |
1884 | | |
1885 | | int pdfi_open_resource_file(pdf_context *ctx, const char *fname, const int fnamelen, stream **s) |
1886 | 117k | { |
1887 | 117k | return pdfi_open_resource_file_inner(ctx, fname, fnamelen, s); |
1888 | 117k | } |
1889 | | |
1890 | | bool pdfi_resource_file_exists(pdf_context *ctx, const char *fname, const int fnamelen) |
1891 | 0 | { |
1892 | 0 | stream *s = NULL; |
1893 | 0 | int code = pdfi_open_resource_file_inner(ctx, fname, fnamelen, &s); |
1894 | 0 | if (s) |
1895 | 0 | sfclose(s); |
1896 | |
|
1897 | 0 | return (code >= 0); |
1898 | 0 | } |
1899 | | |
1900 | | static int pdfi_open_font_file_inner(pdf_context *ctx, const char *fname, const int fnamelen, stream **s) |
1901 | 3.30M | { |
1902 | 3.30M | int code = 0; |
1903 | 3.30M | const char *fontdirstr = "Font/"; |
1904 | 3.30M | const int fontdirstrlen = strlen(fontdirstr); |
1905 | 3.30M | uint fnlen; |
1906 | 3.30M | gp_file_name_combine_result r; |
1907 | 3.30M | char fnametotry[gp_file_name_sizeof]; |
1908 | | |
1909 | 3.30M | if (fname == NULL || fnamelen == 0 || fnamelen >= (gp_file_name_sizeof - fontdirstrlen)) |
1910 | 3 | *s = NULL; |
1911 | 3.30M | else if (gp_file_name_is_absolute(fname, fnamelen) || fname[0] == '%') { |
1912 | 0 | char CFileName[gp_file_name_sizeof]; |
1913 | |
|
1914 | 0 | if (fnamelen + 1 > gp_file_name_sizeof) |
1915 | 0 | return_error(gs_error_ioerror); |
1916 | 0 | memcpy(CFileName, fname, fnamelen); |
1917 | 0 | CFileName[fnamelen] = 0x00; |
1918 | | /* If it's an absolute path or an explicit PS style device, just try to open it */ |
1919 | 0 | *s = sfopen(CFileName, "r", ctx->memory); |
1920 | 0 | } |
1921 | 3.30M | else { |
1922 | 3.30M | char fnametotry[gp_file_name_sizeof]; |
1923 | 3.30M | gs_parsed_file_name_t pname; |
1924 | 3.30M | int i; |
1925 | | |
1926 | 3.30M | *s = NULL; |
1927 | 3.30M | for (i = 0; i < ctx->search_paths.num_font_paths; i++) { |
1928 | 0 | gs_param_string *ss = &ctx->search_paths.font_paths[i]; |
1929 | |
|
1930 | 0 | if (ss->data[0] == '%') { |
1931 | 0 | code = gs_parse_file_name(&pname, (char *)ss->data, ss->size, ctx->memory); |
1932 | 0 | if (code < 0 || (pname.len + fnamelen >= gp_file_name_sizeof)) { |
1933 | 0 | continue; |
1934 | 0 | } |
1935 | 0 | memcpy(fnametotry, pname.fname, pname.len); |
1936 | 0 | memcpy(fnametotry + pname.len, fname, fnamelen); |
1937 | 0 | code = pname.iodev->procs.open_file(pname.iodev, fnametotry, pname.len + fnamelen, "r", s, ctx->memory); |
1938 | 0 | if (code < 0) { |
1939 | 0 | continue; |
1940 | 0 | } |
1941 | 0 | break; |
1942 | 0 | } |
1943 | 0 | else { |
1944 | 0 | fnlen = gp_file_name_sizeof; |
1945 | 0 | r = gp_file_name_combine((char *)ss->data, ss->size, fname, fnamelen, false, fnametotry, &fnlen); |
1946 | 0 | if (r != gp_combine_success || fnlen > gp_file_name_sizeof - 1) |
1947 | 0 | continue; |
1948 | 0 | fnametotry[fnlen] = '\0'; |
1949 | 0 | *s = sfopen(fnametotry, "r", ctx->memory); |
1950 | 0 | if (*s != NULL) |
1951 | 0 | break; |
1952 | 0 | } |
1953 | 0 | } |
1954 | 3.30M | } |
1955 | | |
1956 | | /* If not in the font specific search path, try it as a resource */ |
1957 | 3.30M | if (*s == NULL) |
1958 | 3.30M | code = pdfi_open_resource_file_inner(ctx, fname, fnamelen, s); |
1959 | | |
1960 | 3.30M | if (*s == NULL) { |
1961 | 3.30M | gs_param_string *ss = &ctx->search_paths.genericresourcedir; |
1962 | 3.30M | char fstr[gp_file_name_sizeof]; |
1963 | | |
1964 | 3.30M | fnlen = gp_file_name_sizeof; |
1965 | | |
1966 | 3.30M | if (fontdirstrlen + 1 > gp_file_name_sizeof) |
1967 | 0 | return_error(gs_error_undefinedfilename); |
1968 | | |
1969 | 3.30M | memcpy(fstr, fontdirstr, fontdirstrlen); |
1970 | 3.30M | if (fname != NULL) { |
1971 | 3.30M | if (fontdirstrlen + fnamelen < gp_file_name_sizeof) |
1972 | 3.30M | memcpy(fstr + fontdirstrlen, fname, fnamelen); |
1973 | 3 | else |
1974 | 3 | return_error(gs_error_undefinedfilename); |
1975 | 3.30M | } |
1976 | | |
1977 | 3.30M | r = gp_file_name_combine((char *)ss->data, ss->size, fstr, fontdirstrlen + fnamelen, false, fnametotry, &fnlen); |
1978 | 3.30M | if (r == gp_combine_success || fnlen < gp_file_name_sizeof) { |
1979 | 3.30M | fnametotry[fnlen] = '\0'; |
1980 | 3.30M | *s = sfopen(fnametotry, "r", ctx->memory); |
1981 | 3.30M | } |
1982 | 3.30M | } |
1983 | | |
1984 | 3.30M | return *s == NULL ? gs_error_undefinedfilename : 0; |
1985 | 3.30M | } |
1986 | | |
1987 | | int pdfi_open_font_file(pdf_context *ctx, const char *fname, const int fnamelen, stream **s) |
1988 | 1.65M | { |
1989 | 1.65M | return pdfi_open_font_file_inner(ctx, fname, fnamelen, s); |
1990 | 1.65M | } |
1991 | | |
1992 | | bool pdfi_font_file_exists(pdf_context *ctx, const char *fname, const int fnamelen) |
1993 | 1.65M | { |
1994 | 1.65M | stream *s = NULL; |
1995 | 1.65M | int code = pdfi_open_font_file_inner(ctx, fname, fnamelen, &s); |
1996 | 1.65M | if (s) |
1997 | 0 | sfclose(s); |
1998 | | |
1999 | 1.65M | return (code >= 0); |
2000 | 1.65M | } |