Skip to content

Commit

Permalink
Fix parseStream bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopding committed Jul 6, 2018
1 parent 75f1417 commit c3925c9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
22 changes: 15 additions & 7 deletions src/core/pdf-parser/parseStream.ts
@@ -1,6 +1,12 @@
import { PDFDictionary, PDFName, PDFRawStream } from 'core/pdf-objects';
import { PDFObjectStream } from 'core/pdf-structures';
import { arrayIndexOf, arrayToString, error, trimArray } from 'utils';
import {
arrayIndexOf,
arrayIndexOneOf,
arrayToString,
error,
trimArray,
} from 'utils';

import PDFObjectIndex from 'core/pdf-document/PDFObjectIndex';

Expand Down Expand Up @@ -37,11 +43,13 @@ const parseStream = (
dictionary to jump to the end of the stream, instead of traversing each byte.
*/
// Locate the end of the stream
const endstreamIdx =
arrayIndexOf(trimmed, '\nendstream') ||
arrayIndexOf(trimmed, '\rendstream') ||
arrayIndexOf(trimmed, 'endstream');
if (!endstreamIdx && endstreamIdx !== 0) error('Invalid Stream!');
const endstreamMatchTuple = arrayIndexOneOf(trimmed, [
'\nendstream',
'\rendstream',
'endstream',
]);
if (!endstreamMatchTuple) error('Invalid Stream!');
const [endstreamIdx, endstreamMatch] = endstreamMatchTuple!;

/*
TODO: See if it makes sense to .slice() the stream contents, even though this
Expand All @@ -56,7 +64,7 @@ const parseStream = (
error('Invalid Stream!');
}

return [contents, trimmed.subarray(endstreamIdx + 10)];
return [contents, trimmed.subarray(endstreamIdx + endstreamMatch.length)];
};

/**
Expand Down
35 changes: 28 additions & 7 deletions src/utils/index.ts
Expand Up @@ -69,7 +69,7 @@ export const charCodes = (str: string) =>
export const typedArrayFor = (str: string) => new Uint8Array(charCodes(str));

export const arrayToString = (
arr: Uint8Array,
arr: Uint8Array | number[],
startAt = 0,
stopAt?: number,
) => {
Expand Down Expand Up @@ -112,12 +112,6 @@ export const arrayIndexOf = (
targetStr: string,
startFrom = 0,
) => {
// validate(
// startFrom,
// and(_.isNumber, not(_.isNaN)),
// `startFrom must be a number, found: "${startFrom}"`,
// );

const targetArr = targetStr.split('').map((c) => c.charCodeAt(0));
let currIdx = startFrom;

Expand All @@ -138,6 +132,33 @@ export const arrayIndexOf = (
return currIdx;
};

export const arrayIndexOneOf = (
arr: any[] | Uint8Array,
targetStrings: string[],
startFrom = 0,
): [number, string] | void => {
const targetArrs = targetStrings.map((str) => str.split('').map(charCode));
let currIdx = startFrom;
let match = null;

while (!match) {
currIdx += 1;
if (currIdx >= arr.length) return undefined;
match = targetArrs.find((target) =>
arraysAreEqual(
arr,
currIdx,
currIdx + target.length,
target,
0,
target.length,
),
);
}

return [currIdx, arrayToString(match)];
};

export const arrayIndexOfReverse = (
arr: any[],
targetStr: string,
Expand Down

0 comments on commit c3925c9

Please sign in to comment.