From 038fd8686b641bb80c5eb509bc54c22d7babfb35 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Mon, 11 Dec 2023 19:56:00 -0500 Subject: [PATCH] Fix trailer dictionary handling (Issue #58) Fix generation number handling for object 0 (Issue #59) --- CHANGES.md | 4 ++++ pdfio-file.c | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6a8ad9c..d69bbfd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,10 @@ v1.2.0 (Month DD, YYYY) - Updated the token reading code to protect against some obvious abuses of the PDF format. - Updated the xref reading code to protect against loops. +- Fixed handling of of trailer dictionaries that started immediately after the + "trailer" keyword (Issue #58) +- Fixed handling of invalid, but common, PDF files with a generation number of + 65536 in the xref table (Issue #59) v1.1.4 (December 3, 2023) diff --git a/pdfio-file.c b/pdfio-file.c index 5e7d97d..53eee57 100644 --- a/pdfio-file.c +++ b/pdfio-file.c @@ -1736,7 +1736,7 @@ load_xref( return (false); } - if ((generation = (int)strtol(ptr, &ptr, 10)) < 0 || generation > 65535) + if ((generation = (int)strtol(ptr, &ptr, 10)) < 0 || (generation > 65535 && number != 0)) { _pdfioFileError(pdf, "Bad xref table header '%s'.", line); return (false); @@ -1979,9 +1979,20 @@ load_xref( while (_pdfioFileGets(pdf, line, sizeof(line))) { if (!strncmp(line, "trailer", 7) && (!line[7] || isspace(line[7] & 255))) + { + if (line[7]) + { + // Probably the start of the trailer dictionary, rewind the file so + // we can read it... + _pdfioFileSeek(pdf, 7 - strlen(line), SEEK_CUR); + } + break; + } else if (!line[0]) + { continue; + } if (sscanf(line, "%jd%jd", &number, &num_objects) != 2) { @@ -2012,7 +2023,7 @@ load_xref( return (false); } - if ((generation = (int)strtol(ptr, &ptr, 10)) < 0 || generation > 65535) + if ((generation = (int)strtol(ptr, &ptr, 10)) < 0 || (generation > 65535 && offset != 0)) { _pdfioFileError(pdf, "Malformed xref table entry '%s'.", line); return (false);