| 1 |
|
| 2 |
#include "ppfilter.h" |
| 3 |
#include "pplib.h" |
| 4 |
|
| 5 |
ppstream * ppstream_create (ppdoc *pdf, ppdict *dict, size_t offset) |
| 6 |
{ |
| 7 |
ppstream *stream; |
| 8 |
stream = (ppstream *)ppstruct_take(&pdf->heap, sizeof(ppstream)); |
| 9 |
stream->dict = dict; |
| 10 |
stream->offset = offset; |
| 11 |
//if (!ppdict_rget_uint(dict, "Length", &stream->length)) // may be indirect pointing PPNONE at this moment |
| 12 |
// stream->length = 0; |
| 13 |
stream->length = 0; |
| 14 |
stream->filespec = NULL; |
| 15 |
stream->filter.filters = NULL; |
| 16 |
stream->filter.params = NULL; |
| 17 |
stream->filter.count = 0; |
| 18 |
stream->input = &pdf->input; |
| 19 |
stream->I = NULL; |
| 20 |
stream->cryptkey = NULL; |
| 21 |
stream->flags = 0; |
| 22 |
return stream; |
| 23 |
} |
| 24 |
|
| 25 |
static iof * ppstream_predictor (ppdict *params, iof *N) |
| 26 |
{ |
| 27 |
ppint predictor, rowsamples, components, samplebits; |
| 28 |
|
| 29 |
if (!ppdict_get_int(params, "Predictor", &predictor) || predictor <= 1) |
| 30 |
return N; |
| 31 |
if (!ppdict_get_int(params, "Columns", &rowsamples) || rowsamples == 0) // sanity, filter probably expects >0 |
| 32 |
rowsamples = 1;; |
| 33 |
if (!ppdict_get_int(params, "Colors", &components) || components == 0) // ditto |
| 34 |
components = 1; |
| 35 |
if (!ppdict_get_int(params, "BitsPerComponent", &samplebits) || samplebits == 0) |
| 36 |
samplebits = 8; |
| 37 |
return iof_filter_predictor_decoder(N, (int)predictor, (int)rowsamples, (int)components, (int)samplebits); |
| 38 |
} |
| 39 |
|
| 40 |
static iof * ppstream_decoder (ppstream *stream, ppstreamtp filtertype, ppdict *params, iof *N) |
| 41 |
{ |
| 42 |
int flags; |
| 43 |
iof *F, *P; |
| 44 |
ppint earlychange; |
| 45 |
ppstring *cryptkey; |
| 46 |
|
| 47 |
switch (filtertype) |
| 48 |
{ |
| 49 |
case PPSTREAM_BASE16: |
| 50 |
return iof_filter_base16_decoder(N); |
| 51 |
case PPSTREAM_BASE85: |
| 52 |
return iof_filter_base85_decoder(N); |
| 53 |
case PPSTREAM_RUNLENGTH: |
| 54 |
return iof_filter_runlength_decoder(N); |
| 55 |
case PPSTREAM_FLATE: |
| 56 |
if ((F = iof_filter_flate_decoder(N)) != NULL) |
| 57 |
{ |
| 58 |
if (params != NULL) |
| 59 |
{ |
| 60 |
if ((P = ppstream_predictor(params, F)) != NULL) |
| 61 |
return P; |
| 62 |
iof_close(F); |
| 63 |
break; |
| 64 |
} |
| 65 |
return F; |
| 66 |
} |
| 67 |
break; |
| 68 |
case PPSTREAM_LZW: |
| 69 |
flags = LZW_DECODER_DEFAULTS; |
| 70 |
if (params != NULL && ppdict_get_int(params, "EarlyChange", &earlychange) && earlychange == 0) // integer, not boolean |
| 71 |
flags &= ~LZW_EARLY_INDEX; |
| 72 |
if ((F = iof_filter_lzw_decoder(N, flags)) != NULL) |
| 73 |
{ |
| 74 |
if (params != NULL) |
| 75 |
{ |
| 76 |
if ((P = ppstream_predictor(params, F)) != NULL) |
| 77 |
return P; |
| 78 |
iof_close(F); |
| 79 |
break; |
| 80 |
} |
| 81 |
return F; |
| 82 |
} |
| 83 |
break; |
| 84 |
case PPSTREAM_CRYPT: |
| 85 |
if ((cryptkey = stream->cryptkey) == NULL) |
| 86 |
return N; // /Identity crypt |
| 87 |
if (stream->flags & PPSTREAM_ENCRYPTED_AES) |
| 88 |
return iof_filter_aes_decoder(N, cryptkey->data, cryptkey->size); |
| 89 |
if (stream->flags & PPSTREAM_ENCRYPTED_RC4) |
| 90 |
return iof_filter_rc4_decoder(N, cryptkey->data, cryptkey->size); |
| 91 |
return NULL; // if neither AES or RC4 but cryptkey present, something went wrong; see ppstream_info() |
| 92 |
case PPSTREAM_CCITT: |
| 93 |
case PPSTREAM_DCT: |
| 94 |
case PPSTREAM_JBIG2: |
| 95 |
case PPSTREAM_JPX: |
| 96 |
break; |
| 97 |
} |
| 98 |
return NULL; |
| 99 |
} |
| 100 |
|
| 101 |
#define ppstream_source(stream) iof_filter_stream_coreader((iof_file *)((stream)->input), (size_t)((stream)->offset), (size_t)((stream)->length)) |
| 102 |
#define ppstream_auxsource(filename) iof_filter_file_reader(filename) |
| 103 |
|
| 104 |
static ppname * ppstream_get_filter_name (ppobj *filterobj, size_t index) |
| 105 |
{ |
| 106 |
if (filterobj->type == PPNAME) |
| 107 |
return index == 0 ? filterobj->name : NULL; |
| 108 |
if (filterobj->type == PPARRAY) |
| 109 |
return pparray_get_name(filterobj->array, index); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
static ppdict * ppstream_get_filter_params (ppobj *paramsobj, size_t index) |
| 114 |
{ |
| 115 |
if (paramsobj->type == PPDICT) |
| 116 |
return index == 0 ? paramsobj->dict : NULL; |
| 117 |
if (paramsobj->type == PPARRAY) |
| 118 |
return pparray_rget_dict(paramsobj->array, index); |
| 119 |
return NULL; |
| 120 |
} |
| 121 |
|
| 122 |
static const char * ppstream_aux_filename (ppobj *filespec) |
| 123 |
{ // mockup, here we should decode the string |
| 124 |
if (filespec->type == PPSTRING) |
| 125 |
{ |
| 126 |
return (const char *)(filespec->string); |
| 127 |
} |
| 128 |
// else might be a dict - todo |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
#define ppstream_image_filter(fcode) (fcode == PPSTREAM_DCT || fcode == PPSTREAM_CCITT || fcode == PPSTREAM_JBIG2 || fcode == PPSTREAM_JPX) |
| 133 |
|
| 134 |
iof * ppstream_read (ppstream *stream, int decode, int all) |
| 135 |
{ |
| 136 |
iof *I, *F; |
| 137 |
ppstreamtp *filtertypes, filtertype; |
| 138 |
int owncrypt; |
| 139 |
ppdict **filterparams, *fparams; |
| 140 |
size_t index, filtercount; |
| 141 |
const char *filename; |
| 142 |
|
| 143 |
if (ppstream_iof(stream) != NULL) |
| 144 |
return NULL; // usage error |
| 145 |
|
| 146 |
if (stream->filespec != NULL) |
| 147 |
{ |
| 148 |
filename = ppstream_aux_filename(stream->filespec); // mockup, basic support |
| 149 |
I = filename != NULL ? ppstream_auxsource(filename) : NULL; |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
I = ppstream_source(stream); |
| 154 |
} |
| 155 |
if (I == NULL) |
| 156 |
return NULL; |
| 157 |
|
| 158 |
/* If the stream is encrypted, decipher is the first to be applied */ |
| 159 |
owncrypt = (stream->flags & PPSTREAM_ENCRYPTED_OWN) != 0; |
| 160 |
if (!owncrypt) |
| 161 |
{ |
| 162 |
if (stream->cryptkey != NULL && stream->filespec == NULL) |
| 163 |
{ /* implied global crypt; does not apply to external files (pdf psec page 115), except for embedded file streams (not supported so far) */ |
| 164 |
if ((F = ppstream_decoder(stream, PPSTREAM_CRYPT, NULL, I)) == NULL) |
| 165 |
goto stream_error; |
| 166 |
I = F; |
| 167 |
} /* otherwise no crypt at all or /Identity */ |
| 168 |
} |
| 169 |
|
| 170 |
if (decode || owncrypt) |
| 171 |
{ |
| 172 |
if ((filtercount = stream->filter.count) > 0) |
| 173 |
{ |
| 174 |
filtertypes = stream->filter.filters; |
| 175 |
filterparams = stream->filter.params; |
| 176 |
for (index = 0; index < filtercount; ++index) |
| 177 |
{ |
| 178 |
fparams = filterparams != NULL ? filterparams[index] : NULL; |
| 179 |
filtertype = filtertypes[index]; |
| 180 |
if ((F = ppstream_decoder(stream, filtertype, fparams, I)) != NULL) |
| 181 |
{ |
| 182 |
I = F; |
| 183 |
if (owncrypt && !decode && filtertype == PPSTREAM_CRYPT) |
| 184 |
break; // /Crypt filter should always be first, so in practise we return decrypted but compressed |
| 185 |
continue; |
| 186 |
} |
| 187 |
if (!ppstream_image_filter(filtertype)) |
| 188 |
goto stream_error; // failed to create non-image filter, something unexpected |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
if (all) |
| 194 |
iof_load(I); |
| 195 |
else |
| 196 |
iof_input(I); |
| 197 |
stream->I = I; |
| 198 |
return I; |
| 199 |
stream_error: |
| 200 |
iof_close(I); |
| 201 |
return NULL; |
| 202 |
} |
| 203 |
|
| 204 |
uint8_t * ppstream_first (ppstream *stream, size_t *size, int decode) |
| 205 |
{ |
| 206 |
iof *I; |
| 207 |
if ((I = ppstream_read(stream, decode, 0)) != NULL) |
| 208 |
{ |
| 209 |
*size = (size_t)iof_left(I); |
| 210 |
return I->pos; |
| 211 |
} |
| 212 |
*size = 0; |
| 213 |
return NULL; |
| 214 |
} |
| 215 |
|
| 216 |
uint8_t * ppstream_next (ppstream *stream, size_t *size) |
| 217 |
{ |
| 218 |
iof *I; |
| 219 |
if ((I = ppstream_iof(stream)) != NULL) |
| 220 |
{ |
| 221 |
I->pos = I->end; |
| 222 |
if ((*size = iof_input(I)) > 0) |
| 223 |
return I->pos; |
| 224 |
} |
| 225 |
*size = 0; |
| 226 |
return NULL; |
| 227 |
} |
| 228 |
|
| 229 |
uint8_t * ppstream_all (ppstream *stream, size_t *size, int decode) |
| 230 |
{ |
| 231 |
iof *I; |
| 232 |
if ((I = ppstream_read(stream, decode, 1)) != NULL) |
| 233 |
{ |
| 234 |
*size = (size_t)iof_left(I); |
| 235 |
return I->pos; |
| 236 |
} |
| 237 |
*size = 0; |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
void ppstream_done (ppstream *stream) |
| 242 |
{ |
| 243 |
iof *I; |
| 244 |
if ((I = ppstream_iof(stream)) != NULL) |
| 245 |
{ |
| 246 |
iof_close(I); |
| 247 |
stream->I = NULL; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/* fetching stream info |
| 252 |
PJ20180916: revealed it makes sense to do a lilbit more just after parsing stream entry to simplify stream operations |
| 253 |
and extend ppstream api |
| 254 |
*/ |
| 255 |
|
| 256 |
/* stream filters */ |
| 257 |
|
| 258 |
const char * ppstream_filter_name[] = { |
| 259 |
"ASCIIHexDecode", |
| 260 |
"ASCII85Decode", |
| 261 |
"RunLengthDecode", |
| 262 |
"FlateDecode", |
| 263 |
"LZWDecode", |
| 264 |
"CCITTFaxDecode", |
| 265 |
"DCTDecode", |
| 266 |
"JBIG2Decode", |
| 267 |
"JPXDecode", |
| 268 |
"Crypt" |
| 269 |
}; |
| 270 |
|
| 271 |
int ppstream_filter_type (ppname *name, ppstreamtp *filtertype) |
| 272 |
{ |
| 273 |
switch (name->data[0]) |
| 274 |
{ |
| 275 |
case 'A': |
| 276 |
if (ppname_is(name, "ASCIIHexDecode")) { *filtertype = PPSTREAM_BASE16; return 1; } |
| 277 |
if (ppname_is(name, "ASCII85Decode")) { *filtertype = PPSTREAM_BASE85; return 1; } |
| 278 |
break; |
| 279 |
case 'R': |
| 280 |
if (ppname_is(name, "RunLengthDecode")) { *filtertype = PPSTREAM_RUNLENGTH; return 1; } |
| 281 |
break; |
| 282 |
case 'F': |
| 283 |
if (ppname_is(name, "FlateDecode")) { *filtertype = PPSTREAM_FLATE; return 1; } |
| 284 |
break; |
| 285 |
case 'L': |
| 286 |
if (ppname_is(name, "LZWDecode")) { *filtertype = PPSTREAM_LZW; return 1; } |
| 287 |
break; |
| 288 |
case 'D': |
| 289 |
if (ppname_is(name, "DCTDecode")) { *filtertype = PPSTREAM_DCT; return 1; } |
| 290 |
break; |
| 291 |
case 'C': |
| 292 |
if (ppname_is(name, "CCITTFaxDecode")) { *filtertype = PPSTREAM_CCITT; return 1; } |
| 293 |
if (ppname_is(name, "Crypt")) { *filtertype = PPSTREAM_CRYPT; return 1; } |
| 294 |
break; |
| 295 |
case 'J': |
| 296 |
if (ppname_is(name, "JPXDecode")) { *filtertype = PPSTREAM_JPX; return 1; } |
| 297 |
if (ppname_is(name, "JBIG2Decode")) { *filtertype = PPSTREAM_JBIG2; return 1; } |
| 298 |
break; |
| 299 |
} |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
void ppstream_info (ppstream *stream, ppdoc *pdf) |
| 304 |
{ // called in ppdoc_load_entries() for every stream, but after loading non-stream objects (eg. /Length..) |
| 305 |
ppdict *dict, *fparams; |
| 306 |
ppobj *fobj, *pobj; |
| 307 |
ppname *fname, *tname, *owncryptfilter = NULL; |
| 308 |
ppcrypt *crypt; |
| 309 |
ppref *ref; |
| 310 |
size_t i; |
| 311 |
int cflags; |
| 312 |
|
| 313 |
ppstreamtp *filtertypes = NULL, filtertype; |
| 314 |
ppdict **filterparams = NULL; |
| 315 |
size_t filtercount = 0, farraysize = 0; |
| 316 |
|
| 317 |
const char *filterkey, *paramskey; |
| 318 |
|
| 319 |
dict = stream->dict; |
| 320 |
ppdict_rget_uint(dict, "Length", &stream->length); |
| 321 |
|
| 322 |
if ((stream->filespec = ppdict_get_obj(dict, "F")) != NULL) |
| 323 |
{ |
| 324 |
stream->flags |= PPSTREAM_NOT_SUPPORTED; |
| 325 |
filterkey = "FFilter", paramskey = "FDecodeParms"; |
| 326 |
} |
| 327 |
else |
| 328 |
filterkey = "Filter", paramskey = "DecodeParms"; |
| 329 |
|
| 330 |
if ((fobj = ppdict_rget_obj(dict, filterkey)) != NULL) |
| 331 |
{ |
| 332 |
switch (fobj->type) |
| 333 |
{ |
| 334 |
case PPNAME: |
| 335 |
farraysize = 1; |
| 336 |
break; |
| 337 |
case PPARRAY: |
| 338 |
farraysize = fobj->array->size; |
| 339 |
break; |
| 340 |
default: |
| 341 |
break; |
| 342 |
} |
| 343 |
if (farraysize > 0) |
| 344 |
{ |
| 345 |
filtertypes = (ppstreamtp *)ppstruct_take(&pdf->heap, farraysize * sizeof(ppstreamtp)); |
| 346 |
if ((pobj = ppdict_rget_obj(dict, paramskey)) != NULL) |
| 347 |
{ |
| 348 |
filterparams = (ppdict **)ppstruct_take(&pdf->heap, farraysize * sizeof(ppdict *)); |
| 349 |
} |
| 350 |
for (i = 0; i < farraysize; ++i) |
| 351 |
{ |
| 352 |
if ((fname = ppstream_get_filter_name(fobj, i)) != NULL && ppstream_filter_type(fname, &filtertype)) |
| 353 |
{ |
| 354 |
filtertypes[filtercount] = filtertype; |
| 355 |
if (pobj != NULL) |
| 356 |
{ |
| 357 |
fparams = ppstream_get_filter_params(pobj, i); |
| 358 |
filterparams[filtercount] = fparams; |
| 359 |
} |
| 360 |
else |
| 361 |
fparams = NULL; |
| 362 |
switch (filtertype) |
| 363 |
{ |
| 364 |
case PPSTREAM_BASE16: |
| 365 |
case PPSTREAM_BASE85: |
| 366 |
case PPSTREAM_RUNLENGTH: |
| 367 |
case PPSTREAM_FLATE: |
| 368 |
case PPSTREAM_LZW: |
| 369 |
stream->flags |= PPSTREAM_FILTER; |
| 370 |
break; |
| 371 |
case PPSTREAM_CCITT: |
| 372 |
case PPSTREAM_DCT: |
| 373 |
case PPSTREAM_JBIG2: |
| 374 |
case PPSTREAM_JPX: |
| 375 |
stream->flags |= PPSTREAM_IMAGE; |
| 376 |
break; |
| 377 |
case PPSTREAM_CRYPT: |
| 378 |
stream->flags |= PPSTREAM_ENCRYPTED_OWN; |
| 379 |
owncryptfilter = fparams != NULL ? ppdict_get_name(fparams, "Name") : NULL; // /Type /CryptFilterDecodeParms /Name ... |
| 380 |
if (i != 0) // we assume it is first |
| 381 |
stream->flags |= PPSTREAM_NOT_SUPPORTED; |
| 382 |
break; |
| 383 |
} |
| 384 |
++filtercount; |
| 385 |
} |
| 386 |
else |
| 387 |
{ |
| 388 |
stream->flags |= PPSTREAM_NOT_SUPPORTED; |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
stream->filter.filters = filtertypes; |
| 394 |
stream->filter.params = filterparams; |
| 395 |
stream->filter.count = filtercount; |
| 396 |
|
| 397 |
if ((crypt = pdf->crypt) == NULL || (ref = crypt->ref) == NULL) |
| 398 |
return; |
| 399 |
if (stream->flags & PPSTREAM_ENCRYPTED_OWN) |
| 400 |
{ |
| 401 |
/* Seems a common habit to use just /Crypt filter name with no params, which defaults to /Identity. |
| 402 |
A real example with uncompressed metadata: <</Filter[/Crypt]/Length 4217/Subtype/XML/Type/Metadata>> */ |
| 403 |
if (owncryptfilter != NULL && !ppname_is(owncryptfilter, "Identity") && stream->filespec == NULL) // ? |
| 404 |
{ |
| 405 |
if (crypt->map != NULL && ppcrypt_type(crypt, owncryptfilter, NULL, &cflags)) |
| 406 |
{ |
| 407 |
if (cflags & PPCRYPT_INFO_AES) |
| 408 |
stream->flags |= PPSTREAM_ENCRYPTED_AES; |
| 409 |
else if (cflags & PPCRYPT_INFO_RC4) |
| 410 |
stream->flags |= PPSTREAM_ENCRYPTED_RC4; |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
else |
| 415 |
{ |
| 416 |
if ((crypt->flags & PPCRYPT_NO_METADATA) && (tname = ppdict_get_name(dict, "Type")) != NULL && ppname_is(tname, "Metadata")) |
| 417 |
; /* special treatment of metadata stream; we assume that explicit /Filter /Crypt setup overrides document level setup of EncryptMetadata. */ |
| 418 |
else if (stream->filespec == NULL) /* external files are not encrypted, expect embedded files (not supported yet) */ |
| 419 |
{ |
| 420 |
if (crypt->flags & PPCRYPT_STREAM_RC4) |
| 421 |
stream->flags |= PPSTREAM_ENCRYPTED_RC4; |
| 422 |
else if (crypt->flags & PPCRYPT_STREAM_AES) |
| 423 |
stream->flags |= PPSTREAM_ENCRYPTED_AES; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
/* finally, if the stream is encrypted with non-identity crypt (implicit or explicit), make and save the crypt key */ |
| 428 |
if (stream->flags & PPSTREAM_ENCRYPTED) |
| 429 |
stream->cryptkey = ppcrypt_stmkey(crypt, ref, ((stream->flags & PPSTREAM_ENCRYPTED_AES) != 0), &pdf->heap); |
| 430 |
} |
| 431 |
|
| 432 |
void ppstream_filter_info (ppstream *stream, ppstream_filter *info, int decode) |
| 433 |
{ |
| 434 |
size_t from, index; |
| 435 |
ppstreamtp filtertype; |
| 436 |
ppdict *params; |
| 437 |
|
| 438 |
*info = stream->filter; |
| 439 |
if (info->count > 0) |
| 440 |
{ |
| 441 |
from = (stream->flags & PPSTREAM_ENCRYPTED_OWN) && info->filters[0] == PPSTREAM_CRYPT ? 1 : 0; |
| 442 |
if (decode) |
| 443 |
{ |
| 444 |
for (index = from; index < info->count; ++index) |
| 445 |
{ |
| 446 |
filtertype = info->filters[index]; |
| 447 |
if (ppstream_image_filter(filtertype)) |
| 448 |
{ |
| 449 |
break; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
else |
| 454 |
{ |
| 455 |
index = from; |
| 456 |
} |
| 457 |
if (index > 0) { |
| 458 |
info->count -= index; |
| 459 |
if (info->count > 0) |
| 460 |
{ |
| 461 |
info->filters += index; |
| 462 |
if (info->params != NULL) |
| 463 |
{ |
| 464 |
info->params += index; |
| 465 |
for (index = 0, params = NULL; index < info->count; ++index) |
| 466 |
if ((params = info->params[index]) != NULL) |
| 467 |
break; |
| 468 |
if (params == NULL) |
| 469 |
info->params = NULL; |
| 470 |
} |
| 471 |
} |
| 472 |
else |
| 473 |
{ |
| 474 |
info->filters = NULL; |
| 475 |
info->params = NULL; |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/* */ |
| 482 |
|
| 483 |
void ppstream_init_buffers (void) |
| 484 |
{ |
| 485 |
iof_filters_init(); |
| 486 |
} |
| 487 |
|
| 488 |
void ppstream_free_buffers (void) |
| 489 |
{ |
| 490 |
iof_filters_free(); |
| 491 |
} |