Enable the no-var
rule in the src/core/xref.js
file
This commit is contained in:
parent
bc828cd41f
commit
088a55f80d
117
src/core/xref.js
117
src/core/xref.js
@ -12,7 +12,6 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
/* eslint-disable no-var */
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
assert,
|
assert,
|
||||||
@ -71,7 +70,7 @@ class XRef {
|
|||||||
}
|
}
|
||||||
|
|
||||||
parse(recoveryMode = false) {
|
parse(recoveryMode = false) {
|
||||||
var trailerDict;
|
let trailerDict;
|
||||||
if (!recoveryMode) {
|
if (!recoveryMode) {
|
||||||
trailerDict = this.readXRef();
|
trailerDict = this.readXRef();
|
||||||
} else {
|
} else {
|
||||||
@ -91,8 +90,8 @@ class XRef {
|
|||||||
warn(`XRef.parse - Invalid "Encrypt" reference: "${ex}".`);
|
warn(`XRef.parse - Invalid "Encrypt" reference: "${ex}".`);
|
||||||
}
|
}
|
||||||
if (isDict(encrypt)) {
|
if (isDict(encrypt)) {
|
||||||
var ids = trailerDict.get("ID");
|
const ids = trailerDict.get("ID");
|
||||||
var fileId = ids && ids.length ? ids[0] : "";
|
const fileId = ids && ids.length ? ids[0] : "";
|
||||||
// The 'Encrypt' dictionary itself should not be encrypted, and by
|
// The 'Encrypt' dictionary itself should not be encrypted, and by
|
||||||
// setting `suppressEncryption` we can prevent an infinite loop inside
|
// setting `suppressEncryption` we can prevent an infinite loop inside
|
||||||
// of `XRef_fetchUncompressed` if the dictionary contains indirect
|
// of `XRef_fetchUncompressed` if the dictionary contains indirect
|
||||||
@ -137,7 +136,7 @@ class XRef {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var obj = this.readXRefTable(parser);
|
const obj = this.readXRefTable(parser);
|
||||||
|
|
||||||
// Sanity check
|
// Sanity check
|
||||||
if (!isCmd(obj, "trailer")) {
|
if (!isCmd(obj, "trailer")) {
|
||||||
@ -154,7 +153,7 @@ class XRef {
|
|||||||
// >>
|
// >>
|
||||||
// The parser goes through the entire stream << ... >> and provides
|
// The parser goes through the entire stream << ... >> and provides
|
||||||
// a getter interface for the key-value table
|
// a getter interface for the key-value table
|
||||||
var dict = parser.getObj();
|
let dict = parser.getObj();
|
||||||
|
|
||||||
// The pdflib PDF generator can generate a nested trailer dictionary
|
// The pdflib PDF generator can generate a nested trailer dictionary
|
||||||
if (!isDict(dict) && dict.dict) {
|
if (!isDict(dict) && dict.dict) {
|
||||||
@ -181,14 +180,14 @@ class XRef {
|
|||||||
// trailer
|
// trailer
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
var stream = parser.lexer.stream;
|
const stream = parser.lexer.stream;
|
||||||
var tableState = this.tableState;
|
const tableState = this.tableState;
|
||||||
stream.pos = tableState.streamPos;
|
stream.pos = tableState.streamPos;
|
||||||
parser.buf1 = tableState.parserBuf1;
|
parser.buf1 = tableState.parserBuf1;
|
||||||
parser.buf2 = tableState.parserBuf2;
|
parser.buf2 = tableState.parserBuf2;
|
||||||
|
|
||||||
// Outer loop is over subsection headers
|
// Outer loop is over subsection headers
|
||||||
var obj;
|
let obj;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!("firstEntryNum" in tableState) || !("entryCount" in tableState)) {
|
if (!("firstEntryNum" in tableState) || !("entryCount" in tableState)) {
|
||||||
@ -199,24 +198,24 @@ class XRef {
|
|||||||
tableState.entryCount = parser.getObj();
|
tableState.entryCount = parser.getObj();
|
||||||
}
|
}
|
||||||
|
|
||||||
var first = tableState.firstEntryNum;
|
let first = tableState.firstEntryNum;
|
||||||
var count = tableState.entryCount;
|
const count = tableState.entryCount;
|
||||||
if (!Number.isInteger(first) || !Number.isInteger(count)) {
|
if (!Number.isInteger(first) || !Number.isInteger(count)) {
|
||||||
throw new FormatError(
|
throw new FormatError(
|
||||||
"Invalid XRef table: wrong types in subsection header"
|
"Invalid XRef table: wrong types in subsection header"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// Inner loop is over objects themselves
|
// Inner loop is over objects themselves
|
||||||
for (var i = tableState.entryNum; i < count; i++) {
|
for (let i = tableState.entryNum; i < count; i++) {
|
||||||
tableState.streamPos = stream.pos;
|
tableState.streamPos = stream.pos;
|
||||||
tableState.entryNum = i;
|
tableState.entryNum = i;
|
||||||
tableState.parserBuf1 = parser.buf1;
|
tableState.parserBuf1 = parser.buf1;
|
||||||
tableState.parserBuf2 = parser.buf2;
|
tableState.parserBuf2 = parser.buf2;
|
||||||
|
|
||||||
var entry = {};
|
const entry = {};
|
||||||
entry.offset = parser.getObj();
|
entry.offset = parser.getObj();
|
||||||
entry.gen = parser.getObj();
|
entry.gen = parser.getObj();
|
||||||
var type = parser.getObj();
|
const type = parser.getObj();
|
||||||
|
|
||||||
if (type instanceof Cmd) {
|
if (type instanceof Cmd) {
|
||||||
switch (type.cmd) {
|
switch (type.cmd) {
|
||||||
@ -270,9 +269,9 @@ class XRef {
|
|||||||
if (!("streamState" in this)) {
|
if (!("streamState" in this)) {
|
||||||
// Stores state of the stream as we process it so we can resume
|
// Stores state of the stream as we process it so we can resume
|
||||||
// from middle of stream in case of missing data error
|
// from middle of stream in case of missing data error
|
||||||
var streamParameters = stream.dict;
|
const streamParameters = stream.dict;
|
||||||
var byteWidths = streamParameters.get("W");
|
const byteWidths = streamParameters.get("W");
|
||||||
var range = streamParameters.get("Index");
|
let range = streamParameters.get("Index");
|
||||||
if (!range) {
|
if (!range) {
|
||||||
range = [0, streamParameters.get("Size")];
|
range = [0, streamParameters.get("Size")];
|
||||||
}
|
}
|
||||||
@ -291,19 +290,19 @@ class XRef {
|
|||||||
}
|
}
|
||||||
|
|
||||||
readXRefStream(stream) {
|
readXRefStream(stream) {
|
||||||
var i, j;
|
let i, j;
|
||||||
var streamState = this.streamState;
|
const streamState = this.streamState;
|
||||||
stream.pos = streamState.streamPos;
|
stream.pos = streamState.streamPos;
|
||||||
|
|
||||||
var byteWidths = streamState.byteWidths;
|
const byteWidths = streamState.byteWidths;
|
||||||
var typeFieldWidth = byteWidths[0];
|
const typeFieldWidth = byteWidths[0];
|
||||||
var offsetFieldWidth = byteWidths[1];
|
const offsetFieldWidth = byteWidths[1];
|
||||||
var generationFieldWidth = byteWidths[2];
|
const generationFieldWidth = byteWidths[2];
|
||||||
|
|
||||||
var entryRanges = streamState.entryRanges;
|
const entryRanges = streamState.entryRanges;
|
||||||
while (entryRanges.length > 0) {
|
while (entryRanges.length > 0) {
|
||||||
var first = entryRanges[0];
|
const first = entryRanges[0];
|
||||||
var n = entryRanges[1];
|
const n = entryRanges[1];
|
||||||
|
|
||||||
if (!Number.isInteger(first) || !Number.isInteger(n)) {
|
if (!Number.isInteger(first) || !Number.isInteger(n)) {
|
||||||
throw new FormatError(`Invalid XRef range fields: ${first}, ${n}`);
|
throw new FormatError(`Invalid XRef range fields: ${first}, ${n}`);
|
||||||
@ -321,7 +320,7 @@ class XRef {
|
|||||||
streamState.entryNum = i;
|
streamState.entryNum = i;
|
||||||
streamState.streamPos = stream.pos;
|
streamState.streamPos = stream.pos;
|
||||||
|
|
||||||
var type = 0,
|
let type = 0,
|
||||||
offset = 0,
|
offset = 0,
|
||||||
generation = 0;
|
generation = 0;
|
||||||
for (j = 0; j < typeFieldWidth; ++j) {
|
for (j = 0; j < typeFieldWidth; ++j) {
|
||||||
@ -337,7 +336,7 @@ class XRef {
|
|||||||
for (j = 0; j < generationFieldWidth; ++j) {
|
for (j = 0; j < generationFieldWidth; ++j) {
|
||||||
generation = (generation << 8) | stream.getByte();
|
generation = (generation << 8) | stream.getByte();
|
||||||
}
|
}
|
||||||
var entry = {};
|
const entry = {};
|
||||||
entry.offset = offset;
|
entry.offset = offset;
|
||||||
entry.gen = generation;
|
entry.gen = generation;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@ -366,15 +365,15 @@ class XRef {
|
|||||||
indexObjects() {
|
indexObjects() {
|
||||||
// Simple scan through the PDF content to find objects,
|
// Simple scan through the PDF content to find objects,
|
||||||
// trailers and XRef streams.
|
// trailers and XRef streams.
|
||||||
var TAB = 0x9,
|
const TAB = 0x9,
|
||||||
LF = 0xa,
|
LF = 0xa,
|
||||||
CR = 0xd,
|
CR = 0xd,
|
||||||
SPACE = 0x20;
|
SPACE = 0x20;
|
||||||
var PERCENT = 0x25,
|
const PERCENT = 0x25,
|
||||||
LT = 0x3c;
|
LT = 0x3c;
|
||||||
|
|
||||||
function readToken(data, offset) {
|
function readToken(data, offset) {
|
||||||
var token = "",
|
let token = "",
|
||||||
ch = data[offset];
|
ch = data[offset];
|
||||||
while (ch !== LF && ch !== CR && ch !== LT) {
|
while (ch !== LF && ch !== CR && ch !== LT) {
|
||||||
if (++offset >= data.length) {
|
if (++offset >= data.length) {
|
||||||
@ -386,12 +385,12 @@ class XRef {
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
function skipUntil(data, offset, what) {
|
function skipUntil(data, offset, what) {
|
||||||
var length = what.length,
|
const length = what.length,
|
||||||
dataLength = data.length;
|
dataLength = data.length;
|
||||||
var skipped = 0;
|
let skipped = 0;
|
||||||
// finding byte sequence
|
// finding byte sequence
|
||||||
while (offset < dataLength) {
|
while (offset < dataLength) {
|
||||||
var i = 0;
|
let i = 0;
|
||||||
while (i < length && data[offset + i] === what[i]) {
|
while (i < length && data[offset + i] === what[i]) {
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
@ -403,30 +402,30 @@ class XRef {
|
|||||||
}
|
}
|
||||||
return skipped;
|
return skipped;
|
||||||
}
|
}
|
||||||
var objRegExp = /^(\d+)\s+(\d+)\s+obj\b/;
|
const objRegExp = /^(\d+)\s+(\d+)\s+obj\b/;
|
||||||
const endobjRegExp = /\bendobj[\b\s]$/;
|
const endobjRegExp = /\bendobj[\b\s]$/;
|
||||||
const nestedObjRegExp = /\s+(\d+\s+\d+\s+obj[\b\s<])$/;
|
const nestedObjRegExp = /\s+(\d+\s+\d+\s+obj[\b\s<])$/;
|
||||||
const CHECK_CONTENT_LENGTH = 25;
|
const CHECK_CONTENT_LENGTH = 25;
|
||||||
|
|
||||||
var trailerBytes = new Uint8Array([116, 114, 97, 105, 108, 101, 114]);
|
const trailerBytes = new Uint8Array([116, 114, 97, 105, 108, 101, 114]);
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
var startxrefBytes = new Uint8Array([115, 116, 97, 114, 116, 120, 114,
|
const startxrefBytes = new Uint8Array([115, 116, 97, 114, 116, 120, 114,
|
||||||
101, 102]);
|
101, 102]);
|
||||||
const objBytes = new Uint8Array([111, 98, 106]);
|
const objBytes = new Uint8Array([111, 98, 106]);
|
||||||
var xrefBytes = new Uint8Array([47, 88, 82, 101, 102]);
|
const xrefBytes = new Uint8Array([47, 88, 82, 101, 102]);
|
||||||
|
|
||||||
// Clear out any existing entries, since they may be bogus.
|
// Clear out any existing entries, since they may be bogus.
|
||||||
this.entries.length = 0;
|
this.entries.length = 0;
|
||||||
|
|
||||||
var stream = this.stream;
|
const stream = this.stream;
|
||||||
stream.pos = 0;
|
stream.pos = 0;
|
||||||
var buffer = stream.getBytes();
|
const buffer = stream.getBytes(),
|
||||||
var position = stream.start,
|
|
||||||
length = buffer.length;
|
length = buffer.length;
|
||||||
var trailers = [],
|
let position = stream.start;
|
||||||
|
const trailers = [],
|
||||||
xrefStms = [];
|
xrefStms = [];
|
||||||
while (position < length) {
|
while (position < length) {
|
||||||
var ch = buffer[position];
|
let ch = buffer[position];
|
||||||
if (ch === TAB || ch === LF || ch === CR || ch === SPACE) {
|
if (ch === TAB || ch === LF || ch === CR || ch === SPACE) {
|
||||||
++position;
|
++position;
|
||||||
continue;
|
continue;
|
||||||
@ -442,8 +441,8 @@ class XRef {
|
|||||||
} while (ch !== LF && ch !== CR);
|
} while (ch !== LF && ch !== CR);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
var token = readToken(buffer, position);
|
const token = readToken(buffer, position);
|
||||||
var m;
|
let m;
|
||||||
if (
|
if (
|
||||||
token.startsWith("xref") &&
|
token.startsWith("xref") &&
|
||||||
(token.length === 4 || /\s/.test(token[4]))
|
(token.length === 4 || /\s/.test(token[4]))
|
||||||
@ -497,7 +496,7 @@ class XRef {
|
|||||||
|
|
||||||
// checking XRef stream suspect
|
// checking XRef stream suspect
|
||||||
// (it shall have '/XRef' and next char is not a letter)
|
// (it shall have '/XRef' and next char is not a letter)
|
||||||
var xrefTagOffset = skipUntil(content, 0, xrefBytes);
|
const xrefTagOffset = skipUntil(content, 0, xrefBytes);
|
||||||
if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) {
|
if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) {
|
||||||
xrefStms.push(position - stream.start);
|
xrefStms.push(position - stream.start);
|
||||||
this.xrefstms[position - stream.start] = 1; // Avoid recursion
|
this.xrefstms[position - stream.start] = 1; // Avoid recursion
|
||||||
@ -529,7 +528,7 @@ class XRef {
|
|||||||
allowStreams: true,
|
allowStreams: true,
|
||||||
recoveryMode: true,
|
recoveryMode: true,
|
||||||
});
|
});
|
||||||
var obj = parser.getObj();
|
const obj = parser.getObj();
|
||||||
if (!isCmd(obj, "trailer")) {
|
if (!isCmd(obj, "trailer")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -572,7 +571,7 @@ class XRef {
|
|||||||
}
|
}
|
||||||
|
|
||||||
readXRef(recoveryMode = false) {
|
readXRef(recoveryMode = false) {
|
||||||
var stream = this.stream;
|
const stream = this.stream;
|
||||||
// Keep track of already parsed XRef tables, to prevent an infinite loop
|
// Keep track of already parsed XRef tables, to prevent an infinite loop
|
||||||
// when parsing corrupt PDF files where e.g. the /Prev entries create a
|
// when parsing corrupt PDF files where e.g. the /Prev entries create a
|
||||||
// circular dependency between tables (fixes bug1393476.pdf).
|
// circular dependency between tables (fixes bug1393476.pdf).
|
||||||
@ -580,7 +579,7 @@ class XRef {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
while (this.startXRefQueue.length) {
|
while (this.startXRefQueue.length) {
|
||||||
var startXRef = this.startXRefQueue[0];
|
const startXRef = this.startXRefQueue[0];
|
||||||
|
|
||||||
if (startXRefParsedCache.has(startXRef)) {
|
if (startXRefParsedCache.has(startXRef)) {
|
||||||
warn("readXRef - skipping XRef table since it was already parsed.");
|
warn("readXRef - skipping XRef table since it was already parsed.");
|
||||||
@ -596,8 +595,8 @@ class XRef {
|
|||||||
xref: this,
|
xref: this,
|
||||||
allowStreams: true,
|
allowStreams: true,
|
||||||
});
|
});
|
||||||
var obj = parser.getObj();
|
let obj = parser.getObj();
|
||||||
var dict;
|
let dict;
|
||||||
|
|
||||||
// Get dictionary
|
// Get dictionary
|
||||||
if (isCmd(obj, "xref")) {
|
if (isCmd(obj, "xref")) {
|
||||||
@ -610,7 +609,7 @@ class XRef {
|
|||||||
// Recursively get other XRefs 'XRefStm', if any
|
// Recursively get other XRefs 'XRefStm', if any
|
||||||
obj = dict.get("XRefStm");
|
obj = dict.get("XRefStm");
|
||||||
if (Number.isInteger(obj)) {
|
if (Number.isInteger(obj)) {
|
||||||
var pos = obj;
|
const pos = obj;
|
||||||
// ignore previously loaded xref streams
|
// ignore previously loaded xref streams
|
||||||
// (possible infinite recursion)
|
// (possible infinite recursion)
|
||||||
if (!(pos in this.xrefstms)) {
|
if (!(pos in this.xrefstms)) {
|
||||||
@ -666,7 +665,7 @@ class XRef {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getEntry(i) {
|
getEntry(i) {
|
||||||
var xrefEntry = this.entries[i];
|
const xrefEntry = this.entries[i];
|
||||||
if (xrefEntry && !xrefEntry.free && xrefEntry.offset) {
|
if (xrefEntry && !xrefEntry.free && xrefEntry.offset) {
|
||||||
return xrefEntry;
|
return xrefEntry;
|
||||||
}
|
}
|
||||||
@ -720,12 +719,12 @@ class XRef {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetchUncompressed(ref, xrefEntry, suppressEncryption = false) {
|
fetchUncompressed(ref, xrefEntry, suppressEncryption = false) {
|
||||||
var gen = ref.gen;
|
const gen = ref.gen;
|
||||||
var num = ref.num;
|
let num = ref.num;
|
||||||
if (xrefEntry.gen !== gen) {
|
if (xrefEntry.gen !== gen) {
|
||||||
throw new XRefEntryException(`Inconsistent generation in XRef: ${ref}`);
|
throw new XRefEntryException(`Inconsistent generation in XRef: ${ref}`);
|
||||||
}
|
}
|
||||||
var stream = this.stream.makeSubStream(
|
const stream = this.stream.makeSubStream(
|
||||||
xrefEntry.offset + this.stream.start
|
xrefEntry.offset + this.stream.start
|
||||||
);
|
);
|
||||||
const parser = new Parser({
|
const parser = new Parser({
|
||||||
@ -733,9 +732,9 @@ class XRef {
|
|||||||
xref: this,
|
xref: this,
|
||||||
allowStreams: true,
|
allowStreams: true,
|
||||||
});
|
});
|
||||||
var obj1 = parser.getObj();
|
const obj1 = parser.getObj();
|
||||||
var obj2 = parser.getObj();
|
const obj2 = parser.getObj();
|
||||||
var obj3 = parser.getObj();
|
const obj3 = parser.getObj();
|
||||||
|
|
||||||
if (obj1 !== num || obj2 !== gen || !(obj3 instanceof Cmd)) {
|
if (obj1 !== num || obj2 !== gen || !(obj3 instanceof Cmd)) {
|
||||||
throw new XRefEntryException(`Bad (uncompressed) XRef entry: ${ref}`);
|
throw new XRefEntryException(`Bad (uncompressed) XRef entry: ${ref}`);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user