Convert the Parser
class in src/core/parser.js
to ES6 syntax
This commit is contained in:
parent
d587abbceb
commit
7d0ecee771
@ -34,7 +34,7 @@ const MAX_LENGTH_TO_CACHE = 1000;
|
||||
const MAX_ADLER32_LENGTH = 5552;
|
||||
|
||||
function computeAdler32(bytes) {
|
||||
let bytesLength = bytes.length;
|
||||
const bytesLength = bytes.length;
|
||||
if (typeof PDFJSDev === 'undefined' ||
|
||||
PDFJSDev.test('!PRODUCTION || TESTING')) {
|
||||
assert(bytesLength < MAX_ADLER32_LENGTH,
|
||||
@ -49,22 +49,23 @@ function computeAdler32(bytes) {
|
||||
return ((b % 65521) << 16) | (a % 65521);
|
||||
}
|
||||
|
||||
var Parser = (function ParserClosure() {
|
||||
function Parser(lexer, allowStreams, xref, recoveryMode) {
|
||||
class Parser {
|
||||
constructor(lexer, allowStreams, xref, recoveryMode = false) {
|
||||
this.lexer = lexer;
|
||||
this.allowStreams = allowStreams;
|
||||
this.xref = xref;
|
||||
this.recoveryMode = recoveryMode || false;
|
||||
this.recoveryMode = recoveryMode;
|
||||
|
||||
this.imageCache = Object.create(null);
|
||||
this.refill();
|
||||
}
|
||||
|
||||
Parser.prototype = {
|
||||
refill: function Parser_refill() {
|
||||
refill() {
|
||||
this.buf1 = this.lexer.getObj();
|
||||
this.buf2 = this.lexer.getObj();
|
||||
},
|
||||
shift: function Parser_shift() {
|
||||
}
|
||||
|
||||
shift() {
|
||||
if (isCmd(this.buf2, 'ID')) {
|
||||
this.buf1 = this.buf2;
|
||||
this.buf2 = null;
|
||||
@ -72,8 +73,9 @@ var Parser = (function ParserClosure() {
|
||||
this.buf1 = this.buf2;
|
||||
this.buf2 = this.lexer.getObj();
|
||||
}
|
||||
},
|
||||
tryShift: function Parser_tryShift() {
|
||||
}
|
||||
|
||||
tryShift() {
|
||||
try {
|
||||
this.shift();
|
||||
return true;
|
||||
@ -85,9 +87,10 @@ var Parser = (function ParserClosure() {
|
||||
// state and call this.shift() twice to reset the buffers.
|
||||
return false;
|
||||
}
|
||||
},
|
||||
getObj: function Parser_getObj(cipherTransform) {
|
||||
var buf1 = this.buf1;
|
||||
}
|
||||
|
||||
getObj(cipherTransform) {
|
||||
const buf1 = this.buf1;
|
||||
this.shift();
|
||||
|
||||
if (buf1 instanceof Cmd) {
|
||||
@ -95,7 +98,7 @@ var Parser = (function ParserClosure() {
|
||||
case 'BI': // inline image
|
||||
return this.makeInlineImage(cipherTransform);
|
||||
case '[': // array
|
||||
var array = [];
|
||||
const array = [];
|
||||
while (!isCmd(this.buf1, ']') && !isEOF(this.buf1)) {
|
||||
array.push(this.getObj(cipherTransform));
|
||||
}
|
||||
@ -108,7 +111,7 @@ var Parser = (function ParserClosure() {
|
||||
this.shift();
|
||||
return array;
|
||||
case '<<': // dictionary or stream
|
||||
var dict = new Dict(this.xref);
|
||||
const dict = new Dict(this.xref);
|
||||
while (!isCmd(this.buf1, '>>') && !isEOF(this.buf1)) {
|
||||
if (!isName(this.buf1)) {
|
||||
info('Malformed dictionary: key must be a name object');
|
||||
@ -116,7 +119,7 @@ var Parser = (function ParserClosure() {
|
||||
continue;
|
||||
}
|
||||
|
||||
var key = this.buf1.name;
|
||||
const key = this.buf1.name;
|
||||
this.shift();
|
||||
if (isEOF(this.buf1)) {
|
||||
break;
|
||||
@ -144,9 +147,9 @@ var Parser = (function ParserClosure() {
|
||||
}
|
||||
|
||||
if (Number.isInteger(buf1)) { // indirect reference or integer
|
||||
var num = buf1;
|
||||
const num = buf1;
|
||||
if (Number.isInteger(this.buf1) && isCmd(this.buf2, 'R')) {
|
||||
var ref = new Ref(num, this.buf1);
|
||||
const ref = new Ref(num, this.buf1);
|
||||
this.shift();
|
||||
this.shift();
|
||||
return ref;
|
||||
@ -155,7 +158,7 @@ var Parser = (function ParserClosure() {
|
||||
}
|
||||
|
||||
if (isString(buf1)) { // string
|
||||
var str = buf1;
|
||||
let str = buf1;
|
||||
if (cipherTransform) {
|
||||
str = cipherTransform.decryptString(str);
|
||||
}
|
||||
@ -164,7 +167,8 @@ var Parser = (function ParserClosure() {
|
||||
|
||||
// simple object
|
||||
return buf1;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the end of the stream by searching for the /EI\s/.
|
||||
* @returns {number} The inline stream length.
|
||||
@ -183,7 +187,7 @@ var Parser = (function ParserClosure() {
|
||||
if (ch === SPACE || ch === LF || ch === CR) {
|
||||
maybeEIPos = stream.pos;
|
||||
// Let's check that the next `n` bytes are ASCII... just to be sure.
|
||||
let followingBytes = stream.peekBytes(n);
|
||||
const followingBytes = stream.peekBytes(n);
|
||||
for (let i = 0, ii = followingBytes.length; i < ii; i++) {
|
||||
ch = followingBytes[i];
|
||||
if (ch === NUL && followingBytes[i + 1] !== NUL) {
|
||||
@ -235,14 +239,14 @@ var Parser = (function ParserClosure() {
|
||||
endOffset--;
|
||||
}
|
||||
return ((stream.pos - endOffset) - startPos);
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the EOI (end-of-image) marker 0xFFD9 of the stream.
|
||||
* @returns {number} The inline stream length.
|
||||
*/
|
||||
findDCTDecodeInlineStreamEnd:
|
||||
function Parser_findDCTDecodeInlineStreamEnd(stream) {
|
||||
var startPos = stream.pos, foundEOI = false, b, markerLength, length;
|
||||
findDCTDecodeInlineStreamEnd(stream) {
|
||||
let startPos = stream.pos, foundEOI = false, b, markerLength, length;
|
||||
while ((b = stream.getByte()) !== -1) {
|
||||
if (b !== 0xFF) { // Not a valid marker.
|
||||
continue;
|
||||
@ -331,14 +335,15 @@ var Parser = (function ParserClosure() {
|
||||
}
|
||||
this.inlineStreamSkipEI(stream);
|
||||
return length;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the EOD (end-of-data) marker '~>' (i.e. TILDE + GT) of the stream.
|
||||
* @returns {number} The inline stream length.
|
||||
*/
|
||||
findASCII85DecodeInlineStreamEnd(stream) {
|
||||
var TILDE = 0x7E, GT = 0x3E;
|
||||
var startPos = stream.pos, ch, length;
|
||||
const TILDE = 0x7E, GT = 0x3E;
|
||||
let startPos = stream.pos, ch, length;
|
||||
while ((ch = stream.getByte()) !== -1) {
|
||||
if (ch === TILDE) {
|
||||
ch = stream.peekByte();
|
||||
@ -363,15 +368,15 @@ var Parser = (function ParserClosure() {
|
||||
}
|
||||
this.inlineStreamSkipEI(stream);
|
||||
return length;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the EOD (end-of-data) marker '>' (i.e. GT) of the stream.
|
||||
* @returns {number} The inline stream length.
|
||||
*/
|
||||
findASCIIHexDecodeInlineStreamEnd:
|
||||
function Parser_findASCIIHexDecodeInlineStreamEnd(stream) {
|
||||
var GT = 0x3E;
|
||||
var startPos = stream.pos, ch, length;
|
||||
findASCIIHexDecodeInlineStreamEnd(stream) {
|
||||
const GT = 0x3E;
|
||||
let startPos = stream.pos, ch, length;
|
||||
while ((ch = stream.getByte()) !== -1) {
|
||||
if (ch === GT) {
|
||||
break;
|
||||
@ -386,13 +391,14 @@ var Parser = (function ParserClosure() {
|
||||
}
|
||||
this.inlineStreamSkipEI(stream);
|
||||
return length;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip over the /EI/ for streams where we search for an EOD marker.
|
||||
*/
|
||||
inlineStreamSkipEI: function Parser_inlineStreamSkipEI(stream) {
|
||||
var E = 0x45, I = 0x49;
|
||||
var state = 0, ch;
|
||||
inlineStreamSkipEI(stream) {
|
||||
const E = 0x45, I = 0x49;
|
||||
let state = 0, ch;
|
||||
while ((ch = stream.getByte()) !== -1) {
|
||||
if (state === 0) {
|
||||
state = (ch === E) ? 1 : 0;
|
||||
@ -402,18 +408,20 @@ var Parser = (function ParserClosure() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
makeInlineImage: function Parser_makeInlineImage(cipherTransform) {
|
||||
var lexer = this.lexer;
|
||||
var stream = lexer.stream;
|
||||
}
|
||||
|
||||
makeInlineImage(cipherTransform) {
|
||||
const lexer = this.lexer;
|
||||
const stream = lexer.stream;
|
||||
|
||||
// Parse dictionary.
|
||||
let dict = new Dict(this.xref), dictLength;
|
||||
const dict = new Dict(this.xref);
|
||||
let dictLength;
|
||||
while (!isCmd(this.buf1, 'ID') && !isEOF(this.buf1)) {
|
||||
if (!isName(this.buf1)) {
|
||||
throw new FormatError('Dictionary key must be a name object');
|
||||
}
|
||||
var key = this.buf1.name;
|
||||
const key = this.buf1.name;
|
||||
this.shift();
|
||||
if (isEOF(this.buf1)) {
|
||||
break;
|
||||
@ -425,18 +433,20 @@ var Parser = (function ParserClosure() {
|
||||
}
|
||||
|
||||
// Extract the name of the first (i.e. the current) image filter.
|
||||
var filter = dict.get('Filter', 'F'), filterName;
|
||||
const filter = dict.get('Filter', 'F');
|
||||
let filterName;
|
||||
if (isName(filter)) {
|
||||
filterName = filter.name;
|
||||
} else if (Array.isArray(filter)) {
|
||||
var filterZero = this.xref.fetchIfRef(filter[0]);
|
||||
const filterZero = this.xref.fetchIfRef(filter[0]);
|
||||
if (isName(filterZero)) {
|
||||
filterName = filterZero.name;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse image stream.
|
||||
let startPos = stream.pos, length;
|
||||
const startPos = stream.pos;
|
||||
let length;
|
||||
if (filterName === 'DCTDecode' || filterName === 'DCT') {
|
||||
length = this.findDCTDecodeInlineStreamEnd(stream);
|
||||
} else if (filterName === 'ASCII85Decode' || filterName === 'A85') {
|
||||
@ -446,26 +456,26 @@ var Parser = (function ParserClosure() {
|
||||
} else {
|
||||
length = this.findDefaultInlineStreamEnd(stream);
|
||||
}
|
||||
var imageStream = stream.makeSubStream(startPos, length, dict);
|
||||
let imageStream = stream.makeSubStream(startPos, length, dict);
|
||||
|
||||
// Cache all images below the MAX_LENGTH_TO_CACHE threshold by their
|
||||
// adler32 checksum.
|
||||
let cacheKey;
|
||||
if (length < MAX_LENGTH_TO_CACHE && dictLength < MAX_ADLER32_LENGTH) {
|
||||
var imageBytes = imageStream.getBytes();
|
||||
const imageBytes = imageStream.getBytes();
|
||||
imageStream.reset();
|
||||
|
||||
const initialStreamPos = stream.pos;
|
||||
// Set the stream position to the beginning of the dictionary data...
|
||||
stream.pos = lexer.beginInlineImagePos;
|
||||
// ... and fetch the bytes of the *entire* dictionary.
|
||||
let dictBytes = stream.getBytes(dictLength);
|
||||
const dictBytes = stream.getBytes(dictLength);
|
||||
// Finally, don't forget to reset the stream position.
|
||||
stream.pos = initialStreamPos;
|
||||
|
||||
cacheKey = computeAdler32(imageBytes) + '_' + computeAdler32(dictBytes);
|
||||
|
||||
let cacheEntry = this.imageCache[cacheKey];
|
||||
const cacheEntry = this.imageCache[cacheKey];
|
||||
if (cacheEntry !== undefined) {
|
||||
this.buf2 = Cmd.get('EI');
|
||||
this.shift();
|
||||
@ -482,7 +492,7 @@ var Parser = (function ParserClosure() {
|
||||
imageStream = this.filter(imageStream, dict, length);
|
||||
imageStream.dict = dict;
|
||||
if (cacheKey !== undefined) {
|
||||
imageStream.cacheKey = 'inline_' + length + '_' + cacheKey;
|
||||
imageStream.cacheKey = `inline_${length}_${cacheKey}`;
|
||||
this.imageCache[cacheKey] = imageStream;
|
||||
}
|
||||
|
||||
@ -490,7 +500,7 @@ var Parser = (function ParserClosure() {
|
||||
this.shift();
|
||||
|
||||
return imageStream;
|
||||
},
|
||||
}
|
||||
|
||||
_findStreamLength(startPos, signature) {
|
||||
const { stream, } = this.lexer;
|
||||
@ -521,28 +531,28 @@ var Parser = (function ParserClosure() {
|
||||
stream.pos += scanLength;
|
||||
}
|
||||
return -1;
|
||||
},
|
||||
}
|
||||
|
||||
makeStream: function Parser_makeStream(dict, cipherTransform) {
|
||||
var lexer = this.lexer;
|
||||
var stream = lexer.stream;
|
||||
makeStream(dict, cipherTransform) {
|
||||
const lexer = this.lexer;
|
||||
let stream = lexer.stream;
|
||||
|
||||
// get stream start position
|
||||
// Get the stream's start position.
|
||||
lexer.skipToNextLine();
|
||||
const startPos = stream.pos - 1;
|
||||
|
||||
// get length
|
||||
var length = dict.get('Length');
|
||||
// Get the length.
|
||||
let length = dict.get('Length');
|
||||
if (!Number.isInteger(length)) {
|
||||
info('Bad ' + length + ' attribute in stream');
|
||||
info(`Bad length "${length}" in stream`);
|
||||
length = 0;
|
||||
}
|
||||
|
||||
// skip over the stream data
|
||||
// Skip over the stream data.
|
||||
stream.pos = startPos + length;
|
||||
lexer.nextChar();
|
||||
|
||||
// Shift '>>' and check whether the new object marks the end of the stream
|
||||
// Shift '>>' and check whether the new object marks the end of the stream.
|
||||
if (this.tryShift() && isCmd(this.buf2, 'endstream')) {
|
||||
this.shift(); // 'stream'
|
||||
} else {
|
||||
@ -561,7 +571,7 @@ var Parser = (function ParserClosure() {
|
||||
const end = ENDSTREAM_SIGNATURE.length - i;
|
||||
const TRUNCATED_SIGNATURE = ENDSTREAM_SIGNATURE.slice(0, end);
|
||||
|
||||
let maybeLength = this._findStreamLength(startPos,
|
||||
const maybeLength = this._findStreamLength(startPos,
|
||||
TRUNCATED_SIGNATURE);
|
||||
if (maybeLength >= 0) {
|
||||
// Ensure that the byte immediately following the truncated
|
||||
@ -596,10 +606,12 @@ var Parser = (function ParserClosure() {
|
||||
stream = this.filter(stream, dict, length);
|
||||
stream.dict = dict;
|
||||
return stream;
|
||||
},
|
||||
filter: function Parser_filter(stream, dict, length) {
|
||||
var filter = dict.get('Filter', 'F');
|
||||
var params = dict.get('DecodeParms', 'DP');
|
||||
}
|
||||
|
||||
filter(stream, dict, length) {
|
||||
let filter = dict.get('Filter', 'F');
|
||||
let params = dict.get('DecodeParms', 'DP');
|
||||
|
||||
if (isName(filter)) {
|
||||
if (Array.isArray(params)) {
|
||||
warn('/DecodeParms should not contain an Array, ' +
|
||||
@ -608,14 +620,14 @@ var Parser = (function ParserClosure() {
|
||||
return this.makeFilter(stream, filter.name, length, params);
|
||||
}
|
||||
|
||||
var maybeLength = length;
|
||||
let maybeLength = length;
|
||||
if (Array.isArray(filter)) {
|
||||
var filterArray = filter;
|
||||
var paramsArray = params;
|
||||
for (var i = 0, ii = filterArray.length; i < ii; ++i) {
|
||||
let filterArray = filter;
|
||||
let paramsArray = params;
|
||||
for (let i = 0, ii = filterArray.length; i < ii; ++i) {
|
||||
filter = this.xref.fetchIfRef(filterArray[i]);
|
||||
if (!isName(filter)) {
|
||||
throw new FormatError('Bad filter name: ' + filter);
|
||||
throw new FormatError(`Bad filter name "${filter}"`);
|
||||
}
|
||||
|
||||
params = null;
|
||||
@ -623,22 +635,24 @@ var Parser = (function ParserClosure() {
|
||||
params = this.xref.fetchIfRef(paramsArray[i]);
|
||||
}
|
||||
stream = this.makeFilter(stream, filter.name, maybeLength, params);
|
||||
// after the first stream the length variable is invalid
|
||||
// After the first stream the `length` variable is invalid.
|
||||
maybeLength = null;
|
||||
}
|
||||
}
|
||||
return stream;
|
||||
},
|
||||
makeFilter: function Parser_makeFilter(stream, name, maybeLength, params) {
|
||||
}
|
||||
|
||||
makeFilter(stream, name, maybeLength, params) {
|
||||
// Since the 'Length' entry in the stream dictionary can be completely
|
||||
// wrong, e.g. zero for non-empty streams, only skip parsing the stream
|
||||
// when we can be absolutely certain that it actually is empty.
|
||||
if (maybeLength === 0) {
|
||||
warn('Empty "' + name + '" stream.');
|
||||
warn(`Empty "${name}" stream.`);
|
||||
return new NullStream();
|
||||
}
|
||||
|
||||
try {
|
||||
var xrefStreamStats = this.xref.stats.streamTypes;
|
||||
const xrefStreamStats = this.xref.stats.streamTypes;
|
||||
if (name === 'FlateDecode' || name === 'Fl') {
|
||||
xrefStreamStats[StreamType.FLATE] = true;
|
||||
if (params) {
|
||||
@ -649,7 +663,7 @@ var Parser = (function ParserClosure() {
|
||||
}
|
||||
if (name === 'LZWDecode' || name === 'LZW') {
|
||||
xrefStreamStats[StreamType.LZW] = true;
|
||||
var earlyChange = 1;
|
||||
let earlyChange = 1;
|
||||
if (params) {
|
||||
if (params.has('EarlyChange')) {
|
||||
earlyChange = params.get('EarlyChange');
|
||||
@ -688,20 +702,17 @@ var Parser = (function ParserClosure() {
|
||||
xrefStreamStats[StreamType.JBIG] = true;
|
||||
return new Jbig2Stream(stream, maybeLength, stream.dict, params);
|
||||
}
|
||||
warn('filter "' + name + '" not supported yet');
|
||||
warn(`Filter "${name}" is not supported.`);
|
||||
return stream;
|
||||
} catch (ex) {
|
||||
if (ex instanceof MissingDataException) {
|
||||
throw ex;
|
||||
}
|
||||
warn('Invalid stream: \"' + ex + '\"');
|
||||
warn(`Invalid stream: "${ex}"`);
|
||||
return new NullStream();
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
return Parser;
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
var Lexer = (function LexerClosure() {
|
||||
function Lexer(stream, knownCommands) {
|
||||
|
Loading…
Reference in New Issue
Block a user