Slighthly re-factor XRef.fetchCompressed

- Change all occurences of `var` to `let`/`const`.

 - Initialize the (temporary) Arrays with the correct sizes upfront.

 - Inline the `isCmd` check. Obviously this won't make a huge difference, but given that the check is only relevant for corrupt documents it cannot hurt.
This commit is contained in:
Jonas Jenwald 2019-11-28 16:13:55 +01:00
parent b0aee6b1f0
commit 06412a557b

View File

@ -1707,13 +1707,13 @@ var XRef = (function XRefClosure() {
}, },
fetchCompressed(ref, xrefEntry, suppressEncryption = false) { fetchCompressed(ref, xrefEntry, suppressEncryption = false) {
var tableOffset = xrefEntry.offset; const tableOffset = xrefEntry.offset;
var stream = this.fetch(Ref.get(tableOffset, 0)); const stream = this.fetch(Ref.get(tableOffset, 0));
if (!isStream(stream)) { if (!isStream(stream)) {
throw new FormatError('bad ObjStm stream'); throw new FormatError('bad ObjStm stream');
} }
var first = stream.dict.get('First'); const first = stream.dict.get('First');
var n = stream.dict.get('N'); const n = stream.dict.get('N');
if (!Number.isInteger(first) || !Number.isInteger(n)) { if (!Number.isInteger(first) || !Number.isInteger(n)) {
throw new FormatError( throw new FormatError(
'invalid first and n parameters for ObjStm stream'); 'invalid first and n parameters for ObjStm stream');
@ -1723,33 +1723,34 @@ var XRef = (function XRefClosure() {
xref: this, xref: this,
allowStreams: true, allowStreams: true,
}); });
var i, entries = [], num, nums = []; const nums = new Array(n);
// read the object numbers to populate cache // read the object numbers to populate cache
for (i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
num = parser.getObj(); const num = parser.getObj();
if (!Number.isInteger(num)) { if (!Number.isInteger(num)) {
throw new FormatError( throw new FormatError(
`invalid object number in the ObjStm stream: ${num}`); `invalid object number in the ObjStm stream: ${num}`);
} }
nums.push(num); const offset = parser.getObj();
var offset = parser.getObj();
if (!Number.isInteger(offset)) { if (!Number.isInteger(offset)) {
throw new FormatError( throw new FormatError(
`invalid object offset in the ObjStm stream: ${offset}`); `invalid object offset in the ObjStm stream: ${offset}`);
} }
nums[i] = num;
} }
const entries = new Array(n);
// read stream objects for cache // read stream objects for cache
for (i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
entries.push(parser.getObj()); const obj = parser.getObj();
entries[i] = obj;
// The ObjStm should not contain 'endobj'. If it's present, skip over it // The ObjStm should not contain 'endobj'. If it's present, skip over it
// to support corrupt PDFs (fixes issue 5241, bug 898610, bug 1037816). // to support corrupt PDFs (fixes issue 5241, bug 898610, bug 1037816).
if (isCmd(parser.buf1, 'endobj')) { if ((parser.buf1 instanceof Cmd) && parser.buf1.cmd === 'endobj') {
parser.shift(); parser.shift();
} }
num = nums[i]; const num = nums[i], entry = this.entries[num];
var entry = this.entries[num];
if (entry && entry.offset === tableOffset && entry.gen === i) { if (entry && entry.offset === tableOffset && entry.gen === i) {
this._cacheMap.set(num, entries[i]); this._cacheMap.set(num, obj);
} }
} }
xrefEntry = entries[xrefEntry.gen]; xrefEntry = entries[xrefEntry.gen];