Martin Mirchev created PDFBOX-6330:
--------------------------------------

             Summary: ASCIIHexDecode and ASCII85Decode don't ignore all PDF 
white-space, so valid streams decode wrongly
                 Key: PDFBOX-6330
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6330
             Project: PDFBox
          Issue Type: Bug
          Components: Parsing
    Affects Versions: 4.0.0, 3.0.8 PDFBox
            Reporter: Martin Mirchev
         Attachments: a85-ok.pdf, a85-tabs.pdf, ahx-ok.pdf, ahx-wrapped-odd.pdf

ISO 32000-1 (7.4.2 and 7.4.3) says that white-space characters are ignored in 
ASCIIHexDecode and ASCII85Decode data, and PDF white-space is NUL, HT, LF, FF, 
CR and SP (Table 1). Two cases go wrong on trunk:

* {{ASCIIHexFilter.decode()}} skips white-space only before the first digit of 
a pair. If white-space falls between the two digits, e.g. hex data wrapped at 
an odd column, the white-space character is used as the second digit 
({{REVERSE_HEX}} gives -1), an error is logged and a wrong byte is written: 
{{4\n142>}} decodes to {{3F 14 20}} instead of {{41 42}}.
* {{ASCII85InputStream}} skips only SP, CR and LF. A tab, form feed or NUL 
between groups makes decoding fail with "Invalid data in Ascii85 stream".

Attached are two pages whose content stream ({{BT /F1 24 Tf 72 700 Td (Hello 
PDFBox) Tj ET}}) is encoded this way: {{ahx-wrapped-odd.pdf}} (ASCIIHex in 
31-character lines) and {{a85-tabs.pdf}} (ASCII85 with a tab every 10 
characters). {{PDFTextStripper.getText()}} returns an empty string for both, 
and the pages render blank; {{ahx-ok.pdf}} and {{a85-ok.pdf}}, the same content 
without the extra white-space, extract "Hello PDFBox". Each result was the same 
in 3 runs.

A possible change; with it, both files extract "Hello PDFBox" and the two 
control files are unchanged:

{code}
--- a/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCIIHexFilter.java
+++ b/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCIIHexFilter.java
@@ -86,6 +86,10 @@
             }
             int value = REVERSE_HEX[firstByte] * 16;
             int secondByte = encoded.read();
+            while (isWhitespace(secondByte))
+            {
+                secondByte = encoded.read();
+            }
        
             if (secondByte == -1 || isEOD(secondByte)) 
             {
--- a/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCII85InputStream.java
+++ b/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCII85InputStream.java
@@ -43,6 +43,12 @@
     private static final char PADDING_U = 'u';
     private static final char Z = 'z';
 
+    // PDF white-space characters: NUL, HT, LF, FF, CR and SP
+    private static boolean isWhitespace(byte c)
+    {
+        return c == 0 || c == '\t' || c == NEWLINE || c == '\f' || c == RETURN 
|| c == SPACE;
+    }
+
     /**
      * Constructor.
      *
@@ -86,7 +92,7 @@
                     return -1;
                 }
                 z = (byte) zz;
-            } while (z == NEWLINE || z == RETURN || z == SPACE);
+            } while (isWhitespace(z));
 
             if (z == TERMINATOR)
             {
@@ -114,7 +120,7 @@
                             return -1;
                         }
                         z = (byte) zz;
-                    } while (z == NEWLINE || z == RETURN || z == SPACE);
+                    } while (isWhitespace(z));
                     ascii[k] = z;
                     if (z == TERMINATOR)
                     {
{code}

This issue was found by an LLM-assisted tool and verified manually.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to