Martin Mirchev created PDFBOX-6317:
--------------------------------------

             Summary: ASCII85Decode repeats the last bytes when the stream has 
no ~> end-of-data marker
                 Key: PDFBOX-6317
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6317
             Project: PDFBox
          Issue Type: Bug
          Components: Parsing
    Affects Versions: 4.0.0, 3.0.8 PDFBox
            Reporter: Martin Mirchev
         Attachments: ascii85-no-eod.pdf

When an ASCII85 stream ends without the {{~>}} end-of-data marker, 
{{ASCII85InputStream}} returns the previous group a second time, and an 
incomplete last group is lost:

||input||decoded||
|{{87cURD]i,"Ebo80~>}}|{{Hello World!}}|
|{{87cURD]i,"Ebo80}}|{{Hello World!rld!}}|
|{{87cURD]i~>}}|{{Hello }}|
|{{87cURD]i}}|{{HellHell}}|
|{{z}}|8 zero bytes|

In {{read()}}, {{index}} is set to 0 before the next byte is read; at EOF the 
method returns -1 but {{n}} still holds the size of the previous group, so the 
next {{read(byte[], int, int)}} call returns those bytes again. At EOF inside a 
group the partial group is discarded instead of being decoded as {{~>}} would. 
In addition, {{(byte) in.read()}} makes a 0xFF byte look like EOF; other 
invalid bytes throw "Invalid data in Ascii85 stream".

Attached {{ascii85-no-eod.pdf}} has a page content stream {{0 0 m 90 90 l S}} 
encoded with {{/ASCII85Decode}} and no {{~>}}. {{doc.getPage(0).getContents()}} 
returns {{0 0 m 90 90  90 }}, so the {{l S}} operators are lost (trunk bd0c9c0, 
3/3 runs).

A possible change (tested: the cases above decode as with {{~>}}, 0xFF throws 
the invalid-data IOException, {{TestFilters}} unchanged):

{code}
--- a/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCII85InputStream.java
+++ b/pdfbox/src/main/java/org/apache/pdfbox/filter/ASCII85InputStream.java
@@ -79,10 +79,11 @@
             byte z;
             do
             {
-                int zz = (byte) in.read();
+                int zz = in.read();
                 if (zz == -1)
                 {
                     eof = true;
+                    n = 0;
                     return -1;
                 }
                 z = (byte) zz;
@@ -107,13 +108,9 @@
                 {
                     do
                     {
-                        int zz = (byte) in.read();
-                        if (zz == -1)
-                        {
-                            eof = true;
-                            return -1;
-                        }
-                        z = (byte) zz;
+                        int zz = in.read();
+                        // a missing end-of-data marker ends the last group 
like ~>
+                        z = zz == -1 ? (byte) TERMINATOR : (byte) zz;
                     } while (z == NEWLINE || z == RETURN || z == SPACE);
                     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