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