Skip to content

Commit

Permalink
Fix opening of certain encrypted PDF files (Issue #62)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Aug 21, 2024
1 parent 7c36516 commit 7d22477
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion pdfio-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}


Expand Down
10 changes: 4 additions & 6 deletions pdfio-stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 7d22477

Please sign in to comment.