Skip to content

Commit e6b14bf

Browse files
author
Maruan Sahyoun
committed
PDFBOX-6390: Search the file for xref tables and streams only once
Co-Authored-By: Claude <noreply@anthropic.com> git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1941414 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3ae1856 commit e6b14bf

2 files changed

Lines changed: 101 additions & 2 deletions

File tree

‎pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BruteForceParser.java‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public class BruteForceParser
7575
*/
7676
private final Map<COSObjectKey, Long> bfSearchCOSObjectKeyOffsets = new HashMap<>();
7777

78+
/**
79+
* Contains the offsets of all xref tables and streams found by a brute force search. The file
80+
* is searched only once, an offset is removed when it was used to fix a reference.
81+
*/
82+
private List<Long> bfSearchXRefTablesOffsets = null;
83+
private List<Long> bfSearchXRefStreamsOffsets = null;
84+
7885
private boolean bfSearchTriggered = false;
7986

8087
private final COSParser parser;
@@ -225,9 +232,15 @@ protected long bfSearchForXRef(long xrefOffset) throws IOException
225232
long newOffset = -1;
226233

227234
// initialize bfSearchXRefTablesOffsets -> not null
228-
List<Long> bfSearchXRefTablesOffsets = bfSearchForXRefTables();
235+
if (bfSearchXRefTablesOffsets == null)
236+
{
237+
bfSearchXRefTablesOffsets = bfSearchForXRefTables();
238+
}
229239
// initialize bfSearchXRefStreamsOffsets -> not null
230-
List<Long> bfSearchXRefStreamsOffsets = bfSearchForXRefStreams();
240+
if (bfSearchXRefStreamsOffsets == null)
241+
{
242+
bfSearchXRefStreamsOffsets = bfSearchForXRefStreams();
243+
}
231244

232245
// TODO to be optimized, this won't work in every case
233246
long newOffsetTable = searchNearestValue(bfSearchXRefTablesOffsets, xrefOffset);

‎pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.pdfbox.cos.COSDocument;
3333
import org.apache.pdfbox.cos.COSName;
3434
import org.apache.pdfbox.cos.COSObjectKey;
35+
import org.apache.pdfbox.io.RandomAccessReadBuffer;
3536
import org.apache.pdfbox.pdmodel.PDDocument;
3637
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
3738
import org.apache.pdfbox.pdmodel.font.PDFont;
@@ -437,4 +438,89 @@ private static void writeXrefEntry(ByteArrayOutputStream xref, int type, int fie
437438
xref.write(field3 >>> 8);
438439
xref.write(field3);
439440
}
441+
442+
/**
443+
* A chain of 1000 xref tables whose /Prev offsets point one byte behind the previous table.
444+
* The file must not be searched again for each broken offset.
445+
*
446+
* @throws IOException
447+
*/
448+
@Test
449+
void testBrokenPrevChain() throws IOException
450+
{
451+
byte[] pdf = createBrokenPrevChainPDF(1000, 100000);
452+
CountingRandomAccessReadBuffer source = new CountingRandomAccessReadBuffer(pdf);
453+
try (PDDocument doc = Loader.loadPDF(source))
454+
{
455+
assertEquals(0, doc.getNumberOfPages());
456+
}
457+
assertTrue(source.bytesRead < 10L * pdf.length, source.bytesRead + " bytes read");
458+
}
459+
460+
/**
461+
* Creates a PDF with a comment of the given length and a chain of the given number of xref
462+
* tables. Each /Prev offset points one byte behind the start of the previous xref table.
463+
*/
464+
private static byte[] createBrokenPrevChainPDF(int sections, int commentLength)
465+
{
466+
ByteArrayOutputStream pdf = new ByteArrayOutputStream();
467+
write(pdf, "%PDF-1.4\n%" + new String(new char[commentLength]).replace('\0', 'a') + "\n");
468+
int catalog = pdf.size();
469+
write(pdf, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
470+
int pages = pdf.size();
471+
write(pdf, "2 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\n");
472+
int previous = pdf.size();
473+
write(pdf, String.format("xref\n0 3\n0000000000 65535 f\r\n%010d 00000 n\r\n"
474+
+ "%010d 00000 n\r\ntrailer\n<< /Size 3 /Root 1 0 R >>\n", catalog, pages));
475+
for (int i = 1; i < sections; i++)
476+
{
477+
int offset = pdf.size();
478+
write(pdf, "xref\n0 1\n0000000000 65535 f\r\ntrailer\n<< /Size 3 /Root 1 0 R /Prev "
479+
+ (previous + 1) + " >>\n");
480+
previous = offset;
481+
}
482+
write(pdf, "startxref\n" + previous + "\n%%EOF\n");
483+
return pdf.toByteArray();
484+
}
485+
486+
private static void write(ByteArrayOutputStream out, String text)
487+
{
488+
byte[] bytes = text.getBytes(StandardCharsets.US_ASCII);
489+
out.write(bytes, 0, bytes.length);
490+
}
491+
492+
/**
493+
* Counts the bytes read from the buffer.
494+
*/
495+
private static class CountingRandomAccessReadBuffer extends RandomAccessReadBuffer
496+
{
497+
private long bytesRead = 0;
498+
499+
CountingRandomAccessReadBuffer(byte[] input)
500+
{
501+
super(input);
502+
}
503+
504+
@Override
505+
public int read() throws IOException
506+
{
507+
int b = super.read();
508+
if (b != -1)
509+
{
510+
bytesRead++;
511+
}
512+
return b;
513+
}
514+
515+
@Override
516+
public int read(byte[] b, int offset, int length) throws IOException
517+
{
518+
int n = super.read(b, offset, length);
519+
if (n > 0)
520+
{
521+
bytesRead += n;
522+
}
523+
return n;
524+
}
525+
}
440526
}

0 commit comments

Comments
 (0)