Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ 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
Expand Down
1 change: 1 addition & 0 deletions jpdfium/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ val jpdfiumFunctions = listOf(
"jpdfium_redact_words_ex", "jpdfium_crop_remove_content",
"jpdfium_crop_remove_content_ex",
"jpdfium_page_flatten", "jpdfium_page_to_image",
"jpdfium_doc_rebuild_clean", "jpdfium_page_replace_with_jpeg",
"jpdfium_doc_flatten_annotations", "jpdfium_doc_flatten_to_images",
// Advanced Pattern Engine (PCRE2 JIT)
"jpdfium_pcre2_compile", "jpdfium_pcre2_match_all", "jpdfium_pcre2_free",
Expand Down
111 changes: 84 additions & 27 deletions jpdfium/src/main/java/stirling/software/jpdfium/PdfDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@
import stirling.software.jpdfium.panama.PageEditBindings;
import stirling.software.jpdfium.panama.PdfiumRuntime;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.lang.foreign.Arena;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
Expand Down Expand Up @@ -1054,55 +1063,103 @@ public void flatten(FlattenMode mode) {
* @param dpi render resolution for {@link FlattenMode#FULL} (ignored for other modes)
*/
public void flatten(FlattenMode mode, int dpi) {
flatten(mode, dpi, true);
}

/**
* Flatten all pages using the specified mode, DPI, and annotation visibility behavior.
*
* @param mode what to flatten (see {@link FlattenMode})
* @param dpi render resolution for {@link FlattenMode#FULL} (ignored for other modes)
* @param includeNonPrintableAnnotations {@code true} for PDFBox behavior (render all visible
* annotations and form fields); {@code false} for PDFium
* print behavior ({@code FPDF_PRINTING}, skipping
* non-printable annotations)
*/
public void flatten(FlattenMode mode, int dpi, boolean includeNonPrintableAnnotations) {
ensureOpen();
if (mode == null) throw new IllegalArgumentException("mode must not be null");
switch (mode) {
case ANNOTATIONS -> {
// Flatten rewrites page content in place and keeps every open page
// valid, so a partial pass only needs the content flag.
try {
JpdfiumLib.docFlattenAnnotations(handle);
JpdfiumLib.docFlattenAnnotationsDisplay(handle);
} finally {
// A failed pass can still have baked the pages before it.
markContentModified();
}
}
case FULL -> {
// Validate before the try: the Java guard in JpdfiumLib throws
// for a non-positive dpi before any native work, and the finally
// below would otherwise mark pages stale for an untouched
// document.
if (dpi <= 0) throw new IllegalArgumentException("dpi must be > 0");
// Widget values often exist only as annotation appearances.
// PDFium's raster pass does not initialize a form-fill
// environment, so bake widgets into page content first; the
// raster then contains the same values a viewer shows.
if (securityHandlerRevision() > 0) {
throw new JPDFiumException(
"full flatten would remove encryption; decrypt the document first");
}
// Flatten form widgets and annotations into page content before
// rasterizing. FPDF_ANNOT alone does not draw widgets; they only
// reach the bitmap after FPDFPage_Flatten bakes their appearances.
boolean hadAnnotations = hasAnnotations();
try {
JpdfiumLib.docFlattenAnnotationsDisplay(handle);
JpdfiumLib.docFlattenAnnotations(handle);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Functional Correctness · Quick win

Supported by static analysis

Script executed:

#!/bin/bash
set -euo pipefail
ast-grep outline jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumLib.java \
  --match 'docFlattenAnnotations|pageFlatten'
ast-grep outline native/bridge/src/jpdfium_redact.cpp \
  --match 'flatten|Flatten|hideNoViewAnnotations'
rg -n -A100 -B8 \
  'docFlattenAnnotations\(|jpdfium_doc_flatten_annotations\(|hideNoViewAnnotations\(|FPDFPage_Flatten\(' \
  jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumLib.java \
  native/bridge/src/jpdfium_redact.cpp

Repository: Stirling-Tools/JPDFium

Length of output: 29286


Use the display-only annotation bake for FULL flattening.

docFlattenAnnotations bakes no-view markup into page content as well. FULL mode rasterizes each page immediately after this bake. Any no-view annotation therefore appears in the JPEG output, though viewers hide it. Call docFlattenAnnotationsDisplay instead. It excludes no-view markup and still bakes visible annotations, which matches its documented use before rasterization.

Suggested fix

-                    JpdfiumLib.docFlattenAnnotations(handle);
+                    JpdfiumLib.docFlattenAnnotationsDisplay(handle);
Committable suggestion

IMPORTANT
Review the replacement and indentation, then test before committing.

Suggested change
JpdfiumLib.docFlattenAnnotations(handle);
JpdfiumLib.docFlattenAnnotationsDisplay(handle);

Source: Coding guidelines

} finally {
// The bake rewrites page content and keeps open pages valid.
// A later raster failure only rolls back the raster pass, so
// the annotation edit must stay visible as a modified document.
if (hadAnnotations) markContentModified();
}
// The native pass rolls back any failure it can still undo and reports
// CONTENT_MUTATED only once the replaced pages can no longer be restored.
boolean replacedPages = false;
try {
JpdfiumLib.docFlattenToImages(handle, dpi);
// An empty document leaves nothing replaced, so nothing is stale.
replacedPages = pageCount() > 0;
} catch (JPDFiumException e) {
replacedPages = e.isContentMutated();
throw e;
} finally {
if (replacedPages) {
invalidateOpenPages();
markContentModified();
int count = pageCount();
List<byte[]> jpegs = new ArrayList<>(count);
float[] widths = new float[count];
float[] heights = new float[count];
for (int i = 0; i < count; i++) {
try (PdfPage p = page(i)) {
var sz = p.size();
widths[i] = sz.width();
heights[i] = sz.height();
int renderFlags = includeNonPrintableAnnotations ? 0x01 : (0x01 | 0x800);
Comment thread
balazs-szucs marked this conversation as resolved.
Resolved
BufferedImage img =
p.renderAtFlags(dpi, renderFlags).toBufferedImage();
jpegs.add(encodeJpeg(img, 0.85f));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Stability & Availability · Heavy lift

Do not retain every page JPEG on the Java heap.

The first pass stores all encoded pages before replacement starts. Heap retention therefore grows with the complete rasterized document, in addition to each active render and RGB image. A per-page render limit does not bound this list.

Spool prepared JPEGs to owned temporary storage, or prepare a separate destination incrementally with bounded page buffers. Preserve preparation-before-publication and failure cleanup.

As per coding guidelines, “Flag unbounded readAllBytes, complete-document buffers, repeated serialization/reload, and unnecessary full-result copies.”

Source: Coding guidelines

}
}
for (int i = 0; i < count; i++) {
JpdfiumLib.pageReplaceWithJpeg(handle, i, jpegs.get(i), widths[i], heights[i]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Data Integrity & Integration · Quick win

Reconcile Java state after a partial replacement failure.

If page 0 replacement succeeds and page 1 replacement fails, page 0 remains replaced and native generation has advanced. The exception skips invalidateOpenPages() and markContentModified(). For an annotation-free input, Java still reports unchanged content and accepts the old page epoch.

Track completed replacements and reconcile state in finally, or publish a separately prepared document atomically. Keep rejection before the first mutation from invalidating untouched pages. Add failure injection after one successful replacement.

As per coding guidelines, “Preserve stable public behavior” for ownership and exception semantics.

Source: Coding guidelines

Comment on lines +1123 to +1124

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Stability & Availability · Quick win

Keep the destructive replacement phase inside one admission.

If another thread calls PdfiumRuntime.quiesce() during page 0 replacement, that replacement can finish. Page 1 then makes a new admission and throws because the runtime is QUIESCING. FULL cannot finish its accepted replacement pass or rebuild the document. Nested operations inside one admitted batch can continue after quiescence. (raw.githubusercontent.com)

Keep JPEG encoding outside the domain. Run the replacement loop, rebuild, and state reconciliation inside one executeBatch. Preserve failure bookkeeping for genuine native failures. Add a latch-controlled regression that quiesces during the first replacement and checks completion.

As per coding guidelines, “accepted batches must follow their documented continuation contract.”

Source: Coding guidelines

}
JpdfiumLib.docRebuildClean(handle);
invalidateOpenPages();
markContentModified();
}
}
}

void replacePageWithJpeg(int pageIndex, byte[] jpegBytes, float wPt, float hPt) {
JpdfiumLib.pageReplaceWithJpeg(handle, pageIndex, jpegBytes, wPt, hPt);
}

static byte[] encodeJpeg(BufferedImage image, float quality) {
try {
BufferedImage rgb = new BufferedImage(
image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = rgb.createGraphics();
try {
g.setColor(Color.WHITE);
g.fillRect(0, 0, rgb.getWidth(), rgb.getHeight());
g.drawImage(image, 0, 0, null);
} finally {
g.dispose();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
try (ImageOutputStream ios = ImageIO.createImageOutputStream(baos)) {
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
writer.write(null, new IIOImage(rgb, null, null), param);
} finally {
writer.dispose();
}
return baos.toByteArray();
} catch (IOException e) {
throw new UncheckedIOException("Failed to encode flattened page as JPEG", e);
}
}

Expand Down
13 changes: 12 additions & 1 deletion jpdfium/src/main/java/stirling/software/jpdfium/PdfPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public PageSize size() {
* @param dpi render resolution in DPI; must be positive
* @return rendered pixels and the resulting dimensions
*/
RenderResult renderAtFlags(int dpi, int flags) {
ensureOpen();
return JpdfiumLib.renderPage(handle, dpi, false, flags);
}

public RenderResult renderAt(int dpi) {
return renderAt(dpi, false, null);
}
Expand Down Expand Up @@ -1222,8 +1227,14 @@ public void flatten(FlattenMode mode, int dpi) {
case ANNOTATIONS -> flatten();
case FULL -> {
if (dpi <= 0) throw new IllegalArgumentException("dpi must be > 0");
var sz = size();
float wPt = sz.width();
float hPt = sz.height();
byte[] jpegBytes = PdfDocument.encodeJpeg(renderAt(dpi).toBufferedImage(), 0.85f);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial · Performance & Scalability · Quick win

Supported by static analysis

Script executed:

rg -n 'renderImage|renderAt\(|renderAtFlags|renderPageImage|toBufferedImage' jpdfium/src/main/java/stirling/software/jpdfium/PdfPage.java
sed -n '100,130p' jpdfium/src/main/java/stirling/software/jpdfium/PdfPage.java

Repository: Stirling-Tools/JPDFium

Length of output: 2930


Script executed:

cat -n jpdfium/src/main/java/stirling/software/jpdfium/PdfPage.java | sed -n '150,240p'
cat -n jpdfium/src/main/java/stirling/software/jpdfium/PdfPage.java | sed -n '1220,1245p'
rg -n -F -e 'renderPageImage' -e 'static RenderResult renderPage' -e 'renderPage(' -- jpdfium/src/main/java/stirling/software/jpdfium/ || test "$?" -eq 1
rg -n -F -e 'static BufferedImage encodeJpeg' -e 'encodeJpeg(' -- jpdfium/src/main/java/stirling/software/jpdfium/PdfDocument.java || test "$?" -eq 1

Repository: Stirling-Tools/JPDFium

Length of output: 8557


Script executed:

cat -n jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumLib.java | sed -n '1074,1145p'
cat -n jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumLib.java | sed -n '1240,1295p'
rg -n -F -e 'toBufferedImage' -e 'class RenderResult' -e 'record RenderResult' -- jpdfium/src/main/java/stirling/software/jpdfium/ || test "$?" -eq 1

Repository: Stirling-Tools/JPDFium

Length of output: 10550


Script executed:

cat -n jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumLib.java | sed -n '1145,1200p'
rg -n -F -e 'jpdfium_render_page_into' -e 'jpdfium_render_page_flags' -- . || test "$?" -eq 1

Repository: Stirling-Tools/JPDFium

Length of output: 11877


Script executed:

cat -n native/bridge/src/jpdfium_render.cpp | sed -n '330,430p'
rg -n -F -e 'FPDF_LCD_TEXT' -e 'FPDF_ANNOT' -e 'FPDF_FFLDraw' -e 'FFLDraw' -e 'FPDFBitmap_FillRect' -- native/bridge/src || test "$?" -eq 1

Repository: Stirling-Tools/JPDFium

Length of output: 8939


Use renderImage(dpi) to avoid the heap RGBA copy in FULL flattening.

renderAt(dpi) copies the native RGBA buffer into a heap byte[] and then converts it with a per-pixel pack loop. renderImage(dpi) calls JpdfiumLib.renderPageImage on real natives. That path uses the same opaque white background, the same FPDF_ANNOT | FPDF_LCD_TEXT flags for flags 0, and no form drawing. It copies the native target directly into the int raster. The full-frame heap byte[] and the per-pixel pack loop are removed. A native arena target still exists, so the saving is heap traffic and pack work, not every intermediate buffer. The stub runtime keeps the current conversion.

Suggested fix

--- "a/jpdfium/src/main/java/stirling/software/jpdfium/PdfPage.java"
+++ "b/jpdfium/src/main/java/stirling/software/jpdfium/PdfPage.java"
@@ -1230,7 +1230,7 @@
                 var sz = size();
                 float wPt = sz.width();
                 float hPt = sz.height();
-                byte[] jpegBytes = PdfDocument.encodeJpeg(renderAt(dpi).toBufferedImage(), 0.85f);
+                byte[] jpegBytes = PdfDocument.encodeJpeg(renderImage(dpi), 0.85f);
                 close();
                 ownerDoc.replacePageWithJpeg(pageIndex, jpegBytes, wPt, hPt);
                 ownerDoc.invalidateOpenPages();
Committable suggestion

IMPORTANT
Review the replacement and indentation, then test before committing.

Suggested change
byte[] jpegBytes = PdfDocument.encodeJpeg(renderAt(dpi).toBufferedImage(), 0.85f);
byte[] jpegBytes = PdfDocument.encodeJpeg(renderImage(dpi), 0.85f);

close();
ownerDoc.convertPageToImage(pageIndex, dpi);
ownerDoc.replacePageWithJpeg(pageIndex, jpegBytes, wPt, hPt);
ownerDoc.invalidateOpenPages();
ownerDoc.markContentModified();
}
}
}
Expand Down
125 changes: 125 additions & 0 deletions jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumH.java
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,131 @@ public static int jpdfium_page_to_image(long doc, int pageIndex, int dpi) {
}
}

private static class jpdfium_doc_rebuild_clean {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
public static final FunctionDescriptor DESC = FunctionDescriptor.of(
JpdfiumH.C_INT,
JpdfiumH.C_LONG
);

public static final MemorySegment ADDR = SYMBOL_LOOKUP.findOrThrow("jpdfium_doc_rebuild_clean");

public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
}

/**
* Function descriptor for:
* {@snippet lang=c :
* int32_t jpdfium_doc_rebuild_clean(int64_t doc)
* }
*/
public static FunctionDescriptor jpdfium_doc_rebuild_clean$descriptor() {
return jpdfium_doc_rebuild_clean.DESC;
}

/**
* Downcall method handle for:
* {@snippet lang=c :
* int32_t jpdfium_doc_rebuild_clean(int64_t doc)
* }
*/
public static MethodHandle jpdfium_doc_rebuild_clean$handle() {
return jpdfium_doc_rebuild_clean.HANDLE;
}

/**
* Address for:
* {@snippet lang=c :
* int32_t jpdfium_doc_rebuild_clean(int64_t doc)
* }
*/
public static MemorySegment jpdfium_doc_rebuild_clean$address() {
return jpdfium_doc_rebuild_clean.ADDR;
}

/**
* {@snippet lang=c :
* int32_t jpdfium_doc_rebuild_clean(int64_t doc)
* }
*/
public static int jpdfium_doc_rebuild_clean(long doc) {
var mh$ = jpdfium_doc_rebuild_clean.HANDLE;
try {
if (TRACE_DOWNCALLS) {
traceDowncall("jpdfium_doc_rebuild_clean", doc);
}
return (int)mh$.invokeExact(doc);
} catch (Error | RuntimeException ex) {
throw ex;
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
}
}

private static class jpdfium_page_replace_with_jpeg {
public static final FunctionDescriptor DESC = FunctionDescriptor.of(
JpdfiumH.C_INT,
JpdfiumH.C_LONG,
JpdfiumH.C_INT,
JpdfiumH.C_POINTER,
JpdfiumH.C_LONG,
JpdfiumH.C_FLOAT,
JpdfiumH.C_FLOAT
);

public static final MemorySegment ADDR = SYMBOL_LOOKUP.findOrThrow("jpdfium_page_replace_with_jpeg");

public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
}

/**
* Function descriptor for:
* {@snippet lang=c :
* int32_t jpdfium_page_replace_with_jpeg(int64_t doc, int32_t pageIndex, const uint8_t *jpegData, int64_t jpegLen, float wPt, float hPt)
* }
*/
public static FunctionDescriptor jpdfium_page_replace_with_jpeg$descriptor() {
return jpdfium_page_replace_with_jpeg.DESC;
}

/**
* Downcall method handle for:
* {@snippet lang=c :
* int32_t jpdfium_page_replace_with_jpeg(int64_t doc, int32_t pageIndex, const uint8_t *jpegData, int64_t jpegLen, float wPt, float hPt)
* }
*/
public static MethodHandle jpdfium_page_replace_with_jpeg$handle() {
return jpdfium_page_replace_with_jpeg.HANDLE;
}

/**
* Address for:
* {@snippet lang=c :
* int32_t jpdfium_page_replace_with_jpeg(int64_t doc, int32_t pageIndex, const uint8_t *jpegData, int64_t jpegLen, float wPt, float hPt)
* }
*/
public static MemorySegment jpdfium_page_replace_with_jpeg$address() {
return jpdfium_page_replace_with_jpeg.ADDR;
}

/**
* {@snippet lang=c :
* int32_t jpdfium_page_replace_with_jpeg(int64_t doc, int32_t pageIndex, const uint8_t *jpegData, int64_t jpegLen, float wPt, float hPt)
* }
*/
public static int jpdfium_page_replace_with_jpeg(long doc, int pageIndex, MemorySegment jpegData, long jpegLen, float wPt, float hPt) {
var mh$ = jpdfium_page_replace_with_jpeg.HANDLE;
try {
if (TRACE_DOWNCALLS) {
traceDowncall("jpdfium_page_replace_with_jpeg", doc, pageIndex, jpegData, jpegLen, wPt, hPt);
}
return (int)mh$.invokeExact(doc, pageIndex, jpegData, jpegLen, wPt, hPt);
} catch (Error | RuntimeException ex) {
throw ex;
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
}
}

private static class jpdfium_doc_flatten_annotations {
public static final FunctionDescriptor DESC = FunctionDescriptor.of(
JpdfiumH.C_INT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,25 @@ public static void pageToImage(long doc, int pageIndex, int dpi) {
});
}

public static void docRebuildClean(long doc) {
PdfiumRuntime.execute(() -> {
check(JpdfiumH.jpdfium_doc_rebuild_clean(doc), "docRebuildClean");
});
}

public static void pageReplaceWithJpeg(long doc, int pageIndex, byte[] jpegData, float wPt, float hPt) {
if (jpegData == null || jpegData.length == 0) {
throw new IllegalArgumentException("jpegData must not be empty");
}
PdfiumRuntime.execute(() -> {
try (Arena arena = Arena.ofConfined()) {
MemorySegment seg = arena.allocateFrom(JAVA_BYTE, jpegData);
check(JpdfiumH.jpdfium_page_replace_with_jpeg(doc, pageIndex, seg, jpegData.length, wPt, hPt),
"pageReplaceWithJpeg");
}
});
}

/**
* Bakes annotations and form fields into page content on every page, in one native pass over
* the document handle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@
],
"foreign": {
"downcalls": [
{
"returnType": "jint",
"parameterTypes": ["jlong", "jint", "void*", "jlong", "jfloat", "jfloat"]
},
{
"returnType": "jint",
"parameterTypes": ["void*", "jlong", "jint", "void*", "jint", "jint", "jint", "void*", "void*", "void*", "jint"]
Expand Down
Loading
Loading