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;
});
|