From bea15b6ce558e0a3823a547f882b1983c7b89f77 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 15 Jul 2019 11:26:07 +0200 Subject: [PATCH] Simplify the `PDFDocument.fingerprint` method slightly The way that this method handles documents without an `ID` entry in the Trailer dictionary feels overly complicated to me. Hence this patch adds `getByteRange` methods to the various Stream implementations[1], and utilize that rather than manually calling `ensureRange` when computing a fallback `fingerprint`. --- [1] Note that `PDFDocument` is only ever initialized with either a `Stream` or a `ChunkedStream`, hence why the `DecodeStream.getByteRange` method isn't implemented. --- src/core/chunked_stream.js | 6 ++++++ src/core/document.js | 14 +++++--------- src/core/stream.js | 20 +++++++++++++++++++- 3 files changed, 30 insertions(+), 10 deletions(-) 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;