Martin Mirchev created PDFBOX-6337:
--------------------------------------
Summary: Literal strings: an unescaped CR / CR LF is not read as
LF, and a backslash before EOL drops all following EOLs
Key: PDFBOX-6337
URL: https://issues.apache.org/jira/browse/PDFBOX-6337
Project: PDFBox
Issue Type: Bug
Components: Parsing
Affects Versions: 4.0.0, 3.0.8 PDFBox
Reporter: Martin Mirchev
Attachments: litstr.pdf
ISO 32000-1 7.3.4.2 says that an end-of-line marker inside a literal string
without a preceding backslash "shall be
treated as a byte value of (0Ah), irrespective of whether the end-of-line
marker was a CARRIAGE RETURN (0Dh), a LINE
FEED (0Ah), or both", and that a backslash followed by an end-of-line marker
means neither is part of the string.
{{BaseParser.readLiteralString()}} differs in two ways:
* an unescaped CR or CR LF is copied as is, so {{(a<CR><LF>b)}} gives "a\r\nb"
and {{(c<CR>d)}} gives "c\rd" instead of "a\nb" and "c\nd";
* after a backslash and an end-of-line marker it skips all following CR and LF
bytes, so {{(e\<LF><LF>f)}} gives "ef" instead of "e\nf".
The attached {{litstr.pdf}} has these strings as the /Title, /Subject, /Author
and /Keywords of the Info dictionary:
{noformat}
Title (a CR LF b) -> a\x0d\x0ab expected a\x0ab
Subject (c CR d) -> c\x0dd expected c\x0ad
Author (e \ LF LF f) -> ef expected e\x0af
Keywords (g \ CR LF h) -> gh (correct)
{noformat}
A possible change; with it the four values are a\nb, c\nd, e\nf and gh, and the
io, fontbox, xmpbox and pdfbox
unit tests pass:
{code}
diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java
b/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java
index 4eb058f..de7288e 100644
--- a/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java
+++ b/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java
@@ -304,14 +304,15 @@ public abstract class BaseParser
out.write(next);
break;
case ASCII_LF:
+ // a backslash before an end-of-line marker continues the
string on the
+ // next line; only this end-of-line marker is dropped
(7.3.4.2)
+ break;
case ASCII_CR:
- // this is a break in the line so ignore it and the
newline and continue
c = source.read();
- while (isEOL(c) && c != -1)
+ if (c != ASCII_LF)
{
- c = source.read();
+ nextc = c;
}
- nextc = c;
break;
case '0':
case '1':
@@ -362,6 +363,16 @@ public abstract class BaseParser
out.write(next);
}
}
+ else if (ch == ASCII_CR)
+ {
+ // an unescaped end-of-line marker (CR, LF or CR LF) is read
as a single LF (7.3.4.2)
+ out.write(ASCII_LF);
+ c = source.read();
+ if (c != ASCII_LF)
+ {
+ nextc = c;
+ }
+ }
else
{
out.write(ch);
{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]