all files / core/security/digital-signature/asn1/ abstract-syntax.js

19.15% Statements 86/449
0% Branches 0/185
2.11% Functions 2/95
19.28% Lines 86/446
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
define(["require", "exports", "../../../utils", "./enumerator", "./identifier-mapping", "./utils", "./syntax-verifier"], function (require, exports, utils_1, enumerator_1, identifier_mapping_1, utils_2, syntax_verifier_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfAbstractSyntaxElement = (function () {
        function _PdfAbstractSyntaxElement() {
            this._recursionCount = 0;
            this._nestingRecursionLimit = 5;
            this._name = '';
            this._tagClass = enumerator_1._TagClassType.universal;
            this._construction = enumerator_1._ConstructionType.primitive;
            this._tagNumber = 0;
        }
        _PdfAbstractSyntaxElement.prototype._getTagNumber = function () {
            return this._tagNumber;
        };
        _PdfAbstractSyntaxElement.prototype._setTagNumber = function (value) {
            if (!Number.isSafeInteger(value) || value < 0) {
                throw new Error("Tag " + value + " was not a non-negative number.");
            }
            this._tagNumber = value;
        };
        _PdfAbstractSyntaxElement.prototype._getLength = function () {
            var value = this._getValue();
            return value.length;
        };
        _PdfAbstractSyntaxElement.prototype._tagLength = function () {
            var tagNumber = this._getTagNumber();
            if (tagNumber < 31) {
                return 1;
            }
            var n = tagNumber;
            var i = 0;
            while (n !== 0) {
                n >>>= 7;
                i++;
            }
            return i;
        };
        _PdfAbstractSyntaxElement.prototype._toBytes = function () {
            var buffers = this._toBuffers();
            var totalLength = buffers.reduce(function (sum, arr) { return sum + arr.length; }, 0);
            var result = new Uint8Array(totalLength);
            var offset = 0;
            for (var _i = 0, buffers_1 = buffers; _i < buffers_1.length; _i++) {
                var buffer = buffers_1[_i];
                result.set(buffer, offset);
                offset += buffer.length;
            }
            if (result.length >= 4 && result[0] === 48 && result[1] === 130) {
                var actualContentLength = result.length - 4;
                var lengthHigh = (actualContentLength >> 8) & 0xFF;
                var lengthLow = actualContentLength & 0xFF;
                result[2] = lengthHigh;
                result[3] = lengthLow;
            }
            return result;
        };
        _PdfAbstractSyntaxElement.prototype._validateSize = function (name, units, actualSize, min, max) {
            var effectiveMax = typeof max === 'undefined' ? Infinity : max;
            if (actualSize < min) {
                throw new Error(name + " must be at least " + min + " " + units + ", but was " + actualSize + " " + units + ".");
            }
            if (actualSize > effectiveMax) {
                throw new Error(name + " must not exceed " + effectiveMax + " " + units + ", but was " + actualSize + " " + units + ".");
            }
        };
        _PdfAbstractSyntaxElement.prototype._validateRange = function (name, actualValue, min, max) {
            if (actualValue < min) {
                throw new Error(name + " must be at least " + min + ", but was " + actualValue + ".");
            }
            if (typeof max !== 'undefined' && max !== null && actualValue > max) {
                throw new Error(name + " must not exceed " + max + ", but was " + actualValue + ".");
            }
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedString = function (min, max) {
            var bitString = this._getBitString();
            var ret = bitString;
            this._validateSize(this._name || 'Bit string', 'bits', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedUtf8String = function (min, max) {
            var utf8String = this._getUtf8String();
            var ret = utf8String;
            this._validateSize(this._name || 'Unicode string', 'characters', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedSequenceOf = function (min, max) {
            var sequenceOf = this._getSequenceOf();
            var ret = sequenceOf;
            this._validateSize(this._name || 'Sequence of', 'elements', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedSetOf = function (min, max) {
            var sequenceOf = this._getSequenceOf();
            this._validateSize(this._name || 'Set of', 'elements', sequenceOf.length, min, max);
            return sequenceOf;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedNumericString = function (min, max) {
            var ret = this._getNumericString();
            this._validateSize(this._name || 'Numeric string', 'characters', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedPrintableString = function (min, max) {
            var ret = this._getPrintableString();
            this._validateSize(this._name || 'Printable ASCII string', 'characters', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedTextString = function (min, max) {
            var ret = this._getTeleprinterText();
            this._validateSize(this._name || 'Legacy encoded string', 'characters', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedVideoString = function (min, max) {
            var ret = this._getVideoTextInformation();
            this._validateSize(this._name || 'Videotex string', 'characters', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedIA5String = function (min, max) {
            var ret = this._getInternationalAlphabetString();
            this._validateSize(this._name || 'ASCII string', 'characters', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedGraphicString = function (min, max) {
            var ret = this._getGraphicString();
            this._validateSize(this._name || 'Graphic string', 'characters', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedVisibleString = function (min, max) {
            var ret = this._getVisibleString();
            this._validateSize(this._name || 'Visible string', 'characters', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._sizeConstrainedUniversalString = function (min, max) {
            var ret = this._getUniversalString();
            this._validateSize(this._name || 'Unicode string', 'characters', ret.length, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._rangeConstrainedNumber = function (min, max) {
            var ret = this._getInteger();
            this._validateRange(this._name || 'Number', ret, min, max);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._validateTag = function (permittedClasses, permittedConstruction, permittedNumbers) {
            if (permittedClasses.indexOf(this._tagClass) === -1) {
                return -1;
            }
            else if (permittedConstruction.indexOf(this._construction) === -1) {
                return -2;
            }
            else if (permittedNumbers.indexOf(this._getTagNumber()) === -1) {
                return -3;
            }
            else {
                return 0;
            }
        };
        _PdfAbstractSyntaxElement.prototype._toElement = function () {
            return this;
        };
        _PdfAbstractSyntaxElement.prototype._fromElement = function (el) {
            this._tagClass = el._tagClass;
            this._construction = el._construction;
            var tagNumber = el._getTagNumber();
            this._setTagNumber(tagNumber);
            var value = el._getValue();
            this._setValue(value);
        };
        _PdfAbstractSyntaxElement.prototype._toString = function () {
            if (this._tagClass === enumerator_1._TagClassType.universal) {
                var tagNumber = this._getTagNumber();
                switch (tagNumber) {
                    case enumerator_1._UniversalType.endOfContent: return 'END-OF-CONTENT';
                    case enumerator_1._UniversalType.abstractSyntaxBoolean: return this._getBooleanValue() ? 'TRUE' : 'FALSE';
                    case enumerator_1._UniversalType.integer: return this._getInteger().toString();
                    case enumerator_1._UniversalType.octetString:
                        return "'" + Array
                            .from(this._getOctetString())
                            .map(function (byte) { return utils_1._padStart(byte.toString(16), 2, '0'); })
                            .join('') + "'H";
                    case enumerator_1._UniversalType.nullValue: return 'NULL';
                    case enumerator_1._UniversalType.objectIdentifier: return this._getObjectIdentifier()._getAbstractSyntaxNotation();
                    case enumerator_1._UniversalType.objectDescriptor: return "\"" + this._getObjectDescriptor() + "\"";
                    case enumerator_1._UniversalType.external: return '_PdfExternal';
                    case enumerator_1._UniversalType.enumerated: return this._getEnumerated().toString();
                    case enumerator_1._UniversalType.embeddedDataValue: return 'EMBEDDED PDV';
                    case enumerator_1._UniversalType.utf8String: return "\"" + this._getUtf8String() + "\"";
                    case enumerator_1._UniversalType.relativeObjectIdentifier: return "{ " + this._getRelativeObjectIdentifier()
                        .map(function (arc) { return arc.toString(); }).join('.') + " }";
                    case enumerator_1._UniversalType.time: return "\"" + this._getTime() + "\"";
                    case enumerator_1._UniversalType.sequence: return "{ " + this._getSequenceOf()
                        .map(function (el) { return (el.name.length ? el.name + " " + el.toString() : el.toString()); })
                        .join(' , ') + " }";
                    case enumerator_1._UniversalType.abstractSyntaxSet: return "{ " + this._getAbstractSetOf()
                        .map(function (el) { return (el.name.length ? el.name + " " + el.toString() : el.toString()); })
                        .join(' , ') + " }";
                    case enumerator_1._UniversalType.numericString: return "\"" + this._getNumericString() + "\"";
                    case enumerator_1._UniversalType.printableString: return "\"" + this._getPrintableString() + "\"";
                    case enumerator_1._UniversalType.teleprinterTextExchange: return 'TeletexString';
                    case enumerator_1._UniversalType.videoTextInformationSystem: return 'VideotexString';
                    case enumerator_1._UniversalType.internationalAlphabetString: return "\"" + this._getInternationalAlphabetString() + "\"";
                    case enumerator_1._UniversalType.characterString: return 'CHARACTER STRING';
                    case enumerator_1._UniversalType.date: return "\"" + this._getDate().toISOString() + "\"";
                    case enumerator_1._UniversalType.timeOfDay: {
                        var tod = this._getTimeOfDay();
                        return "\"" + tod.getUTCHours() + ":" + tod.getUTCMinutes() + ":" + tod.getUTCSeconds() + "\"";
                    }
                    case enumerator_1._UniversalType.dateTime: return "\"" + this._getDateTime().toISOString() + "\"";
                    case enumerator_1._UniversalType.objectIdResourceIdentifier: return this._getObjectIdResourceIdentifier();
                    case enumerator_1._UniversalType.relativeResourceIdentifier: return this._getRelativeResourceIdentifier();
                    default: return "[UNIV " + this._getTagNumber() + "]: " + this._getValue().toString();
                }
            }
            else if (this._tagClass === enumerator_1._TagClassType.context) {
                return "[CTXT " + this._getTagNumber() + "]: " + this._getValue().toString();
            }
            else if (this._tagClass === enumerator_1._TagClassType.abstractSyntaxPrivate) {
                return "[PRIV " + this._getTagNumber() + "]: " + this._getValue().toString();
            }
            else {
                return "[APPL " + this._getTagNumber() + "]: " + this._getValue().toString();
            }
        };
        _PdfAbstractSyntaxElement.prototype._toJson = function () {
            if (this._tagClass === enumerator_1._TagClassType.universal) {
                switch (this._getTagNumber()) {
                    case enumerator_1._UniversalType.endOfContent:
                        return undefined;
                    case enumerator_1._UniversalType.abstractSyntaxBoolean:
                        return this._getBooleanValue();
                    case enumerator_1._UniversalType.integer: {
                        var ret = this._getInteger();
                        return ret;
                    }
                    case enumerator_1._UniversalType.bitString: {
                        var bits = this._getBitString();
                        return {
                            length: bits.length,
                            value: Array.from(utils_2._packBits(bits)).map(function (byte) { return byte.toString(16); }).join('')
                        };
                    }
                    case enumerator_1._UniversalType.octetString:
                        return Array.from(this._getOctetString())
                            .map(function (byte) { return byte.toString(16); }).join('');
                    case enumerator_1._UniversalType.nullValue:
                        return null;
                    case enumerator_1._UniversalType.objectIdentifier:
                        return this._getObjectIdentifier()._toJson();
                    case enumerator_1._UniversalType.objectDescriptor:
                        return this._getObjectDescriptor();
                    case enumerator_1._UniversalType.enumerated:
                        return this._getEnumerated().toString();
                    case enumerator_1._UniversalType.utf8String:
                        return this._getUtf8String();
                    case enumerator_1._UniversalType.relativeObjectIdentifier:
                        return this._getRelativeObjectIdentifier()
                            .map(function (arc) { return arc.toString(); }).join('.');
                    case enumerator_1._UniversalType.teleprinterTextExchange:
                        return String.fromCodePoint.apply(String, Array.from(this._getTeleprinterText()));
                    case enumerator_1._UniversalType.videoTextInformationSystem:
                        return String.fromCodePoint.apply(String, Array.from(this._getVideoTextInformation()));
                    case enumerator_1._UniversalType.graphicString:
                        return this._getGraphicString();
                    case enumerator_1._UniversalType.visibleString:
                        return this._getVisibleString();
                    case enumerator_1._UniversalType.universalString:
                        return this._getUniversalString();
                    case enumerator_1._UniversalType.bmpString:
                        return this._getBmpString();
                    case enumerator_1._UniversalType.date:
                        return this._getDate().toISOString();
                    case enumerator_1._UniversalType.timeOfDay: {
                        var tod = this._getTimeOfDay();
                        return tod.getUTCHours() + ":" + tod.getUTCMinutes() + ":" + tod.getUTCSeconds();
                    }
                    case enumerator_1._UniversalType.dateTime:
                        return this._getDateTime().toISOString();
                    case enumerator_1._UniversalType.objectIdResourceIdentifier:
                        return this._getObjectIdResourceIdentifier();
                    case enumerator_1._UniversalType.relativeResourceIdentifier:
                        return this._getRelativeResourceIdentifier();
                    default:
                        return undefined;
                }
            }
            return undefined;
        };
        _PdfAbstractSyntaxElement.prototype._sortCanonically = function (elements) {
            return elements.sort(function (a, b) {
                var aClassOrder = a._tagClass;
                var bClassOrder = b._tagClass;
                if (aClassOrder !== bClassOrder) {
                    return aClassOrder - bClassOrder;
                }
                var aBytes = a._toBytes();
                var bBytes = b._toBytes();
                var minLength = Math.min(aBytes.length, bBytes.length);
                for (var i = 0; i < minLength; i++) {
                    if (aBytes[i] !== bBytes[i]) {
                        return aBytes[i] - bBytes[i];
                    }
                }
                return aBytes.length - bBytes.length;
            });
        };
        _PdfAbstractSyntaxElement.prototype._isUniquelyTagged = function (elements) {
            var finds = new Set([]);
            for (var i = 0; i < elements.length; i++) {
                var key = ((elements[i]._tagClass << 30)
                    + elements[i]._getTagNumber());
                if (finds.has(key)) {
                    return false;
                }
                finds.add(key);
            }
            return true;
        };
        _PdfAbstractSyntaxElement.prototype._getInteger = function () {
            if (this._construction !== enumerator_1._ConstructionType.primitive) {
                throw new Error('Number cannot be constructed.');
            }
            var value = this._getValue();
            return this._decodeInteger(value);
        };
        _PdfAbstractSyntaxElement.prototype._setInteger = function (value) {
            this._setValue(this._encodeInteger(value));
        };
        _PdfAbstractSyntaxElement.prototype._getObjectIdentifier = function () {
            if (this._construction !== enumerator_1._ConstructionType.primitive) {
                throw new Error('Object identifier cannot be constructed.');
            }
            return this._decodeObjectIdentifier(this._getValue());
        };
        _PdfAbstractSyntaxElement.prototype._setObjectIdentifier = function (value) {
            this._setValue(this._encodeObjectIdentifier(value));
        };
        _PdfAbstractSyntaxElement.prototype._getEnumerated = function () {
            return Number(this._getInteger());
        };
        _PdfAbstractSyntaxElement.prototype._setEnumerated = function (value) {
            this._setInteger(value);
        };
        _PdfAbstractSyntaxElement.prototype._getRelativeObjectIdentifier = function () {
            if (this._construction !== enumerator_1._ConstructionType.primitive) {
                throw new Error('Relative oid cannot be constructed.');
            }
            return this._decodeRelativeObjectIdentifier(this._getValue());
        };
        _PdfAbstractSyntaxElement.prototype._setRelativeObjectIdentifier = function (value) {
            this._setValue(this._encodeRelativeObjectIdentifier(value));
        };
        _PdfAbstractSyntaxElement.prototype._getTime = function () {
            return this._decodeTime(this._getValue());
        };
        _PdfAbstractSyntaxElement.prototype._setTime = function (value) {
            this._setValue(this._encodeTime(value));
        };
        _PdfAbstractSyntaxElement.prototype._getDate = function () {
            return this._decodeDate(this._getValue());
        };
        _PdfAbstractSyntaxElement.prototype._setDate = function (value) {
            this._setValue(this._encodeDate(value));
        };
        _PdfAbstractSyntaxElement.prototype._getTimeOfDay = function () {
            return this._decodeTimeOfDay(this._getValue());
        };
        _PdfAbstractSyntaxElement.prototype._setTimeOfDay = function (value) {
            this._setValue(this._encodeTimeOfDay(value));
        };
        _PdfAbstractSyntaxElement.prototype._getDateTime = function () {
            return this._decodeDateTime(this._getValue());
        };
        _PdfAbstractSyntaxElement.prototype._setDateTime = function (value) {
            this._setValue(this._encodeDateTime(value));
        };
        _PdfAbstractSyntaxElement.prototype._getObjectIdResourceIdentifier = function () {
            return this._decodeObjectIdResourceIdentifier(this._getValue());
        };
        _PdfAbstractSyntaxElement.prototype._setObjectIdResourceIdentifier = function (value) {
            this._setValue(this._encodeObjectIdResourceIdentifier(value));
        };
        _PdfAbstractSyntaxElement.prototype._getRelativeResourceIdentifier = function () {
            return this._decodeRelativeResourceIdentifier(this._getValue());
        };
        _PdfAbstractSyntaxElement.prototype._setRelativeResourceIdentifier = function (value) {
            this._setValue(this._encodeRelativeResourceIdentifier(value));
        };
        _PdfAbstractSyntaxElement.prototype._encodeInteger = function (value) {
            return utils_2._integerToBuffer(value);
        };
        _PdfAbstractSyntaxElement.prototype._decodeInteger = function (value) {
            if (value.length === 0) {
                throw new Error('Integer or enumeration encoded on zero bytes');
            }
            if (value.length > 2 && ((value[0] === 0xFF && value[1] >= 128) ||
                (value[0] === 0x00 && value[1] < 128))) {
                var slice = value.slice(0, 16);
                var hexString = Array.from(slice)
                    .map(function (byte) { return utils_1._padStart(byte.toString(16), 2, '0'); })
                    .join('');
                throw new Error('Unnecessary padding bytes on . ' +
                    ("First 16 bytes of the offending value were: 0x" + hexString));
            }
            return utils_2._bufferToInteger(value);
        };
        _PdfAbstractSyntaxElement.prototype._encodeObjectIdentifier = function (value) {
            return value._toBytes();
        };
        _PdfAbstractSyntaxElement.prototype._decodeObjectIdentifier = function (value) {
            var oid = new identifier_mapping_1._PdfObjectIdentifier();
            return oid._fromBytes(value);
        };
        _PdfAbstractSyntaxElement.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);
        };
        _PdfAbstractSyntaxElement.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('The relative object identifier node has unsupported padding.');
                }
                currentNode <<= 7;
                currentNode += (byte & 0x7f);
                if ((byte & 0x80) === 0) {
                    nodes.push(currentNode);
                    currentNode = 0;
                }
            }
            return nodes;
        };
        _PdfAbstractSyntaxElement.prototype._encodeTime = function (value) {
            return utils_1._stringToBytes(value.replace(/,/g, '.'));
        };
        _PdfAbstractSyntaxElement.prototype._decodeTime = function (bytes) {
            return utils_2._convertBytesToText(bytes);
        };
        _PdfAbstractSyntaxElement.prototype._encodeDate = function (date) {
            if (date.getFullYear() < 1582 || date.getFullYear() > 9999) {
                throw new Error("The Date " + date.toISOString() + " may not be encoded, because the "
                    + 'year must be greater than 1581 and less than 10000.');
            }
            return utils_1._stringToBytes(utils_1._padStart(date.getFullYear().toString(), 4, '0') +
                utils_1._padStart((date.getMonth() + 1).toString(), 2, '0') +
                utils_1._padStart(date.getDate().toString(), 2, '0'));
        };
        _PdfAbstractSyntaxElement.prototype._decodeDate = function (bytes) {
            var str = utils_2._convertBytesToText(bytes);
            var year = parseInt(str.slice(0, 4), 10);
            var month = parseInt(str.slice(4, 6), 10) - 1;
            var day = parseInt(str.slice(6, 8), 10);
            syntax_verifier_1._validateDate('DATE', year, month, day);
            return new Date(year, month, day);
        };
        _PdfAbstractSyntaxElement.prototype._encodeTimeOfDay = function (time) {
            return utils_1._stringToBytes(utils_1._padStart(time.getHours().toString(), 2, '0')
                + utils_1._padStart(time.getMinutes().toString(), 2, '0')
                + utils_1._padStart(time.getSeconds().toString(), 2, '0'));
        };
        _PdfAbstractSyntaxElement.prototype._decodeTimeOfDay = function (bytes) {
            var str = utils_2._convertBytesToText(bytes);
            var hours = parseInt(str.slice(0, 2), 10);
            var minutes = parseInt(str.slice(2, 4), 10);
            var seconds = parseInt(str.slice(4, 6), 10);
            syntax_verifier_1._validateTime('TIME-OF-DAY', hours, minutes, seconds);
            var ret = new Date();
            ret.setHours(hours);
            ret.setMinutes(minutes);
            ret.setSeconds(seconds);
            return ret;
        };
        _PdfAbstractSyntaxElement.prototype._decodeDateTime = function (bytes) {
            var str = utils_2._convertBytesToText(bytes);
            var year = parseInt(str.slice(0, 4), 10);
            var month = parseInt(str.slice(4, 6), 10) - 1;
            var day = parseInt(str.slice(6, 8), 10);
            var hours = parseInt(str.slice(8, 10), 10);
            var minutes = parseInt(str.slice(10, 12), 10);
            var seconds = parseInt(str.slice(12, 14), 10);
            syntax_verifier_1._validateDateTime('DATE-TIME', year, month, day, hours, minutes, seconds);
            return new Date(year, month, day, hours, minutes, seconds);
        };
        _PdfAbstractSyntaxElement.prototype._decodeGeneralString = function (value) {
            for (var i = 0; i < value.length; i++) {
                var char = value[i];
                if (!syntax_verifier_1._isGeneralCharacter(char)) {
                    throw new Error('The input must contain only standard ASCII characters.');
                }
            }
            return utils_2._convertBytesToText(value);
        };
        _PdfAbstractSyntaxElement.prototype._decodeGraphicString = function (value) {
            for (var i = 0; i < value.length; i++) {
                var char = value[i];
                if (!syntax_verifier_1._isGraphicCharacter(char)) {
                    throw new Error('Only standard printable ASCII characters are allowed.');
                }
            }
            return utils_2._convertBytesToText(value);
        };
        _PdfAbstractSyntaxElement.prototype._decodeNumericString = function (value) {
            for (var i = 0; i < value.length; i++) {
                var char = value[i];
                if (!syntax_verifier_1.isNumericString(char)) {
                    throw new Error('The input must contain only numeric characters and spaces.');
                }
            }
            return utils_2._convertBytesToText(value);
        };
        _PdfAbstractSyntaxElement.prototype._decodeObjectDescriptor = function (value) {
            for (var i = 0; i < value.length; i++) {
                var char = value[i];
                if (!syntax_verifier_1._isGraphicCharacter(char)) {
                    throw new Error('Only standard printable ASCII characters are allowed in the object descriptor.');
                }
            }
            return utils_2._convertBytesToText(value);
        };
        _PdfAbstractSyntaxElement.prototype._decodeObjectIdResourceIdentifier = function (bytes) {
            return utils_2._convertBytesToText(bytes);
        };
        _PdfAbstractSyntaxElement.prototype._decodeRelativeResourceIdentifier = function (bytes) {
            return utils_2._convertBytesToText(bytes);
        };
        _PdfAbstractSyntaxElement.prototype._decodePrintableString = function (value) {
            var printableStringCharacters = 'etaoinsrhdlucmfywgpbvkxqjzETAOINSRHDLUCMFYWGPBVKXQJZ' +
                '0123456789 \'()+,-./:=?';
            for (var i = 0; i < value.length; i++) {
                var char = value[i];
                if (!syntax_verifier_1._isPrintableCharacter(char)) {
                    throw new Error('Printable ASCII string can only contain these characters: ' +
                        printableStringCharacters + '. ' +
                        ("Encountered character code " + char + "."));
                }
            }
            return utils_2._convertBytesToText(value);
        };
        _PdfAbstractSyntaxElement.prototype._decodeVisibleString = function (value) {
            for (var i = 0; i < value.length; i++) {
                var char = value[i];
                if (!syntax_verifier_1._isGraphicCharacter(char)) {
                    throw new Error('Visible string can only contain characters between 0x20 and 0x7E. ' +
                        ("Encountered character code " + char + "."));
                }
            }
            return utils_2._convertBytesToText(value);
        };
        _PdfAbstractSyntaxElement.prototype._encodeBitString = function (value) {
            if (value.length === 0) {
                return new Uint8Array([0]);
            }
            var byteLength = (value.length >>> 3) + (value.length % 8 !== 0 ? 1 : 0);
            var result = new Uint8Array(byteLength + 1);
            result[0] = 8 - (value.length % 8);
            if (result[0] === 8) {
                result[0] = 0;
            }
            result.set(utils_2._packBits(value), 1);
            return result;
        };
        _PdfAbstractSyntaxElement.prototype._encodeBoolean = function (value) {
            return new Uint8Array([(value ? 0xFF : 0x00)]);
        };
        _PdfAbstractSyntaxElement.prototype._encodeDateTime = function (value) {
            var year = value.getFullYear();
            if (year < 1582 || year > 9999) {
                throw new Error('The date cannot be encoded');
            }
            var dateTimeString = utils_1._padStart(year.toString(), 4, '0') +
                utils_1._padStart((value.getMonth() + 1).toString(), 2, '0') +
                utils_1._padStart(value.getDate().toString(), 2, '0') +
                utils_1._padStart(value.getHours().toString(), 2, '0') +
                utils_1._padStart(value.getMinutes().toString(), 2, '0') +
                utils_1._padStart(value.getSeconds().toString(), 2, '0');
            return utils_1._stringToBytes(dateTimeString);
        };
        _PdfAbstractSyntaxElement.prototype._encodeObjectIdResourceIdentifier = function (value) {
            return utils_1._stringToBytes(value);
        };
        _PdfAbstractSyntaxElement.prototype._encodeRelativeResourceIdentifier = function (value) {
            return utils_1._stringToBytes(value);
        };
        _PdfAbstractSyntaxElement.prototype._encodeSequence = function (value) {
            var byteArrays = value.map(function (element) { return element._toBytes(); });
            var totalLength = byteArrays.reduce(function (sum, arr) { return sum + arr.length; }, 0);
            var result = new Uint8Array(totalLength);
            var offset = 0;
            for (var _i = 0, byteArrays_1 = byteArrays; _i < byteArrays_1.length; _i++) {
                var arr = byteArrays_1[_i];
                result.set(arr, offset);
                offset += arr.length;
            }
            return result;
        };
        _PdfAbstractSyntaxElement.prototype._encodeNumericString = function (value) {
            return utils_1._stringToBytes(value);
        };
        _PdfAbstractSyntaxElement.prototype._encodePrintableString = function (value) {
            return utils_1._stringToBytes(value);
        };
        _PdfAbstractSyntaxElement.prototype._encodeGraphicString = function (value) {
            return utils_1._stringToBytes(value);
        };
        _PdfAbstractSyntaxElement.prototype._encodeVisibleString = function (value) {
            return utils_1._stringToBytes(value);
        };
        _PdfAbstractSyntaxElement.prototype._encodeObjectDescriptor = function (value) {
            return utils_1._stringToBytes(value);
        };
        _PdfAbstractSyntaxElement.prototype._toEncodedBytes = function () {
            return this._toBytes();
        };
        return _PdfAbstractSyntaxElement;
    }());
    exports._PdfAbstractSyntaxElement = _PdfAbstractSyntaxElement;
});