Skip to content
Snippets Groups Projects
pdfjs.parser.js 367 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexey Lunin's avatar
    Alexey Lunin committed
    12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000
            var output = new Uint8Array(outputLength);
            for (i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
              output.set(result[i], j);
            }
            return output;
          }
        };
    
        return AES256Cipher;
      })();
    
      var PDF17 = (function PDF17Closure() {
        function compareByteArrays(array1, array2) {
          if (array1.length !== array2.length) {
            return false;
          }
          for (var i = 0; i < array1.length; i++) {
            if (array1[i] !== array2[i]) {
              return false;
            }
          }
          return true;
        }
    
        function PDF17() {}
    
        PDF17.prototype = {
          checkOwnerPassword: function PDF17_checkOwnerPassword(
            password,
            ownerValidationSalt,
            userBytes,
            ownerPassword
          ) {
            var hashData = new Uint8Array(password.length + 56);
            hashData.set(password, 0);
            hashData.set(ownerValidationSalt, password.length);
            hashData.set(userBytes, password.length + ownerValidationSalt.length);
            var result = calculateSHA256(hashData, 0, hashData.length);
            return compareByteArrays(result, ownerPassword);
          },
          checkUserPassword: function PDF17_checkUserPassword(
            password,
            userValidationSalt,
            userPassword
          ) {
            var hashData = new Uint8Array(password.length + 8);
            hashData.set(password, 0);
            hashData.set(userValidationSalt, password.length);
            var result = calculateSHA256(hashData, 0, hashData.length);
            return compareByteArrays(result, userPassword);
          },
          getOwnerKey: function PDF17_getOwnerKey(
            password,
            ownerKeySalt,
            userBytes,
            ownerEncryption
          ) {
            var hashData = new Uint8Array(password.length + 56);
            hashData.set(password, 0);
            hashData.set(ownerKeySalt, password.length);
            hashData.set(userBytes, password.length + ownerKeySalt.length);
            var key = calculateSHA256(hashData, 0, hashData.length);
            var cipher = new AES256Cipher(key);
            return cipher.decryptBlock(ownerEncryption, false, new Uint8Array(16));
          },
          getUserKey: function PDF17_getUserKey(
            password,
            userKeySalt,
            userEncryption
          ) {
            var hashData = new Uint8Array(password.length + 8);
            hashData.set(password, 0);
            hashData.set(userKeySalt, password.length);
            //key is the decryption key for the UE string
            var key = calculateSHA256(hashData, 0, hashData.length);
            var cipher = new AES256Cipher(key);
            return cipher.decryptBlock(userEncryption, false, new Uint8Array(16));
          }
        };
        return PDF17;
      })();
    
      var PDF20 = (function PDF20Closure() {
        function concatArrays(array1, array2) {
          var t = new Uint8Array(array1.length + array2.length);
          t.set(array1, 0);
          t.set(array2, array1.length);
          return t;
        }
    
        function calculatePDF20Hash(password, input, userBytes) {
          //This refers to Algorithm 2.B as defined in ISO 32000-2
          var k = calculateSHA256(input, 0, input.length).subarray(0, 32);
          var e = [0];
          var i = 0;
          while (i < 64 || e[e.length - 1] > i - 32) {
            var arrayLength = password.length + k.length + userBytes.length;
    
            var k1 = new Uint8Array(arrayLength * 64);
            var array = concatArrays(password, k);
            array = concatArrays(array, userBytes);
            for (var j = 0, pos = 0; j < 64; j++, pos += arrayLength) {
              k1.set(array, pos);
            }
            //AES128 CBC NO PADDING with
            //first 16 bytes of k as the key and the second 16 as the iv.
            var cipher = new AES128Cipher(k.subarray(0, 16));
            e = cipher.encrypt(k1, k.subarray(16, 32));
            //Now we have to take the first 16 bytes of an unsigned
            //big endian integer... and compute the remainder
            //modulo 3.... That is a fairly large number and
            //JavaScript isn't going to handle that well...
            //So we're using a trick that allows us to perform
            //modulo math byte by byte
            var remainder = 0;
            for (var z = 0; z < 16; z++) {
              remainder *= 256 % 3;
              remainder %= 3;
              remainder += (e[z] >>> 0) % 3;
              remainder %= 3;
            }
            if (remainder === 0) {
              k = calculateSHA256(e, 0, e.length);
            } else if (remainder === 1) {
              k = calculateSHA384(e, 0, e.length);
            } else if (remainder === 2) {
              k = calculateSHA512(e, 0, e.length);
            }
            i++;
          }
          return k.subarray(0, 32);
        }
    
        function PDF20() {}
    
        function compareByteArrays(array1, array2) {
          if (array1.length !== array2.length) {
            return false;
          }
          for (var i = 0; i < array1.length; i++) {
            if (array1[i] !== array2[i]) {
              return false;
            }
          }
          return true;
        }
    
        PDF20.prototype = {
          hash: function PDF20_hash(password, concatBytes, userBytes) {
            return calculatePDF20Hash(password, concatBytes, userBytes);
          },
          checkOwnerPassword: function PDF20_checkOwnerPassword(
            password,
            ownerValidationSalt,
            userBytes,
            ownerPassword
          ) {
            var hashData = new Uint8Array(password.length + 56);
            hashData.set(password, 0);
            hashData.set(ownerValidationSalt, password.length);
            hashData.set(userBytes, password.length + ownerValidationSalt.length);
            var result = calculatePDF20Hash(password, hashData, userBytes);
            return compareByteArrays(result, ownerPassword);
          },
          checkUserPassword: function PDF20_checkUserPassword(
            password,
            userValidationSalt,
            userPassword
          ) {
            var hashData = new Uint8Array(password.length + 8);
            hashData.set(password, 0);
            hashData.set(userValidationSalt, password.length);
            var result = calculatePDF20Hash(password, hashData, []);
            return compareByteArrays(result, userPassword);
          },
          getOwnerKey: function PDF20_getOwnerKey(
            password,
            ownerKeySalt,
            userBytes,
            ownerEncryption
          ) {
            var hashData = new Uint8Array(password.length + 56);
            hashData.set(password, 0);
            hashData.set(ownerKeySalt, password.length);
            hashData.set(userBytes, password.length + ownerKeySalt.length);
            var key = calculatePDF20Hash(password, hashData, userBytes);
            var cipher = new AES256Cipher(key);
            return cipher.decryptBlock(ownerEncryption, false, new Uint8Array(16));
          },
          getUserKey: function PDF20_getUserKey(
            password,
            userKeySalt,
            userEncryption
          ) {
            var hashData = new Uint8Array(password.length + 8);
            hashData.set(password, 0);
            hashData.set(userKeySalt, password.length);
            //key is the decryption key for the UE string
            var key = calculatePDF20Hash(password, hashData, []);
            var cipher = new AES256Cipher(key);
            return cipher.decryptBlock(userEncryption, false, new Uint8Array(16));
          }
        };
        return PDF20;
      })();
    
      var CipherTransform = (function CipherTransformClosure() {
        function CipherTransform(stringCipherConstructor, streamCipherConstructor) {
          this.stringCipherConstructor = stringCipherConstructor;
          this.streamCipherConstructor = streamCipherConstructor;
        }
    
        CipherTransform.prototype = {
          createStream: function CipherTransform_createStream(stream, length) {
            var cipher = new this.streamCipherConstructor();
            return new DecryptStream(
              stream,
              length,
              function cipherTransformDecryptStream(data, finalize) {
                return cipher.decryptBlock(data, finalize);
              }
            );
          },
          decryptString: function CipherTransform_decryptString(s) {
            var cipher = new this.stringCipherConstructor();
            var data = stringToBytes(s);
            data = cipher.decryptBlock(data, true);
            return bytesToString(data);
          }
        };
        return CipherTransform;
      })();
    
      var CipherTransformFactory = (function CipherTransformFactoryClosure() {
        var defaultPasswordBytes = new Uint8Array([
          0x28,
          0xbf,
          0x4e,
          0x5e,
          0x4e,
          0x75,
          0x8a,
          0x41,
          0x64,
          0x00,
          0x4e,
          0x56,
          0xff,
          0xfa,
          0x01,
          0x08,
          0x2e,
          0x2e,
          0x00,
          0xb6,
          0xd0,
          0x68,
          0x3e,
          0x80,
          0x2f,
          0x0c,
          0xa9,
          0xfe,
          0x64,
          0x53,
          0x69,
          0x7a
        ]);
    
        function createEncryptionKey20(
          revision,
          password,
          ownerPassword,
          ownerValidationSalt,
          ownerKeySalt,
          uBytes,
          userPassword,
          userValidationSalt,
          userKeySalt,
          ownerEncryption,
          userEncryption,
          perms
        ) {
          if (password) {
            var passwordLength = Math.min(127, password.length);
            password = password.subarray(0, passwordLength);
          } else {
            password = [];
          }
          var pdfAlgorithm;
          if (revision === 6) {
            pdfAlgorithm = new PDF20();
          } else {
            pdfAlgorithm = new PDF17();
          }
    
          if (pdfAlgorithm) {
            if (
              pdfAlgorithm.checkUserPassword(
                password,
                userValidationSalt,
                userPassword
              )
            ) {
              return pdfAlgorithm.getUserKey(password, userKeySalt, userEncryption);
            } else if (
              password.length &&
              pdfAlgorithm.checkOwnerPassword(
                password,
                ownerValidationSalt,
                uBytes,
                ownerPassword
              )
            ) {
              return pdfAlgorithm.getOwnerKey(
                password,
                ownerKeySalt,
                uBytes,
                ownerEncryption
              );
            }
          }
    
          return null;
        }
    
        function prepareKeyData(
          fileId,
          password,
          ownerPassword,
          userPassword,
          flags,
          revision,
          keyLength,
          encryptMetadata
        ) {
          var hashDataSize = 40 + ownerPassword.length + fileId.length;
          var hashData = new Uint8Array(hashDataSize),
            i = 0,
            j,
            n;
          if (password) {
            n = Math.min(32, password.length);
            for (; i < n; ++i) {
              hashData[i] = password[i];
            }
          }
          j = 0;
          while (i < 32) {
            hashData[i++] = defaultPasswordBytes[j++];
          }
          // as now the padded password in the hashData[0..i]
          for (j = 0, n = ownerPassword.length; j < n; ++j) {
            hashData[i++] = ownerPassword[j];
          }
          hashData[i++] = flags & 0xff;
          hashData[i++] = (flags >> 8) & 0xff;
          hashData[i++] = (flags >> 16) & 0xff;
          hashData[i++] = (flags >>> 24) & 0xff;
          for (j = 0, n = fileId.length; j < n; ++j) {
            hashData[i++] = fileId[j];
          }
          if (revision >= 4 && !encryptMetadata) {
            hashData[i++] = 0xff;
            hashData[i++] = 0xff;
            hashData[i++] = 0xff;
            hashData[i++] = 0xff;
          }
          var hash = calculateMD5(hashData, 0, i);
          var keyLengthInBytes = keyLength >> 3;
          if (revision >= 3) {
            for (j = 0; j < 50; ++j) {
              hash = calculateMD5(hash, 0, keyLengthInBytes);
            }
          }
          var encryptionKey = hash.subarray(0, keyLengthInBytes);
          var cipher, checkData;
    
          if (revision >= 3) {
            for (i = 0; i < 32; ++i) {
              hashData[i] = defaultPasswordBytes[i];
            }
            for (j = 0, n = fileId.length; j < n; ++j) {
              hashData[i++] = fileId[j];
            }
            cipher = new ARCFourCipher(encryptionKey);
            checkData = cipher.encryptBlock(calculateMD5(hashData, 0, i));
            n = encryptionKey.length;
            var derivedKey = new Uint8Array(n),
              k;
            for (j = 1; j <= 19; ++j) {
              for (k = 0; k < n; ++k) {
                derivedKey[k] = encryptionKey[k] ^ j;
              }
              cipher = new ARCFourCipher(derivedKey);
              checkData = cipher.encryptBlock(checkData);
            }
            for (j = 0, n = checkData.length; j < n; ++j) {
              if (userPassword[j] !== checkData[j]) {
                return null;
              }
            }
          } else {
            cipher = new ARCFourCipher(encryptionKey);
            checkData = cipher.encryptBlock(defaultPasswordBytes);
            for (j = 0, n = checkData.length; j < n; ++j) {
              if (userPassword[j] !== checkData[j]) {
                return null;
              }
            }
          }
          return encryptionKey;
        }
    
        function decodeUserPassword(password, ownerPassword, revision, keyLength) {
          var hashData = new Uint8Array(32),
            i = 0,
            j,
            n;
          n = Math.min(32, password.length);
          for (; i < n; ++i) {
            hashData[i] = password[i];
          }
          j = 0;
          while (i < 32) {
            hashData[i++] = defaultPasswordBytes[j++];
          }
          var hash = calculateMD5(hashData, 0, i);
          var keyLengthInBytes = keyLength >> 3;
          if (revision >= 3) {
            for (j = 0; j < 50; ++j) {
              hash = calculateMD5(hash, 0, hash.length);
            }
          }
    
          var cipher, userPassword;
          if (revision >= 3) {
            userPassword = ownerPassword;
            var derivedKey = new Uint8Array(keyLengthInBytes),
              k;
            for (j = 19; j >= 0; j--) {
              for (k = 0; k < keyLengthInBytes; ++k) {
                derivedKey[k] = hash[k] ^ j;
              }
              cipher = new ARCFourCipher(derivedKey);
              userPassword = cipher.encryptBlock(userPassword);
            }
          } else {
            cipher = new ARCFourCipher(hash.subarray(0, keyLengthInBytes));
            userPassword = cipher.encryptBlock(ownerPassword);
          }
          return userPassword;
        }
    
        var identityName = Name.get("Identity");
    
        function CipherTransformFactory(dict, fileId, password) {
          var filter = dict.get("Filter");
          if (!isName(filter) || filter.name !== "Standard") {
            error("unknown encryption method");
          }
          this.dict = dict;
          var algorithm = dict.get("V");
          if (
            !isInt(algorithm) ||
            (algorithm !== 1 &&
              algorithm !== 2 &&
              algorithm !== 4 &&
              algorithm !== 5)
          ) {
            error("unsupported encryption algorithm");
          }
          this.algorithm = algorithm;
          var keyLength = dict.get("Length") || 40;
          if (!isInt(keyLength) || keyLength < 40 || keyLength % 8 !== 0) {
            error("invalid key length");
          }
    
          // prepare keys
          var ownerPassword = stringToBytes(dict.get("O")).subarray(0, 32);
          var userPassword = stringToBytes(dict.get("U")).subarray(0, 32);
          var flags = dict.get("P");
          var revision = dict.get("R");
          // meaningful when V is 4 or 5
          var encryptMetadata =
            (algorithm === 4 || algorithm === 5) &&
            dict.get("EncryptMetadata") !== false;
          this.encryptMetadata = encryptMetadata;
    
          var fileIdBytes = stringToBytes(fileId);
          var passwordBytes;
          if (password) {
            if (revision === 6) {
              try {
                password = utf8StringToString(password);
              } catch (ex) {
                warn(
                  "CipherTransformFactory: " +
                    "Unable to convert UTF8 encoded password."
                );
              }
            }
            passwordBytes = stringToBytes(password);
          }
    
          var encryptionKey;
          if (algorithm !== 5) {
            encryptionKey = prepareKeyData(
              fileIdBytes,
              passwordBytes,
              ownerPassword,
              userPassword,
              flags,
              revision,
              keyLength,
              encryptMetadata
            );
          } else {
            var ownerValidationSalt = stringToBytes(dict.get("O")).subarray(32, 40);
            var ownerKeySalt = stringToBytes(dict.get("O")).subarray(40, 48);
            var uBytes = stringToBytes(dict.get("U")).subarray(0, 48);
            var userValidationSalt = stringToBytes(dict.get("U")).subarray(32, 40);
            var userKeySalt = stringToBytes(dict.get("U")).subarray(40, 48);
            var ownerEncryption = stringToBytes(dict.get("OE"));
            var userEncryption = stringToBytes(dict.get("UE"));
            var perms = stringToBytes(dict.get("Perms"));
            encryptionKey = createEncryptionKey20(
              revision,
              passwordBytes,
              ownerPassword,
              ownerValidationSalt,
              ownerKeySalt,
              uBytes,
              userPassword,
              userValidationSalt,
              userKeySalt,
              ownerEncryption,
              userEncryption,
              perms
            );
          }
          if (!encryptionKey && !password) {
            throw new PasswordException(
              "No password given",
              PasswordResponses.NEED_PASSWORD
            );
          } else if (!encryptionKey && password) {
            // Attempting use the password as an owner password
            var decodedPassword = decodeUserPassword(
              passwordBytes,
              ownerPassword,
              revision,
              keyLength
            );
            encryptionKey = prepareKeyData(
              fileIdBytes,
              decodedPassword,
              ownerPassword,
              userPassword,
              flags,
              revision,
              keyLength,
              encryptMetadata
            );
          }
    
          if (!encryptionKey) {
            throw new PasswordException(
              "Incorrect Password",
              PasswordResponses.INCORRECT_PASSWORD
            );
          }
    
          this.encryptionKey = encryptionKey;
    
          if (algorithm >= 4) {
            this.cf = dict.get("CF");
            this.stmf = dict.get("StmF") || identityName;
            this.strf = dict.get("StrF") || identityName;
            this.eff = dict.get("EFF") || this.stmf;
          }
        }
    
        function buildObjectKey(num, gen, encryptionKey, isAes) {
          var key = new Uint8Array(encryptionKey.length + 9),
            i,
            n;
          for (i = 0, n = encryptionKey.length; i < n; ++i) {
            key[i] = encryptionKey[i];
          }
          key[i++] = num & 0xff;
          key[i++] = (num >> 8) & 0xff;
          key[i++] = (num >> 16) & 0xff;
          key[i++] = gen & 0xff;
          key[i++] = (gen >> 8) & 0xff;
          if (isAes) {
            key[i++] = 0x73;
            key[i++] = 0x41;
            key[i++] = 0x6c;
            key[i++] = 0x54;
          }
          var hash = calculateMD5(key, 0, i);
          return hash.subarray(0, Math.min(encryptionKey.length + 5, 16));
        }
    
        function buildCipherConstructor(cf, name, num, gen, key) {
          var cryptFilter = cf.get(name.name);
          var cfm;
          if (cryptFilter !== null && cryptFilter !== undefined) {
            cfm = cryptFilter.get("CFM");
          }
          if (!cfm || cfm.name === "None") {
            return function cipherTransformFactoryBuildCipherConstructorNone() {
              return new NullCipher();
            };
          }
          if ("V2" === cfm.name) {
            return function cipherTransformFactoryBuildCipherConstructorV2() {
              return new ARCFourCipher(buildObjectKey(num, gen, key, false));
            };
          }
          if ("AESV2" === cfm.name) {
            return function cipherTransformFactoryBuildCipherConstructorAESV2() {
              return new AES128Cipher(buildObjectKey(num, gen, key, true));
            };
          }
          if ("AESV3" === cfm.name) {
            return function cipherTransformFactoryBuildCipherConstructorAESV3() {
              return new AES256Cipher(key);
            };
          }
          error("Unknown crypto method");
        }
    
        CipherTransformFactory.prototype = {
          createCipherTransform: function CipherTransformFactory_createCipherTransform(
            num,
            gen
          ) {
            if (this.algorithm === 4 || this.algorithm === 5) {
              return new CipherTransform(
                buildCipherConstructor(
                  this.cf,
                  this.stmf,
                  num,
                  gen,
                  this.encryptionKey
                ),
                buildCipherConstructor(
                  this.cf,
                  this.strf,
                  num,
                  gen,
                  this.encryptionKey
                )
              );
            }
            // algorithms 1 and 2
            var key = buildObjectKey(num, gen, this.encryptionKey, false);
            var cipherConstructor = function buildCipherCipherConstructor() {
              return new ARCFourCipher(key);
            };
            return new CipherTransform(cipherConstructor, cipherConstructor);
          }
        };
    
        return CipherTransformFactory;
      })();
    
      exports.AES128Cipher = AES128Cipher;
      exports.AES256Cipher = AES256Cipher;
      exports.ARCFourCipher = ARCFourCipher;
      exports.CipherTransformFactory = CipherTransformFactory;
      exports.PDF17 = PDF17;
      exports.PDF20 = PDF20;
      exports.calculateMD5 = calculateMD5;
      exports.calculateSHA256 = calculateSHA256;
      exports.calculateSHA384 = calculateSHA384;
      exports.calculateSHA512 = calculateSHA512;
    });
    
    /* Copyright 2012 Mozilla Foundation
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    ("use strict");
    
    (function(root, factory) {
      //if (typeof define === 'function' && define.amd) {
      //  define('pdfjs/core/obj', ['exports', 'pdfjs/shared/util',
      //    'pdfjs/core/primitives', 'pdfjs/core/crypto', 'pdfjs/core/parser',
      //    'pdfjs/core/chunked_stream'], factory);
      // } else if (typeof exports !== 'undefined') {
      //   factory(exports, require('../shared/util.js'), require('./primitives.js'),
      //     require('./crypto.js'), require('./parser.js'),
      //     require('./chunked_stream.js'));
      //} else {
      factory(
        (root.pdfjsCoreObj = {}),
        root.pdfjsSharedUtil,
        root.pdfjsCorePrimitives,
        root.pdfjsCoreCrypto,
        root.pdfjsCoreParser,
        root.pdfjsCoreChunkedStream
      );
      //}
    })(window, function(
      exports,
      sharedUtil,
      corePrimitives,
      coreCrypto,
      coreParser,
      coreChunkedStream
    ) {
      var InvalidPDFException = sharedUtil.InvalidPDFException;
      var MissingDataException = sharedUtil.MissingDataException;
      var XRefParseException = sharedUtil.XRefParseException;
      var assert = sharedUtil.assert;
      var bytesToString = sharedUtil.bytesToString;
      var createPromiseCapability = sharedUtil.createPromiseCapability;
      var error = sharedUtil.error;
      var info = sharedUtil.info;
      var isArray = sharedUtil.isArray;
      var isInt = sharedUtil.isInt;
      var isString = sharedUtil.isString;
      var shadow = sharedUtil.shadow;
      var stringToPDFString = sharedUtil.stringToPDFString;
      var stringToUTF8String = sharedUtil.stringToUTF8String;
      var warn = sharedUtil.warn;
      var Ref = corePrimitives.Ref;
      var RefSet = corePrimitives.RefSet;
      var RefSetCache = corePrimitives.RefSetCache;
      var isName = corePrimitives.isName;
      var isCmd = corePrimitives.isCmd;
      var isDict = corePrimitives.isDict;
      var isRef = corePrimitives.isRef;
      var isStream = corePrimitives.isStream;
      var CipherTransformFactory = coreCrypto.CipherTransformFactory;
      var Lexer = coreParser.Lexer;
      var Parser = coreParser.Parser;
      var ChunkedStream = coreChunkedStream.ChunkedStream;
    
      var Catalog = (function CatalogClosure() {
        function Catalog(pdfManager, xref, pageFactory) {
          this.pdfManager = pdfManager;
          this.xref = xref;
          this.catDict = xref.getCatalogObj();
          this.fontCache = new RefSetCache();
          assert(isDict(this.catDict), "catalog object is not a dictionary");
    
          // TODO refactor to move getPage() to the PDFDocument.
          this.pageFactory = pageFactory;
          this.pagePromises = [];
        }
    
        Catalog.prototype = {
          get metadata() {
            var streamRef = this.catDict.getRaw("Metadata");
            if (!isRef(streamRef)) {
              return shadow(this, "metadata", null);
            }
    
            var encryptMetadata = !this.xref.encrypt
              ? false
              : this.xref.encrypt.encryptMetadata;
    
            var stream = this.xref.fetch(streamRef, !encryptMetadata);
            var metadata;
            if (stream && isDict(stream.dict)) {
              var type = stream.dict.get("Type");
              var subtype = stream.dict.get("Subtype");
    
              if (
                isName(type) &&
                isName(subtype) &&
                type.name === "Metadata" &&
                subtype.name === "XML"
              ) {
                // XXX: This should examine the charset the XML document defines,
                // however since there are currently no real means to decode
                // arbitrary charsets, let's just hope that the author of the PDF
                // was reasonable enough to stick with the XML default charset,
                // which is UTF-8.
                try {
                  metadata = stringToUTF8String(bytesToString(stream.getBytes()));
                } catch (e) {
                  info("Skipping invalid metadata.");
                }
              }
            }
    
            return shadow(this, "metadata", metadata);
          },
          get toplevelPagesDict() {
            var pagesObj = this.catDict.get("Pages");
            assert(isDict(pagesObj), "invalid top-level pages dictionary");
            // shadow the prototype getter
            return shadow(this, "toplevelPagesDict", pagesObj);
          },
          get documentOutline() {
            var obj = null;
            try {
              obj = this.readDocumentOutline();
            } catch (ex) {
              if (ex instanceof MissingDataException) {
                throw ex;
              }
              warn("Unable to read document outline");
            }
            return shadow(this, "documentOutline", obj);
          },
          readDocumentOutline: function Catalog_readDocumentOutline() {
            var xref = this.xref;
            var obj = this.catDict.get("Outlines");
            var root = { items: [] };
            if (isDict(obj)) {
              obj = obj.getRaw("First");
              var processed = new RefSet();
              if (isRef(obj)) {
                var queue = [{ obj: obj, parent: root }];
                // to avoid recursion keeping track of the items
                // in the processed dictionary
                processed.put(obj);
                while (queue.length > 0) {
                  var i = queue.shift();
                  var outlineDict = xref.fetchIfRef(i.obj);
                  if (outlineDict === null) {
                    continue;
                  }
                  if (!outlineDict.has("Title")) {
                    error("Invalid outline item");
                  }
                  var dest = outlineDict.get("A");
                  if (dest) {
                    dest = dest.get("D");
                  } else if (outlineDict.has("Dest")) {
                    dest = outlineDict.getRaw("Dest");
                    if (isName(dest)) {
                      dest = dest.name;
                    }
                  }
                  var title = outlineDict.get("Title");
                  var outlineItem = {
                    dest: dest,
                    title: stringToPDFString(title),
                    color: outlineDict.get("C") || [0, 0, 0],
                    count: outlineDict.get("Count"),
                    bold: !!(outlineDict.get("F") & 2),
                    italic: !!(outlineDict.get("F") & 1),
                    items: []
                  };
                  i.parent.items.push(outlineItem);
                  obj = outlineDict.getRaw("First");
                  if (isRef(obj) && !processed.has(obj)) {
                    queue.push({ obj: obj, parent: outlineItem });
                    processed.put(obj);
                  }
                  obj = outlineDict.getRaw("Next");
                  if (isRef(obj) && !processed.has(obj)) {
                    queue.push({ obj: obj, parent: i.parent });
                    processed.put(obj);
                  }
                }
              }
            }
            return root.items.length > 0 ? root.items : null;
          },
          get numPages() {
            var obj = this.toplevelPagesDict.get("Count");
            assert(
              isInt(obj),
              "page count in top level pages object is not an integer"
            );
            // shadow the prototype getter
            return shadow(this, "num", obj);
          },
          get destinations() {
            function fetchDestination(dest) {
              return isDict(dest) ? dest.get("D") : dest;
            }
    
            var xref = this.xref;
            var dests = {},
              nameTreeRef,
              nameDictionaryRef;
            var obj = this.catDict.get("Names");
            if (obj && obj.has("Dests")) {
              nameTreeRef = obj.getRaw("Dests");
            } else if (this.catDict.has("Dests")) {
              nameDictionaryRef = this.catDict.get("Dests");
            }
    
            if (nameDictionaryRef) {
              // reading simple destination dictionary
              obj = nameDictionaryRef;
              obj.forEach(function catalogForEach(key, value) {
                if (!value) {
                  return;
                }
                dests[key] = fetchDestination(value);
              });
            }
            if (nameTreeRef) {
              var nameTree = new NameTree(nameTreeRef, xref);
              var names = nameTree.getAll();
              for (var name in names) {
                if (!names.hasOwnProperty(name)) {
                  continue;
                }
                dests[name] = fetchDestination(names[name]);
              }
            }
            return shadow(this, "destinations", dests);
          },
          getDestination: function Catalog_getDestination(destinationId) {
            function fetchDestination(dest) {
              return isDict(dest) ? dest.get("D") : dest;
            }
    
            var xref = this.xref;
            var dest = null,
              nameTreeRef,
              nameDictionaryRef;
            var obj = this.catDict.get("Names");
            if (obj && obj.has("Dests")) {
              nameTreeRef = obj.getRaw("Dests");
            } else if (this.catDict.has("Dests")) {
              nameDictionaryRef = this.catDict.get("Dests");
            }
    
            if (nameDictionaryRef) {
              // Simple destination dictionary.
              var value = nameDictionaryRef.get(destinationId);
              if (value) {
                dest = fetchDestination(value);
              }
            }
            if (nameTreeRef) {
              var nameTree = new NameTree(nameTreeRef, xref);
              dest = fetchDestination(nameTree.get(destinationId));
            }
            return dest;
          },
          get attachments() {
            var xref = this.xref;
            var attachments = null,
              nameTreeRef;
            var obj = this.catDict.get("Names");
            if (obj) {
              nameTreeRef = obj.getRaw("EmbeddedFiles");
            }
    
            if (nameTreeRef) {
              var nameTree = new NameTree(nameTreeRef, xref);
              var names = nameTree.getAll();
              for (var name in names) {
                if (!names.hasOwnProperty(name)) {
                  continue;
                }
                var fs = new FileSpec(names[name], xref);
                if (!attachments) {
                  attachments = {};
                }
                attachments[stringToPDFString(name)] = fs.serializable;
              }
            }
            return shadow(this, "attachments", attachments);
          },
          get javaScript() {
            var xref = this.xref;
            var obj = this.catDict.get("Names");
    
            var javaScript = [];
            function appendIfJavaScriptDict(jsDict) {
              var type = jsDict.get("S");
              if (!isName(type) || type.name !== "JavaScript") {
                return;
              }
              var js = jsDict.get("JS");
              if (isStream(js)) {
                js = bytesToString(js.getBytes());
              } else if (!isString(js)) {
                return;
              }
              javaScript.push(stringToPDFString(js));
            }
            if (obj && obj.has("JavaScript")) {
              var nameTree = new NameTree(obj.getRaw("JavaScript"), xref);
              var names = nameTree.getAll();