[ 
https://issues.apache.org/jira/browse/PDFBOX-6317?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18148149#comment-18148149
 ] 

Tilman Hausherr edited comment on PDFBOX-6317 at 11/20/26, 12:43 PM:
---------------------------------------------------------------------

Thank you. I have not fully understood the change, but for that I would need to 
understand that algorithm. However I didn't find anything wrong either, and it 
passes all the tests, including real world files that the AI knows nothing 
about.

In case something goes wrong in the future, these are the things that I didn't 
understand:
 - why not return TERMINATOR in the first read
 - does the problem only occur in the second read? (you seem to mention this in 
your text)
 - why set n to 0 at the first failed read? (somewhat answered in your text)
 - what is the role of "n"?

I could probably ask Claude to explain these, but it's not that important.


was (Author: tilman):
Thank you. I have not fully understood the change, but for that I would need to 
understand that algorithm. However I didn't find anything wrong either, and it 
passes all the tests, including real world files that the AI knows nothing 
about.

In case something goes wrong in the future, these are the things that I didn't 
understand:
 - why not return TERMINATOR in the first read
 - does the problem only occur in the second read? (you seem to mention this in 
your text)
 - why set n to 0 at the first failed read? (somewhat answered in your text)
 - what is the role of "n"?

> 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: 2.0.37, 3.0.8 PDFBox, 4.0.0
>            Reporter: Martin Mirchev
>            Assignee: Tilman Hausherr
>            Priority: Major
>              Labels: Ascii85Decode
>             Fix For: 2.0.38, 3.0.9 PDFBox, 4.0.0
>
>         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