Skip to content

Commit

Permalink
Merge pull request #10033 from timvandermeij/permissions-api
Browse files Browse the repository at this point in the history
[api-minor] Implement a permissions API
  • Loading branch information
timvandermeij authored Sep 2, 2018
2 parents d409c42 + e812c6e commit af89ec2
Show file tree
Hide file tree
Showing 9 changed files with 806 additions and 778 deletions.
48 changes: 45 additions & 3 deletions src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

import {
bytesToString, createPromiseCapability, createValidAbsoluteUrl, FormatError,
info, InvalidPDFException, isBool, isString, MissingDataException, shadow,
stringToPDFString, stringToUTF8String, toRomanNumerals, unreachable, warn,
XRefParseException
info, InvalidPDFException, isBool, isNum, isString, MissingDataException,
PermissionFlag, shadow, stringToPDFString, stringToUTF8String,
toRomanNumerals, unreachable, warn, XRefParseException
} from '../shared/util';
import {
Dict, isCmd, isDict, isName, isRef, isRefsEqual, isStream, Ref, RefSet,
Expand Down Expand Up @@ -177,6 +177,48 @@ class Catalog {
return (root.items.length > 0 ? root.items : null);
}

get permissions() {
let permissions = null;
try {
permissions = this._readPermissions();
} catch (ex) {
if (ex instanceof MissingDataException) {
throw ex;
}
warn('Unable to read permissions.');
}
return shadow(this, 'permissions', permissions);
}

/**
* @private
*/
_readPermissions() {
const encrypt = this.xref.trailer.get('Encrypt');
if (!isDict(encrypt)) {
return null;
}

let flags = encrypt.get('P');
if (!isNum(flags)) {
return null;
}

// PDF integer objects are represented internally in signed 2's complement
// form. Therefore, convert the signed decimal integer to a signed 2's
// complement binary integer so we can use regular bitwise operations on it.
flags += 2 ** 32;

const permissions = [];
for (const key in PermissionFlag) {
const value = PermissionFlag[key];
if (flags & value) {
permissions.push(value);
}
}
return permissions;
}

get numPages() {
const obj = this.toplevelPagesDict.get('Count');
if (!Number.isInteger(obj)) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ var WorkerMessageHandler = {
}
);

handler.on('GetPermissions', function(data) {
return pdfManager.ensureCatalog('permissions');
});

handler.on('GetMetadata',
function wphSetupGetMetadata(data) {
return Promise.all([pdfManager.ensureDoc('documentInfo'),
Expand Down
Loading

0 comments on commit af89ec2

Please sign in to comment.