diff --git a/src/core/chunked_stream.js b/src/core/chunked_stream.js index 26a93f673..39c1cb677 100644 --- a/src/core/chunked_stream.js +++ b/src/core/chunked_stream.js @@ -218,6 +218,12 @@ class ChunkedStream { } getByteRange(begin, end) { + if (begin < 0) { + begin = 0; + } + if (end > this.end) { + end = this.end; + } this.ensureRange(begin, end); return this.bytes.subarray(begin, end); } diff --git a/src/core/document.js b/src/core/document.js index 05cc27374..73b14e9a9 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -598,20 +598,16 @@ class PDFDocument { idArray[0] !== EMPTY_FINGERPRINT) { hash = stringToBytes(idArray[0]); } else { - if (this.stream.ensureRange) { - this.stream.ensureRange(0, - Math.min(FINGERPRINT_FIRST_BYTES, this.stream.end)); - } - hash = calculateMD5(this.stream.bytes.subarray(0, - FINGERPRINT_FIRST_BYTES), 0, FINGERPRINT_FIRST_BYTES); + hash = calculateMD5(this.stream.getByteRange(0, FINGERPRINT_FIRST_BYTES), + 0, FINGERPRINT_FIRST_BYTES); } - let fingerprint = ''; + const fingerprintBuf = []; for (let i = 0, ii = hash.length; i < ii; i++) { const hex = hash[i].toString(16); - fingerprint += (hex.length === 1 ? '0' + hex : hex); + fingerprintBuf.push(hex.padStart(2, '0')); } - return shadow(this, 'fingerprint', fingerprint); + return shadow(this, 'fingerprint', fingerprintBuf.join('')); } _getLinearizationPage(pageIndex) { diff --git a/src/core/stream.js b/src/core/stream.js index ae57f9023..1867c8c99 100644 --- a/src/core/stream.js +++ b/src/core/stream.js @@ -19,7 +19,9 @@ * license. */ -import { FormatError, isSpace, stringToBytes } from '../shared/util'; +import { + FormatError, isSpace, stringToBytes, unreachable +} from '../shared/util'; import { isDict } from './primitives'; var Stream = (function StreamClosure() { @@ -92,6 +94,17 @@ var Stream = (function StreamClosure() { this.pos -= bytes.length; return bytes; }, + + getByteRange(begin, end) { + if (begin < 0) { + begin = 0; + } + if (end > this.end) { + end = this.end; + } + return this.bytes.subarray(begin, end); + }, + skip: function Stream_skip(n) { if (!n) { n = 1; @@ -236,6 +249,11 @@ var DecodeStream = (function DecodeStreamClosure() { } return new Stream(this.buffer, start, length, dict); }, + + getByteRange(begin, end) { + unreachable('Should not call DecodeStream.getByteRange'); + }, + skip: function DecodeStream_skip(n) { if (!n) { n = 1;