From 7d224779170cf6e3a6cb1d844c1e878f5ad3a5e8 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Wed, 21 Aug 2024 11:28:39 -0400 Subject: [PATCH] Fix opening of certain encrypted PDF files (Issue #62) --- CHANGES.md | 1 + pdfio-file.c | 12 +++++++++++- pdfio-stream.c | 10 ++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d82e8e1..42baa8b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ v1.3.2 - YYYY-MM-DD ------------------- - Added some more sanity checks to the TrueType font reader. +- Fixed an issue when opening certain encrypted PDF files (Issue #62) v1.3.1 - 2024-08-05 diff --git a/pdfio-file.c b/pdfio-file.c index abc2f65..ee972a8 100644 --- a/pdfio-file.c +++ b/pdfio-file.c @@ -648,11 +648,12 @@ pdfioFileFindObj( if ((current = number - 1) >= pdf->num_objs) current = pdf->num_objs / 2; - PDFIO_DEBUG("pdfioFileFindObj: objs[current=%lu]=%p\n", (unsigned long)current, (void *)pdf->objs[current]); + PDFIO_DEBUG("pdfioFileFindObj: objs[current=%lu]=%p(%lu)\n", (unsigned long)current, (void *)pdf->objs[current], (unsigned long)(pdf->objs[current] ? pdf->objs[current]->number : 0)); if (number == pdf->objs[current]->number) { // Fast match... + PDFIO_DEBUG("pdfioFileFindObj: Returning %lu (%p)\n", (unsigned long)current, pdf->objs[current]); return (pdf->objs[current]); } else if (number < pdf->objs[current]->number) @@ -679,11 +680,20 @@ pdfioFileFindObj( } if (number == pdf->objs[left]->number) + { + PDFIO_DEBUG("pdfioFileFindObj: Returning %lu (%p)\n", (unsigned long)left, pdf->objs[left]); return (pdf->objs[left]); + } else if (number == pdf->objs[right]->number) + { + PDFIO_DEBUG("pdfioFileFindObj: Returning %lu (%p)\n", (unsigned long)right, pdf->objs[right]); return (pdf->objs[right]); + } else + { + PDFIO_DEBUG("pdfioFileFindObj: Returning NULL\n"); return (NULL); + } } diff --git a/pdfio-stream.c b/pdfio-stream.c index 3a7f419..d7ebcb3 100644 --- a/pdfio-stream.c +++ b/pdfio-stream.c @@ -408,6 +408,7 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object pdfio_stream_t *st; // Stream pdfio_dict_t *dict = pdfioObjGetDict(obj); // Object dictionary + const char *type; // Object type PDFIO_DEBUG("_pdfioStreamOpen(obj=%p(%u), decode=%s)\n", obj, (unsigned)obj->number, decode ? "true" : "false"); @@ -434,7 +435,9 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object return (NULL); } - if (obj->pdf->encryption) + type = pdfioObjGetType(obj); + + if (obj->pdf->encryption && (!type || strcmp(type, "XRef"))) { uint8_t iv[64]; // Initialization vector size_t ivlen; // Length of initialization vector, if any @@ -1069,11 +1072,6 @@ stream_read(pdfio_stream_t *st, // I - Stream _pdfioFileError(st->pdf, "Unable to decompress stream data for object %ld: %s", (long)st->obj->number, zstrerror(status)); return (-1); } - else if (avail_in == st->flate.avail_in && avail_out == st->flate.avail_out) - { - _pdfioFileError(st->pdf, "Corrupt stream data."); - return (-1); - } return (st->flate.next_out - (Bytef *)buffer); }