|
32 | 32 | import org.apache.pdfbox.cos.COSDocument; |
33 | 33 | import org.apache.pdfbox.cos.COSName; |
34 | 34 | import org.apache.pdfbox.cos.COSObjectKey; |
| 35 | +import org.apache.pdfbox.io.RandomAccessReadBuffer; |
35 | 36 | import org.apache.pdfbox.pdmodel.PDDocument; |
36 | 37 | import org.apache.pdfbox.pdmodel.PDDocumentInformation; |
37 | 38 | import org.apache.pdfbox.pdmodel.font.PDFont; |
@@ -437,4 +438,89 @@ private static void writeXrefEntry(ByteArrayOutputStream xref, int type, int fie |
437 | 438 | xref.write(field3 >>> 8); |
438 | 439 | xref.write(field3); |
439 | 440 | } |
| 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 | + } |
440 | 526 | } |
0 commit comments