UNPKG

@syncfusion/ej2-pdf

Version:

Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.

254 lines (253 loc) 9.72 kB
/** * Represents an internal object identifier (OID) helper that encodes, decodes, * and formats object identifiers used within PDF structures. * * @private */ var _PdfObjectIdentifier = /** @class */ (function () { function _PdfObjectIdentifier() { /** * Holds the internal byte-encoded representation of the object identifier. * * @private */ this._encoding = new Uint8Array(0); /** * Stores the character code for the dot ('.') used in dotted OID notation. * * @private */ this._period = '.'.charCodeAt(0); } /** * Creates an internal object identifier from individual numeric nodes, with an optional prefix * either a leading node or an existing identifier to extend. * * @private * @param {number[]} nodes The sequence of OID nodes. * @param {_PdfObjectIdentifier | number} [prefix] An optional prefix OID or the first node to prepend. * @returns {_PdfObjectIdentifier} A new object identifier built from the specified parts. */ _PdfObjectIdentifier.prototype._fromParts = function (nodes, prefix) { var resolvedNodes = typeof prefix === 'number' ? [prefix].concat(nodes) : nodes; if (!prefix || typeof prefix === 'number') { if (resolvedNodes.length < 2) { throw new Error('An oid must contain at least two nodes'); } else if (resolvedNodes[0] < 0 || resolvedNodes[0] > 2) { throw new Error('Invalid oid: The first node must be 0, 1, or 2.'); } else if (resolvedNodes[0] < 2 && resolvedNodes[1] > 39) { throw new Error("Invalid oid: When Node #1 is 0 or 1, Node #2 must be 0\u201339. Received: " + resolvedNodes + "."); } } var oid = new _PdfObjectIdentifier(); var encoded = oid._encodeRelativeObjectIdentifier(typeof prefix === 'number' ? [(resolvedNodes[0] * 40) + resolvedNodes[1]].concat(resolvedNodes.slice(2)) : resolvedNodes); if (prefix && typeof prefix !== 'number') { oid._encoding = this._mergeUint8Arrays(prefix._encoding, encoded); } else { oid._encoding = encoded; } return oid; }; /** * Decodes the internal byte representation into an array of OID nodes. * * @private * @returns {number[]} The decoded list of object identifier nodes. */ _PdfObjectIdentifier.prototype._getNodes = function () { var subcomponents = this._decodeRelativeObjectIdentifier(this._encoding); return [ Math.min(2, Math.floor(subcomponents[0] / 40)), subcomponents[0] >= 80 ? subcomponents[0] - 80 : subcomponents[0] % 40 ].concat(subcomponents.slice(1)); }; /** * Formats the internal object identifier as a dot-delimited string (e.g., "1.2.840.113549"). * * @private * @returns {string} The dot-delimited representation of the OID. */ _PdfObjectIdentifier.prototype._getDotDelimitedNotation = function () { return this._getNodes().join('.'); }; /** * Formats the internal object identifier in abstract syntax notation (ASN) form. * * @private * @returns {string} The ASN-style representation of the OID. */ _PdfObjectIdentifier.prototype._getAbstractSyntaxNotation = function () { return "{ " + this._getNodes().map(function (node) { return node.toString(); }).join(' ') + " }"; }; /** * Merges two byte arrays into a single contiguous byte sequence. * * @private * @param {Uint8Array} a The first byte array. * @param {Uint8Array} b The second byte array. * @returns {Uint8Array} A new byte array containing the concatenation of both inputs. */ _PdfObjectIdentifier.prototype._mergeUint8Arrays = function (a, b) { var result = new Uint8Array(a.length + b.length); result.set(a, 0); result.set(b, a.length); return result; }; /** * Parses a dot-delimited object identifier string into an internal object identifier. * * @private * @param {string} str The dot-delimited OID string to parse (e.g., "1.2.840.113549"). * @returns {_PdfObjectIdentifier} A new object identifier built from the parsed string. */ _PdfObjectIdentifier.prototype._fromString = function (str) { var arcs = []; var last = 0; for (var i = 0; i < str.length; i++) { if (str.charCodeAt(i) === this._period) { var arc_1 = Number.parseInt(str.slice(last, i), 10); arcs.push(arc_1); last = i + 1; } } var arc = Number.parseInt(str.slice(last), 10); arcs.push(arc); return this._fromParts(arcs); }; /** * Parses an encoded object identifier from a validated byte sequence. * * @private * @param {Uint8Array} bytes The encoded OID bytes to parse. * @returns {_PdfObjectIdentifier} A new object identifier created from the provided bytes. */ _PdfObjectIdentifier.prototype._fromBytes = function (bytes) { if (bytes.length === 0) { throw new Error('Encoded value was too short to be an object identifier'); } else if ((bytes[bytes.length - 1] & 128) !== 0) { throw new Error('oid was truncated.'); } var currentNode = 0; for (var i = 1; i < bytes.length; i++) { var byte = bytes[i]; if (currentNode === 0 && byte === 0x80) { throw new Error('Padding is not allowed in object identifier nodes'); } if (byte < 0x80) { currentNode = 0; } else { currentNode++; } } var oid = new _PdfObjectIdentifier(); oid._encoding = new Uint8Array(bytes); return oid; }; /** * Creates an object identifier from a byte sequence without validating its structure. * * @private * @param {Uint8Array} bytes The encoded OID bytes to attach directly. * @returns {_PdfObjectIdentifier} A new object identifier referencing the provided bytes. */ _PdfObjectIdentifier.prototype._fromBytesUnsafe = function (bytes) { var oid = new _PdfObjectIdentifier(); oid._encoding = new Uint8Array(bytes); return oid; }; /** * Returns the object identifier in its dot delimited string representation. * * @returns {string} The dot delimited notation generated from the internal nodes. */ _PdfObjectIdentifier.prototype.toString = function () { return this._getDotDelimitedNotation(); }; /** * Converts the internal object identifier to its dot-delimited string form for JSON usage. * * @private * @returns {string} The dot-delimited representation of the OID. */ _PdfObjectIdentifier.prototype._toJson = function () { return this._getDotDelimitedNotation(); }; /** * Gets a copy of the internal byte-encoded representation of this object identifier. * * @private * @returns {Uint8Array} A new byte array containing the encoded OID. */ _PdfObjectIdentifier.prototype._toBytes = function () { return new Uint8Array(this._encoding); }; /** * Encodes a set of relative object identifier arcs (nodes) into a byte sequence. * * @private * @param {number[]} value The list of relative OID nodes to encode. * @returns {Uint8Array} The encoded byte sequence representing the relative OID. */ _PdfObjectIdentifier.prototype._encodeRelativeObjectIdentifier = function (value) { var result = []; for (var _i = 0, value_1 = value; _i < value_1.length; _i++) { var arc = value_1[_i]; if (arc < 128) { result.push(arc); continue; } var length_1 = 0; var tempArc = arc; while (tempArc > 0) { length_1++; tempArc >>>= 7; } for (var j = length_1 - 1; j >= 0; j--) { var byte = (arc >>> (j * 7)) & 0x7f; if (j !== 0) { byte |= 0x80; } result.push(byte); } } return new Uint8Array(result); }; /** * Decodes a byte sequence into a list of relative object identifier arcs (nodes). * * @private * @param {Uint8Array} value The encoded byte sequence to decode. * @returns {number[]} The decoded list of relative OID nodes. */ _PdfObjectIdentifier.prototype._decodeRelativeObjectIdentifier = function (value) { if (value.length === 0) { return []; } else if (value.length > 1 && (value[value.length - 1] & 128) !== 0) { throw new Error('The relative object identifier is too long and was shortened.'); } var nodes = []; var currentNode = 0; for (var i = 0; i < value.length; i++) { var byte = value[i]; if (byte === 0x80 && currentNode === 0) { throw new Error('Relative oid node has unsupported padding.'); } currentNode <<= 7; currentNode += (byte & 0x7f); if ((byte & 0x80) === 0) { nodes.push(currentNode); currentNode = 0; } } return nodes; }; return _PdfObjectIdentifier; }()); export { _PdfObjectIdentifier };