all files / core/security/digital-signature/asn1/ identifier-mapping.js

17.59% Statements 19/108
0% Branches 0/52
11.76% Functions 2/17
17.76% Lines 19/107
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155                                                                                                                                                                                                                                                                                
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfObjectIdentifier = (function () {
        function _PdfObjectIdentifier() {
            this._encoding = new Uint8Array(0);
            this._period = '.'.charCodeAt(0);
        }
        _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;
        };
        _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));
        };
        _PdfObjectIdentifier.prototype._getDotDelimitedNotation = function () {
            return this._getNodes().join('.');
        };
        _PdfObjectIdentifier.prototype._getAbstractSyntaxNotation = function () {
            return "{ " + this._getNodes().map(function (node) { return node.toString(); }).join(' ') + " }";
        };
        _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;
        };
        _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);
        };
        _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;
        };
        _PdfObjectIdentifier.prototype._fromBytesUnsafe = function (bytes) {
            var oid = new _PdfObjectIdentifier();
            oid._encoding = new Uint8Array(bytes);
            return oid;
        };
        _PdfObjectIdentifier.prototype.toString = function () {
            return this._getDotDelimitedNotation();
        };
        _PdfObjectIdentifier.prototype._toJson = function () {
            return this._getDotDelimitedNotation();
        };
        _PdfObjectIdentifier.prototype._toBytes = function () {
            return new Uint8Array(this._encoding);
        };
        _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);
        };
        _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;
    }());
    exports._PdfObjectIdentifier = _PdfObjectIdentifier;
});