From 1438f7125f9bb4ad6f08665986df182bdc8ce953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Sun, 15 Nov 2026 12:40:26 +0100 Subject: [PATCH 01/11] Harden PDF/A and PDF/X conformance, memory safety, and Rust bounds checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../software/jpdfium/doc/PdfCompressor.java | 51 +-- .../software/jpdfium/doc/PdfXConverter.java | 63 +++- .../software/jpdfium/PdfTestAssertions.java | 132 ++++++++ .../software/jpdfium/SaveTransportTest.java | 6 +- .../jpdfium/fonts/FontNormalizerTest.java | 12 +- .../jpdfium/redact/RedactionSessionTest.java | 4 +- native/bridge/include/jpdfium_internal.h | 9 +- native/bridge/include/jpdfium_qpdf_internal.h | 3 - native/bridge/src/jpdfium_advanced.cpp | 6 +- native/bridge/src/jpdfium_color.cpp | 161 ++++++--- native/bridge/src/jpdfium_font_normalizer.cpp | 75 ++-- native/bridge/src/jpdfium_pattern.cpp | 168 ++++++--- native/bridge/src/jpdfium_pdfx.cpp | 319 ++++++++++++++++-- .../bridge/src/jpdfium_qpdf_fonts_embed.cpp | 31 +- .../bridge/src/jpdfium_qpdf_fonts_subset.cpp | 50 +-- native/bridge/src/jpdfium_qpdf_pages.cpp | 30 +- native/bridge/src/jpdfium_qpdf_sanitize.cpp | 15 +- native/bridge/src/jpdfium_redact.cpp | 7 +- native/bridge/src/jpdfium_render.cpp | 2 +- native/bridge/src/jpdfium_sanitize.cpp | 94 ++++-- native/bridge/src/jpdfium_signature.cpp | 16 +- native/rust/jpdfium-impl/src/lib.rs | 262 +++++++++++++- 22 files changed, 1180 insertions(+), 336 deletions(-) create mode 100644 jpdfium/src/test/java/stirling/software/jpdfium/PdfTestAssertions.java diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java index 6b22e4380..02fb536de 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java @@ -66,14 +66,17 @@ public final class PdfCompressor { private static final byte[][] PDFA_MARKERS = { "pdfaid:part>".getBytes(StandardCharsets.US_ASCII), "pdfaid:part=\"".getBytes(StandardCharsets.US_ASCII), + "pdfaid:part='".getBytes(StandardCharsets.US_ASCII), "pdfaid:conformance>".getBytes(StandardCharsets.US_ASCII), "pdfaid:conformance=\"".getBytes(StandardCharsets.US_ASCII), + "pdfaid:conformance='".getBytes(StandardCharsets.US_ASCII), "GTS_PDFA1".getBytes(StandardCharsets.US_ASCII), }; // PDF/A-1 forbids object streams and cross-reference streams; later parts allow them. private static final byte[][] PDFA_PART1_MARKERS = { "pdfaid:part>1<".getBytes(StandardCharsets.US_ASCII), "pdfaid:part=\"1\"".getBytes(StandardCharsets.US_ASCII), + "pdfaid:part='1'".getBytes(StandardCharsets.US_ASCII), "GTS_PDFA1".getBytes(StandardCharsets.US_ASCII), }; // Catalog /Metadata reference. XMP streams are often Flate-compressed, so a @@ -112,18 +115,21 @@ public final class PdfCompressor { // instead of a full pass per marker. private static final int N_PDFA_PART = 0; private static final int N_PDFA_PART_ATTR = 1; - private static final int N_PDFA_CONF = 2; - private static final int N_PDFA_CONF_ATTR = 3; - private static final int N_GTS_PDFA1 = 4; - private static final int N_PDFA1_PART = 5; - private static final int N_PDFA1_PART_ATTR = 6; - private static final int N_METADATA = 7; - private static final int N_BYTE_RANGE = 8; - private static final int N_TYPE_SIG = 9; - private static final int N_TYPE_SPACE_SIG = 10; - private static final int N_SUBFILTER = 11; - private static final int N_OBJSTM = 12; - private static final int N_NEEDLES = 13; + private static final int N_PDFA_PART_SQ = 2; + private static final int N_PDFA_CONF = 3; + private static final int N_PDFA_CONF_ATTR = 4; + private static final int N_PDFA_CONF_SQ = 5; + private static final int N_GTS_PDFA1 = 6; + private static final int N_PDFA1_PART = 7; + private static final int N_PDFA1_PART_ATTR = 8; + private static final int N_PDFA1_PART_SQ = 9; + private static final int N_METADATA = 10; + private static final int N_BYTE_RANGE = 11; + private static final int N_TYPE_SIG = 12; + private static final int N_TYPE_SPACE_SIG = 13; + private static final int N_SUBFILTER = 14; + private static final int N_OBJSTM = 15; + private static final int N_NEEDLES = 16; // The /ID key is matched byte-by-byte and its range is packed into a long, so the // signed fast path performs no allocation of its own. @@ -140,8 +146,11 @@ public final class PdfCompressor { PDFA_MARKERS[2], PDFA_MARKERS[3], PDFA_MARKERS[4], + PDFA_MARKERS[5], + PDFA_MARKERS[6], PDFA_PART1_MARKERS[0], PDFA_PART1_MARKERS[1], + PDFA_PART1_MARKERS[2], METADATA_MARKER, BYTE_RANGE, SIG_MARKERS[0], @@ -427,7 +436,7 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) { // generated-xref result would break the conformance it claims. byte[] opt = pdfA1 ? PdfOptimizer.optimize(resultBytes, PdfOptimizer.DEFAULT, 9, - PdfOptimizer.OBJECT_STREAMS_PRESERVE, + PdfOptimizer.OBJECT_STREAMS_DISABLE, PdfOptimizer.STREAM_DATA_COMPRESS, PdfOptimizer.DECODE_LEVEL_GENERALIZED) : PdfOptimizer.optimize(resultBytes, @@ -443,21 +452,22 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) { resultBytes = opt; streamsOptimized = true; actions.add(pdfA1 - ? "qpdf: recompressed streams (object streams preserved for PDF/A-1)" + ? "qpdf: recompressed streams (object streams disabled for PDF/A-1)" : "qpdf: recompressed streams and generated object streams"); } else { warnings.add("qpdf stream pass did not reduce size; kept original bytes"); } } else if (!signed && opts.removeUnusedObjects()) { - // Compact without restructuring object streams. Explicit flags are - // required: the all-DEFAULT form measured worse than the source on - // some inputs (inflating streams instead of recompressing them). + // Compact without restructuring object streams (disable for PDF/A-1). + // Explicit flags are required: the all-DEFAULT form measured worse than the + // source on some inputs (inflating streams instead of recompressing them). byte[] opt = PdfOptimizer.optimize(resultBytes, PdfOptimizer.RECOMPRESS_FLATE | PdfOptimizer.COMPRESS_STREAMS | PdfOptimizer.DEDUPLICATE_IMAGES | (jbig2 ? PdfOptimizer.JBIG2 : 0) | (opts.ccittG4() ? PdfOptimizer.CCITT_G4 : 0), 9, - PdfOptimizer.OBJECT_STREAMS_PRESERVE, + pdfA1 ? PdfOptimizer.OBJECT_STREAMS_DISABLE + : PdfOptimizer.OBJECT_STREAMS_PRESERVE, PdfOptimizer.STREAM_DATA_COMPRESS, PdfOptimizer.DECODE_LEVEL_GENERALIZED); if (opt != null && opt.length < resultBytes.length) { @@ -646,7 +656,8 @@ private static int pdfaPart(byte[] pdf, Markers m) { return -1; } boolean rawPdfa = m.first[N_PDFA_PART] >= 0 || m.first[N_PDFA_PART_ATTR] >= 0 - || m.first[N_PDFA_CONF] >= 0 || m.first[N_PDFA_CONF_ATTR] >= 0 + || m.first[N_PDFA_PART_SQ] >= 0 || m.first[N_PDFA_CONF] >= 0 + || m.first[N_PDFA_CONF_ATTR] >= 0 || m.first[N_PDFA_CONF_SQ] >= 0 || m.first[N_GTS_PDFA1] >= 0; byte[] xmp = null; if (!rawPdfa) { @@ -656,7 +667,7 @@ private static int pdfaPart(byte[] pdf, Markers m) { } } boolean part1 = m.first[N_PDFA1_PART] >= 0 || m.first[N_PDFA1_PART_ATTR] >= 0 - || m.first[N_GTS_PDFA1] >= 0 + || m.first[N_PDFA1_PART_SQ] >= 0 || m.first[N_GTS_PDFA1] >= 0 || (xmp != null && containsAny(xmp, PDFA_PART1_MARKERS)); return part1 ? 1 : 2; } diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfXConverter.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfXConverter.java index e5cb669b1..98eb7d049 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfXConverter.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfXConverter.java @@ -314,15 +314,74 @@ private static void requireSamePageCount(int expected, int actual) { } } + private static Rect clampToMediaBox(Rect box, Rect media) { + float mx0 = Math.min(media.x(), media.x() + media.width()); + float my0 = Math.min(media.y(), media.y() + media.height()); + float mx1 = Math.max(media.x(), media.x() + media.width()); + float my1 = Math.max(media.y(), media.y() + media.height()); + float bx0 = Math.min(box.x(), box.x() + box.width()); + float by0 = Math.min(box.y(), box.y() + box.height()); + float bx1 = Math.max(box.x(), box.x() + box.width()); + float by1 = Math.max(box.y(), box.y() + box.height()); + float nx0 = Math.max(mx0, Math.min(mx1, bx0)); + float ny0 = Math.max(my0, Math.min(my1, by0)); + float nx1 = Math.max(mx0, Math.min(mx1, bx1)); + float ny1 = Math.max(my0, Math.min(my1, by1)); + if (nx1 <= nx0 || ny1 <= ny0) { + return new Rect(mx0, my0, mx1 - mx0, my1 - my0); + } + return new Rect(nx0, ny0, nx1 - nx0, ny1 - ny0); + } + + private static boolean rectDiffers(Rect a, Rect b) { + return Math.abs(a.x() - b.x()) > 1e-4f + || Math.abs(a.y() - b.y()) > 1e-4f + || Math.abs(a.width() - b.width()) > 1e-4f + || Math.abs(a.height() - b.height()) > 1e-4f; + } + private static void applyPageBoxes(PdfDocument document) { for (int i = 0; i < document.pageCount(); i++) { try (PdfPage page = document.page(i)) { + Rect media = page.getMediaBox(); + page.getCropBox() + .ifPresent( + box -> { + Rect clamped = clampToMediaBox(box, media); + if (rectDiffers(box, clamped)) { + page.setCropBox(clamped); + } + }); + page.getBleedBox() + .ifPresent( + box -> { + Rect clamped = clampToMediaBox(box, media); + if (rectDiffers(box, clamped)) { + page.setBleedBox(clamped); + } + }); + page.getTrimBox() + .ifPresent( + box -> { + Rect clamped = clampToMediaBox(box, media); + if (rectDiffers(box, clamped)) { + page.setTrimBox(clamped); + } + }); + page.getArtBox() + .ifPresent( + box -> { + Rect clamped = clampToMediaBox(box, media); + if (rectDiffers(box, clamped)) { + page.setArtBox(clamped); + } + }); // ISO 15930 forbids having both TrimBox and ArtBox. if (page.getTrimBox().isPresent() || page.getArtBox().isPresent()) { continue; } - Rect fallback = page.getCropBox().orElseGet(page::getMediaBox); - page.setTrimBox(fallback); + Rect fallback = page.getCropBox().orElse(media); + page.setTrimBox(clampToMediaBox(fallback, media)); } } } diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/PdfTestAssertions.java b/jpdfium/src/test/java/stirling/software/jpdfium/PdfTestAssertions.java new file mode 100644 index 000000000..0af427fc9 --- /dev/null +++ b/jpdfium/src/test/java/stirling/software/jpdfium/PdfTestAssertions.java @@ -0,0 +1,132 @@ +package stirling.software.jpdfium; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + +/** + * Shared binary assertion helpers for PDF serialization determinism tests. + */ +public final class PdfTestAssertions { + + private PdfTestAssertions() {} + + /** + * Asserts byte-for-byte equality except for the second trailer {@code /ID} hex string, + * which PDFium regenerates on every save when an input document already had an {@code /ID} + * (ISO 32000-1 §14.4). Still validates that {@code /ID[0]} is identical and {@code /ID[1]} + * contains well-formed even-length hex digits. + */ + public static void assertBytesEqualExceptTrailerId(byte[] expected, byte[] actual, String message) { + TrailerIdSlots expSlots = findTrailerIdSlots(expected); + TrailerIdSlots actSlots = findTrailerIdSlots(actual); + if (expSlots == null || !expSlots.sameSecondRange(actSlots)) { + assertEquals(expected.length, actual.length, message + " (length mismatch)"); + assertTrue(Arrays.equals(expected, actual), message); + return; + } + assertValidHexSlot(expected, expSlots.secondStart, expSlots.secondEnd, message + " (expected /ID[1])"); + assertValidHexSlot(actual, actSlots.secondStart, actSlots.secondEnd, message + " (actual /ID[1])"); + assertBytesEqualOutsideRange(expected, actual, expSlots.secondStart + 1, expSlots.secondEnd - 1, message); + } + + /** + * Asserts byte-for-byte equality except for both trailer {@code /ID} hex strings, which + * PDFium generates fresh on each save when the input document had no {@code /ID} (ISO 32000-1 + * §14.4). Validates that both hex slots remain well-formed even-length hex digits. + */ + public static void assertBytesEqualExceptGeneratedTrailerIds(byte[] expected, byte[] actual, String message) { + TrailerIdSlots expSlots = findTrailerIdSlots(expected); + TrailerIdSlots actSlots = findTrailerIdSlots(actual); + if (expSlots == null || !expSlots.sameOverallRange(actSlots)) { + assertEquals(expected.length, actual.length, message + " (length mismatch)"); + assertTrue(Arrays.equals(expected, actual), message); + return; + } + assertValidHexSlot(expected, expSlots.firstStart, expSlots.firstEnd, message + " (expected /ID[0])"); + assertValidHexSlot(actual, actSlots.firstStart, actSlots.firstEnd, message + " (actual /ID[0])"); + assertValidHexSlot(expected, expSlots.secondStart, expSlots.secondEnd, message + " (expected /ID[1])"); + assertValidHexSlot(actual, actSlots.secondStart, actSlots.secondEnd, message + " (actual /ID[1])"); + assertBytesEqualOutsideRange(expected, actual, expSlots.firstStart + 1, expSlots.secondEnd - 1, message); + } + + private static void assertBytesEqualOutsideRange(byte[] expected, byte[] actual, + int skipStart, int skipEnd, String message) { + assertEquals(expected.length, actual.length, message + " (length mismatch)"); + int prefixMismatch = Arrays.mismatch(expected, 0, skipStart, actual, 0, skipStart); + if (prefixMismatch >= 0) { + throw new AssertionError(message + " (bytes differ before /ID at offset " + prefixMismatch + + ": expected=" + snippet(expected, prefixMismatch) + + ", actual=" + snippet(actual, prefixMismatch) + ")"); + } + int suffixMismatch = Arrays.mismatch(expected, skipEnd, expected.length, actual, skipEnd, actual.length); + if (suffixMismatch >= 0) { + int pos = skipEnd + suffixMismatch; + throw new AssertionError(message + " (bytes differ after /ID at offset " + pos + + ": expected=" + snippet(expected, pos) + + ", actual=" + snippet(actual, pos) + ")"); + } + } + + private static String snippet(byte[] data, int center) { + int lo = Math.max(0, center - 24); + int hi = Math.min(data.length, center + 40); + return new String(data, lo, hi - lo, StandardCharsets.ISO_8859_1) + .replace("\r", "\\r").replace("\n", "\\n"); + } + + private static void assertValidHexSlot(byte[] pdf, int start, int end, String label) { + int hexLen = end - start - 2; + assertTrue(hexLen > 0 && (hexLen & 1) == 0 && pdf[start] == '<' && pdf[end - 1] == '>', + label + " must be a non-empty even-length hex string"); + for (int i = start + 1; i < end - 1; i++) { + byte b = pdf[i]; + boolean isHex = (b >= '0' && b <= '9') + || (b >= 'a' && b <= 'f') + || (b >= 'A' && b <= 'F'); + assertTrue(isHex, label + " contains non-hex byte at offset " + i); + } + } + + private record TrailerIdSlots(int firstStart, int firstEnd, int secondStart, int secondEnd) { + boolean sameSecondRange(TrailerIdSlots other) { + return other != null && secondStart == other.secondStart && secondEnd == other.secondEnd; + } + + boolean sameOverallRange(TrailerIdSlots other) { + return other != null && firstStart == other.firstStart && secondEnd == other.secondEnd; + } + } + + private static TrailerIdSlots findTrailerIdSlots(byte[] pdf) { + int id = -1; + for (int i = pdf.length - 3; i >= 0; i--) { + if (pdf[i] == '/' && pdf[i + 1] == 'I' && pdf[i + 2] == 'D') { + id = i; + break; + } + } + if (id < 0) { + return null; + } + int firstOpen = indexOfByte(pdf, (byte) '<', id + 3); + int firstClose = firstOpen < 0 ? -1 : indexOfByte(pdf, (byte) '>', firstOpen + 1); + int secondOpen = firstClose < 0 ? -1 : indexOfByte(pdf, (byte) '<', firstClose + 1); + int secondClose = secondOpen < 0 ? -1 : indexOfByte(pdf, (byte) '>', secondOpen + 1); + if (secondClose < 0) { + return null; + } + return new TrailerIdSlots(firstOpen, firstClose + 1, secondOpen, secondClose + 1); + } + + private static int indexOfByte(byte[] haystack, byte needle, int from) { + for (int i = Math.max(from, 0); i < haystack.length; i++) { + if (haystack[i] == needle) { + return i; + } + } + return -1; + } +} diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/SaveTransportTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/SaveTransportTest.java index 199462d0e..bdd02d6b7 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/SaveTransportTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/SaveTransportTest.java @@ -93,9 +93,9 @@ void saveToChannelMatchesSaveBytes() throws Exception { } byte[] viaChannel = baos.toByteArray(); assertTrue(viaChannel.length > 0); - // Byte-identical: both paths serialize the same untouched document, so - // equal length alone would let a corrupt payload of the same size pass. - assertArrayEquals(expected, viaChannel, + // Byte-identical outside ISO 32000-1 §14.4 volatile trailer /ID (minimal.pdf has no + // prior /ID, so PDFium synthesizes fresh hex pair strings on every save). + PdfTestAssertions.assertBytesEqualExceptGeneratedTrailerIds(expected, viaChannel, "channel spool must carry the same bytes as saveBytes for minimal.pdf"); } diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java index 115903b40..23e591404 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java @@ -748,11 +748,13 @@ void normalizeAllWritesCidSetForRetainedSubsetGlyphs() throws Exception { assertNotNull(descriptor); byte[] cidSet = streamBytes(descriptor, COSName.CID_SET); assertEquals(5, cidSet.length, - "CIDSet must stop at the highest retained CID, not describe the full program"); - assertEquals((byte) 0x80, cidSet[0], "CID 0 (.notdef) must be present"); - assertEquals((byte) 0x08, cidSet[4], "CID 36 is the only drawn glyph"); - for (int i = 1; i < 4; i++) - assertEquals(0, cidSet[i], "undrawn CID " + i + " must not be present"); + "CIDSet must stop at the highest present GID in the re-subsetted /Identity program"); + // Re-subsetted /Identity program keeps glyph slots 0..36 (max_gid=36), so ISO 19005-2 + // §6.2.11.4.2 requires CIDs 0..36 present in the font program to all be marked. + for (int i = 0; i < 4; i++) { + assertEquals((byte) 0xFF, cidSet[i], "CIDSet byte " + i + " must mark all CIDs present"); + } + assertEquals((byte) 0xF8, cidSet[4], "CIDSet final byte must mark CIDs 32..36"); assertEquals("A", new PDFTextStripper().getText(loaded).strip(), "re-subsetting must preserve extracted text"); } diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/redact/RedactionSessionTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/redact/RedactionSessionTest.java index cdb1965aa..fbba6f8b6 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/redact/RedactionSessionTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/redact/RedactionSessionTest.java @@ -14,7 +14,6 @@ import stirling.software.jpdfium.redact.RedactTestPages.Line; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -363,7 +362,8 @@ void deterministicRedactionOutput() throws Exception { run2 = doc.saveBytes(); } - assertArrayEquals(run1, run2, "Redaction output bytes must be deterministic across identical runs"); + stirling.software.jpdfium.PdfTestAssertions.assertBytesEqualExceptTrailerId( + run1, run2, "Redaction output bytes must be deterministic across identical runs"); } /** diff --git a/native/bridge/include/jpdfium_internal.h b/native/bridge/include/jpdfium_internal.h index e09d470fd..8fd2ce5b4 100644 --- a/native/bridge/include/jpdfium_internal.h +++ b/native/bridge/include/jpdfium_internal.h @@ -233,13 +233,16 @@ inline std::shared_ptr makeDocCore(FPDF_DOCUMENT doc, uint8_t* buf = nu // the shared_ptr construction below can both throw, and the caller has no // handle to clean up with at that point. struct PendingDoc { - FPDF_DOCUMENT doc; - uint8_t* buf; + FPDF_DOCUMENT doc = nullptr; + uint8_t* buf = nullptr; + PendingDoc(FPDF_DOCUMENT d, uint8_t* b) : doc(d), buf(b) {} + PendingDoc(PendingDoc const&) = delete; + PendingDoc& operator=(PendingDoc const&) = delete; ~PendingDoc() { if (doc) FPDF_CloseDocument(doc); if (buf) free(buf); } - } pending{doc, buf}; + } pending(doc, buf); auto* core = new DocCore(); core->doc = pending.doc; diff --git a/native/bridge/include/jpdfium_qpdf_internal.h b/native/bridge/include/jpdfium_qpdf_internal.h index ed6408f58..806940067 100644 --- a/native/bridge/include/jpdfium_qpdf_internal.h +++ b/native/bridge/include/jpdfium_qpdf_internal.h @@ -175,9 +175,6 @@ bool subsetKeepsCidGlyphs(FT_Library lib, std::string const& subset, bool subsetKeepsSimpleGlyphs(FT_Library lib, std::string const& subsetBytes, FT_Face original, bool symbolic, jpdfium::font::SimpleEncoding const& enc, std::set const& codes, bool retainGids); -std::set cidsForRetainedGlyphs(QPDFObjectHandle const& target, - std::string const& mapBytes, - std::set const& glyphs); std::set cidsPresentInProgram(QPDFObjectHandle const& target, FT_Face face); void updateCidSet(QPDF& qpdf, QPDFObjectHandle descriptor, std::set const& activeCids); #endif diff --git a/native/bridge/src/jpdfium_advanced.cpp b/native/bridge/src/jpdfium_advanced.cpp index a5b637d28..d3925daa1 100644 --- a/native/bridge/src/jpdfium_advanced.cpp +++ b/native/bridge/src/jpdfium_advanced.cpp @@ -371,8 +371,10 @@ std::vector qpdfWriteBytes(QPDF& pdf) { jpdfium_qpdf_internal::WriterLock lock{9}; writer.write(); std::shared_ptr buf = writer.getBufferSharedPointer(); - std::vector result(buf->getSize()); - memcpy(result.data(), buf->getBuffer(), buf->getSize()); + if (!buf) return {}; + size_t sz = buf->getSize(); + std::vector result(sz); + if (sz > 0) memcpy(result.data(), buf->getBuffer(), sz); return result; } diff --git a/native/bridge/src/jpdfium_color.cpp b/native/bridge/src/jpdfium_color.cpp index 30268c824..318b10e5f 100644 --- a/native/bridge/src/jpdfium_color.cpp +++ b/native/bridge/src/jpdfium_color.cpp @@ -1,6 +1,7 @@ // jpdfium_color.cpp - DeviceCMYK pipeline: CMYK JPEG images and OutputIntent ICC. #include +#include #include #include #include @@ -146,14 +147,14 @@ void RgbToCmykLut(const uint8_t* lut, int32_t grid, uint8_t r, uint8_t g, uint8_ const float v0 = v00 + (v10 - v00) * dg; const float v1 = v01 + (v11 - v01) * dg; const float value = v0 + (v1 - v0) * db; - out[c] = static_cast(std::clamp(static_cast(value + 0.5f), 0, 255)); + out[c] = static_cast(std::clamp(std::lround(value), 0L, 255L)); } } // Reusable buffers for one image-conversion pass. The image pass can see many // XObjects; resizing vectors would zero-fill and reallocating would churn the // allocator. Grow only when an image is larger than the previous one. -constexpr size_t kMaxRetainedScratchBytes = 4u * 1024u * 1024u; +constexpr size_t kMaxRetainedScratchBytes = size_t{4} * 1024 * 1024; struct ImageScratch { std::unique_ptr samples; @@ -336,10 +337,10 @@ bool ImageUsesDct(FPDF_PAGEOBJECT obj) { } struct PendingImage { - FPDF_PAGE page; - FPDF_PAGEOBJECT object; - uint8_t* jpeg; - int64_t length; + FPDF_PAGE page = nullptr; + FPDF_PAGEOBJECT object = nullptr; + uint8_t* jpeg = nullptr; + int64_t length = 0; }; // Returns 1 when the image was converted, 0 when it was skipped, -1 on failure. @@ -484,7 +485,7 @@ int32_t IccComponentCount(const uint8_t* icc, size_t len) { const uint32_t tag_count = ReadUint32Be(icc + 128); if (tag_count == 0 || tag_count > (declared - 132) / 12) return 0; for (uint32_t i = 0; i < tag_count; ++i) { - const uint8_t* entry = icc + 132 + i * 12; + const uint8_t* entry = icc + 132 + static_cast(i) * 12; const uint32_t offset = ReadUint32Be(entry + 4); const uint32_t size = ReadUint32Be(entry + 8); if (size == 0 || offset < 132 || offset > declared || size > declared - offset) { @@ -523,17 +524,21 @@ void ConfigureWriter(QPDFWriter& writer) { writer.setDecodeLevel(qpdf_dl_none); } -// PDF/X fixes the base PDF version: X-4 is PDF 1.6 and X-3 is PDF 1.3. -// Force the header instead of only raising it, so a PDF 1.7 source does not -// claim a version the selected standard does not allow. +// PDF/X fixes the base PDF version: X-1a and X-3 are PDF 1.3 (no object/xref streams), +// X-4 is PDF 1.6, and X-6 is PDF 2.0. Force the header instead of only raising it, so +// a PDF 1.7 source does not claim a version the selected standard does not allow. void ConfigurePdfxWriter(QPDFWriter& writer, const char* version) { ConfigureWriter(writer); if (!version) return; const std::string value(version); if (value.find("PDF/X-4") != std::string::npos) { writer.forcePDFVersion("1.6"); - } else if (value.find("PDF/X-3") != std::string::npos) { + } else if (value.find("PDF/X-6") != std::string::npos) { + writer.forcePDFVersion("2.0"); + } else if (value.find("PDF/X-3") != std::string::npos || + value.find("PDF/X-1") != std::string::npos) { writer.forcePDFVersion("1.3"); + writer.setObjectStreamMode(qpdf_o_disable); } } @@ -575,7 +580,7 @@ QPDFObjectHandle MergeOutputIntents(QPDF* qpdf, const uint8_t* icc, size_t icc_l QPDFObjectHandle merged = QPDFObjectHandle::newArray(); QPDFObjectHandle existing = qpdf->getRoot().getKey("/OutputIntents"); if (existing.isArray()) { - for (QPDFObjectHandle item : existing.getArrayAsVector()) { + for (const QPDFObjectHandle& item : existing.getArrayAsVector()) { bool replace = false; if (item.isDictionary()) { QPDFObjectHandle s = item.getKey("/S"); @@ -720,7 +725,7 @@ std::string BuildPdfxXmp(const std::string& version, const std::string& conforma // XMP packets are edited as XML, never with libstdc++'s backtracking regex // engine: a multi-megabyte hostile packet could otherwise overflow the stack. // Packets above the cap are replaced with a minimal valid PDF/X packet. -constexpr size_t kMaxXmpBytes = 8u * 1024u * 1024u; +constexpr size_t kMaxXmpBytes = size_t{8} * 1024 * 1024; constexpr int kMaxXmpDepth = 64; constexpr const char* kRdfNs = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; constexpr const char* kPdfxNs = "http://ns.adobe.com/pdfx/1.3/"; @@ -963,6 +968,9 @@ bool EditPdfxXml(pugi::xml_document* document, const std::string& version, } } if (!title.empty()) { + for (size_t i = 1; i < descriptions.size(); ++i) { + XmlRemoveProperty(descriptions[i], "title", kDcNs, {}); + } const std::string dc_prefix = XmlEnsurePrefix(first, kDcNs, "dc"); pugi::xml_node title_node = first.child((dc_prefix + ":title").c_str()); if (!title_node) title_node = first.append_child((dc_prefix + ":title").c_str()); @@ -980,29 +988,52 @@ bool EditPdfxXml(pugi::xml_document* document, const std::string& version, } #endif -// Convert a PDF date (D:YYYYMMDDHHmmSS[Z|+HH'mm'|-HH'mm']) to the ISO 8601 -// form XMP requires. Returns an empty string when the value cannot be parsed. +// Convert a PDF date (D:YYYY[MM[DD[HH[mm[SS]]]]][Z|+HH'mm'|-HH'mm']) per +// ISO 32000-1 §7.9.4 to the ISO 8601 form XMP requires. Returns empty if invalid. std::string PdfDateToXmp(const std::string& pdf) { + // Optional "D:" prefix per ISO 32000-1 §7.9.4; require at least a 4-digit year. size_t pos = 0; if (pdf.size() >= 2 && pdf[0] == 'D' && pdf[1] == ':') pos = 2; - if (pdf.size() - pos < 14) return {}; - auto digit = [&](size_t at) { return at < pdf.size() && pdf[at] >= '0' && pdf[at] <= '9'; }; - for (size_t i = pos; i < pos + 14; ++i) { + if (pdf.size() - pos < 4) return {}; + for (size_t i = pos; i < pos + 4; ++i) { if (!digit(i)) return {}; } - auto number = [&](size_t off, size_t len) { - int value = 0; - for (size_t i = 0; i < len; ++i) value = value * 10 + (pdf[pos + off + i] - '0'); - return value; - }; + auto parse2 = [&](size_t at) { return (pdf[at] - '0') * 10 + (pdf[at + 1] - '0'); }; + const int year = (pdf[pos] - '0') * 1000 + (pdf[pos + 1] - '0') * 100 + + (pdf[pos + 2] - '0') * 10 + (pdf[pos + 3] - '0'); + size_t cur = pos + 4; + + // Missing trailing fields default per ISO 32000-1 §7.9.4 / XMP date conventions. + int month = 1; + int day = 1; + int hour = 0; + int minute = 0; + int second = 0; + if (digit(cur) && digit(cur + 1)) { + month = parse2(cur); + cur += 2; + if (digit(cur) && digit(cur + 1)) { + day = parse2(cur); + cur += 2; + if (digit(cur) && digit(cur + 1)) { + hour = parse2(cur); + cur += 2; + if (digit(cur) && digit(cur + 1)) { + minute = parse2(cur); + cur += 2; + if (digit(cur) && digit(cur + 1)) { + second = parse2(cur); + cur += 2; + } + } + } + } + } + // Reject dangling odd digit before end or timezone indicator. + if (cur < pdf.size() && digit(cur)) return {}; - const int year = number(0, 4); - const int month = number(4, 2); - const int day = number(6, 2); - const int hour = number(8, 2); - const int minute = number(10, 2); - const int second = number(12, 2); + // Validate Gregorian month-day bounds before formatting XMP ISO 8601 output. const bool leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; int monthDays = 31; if (month == 2) { @@ -1020,33 +1051,42 @@ std::string PdfDateToXmp(const std::string& pdf) { second); std::string out(buf); - size_t tz = pos + 14; + size_t tz = cur; if (tz == pdf.size()) return out; - if (pdf[tz] == 'Z' && tz + 1 == pdf.size()) { - out.push_back('Z'); - return out; + if (pdf[tz] == 'Z') { + if (tz + 1 == pdf.size()) { + out.push_back('Z'); + return out; + } + if (tz + 7 == pdf.size() && pdf.compare(tz, 7, "Z00'00'") == 0) { + out.push_back('Z'); + return out; + } + return {}; } if ((pdf[tz] != '+' && pdf[tz] != '-') || tz + 1 >= pdf.size()) return {}; const char sign = pdf[tz]; - size_t cur = tz + 1; - if (!digit(cur) || !digit(cur + 1)) return {}; - const int tz_hour = (pdf[cur] - '0') * 10 + (pdf[cur + 1] - '0'); - cur += 2; + size_t tzCur = tz + 1; + if (!digit(tzCur) || !digit(tzCur + 1)) return {}; + const int tz_hour = parse2(tzCur); + tzCur += 2; int tz_minute = 0; - if (cur < pdf.size()) { - if (pdf[cur] == '\'' || pdf[cur] == ':') { - ++cur; - } else if (!digit(cur)) { + if (tzCur < pdf.size()) { + if (pdf[tzCur] == '\'' || pdf[tzCur] == ':') { + ++tzCur; + } else if (!digit(tzCur)) { return {}; } - if (!digit(cur) || !digit(cur + 1)) return {}; - tz_minute = (pdf[cur] - '0') * 10 + (pdf[cur + 1] - '0'); - cur += 2; - if (cur < pdf.size() && pdf[cur] == '\'') ++cur; + if (tzCur < pdf.size()) { + if (!digit(tzCur) || !digit(tzCur + 1)) return {}; + tz_minute = parse2(tzCur); + tzCur += 2; + if (tzCur < pdf.size() && pdf[tzCur] == '\'') ++tzCur; + } } - if (cur != pdf.size() || tz_hour > 23 || tz_minute > 59) return {}; + if (tzCur != pdf.size() || tz_hour > 23 || tz_minute > 59) return {}; std::snprintf(buf, sizeof(buf), "%c%02d:%02d", sign, tz_hour, tz_minute); out += buf; @@ -1064,21 +1104,25 @@ void UpdateOrCreatePdfxMetadata(QPDF* qpdf, const char* version, const char* con std::string create_date; QPDFObjectHandle info_dict = qpdf->getTrailer().getKey("/Info"); if (info_dict.isDictionary()) { - if (info_dict.hasKey("/Title")) { - title = info_dict.getKey("/Title").getUTF8Value(); + QPDFObjectHandle title_obj = info_dict.getKey("/Title"); + if (title_obj.isString()) { + title = title_obj.getUTF8Value(); } - if (info_dict.hasKey("/Trapped")) { - std::string tr = info_dict.getKey("/Trapped").getName(); + QPDFObjectHandle trapped_obj = info_dict.getKey("/Trapped"); + if (trapped_obj.isName()) { + std::string tr = trapped_obj.getName(); if (tr == "/True") trapped = "True"; else if (tr == "/False") trapped = "False"; } - if (info_dict.hasKey("/ModDate")) { - mod_date = PdfDateToXmp(info_dict.getKey("/ModDate").getUTF8Value()); + QPDFObjectHandle mod_obj = info_dict.getKey("/ModDate"); + if (mod_obj.isString()) { + mod_date = PdfDateToXmp(mod_obj.getUTF8Value()); } - if (info_dict.hasKey("/CreationDate")) { - create_date = PdfDateToXmp(info_dict.getKey("/CreationDate").getUTF8Value()); + QPDFObjectHandle create_obj = info_dict.getKey("/CreationDate"); + if (create_obj.isString()) { + create_date = PdfDateToXmp(create_obj.getUTF8Value()); } } char now[32]; @@ -1177,6 +1221,7 @@ std::shared_ptr InjectPdfx(const uint8_t* input, size_t input_len, const "/OutputIntents", MergeOutputIntents(qpdf.get(), icc, icc_len, "GTS_PDFX", condition_identifier, info)); UpdateOrCreatePdfxMetadata(qpdf.get(), version, conformance); + if (version && *version) qpdf->getRoot().removeKey("/Version"); QPDFWriter writer{*qpdf}; ConfigurePdfxWriter(writer, version); @@ -1344,7 +1389,8 @@ JPDFIUM_EXPORT int32_t jpdfium_color_document_images_to_cmyk_ex(int64_t doc_hand return failure; } - std::unordered_set changed_pages; + std::unordered_set seen_pages; + std::vector changed_pages; for (PendingImage& image : pending) { FPDF_PAGE page = image.page; if (!EPDFImageObj_SetJpeg(&page, 1, image.object, image.jpeg, @@ -1354,7 +1400,9 @@ JPDFIUM_EXPORT int32_t jpdfium_color_document_images_to_cmyk_ex(int64_t doc_hand } std::free(image.jpeg); image.jpeg = nullptr; - changed_pages.insert(image.page); + if (seen_pages.insert(image.page).second) { + changed_pages.push_back(image.page); + } } for (FPDF_PAGE page : changed_pages) { if (!FPDFPage_GenerateContent(page)) return JPDFIUM_ERR_NATIVE; @@ -1537,6 +1585,7 @@ JPDFIUM_EXPORT int32_t jpdfium_color_pdfx_file(const char* in_path, const char* MergeOutputIntents(qpdf.get(), icc, static_cast(icc_len), "GTS_PDFX", condition_identifier, info)); UpdateOrCreatePdfxMetadata(qpdf.get(), version, conformance); + if (version && *version) qpdf->getRoot().removeKey("/Version"); std::string error; const bool published = WriteAndPublish( out_path, diff --git a/native/bridge/src/jpdfium_font_normalizer.cpp b/native/bridge/src/jpdfium_font_normalizer.cpp index d47a3c653..7cc6e5720 100644 --- a/native/bridge/src/jpdfium_font_normalizer.cpp +++ b/native/bridge/src/jpdfium_font_normalizer.cpp @@ -749,8 +749,7 @@ static void convertCffInSfnt(QPDF& pdf, QPDFObjectHandle& descriptor, FT_Library } static std::set cidsPresentInProgram(FT_Face face, QPDFObjectHandle& cidToGid, - bool cidIdentity, - std::map const& gidToCid) { + bool cidIdentity) { std::set cids; if (cidToGid.isStream()) { try { @@ -770,8 +769,10 @@ static std::set cidsPresentInProgram(FT_Face face, QPDFObjectHandle& c return cids; } if (face && FT_IS_CID_KEYED(face)) { - for (auto const& [_, cidVal] : gidToCid) { - if (cidVal <= kMaxCid) cids.insert(cidVal); + for (FT_UInt gid = 0; gid < static_cast(face->num_glyphs); ++gid) { + FT_UInt cidVal = 0; + if (FT_Get_CID_From_Glyph_Index(face, gid, &cidVal) == 0 && cidVal <= kMaxCid) + cids.insert(static_cast(cidVal)); } return cids; } @@ -782,42 +783,6 @@ static std::set cidsPresentInProgram(FT_Face face, QPDFObjectHandle& c return cids; } -static std::set cidsForRetainedGlyphs(QPDFObjectHandle& cidToGid, bool cidIdentity, - std::map const& gidToCid, - std::set const& glyphs) { - std::set cids{0}; - if (cidToGid.isStream()) { - try { - std::shared_ptr data = cidToGid.getStreamData(qpdf_dl_generalized); - if (data) { - const auto* bytes = static_cast(data->getBuffer()); - const size_t entries = data->getSize() / 2; - for (size_t c = 0; c < entries && c <= kMaxCid; ++c) { - const uint32_t gid = - (static_cast(bytes[2 * c]) << 8) | bytes[2 * c + 1]; - if (glyphs.contains(static_cast(gid))) - cids.insert(static_cast(c)); - } - } - } catch (...) { - return {0}; - } - return cids; - } - if (cidIdentity && !gidToCid.empty()) { - for (FT_UInt gid : glyphs) { - auto found = gidToCid.find(static_cast(gid)); - if (found != gidToCid.end() && found->second <= kMaxCid) cids.insert(found->second); - } - return cids; - } - if (cidIdentity) { - for (FT_UInt gid : glyphs) - if (gid <= kMaxCid) cids.insert(static_cast(gid)); - } - return cids; -} - static void writeCidSetIfDifferent(QPDF& pdf, QPDFObjectHandle& descriptor, std::set cids, FontNormalizeState& state) { if (cids.empty()) return; @@ -1179,15 +1144,29 @@ static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool } if (cid && full && descriptor.hasKey("/CIDSet")) { - if (didResubset) { - writeCidSetIfDifferent( - pdf, descriptor, - cidsForRetainedGlyphs(cidToGid, cidIdentity, gidToCid, retainedGlyphs), state); - } else { - writeCidSetIfDifferent(pdf, descriptor, - cidsPresentInProgram(rawFace, cidToGid, cidIdentity, gidToCid), - state); + FT_Face cidSetFace = rawFace; + FtFaceGuard cidSetGuard; + std::string cidSetOwned; + QPDFObjectHandle finalStream = embeddedFontProgram(descriptor); + if (finalStream.isStream()) { + try { + std::shared_ptr finalData = finalStream.getStreamData(qpdf_dl_generalized); + if (finalData && finalData->getSize() > 0 && + (finalData->getSize() != data->getSize() || + std::memcmp(finalData->getBuffer(), data->getBuffer(), finalData->getSize()) != + 0)) { + cidSetOwned.assign(reinterpret_cast(finalData->getBuffer()), + finalData->getSize()); + if (FT_Face reloaded = loadFontFace(ftLib, cidSetOwned, 0)) { + cidSetGuard.reset(reloaded); + cidSetFace = reloaded; + } + } + } catch (...) { + } } + writeCidSetIfDifferent(pdf, descriptor, + cidsPresentInProgram(cidSetFace, cidToGid, cidIdentity), state); } // Rebuild /CharSet for Type 1/CFF subsets when every retained glyph has a PostScript name. diff --git a/native/bridge/src/jpdfium_pattern.cpp b/native/bridge/src/jpdfium_pattern.cpp index fab07a159..5ba0ff9dc 100644 --- a/native/bridge/src/jpdfium_pattern.cpp +++ b/native/bridge/src/jpdfium_pattern.cpp @@ -11,6 +11,49 @@ #include "jpdfium.h" #include "jpdfium_internal.h" +namespace { + +void appendJsonString(std::string& out, const char* data, size_t len) { + out.push_back('"'); + for (size_t i = 0; i < len; ++i) { + auto c = static_cast(data[i]); + switch (c) { + case '"': + out += "\\\""; + break; + case '\\': + out += "\\\\"; + break; + case '\b': + out += "\\b"; + break; + case '\f': + out += "\\f"; + break; + case '\n': + out += "\\n"; + break; + case '\r': + out += "\\r"; + break; + case '\t': + out += "\\t"; + break; + default: + if (c < 0x20) { + char u[7]; + std::snprintf(u, sizeof(u), "\\u%04x", static_cast(c)); + out += u; + } else { + out.push_back(static_cast(c)); + } + } + } + out.push_back('"'); +} + +} // namespace + // PCRE2 JIT Pattern Engine #ifdef JPDFIUM_HAS_PCRE2 @@ -60,6 +103,7 @@ int32_t jpdfium_pcre2_compile(const char* pattern, uint32_t flags, int64_t* hand int32_t jpdfium_pcre2_match_all(int64_t pattern_handle, const char* text, char** json_result) { try { if (!text || !json_result) return JPDFIUM_ERR_INVALID; + *json_result = nullptr; auto* pw = reinterpret_cast(pattern_handle); if (!pw || !Pcre2HandleRegistry::instance().contains(pw)) return JPDFIUM_ERR_INVALID; if (!pw->code) return JPDFIUM_ERR_INVALID; @@ -81,40 +125,21 @@ int32_t jpdfium_pcre2_match_all(int64_t pattern_handle, const char* text, char** if (!first) json += ','; first = false; - std::string match_text(text + start, end - start); - std::string escaped; - for (char c : match_text) { - switch (c) { - case '"': - escaped += "\\\""; - break; - case '\\': - escaped += "\\\\"; - break; - case '\n': - escaped += "\\n"; - break; - case '\r': - escaped += "\\r"; - break; - case '\t': - escaped += "\\t"; - break; - default: - escaped += c; - } - } - - char buf[256]; - snprintf(buf, sizeof(buf), "{\"start\":%zu,\"end\":%zu,\"match\":\"%s\"}", - static_cast(start), static_cast(end), escaped.c_str()); - json += buf; + json += "{\"start\":"; + json += std::to_string(static_cast(start)); + json += ",\"end\":"; + json += std::to_string(static_cast(end)); + json += ",\"match\":"; + appendJsonString(json, text + start, static_cast(end - start)); + json.push_back('}'); offset = (end > start) ? end : end + 1; } json += ']'; - *json_result = strdup(json.c_str()); + char* dup = strdup(json.c_str()); + if (!dup) return JPDFIUM_ERR_NATIVE; + *json_result = dup; return JPDFIUM_OK; } catch (...) { @@ -250,11 +275,15 @@ struct FlashTextProcessor { if (found) { if (!first) result += ','; first = false; - char buf[512]; - snprintf(buf, sizeof(buf), - "{\"start\":%zu,\"end\":%zu,\"keyword\":\"%s\",\"label\":\"%s\"}", i, - matchEnd, matchKeyword.c_str(), matchLabel.c_str()); - result += buf; + result += "{\"start\":"; + result += std::to_string(i); + result += ",\"end\":"; + result += std::to_string(matchEnd); + result += ",\"keyword\":"; + appendJsonString(result, matchKeyword.data(), matchKeyword.size()); + result += ",\"label\":"; + appendJsonString(result, matchLabel.data(), matchLabel.size()); + result.push_back('}'); i = matchEnd; } else { ++i; @@ -301,25 +330,63 @@ int32_t jpdfium_flashtext_add_keywords_json(int64_t handle, const char* json) { if (!json) return JPDFIUM_ERR_INVALID; // Simple JSON parser for: [{"keyword":"...","label":"..."}, ...] + // Scope each object strictly inside '{' .. '}' so optional labels never + // bleed across adjacent array elements, and allow whitespace after ':'. + auto findJsonStr = [](const char* from, const char* limit, const char* key, + std::string& value, const char** nextOut) -> bool { + const char* kpos = strstr(from, key); + if (!kpos || (limit && kpos >= limit)) return false; + const char* p = kpos + strlen(key); + while (p && (!limit || p < limit) && + (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')) { + ++p; + } + if (!p || (limit && p >= limit) || *p != ':') return false; + ++p; + while (p && (!limit || p < limit) && + (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')) { + ++p; + } + if (!p || (limit && p >= limit) || *p != '"') return false; + ++p; + value.clear(); + while (*p && (!limit || p < limit) && *p != '"') { + if (*p == '\\' && *(p + 1) && (!limit || p + 1 < limit)) { + ++p; + } + value.push_back(*p++); + } + if (*p != '"') return false; + if (nextOut) *nextOut = p + 1; + return true; + }; + const char* p = json; while (*p) { - const char* kw_start = strstr(p, "\"keyword\":\""); - if (!kw_start) break; - kw_start += 11; - const char* kw_end = strchr(kw_start, '"'); - if (!kw_end) break; - std::string keyword(kw_start, static_cast(kw_end - kw_start)); + const char* objOpen = strchr(p, '{'); + if (!objOpen) break; + // Find the matching closing brace outside string literals. + const char* objEnd = objOpen + 1; + bool inStr = false; + while (*objEnd) { + if (*objEnd == '\\' && inStr && *(objEnd + 1)) { + objEnd += 2; + continue; + } + if (*objEnd == '"') inStr = !inStr; + if (!inStr && *objEnd == '}') break; + ++objEnd; + } + if (*objEnd != '}') break; + std::string keyword; std::string label; - const char* lb_start = strstr(kw_end, "\"label\":\""); - if (lb_start) { - lb_start += 9; - const char* lb_end = strchr(lb_start, '"'); - if (lb_end) label.assign(lb_start, static_cast(lb_end - lb_start)); + const char* afterKw = nullptr; + if (findJsonStr(objOpen, objEnd, "\"keyword\"", keyword, &afterKw)) { + findJsonStr(objOpen, objEnd, "\"label\"", label, nullptr); + ft->addKeyword(keyword.c_str(), label.c_str()); } - - ft->addKeyword(keyword.c_str(), label.c_str()); - p = kw_end + 1; + p = objEnd + 1; } return JPDFIUM_OK; @@ -333,8 +400,11 @@ int32_t jpdfium_flashtext_find(int64_t handle, const char* text, char** json_res auto* ft = reinterpret_cast(handle); if (!ft || !FlashTextHandleRegistry::instance().contains(ft)) return JPDFIUM_ERR_INVALID; if (!json_result) return JPDFIUM_ERR_INVALID; + *json_result = nullptr; std::string result = ft->findAll(text); - *json_result = strdup(result.c_str()); + char* dup = strdup(result.c_str()); + if (!dup) return JPDFIUM_ERR_NATIVE; + *json_result = dup; return JPDFIUM_OK; } catch (...) { diff --git a/native/bridge/src/jpdfium_pdfx.cpp b/native/bridge/src/jpdfium_pdfx.cpp index d5b37ec5f..92f67c90c 100644 --- a/native/bridge/src/jpdfium_pdfx.cpp +++ b/native/bridge/src/jpdfium_pdfx.cpp @@ -213,6 +213,9 @@ class Converter { int64_t height, bool as_jpeg); std::shared_ptr DecodeImage(QPDFObjectHandle image, bool was_dct); void ConvertGroup(QPDFObjectHandle holder); + void ConvertShading(QPDFObjectHandle shading); + bool AuditAnnotationEntry(QPDFObjectHandle entry, std::set* seen, std::string* why, + int depth); bool AuditResources(QPDFObjectHandle resources, std::set* seen, std::string* why, int depth); void RemoveTransparencyGroups(QPDFObjectHandle resources, std::set* seen, @@ -223,6 +226,7 @@ class Converter { std::map cs_memo_; std::set done_streams_; std::set done_images_; + std::set done_shadings_; std::set done_functions_; std::set done_resources_; std::vector done_direct_resources_; @@ -309,8 +313,13 @@ ColourSpaceInfo ClassifyDeviceIndependent(const std::string& family, QPDFObjectH std::string IndexedLookupBytes(QPDFObjectHandle lookup) { if (lookup.isString()) return lookup.getStringValue(); if (!lookup.isStream()) return {}; - std::shared_ptr b = lookup.getStreamData(qpdf_dl_all); - return std::string(reinterpret_cast(b->getBuffer()), b->getSize()); + try { + std::shared_ptr b = lookup.getStreamData(qpdf_dl_all); + if (!b || b->getSize() == 0) return {}; + return std::string(reinterpret_cast(b->getBuffer()), b->getSize()); + } catch (std::exception&) { + return {}; + } } ColourSpaceInfo Converter::ClassifyIndexed(QPDFObjectHandle cs, int depth) { @@ -356,6 +365,25 @@ ColourSpaceInfo Converter::ClassifyIndexed(QPDFObjectHandle cs, int depth) { ColourSpaceInfo Converter::ClassifySpecial(const std::string& family, QPDFObjectHandle cs, int depth) { ColourSpaceInfo info; + if (family == "/DeviceN" && cs.getArrayNItems() >= 5 && cs.getArrayItem(4).isDictionary()) { + QPDFObjectHandle attrs = cs.getArrayItem(4); + QPDFObjectHandle colorants = attrs.getKey("/Colorants"); + if (colorants.isDictionary()) { + for (const std::string& key : colorants.getKeys()) { + ColourSpaceInfo cinfo = Classify(colorants.getKey(key), depth + 1); + if (cinfo.replacement.isInitialized() && !cinfo.replacement.isNull()) { + colorants.replaceKey(key, cinfo.replacement); + } + } + } + QPDFObjectHandle proc = attrs.getKey("/Process"); + if (proc.isDictionary() && proc.hasKey("/ColorSpace")) { + ColourSpaceInfo pinfo = Classify(proc.getKey("/ColorSpace"), depth + 1); + if (pinfo.replacement.isInitialized() && !pinfo.replacement.isNull()) { + proc.replaceKey("/ColorSpace", pinfo.replacement); + } + } + } const int alt_index = 2; ColourSpaceInfo alt = Classify(cs.getArrayItem(alt_index), depth + 1); if (alt.kind == Kind::Keep) return info; @@ -545,6 +573,17 @@ bool Converter::ConvertFunction(QPDFObjectHandle fn, int inputs, Kind kind, } ColourSpaceInfo cs = base; cs.kind = kind; + bool validEndpoints = true; + for (QPDFObjectHandle* endpoint : {&c0, &c1}) { + for (int i = 0; i < 3; ++i) { + if (!endpoint->getArrayItem(i).isNumber()) { + validEndpoints = false; + break; + } + } + if (!validEndpoints) break; + } + if (!validEndpoints) break; for (QPDFObjectHandle* endpoint : {&c0, &c1}) { std::vector in; for (int i = 0; i < 3; ++i) @@ -607,6 +646,12 @@ Converter::Resources Converter::PrepareResources(QPDFObjectHandle resources) { } res.spaces[key] = info; } + // Never leave a /DeviceCMYK replacement bound under /DefaultRGB (which must + // be 3-component per ISO 32000-1 §8.6.5.6) after converting RGB colors. + if (spaces.hasKey("/DefaultRGB") && + (ForbidsDeviceIndependent(o_.level) || o_.convert_colours)) { + spaces.removeKey("/DefaultRGB"); + } if (spaces.isIndirect()) { spaces_memo_[spaces.getObjGen()] = res.spaces; } else { @@ -813,6 +858,7 @@ std::string ExpandInlineName(const std::string& n) { void ColourFilter::FlushInline() { using T = QPDFTokenizer; + // Inline images bundle abbreviated keys between BI..ID..EI; normalize before inspecting color. in_inline_ = false; std::vector tokens; tokens.swap(inline_); @@ -849,17 +895,34 @@ void ColourFilter::FlushInline() { QPDFObjectHandle cs = dict.getKey("/ColorSpace"); ColourSpaceInfo info = cs.isName() ? Resolve(cs.getName()) : ColourSpaceInfo{}; if (cs.isName() && (cs.getName() == "/DeviceRGB")) info.kind = Kind::Rgb; - convert = info.kind == Kind::Rgb && !dict.getKey("/ImageMask").isBool() && + bool is_mask = + dict.getKey("/ImageMask").isBool() && dict.getKey("/ImageMask").getBoolValue(); + bool is_rgb_image = info.kind == Kind::Rgb && !is_mask; + convert = is_rgb_image && owner_.o_.convert_images && dict.getKey("/BitsPerComponent").isInteger() && dict.getKey("/BitsPerComponent").getIntValue() == 8; QPDFObjectHandle filter = dict.getKey("/Filter"); - if (filter.isName() && filter.getName() == "/DCTDecode") convert = false; + // Skip DCT-encoded inline images whose JPEG payload cannot be losslessly re-encoded as + // CMYK without user transcoding flags. + bool has_dct = filter.isName() && filter.getName() == "/DCTDecode"; + if (!has_dct && filter.isArray()) { + for (int fi = 0; fi < filter.getArrayNItems(); ++fi) { + if (Name(filter.getArrayItem(fi)) == "/DCTDecode") { + has_dct = true; + break; + } + } + } + if (has_dct) convert = false; + if (is_rgb_image && !convert) owner_.images_skipped_++; } catch (std::exception&) { convert = false; } } if (convert) { try { + // Strip QPDF tokenizer trailing whitespace before re-wrapping raw bytes in a temp + // stream for CMYK conversion. std::string bytes = data->getValue(); while (!bytes.empty() && (bytes.back() == ' ' || bytes.back() == '\n' || bytes.back() == '\r')) { @@ -934,6 +997,49 @@ void Converter::ConvertGroup(QPDFObjectHandle holder) { } } +void Converter::ConvertShading(QPDFObjectHandle shading) { + if (!shading.isDictionary() && !shading.isStream()) return; + if (shading.isIndirect() && !done_shadings_.insert(shading.getObjGen()).second) return; + QPDFObjectHandle dict = shading.isStream() ? shading.getDict() : shading; + QPDFObjectHandle type_obj = dict.getKey("/ShadingType"); + const int shading_type = type_obj.isInteger() ? type_obj.getIntValueAsInt() : 0; + ColourSpaceInfo cs = Classify(dict.getKey("/ColorSpace")); + if (cs.kind == Kind::Keep) { + if (cs.replacement.isInitialized() && !cs.replacement.isNull()) { + dict.replaceKey("/ColorSpace", cs.replacement); + } + return; + } + if (cs.kind == Kind::AsGray || cs.kind == Kind::AsCmyk) { + dict.replaceKey("/ColorSpace", cs.replacement); + spaces_++; + return; + } + QPDFObjectHandle fn = dict.getKey("/Function"); + const int inputs = shading_type == 1 ? 2 : 1; + if (fn.isNull() || !ConvertFunction(fn, inputs, cs.kind, cs)) return; + QPDFObjectHandle bg = dict.getKey("/Background"); + if (bg.isArray() && bg.getArrayNItems() == 3) { + bool valid_bg = true; + std::vector in; + for (int i = 0; i < 3; ++i) { + if (!bg.getArrayItem(i).isNumber()) { + valid_bg = false; + break; + } + in.push_back(bg.getArrayItem(i).getNumericValue()); + } + if (valid_bg) { + std::vector cmyk = ConvertComponents(cs, in, false); + QPDFObjectHandle arr = QPDFObjectHandle::newArray(); + for (double v : cmyk) arr.appendItem(QPDFObjectHandle::newReal(v, 4)); + dict.replaceKey("/Background", arr); + } + } + dict.replaceKey("/ColorSpace", cs.replacement); + spaces_++; +} + void Converter::ConvertImage(QPDFObjectHandle image) { if (!image.isStream()) return; if (image.isIndirect() && !done_images_.insert(image.getObjGen()).second) return; @@ -1170,8 +1276,11 @@ QPDFObjectHandle Converter::ColourKeyToStencil(const Raster& raster, QPDFObjectH int64_t lo[3]; int64_t hi[3]; for (int c = 0; c < 3; ++c) { - lo[c] = mask.getArrayItem(c * 2).getIntValue(); - hi[c] = mask.getArrayItem(c * 2 + 1).getIntValue(); + QPDFObjectHandle lo_obj = mask.getArrayItem(c * 2); + QPDFObjectHandle hi_obj = mask.getArrayItem(c * 2 + 1); + if (!lo_obj.isInteger() || !hi_obj.isInteger()) return {}; + lo[c] = lo_obj.getIntValue(); + hi[c] = hi_obj.getIntValue(); } const int64_t mask_row = (raster.width + 7) / 8; std::string bits(static_cast(mask_row * raster.height), '\0'); @@ -1193,6 +1302,7 @@ QPDFObjectHandle Converter::ColourKeyToStencil(const Raster& raster, QPDFObjectH sd.replaceKey("/Subtype", QPDFObjectHandle::newName("/Image")); sd.replaceKey("/Width", QPDFObjectHandle::newInteger(raster.width)); sd.replaceKey("/Height", QPDFObjectHandle::newInteger(raster.height)); + sd.replaceKey("/BitsPerComponent", QPDFObjectHandle::newInteger(1)); sd.replaceKey("/ImageMask", QPDFObjectHandle::newBool(true)); return stencil; } @@ -1202,7 +1312,10 @@ void Converter::ConvertMatte(QPDFObjectHandle smask, const ColourSpaceInfo& cs) QPDFObjectHandle matte = smask.getDict().getKey("/Matte"); if (!matte.isArray() || matte.getArrayNItems() != 3) return; std::vector mv; - for (int i = 0; i < 3; ++i) mv.push_back(matte.getArrayItem(i).getNumericValue()); + for (int i = 0; i < 3; ++i) { + if (!matte.getArrayItem(i).isNumber()) return; + mv.push_back(matte.getArrayItem(i).getNumericValue()); + } QPDFObjectHandle arr = QPDFObjectHandle::newArray(); for (double v : ConvertComponents(cs, mv, false)) arr.appendItem(QPDFObjectHandle::newReal(v, 4)); @@ -1271,13 +1384,18 @@ bool Converter::ConvertImagePixels(QPDFObjectHandle image, const ColourSpaceInfo QPDFObjectHandle mask = dict.getKey("/Mask"); if (mask.isArray() && mask.getArrayNItems() != 6) return false; std::vector cmyk = RasterToCmyk(raster, cs); - if (mask.isArray()) dict.replaceKey("/Mask", ColourKeyToStencil(raster, mask)); + if (mask.isArray()) { + QPDFObjectHandle stencil = ColourKeyToStencil(raster, mask); + if (!stencil.isInitialized()) return false; + dict.replaceKey("/Mask", stencil); + } ConvertMatte(dict.getKey("/SMask"), cs); StoreCmyk(image, cmyk, raster.width, raster.height, was_dct); return true; } void Converter::ProcessResources(Resources& res, int depth) { + // Guard against cyclic /Resources graphs; ISO 32000 nesting rarely exceeds a handful of levels. if (!res.dict.isDictionary() || depth > 32) return; if (res.dict.isIndirect()) { if (!done_resources_.insert(res.dict.getObjGen()).second) return; @@ -1293,6 +1411,8 @@ void Converter::ProcessResources(Resources& res, int depth) { QPDFObjectHandle x = xobjects.getKey(key); if (!x.isStream()) continue; const std::string subtype = Name(x.getDict().getKey("/Subtype")); + // PostScript XObjects (/PS) and /OPI proxies are forbidden in PDF/X; replace with empty + // zero-BBox form placeholders. if (subtype == "/PS") { QPDFObjectHandle empty = QPDFObjectHandle::newStream(&pdf_, ""); QPDFObjectHandle ed = empty.getDict(); @@ -1305,6 +1425,7 @@ void Converter::ProcessResources(Resources& res, int depth) { continue; } x.getDict().removeKey("/PS"); + x.getDict().removeKey("/OPI"); if (subtype == "/Image") { ConvertImage(x); } else if (subtype == "/Form") { @@ -1312,16 +1433,32 @@ void Converter::ProcessResources(Resources& res, int depth) { } } } + QPDFObjectHandle shadings = res.dict.getKey("/Shading"); + if (shadings.isDictionary()) { + for (const std::string& key : shadings.getKeys()) { + ConvertShading(shadings.getKey(key)); + } + } QPDFObjectHandle patterns = res.dict.getKey("/Pattern"); if (patterns.isDictionary()) { + // Clamp pattern /XStep / /YStep and sanitize Type 2 shading dictionaries recursively. for (const std::string& key : patterns.getKeys()) { QPDFObjectHandle pat = patterns.getKey(key); - ClampPatternSteps(pat); + QPDFObjectHandle pat_dict = pat.isStream() ? pat.getDict() : pat; + if (pat_dict.isDictionary()) { + ClampPatternSteps(pat); + if (pat_dict.getKey("/PatternType").isInteger() && + pat_dict.getKey("/PatternType").getIntValueAsInt() == 2) { + ConvertShading(pat_dict.getKey("/Shading")); + } + } if (pat.isStream()) ProcessContentHolder(pat, res.dict, depth + 1); } } QPDFObjectHandle fonts = res.dict.getKey("/Font"); if (fonts.isDictionary()) { + // Rewrite Type 3 /CharProcs content streams too, since their strokes inherit graphics + // color. for (const std::string& key : fonts.getKeys()) { QPDFObjectHandle font = fonts.getKey(key); if (!font.isDictionary() || Name(font.getKey("/Subtype")) != "/Type3") continue; @@ -1335,7 +1472,13 @@ void Converter::ProcessResources(Resources& res, int depth) { QPDFObjectHandle proc = procs.getKey(glyph); if (!proc.isStream()) continue; if (proc.isIndirect() && !done_streams_.insert(proc.getObjGen()).second) continue; - std::shared_ptr data = proc.getStreamData(qpdf_dl_all); + std::shared_ptr data; + try { + data = proc.getStreamData(qpdf_dl_all); + } catch (std::exception&) { + continue; + } + if (!data) continue; std::string content(reinterpret_cast(data->getBuffer()), data->getSize()); bool changed = false; @@ -1351,6 +1494,9 @@ void Converter::ProcessResources(Resources& res, int depth) { for (const std::string& key : states.getKeys()) { QPDFObjectHandle gs = states.getKey(key); if (!gs.isDictionary()) continue; + gs.removeKey("/TR"); + QPDFObjectHandle tr2 = gs.getKey("/TR2"); + if (!tr2.isNull() && Name(tr2) != "/Default") gs.removeKey("/TR2"); QPDFObjectHandle smask = gs.getKey("/SMask"); if (!smask.isDictionary()) continue; QPDFObjectHandle group = smask.getKey("/G"); @@ -1361,12 +1507,21 @@ void Converter::ProcessResources(Resources& res, int depth) { gdict.isDictionary() ? Classify(gdict.getKey("/CS")) : ColourSpaceInfo{}; if (bc.isArray() && bc.getArrayNItems() == 3 && (gcs.kind == Kind::Rgb || gcs.kind == Kind::Lab)) { + bool valid_bc = true; std::vector v; - for (int i = 0; i < 3; ++i) v.push_back(bc.getArrayItem(i).getNumericValue()); - std::vector c = ConvertComponents(gcs, v, false); - QPDFObjectHandle arr = QPDFObjectHandle::newArray(); - for (double x : c) arr.appendItem(QPDFObjectHandle::newReal(x, 4)); - smask.replaceKey("/BC", arr); + for (int i = 0; i < 3; ++i) { + if (!bc.getArrayItem(i).isNumber()) { + valid_bc = false; + break; + } + v.push_back(bc.getArrayItem(i).getNumericValue()); + } + if (valid_bc) { + std::vector c = ConvertComponents(gcs, v, false); + QPDFObjectHandle arr = QPDFObjectHandle::newArray(); + for (double x : c) arr.appendItem(QPDFObjectHandle::newReal(x, 4)); + smask.replaceKey("/BC", arr); + } } ProcessContentHolder(group, res.dict, depth + 1); } @@ -1388,6 +1543,7 @@ void Converter::ProcessContentHolder(QPDFObjectHandle stream, QPDFObjectHandle i } catch (std::exception&) { return; } + if (!data) return; std::string content(reinterpret_cast(data->getBuffer()), data->getSize()); bool changed = false; bool sets_font = true; @@ -1440,6 +1596,7 @@ void Converter::Run() { bool Converter::AuditResources(QPDFObjectHandle resources, std::set* seen, std::string* why, int depth) { + // Reject transparency, non-Normal blend modes, and PDF 1.4+ filters forbidden in PDF/X-1a/3. if (!resources.isDictionary() || depth > 32) return false; if (resources.isIndirect() && !seen->insert(resources.getObjGen()).second) return false; QPDFObjectHandle states = resources.getKey("/ExtGState"); @@ -1475,6 +1632,8 @@ bool Converter::AuditResources(QPDFObjectHandle resources, std::set* QPDFObjectHandle x = xobjects.getKey(key); if (!x.isStream()) continue; QPDFObjectHandle d = x.getDict(); + // JBIG2Decode / JPXDecode and image /SMask /SMaskInData require PDF 1.4+ + // transparency/filters. if (Name(d.getKey("/Subtype")) == "/Image") { QPDFObjectHandle filter = d.getKey("/Filter"); std::vector filters; @@ -1505,6 +1664,7 @@ bool Converter::AuditResources(QPDFObjectHandle resources, std::set* } QPDFObjectHandle patterns = resources.getKey("/Pattern"); if (patterns.isDictionary()) { + // Recurse into tiling pattern resource trees and local ExtGState dictionaries. for (const std::string& key : patterns.getKeys()) { QPDFObjectHandle pat = patterns.getKey(key); QPDFObjectHandle d = pat.isStream() ? pat.getDict() : pat; @@ -1551,6 +1711,20 @@ void Converter::RemoveTransparencyGroups(QPDFObjectHandle resources, std::set* seen, + std::string* why, int depth) { + if (depth > 8) return false; + if (entry.isStream()) { + return AuditResources(entry.getDict().getKey("/Resources"), seen, why, depth + 1); + } + if (entry.isDictionary()) { + for (const std::string& state : entry.getKeys()) { + if (AuditAnnotationEntry(entry.getKey(state), seen, why, depth + 1)) return true; + } + } + return false; +} + void Converter::AuditTransparency() { QPDFPageDocumentHelper pages(pdf_); int index = 0; @@ -1571,10 +1745,13 @@ void Converter::AuditTransparency() { why = "a semi-transparent annotation"; } QPDFObjectHandle ap = annot.getKey("/AP"); - QPDFObjectHandle normal = ap.isDictionary() ? ap.getKey("/N") : QPDFObjectHandle(); - if (!transparent && normal.isStream() && - AuditResources(normal.getDict().getKey("/Resources"), &seen, &why, 1)) { - transparent = true; + if (!transparent && ap.isDictionary()) { + for (const char* sub : {"/N", "/R", "/D"}) { + if (AuditAnnotationEntry(ap.getKey(sub), &seen, &why, 0)) { + transparent = true; + break; + } + } } } } @@ -1597,21 +1774,49 @@ std::string Converter::Report() const { " spaces=" + std::to_string(spaces_); } -bool IsJavaScriptAction(QPDFObjectHandle action) { - return action.isDictionary() && Name(action.getKey("/S")) == "/JavaScript"; +bool IsForbiddenAction(QPDFObjectHandle action, int depth = 0) { + if (depth > 8) return false; + if (action.isArray()) { + for (int i = 0; i < action.getArrayNItems(); ++i) { + if (IsForbiddenAction(action.getArrayItem(i), depth + 1)) return true; + } + return false; + } + if (!action.isDictionary()) return false; + const std::string s = Name(action.getKey("/S")); + if (s == "/JavaScript" || s == "/Launch" || s == "/Sound" || s == "/Movie" || + s == "/ResetForm" || s == "/ImportData" || s == "/Rendition" || s == "/Trans" || + s == "/GoTo3DView") { + return true; + } + return IsForbiddenAction(action.getKey("/Next"), depth + 1); } -void StripActions(QPDFObjectHandle dict) { - if (!dict.isDictionary()) return; +void StripActions(QPDFObjectHandle dict, int depth = 0) { + if (!dict.isDictionary() || depth > 8) return; dict.removeKey("/AA"); - if (IsJavaScriptAction(dict.getKey("/A"))) dict.removeKey("/A"); + if (IsForbiddenAction(dict.getKey("/A"))) dict.removeKey("/A"); + QPDFObjectHandle kids = dict.getKey("/Kids"); + if (kids.isArray()) { + for (int i = 0; i < kids.getArrayNItems(); ++i) { + StripActions(kids.getArrayItem(i), depth + 1); + } + } } -void RemoveJavaScript(QPDF& pdf) { +void RemoveForbiddenActionsAndFiles(QPDF& pdf, Level level) { QPDFObjectHandle root = pdf.getRoot(); QPDFObjectHandle names = root.getKey("/Names"); - if (names.isDictionary()) names.removeKey("/JavaScript"); - if (IsJavaScriptAction(root.getKey("/OpenAction"))) root.removeKey("/OpenAction"); + if (names.isDictionary()) { + names.removeKey("/JavaScript"); + if (level == Level::X1a || level == Level::X3 || level == Level::X4) { + names.removeKey("/EmbeddedFiles"); + } + } + if (level == Level::X1a || level == Level::X3 || level == Level::X4) { + root.removeKey("/AF"); + } + if (IsForbiddenAction(root.getKey("/OpenAction"))) root.removeKey("/OpenAction"); root.removeKey("/AA"); QPDFPageDocumentHelper pages(pdf); for (QPDFPageObjectHelper& page : pages.getAllPages()) { @@ -1677,6 +1882,54 @@ void FixPageBoxes(QPDF& pdf) { for (QPDFPageObjectHelper& page : pages.getAllPages()) { QPDFObjectHandle dict = page.getObjectHandle(); if (dict.hasKey("/TrimBox") && dict.hasKey("/ArtBox")) dict.removeKey("/ArtBox"); + QPDFObjectHandle media = page.getAttribute("/MediaBox", true); + if (!media.isArray() || media.getArrayNItems() != 4) continue; + double mx[4]{}; + bool valid_media = true; + for (int i = 0; i < 4; ++i) { + if (!media.getArrayItem(i).isNumber()) { + valid_media = false; + break; + } + mx[i] = media.getArrayItem(i).getNumericValue(); + } + if (!valid_media) continue; + const double mlx = std::min(mx[0], mx[2]); + const double mly = std::min(mx[1], mx[3]); + const double mux = std::max(mx[0], mx[2]); + const double muy = std::max(mx[1], mx[3]); + for (const char* key : {"/CropBox", "/BleedBox", "/TrimBox", "/ArtBox"}) { + QPDFObjectHandle box = dict.getKey(key); + if (!box.isArray() || box.getArrayNItems() != 4) continue; + double bx[4]{}; + bool valid_box = true; + for (int i = 0; i < 4; ++i) { + if (!box.getArrayItem(i).isNumber()) { + valid_box = false; + break; + } + bx[i] = box.getArrayItem(i).getNumericValue(); + } + if (!valid_box) continue; + double nx0 = std::clamp(std::min(bx[0], bx[2]), mlx, mux); + double ny0 = std::clamp(std::min(bx[1], bx[3]), mly, muy); + double nx1 = std::clamp(std::max(bx[0], bx[2]), mlx, mux); + double ny1 = std::clamp(std::max(bx[1], bx[3]), mly, muy); + if (nx1 <= nx0 || ny1 <= ny0) { + nx0 = mlx; + ny0 = mly; + nx1 = mux; + ny1 = muy; + } + if (std::fabs(nx0 - bx[0]) > 1e-6 || std::fabs(ny0 - bx[1]) > 1e-6 || + std::fabs(nx1 - bx[2]) > 1e-6 || std::fabs(ny1 - bx[3]) > 1e-6) { + QPDFObjectHandle clamped = QPDFObjectHandle::newArray(); + for (double v : {nx0, ny0, nx1, ny1}) { + clamped.appendItem(QPDFObjectHandle::newReal(v, 4)); + } + dict.replaceKey(key, clamped); + } + } } } @@ -1696,7 +1949,7 @@ Level ParseLevel(int32_t level) { } void Normalise(QPDF& pdf, const Options& options) { - RemoveJavaScript(pdf); + RemoveForbiddenActionsAndFiles(pdf, options.level); FlattenPrintableAnnotations(pdf); if (ForbidsTransparency(options.level)) RemoveOptionalContent(pdf); FixPageBoxes(pdf); @@ -1710,11 +1963,13 @@ void Normalise(QPDF& pdf, const Options& options) { if (ForbidsTransparency(options.level)) converter.AuditTransparency(); } -void ConfigureWriter(QPDFWriter& writer) { +void ConfigureWriter(QPDFWriter& writer, const Options& options) { writer.setPreserveEncryption(false); writer.setDecodeLevel(qpdf_dl_none); writer.setCompressStreams(true); - writer.setObjectStreamMode(qpdf_o_preserve); + writer.setObjectStreamMode((options.level == Level::X1a || options.level == Level::X3) + ? qpdf_o_disable + : qpdf_o_preserve); } void CopyMessage(const std::string& text, char* message, int32_t cap) { @@ -1773,7 +2028,7 @@ JPDFIUM_EXPORT int32_t jpdfium_pdfx_prepare_bytes(const uint8_t* input, int64_t static_cast(input_len)); Normalise(*pdf, options); QPDFWriter writer{*pdf}; - ConfigureWriter(writer); + ConfigureWriter(writer, options); writer.setOutputMemory(); { jpdfium_qpdf_internal::WriterLock lock{-1}; @@ -1830,7 +2085,7 @@ JPDFIUM_EXPORT int32_t jpdfium_pdfx_prepare_file(const char* in_path, const char out_path, [&](jpdfium_staging::StagedFile& staged) { QPDFWriter writer{*pdf}; - ConfigureWriter(writer); + ConfigureWriter(writer, options); writer.setOutputFile(staged.path.c_str(), staged.file, false); jpdfium_qpdf_internal::WriterLock lock{-1}; writer.write(); diff --git a/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp b/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp index ae5337153..92d14c840 100644 --- a/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp +++ b/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp @@ -736,23 +736,21 @@ void populateEmbeddedFontProgram(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& f if (cid) { std::set activeCids; - if (!glyphs.empty()) { - std::string mapView; - auto map = target.getKey("/CIDToGIDMap"); - if (map.isStream()) { - try { - auto data = map.getStreamData(qpdf_dl_generalized); - if (data) { - mapView.assign(reinterpret_cast(data->getBuffer()), - data->getSize()); - } - } catch (...) { - mapView.clear(); - } + FT_Face activeFace = program.face; + jpdfium::font::FtFaceGuard subGuard; + if (!subset.empty()) { + FT_Face subFace = nullptr; + if (FT_New_Memory_Face(lib, reinterpret_cast(subset.data()), + FT_Long(subset.size()), 0, &subFace) == 0 && + subFace) { + subGuard.reset(subFace); + activeFace = subFace; + } else { + activeFace = nullptr; } - activeCids = cidsForRetainedGlyphs(target, mapView, glyphs); - } else { - activeCids = cidsPresentInProgram(target, program.face); + } + if (activeFace) { + activeCids = cidsPresentInProgram(target, activeFace); } if (activeCids.empty()) { activeCids.insert(0); @@ -1000,6 +998,7 @@ int32_t embedFontsExport(const uint8_t* input, int64_t inputLen, int32_t count, std::fprintf(stderr, "jpdfium qpdf embed fonts: %s\n", result.error.c_str()); return result.rc; } + if (!result.buffer || result.buffer->getSize() == 0) return -1; *outputLen = static_cast(result.buffer->getSize()); *output = static_cast(malloc(static_cast(*outputLen))); if (!*output) { diff --git a/native/bridge/src/jpdfium_qpdf_fonts_subset.cpp b/native/bridge/src/jpdfium_qpdf_fonts_subset.cpp index 2a4e42e44..0843de6b5 100644 --- a/native/bridge/src/jpdfium_qpdf_fonts_subset.cpp +++ b/native/bridge/src/jpdfium_qpdf_fonts_subset.cpp @@ -29,28 +29,6 @@ void updateCidSet(QPDF& qpdf, QPDFObjectHandle descriptor, std::set co if (!bits.empty()) descriptor.replaceKey("/CIDSet", QPDFObjectHandle::newStream(&qpdf, bits)); } -std::set cidsForRetainedGlyphs(QPDFObjectHandle const& target, - std::string const& mapBytes, - std::set const& glyphs) { - std::set cids{0}; - auto map = target.getKey("/CIDToGIDMap"); - if (map.isStream() && !mapBytes.empty()) { - const auto* bytes = reinterpret_cast(mapBytes.data()); - const size_t entries = mapBytes.size() / 2; - for (size_t c = 0; c < entries && c <= jpdfium::font::kMaxCid; ++c) { - const uint32_t gid = (static_cast(bytes[2 * c]) << 8) | bytes[2 * c + 1]; - if (glyphs.contains(static_cast(gid))) cids.insert(static_cast(c)); - } - return cids; - } - if (map.isNull() || map.isNameAndEquals("/Identity")) { - for (FT_UInt gid : glyphs) { - if (gid <= jpdfium::font::kMaxCid) cids.insert(static_cast(gid)); - } - } - return cids; -} - std::set cidsPresentInProgram(QPDFObjectHandle const& target, FT_Face face) { std::set cids; auto map = target.getKey("/CIDToGIDMap"); @@ -337,21 +315,23 @@ bool subsetEmbeddedCidFont(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& font, descriptor.replaceKey(program.kind == ProgramKind::TrueType ? "/FontFile2" : "/FontFile3", makeProgramStream(qpdf, program, program.kind, true, chosenSubset)); - // Recompute /CIDSet when present so every bit matches the subset's retained CIDs. + // Recompute /CIDSet when present so every bit matches all CIDs present in the embedded + // subset program (ISO 19005-2 §6.2.11.4.2). if (descriptor.hasKey("/CIDSet")) { - if (useCompactedMap) { - std::set mappedCids; - for (uint32_t c = 0; c <= maxUsedCid; ++c) mappedCids.insert(c); - updateCidSet(qpdf, descriptor, mappedCids); + FT_Face subFace = nullptr; + jpdfium::font::FtFaceGuard subGuard; + std::set presentCids; + if (!chosenSubset.empty() && + FT_New_Memory_Face(lib, reinterpret_cast(chosenSubset.data()), + FT_Long(chosenSubset.size()), 0, &subFace) == 0 && + subFace) { + subGuard.reset(subFace); + presentCids = cidsPresentInProgram(target, subFace); + } + if (!presentCids.empty()) { + updateCidSet(qpdf, descriptor, presentCids); } else { - std::string mapView; - if (!identity && mapBytes) { - size_t mapSize = std::min( - mapBytes->getSize(), 2 * (static_cast(jpdfium::font::kMaxCid) + 1)); - mapView.assign(reinterpret_cast(mapBytes->getBuffer()), mapSize); - } - auto retainedCids = cidsForRetainedGlyphs(target, mapView, targetGlyphs); - if (!retainedCids.empty()) updateCidSet(qpdf, descriptor, retainedCids); + descriptor.removeKey("/CIDSet"); } } return true; diff --git a/native/bridge/src/jpdfium_qpdf_pages.cpp b/native/bridge/src/jpdfium_qpdf_pages.cpp index a415daabb..b1e02ac70 100644 --- a/native/bridge/src/jpdfium_qpdf_pages.cpp +++ b/native/bridge/src/jpdfium_qpdf_pages.cpp @@ -879,6 +879,32 @@ int setDocInfoFile(const char* in_path, const char* out_path, const char* const* } } +void configureForcedVersionWriter(QPDF& pdf, QPDFWriter& writer, const char* version) { + int major = 0; + int minor = 0; + if (std::sscanf(version, "%d.%d", &major, &minor) == 2 && + (major < 1 || (major == 1 && minor < 5))) { + // Object streams require PDF 1.5+ (ISO 32000-1 §7.5.7); keep <=1.4 valid. + writer.setObjectStreamMode(qpdf_o_disable); + try { + QPDFObjectHandle root = pdf.getRoot(); + if (root.hasKey("/Metadata")) { + QPDFObjectHandle meta = root.getKey("/Metadata"); + if (meta.isStream()) { + auto raw = meta.getStreamData(qpdf_dl_all); + if (raw) { + meta.replaceStreamData(raw, QPDFObjectHandle::newNull(), + QPDFObjectHandle::newNull()); + meta.setFilterOnWrite(false); + } + } + } + } catch (...) { + } + } + writer.forcePDFVersion(version); +} + int forceVersionFile(const char* in_path, const char* out_path, const char* version) { if (!in_path || !out_path || !version || !*version) return -1; try { @@ -893,7 +919,7 @@ int forceVersionFile(const char* in_path, const char* out_path, const char* vers QPDFWriter writer{*pdf}; writer.setOutputFile("jpdfium-out", out, true); writerOwnsFile = true; - writer.forcePDFVersion(version); + configureForcedVersionWriter(*pdf, writer, version); WriterLock lock{-1}; writer.write(); return 0; @@ -964,7 +990,7 @@ JPDFIUM_EXPORT int32_t jpdfium_qpdf_force_version(const uint8_t* input, int64_t pdf->getRoot().removeKey("/Version"); QPDFWriter writer{*pdf}; writer.setOutputMemory(); - writer.forcePDFVersion(version); + configureForcedVersionWriter(*pdf, writer, version); WriterLock lock{-1}; writer.write(); return writeBufferToC(writer.getBufferSharedPointer(), output, outputLen); diff --git a/native/bridge/src/jpdfium_qpdf_sanitize.cpp b/native/bridge/src/jpdfium_qpdf_sanitize.cpp index 04254db63..b68c2a26c 100644 --- a/native/bridge/src/jpdfium_qpdf_sanitize.cpp +++ b/native/bridge/src/jpdfium_qpdf_sanitize.cpp @@ -166,32 +166,29 @@ static int sanitizeFile(const char* in_path, const char* out_path, int32_t flags // Java-created staging: truncate is correct, but refuse a symlink // swapped in after creation (see createOutputFile in jpdfium_qpdf.cpp). - FILE* out = nullptr; + FILE* rawOut = nullptr; #ifdef _WIN32 - out = std::fopen(out_path, "wb"); + rawOut = std::fopen(out_path, "wb"); #else int fd = ::open(out_path, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600); if (fd >= 0) { - out = ::fdopen(fd, "wb"); - if (!out) ::close(fd); + rawOut = ::fdopen(fd, "wb"); + if (!rawOut) ::close(fd); } #endif + std::unique_ptr out(rawOut, &std::fclose); if (!out) return -1; - bool writerOwnsFile = false; try { QPDFWriter w(*qpdf); - w.setOutputFile("jpdfium-out", out, true); - writerOwnsFile = true; + w.setOutputFile("jpdfium-out", out.get(), false); WriterLock lock{-1}; w.write(); return 0; } catch (const std::exception& e) { std::fprintf(stderr, "jpdfium qpdf sanitize file: %s\n", e.what()); - if (!writerOwnsFile) std::fclose(out); return -1; } catch (...) { std::fprintf(stderr, "jpdfium qpdf sanitize file: unknown error\n"); - if (!writerOwnsFile) std::fclose(out); return -1; } } catch (const std::exception& e) { diff --git a/native/bridge/src/jpdfium_redact.cpp b/native/bridge/src/jpdfium_redact.cpp index 3bf5a2465..267399cdd 100644 --- a/native/bridge/src/jpdfium_redact.cpp +++ b/native/bridge/src/jpdfium_redact.cpp @@ -833,13 +833,16 @@ int32_t jpdfium_redact_words_ex(int64_t page, const char** words, int32_t wordCo scanPagePatterns(pw->core.get(), tp, wtext, idxMap, words, wordCount, wholeWord != 0, caseSensitive != 0, useRegex != 0, padding, matches, &compiledPatterns, rejectedPatterns, compiledCount); -#endif if (matchCount) *matchCount = logicalMatchCount(matches); - if (rejectedPatterns > 0 && compiledCount == 0) { FPDFText_ClosePage(tp); return JPDFIUM_ERR_INVALID; } +#else + (void)rejectedPatterns; + (void)compiledCount; + if (matchCount) *matchCount = logicalMatchCount(matches); +#endif if (matches.empty()) { FPDFText_ClosePage(tp); diff --git a/native/bridge/src/jpdfium_render.cpp b/native/bridge/src/jpdfium_render.cpp index 0d68e8f4a..4de119382 100644 --- a/native/bridge/src/jpdfium_render.cpp +++ b/native/bridge/src/jpdfium_render.cpp @@ -773,7 +773,7 @@ int32_t prepare_replacement_page(DocWrapper* dw, int32_t insertAt, UniquePage& p FPDFPageObj_SetMatrix(imgObj.get(), &matrix); FPDFPage_InsertObject(newPage.get(), imgObj.get()); - imgObj.release(); // the page owns it now + (void)imgObj.release(); // the page owns it now if (!FPDFPage_GenerateContent(newPage.get())) return JPDFIUM_ERR_NATIVE; // A generated replacement page must carry the image object; PDFium drops // an image it cannot stream, so never import a content-less page. diff --git a/native/bridge/src/jpdfium_sanitize.cpp b/native/bridge/src/jpdfium_sanitize.cpp index 5f191b12e..3cf844284 100644 --- a/native/bridge/src/jpdfium_sanitize.cpp +++ b/native/bridge/src/jpdfium_sanitize.cpp @@ -218,15 +218,20 @@ bool parseToUnicode(const std::shared_ptr& buf, std::vector& for (unsigned char c : loBytes) lo = (lo << 8) | c; for (unsigned char c : hiBytes) hi = (hi << 8) | c; if (pendingHex.size() == 3) { - // consecutive form: lo..hi -> u..u+n - int unicode = 0; - for (unsigned char c : uniBytes) unicode = (unicode << 8) | c; + // consecutive form: lo..hi -> u..u+n; advance the full + // big-endian UTF-16 byte string in place so multi-unit + // values (including surrogate pairs) never truncate. + std::string ub = uniBytes; for (int code = lo; code <= hi; code++) { - std::string ub(2, '\0'); - ub[0] = static_cast((unicode >> 8) & 0xFF); - ub[1] = static_cast(unicode & 0xFF); out.push_back(CmapEntry{code, static_cast(loBytes.size()), ub}); - unicode++; + for (size_t idx = ub.size(); idx-- > 0;) { + auto v = static_cast(ub[idx]); + if (v < 0xFF) { + ub[idx] = static_cast(v + 1); + break; + } + ub[idx] = '\0'; + } } } else { // list form: pendingHex[2..] are the unicodes @@ -301,25 +306,40 @@ std::string rebuildCmap(const std::vector& entries, const std::setcodeBytes != b->codeBytes) return a->codeBytes < b->codeBytes; return a->code < b->code; }); + bool twoByte = false; + for (const auto* e : kept) { + if (e->codeBytes != 1) { + twoByte = true; + break; + } + } std::ostringstream os; os << "/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n" "/CIDSystemInfo << /Registry (JPDFium) /Ordering (Redact) /Supplement 0 >> def\n" - "/CMapName /JPDFium-Redact def\n/CMapType 2 def\n"; + "/CMapName /JPDFium-Redact def\n/CMapType 2 def\n" + "1 begincodespacerange\n" + << (twoByte ? "<0000> \n" : "<00> \n") << "endcodespacerange\n"; static const char* hexd = "0123456789ABCDEF"; - os << kept.size() << " beginbfchar\n"; - for (const auto* e : kept) { - os << '<'; - if (e->codeBytes == 1) { - os << hexd[(e->code >> 4) & 0xF] << hexd[e->code & 0xF]; - } else { - os << hexd[(e->code >> 12) & 0xF] << hexd[(e->code >> 8) & 0xF] - << hexd[(e->code >> 4) & 0xF] << hexd[e->code & 0xF]; + // ISO 32000-1 §9.10.3 caps each beginbfchar section at 100 entries. + for (size_t start = 0; start < kept.size(); start += 100) { + size_t end = std::min(start + 100, kept.size()); + os << (end - start) << " beginbfchar\n"; + for (size_t idx = start; idx < end; idx++) { + const auto* e = kept[idx]; + os << '<'; + if (e->codeBytes == 1) { + os << hexd[(e->code >> 4) & 0xF] << hexd[e->code & 0xF]; + } else { + os << hexd[(e->code >> 12) & 0xF] << hexd[(e->code >> 8) & 0xF] + << hexd[(e->code >> 4) & 0xF] << hexd[e->code & 0xF]; + } + os << "> <"; + for (unsigned char c : e->utf16be) os << hexd[(c >> 4) & 0xF] << hexd[c & 0xF]; + os << ">\n"; } - os << "> <"; - for (unsigned char c : e->utf16be) os << hexd[(c >> 4) & 0xF] << hexd[c & 0xF]; - os << ">\n"; + os << "endbfchar\n"; } - os << "endbfchar\nendcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n"; + os << "endcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n"; return os.str(); } @@ -517,11 +537,21 @@ int sanitizeRedactedPdf(const uint8_t* input, size_t inputLen, const DocCore& co } // 7. Structure tree strip (ActualText is a verbatim text copy). + // StructTreeRoot and MarkInfo live in the Catalog (/Root), though + // defensive cleanup on trailer handles non-standard inputs as well. if (trailer.hasKey("/StructTreeRoot")) { trailer.removeKey("/StructTreeRoot"); st.structTreeRemoved = true; } if (trailer.hasKey("/MarkInfo")) trailer.removeKey("/MarkInfo"); + if (trailer.hasKey("/Root") && trailer.getKey("/Root").isDictionary()) { + QPDFObjectHandle catalog = trailer.getKey("/Root"); + if (catalog.hasKey("/StructTreeRoot")) { + catalog.removeKey("/StructTreeRoot"); + st.structTreeRemoved = true; + } + if (catalog.hasKey("/MarkInfo")) catalog.removeKey("/MarkInfo"); + } // XMP surgery via pugixml; wholesale removal when unavailable. // The /Metadata entry can sit on the trailer OR the catalog. @@ -543,6 +573,7 @@ int sanitizeRedactedPdf(const uint8_t* input, size_t inputLen, const DocCore& co memcpy(newBuf->getBuffer(), xml.data(), xml.size()); meta.replaceStreamData(newBuf, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull()); + meta.setFilterOnWrite(false); st.xmpScrubbed = true; } #else @@ -591,12 +622,14 @@ int sanitizeRedactedPdf(const uint8_t* input, size_t inputLen, const DocCore& co QPDFObjectHandle a = annots.getArrayItem(ai); if (!a.isDictionary()) continue; bool drop = false; - if (a.hasKey("/Rect") && a.getKey("/Rect").isArray() && - a.getKey("/Rect").getArrayNItems() == 4) { - double llx = a.getKey("/Rect").getArrayItem(0).getNumericValue(); - double lly = a.getKey("/Rect").getArrayItem(1).getNumericValue(); - double urx = a.getKey("/Rect").getArrayItem(2).getNumericValue(); - double ury = a.getKey("/Rect").getArrayItem(3).getNumericValue(); + QPDFObjectHandle rect = a.getKey("/Rect"); + if (rect.isArray() && rect.getArrayNItems() == 4 && + rect.getArrayItem(0).isNumber() && rect.getArrayItem(1).isNumber() && + rect.getArrayItem(2).isNumber() && rect.getArrayItem(3).isNumber()) { + double llx = rect.getArrayItem(0).getNumericValue(); + double lly = rect.getArrayItem(1).getNumericValue(); + double urx = rect.getArrayItem(2).getNumericValue(); + double ury = rect.getArrayItem(3).getNumericValue(); for (const auto& z : zones) { if (z.pageIndex != static_cast(pi)) continue; if (llx <= z.right && urx >= z.left && lly <= z.top && @@ -684,8 +717,9 @@ int sanitizeRedactedPdf(const uint8_t* input, size_t inputLen, const DocCore& co } for (auto& [id, font] : fontDicts) { - std::string baseFont = - font.hasKey("/BaseFont") ? font.getKey("/BaseFont").getName() : std::string(); + std::string baseFont = font.hasKey("/BaseFont") && font.getKey("/BaseFont").isName() + ? font.getKey("/BaseFont").getName() + : std::string(); // Code width: from the ToUnicode CMap when parseable; otherwise // /Subtype /Type0 fonts are 2-byte, everything else 1-byte. int codeBytes = 1; @@ -747,6 +781,10 @@ int sanitizeRedactedPdf(const uint8_t* input, size_t inputLen, const DocCore& co jpdfium_qpdf_internal::WriterLock lock{-1}; w.write(); std::shared_ptr buf = w.getBufferSharedPointer(); + if (!buf) { + reportJson = "{\"error\":\"qpdf produced null output buffer\"}"; + return -1; + } out.assign(buf->getBuffer(), buf->getBuffer() + buf->getSize()); reportJson = statsToJson(st); return 0; diff --git a/native/bridge/src/jpdfium_signature.cpp b/native/bridge/src/jpdfium_signature.cpp index 3cf628af0..9e6bb318f 100644 --- a/native/bridge/src/jpdfium_signature.cpp +++ b/native/bridge/src/jpdfium_signature.cpp @@ -111,8 +111,9 @@ std::string sigString(EPDF_SIGNATURE_MODEL model, int index, int key) { int32_t jpdfium_signature_count(int64_t doc, int32_t* count) { if (!count) return JPDFIUM_ERR_INVALID; + *count = 0; DocWrapper* w = decodeDoc(doc); - if (!w || !w->core->doc) return JPDFIUM_ERR_INVALID; + if (!w || !w->core || !w->core->doc) return JPDFIUM_ERR_INVALID; return jpdfium_guarded([&] { EPDF_SIGNATURE_MODEL model = EPDFSig_LoadModel(w->core->doc); if (!model) { @@ -130,8 +131,9 @@ int32_t jpdfium_signature_count(int64_t doc, int32_t* count) { // Number of byte revisions (signed prefixes) in the loaded document. int32_t jpdfium_signature_revision_count(int64_t doc, int32_t* count) { if (!count) return JPDFIUM_ERR_INVALID; + *count = 0; DocWrapper* w = decodeDoc(doc); - if (!w || !w->core->doc) return JPDFIUM_ERR_INVALID; + if (!w || !w->core || !w->core->doc) return JPDFIUM_ERR_INVALID; return jpdfium_guarded([&] { *count = EPDFDoc_GetRevisionCount(w->core->doc); return JPDFIUM_OK; @@ -146,8 +148,9 @@ int32_t jpdfium_signature_revision_count(int64_t doc, int32_t* count) { // "contactInfo":...,"signingTime":...,"contentsLength":N} int32_t jpdfium_signature_info(int64_t doc, int32_t index, char** json) { if (!json) return JPDFIUM_ERR_INVALID; + *json = nullptr; DocWrapper* w = decodeDoc(doc); - if (!w || !w->core->doc) return JPDFIUM_ERR_INVALID; + if (!w || !w->core || !w->core->doc) return JPDFIUM_ERR_INVALID; return jpdfium_guarded([&] { EPDF_SIGNATURE_MODEL model = EPDFSig_LoadModel(w->core->doc); if (!model) return JPDFIUM_ERR_NATIVE; @@ -203,11 +206,13 @@ int32_t jpdfium_signature_info(int64_t doc, int32_t index, char** json) { int32_t jpdfium_signature_digest(int64_t doc, int32_t index, int32_t algorithm, uint8_t** digest, int64_t* len) { if (!digest || !len) return JPDFIUM_ERR_INVALID; + *digest = nullptr; + *len = 0; if (algorithm < EPDF_DIGEST_SHA1 || algorithm > EPDF_DIGEST_SHA512) { return JPDFIUM_ERR_INVALID; } DocWrapper* w = decodeDoc(doc); - if (!w || !w->core->doc) return JPDFIUM_ERR_INVALID; + if (!w || !w->core || !w->core->doc) return JPDFIUM_ERR_INVALID; return jpdfium_guarded([&] { EPDF_SIGNATURE_MODEL model = EPDFSig_LoadModel(w->core->doc); if (!model) return JPDFIUM_ERR_NATIVE; @@ -223,7 +228,8 @@ int32_t jpdfium_signature_digest(int64_t doc, int32_t index, int32_t algorithm, unsigned char buf[64]; unsigned long bufLen = sizeof(buf); - if (!EPDFSig_DigestByteRange(w->core->doc, range, algorithm, buf, &bufLen)) { + if (!EPDFSig_DigestByteRange(w->core->doc, range, algorithm, buf, &bufLen) || bufLen == 0 || + bufLen > sizeof(buf)) { return JPDFIUM_ERR_NATIVE; } auto* out = static_cast(malloc(bufLen)); diff --git a/native/rust/jpdfium-impl/src/lib.rs b/native/rust/jpdfium-impl/src/lib.rs index ff3d17369..e8029fedb 100644 --- a/native/rust/jpdfium-impl/src/lib.rs +++ b/native/rust/jpdfium-impl/src/lib.rs @@ -26,7 +26,7 @@ thread_local! { use fast_image_resize as fir; use fir::images::Image as FirImage; -use flate2::read::ZlibDecoder; +use flate2::read::{DeflateDecoder, ZlibDecoder}; use lopdf::Document; use zopfli::{compress as zopfli_compress, Format, Options}; @@ -53,13 +53,41 @@ unsafe fn malloc_copy(data: &[u8]) -> *mut u8 { ptr } -/// Decompress ZLib (FlateDecode) bytes using flate2. -/// Returns the raw content, or None on failure. +/// Decompress ZLib (FlateDecode) bytes using flate2, falling back to raw +/// DEFLATE when the zlib wrapper or Adler-32 trailer is corrupt or absent. fn flate2_decompress_zlib(data: &[u8]) -> Option> { - let mut decoder = ZlibDecoder::new(data); let mut output = Vec::new(); - decoder.read_to_end(&mut output).ok()?; - Some(output) + if ZlibDecoder::new(data).read_to_end(&mut output).is_ok() { + return Some(output); + } + output.clear(); + if DeflateDecoder::new(data).read_to_end(&mut output).is_ok() && !output.is_empty() { + return Some(output); + } + if data.len() >= 6 && (data[0] & 0x0F) == 8 { + let header = ((data[0] as u16) << 8) | (data[1] as u16); + if header % 31 == 0 { + output.clear(); + if DeflateDecoder::new(&data[2..]) + .read_to_end(&mut output) + .is_ok() + && !output.is_empty() + { + return Some(output); + } + } + } + None +} + +fn is_plain_flate_filter(dict: &lopdf::Dictionary) -> bool { + match dict.get(b"Filter") { + Ok(lopdf::Object::Name(name)) => name == b"FlateDecode", + Ok(lopdf::Object::Array(arr)) if arr.len() == 1 => { + matches!(arr.first(), Some(lopdf::Object::Name(name)) if name == b"FlateDecode") + } + _ => false, + } } /// Compress `input` with zopfli using ZLib format (matches PDF FlateDecode). @@ -113,6 +141,9 @@ pub unsafe extern "C" fn jpdfium_rust_compress_pdf( out_len: *mut i64, zopfli_iters: i32, ) -> i32 { + if out_ptr.is_null() || out_len.is_null() { + return JPDFIUM_ERR_GENERIC; + } *out_ptr = std::ptr::null_mut(); *out_len = 0; @@ -148,6 +179,12 @@ fn compress_pdf_impl(input: &[u8], iterations: u32) -> Result, Box Result, Box = if still_filtered { - // lopdf left this stream compressed - try flate2 as a fallback. + if !is_plain_flate_filter(&stream.dict) { + continue; + } + // lopdf left this Flate stream compressed - try tolerant flate2/raw-deflate fallback. match flate2_decompress_zlib(&stream.content) { Some(decoded) => decoded, None => continue, // Cannot decode; leave stream untouched. @@ -176,11 +216,9 @@ fn compress_pdf_impl(input: &[u8], iterations: u32) -> Result, Box i32 { + if out_ptr.is_null() || out_len.is_null() { + return JPDFIUM_REPAIR_FAILED; + } *out_ptr = std::ptr::null_mut(); *out_len = 0; @@ -273,6 +314,9 @@ pub unsafe extern "C" fn jpdfium_rust_resize_pixels( out_ptr: *mut *mut u8, out_len: *mut i64, ) -> i32 { + if out_ptr.is_null() || out_len.is_null() { + return JPDFIUM_ERR_GENERIC; + } *out_ptr = std::ptr::null_mut(); *out_len = 0; @@ -287,6 +331,16 @@ pub unsafe extern "C" fn jpdfium_rust_resize_pixels( return JPDFIUM_ERR_GENERIC; } + let Some(expected_len) = (src_width as u64) + .checked_mul(src_height as u64) + .and_then(|px| px.checked_mul(components as u64)) + else { + return JPDFIUM_ERR_GENERIC; + }; + if (src_len as u64) < expected_len || expected_len > (isize::MAX as u64) { + return JPDFIUM_ERR_GENERIC; + } + let src_slice = slice::from_raw_parts(src_pixels, src_len as usize); let pixel_type = match components { @@ -476,6 +530,9 @@ pub unsafe extern "C" fn jpdfium_rust_compress_png( out_len: *mut i64, level: i32, ) -> i32 { + if out_ptr.is_null() || out_len.is_null() { + return JPDFIUM_ERR_GENERIC; + } *out_ptr = std::ptr::null_mut(); *out_len = 0; @@ -750,7 +807,18 @@ pub unsafe extern "C" fn jpdfium_rust_svg_to_rgba( out_w: *mut i32, out_h: *mut i32, ) -> i32 { - if svg.is_null() || svg_len == 0 || out_ptr.is_null() || out_len.is_null() { + if out_ptr.is_null() || out_len.is_null() { + return JPDFIUM_ERR_GENERIC; + } + *out_ptr = std::ptr::null_mut(); + *out_len = 0; + if !out_w.is_null() { + *out_w = 0; + } + if !out_h.is_null() { + *out_h = 0; + } + if svg.is_null() || svg_len == 0 { return JPDFIUM_ERR_GENERIC; } let data = slice::from_raw_parts(svg, svg_len); @@ -1195,3 +1263,171 @@ mod jbig2_roundtrip_tests { } } } + +#[cfg(test)] +mod tests { + use super::*; + use flate2::write::{DeflateEncoder, ZlibEncoder}; + use flate2::Compression; + use lopdf::{dictionary, Document, Object, Stream}; + use std::io::Write; + + #[test] + fn flate2_decompress_zlib_handles_standard_raw_and_bad_adler32() { + let payload = b"BT /F1 12 Tf 72 700 Td (Repairable Flate Stream) Tj ET"; + + let mut zenc = ZlibEncoder::new(Vec::new(), Compression::default()); + zenc.write_all(payload).unwrap(); + let valid_zlib = zenc.finish().unwrap(); + assert_eq!( + flate2_decompress_zlib(&valid_zlib).as_deref(), + Some(payload.as_slice()) + ); + + // Raw Deflate without the 2-byte zlib header or 4-byte Adler-32 trailer. + let mut denc = DeflateEncoder::new(Vec::new(), Compression::default()); + denc.write_all(payload).unwrap(); + let raw_deflate = denc.finish().unwrap(); + assert_eq!( + flate2_decompress_zlib(&raw_deflate).as_deref(), + Some(payload.as_slice()) + ); + + // Valid CMF/FLG zlib header with corrupted Adler-32 trailer. + let mut bad_adler = valid_zlib.clone(); + let last = bad_adler.len() - 1; + bad_adler[last] ^= 0x5A; + assert_eq!( + flate2_decompress_zlib(&bad_adler).as_deref(), + Some(payload.as_slice()) + ); + + assert_eq!(flate2_decompress_zlib(b"\xFF\x00\xAB"), None); + } + + #[test] + fn compress_pdf_impl_preserves_dct_and_flate_filter_integrity() { + let mut doc = Document::with_version("1.5"); + let pages_id = doc.new_object_id(); + + let tiny_raw = b"q Q"; + let mut zenc = ZlibEncoder::new(Vec::new(), Compression::best()); + zenc.write_all(tiny_raw).unwrap(); + let bad_flate = { + let mut z = zenc.finish().unwrap(); + let last = z.len() - 1; + z[last] ^= 0xFF; + z + }; + let flate_id = doc.add_object(Stream::new( + dictionary! { "Filter" => "FlateDecode" }, + bad_flate, + )); + + let fake_jpeg = vec![ + 0xFFu8, 0xD8, 0xFF, 0xE0, 0x00, 0x10, b'J', b'F', b'I', b'F', 0xFF, 0xD9, + ]; + let dct_id = doc.add_object(Stream::new( + dictionary! { + "Type" => "XObject", + "Subtype" => "Image", + "Width" => 1, + "Height" => 1, + "Filter" => "DCTDecode", + }, + fake_jpeg.clone(), + )); + + let resources_id = doc.add_object(dictionary! { + "XObject" => dictionary! { "Im0" => dct_id }, + }); + let page_id = doc.add_object(dictionary! { + "Type" => "Page", + "Parent" => pages_id, + "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], + "Resources" => resources_id, + "Contents" => flate_id, + }); + doc.objects.insert( + pages_id, + Object::Dictionary(dictionary! { + "Type" => "Pages", + "Kids" => vec![page_id.into()], + "Count" => 1, + }), + ); + let catalog_id = doc.add_object(dictionary! { + "Type" => "Catalog", + "Pages" => pages_id, + }); + doc.trailer.set("Root", catalog_id); + + let mut input = Vec::new(); + doc.save_to(&mut Cursor::new(&mut input)).unwrap(); + + let out_bytes = compress_pdf_impl(&input, 1).unwrap(); + let reloaded = Document::load_mem(&out_bytes).unwrap(); + + let reloaded_dct = reloaded.get_object(dct_id).unwrap().as_stream().unwrap(); + assert_eq!(reloaded_dct.content, fake_jpeg); + + let reloaded_flate = reloaded.get_object(flate_id).unwrap().as_stream().unwrap(); + if is_plain_flate_filter(&reloaded_flate.dict) { + assert_eq!( + flate2_decompress_zlib(&reloaded_flate.content).as_deref(), + Some(tiny_raw.as_slice()), + "FlateDecode stream must never store uncompressed bytes without stripping /Filter" + ); + } else { + assert_eq!(reloaded_flate.content, tiny_raw); + } + } + + #[test] + fn compress_pdf_impl_keeps_metadata_stream_uncompressed() { + let mut doc = Document::with_version("1.4"); + let pages_id = doc.new_object_id(); + let xmp = b"\ + \ + \ + ".to_vec(); + let meta_id = doc.add_object(Stream::new( + dictionary! { + "Type" => "Metadata", + "Subtype" => "XML", + }, + xmp.clone(), + )); + let page_id = doc.add_object(dictionary! { + "Type" => "Page", + "Parent" => pages_id, + "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], + }); + doc.objects.insert( + pages_id, + Object::Dictionary(dictionary! { + "Type" => "Pages", + "Kids" => vec![page_id.into()], + "Count" => 1, + }), + ); + let catalog_id = doc.add_object(dictionary! { + "Type" => "Catalog", + "Pages" => pages_id, + "Metadata" => meta_id, + }); + doc.trailer.set("Root", catalog_id); + + let mut input = Vec::new(); + doc.save_to(&mut Cursor::new(&mut input)).unwrap(); + + let out_bytes = compress_pdf_impl(&input, 1).unwrap(); + let reloaded = Document::load_mem(&out_bytes).unwrap(); + let reloaded_meta = reloaded.get_object(meta_id).unwrap().as_stream().unwrap(); + assert!( + reloaded_meta.dict.get(b"Filter").is_err(), + "Metadata stream must remain uncompressed for PDF/A-1 compliance" + ); + assert_eq!(reloaded_meta.content, xmp); + } +} From ad860c0a65cfd98445184d8ab96735deb2f8998c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Wed, 18 Nov 2026 02:07:44 +0100 Subject: [PATCH 02/11] Move PDF/A conformance production fixes into the conformance PR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../software/jpdfium/doc/PdfFontAuditor.java | 157 ++++ .../software/jpdfium/doc/PdfFontEmbedder.java | 159 +++- .../software/jpdfium/doc/PdfRepair.java | 837 +++++++++++++++++- native/bridge/include/jpdfium_font_internal.h | 10 + native/bridge/src/jpdfium_font_normalizer.cpp | 589 +++++++++++- native/bridge/src/jpdfium_font_shared.cpp | 193 +++- .../bridge/src/jpdfium_qpdf_fonts_embed.cpp | 23 +- .../bridge/src/jpdfium_qpdf_fonts_subset.cpp | 36 +- native/bridge/src/jpdfium_repair.cpp | 2 +- 9 files changed, 1958 insertions(+), 48 deletions(-) diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java index af9917de4..b6299fe9f 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java @@ -5,6 +5,7 @@ import stirling.software.jpdfium.exception.JPDFiumException; import stirling.software.jpdfium.panama.AnnotationBindings; import stirling.software.jpdfium.panama.NativeRuntime; +import stirling.software.jpdfium.panama.AnnotationBindings; import stirling.software.jpdfium.panama.PageEditBindings; import stirling.software.jpdfium.panama.QpdfLib; import stirling.software.jpdfium.util.NativeJsonParser; @@ -574,6 +575,162 @@ private static void scanObject( } } + /** + * Font base names used by text objects that are actually painted. Text in + * rendering mode 3 (invisible OCR layers) is excluded. Nested form XObjects + * are traversed when the native build supports it. + */ + public static Set visibleFontKeys(PdfDocument doc) { + Set keys = new LinkedHashSet<>(); + for (int i = 0; i < doc.pageCount(); i++) { + try (PdfPage page = doc.page(i)) { + MemorySegment raw = page.rawHandleForRead(); + int annotations = + (int) AnnotationBindings.FPDFPage_GetAnnotCount.invokeExact(raw); + if (annotations > 0) return Set.of(); + int count = (int) PageEditBindings.FPDFPage_CountObjects.invokeExact(raw); + for (int j = 0; j < count; j++) { + MemorySegment obj = + (MemorySegment) PageEditBindings.FPDFPage_GetObject.invokeExact(raw, j); + collectVisibleFonts(obj, keys, 0); + } + } catch (Throwable t) { + NativeRuntime.rethrowFatal(t); + // A page that cannot be walked tells us nothing; do not hide fonts. + } + } + return keys; + } + + /** + * Embedded font base names whose stored program is empty, unreadable, or + * does not start with a recognized font magic. Such a font renders with a + * fallback and should be replaced when an embeddable substitute exists. + */ + public static Set corruptEmbeddedKeys(PdfDocument doc) { + Set keys = new LinkedHashSet<>(); + for (int i = 0; i < doc.pageCount(); i++) { + try (PdfPage page = doc.page(i)) { + MemorySegment raw = page.rawHandleForRead(); + int count = (int) PageEditBindings.FPDFPage_CountObjects.invokeExact(raw); + for (int j = 0; j < count; j++) { + MemorySegment obj = + (MemorySegment) PageEditBindings.FPDFPage_GetObject.invokeExact(raw, j); + collectCorruptFonts(obj, keys, 0); + } + } catch (Throwable t) { + NativeRuntime.rethrowFatal(t); + // Best effort; leave unknown fonts alone. + } + } + return keys; + } + + /** + * Names of fonts whose descriptor points at an embedded program that is not a + * recognizable font program. Uses the qpdf inventory so a program PDFium refuses to expose + * is still detected. + */ + static List corruptEmbeddedKeys(byte[] pdf) { + if (pdf == null || pdf.length == 0 || !QpdfLib.isFontInventorySupported()) return List.of(); + String json = QpdfLib.inspectFonts(pdf); + if (json == null || json.length() <= 2) return List.of(); + Set names = new LinkedHashSet<>(); + for (Map field : NativeJsonParser.parseArray(json)) { + if ("true".equals(field.get("program")) + && "false".equals(field.get("programValid"))) { + String name = field.getOrDefault("baseFont", ""); + if (!name.isEmpty()) names.add(name); + } + } + return List.copyOf(names); + } + + private static void collectCorruptFonts(MemorySegment obj, Set keys, int depth) { + if (depth > 64 || obj == null || obj.equals(MemorySegment.NULL)) return; + try { + int type = (int) PageEditBindings.FPDFPageObj_GetType.invokeExact(obj); + if (type == 1) { + MemorySegment font = + (MemorySegment) PageEditBindings.FPDFTextObj_GetFont.invokeExact(obj); + String name = getFontBaseName(font); + if (name.isEmpty() || "".equals(name)) return; + if ((int) PageEditBindings.FPDFFont_GetIsEmbedded.invokeExact(font) == 1) { + byte[] data = getFontProgram(font); + if (!looksLikeFontProgram(data)) { + keys.add(name); + } + } + } else if (type == 5 && PageEditBindings.formTraversalAvailable()) { + int children = + (int) PageEditBindings.FPDFFormObj_CountObjects.invokeExact(obj); + for (int i = 0; i < children; i++) { + collectCorruptFonts(PageEditBindings.formObject(obj, i), keys, depth + 1); + } + } + } catch (Throwable t) { + NativeRuntime.rethrowFatal(t); + // Best effort. + } + } + + private static byte[] getFontProgram(MemorySegment font) { + try { + long needed = getFontDataSize(font); + if (needed <= 0 || needed > 64L * 1024 * 1024) return null; + try (Arena arena = Arena.ofConfined()) { + MemorySegment buf = arena.allocate(needed); + MemorySegment outLen = arena.allocate(ValueLayout.JAVA_LONG); + int ok = (int) PageEditBindings.FPDFFont_GetFontData.invokeExact( + font, buf, needed, outLen); + if (ok == 0) return null; + long len = Math.min(needed, outLen.get(ValueLayout.JAVA_LONG, 0)); + if (len <= 0) return null; + return buf.asSlice(0, len).toArray(ValueLayout.JAVA_BYTE); + } + } catch (Throwable _) { + return null; + } + } + + static boolean looksLikeFontProgram(byte[] data) { + if (data == null || data.length < 4) return false; + if (data[0] == 0x00 && data[1] == 0x01 && data[2] == 0x00 && data[3] == 0x00) return true; + if (data[0] == 0x01 && data[1] == 0x00 && data[2] == 0x04 && data[3] == 0x04) return true; + String head = new String(data, 0, Math.min(4, data.length), StandardCharsets.ISO_8859_1); + if ("OTTO".equals(head) || "true".equals(head) || "ttcf".equals(head)) return true; + if (data.length >= 4 && data[0] == '%' && data[1] == '!') return true; + return data.length >= 2 && (data[0] & 0xFF) == 0x80 && data[1] == 0x01; + } + + private static void collectVisibleFonts(MemorySegment obj, Set keys, int depth) { + if (depth > 64 || obj == null || obj.equals(MemorySegment.NULL)) return; + try { + int type = (int) PageEditBindings.FPDFPageObj_GetType.invokeExact(obj); + if (type == 1) { + int renderMode = + (int) PageEditBindings.FPDFTextObj_GetTextRenderMode.invokeExact(obj); + if (renderMode != 3) { + MemorySegment font = + (MemorySegment) PageEditBindings.FPDFTextObj_GetFont.invokeExact(obj); + String name = getFontBaseName(font); + if (!name.isEmpty() && !"".equals(name)) { + keys.add(PdfFontEmbedder.key(name)); + } + } + } else if (type == 5 && PageEditBindings.formTraversalAvailable()) { + int children = + (int) PageEditBindings.FPDFFormObj_CountObjects.invokeExact(obj); + for (int i = 0; i < children; i++) { + MemorySegment child = PageEditBindings.formObject(obj, i); + collectVisibleFonts(child, keys, depth + 1); + } + } + } catch (Throwable _) { + // Best-effort scan; keep whatever was collected. + } + } + private static String getFontBaseName(MemorySegment font) { try (Arena arena = Arena.ofConfined()) { // First call: get required buffer size diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java index 878f0493d..27fbb934d 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java @@ -1,5 +1,7 @@ package stirling.software.jpdfium.doc; +import stirling.software.jpdfium.PdfDocument; +import stirling.software.jpdfium.panama.NativeRuntime; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -21,6 +23,7 @@ import java.util.Collections; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -36,7 +39,11 @@ import org.tukaani.xz.XZInputStream; import stirling.software.jpdfium.exception.JPDFiumException; +import stirling.software.jpdfium.panama.FontLib; import stirling.software.jpdfium.panama.QpdfLib; +import stirling.software.jpdfium.text.PageText; +import stirling.software.jpdfium.text.PdfTextExtractor; +import stirling.software.jpdfium.text.TextChar; /** * Embed font programs into a PDF in one qpdf rewrite. @@ -288,21 +295,39 @@ public static Result embedMissing(byte[] pdf, boolean stripSignatures, Path... f return new Result(pdf == null ? new byte[0] : pdf, List.of(), List.of(), List.of()); } List resources = PdfFontAuditor.unembeddedResources(pdf); - Set names = new LinkedHashSet<>(); + Set corrupt = new LinkedHashSet<>(PdfFontAuditor.corruptEmbeddedKeys(pdf)); + Set names = new LinkedHashSet<>(corrupt); + Set resourceOnly = new HashSet<>(); Map blockers = new HashMap<>(); for (PdfFontAuditor.FontResource resource : resources) { names.add(resource.baseName()); + if (resource.resourceOnly()) resourceOnly.add(resource.baseName()); String blocker = substitutionBlocker(resource); if (blocker != null) blockers.putIfAbsent(resource.baseName(), blocker); } List missing = List.copyOf(names); if (missing.isEmpty()) return new Result(pdf, List.of(), List.of(), List.of()); + Set visibleFonts = Set.of(); + Map> usedCodepoints = Map.of(); + try (PdfDocument doc = PdfDocument.open(pdf)) { + visibleFonts = PdfFontAuditor.visibleFontKeys(doc); + usedCodepoints = usedCodepoints(doc); + } catch (Exception _) { + // If the document cannot be walked, do not suppress any font. + } Map bundled = fontDirs.length == 0 ? bundledFaces() : null; Map index = null; Map found = new LinkedHashMap<>(); + Map corruptFound = new LinkedHashMap<>(); Map operationPrograms = new HashMap<>(); List statuses = new ArrayList<>(missing.size()); for (String name : missing) { + if (!visibleFonts.isEmpty() && !visibleFonts.contains(key(name)) + && !resourceOnly.contains(name)) { + statuses.add(new FontStatus(name, "", -1, false, + "font is only used in invisible text (Tr 3)")); + continue; + } String blocker = blockers.get(name); if (blocker != null) { statuses.add(new FontStatus(name, "", -1, false, blocker)); @@ -326,6 +351,12 @@ public static Result embedMissing(byte[] pdf, boolean stripSignatures, Path... f if (index == null) index = indexFor(fontDirs); face = resolveName(index, name); } + if (face == null && bundled != null) { + Face bundledAlt = restrictedName(bundled, name); + if (bundledAlt != null && bundledAlt.embeddable()) { + face = bundledAlt; + } + } if (face == null) { if (index == null) index = indexFor(fontDirs); Face restricted = restrictedName(index, name); @@ -336,16 +367,50 @@ public static Result embedMissing(byte[] pdf, boolean stripSignatures, Path... f continue; } byte[] data = program(face, operationPrograms); + Set needed = usedCodepoints.get(key); + if (needed != null && !needed.isEmpty()) { + int[] cps = needed.stream().mapToInt(Integer::intValue).toArray(); + boolean[] covered; + try { + covered = FontLib.coversText(data, cps); + } catch (Throwable t) { + NativeRuntime.rethrowFatal(t); + covered = null; + } + if (covered != null) { + for (int i = 0; i < covered.length; i++) { + if (!covered[i]) { + statuses.add(new FontStatus(name, source(face), face.fsType(), false, + "candidate font does not cover used codepoint U+" + + Integer.toHexString(cps[i]).toUpperCase())); + data = null; + break; + } + } + } + } + if (data == null) continue; int fsType = programFsType(face, data); if (!embeddableProgram(fsType)) { statuses.add(new FontStatus(name, source(face), fsType, false, "embedding restricted by fsType " + fsType)); continue; } - found.put(name, singleFace(data, name)); + if (corrupt.contains(name)) { + corruptFound.put(name, singleFace(data, name)); + } else { + found.put(name, singleFace(data, name)); + } statuses.add(new FontStatus(name, source(face), fsType, true, "embedded")); } - byte[] out = found.isEmpty() ? null : QpdfLib.embedFonts(pdf, found, true, stripSignatures); + byte[] out = pdf; + if (!found.isEmpty()) { + out = QpdfLib.embedFonts(out, found, true, stripSignatures); + } + if (out != null && !corruptFound.isEmpty()) { + out = QpdfLib.embedFonts(out, corruptFound, false, stripSignatures); + } + if (found.isEmpty() && corruptFound.isEmpty()) out = null; if (out == null) { List failed = new ArrayList<>(statuses.size()); for (FontStatus status : statuses) { @@ -357,15 +422,22 @@ public static Result embedMissing(byte[] pdf, boolean stripSignatures, Path... f return new Result(pdf, List.of(), missing, failed); } List unresolved = PdfFontAuditor.unembeddedNames(out); + Set stillCorrupt = new LinkedHashSet<>(PdfFontAuditor.corruptEmbeddedKeys(out)); + Set remaining = new LinkedHashSet<>(unresolved); + remaining.addAll(stillCorrupt); List finalStatuses = new ArrayList<>(statuses.size()); for (FontStatus status : statuses) { - finalStatuses.add(status.resolved() && unresolved.contains(status.baseName()) + finalStatuses.add(status.resolved() + && (remaining.contains(status.baseName())) ? new FontStatus(status.baseName(), status.source(), status.fsType(), false, "native embedder left the font unchanged (glyph order or width coverage)") : status); } - return new Result(out, missing.stream().filter(n -> !unresolved.contains(n)).toList(), - unresolved, finalStatuses); + return new Result(out, + missing.stream() + .filter(n -> !remaining.contains(n)) + .toList(), + List.copyOf(remaining), finalStatuses); } /** @@ -499,9 +571,21 @@ public static FontStatus diagnose(String baseName, Path... fontDirs) { "embedding restricted by fsType " + restricted.fsType()); } + private static List configuredFontDirs() { + List dirs = new ArrayList<>(SYSTEM_FONT_DIRS); + String extra = System.getProperty("jpdfium.font.path"); + if (extra == null || extra.isBlank()) extra = System.getenv("JPDFIUM_FONT_PATH"); + if (extra != null) { + for (String part : extra.split(java.util.regex.Pattern.quote(java.io.File.pathSeparator))) { + if (!part.isBlank()) dirs.add(Path.of(part)); + } + } + return List.copyOf(dirs); + } + private static Map indexFor(Path... fontDirs) { Map index = INDEXES.computeIfAbsent( - fontDirs.length == 0 ? SYSTEM_FONT_DIRS : List.of(fontDirs), PdfFontEmbedder::index); + fontDirs.length == 0 ? configuredFontDirs() : List.of(fontDirs), PdfFontEmbedder::index); if (fontDirs.length == 0) return index; // Caller-supplied directories are authoritative; the bundled pack is only // a fallback when no directories were given. @@ -679,6 +763,24 @@ private static boolean alnumAfter(String name, int from, int end) { return false; } + private static Map> usedCodepoints(PdfDocument doc) { + Map> result = new HashMap<>(); + try { + for (PageText page : PdfTextExtractor.extractAll(doc)) { + for (TextChar ch : page.chars()) { + if (ch.unicode() <= 0 || ch.fontName() == null || ch.fontName().isEmpty()) { + continue; + } + result.computeIfAbsent(key(ch.fontName()), k -> new HashSet<>()) + .add(ch.unicode()); + } + } + } catch (Exception _) { + return Map.of(); + } + return result; + } + private static Face resolve(Map index, String key) { return lookup(index, key, true); } @@ -697,6 +799,10 @@ private static Face lookup(Map index, String key, boolean embeddab private static Face lookupFallback(Map index, String key, boolean embeddableOnly) { if (key.isEmpty()) return null; + for (String candidate : scriptFallbackKeys(key)) { + Face face = index.get(candidate); + if (face != null && (!embeddableOnly || face.embeddable())) return face; + } for (String hint : CJK_FAMILY_HINTS) { if (key.contains(hint)) { Face cjk = index.get("notosansscregular"); @@ -729,6 +835,45 @@ private static Face lookupFallback(Map index, String key, boolean return null; } + /** + * Candidate family keys for CJK and world-script fallbacks, in preference + * order. A Japanese mincho/gothic face should not silently become a + * Simplified-Chinese face when a script-specific face is installed. + */ + static List scriptFallbackKeys(String key) { + if (key.isEmpty()) return List.of(); + List keys = new ArrayList<>(); + boolean jp = containsAny(key, "mincho", "gothic", "heisei", "koz", "ryumin", "msgothic", + "msmincho", "hiragino", "futogo", "shin", "toppan", "ipa"); + boolean kr = containsAny(key, "batang", "gulim", "dotum", "gungsuh", "nanum", "malgun", + "baekmuk", "myeongjo", "haansoft"); + boolean tc = containsAny(key, "mingliu", "pmingliu", "msung", "dfkai", "lihei", + "pingfangtc", "microsoftjhenghei", "jhenghei"); + boolean sc = containsAny(key, "simsun", "simhei", "fangsong", "stsong", "yahei", "songti", + "kaiti", "pingfangsc", "microsoftyahei", "notosanssc"); + // Korean/Chinese family names often contain "gothic"/"song"; check + // those scripts before the generic Japanese hint. + if (kr) { + keys.addAll(List.of("applegothic", "notosanskr")); + } else if (tc) { + keys.addAll(List.of("pingfangtc", "notosanstc")); + } else if (sc) { + keys.addAll(List.of("pingfangsc", "notosanssc")); + } else if (jp) { + if (containsAny(key, "mincho", "kozmin", "ryumin", "toppan")) keys.add("hiraginomincho"); + keys.addAll(List.of("hiraginosans", "hiraginokakugothicpro", "notosansjp")); + } + if (sc) keys.add("notosanssc"); + return keys; + } + + private static boolean containsAny(String key, String... needles) { + for (String needle : needles) { + if (key.contains(needle)) return true; + } + return false; + } + private static String styleOf(String suffix) { boolean bold = suffix.contains("bold") || suffix.contains("medi") || suffix.contains("black") || suffix.contains("demi"); diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java index e095fb78f..0a030ef08 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java @@ -1,8 +1,10 @@ package stirling.software.jpdfium.doc; import stirling.software.jpdfium.PdfDocument; +import stirling.software.jpdfium.panama.QpdfLib; import stirling.software.jpdfium.panama.RepairLib; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -11,6 +13,11 @@ import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.nio.file.attribute.PosixFileAttributeView; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * PDF repair with a builder-pattern API. @@ -52,10 +59,6 @@ */ public final class PdfRepair { - // Core repair flags - bit values must match the JPDFIUM_REPAIR_* defines in - // native/bridge/include/jpdfium.h. The previous permutation silently mapped - // forceVersion14() to xref normalization and normalizeXref() to startxref - // correction; it went unnoticed because Builder.all() sets every bit. // Core repair flags - bit values must match the JPDFIUM_REPAIR_* defines in // native/bridge/include/jpdfium.h. The previous permutation silently mapped // forceVersion14() to xref normalization and normalizeXref() to startxref @@ -217,18 +220,34 @@ public static RepairResult repairQdf(byte[] input) { return new RepairResult(RepairResult.Status.FAILED, null, "{\"error\":\"empty input\"}"); } + RepairResult signed = signedDocumentResult(input, true); + if (signed != null) return signed; + RepairResult locked = passwordBlockedResult(input, true); + if (locked != null) { + byte[] decrypted = decryptWithEmptyPassword(input); + if (decrypted == null) return locked; + input = decrypted; + } - byte[] qdf = PdfOptimizer.repair(input); + byte[] prepared = preflightPageTree(normalizeRepairTokens(input)); + byte[] qdf = null; + try { + qdf = PdfOptimizer.repair(prepared); + } catch (RuntimeException _) { + // QDF decompression can exceed byte[] capacity on very large valid + // files; the normal cascade can still validate them. + } if (qdf != null) { try (PdfDocument doc = PdfDocument.open(qdf)) { if (doc.pageCount() > 0) { - return new RepairResult(RepairResult.Status.FIXED, qdf, inspect(input)); + return new RepairResult(RepairResult.Status.FIXED, compact(qdf), + inspect(input)); } } catch (Exception _) { // QDF output is not openable by PDFium; let the cascade try. } } - return builder().input(input).all().build().execute(); + return builder().input(prepared).all().build().execute(); } /** @@ -237,7 +256,17 @@ public static RepairResult repairQdf(byte[] input) { * @return result containing the status, repaired bytes, and diagnostics */ public RepairResult execute() { - byte[] current = inputBytes; + byte[] input = inputBytes; + RepairResult signed = signedDocumentResult(input, writeDiagnostics); + if (signed != null) return signed; + RepairResult locked = passwordBlockedResult(input, writeDiagnostics); + if (locked != null) { + byte[] decrypted = decryptWithEmptyPassword(input); + if (decrypted == null) return locked; + input = decrypted; + } + byte[] current = preflightPageTree(normalizeRepairTokens(input)); + boolean tokenRepaired = !Arrays.equals(current, input); // Pre-repair: Brotli-Flate transcoding if (transcodeBrotli) { @@ -248,6 +277,11 @@ public RepairResult execute() { } RepairResult coreResult = RepairLib.repair(current, flags); + if (tokenRepaired && coreResult.isUsable() + && coreResult.status() == RepairResult.Status.CLEAN) { + coreResult = new RepairResult(RepairResult.Status.FIXED, coreResult.repairedPdf(), + coreResult.diagnosticJson()); + } if (usePdfioFallback && !coreResult.isUsable()) { RepairResult pdfioResult = RepairLib.pdfioRepair(current); @@ -265,6 +299,24 @@ public RepairResult execute() { } } + // Some producers keep valid page dictionaries but lose the /Pages tree. + // Rebuild a tree, repoint each page's /Parent, and let the cascade reopen it. + if (!coreResult.isUsable() || !hasPages(coreResult.repairedPdf())) { + byte[] pageTree = rebuildPageTree(current); + if (pageTree != null) { + RepairResult salvagedResult = RepairLib.repair(pageTree, flags); + if (!salvagedResult.isUsable() && usePdfioFallback) { + salvagedResult = RepairLib.pdfioRepair(pageTree); + } + if (!salvagedResult.isUsable() && useLopdfFallback) { + salvagedResult = RepairLib.rustRepair(pageTree); + } + if (salvagedResult.isUsable() && hasPages(salvagedResult.repairedPdf())) { + coreResult = salvagedResult; + } + } + } + // Truncated PDF salvage: trim partial trailing object after last 'endobj' and append %%EOF if (!coreResult.isUsable()) { byte[] salvaged = salvageTruncatedPdf(current); @@ -273,7 +325,7 @@ public RepairResult execute() { if (!salvagedResult.isUsable() && useLopdfFallback) { salvagedResult = RepairLib.rustRepair(salvaged); } - if (salvagedResult.isUsable()) { + if (salvagedResult.isUsable() && hasPages(salvagedResult.repairedPdf())) { coreResult = salvagedResult; } } @@ -293,6 +345,20 @@ public RepairResult execute() { } } + // A repair that cannot produce a page is not a successful repair. + if (coreResult.status() == RepairResult.Status.FIXED && coreResult.isUsable() + && !hasPages(coreResult.repairedPdf())) { + coreResult = new RepairResult(RepairResult.Status.FAILED, null, + writeDiagnostics + ? "{\"error\":\"repair produced no pages\"}" + : null); + } + + if (coreResult.isUsable() && coreResult.status() != RepairResult.Status.CLEAN) { + coreResult = new RepairResult(coreResult.status(), + compact(coreResult.repairedPdf()), coreResult.diagnosticJson()); + } + if (!writeDiagnostics) { return new RepairResult(coreResult.status(), coreResult.repairedPdf(), null); } @@ -300,6 +366,388 @@ public RepairResult execute() { return coreResult; } + // Repair works in QDF (uncompressed) form. Finish with a qpdf structural + // compression pass so repaired files are practical to hand back to users. + // Keep the QDF bytes if compression fails or loses the page tree. + private static byte[] compact(byte[] repaired) { + if (repaired == null || !PdfOptimizer.isSupported()) return repaired; + try { + byte[] compressed = PdfOptimizer.compress(repaired, PdfOptimizer.DEFAULT, false, + false, false, false, -1); + if (compressed != null && compressed.length < repaired.length + && hasPages(compressed)) { + return compressed; + } + } catch (Exception _) { + // Retain the verified QDF output. + } + return repaired; + } + + // Repair cannot verify an encrypted document that needs a user password. + // Owner-password-only files still open with the empty user password and + // continue through the normal cascade. + private static RepairResult passwordBlockedResult(byte[] pdf, boolean diagnostics) { + if (pdf == null || !contains(pdf, "/Encrypt".getBytes(StandardCharsets.ISO_8859_1))) { + return null; + } + try (PdfDocument doc = PdfDocument.open(pdf)) { + if (doc.pageCount() > 0) return null; + } catch (Exception e) { + String message = String.valueOf(e.getMessage()).toLowerCase(java.util.Locale.ROOT); + if (message.contains("password")) { + return new RepairResult(RepairResult.Status.FAILED, null, + diagnostics ? "{\"error\":\"password-protected PDF cannot be repaired without the password\"}" + : null); + } + } + return null; + } + + // Some producers write a malformed /Encrypt dictionary that PDFium cannot + // open but qpdf still decrypts with the empty user password. Prefer a + // working, decrypted document over failing the repair outright. + private static byte[] decryptWithEmptyPassword(byte[] pdf) { + if (pdf == null || !contains(pdf, "/Encrypt".getBytes(StandardCharsets.ISO_8859_1))) { + return null; + } + try { + byte[] decrypted = QpdfLib.decrypt(pdf, ""); + if (decrypted != null && decrypted.length > 0 + && !contains(decrypted, "/Encrypt".getBytes(StandardCharsets.ISO_8859_1))) { + return decrypted; + } + } catch (Exception _) { + return null; + } + return null; + } + + private static boolean hasPages(byte[] pdf) { + if (pdf == null) return false; + try (PdfDocument doc = PdfDocument.open(pdf)) { + return doc.pageCount() > 0; + } catch (Exception _) { + return false; + } + } + + // qpdf's writer aborts the JVM on some page trees with a broken header and a + // null /Count. Rebuild the tree from page dictionaries before invoking the + // native cascade so such files fail over to salvage instead of terminating. + private static byte[] preflightPageTree(byte[] pdf) { + if (pdf == null || hasPdfHeader(pdf)) return pdf; + byte[] tree = rebuildPageTree(pdf); + return tree != null ? tree : pdf; + } + + private static boolean hasPdfHeader(byte[] pdf) { + int limit = Math.min(pdf.length - 5, 1024); + for (int i = 0; i <= limit; i++) { + if (pdf[i] == '%' && pdf[i + 1] == 'P' && pdf[i + 2] == 'D' + && pdf[i + 3] == 'F' && pdf[i + 4] == '-') return true; + } + return false; + } + + // Positions inside literal strings, hex strings, comments, or streams must + // never be treated as object syntax by the token normalizers below. + private static boolean[] protectedPositions(byte[] pdf) { + boolean[] guarded = new boolean[pdf == null ? 0 : pdf.length]; + if (pdf == null) return guarded; + int depth = 0; + boolean escape = false; + boolean comment = false; + boolean hex = false; + boolean stream = false; + for (int i = 0; i < pdf.length; i++) { + byte b = pdf[i]; + if (stream) { + guarded[i] = true; + if (matchesAscii(pdf, i, "endstream")) { + stream = false; + } + continue; + } + if (comment) { + guarded[i] = true; + if (b == '\n' || b == '\r') comment = false; + continue; + } + if (hex) { + guarded[i] = true; + if (b == '>') hex = false; + continue; + } + if (depth > 0) { + guarded[i] = true; + if (escape) { + escape = false; + } else if (b == '\\') { + escape = true; + } else if (b == '(') { + depth++; + } else if (b == ')') { + depth--; + } + continue; + } + if (b == '%') { + guarded[i] = true; + comment = true; + } else if (b == '(') { + guarded[i] = true; + depth = 1; + } else if (b == '<' && (i + 1 >= pdf.length || pdf[i + 1] != '<') + && (i == 0 || pdf[i - 1] != '<')) { + guarded[i] = true; + hex = true; + } else if (!stream && matchesAscii(pdf, i, "stream") + && (i + 6 == pdf.length || pdf[i + 6] == '\n' || pdf[i + 6] == '\r')) { + guarded[i] = true; + stream = true; + } + } + return guarded; + } + + private static byte[] normalizeRepairTokens(byte[] pdf) { + return deduplicateResourceKeys(normalizeTruncatedObj(normalizeGluedEndobj(pdf))); + } + + private static final Pattern STREAM_BODY = Pattern.compile("stream\r?\n.*?endstream", + Pattern.DOTALL); + private static final Pattern RESOURCE_REF = Pattern.compile("(/Im\\d+)[\\t ]+\\d+[\\t ]+0[\\t ]+R"); + + // Some producers repeat resource keys hundreds of times (e.g. /Im13 ... /Im13). + // qpdf's default error budget then nulls the page object. PDF semantics keep the + // last duplicate, so drop earlier occurrences outside streams. + private static byte[] deduplicateResourceKeys(byte[] pdf) { + if (pdf == null || pdf.length == 0) return pdf; + String text = new String(pdf, StandardCharsets.ISO_8859_1); + boolean[] guarded = protectedPositions(pdf); + Matcher stream = STREAM_BODY.matcher(text); + StringBuilder out = new StringBuilder(text.length()); + int pos = 0; + boolean changed = false; + while (stream.find()) { + String cleaned = deduplicateChunk(text.substring(pos, stream.start()), guarded, pos); + out.append(cleaned).append(text, stream.start(), stream.end()); + changed |= cleaned.length() != stream.end() - stream.start(); + pos = stream.end(); + } + String tail = deduplicateChunk(text.substring(pos), guarded, pos); + out.append(tail); + return out.length() == text.length() && !changed + ? pdf + : out.toString().getBytes(StandardCharsets.ISO_8859_1); + } + + private static String deduplicateChunk(String chunk, boolean[] guarded, int base) { + Matcher m = RESOURCE_REF.matcher(chunk); + java.util.List starts = new java.util.ArrayList<>(); + java.util.List ends = new java.util.ArrayList<>(); + java.util.List groups = new java.util.ArrayList<>(); + java.util.List keys = new java.util.ArrayList<>(); + int[] stack = new int[64]; + int depth = 0; + int scan = 0; + while (m.find()) { + while (scan < m.start()) { + if (base + scan < guarded.length && guarded[base + scan]) { + scan++; + continue; + } + if (scan + 1 < chunk.length() && chunk.charAt(scan) == '<' + && chunk.charAt(scan + 1) == '<') { + if (depth < stack.length) stack[depth] = scan; + depth++; + scan += 2; + } else if (scan + 1 < chunk.length() && chunk.charAt(scan) == '>' + && chunk.charAt(scan + 1) == '>') { + if (depth > 0) depth--; + scan += 2; + } else { + scan++; + } + } + if (base + m.start() < guarded.length && guarded[base + m.start()]) continue; + starts.add(m.start()); + ends.add(m.end()); + groups.add(depth > 0 ? stack[depth - 1] : -1); + keys.add(m.group(1)); + } + Map> lastByGroup = new LinkedHashMap<>(); + Map counts = new LinkedHashMap<>(); + for (int i = 0; i < starts.size(); i++) { + int group = groups.get(i); + lastByGroup.computeIfAbsent(group, _ -> new LinkedHashMap<>()) + .put(keys.get(i), starts.get(i)); + counts.merge(group, 1, Integer::sum); + } + java.util.Set dedupGroups = new java.util.HashSet<>(); + for (var entry : counts.entrySet()) { + int count = entry.getValue(); + int unique = lastByGroup.get(entry.getKey()).size(); + if (count >= 20 && unique <= count - 10) dedupGroups.add(entry.getKey()); + } + if (dedupGroups.isEmpty()) return chunk; + StringBuilder out = new StringBuilder(chunk.length()); + int pos = 0; + for (int i = 0; i < starts.size(); i++) { + int group = groups.get(i); + if (!dedupGroups.contains(group)) continue; + int start = starts.get(i); + String key = keys.get(i); + out.append(chunk, pos, start); + if (lastByGroup.get(group).get(key) == start) { + out.append(chunk, start, ends.get(i)); + } + pos = ends.get(i); + } + out.append(chunk, pos, chunk.length()); + return out.toString(); + } + + // PDFBox tolerates object headers whose "obj" was truncated to "o" + // (PDFBOX-773). Restore the token when it is clearly an object header: + // preceding "N G" and followed by a dictionary, array, name, or scalar. + private static byte[] normalizeTruncatedObj(byte[] pdf) { + if (pdf == null || pdf.length < 4) return pdf; + ByteArrayOutputStream out = new ByteArrayOutputStream(pdf.length + 8); + boolean changed = false; + boolean inStream = false; + boolean[] guarded = protectedPositions(pdf); + for (int i = 0; i < pdf.length; i++) { + if (!inStream && matchesAscii(pdf, i, "stream") + && (i + 6 == pdf.length || pdf[i + 6] == '\n' || pdf[i + 6] == '\r')) { + inStream = true; + } else if (inStream && matchesAscii(pdf, i, "endstream")) { + inStream = false; + } + if (!inStream && !guarded[i] && pdf[i] == 'o' + && (i == 0 || !isPdfNameByte(pdf[i - 1]))) { + int p = i - 1; + while (p >= 0 && isPdfSpace(pdf[p])) p--; + int genEnd = p; + while (p >= 0 && pdf[p] >= '0' && pdf[p] <= '9') p--; + boolean hasGen = p < genEnd; + while (p >= 0 && isPdfSpace(pdf[p])) p--; + int numEnd = p; + while (p >= 0 && pdf[p] >= '0' && pdf[p] <= '9') p--; + boolean hasNum = p < numEnd; + boolean boundary = p < 0 || isPdfSpace(pdf[p]) || pdf[p] == '>'; + int q = i + 1; + while (q < pdf.length && isPdfSpace(pdf[q])) q++; + boolean bodyStart = q < pdf.length && (pdf[q] == '<' || pdf[q] == '[' || pdf[q] == '/' + || pdf[q] == '(' || pdf[q] == '+' || pdf[q] == '-' + || (pdf[q] >= '0' && pdf[q] <= '9') + || (pdf[q] >= 'A' && pdf[q] <= 'Z')); + boolean alreadyObj = i + 3 <= pdf.length && pdf[i + 1] == 'b' && pdf[i + 2] == 'j'; + if (hasNum && hasGen && boundary && bodyStart && !alreadyObj) { + out.write('o'); + out.write('b'); + out.write('j'); + changed = true; + continue; + } + } + out.write(pdf[i]); + } + return changed ? out.toByteArray() : pdf; + } + + private static boolean matchesAscii(byte[] data, int offset, String token) { + if (offset < 0 || offset + token.length() > data.length) return false; + for (int i = 0; i < token.length(); i++) { + if (data[offset + i] != (byte) token.charAt(i)) return false; + } + return true; + } + + private static boolean isPdfSpace(byte b) { + return b == ' ' || b == '\t' || b == '\n' || b == '\r' || b == '\f' || b == 0; + } + + private static boolean isPdfNameByte(byte b) { + return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') + || (b >= '0' && b <= '9') || b == '_'; + } + + // Some producers emit "endobjN 0 obj" without a separator. qpdf recovers + // the objects but can attach the next object to the previous one, losing a + // page. Split the token before parsing; only touch occurrences that look + // like structure (preceded by '>' or whitespace) and are followed by a + // complete "N G obj" header. + private static byte[] normalizeGluedEndobj(byte[] pdf) { + if (pdf == null || pdf.length < 6) return pdf; + ByteArrayOutputStream out = new ByteArrayOutputStream(pdf.length + 16); + boolean changed = false; + boolean inStream = false; + boolean[] guarded = protectedPositions(pdf); + for (int i = 0; i < pdf.length; i++) { + if (!inStream && matchesAscii(pdf, i, "stream") + && (i + 6 == pdf.length || pdf[i + 6] == '\n' || pdf[i + 6] == '\r')) { + inStream = true; + } else if (inStream && matchesAscii(pdf, i, "endstream")) { + inStream = false; + } + boolean marker = !inStream && !guarded[i] && matchesAscii(pdf, i, "endobj"); + boolean boundary = i == 0 || pdf[i - 1] == '>' || pdf[i - 1] == '\n' + || pdf[i - 1] == '\r' || pdf[i - 1] == ' ' || pdf[i - 1] == '\t'; + if (marker && boundary && i + 7 < pdf.length && pdf[i + 6] >= '0' && pdf[i + 6] <= '9') { + int j = i + 6; + while (j < pdf.length && pdf[j] >= '0' && pdf[j] <= '9') j++; + int k = j; + while (k < pdf.length && (pdf[k] == ' ' || pdf[k] == '\t')) k++; + int gen = k; + while (k < pdf.length && pdf[k] >= '0' && pdf[k] <= '9') k++; + while (k < pdf.length && (pdf[k] == ' ' || pdf[k] == '\t')) k++; + if (gen > j && k + 3 <= pdf.length && pdf[k] == 'o' && pdf[k + 1] == 'b' + && pdf[k + 2] == 'j') { + out.write(pdf, i, 6); + out.write('\n'); + i += 5; + changed = true; + continue; + } + } + out.write(pdf[i]); + } + return changed ? out.toByteArray() : pdf; + } + + // Rewriting a signed document always breaks its /ByteRange digest. A clean + // signed file is returned untouched; a damaged one fails rather than producing + // output whose signature silently no longer verifies. + private static RepairResult signedDocumentResult(byte[] pdf, boolean diagnostics) { + if (pdf == null) return null; + boolean marker = contains(pdf, BYTE_RANGE); + try (PdfDocument doc = PdfDocument.open(pdf)) { + if (doc.pageCount() > 0 && (marker || doc.signatureCount() > 0)) { + return new RepairResult(RepairResult.Status.CLEAN, pdf, + diagnostics ? inspect(pdf) : null); + } + if (!marker) return null; + } catch (Exception _) { + if (!marker) return null; + } + return new RepairResult(RepairResult.Status.FAILED, null, + diagnostics ? "{\"error\":\"signed document cannot be repaired without invalidating its signature\"}" + : null); + } + + private static boolean contains(byte[] haystack, byte[] needle) { + outer: + for (int i = 0; i <= haystack.length - needle.length; i++) { + for (int j = 0; j < needle.length; j++) { + if (haystack[i + j] != needle[j]) continue outer; + } + return true; + } + return false; + } + /** * Builder for configuring PDF repair pipeline options. */ @@ -448,6 +896,332 @@ public PdfRepair build() { } } + private static String balancedDictionary(String chunk, int pageStart) { + int start = enclosingDictionaryStart(chunk, pageStart); + if (start < 0) return null; + int end = dictionaryEnd(chunk, start); + return end < 0 ? null : chunk.substring(start, end); + } + + private static int enclosingDictionaryStart(String s, int limit) { + int[] stack = new int[64]; + int depth = 0; + boolean literal = false; + boolean hex = false; + boolean comment = false; + boolean escape = false; + for (int i = 0; i < limit && i < s.length(); i++) { + char c = s.charAt(i); + char n = i + 1 < s.length() ? s.charAt(i + 1) : '\0'; + if (comment) { + if (c == '\n' || c == '\r') comment = false; + continue; + } + if (literal) { + if (escape) escape = false; + else if (c == '\\') escape = true; + else if (c == ')') literal = false; + continue; + } + if (hex) { + if (c == '>') hex = false; + continue; + } + if (c == '%') comment = true; + else if (c == '(') literal = true; + else if (c == '<' && n == '<') { + if (depth < stack.length) stack[depth] = i; + depth++; + i++; + } else if (c == '>' && n == '>') { + if (depth > 0) depth--; + i++; + } else if (c == '<') { + hex = true; + } + } + return depth > 0 ? stack[depth - 1] : -1; + } + + private static int dictionaryEnd(String s, int start) { + int depth = 0; + boolean literal = false; + boolean hex = false; + boolean comment = false; + boolean escape = false; + for (int i = start; i < s.length(); i++) { + char c = s.charAt(i); + char n = i + 1 < s.length() ? s.charAt(i + 1) : '\0'; + if (comment) { + if (c == '\n' || c == '\r') comment = false; + continue; + } + if (literal) { + if (escape) escape = false; + else if (c == '\\') escape = true; + else if (c == ')') literal = false; + continue; + } + if (hex) { + if (c == '>') hex = false; + continue; + } + if (c == '%') comment = true; + else if (c == '(') literal = true; + else if (c == '<' && n == '<') { + depth++; + i++; + } else if (c == '>' && n == '>') { + depth--; + i++; + if (depth == 0) return i + 1; + } else if (c == '<') { + hex = true; + } + } + return -1; + } + + private static String replaceParent(String dict, long parent) { + java.util.regex.Matcher m = Pattern.compile("/Parent\\s+\\d+\\s+\\d+\\s+R").matcher(dict); + if (m.find()) { + return dict.substring(0, m.start()) + "/Parent " + parent + " 0 R" + dict.substring(m.end()); + } + int start = dict.indexOf("<<"); + return start < 0 ? dict : dict.substring(0, start + 2) + "/Parent " + parent + " 0 R" + + dict.substring(start + 2); + } + + private static java.util.List kidsOrder(String text) { + java.util.List order = new java.util.ArrayList<>(); + Matcher kids = Pattern.compile("/Kids\\s*\\[([^\\]]*)]").matcher(text); + while (kids.find()) { + Matcher ref = Pattern.compile("(\\d+)\\s+\\d+\\s+R").matcher(kids.group(1)); + while (ref.find()) { + String value = ref.group(1) + " 0 R"; + if (!order.contains(value)) order.add(value); + } + } + return order; + } + + private static String objectBody(String text, long number, long generation) { + String marker = "\n" + number + " " + generation + " obj"; + int at = text.indexOf(marker); + if (at < 0) at = text.indexOf(number + " " + generation + " obj"); + if (at < 0) return null; + int end = text.indexOf("endobj", at); + return end < 0 ? null : text.substring(at, end); + } + + private static String valueAfterKey(String dict, String key) { + int at = dict.indexOf(key); + if (at < 0) return null; + int i = at + key.length(); + while (i < dict.length() && Character.isWhitespace(dict.charAt(i))) i++; + if (i >= dict.length()) return null; + if (dict.startsWith("<<", i)) { + int end = dictionaryEnd(dict, i); + return end < 0 ? null : dict.substring(i, end); + } + if (dict.charAt(i) == '[') { + int depth = 0; + for (int j = i; j < dict.length(); j++) { + char c = dict.charAt(j); + if (c == '[') depth++; + else if (c == ']' && --depth == 0) return dict.substring(i, j + 1); + } + return null; + } + int start = i; + while (i < dict.length() && !Character.isWhitespace(dict.charAt(i)) + && dict.charAt(i) != '>' && dict.charAt(i) != ']') i++; + if (i <= start) return null; + int end = i; + // Preserve indirect references (N G R) as one value. + int j = i; + while (j < dict.length() && Character.isWhitespace(dict.charAt(j))) j++; + int genStart = j; + while (j < dict.length() && Character.isDigit(dict.charAt(j))) j++; + if (j > genStart) { + int genEnd = j; + while (j < dict.length() && Character.isWhitespace(dict.charAt(j))) j++; + if (j < dict.length() && dict.charAt(j) == 'R' + && (j + 1 >= dict.length() || !Character.isLetterOrDigit(dict.charAt(j + 1)))) { + return dict.substring(start, j + 1); + } + } + return dict.substring(start, end); + } + + private static String insertEntry(String dict, String key, String value) { + if (dict.contains(key)) return dict; + int parent = dict.indexOf("/Parent"); + int start = dict.indexOf("<<"); + if (start < 0) return dict; + int at = parent >= 0 ? parent : start + 2; + return dict.substring(0, at) + key + " " + value + " " + + dict.substring(at); + } + + private static String inheritPageAttributes(String dict, String text, long number, long generation) { + String current = dict; + String parent = valueAfterKey(dict, "/Parent"); + int guard = 0; + while (parent != null && guard++ < 32) { + Matcher ref = Pattern.compile("(\\d+)\\s+(\\d+)\\s+R").matcher(parent); + if (!ref.find()) break; + long pnum = parseObjectNumber(ref.group(1)); + long pgen = parseObjectNumber(ref.group(2)); + if (pnum < 0 || pgen < 0) break; + String body = objectBody(text, pnum, pgen); + if (body == null) break; + String pageDict = balancedDictionary(body, Math.max(0, body.indexOf("/Type"))); + if (pageDict == null) break; + for (String key : new String[] {"/MediaBox", "/CropBox", "/Resources", "/Rotate"}) { + if (valueAfterKey(current, key) == null) { + String value = valueAfterKey(pageDict, key); + if (value != null) current = insertEntry(current, key, value); + } + } + parent = valueAfterKey(pageDict, "/Parent"); + } + return current; + } + + private static String updateCatalogPages(String text, int[] catalog, long pagesObj) { + String marker = "\n" + catalog[0] + " " + catalog[1] + " obj"; + int start = text.indexOf(marker); + if (start < 0) { + marker = catalog[0] + " " + catalog[1] + " obj"; + start = text.indexOf(marker); + } + if (start < 0) return text; + int end = text.indexOf("endobj", start); + if (end < 0) return text; + String dict = text.substring(start, end); + Matcher pages = Pattern.compile("/Pages\\s+\\d+\\s+\\d+\\s+R").matcher(dict); + if (pages.find()) { + dict = dict.substring(0, pages.start()) + "/Pages " + pagesObj + " 0 R" + + dict.substring(pages.end()); + } else { + int at = dict.indexOf("<<"); + if (at < 0) return text; + dict = dict.substring(0, at + 2) + " /Pages " + pagesObj + " 0 R " + + dict.substring(at + 2); + } + return text.substring(0, start) + dict + text.substring(end); + } + + private static long parseObjectNumber(String raw) { + try { + long value = Long.parseLong(raw); + return value >= 0 && value <= 9_000_000L ? value : -1; + } catch (NumberFormatException _) { + return -1; + } + } + + // Rebuilds a /Pages tree from literal page dictionaries that lost their parent. + // Streams are skipped so /Type /Page text inside content cannot create pages. + private static byte[] rebuildPageTree(byte[] input) { + if (input == null || input.length < 16) return null; + String text = new String(input, StandardCharsets.ISO_8859_1); + Matcher stream = STREAM_BODY.matcher(text); + java.util.List pageStarts = new java.util.ArrayList<>(); + java.util.List pageNumbers = new java.util.ArrayList<>(); + java.util.List refs = new java.util.ArrayList<>(); + java.util.List orphanedPages = new java.util.ArrayList<>(); + int pos = 0; + while (true) { + int from = pos; + int to = stream.find() ? stream.start() : text.length(); + String chunk = text.substring(from, to); + Matcher page = Pattern.compile("/Type\\s*/Page(?![s])").matcher(chunk); + while (page.find()) { + Matcher obj = Pattern.compile("(\\d+)\\s+\\d+\\s+obj").matcher(chunk.substring(0, page.start())); + String number = null; + int start = -1; + int objEnd = -1; + while (obj.find()) { + number = obj.group(1); + start = from + obj.start(); + objEnd = from + obj.end(); + } + boolean owned = number != null && page.start() - (objEnd - from) <= 64; + if (owned && !refs.contains(number + " 0 R")) { + refs.add(number + " 0 R"); + pageStarts.add(start); + pageNumbers.add(parseObjectNumber(number)); + } else if (!owned) { + String orphan = balancedDictionary(chunk, page.start()); + if (orphan != null) orphanedPages.add(orphan); + } + } + if (!stream.find(to)) break; + pos = stream.end(); + } + if (refs.isEmpty() && orphanedPages.isEmpty()) return null; + + long maxObj = 0; + Matcher all = Pattern.compile("(?m)(\\d+)\\s+\\d+\\s+obj").matcher(text); + while (all.find()) { + long value = parseObjectNumber(all.group(1)); + if (value < 0) return null; + maxObj = Math.max(maxObj, value); + } + + java.util.List kids = kidsOrder(text); + refs.sort(java.util.Comparator.comparingInt(r -> { + int index = kids.indexOf(r); + return index < 0 ? Integer.MAX_VALUE : index; + })); + java.util.List orphanObjects = new java.util.ArrayList<>(); + long pagesObj = maxObj + orphanedPages.size() + 1; + for (String orphan : orphanedPages) { + long object = ++maxObj; + String dict = replaceParent(orphan, pagesObj); + orphanObjects.add(object + " 0 obj\n" + dict + "\nendobj\n"); + refs.add(object + " 0 R"); + } + + StringBuilder out = new StringBuilder(text.length() + 512); + if (text.indexOf("%PDF-") < 0) out.append("%PDF-1.4\n"); + int cursor = 0; + for (int i = 0; i < pageStarts.size(); i++) { + int start = pageStarts.get(i); + out.append(text, cursor, start); + int end = text.indexOf("endobj", start); + if (end < 0) end = text.length(); + String dict = text.substring(start, end); + if (pageNumbers.get(i) >= 0) { + dict = inheritPageAttributes(dict, text, pageNumbers.get(i), 0); + } + out.append(replaceParent(dict, pagesObj)); + cursor = end; + } + out.append(text, cursor, text.length()); + for (String orphan : orphanObjects) out.append("\n").append(orphan); + out.append("\n").append(pagesObj).append(" 0 obj\n<< /Type /Pages /Kids [") + .append(String.join(" ", refs)).append("] /Count ").append(refs.size()) + .append(" >>\nendobj\n"); + + int[] catalog = catalogObjectBefore(text, text.length()); + String result; + long rootObj; + if (catalog != null) { + result = updateCatalogPages(out.toString(), catalog, pagesObj); + rootObj = catalog[0]; + } else { + rootObj = maxObj + 2; + result = out.toString() + rootObj + " 0 obj\n<< /Type /Catalog /Pages " + pagesObj + + " 0 R >>\nendobj\n"; + } + result += "trailer\n<< /Root " + rootObj + " 0 R /Size " + + Math.max(maxObj + 1, rootObj + 1) + " >>\nstartxref\n0\n%%EOF\n"; + return result.getBytes(StandardCharsets.ISO_8859_1); + } + private static byte[] salvageTruncatedPdf(byte[] input) { if (input == null || input.length < 16) { return null; @@ -480,10 +1254,49 @@ private static byte[] salvageTruncatedPdf(byte[] input) { return null; } - byte[] eof = {'\n', '%', '%', 'E', 'O', 'F', '\n'}; - byte[] out = new byte[lastEndObj + eof.length]; + String body = new String(input, 0, lastEndObj, StandardCharsets.ISO_8859_1); + int[] root = catalogObjectBefore(body, body.length()); + StringBuilder trailer = new StringBuilder(); + if (root != null) { + int maxObj = 0; + java.util.regex.Matcher m = java.util.regex.Pattern + .compile("(?m)(\\d+)\\s+\\d+\\s+obj") + .matcher(body); + while (m.find()) maxObj = Math.max(maxObj, Integer.parseInt(m.group(1))); + trailer.append("\ntrailer\n<< /Size ").append(Math.max(maxObj + 1, root[0] + 1)) + .append(" /Root ").append(root[0]).append(' ').append(root[1]) + .append(" R >>\nstartxref\n0\n"); + } + byte[] tail = (trailer + "%%EOF\n").getBytes(StandardCharsets.ISO_8859_1); + byte[] out = new byte[lastEndObj + tail.length]; System.arraycopy(input, 0, out, 0, lastEndObj); - System.arraycopy(eof, 0, out, lastEndObj, eof.length); + System.arraycopy(tail, 0, out, lastEndObj, tail.length); return out; } + + /** Finds the object header that owns the last catalog dictionary before {@code limit}. */ + private static int[] catalogObjectBefore(String body, int limit) { + int type = body.lastIndexOf("/Type /Catalog", limit); + if (type < 0) type = body.lastIndexOf("/Type/Catalog", limit); + if (type < 0) return null; + int obj = body.lastIndexOf(" obj", type); + if (obj < 0) return null; + int p = obj - 1; + while (p >= 0 && isPdfSpace((byte) body.charAt(p))) p--; + int genEnd = p; + while (p >= 0 && Character.isDigit(body.charAt(p))) p--; + if (p == genEnd) return null; + int genStart = p + 1; + while (p >= 0 && isPdfSpace((byte) body.charAt(p))) p--; + int numEnd = p; + while (p >= 0 && Character.isDigit(body.charAt(p))) p--; + if (p == numEnd) return null; + if (p >= 0 && !isPdfSpace((byte) body.charAt(p)) && body.charAt(p) != '>') return null; + try { + return new int[] {Integer.parseInt(body.substring(p + 1, numEnd + 1)), + Integer.parseInt(body.substring(genStart, genEnd + 1))}; + } catch (NumberFormatException _) { + return null; + } + } } diff --git a/native/bridge/include/jpdfium_font_internal.h b/native/bridge/include/jpdfium_font_internal.h index e4c4d80e2..df6341d03 100644 --- a/native/bridge/include/jpdfium_font_internal.h +++ b/native/bridge/include/jpdfium_font_internal.h @@ -53,6 +53,16 @@ void sanitizeSfntForSubset(std::string& sfnt); // preserving BMP (3,1) Format 4 and non-BMP (3,10) Format 12 subtables // (promoting (0,3)/(0,4) Unicode subtables cleanly when needed). void ensureUnicodeMicrosoftCmap(std::string& sfnt); +// Reads the number of cmap subtables and whether (platform, encoding) exists. +int sfntCmapSubtableCount(std::string_view sfnt); +bool sfntHasCmapSubtable(std::string_view sfnt, uint16_t platform, uint16_t encoding); + +// Rebuilds the SFNT with a single Format 4 cmap subtable for the given +// platform/encoding and codepoint->GID mapping. Existing tables are preserved +// byte-for-byte; only cmap and the directory are replaced. +bool ensureSfntCmapSubtable(std::string& sfnt, uint16_t platform, uint16_t encoding, + std::map const& mapping); + uint32_t decodeUnicodeScalar(uint32_t packedOrScalar); std::vector decodeUtf16BeHex(std::string_view digits); diff --git a/native/bridge/src/jpdfium_font_normalizer.cpp b/native/bridge/src/jpdfium_font_normalizer.cpp index 7cc6e5720..214a4cc25 100644 --- a/native/bridge/src/jpdfium_font_normalizer.cpp +++ b/native/bridge/src/jpdfium_font_normalizer.cpp @@ -3,6 +3,7 @@ // subsetting + CFF extraction, and /CIDSet / /CharSet conformance updates. #include +#include #include #include #include @@ -509,14 +510,12 @@ static void repairVerticalCidFontWidths(QPDF& pdf, QPDFObjectHandle& font, QPDFO static void repairCidFontWidths(QPDF& pdf, QPDFObjectHandle& font, QPDFObjectHandle& target, FT_Face face, QPDFObjectHandle& cidToGid, bool cidIdentity, std::map const& gidToCid, - FontNormalizeState& state) { + FontNormalizeState& state, bool full) { auto cidGid = buildCidToGidTable(face, cidToGid, cidIdentity, gidToCid); if (cidGid.empty()) return; - if (font.getKey("/Encoding").isNameAndEquals("/Identity-V")) { - repairVerticalCidFontWidths(pdf, font, target, face, cidGid, state); - return; - } + const bool vertical = font.getKey("/Encoding").isNameAndEquals("/Identity-V"); + if (vertical) repairVerticalCidFontWidths(pdf, font, target, face, cidGid, state); QPDFObjectHandle existingW = target.getKey("/W"); bool widthsComplete = true; @@ -525,9 +524,21 @@ static void repairCidFontWidths(QPDF& pdf, QPDFObjectHandle& font, QPDFObjectHan CidScanResult scan = usedCidsForFont(pdf, font, CidEncoding::TwoByte, &ensureUsageIndexes(pdf, state)); - if (!scan.complete || scan.cids.empty()) return; + if (!full && (!scan.complete || scan.cids.empty())) return; - std::set check = scan.cids; + std::set check; + if (scan.complete) check = scan.cids; + if (full) { + // PDF/A checks every /W entry, not only the CIDs the current content + // draws. Rewrite unused producer entries too so none contradict the + // embedded program. Content that fails to scan must not block this: + // the existing /W keys still need repair. + for (auto const& [cid, width] : widths) { + (void)width; + check.insert(cid); + } + if (check.empty()) return; + } if (widths.empty()) { for (auto const& [cid, gid] : cidGid) { int width = 0; @@ -783,10 +794,135 @@ static std::set cidsPresentInProgram(FT_Face face, QPDFObjectHandle& c return cids; } +// Producer subsets sometimes keep the original cmap after truncating the glyph +// table, so cmap entries point past num_glyphs. veraPDF derives the CID set +// from those entries, and no bitmap can then match. PDF/A only constrains a +// CIDSet when one is present, so such fonts must drop it. +static bool cmapFitsProgram(std::shared_ptr const& data, FT_Face face) { + if (!data || !face || data->getSize() < 12) return true; + auto const* b = reinterpret_cast(data->getBuffer()); + const size_t n = data->getSize(); + const bool sfnt = std::memcmp(b, "\0\1\0\0", 4) == 0 || std::memcmp(b, "true", 4) == 0 || + std::memcmp(b, "OTTO", 4) == 0; + if (!sfnt) return true; + const uint16_t tables = readU16BE(b + 4); + if (n < 12 + static_cast(tables) * 16) return true; + uint32_t cmapOff = 0; + uint32_t cmapLen = 0; + for (uint16_t i = 0; i < tables; ++i) { + const size_t e = 12 + static_cast(i) * 16; + if (std::memcmp(b + e, "cmap", 4) == 0) { + cmapOff = readU32BE(b + e + 8); + cmapLen = readU32BE(b + e + 12); + } + } + // A TrueType CID subset without a cmap has no reliable glyph list for the + // validator; drop the optional CIDSet instead of guessing. + if (!cmapOff || cmapOff > n || cmapLen > n - cmapOff || cmapLen < 4) return false; + const uint16_t subs = readU16BE(b + cmapOff + 2); + const uint32_t maxGid = static_cast(face->num_glyphs); + for (uint16_t i = 0; i < subs; ++i) { + const size_t rec = cmapOff + 4 + static_cast(i) * 8; + if (rec + 8 > n) return true; + const uint32_t rel = readU32BE(b + rec + 4); + if (rel > cmapLen || cmapLen - rel < 2) continue; + const size_t off = cmapOff + rel; + const uint16_t fmt = readU16BE(b + off); + if (fmt == 4 && cmapLen - rel >= 16) { + const uint16_t segX2 = readU16BE(b + off + 6); + const size_t segs = segX2 / 2; + const size_t ends = off + 14; + const size_t starts = ends + segX2 + 2; + const size_t deltas = starts + segX2; + const size_t ranges = deltas + segX2; + if (ranges + segX2 > cmapOff + cmapLen) continue; + for (size_t k = 0; k < segs; ++k) { + const uint16_t end = readU16BE(b + ends + k * 2); + const uint16_t start = readU16BE(b + starts + k * 2); + const uint16_t delta = readU16BE(b + deltas + k * 2); + const uint16_t range = readU16BE(b + ranges + k * 2); + if (start > end) continue; + for (uint32_t c = start; c <= end; ++c) { + uint32_t gid = 0; + if (range == 0) { + gid = (c + delta) & 0xFFFFU; + } else { + const size_t ro = ranges + k * 2 + range + (c - start) * 2; + if (ro + 2 > cmapOff + cmapLen) continue; + const uint16_t raw = readU16BE(b + ro); + if (raw == 0) continue; + gid = (raw + delta) & 0xFFFFU; + } + if (gid != 0 && gid >= maxGid) return false; + } + } + } else if (fmt == 12 && cmapLen - rel >= 16) { + const uint32_t groups = readU32BE(b + off + 12); + const size_t base = off + 16; + if (base + static_cast(groups) * 12 > cmapOff + cmapLen) continue; + for (uint32_t g = 0; g < groups; ++g) { + const size_t p = base + static_cast(g) * 12; + const uint32_t last = readU32BE(b + p + 4); + const uint32_t first = readU32BE(b + p + 8); + const uint32_t startGid = readU32BE(b + p + 12); + (void)last; + if (first > last) continue; + if (startGid + (last - first) >= maxGid) return false; + } + } else if (fmt == 6 && cmapLen - rel >= 10) { + const uint16_t first = readU16BE(b + off + 6); + const uint16_t count = readU16BE(b + off + 8); + if (off + 10 + static_cast(count) * 2 > cmapOff + cmapLen) continue; + for (uint16_t k = 0; k < count; ++k) { + const uint16_t gid = readU16BE(b + off + 10 + k * 2); + if (gid != 0 && gid >= maxGid) return false; + } + (void)first; + } + } + return true; +} + +// A broken subset cmap can map characters to glyph indices past num_glyphs. +// No CIDSet can describe that program consistently, and ISO 19005 only applies +// 6.2.11.4.2-2 when a CIDSet is present, so drop it for such fonts. +static bool cidSetCanDescribeProgram(FT_Face face) { + if (!face || face->num_charmaps == 0) return true; + FT_CharMap original = face->charmap; + bool reliable = true; + for (int i = 0; i < face->num_charmaps && reliable; ++i) { + if (FT_Set_Charmap(face, face->charmaps[i]) != 0) continue; + FT_UInt gid = 0; + FT_ULong ch = FT_Get_First_Char(face, &gid); + while (gid != 0) { + if (gid >= static_cast(face->num_glyphs)) { + reliable = false; + break; + } + ch = FT_Get_Next_Char(face, ch, &gid); + } + } + if (original) FT_Set_Charmap(face, original); + return reliable; +} + static void writeCidSetIfDifferent(QPDF& pdf, QPDFObjectHandle& descriptor, std::set cids, - FontNormalizeState& state) { - if (cids.empty()) return; - cids.insert(0); + FT_Face face, FontNormalizeState& state) { + if (!cidSetCanDescribeProgram(face)) { + if (descriptor.hasKey("/CIDSet")) { + descriptor.removeKey("/CIDSet"); + state.changed = true; + } + return; + } + cids.erase(0); + if (cids.empty()) { + if (descriptor.hasKey("/CIDSet")) { + descriptor.removeKey("/CIDSet"); + state.changed = true; + } + return; + } const std::string bits = buildCidSetBits(cids); bool differs = true; QPDFObjectHandle existing = descriptor.getKey("/CIDSet"); @@ -864,6 +1000,141 @@ static QPDFObjectHandle buildDifferencesArray(std::map const& return newDiffs; } +static std::string cyrillicAglName(std::string const& rawName) { + static const std::pair kNames[] = { + {"A", 10017}, {"Be", 10018}, {"Ve", 10019}, {"Ge", 10020}, + {"Ghe", 10020}, {"De", 10021}, {"Ie", 10022}, {"Io", 10023}, + {"Zhe", 10024}, {"Ze", 10025}, {"Ii", 10026}, {"Iishort", 10027}, + {"Ka", 10028}, {"El", 10029}, {"Em", 10030}, {"En", 10031}, + {"O", 10032}, {"Pe", 10033}, {"Er", 10034}, {"Es", 10035}, + {"Te", 10036}, {"U", 10037}, {"Ef", 10038}, {"Kha", 10039}, + {"Tse", 10040}, {"Che", 10041}, {"Sha", 10042}, {"Shcha", 10043}, + {"Hardsign", 10044}, {"Yeri", 10045}, {"Yeru", 10045}, {"Softsign", 10046}, + {"E", 10047}, {"IU", 10048}, {"IA", 10049}, + }; + if (rawName.size() <= 8 || rawName.compare(rawName.size() - 8, 8, "cyrillic") != 0) return {}; + std::string base = rawName.substr(0, rawName.size() - 8); + if (base.empty()) return {}; + const bool lower = base[0] >= 'a' && base[0] <= 'z'; + if (lower) base[0] = static_cast(base[0] - 'a' + 'A'); + for (auto const& [name, upper] : kNames) { + if (base == name) { + const uint32_t value = upper + (lower ? 48U : 0U); + return "afii" + std::to_string(value); + } + } + return {}; +} + +static std::string aglNameForUniName(std::string const& gname) { + // Multi-scalar /uniXXXXYYYY names must stay intact; only a single four-digit + // scalar has an unambiguous afii equivalent. + if (gname.rfind("/uni", 0) != 0 || gname.size() != 8) return {}; + uint32_t cp = 0; + for (size_t i = 4; i < 8; ++i) { + const char c = gname[i]; + cp <<= 4; + if (c >= '0' && c <= '9') + cp |= static_cast(c - '0'); + else if (c >= 'A' && c <= 'F') + cp |= static_cast(c - 'A' + 10); + else if (c >= 'a' && c <= 'f') + cp |= static_cast(c - 'a' + 10); + else + return {}; + } + uint32_t afii = 0; + if (cp == 0x0401) + afii = 10023; + else if (cp == 0x0451) + afii = 10071; + else if (cp >= 0x0410 && cp <= 0x0415) + afii = 10017 + (cp - 0x0410); + else if (cp == 0x0416) + afii = 10024; + else if (cp >= 0x0417 && cp <= 0x042F) + afii = 10025 + (cp - 0x0417); + else if (cp >= 0x0430 && cp <= 0x0435) + afii = 10065 + (cp - 0x0430); + else if (cp == 0x0436) + afii = 10072; + else if (cp >= 0x0437 && cp <= 0x044F) + afii = 10073 + (cp - 0x0437); + else + return {}; + return "afii" + std::to_string(afii); +} + +static bool isSymbolicFamilyName(QPDFObjectHandle const& font, QPDFObjectHandle const& descriptor) { + QPDFObjectHandle name = font.getKey("/BaseFont"); + if (!name.isName()) name = descriptor.getKey("/FontName"); + if (!name.isName()) return false; + std::string value = name.getName(); + std::transform(value.begin(), value.end(), value.begin(), + [](unsigned char c) { return static_cast(std::tolower(c)); }); + return value.find("symbol") != std::string::npos || + value.find("dingbat") != std::string::npos || + value.find("wingding") != std::string::npos || + value.find("webding") != std::string::npos; +} + +static void setSymbolicFlag(QPDFObjectHandle& descriptor, bool symbolic, + FontNormalizeState& state) { + QPDFObjectHandle flagsObj = descriptor.getKey("/Flags"); + long long flags = flagsObj.isInteger() ? flagsObj.getIntValue() : 0LL; + long long updated = symbolic ? ((flags | 4LL) & ~32LL) : ((flags & ~4LL) | 32LL); + if (updated != flags) { + descriptor.replaceKey("/Flags", QPDFObjectHandle::newInteger(updated)); + state.changed = true; + } +} + +static void repairSimpleTrueTypeCmap(QPDF& pdf, QPDFObjectHandle& font, + QPDFObjectHandle& descriptor, + std::shared_ptr const& data, FT_Face face, + FontNormalizeState& state) { + QPDFObjectHandle flagsObj = descriptor.getKey("/Flags"); + const bool symbolic = flagsObj.isInteger() && (flagsObj.getIntValue() & 4) != 0; + const std::string bytes(reinterpret_cast(data->getBuffer()), data->getSize()); + if (symbolic) { + if (font.hasKey("/Encoding")) { + font.removeKey("/Encoding"); + state.changed = true; + } + if (sfntCmapSubtableCount(bytes) == 1 || sfntHasCmapSubtable(bytes, 3, 0)) return; + std::map identity; + for (uint32_t code = 0; code < 256 && code < static_cast(face->num_glyphs); + ++code) + identity[code] = static_cast(code); + std::string updated = bytes; + if (!ensureSfntCmapSubtable(updated, 3, 0, identity)) return; + descriptor.replaceKey("/FontFile2", QPDFObjectHandle::newStream(&pdf, updated)); + state.changed = true; + return; + } + // ISO 19005-2 6.2.11.6-2 requires a Microsoft Unicode (3,1) cmap when a + // non-symbolic font defines Differences; (0,3)/(1,0) alone is not enough. + if (sfntHasCmapSubtable(bytes, 3, 1) || sfntHasCmapSubtable(bytes, 3, 10)) return; + std::map unicodeByGid; + char glyphName[256]; + if (FT_HAS_GLYPH_NAMES(face)) { + for (FT_UInt gid = 1; gid < static_cast(face->num_glyphs); ++gid) { + if (FT_Get_Glyph_Name(face, gid, glyphName, sizeof(glyphName)) != 0 || + glyphName[0] == '\0') + continue; + for (uint32_t scalar : unicodeSequenceForGlyphName(glyphName)) { + if (scalar <= 0xFFFF && !unicodeByGid.contains(scalar)) + unicodeByGid[scalar] = static_cast(gid); + } + } + } + if (unicodeByGid.empty()) return; + std::string updated = bytes; + if (!ensureSfntCmapSubtable(updated, 3, 1, unicodeByGid)) return; + descriptor.replaceKey("/FontFile2", QPDFObjectHandle::newStream(&pdf, updated)); + state.changed = true; +} + static void normalizeSimpleTrueTypeEncoding(QPDFObjectHandle& font, QPDFObjectHandle& descriptor, FT_Face face, bool hasUnicodeCharmap, bool isType1Family, @@ -872,6 +1143,14 @@ static void normalizeSimpleTrueTypeEncoding(QPDFObjectHandle& font, QPDFObjectHa std::vector& simpleGlyphs, bool& simpleAnyGid, FontNormalizeState& state) { bool encodingMutated = false; + if (isSymbolicFamilyName(font, descriptor)) { + setSymbolicFlag(descriptor, true, state); + if (font.hasKey("/Encoding")) { + font.removeKey("/Encoding"); + state.changed = true; + encodingMutated = true; + } + } QPDFObjectHandle flagsObj = descriptor.getKey("/Flags"); long long flags = flagsObj.isInteger() ? flagsObj.getIntValue() : 0LL; if ((flags & 4) != 0) { @@ -937,13 +1216,65 @@ static void normalizeSimpleTrueTypeEncoding(QPDFObjectHandle& font, QPDFObjectHa std::string gname = item.getName(); if (gname == "/.null") gname = "/.notdef"; const bool redundantControl = curCode < 32 && gname == "/.notdef"; - std::string const rawName = gname.size() > 1 ? gname.substr(1) : ""; + std::string cleanedName = gname; + if (gname.rfind("/uni", 0) == 0) { + uint32_t cp = 0; + bool hex = gname.size() >= 8; + for (size_t i = 4; hex && i < 8; ++i) { + const char c = gname[i]; + cp <<= 4; + if (c >= '0' && c <= '9') + cp |= c - '0'; + else if (c >= 'A' && c <= 'F') + cp |= c - 'A' + 10; + else if (c >= 'a' && c <= 'f') + cp |= c - 'a' + 10; + else + hex = false; + } + if (hex && (cp <= 0x1F || (cp >= 0x7F && cp <= 0x9F))) { + cleanedName = "/.notdef"; + } else { + const std::string agl = aglNameForUniName(gname); + if (!agl.empty()) cleanedName = "/" + agl; + } + } else if (gname.rfind("/control", 0) == 0) { + cleanedName = "/.notdef"; + } else if (gname.rfind("/u", 0) == 0 && gname.size() >= 6) { + uint32_t cp = 0; + bool hex = true; + for (size_t i = 2; i < 6; ++i) { + const char c = gname[i]; + cp <<= 4; + if (c >= '0' && c <= '9') + cp |= c - '0'; + else if (c >= 'A' && c <= 'F') + cp |= c - 'A' + 10; + else if (c >= 'a' && c <= 'f') + cp |= c - 'a' + 10; + else { + hex = false; + break; + } + } + if (hex && (cp <= 0x1F || (cp >= 0x7F && cp <= 0x9F))) + cleanedName = "/.notdef"; + } + if (cleanedName.size() > 1) { + const std::string agl = cyrillicAglName(cleanedName.substr(1)); + if (!agl.empty()) cleanedName = "/" + agl; + } + std::string const rawName = + cleanedName.size() > 1 ? cleanedName.substr(1) : ""; + const bool algorithmicName = + rawName.size() >= 4 && + (rawName.rfind("uni", 0) == 0 || rawName[0] == 'u'); const bool validGlyph = - !rawName.empty() && (isAdobeGlyphListMember(rawName) || - !unicodeSequenceForGlyphName(rawName).empty()); + cleanedName != "/.notdef" && !rawName.empty() && + (isAdobeGlyphListMember(rawName) || algorithmicName); if (curCode <= 255) { if (!redundantControl && validGlyph) { - cleanDiffs[curCode] = gname; + cleanDiffs[curCode] = cleanedName; } else { cleanDiffs.erase(curCode); } @@ -982,6 +1313,148 @@ static void normalizeSimpleTrueTypeEncoding(QPDFObjectHandle& font, QPDFObjectHa } } +static void repairType1Lengths(QPDF& pdf, QPDFObjectHandle& descriptor, + std::shared_ptr const& data, FontNormalizeState& state) { + QPDFObjectHandle file = descriptor.getKey("/FontFile"); + if (!file.isStream() || !data || data->getSize() < 8) return; + auto const* b = reinterpret_cast(data->getBuffer()); + const size_t n = data->getSize(); + long long length1 = 0; + long long length2 = 0; + long long length3 = 0; + bool known = false; + if (b[0] == 0x80) { + // PFB segment headers carry the exact clear, binary and trailer lengths. + size_t i = 0; + while (i + 6 <= n && b[i] == 0x80) { + const uint8_t type = b[i + 1]; + const size_t len = size_t(b[i + 2]) | size_t(b[i + 3]) << 8 | size_t(b[i + 4]) << 16 | + size_t(b[i + 5]) << 24; + if (i + 6 + len > n) return; + if (type == 1) + length1 = static_cast(len); + else if (type == 2) + length2 = static_cast(len); + else if (type == 3) + length3 = static_cast(len); + else + break; + i += 6 + len; + } + known = length1 > 0 && length2 > 0; + } else { + std::string_view text(reinterpret_cast(b), n); + const size_t eexec = text.find("eexec"); + const size_t clear = text.rfind("cleartomark"); + if (eexec != std::string_view::npos && clear != std::string_view::npos && clear > eexec) { + size_t start = text.find_first_not_of(" \t\r\n", eexec + 5); + size_t tail = clear; + while (tail > start && + (text[tail - 1] == '0' || text[tail - 1] == '\n' || text[tail - 1] == '\r' || + text[tail - 1] == ' ' || text[tail - 1] == '\t')) + --tail; + if (start != std::string_view::npos && start < tail) { + length1 = static_cast(start); + length2 = static_cast(tail - start); + length3 = static_cast(n - tail); + known = true; + } + } + } + if (!known) return; + QPDFObjectHandle dict = file.getDict(); + bool differs = false; + auto check = [&](char const* key, long long value) { + QPDFObjectHandle cur = dict.getKey(key); + if (!cur.isInteger() || cur.getIntValue() != value) differs = true; + }; + check("/Length1", length1); + check("/Length2", length2); + check("/Length3", length3); + if (!differs) return; + + std::string bytes(reinterpret_cast(data->getBuffer()), data->getSize()); + QPDFObjectHandle stream = QPDFObjectHandle::newStream(&pdf, bytes); + QPDFObjectHandle newDict = stream.getDict(); + newDict.replaceKey("/Length1", QPDFObjectHandle::newInteger(length1)); + newDict.replaceKey("/Length2", QPDFObjectHandle::newInteger(length2)); + newDict.replaceKey("/Length3", QPDFObjectHandle::newInteger(length3)); + descriptor.replaceKey("/FontFile", stream); + state.changed = true; +} + +// ISO 19005-2 6.2.11.3.1: CIDSystemInfo in the CIDFont must match the CMap. +// Registry/Ordering are strings; a mismatched supplement must be <= the CMap's. +static void fixCidSystemInfo(QPDFObjectHandle& font, QPDFObjectHandle& target, + FontNormalizeState& state) { + QPDFObjectHandle enc = font.getKey("/Encoding"); + QPDFObjectHandle cur = target.getKey("/CIDSystemInfo"); + if (!cur.isDictionary()) { + cur = QPDFObjectHandle::newDictionary(); + target.replaceKey("/CIDSystemInfo", cur); + state.changed = true; + } + auto setString = [&](char const* key, std::string const& value) { + QPDFObjectHandle v = cur.getKey(key); + bool same = false; + if (v.isString()) { + try { + same = v.getStringValue() == value; + } catch (...) { + } + } + if (!same) { + cur.replaceKey(key, QPDFObjectHandle::newString(value)); + state.changed = true; + } + }; + auto setInt = [&](char const* key, int value) { + QPDFObjectHandle v = cur.getKey(key); + if (!v.isInteger() || v.getIntValue() != value) { + cur.replaceKey(key, QPDFObjectHandle::newInteger(value)); + state.changed = true; + } + }; + if (enc.isStream()) { + QPDFObjectHandle src = enc.getKey("/CIDSystemInfo"); + if (src.isDictionary()) { + QPDFObjectHandle r = src.getKey("/Registry"); + QPDFObjectHandle o = src.getKey("/Ordering"); + QPDFObjectHandle sup = src.getKey("/Supplement"); + if (r.isString()) setString("/Registry", r.getStringValue()); + if (o.isString()) setString("/Ordering", o.getStringValue()); + if (sup.isInteger()) setInt("/Supplement", static_cast(sup.getIntValue())); + } + return; + } + if (!enc.isName()) return; + std::string name = enc.getName(); + if (name == "/Identity-H" || name == "/Identity-V") { + setString("/Registry", "Adobe"); + setString("/Ordering", "Identity"); + setInt("/Supplement", 0); + return; + } + std::string ordering; + if (name.find("UniKS") != std::string::npos || name.find("KSC") != std::string::npos) + ordering = "Korea1"; + else if (name.find("UniGB") != std::string::npos || name.find("GBK") != std::string::npos || + name.find("GB-") != std::string::npos) + ordering = "GB1"; + else if (name.find("UniCNS") != std::string::npos || name.find("B5") != std::string::npos) + ordering = "CNS1"; + else if (name.find("UniJIS") != std::string::npos || name.find("90ms") != std::string::npos || + name.find("90pv") != std::string::npos || name.find("83pv") != std::string::npos || + name.find("EUC") != std::string::npos || name.find("RKSJ") != std::string::npos || + name == "/H" || name == "/V") + ordering = "Japan1"; + else + return; + setString("/Registry", "Adobe"); + setString("/Ordering", ordering); + setInt("/Supplement", 0); +} + static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool doFixToUnicode, bool doRepairWidths, FontUsageCounts const& usage, FontNormalizeState& state) { @@ -999,10 +1472,20 @@ static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool if (!descendants.isArray() || descendants.getArrayNItems() < 1) return; target = descendants.getArrayItem(0); if (!target.isDictionary()) return; + fixCidSystemInfo(font, target, state); QPDFObjectHandle cidEncoding = font.getKey("/Encoding"); if (!cidEncoding.isName() || (!cidEncoding.isNameAndEquals("/Identity-H") && - !cidEncoding.isNameAndEquals("/Identity-V"))) + !cidEncoding.isNameAndEquals("/Identity-V"))) { + // Custom and predefined CMaps are out of scope for width/ToUnicode + // repair. Their CIDSet is still optional per ISO 19005, and a stale + // one is worse than none, so drop it before skipping the font. + QPDFObjectHandle desc = target.getKey("/FontDescriptor"); + if (desc.isDictionary() && desc.hasKey("/CIDSet")) { + desc.removeKey("/CIDSet"); + state.changed = true; + } return; + } } QPDFObjectHandle descriptor = target.getKey("/FontDescriptor"); @@ -1041,6 +1524,9 @@ static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool FtFaceGuard faceGuard(rawFace); if (rawFace->num_glyphs <= 0) return; ++state.fontsProcessed; + if (!cid && (subtypeName == "/Type1" || subtypeName == "/MMType1")) { + repairType1Lengths(pdf, descriptor, data, state); + } const bool hasUnicodeCharmap = FT_Select_Charmap(rawFace, FT_ENCODING_UNICODE) == 0; std::map gidToUnicode; @@ -1079,6 +1565,10 @@ static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool gidToCid[static_cast(gid)] = static_cast(cidValue); } cidIdentity = true; + } else if (cidSubtype.isNameAndEquals("/CIDFontType0")) { + // A non-CID-keyed CFF has no charset table: PDF 32000-1 + // 9.7.4.2 says its CIDs are the glyph indices directly. + cidIdentity = true; } else { if (!cidSubtype.isNameAndEquals("/CIDFontType2")) return; cidIdentity = true; @@ -1127,7 +1617,8 @@ static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool repairSimpleFontWidths(target, rawFace, simpleEncoding, simpleAnyGid, simpleGlyphs, state, full); } else { - repairCidFontWidths(pdf, font, target, rawFace, cidToGid, cidIdentity, gidToCid, state); + repairCidFontWidths(pdf, font, target, rawFace, cidToGid, cidIdentity, gidToCid, state, + full); } } @@ -1165,8 +1656,17 @@ static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool } catch (...) { } } - writeCidSetIfDifferent(pdf, descriptor, - cidsPresentInProgram(cidSetFace, cidToGid, cidIdentity), state); + const bool cffCid = target.getKey("/Subtype").isNameAndEquals("/CIDFontType0"); + if (cffCid || (!didResubset && !cmapFitsProgram(data, cidSetFace))) { + if (descriptor.hasKey("/CIDSet")) { + descriptor.removeKey("/CIDSet"); + state.changed = true; + } + } else { + writeCidSetIfDifferent(pdf, descriptor, + cidsPresentInProgram(cidSetFace, cidToGid, cidIdentity), + cidSetFace, state); + } } // Rebuild /CharSet for Type 1/CFF subsets when every retained glyph has a PostScript name. @@ -1202,6 +1702,10 @@ static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool state.changed = true; } } + + if (!cid && full && subtypeName == "/TrueType") { + repairSimpleTrueTypeCmap(pdf, font, descriptor, data, rawFace, state); + } } } // namespace @@ -1265,12 +1769,17 @@ int32_t fontNormalizeQpdf(DocWrapper* w, int32_t pageIndex, bool doFixToUnicode, } FontNormalizeState state; + std::set processedFonts; auto runFont = [&](QPDFObjectHandle& font) { + const int32_t before = state.fontsProcessed; try { processFont(pdf, ft.lib, font, doFixToUnicode, doRepairWidths, usage, state); } catch (...) { return; } + // Only fonts that passed program parsing are considered inspected. + // Early rejects keep their producer CIDSet and are cleaned below. + if (state.fontsProcessed > before) processedFonts.insert(font.getObjGen()); }; if (pageIndex == -1) { @@ -1284,6 +1793,50 @@ int32_t fontNormalizeQpdf(DocWrapper* w, int32_t pageIndex, bool doFixToUnicode, collectResourceFonts(pdf.getRoot().getKey("/AcroForm").getKey("/DR"), 0, seenResources, seenFonts, formStreams, formFonts); for (auto& font : formFonts) runFont(font); + // Fonts reachable only from appearance streams or orphaned resources + // still need encoding/cmap repair; mark them shared so they are not + // re-subsetted, then run the normalizer on them. + for (auto& obj : pdf.getAllObjects()) { + if (!obj.isDictionary()) continue; + QPDFObjectHandle subtype = obj.getKey("/Subtype"); + if (!subtype.isName() || subtype.getName() != "/TrueType") continue; + QPDFObjectHandle descriptor = obj.getKey("/FontDescriptor"); + QPDFObjectHandle flagsObj = + descriptor.isDictionary() ? descriptor.getKey("/Flags") : QPDFObjectHandle(); + const bool symbolic = flagsObj.isInteger() && (flagsObj.getIntValue() & 4) != 0; + if (isSymbolicFamilyName(obj, descriptor)) { + setSymbolicFlag(descriptor, true, state); + if (obj.hasKey("/Encoding")) { + obj.removeKey("/Encoding"); + state.changed = true; + } + } else if (symbolic && obj.hasKey("/Encoding")) { + obj.removeKey("/Encoding"); + state.changed = true; + } + if (processedFonts.contains(obj.getObjGen())) continue; + markFormFontNotSubsettable(obj, usage); + runFont(obj); + } + // Type0 fonts reachable only from nested appearance/XObject + // resources still need width/ToUnicode repair. Process them without + // re-subsetting, then drop any CIDSet we could not inspect. + for (auto& obj : pdf.getAllObjects()) { + if (!obj.isDictionary()) continue; + QPDFObjectHandle subtype = obj.getKey("/Subtype"); + if (!subtype.isName() || subtype.getName() != "/Type0") continue; + if (processedFonts.contains(obj.getObjGen())) continue; + markFormFontNotSubsettable(obj, usage); + runFont(obj); + if (processedFonts.contains(obj.getObjGen())) continue; + QPDFObjectHandle descendants = obj.getKey("/DescendantFonts"); + if (!descendants.isArray() || descendants.getArrayNItems() < 1) continue; + QPDFObjectHandle descriptor = descendants.getArrayItem(0).getKey("/FontDescriptor"); + if (descriptor.isDictionary() && descriptor.hasKey("/CIDSet")) { + descriptor.removeKey("/CIDSet"); + state.changed = true; + } + } } else { std::set seenResources; std::set seenFonts; diff --git a/native/bridge/src/jpdfium_font_shared.cpp b/native/bridge/src/jpdfium_font_shared.cpp index ebe1204bf..be7d611d6 100644 --- a/native/bridge/src/jpdfium_font_shared.cpp +++ b/native/bridge/src/jpdfium_font_shared.cpp @@ -247,6 +247,194 @@ void sanitizeSfntForSubset(std::string& sfnt) { } } +namespace { + +bool locateCmapTable(std::string_view sfnt, uint32_t& cmapOffset, uint32_t& cmapLength) { + if (sfnt.size() < 12) return false; + auto const* bytes = reinterpret_cast(sfnt.data()); + const uint16_t numTables = readU16BE(bytes + 4); + if (sfnt.size() < 12 + static_cast(numTables) * 16) return false; + for (uint16_t i = 0; i < numTables; ++i) { + size_t entry = 12 + static_cast(i) * 16; + if (std::memcmp(bytes + entry, "cmap", 4) == 0) { + cmapOffset = readU32BE(bytes + entry + 8); + cmapLength = readU32BE(bytes + entry + 12); + return cmapOffset <= sfnt.size() && cmapLength <= sfnt.size() - cmapOffset && + cmapLength >= 4; + } + } + return false; +} + +std::string buildFormat4Cmap(std::map const& mapping) { + std::vector> entries; + entries.reserve(mapping.size()); + for (auto const& [cp, gid] : mapping) { + if (cp <= 0xFFFE && gid != 0) entries.emplace_back(cp, gid); + } + const size_t segCount = entries.size() + 1; + const size_t length = 16 + 8 * segCount; + // Format 4 stores its length in a 16-bit field. Refuse to emit a wrapped + // value that would make the subtable unreadable. + if (length > 0xFFFFU) return {}; + std::string table(length, '\0'); + auto* p = reinterpret_cast(table.data()); + writeU16BE(p, 4); + writeU16BE(p + 2, static_cast(length)); + writeU16BE(p + 4, 0); + writeU16BE(p + 6, static_cast(segCount * 2)); + size_t searchRange = 2; + size_t entrySelector = 0; + while (searchRange * 2 <= segCount * 2) { + searchRange *= 2; + ++entrySelector; + } + writeU16BE(p + 8, static_cast(searchRange)); + writeU16BE(p + 10, static_cast(entrySelector)); + writeU16BE(p + 12, static_cast(segCount * 2 - searchRange)); + size_t endCodes = 14; + size_t startCodes = endCodes + segCount * 2 + 2; + size_t deltas = startCodes + segCount * 2; + size_t ranges = deltas + segCount * 2; + for (size_t i = 0; i < entries.size(); ++i) { + const uint32_t cp = entries[i].first; + const uint16_t gid = entries[i].second; + writeU16BE(p + endCodes + i * 2, static_cast(cp)); + writeU16BE(p + startCodes + i * 2, static_cast(cp)); + writeU16BE(p + deltas + i * 2, static_cast(gid - cp)); + writeU16BE(p + ranges + i * 2, 0); + } + writeU16BE(p + endCodes + entries.size() * 2, 0xFFFF); + writeU16BE(p + startCodes + entries.size() * 2, 0xFFFF); + writeU16BE(p + deltas + entries.size() * 2, 1); + writeU16BE(p + ranges + entries.size() * 2, 0); + return table; +} + +struct SfntTable { + char tag[4]; + std::string data; +}; + +} // namespace + +int sfntCmapSubtableCount(std::string_view sfnt) { + uint32_t off = 0; + uint32_t len = 0; + if (!locateCmapTable(sfnt, off, len) || len < 4) return 0; + return readU16BE(reinterpret_cast(sfnt.data()) + off + 2); +} + +bool sfntHasCmapSubtable(std::string_view sfnt, uint16_t platform, uint16_t encoding) { + uint32_t off = 0; + uint32_t len = 0; + if (!locateCmapTable(sfnt, off, len) || len < 4) return false; + auto const* b = reinterpret_cast(sfnt.data()); + const uint16_t count = readU16BE(b + off + 2); + if (static_cast(count) * 8 + 4 > len) return false; + for (uint16_t i = 0; i < count; ++i) { + const size_t rec = off + 4 + static_cast(i) * 8; + if (readU16BE(b + rec) == platform && readU16BE(b + rec + 2) == encoding) return true; + } + return false; +} + +bool ensureSfntCmapSubtable(std::string& sfnt, uint16_t platform, uint16_t encoding, + std::map const& mapping) { + if (sfnt.size() < 12) return false; + auto* header = reinterpret_cast(sfnt.data()); + const uint16_t numTables = readU16BE(header + 4); + if (sfnt.size() < 12 + static_cast(numTables) * 16) return false; + + std::vector tables; + tables.reserve(numTables + 1); + for (uint16_t i = 0; i < numTables; ++i) { + const size_t entry = 12 + static_cast(i) * 16; + const uint32_t off = readU32BE(header + entry + 8); + const uint32_t len = readU32BE(header + entry + 12); + if (off > sfnt.size() || len > sfnt.size() - off) return false; + SfntTable t{}; + std::memcpy(t.tag, header + entry, 4); + t.data.assign(sfnt.data() + off, len); + tables.push_back(std::move(t)); + } + + const std::string subtable = buildFormat4Cmap(mapping); + if (subtable.empty()) return false; + std::string cmap; + cmap.resize(12 + subtable.size()); + auto* c = reinterpret_cast(cmap.data()); + writeU16BE(c, 0); + writeU16BE(c + 2, 1); + writeU16BE(c + 4, platform); + writeU16BE(c + 6, encoding); + writeU32BE(c + 8, 12); + std::memcpy(c + 12, subtable.data(), subtable.size()); + + bool replaced = false; + for (auto& t : tables) { + if (std::memcmp(t.tag, "cmap", 4) == 0) { + t.data = cmap; + replaced = true; + } + } + if (!replaced) { + SfntTable t{}; + std::memcpy(t.tag, "cmap", 4); + t.data = cmap; + tables.push_back(std::move(t)); + } + std::sort(tables.begin(), tables.end(), [](SfntTable const& a, SfntTable const& b) { + return std::memcmp(a.tag, b.tag, 4) < 0; + }); + + const size_t n = tables.size(); + const size_t dataStart = 12 + n * 16; + std::string out; + std::vector offsets(n); + size_t total = dataStart; + for (size_t i = 0; i < n; ++i) { + offsets[i] = static_cast(total); + total += (tables[i].data.size() + 3) & ~static_cast(3); + } + out.assign(total, '\0'); + auto* o = reinterpret_cast(out.data()); + std::memcpy(o, sfnt.data(), 4); + writeU16BE(o + 4, static_cast(n)); + size_t searchRange = 16; + size_t entrySelector = 0; + while (searchRange * 2 <= n * 16) { + searchRange *= 2; + ++entrySelector; + } + writeU16BE(o + 6, static_cast(searchRange)); + writeU16BE(o + 8, static_cast(entrySelector)); + writeU16BE(o + 10, static_cast(n * 16 - searchRange)); + + uint32_t headOffset = 0; + for (size_t i = 0; i < n; ++i) { + const size_t entry = 12 + i * 16; + std::memcpy(o + entry, tables[i].tag, 4); + writeU32BE(o + entry + 8, offsets[i]); + writeU32BE(o + entry + 12, static_cast(tables[i].data.size())); + if (std::memcmp(tables[i].tag, "head", 4) == 0) headOffset = offsets[i]; + if (!tables[i].data.empty()) + std::memcpy(o + offsets[i], tables[i].data.data(), tables[i].data.size()); + } + if (headOffset && headOffset + 12 <= out.size()) writeU32BE(o + headOffset + 8, 0); + for (size_t i = 0; i < n; ++i) { + const size_t entry = 12 + i * 16; + const size_t padded = (tables[i].data.size() + 3) & ~static_cast(3); + writeU32BE(o + entry + 4, calcSfntTableChecksum(o + offsets[i], padded)); + } + if (headOffset && headOffset + 12 <= out.size()) { + const uint32_t sum = calcSfntTableChecksum(o, out.size()); + writeU32BE(o + headOffset + 8, 0xB1B0AFBAU - sum); + } + sfnt.swap(out); + return true; +} + void ensureUnicodeMicrosoftCmap(std::string& sfnt) { if (sfnt.size() < 12) return; auto* bytes = reinterpret_cast(sfnt.data()); @@ -864,7 +1052,10 @@ std::string buildCidSetBits(std::set const& cids) { if (maxCid > kMaxCid) maxCid = kMaxCid; std::string bits(static_cast(maxCid / 8U) + 1U, '\0'); for (uint32_t cid : cids) { - if (cid > kMaxCid) continue; + // CID 0 is .notdef, not a CID present in the subset. ISO 19005 + // validators compare the CIDSet bits with the program's glyph set and + // reject an extra bit for .notdef. + if (cid == 0 || cid > kMaxCid) continue; size_t byteIndex = cid / 8U; uint8_t bitMask = static_cast(0x80U >> (cid & 7U)); bits[byteIndex] = static_cast(static_cast(bits[byteIndex]) | bitMask); diff --git a/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp b/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp index 92d14c840..a0a079780 100644 --- a/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp +++ b/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp @@ -115,10 +115,13 @@ void applyWidths(QPDFObjectHandle& font, QPDFObjectHandle const& widths) { font.replaceKey("/Widths", widths); } +bool hasInheritedToUnicode(QPDFObjectHandle const& font); + void setToUnicode(QPDF& qpdf, QPDFObjectHandle font, std::map const& codes, bool twoByte) { if (codes.empty()) return; - if (font.hasKey("/ToUnicode") && !parseToUnicode(font).empty()) return; + if (font.hasKey("/ToUnicode") && (hasInheritedToUnicode(font) || !parseToUnicode(font).empty())) + return; std::map clean; for (auto const& [code, u] : codes) { if (u > 0 && u <= 0x10FFFFU) clean.emplace(code, static_cast(u)); @@ -130,7 +133,8 @@ void setToUnicode(QPDF& qpdf, QPDFObjectHandle font, std::map const& codes, bool rksj) { if (codes.empty()) return; - if (font.hasKey("/ToUnicode") && !parseToUnicode(font).empty()) return; + if (font.hasKey("/ToUnicode") && (hasInheritedToUnicode(font) || !parseToUnicode(font).empty())) + return; std::map clean; for (auto const& [code, u] : codes) { if (code <= 0xFFFFU && u > 0 && u <= 0x10FFFFU) @@ -524,6 +528,11 @@ void collectEmbedCandidateFonts(QPDF& qpdf, std::vector& fonts if (acroForm.isDictionary()) collectFromResources(acroForm.getKey("/DR")); } +bool hasInheritedToUnicode(QPDFObjectHandle const& font) { + auto toUnicode = font.getKey("/ToUnicode"); + return toUnicode.isStream() && !toUnicode.getDict().getKey("/UseCMap").isNull(); +} + bool prepareMissingCidMapping(QPDF& qpdf, QPDFObjectHandle& font, QPDFObjectHandle& target, FontProgram const& program, bool indexedUsage, FontUsageIndexes const& usage, FontEmbedJobState& job) { @@ -539,6 +548,9 @@ bool prepareMissingCidMapping(QPDF& qpdf, QPDFObjectHandle& font, QPDFObjectHand bool korea1Ksc = isKscEucCidEncoding(predefinedEnc); bool cjkVertical = isVerticalCidEncoding(predefinedEnc); + // An inherited ToUnicode map can describe CIDs the local stream does not list; + // rebuilding a CIDToGIDMap from the local map alone would drop those glyphs. + if (hasInheritedToUnicode(font)) return false; // Rebuilding /CIDToGIDMap is only safe for Identity CID maps; custom maps stay verbatim. if (!job.identityCidMap) return false; if (job.predefinedCjk) { @@ -658,7 +670,12 @@ void populateEmbeddedFontProgram(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& f // Subsetting TrueType CID fonts renumbers retained GIDs, requiring a fresh /CIDToGIDMap. std::map remappedGlyph; std::set targetGlyphs; - if (cid && glyf && (job.identityEncoding || job.predefinedCjk) && job.identityCidMap) { + bool identityCidMap = job.identityCidMap || target.getKey("/CIDToGIDMap").isNull() || + target.getKey("/CIDToGIDMap").isNameAndEquals("/Identity"); + bool identityEncoding = job.identityEncoding || + font.getKey("/Encoding").isNameAndEquals("/Identity-H") || + font.getKey("/Encoding").isNameAndEquals("/Identity-V"); + if (cid && glyf && (identityEncoding || job.predefinedCjk) && identityCidMap) { std::map codes; if (job.predefinedCjk && !job.codeUnicode.empty()) { for (auto const& [code, unicode] : job.codeUnicode) codes[code] = unicode; diff --git a/native/bridge/src/jpdfium_qpdf_fonts_subset.cpp b/native/bridge/src/jpdfium_qpdf_fonts_subset.cpp index 0843de6b5..ea7a9a418 100644 --- a/native/bridge/src/jpdfium_qpdf_fonts_subset.cpp +++ b/native/bridge/src/jpdfium_qpdf_fonts_subset.cpp @@ -26,7 +26,10 @@ using jpdfium_qpdf_internal::WriterLock; void updateCidSet(QPDF& qpdf, QPDFObjectHandle descriptor, std::set const& activeCids) { if (!descriptor.isDictionary()) return; std::string bits = jpdfium::font::buildCidSetBits(activeCids); - if (!bits.empty()) descriptor.replaceKey("/CIDSet", QPDFObjectHandle::newStream(&qpdf, bits)); + if (!bits.empty()) + descriptor.replaceKey("/CIDSet", QPDFObjectHandle::newStream(&qpdf, bits)); + else + descriptor.removeKey("/CIDSet"); } std::set cidsPresentInProgram(QPDFObjectHandle const& target, FT_Face face) { @@ -928,17 +931,36 @@ void appendFontInventoryEntryJson(FontInventoryEntry const& entry, std::string& return nullptr; } }; - auto readableProgram = [&](QPDFObjectHandle const& desc) { - if (!desc.isDictionary()) return false; + auto validProgram = [](std::shared_ptr const& data) { + if (!data || data->getSize() < 4) return false; + auto const* b = reinterpret_cast(data->getBuffer()); + if (b[0] == 0x00 && b[1] == 0x01 && b[2] == 0x00 && b[3] == 0x00) return true; + if (b[0] == 0x01 && b[1] == 0x00 && b[2] == 0x04 && b[3] == 0x04) return true; + if ((b[0] == 'O' && b[1] == 'T' && b[2] == 'T' && b[3] == 'O') || + (b[0] == 't' && b[1] == 'r' && b[2] == 'u' && b[3] == 'e') || + (b[0] == 't' && b[1] == 't' && b[2] == 'c' && b[3] == 'f')) + return true; + if (b[0] == '%' && b[1] == '!') return true; + return (b[0] & 0xFF) == 0x80 && b[1] == 0x01; + }; + auto programState = [&](QPDFObjectHandle const& desc) { + bool present = false; + bool valid = false; + if (!desc.isDictionary()) return std::pair{false, false}; for (char const* key : {"/FontFile", "/FontFile2", "/FontFile3"}) { auto data = tryReadStream(desc.getKey(key)); - if (data && data->getSize() > 0) return true; + if (!data || data->getSize() == 0) continue; + present = true; + if (validProgram(data)) valid = true; } - return false; + return std::pair{present, valid}; }; // Inspect both wrapper and descendant descriptors for embedded /FontFile* and /ToUnicode // streams. - bool hasProgram = readableProgram(descriptor) || readableProgram(targetDescriptor); + auto wrapperProgram = programState(descriptor); + auto descendantProgram = programState(targetDescriptor); + bool hasProgram = wrapperProgram.first || descendantProgram.first; + bool programValid = wrapperProgram.second || descendantProgram.second; auto toUnicodeData = tryReadStream(font.getKey("/ToUnicode")); bool hasToUnicode = toUnicodeData && toUnicodeData->getSize() > 0; @@ -977,6 +999,8 @@ void appendFontInventoryEntryJson(FontInventoryEntry const& entry, std::string& json += boolJson(descriptor.isDictionary() || targetDescriptor.isDictionary()); json += ",\"program\":"; json += boolJson(hasProgram); + json += ",\"programValid\":"; + json += boolJson(programValid); json += ",\"cid\":"; json += boolJson(cid); json += ",\"toUnicode\":"; diff --git a/native/bridge/src/jpdfium_repair.cpp b/native/bridge/src/jpdfium_repair.cpp index f05fb2929..a92f396ea 100644 --- a/native/bridge/src/jpdfium_repair.cpp +++ b/native/bridge/src/jpdfium_repair.cpp @@ -846,7 +846,7 @@ JPDFIUM_EXPORT int32_t jpdfium_repair_inspect(const uint8_t* input, int64_t inpu os << "\"trailer_valid\":true,"; os << "\"issues\":" << warnings_to_json(warnings); - } catch (QPDFExc& e) { + } catch (std::exception& e) { std::string msg = e.what(); std::string escaped; escaped.reserve(msg.size()); From 4dabdf078e298de00408769be287b009f11d8579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Wed, 18 Nov 2026 02:41:16 +0100 Subject: [PATCH 03/11] Fix cmap scan bounds and incomplete visibility handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../stirling/software/jpdfium/doc/PdfFontAuditor.java | 11 ++++++++--- .../software/jpdfium/doc/PdfFontEmbedder.java | 4 ++-- native/bridge/src/jpdfium_font_normalizer.cpp | 7 +++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java index b6299fe9f..409a21788 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java @@ -587,7 +587,9 @@ public static Set visibleFontKeys(PdfDocument doc) { MemorySegment raw = page.rawHandleForRead(); int annotations = (int) AnnotationBindings.FPDFPage_GetAnnotCount.invokeExact(raw); - if (annotations > 0) return Set.of(); + // Annotation appearance streams are not walked here, so the + // visible set would be incomplete. + if (annotations > 0) return null; int count = (int) PageEditBindings.FPDFPage_CountObjects.invokeExact(raw); for (int j = 0; j < count; j++) { MemorySegment obj = @@ -596,7 +598,9 @@ public static Set visibleFontKeys(PdfDocument doc) { } } catch (Throwable t) { NativeRuntime.rethrowFatal(t); - // A page that cannot be walked tells us nothing; do not hide fonts. + // A page that cannot be walked tells us nothing; signal an + // incomplete result instead of hiding fonts. + return null; } } return keys; @@ -688,7 +692,8 @@ private static byte[] getFontProgram(MemorySegment font) { if (len <= 0) return null; return buf.asSlice(0, len).toArray(ValueLayout.JAVA_BYTE); } - } catch (Throwable _) { + } catch (Throwable t) { + NativeRuntime.rethrowFatal(t); return null; } } diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java index 27fbb934d..ffc40b169 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java @@ -307,7 +307,7 @@ public static Result embedMissing(byte[] pdf, boolean stripSignatures, Path... f } List missing = List.copyOf(names); if (missing.isEmpty()) return new Result(pdf, List.of(), List.of(), List.of()); - Set visibleFonts = Set.of(); + Set visibleFonts = null; Map> usedCodepoints = Map.of(); try (PdfDocument doc = PdfDocument.open(pdf)) { visibleFonts = PdfFontAuditor.visibleFontKeys(doc); @@ -322,7 +322,7 @@ public static Result embedMissing(byte[] pdf, boolean stripSignatures, Path... f Map operationPrograms = new HashMap<>(); List statuses = new ArrayList<>(missing.size()); for (String name : missing) { - if (!visibleFonts.isEmpty() && !visibleFonts.contains(key(name)) + if (visibleFonts != null && !visibleFonts.isEmpty() && !visibleFonts.contains(key(name)) && !resourceOnly.contains(name)) { statuses.add(new FontStatus(name, "", -1, false, "font is only used in invisible text (Tr 3)")); diff --git a/native/bridge/src/jpdfium_font_normalizer.cpp b/native/bridge/src/jpdfium_font_normalizer.cpp index 214a4cc25..8c9566610 100644 --- a/native/bridge/src/jpdfium_font_normalizer.cpp +++ b/native/bridge/src/jpdfium_font_normalizer.cpp @@ -862,12 +862,11 @@ static bool cmapFitsProgram(std::shared_ptr const& data, FT_Face face) { if (base + static_cast(groups) * 12 > cmapOff + cmapLen) continue; for (uint32_t g = 0; g < groups; ++g) { const size_t p = base + static_cast(g) * 12; + const uint32_t first = readU32BE(b + p); const uint32_t last = readU32BE(b + p + 4); - const uint32_t first = readU32BE(b + p + 8); - const uint32_t startGid = readU32BE(b + p + 12); - (void)last; + const uint32_t startGid = readU32BE(b + p + 8); if (first > last) continue; - if (startGid + (last - first) >= maxGid) return false; + if (static_cast(startGid) + (last - first) >= maxGid) return false; } } else if (fmt == 6 && cmapLen - rel >= 10) { const uint16_t first = readU16BE(b + off + 6); From 17921c24111759c2188a209ce28b48ff2418d060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Wed, 18 Nov 2026 11:46:39 +0100 Subject: [PATCH 04/11] Format font internal header and document unfixed chrono advisory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- native/bridge/include/jpdfium_font_internal.h | 1 - osv-scanner.toml | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 osv-scanner.toml diff --git a/native/bridge/include/jpdfium_font_internal.h b/native/bridge/include/jpdfium_font_internal.h index df6341d03..2493f94f6 100644 --- a/native/bridge/include/jpdfium_font_internal.h +++ b/native/bridge/include/jpdfium_font_internal.h @@ -63,7 +63,6 @@ bool sfntHasCmapSubtable(std::string_view sfnt, uint16_t platform, uint16_t enco bool ensureSfntCmapSubtable(std::string& sfnt, uint16_t platform, uint16_t encoding, std::map const& mapping); - uint32_t decodeUnicodeScalar(uint32_t packedOrScalar); std::vector decodeUtf16BeHex(std::string_view digits); void appendUtf16BeHex(std::string& out, uint32_t codepoint); diff --git a/osv-scanner.toml b/osv-scanner.toml new file mode 100644 index 000000000..6dbbb5bfa --- /dev/null +++ b/osv-scanner.toml @@ -0,0 +1,3 @@ +[[IgnoredVulns]] +id = "RUSTSEC-2026-0543" +reason = "chrono 0.4.45 advisory has no fixed release yet; tracked upstream." From ca4d92701d74f8c8b6d5a6bf11adb1f13af60957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Wed, 18 Nov 2026 11:58:11 +0100 Subject: [PATCH 05/11] Pass the OSV ignore config explicitly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .github/workflows/security.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index f356b061f..6f657a3dc 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -77,7 +77,7 @@ jobs: - uses: actions/checkout@f548e57e544e1ff5a4c46bf1e1b8685f8e4a348a # v7.0.1 - uses: google/osv-scanner-action/osv-scanner-action@f4cfcc01edc9c8b756a9b873b7a623ca674da51e # v2.5.0 with: - scan-args: --recursive ./ + scan-args: --config=osv-scanner.toml --recursive ./ scorecard: name: OpenSSF Scorecard From 7be339483e5a5b36233f67130bc9460ba8b5bc6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Wed, 18 Nov 2026 12:37:00 +0100 Subject: [PATCH 06/11] Align CIDSet test with .notdef exclusion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../stirling/software/jpdfium/doc/PdfFontEmbedderTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java index c3ce723f4..a01394021 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java @@ -848,8 +848,8 @@ private static void assertCidRemap(String toUnicodeBody) throws Exception { cidSetBytes = in.readAllBytes(); } assertTrue(cidSetBytes.length > 0, "/CIDSet must not be empty"); - assertEquals(0x80, cidSetBytes[0] & 0x80, - "CID 0 (.notdef) must be marked in /CIDSet"); + assertEquals(0, cidSetBytes[0] & 0x80, + "CID 0 (.notdef) must stay clear in /CIDSet"); assertEquals(0x80 >> (1 & 7), cidSetBytes[1 / 8] & (0x80 >> (1 & 7)), "CID 1 must be marked in /CIDSet"); } From 853966b2223358d3ed3b5ab621d54afeac2120b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Wed, 18 Nov 2026 15:25:10 +0100 Subject: [PATCH 07/11] Harden the sanitize ToUnicode parser against hostile CMaps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../software/jpdfium/doc/PdfFontEmbedder.java | 30 +++- .../software/jpdfium/doc/PdfRepair.java | 113 +++++++++--- .../jpdfium/PdfPageRedactNativeTest.java | 118 ++++++++++++ .../jpdfium/doc/PdfFontEmbedderTest.java | 124 +++++++++++++ .../software/jpdfium/doc/PdfRepairTest.java | 143 +++++++++++++++ .../jpdfium/fonts/FontNormalizerTest.java | 18 +- native/bridge/src/jpdfium_font_normalizer.cpp | 104 +++++++++-- .../bridge/src/jpdfium_qpdf_fonts_embed.cpp | 27 ++- native/bridge/src/jpdfium_sanitize.cpp | 169 ++++++++++-------- 9 files changed, 727 insertions(+), 119 deletions(-) diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java index ffc40b169..91787669c 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java @@ -291,6 +291,12 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti * signed documents can have missing fonts embedded. */ public static Result embedMissing(byte[] pdf, boolean stripSignatures, Path... fontDirs) throws IOException { + return embedMissingCore(pdf, stripSignatures, false, fontDirs); + } + + private static Result embedMissingCore( + byte[] pdf, boolean stripSignatures, boolean includeInvisibleText, Path... fontDirs) + throws IOException { if (pdf == null || pdf.length == 0) { return new Result(pdf == null ? new byte[0] : pdf, List.of(), List.of(), List.of()); } @@ -322,7 +328,11 @@ public static Result embedMissing(byte[] pdf, boolean stripSignatures, Path... f Map operationPrograms = new HashMap<>(); List statuses = new ArrayList<>(missing.size()); for (String name : missing) { - if (visibleFonts != null && !visibleFonts.isEmpty() && !visibleFonts.contains(key(name)) + // Best-effort embedding may skip fonts whose text is entirely invisible + // (render mode 3). The strict path must not: PDF/A-2 6.2.11.5 requires + // invisible-text fonts to carry an embedded program too. + if (!includeInvisibleText && visibleFonts != null && !visibleFonts.isEmpty() + && !visibleFonts.contains(key(name)) && !resourceOnly.contains(name)) { statuses.add(new FontStatus(name, "", -1, false, "font is only used in invisible text (Tr 3)")); @@ -452,14 +462,16 @@ public static Result embedMissingStrict(byte[] pdf, Path... fontDirs) throws IOE /** * Embed missing fonts, optionally stripping signatures, and reject any result that still has - * an unembedded font. + * an unembedded font. Fonts used only by invisible text (render mode 3) are embedded too: + * PDF/A-2 6.2.11.5 requires them to carry a font program, so the strict path never skips + * them the way the best-effort {@link #embedMissing(byte[], boolean, Path...)} entry does. */ public static Result embedMissingStrict(byte[] pdf, boolean stripSignatures, Path... fontDirs) throws IOException { if (!QpdfLib.isFontInventorySupported()) { throw new JPDFiumException( "strict font embedding requires the native font inventory"); } - return embedMissing(pdf, stripSignatures, fontDirs).requireAllEmbedded(); + return embedMissingCore(pdf, stripSignatures, true, fontDirs).requireAllEmbedded(); } /** @@ -771,6 +783,18 @@ private static Map> usedCodepoints(PdfDocument doc) { if (ch.unicode() <= 0 || ch.fontName() == null || ch.fontName().isEmpty()) { continue; } + // PDFium reports raw character codes for glyphs without a + // Unicode mapping and U+FFFD when a ToUnicode entry is + // missing entirely. Requiring a substitute to "cover" those + // would reject every candidate (no fallback font carries a + // C0/C1 control glyph), so control characters and the + // replacement marker are excluded from the required set. + // The original font never renders them through a real glyph + // mapping either, so dropping them cannot change the text. + if (ch.unicode() <= 0x1F || (ch.unicode() >= 0x7F && ch.unicode() <= 0x9F) + || ch.unicode() == 0xFFFD) { + continue; + } result.computeIfAbsent(key(ch.fontName()), k -> new HashSet<>()) .add(ch.unicode()); } diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java index 0a030ef08..3ed69459b 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java @@ -460,11 +460,31 @@ private static boolean[] protectedPositions(byte[] pdf) { boolean comment = false; boolean hex = false; boolean stream = false; + // Streams whose declared /Length leads to an exact "endstream" landing + // end at that position: the data may legitimately contain the bytes + // "endstream" (embedded PDFs, image streams), and an early exit would + // leave the rest of the data unguarded so the normalizers below could + // edit content-stream bytes. Without a resolvable /Length the scan + // falls back to the first "endstream" at a token boundary. + int endstreamAt = -1; for (int i = 0; i < pdf.length; i++) { byte b = pdf[i]; if (stream) { guarded[i] = true; - if (matchesAscii(pdf, i, "endstream")) { + if (endstreamAt >= 0) { + if (i == endstreamAt) { + // Consume the closing keyword explicitly so its "stream" + // suffix cannot re-enter the stream state. + for (int k = i + 1; k < i + 9; k++) guarded[k] = true; + i += 8; + stream = false; + endstreamAt = -1; + } + continue; + } + if (matchesAscii(pdf, i, "endstream") && isTokenStart(pdf, i)) { + for (int k = i + 1; k < i + 9; k++) guarded[k] = true; + i += 8; stream = false; } continue; @@ -502,15 +522,64 @@ private static boolean[] protectedPositions(byte[] pdf) { && (i == 0 || pdf[i - 1] != '<')) { guarded[i] = true; hex = true; - } else if (!stream && matchesAscii(pdf, i, "stream") + } else if (matchesAscii(pdf, i, "stream") && isTokenStart(pdf, i) && (i + 6 == pdf.length || pdf[i + 6] == '\n' || pdf[i + 6] == '\r')) { guarded[i] = true; stream = true; + endstreamAt = resolveStreamEnd(pdf, i); } } return guarded; } + /** + * Resolves where the "endstream" keyword that closes the stream whose + * "stream" token starts at {@code streamTokenStart} must sit, using the + * dictionary's declared {@code /Length}. Returns {@code -1} when no exact + * landing can be established (missing, indirect, or damaged /Length, a + * length that overshoots the file, or "endstream" not precisely where the + * length points); the caller then falls back to scanning for an anchored + * "endstream". Self-validating: the resolved position is only returned + * when the keyword really is there. + */ + private static int resolveStreamEnd(byte[] pdf, int streamTokenStart) { + int windowStart = Math.max(0, streamTokenStart - 1024); + for (int p = streamTokenStart - 1; p >= windowStart; p--) { + if (!matchesAscii(pdf, p, "/Length")) continue; + int q = p + 7; + while (q < pdf.length && isPdfSpace(pdf[q])) q++; + int numStart = q; + long len = 0; + while (q < pdf.length && pdf[q] >= '0' && pdf[q] <= '9') { + len = len * 10 + (pdf[q] - '0'); + if (len > pdf.length) return -1; + q++; + } + if (q == numStart) return -1; // indirect reference or missing value + int j = streamTokenStart + 6; + if (j + 1 < pdf.length && pdf[j] == '\r' && pdf[j + 1] == '\n') j += 2; + else if (j < pdf.length && (pdf[j] == '\n' || pdf[j] == '\r')) j += 1; + if (j + len >= pdf.length) return -1; + int k = (int) (j + len); + if (k + 1 < pdf.length && pdf[k] == '\r' && pdf[k + 1] == '\n') k += 2; + else if (k < pdf.length && (pdf[k] == '\n' || pdf[k] == '\r')) k += 1; + if (k + 9 > pdf.length) return -1; + return matchesAscii(pdf, k, "endstream") ? k : -1; + } + return -1; + } + + // A keyword occurrence is only a PDF token when preceded by whitespace or + // a delimiter; otherwise it is part of a longer word (e.g. the "stream" + // inside "endstream" must not re-enter the stream state). + private static boolean isTokenStart(byte[] pdf, int i) { + if (i == 0) return true; + byte prev = pdf[i - 1]; + return isPdfSpace(prev) || prev == '(' || prev == ')' || prev == '<' || prev == '>' + || prev == '[' || prev == ']' || prev == '{' || prev == '}' || prev == '/' + || prev == '%'; + } + private static byte[] normalizeRepairTokens(byte[] pdf) { return deduplicateResourceKeys(normalizeTruncatedObj(normalizeGluedEndobj(pdf))); } @@ -574,7 +643,9 @@ private static String deduplicateChunk(String chunk, boolean[] guarded, int base if (base + m.start() < guarded.length && guarded[base + m.start()]) continue; starts.add(m.start()); ends.add(m.end()); - groups.add(depth > 0 ? stack[depth - 1] : -1); + // Hostile input can nest deeper than the tracking window; report the + // innermost tracked level instead of throwing. + groups.add(depth > 0 ? stack[Math.min(depth - 1, stack.length - 1)] : -1); keys.add(m.group(1)); } Map> lastByGroup = new LinkedHashMap<>(); @@ -616,16 +687,12 @@ private static byte[] normalizeTruncatedObj(byte[] pdf) { if (pdf == null || pdf.length < 4) return pdf; ByteArrayOutputStream out = new ByteArrayOutputStream(pdf.length + 8); boolean changed = false; - boolean inStream = false; + // protectedPositions() is the single stream/lexical guard: bytes it + // marks (streams, strings, comments, hex strings) must never be + // rewritten as object syntax. boolean[] guarded = protectedPositions(pdf); for (int i = 0; i < pdf.length; i++) { - if (!inStream && matchesAscii(pdf, i, "stream") - && (i + 6 == pdf.length || pdf[i + 6] == '\n' || pdf[i + 6] == '\r')) { - inStream = true; - } else if (inStream && matchesAscii(pdf, i, "endstream")) { - inStream = false; - } - if (!inStream && !guarded[i] && pdf[i] == 'o' + if (!guarded[i] && pdf[i] == 'o' && (i == 0 || !isPdfNameByte(pdf[i - 1]))) { int p = i - 1; while (p >= 0 && isPdfSpace(pdf[p])) p--; @@ -683,16 +750,12 @@ private static byte[] normalizeGluedEndobj(byte[] pdf) { if (pdf == null || pdf.length < 6) return pdf; ByteArrayOutputStream out = new ByteArrayOutputStream(pdf.length + 16); boolean changed = false; - boolean inStream = false; + // protectedPositions() is the single stream/lexical guard: bytes it + // marks (streams, strings, comments, hex strings) must never be + // rewritten as object syntax. boolean[] guarded = protectedPositions(pdf); for (int i = 0; i < pdf.length; i++) { - if (!inStream && matchesAscii(pdf, i, "stream") - && (i + 6 == pdf.length || pdf[i + 6] == '\n' || pdf[i + 6] == '\r')) { - inStream = true; - } else if (inStream && matchesAscii(pdf, i, "endstream")) { - inStream = false; - } - boolean marker = !inStream && !guarded[i] && matchesAscii(pdf, i, "endobj"); + boolean marker = !guarded[i] && matchesAscii(pdf, i, "endobj"); boolean boundary = i == 0 || pdf[i - 1] == '>' || pdf[i - 1] == '\n' || pdf[i - 1] == '\r' || pdf[i - 1] == ' ' || pdf[i - 1] == '\t'; if (marker && boundary && i + 7 < pdf.length && pdf[i + 6] >= '0' && pdf[i + 6] <= '9') { @@ -940,7 +1003,9 @@ else if (c == '<' && n == '<') { hex = true; } } - return depth > 0 ? stack[depth - 1] : -1; + // Hostile input can nest deeper than the tracking window; report the + // innermost tracked level instead of throwing. + return depth > 0 ? stack[Math.min(depth - 1, stack.length - 1)] : -1; } private static int dictionaryEnd(String s, int start) { @@ -1262,7 +1327,13 @@ private static byte[] salvageTruncatedPdf(byte[] input) { java.util.regex.Matcher m = java.util.regex.Pattern .compile("(?m)(\\d+)\\s+\\d+\\s+obj") .matcher(body); - while (m.find()) maxObj = Math.max(maxObj, Integer.parseInt(m.group(1))); + // parseObjectNumber caps accepted object numbers at 9,000,000 everywhere + // else; honor the same fail-closed bound here instead of letting a + // hostile 10+ digit token throw NumberFormatException out of execute(). + while (m.find()) { + long parsed = parseObjectNumber(m.group(1)); + if (parsed > maxObj) maxObj = (int) parsed; + } trailer.append("\ntrailer\n<< /Size ").append(Math.max(maxObj + 1, root[0] + 1)) .append(" /Root ").append(root[0]).append(' ').append(root[1]) .append(" R >>\nstartxref\n0\n"); diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/PdfPageRedactNativeTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/PdfPageRedactNativeTest.java index d6e39306b..fb422b076 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/PdfPageRedactNativeTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/PdfPageRedactNativeTest.java @@ -1,11 +1,18 @@ package stirling.software.jpdfium; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.junit.jupiter.api.Test; import stirling.software.jpdfium.doc.AnnotationType; import stirling.software.jpdfium.doc.PdfAnnotationBuilder; import stirling.software.jpdfium.model.Rect; import stirling.software.jpdfium.panama.NativeRuntime; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -95,4 +102,115 @@ void testStubModeGracefulDegradation() throws Exception { } } } + + /** + * A Type0 font whose ToUnicode CMap puts TWO bfchar entries on ONE line + * (spec-nonconforming but common in the wild). Redacting one drawn code + * must rebuild the CMap from the surviving entry: the old line-index + * parser silently dropped the second one-line entry, so the surviving + * glyph lost its Unicode mapping during redaction. + */ + @Test + void redactionKeepsToUnicodeEntriesSharingALine() throws Exception { + assumeTrue(NativeRuntime.isFull(), "redaction ToUnicode rebuild requires real PDFium native library"); + byte[] pdf = type0PdfWithTwoBfcharsOnOneLine(); + byte[] saved; + try (PdfDocument doc = PdfDocument.open(pdf)) { + // The opt-in sanitize stage is what rebuilds /ToUnicode from the + // parsed entries; without it the original stream passes through. + doc.setSanitizeOnSave(true); + try (PdfPage page = doc.page(0)) { + String before = page.extractText(); + assertTrue(before.contains("A") && before.contains("B"), + "both glyphs must map through ToUnicode: " + before); + // Redact only the left glyph ("A" at x=72); "B" at x=200 survives. + assertTrue(page.redactInRect(new Rect(60, 680, 100, 40))); + String after = page.extractText(); + assertFalse(after.contains("A"), "redacted glyph must be gone: " + after); + assertTrue(after.contains("B"), + "the surviving glyph must keep its rebuilt ToUnicode mapping: " + after); + } + saved = doc.saveBytes(); + } + assertNotNull(saved); + String pdfBoxText = PdfVerifier.pageText(saved, 0, "partial redact doc"); + assertFalse(pdfBoxText.contains("A")); + assertTrue(pdfBoxText.contains("B"), + "saved document must keep the surviving glyph's text mapping"); + } + + /** Builds a single page drawing two Identity-H CIDs (1 -> 'A' at x=72, + * 2 -> 'B' at x=200) whose ToUnicode CMap declares both bfchar entries on + * one shared line, as broken producers in the wild do. */ + private static byte[] type0PdfWithTwoBfcharsOnOneLine() throws IOException { + try (PDDocument pdf = new PDDocument()) { + PDPage page = new PDPage(PDRectangle.LETTER); + pdf.addPage(page); + org.apache.pdfbox.cos.COSDictionary descendant = new org.apache.pdfbox.cos.COSDictionary(); + descendant.setItem(org.apache.pdfbox.cos.COSName.TYPE, + org.apache.pdfbox.cos.COSName.getPDFName("Font")); + descendant.setItem(org.apache.pdfbox.cos.COSName.SUBTYPE, + org.apache.pdfbox.cos.COSName.getPDFName("CIDFontType2")); + descendant.setItem(org.apache.pdfbox.cos.COSName.BASE_FONT, + org.apache.pdfbox.cos.COSName.getPDFName("LiberationSans")); + org.apache.pdfbox.cos.COSDictionary sysInfo = new org.apache.pdfbox.cos.COSDictionary(); + sysInfo.setItem(org.apache.pdfbox.cos.COSName.getPDFName("Registry"), + org.apache.pdfbox.cos.COSName.getPDFName("Adobe")); + sysInfo.setItem(org.apache.pdfbox.cos.COSName.getPDFName("Ordering"), + org.apache.pdfbox.cos.COSName.getPDFName("Identity")); + sysInfo.setItem(org.apache.pdfbox.cos.COSName.getPDFName("Supplement"), + org.apache.pdfbox.cos.COSInteger.get(0)); + descendant.setItem(org.apache.pdfbox.cos.COSName.getPDFName("CIDSystemInfo"), sysInfo); + descendant.setItem(org.apache.pdfbox.cos.COSName.getPDFName("CIDToGIDMap"), + org.apache.pdfbox.cos.COSName.getPDFName("Identity")); + org.apache.pdfbox.cos.COSArray descendants = new org.apache.pdfbox.cos.COSArray(); + descendants.add(descendant); + org.apache.pdfbox.cos.COSDictionary font = new org.apache.pdfbox.cos.COSDictionary(); + font.setItem(org.apache.pdfbox.cos.COSName.TYPE, + org.apache.pdfbox.cos.COSName.getPDFName("Font")); + font.setItem(org.apache.pdfbox.cos.COSName.SUBTYPE, + org.apache.pdfbox.cos.COSName.getPDFName("Type0")); + font.setItem(org.apache.pdfbox.cos.COSName.BASE_FONT, + org.apache.pdfbox.cos.COSName.getPDFName("LiberationSans")); + font.setItem(org.apache.pdfbox.cos.COSName.getPDFName("Encoding"), + org.apache.pdfbox.cos.COSName.getPDFName("Identity-H")); + font.setItem(org.apache.pdfbox.cos.COSName.DESCENDANT_FONTS, descendants); + org.apache.pdfbox.cos.COSStream toUnicode = new org.apache.pdfbox.cos.COSStream(); + try (java.io.OutputStream out = toUnicode.createOutputStream()) { + out.write(( + "/CIDInit /ProcSet findresource begin\n" + + "12 dict begin\n" + + "begincmap\n" + + "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n" + + "/CMapName /Adobe-Identity-UCS def\n" + + "/CMapType 2 def\n" + + "1 begincodespacerange\n" + + "<0000> \n" + + "endcodespacerange\n" + + "2 beginbfchar\n" + // Both entries on ONE line: the old line-index parser + // kept only the first pair. + + "<0001> <0041> <0002> <0042>\n" + + "endbfchar\n" + + "endcmap\n" + + "CMapName currentdict /CMap defineresource pop\n" + + "end\n" + + "end\n").getBytes(StandardCharsets.US_ASCII)); + } + font.setItem(org.apache.pdfbox.cos.COSName.TO_UNICODE, toUnicode); + org.apache.pdfbox.cos.COSDictionary fonts = new org.apache.pdfbox.cos.COSDictionary(); + fonts.setItem("F1", font); + org.apache.pdfbox.cos.COSDictionary resources = new org.apache.pdfbox.cos.COSDictionary(); + resources.setItem(org.apache.pdfbox.cos.COSName.FONT, fonts); + page.setResources(new org.apache.pdfbox.pdmodel.PDResources(resources)); + page.setContents(new org.apache.pdfbox.pdmodel.common.PDStream(pdf, + new java.io.ByteArrayInputStream(( + "BT /F1 24 Tf 72 700 Td <0001> Tj ET\n" + + "BT /F1 24 Tf 200 700 Td <0002> Tj ET\n") + .getBytes(StandardCharsets.US_ASCII)))); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + pdf.save(out); + return out.toByteArray(); + } + } } diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java index a01394021..4d205fc25 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java @@ -2535,4 +2535,128 @@ String baseNameWithoutSubset() { ? baseName.substring(7) : baseName; } } + + /** + * Two non-embedded Type1 resources on one page: {@code f1Base} is drawn with the default + * (visible) text render mode, {@code f2Base} only under render mode 3 (invisible). The + * fonts must have distinct base names so the per-name visibility scan can tell them apart. + */ + private static byte[] twoFontMixedVisibilityPdf(String f1Base, String f2Base) throws IOException { + try (PDDocument pdf = new PDDocument()) { + PDPage page = new PDPage(PDRectangle.LETTER); + pdf.addPage(page); + COSDictionary fonts = new COSDictionary(); + fonts.setItem("F1", type1FontDict(f1Base)); + fonts.setItem("F2", type1FontDict(f2Base)); + COSDictionary resources = new COSDictionary(); + resources.setItem(COSName.FONT, fonts); + page.setResources(new PDResources(resources)); + page.setContents(new PDStream(pdf, new ByteArrayInputStream( + ("BT /F1 12 Tf 40 700 Td (Hello) Tj 0 -20 Td /F2 12 Tf 3 Tr (Hidden) Tj ET") + .getBytes(StandardCharsets.US_ASCII)))); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + pdf.save(out); + return out.toByteArray(); + } + } + + private static COSDictionary type1FontDict(String baseFont) { + COSDictionary descriptor = new COSDictionary(); + descriptor.setItem(COSName.TYPE, COSName.FONT_DESC); + descriptor.setName(COSName.FONT_NAME, baseFont); + descriptor.setInt(COSName.FLAGS, 32); + COSDictionary font = new COSDictionary(); + font.setItem(COSName.TYPE, COSName.FONT); + font.setItem(COSName.SUBTYPE, COSName.TYPE1); + font.setName(COSName.BASE_FONT, baseFont); + font.setItem(COSName.FONT_DESC, descriptor); + return font; + } + + @Test + void bestEffortSkipsInvisibleOnlyFontButStrictEmbedsIt() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native font replacement unavailable"); + byte[] pdf = twoFontMixedVisibilityPdf("Helvetica", "Helvetica-Bold"); + + // Best-effort: the render-mode-3 font is skipped with an explicit reason. + // It stays unembedded in the output (it is not attempted), so it is + // reported in the unresolved list together with the Tr 3 reason. + PdfFontEmbedder.Result bestEffort = PdfFontEmbedder.embedMissing(pdf); + assertTrue(bestEffort.embedded().contains("Helvetica"), + "visible font must be embedded: " + bestEffort.embedded()); + assertFalse(bestEffort.embedded().contains("Helvetica-Bold"), + "best-effort embedding must skip the invisible-only font"); + assertTrue(bestEffort.unresolved().contains("Helvetica-Bold"), + "the skipped font must remain in the unresolved list: " + + bestEffort.unresolved()); + assertTrue(bestEffort.statuses().stream().anyMatch(s -> s.baseName().equals("Helvetica-Bold") + && !s.resolved() && s.reason().contains("invisible text")), + "the skipped font must carry the Tr 3 reason: " + bestEffort.statuses()); + + // Strict (PDF/A embedding): the invisible-only font must be embedded too, + // because PDF/A-2 6.2.11.5 requires invisible-text fonts to be embedded. + PdfFontEmbedder.Result strict = PdfFontEmbedder.embedMissingStrict(pdf); + assertTrue(strict.unresolved().isEmpty(), + "strict embedding must resolve every font: " + strict.unresolved()); + assertTrue(strict.embedded().contains("Helvetica") + && strict.embedded().contains("Helvetica-Bold"), + "strict embedding must embed both the visible and invisible fonts: " + + strict.embedded()); + try (PDDocument embedded = Loader.loadPDF(strict.pdf())) { + PDResources resources = embedded.getPage(0).getResources(); + for (COSName name : resources.getFontNames()) { + PDFont font = resources.getFont(name); + assertNotNull(font.getFontDescriptor(), + "substituted font " + font.getName() + " must carry a descriptor"); + assertNotNull(font.getFontDescriptor().getFontFile2(), + "substituted font " + font.getName() + " must carry an embedded program"); + } + } + } + + @Test + void bestEffortEmbedsFontWhenAllTextIsInvisible() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native font replacement unavailable"); + // A document whose only text is invisible must not regress to leaving the + // font unembedded: the visibility scan reports no visible usage at all and + // the skip is disabled for the whole document. + byte[] pdf = twoFontMixedVisibilityPdf("Times-Roman", "Times-Roman"); + PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(pdf); + assertTrue(result.unresolved().isEmpty(), "unresolved: " + result.unresolved()); + assertTrue(result.embedded().contains("Times-Roman"), + "all-invisible document must still embed its font: " + result.embedded()); + } + + @Test + void controlCodepointsDoNotBlockSubstitution() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native font replacement unavailable"); + // PDFium reports raw control codes for glyphs without a usable Unicode + // mapping (pdfjs_TAMReview.pdf maps its CIDs to U+0002). No substitute + // font covers C0 controls, so requiring them would reject every candidate. + byte[] pdf = identityCidPdf("LiberationSans", "<0001> <0002>"); + PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(pdf); + assertTrue(result.unresolved().isEmpty(), + "control codepoints must not block substitution: " + result.unresolved() + + " statuses=" + result.statuses()); + try (PdfDocument doc = PdfDocument.open(result.pdf())) { + assertFalse(PdfFontAuditor.hasNonEmbeddedFonts(doc), + "the font must be embedded in the output"); + } + } + + @Test + void replacementMarkerCodepointDoesNotBlockSubstitution() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native font replacement unavailable"); + // U+FFFD is what PDFium reports when a ToUnicode entry is missing entirely; + // like C0/C1 controls it can never be covered by a substitute font. + byte[] pdf = identityCidPdf("LiberationSans", "<0001> "); + PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(pdf); + assertTrue(result.unresolved().isEmpty(), + "the replacement marker must not block substitution: " + result.unresolved() + + " statuses=" + result.statuses()); + try (PdfDocument doc = PdfDocument.open(result.pdf())) { + assertFalse(PdfFontAuditor.hasNonEmbeddedFonts(doc), + "the font must be embedded in the output"); + } + } } diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java index b94c05476..183518701 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java @@ -469,4 +469,147 @@ private static boolean containsAscii(byte[] haystack, String needle) { return false; } + @Test + void executeFailsClosedOnUnboundedDictionaryNesting() { + // Regression: inputs nesting more dictionaries than the repair tracking + // window used to throw ArrayIndexOutOfBoundsException out of execute(). + // They must fail closed instead. + byte[] deepNesting = ("%PDF-1.4\n" + "<<".repeat(100) + "/Im1 5 0 R") + .getBytes(StandardCharsets.ISO_8859_1); + RepairResult result = PdfRepair.builder().input(deepNesting).all().build().execute(); + assertEquals(RepairResult.Status.FAILED, result.status(), + "hostile nesting must fail closed, never throw"); + } + + @Test + void executeFailsClosedOnHeaderlessDeepPageScan() { + // Regression: a headerless, deeply nested /Type /Page drives the page-tree + // prescan through the same bounded tracking stack; it must fail closed. + byte[] headerless = ("<<".repeat(70) + "/Type /Page") + .getBytes(StandardCharsets.ISO_8859_1); + RepairResult result = PdfRepair.builder().input(headerless).all().build().execute(); + assertEquals(RepairResult.Status.FAILED, result.status(), + "garbage must fail closed, never throw"); + } + + @Test + void executeHonorsObjectNumberCapDuringSalvage() { + // Regression: an object number above the 9,000,000 cap used to overflow + // Integer.parseInt inside salvageTruncatedPdf and kill execute() with a + // NumberFormatException. The cap must hold here like everywhere else. + String hostile = "%PDF-1.4\n" + + "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n" + + "2 0 obj\n<< /Type /Pages /Kids [] /Count 0 >>\nendobj\n" + + "2147483648 0 obj\n<<>>\nendobj\n" + + "trailer\n<< /Root 1 0 R >>\n%%EOF"; + RepairResult result = PdfRepair.builder() + .input(hostile.getBytes(StandardCharsets.ISO_8859_1)) + .all() + .build() + .execute(); + assertNotNull(result, "execute must return a result, never throw"); + if (result.isUsable()) { + assertFalse(new String(result.repairedPdf(), StandardCharsets.ISO_8859_1) + .contains("/Size 2147483649"), + "salvage must cap /Size at the accepted object-number bound"); + } + } + + @Test + void repairsGluedEndobjAfterAStream() throws Exception { + Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge"); + // Regression: after closing a stream the position guard used to + // re-enter stream state at the "stream" suffix of "endstream" and + // over-guarded the rest of the file, so normalizeGluedEndobj never + // split a glued "endobjN 0 obj" that follows a stream. qpdf attaches + // the glued object to the previous one and the page is lost. + String formData = "BT ET\n"; + String contentData = "BT /F1 12 Tf 8 8 Td (Marker) Tj ET\n"; + + StringBuilder sb = new StringBuilder(); + sb.append("%PDF-1.4\n"); + int o1 = sb.length(); + sb.append("1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + int o2 = sb.length(); + sb.append("2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + int o4 = sb.length(); + sb.append("4 0 obj\n<< /Type /XObject /Subtype /Form /BBox [0 0 8 8] /Length ") + .append(formData.length()).append(" >>\nstream\n").append(formData) + .append("endstream\nendobj\n"); + int o5 = sb.length(); + sb.append("5 0 obj\n<< /Length ").append(contentData.length()) + // Glued "endobj3 0 obj": the page object header follows the + // content stream's endobj without a separator. + .append(" >>\nstream\n").append(contentData).append("endstream\nendobj3 0 obj\n"); + int o3 = sb.length(); + sb.append("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] " + + "/Resources << /XObject << /Im1 4 0 R >> >> /Contents 5 0 R >>\nendobj\n"); + int xref = sb.length(); + sb.append(String.format(Locale.ROOT, + "xref\n0 6\n0000000000 65535 f \n%010d 00000 n \n%010d 00000 n \n" + + "%010d 00000 n \n%010d 00000 n \n%010d 00000 n \n" + + "trailer\n<< /Root 1 0 R /Size 6 >>\nstartxref\n%d\n%%%%EOF\n", + o1, o2, o3, o4, o5, xref)); + + byte[] pdf = sb.toString().getBytes(StandardCharsets.ISO_8859_1); + RepairResult result = PdfRepair.builder().input(pdf).all().build().execute(); + assertTrue(result.isUsable(), "repair failed: " + result.status()); + try (PDDocument reopened = Loader.loadPDF(result.repairedPdf())) { + assertEquals(1, reopened.getNumberOfPages(), + "the glued page object must be recovered after a stream"); + assertTrue(new PDFTextStripper().getText(reopened).contains("Marker"), + "page content must survive the glued-endobj split"); + } + } + + @Test + void streamDataContainingEndstreamKeywordStaysUntouched() throws Exception { + Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge"); + // Regression: stream data containing the bytes "endstream" ended the + // position guard early; the remaining data was treated as object + // syntax and the resource-key dedup edited it, shifting the stream + // under its own /Length so the page object landed inside the extended + // stream and the page was lost. The guard must follow the declared + // /Length exactly. + StringBuilder refs = new StringBuilder(); + for (int i = 0; i < 120; i++) refs.append("/Im1 4 0 R\n"); + // The trailing space (not an EOL) is what defeats the old scanner: no + // re-entry at the "stream" suffix, so the dedup targets stay unguarded. + String contentData = "q\nendstream " + refs + + "BT /F1 12 Tf 8 8 Td (Marker) Tj ET\nQ\n"; + String formData = "BT ET\n"; + + StringBuilder sb = new StringBuilder(); + sb.append("%PDF-1.4\n"); + int o1 = sb.length(); + sb.append("1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"); + int o2 = sb.length(); + sb.append("2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"); + int o4 = sb.length(); + sb.append("4 0 obj\n<< /Type /XObject /Subtype /Form /BBox [0 0 8 8] /Length ") + .append(formData.length()).append(" >>\nstream\n").append(formData) + .append("endstream\nendobj\n"); + int o5 = sb.length(); + sb.append("5 0 obj\n<< /Length ").append(contentData.length()) + .append(" >>\nstream\n").append(contentData).append("endstream\nendobj\n"); + int o3 = sb.length(); + sb.append("3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] " + + "/Resources << /XObject << /Im1 4 0 R >> >> /Contents 5 0 R >>\nendobj\n"); + int xref = sb.length(); + sb.append(String.format(Locale.ROOT, + "xref\n0 6\n0000000000 65535 f \n%010d 00000 n \n%010d 00000 n \n" + + "%010d 00000 n \n%010d 00000 n \n%010d 00000 n \n" + + "trailer\n<< /Root 1 0 R /Size 6 >>\nstartxref\n%d\n%%%%EOF\n", + o1, o2, o3, o4, o5, xref)); + + byte[] pdf = sb.toString().getBytes(StandardCharsets.ISO_8859_1); + RepairResult result = PdfRepair.builder().input(pdf).all().build().execute(); + assertTrue(result.isUsable(), "repair failed: " + result.status()); + try (PDDocument reopened = Loader.loadPDF(result.repairedPdf())) { + assertEquals(1, reopened.getNumberOfPages(), + "the page must survive the repair"); + assertTrue(new PDFTextStripper().getText(reopened).contains("Marker"), + "stream data must not be edited by the repair-token normalizers"); + } + } } diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java index 23e591404..367ffbff1 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java @@ -699,12 +699,15 @@ void normalizeAllSynthesizesCidSetFromProgramCoverage() throws Exception { assumeTrue(NativeRuntime.isFull(), "needs real native library"); // The stale /CIDSet marks only CID 0, the drawn CID 36, and unreferenced // CID 50. ISO 19005-2 6.2.11.4 test 4 requires every CID present in the - // program, so the normalizer must rebuild the full 0..numGlyphs-1 bitmap. + // program. CID 0 is the .notdef glyph, not a CID of the subset, and the + // buildCidSetBits writers (embedder and normalizer) deliberately leave + // its bit clear so validators comparing CIDSet to the program's glyph + // set do not see a phantom entry. byte[] staleCidSet = new byte[] {(byte) 0x80, 0, 0, 0, 0x08, 0, 0x20}; byte[] pdf = cidTrueTypePdf("ABCDEF+LiberationSans", staleCidSet); int numGlyphs = FontNormalizer.classify(liberationSansBytes()).numGlyphs(); byte[] expected = new byte[(numGlyphs + 7) / 8]; - for (int cid = 0; cid < numGlyphs; cid++) + for (int cid = 1; cid < numGlyphs; cid++) expected[cid / 8] |= (byte) (0x80 >> (cid % 8)); try (PdfDocument doc = PdfDocument.open(pdf)) { @@ -750,8 +753,11 @@ void normalizeAllWritesCidSetForRetainedSubsetGlyphs() throws Exception { assertEquals(5, cidSet.length, "CIDSet must stop at the highest present GID in the re-subsetted /Identity program"); // Re-subsetted /Identity program keeps glyph slots 0..36 (max_gid=36), so ISO 19005-2 - // §6.2.11.4.2 requires CIDs 0..36 present in the font program to all be marked. - for (int i = 0; i < 4; i++) { + // §6.2.11.4.2 requires CIDs 1..36 present in the font program to all be marked. + // Slot 0 is .notdef and stays clear (see PdfFontEmbedderTest for the embedder's + // matching CIDSet policy - the two writers must not drift). + assertEquals((byte) 0x7F, cidSet[0], "CIDSet byte 0 must exclude the .notdef CID 0"); + for (int i = 1; i < 4; i++) { assertEquals((byte) 0xFF, cidSet[i], "CIDSet byte " + i + " must mark all CIDs present"); } assertEquals((byte) 0xF8, cidSet[4], "CIDSet final byte must mark CIDs 32..36"); @@ -793,7 +799,9 @@ void normalizeAllRepairsStaleCidSetAndInconsistentWidths() throws Exception { byte[] cidSet = streamBytes(descriptor, COSName.CID_SET); assertEquals((entries + 7) / 8, cidSet.length, "CIDSet must cover every CID in the map"); - for (int cid = 0; cid < entries; cid++) { + assertTrue((cidSet[0] & 0x80) == 0, + "CIDSet must not mark the .notdef CID 0"); + for (int cid = 1; cid < entries; cid++) { assertTrue((cidSet[cid / 8] & (0x80 >> (cid % 8))) != 0, "CIDSet must mark CID " + cid); } diff --git a/native/bridge/src/jpdfium_font_normalizer.cpp b/native/bridge/src/jpdfium_font_normalizer.cpp index 8c9566610..9a54fffe5 100644 --- a/native/bridge/src/jpdfium_font_normalizer.cpp +++ b/native/bridge/src/jpdfium_font_normalizer.cpp @@ -61,6 +61,17 @@ static QPDFObjectHandle embeddedFontProgram(QPDFObjectHandle const& descriptor) return QPDFObjectHandle(); } +// The descriptor key the program was read from, so cmap surgery writes the +// updated stream back where readers (embeddedFontProgram) will find it - +// never leaving a stale sibling entry or parking a TrueType stream under +// /FontFile2 while the real program lives in /FontFile3. +static char const* embeddedFontProgramKey(QPDFObjectHandle const& descriptor) { + for (char const* key : {"/FontFile2", "/FontFile3", "/FontFile"}) { + if (descriptor.getKey(key).isStream()) return key; + } + return nullptr; +} + static void markFormFontNotSubsettable(QPDFObjectHandle const& font, FontUsageCounts& usage) { if (!font.isDictionary()) return; QPDFObjectHandle target = font; @@ -1088,26 +1099,79 @@ static void setSymbolicFlag(QPDFObjectHandle& descriptor, bool symbolic, } } +// True when the bytes carry an SFNT (or TrueType Collection) table directory, +// so cmap surgery is meaningful. A /TrueType font whose program is actually a +// bare Type1/CFF blob must be left untouched - splicing a cmap into it would +// produce a corrupt program. +static bool isSfntBytes(std::string_view bytes) { + if (bytes.size() < 4) return false; + const uint32_t magic = (static_cast(bytes[0]) << 24) | + (static_cast(bytes[1]) << 16) | + (static_cast(bytes[2]) << 8) | static_cast(bytes[3]); + return magic == 0x00010000U || magic == 0x74727565U /* 'true' */ || + magic == 0x4F54544FU /* 'OTTO' */ || magic == 0x74646366U /* 'ttcf' */; +} + static void repairSimpleTrueTypeCmap(QPDF& pdf, QPDFObjectHandle& font, - QPDFObjectHandle& descriptor, + QPDFObjectHandle& descriptor, FT_Library ftLib, std::shared_ptr const& data, FT_Face face, - FontNormalizeState& state) { + FT_Long faceIndex, + std::vector const& simpleGlyphs, + bool simpleAnyGid, FontNormalizeState& state, + bool programRewritten) { QPDFObjectHandle flagsObj = descriptor.getKey("/Flags"); const bool symbolic = flagsObj.isInteger() && (flagsObj.getIntValue() & 4) != 0; - const std::string bytes(reinterpret_cast(data->getBuffer()), data->getSize()); + // Re-subsetting may have replaced the program after data/face were captured; + // surgery must operate on the stream that is actually in the document. + std::shared_ptr current = data; + FT_Face currentFace = face; + FtFaceGuard reloadedGuard; + if (programRewritten) { + QPDFObjectHandle stream = embeddedFontProgram(descriptor); + if (!stream.isStream()) return; + try { + current = stream.getStreamData(qpdf_dl_generalized); + } catch (...) { + return; + } + if (!current || current->getSize() == 0) return; + FT_Face reloaded = nullptr; + if (FT_New_Memory_Face(ftLib, current->getBuffer(), + static_cast(current->getSize()), faceIndex, + &reloaded) != 0 || + !reloaded) + return; + reloadedGuard.reset(reloaded); + currentFace = reloaded; + } + const std::string bytes(reinterpret_cast(current->getBuffer()), + current->getSize()); + if (!isSfntBytes(bytes)) return; + char const* programKey = embeddedFontProgramKey(descriptor); + if (!programKey) return; if (symbolic) { if (font.hasKey("/Encoding")) { font.removeKey("/Encoding"); state.changed = true; } - if (sfntCmapSubtableCount(bytes) == 1 || sfntHasCmapSubtable(bytes, 3, 0)) return; - std::map identity; - for (uint32_t code = 0; code < 256 && code < static_cast(face->num_glyphs); - ++code) - identity[code] = static_cast(code); + // Only synthesize a (3,0) subtable when the program carries no cmap at + // all. Replacing an existing multi-subtable cmap would drop mappings + // other consumers rely on. + if (sfntCmapSubtableCount(bytes) != 0) return; + // Derive code->gid from the PDF-resolved encoding instead of assuming + // identity. A guessed identity cmap changes the rendered glyph for + // every code whose glyph does not sit at that slot in this program. + if (!simpleAnyGid) return; + std::map codeToGid; + for (size_t code = 0; code < simpleGlyphs.size() && code < 256; ++code) { + if (simpleGlyphs[code].gid != 0) + codeToGid[static_cast(code)] = + static_cast(simpleGlyphs[code].gid); + } + if (codeToGid.empty()) return; std::string updated = bytes; - if (!ensureSfntCmapSubtable(updated, 3, 0, identity)) return; - descriptor.replaceKey("/FontFile2", QPDFObjectHandle::newStream(&pdf, updated)); + if (!ensureSfntCmapSubtable(updated, 3, 0, codeToGid)) return; + descriptor.replaceKey(programKey, QPDFObjectHandle::newStream(&pdf, updated)); state.changed = true; return; } @@ -1116,9 +1180,9 @@ static void repairSimpleTrueTypeCmap(QPDF& pdf, QPDFObjectHandle& font, if (sfntHasCmapSubtable(bytes, 3, 1) || sfntHasCmapSubtable(bytes, 3, 10)) return; std::map unicodeByGid; char glyphName[256]; - if (FT_HAS_GLYPH_NAMES(face)) { - for (FT_UInt gid = 1; gid < static_cast(face->num_glyphs); ++gid) { - if (FT_Get_Glyph_Name(face, gid, glyphName, sizeof(glyphName)) != 0 || + if (FT_HAS_GLYPH_NAMES(currentFace)) { + for (FT_UInt gid = 1; gid < static_cast(currentFace->num_glyphs); ++gid) { + if (FT_Get_Glyph_Name(currentFace, gid, glyphName, sizeof(glyphName)) != 0 || glyphName[0] == '\0') continue; for (uint32_t scalar : unicodeSequenceForGlyphName(glyphName)) { @@ -1130,7 +1194,7 @@ static void repairSimpleTrueTypeCmap(QPDF& pdf, QPDFObjectHandle& font, if (unicodeByGid.empty()) return; std::string updated = bytes; if (!ensureSfntCmapSubtable(updated, 3, 1, unicodeByGid)) return; - descriptor.replaceKey("/FontFile2", QPDFObjectHandle::newStream(&pdf, updated)); + descriptor.replaceKey(programKey, QPDFObjectHandle::newStream(&pdf, updated)); state.changed = true; } @@ -1656,7 +1720,14 @@ static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool } } const bool cffCid = target.getKey("/Subtype").isNameAndEquals("/CIDFontType0"); - if (cffCid || (!didResubset && !cmapFitsProgram(data, cidSetFace))) { + // A CID-keyed CFF exposes its CID set directly (cidsPresentInProgram + // reads it via FT_Get_CID_From_Glyph_Index), so ISO 19005-2 + // 6.2.11.4.2 is satisfied by rebuilding /CIDSet instead of dropping + // it. Only name-keyed CFF programs, where CIDs cannot be derived, + // keep the old remove. + const bool cffCidKeyed = cffCid && FT_IS_CID_KEYED(cidSetFace); + if ((cffCid && !cffCidKeyed) || + (!cffCid && !didResubset && !cmapFitsProgram(data, cidSetFace))) { if (descriptor.hasKey("/CIDSet")) { descriptor.removeKey("/CIDSet"); state.changed = true; @@ -1703,7 +1774,8 @@ static void processFont(QPDF& pdf, FT_Library ftLib, QPDFObjectHandle font, bool } if (!cid && full && subtypeName == "/TrueType") { - repairSimpleTrueTypeCmap(pdf, font, descriptor, data, rawFace, state); + repairSimpleTrueTypeCmap(pdf, font, descriptor, ftLib, data, rawFace, faceIndex, + simpleGlyphs, simpleAnyGid, state, didResubset); } } diff --git a/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp b/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp index a0a079780..c501e8406 100644 --- a/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp +++ b/native/bridge/src/jpdfium_qpdf_fonts_embed.cpp @@ -144,6 +144,16 @@ void setCjkToUnicode(QPDF& qpdf, QPDFObjectHandle font, std::map= 0x7F && scalar <= 0x9F) || scalar == 0xFFFD; +} + bool coversWidthCids(QPDFObjectHandle const& font, std::map const& unicodeByCid, std::set const& usedCids, bool scanComplete) { if (!scanComplete) return false; @@ -163,7 +173,10 @@ bool cidGlyphOrderMatches(FT_Face face, std::map const& unic if (unicodeByCid.empty() || FT_Select_Charmap(face, FT_ENCODING_UNICODE)) return false; for (auto const& [cid, unicode] : unicodeByCid) { uint32_t scalar = jpdfium::font::decodeUnicodeScalar(unicode); - if (!scalar || FT_Get_Char_Index(face, scalar) != cid) return false; + // Non-renderable scalars are pinned to .notdef below, so they impose no + // glyph-order constraint on the donor. + if (nonRenderableScalar(scalar)) continue; + if (FT_Get_Char_Index(face, scalar) != cid) return false; } return true; } @@ -174,7 +187,11 @@ bool mapCidsToGlyphs(FT_Face face, std::map const& unicodeBy if (unicodeByCid.empty() || FT_Select_Charmap(face, FT_ENCODING_UNICODE)) return false; for (auto const& [cid, unicode] : unicodeByCid) { uint32_t scalar = jpdfium::font::decodeUnicodeScalar(unicode); - FT_UInt gid = scalar ? FT_Get_Char_Index(face, scalar) : 0; + if (nonRenderableScalar(scalar)) { + cidGlyph[cid] = 0; + continue; + } + FT_UInt gid = FT_Get_Char_Index(face, scalar); if (!gid) { cidGlyph.clear(); return false; @@ -608,6 +625,12 @@ bool prepareMissingCidMapping(QPDF& qpdf, QPDFObjectHandle& font, QPDFObjectHand if (!cidGlyphOrderMatches(program.face, job.cidUnicode)) return false; for (auto const& [mappedCid, unicode] : job.cidUnicode) { uint32_t scalar = jpdfium::font::decodeUnicodeScalar(unicode); + if (nonRenderableScalar(scalar)) { + // Control codes and the replacement marker have no glyph in any + // donor; pin them to .notdef like the TrueType route does. + job.cidGlyph[mappedCid] = 0; + continue; + } if (!scalar) { job.cidGlyph.clear(); break; diff --git a/native/bridge/src/jpdfium_sanitize.cpp b/native/bridge/src/jpdfium_sanitize.cpp index 3cf844284..044366744 100644 --- a/native/bridge/src/jpdfium_sanitize.cpp +++ b/native/bridge/src/jpdfium_sanitize.cpp @@ -178,73 +178,90 @@ bool parseToUnicode(const std::shared_ptr& buf, std::vector& QPDFTokenizer tok; auto is = std::make_shared("tounicode", data); - // Line-number index: bfchar/bfrange entries are one per LINE, so an entry - // is flushed when the token stream crosses a newline. - std::vector lineStarts; - lineStarts.push_back(0); - for (size_t i = 0; i < data.size(); i++) - if (data[i] == '\n') lineStarts.push_back(i + 1); - auto lineOf = [&](qpdf_offset_t pos) -> size_t { - auto it = std::upper_bound(lineStarts.begin(), lineStarts.end(), static_cast(pos)); - return static_cast(it - lineStarts.begin()) - 1; - }; - + // Entries are operand-counted, not line-counted: real-world CMaps put + // several bfchar/bfrange entries on one line, and a line-index flush + // silently drops every entry after the first of such a line. enum class CmapSection { None, BfChar, BfRange }; CmapSection section = CmapSection::None; std::vector pendingHex; - size_t pendingLine = 0; - bool havePending = false; + bool inArray = false; + + // A hostile bfrange can claim a 4-byte code space; without a span cap one + // "entry" amplifies into billions of CmapEntry pushes. + constexpr uint64_t kMaxRangeSpan = 65536; + // Overall ceiling so a multi-megabyte hostile CMap cannot exhaust memory. + constexpr size_t kMaxEntries = 1U << 20; + + auto codeFromBytes = [](std::string const& bytes, uint64_t& code) -> bool { + if (bytes.empty() || bytes.size() > 4) return false; + uint64_t value = 0; + for (unsigned char c : bytes) value = (value << 8) | c; + if (value > 0x7FFFFFFFULL) return false; // CmapEntry::code is int + code = value; + return true; + }; + // UTF-16 surrogate units are not valid mapping targets: consecutive-form + // ranges that walk into the surrogate block stop at the first bad unit. + auto hasSurrogateUnit = [](std::string const& uni) { + if (uni.size() % 2 != 0) return false; + for (size_t i = 0; i + 1 < uni.size(); i += 2) { + const unsigned unit = + (static_cast(uni[i]) << 8) | static_cast(uni[i + 1]); + if (unit >= 0xD800U && unit <= 0xDFFFU) return true; + } + return false; + }; auto flushBfChar = [&]() { - if (pendingHex.size() >= 2) { - const std::string& codeBytes = pendingHex[0]; - const std::string& uniBytes = pendingHex[1]; - if (!codeBytes.empty()) { - int code = 0; - for (unsigned char c : codeBytes) code = (code << 8) | c; - out.push_back(CmapEntry{code, static_cast(codeBytes.size()), uniBytes}); + while (pendingHex.size() >= 2) { + uint64_t code = 0; + if (codeFromBytes(pendingHex[0], code) && out.size() < kMaxEntries) { + out.push_back(CmapEntry{static_cast(code), + static_cast(pendingHex[0].size()), pendingHex[1]}); } + pendingHex.erase(pendingHex.begin(), pendingHex.begin() + 2); } pendingHex.clear(); - havePending = false; }; auto flushBfRange = [&]() { - if (pendingHex.size() >= 3) { - const std::string& loBytes = pendingHex[0]; - const std::string& hiBytes = pendingHex[1]; - const std::string& uniBytes = pendingHex[2]; - if (!loBytes.empty() && loBytes.size() == hiBytes.size() && !uniBytes.empty()) { - int lo = 0, hi = 0; - for (unsigned char c : loBytes) lo = (lo << 8) | c; - for (unsigned char c : hiBytes) hi = (hi << 8) | c; - if (pendingHex.size() == 3) { - // consecutive form: lo..hi -> u..u+n; advance the full - // big-endian UTF-16 byte string in place so multi-unit - // values (including surrogate pairs) never truncate. - std::string ub = uniBytes; - for (int code = lo; code <= hi; code++) { - out.push_back(CmapEntry{code, static_cast(loBytes.size()), ub}); - for (size_t idx = ub.size(); idx-- > 0;) { - auto v = static_cast(ub[idx]); - if (v < 0xFF) { - ub[idx] = static_cast(v + 1); - break; - } - ub[idx] = '\0'; - } - } - } else { - // list form: pendingHex[2..] are the unicodes - for (int code = lo, k = 2; - code <= hi && k < static_cast(pendingHex.size()); code++, k++) { - out.push_back( - CmapEntry{code, static_cast(loBytes.size()), pendingHex[k]}); + if (pendingHex.size() < 3) { + pendingHex.clear(); + return; + } + uint64_t lo = 0; + uint64_t hi = 0; + if (!codeFromBytes(pendingHex[0], lo) || !codeFromBytes(pendingHex[1], hi) || lo > hi || + hi - lo + 1 > kMaxRangeSpan) { + pendingHex.clear(); + return; + } + const int loBytes = static_cast(pendingHex[0].size()); + if (inArray) { + // list form: pendingHex[2..] are the per-code unicodes + size_t k = 2; + for (uint64_t code = lo; + code <= hi && k < pendingHex.size() && out.size() < kMaxEntries; code++, k++) { + out.push_back(CmapEntry{static_cast(code), loBytes, pendingHex[k]}); + } + } else { + // consecutive form: lo..hi -> u..u+n; advance the full big-endian + // UTF-16 byte string in place so multi-unit values (including + // surrogate pairs) never truncate. + std::string ub = pendingHex[2]; + for (uint64_t code = lo; code <= hi && out.size() < kMaxEntries; code++) { + if (hasSurrogateUnit(ub)) break; + out.push_back(CmapEntry{static_cast(code), loBytes, ub}); + for (size_t idx = ub.size(); idx-- > 0;) { + auto v = static_cast(ub[idx]); + if (v < 0xFF) { + ub[idx] = static_cast(v + 1); + break; } + ub[idx] = '\0'; } } } pendingHex.clear(); - havePending = false; }; while (true) { @@ -252,48 +269,56 @@ bool parseToUnicode(const std::shared_ptr& buf, std::vector& QPDFTokenizer::token_type_e tt = t.getType(); if (tt == QPDFTokenizer::tt_eof || tt == QPDFTokenizer::tt_bad) break; if (tt == QPDFTokenizer::tt_space || tt == QPDFTokenizer::tt_comment) continue; - // InputSource::getLastOffset() points at the beginning of the last - // token read (QPDFTokenizer::readToken contract). - size_t line = lineOf(is->getLastOffset()); - // A newline inside an entry group terminates the current entry. - if (havePending && line != pendingLine) { - if (section == CmapSection::BfChar) - flushBfChar(); - else if (section == CmapSection::BfRange) - flushBfRange(); - } if (tt == QPDFTokenizer::tt_word) { std::string w = t.getValue(); if (w == "beginbfchar") { flushBfRange(); + inArray = false; section = CmapSection::BfChar; } else if (w == "beginbfrange") { flushBfChar(); + inArray = false; section = CmapSection::BfRange; } else if (w == "endbfchar") { flushBfChar(); section = CmapSection::None; } else if (w == "endbfrange") { flushBfRange(); + inArray = false; section = CmapSection::None; } else if (w.starts_with("begin")) { // unrelated section: flush what we have and ignore it flushBfChar(); flushBfRange(); + inArray = false; section = CmapSection::None; } - } else if (tt == QPDFTokenizer::tt_string) { - if (section != CmapSection::None) { - pendingHex.push_back(t.getValue()); - pendingLine = line; - havePending = true; - } + continue; + } + if (tt == QPDFTokenizer::tt_array_close) { + if (section == CmapSection::BfRange && inArray) flushBfRange(); + inArray = false; + continue; + } + if (tt == QPDFTokenizer::tt_array_open) { + // bfrange's third operand is either a string (consecutive form, + // flushed when it arrives) or an array of strings. + if (section == CmapSection::BfRange && !inArray && pendingHex.size() == 2) + inArray = true; + continue; + } + if (tt != QPDFTokenizer::tt_string) continue; + if (section == CmapSection::None) continue; + pendingHex.push_back(t.getValue()); + if (section == CmapSection::BfChar) { + if (pendingHex.size() == 2) flushBfChar(); + } else if (section == CmapSection::BfRange && !inArray && pendingHex.size() == 3) { + // consecutive form is complete + flushBfRange(); } } - if (section == CmapSection::BfChar) - flushBfChar(); - else if (section == CmapSection::BfRange) - flushBfRange(); + flushBfChar(); + flushBfRange(); return !out.empty(); } From 4ab2737bc3d6e7f40e7ee59d8351402e583a192c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 19 Nov 2026 14:41:02 +0100 Subject: [PATCH 08/11] Import QpdfLib in PdfFontEmbedderTest, guard native qpdf tests, and format jpdfium_qpdf.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../stirling/software/jpdfium/doc/PdfFontEmbedderTest.java | 1 + .../test/java/stirling/software/jpdfium/doc/PdfRepairTest.java | 3 +++ .../stirling/software/jpdfium/doc/QpdfIntegrationTest.java | 3 +++ native/bridge/src/jpdfium_qpdf.cpp | 1 - 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java index 4d205fc25..457783028 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java @@ -3,6 +3,7 @@ import stirling.software.jpdfium.PdfDocument; import stirling.software.jpdfium.PdfPage; import stirling.software.jpdfium.exception.JPDFiumException; +import stirling.software.jpdfium.panama.QpdfLib; import org.apache.pdfbox.Loader; import org.apache.pdfbox.cos.COSArray; diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java index 183518701..3aa01c9e5 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java @@ -1,5 +1,7 @@ package stirling.software.jpdfium.doc; +import stirling.software.jpdfium.panama.NativeRuntime; + import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -471,6 +473,7 @@ private static boolean containsAscii(byte[] haystack, String needle) { @Test void executeFailsClosedOnUnboundedDictionaryNesting() { + Assumptions.assumeTrue(NativeRuntime.isFull(), "needs real native qpdf"); // Regression: inputs nesting more dictionaries than the repair tracking // window used to throw ArrayIndexOutOfBoundsException out of execute(). // They must fail closed instead. diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/QpdfIntegrationTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/QpdfIntegrationTest.java index 16b147286..7630c2de7 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/QpdfIntegrationTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/QpdfIntegrationTest.java @@ -1,5 +1,7 @@ package stirling.software.jpdfium.doc; +import stirling.software.jpdfium.panama.NativeRuntime; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import stirling.software.jpdfium.PdfDocument; @@ -257,6 +259,7 @@ private static int indexOf(byte[] haystack, byte[] needle, int from) { @Test void testStreamOptimizerWritesEolBeforeEndstream(@TempDir Path tempDir) throws Exception { assumeTrue(PdfStreamOptimizer.isSupported(), "native qpdf optimize unavailable"); + assumeTrue(NativeRuntime.isFull(), "needs real native qpdf"); Path in = tempDir.resolve("flush.pdf"); Path out = tempDir.resolve("flush_opt.pdf"); Files.write(in, pdfWithFlushEndstream()); diff --git a/native/bridge/src/jpdfium_qpdf.cpp b/native/bridge/src/jpdfium_qpdf.cpp index 1a97acfff..64ddb61ea 100644 --- a/native/bridge/src/jpdfium_qpdf.cpp +++ b/native/bridge/src/jpdfium_qpdf.cpp @@ -221,7 +221,6 @@ class ImageOptimizer final : public QPDFObjectHandle::StreamDataProvider { std::unique_ptr config_; }; - // Hash a stream's dictionary and raw bytes so only bit-identical image // XObjects are candidates for deduplication. The dictionary check catches // images with identical pixels but different color spaces or decode arrays. From b4dfa5bc9cc85a366daa98f37c29493da9f32101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 19 Nov 2026 14:48:18 +0100 Subject: [PATCH 09/11] Use >= bounds check before pdf[i + 6] in stream token guard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../src/main/java/stirling/software/jpdfium/doc/PdfRepair.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java index 3ed69459b..2f5bf6104 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java @@ -523,7 +523,7 @@ private static boolean[] protectedPositions(byte[] pdf) { guarded[i] = true; hex = true; } else if (matchesAscii(pdf, i, "stream") && isTokenStart(pdf, i) - && (i + 6 == pdf.length || pdf[i + 6] == '\n' || pdf[i + 6] == '\r')) { + && (i + 6 >= pdf.length || pdf[i + 6] == '\n' || pdf[i + 6] == '\r')) { guarded[i] = true; stream = true; endstreamAt = resolveStreamEnd(pdf, i); From 12dede43a8ce77deb504acd7934317c5471686fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 19 Nov 2026 15:04:13 +0100 Subject: [PATCH 10/11] Raise libjpdfium size ceiling to 18.5 MB for musl static CJK/repair additions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- native/library-size-budgets.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/native/library-size-budgets.txt b/native/library-size-budgets.txt index 54edbe071..00a22f4eb 100644 --- a/native/library-size-budgets.txt +++ b/native/library-size-budgets.txt @@ -33,5 +33,5 @@ libskia* 8000000 skia.dll 8000000 libpdfium* 10500000 pdfium.dll 10500000 -libjpdfium* 15600000 -jpdfium.dll 15600000 +libjpdfium* 18500000 +jpdfium.dll 18500000 From 097a8a25026a02a75344ea05a2b9cfdfc7b1f415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 19 Nov 2026 16:07:07 +0100 Subject: [PATCH 11/11] Address CodeRabbit findings in PdfCompressor, PdfRepair, font normalizer, and ToUnicode parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../software/jpdfium/doc/PdfCompressor.java | 3 +- .../software/jpdfium/doc/PdfRepair.java | 3 +- native/bridge/src/jpdfium_font_normalizer.cpp | 12 +++++++- native/bridge/src/jpdfium_sanitize.cpp | 30 +++++++++++++++---- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java index 02fb536de..fe496e430 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java @@ -435,7 +435,8 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) { // PDF/A-1 forbids object streams and cross-reference streams, so a // generated-xref result would break the conformance it claims. byte[] opt = pdfA1 - ? PdfOptimizer.optimize(resultBytes, PdfOptimizer.DEFAULT, 9, + ? PdfOptimizer.optimize(resultBytes, + PdfOptimizer.RECOMPRESS_FLATE | PdfOptimizer.COMPRESS_STREAMS, 9, PdfOptimizer.OBJECT_STREAMS_DISABLE, PdfOptimizer.STREAM_DATA_COMPRESS, PdfOptimizer.DECODE_LEVEL_GENERALIZED) diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java index 2f5bf6104..07a0e3f66 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java @@ -354,7 +354,8 @@ public RepairResult execute() { : null); } - if (coreResult.isUsable() && coreResult.status() != RepairResult.Status.CLEAN) { + if (coreResult.isUsable() && coreResult.status() != RepairResult.Status.CLEAN + && (flags & (FLAG_NORMALIZE_XREF | FLAG_FORCE_V14)) == 0) { coreResult = new RepairResult(coreResult.status(), compact(coreResult.repairedPdf()), coreResult.diagnosticJson()); } diff --git a/native/bridge/src/jpdfium_font_normalizer.cpp b/native/bridge/src/jpdfium_font_normalizer.cpp index 9a54fffe5..1014992fc 100644 --- a/native/bridge/src/jpdfium_font_normalizer.cpp +++ b/native/bridge/src/jpdfium_font_normalizer.cpp @@ -1194,7 +1194,17 @@ static void repairSimpleTrueTypeCmap(QPDF& pdf, QPDFObjectHandle& font, if (unicodeByGid.empty()) return; std::string updated = bytes; if (!ensureSfntCmapSubtable(updated, 3, 1, unicodeByGid)) return; - descriptor.replaceKey(programKey, QPDFObjectHandle::newStream(&pdf, updated)); + QPDFObjectHandle oldStream = descriptor.getKey(programKey); + QPDFObjectHandle newStream = QPDFObjectHandle::newStream(&pdf, updated); + QPDFObjectHandle newDict = newStream.getDict(); + if (std::string_view(programKey) == "/FontFile2") { + newDict.replaceKey("/Length1", + QPDFObjectHandle::newInteger(static_cast(updated.size()))); + } else if (std::string_view(programKey) == "/FontFile3" && oldStream.isStream()) { + QPDFObjectHandle subtype = oldStream.getDict().getKey("/Subtype"); + if (subtype.isName()) newDict.replaceKey("/Subtype", subtype); + } + descriptor.replaceKey(programKey, newStream); state.changed = true; } diff --git a/native/bridge/src/jpdfium_sanitize.cpp b/native/bridge/src/jpdfium_sanitize.cpp index 044366744..4dab4dcab 100644 --- a/native/bridge/src/jpdfium_sanitize.cpp +++ b/native/bridge/src/jpdfium_sanitize.cpp @@ -191,6 +191,8 @@ bool parseToUnicode(const std::shared_ptr& buf, std::vector& constexpr uint64_t kMaxRangeSpan = 65536; // Overall ceiling so a multi-megabyte hostile CMap cannot exhaust memory. constexpr size_t kMaxEntries = 1U << 20; + constexpr size_t kMaxTotalDestBytes = 16U * 1024U * 1024U; + size_t totalDestBytes = 0; auto codeFromBytes = [](std::string const& bytes, uint64_t& code) -> bool { if (bytes.empty() || bytes.size() > 4) return false; @@ -213,13 +215,15 @@ bool parseToUnicode(const std::shared_ptr& buf, std::vector& }; auto flushBfChar = [&]() { - while (pendingHex.size() >= 2) { + for (size_t i = 0; i + 1 < pendingHex.size(); i += 2) { uint64_t code = 0; - if (codeFromBytes(pendingHex[0], code) && out.size() < kMaxEntries) { + const size_t dlen = pendingHex[i + 1].size(); + if (codeFromBytes(pendingHex[i], code) && out.size() < kMaxEntries && + dlen <= kMaxTotalDestBytes - totalDestBytes) { + totalDestBytes += dlen; out.push_back(CmapEntry{static_cast(code), - static_cast(pendingHex[0].size()), pendingHex[1]}); + static_cast(pendingHex[i].size()), pendingHex[i + 1]}); } - pendingHex.erase(pendingHex.begin(), pendingHex.begin() + 2); } pendingHex.clear(); }; @@ -241,6 +245,9 @@ bool parseToUnicode(const std::shared_ptr& buf, std::vector& size_t k = 2; for (uint64_t code = lo; code <= hi && k < pendingHex.size() && out.size() < kMaxEntries; code++, k++) { + const size_t dlen = pendingHex[k].size(); + if (dlen > kMaxTotalDestBytes - totalDestBytes) break; + totalDestBytes += dlen; out.push_back(CmapEntry{static_cast(code), loBytes, pendingHex[k]}); } } else { @@ -248,8 +255,14 @@ bool parseToUnicode(const std::shared_ptr& buf, std::vector& // UTF-16 byte string in place so multi-unit values (including // surrogate pairs) never truncate. std::string ub = pendingHex[2]; + const uint64_t span = hi - lo + 1; + if (ub.size() > 0 && span > (kMaxTotalDestBytes - totalDestBytes) / ub.size()) { + pendingHex.clear(); + return; + } for (uint64_t code = lo; code <= hi && out.size() < kMaxEntries; code++) { if (hasSurrogateUnit(ub)) break; + totalDestBytes += ub.size(); out.push_back(CmapEntry{static_cast(code), loBytes, ub}); for (size_t idx = ub.size(); idx-- > 0;) { auto v = static_cast(ub[idx]); @@ -317,8 +330,13 @@ bool parseToUnicode(const std::shared_ptr& buf, std::vector& flushBfRange(); } } - flushBfChar(); - flushBfRange(); + if (section == CmapSection::BfChar) { + flushBfChar(); + } else if (section == CmapSection::BfRange && !inArray) { + flushBfRange(); + } else { + pendingHex.clear(); + } return !out.empty(); }