Skip to content

Commit

Permalink
Refactor the password handling so that it's stored in the `PdfManag…
Browse files Browse the repository at this point in the history
…er`s, instead of in the `XRef`

We're already passing in a, currently unused, `PdfManager` instance when initializing the `XRef`. To avoid having to pass a single `password` parameter around, we could thus simply get the `password` through the `PdfManager` instance instead.
  • Loading branch information
Snuffleupagus committed Jan 3, 2017
1 parent 27513cd commit 14b8523
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
14 changes: 6 additions & 8 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,22 +370,20 @@ var PDFDocument = (function PDFDocumentClosure() {
var EMPTY_FINGERPRINT = '\x00\x00\x00\x00\x00\x00\x00' +
'\x00\x00\x00\x00\x00\x00\x00\x00\x00';

function PDFDocument(pdfManager, arg, password) {
function PDFDocument(pdfManager, arg) {
var stream;
if (isStream(arg)) {
init.call(this, pdfManager, arg, password);
stream = arg;
} else if (isArrayBuffer(arg)) {
init.call(this, pdfManager, new Stream(arg), password);
stream = new Stream(arg);
} else {
error('PDFDocument: Unknown argument type');
}
}

function init(pdfManager, stream, password) {
assert(stream.length > 0, 'stream must have data');

this.pdfManager = pdfManager;
this.stream = stream;
var xref = new XRef(this.stream, password, pdfManager);
this.xref = xref;
this.xref = new XRef(stream, pdfManager);
}

function find(stream, needle, limit, backwards) {
Expand Down
8 changes: 4 additions & 4 deletions src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,13 +758,13 @@ var Catalog = (function CatalogClosure() {
})();

var XRef = (function XRefClosure() {
function XRef(stream, password) {
function XRef(stream, pdfManager) {
this.stream = stream;
this.pdfManager = pdfManager;
this.entries = [];
this.xrefstms = Object.create(null);
// prepare the XRef cache
this.cache = [];
this.password = password;
this.stats = {
streamTypes: [],
fontTypes: []
Expand All @@ -789,7 +789,7 @@ var XRef = (function XRefClosure() {
trailerDict.assignXref(this);
this.trailer = trailerDict;
var encrypt = trailerDict.get('Encrypt');
if (encrypt) {
if (isDict(encrypt)) {
var ids = trailerDict.get('ID');
var fileId = (ids && ids.length) ? ids[0] : '';
// The 'Encrypt' dictionary itself should not be encrypted, and by
Expand All @@ -798,7 +798,7 @@ var XRef = (function XRefClosure() {
// objects (fixes issue7665.pdf).
encrypt.suppressEncryption = true;
this.encrypt = new CipherTransformFactory(encrypt, fileId,
this.password);
this.pdfManager.password);
}

// get the root dictionary (catalog) object
Expand Down
13 changes: 9 additions & 4 deletions src/core/pdf_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ var BasePdfManager = (function BasePdfManagerClosure() {
return this._docId;
},

get password() {
return this._password;
},

get docBaseUrl() {
var docBaseUrl = null;
if (this._docBaseUrl) {
Expand Down Expand Up @@ -106,7 +110,7 @@ var BasePdfManager = (function BasePdfManagerClosure() {
},

updatePassword: function BasePdfManager_updatePassword(password) {
this.pdfDocument.xref.password = this.password = password;
this._password = password;
},

terminate: function BasePdfManager_terminate() {
Expand All @@ -121,10 +125,11 @@ var LocalPdfManager = (function LocalPdfManagerClosure() {
function LocalPdfManager(docId, data, password, evaluatorOptions,
docBaseUrl) {
this._docId = docId;
this._password = password;
this._docBaseUrl = docBaseUrl;
this.evaluatorOptions = evaluatorOptions;
var stream = new Stream(data);
this.pdfDocument = new PDFDocument(this, stream, password);
this.pdfDocument = new PDFDocument(this, stream);
this._loadedStreamCapability = createPromiseCapability();
this._loadedStreamCapability.resolve(stream);
}
Expand Down Expand Up @@ -171,6 +176,7 @@ var NetworkPdfManager = (function NetworkPdfManagerClosure() {
function NetworkPdfManager(docId, pdfNetworkStream, args, evaluatorOptions,
docBaseUrl) {
this._docId = docId;
this._password = args.password;
this._docBaseUrl = docBaseUrl;
this.msgHandler = args.msgHandler;
this.evaluatorOptions = evaluatorOptions;
Expand All @@ -183,8 +189,7 @@ var NetworkPdfManager = (function NetworkPdfManagerClosure() {
rangeChunkSize: args.rangeChunkSize
};
this.streamManager = new ChunkedStreamManager(pdfNetworkStream, params);
this.pdfDocument = new PDFDocument(this, this.streamManager.getStream(),
args.password);
this.pdfDocument = new PDFDocument(this, this.streamManager.getStream());
}

Util.inherit(NetworkPdfManager, BasePdfManager, {
Expand Down

0 comments on commit 14b8523

Please sign in to comment.