UNPKG

@syncfusion/ej2-pdf

Version:

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

169 lines (168 loc) 5.75 kB
/** * Provides an internal sink that accumulates hash or encoding results, * tracking appended byte events and the latest result. * * @private */ var _PdfNativeAccumulatorSink = /** @class */ (function () { function _PdfNativeAccumulatorSink() { /** * Maintains the ordered list of events appended to the sink. * * @private */ this._events = []; } /* eslint-disable */ /** * Appends a new byte event to the sink and updates the current result. * * @private * @param {{bytes: Uint8Array}} event The event containing the bytes to add. // eslint-disable-line * @returns {void} nothing. */ _PdfNativeAccumulatorSink.prototype._add = function (event) { this._events.push(event); this._result = event.bytes; }; /* eslint-enable */ /** * Sets the accumulator result and replaces the internal event list with a single entry. * * @private * @param {Uint8Array} result The byte sequence to assign as the current result. * @returns {void} nothing. */ _PdfNativeAccumulatorSink.prototype._setResult = function (result) { this._result = result; this._events = [{ bytes: result }]; }; /** * Retrieves the most recently stored result bytes from the accumulator. * * @private * @returns {Uint8Array | null} The latest events byte value, or the stored result if no events exist. */ _PdfNativeAccumulatorSink.prototype._getResult = function () { if (this._events.length > 0) { return this._events[this._events.length - 1].bytes; } return this._result; }; return _PdfNativeAccumulatorSink; }()); export { _PdfNativeAccumulatorSink }; /** * Provides an internal buffered hash input adapter that accepts byte chunks * and emits the final digest to an accumulator sink on close. * * @private */ var _PdfNativeHashInput = /** @class */ (function () { function _PdfNativeHashInput(hasher, outputSink) { this._buffer = []; this._closed = false; this._hasher = hasher; this._outputSink = outputSink; } /** * Appends a chunk of data to the internal buffer for later hashing. * * @private * @param {Uint8Array} data The byte array to append. * @throws {Error} Thrown if data is added after the input is closed. * @returns {void} nothing. */ _PdfNativeHashInput.prototype._add = function (data) { if (this._closed) { throw new Error('Cannot add data to closed hash input'); } for (var i = 0; i < data.length; i++) { this._buffer.push(data[i]); } }; /** * Finalizes the input, computes the hash over the buffered data, and pushes the result * to the output sink. Subsequent calls are ignored. * * @private * @returns {void} This method does not return a value. */ _PdfNativeHashInput.prototype._close = function () { if (this._closed) { return; } this._closed = true; if (this._buffer.length > 0) { var inputData = new Uint8Array(this._buffer); var result = this._hasher._hash(inputData, 0, inputData.length); this._outputSink._add({ bytes: result }); } }; return _PdfNativeHashInput; }()); export { _PdfNativeHashInput }; /** * Represents an internal ASN.1 AlgorithmIdentifier encoder that produces * DER-encoded bytes for an algorithm OID with a NULL parameter. * * @private */ var _PdfNativeAlgorithmIdentifier = /** @class */ (function () { function _PdfNativeAlgorithmIdentifier(oid) { this._oid = oid; } /** * Encodes the algorithm identifier as DER: SEQUENCE { OBJECT IDENTIFIER, NULL }. * * @private * @returns {Uint8Array} The DER-encoded AlgorithmIdentifier bytes. */ _PdfNativeAlgorithmIdentifier.prototype._getEncoded = function () { var oidBytes = this._encodeObjectIdentifier(this._oid); var nullBytes = new Uint8Array([0x05, 0x00]); var contentLength = oidBytes.length + nullBytes.length; var result = []; result.push(0x30); result.push(contentLength); result.push.apply(result, Array.from(oidBytes)); result.push.apply(result, Array.from(nullBytes)); return new Uint8Array(result); }; /** * Encodes a dotted string object identifier (e.g., "1.2.840.113549") into DER form. * * @private * @param {string} oidString The dotted OID string to encode. * @returns {Uint8Array} The DER-encoded OBJECT IDENTIFIER bytes. */ _PdfNativeAlgorithmIdentifier.prototype._encodeObjectIdentifier = function (oidString) { var parts = oidString.split('.').map(Number); var bytes = []; bytes.push(parts[0] * 40 + parts[1]); for (var i = 2; i < parts.length; i++) { var value = parts[i]; if (value < 128) { bytes.push(value); } else { var temp = []; while (value > 0) { temp.unshift(value & 0x7F); value = Math.floor(value / 128); } for (var j = 0; j < temp.length - 1; j++) { temp[j] |= 0x80; } bytes.push.apply(bytes, temp); } } var result = []; result.push(0x06); result.push(bytes.length); result.push.apply(result, bytes); return new Uint8Array(result); }; return _PdfNativeAlgorithmIdentifier; }()); export { _PdfNativeAlgorithmIdentifier };