Skip to content
Open
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
15 changes: 14 additions & 1 deletion src/core/parser/PDFObjectParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,36 @@ class PDFObjectParser extends BaseParser {

protected findEndOfStreamFallback(startPos: Position) {
// Move to end of stream, while handling nested streams
let acceptUnprefixedStream = true;
let nestingLvl = 1;
let end = this.bytes.offset();

while (!this.bytes.done()) {
end = this.bytes.offset();

if (this.matchKeyword(Keywords.stream)) {
if (
this.matchKeyword(Keywords.embeddedStream1) ||
this.matchKeyword(Keywords.embeddedStream2) ||
this.matchKeyword(Keywords.embeddedStream3) ||
this.matchKeyword(Keywords.embeddedStream4) ||
this.matchKeyword(Keywords.embeddedStream5) ||
this.matchKeyword(Keywords.embeddedStream6) ||
this.matchKeyword(Keywords.embeddedStream7) ||
(acceptUnprefixedStream && this.matchKeyword(Keywords.stream))
) {
nestingLvl += 1;
acceptUnprefixedStream = true;
} else if (
this.matchKeyword(Keywords.EOF1endstream) ||
this.matchKeyword(Keywords.EOF2endstream) ||
this.matchKeyword(Keywords.EOF3endstream) ||
this.matchKeyword(Keywords.endstream)
) {
nestingLvl -= 1;
acceptUnprefixedStream = true;
} else {
this.bytes.next();
acceptUnprefixedStream = false;
}

if (nestingLvl === 0) break;
Expand Down
17 changes: 16 additions & 1 deletion src/core/syntax/Keywords.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import CharCodes from 'src/core/syntax/CharCodes';

const { Space, CarriageReturn, Newline } = CharCodes;
const {
Space,
CarriageReturn,
Newline,
Tab,
LessThan,
GreaterThan,
BackSlash,
} = CharCodes;

const stream = [
CharCodes.s,
Expand Down Expand Up @@ -76,6 +84,13 @@ export const Keywords = {
streamEOF2: [...stream, CarriageReturn, Newline],
streamEOF3: [...stream, CarriageReturn],
streamEOF4: [...stream, Newline],
embeddedStream1: [Tab, ...stream],
embeddedStream2: [Space, ...stream],
embeddedStream3: [CarriageReturn, ...stream],
embeddedStream4: [Newline, ...stream],
embeddedStream5: [LessThan, ...stream],
embeddedStream6: [GreaterThan, ...stream],
embeddedStream7: [BackSlash, ...stream],
endstream,
EOF1endstream: [CarriageReturn, Newline, ...endstream],
EOF2endstream: [CarriageReturn, ...endstream],
Expand Down
4 changes: 4 additions & 0 deletions tests/core/parser/PDFObjectParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ describe(`PDFObjectParser`, () => {
'<<>>\n\rstream\n\rthingz\n\rendstream',
'<<\n/Length 8\n>>\nstream\n\rthingz\n\nendstream',
],
[
'<<>>\n\rstream\n\rthingz bitstream\n\rendstream',
'<<\n/Length 18\n>>\nstream\n\rthingz bitstream\n\nendstream',
],
].forEach(([input, output]) => {
it(`can parse ${JSON.stringify(input)}`, () => {
const object = parse(typedArrayFor(input));
Expand Down