all files / core/ pdf-parser.js

42.21% Statements 325/770
32.93% Branches 190/577
63.89% Functions 23/36
41.96% Lines 321/765
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 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175                                     87× 87× 87× 87× 87× 87× 87×   68030×       5978× 5978× 5978× 5978× 5978×       5977×       5978×         5978× 11× 11×   5978×                     5978× 5978× 5978× 5978× 5978× 5978× 38649× 32309× 32309×       32309× 1094×   32309×     6340× 362× 362×           5978×       5978×                       5978×   32671×   5978× 373×   5978×     5978×   82× 82× 82× 82× 82× 82× 1362× 1362×                 82× 82× 82×         82×                                                                                                                         1280× 1280×   1362× 82×   1280× 1280×     82×   834× 834× 834× 834× 834× 834× 4250×                                                     4250×   4250×   834×   198×     198×   192×         192× 96× 96×           96× 96×       96×   192× 192×       10195× 10195× 10195× 28817× 106×   28711× 25×     28686×   28681× 10089×   18622×   10089×                           5978×   82×   834×   135× 135×   135× 135×   153× 153× 147× 147×     145× 145× 145× 145×                         2627× 2627×             2627× 2627× 426× 426×     426× 426×   2627×   2621×   2614×     2614×     2614×                                               192× 120×   72× 72×         87× 87× 87× 87× 87× 87× 87× 87× 87× 87× 87× 87×   87× 87×   10021×         10021× 10021×               8623× 8623× 8623× 8623× 2611×                       135× 135× 807× 807×       807×         807×   807×     807× 807×   135×           135× 135× 135×   145× 145× 640×       640× 640×   640× 640× 640×     640×     640× 640×     640×       640×   640× 640× 164× 164×       640× 640×   145×           145×                           138× 138×   2331×     6012× 5346× 232× 232× 232× 232×   5114×   666× 88×     88×     88×     88×   578×                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     640×       640×                                                                                
define(["require", "exports", "./pdf-primitives", "./utils", "./base-stream", "./predictor-stream", "./flate-stream", "./compression/jpeg-stream", "./compression/lempel-ziv-welch-stream", "./compression/jpx-stream", "./compression/ascii-85-stream", "./compression/ascii-hex-stream", "./compression/pdf-fax-stream", "./compression/run-length-stream", "./compression/jbig2-stream", "./security/encryptors/cipher-tranform"], function (require, exports, pdf_primitives_1, utils_1, base_stream_1, predictor_stream_1, flate_stream_1, jpeg_stream_1, lempel_ziv_welch_stream_1, jpx_stream_1, ascii_85_stream_1, ascii_hex_stream_1, pdf_fax_stream_1, run_length_stream_1, jbig2_stream_1, cipher_tranform_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var maxCacheLength = 1000;
    var maxNumberLength = 5552;
    var endOfFile = 'EOF';
    var specialChars = [
        1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        1, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ];
    var _PdfLexicalOperator = (function () {
        function _PdfLexicalOperator(stream, isFormDataFormat) {
            Eif (isFormDataFormat === void 0) { isFormDataFormat = false; }
            this.stream = stream;
            this.nextChar();
            this.stringBuffer = [];
            this._hexStringNumber = 0;
            this.beginInlineImagePosition = -1;
            this._isFormsDataFormat = isFormDataFormat;
        }
        _PdfLexicalOperator.prototype.nextChar = function () {
            return (this.currentChar = this.stream.getByte());
        };
        _PdfLexicalOperator.prototype.peekChar = function () {
            return this.stream.peekByte();
        };
        _PdfLexicalOperator.prototype.getNumber = function () {
            var ch = this.currentChar;
            var eNotation = false;
            var divideBy = 0;
            var sign = 0;
            if (ch === 0x2d) {
                sign = -1;
                ch = this.nextChar();
                Iif (ch === 0x2d) {
                    ch = this.nextChar();
                }
            }
            else Iif (ch === 0x2b) {
                sign = 1;
                ch = this.nextChar();
            }
            Iif (ch === 0x0a || ch === 0x0d) {
                do {
                    ch = this.nextChar();
                } while (ch === 0x0a || ch === 0x0d);
            }
            if (ch === 0x2e) {
                divideBy = 10;
                ch = this.nextChar();
            }
            Iif (ch < 0x30 || ch > 0x39) {
                if (utils_1._isWhiteSpace(ch) || ch === -1) {
                    if (divideBy === 10 && sign === 0) {
                        return 0;
                    }
                    if (divideBy === 0 && sign === -1) {
                        return 0;
                    }
                }
                throw new utils_1.FormatError("Invalid number: " + String.fromCharCode(ch) + " (charCode " + ch + ")");
            }
            sign = sign || 1;
            var baseValue = ch - 0x30;
            var powerValue = 0;
            var powerValueSign = 1;
            ch = this.nextChar();
            while (ch >= 0) {
                if (ch >= 0x30 && ch <= 0x39) {
                    var currentDigit = ch - 0x30;
                    Iif (eNotation) {
                        powerValue = powerValue * 10 + currentDigit;
                    }
                    else {
                        if (divideBy !== 0) {
                            divideBy *= 10;
                        }
                        baseValue = baseValue * 10 + currentDigit;
                    }
                }
                else if (ch === 0x2e) {
                    Eif (divideBy === 0) {
                        divideBy = 1;
                    }
                    else {
                        break;
                    }
                }
                else Iif (ch === 0x2d) {
                    ch = this.nextChar();
                    continue;
                }
                else Iif (ch === 0x45 || ch === 0x65) {
                    ch = this.peekChar();
                    if (ch === 0x2b || ch === 0x2d) {
                        powerValueSign = ch === 0x2d ? -1 : 1;
                        this.nextChar();
                    }
                    else if (ch < 0x30 || ch > 0x39) {
                        break;
                    }
                    eNotation = true;
                }
                else {
                    break;
                }
                ch = this.nextChar();
            }
            if (divideBy !== 0) {
                baseValue /= divideBy;
            }
            Iif (eNotation) {
                baseValue *= Math.pow(10, (powerValueSign * powerValue));
            }
            return sign * baseValue;
        };
        _PdfLexicalOperator.prototype.getString = function () {
            var numParen = 1;
            var done = false;
            var stringBuffer = this.stringBuffer;
            stringBuffer.length = 0;
            var ch = this.nextChar();
            while (true) {
                var charBuffered = false;
                switch (ch | 0) {
                    case -1:
                        done = true;
                        break;
                    case 0x28:
                        ++numParen;
                        stringBuffer.push('(');
                        break;
                    case 0x29:
                        Eif (--numParen === 0) {
                            this.nextChar();
                            done = true;
                        }
                        else {
                            stringBuffer.push(')');
                        }
                        break;
                    case 0x5c:
                        ch = this.nextChar();
                        switch (ch) {
                            case -1:
                                done = true;
                                break;
                            case 0x6e:
                                stringBuffer.push('\n');
                                break;
                            case 0x72:
                                stringBuffer.push('\r');
                                break;
                            case 0x74:
                                stringBuffer.push('\t');
                                break;
                            case 0x62:
                                stringBuffer.push('\b');
                                break;
                            case 0x66:
                                stringBuffer.push('\f');
                                break;
                            case 0x5c:
                            case 0x28:
                            case 0x29:
                                stringBuffer.push(String.fromCharCode(ch));
                                break;
                            case 0x30:
                            case 0x31:
                            case 0x32:
                            case 0x33:
                            case 0x34:
                            case 0x35:
                            case 0x36:
                            case 0x37:
                                var x = ch & 0x0f;
                                ch = this.nextChar();
                                charBuffered = true;
                                if (ch >= 0x30 && ch <= 0x37) {
                                    x = (x << 3) + (ch & 0x0f);
                                    ch = this.nextChar();
                                    if (ch >= 0x30 && ch <= 0x37) {
                                        charBuffered = false;
                                        x = (x << 3) + (ch & 0x0f);
                                    }
                                }
                                stringBuffer.push(String.fromCharCode(x));
                                break;
                            case 0x0d:
                                if (this.peekChar() === 0x0a) {
                                    this.nextChar();
                                }
                                break;
                            case 0x0a:
                                break;
                            default:
                                stringBuffer.push(String.fromCharCode(ch));
                                break;
                        }
                        break;
                    default:
                        stringBuffer.push(String.fromCharCode(ch));
                        break;
                }
                if (done) {
                    break;
                }
                Eif (!charBuffered) {
                    ch = this.nextChar();
                }
            }
            return stringBuffer.join('');
        };
        _PdfLexicalOperator.prototype.getName = function () {
            var ch;
            var previousCh;
            var stringBuffer = this.stringBuffer;
            stringBuffer.length = 0;
            ch = this.nextChar();
            while (ch >= 0 && !specialChars[ch]) {
                Iif (ch === 0x23) {
                    ch = this.nextChar();
                    if (specialChars[ch]) {
                        stringBuffer.push('#');
                        break;
                    }
                    var x = this._toHexDigit(ch);
                    if (x !== -1) {
                        previousCh = ch;
                        ch = this.nextChar();
                        var x2 = this._toHexDigit(ch);
                        if (x2 === -1) {
                            stringBuffer.push('#', String.fromCharCode(previousCh));
                            if (specialChars[ch]) {
                                break;
                            }
                            stringBuffer.push(String.fromCharCode(ch));
                            ch = this.nextChar();
                            continue;
                        }
                        stringBuffer.push(String.fromCharCode((x << 4) | x2));
                    }
                    else {
                        stringBuffer.push('#', String.fromCharCode(ch));
                    }
                }
                else {
                    stringBuffer.push(String.fromCharCode(ch));
                }
                ch = this.nextChar();
            }
            return pdf_primitives_1._PdfName.get(stringBuffer.join(''));
        };
        _PdfLexicalOperator.prototype.getHexString = function () {
            var stringBuffer = this.stringBuffer;
            stringBuffer.length = 0;
            var ch = this.currentChar;
            var isFirstHex = true;
            var firstDigit;
            var secondDigit;
            this._hexStringNumber = 0;
            while (true) {
                Iif (ch < 0) {
                    break;
                }
                else if (ch === 0x3e) {
                    this.nextChar();
                    break;
                }
                else Iif (specialChars[ch] === 1) {
                    ch = this.nextChar();
                    continue;
                }
                else {
                    if (isFirstHex) {
                        firstDigit = this._toHexDigit(ch);
                        Iif (firstDigit === -1) {
                            ch = this.nextChar();
                            continue;
                        }
                    }
                    else {
                        secondDigit = this._toHexDigit(ch);
                        Iif (secondDigit === -1) {
                            ch = this.nextChar();
                            continue;
                        }
                        stringBuffer.push(String.fromCharCode((firstDigit << 4) | secondDigit));
                    }
                    isFirstHex = !isFirstHex;
                    ch = this.nextChar();
                }
            }
            return stringBuffer.join('');
        };
        _PdfLexicalOperator.prototype.getObject = function () {
            var comment = false;
            var ch = this.currentChar;
            while (true) {
                if (ch < 0) {
                    return endOfFile;
                }
                if (comment) {
                    if (ch === 0x0a || ch === 0x0d) {
                        comment = false;
                    }
                }
                else if (ch === 0x25) {
                    comment = true;
                }
                else if (specialChars[ch] !== 1) {
                    break;
                }
                ch = this.nextChar();
            }
            switch (ch | 0) {
                case 0x30:
                case 0x31:
                case 0x32:
                case 0x33:
                case 0x34:
                case 0x35:
                case 0x36:
                case 0x37:
                case 0x38:
                case 0x39:
                case 0x2b:
                case 0x2d:
                case 0x2e:
                    return this.getNumber();
                case 0x28:
                    return this.getString();
                case 0x2f:
                    return this.getName();
                case 0x5b:
                    this.nextChar();
                    return pdf_primitives_1._PdfCommand.get('[');
                case 0x5d:
                    this.nextChar();
                    return pdf_primitives_1._PdfCommand.get(']');
                case 0x3c:
                    ch = this.nextChar();
                    if (ch === 0x3c) {
                        this.nextChar();
                        return pdf_primitives_1._PdfCommand.get('<<');
                    }
                    return this.getHexString();
                case 0x3e:
                    ch = this.nextChar();
                    Eif (ch === 0x3e) {
                        this.nextChar();
                        return pdf_primitives_1._PdfCommand.get('>>');
                    }
                    return pdf_primitives_1._PdfCommand.get('>');
                case 0x7b:
                    this.nextChar();
                    return pdf_primitives_1._PdfCommand.get('{');
                case 0x7d:
                    this.nextChar();
                    return pdf_primitives_1._PdfCommand.get('}');
                case 0x29:
                    this.nextChar();
                    throw new utils_1.FormatError("Illegal character: " + ch);
            }
            var str = String.fromCharCode(ch);
            Iif (ch < 0x20 || ch > 0x7f) {
                var nextCh = this.peekChar();
                if (nextCh >= 0x20 && nextCh <= 0x7f) {
                    this.nextChar();
                    return pdf_primitives_1._PdfCommand.get(str);
                }
            }
            ch = this.nextChar();
            while (ch >= 0 && !specialChars[ch]) {
                var possibleCommand = str + String.fromCharCode(ch);
                Iif (!this._isFormsDataFormat && str.length === 128) {
                    throw new utils_1.FormatError("Command token too long: " + str.length);
                }
                str = possibleCommand;
                ch = this.nextChar();
            }
            if (str === 'true') {
                return true;
            }
            if (str === 'false') {
                return false;
            }
            Iif (str === 'null') {
                return null;
            }
            Iif (str === 'BI') {
                this.beginInlineImagePosition = this.stream.position;
            }
            return pdf_primitives_1._PdfCommand.get(str);
        };
        _PdfLexicalOperator.prototype.peekObj = function () {
            var streamPos = this.stream.position;
            var currentChar = this.currentChar;
            var beginInlineImagePosition = this.beginInlineImagePosition;
            var nextObj;
            try {
                nextObj = this.getObject();
            }
            catch (ex) { }
            this.stream.position = streamPos;
            this.currentChar = currentChar;
            this.beginInlineImagePosition = beginInlineImagePosition;
            return nextObj;
        };
        _PdfLexicalOperator.prototype.skipToNextLine = function () {
            var ch = this.currentChar;
            while (ch >= 0) {
                Eif (ch === 0x0d) {
                    ch = this.nextChar();
                    Eif (ch === 0x0a) {
                        this.nextChar();
                    }
                    break;
                }
                else if (ch === 0x0a) {
                    this.nextChar();
                    break;
                }
                ch = this.nextChar();
            }
        };
        _PdfLexicalOperator.prototype._toHexDigit = function (ch) {
            if (ch >= 0x30 && ch <= 0x39) {
                return ch & 0x0f;
            }
            Eif ((ch >= 0x41 && ch <= 0x46) || (ch >= 0x61 && ch <= 0x66)) {
                return (ch & 0x0f) + 9;
            }
            return -1;
        };
        return _PdfLexicalOperator;
    }());
    exports._PdfLexicalOperator = _PdfLexicalOperator;
    var _PdfParser = (function () {
        function _PdfParser(lexicalOperator, xref, allowStreams, recoveryMode, encryptor) {
            if (allowStreams === void 0) { allowStreams = false; }
            if (recoveryMode === void 0) { recoveryMode = false; }
            this._isColorSpace = false;
            this._isPassword = false;
            this._isImageExtraction = false;
            this.lexicalOperator = lexicalOperator;
            this.xref = xref;
            this.allowStreams = allowStreams;
            this.recoveryMode = recoveryMode;
            this.imageCache = new Map();
            this._encryptor = encryptor;
            this.refill();
        }
        _PdfParser.prototype.refill = function () {
            this.first = this.lexicalOperator.getObject();
            this.second = this.lexicalOperator.getObject();
        };
        _PdfParser.prototype.shift = function () {
            Iif (this.second instanceof pdf_primitives_1._PdfCommand && this.second.command === 'ID') {
                this.first = this.second;
                this.second = null;
            }
            else {
                this.first = this.second;
                this.second = this.lexicalOperator.getObject();
            }
        };
        _PdfParser.prototype.tryShift = function () {
            try {
                this.shift();
                return true;
            }
            catch (e) {
                return false;
            }
        };
        _PdfParser.prototype.getObject = function (arguement1, arguement2, arguement3) {
            var cipherTransform;
            var first = this.first;
            this.shift();
            if (first instanceof pdf_primitives_1._PdfCommand) {
                switch (first.command) {
                    case 'BI':
                        if (typeof arguement1 === 'number' && typeof arguement2 === 'number') {
                            return this.makeInlineImage(arguement1, arguement2, arguement3);
                        }
                        else if (arguement1 instanceof cipher_tranform_1._CipherTransform) {
                            return this.makeInlineImage(arguement1);
                        }
                        else {
                            return this.makeInlineImage();
                        }
                    case '[':
                        var array = [];
                        while (!pdf_primitives_1._isCommand(this.first, ']') && this.first !== endOfFile) {
                            var entry = void 0;
                            Iif (typeof arguement1 === 'number' && typeof arguement2 === 'number') {
                                cipherTransform = this._encryptor._createCipherTransform(arguement1, arguement2);
                                entry = this.getObject(arguement1, arguement2, arguement3);
                            }
                            else Iif (arguement1 instanceof cipher_tranform_1._CipherTransform) {
                                cipherTransform = arguement1;
                                entry = this.getObject(arguement1);
                            }
                            else {
                                entry = this.getObject(arguement1);
                            }
                            Iif (array.length === 0 && pdf_primitives_1._isName(entry, 'Indexed')) {
                                this._isColorSpace = true;
                            }
                            entry = utils_1._decodeText(entry, this._isColorSpace, this._isPassword);
                            array.push(entry);
                        }
                        Iif (this.first === endOfFile) {
                            if (this.recoveryMode) {
                                return array;
                            }
                            throw new utils_1.ParserEndOfFileException('End of file inside array.');
                        }
                        this._isColorSpace = false;
                        this.shift();
                        return array;
                    case '<<':
                        var dictionary = new pdf_primitives_1._PdfDictionary(this.xref);
                        while (!pdf_primitives_1._isCommand(this.first, '>>') && this.first !== endOfFile) {
                            Iif (!(this.first instanceof pdf_primitives_1._PdfName)) {
                                this.shift();
                                continue;
                            }
                            var key = this.first.name;
                            if (key === 'U' || key === 'O' || key === 'ID') {
                                this._isPassword = true;
                            }
                            this.shift();
                            var isEnd = this._checkEnd();
                            Iif (isEnd || (pdf_primitives_1._isCommand(this.first, '>>') && pdf_primitives_1._isCommand(this.second, 'stream'))) {
                                break;
                            }
                            Iif (typeof arguement1 === 'number' && typeof arguement2 === 'number') {
                                cipherTransform = this._encryptor._createCipherTransform(arguement1, arguement2);
                            }
                            var value = void 0;
                            Iif (typeof arguement1 === 'number' && typeof arguement2 === 'number') {
                                value = this.getObject(arguement1, arguement2, arguement3);
                            }
                            else Iif (arguement1 instanceof cipher_tranform_1._CipherTransform) {
                                value = this.getObject(arguement1);
                            }
                            else {
                                value = this.getObject();
                            }
                            value = utils_1._decodeText(value, this._isColorSpace, this._isPassword);
                            if (value && value instanceof pdf_primitives_1._PdfName) {
                                var name_1 = value.name;
                                Iif (name_1.includes('#20')) {
                                    value.name = name_1.replace(/#20/g, ' ');
                                }
                            }
                            this._isPassword = false;
                            dictionary.set(key, value);
                        }
                        Iif (this.first === endOfFile) {
                            if (this.recoveryMode) {
                                return dictionary;
                            }
                            throw new utils_1.ParserEndOfFileException('End of file inside dictionary.');
                        }
                        if (pdf_primitives_1._isCommand(this.second, 'stream')) {
                            Eif (this.allowStreams === true) {
                                Iif (arguement1 instanceof cipher_tranform_1._CipherTransform) {
                                    cipherTransform = arguement1;
                                }
                                else Iif (arguement3 && typeof arguement2 === 'number') {
                                    cipherTransform = this._encryptor._createCipherTransform(arguement1, arguement2);
                                }
                                Iif (typeof arguement2 === 'boolean' && arguement2) {
                                    return this.makeStream(dictionary, cipherTransform, arguement2);
                                }
                                else {
                                    return this.makeStream(dictionary, cipherTransform);
                                }
                            }
                            else {
                                return dictionary;
                            }
                        }
                        this.shift();
                        return dictionary;
                    default:
                        return first;
                }
            }
            if (Number.isInteger(first)) {
                if (Number.isInteger(this.first) && pdf_primitives_1._isCommand(this.second, 'R')) {
                    var ref = pdf_primitives_1._PdfReference.get(first, this.first);
                    this.shift();
                    this.shift();
                    return ref;
                }
                return first;
            }
            if (typeof first === 'string') {
                Iif (arguement1 instanceof cipher_tranform_1._CipherTransform) {
                    cipherTransform = arguement1;
                }
                else Iif (typeof arguement1 === 'number' && typeof arguement2 === 'number') {
                    cipherTransform = this._encryptor._createCipherTransform(arguement1, arguement2);
                }
                Iif (cipherTransform) {
                    return cipherTransform.decryptString(first);
                }
                return first;
            }
            return first;
        };
        _PdfParser.prototype.findDiscreteDecodeInlineStreamEnd = function (stream) {
            var startPos = stream.position;
            var foundEnd = false;
            var b;
            var markerLength;
            b = stream.getByte();
            while (b !== -1) {
                if (b !== 0xff) {
                    b = stream.getByte();
                    continue;
                }
                switch (stream.getByte()) {
                    case 0x00:
                        break;
                    case 0xff:
                        stream.skip(-1);
                        break;
                    case 0xd9:
                        foundEnd = true;
                        break;
                    case 0xc0:
                    case 0xc1:
                    case 0xc2:
                    case 0xc3:
                    case 0xc5:
                    case 0xc6:
                    case 0xc7:
                    case 0xc9:
                    case 0xca:
                    case 0xcb:
                    case 0xcd:
                    case 0xce:
                    case 0xcf:
                    case 0xc4:
                    case 0xcc:
                    case 0xda:
                    case 0xdb:
                    case 0xdc:
                    case 0xdd:
                    case 0xde:
                    case 0xdf:
                    case 0xe0:
                    case 0xe1:
                    case 0xe2:
                    case 0xe3:
                    case 0xe4:
                    case 0xe5:
                    case 0xe6:
                    case 0xe7:
                    case 0xe8:
                    case 0xe9:
                    case 0xea:
                    case 0xeb:
                    case 0xec:
                    case 0xed:
                    case 0xee:
                    case 0xef:
                    case 0xfe:
                        markerLength = stream.getUnsignedInteger16();
                        if (markerLength > 2) {
                            stream.skip(markerLength - 2);
                        }
                        else {
                            stream.skip(-2);
                        }
                        break;
                }
                if (foundEnd) {
                    break;
                }
                b = stream.getByte();
            }
            var length = stream.position - startPos;
            if (b === -1) {
                stream.skip(-length);
                return this.findDefaultInlineStreamEnd(stream);
            }
            this.inlineStreamSkipEI(stream);
            return length;
        };
        _PdfParser.prototype.findDecodeInlineStreamEnd = function (stream) {
            var startPos = stream.position;
            var ch;
            while ((ch = stream.getByte()) !== -1) {
                if (ch === 0x7e) {
                    var tildePos = stream.position;
                    ch = stream.peekByte();
                    while (utils_1._isWhiteSpace(ch)) {
                        stream.skip();
                        ch = stream.peekByte();
                    }
                    if (ch === 0x3e) {
                        stream.skip();
                        break;
                    }
                    if (stream.position > tildePos) {
                        var maybeEI = stream.peekBytes(2);
                        if (maybeEI[0] === 0x45 && maybeEI[1] === 0x49) {
                            break;
                        }
                    }
                }
            }
            var length = stream.position - startPos;
            if (ch === -1) {
                stream.skip(-length);
                return this.findDefaultInlineStreamEnd(stream);
            }
            this.inlineStreamSkipEI(stream);
            return length;
        };
        _PdfParser.prototype.findHexDecodeInlineStreamEnd = function (stream) {
            var startPos = stream.position;
            var ch;
            ch = stream.getByte();
            while (ch !== -1) {
                if (ch === 0x3e) {
                    break;
                }
                ch = stream.getByte();
            }
            var length = stream.position - startPos;
            if (ch === -1) {
                stream.skip(-length);
                return this.findDefaultInlineStreamEnd(stream);
            }
            this.inlineStreamSkipEI(stream);
            return length;
        };
        _PdfParser.prototype.inlineStreamSkipEI = function (stream) {
            var state = 0;
            var ch;
            ch = stream.getByte();
            while (ch !== -1) {
                if (state === 0) {
                    state = ch === 0x45 ? 1 : 0;
                }
                else if (state === 1) {
                    state = ch === 0x49 ? 2 : 0;
                }
                else if (state === 2) {
                    break;
                }
                ch = stream.getByte();
            }
        };
        _PdfParser.prototype.makeInlineImage = function (arguement1, arguement2, arguement3) {
            var lexicalOperator = this.lexicalOperator;
            var stream = lexicalOperator.stream;
            var dictionary = new pdf_primitives_1._PdfDictionary(this.xref);
            var dictLength;
            var cipherTransform;
            if (arguement3) {
                if (arguement1 instanceof cipher_tranform_1._CipherTransform) {
                    cipherTransform = arguement1;
                }
                else {
                    cipherTransform = this._encryptor._createCipherTransform(arguement1, arguement2);
                }
            }
            while (!pdf_primitives_1._isCommand(this.first, 'ID') && this.first !== endOfFile) {
                if (!(this.first instanceof pdf_primitives_1._PdfName)) {
                    throw new utils_1.FormatError('Dictionary key must be a name object');
                }
                var key = this.first.name;
                this.shift();
                if (this.first.name === endOfFile) {
                    break;
                }
                if (arguement1 instanceof cipher_tranform_1._CipherTransform) {
                    dictionary.set(key, this.getObject(arguement1));
                }
                else {
                    dictionary.set(key, this.getObject(arguement1, arguement2, arguement3));
                }
            }
            if (lexicalOperator.beginInlineImagePosition !== -1) {
                dictLength = stream.position - lexicalOperator.beginInlineImagePosition;
            }
            var filter = dictionary.get('F', 'Filter');
            var filterName;
            if (filter instanceof pdf_primitives_1._PdfName) {
                filterName = filter.name;
            }
            else if (Array.isArray(filter)) {
                var reference = filter[0];
                var filterZero = (reference !== null && typeof reference !== 'undefined' && reference instanceof pdf_primitives_1._PdfReference) ?
                    this.xref._fetch(reference) :
                    reference;
                if (filterZero) {
                    filterName = filterZero.name;
                }
            }
            var startPos = stream.position;
            var length;
            switch (filterName) {
                case 'DCT':
                case 'DCTDecode':
                    length = this.findDiscreteDecodeInlineStreamEnd(stream);
                    break;
                case 'A85':
                case 'ASCII85Decode':
                    length = this.findDecodeInlineStreamEnd(stream);
                    break;
                case 'AHx':
                case 'ASCIIHexDecode':
                    length = this.findHexDecodeInlineStreamEnd(stream);
                    break;
                default:
                    length = this.findDefaultInlineStreamEnd(stream);
            }
            var imageStream = stream.makeSubStream(startPos, length, dictionary);
            var cacheKey;
            if (length < maxCacheLength && dictLength < maxNumberLength) {
                var imageBytes = imageStream.getBytes();
                imageStream.reset();
                var initialStreamPos = stream.position;
                stream.position = lexicalOperator.beginInlineImagePosition;
                var dictBytes = stream.getBytes(dictLength);
                stream.position = initialStreamPos;
                cacheKey = this._computeMaxNumber(imageBytes) + '_' + this._computeMaxNumber(dictBytes);
                var cacheEntry = this.imageCache.get(cacheKey);
                if (cacheEntry !== undefined) {
                    this.second = pdf_primitives_1._PdfCommand.get('EI');
                    this.shift();
                    cacheEntry.reset();
                    return cacheEntry;
                }
            }
            if (cipherTransform) {
                imageStream = cipherTransform.createStream(imageStream, length);
            }
            imageStream = this.filter(imageStream, dictionary, length);
            imageStream.dictionary = dictionary;
            if (cacheKey !== undefined) {
                this.imageCache.set(cacheKey, imageStream);
            }
            this.second = pdf_primitives_1._PdfCommand.get('EI');
            this.shift();
            return imageStream;
        };
        _PdfParser.prototype._computeMaxNumber = function (bytes) {
            var bytesLength = bytes.length;
            var a = 1;
            var b = 0;
            for (var i = 0; i < bytesLength; ++i) {
                a += bytes[i] & 0xff;
                b += a;
            }
            return (b % 65521 << 16) | a % 65521;
        };
        _PdfParser.prototype.makeStream = function (dictionary, cipherTransform, makeFilter) {
            Eif (makeFilter === void 0) { makeFilter = false; }
            var lexicalOperator = this.lexicalOperator;
            var stream = lexicalOperator.stream;
            lexicalOperator.skipToNextLine();
            var startPosition = stream.position - 1;
            var length = dictionary.get('Length');
            Iif (!Number.isInteger(length)) {
                length = 0;
            }
            stream.position = startPosition + length;
            lexicalOperator.nextChar();
            Eif (this.tryShift() && pdf_primitives_1._isCommand(this.second, 'endstream')) {
                this.shift();
            }
            else {
                var endStreamSignature = new Uint8Array([0x65, 0x6e, 0x64, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d]);
                var actualLength = this._findStreamLength(startPosition, endStreamSignature);
                if (actualLength < 0) {
                    var end = endStreamSignature.length - 1;
                    var truncatedSignature = endStreamSignature.slice(0, end);
                    var maybeLength = this._findStreamLength(startPosition, truncatedSignature);
                    if (maybeLength >= 0) {
                        var lastByte = stream.peekBytes(end + 1)[end];
                        if (utils_1._isWhiteSpace(lastByte)) {
                            actualLength = maybeLength;
                        }
                    }
                    if (actualLength < 0) {
                        throw new utils_1.FormatError('Missing endstream command.');
                    }
                }
                length = actualLength;
                lexicalOperator.nextChar();
                this.shift();
                this.shift();
            }
            this.shift();
            stream = stream.makeSubStream(startPosition, length, dictionary);
            Eif (!makeFilter) {
                Iif (cipherTransform) {
                    stream = cipherTransform.createStream(stream, length);
                }
                stream = this.filter(stream, dictionary, length);
            }
            stream.dictionary = dictionary;
            return stream;
        };
        _PdfParser.prototype.filter = function (stream, dictionary, length) {
            var _this = this;
            var filter = dictionary.get('F', 'Filter');
            var params = dictionary.get('DP', 'DecodeParms');
            Eif (filter instanceof pdf_primitives_1._PdfName) {
                return this.makeFilter(stream, filter.name, length, params);
            }
            else if (Array.isArray(filter)) {
                var item = filter[0];
                if (item instanceof pdf_primitives_1._PdfName) {
                    return this.makeFilter(stream, item.name, length, params);
                }
                else if (item instanceof pdf_primitives_1._PdfCommand) {
                    return this.makeFilter(stream, item.command, length, params);
                }
            }
            var maybeLength = length;
            if (Array.isArray(filter)) {
                var filterArray = filter;
                var paramsArray_1 = params;
                filterArray.forEach(function (reference, i) {
                    filter = reference instanceof pdf_primitives_1._PdfReference ? _this.xref._fetch(reference) : reference;
                    if (!(filter instanceof pdf_primitives_1._PdfName) && !(filter instanceof pdf_primitives_1._PdfCommand)) {
                        throw new utils_1.FormatError("Bad filter name '" + filter + "'");
                    }
                    params = null;
                    if (Array.isArray(paramsArray_1) && i !== null && typeof i !== 'undefined' && i in paramsArray_1) {
                        var ref = paramsArray_1[i];
                        params = ref instanceof pdf_primitives_1._PdfReference ? _this.xref._fetch(ref) : ref;
                    }
                    if (filter instanceof pdf_primitives_1._PdfName) {
                        stream = _this.makeFilter(stream, filter.name, maybeLength, params);
                    }
                    else if (filter instanceof pdf_primitives_1._PdfCommand) {
                        stream = _this.makeFilter(stream, filter.command, maybeLength, params);
                    }
                    maybeLength = null;
                });
            }
            return stream;
        };
        _PdfParser.prototype.makeFilter = function (stream, name, maybeLength, params) {
            Iif (maybeLength === 0) {
                return new base_stream_1._PdfNullStream();
            }
            var earlyChange = 1;
            try {
                Eif (name === 'Fl' || name === 'FlateDecode') {
                    Iif (params) {
                        return new predictor_stream_1.PdfPredictorStream(new flate_stream_1._PdfFlateStream(stream, maybeLength), maybeLength, params);
                    }
                    return new flate_stream_1._PdfFlateStream(stream, maybeLength);
                }
                else if (this._isImageExtraction) {
                    switch (name) {
                        case 'LZW':
                        case 'LZWDecode':
                            if (params) {
                                if (params.has('EarlyChange')) {
                                    earlyChange = params.get('EarlyChange');
                                }
                                return new predictor_stream_1.PdfPredictorStream(new lempel_ziv_welch_stream_1._PdfLempelZivWelchStream(stream, maybeLength, earlyChange), maybeLength, params);
                            }
                            return new lempel_ziv_welch_stream_1._PdfLempelZivWelchStream(stream, maybeLength, earlyChange);
                        case 'DCT':
                        case 'DCTDecode':
                            return new jpeg_stream_1._PdfJpegStream(stream, maybeLength, params);
                        case 'JPX':
                        case 'JPXDecode':
                            return new jpx_stream_1._PdfJpxStream(stream, maybeLength, params);
                        case 'A85':
                        case 'ASCII85Decode':
                            return new ascii_85_stream_1._PdfAscii85Stream(stream, maybeLength);
                        case 'AHx':
                        case 'ASCIIHexDecode':
                            return new ascii_hex_stream_1._PdfAsciiHexStream(stream, maybeLength);
                        case 'CCF':
                        case 'CCITTFaxDecode':
                            return new pdf_fax_stream_1._PdfFaxStream(stream, maybeLength, params);
                        case 'RL':
                        case 'RunLengthDecode':
                            return new run_length_stream_1._PdfRunLengthStream(stream, maybeLength);
                        case 'JBIG2Decode':
                            return new jbig2_stream_1._PdfJbig2Stream(stream, maybeLength, params);
                        default:
                            return stream;
                    }
                }
                else {
                    return stream;
                }
            }
            catch (ex) {
                return new base_stream_1._PdfNullStream();
            }
        };
        _PdfParser.prototype._findStreamLength = function (startPosition, signature) {
            var stream = this.lexicalOperator.stream;
            stream.position = startPosition;
            var length = 2048;
            var signatureLength = signature.length;
            while (stream.position < stream.end) {
                var scanBytes = stream.peekBytes(length);
                var scanLength = scanBytes.length - signatureLength;
                if (scanLength <= 0) {
                    break;
                }
                var position = 0;
                while (position < scanLength) {
                    var j = 0;
                    while (j < signatureLength && scanBytes[position + j] === signature[j]) {
                        j++;
                    }
                    if (j >= signatureLength) {
                        stream.position += position;
                        return stream.position - startPosition;
                    }
                    position++;
                }
                stream.position += scanLength;
            }
            return -1;
        };
        _PdfParser.prototype.findDefaultInlineStreamEnd = function (stream) {
            var startPosition = stream.position;
            var n = 10;
            var state = 0;
            var ch;
            var endImagePosition;
            ch = stream.getByte();
            while (ch !== -1) {
                if (state === 0) {
                    state = ch === 0x45 ? 1 : 0;
                }
                else if (state === 1) {
                    state = ch === 0x49 ? 2 : 0;
                }
                else {
                    if (state !== 2) {
                        throw new Error('findDefaultInlineStreamEnd - invalid state.');
                    }
                    if (ch === 0x20 || ch === 0xa || ch === 0xd) {
                        endImagePosition = stream.position;
                        var followingBytes = stream.peekBytes(n);
                        for (var i = 0, ii = followingBytes.length; i < ii; i++) {
                            ch = followingBytes[i];
                            if (ch === 0x0 && followingBytes[i + 1] !== 0x0) {
                                continue;
                            }
                            if (ch !== 0xa && ch !== 0xd && (ch < 0x20 || ch > 0x7f)) {
                                state = 0;
                                break;
                            }
                        }
                        if (state !== 2) {
                            ch = stream.getByte();
                            continue;
                        }
                        if (state === 2) {
                            break;
                        }
                    }
                    else {
                        state = 0;
                    }
                }
                ch = stream.getByte();
            }
            if (ch === -1) {
                if (typeof endImagePosition !== 'undefined') {
                    stream.skip(-(stream.position - endImagePosition));
                }
            }
            var endOffset = 4;
            stream.skip(-endOffset);
            ch = stream.peekByte();
            stream.skip(endOffset);
            if (!utils_1._isWhiteSpace(ch)) {
                endOffset--;
            }
            return stream.position - endOffset - startPosition;
        };
        _PdfParser.prototype._checkEnd = function () {
            Iif (this.first === endOfFile) {
                return true;
            }
            else {
                return false;
            }
        };
        return _PdfParser;
    }());
    exports._PdfParser = _PdfParser;
    var _Linearization = (function () {
        function _Linearization(stream) {
            this.isValid = false;
            var parser = new _PdfParser(new _PdfLexicalOperator(stream), null);
            var obj1 = parser.getObject();
            var obj2 = parser.getObject();
            var obj3 = parser.getObject();
            var dictionary = parser.getObject();
            this.isValid = Number.isInteger(obj1) && Number.isInteger(obj2) && pdf_primitives_1._isCommand(obj3, 'obj') && typeof dictionary !== 'undefined';
            Eif (this.isValid) {
                var obj = dictionary.get('Linearized');
                this.isValid = typeof obj !== 'undefined' && obj > 0;
            }
            Iif (this.isValid) {
                var length_1 = this.getInt(dictionary, 'L');
                if (length_1 !== stream.length) {
                    throw new Error('The L parameter in the linearization dictionary ' + 'does not equal the stream length.');
                }
                this.length = length_1;
                this.hints = this.getHints(dictionary);
                this.objectNumberFirst = this.getInt(dictionary, 'O');
                this.endFirst = this.getInt(dictionary, 'E');
                this.pageCount = this.getInt(dictionary, 'N');
                this.mainXRefEntriesOffset = this.getInt(dictionary, 'T');
                this.pageFirst = dictionary.has('P') ? this.getInt(dictionary, 'P', true) : 0;
            }
        }
        _Linearization.prototype.getInt = function (dictionary, name, allowZeroValue) {
            if (allowZeroValue === void 0) { allowZeroValue = false; }
            var obj = dictionary.get(name);
            if (typeof obj !== 'undefined' && Number.isInteger(obj) && (allowZeroValue ? obj >= 0 : obj > 0)) {
                return obj;
            }
            throw new Error("The '" + name + "' parameter in the linearization " + 'dictionary is invalid.');
        };
        _Linearization.prototype.getHints = function (dictionary) {
            var hints = dictionary.getArray('H');
            var hintsLength = hints.length;
            if (hints && (hintsLength === 2 || hintsLength === 4)) {
                for (var index = 0; index < hintsLength; index++) {
                    var hint = hints[index];
                    if (!(Number.isInteger(hint) && hint > 0)) {
                        throw new Error("Hint (" + index + ") in the linearization dictionary is invalid.");
                    }
                }
                return hints;
            }
            throw new Error('Hint array in the linearization dictionary is invalid.');
        };
        return _Linearization;
    }());
    exports._Linearization = _Linearization;
});