From 7266a89f5a70238c5f705c55c1a0715478fd6d5f Mon Sep 17 00:00:00 2001 From: LabyStudio Date: Sun, 19 Jun 2022 14:25:40 +0200 Subject: [PATCH] implement packet compression, implement player controller, implement join server authentication, add cobblestone, implement chunk provider, implement block position, implement session, implement movement, chunk, chat and block update packets, version 1.1.5 --- README.md | 16 +- index.html | 11 +- libraries/aes.js | 1430 +-- libraries/asn1.js | 237 +- libraries/bigint-mod-arith.js | 160 + libraries/chat.js | 132 +- libraries/long.js | 2735 +++--- libraries/modpow.js | 257 - libraries/pako.es5.min.js | 8027 +++++++++++++++++ libraries/sha1.min.js | 9 + src/js/Start.js | 7 +- src/js/net/minecraft/client/Minecraft.js | 46 +- src/js/net/minecraft/client/entity/Entity.js | 10 +- .../client/entity/PlayerEntityMultiplayer.js | 69 + .../client/gui/overlay/IngameOverlay.js | 2 +- .../minecraft/client/gui/screens/GuiChat.js | 2 +- .../client/gui/screens/GuiConnecting.js | 4 +- .../client/gui/screens/GuiCreateWorld.js | 11 +- .../client/network/NetworkManager.js | 181 +- .../client/network/PacketRegistry.js | 55 +- .../minecraft/client/network/ProtocolState.js | 38 +- .../network/controller/PlayerController.js | 16 + .../controller/PlayerControllerMultiplayer.js | 24 + .../network/handler/NetworkLoginHandler.js | 27 +- .../network/handler/NetworkPlayHandler.js | 76 + .../network/{ => handler}/PacketHandler.js | 8 - .../handshake/client/HandshakePacket.js | 2 +- .../login/server/EnableCompressionPacket.js | 26 + .../login/server/LoginDisconnectPacket.js | 2 +- .../packet/login/server/LoginSuccessPacket.js | 27 + .../packet/play/client/ClientChatPacket.js | 14 + .../play/client/ClientKeepAlivePacket.js | 18 + .../play/client/ClientPlayerMovementPacket.js | 35 + .../play/client/ClientPlayerPositionPacket.js | 14 + .../ClientPlayerPositionRotationPacket.js | 18 + .../play/client/ClientPlayerRotationPacket.js | 13 + .../play/server/ServerBlockChangePacket.js | 27 + .../packet/play/server/ServerChatPacket.js | 31 + .../play/server/ServerChunkDataPacket.js | 46 + .../play/server/ServerJoinGamePacket.js | 37 + .../play/server/ServerKeepAlivePacket.js | 22 + .../play/server/ServerMultiChunkDataPacket.js | 38 + .../network/pinger/NetworkStatusHandler.js | 2 +- .../client/network/util/Authentication.js | 50 + .../minecraft/client/network/util/ByteBuf.js | 92 +- .../client/network/util/CryptManager.js | 18 +- .../minecraft/client/render/WorldRenderer.js | 6 +- .../render/entity/EntityRenderManager.js | 4 +- src/js/net/minecraft/client/world/Chunk.js | 32 +- .../minecraft/client/world/ChunkSection.js | 2 +- src/js/net/minecraft/client/world/World.js | 92 +- .../net/minecraft/client/world/WorldClient.js | 12 + .../net/minecraft/client/world/block/Block.js | 10 +- .../client/world/block/BlockRegistry.js | 2 + .../world/block/type/BlockCobblestone.js | 9 + .../client/world/generator/Generator.js | 10 + .../client/world/generator/WorldGenerator.js | 2 - .../client/world/provider/ChunkProvider.js | 61 + .../world/provider/ChunkProviderClient.js | 20 + .../world/provider/ChunkProviderGenerate.js | 67 + src/js/net/minecraft/util/BlockPosition.js | 59 + src/js/net/minecraft/util/ChatLine.js | 5 + src/js/net/minecraft/util/GameProfile.js | 12 + .../net/minecraft/util/MetadataChunkBlock.js | 2 +- src/js/net/minecraft/util/Session.js | 16 + src/js/net/minecraft/util/UUID.js | 47 +- src/resources/terrain/terrain.png | Bin 8621 -> 9063 bytes 67 files changed, 11831 insertions(+), 2761 deletions(-) create mode 100644 libraries/bigint-mod-arith.js delete mode 100644 libraries/modpow.js create mode 100644 libraries/pako.es5.min.js create mode 100644 libraries/sha1.min.js create mode 100644 src/js/net/minecraft/client/entity/PlayerEntityMultiplayer.js create mode 100644 src/js/net/minecraft/client/network/controller/PlayerController.js create mode 100644 src/js/net/minecraft/client/network/controller/PlayerControllerMultiplayer.js create mode 100644 src/js/net/minecraft/client/network/handler/NetworkPlayHandler.js rename src/js/net/minecraft/client/network/{ => handler}/PacketHandler.js (50%) create mode 100644 src/js/net/minecraft/client/network/packet/login/server/EnableCompressionPacket.js create mode 100644 src/js/net/minecraft/client/network/packet/login/server/LoginSuccessPacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/client/ClientChatPacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/client/ClientKeepAlivePacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/client/ClientPlayerMovementPacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/client/ClientPlayerPositionPacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/client/ClientPlayerPositionRotationPacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/client/ClientPlayerRotationPacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/server/ServerBlockChangePacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/server/ServerChatPacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/server/ServerChunkDataPacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/server/ServerJoinGamePacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/server/ServerKeepAlivePacket.js create mode 100644 src/js/net/minecraft/client/network/packet/play/server/ServerMultiChunkDataPacket.js create mode 100644 src/js/net/minecraft/client/network/util/Authentication.js create mode 100644 src/js/net/minecraft/client/world/WorldClient.js create mode 100644 src/js/net/minecraft/client/world/block/type/BlockCobblestone.js create mode 100644 src/js/net/minecraft/client/world/provider/ChunkProvider.js create mode 100644 src/js/net/minecraft/client/world/provider/ChunkProviderClient.js create mode 100644 src/js/net/minecraft/client/world/provider/ChunkProviderGenerate.js create mode 100644 src/js/net/minecraft/util/BlockPosition.js create mode 100644 src/js/net/minecraft/util/Session.js diff --git a/README.md b/README.md index a55a664..b4a78cf 100644 --- a/README.md +++ b/README.md @@ -75,12 +75,20 @@ Click [here](https://labystudio.de/page/minecraft/) for a demo! - Debug - Multiplayer - Networking - - Sub-Protocols - - Handshake - - Status - - Login - RSA Encryption - AES Encryption + - Compression + - Splitting + - Sub-Protocols + - Handshake + - Status + - Login + - Play + - Packets + - Chunk Packets + - Movement Packets + - Block Update Packets + - Chat Packets - Commands - /help - /time diff --git a/index.html b/index.html index 1ae167f..23cdedd 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,8 @@ Minecraft - + @@ -16,6 +17,14 @@ + + + + + + + + \ No newline at end of file diff --git a/libraries/aes.js b/libraries/aes.js index ff703b2..735f233 100644 --- a/libraries/aes.js +++ b/libraries/aes.js @@ -1,780 +1,816 @@ /*! MIT License. Copyright 2015-2018 Richard Moore . See LICENSE.txt. */ // https://github.com/ricmoo/aes-js -function checkInt(value) { - return (parseInt(value) === value); -} +(function (root) { + "use strict"; -function checkInts(arrayish) { - if (!checkInt(arrayish.length)) { - return false; + function checkInt(value) { + return (parseInt(value) === value); } - for (var i = 0; i < arrayish.length; i++) { - if (!checkInt(arrayish[i]) || arrayish[i] < 0 || arrayish[i] > 255) { + function checkInts(arrayish) { + if (!checkInt(arrayish.length)) { return false; } - } - return true; -} - -function coerceArray(arg, copy) { - - // ArrayBuffer view - if (arg.buffer && ArrayBuffer.isView(arg) && arg.name === 'Uint8Array') { - - if (copy) { - if (arg.slice) { - arg = arg.slice(); - } else { - arg = Array.prototype.slice.call(arg); + for (var i = 0; i < arrayish.length; i++) { + if (!checkInt(arrayish[i]) || arrayish[i] < 0 || arrayish[i] > 255) { + return false; } } - return arg; + return true; } - // It's an array; check it is a valid representation of a byte - if (Array.isArray(arg)) { - if (!checkInts(arg)) { - throw new Error('Array contains invalid value: ' + arg); + function coerceArray(arg, copy) { + + // ArrayBuffer view + if (arg.buffer && arg.name === 'Uint8Array') { + + if (copy) { + if (arg.slice) { + arg = arg.slice(); + } else { + arg = Array.prototype.slice.call(arg); + } + } + + return arg; } - return new Uint8Array(arg); - } + // It's an array; check it is a valid representation of a byte + if (Array.isArray(arg)) { + if (!checkInts(arg)) { + throw new Error('Array contains invalid value: ' + arg); + } - // Something else, but behaves like an array (maybe a Buffer? Arguments?) - if (checkInt(arg.length) && checkInts(arg)) { - return new Uint8Array(arg); - } - - throw new Error('unsupported array-like object'); -} - -function createArray(length) { - return new Uint8Array(length); -} - -function copyArray(sourceArray, targetArray, targetStart, sourceStart, sourceEnd) { - if (sourceStart != null || sourceEnd != null) { - if (sourceArray.slice) { - sourceArray = sourceArray.slice(sourceStart, sourceEnd); - } else { - sourceArray = Array.prototype.slice.call(sourceArray, sourceStart, sourceEnd); + return new Uint8Array(arg); } + + // Something else, but behaves like an array (maybe a Buffer? Arguments?) + if (checkInt(arg.length) && checkInts(arg)) { + return new Uint8Array(arg); + } + + throw new Error('unsupported array-like object'); } - targetArray.set(sourceArray, targetStart); -} + function createArray(length) { + return new Uint8Array(length); + } -var convertUtf8 = (function () { - function toBytes(text) { - var result = [], i = 0; - text = encodeURI(text); - while (i < text.length) { - var c = text.charCodeAt(i++); - - // if it is a % sign, encode the following 2 bytes as a hex value - if (c === 37) { - result.push(parseInt(text.substr(i, 2), 16)) - i += 2; - - // otherwise, just the actual byte + function copyArray(sourceArray, targetArray, targetStart, sourceStart, sourceEnd) { + if (sourceStart != null || sourceEnd != null) { + if (sourceArray.slice) { + sourceArray = sourceArray.slice(sourceStart, sourceEnd); } else { - result.push(c) + sourceArray = Array.prototype.slice.call(sourceArray, sourceStart, sourceEnd); } } - - return coerceArray(result); + targetArray.set(sourceArray, targetStart); } - function fromBytes(bytes) { - var result = [], i = 0; - while (i < bytes.length) { - var c = bytes[i]; + var convertUtf8 = (function () { + function toBytes(text) { + var result = [], i = 0; + text = encodeURI(text); + while (i < text.length) { + var c = text.charCodeAt(i++); - if (c < 128) { - result.push(String.fromCharCode(c)); - i++; - } else if (c > 191 && c < 224) { - result.push(String.fromCharCode(((c & 0x1f) << 6) | (bytes[i + 1] & 0x3f))); - i += 2; - } else { - result.push(String.fromCharCode(((c & 0x0f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f))); - i += 3; + // if it is a % sign, encode the following 2 bytes as a hex value + if (c === 37) { + result.push(parseInt(text.substr(i, 2), 16)) + i += 2; + + // otherwise, just the actual byte + } else { + result.push(c) + } } + + return coerceArray(result); } - return result.join(''); - } + function fromBytes(bytes) { + var result = [], i = 0; - return { - toBytes: toBytes, - fromBytes: fromBytes, - } -})(); + while (i < bytes.length) { + var c = bytes[i]; -var convertHex = (function () { - function toBytes(text) { + if (c < 128) { + result.push(String.fromCharCode(c)); + i++; + } else if (c > 191 && c < 224) { + result.push(String.fromCharCode(((c & 0x1f) << 6) | (bytes[i + 1] & 0x3f))); + i += 2; + } else { + result.push(String.fromCharCode(((c & 0x0f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f))); + i += 3; + } + } + + return result.join(''); + } + + return { + toBytes: toBytes, + fromBytes: fromBytes, + } + })(); + + var convertHex = (function () { + function toBytes(text) { + var result = []; + for (var i = 0; i < text.length; i += 2) { + result.push(parseInt(text.substr(i, 2), 16)); + } + + return result; + } + + // http://ixti.net/development/javascript/2011/11/11/base64-encodedecode-of-utf8-in-browser-with-js.html + var Hex = '0123456789abcdef'; + + function fromBytes(bytes) { + var result = []; + for (var i = 0; i < bytes.length; i++) { + var v = bytes[i]; + result.push(Hex[(v & 0xf0) >> 4] + Hex[v & 0x0f]); + } + return result.join(''); + } + + return { + toBytes: toBytes, + fromBytes: fromBytes, + } + })(); + + + // Number of rounds by keysize + var numberOfRounds = {16: 10, 24: 12, 32: 14} + + // Round constant words + var rcon = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91]; + + // S-box and Inverse S-box (S is for Substitution) + var S = [0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16]; + var Si = [0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d]; + + // Transformations for encryption + var T1 = [0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a, 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b, 0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b, 0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a, 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f, 0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108, 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f, 0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e, 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5, 0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d, 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f, 0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e, 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb, 0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce, 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497, 0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c, 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed, 0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b, 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a, 0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16, 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594, 0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81, 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3, 0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a, 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504, 0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163, 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d, 0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f, 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739, 0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47, 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395, 0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f, 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883, 0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c, 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76, 0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e, 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4, 0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6, 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b, 0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7, 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0, 0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25, 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818, 0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72, 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651, 0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21, 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85, 0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa, 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12, 0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0, 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9, 0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133, 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7, 0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920, 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a, 0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8, 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a]; + var T2 = [0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5, 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676, 0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0, 0xec41adad, 0x67b3d4d4, 0xfd5fa2a2, 0xea45afaf, 0xbf239c9c, 0xf753a4a4, 0x96e47272, 0x5b9bc0c0, 0xc275b7b7, 0x1ce1fdfd, 0xae3d9393, 0x6a4c2626, 0x5a6c3636, 0x417e3f3f, 0x02f5f7f7, 0x4f83cccc, 0x5c683434, 0xf451a5a5, 0x34d1e5e5, 0x08f9f1f1, 0x93e27171, 0x73abd8d8, 0x53623131, 0x3f2a1515, 0x0c080404, 0x5295c7c7, 0x65462323, 0x5e9dc3c3, 0x28301818, 0xa1379696, 0x0f0a0505, 0xb52f9a9a, 0x090e0707, 0x36241212, 0x9b1b8080, 0x3ddfe2e2, 0x26cdebeb, 0x694e2727, 0xcd7fb2b2, 0x9fea7575, 0x1b120909, 0x9e1d8383, 0x74582c2c, 0x2e341a1a, 0x2d361b1b, 0xb2dc6e6e, 0xeeb45a5a, 0xfb5ba0a0, 0xf6a45252, 0x4d763b3b, 0x61b7d6d6, 0xce7db3b3, 0x7b522929, 0x3edde3e3, 0x715e2f2f, 0x97138484, 0xf5a65353, 0x68b9d1d1, 0x00000000, 0x2cc1eded, 0x60402020, 0x1fe3fcfc, 0xc879b1b1, 0xedb65b5b, 0xbed46a6a, 0x468dcbcb, 0xd967bebe, 0x4b723939, 0xde944a4a, 0xd4984c4c, 0xe8b05858, 0x4a85cfcf, 0x6bbbd0d0, 0x2ac5efef, 0xe54faaaa, 0x16edfbfb, 0xc5864343, 0xd79a4d4d, 0x55663333, 0x94118585, 0xcf8a4545, 0x10e9f9f9, 0x06040202, 0x81fe7f7f, 0xf0a05050, 0x44783c3c, 0xba259f9f, 0xe34ba8a8, 0xf3a25151, 0xfe5da3a3, 0xc0804040, 0x8a058f8f, 0xad3f9292, 0xbc219d9d, 0x48703838, 0x04f1f5f5, 0xdf63bcbc, 0xc177b6b6, 0x75afdada, 0x63422121, 0x30201010, 0x1ae5ffff, 0x0efdf3f3, 0x6dbfd2d2, 0x4c81cdcd, 0x14180c0c, 0x35261313, 0x2fc3ecec, 0xe1be5f5f, 0xa2359797, 0xcc884444, 0x392e1717, 0x5793c4c4, 0xf255a7a7, 0x82fc7e7e, 0x477a3d3d, 0xacc86464, 0xe7ba5d5d, 0x2b321919, 0x95e67373, 0xa0c06060, 0x98198181, 0xd19e4f4f, 0x7fa3dcdc, 0x66442222, 0x7e542a2a, 0xab3b9090, 0x830b8888, 0xca8c4646, 0x29c7eeee, 0xd36bb8b8, 0x3c281414, 0x79a7dede, 0xe2bc5e5e, 0x1d160b0b, 0x76addbdb, 0x3bdbe0e0, 0x56643232, 0x4e743a3a, 0x1e140a0a, 0xdb924949, 0x0a0c0606, 0x6c482424, 0xe4b85c5c, 0x5d9fc2c2, 0x6ebdd3d3, 0xef43acac, 0xa6c46262, 0xa8399191, 0xa4319595, 0x37d3e4e4, 0x8bf27979, 0x32d5e7e7, 0x438bc8c8, 0x596e3737, 0xb7da6d6d, 0x8c018d8d, 0x64b1d5d5, 0xd29c4e4e, 0xe049a9a9, 0xb4d86c6c, 0xfaac5656, 0x07f3f4f4, 0x25cfeaea, 0xafca6565, 0x8ef47a7a, 0xe947aeae, 0x18100808, 0xd56fbaba, 0x88f07878, 0x6f4a2525, 0x725c2e2e, 0x24381c1c, 0xf157a6a6, 0xc773b4b4, 0x5197c6c6, 0x23cbe8e8, 0x7ca1dddd, 0x9ce87474, 0x213e1f1f, 0xdd964b4b, 0xdc61bdbd, 0x860d8b8b, 0x850f8a8a, 0x90e07070, 0x427c3e3e, 0xc471b5b5, 0xaacc6666, 0xd8904848, 0x05060303, 0x01f7f6f6, 0x121c0e0e, 0xa3c26161, 0x5f6a3535, 0xf9ae5757, 0xd069b9b9, 0x91178686, 0x5899c1c1, 0x273a1d1d, 0xb9279e9e, 0x38d9e1e1, 0x13ebf8f8, 0xb32b9898, 0x33221111, 0xbbd26969, 0x70a9d9d9, 0x89078e8e, 0xa7339494, 0xb62d9b9b, 0x223c1e1e, 0x92158787, 0x20c9e9e9, 0x4987cece, 0xffaa5555, 0x78502828, 0x7aa5dfdf, 0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d, 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868, 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616]; + var T3 = [0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5, 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76, 0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0, 0xadec41ad, 0xd467b3d4, 0xa2fd5fa2, 0xafea45af, 0x9cbf239c, 0xa4f753a4, 0x7296e472, 0xc05b9bc0, 0xb7c275b7, 0xfd1ce1fd, 0x93ae3d93, 0x266a4c26, 0x365a6c36, 0x3f417e3f, 0xf702f5f7, 0xcc4f83cc, 0x345c6834, 0xa5f451a5, 0xe534d1e5, 0xf108f9f1, 0x7193e271, 0xd873abd8, 0x31536231, 0x153f2a15, 0x040c0804, 0xc75295c7, 0x23654623, 0xc35e9dc3, 0x18283018, 0x96a13796, 0x050f0a05, 0x9ab52f9a, 0x07090e07, 0x12362412, 0x809b1b80, 0xe23ddfe2, 0xeb26cdeb, 0x27694e27, 0xb2cd7fb2, 0x759fea75, 0x091b1209, 0x839e1d83, 0x2c74582c, 0x1a2e341a, 0x1b2d361b, 0x6eb2dc6e, 0x5aeeb45a, 0xa0fb5ba0, 0x52f6a452, 0x3b4d763b, 0xd661b7d6, 0xb3ce7db3, 0x297b5229, 0xe33edde3, 0x2f715e2f, 0x84971384, 0x53f5a653, 0xd168b9d1, 0x00000000, 0xed2cc1ed, 0x20604020, 0xfc1fe3fc, 0xb1c879b1, 0x5bedb65b, 0x6abed46a, 0xcb468dcb, 0xbed967be, 0x394b7239, 0x4ade944a, 0x4cd4984c, 0x58e8b058, 0xcf4a85cf, 0xd06bbbd0, 0xef2ac5ef, 0xaae54faa, 0xfb16edfb, 0x43c58643, 0x4dd79a4d, 0x33556633, 0x85941185, 0x45cf8a45, 0xf910e9f9, 0x02060402, 0x7f81fe7f, 0x50f0a050, 0x3c44783c, 0x9fba259f, 0xa8e34ba8, 0x51f3a251, 0xa3fe5da3, 0x40c08040, 0x8f8a058f, 0x92ad3f92, 0x9dbc219d, 0x38487038, 0xf504f1f5, 0xbcdf63bc, 0xb6c177b6, 0xda75afda, 0x21634221, 0x10302010, 0xff1ae5ff, 0xf30efdf3, 0xd26dbfd2, 0xcd4c81cd, 0x0c14180c, 0x13352613, 0xec2fc3ec, 0x5fe1be5f, 0x97a23597, 0x44cc8844, 0x17392e17, 0xc45793c4, 0xa7f255a7, 0x7e82fc7e, 0x3d477a3d, 0x64acc864, 0x5de7ba5d, 0x192b3219, 0x7395e673, 0x60a0c060, 0x81981981, 0x4fd19e4f, 0xdc7fa3dc, 0x22664422, 0x2a7e542a, 0x90ab3b90, 0x88830b88, 0x46ca8c46, 0xee29c7ee, 0xb8d36bb8, 0x143c2814, 0xde79a7de, 0x5ee2bc5e, 0x0b1d160b, 0xdb76addb, 0xe03bdbe0, 0x32566432, 0x3a4e743a, 0x0a1e140a, 0x49db9249, 0x060a0c06, 0x246c4824, 0x5ce4b85c, 0xc25d9fc2, 0xd36ebdd3, 0xacef43ac, 0x62a6c462, 0x91a83991, 0x95a43195, 0xe437d3e4, 0x798bf279, 0xe732d5e7, 0xc8438bc8, 0x37596e37, 0x6db7da6d, 0x8d8c018d, 0xd564b1d5, 0x4ed29c4e, 0xa9e049a9, 0x6cb4d86c, 0x56faac56, 0xf407f3f4, 0xea25cfea, 0x65afca65, 0x7a8ef47a, 0xaee947ae, 0x08181008, 0xbad56fba, 0x7888f078, 0x256f4a25, 0x2e725c2e, 0x1c24381c, 0xa6f157a6, 0xb4c773b4, 0xc65197c6, 0xe823cbe8, 0xdd7ca1dd, 0x749ce874, 0x1f213e1f, 0x4bdd964b, 0xbddc61bd, 0x8b860d8b, 0x8a850f8a, 0x7090e070, 0x3e427c3e, 0xb5c471b5, 0x66aacc66, 0x48d89048, 0x03050603, 0xf601f7f6, 0x0e121c0e, 0x61a3c261, 0x355f6a35, 0x57f9ae57, 0xb9d069b9, 0x86911786, 0xc15899c1, 0x1d273a1d, 0x9eb9279e, 0xe138d9e1, 0xf813ebf8, 0x98b32b98, 0x11332211, 0x69bbd269, 0xd970a9d9, 0x8e89078e, 0x94a73394, 0x9bb62d9b, 0x1e223c1e, 0x87921587, 0xe920c9e9, 0xce4987ce, 0x55ffaa55, 0x28785028, 0xdf7aa5df, 0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d, 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068, 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16]; + var T4 = [0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491, 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec, 0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb, 0xadadec41, 0xd4d467b3, 0xa2a2fd5f, 0xafafea45, 0x9c9cbf23, 0xa4a4f753, 0x727296e4, 0xc0c05b9b, 0xb7b7c275, 0xfdfd1ce1, 0x9393ae3d, 0x26266a4c, 0x36365a6c, 0x3f3f417e, 0xf7f702f5, 0xcccc4f83, 0x34345c68, 0xa5a5f451, 0xe5e534d1, 0xf1f108f9, 0x717193e2, 0xd8d873ab, 0x31315362, 0x15153f2a, 0x04040c08, 0xc7c75295, 0x23236546, 0xc3c35e9d, 0x18182830, 0x9696a137, 0x05050f0a, 0x9a9ab52f, 0x0707090e, 0x12123624, 0x80809b1b, 0xe2e23ddf, 0xebeb26cd, 0x2727694e, 0xb2b2cd7f, 0x75759fea, 0x09091b12, 0x83839e1d, 0x2c2c7458, 0x1a1a2e34, 0x1b1b2d36, 0x6e6eb2dc, 0x5a5aeeb4, 0xa0a0fb5b, 0x5252f6a4, 0x3b3b4d76, 0xd6d661b7, 0xb3b3ce7d, 0x29297b52, 0xe3e33edd, 0x2f2f715e, 0x84849713, 0x5353f5a6, 0xd1d168b9, 0x00000000, 0xeded2cc1, 0x20206040, 0xfcfc1fe3, 0xb1b1c879, 0x5b5bedb6, 0x6a6abed4, 0xcbcb468d, 0xbebed967, 0x39394b72, 0x4a4ade94, 0x4c4cd498, 0x5858e8b0, 0xcfcf4a85, 0xd0d06bbb, 0xefef2ac5, 0xaaaae54f, 0xfbfb16ed, 0x4343c586, 0x4d4dd79a, 0x33335566, 0x85859411, 0x4545cf8a, 0xf9f910e9, 0x02020604, 0x7f7f81fe, 0x5050f0a0, 0x3c3c4478, 0x9f9fba25, 0xa8a8e34b, 0x5151f3a2, 0xa3a3fe5d, 0x4040c080, 0x8f8f8a05, 0x9292ad3f, 0x9d9dbc21, 0x38384870, 0xf5f504f1, 0xbcbcdf63, 0xb6b6c177, 0xdada75af, 0x21216342, 0x10103020, 0xffff1ae5, 0xf3f30efd, 0xd2d26dbf, 0xcdcd4c81, 0x0c0c1418, 0x13133526, 0xecec2fc3, 0x5f5fe1be, 0x9797a235, 0x4444cc88, 0x1717392e, 0xc4c45793, 0xa7a7f255, 0x7e7e82fc, 0x3d3d477a, 0x6464acc8, 0x5d5de7ba, 0x19192b32, 0x737395e6, 0x6060a0c0, 0x81819819, 0x4f4fd19e, 0xdcdc7fa3, 0x22226644, 0x2a2a7e54, 0x9090ab3b, 0x8888830b, 0x4646ca8c, 0xeeee29c7, 0xb8b8d36b, 0x14143c28, 0xdede79a7, 0x5e5ee2bc, 0x0b0b1d16, 0xdbdb76ad, 0xe0e03bdb, 0x32325664, 0x3a3a4e74, 0x0a0a1e14, 0x4949db92, 0x06060a0c, 0x24246c48, 0x5c5ce4b8, 0xc2c25d9f, 0xd3d36ebd, 0xacacef43, 0x6262a6c4, 0x9191a839, 0x9595a431, 0xe4e437d3, 0x79798bf2, 0xe7e732d5, 0xc8c8438b, 0x3737596e, 0x6d6db7da, 0x8d8d8c01, 0xd5d564b1, 0x4e4ed29c, 0xa9a9e049, 0x6c6cb4d8, 0x5656faac, 0xf4f407f3, 0xeaea25cf, 0x6565afca, 0x7a7a8ef4, 0xaeaee947, 0x08081810, 0xbabad56f, 0x787888f0, 0x25256f4a, 0x2e2e725c, 0x1c1c2438, 0xa6a6f157, 0xb4b4c773, 0xc6c65197, 0xe8e823cb, 0xdddd7ca1, 0x74749ce8, 0x1f1f213e, 0x4b4bdd96, 0xbdbddc61, 0x8b8b860d, 0x8a8a850f, 0x707090e0, 0x3e3e427c, 0xb5b5c471, 0x6666aacc, 0x4848d890, 0x03030506, 0xf6f601f7, 0x0e0e121c, 0x6161a3c2, 0x35355f6a, 0x5757f9ae, 0xb9b9d069, 0x86869117, 0xc1c15899, 0x1d1d273a, 0x9e9eb927, 0xe1e138d9, 0xf8f813eb, 0x9898b32b, 0x11113322, 0x6969bbd2, 0xd9d970a9, 0x8e8e8907, 0x9494a733, 0x9b9bb62d, 0x1e1e223c, 0x87879215, 0xe9e920c9, 0xcece4987, 0x5555ffaa, 0x28287850, 0xdfdf7aa5, 0x8c8c8f03, 0xa1a1f859, 0x89898009, 0x0d0d171a, 0xbfbfda65, 0xe6e631d7, 0x4242c684, 0x6868b8d0, 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c]; + + // Transformations for decryption + var T5 = [0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f, 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6, 0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844, 0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd, 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4, 0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45, 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94, 0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7, 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a, 0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5, 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c, 0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1, 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a, 0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75, 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051, 0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46, 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff, 0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77, 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb, 0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000, 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e, 0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927, 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a, 0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e, 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16, 0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d, 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8, 0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd, 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34, 0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163, 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120, 0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d, 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0, 0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422, 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef, 0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36, 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4, 0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662, 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5, 0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3, 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b, 0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8, 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6, 0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6, 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0, 0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815, 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f, 0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df, 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f, 0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e, 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713, 0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89, 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c, 0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf, 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86, 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541, 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742]; + var T6 = [0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303, 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3, 0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9, 0xe7038f5f, 0x9515929c, 0xebbf6d7a, 0xda955259, 0x2dd4be83, 0xd3587421, 0x2949e069, 0x448ec9c8, 0x6a75c289, 0x78f48e79, 0x6b99583e, 0xdd27b971, 0xb6bee14f, 0x17f088ad, 0x66c920ac, 0xb47dce3a, 0x1863df4a, 0x82e51a31, 0x60975133, 0x4562537f, 0xe0b16477, 0x84bb6bae, 0x1cfe81a0, 0x94f9082b, 0x58704868, 0x198f45fd, 0x8794de6c, 0xb7527bf8, 0x23ab73d3, 0xe2724b02, 0x57e31f8f, 0x2a6655ab, 0x07b2eb28, 0x032fb5c2, 0x9a86c57b, 0xa5d33708, 0xf2302887, 0xb223bfa5, 0xba02036a, 0x5ced1682, 0x2b8acf1c, 0x92a779b4, 0xf0f307f2, 0xa14e69e2, 0xcd65daf4, 0xd50605be, 0x1fd13462, 0x8ac4a6fe, 0x9d342e53, 0xa0a2f355, 0x32058ae1, 0x75a4f6eb, 0x390b83ec, 0xaa4060ef, 0x065e719f, 0x51bd6e10, 0xf93e218a, 0x3d96dd06, 0xaedd3e05, 0x464de6bd, 0xb591548d, 0x0571c45d, 0x6f0406d4, 0xff605015, 0x241998fb, 0x97d6bde9, 0xcc894043, 0x7767d99e, 0xbdb0e842, 0x8807898b, 0x38e7195b, 0xdb79c8ee, 0x47a17c0a, 0xe97c420f, 0xc9f8841e, 0x00000000, 0x83098086, 0x48322bed, 0xac1e1170, 0x4e6c5a72, 0xfbfd0eff, 0x560f8538, 0x1e3daed5, 0x27362d39, 0x640a0fd9, 0x21685ca6, 0xd19b5b54, 0x3a24362e, 0xb10c0a67, 0x0f9357e7, 0xd2b4ee96, 0x9e1b9b91, 0x4f80c0c5, 0xa261dc20, 0x695a774b, 0x161c121a, 0x0ae293ba, 0xe5c0a02a, 0x433c22e0, 0x1d121b17, 0x0b0e090d, 0xadf28bc7, 0xb92db6a8, 0xc8141ea9, 0x8557f119, 0x4caf7507, 0xbbee99dd, 0xfda37f60, 0x9ff70126, 0xbc5c72f5, 0xc544663b, 0x345bfb7e, 0x768b4329, 0xdccb23c6, 0x68b6edfc, 0x63b8e4f1, 0xcad731dc, 0x10426385, 0x40139722, 0x2084c611, 0x7d854a24, 0xf8d2bb3d, 0x11aef932, 0x6dc729a1, 0x4b1d9e2f, 0xf3dcb230, 0xec0d8652, 0xd077c1e3, 0x6c2bb316, 0x99a970b9, 0xfa119448, 0x2247e964, 0xc4a8fc8c, 0x1aa0f03f, 0xd8567d2c, 0xef223390, 0xc787494e, 0xc1d938d1, 0xfe8ccaa2, 0x3698d40b, 0xcfa6f581, 0x28a57ade, 0x26dab78e, 0xa43fadbf, 0xe42c3a9d, 0x0d507892, 0x9b6a5fcc, 0x62547e46, 0xc2f68d13, 0xe890d8b8, 0x5e2e39f7, 0xf582c3af, 0xbe9f5d80, 0x7c69d093, 0xa96fd52d, 0xb3cf2512, 0x3bc8ac99, 0xa710187d, 0x6ee89c63, 0x7bdb3bbb, 0x09cd2678, 0xf46e5918, 0x01ec9ab7, 0xa8834f9a, 0x65e6956e, 0x7eaaffe6, 0x0821bccf, 0xe6ef15e8, 0xd9bae79b, 0xce4a6f36, 0xd4ea9f09, 0xd629b07c, 0xaf31a4b2, 0x312a3f23, 0x30c6a594, 0xc035a266, 0x37744ebc, 0xa6fc82ca, 0xb0e090d0, 0x1533a7d8, 0x4af10498, 0xf741ecda, 0x0e7fcd50, 0x2f1791f6, 0x8d764dd6, 0x4d43efb0, 0x54ccaa4d, 0xdfe49604, 0xe39ed1b5, 0x1b4c6a88, 0xb8c12c1f, 0x7f466551, 0x049d5eea, 0x5d018c35, 0x73fa8774, 0x2efb0b41, 0x5ab3671d, 0x5292dbd2, 0x33e91056, 0x136dd647, 0x8c9ad761, 0x7a37a10c, 0x8e59f814, 0x89eb133c, 0xeecea927, 0x35b761c9, 0xede11ce5, 0x3c7a47b1, 0x599cd2df, 0x3f55f273, 0x791814ce, 0xbf73c737, 0xea53f7cd, 0x5b5ffdaa, 0x14df3d6f, 0x867844db, 0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340, 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95, 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857]; + var T7 = [0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3, 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562, 0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3, 0x5fe7038f, 0x9c951592, 0x7aebbf6d, 0x59da9552, 0x832dd4be, 0x21d35874, 0x692949e0, 0xc8448ec9, 0x896a75c2, 0x7978f48e, 0x3e6b9958, 0x71dd27b9, 0x4fb6bee1, 0xad17f088, 0xac66c920, 0x3ab47dce, 0x4a1863df, 0x3182e51a, 0x33609751, 0x7f456253, 0x77e0b164, 0xae84bb6b, 0xa01cfe81, 0x2b94f908, 0x68587048, 0xfd198f45, 0x6c8794de, 0xf8b7527b, 0xd323ab73, 0x02e2724b, 0x8f57e31f, 0xab2a6655, 0x2807b2eb, 0xc2032fb5, 0x7b9a86c5, 0x08a5d337, 0x87f23028, 0xa5b223bf, 0x6aba0203, 0x825ced16, 0x1c2b8acf, 0xb492a779, 0xf2f0f307, 0xe2a14e69, 0xf4cd65da, 0xbed50605, 0x621fd134, 0xfe8ac4a6, 0x539d342e, 0x55a0a2f3, 0xe132058a, 0xeb75a4f6, 0xec390b83, 0xefaa4060, 0x9f065e71, 0x1051bd6e, 0x8af93e21, 0x063d96dd, 0x05aedd3e, 0xbd464de6, 0x8db59154, 0x5d0571c4, 0xd46f0406, 0x15ff6050, 0xfb241998, 0xe997d6bd, 0x43cc8940, 0x9e7767d9, 0x42bdb0e8, 0x8b880789, 0x5b38e719, 0xeedb79c8, 0x0a47a17c, 0x0fe97c42, 0x1ec9f884, 0x00000000, 0x86830980, 0xed48322b, 0x70ac1e11, 0x724e6c5a, 0xfffbfd0e, 0x38560f85, 0xd51e3dae, 0x3927362d, 0xd9640a0f, 0xa621685c, 0x54d19b5b, 0x2e3a2436, 0x67b10c0a, 0xe70f9357, 0x96d2b4ee, 0x919e1b9b, 0xc54f80c0, 0x20a261dc, 0x4b695a77, 0x1a161c12, 0xba0ae293, 0x2ae5c0a0, 0xe0433c22, 0x171d121b, 0x0d0b0e09, 0xc7adf28b, 0xa8b92db6, 0xa9c8141e, 0x198557f1, 0x074caf75, 0xddbbee99, 0x60fda37f, 0x269ff701, 0xf5bc5c72, 0x3bc54466, 0x7e345bfb, 0x29768b43, 0xc6dccb23, 0xfc68b6ed, 0xf163b8e4, 0xdccad731, 0x85104263, 0x22401397, 0x112084c6, 0x247d854a, 0x3df8d2bb, 0x3211aef9, 0xa16dc729, 0x2f4b1d9e, 0x30f3dcb2, 0x52ec0d86, 0xe3d077c1, 0x166c2bb3, 0xb999a970, 0x48fa1194, 0x642247e9, 0x8cc4a8fc, 0x3f1aa0f0, 0x2cd8567d, 0x90ef2233, 0x4ec78749, 0xd1c1d938, 0xa2fe8cca, 0x0b3698d4, 0x81cfa6f5, 0xde28a57a, 0x8e26dab7, 0xbfa43fad, 0x9de42c3a, 0x920d5078, 0xcc9b6a5f, 0x4662547e, 0x13c2f68d, 0xb8e890d8, 0xf75e2e39, 0xaff582c3, 0x80be9f5d, 0x937c69d0, 0x2da96fd5, 0x12b3cf25, 0x993bc8ac, 0x7da71018, 0x636ee89c, 0xbb7bdb3b, 0x7809cd26, 0x18f46e59, 0xb701ec9a, 0x9aa8834f, 0x6e65e695, 0xe67eaaff, 0xcf0821bc, 0xe8e6ef15, 0x9bd9bae7, 0x36ce4a6f, 0x09d4ea9f, 0x7cd629b0, 0xb2af31a4, 0x23312a3f, 0x9430c6a5, 0x66c035a2, 0xbc37744e, 0xcaa6fc82, 0xd0b0e090, 0xd81533a7, 0x984af104, 0xdaf741ec, 0x500e7fcd, 0xf62f1791, 0xd68d764d, 0xb04d43ef, 0x4d54ccaa, 0x04dfe496, 0xb5e39ed1, 0x881b4c6a, 0x1fb8c12c, 0x517f4665, 0xea049d5e, 0x355d018c, 0x7473fa87, 0x412efb0b, 0x1d5ab367, 0xd25292db, 0x5633e910, 0x47136dd6, 0x618c9ad7, 0x0c7a37a1, 0x148e59f8, 0x3c89eb13, 0x27eecea9, 0xc935b761, 0xe5ede11c, 0xb13c7a47, 0xdf599cd2, 0x733f55f2, 0xce791814, 0x37bf73c7, 0xcdea53f7, 0xaa5b5ffd, 0x6f14df3d, 0xdb867844, 0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3, 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d, 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8]; + var T8 = [0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b, 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5, 0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b, 0x8f5fe703, 0x929c9515, 0x6d7aebbf, 0x5259da95, 0xbe832dd4, 0x7421d358, 0xe0692949, 0xc9c8448e, 0xc2896a75, 0x8e7978f4, 0x583e6b99, 0xb971dd27, 0xe14fb6be, 0x88ad17f0, 0x20ac66c9, 0xce3ab47d, 0xdf4a1863, 0x1a3182e5, 0x51336097, 0x537f4562, 0x6477e0b1, 0x6bae84bb, 0x81a01cfe, 0x082b94f9, 0x48685870, 0x45fd198f, 0xde6c8794, 0x7bf8b752, 0x73d323ab, 0x4b02e272, 0x1f8f57e3, 0x55ab2a66, 0xeb2807b2, 0xb5c2032f, 0xc57b9a86, 0x3708a5d3, 0x2887f230, 0xbfa5b223, 0x036aba02, 0x16825ced, 0xcf1c2b8a, 0x79b492a7, 0x07f2f0f3, 0x69e2a14e, 0xdaf4cd65, 0x05bed506, 0x34621fd1, 0xa6fe8ac4, 0x2e539d34, 0xf355a0a2, 0x8ae13205, 0xf6eb75a4, 0x83ec390b, 0x60efaa40, 0x719f065e, 0x6e1051bd, 0x218af93e, 0xdd063d96, 0x3e05aedd, 0xe6bd464d, 0x548db591, 0xc45d0571, 0x06d46f04, 0x5015ff60, 0x98fb2419, 0xbde997d6, 0x4043cc89, 0xd99e7767, 0xe842bdb0, 0x898b8807, 0x195b38e7, 0xc8eedb79, 0x7c0a47a1, 0x420fe97c, 0x841ec9f8, 0x00000000, 0x80868309, 0x2bed4832, 0x1170ac1e, 0x5a724e6c, 0x0efffbfd, 0x8538560f, 0xaed51e3d, 0x2d392736, 0x0fd9640a, 0x5ca62168, 0x5b54d19b, 0x362e3a24, 0x0a67b10c, 0x57e70f93, 0xee96d2b4, 0x9b919e1b, 0xc0c54f80, 0xdc20a261, 0x774b695a, 0x121a161c, 0x93ba0ae2, 0xa02ae5c0, 0x22e0433c, 0x1b171d12, 0x090d0b0e, 0x8bc7adf2, 0xb6a8b92d, 0x1ea9c814, 0xf1198557, 0x75074caf, 0x99ddbbee, 0x7f60fda3, 0x01269ff7, 0x72f5bc5c, 0x663bc544, 0xfb7e345b, 0x4329768b, 0x23c6dccb, 0xedfc68b6, 0xe4f163b8, 0x31dccad7, 0x63851042, 0x97224013, 0xc6112084, 0x4a247d85, 0xbb3df8d2, 0xf93211ae, 0x29a16dc7, 0x9e2f4b1d, 0xb230f3dc, 0x8652ec0d, 0xc1e3d077, 0xb3166c2b, 0x70b999a9, 0x9448fa11, 0xe9642247, 0xfc8cc4a8, 0xf03f1aa0, 0x7d2cd856, 0x3390ef22, 0x494ec787, 0x38d1c1d9, 0xcaa2fe8c, 0xd40b3698, 0xf581cfa6, 0x7ade28a5, 0xb78e26da, 0xadbfa43f, 0x3a9de42c, 0x78920d50, 0x5fcc9b6a, 0x7e466254, 0x8d13c2f6, 0xd8b8e890, 0x39f75e2e, 0xc3aff582, 0x5d80be9f, 0xd0937c69, 0xd52da96f, 0x2512b3cf, 0xac993bc8, 0x187da710, 0x9c636ee8, 0x3bbb7bdb, 0x267809cd, 0x5918f46e, 0x9ab701ec, 0x4f9aa883, 0x956e65e6, 0xffe67eaa, 0xbccf0821, 0x15e8e6ef, 0xe79bd9ba, 0x6f36ce4a, 0x9f09d4ea, 0xb07cd629, 0xa4b2af31, 0x3f23312a, 0xa59430c6, 0xa266c035, 0x4ebc3774, 0x82caa6fc, 0x90d0b0e0, 0xa7d81533, 0x04984af1, 0xecdaf741, 0xcd500e7f, 0x91f62f17, 0x4dd68d76, 0xefb04d43, 0xaa4d54cc, 0x9604dfe4, 0xd1b5e39e, 0x6a881b4c, 0x2c1fb8c1, 0x65517f46, 0x5eea049d, 0x8c355d01, 0x877473fa, 0x0b412efb, 0x671d5ab3, 0xdbd25292, 0x105633e9, 0xd647136d, 0xd7618c9a, 0xa10c7a37, 0xf8148e59, 0x133c89eb, 0xa927eece, 0x61c935b7, 0x1ce5ede1, 0x47b13c7a, 0xd2df599c, 0xf2733f55, 0x14ce7918, 0xc737bf73, 0xf7cdea53, 0xfdaa5b5f, 0x3d6f14df, 0x44db8678, 0xaff381ca, 0x68c43eb9, 0x24342c38, 0xa3405fc2, 0x1dc37216, 0xe2250cbc, 0x3c498b28, 0x0d9541ff, 0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0]; + + // Transformations for decryption key expansion + var U1 = [0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927, 0x24362e3a, 0x2a3f2331, 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45, 0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, 0xe090d0b0, 0xee99ddbb, 0xfc82caa6, 0xf28bc7ad, 0xd8b4e49c, 0xd6bde997, 0xc4a6fe8a, 0xcaaff381, 0x90d8b8e8, 0x9ed1b5e3, 0x8ccaa2fe, 0x82c3aff5, 0xa8fc8cc4, 0xa6f581cf, 0xb4ee96d2, 0xbae79bd9, 0xdb3bbb7b, 0xd532b670, 0xc729a16d, 0xc920ac66, 0xe31f8f57, 0xed16825c, 0xff0d9541, 0xf104984a, 0xab73d323, 0xa57ade28, 0xb761c935, 0xb968c43e, 0x9357e70f, 0x9d5eea04, 0x8f45fd19, 0x814cf012, 0x3bab6bcb, 0x35a266c0, 0x27b971dd, 0x29b07cd6, 0x038f5fe7, 0x0d8652ec, 0x1f9d45f1, 0x119448fa, 0x4be30393, 0x45ea0e98, 0x57f11985, 0x59f8148e, 0x73c737bf, 0x7dce3ab4, 0x6fd52da9, 0x61dc20a2, 0xad766df6, 0xa37f60fd, 0xb16477e0, 0xbf6d7aeb, 0x955259da, 0x9b5b54d1, 0x894043cc, 0x87494ec7, 0xdd3e05ae, 0xd33708a5, 0xc12c1fb8, 0xcf2512b3, 0xe51a3182, 0xeb133c89, 0xf9082b94, 0xf701269f, 0x4de6bd46, 0x43efb04d, 0x51f4a750, 0x5ffdaa5b, 0x75c2896a, 0x7bcb8461, 0x69d0937c, 0x67d99e77, 0x3daed51e, 0x33a7d815, 0x21bccf08, 0x2fb5c203, 0x058ae132, 0x0b83ec39, 0x1998fb24, 0x1791f62f, 0x764dd68d, 0x7844db86, 0x6a5fcc9b, 0x6456c190, 0x4e69e2a1, 0x4060efaa, 0x527bf8b7, 0x5c72f5bc, 0x0605bed5, 0x080cb3de, 0x1a17a4c3, 0x141ea9c8, 0x3e218af9, 0x302887f2, 0x223390ef, 0x2c3a9de4, 0x96dd063d, 0x98d40b36, 0x8acf1c2b, 0x84c61120, 0xaef93211, 0xa0f03f1a, 0xb2eb2807, 0xbce2250c, 0xe6956e65, 0xe89c636e, 0xfa877473, 0xf48e7978, 0xdeb15a49, 0xd0b85742, 0xc2a3405f, 0xccaa4d54, 0x41ecdaf7, 0x4fe5d7fc, 0x5dfec0e1, 0x53f7cdea, 0x79c8eedb, 0x77c1e3d0, 0x65daf4cd, 0x6bd3f9c6, 0x31a4b2af, 0x3fadbfa4, 0x2db6a8b9, 0x23bfa5b2, 0x09808683, 0x07898b88, 0x15929c95, 0x1b9b919e, 0xa17c0a47, 0xaf75074c, 0xbd6e1051, 0xb3671d5a, 0x99583e6b, 0x97513360, 0x854a247d, 0x8b432976, 0xd134621f, 0xdf3d6f14, 0xcd267809, 0xc32f7502, 0xe9105633, 0xe7195b38, 0xf5024c25, 0xfb0b412e, 0x9ad7618c, 0x94de6c87, 0x86c57b9a, 0x88cc7691, 0xa2f355a0, 0xacfa58ab, 0xbee14fb6, 0xb0e842bd, 0xea9f09d4, 0xe49604df, 0xf68d13c2, 0xf8841ec9, 0xd2bb3df8, 0xdcb230f3, 0xcea927ee, 0xc0a02ae5, 0x7a47b13c, 0x744ebc37, 0x6655ab2a, 0x685ca621, 0x42638510, 0x4c6a881b, 0x5e719f06, 0x5078920d, 0x0a0fd964, 0x0406d46f, 0x161dc372, 0x1814ce79, 0x322bed48, 0x3c22e043, 0x2e39f75e, 0x2030fa55, 0xec9ab701, 0xe293ba0a, 0xf088ad17, 0xfe81a01c, 0xd4be832d, 0xdab78e26, 0xc8ac993b, 0xc6a59430, 0x9cd2df59, 0x92dbd252, 0x80c0c54f, 0x8ec9c844, 0xa4f6eb75, 0xaaffe67e, 0xb8e4f163, 0xb6edfc68, 0x0c0a67b1, 0x02036aba, 0x10187da7, 0x1e1170ac, 0x342e539d, 0x3a275e96, 0x283c498b, 0x26354480, 0x7c420fe9, 0x724b02e2, 0x605015ff, 0x6e5918f4, 0x44663bc5, 0x4a6f36ce, 0x587421d3, 0x567d2cd8, 0x37a10c7a, 0x39a80171, 0x2bb3166c, 0x25ba1b67, 0x0f853856, 0x018c355d, 0x13972240, 0x1d9e2f4b, 0x47e96422, 0x49e06929, 0x5bfb7e34, 0x55f2733f, 0x7fcd500e, 0x71c45d05, 0x63df4a18, 0x6dd64713, 0xd731dcca, 0xd938d1c1, 0xcb23c6dc, 0xc52acbd7, 0xef15e8e6, 0xe11ce5ed, 0xf307f2f0, 0xfd0efffb, 0xa779b492, 0xa970b999, 0xbb6bae84, 0xb562a38f, 0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3]; + var U2 = [0x00000000, 0x0b0e090d, 0x161c121a, 0x1d121b17, 0x2c382434, 0x27362d39, 0x3a24362e, 0x312a3f23, 0x58704868, 0x537e4165, 0x4e6c5a72, 0x4562537f, 0x74486c5c, 0x7f466551, 0x62547e46, 0x695a774b, 0xb0e090d0, 0xbbee99dd, 0xa6fc82ca, 0xadf28bc7, 0x9cd8b4e4, 0x97d6bde9, 0x8ac4a6fe, 0x81caaff3, 0xe890d8b8, 0xe39ed1b5, 0xfe8ccaa2, 0xf582c3af, 0xc4a8fc8c, 0xcfa6f581, 0xd2b4ee96, 0xd9bae79b, 0x7bdb3bbb, 0x70d532b6, 0x6dc729a1, 0x66c920ac, 0x57e31f8f, 0x5ced1682, 0x41ff0d95, 0x4af10498, 0x23ab73d3, 0x28a57ade, 0x35b761c9, 0x3eb968c4, 0x0f9357e7, 0x049d5eea, 0x198f45fd, 0x12814cf0, 0xcb3bab6b, 0xc035a266, 0xdd27b971, 0xd629b07c, 0xe7038f5f, 0xec0d8652, 0xf11f9d45, 0xfa119448, 0x934be303, 0x9845ea0e, 0x8557f119, 0x8e59f814, 0xbf73c737, 0xb47dce3a, 0xa96fd52d, 0xa261dc20, 0xf6ad766d, 0xfda37f60, 0xe0b16477, 0xebbf6d7a, 0xda955259, 0xd19b5b54, 0xcc894043, 0xc787494e, 0xaedd3e05, 0xa5d33708, 0xb8c12c1f, 0xb3cf2512, 0x82e51a31, 0x89eb133c, 0x94f9082b, 0x9ff70126, 0x464de6bd, 0x4d43efb0, 0x5051f4a7, 0x5b5ffdaa, 0x6a75c289, 0x617bcb84, 0x7c69d093, 0x7767d99e, 0x1e3daed5, 0x1533a7d8, 0x0821bccf, 0x032fb5c2, 0x32058ae1, 0x390b83ec, 0x241998fb, 0x2f1791f6, 0x8d764dd6, 0x867844db, 0x9b6a5fcc, 0x906456c1, 0xa14e69e2, 0xaa4060ef, 0xb7527bf8, 0xbc5c72f5, 0xd50605be, 0xde080cb3, 0xc31a17a4, 0xc8141ea9, 0xf93e218a, 0xf2302887, 0xef223390, 0xe42c3a9d, 0x3d96dd06, 0x3698d40b, 0x2b8acf1c, 0x2084c611, 0x11aef932, 0x1aa0f03f, 0x07b2eb28, 0x0cbce225, 0x65e6956e, 0x6ee89c63, 0x73fa8774, 0x78f48e79, 0x49deb15a, 0x42d0b857, 0x5fc2a340, 0x54ccaa4d, 0xf741ecda, 0xfc4fe5d7, 0xe15dfec0, 0xea53f7cd, 0xdb79c8ee, 0xd077c1e3, 0xcd65daf4, 0xc66bd3f9, 0xaf31a4b2, 0xa43fadbf, 0xb92db6a8, 0xb223bfa5, 0x83098086, 0x8807898b, 0x9515929c, 0x9e1b9b91, 0x47a17c0a, 0x4caf7507, 0x51bd6e10, 0x5ab3671d, 0x6b99583e, 0x60975133, 0x7d854a24, 0x768b4329, 0x1fd13462, 0x14df3d6f, 0x09cd2678, 0x02c32f75, 0x33e91056, 0x38e7195b, 0x25f5024c, 0x2efb0b41, 0x8c9ad761, 0x8794de6c, 0x9a86c57b, 0x9188cc76, 0xa0a2f355, 0xabacfa58, 0xb6bee14f, 0xbdb0e842, 0xd4ea9f09, 0xdfe49604, 0xc2f68d13, 0xc9f8841e, 0xf8d2bb3d, 0xf3dcb230, 0xeecea927, 0xe5c0a02a, 0x3c7a47b1, 0x37744ebc, 0x2a6655ab, 0x21685ca6, 0x10426385, 0x1b4c6a88, 0x065e719f, 0x0d507892, 0x640a0fd9, 0x6f0406d4, 0x72161dc3, 0x791814ce, 0x48322bed, 0x433c22e0, 0x5e2e39f7, 0x552030fa, 0x01ec9ab7, 0x0ae293ba, 0x17f088ad, 0x1cfe81a0, 0x2dd4be83, 0x26dab78e, 0x3bc8ac99, 0x30c6a594, 0x599cd2df, 0x5292dbd2, 0x4f80c0c5, 0x448ec9c8, 0x75a4f6eb, 0x7eaaffe6, 0x63b8e4f1, 0x68b6edfc, 0xb10c0a67, 0xba02036a, 0xa710187d, 0xac1e1170, 0x9d342e53, 0x963a275e, 0x8b283c49, 0x80263544, 0xe97c420f, 0xe2724b02, 0xff605015, 0xf46e5918, 0xc544663b, 0xce4a6f36, 0xd3587421, 0xd8567d2c, 0x7a37a10c, 0x7139a801, 0x6c2bb316, 0x6725ba1b, 0x560f8538, 0x5d018c35, 0x40139722, 0x4b1d9e2f, 0x2247e964, 0x2949e069, 0x345bfb7e, 0x3f55f273, 0x0e7fcd50, 0x0571c45d, 0x1863df4a, 0x136dd647, 0xcad731dc, 0xc1d938d1, 0xdccb23c6, 0xd7c52acb, 0xe6ef15e8, 0xede11ce5, 0xf0f307f2, 0xfbfd0eff, 0x92a779b4, 0x99a970b9, 0x84bb6bae, 0x8fb562a3, 0xbe9f5d80, 0xb591548d, 0xa8834f9a, 0xa38d4697]; + var U3 = [0x00000000, 0x0d0b0e09, 0x1a161c12, 0x171d121b, 0x342c3824, 0x3927362d, 0x2e3a2436, 0x23312a3f, 0x68587048, 0x65537e41, 0x724e6c5a, 0x7f456253, 0x5c74486c, 0x517f4665, 0x4662547e, 0x4b695a77, 0xd0b0e090, 0xddbbee99, 0xcaa6fc82, 0xc7adf28b, 0xe49cd8b4, 0xe997d6bd, 0xfe8ac4a6, 0xf381caaf, 0xb8e890d8, 0xb5e39ed1, 0xa2fe8cca, 0xaff582c3, 0x8cc4a8fc, 0x81cfa6f5, 0x96d2b4ee, 0x9bd9bae7, 0xbb7bdb3b, 0xb670d532, 0xa16dc729, 0xac66c920, 0x8f57e31f, 0x825ced16, 0x9541ff0d, 0x984af104, 0xd323ab73, 0xde28a57a, 0xc935b761, 0xc43eb968, 0xe70f9357, 0xea049d5e, 0xfd198f45, 0xf012814c, 0x6bcb3bab, 0x66c035a2, 0x71dd27b9, 0x7cd629b0, 0x5fe7038f, 0x52ec0d86, 0x45f11f9d, 0x48fa1194, 0x03934be3, 0x0e9845ea, 0x198557f1, 0x148e59f8, 0x37bf73c7, 0x3ab47dce, 0x2da96fd5, 0x20a261dc, 0x6df6ad76, 0x60fda37f, 0x77e0b164, 0x7aebbf6d, 0x59da9552, 0x54d19b5b, 0x43cc8940, 0x4ec78749, 0x05aedd3e, 0x08a5d337, 0x1fb8c12c, 0x12b3cf25, 0x3182e51a, 0x3c89eb13, 0x2b94f908, 0x269ff701, 0xbd464de6, 0xb04d43ef, 0xa75051f4, 0xaa5b5ffd, 0x896a75c2, 0x84617bcb, 0x937c69d0, 0x9e7767d9, 0xd51e3dae, 0xd81533a7, 0xcf0821bc, 0xc2032fb5, 0xe132058a, 0xec390b83, 0xfb241998, 0xf62f1791, 0xd68d764d, 0xdb867844, 0xcc9b6a5f, 0xc1906456, 0xe2a14e69, 0xefaa4060, 0xf8b7527b, 0xf5bc5c72, 0xbed50605, 0xb3de080c, 0xa4c31a17, 0xa9c8141e, 0x8af93e21, 0x87f23028, 0x90ef2233, 0x9de42c3a, 0x063d96dd, 0x0b3698d4, 0x1c2b8acf, 0x112084c6, 0x3211aef9, 0x3f1aa0f0, 0x2807b2eb, 0x250cbce2, 0x6e65e695, 0x636ee89c, 0x7473fa87, 0x7978f48e, 0x5a49deb1, 0x5742d0b8, 0x405fc2a3, 0x4d54ccaa, 0xdaf741ec, 0xd7fc4fe5, 0xc0e15dfe, 0xcdea53f7, 0xeedb79c8, 0xe3d077c1, 0xf4cd65da, 0xf9c66bd3, 0xb2af31a4, 0xbfa43fad, 0xa8b92db6, 0xa5b223bf, 0x86830980, 0x8b880789, 0x9c951592, 0x919e1b9b, 0x0a47a17c, 0x074caf75, 0x1051bd6e, 0x1d5ab367, 0x3e6b9958, 0x33609751, 0x247d854a, 0x29768b43, 0x621fd134, 0x6f14df3d, 0x7809cd26, 0x7502c32f, 0x5633e910, 0x5b38e719, 0x4c25f502, 0x412efb0b, 0x618c9ad7, 0x6c8794de, 0x7b9a86c5, 0x769188cc, 0x55a0a2f3, 0x58abacfa, 0x4fb6bee1, 0x42bdb0e8, 0x09d4ea9f, 0x04dfe496, 0x13c2f68d, 0x1ec9f884, 0x3df8d2bb, 0x30f3dcb2, 0x27eecea9, 0x2ae5c0a0, 0xb13c7a47, 0xbc37744e, 0xab2a6655, 0xa621685c, 0x85104263, 0x881b4c6a, 0x9f065e71, 0x920d5078, 0xd9640a0f, 0xd46f0406, 0xc372161d, 0xce791814, 0xed48322b, 0xe0433c22, 0xf75e2e39, 0xfa552030, 0xb701ec9a, 0xba0ae293, 0xad17f088, 0xa01cfe81, 0x832dd4be, 0x8e26dab7, 0x993bc8ac, 0x9430c6a5, 0xdf599cd2, 0xd25292db, 0xc54f80c0, 0xc8448ec9, 0xeb75a4f6, 0xe67eaaff, 0xf163b8e4, 0xfc68b6ed, 0x67b10c0a, 0x6aba0203, 0x7da71018, 0x70ac1e11, 0x539d342e, 0x5e963a27, 0x498b283c, 0x44802635, 0x0fe97c42, 0x02e2724b, 0x15ff6050, 0x18f46e59, 0x3bc54466, 0x36ce4a6f, 0x21d35874, 0x2cd8567d, 0x0c7a37a1, 0x017139a8, 0x166c2bb3, 0x1b6725ba, 0x38560f85, 0x355d018c, 0x22401397, 0x2f4b1d9e, 0x642247e9, 0x692949e0, 0x7e345bfb, 0x733f55f2, 0x500e7fcd, 0x5d0571c4, 0x4a1863df, 0x47136dd6, 0xdccad731, 0xd1c1d938, 0xc6dccb23, 0xcbd7c52a, 0xe8e6ef15, 0xe5ede11c, 0xf2f0f307, 0xfffbfd0e, 0xb492a779, 0xb999a970, 0xae84bb6b, 0xa38fb562, 0x80be9f5d, 0x8db59154, 0x9aa8834f, 0x97a38d46]; + var U4 = [0x00000000, 0x090d0b0e, 0x121a161c, 0x1b171d12, 0x24342c38, 0x2d392736, 0x362e3a24, 0x3f23312a, 0x48685870, 0x4165537e, 0x5a724e6c, 0x537f4562, 0x6c5c7448, 0x65517f46, 0x7e466254, 0x774b695a, 0x90d0b0e0, 0x99ddbbee, 0x82caa6fc, 0x8bc7adf2, 0xb4e49cd8, 0xbde997d6, 0xa6fe8ac4, 0xaff381ca, 0xd8b8e890, 0xd1b5e39e, 0xcaa2fe8c, 0xc3aff582, 0xfc8cc4a8, 0xf581cfa6, 0xee96d2b4, 0xe79bd9ba, 0x3bbb7bdb, 0x32b670d5, 0x29a16dc7, 0x20ac66c9, 0x1f8f57e3, 0x16825ced, 0x0d9541ff, 0x04984af1, 0x73d323ab, 0x7ade28a5, 0x61c935b7, 0x68c43eb9, 0x57e70f93, 0x5eea049d, 0x45fd198f, 0x4cf01281, 0xab6bcb3b, 0xa266c035, 0xb971dd27, 0xb07cd629, 0x8f5fe703, 0x8652ec0d, 0x9d45f11f, 0x9448fa11, 0xe303934b, 0xea0e9845, 0xf1198557, 0xf8148e59, 0xc737bf73, 0xce3ab47d, 0xd52da96f, 0xdc20a261, 0x766df6ad, 0x7f60fda3, 0x6477e0b1, 0x6d7aebbf, 0x5259da95, 0x5b54d19b, 0x4043cc89, 0x494ec787, 0x3e05aedd, 0x3708a5d3, 0x2c1fb8c1, 0x2512b3cf, 0x1a3182e5, 0x133c89eb, 0x082b94f9, 0x01269ff7, 0xe6bd464d, 0xefb04d43, 0xf4a75051, 0xfdaa5b5f, 0xc2896a75, 0xcb84617b, 0xd0937c69, 0xd99e7767, 0xaed51e3d, 0xa7d81533, 0xbccf0821, 0xb5c2032f, 0x8ae13205, 0x83ec390b, 0x98fb2419, 0x91f62f17, 0x4dd68d76, 0x44db8678, 0x5fcc9b6a, 0x56c19064, 0x69e2a14e, 0x60efaa40, 0x7bf8b752, 0x72f5bc5c, 0x05bed506, 0x0cb3de08, 0x17a4c31a, 0x1ea9c814, 0x218af93e, 0x2887f230, 0x3390ef22, 0x3a9de42c, 0xdd063d96, 0xd40b3698, 0xcf1c2b8a, 0xc6112084, 0xf93211ae, 0xf03f1aa0, 0xeb2807b2, 0xe2250cbc, 0x956e65e6, 0x9c636ee8, 0x877473fa, 0x8e7978f4, 0xb15a49de, 0xb85742d0, 0xa3405fc2, 0xaa4d54cc, 0xecdaf741, 0xe5d7fc4f, 0xfec0e15d, 0xf7cdea53, 0xc8eedb79, 0xc1e3d077, 0xdaf4cd65, 0xd3f9c66b, 0xa4b2af31, 0xadbfa43f, 0xb6a8b92d, 0xbfa5b223, 0x80868309, 0x898b8807, 0x929c9515, 0x9b919e1b, 0x7c0a47a1, 0x75074caf, 0x6e1051bd, 0x671d5ab3, 0x583e6b99, 0x51336097, 0x4a247d85, 0x4329768b, 0x34621fd1, 0x3d6f14df, 0x267809cd, 0x2f7502c3, 0x105633e9, 0x195b38e7, 0x024c25f5, 0x0b412efb, 0xd7618c9a, 0xde6c8794, 0xc57b9a86, 0xcc769188, 0xf355a0a2, 0xfa58abac, 0xe14fb6be, 0xe842bdb0, 0x9f09d4ea, 0x9604dfe4, 0x8d13c2f6, 0x841ec9f8, 0xbb3df8d2, 0xb230f3dc, 0xa927eece, 0xa02ae5c0, 0x47b13c7a, 0x4ebc3774, 0x55ab2a66, 0x5ca62168, 0x63851042, 0x6a881b4c, 0x719f065e, 0x78920d50, 0x0fd9640a, 0x06d46f04, 0x1dc37216, 0x14ce7918, 0x2bed4832, 0x22e0433c, 0x39f75e2e, 0x30fa5520, 0x9ab701ec, 0x93ba0ae2, 0x88ad17f0, 0x81a01cfe, 0xbe832dd4, 0xb78e26da, 0xac993bc8, 0xa59430c6, 0xd2df599c, 0xdbd25292, 0xc0c54f80, 0xc9c8448e, 0xf6eb75a4, 0xffe67eaa, 0xe4f163b8, 0xedfc68b6, 0x0a67b10c, 0x036aba02, 0x187da710, 0x1170ac1e, 0x2e539d34, 0x275e963a, 0x3c498b28, 0x35448026, 0x420fe97c, 0x4b02e272, 0x5015ff60, 0x5918f46e, 0x663bc544, 0x6f36ce4a, 0x7421d358, 0x7d2cd856, 0xa10c7a37, 0xa8017139, 0xb3166c2b, 0xba1b6725, 0x8538560f, 0x8c355d01, 0x97224013, 0x9e2f4b1d, 0xe9642247, 0xe0692949, 0xfb7e345b, 0xf2733f55, 0xcd500e7f, 0xc45d0571, 0xdf4a1863, 0xd647136d, 0x31dccad7, 0x38d1c1d9, 0x23c6dccb, 0x2acbd7c5, 0x15e8e6ef, 0x1ce5ede1, 0x07f2f0f3, 0x0efffbfd, 0x79b492a7, 0x70b999a9, 0x6bae84bb, 0x62a38fb5, 0x5d80be9f, 0x548db591, 0x4f9aa883, 0x4697a38d]; + + function convertToInt32(bytes) { var result = []; - for (var i = 0; i < text.length; i += 2) { - result.push(parseInt(text.substr(i, 2), 16)); + for (var i = 0; i < bytes.length; i += 4) { + result.push( + (bytes[i] << 24) | + (bytes[i + 1] << 16) | + (bytes[i + 2] << 8) | + bytes[i + 3] + ); + } + return result; + } + + var AES = function (key) { + if (!(this instanceof AES)) { + throw Error('AES must be instanitated with `new`'); + } + + Object.defineProperty(this, 'key', { + value: coerceArray(key, true) + }); + + this._prepare(); + } + + + AES.prototype._prepare = function () { + + var rounds = numberOfRounds[this.key.length]; + if (rounds == null) { + throw new Error('invalid key size (must be 16, 24 or 32 bytes)'); + } + + // encryption round keys + this._Ke = []; + + // decryption round keys + this._Kd = []; + + for (var i = 0; i <= rounds; i++) { + this._Ke.push([0, 0, 0, 0]); + this._Kd.push([0, 0, 0, 0]); + } + + var roundKeyCount = (rounds + 1) * 4; + var KC = this.key.length / 4; + + // convert the key into ints + var tk = convertToInt32(this.key); + + // copy values into round key arrays + var index; + for (var i = 0; i < KC; i++) { + index = i >> 2; + this._Ke[index][i % 4] = tk[i]; + this._Kd[rounds - index][i % 4] = tk[i]; + } + + // key expansion (fips-197 section 5.2) + var rconpointer = 0; + var t = KC, tt; + while (t < roundKeyCount) { + tt = tk[KC - 1]; + tk[0] ^= ((S[(tt >> 16) & 0xFF] << 24) ^ + (S[(tt >> 8) & 0xFF] << 16) ^ + (S[tt & 0xFF] << 8) ^ + S[(tt >> 24) & 0xFF] ^ + (rcon[rconpointer] << 24)); + rconpointer += 1; + + // key expansion (for non-256 bit) + if (KC != 8) { + for (var i = 1; i < KC; i++) { + tk[i] ^= tk[i - 1]; + } + + // key expansion for 256-bit keys is "slightly different" (fips-197) + } else { + for (var i = 1; i < (KC / 2); i++) { + tk[i] ^= tk[i - 1]; + } + tt = tk[(KC / 2) - 1]; + + tk[KC / 2] ^= (S[tt & 0xFF] ^ + (S[(tt >> 8) & 0xFF] << 8) ^ + (S[(tt >> 16) & 0xFF] << 16) ^ + (S[(tt >> 24) & 0xFF] << 24)); + + for (var i = (KC / 2) + 1; i < KC; i++) { + tk[i] ^= tk[i - 1]; + } + } + + // copy values into round key arrays + var i = 0, r, c; + while (i < KC && t < roundKeyCount) { + r = t >> 2; + c = t % 4; + this._Ke[r][c] = tk[i]; + this._Kd[rounds - r][c] = tk[i++]; + t++; + } + } + + // inverse-cipher-ify the decryption round key (fips-197 section 5.3) + for (var r = 1; r < rounds; r++) { + for (var c = 0; c < 4; c++) { + tt = this._Kd[r][c]; + this._Kd[r][c] = (U1[(tt >> 24) & 0xFF] ^ + U2[(tt >> 16) & 0xFF] ^ + U3[(tt >> 8) & 0xFF] ^ + U4[tt & 0xFF]); + } + } + } + + AES.prototype.encrypt = function (plaintext) { + if (plaintext.length != 16) { + throw new Error('invalid plaintext size (must be 16 bytes)'); + } + + var rounds = this._Ke.length - 1; + var a = [0, 0, 0, 0]; + + // convert plaintext to (ints ^ key) + var t = convertToInt32(plaintext); + for (var i = 0; i < 4; i++) { + t[i] ^= this._Ke[0][i]; + } + + // apply round transforms + for (var r = 1; r < rounds; r++) { + for (var i = 0; i < 4; i++) { + a[i] = (T1[(t[i] >> 24) & 0xff] ^ + T2[(t[(i + 1) % 4] >> 16) & 0xff] ^ + T3[(t[(i + 2) % 4] >> 8) & 0xff] ^ + T4[t[(i + 3) % 4] & 0xff] ^ + this._Ke[r][i]); + } + t = a.slice(); + } + + // the last round is special + var result = createArray(16), tt; + for (var i = 0; i < 4; i++) { + tt = this._Ke[rounds][i]; + result[4 * i] = (S[(t[i] >> 24) & 0xff] ^ (tt >> 24)) & 0xff; + result[4 * i + 1] = (S[(t[(i + 1) % 4] >> 16) & 0xff] ^ (tt >> 16)) & 0xff; + result[4 * i + 2] = (S[(t[(i + 2) % 4] >> 8) & 0xff] ^ (tt >> 8)) & 0xff; + result[4 * i + 3] = (S[t[(i + 3) % 4] & 0xff] ^ tt) & 0xff; } return result; } - // http://ixti.net/development/javascript/2011/11/11/base64-encodedecode-of-utf8-in-browser-with-js.html - var Hex = '0123456789abcdef'; - - function fromBytes(bytes) { - var result = []; - for (var i = 0; i < bytes.length; i++) { - var v = bytes[i]; - result.push(Hex[(v & 0xf0) >> 4] + Hex[v & 0x0f]); + AES.prototype.decrypt = function (ciphertext) { + if (ciphertext.length != 16) { + throw new Error('invalid ciphertext size (must be 16 bytes)'); } - return result.join(''); + + var rounds = this._Kd.length - 1; + var a = [0, 0, 0, 0]; + + // convert plaintext to (ints ^ key) + var t = convertToInt32(ciphertext); + for (var i = 0; i < 4; i++) { + t[i] ^= this._Kd[0][i]; + } + + // apply round transforms + for (var r = 1; r < rounds; r++) { + for (var i = 0; i < 4; i++) { + a[i] = (T5[(t[i] >> 24) & 0xff] ^ + T6[(t[(i + 3) % 4] >> 16) & 0xff] ^ + T7[(t[(i + 2) % 4] >> 8) & 0xff] ^ + T8[t[(i + 1) % 4] & 0xff] ^ + this._Kd[r][i]); + } + t = a.slice(); + } + + // the last round is special + var result = createArray(16), tt; + for (var i = 0; i < 4; i++) { + tt = this._Kd[rounds][i]; + result[4 * i] = (Si[(t[i] >> 24) & 0xff] ^ (tt >> 24)) & 0xff; + result[4 * i + 1] = (Si[(t[(i + 3) % 4] >> 16) & 0xff] ^ (tt >> 16)) & 0xff; + result[4 * i + 2] = (Si[(t[(i + 2) % 4] >> 8) & 0xff] ^ (tt >> 8)) & 0xff; + result[4 * i + 3] = (Si[t[(i + 1) % 4] & 0xff] ^ tt) & 0xff; + } + + return result; } - return { - toBytes: toBytes, - fromBytes: fromBytes, - } -})(); + /** + * Mode Of Operation - Electonic Codebook (ECB) + */ + var ModeOfOperationECB = function (key) { + if (!(this instanceof ModeOfOperationECB)) { + throw Error('AES must be instanitated with `new`'); + } -// Number of rounds by keysize -var numberOfRounds = {16: 10, 24: 12, 32: 14} + this.description = "Electronic Code Block"; + this.name = "ecb"; -// Round constant words -var rcon = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91]; - -// S-box and Inverse S-box (S is for Substitution) -var S = [0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16]; -var Si = [0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d]; - -// Transformations for encryption -var T1 = [0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a, 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b, 0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b, 0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a, 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f, 0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108, 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f, 0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e, 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5, 0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d, 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f, 0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e, 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb, 0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce, 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497, 0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c, 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed, 0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b, 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a, 0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16, 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594, 0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81, 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3, 0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a, 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504, 0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163, 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d, 0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f, 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739, 0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47, 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395, 0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f, 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883, 0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c, 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76, 0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e, 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4, 0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6, 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b, 0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7, 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0, 0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25, 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818, 0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72, 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651, 0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21, 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85, 0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa, 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12, 0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0, 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9, 0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133, 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7, 0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920, 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a, 0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8, 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a]; -var T2 = [0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5, 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676, 0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0, 0xec41adad, 0x67b3d4d4, 0xfd5fa2a2, 0xea45afaf, 0xbf239c9c, 0xf753a4a4, 0x96e47272, 0x5b9bc0c0, 0xc275b7b7, 0x1ce1fdfd, 0xae3d9393, 0x6a4c2626, 0x5a6c3636, 0x417e3f3f, 0x02f5f7f7, 0x4f83cccc, 0x5c683434, 0xf451a5a5, 0x34d1e5e5, 0x08f9f1f1, 0x93e27171, 0x73abd8d8, 0x53623131, 0x3f2a1515, 0x0c080404, 0x5295c7c7, 0x65462323, 0x5e9dc3c3, 0x28301818, 0xa1379696, 0x0f0a0505, 0xb52f9a9a, 0x090e0707, 0x36241212, 0x9b1b8080, 0x3ddfe2e2, 0x26cdebeb, 0x694e2727, 0xcd7fb2b2, 0x9fea7575, 0x1b120909, 0x9e1d8383, 0x74582c2c, 0x2e341a1a, 0x2d361b1b, 0xb2dc6e6e, 0xeeb45a5a, 0xfb5ba0a0, 0xf6a45252, 0x4d763b3b, 0x61b7d6d6, 0xce7db3b3, 0x7b522929, 0x3edde3e3, 0x715e2f2f, 0x97138484, 0xf5a65353, 0x68b9d1d1, 0x00000000, 0x2cc1eded, 0x60402020, 0x1fe3fcfc, 0xc879b1b1, 0xedb65b5b, 0xbed46a6a, 0x468dcbcb, 0xd967bebe, 0x4b723939, 0xde944a4a, 0xd4984c4c, 0xe8b05858, 0x4a85cfcf, 0x6bbbd0d0, 0x2ac5efef, 0xe54faaaa, 0x16edfbfb, 0xc5864343, 0xd79a4d4d, 0x55663333, 0x94118585, 0xcf8a4545, 0x10e9f9f9, 0x06040202, 0x81fe7f7f, 0xf0a05050, 0x44783c3c, 0xba259f9f, 0xe34ba8a8, 0xf3a25151, 0xfe5da3a3, 0xc0804040, 0x8a058f8f, 0xad3f9292, 0xbc219d9d, 0x48703838, 0x04f1f5f5, 0xdf63bcbc, 0xc177b6b6, 0x75afdada, 0x63422121, 0x30201010, 0x1ae5ffff, 0x0efdf3f3, 0x6dbfd2d2, 0x4c81cdcd, 0x14180c0c, 0x35261313, 0x2fc3ecec, 0xe1be5f5f, 0xa2359797, 0xcc884444, 0x392e1717, 0x5793c4c4, 0xf255a7a7, 0x82fc7e7e, 0x477a3d3d, 0xacc86464, 0xe7ba5d5d, 0x2b321919, 0x95e67373, 0xa0c06060, 0x98198181, 0xd19e4f4f, 0x7fa3dcdc, 0x66442222, 0x7e542a2a, 0xab3b9090, 0x830b8888, 0xca8c4646, 0x29c7eeee, 0xd36bb8b8, 0x3c281414, 0x79a7dede, 0xe2bc5e5e, 0x1d160b0b, 0x76addbdb, 0x3bdbe0e0, 0x56643232, 0x4e743a3a, 0x1e140a0a, 0xdb924949, 0x0a0c0606, 0x6c482424, 0xe4b85c5c, 0x5d9fc2c2, 0x6ebdd3d3, 0xef43acac, 0xa6c46262, 0xa8399191, 0xa4319595, 0x37d3e4e4, 0x8bf27979, 0x32d5e7e7, 0x438bc8c8, 0x596e3737, 0xb7da6d6d, 0x8c018d8d, 0x64b1d5d5, 0xd29c4e4e, 0xe049a9a9, 0xb4d86c6c, 0xfaac5656, 0x07f3f4f4, 0x25cfeaea, 0xafca6565, 0x8ef47a7a, 0xe947aeae, 0x18100808, 0xd56fbaba, 0x88f07878, 0x6f4a2525, 0x725c2e2e, 0x24381c1c, 0xf157a6a6, 0xc773b4b4, 0x5197c6c6, 0x23cbe8e8, 0x7ca1dddd, 0x9ce87474, 0x213e1f1f, 0xdd964b4b, 0xdc61bdbd, 0x860d8b8b, 0x850f8a8a, 0x90e07070, 0x427c3e3e, 0xc471b5b5, 0xaacc6666, 0xd8904848, 0x05060303, 0x01f7f6f6, 0x121c0e0e, 0xa3c26161, 0x5f6a3535, 0xf9ae5757, 0xd069b9b9, 0x91178686, 0x5899c1c1, 0x273a1d1d, 0xb9279e9e, 0x38d9e1e1, 0x13ebf8f8, 0xb32b9898, 0x33221111, 0xbbd26969, 0x70a9d9d9, 0x89078e8e, 0xa7339494, 0xb62d9b9b, 0x223c1e1e, 0x92158787, 0x20c9e9e9, 0x4987cece, 0xffaa5555, 0x78502828, 0x7aa5dfdf, 0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d, 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868, 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616]; -var T3 = [0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5, 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76, 0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0, 0xadec41ad, 0xd467b3d4, 0xa2fd5fa2, 0xafea45af, 0x9cbf239c, 0xa4f753a4, 0x7296e472, 0xc05b9bc0, 0xb7c275b7, 0xfd1ce1fd, 0x93ae3d93, 0x266a4c26, 0x365a6c36, 0x3f417e3f, 0xf702f5f7, 0xcc4f83cc, 0x345c6834, 0xa5f451a5, 0xe534d1e5, 0xf108f9f1, 0x7193e271, 0xd873abd8, 0x31536231, 0x153f2a15, 0x040c0804, 0xc75295c7, 0x23654623, 0xc35e9dc3, 0x18283018, 0x96a13796, 0x050f0a05, 0x9ab52f9a, 0x07090e07, 0x12362412, 0x809b1b80, 0xe23ddfe2, 0xeb26cdeb, 0x27694e27, 0xb2cd7fb2, 0x759fea75, 0x091b1209, 0x839e1d83, 0x2c74582c, 0x1a2e341a, 0x1b2d361b, 0x6eb2dc6e, 0x5aeeb45a, 0xa0fb5ba0, 0x52f6a452, 0x3b4d763b, 0xd661b7d6, 0xb3ce7db3, 0x297b5229, 0xe33edde3, 0x2f715e2f, 0x84971384, 0x53f5a653, 0xd168b9d1, 0x00000000, 0xed2cc1ed, 0x20604020, 0xfc1fe3fc, 0xb1c879b1, 0x5bedb65b, 0x6abed46a, 0xcb468dcb, 0xbed967be, 0x394b7239, 0x4ade944a, 0x4cd4984c, 0x58e8b058, 0xcf4a85cf, 0xd06bbbd0, 0xef2ac5ef, 0xaae54faa, 0xfb16edfb, 0x43c58643, 0x4dd79a4d, 0x33556633, 0x85941185, 0x45cf8a45, 0xf910e9f9, 0x02060402, 0x7f81fe7f, 0x50f0a050, 0x3c44783c, 0x9fba259f, 0xa8e34ba8, 0x51f3a251, 0xa3fe5da3, 0x40c08040, 0x8f8a058f, 0x92ad3f92, 0x9dbc219d, 0x38487038, 0xf504f1f5, 0xbcdf63bc, 0xb6c177b6, 0xda75afda, 0x21634221, 0x10302010, 0xff1ae5ff, 0xf30efdf3, 0xd26dbfd2, 0xcd4c81cd, 0x0c14180c, 0x13352613, 0xec2fc3ec, 0x5fe1be5f, 0x97a23597, 0x44cc8844, 0x17392e17, 0xc45793c4, 0xa7f255a7, 0x7e82fc7e, 0x3d477a3d, 0x64acc864, 0x5de7ba5d, 0x192b3219, 0x7395e673, 0x60a0c060, 0x81981981, 0x4fd19e4f, 0xdc7fa3dc, 0x22664422, 0x2a7e542a, 0x90ab3b90, 0x88830b88, 0x46ca8c46, 0xee29c7ee, 0xb8d36bb8, 0x143c2814, 0xde79a7de, 0x5ee2bc5e, 0x0b1d160b, 0xdb76addb, 0xe03bdbe0, 0x32566432, 0x3a4e743a, 0x0a1e140a, 0x49db9249, 0x060a0c06, 0x246c4824, 0x5ce4b85c, 0xc25d9fc2, 0xd36ebdd3, 0xacef43ac, 0x62a6c462, 0x91a83991, 0x95a43195, 0xe437d3e4, 0x798bf279, 0xe732d5e7, 0xc8438bc8, 0x37596e37, 0x6db7da6d, 0x8d8c018d, 0xd564b1d5, 0x4ed29c4e, 0xa9e049a9, 0x6cb4d86c, 0x56faac56, 0xf407f3f4, 0xea25cfea, 0x65afca65, 0x7a8ef47a, 0xaee947ae, 0x08181008, 0xbad56fba, 0x7888f078, 0x256f4a25, 0x2e725c2e, 0x1c24381c, 0xa6f157a6, 0xb4c773b4, 0xc65197c6, 0xe823cbe8, 0xdd7ca1dd, 0x749ce874, 0x1f213e1f, 0x4bdd964b, 0xbddc61bd, 0x8b860d8b, 0x8a850f8a, 0x7090e070, 0x3e427c3e, 0xb5c471b5, 0x66aacc66, 0x48d89048, 0x03050603, 0xf601f7f6, 0x0e121c0e, 0x61a3c261, 0x355f6a35, 0x57f9ae57, 0xb9d069b9, 0x86911786, 0xc15899c1, 0x1d273a1d, 0x9eb9279e, 0xe138d9e1, 0xf813ebf8, 0x98b32b98, 0x11332211, 0x69bbd269, 0xd970a9d9, 0x8e89078e, 0x94a73394, 0x9bb62d9b, 0x1e223c1e, 0x87921587, 0xe920c9e9, 0xce4987ce, 0x55ffaa55, 0x28785028, 0xdf7aa5df, 0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d, 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068, 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16]; -var T4 = [0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491, 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec, 0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb, 0xadadec41, 0xd4d467b3, 0xa2a2fd5f, 0xafafea45, 0x9c9cbf23, 0xa4a4f753, 0x727296e4, 0xc0c05b9b, 0xb7b7c275, 0xfdfd1ce1, 0x9393ae3d, 0x26266a4c, 0x36365a6c, 0x3f3f417e, 0xf7f702f5, 0xcccc4f83, 0x34345c68, 0xa5a5f451, 0xe5e534d1, 0xf1f108f9, 0x717193e2, 0xd8d873ab, 0x31315362, 0x15153f2a, 0x04040c08, 0xc7c75295, 0x23236546, 0xc3c35e9d, 0x18182830, 0x9696a137, 0x05050f0a, 0x9a9ab52f, 0x0707090e, 0x12123624, 0x80809b1b, 0xe2e23ddf, 0xebeb26cd, 0x2727694e, 0xb2b2cd7f, 0x75759fea, 0x09091b12, 0x83839e1d, 0x2c2c7458, 0x1a1a2e34, 0x1b1b2d36, 0x6e6eb2dc, 0x5a5aeeb4, 0xa0a0fb5b, 0x5252f6a4, 0x3b3b4d76, 0xd6d661b7, 0xb3b3ce7d, 0x29297b52, 0xe3e33edd, 0x2f2f715e, 0x84849713, 0x5353f5a6, 0xd1d168b9, 0x00000000, 0xeded2cc1, 0x20206040, 0xfcfc1fe3, 0xb1b1c879, 0x5b5bedb6, 0x6a6abed4, 0xcbcb468d, 0xbebed967, 0x39394b72, 0x4a4ade94, 0x4c4cd498, 0x5858e8b0, 0xcfcf4a85, 0xd0d06bbb, 0xefef2ac5, 0xaaaae54f, 0xfbfb16ed, 0x4343c586, 0x4d4dd79a, 0x33335566, 0x85859411, 0x4545cf8a, 0xf9f910e9, 0x02020604, 0x7f7f81fe, 0x5050f0a0, 0x3c3c4478, 0x9f9fba25, 0xa8a8e34b, 0x5151f3a2, 0xa3a3fe5d, 0x4040c080, 0x8f8f8a05, 0x9292ad3f, 0x9d9dbc21, 0x38384870, 0xf5f504f1, 0xbcbcdf63, 0xb6b6c177, 0xdada75af, 0x21216342, 0x10103020, 0xffff1ae5, 0xf3f30efd, 0xd2d26dbf, 0xcdcd4c81, 0x0c0c1418, 0x13133526, 0xecec2fc3, 0x5f5fe1be, 0x9797a235, 0x4444cc88, 0x1717392e, 0xc4c45793, 0xa7a7f255, 0x7e7e82fc, 0x3d3d477a, 0x6464acc8, 0x5d5de7ba, 0x19192b32, 0x737395e6, 0x6060a0c0, 0x81819819, 0x4f4fd19e, 0xdcdc7fa3, 0x22226644, 0x2a2a7e54, 0x9090ab3b, 0x8888830b, 0x4646ca8c, 0xeeee29c7, 0xb8b8d36b, 0x14143c28, 0xdede79a7, 0x5e5ee2bc, 0x0b0b1d16, 0xdbdb76ad, 0xe0e03bdb, 0x32325664, 0x3a3a4e74, 0x0a0a1e14, 0x4949db92, 0x06060a0c, 0x24246c48, 0x5c5ce4b8, 0xc2c25d9f, 0xd3d36ebd, 0xacacef43, 0x6262a6c4, 0x9191a839, 0x9595a431, 0xe4e437d3, 0x79798bf2, 0xe7e732d5, 0xc8c8438b, 0x3737596e, 0x6d6db7da, 0x8d8d8c01, 0xd5d564b1, 0x4e4ed29c, 0xa9a9e049, 0x6c6cb4d8, 0x5656faac, 0xf4f407f3, 0xeaea25cf, 0x6565afca, 0x7a7a8ef4, 0xaeaee947, 0x08081810, 0xbabad56f, 0x787888f0, 0x25256f4a, 0x2e2e725c, 0x1c1c2438, 0xa6a6f157, 0xb4b4c773, 0xc6c65197, 0xe8e823cb, 0xdddd7ca1, 0x74749ce8, 0x1f1f213e, 0x4b4bdd96, 0xbdbddc61, 0x8b8b860d, 0x8a8a850f, 0x707090e0, 0x3e3e427c, 0xb5b5c471, 0x6666aacc, 0x4848d890, 0x03030506, 0xf6f601f7, 0x0e0e121c, 0x6161a3c2, 0x35355f6a, 0x5757f9ae, 0xb9b9d069, 0x86869117, 0xc1c15899, 0x1d1d273a, 0x9e9eb927, 0xe1e138d9, 0xf8f813eb, 0x9898b32b, 0x11113322, 0x6969bbd2, 0xd9d970a9, 0x8e8e8907, 0x9494a733, 0x9b9bb62d, 0x1e1e223c, 0x87879215, 0xe9e920c9, 0xcece4987, 0x5555ffaa, 0x28287850, 0xdfdf7aa5, 0x8c8c8f03, 0xa1a1f859, 0x89898009, 0x0d0d171a, 0xbfbfda65, 0xe6e631d7, 0x4242c684, 0x6868b8d0, 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c]; - -// Transformations for decryption -var T5 = [0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f, 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6, 0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844, 0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd, 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4, 0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45, 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94, 0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7, 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a, 0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5, 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c, 0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1, 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a, 0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75, 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051, 0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46, 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff, 0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77, 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb, 0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000, 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e, 0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927, 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a, 0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e, 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16, 0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d, 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8, 0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd, 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34, 0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163, 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120, 0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d, 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0, 0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422, 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef, 0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36, 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4, 0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662, 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5, 0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3, 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b, 0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8, 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6, 0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6, 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0, 0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815, 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f, 0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df, 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f, 0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e, 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713, 0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89, 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c, 0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf, 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86, 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541, 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742]; -var T6 = [0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303, 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3, 0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9, 0xe7038f5f, 0x9515929c, 0xebbf6d7a, 0xda955259, 0x2dd4be83, 0xd3587421, 0x2949e069, 0x448ec9c8, 0x6a75c289, 0x78f48e79, 0x6b99583e, 0xdd27b971, 0xb6bee14f, 0x17f088ad, 0x66c920ac, 0xb47dce3a, 0x1863df4a, 0x82e51a31, 0x60975133, 0x4562537f, 0xe0b16477, 0x84bb6bae, 0x1cfe81a0, 0x94f9082b, 0x58704868, 0x198f45fd, 0x8794de6c, 0xb7527bf8, 0x23ab73d3, 0xe2724b02, 0x57e31f8f, 0x2a6655ab, 0x07b2eb28, 0x032fb5c2, 0x9a86c57b, 0xa5d33708, 0xf2302887, 0xb223bfa5, 0xba02036a, 0x5ced1682, 0x2b8acf1c, 0x92a779b4, 0xf0f307f2, 0xa14e69e2, 0xcd65daf4, 0xd50605be, 0x1fd13462, 0x8ac4a6fe, 0x9d342e53, 0xa0a2f355, 0x32058ae1, 0x75a4f6eb, 0x390b83ec, 0xaa4060ef, 0x065e719f, 0x51bd6e10, 0xf93e218a, 0x3d96dd06, 0xaedd3e05, 0x464de6bd, 0xb591548d, 0x0571c45d, 0x6f0406d4, 0xff605015, 0x241998fb, 0x97d6bde9, 0xcc894043, 0x7767d99e, 0xbdb0e842, 0x8807898b, 0x38e7195b, 0xdb79c8ee, 0x47a17c0a, 0xe97c420f, 0xc9f8841e, 0x00000000, 0x83098086, 0x48322bed, 0xac1e1170, 0x4e6c5a72, 0xfbfd0eff, 0x560f8538, 0x1e3daed5, 0x27362d39, 0x640a0fd9, 0x21685ca6, 0xd19b5b54, 0x3a24362e, 0xb10c0a67, 0x0f9357e7, 0xd2b4ee96, 0x9e1b9b91, 0x4f80c0c5, 0xa261dc20, 0x695a774b, 0x161c121a, 0x0ae293ba, 0xe5c0a02a, 0x433c22e0, 0x1d121b17, 0x0b0e090d, 0xadf28bc7, 0xb92db6a8, 0xc8141ea9, 0x8557f119, 0x4caf7507, 0xbbee99dd, 0xfda37f60, 0x9ff70126, 0xbc5c72f5, 0xc544663b, 0x345bfb7e, 0x768b4329, 0xdccb23c6, 0x68b6edfc, 0x63b8e4f1, 0xcad731dc, 0x10426385, 0x40139722, 0x2084c611, 0x7d854a24, 0xf8d2bb3d, 0x11aef932, 0x6dc729a1, 0x4b1d9e2f, 0xf3dcb230, 0xec0d8652, 0xd077c1e3, 0x6c2bb316, 0x99a970b9, 0xfa119448, 0x2247e964, 0xc4a8fc8c, 0x1aa0f03f, 0xd8567d2c, 0xef223390, 0xc787494e, 0xc1d938d1, 0xfe8ccaa2, 0x3698d40b, 0xcfa6f581, 0x28a57ade, 0x26dab78e, 0xa43fadbf, 0xe42c3a9d, 0x0d507892, 0x9b6a5fcc, 0x62547e46, 0xc2f68d13, 0xe890d8b8, 0x5e2e39f7, 0xf582c3af, 0xbe9f5d80, 0x7c69d093, 0xa96fd52d, 0xb3cf2512, 0x3bc8ac99, 0xa710187d, 0x6ee89c63, 0x7bdb3bbb, 0x09cd2678, 0xf46e5918, 0x01ec9ab7, 0xa8834f9a, 0x65e6956e, 0x7eaaffe6, 0x0821bccf, 0xe6ef15e8, 0xd9bae79b, 0xce4a6f36, 0xd4ea9f09, 0xd629b07c, 0xaf31a4b2, 0x312a3f23, 0x30c6a594, 0xc035a266, 0x37744ebc, 0xa6fc82ca, 0xb0e090d0, 0x1533a7d8, 0x4af10498, 0xf741ecda, 0x0e7fcd50, 0x2f1791f6, 0x8d764dd6, 0x4d43efb0, 0x54ccaa4d, 0xdfe49604, 0xe39ed1b5, 0x1b4c6a88, 0xb8c12c1f, 0x7f466551, 0x049d5eea, 0x5d018c35, 0x73fa8774, 0x2efb0b41, 0x5ab3671d, 0x5292dbd2, 0x33e91056, 0x136dd647, 0x8c9ad761, 0x7a37a10c, 0x8e59f814, 0x89eb133c, 0xeecea927, 0x35b761c9, 0xede11ce5, 0x3c7a47b1, 0x599cd2df, 0x3f55f273, 0x791814ce, 0xbf73c737, 0xea53f7cd, 0x5b5ffdaa, 0x14df3d6f, 0x867844db, 0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340, 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95, 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857]; -var T7 = [0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3, 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562, 0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3, 0x5fe7038f, 0x9c951592, 0x7aebbf6d, 0x59da9552, 0x832dd4be, 0x21d35874, 0x692949e0, 0xc8448ec9, 0x896a75c2, 0x7978f48e, 0x3e6b9958, 0x71dd27b9, 0x4fb6bee1, 0xad17f088, 0xac66c920, 0x3ab47dce, 0x4a1863df, 0x3182e51a, 0x33609751, 0x7f456253, 0x77e0b164, 0xae84bb6b, 0xa01cfe81, 0x2b94f908, 0x68587048, 0xfd198f45, 0x6c8794de, 0xf8b7527b, 0xd323ab73, 0x02e2724b, 0x8f57e31f, 0xab2a6655, 0x2807b2eb, 0xc2032fb5, 0x7b9a86c5, 0x08a5d337, 0x87f23028, 0xa5b223bf, 0x6aba0203, 0x825ced16, 0x1c2b8acf, 0xb492a779, 0xf2f0f307, 0xe2a14e69, 0xf4cd65da, 0xbed50605, 0x621fd134, 0xfe8ac4a6, 0x539d342e, 0x55a0a2f3, 0xe132058a, 0xeb75a4f6, 0xec390b83, 0xefaa4060, 0x9f065e71, 0x1051bd6e, 0x8af93e21, 0x063d96dd, 0x05aedd3e, 0xbd464de6, 0x8db59154, 0x5d0571c4, 0xd46f0406, 0x15ff6050, 0xfb241998, 0xe997d6bd, 0x43cc8940, 0x9e7767d9, 0x42bdb0e8, 0x8b880789, 0x5b38e719, 0xeedb79c8, 0x0a47a17c, 0x0fe97c42, 0x1ec9f884, 0x00000000, 0x86830980, 0xed48322b, 0x70ac1e11, 0x724e6c5a, 0xfffbfd0e, 0x38560f85, 0xd51e3dae, 0x3927362d, 0xd9640a0f, 0xa621685c, 0x54d19b5b, 0x2e3a2436, 0x67b10c0a, 0xe70f9357, 0x96d2b4ee, 0x919e1b9b, 0xc54f80c0, 0x20a261dc, 0x4b695a77, 0x1a161c12, 0xba0ae293, 0x2ae5c0a0, 0xe0433c22, 0x171d121b, 0x0d0b0e09, 0xc7adf28b, 0xa8b92db6, 0xa9c8141e, 0x198557f1, 0x074caf75, 0xddbbee99, 0x60fda37f, 0x269ff701, 0xf5bc5c72, 0x3bc54466, 0x7e345bfb, 0x29768b43, 0xc6dccb23, 0xfc68b6ed, 0xf163b8e4, 0xdccad731, 0x85104263, 0x22401397, 0x112084c6, 0x247d854a, 0x3df8d2bb, 0x3211aef9, 0xa16dc729, 0x2f4b1d9e, 0x30f3dcb2, 0x52ec0d86, 0xe3d077c1, 0x166c2bb3, 0xb999a970, 0x48fa1194, 0x642247e9, 0x8cc4a8fc, 0x3f1aa0f0, 0x2cd8567d, 0x90ef2233, 0x4ec78749, 0xd1c1d938, 0xa2fe8cca, 0x0b3698d4, 0x81cfa6f5, 0xde28a57a, 0x8e26dab7, 0xbfa43fad, 0x9de42c3a, 0x920d5078, 0xcc9b6a5f, 0x4662547e, 0x13c2f68d, 0xb8e890d8, 0xf75e2e39, 0xaff582c3, 0x80be9f5d, 0x937c69d0, 0x2da96fd5, 0x12b3cf25, 0x993bc8ac, 0x7da71018, 0x636ee89c, 0xbb7bdb3b, 0x7809cd26, 0x18f46e59, 0xb701ec9a, 0x9aa8834f, 0x6e65e695, 0xe67eaaff, 0xcf0821bc, 0xe8e6ef15, 0x9bd9bae7, 0x36ce4a6f, 0x09d4ea9f, 0x7cd629b0, 0xb2af31a4, 0x23312a3f, 0x9430c6a5, 0x66c035a2, 0xbc37744e, 0xcaa6fc82, 0xd0b0e090, 0xd81533a7, 0x984af104, 0xdaf741ec, 0x500e7fcd, 0xf62f1791, 0xd68d764d, 0xb04d43ef, 0x4d54ccaa, 0x04dfe496, 0xb5e39ed1, 0x881b4c6a, 0x1fb8c12c, 0x517f4665, 0xea049d5e, 0x355d018c, 0x7473fa87, 0x412efb0b, 0x1d5ab367, 0xd25292db, 0x5633e910, 0x47136dd6, 0x618c9ad7, 0x0c7a37a1, 0x148e59f8, 0x3c89eb13, 0x27eecea9, 0xc935b761, 0xe5ede11c, 0xb13c7a47, 0xdf599cd2, 0x733f55f2, 0xce791814, 0x37bf73c7, 0xcdea53f7, 0xaa5b5ffd, 0x6f14df3d, 0xdb867844, 0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3, 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d, 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8]; -var T8 = [0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b, 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5, 0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b, 0x8f5fe703, 0x929c9515, 0x6d7aebbf, 0x5259da95, 0xbe832dd4, 0x7421d358, 0xe0692949, 0xc9c8448e, 0xc2896a75, 0x8e7978f4, 0x583e6b99, 0xb971dd27, 0xe14fb6be, 0x88ad17f0, 0x20ac66c9, 0xce3ab47d, 0xdf4a1863, 0x1a3182e5, 0x51336097, 0x537f4562, 0x6477e0b1, 0x6bae84bb, 0x81a01cfe, 0x082b94f9, 0x48685870, 0x45fd198f, 0xde6c8794, 0x7bf8b752, 0x73d323ab, 0x4b02e272, 0x1f8f57e3, 0x55ab2a66, 0xeb2807b2, 0xb5c2032f, 0xc57b9a86, 0x3708a5d3, 0x2887f230, 0xbfa5b223, 0x036aba02, 0x16825ced, 0xcf1c2b8a, 0x79b492a7, 0x07f2f0f3, 0x69e2a14e, 0xdaf4cd65, 0x05bed506, 0x34621fd1, 0xa6fe8ac4, 0x2e539d34, 0xf355a0a2, 0x8ae13205, 0xf6eb75a4, 0x83ec390b, 0x60efaa40, 0x719f065e, 0x6e1051bd, 0x218af93e, 0xdd063d96, 0x3e05aedd, 0xe6bd464d, 0x548db591, 0xc45d0571, 0x06d46f04, 0x5015ff60, 0x98fb2419, 0xbde997d6, 0x4043cc89, 0xd99e7767, 0xe842bdb0, 0x898b8807, 0x195b38e7, 0xc8eedb79, 0x7c0a47a1, 0x420fe97c, 0x841ec9f8, 0x00000000, 0x80868309, 0x2bed4832, 0x1170ac1e, 0x5a724e6c, 0x0efffbfd, 0x8538560f, 0xaed51e3d, 0x2d392736, 0x0fd9640a, 0x5ca62168, 0x5b54d19b, 0x362e3a24, 0x0a67b10c, 0x57e70f93, 0xee96d2b4, 0x9b919e1b, 0xc0c54f80, 0xdc20a261, 0x774b695a, 0x121a161c, 0x93ba0ae2, 0xa02ae5c0, 0x22e0433c, 0x1b171d12, 0x090d0b0e, 0x8bc7adf2, 0xb6a8b92d, 0x1ea9c814, 0xf1198557, 0x75074caf, 0x99ddbbee, 0x7f60fda3, 0x01269ff7, 0x72f5bc5c, 0x663bc544, 0xfb7e345b, 0x4329768b, 0x23c6dccb, 0xedfc68b6, 0xe4f163b8, 0x31dccad7, 0x63851042, 0x97224013, 0xc6112084, 0x4a247d85, 0xbb3df8d2, 0xf93211ae, 0x29a16dc7, 0x9e2f4b1d, 0xb230f3dc, 0x8652ec0d, 0xc1e3d077, 0xb3166c2b, 0x70b999a9, 0x9448fa11, 0xe9642247, 0xfc8cc4a8, 0xf03f1aa0, 0x7d2cd856, 0x3390ef22, 0x494ec787, 0x38d1c1d9, 0xcaa2fe8c, 0xd40b3698, 0xf581cfa6, 0x7ade28a5, 0xb78e26da, 0xadbfa43f, 0x3a9de42c, 0x78920d50, 0x5fcc9b6a, 0x7e466254, 0x8d13c2f6, 0xd8b8e890, 0x39f75e2e, 0xc3aff582, 0x5d80be9f, 0xd0937c69, 0xd52da96f, 0x2512b3cf, 0xac993bc8, 0x187da710, 0x9c636ee8, 0x3bbb7bdb, 0x267809cd, 0x5918f46e, 0x9ab701ec, 0x4f9aa883, 0x956e65e6, 0xffe67eaa, 0xbccf0821, 0x15e8e6ef, 0xe79bd9ba, 0x6f36ce4a, 0x9f09d4ea, 0xb07cd629, 0xa4b2af31, 0x3f23312a, 0xa59430c6, 0xa266c035, 0x4ebc3774, 0x82caa6fc, 0x90d0b0e0, 0xa7d81533, 0x04984af1, 0xecdaf741, 0xcd500e7f, 0x91f62f17, 0x4dd68d76, 0xefb04d43, 0xaa4d54cc, 0x9604dfe4, 0xd1b5e39e, 0x6a881b4c, 0x2c1fb8c1, 0x65517f46, 0x5eea049d, 0x8c355d01, 0x877473fa, 0x0b412efb, 0x671d5ab3, 0xdbd25292, 0x105633e9, 0xd647136d, 0xd7618c9a, 0xa10c7a37, 0xf8148e59, 0x133c89eb, 0xa927eece, 0x61c935b7, 0x1ce5ede1, 0x47b13c7a, 0xd2df599c, 0xf2733f55, 0x14ce7918, 0xc737bf73, 0xf7cdea53, 0xfdaa5b5f, 0x3d6f14df, 0x44db8678, 0xaff381ca, 0x68c43eb9, 0x24342c38, 0xa3405fc2, 0x1dc37216, 0xe2250cbc, 0x3c498b28, 0x0d9541ff, 0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0]; - -// Transformations for decryption key expansion -var U1 = [0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927, 0x24362e3a, 0x2a3f2331, 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45, 0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, 0xe090d0b0, 0xee99ddbb, 0xfc82caa6, 0xf28bc7ad, 0xd8b4e49c, 0xd6bde997, 0xc4a6fe8a, 0xcaaff381, 0x90d8b8e8, 0x9ed1b5e3, 0x8ccaa2fe, 0x82c3aff5, 0xa8fc8cc4, 0xa6f581cf, 0xb4ee96d2, 0xbae79bd9, 0xdb3bbb7b, 0xd532b670, 0xc729a16d, 0xc920ac66, 0xe31f8f57, 0xed16825c, 0xff0d9541, 0xf104984a, 0xab73d323, 0xa57ade28, 0xb761c935, 0xb968c43e, 0x9357e70f, 0x9d5eea04, 0x8f45fd19, 0x814cf012, 0x3bab6bcb, 0x35a266c0, 0x27b971dd, 0x29b07cd6, 0x038f5fe7, 0x0d8652ec, 0x1f9d45f1, 0x119448fa, 0x4be30393, 0x45ea0e98, 0x57f11985, 0x59f8148e, 0x73c737bf, 0x7dce3ab4, 0x6fd52da9, 0x61dc20a2, 0xad766df6, 0xa37f60fd, 0xb16477e0, 0xbf6d7aeb, 0x955259da, 0x9b5b54d1, 0x894043cc, 0x87494ec7, 0xdd3e05ae, 0xd33708a5, 0xc12c1fb8, 0xcf2512b3, 0xe51a3182, 0xeb133c89, 0xf9082b94, 0xf701269f, 0x4de6bd46, 0x43efb04d, 0x51f4a750, 0x5ffdaa5b, 0x75c2896a, 0x7bcb8461, 0x69d0937c, 0x67d99e77, 0x3daed51e, 0x33a7d815, 0x21bccf08, 0x2fb5c203, 0x058ae132, 0x0b83ec39, 0x1998fb24, 0x1791f62f, 0x764dd68d, 0x7844db86, 0x6a5fcc9b, 0x6456c190, 0x4e69e2a1, 0x4060efaa, 0x527bf8b7, 0x5c72f5bc, 0x0605bed5, 0x080cb3de, 0x1a17a4c3, 0x141ea9c8, 0x3e218af9, 0x302887f2, 0x223390ef, 0x2c3a9de4, 0x96dd063d, 0x98d40b36, 0x8acf1c2b, 0x84c61120, 0xaef93211, 0xa0f03f1a, 0xb2eb2807, 0xbce2250c, 0xe6956e65, 0xe89c636e, 0xfa877473, 0xf48e7978, 0xdeb15a49, 0xd0b85742, 0xc2a3405f, 0xccaa4d54, 0x41ecdaf7, 0x4fe5d7fc, 0x5dfec0e1, 0x53f7cdea, 0x79c8eedb, 0x77c1e3d0, 0x65daf4cd, 0x6bd3f9c6, 0x31a4b2af, 0x3fadbfa4, 0x2db6a8b9, 0x23bfa5b2, 0x09808683, 0x07898b88, 0x15929c95, 0x1b9b919e, 0xa17c0a47, 0xaf75074c, 0xbd6e1051, 0xb3671d5a, 0x99583e6b, 0x97513360, 0x854a247d, 0x8b432976, 0xd134621f, 0xdf3d6f14, 0xcd267809, 0xc32f7502, 0xe9105633, 0xe7195b38, 0xf5024c25, 0xfb0b412e, 0x9ad7618c, 0x94de6c87, 0x86c57b9a, 0x88cc7691, 0xa2f355a0, 0xacfa58ab, 0xbee14fb6, 0xb0e842bd, 0xea9f09d4, 0xe49604df, 0xf68d13c2, 0xf8841ec9, 0xd2bb3df8, 0xdcb230f3, 0xcea927ee, 0xc0a02ae5, 0x7a47b13c, 0x744ebc37, 0x6655ab2a, 0x685ca621, 0x42638510, 0x4c6a881b, 0x5e719f06, 0x5078920d, 0x0a0fd964, 0x0406d46f, 0x161dc372, 0x1814ce79, 0x322bed48, 0x3c22e043, 0x2e39f75e, 0x2030fa55, 0xec9ab701, 0xe293ba0a, 0xf088ad17, 0xfe81a01c, 0xd4be832d, 0xdab78e26, 0xc8ac993b, 0xc6a59430, 0x9cd2df59, 0x92dbd252, 0x80c0c54f, 0x8ec9c844, 0xa4f6eb75, 0xaaffe67e, 0xb8e4f163, 0xb6edfc68, 0x0c0a67b1, 0x02036aba, 0x10187da7, 0x1e1170ac, 0x342e539d, 0x3a275e96, 0x283c498b, 0x26354480, 0x7c420fe9, 0x724b02e2, 0x605015ff, 0x6e5918f4, 0x44663bc5, 0x4a6f36ce, 0x587421d3, 0x567d2cd8, 0x37a10c7a, 0x39a80171, 0x2bb3166c, 0x25ba1b67, 0x0f853856, 0x018c355d, 0x13972240, 0x1d9e2f4b, 0x47e96422, 0x49e06929, 0x5bfb7e34, 0x55f2733f, 0x7fcd500e, 0x71c45d05, 0x63df4a18, 0x6dd64713, 0xd731dcca, 0xd938d1c1, 0xcb23c6dc, 0xc52acbd7, 0xef15e8e6, 0xe11ce5ed, 0xf307f2f0, 0xfd0efffb, 0xa779b492, 0xa970b999, 0xbb6bae84, 0xb562a38f, 0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3]; -var U2 = [0x00000000, 0x0b0e090d, 0x161c121a, 0x1d121b17, 0x2c382434, 0x27362d39, 0x3a24362e, 0x312a3f23, 0x58704868, 0x537e4165, 0x4e6c5a72, 0x4562537f, 0x74486c5c, 0x7f466551, 0x62547e46, 0x695a774b, 0xb0e090d0, 0xbbee99dd, 0xa6fc82ca, 0xadf28bc7, 0x9cd8b4e4, 0x97d6bde9, 0x8ac4a6fe, 0x81caaff3, 0xe890d8b8, 0xe39ed1b5, 0xfe8ccaa2, 0xf582c3af, 0xc4a8fc8c, 0xcfa6f581, 0xd2b4ee96, 0xd9bae79b, 0x7bdb3bbb, 0x70d532b6, 0x6dc729a1, 0x66c920ac, 0x57e31f8f, 0x5ced1682, 0x41ff0d95, 0x4af10498, 0x23ab73d3, 0x28a57ade, 0x35b761c9, 0x3eb968c4, 0x0f9357e7, 0x049d5eea, 0x198f45fd, 0x12814cf0, 0xcb3bab6b, 0xc035a266, 0xdd27b971, 0xd629b07c, 0xe7038f5f, 0xec0d8652, 0xf11f9d45, 0xfa119448, 0x934be303, 0x9845ea0e, 0x8557f119, 0x8e59f814, 0xbf73c737, 0xb47dce3a, 0xa96fd52d, 0xa261dc20, 0xf6ad766d, 0xfda37f60, 0xe0b16477, 0xebbf6d7a, 0xda955259, 0xd19b5b54, 0xcc894043, 0xc787494e, 0xaedd3e05, 0xa5d33708, 0xb8c12c1f, 0xb3cf2512, 0x82e51a31, 0x89eb133c, 0x94f9082b, 0x9ff70126, 0x464de6bd, 0x4d43efb0, 0x5051f4a7, 0x5b5ffdaa, 0x6a75c289, 0x617bcb84, 0x7c69d093, 0x7767d99e, 0x1e3daed5, 0x1533a7d8, 0x0821bccf, 0x032fb5c2, 0x32058ae1, 0x390b83ec, 0x241998fb, 0x2f1791f6, 0x8d764dd6, 0x867844db, 0x9b6a5fcc, 0x906456c1, 0xa14e69e2, 0xaa4060ef, 0xb7527bf8, 0xbc5c72f5, 0xd50605be, 0xde080cb3, 0xc31a17a4, 0xc8141ea9, 0xf93e218a, 0xf2302887, 0xef223390, 0xe42c3a9d, 0x3d96dd06, 0x3698d40b, 0x2b8acf1c, 0x2084c611, 0x11aef932, 0x1aa0f03f, 0x07b2eb28, 0x0cbce225, 0x65e6956e, 0x6ee89c63, 0x73fa8774, 0x78f48e79, 0x49deb15a, 0x42d0b857, 0x5fc2a340, 0x54ccaa4d, 0xf741ecda, 0xfc4fe5d7, 0xe15dfec0, 0xea53f7cd, 0xdb79c8ee, 0xd077c1e3, 0xcd65daf4, 0xc66bd3f9, 0xaf31a4b2, 0xa43fadbf, 0xb92db6a8, 0xb223bfa5, 0x83098086, 0x8807898b, 0x9515929c, 0x9e1b9b91, 0x47a17c0a, 0x4caf7507, 0x51bd6e10, 0x5ab3671d, 0x6b99583e, 0x60975133, 0x7d854a24, 0x768b4329, 0x1fd13462, 0x14df3d6f, 0x09cd2678, 0x02c32f75, 0x33e91056, 0x38e7195b, 0x25f5024c, 0x2efb0b41, 0x8c9ad761, 0x8794de6c, 0x9a86c57b, 0x9188cc76, 0xa0a2f355, 0xabacfa58, 0xb6bee14f, 0xbdb0e842, 0xd4ea9f09, 0xdfe49604, 0xc2f68d13, 0xc9f8841e, 0xf8d2bb3d, 0xf3dcb230, 0xeecea927, 0xe5c0a02a, 0x3c7a47b1, 0x37744ebc, 0x2a6655ab, 0x21685ca6, 0x10426385, 0x1b4c6a88, 0x065e719f, 0x0d507892, 0x640a0fd9, 0x6f0406d4, 0x72161dc3, 0x791814ce, 0x48322bed, 0x433c22e0, 0x5e2e39f7, 0x552030fa, 0x01ec9ab7, 0x0ae293ba, 0x17f088ad, 0x1cfe81a0, 0x2dd4be83, 0x26dab78e, 0x3bc8ac99, 0x30c6a594, 0x599cd2df, 0x5292dbd2, 0x4f80c0c5, 0x448ec9c8, 0x75a4f6eb, 0x7eaaffe6, 0x63b8e4f1, 0x68b6edfc, 0xb10c0a67, 0xba02036a, 0xa710187d, 0xac1e1170, 0x9d342e53, 0x963a275e, 0x8b283c49, 0x80263544, 0xe97c420f, 0xe2724b02, 0xff605015, 0xf46e5918, 0xc544663b, 0xce4a6f36, 0xd3587421, 0xd8567d2c, 0x7a37a10c, 0x7139a801, 0x6c2bb316, 0x6725ba1b, 0x560f8538, 0x5d018c35, 0x40139722, 0x4b1d9e2f, 0x2247e964, 0x2949e069, 0x345bfb7e, 0x3f55f273, 0x0e7fcd50, 0x0571c45d, 0x1863df4a, 0x136dd647, 0xcad731dc, 0xc1d938d1, 0xdccb23c6, 0xd7c52acb, 0xe6ef15e8, 0xede11ce5, 0xf0f307f2, 0xfbfd0eff, 0x92a779b4, 0x99a970b9, 0x84bb6bae, 0x8fb562a3, 0xbe9f5d80, 0xb591548d, 0xa8834f9a, 0xa38d4697]; -var U3 = [0x00000000, 0x0d0b0e09, 0x1a161c12, 0x171d121b, 0x342c3824, 0x3927362d, 0x2e3a2436, 0x23312a3f, 0x68587048, 0x65537e41, 0x724e6c5a, 0x7f456253, 0x5c74486c, 0x517f4665, 0x4662547e, 0x4b695a77, 0xd0b0e090, 0xddbbee99, 0xcaa6fc82, 0xc7adf28b, 0xe49cd8b4, 0xe997d6bd, 0xfe8ac4a6, 0xf381caaf, 0xb8e890d8, 0xb5e39ed1, 0xa2fe8cca, 0xaff582c3, 0x8cc4a8fc, 0x81cfa6f5, 0x96d2b4ee, 0x9bd9bae7, 0xbb7bdb3b, 0xb670d532, 0xa16dc729, 0xac66c920, 0x8f57e31f, 0x825ced16, 0x9541ff0d, 0x984af104, 0xd323ab73, 0xde28a57a, 0xc935b761, 0xc43eb968, 0xe70f9357, 0xea049d5e, 0xfd198f45, 0xf012814c, 0x6bcb3bab, 0x66c035a2, 0x71dd27b9, 0x7cd629b0, 0x5fe7038f, 0x52ec0d86, 0x45f11f9d, 0x48fa1194, 0x03934be3, 0x0e9845ea, 0x198557f1, 0x148e59f8, 0x37bf73c7, 0x3ab47dce, 0x2da96fd5, 0x20a261dc, 0x6df6ad76, 0x60fda37f, 0x77e0b164, 0x7aebbf6d, 0x59da9552, 0x54d19b5b, 0x43cc8940, 0x4ec78749, 0x05aedd3e, 0x08a5d337, 0x1fb8c12c, 0x12b3cf25, 0x3182e51a, 0x3c89eb13, 0x2b94f908, 0x269ff701, 0xbd464de6, 0xb04d43ef, 0xa75051f4, 0xaa5b5ffd, 0x896a75c2, 0x84617bcb, 0x937c69d0, 0x9e7767d9, 0xd51e3dae, 0xd81533a7, 0xcf0821bc, 0xc2032fb5, 0xe132058a, 0xec390b83, 0xfb241998, 0xf62f1791, 0xd68d764d, 0xdb867844, 0xcc9b6a5f, 0xc1906456, 0xe2a14e69, 0xefaa4060, 0xf8b7527b, 0xf5bc5c72, 0xbed50605, 0xb3de080c, 0xa4c31a17, 0xa9c8141e, 0x8af93e21, 0x87f23028, 0x90ef2233, 0x9de42c3a, 0x063d96dd, 0x0b3698d4, 0x1c2b8acf, 0x112084c6, 0x3211aef9, 0x3f1aa0f0, 0x2807b2eb, 0x250cbce2, 0x6e65e695, 0x636ee89c, 0x7473fa87, 0x7978f48e, 0x5a49deb1, 0x5742d0b8, 0x405fc2a3, 0x4d54ccaa, 0xdaf741ec, 0xd7fc4fe5, 0xc0e15dfe, 0xcdea53f7, 0xeedb79c8, 0xe3d077c1, 0xf4cd65da, 0xf9c66bd3, 0xb2af31a4, 0xbfa43fad, 0xa8b92db6, 0xa5b223bf, 0x86830980, 0x8b880789, 0x9c951592, 0x919e1b9b, 0x0a47a17c, 0x074caf75, 0x1051bd6e, 0x1d5ab367, 0x3e6b9958, 0x33609751, 0x247d854a, 0x29768b43, 0x621fd134, 0x6f14df3d, 0x7809cd26, 0x7502c32f, 0x5633e910, 0x5b38e719, 0x4c25f502, 0x412efb0b, 0x618c9ad7, 0x6c8794de, 0x7b9a86c5, 0x769188cc, 0x55a0a2f3, 0x58abacfa, 0x4fb6bee1, 0x42bdb0e8, 0x09d4ea9f, 0x04dfe496, 0x13c2f68d, 0x1ec9f884, 0x3df8d2bb, 0x30f3dcb2, 0x27eecea9, 0x2ae5c0a0, 0xb13c7a47, 0xbc37744e, 0xab2a6655, 0xa621685c, 0x85104263, 0x881b4c6a, 0x9f065e71, 0x920d5078, 0xd9640a0f, 0xd46f0406, 0xc372161d, 0xce791814, 0xed48322b, 0xe0433c22, 0xf75e2e39, 0xfa552030, 0xb701ec9a, 0xba0ae293, 0xad17f088, 0xa01cfe81, 0x832dd4be, 0x8e26dab7, 0x993bc8ac, 0x9430c6a5, 0xdf599cd2, 0xd25292db, 0xc54f80c0, 0xc8448ec9, 0xeb75a4f6, 0xe67eaaff, 0xf163b8e4, 0xfc68b6ed, 0x67b10c0a, 0x6aba0203, 0x7da71018, 0x70ac1e11, 0x539d342e, 0x5e963a27, 0x498b283c, 0x44802635, 0x0fe97c42, 0x02e2724b, 0x15ff6050, 0x18f46e59, 0x3bc54466, 0x36ce4a6f, 0x21d35874, 0x2cd8567d, 0x0c7a37a1, 0x017139a8, 0x166c2bb3, 0x1b6725ba, 0x38560f85, 0x355d018c, 0x22401397, 0x2f4b1d9e, 0x642247e9, 0x692949e0, 0x7e345bfb, 0x733f55f2, 0x500e7fcd, 0x5d0571c4, 0x4a1863df, 0x47136dd6, 0xdccad731, 0xd1c1d938, 0xc6dccb23, 0xcbd7c52a, 0xe8e6ef15, 0xe5ede11c, 0xf2f0f307, 0xfffbfd0e, 0xb492a779, 0xb999a970, 0xae84bb6b, 0xa38fb562, 0x80be9f5d, 0x8db59154, 0x9aa8834f, 0x97a38d46]; -var U4 = [0x00000000, 0x090d0b0e, 0x121a161c, 0x1b171d12, 0x24342c38, 0x2d392736, 0x362e3a24, 0x3f23312a, 0x48685870, 0x4165537e, 0x5a724e6c, 0x537f4562, 0x6c5c7448, 0x65517f46, 0x7e466254, 0x774b695a, 0x90d0b0e0, 0x99ddbbee, 0x82caa6fc, 0x8bc7adf2, 0xb4e49cd8, 0xbde997d6, 0xa6fe8ac4, 0xaff381ca, 0xd8b8e890, 0xd1b5e39e, 0xcaa2fe8c, 0xc3aff582, 0xfc8cc4a8, 0xf581cfa6, 0xee96d2b4, 0xe79bd9ba, 0x3bbb7bdb, 0x32b670d5, 0x29a16dc7, 0x20ac66c9, 0x1f8f57e3, 0x16825ced, 0x0d9541ff, 0x04984af1, 0x73d323ab, 0x7ade28a5, 0x61c935b7, 0x68c43eb9, 0x57e70f93, 0x5eea049d, 0x45fd198f, 0x4cf01281, 0xab6bcb3b, 0xa266c035, 0xb971dd27, 0xb07cd629, 0x8f5fe703, 0x8652ec0d, 0x9d45f11f, 0x9448fa11, 0xe303934b, 0xea0e9845, 0xf1198557, 0xf8148e59, 0xc737bf73, 0xce3ab47d, 0xd52da96f, 0xdc20a261, 0x766df6ad, 0x7f60fda3, 0x6477e0b1, 0x6d7aebbf, 0x5259da95, 0x5b54d19b, 0x4043cc89, 0x494ec787, 0x3e05aedd, 0x3708a5d3, 0x2c1fb8c1, 0x2512b3cf, 0x1a3182e5, 0x133c89eb, 0x082b94f9, 0x01269ff7, 0xe6bd464d, 0xefb04d43, 0xf4a75051, 0xfdaa5b5f, 0xc2896a75, 0xcb84617b, 0xd0937c69, 0xd99e7767, 0xaed51e3d, 0xa7d81533, 0xbccf0821, 0xb5c2032f, 0x8ae13205, 0x83ec390b, 0x98fb2419, 0x91f62f17, 0x4dd68d76, 0x44db8678, 0x5fcc9b6a, 0x56c19064, 0x69e2a14e, 0x60efaa40, 0x7bf8b752, 0x72f5bc5c, 0x05bed506, 0x0cb3de08, 0x17a4c31a, 0x1ea9c814, 0x218af93e, 0x2887f230, 0x3390ef22, 0x3a9de42c, 0xdd063d96, 0xd40b3698, 0xcf1c2b8a, 0xc6112084, 0xf93211ae, 0xf03f1aa0, 0xeb2807b2, 0xe2250cbc, 0x956e65e6, 0x9c636ee8, 0x877473fa, 0x8e7978f4, 0xb15a49de, 0xb85742d0, 0xa3405fc2, 0xaa4d54cc, 0xecdaf741, 0xe5d7fc4f, 0xfec0e15d, 0xf7cdea53, 0xc8eedb79, 0xc1e3d077, 0xdaf4cd65, 0xd3f9c66b, 0xa4b2af31, 0xadbfa43f, 0xb6a8b92d, 0xbfa5b223, 0x80868309, 0x898b8807, 0x929c9515, 0x9b919e1b, 0x7c0a47a1, 0x75074caf, 0x6e1051bd, 0x671d5ab3, 0x583e6b99, 0x51336097, 0x4a247d85, 0x4329768b, 0x34621fd1, 0x3d6f14df, 0x267809cd, 0x2f7502c3, 0x105633e9, 0x195b38e7, 0x024c25f5, 0x0b412efb, 0xd7618c9a, 0xde6c8794, 0xc57b9a86, 0xcc769188, 0xf355a0a2, 0xfa58abac, 0xe14fb6be, 0xe842bdb0, 0x9f09d4ea, 0x9604dfe4, 0x8d13c2f6, 0x841ec9f8, 0xbb3df8d2, 0xb230f3dc, 0xa927eece, 0xa02ae5c0, 0x47b13c7a, 0x4ebc3774, 0x55ab2a66, 0x5ca62168, 0x63851042, 0x6a881b4c, 0x719f065e, 0x78920d50, 0x0fd9640a, 0x06d46f04, 0x1dc37216, 0x14ce7918, 0x2bed4832, 0x22e0433c, 0x39f75e2e, 0x30fa5520, 0x9ab701ec, 0x93ba0ae2, 0x88ad17f0, 0x81a01cfe, 0xbe832dd4, 0xb78e26da, 0xac993bc8, 0xa59430c6, 0xd2df599c, 0xdbd25292, 0xc0c54f80, 0xc9c8448e, 0xf6eb75a4, 0xffe67eaa, 0xe4f163b8, 0xedfc68b6, 0x0a67b10c, 0x036aba02, 0x187da710, 0x1170ac1e, 0x2e539d34, 0x275e963a, 0x3c498b28, 0x35448026, 0x420fe97c, 0x4b02e272, 0x5015ff60, 0x5918f46e, 0x663bc544, 0x6f36ce4a, 0x7421d358, 0x7d2cd856, 0xa10c7a37, 0xa8017139, 0xb3166c2b, 0xba1b6725, 0x8538560f, 0x8c355d01, 0x97224013, 0x9e2f4b1d, 0xe9642247, 0xe0692949, 0xfb7e345b, 0xf2733f55, 0xcd500e7f, 0xc45d0571, 0xdf4a1863, 0xd647136d, 0x31dccad7, 0x38d1c1d9, 0x23c6dccb, 0x2acbd7c5, 0x15e8e6ef, 0x1ce5ede1, 0x07f2f0f3, 0x0efffbfd, 0x79b492a7, 0x70b999a9, 0x6bae84bb, 0x62a38fb5, 0x5d80be9f, 0x548db591, 0x4f9aa883, 0x4697a38d]; - -function convertToInt32(bytes) { - var result = []; - for (var i = 0; i < bytes.length; i += 4) { - result.push( - (bytes[i] << 24) | - (bytes[i + 1] << 16) | - (bytes[i + 2] << 8) | - bytes[i + 3] - ); - } - return result; -} - -var AES = function (key) { - if (!(this instanceof AES)) { - throw Error('AES must be instanitated with `new`'); + this._aes = new AES(key); } - Object.defineProperty(this, 'key', { - value: coerceArray(key, true) - }); + ModeOfOperationECB.prototype.encrypt = function (plaintext) { + plaintext = coerceArray(plaintext); - this._prepare(); -} + if ((plaintext.length % 16) !== 0) { + throw new Error('invalid plaintext size (must be multiple of 16 bytes)'); + } + var ciphertext = createArray(plaintext.length); + var block = createArray(16); -AES.prototype._prepare = function () { + for (var i = 0; i < plaintext.length; i += 16) { + copyArray(plaintext, block, 0, i, i + 16); + block = this._aes.encrypt(block); + copyArray(block, ciphertext, i); + } - var rounds = numberOfRounds[this.key.length]; - if (rounds == null) { - throw new Error('invalid key size (must be 16, 24 or 32 bytes)'); + return ciphertext; } - // encryption round keys - this._Ke = []; + ModeOfOperationECB.prototype.decrypt = function (ciphertext) { + ciphertext = coerceArray(ciphertext); - // decryption round keys - this._Kd = []; + if ((ciphertext.length % 16) !== 0) { + throw new Error('invalid ciphertext size (must be multiple of 16 bytes)'); + } - for (var i = 0; i <= rounds; i++) { - this._Ke.push([0, 0, 0, 0]); - this._Kd.push([0, 0, 0, 0]); + var plaintext = createArray(ciphertext.length); + var block = createArray(16); + + for (var i = 0; i < ciphertext.length; i += 16) { + copyArray(ciphertext, block, 0, i, i + 16); + block = this._aes.decrypt(block); + copyArray(block, plaintext, i); + } + + return plaintext; } - var roundKeyCount = (rounds + 1) * 4; - var KC = this.key.length / 4; - // convert the key into ints - var tk = convertToInt32(this.key); + /** + * Mode Of Operation - Cipher Block Chaining (CBC) + */ + var ModeOfOperationCBC = function (key, iv) { + if (!(this instanceof ModeOfOperationCBC)) { + throw Error('AES must be instanitated with `new`'); + } - // copy values into round key arrays - var index; - for (var i = 0; i < KC; i++) { - index = i >> 2; - this._Ke[index][i % 4] = tk[i]; - this._Kd[rounds - index][i % 4] = tk[i]; + this.description = "Cipher Block Chaining"; + this.name = "cbc"; + + if (!iv) { + iv = createArray(16); + + } else if (iv.length != 16) { + throw new Error('invalid initialation vector size (must be 16 bytes)'); + } + + this._lastCipherblock = coerceArray(iv, true); + + this._aes = new AES(key); } - // key expansion (fips-197 section 5.2) - var rconpointer = 0; - var t = KC, tt; - while (t < roundKeyCount) { - tt = tk[KC - 1]; - tk[0] ^= ((S[(tt >> 16) & 0xFF] << 24) ^ - (S[(tt >> 8) & 0xFF] << 16) ^ - (S[tt & 0xFF] << 8) ^ - S[(tt >> 24) & 0xFF] ^ - (rcon[rconpointer] << 24)); - rconpointer += 1; + ModeOfOperationCBC.prototype.encrypt = function (plaintext) { + plaintext = coerceArray(plaintext); - // key expansion (for non-256 bit) - if (KC != 8) { - for (var i = 1; i < KC; i++) { - tk[i] ^= tk[i - 1]; + if ((plaintext.length % 16) !== 0) { + throw new Error('invalid plaintext size (must be multiple of 16 bytes)'); + } + + var ciphertext = createArray(plaintext.length); + var block = createArray(16); + + for (var i = 0; i < plaintext.length; i += 16) { + copyArray(plaintext, block, 0, i, i + 16); + + for (var j = 0; j < 16; j++) { + block[j] ^= this._lastCipherblock[j]; } - // key expansion for 256-bit keys is "slightly different" (fips-197) + this._lastCipherblock = this._aes.encrypt(block); + copyArray(this._lastCipherblock, ciphertext, i); + } + + return ciphertext; + } + + ModeOfOperationCBC.prototype.decrypt = function (ciphertext) { + ciphertext = coerceArray(ciphertext); + + if ((ciphertext.length % 16) !== 0) { + throw new Error('invalid ciphertext size (must be multiple of 16 bytes)'); + } + + var plaintext = createArray(ciphertext.length); + var block = createArray(16); + + for (var i = 0; i < ciphertext.length; i += 16) { + copyArray(ciphertext, block, 0, i, i + 16); + block = this._aes.decrypt(block); + + for (var j = 0; j < 16; j++) { + plaintext[i + j] = block[j] ^ this._lastCipherblock[j]; + } + + copyArray(ciphertext, this._lastCipherblock, 0, i, i + 16); + } + + return plaintext; + } + + + /** + * Mode Of Operation - Cipher Feedback (CFB) + */ + var ModeOfOperationCFB = function (key, iv, segmentSize) { + if (!(this instanceof ModeOfOperationCFB)) { + throw Error('AES must be instanitated with `new`'); + } + + this.description = "Cipher Feedback"; + this.name = "cfb"; + + if (!iv) { + iv = createArray(16); + + } else if (iv.length != 16) { + throw new Error('invalid initialation vector size (must be 16 size)'); + } + + if (!segmentSize) { + segmentSize = 1; + } + + this.segmentSize = segmentSize; + + this._shiftRegister = coerceArray(iv, true); + + this._aes = new AES(key); + } + + ModeOfOperationCFB.prototype.encrypt = function (plaintext) { + if ((plaintext.length % this.segmentSize) != 0) { + throw new Error('invalid plaintext size (must be segmentSize bytes)'); + } + + var encrypted = coerceArray(plaintext, true); + + var xorSegment; + for (var i = 0; i < encrypted.length; i += this.segmentSize) { + xorSegment = this._aes.encrypt(this._shiftRegister); + for (var j = 0; j < this.segmentSize; j++) { + encrypted[i + j] ^= xorSegment[j]; + } + + // Shift the register + copyArray(this._shiftRegister, this._shiftRegister, 0, this.segmentSize); + copyArray(encrypted, this._shiftRegister, 16 - this.segmentSize, i, i + this.segmentSize); + } + + return encrypted; + } + + ModeOfOperationCFB.prototype.decrypt = function (ciphertext) { + if ((ciphertext.length % this.segmentSize) != 0) { + throw new Error('invalid ciphertext size (must be segmentSize bytes)'); + } + + var plaintext = coerceArray(ciphertext, true); + + var xorSegment; + for (var i = 0; i < plaintext.length; i += this.segmentSize) { + xorSegment = this._aes.encrypt(this._shiftRegister); + + for (var j = 0; j < this.segmentSize; j++) { + plaintext[i + j] ^= xorSegment[j]; + } + + // Shift the register + copyArray(this._shiftRegister, this._shiftRegister, 0, this.segmentSize); + copyArray(ciphertext, this._shiftRegister, 16 - this.segmentSize, i, i + this.segmentSize); + } + + return plaintext; + } + + /** + * Mode Of Operation - Output Feedback (OFB) + */ + var ModeOfOperationOFB = function (key, iv) { + if (!(this instanceof ModeOfOperationOFB)) { + throw Error('AES must be instanitated with `new`'); + } + + this.description = "Output Feedback"; + this.name = "ofb"; + + if (!iv) { + iv = createArray(16); + + } else if (iv.length != 16) { + throw new Error('invalid initialation vector size (must be 16 bytes)'); + } + + this._lastPrecipher = coerceArray(iv, true); + this._lastPrecipherIndex = 16; + + this._aes = new AES(key); + } + + ModeOfOperationOFB.prototype.encrypt = function (plaintext) { + var encrypted = coerceArray(plaintext, true); + + for (var i = 0; i < encrypted.length; i++) { + if (this._lastPrecipherIndex === 16) { + this._lastPrecipher = this._aes.encrypt(this._lastPrecipher); + this._lastPrecipherIndex = 0; + } + encrypted[i] ^= this._lastPrecipher[this._lastPrecipherIndex++]; + } + + return encrypted; + } + + // Decryption is symetric + ModeOfOperationOFB.prototype.decrypt = ModeOfOperationOFB.prototype.encrypt; + + + /** + * Counter object for CTR common mode of operation + */ + var Counter = function (initialValue) { + if (!(this instanceof Counter)) { + throw Error('Counter must be instanitated with `new`'); + } + + // We allow 0, but anything false-ish uses the default 1 + if (initialValue !== 0 && !initialValue) { + initialValue = 1; + } + + if (typeof (initialValue) === 'number') { + this._counter = createArray(16); + this.setValue(initialValue); + } else { - for (var i = 1; i < (KC / 2); i++) { - tk[i] ^= tk[i - 1]; + this.setBytes(initialValue); + } + } + + Counter.prototype.setValue = function (value) { + if (typeof (value) !== 'number' || parseInt(value) != value) { + throw new Error('invalid counter value (must be an integer)'); + } + + // We cannot safely handle numbers beyond the safe range for integers + if (value > Number.MAX_SAFE_INTEGER) { + throw new Error('integer value out of safe range'); + } + + for (var index = 15; index >= 0; --index) { + this._counter[index] = value % 256; + value = parseInt(value / 256); + } + } + + Counter.prototype.setBytes = function (bytes) { + bytes = coerceArray(bytes, true); + + if (bytes.length != 16) { + throw new Error('invalid counter bytes size (must be 16 bytes)'); + } + + this._counter = bytes; + }; + + Counter.prototype.increment = function () { + for (var i = 15; i >= 0; i--) { + if (this._counter[i] === 255) { + this._counter[i] = 0; + } else { + this._counter[i]++; + break; } - tt = tk[(KC / 2) - 1]; + } + } - tk[KC / 2] ^= (S[tt & 0xFF] ^ - (S[(tt >> 8) & 0xFF] << 8) ^ - (S[(tt >> 16) & 0xFF] << 16) ^ - (S[(tt >> 24) & 0xFF] << 24)); - for (var i = (KC / 2) + 1; i < KC; i++) { - tk[i] ^= tk[i - 1]; + /** + * Mode Of Operation - Counter (CTR) + */ + var ModeOfOperationCTR = function (key, counter) { + if (!(this instanceof ModeOfOperationCTR)) { + throw Error('AES must be instanitated with `new`'); + } + + this.description = "Counter"; + this.name = "ctr"; + + if (!(counter instanceof Counter)) { + counter = new Counter(counter) + } + + this._counter = counter; + + this._remainingCounter = null; + this._remainingCounterIndex = 16; + + this._aes = new AES(key); + } + + ModeOfOperationCTR.prototype.encrypt = function (plaintext) { + var encrypted = coerceArray(plaintext, true); + + for (var i = 0; i < encrypted.length; i++) { + if (this._remainingCounterIndex === 16) { + this._remainingCounter = this._aes.encrypt(this._counter._counter); + this._remainingCounterIndex = 0; + this._counter.increment(); + } + encrypted[i] ^= this._remainingCounter[this._remainingCounterIndex++]; + } + + return encrypted; + } + + // Decryption is symetric + ModeOfOperationCTR.prototype.decrypt = ModeOfOperationCTR.prototype.encrypt; + + + /////////////////////// + // Padding + + // See:https://tools.ietf.org/html/rfc2315 + function pkcs7pad(data) { + data = coerceArray(data, true); + var padder = 16 - (data.length % 16); + var result = createArray(data.length + padder); + copyArray(data, result); + for (var i = data.length; i < result.length; i++) { + result[i] = padder; + } + return result; + } + + function pkcs7strip(data) { + data = coerceArray(data, true); + if (data.length < 16) { + throw new Error('PKCS#7 invalid length'); + } + + var padder = data[data.length - 1]; + if (padder > 16) { + throw new Error('PKCS#7 padding byte out of range'); + } + + var length = data.length - padder; + for (var i = 0; i < padder; i++) { + if (data[length + i] !== padder) { + throw new Error('PKCS#7 invalid padding byte'); } } - // copy values into round key arrays - var i = 0, r, c; - while (i < KC && t < roundKeyCount) { - r = t >> 2; - c = t % 4; - this._Ke[r][c] = tk[i]; - this._Kd[rounds - r][c] = tk[i++]; - t++; + var result = createArray(length); + copyArray(data, result, 0, 0, length); + return result; + } + + /////////////////////// + // Exporting + + + // The block cipher + var aesjs = { + AES: AES, + Counter: Counter, + + ModeOfOperation: { + ecb: ModeOfOperationECB, + cbc: ModeOfOperationCBC, + cfb: ModeOfOperationCFB, + ofb: ModeOfOperationOFB, + ctr: ModeOfOperationCTR + }, + + utils: { + hex: convertHex, + utf8: convertUtf8 + }, + + padding: { + pkcs7: { + pad: pkcs7pad, + strip: pkcs7strip + } + }, + + _arrayTest: { + coerceArray: coerceArray, + createArray: createArray, + copyArray: copyArray, } - } + }; - // inverse-cipher-ify the decryption round key (fips-197 section 5.3) - for (var r = 1; r < rounds; r++) { - for (var c = 0; c < 4; c++) { - tt = this._Kd[r][c]; - this._Kd[r][c] = (U1[(tt >> 24) & 0xFF] ^ - U2[(tt >> 16) & 0xFF] ^ - U3[(tt >> 8) & 0xFF] ^ - U4[tt & 0xFF]); - } - } -} -AES.prototype.encrypt = function (plaintext) { - if (plaintext.length != 16) { - throw new Error('invalid plaintext size (must be 16 bytes)'); - } + // node.js + if (typeof exports !== 'undefined') { + module.exports = aesjs - var rounds = this._Ke.length - 1; - var a = [0, 0, 0, 0]; - - // convert plaintext to (ints ^ key) - var t = convertToInt32(plaintext); - for (var i = 0; i < 4; i++) { - t[i] ^= this._Ke[0][i]; - } - - // apply round transforms - for (var r = 1; r < rounds; r++) { - for (var i = 0; i < 4; i++) { - a[i] = (T1[(t[i] >> 24) & 0xff] ^ - T2[(t[(i + 1) % 4] >> 16) & 0xff] ^ - T3[(t[(i + 2) % 4] >> 8) & 0xff] ^ - T4[t[(i + 3) % 4] & 0xff] ^ - this._Ke[r][i]); - } - t = a.slice(); - } - - // the last round is special - var result = createArray(16), tt; - for (var i = 0; i < 4; i++) { - tt = this._Ke[rounds][i]; - result[4 * i] = (S[(t[i] >> 24) & 0xff] ^ (tt >> 24)) & 0xff; - result[4 * i + 1] = (S[(t[(i + 1) % 4] >> 16) & 0xff] ^ (tt >> 16)) & 0xff; - result[4 * i + 2] = (S[(t[(i + 2) % 4] >> 8) & 0xff] ^ (tt >> 8)) & 0xff; - result[4 * i + 3] = (S[t[(i + 3) % 4] & 0xff] ^ tt) & 0xff; - } - - return result; -} - -AES.prototype.decrypt = function (ciphertext) { - if (ciphertext.length != 16) { - throw new Error('invalid ciphertext size (must be 16 bytes)'); - } - - var rounds = this._Kd.length - 1; - var a = [0, 0, 0, 0]; - - // convert plaintext to (ints ^ key) - var t = convertToInt32(ciphertext); - for (var i = 0; i < 4; i++) { - t[i] ^= this._Kd[0][i]; - } - - // apply round transforms - for (var r = 1; r < rounds; r++) { - for (var i = 0; i < 4; i++) { - a[i] = (T5[(t[i] >> 24) & 0xff] ^ - T6[(t[(i + 3) % 4] >> 16) & 0xff] ^ - T7[(t[(i + 2) % 4] >> 8) & 0xff] ^ - T8[t[(i + 1) % 4] & 0xff] ^ - this._Kd[r][i]); - } - t = a.slice(); - } - - // the last round is special - var result = createArray(16), tt; - for (var i = 0; i < 4; i++) { - tt = this._Kd[rounds][i]; - result[4 * i] = (Si[(t[i] >> 24) & 0xff] ^ (tt >> 24)) & 0xff; - result[4 * i + 1] = (Si[(t[(i + 3) % 4] >> 16) & 0xff] ^ (tt >> 16)) & 0xff; - result[4 * i + 2] = (Si[(t[(i + 2) % 4] >> 8) & 0xff] ^ (tt >> 8)) & 0xff; - result[4 * i + 3] = (Si[t[(i + 1) % 4] & 0xff] ^ tt) & 0xff; - } - - return result; -} - - -/** - * Mode Of Operation - Electonic Codebook (ECB) - */ -var ModeOfOperationECB = function (key) { - if (!(this instanceof ModeOfOperationECB)) { - throw Error('AES must be instanitated with `new`'); - } - - this.description = "Electronic Code Block"; - this.name = "ecb"; - - this._aes = new AES(key); -} - -ModeOfOperationECB.prototype.encrypt = function (plaintext) { - plaintext = coerceArray(plaintext); - - if ((plaintext.length % 16) !== 0) { - throw new Error('invalid plaintext size (must be multiple of 16 bytes)'); - } - - var ciphertext = createArray(plaintext.length); - var block = createArray(16); - - for (var i = 0; i < plaintext.length; i += 16) { - copyArray(plaintext, block, 0, i, i + 16); - block = this._aes.encrypt(block); - copyArray(block, ciphertext, i); - } - - return ciphertext; -} - -ModeOfOperationECB.prototype.decrypt = function (ciphertext) { - ciphertext = coerceArray(ciphertext); - - if ((ciphertext.length % 16) !== 0) { - throw new Error('invalid ciphertext size (must be multiple of 16 bytes)'); - } - - var plaintext = createArray(ciphertext.length); - var block = createArray(16); - - for (var i = 0; i < ciphertext.length; i += 16) { - copyArray(ciphertext, block, 0, i, i + 16); - block = this._aes.decrypt(block); - copyArray(block, plaintext, i); - } - - return plaintext; -} - - -/** - * Mode Of Operation - Cipher Block Chaining (CBC) - */ -var ModeOfOperationCBC = function (key, iv) { - if (!(this instanceof ModeOfOperationCBC)) { - throw Error('AES must be instanitated with `new`'); - } - - this.description = "Cipher Block Chaining"; - this.name = "cbc"; - - if (!iv) { - iv = createArray(16); - - } else if (iv.length != 16) { - throw new Error('invalid initialation vector size (must be 16 bytes)'); - } - - this._lastCipherblock = coerceArray(iv, true); - - this._aes = new AES(key); -} - -ModeOfOperationCBC.prototype.encrypt = function (plaintext) { - plaintext = coerceArray(plaintext); - - if ((plaintext.length % 16) !== 0) { - throw new Error('invalid plaintext size (must be multiple of 16 bytes)'); - } - - var ciphertext = createArray(plaintext.length); - var block = createArray(16); - - for (var i = 0; i < plaintext.length; i += 16) { - copyArray(plaintext, block, 0, i, i + 16); - - for (var j = 0; j < 16; j++) { - block[j] ^= this._lastCipherblock[j]; - } - - this._lastCipherblock = this._aes.encrypt(block); - copyArray(this._lastCipherblock, ciphertext, i); - } - - return ciphertext; -} - -ModeOfOperationCBC.prototype.decrypt = function (ciphertext) { - ciphertext = coerceArray(ciphertext); - - if ((ciphertext.length % 16) !== 0) { - throw new Error('invalid ciphertext size (must be multiple of 16 bytes)'); - } - - var plaintext = createArray(ciphertext.length); - var block = createArray(16); - - for (var i = 0; i < ciphertext.length; i += 16) { - copyArray(ciphertext, block, 0, i, i + 16); - block = this._aes.decrypt(block); - - for (var j = 0; j < 16; j++) { - plaintext[i + j] = block[j] ^ this._lastCipherblock[j]; - } - - copyArray(ciphertext, this._lastCipherblock, 0, i, i + 16); - } - - return plaintext; -} - - -/** - * Mode Of Operation - Cipher Feedback (CFB) - */ -var ModeOfOperationCFB = function (key, iv, segmentSize) { - if (!(this instanceof ModeOfOperationCFB)) { - throw Error('AES must be instanitated with `new`'); - } - - this.description = "Cipher Feedback"; - this.name = "cfb"; - - if (!iv) { - iv = createArray(16); - - } else if (iv.length != 16) { - throw new Error('invalid initialation vector size (must be 16 size)'); - } - - if (!segmentSize) { - segmentSize = 1; - } - - this.segmentSize = segmentSize; - - this._shiftRegister = coerceArray(iv, true); - - this._aes = new AES(key); -} - -ModeOfOperationCFB.prototype.encrypt = function (plaintext) { - if ((plaintext.length % this.segmentSize) != 0) { - throw new Error('invalid plaintext size (must be segmentSize bytes)'); - } - - var encrypted = coerceArray(plaintext, true); - - var xorSegment; - for (var i = 0; i < encrypted.length; i += this.segmentSize) { - xorSegment = this._aes.encrypt(this._shiftRegister); - for (var j = 0; j < this.segmentSize; j++) { - encrypted[i + j] ^= xorSegment[j]; - } - - // Shift the register - copyArray(this._shiftRegister, this._shiftRegister, 0, this.segmentSize); - copyArray(encrypted, this._shiftRegister, 16 - this.segmentSize, i, i + this.segmentSize); - } - - return encrypted; -} - -ModeOfOperationCFB.prototype.decrypt = function (ciphertext) { - if ((ciphertext.length % this.segmentSize) != 0) { - throw new Error('invalid ciphertext size (must be segmentSize bytes)'); - } - - var plaintext = coerceArray(ciphertext, true); - - var xorSegment; - for (var i = 0; i < plaintext.length; i += this.segmentSize) { - xorSegment = this._aes.encrypt(this._shiftRegister); - - for (var j = 0; j < this.segmentSize; j++) { - plaintext[i + j] ^= xorSegment[j]; - } - - // Shift the register - copyArray(this._shiftRegister, this._shiftRegister, 0, this.segmentSize); - copyArray(ciphertext, this._shiftRegister, 16 - this.segmentSize, i, i + this.segmentSize); - } - - return plaintext; -} - -/** - * Mode Of Operation - Output Feedback (OFB) - */ -var ModeOfOperationOFB = function (key, iv) { - if (!(this instanceof ModeOfOperationOFB)) { - throw Error('AES must be instanitated with `new`'); - } - - this.description = "Output Feedback"; - this.name = "ofb"; - - if (!iv) { - iv = createArray(16); - - } else if (iv.length != 16) { - throw new Error('invalid initialation vector size (must be 16 bytes)'); - } - - this._lastPrecipher = coerceArray(iv, true); - this._lastPrecipherIndex = 16; - - this._aes = new AES(key); -} - -ModeOfOperationOFB.prototype.encrypt = function (plaintext) { - var encrypted = coerceArray(plaintext, true); - - for (var i = 0; i < encrypted.length; i++) { - if (this._lastPrecipherIndex === 16) { - this._lastPrecipher = this._aes.encrypt(this._lastPrecipher); - this._lastPrecipherIndex = 0; - } - encrypted[i] ^= this._lastPrecipher[this._lastPrecipherIndex++]; - } - - return encrypted; -} - -// Decryption is symetric -ModeOfOperationOFB.prototype.decrypt = ModeOfOperationOFB.prototype.encrypt; - - -/** - * Counter object for CTR common mode of operation - */ -var Counter = function (initialValue) { - if (!(this instanceof Counter)) { - throw Error('Counter must be instanitated with `new`'); - } - - // We allow 0, but anything false-ish uses the default 1 - if (initialValue !== 0 && !initialValue) { - initialValue = 1; - } - - if (typeof (initialValue) === 'number') { - this._counter = createArray(16); - this.setValue(initialValue); + // RequireJS/AMD + // http://www.requirejs.org/docs/api.html + // https://github.com/amdjs/amdjs-api/wiki/AMD + } else if (typeof (define) === 'function' && define.amd) { + define([], function () { + return aesjs; + }); + // Web Browsers } else { - this.setBytes(initialValue); - } -} -Counter.prototype.setValue = function (value) { - if (typeof (value) !== 'number' || parseInt(value) != value) { - throw new Error('invalid counter value (must be an integer)'); - } - - for (var index = 15; index >= 0; --index) { - this._counter[index] = value % 256; - value = value >> 8; - } -} - -Counter.prototype.setBytes = function (bytes) { - bytes = coerceArray(bytes, true); - - if (bytes.length != 16) { - throw new Error('invalid counter bytes size (must be 16 bytes)'); - } - - this._counter = bytes; -}; - -Counter.prototype.increment = function () { - for (var i = 15; i >= 0; i--) { - if (this._counter[i] === 255) { - this._counter[i] = 0; - } else { - this._counter[i]++; - break; + // If there was an existing library at "aesjs" make sure it's still available + if (root.aesjs) { + aesjs._aesjs = root.aesjs; } - } -} - -/** - * Mode Of Operation - Counter (CTR) - */ -var ModeOfOperationCTR = function (key, counter) { - if (!(this instanceof ModeOfOperationCTR)) { - throw Error('AES must be instanitated with `new`'); + root.aesjs = aesjs; } - this.description = "Counter"; - this.name = "ctr"; - - if (!(counter instanceof Counter)) { - counter = new Counter(counter) - } - - this._counter = counter; - - this._remainingCounter = null; - this._remainingCounterIndex = 16; - - this._aes = new AES(key); -} - -ModeOfOperationCTR.prototype.encrypt = function (plaintext) { - var encrypted = coerceArray(plaintext, true); - - for (var i = 0; i < encrypted.length; i++) { - if (this._remainingCounterIndex === 16) { - this._remainingCounter = this._aes.encrypt(this._counter._counter); - this._remainingCounterIndex = 0; - this._counter.increment(); - } - encrypted[i] ^= this._remainingCounter[this._remainingCounterIndex++]; - } - - return encrypted; -} - -// Decryption is symetric -ModeOfOperationCTR.prototype.decrypt = ModeOfOperationCTR.prototype.encrypt; - - -/////////////////////// -// Padding - -// See:https://tools.ietf.org/html/rfc2315 -function pkcs7pad(data) { - data = coerceArray(data, true); - var padder = 16 - (data.length % 16); - var result = createArray(data.length + padder); - copyArray(data, result); - for (var i = data.length; i < result.length; i++) { - result[i] = padder; - } - return result; -} - -function pkcs7strip(data) { - data = coerceArray(data, true); - if (data.length < 16) { - throw new Error('PKCS#7 invalid length'); - } - - var padder = data[data.length - 1]; - if (padder > 16) { - throw new Error('PKCS#7 padding byte out of range'); - } - - var length = data.length - padder; - for (var i = 0; i < padder; i++) { - if (data[length + i] !== padder) { - throw new Error('PKCS#7 invalid padding byte'); - } - } - - var result = createArray(length); - copyArray(data, result, 0, 0, length); - return result; -} - -// The block cipher -let aesjs = { - AES: AES, - Counter: Counter, - - ModeOfOperation: { - ecb: ModeOfOperationECB, - cbc: ModeOfOperationCBC, - cfb: ModeOfOperationCFB, - ofb: ModeOfOperationOFB, - ctr: ModeOfOperationCTR - }, - - utils: { - hex: convertHex, - utf8: convertUtf8 - }, - - padding: { - pkcs7: { - pad: pkcs7pad, - strip: pkcs7strip - } - }, - - _arrayTest: { - coerceArray: coerceArray, - createArray: createArray, - copyArray: copyArray, - } -}; - -export {aesjs}; +})(window); \ No newline at end of file diff --git a/libraries/asn1.js b/libraries/asn1.js index c6b2f23..246a825 100644 --- a/libraries/asn1.js +++ b/libraries/asn1.js @@ -2,126 +2,159 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - // https://git.coolaj86.com/coolaj86/asn1-parser.js +;(function (exports) { + 'use strict'; -let ELOOPN = 102; -let EDEEPN = 60; -let CTYPES = [0x30, 0x31, 0xa0, 0xa1]; -let VTYPES = [0x01, 0x02, 0x05, 0x06, 0x0c, 0x82]; + if (!exports.ASN1) { exports.ASN1 = {}; } + if (!exports.Enc) { exports.Enc = {}; } + if (!exports.PEM) { exports.PEM = {}; } -function parseAsn1(buf) { - //let ws = ' '; - function parseAsn1(buf, depth, eager) { - if (depth.length >= EDEEPN) { - throw new Error("EDEEP"); - } + var ASN1 = exports.ASN1; + var Enc = exports.Enc; + var PEM = exports.PEM; - let index = 2; // we know, at minimum, data starts after type (0) and lengthSize (1) - let asn1 = {type: buf[0], lengthSize: 0, length: buf[1]}; - let child; - let iters = 0; - let adjust = 0; - let adjustedLen; +// +// Parser +// +// Although I've only seen 9 max in https certificates themselves, +// but each domain list could have up to 100 + ASN1.ELOOPN = 102; + ASN1.ELOOP = "uASN1.js Error: iterated over " + ASN1.ELOOPN + "+ elements (probably a malformed file)"; +// I've seen https certificates go 29 deep + ASN1.EDEEPN = 60; + ASN1.EDEEP = "uASN1.js Error: element nested " + ASN1.EDEEPN + "+ layers deep (probably a malformed file)"; +// Container Types are Sequence 0x30, Container Array? (0xA0, 0xA1) +// Value Types are Boolean 0x01, Integer 0x02, Null 0x05, Object ID 0x06, String 0x0C, 0x16, 0x13, 0x1e Value Array? (0x82) +// Bit String (0x03) and Octet String (0x04) may be values or containers +// Sometimes Bit String is used as a container (RSA Pub Spki) + ASN1.CTYPES = [ 0x30, 0x31, 0xa0, 0xa1 ]; + ASN1.VTYPES = [ 0x01, 0x02, 0x05, 0x06, 0x0c, 0x82 ]; + ASN1.parse = function parseAsn1Helper(buf) { + //var ws = ' '; + function parseAsn1(buf, depth, eager) { + if (depth.length >= ASN1.EDEEPN) { throw new Error(ASN1.EDEEP); } - // Determine how many bytes the length uses, and what it is - if (0x80 & asn1.length) { - asn1.lengthSize = 0x7f & asn1.length; - // I think that buf->hex->int solves the problem of Endianness... not sure - asn1.length = parseInt(bufToHex(buf.slice(index, index + asn1.lengthSize)), 16); - index += asn1.lengthSize; - } + var index = 2; // we know, at minimum, data starts after type (0) and lengthSize (1) + var asn1 = { type: buf[0], lengthSize: 0, length: buf[1] }; + var child; + var iters = 0; + var adjust = 0; + var adjustedLen; - // High-order bit Integers have a leading 0x00 to signify that they are positive. - // Bit Streams use the first byte to signify padding, which x.509 doesn't use. - if (0x00 === buf[index] && (0x02 === asn1.type || 0x03 === asn1.type)) { - // However, 0x00 on its own is a valid number - if (asn1.length > 1) { - index += 1; - adjust = -1; + // Determine how many bytes the length uses, and what it is + if (0x80 & asn1.length) { + asn1.lengthSize = 0x7f & asn1.length; + // I think that buf->hex->int solves the problem of Endianness... not sure + asn1.length = parseInt(Enc.bufToHex(buf.slice(index, index + asn1.lengthSize)), 16); + index += asn1.lengthSize; } - } - adjustedLen = asn1.length + adjust; - function bufToHex(u8) { - let hex = []; - let i, h; - let len = (u8.byteLength || u8.length); - - for (i = 0; i < len; i += 1) { - h = u8[i].toString(16); - if (h.length % 2) { - h = '0' + h; + // High-order bit Integers have a leading 0x00 to signify that they are positive. + // Bit Streams use the first byte to signify padding, which x.509 doesn't use. + if (0x00 === buf[index] && (0x02 === asn1.type || 0x03 === asn1.type)) { + // However, 0x00 on its own is a valid number + if (asn1.length > 1) { + index += 1; + adjust = -1; } - hex.push(h); } + adjustedLen = asn1.length + adjust; - return hex.join('').toLowerCase(); - } - - //console.warn(depth.join(ws) + '0x' + Enc.numToHex(asn1.type), index, 'len:', asn1.length, asn1); - function parseChildren(eager) { - asn1.children = []; - //console.warn('1 len:', (2 + asn1.lengthSize + asn1.length), 'idx:', index, 'clen:', 0); - while (iters < ELOOPN && index < (2 + asn1.length + asn1.lengthSize)) { - iters += 1; - depth.length += 1; - child = parseAsn1(buf.slice(index, index + adjustedLen), depth, eager); - depth.length -= 1; - // The numbers don't match up exactly and I don't remember why... - // probably something with adjustedLen or some such, but the tests pass - index += (2 + child.lengthSize + child.length); - //console.warn('2 len:', (2 + asn1.lengthSize + asn1.length), 'idx:', index, 'clen:', (2 + child.lengthSize + child.length)); - if (index > (2 + asn1.lengthSize + asn1.length)) { - if (!eager) { - console.error("error"); + //console.warn(depth.join(ws) + '0x' + Enc.numToHex(asn1.type), index, 'len:', asn1.length, asn1); + function parseChildren(eager) { + asn1.children = []; + //console.warn('1 len:', (2 + asn1.lengthSize + asn1.length), 'idx:', index, 'clen:', 0); + while (iters < ASN1.ELOOPN && index < (2 + asn1.length + asn1.lengthSize)) { + iters += 1; + depth.length += 1; + child = parseAsn1(buf.slice(index, index + adjustedLen), depth, eager); + depth.length -= 1; + // The numbers don't match up exactly and I don't remember why... + // probably something with adjustedLen or some such, but the tests pass + index += (2 + child.lengthSize + child.length); + //console.warn('2 len:', (2 + asn1.lengthSize + asn1.length), 'idx:', index, 'clen:', (2 + child.lengthSize + child.length)); + if (index > (2 + asn1.lengthSize + asn1.length)) { + if (!eager) { console.error(JSON.stringify(asn1, ASN1._replacer, 2)); } + throw new Error("Parse error: child value length (" + child.length + + ") is greater than remaining parent length (" + (asn1.length - index) + + " = " + asn1.length + " - " + index + ")"); } - throw new Error("Parse error: child value length (" + child.length - + ") is greater than remaining parent length (" + (asn1.length - index) - + " = " + asn1.length + " - " + index + ")"); + asn1.children.push(child); + //console.warn(depth.join(ws) + '0x' + Enc.numToHex(asn1.type), index, 'len:', asn1.length, asn1); } - asn1.children.push(child); - //console.warn(depth.join(ws) + '0x' + Enc.numToHex(asn1.type), index, 'len:', asn1.length, asn1); - } - if (index !== (2 + asn1.lengthSize + asn1.length)) { - //console.warn('index:', index, 'length:', (2 + asn1.lengthSize + asn1.length)); - throw new Error("premature end-of-file"); - } - if (iters >= ELOOPN) { - throw new Error("ELOOP"); + if (index !== (2 + asn1.lengthSize + asn1.length)) { + //console.warn('index:', index, 'length:', (2 + asn1.lengthSize + asn1.length)); + throw new Error("premature end-of-file"); + } + if (iters >= ASN1.ELOOPN) { throw new Error(ASN1.ELOOP); } + + delete asn1.value; + return asn1; } - delete asn1.value; - return asn1; + // Recurse into types that are _always_ containers + if (-1 !== ASN1.CTYPES.indexOf(asn1.type)) { return parseChildren(eager); } + + // Return types that are _always_ values + asn1.value = buf.slice(index, index + adjustedLen); + if (-1 !== ASN1.VTYPES.indexOf(asn1.type)) { return asn1; } + + // For ambigious / unknown types, recurse and return on failure + // (and return child array size to zero) + try { return parseChildren(true); } + catch(e) { asn1.children.length = 0; return asn1; } } - // Recurse into types that are _always_ containers - if (-1 !== CTYPES.indexOf(asn1.type)) { - return parseChildren(eager); + var asn1 = parseAsn1(buf, []); + var len = buf.byteLength || buf.length; + if (len !== 2 + asn1.lengthSize + asn1.length) { + throw new Error("Length of buffer does not match length of ASN.1 sequence."); + } + return asn1; + }; + ASN1._replacer = function (k, v) { + if ('type' === k) { return '0x' + Enc.numToHex(v); } + if (v && 'value' === k) { return '0x' + Enc.bufToHex(v.data || v); } + return v; + }; + +// don't replace the full parseBlock, if it exists + PEM.parseBlock = PEM.parseBlock || function (str) { + var der = str.split(/\n/).filter(function (line) { + return !/-----/.test(line); + }).join(''); + return { der: Enc.base64ToBuf(der) }; + }; + + Enc.base64ToBuf = function (b64) { + return Enc.binToBuf(atob(b64)); + }; + Enc.binToBuf = function (bin) { + var arr = bin.split('').map(function (ch) { + return ch.charCodeAt(0); + }); + return 'undefined' !== typeof Uint8Array ? new Uint8Array(arr) : arr; + }; + Enc.bufToHex = function (u8) { + var hex = []; + var i, h; + var len = (u8.byteLength || u8.length); + + for (i = 0; i < len; i += 1) { + h = u8[i].toString(16); + if (h.length % 2) { h = '0' + h; } + hex.push(h); } - // Return types that are _always_ values - asn1.value = buf.slice(index, index + adjustedLen); - if (-1 !== VTYPES.indexOf(asn1.type)) { - return asn1; + return hex.join('').toLowerCase(); + }; + Enc.numToHex = function (d) { + d = d.toString(16); + if (d.length % 2) { + return '0' + d; } + return d; + }; - // For ambigious / unknown types, recurse and return on failure - // (and return child array size to zero) - try { - return parseChildren(true); - } catch (e) { - asn1.children.length = 0; - return asn1; - } - } - - let asn1 = parseAsn1(buf, []); - let len = buf.byteLength || buf.length; - if (len !== 2 + asn1.lengthSize + asn1.length) { - throw new Error("Length of buffer does not match length of ASN.1 sequence."); - } - return asn1; -} - -export {parseAsn1}; \ No newline at end of file +}('undefined' !== typeof window ? window : module.exports)); \ No newline at end of file diff --git a/libraries/bigint-mod-arith.js b/libraries/bigint-mod-arith.js new file mode 100644 index 0000000..63bba3f --- /dev/null +++ b/libraries/bigint-mod-arith.js @@ -0,0 +1,160 @@ +// https://github.com/juanelas/bigint-mod-arith + +window["bigint-mod-arith"] = function () { + /** + * Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0 + * + * @param a + * + * @returns The absolute value of a + */ + function abs(a) { + return (a >= 0) ? a : -a; + } + + /** + * An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. + * Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b). + * + * @param a + * @param b + * + * @throws {RangeError} + * This excepction is thrown if a or b are less than 0 + * + * @returns A triple (g, x, y), such that ax + by = g = gcd(a, b). + */ + function eGcd(a, b) { + if (typeof a === 'number') + a = BigInt(a); + if (typeof b === 'number') + b = BigInt(b); + if (a <= 0n || b <= 0n) + throw new RangeError('a and b MUST be > 0'); // a and b MUST be positive + let x = 0n; + let y = 1n; + let u = 1n; + let v = 0n; + while (a !== 0n) { + const q = b / a; + const r = b % a; + const m = x - (u * q); + const n = y - (v * q); + b = a; + a = r; + x = u; + y = v; + u = m; + v = n; + } + return { + g: b, + x: x, + y: y + }; + } + + /** + * Finds the smallest positive element that is congruent to a in modulo n + * + * @remarks + * a and b must be the same type, either number or bigint + * + * @param a - An integer + * @param n - The modulo + * + * @throws {RangeError} + * Excpeption thrown when n is not > 0 + * + * @returns A bigint with the smallest positive representation of a modulo n + */ + function toZn(a, n) { + if (typeof a === 'number') + a = BigInt(a); + if (typeof n === 'number') + n = BigInt(n); + if (n <= 0n) { + throw new RangeError('n must be > 0'); + } + const aZn = a % n; + return (aZn < 0n) ? aZn + n : aZn; + } + + /** + * Modular inverse. + * + * @param a The number to find an inverse for + * @param n The modulo + * + * @throws {RangeError} + * Excpeption thorwn when a does not have inverse modulo n + * + * @returns The inverse modulo n + */ + function modInv(a, n) { + const egcd = eGcd(toZn(a, n), n); + if (egcd.g !== 1n) { + throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`); // modular inverse does not exist + } else { + return toZn(egcd.x, n); + } + } + + /** + * Modular exponentiation b**e mod n. Currently using the right-to-left binary method + * + * @param b base + * @param e exponent + * @param n modulo + * + * @throws {RangeError} + * Excpeption thrown when n is not > 0 + * + * @returns b**e mod n + */ + function modPow(b, e, n) { + if (typeof b === 'number') + b = BigInt(b); + if (typeof e === 'number') + e = BigInt(e); + if (typeof n === 'number') + n = BigInt(n); + if (n <= 0n) { + throw new RangeError('n must be > 0'); + } else if (n === 1n) { + return 0n; + } + b = toZn(b, n); + if (e < 0n) { + return modInv(modPow(b, abs(e), n), n); + } + let r = 1n; + while (e > 0) { + if ((e % 2n) === 1n) { + r = r * b % n; + } + e = e / 2n; + b = b ** 2n % n; + } + return r; + } + + function bytesToBigInt(bytes) { + return BigInt("0x" + Array.from(bytes, byte => { + return ('0' + (byte & 0xFF).toString(16)).slice(-2); + }).join('')); + } + + function bigIntToBytes(bigInt) { + let hex = bigInt.toString(16); + + // Convert hex to bytes + let bytes = []; + for (let c = 0; c < hex.length; c += 2) { + bytes.push(parseInt(hex.substr(c, 2), 16)); + } + return bytes; + } + + return {bigIntToBytes, bytesToBigInt, modPow}; +}(); \ No newline at end of file diff --git a/libraries/chat.js b/libraries/chat.js index 126b545..eb7bc8e 100644 --- a/libraries/chat.js +++ b/libraries/chat.js @@ -157,158 +157,74 @@ export function flatten(component) { } /** Converts a `StringComponent` to plain text and can format it using ANSI codes. */ -export function formatString(component, useAnsiCodes = false) { - let text = flatten(component).map((c) => { - if (!useAnsiCodes) - return c.text; +export function formatString(component) { + return flatten(component).map((c) => { let codes = colorToAnsiCode(c.color); if (c.bold) - codes += "\x1b[1m"; + codes += "\u00a7l"; if (c.italic) - codes += "\x1b[3m"; + codes += "\u00a7o"; if (c.underlined) - codes += "\x1b[4m"; + codes += "\u00a7n"; if (c.strikethrough) - codes += "\x1b[9m"; - return codes ? codes + c.text + "\x1b[0m" : c.text; + codes += "\u00a7m"; + return codes ? codes + c.text + "\u00a7r" : c.text; }).join(""); - if (!useAnsiCodes) - return text; - const resetCodes = new Set(); - text = text.split(/ยง(.)/).map((t, i) => { - if (i % 2 === 0) - return t; - else - switch (t) { - case "l": - resetCodes.add("\x1b[22m"); - return "\x1b[1m"; - case "m": - resetCodes.add("\x1b[29m"); - return "\x1b[9m"; - case "n": - resetCodes.add("\x1b[24m"); - return "\x1b[4m"; - case "o": - resetCodes.add("\x1b[23m"); - return "\x1b[3m"; - case "r": - resetCodes.clear(); - return "\x1b[0m"; - case "k": - t = ""; - break; - case "0": - t = "\x1b[38;2;0;0;0m"; - break; - case "1": - t = "\x1b[38;2;0;0;170m"; - break; - case "2": - t = "\x1b[38;2;0;170;0m"; - break; - case "3": - t = "\x1b[38;2;0;170;170m"; - break; - case "4": - t = "\x1b[38;2;170;0;0m"; - break; - case "5": - t = "\x1b[38;2;170;0;170m"; - break; - case "6": - t = "\x1b[38;2;255;170;0m"; - break; - case "7": - t = "\x1b[38;2;170;170;170m"; - break; - case "8": - t = "\x1b[38;2;85;85;85m"; - break; - case "9": - t = "\x1b[38;2;85;85;255m"; - break; - case "a": - t = "\x1b[38;2;85;255;85m"; - break; - case "b": - t = "\x1b[38;2;85;255;255m"; - break; - case "c": - t = "\x1b[38;2;255;85;85m"; - break; - case "d": - t = "\x1b[38;2;255;85;255m"; - break; - case "e": - t = "\x1b[38;2;255;255;85m"; - break; - case "f": - t = "\x1b[38;2;255;255;255m"; - break; - } - return t + [...resetCodes.values()].join(""); - }).join(""); - const index = text.lastIndexOf("\x1b["); - if (index === -1) - return text; - const code = text.slice(index + 2).match(/(.+)m/)[1]; - return code === "0" ? text : text + "\x1b[0m"; } function colorToAnsiCode(color) { let code = ""; switch (color) { case "black": - code += "0;0;0"; + code += "0"; break; case "dark_blue": - code += "0;0;170"; + code += "1"; break; case "dark_green": - code += "0;170;0"; + code += "2"; break; case "dark_aqua": - code += "0;170;170"; + code += "3"; break; case "dark_red": - code += "170;0;0"; + code += "4"; break; case "dark_purple": - code += "170;0;170"; + code += "5"; break; case "gold": - code += "255;170;0"; + code += "6"; break; case "gray": - code += "170;170;170"; + code += "7"; break; case "dark_gray": - code += "85;85;85"; + code += "8"; break; case "blue": - code += "85;85;255"; + code += "9"; break; case "green": - code += "85;255;85"; + code += "a"; break; case "aqua": - code += "85;255;255"; + code += "b"; break; case "red": - code += "255;85;85"; + code += "c"; break; case "light_purple": - code += "255;85;255"; + code += "d"; break; case "yellow": - code += "255;255;85"; + code += "e"; break; case "white": - code += "255;255;255"; + code += "f"; break; } - return code && "\x1b[38;2;" + code + "m"; + return code && "\u00a7" + code; } function flattenArray(array) { diff --git a/libraries/long.js b/libraries/long.js index d2fa2a1..2f17cea 100644 --- a/libraries/long.js +++ b/libraries/long.js @@ -18,46 +18,50 @@ * SPDX-License-Identifier: Apache-2.0 */ +// https://github.com/dcodeIO/long.js + +let Long = function () { + // WebAssembly optimizations to do native i64 multiplication and divide -var wasm = null; -try { - wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([ - 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11 - ])), {}).exports; -} catch (e) { - // no wasm support :( -} - -/** - * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers. - * See the from* functions below for more convenient ways of constructing Longs. - * @exports Long - * @class A Long class for representing a 64 bit two's-complement integer value. - * @param {number} low The low (signed) 32 bits of the long - * @param {number} high The high (signed) 32 bits of the long - * @param {boolean=} unsigned Whether unsigned or not, defaults to signed - * @constructor - */ -function Long(low, high, unsigned) { + var wasm = null; + try { + wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([ + 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11 + ])), {}).exports; + } catch (e) { + // no wasm support :( + } /** - * The low 32 bits as a signed value. - * @type {number} + * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers. + * See the from* functions below for more convenient ways of constructing Longs. + * @exports Long + * @class A Long class for representing a 64 bit two's-complement integer value. + * @param {number} low The low (signed) 32 bits of the long + * @param {number} high The high (signed) 32 bits of the long + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @constructor */ - this.low = low | 0; + function Long(low, high, unsigned) { - /** - * The high 32 bits as a signed value. - * @type {number} - */ - this.high = high | 0; + /** + * The low 32 bits as a signed value. + * @type {number} + */ + this.low = low | 0; - /** - * Whether unsigned or not. - * @type {boolean} - */ - this.unsigned = !!unsigned; -} + /** + * The high 32 bits as a signed value. + * @type {number} + */ + this.high = high | 0; + + /** + * Whether unsigned or not. + * @type {boolean} + */ + this.unsigned = !!unsigned; + } // The internal representation of a long is the two given signed, 32-bit values. // We use 32-bit pieces because these are the size of integers on which @@ -76,1392 +80,1395 @@ function Long(low, high, unsigned) { // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from* // methods on which they depend. -/** - * An indicator used to reliably determine if an object is a Long or not. - * @type {boolean} - * @const - * @private - */ -Long.prototype.__isLong__; + /** + * An indicator used to reliably determine if an object is a Long or not. + * @type {boolean} + * @const + * @private + */ + Long.prototype.__isLong__; -Object.defineProperty(Long.prototype, "__isLong__", { value: true }); + Object.defineProperty(Long.prototype, "__isLong__", {value: true}); -/** - * @function - * @param {*} obj Object - * @returns {boolean} - * @inner - */ -function isLong(obj) { - return (obj && obj["__isLong__"]) === true; -} - -/** - * @function - * @param {*} value number - * @returns {number} - * @inner - */ -function ctz32(value) { - var c = Math.clz32(value & -value); - return value ? 31 - c : c; -} - -/** - * Tests if the specified object is a Long. - * @function - * @param {*} obj Object - * @returns {boolean} - */ -Long.isLong = isLong; - -/** - * A cache of the Long representations of small integer values. - * @type {!Object} - * @inner - */ -var INT_CACHE = {}; - -/** - * A cache of the Long representations of small unsigned integer values. - * @type {!Object} - * @inner - */ -var UINT_CACHE = {}; - -/** - * @param {number} value - * @param {boolean=} unsigned - * @returns {!Long} - * @inner - */ -function fromInt(value, unsigned) { - var obj, cachedObj, cache; - if (unsigned) { - value >>>= 0; - if (cache = (0 <= value && value < 256)) { - cachedObj = UINT_CACHE[value]; - if (cachedObj) - return cachedObj; - } - obj = fromBits(value, 0, true); - if (cache) - UINT_CACHE[value] = obj; - return obj; - } else { - value |= 0; - if (cache = (-128 <= value && value < 128)) { - cachedObj = INT_CACHE[value]; - if (cachedObj) - return cachedObj; - } - obj = fromBits(value, value < 0 ? -1 : 0, false); - if (cache) - INT_CACHE[value] = obj; - return obj; - } -} - -/** - * Returns a Long representing the given 32 bit integer value. - * @function - * @param {number} value The 32 bit integer in question - * @param {boolean=} unsigned Whether unsigned or not, defaults to signed - * @returns {!Long} The corresponding Long value - */ -Long.fromInt = fromInt; - -/** - * @param {number} value - * @param {boolean=} unsigned - * @returns {!Long} - * @inner - */ -function fromNumber(value, unsigned) { - if (isNaN(value)) - return unsigned ? UZERO : ZERO; - if (unsigned) { - if (value < 0) - return UZERO; - if (value >= TWO_PWR_64_DBL) - return MAX_UNSIGNED_VALUE; - } else { - if (value <= -TWO_PWR_63_DBL) - return MIN_VALUE; - if (value + 1 >= TWO_PWR_63_DBL) - return MAX_VALUE; - } - if (value < 0) - return fromNumber(-value, unsigned).neg(); - return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); -} - -/** - * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. - * @function - * @param {number} value The number in question - * @param {boolean=} unsigned Whether unsigned or not, defaults to signed - * @returns {!Long} The corresponding Long value - */ -Long.fromNumber = fromNumber; - -/** - * @param {number} lowBits - * @param {number} highBits - * @param {boolean=} unsigned - * @returns {!Long} - * @inner - */ -function fromBits(lowBits, highBits, unsigned) { - return new Long(lowBits, highBits, unsigned); -} - -/** - * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is - * assumed to use 32 bits. - * @function - * @param {number} lowBits The low 32 bits - * @param {number} highBits The high 32 bits - * @param {boolean=} unsigned Whether unsigned or not, defaults to signed - * @returns {!Long} The corresponding Long value - */ -Long.fromBits = fromBits; - -/** - * @function - * @param {number} base - * @param {number} exponent - * @returns {number} - * @inner - */ -var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4) - -/** - * @param {string} str - * @param {(boolean|number)=} unsigned - * @param {number=} radix - * @returns {!Long} - * @inner - */ -function fromString(str, unsigned, radix) { - if (str.length === 0) - throw Error('empty string'); - if (typeof unsigned === 'number') { - // For goog.math.long compatibility - radix = unsigned; - unsigned = false; - } else { - unsigned = !!unsigned; - } - if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") - return unsigned ? UZERO : ZERO; - radix = radix || 10; - if (radix < 2 || 36 < radix) - throw RangeError('radix'); - - var p; - if ((p = str.indexOf('-')) > 0) - throw Error('interior hyphen'); - else if (p === 0) { - return fromString(str.substring(1), unsigned, radix).neg(); + /** + * @function + * @param {*} obj Object + * @returns {boolean} + * @inner + */ + function isLong(obj) { + return (obj && obj["__isLong__"]) === true; } - // Do several (8) digits each time through the loop, so as to - // minimize the calls to the very expensive emulated div. - var radixToPower = fromNumber(pow_dbl(radix, 8)); + /** + * @function + * @param {*} value number + * @returns {number} + * @inner + */ + function ctz32(value) { + var c = Math.clz32(value & -value); + return value ? 31 - c : c; + } - var result = ZERO; - for (var i = 0; i < str.length; i += 8) { - var size = Math.min(8, str.length - i), - value = parseInt(str.substring(i, i + size), radix); - if (size < 8) { - var power = fromNumber(pow_dbl(radix, size)); - result = result.mul(power).add(fromNumber(value)); + /** + * Tests if the specified object is a Long. + * @function + * @param {*} obj Object + * @returns {boolean} + */ + Long.isLong = isLong; + + /** + * A cache of the Long representations of small integer values. + * @type {!Object} + * @inner + */ + var INT_CACHE = {}; + + /** + * A cache of the Long representations of small unsigned integer values. + * @type {!Object} + * @inner + */ + var UINT_CACHE = {}; + + /** + * @param {number} value + * @param {boolean=} unsigned + * @returns {!Long} + * @inner + */ + function fromInt(value, unsigned) { + var obj, cachedObj, cache; + if (unsigned) { + value >>>= 0; + if (cache = (0 <= value && value < 256)) { + cachedObj = UINT_CACHE[value]; + if (cachedObj) + return cachedObj; + } + obj = fromBits(value, 0, true); + if (cache) + UINT_CACHE[value] = obj; + return obj; } else { - result = result.mul(radixToPower); - result = result.add(fromNumber(value)); + value |= 0; + if (cache = (-128 <= value && value < 128)) { + cachedObj = INT_CACHE[value]; + if (cachedObj) + return cachedObj; + } + obj = fromBits(value, value < 0 ? -1 : 0, false); + if (cache) + INT_CACHE[value] = obj; + return obj; } } - result.unsigned = unsigned; - return result; -} -/** - * Returns a Long representation of the given string, written using the specified radix. - * @function - * @param {string} str The textual representation of the Long - * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to signed - * @param {number=} radix The radix in which the text is written (2-36), defaults to 10 - * @returns {!Long} The corresponding Long value - */ -Long.fromString = fromString; + /** + * Returns a Long representing the given 32 bit integer value. + * @function + * @param {number} value The 32 bit integer in question + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {!Long} The corresponding Long value + */ + Long.fromInt = fromInt; -/** - * @function - * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val - * @param {boolean=} unsigned - * @returns {!Long} - * @inner - */ -function fromValue(val, unsigned) { - if (typeof val === 'number') - return fromNumber(val, unsigned); - if (typeof val === 'string') - return fromString(val, unsigned); - // Throws for non-objects, converts non-instanceof Long: - return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned); -} + /** + * @param {number} value + * @param {boolean=} unsigned + * @returns {!Long} + * @inner + */ + function fromNumber(value, unsigned) { + if (isNaN(value)) + return unsigned ? UZERO : ZERO; + if (unsigned) { + if (value < 0) + return UZERO; + if (value >= TWO_PWR_64_DBL) + return MAX_UNSIGNED_VALUE; + } else { + if (value <= -TWO_PWR_63_DBL) + return MIN_VALUE; + if (value + 1 >= TWO_PWR_63_DBL) + return MAX_VALUE; + } + if (value < 0) + return fromNumber(-value, unsigned).neg(); + return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); + } -/** - * Converts the specified value to a Long using the appropriate from* function for its type. - * @function - * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value - * @param {boolean=} unsigned Whether unsigned or not, defaults to signed - * @returns {!Long} - */ -Long.fromValue = fromValue; + /** + * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * @function + * @param {number} value The number in question + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {!Long} The corresponding Long value + */ + Long.fromNumber = fromNumber; + + /** + * @param {number} lowBits + * @param {number} highBits + * @param {boolean=} unsigned + * @returns {!Long} + * @inner + */ + function fromBits(lowBits, highBits, unsigned) { + return new Long(lowBits, highBits, unsigned); + } + + /** + * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is + * assumed to use 32 bits. + * @function + * @param {number} lowBits The low 32 bits + * @param {number} highBits The high 32 bits + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {!Long} The corresponding Long value + */ + Long.fromBits = fromBits; + + /** + * @function + * @param {number} base + * @param {number} exponent + * @returns {number} + * @inner + */ + var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4) + + /** + * @param {string} str + * @param {(boolean|number)=} unsigned + * @param {number=} radix + * @returns {!Long} + * @inner + */ + function fromString(str, unsigned, radix) { + if (str.length === 0) + throw Error('empty string'); + if (typeof unsigned === 'number') { + // For goog.math.long compatibility + radix = unsigned; + unsigned = false; + } else { + unsigned = !!unsigned; + } + if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") + return unsigned ? UZERO : ZERO; + radix = radix || 10; + if (radix < 2 || 36 < radix) + throw RangeError('radix'); + + var p; + if ((p = str.indexOf('-')) > 0) + throw Error('interior hyphen'); + else if (p === 0) { + return fromString(str.substring(1), unsigned, radix).neg(); + } + + // Do several (8) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = fromNumber(pow_dbl(radix, 8)); + + var result = ZERO; + for (var i = 0; i < str.length; i += 8) { + var size = Math.min(8, str.length - i), + value = parseInt(str.substring(i, i + size), radix); + if (size < 8) { + var power = fromNumber(pow_dbl(radix, size)); + result = result.mul(power).add(fromNumber(value)); + } else { + result = result.mul(radixToPower); + result = result.add(fromNumber(value)); + } + } + result.unsigned = unsigned; + return result; + } + + /** + * Returns a Long representation of the given string, written using the specified radix. + * @function + * @param {string} str The textual representation of the Long + * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to signed + * @param {number=} radix The radix in which the text is written (2-36), defaults to 10 + * @returns {!Long} The corresponding Long value + */ + Long.fromString = fromString; + + /** + * @function + * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val + * @param {boolean=} unsigned + * @returns {!Long} + * @inner + */ + function fromValue(val, unsigned) { + if (typeof val === 'number') + return fromNumber(val, unsigned); + if (typeof val === 'string') + return fromString(val, unsigned); + // Throws for non-objects, converts non-instanceof Long: + return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned); + } + + /** + * Converts the specified value to a Long using the appropriate from* function for its type. + * @function + * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {!Long} + */ + Long.fromValue = fromValue; // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be // no runtime penalty for these. -/** - * @type {number} - * @const - * @inner - */ -var TWO_PWR_16_DBL = 1 << 16; + /** + * @type {number} + * @const + * @inner + */ + var TWO_PWR_16_DBL = 1 << 16; -/** - * @type {number} - * @const - * @inner - */ -var TWO_PWR_24_DBL = 1 << 24; + /** + * @type {number} + * @const + * @inner + */ + var TWO_PWR_24_DBL = 1 << 24; -/** - * @type {number} - * @const - * @inner - */ -var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL; + /** + * @type {number} + * @const + * @inner + */ + var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL; -/** - * @type {number} - * @const - * @inner - */ -var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL; + /** + * @type {number} + * @const + * @inner + */ + var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL; -/** - * @type {number} - * @const - * @inner - */ -var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2; + /** + * @type {number} + * @const + * @inner + */ + var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2; -/** - * @type {!Long} - * @const - * @inner - */ -var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL); + /** + * @type {!Long} + * @const + * @inner + */ + var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL); -/** - * @type {!Long} - * @inner - */ -var ZERO = fromInt(0); + /** + * @type {!Long} + * @inner + */ + var ZERO = fromInt(0); -/** - * Signed zero. - * @type {!Long} - */ -Long.ZERO = ZERO; + /** + * Signed zero. + * @type {!Long} + */ + Long.ZERO = ZERO; -/** - * @type {!Long} - * @inner - */ -var UZERO = fromInt(0, true); + /** + * @type {!Long} + * @inner + */ + var UZERO = fromInt(0, true); -/** - * Unsigned zero. - * @type {!Long} - */ -Long.UZERO = UZERO; + /** + * Unsigned zero. + * @type {!Long} + */ + Long.UZERO = UZERO; -/** - * @type {!Long} - * @inner - */ -var ONE = fromInt(1); + /** + * @type {!Long} + * @inner + */ + var ONE = fromInt(1); -/** - * Signed one. - * @type {!Long} - */ -Long.ONE = ONE; + /** + * Signed one. + * @type {!Long} + */ + Long.ONE = ONE; -/** - * @type {!Long} - * @inner - */ -var UONE = fromInt(1, true); + /** + * @type {!Long} + * @inner + */ + var UONE = fromInt(1, true); -/** - * Unsigned one. - * @type {!Long} - */ -Long.UONE = UONE; + /** + * Unsigned one. + * @type {!Long} + */ + Long.UONE = UONE; -/** - * @type {!Long} - * @inner - */ -var NEG_ONE = fromInt(-1); + /** + * @type {!Long} + * @inner + */ + var NEG_ONE = fromInt(-1); -/** - * Signed negative one. - * @type {!Long} - */ -Long.NEG_ONE = NEG_ONE; + /** + * Signed negative one. + * @type {!Long} + */ + Long.NEG_ONE = NEG_ONE; -/** - * @type {!Long} - * @inner - */ -var MAX_VALUE = fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0, false); + /** + * @type {!Long} + * @inner + */ + var MAX_VALUE = fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0, false); -/** - * Maximum signed value. - * @type {!Long} - */ -Long.MAX_VALUE = MAX_VALUE; + /** + * Maximum signed value. + * @type {!Long} + */ + Long.MAX_VALUE = MAX_VALUE; -/** - * @type {!Long} - * @inner - */ -var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF | 0, 0xFFFFFFFF | 0, true); + /** + * @type {!Long} + * @inner + */ + var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF | 0, 0xFFFFFFFF | 0, true); -/** - * Maximum unsigned value. - * @type {!Long} - */ -Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE; + /** + * Maximum unsigned value. + * @type {!Long} + */ + Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE; -/** - * @type {!Long} - * @inner - */ -var MIN_VALUE = fromBits(0, 0x80000000 | 0, false); + /** + * @type {!Long} + * @inner + */ + var MIN_VALUE = fromBits(0, 0x80000000 | 0, false); -/** - * Minimum signed value. - * @type {!Long} - */ -Long.MIN_VALUE = MIN_VALUE; + /** + * Minimum signed value. + * @type {!Long} + */ + Long.MIN_VALUE = MIN_VALUE; -/** - * @alias Long.prototype - * @inner - */ -var LongPrototype = Long.prototype; + /** + * @alias Long.prototype + * @inner + */ + var LongPrototype = Long.prototype; -/** - * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. - * @this {!Long} - * @returns {number} - */ -LongPrototype.toInt = function toInt() { - return this.unsigned ? this.low >>> 0 : this.low; -}; + /** + * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. + * @this {!Long} + * @returns {number} + */ + LongPrototype.toInt = function toInt() { + return this.unsigned ? this.low >>> 0 : this.low; + }; -/** - * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). - * @this {!Long} - * @returns {number} - */ -LongPrototype.toNumber = function toNumber() { - if (this.unsigned) - return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0); - return this.high * TWO_PWR_32_DBL + (this.low >>> 0); -}; + /** + * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). + * @this {!Long} + * @returns {number} + */ + LongPrototype.toNumber = function toNumber() { + if (this.unsigned) + return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0); + return this.high * TWO_PWR_32_DBL + (this.low >>> 0); + }; -/** - * Converts the Long to a string written in the specified radix. - * @this {!Long} - * @param {number=} radix Radix (2-36), defaults to 10 - * @returns {string} - * @override - * @throws {RangeError} If `radix` is out of range - */ -LongPrototype.toString = function toString(radix) { - radix = radix || 10; - if (radix < 2 || 36 < radix) - throw RangeError('radix'); - if (this.isZero()) - return '0'; - if (this.isNegative()) { // Unsigned Longs are never negative - if (this.eq(MIN_VALUE)) { - // We need to change the Long value before it can be negated, so we remove - // the bottom-most digit in this base and then recurse to do the rest. - var radixLong = fromNumber(radix), - div = this.div(radixLong), - rem1 = div.mul(radixLong).sub(this); - return div.toString(radix) + rem1.toInt().toString(radix); - } else - return '-' + this.neg().toString(radix); - } - - // Do several (6) digits each time through the loop, so as to - // minimize the calls to the very expensive emulated div. - var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned), - rem = this; - var result = ''; - while (true) { - var remDiv = rem.div(radixToPower), - intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0, - digits = intval.toString(radix); - rem = remDiv; - if (rem.isZero()) - return digits + result; - else { - while (digits.length < 6) - digits = '0' + digits; - result = '' + digits + result; + /** + * Converts the Long to a string written in the specified radix. + * @this {!Long} + * @param {number=} radix Radix (2-36), defaults to 10 + * @returns {string} + * @override + * @throws {RangeError} If `radix` is out of range + */ + LongPrototype.toString = function toString(radix) { + radix = radix || 10; + if (radix < 2 || 36 < radix) + throw RangeError('radix'); + if (this.isZero()) + return '0'; + if (this.isNegative()) { // Unsigned Longs are never negative + if (this.eq(MIN_VALUE)) { + // We need to change the Long value before it can be negated, so we remove + // the bottom-most digit in this base and then recurse to do the rest. + var radixLong = fromNumber(radix), + div = this.div(radixLong), + rem1 = div.mul(radixLong).sub(this); + return div.toString(radix) + rem1.toInt().toString(radix); + } else + return '-' + this.neg().toString(radix); } - } -}; -/** - * Gets the high 32 bits as a signed integer. - * @this {!Long} - * @returns {number} Signed high bits - */ -LongPrototype.getHighBits = function getHighBits() { - return this.high; -}; - -/** - * Gets the high 32 bits as an unsigned integer. - * @this {!Long} - * @returns {number} Unsigned high bits - */ -LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() { - return this.high >>> 0; -}; - -/** - * Gets the low 32 bits as a signed integer. - * @this {!Long} - * @returns {number} Signed low bits - */ -LongPrototype.getLowBits = function getLowBits() { - return this.low; -}; - -/** - * Gets the low 32 bits as an unsigned integer. - * @this {!Long} - * @returns {number} Unsigned low bits - */ -LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() { - return this.low >>> 0; -}; - -/** - * Gets the number of bits needed to represent the absolute value of this Long. - * @this {!Long} - * @returns {number} - */ -LongPrototype.getNumBitsAbs = function getNumBitsAbs() { - if (this.isNegative()) // Unsigned Longs are never negative - return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); - var val = this.high != 0 ? this.high : this.low; - for (var bit = 31; bit > 0; bit--) - if ((val & (1 << bit)) != 0) - break; - return this.high != 0 ? bit + 33 : bit + 1; -}; - -/** - * Tests if this Long's value equals zero. - * @this {!Long} - * @returns {boolean} - */ -LongPrototype.isZero = function isZero() { - return this.high === 0 && this.low === 0; -}; - -/** - * Tests if this Long's value equals zero. This is an alias of {@link Long#isZero}. - * @returns {boolean} - */ -LongPrototype.eqz = LongPrototype.isZero; - -/** - * Tests if this Long's value is negative. - * @this {!Long} - * @returns {boolean} - */ -LongPrototype.isNegative = function isNegative() { - return !this.unsigned && this.high < 0; -}; - -/** - * Tests if this Long's value is positive or zero. - * @this {!Long} - * @returns {boolean} - */ -LongPrototype.isPositive = function isPositive() { - return this.unsigned || this.high >= 0; -}; - -/** - * Tests if this Long's value is odd. - * @this {!Long} - * @returns {boolean} - */ -LongPrototype.isOdd = function isOdd() { - return (this.low & 1) === 1; -}; - -/** - * Tests if this Long's value is even. - * @this {!Long} - * @returns {boolean} - */ -LongPrototype.isEven = function isEven() { - return (this.low & 1) === 0; -}; - -/** - * Tests if this Long's value equals the specified's. - * @this {!Long} - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.equals = function equals(other) { - if (!isLong(other)) - other = fromValue(other); - if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) - return false; - return this.high === other.high && this.low === other.low; -}; - -/** - * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}. - * @function - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.eq = LongPrototype.equals; - -/** - * Tests if this Long's value differs from the specified's. - * @this {!Long} - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.notEquals = function notEquals(other) { - return !this.eq(/* validates */ other); -}; - -/** - * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}. - * @function - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.neq = LongPrototype.notEquals; - -/** - * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}. - * @function - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.ne = LongPrototype.notEquals; - -/** - * Tests if this Long's value is less than the specified's. - * @this {!Long} - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.lessThan = function lessThan(other) { - return this.comp(/* validates */ other) < 0; -}; - -/** - * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}. - * @function - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.lt = LongPrototype.lessThan; - -/** - * Tests if this Long's value is less than or equal the specified's. - * @this {!Long} - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) { - return this.comp(/* validates */ other) <= 0; -}; - -/** - * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}. - * @function - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.lte = LongPrototype.lessThanOrEqual; - -/** - * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}. - * @function - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.le = LongPrototype.lessThanOrEqual; - -/** - * Tests if this Long's value is greater than the specified's. - * @this {!Long} - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.greaterThan = function greaterThan(other) { - return this.comp(/* validates */ other) > 0; -}; - -/** - * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}. - * @function - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.gt = LongPrototype.greaterThan; - -/** - * Tests if this Long's value is greater than or equal the specified's. - * @this {!Long} - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) { - return this.comp(/* validates */ other) >= 0; -}; - -/** - * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}. - * @function - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.gte = LongPrototype.greaterThanOrEqual; - -/** - * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}. - * @function - * @param {!Long|number|string} other Other value - * @returns {boolean} - */ -LongPrototype.ge = LongPrototype.greaterThanOrEqual; - -/** - * Compares this Long's value with the specified's. - * @this {!Long} - * @param {!Long|number|string} other Other value - * @returns {number} 0 if they are the same, 1 if the this is greater and -1 - * if the given one is greater - */ -LongPrototype.compare = function compare(other) { - if (!isLong(other)) - other = fromValue(other); - if (this.eq(other)) - return 0; - var thisNeg = this.isNegative(), - otherNeg = other.isNegative(); - if (thisNeg && !otherNeg) - return -1; - if (!thisNeg && otherNeg) - return 1; - // At this point the sign bits are the same - if (!this.unsigned) - return this.sub(other).isNegative() ? -1 : 1; - // Both are positive if at least one is unsigned - return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1; -}; - -/** - * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}. - * @function - * @param {!Long|number|string} other Other value - * @returns {number} 0 if they are the same, 1 if the this is greater and -1 - * if the given one is greater - */ -LongPrototype.comp = LongPrototype.compare; - -/** - * Negates this Long's value. - * @this {!Long} - * @returns {!Long} Negated Long - */ -LongPrototype.negate = function negate() { - if (!this.unsigned && this.eq(MIN_VALUE)) - return MIN_VALUE; - return this.not().add(ONE); -}; - -/** - * Negates this Long's value. This is an alias of {@link Long#negate}. - * @function - * @returns {!Long} Negated Long - */ -LongPrototype.neg = LongPrototype.negate; - -/** - * Returns the sum of this and the specified Long. - * @this {!Long} - * @param {!Long|number|string} addend Addend - * @returns {!Long} Sum - */ -LongPrototype.add = function add(addend) { - if (!isLong(addend)) - addend = fromValue(addend); - - // Divide each number into 4 chunks of 16 bits, and then sum the chunks. - - var a48 = this.high >>> 16; - var a32 = this.high & 0xFFFF; - var a16 = this.low >>> 16; - var a00 = this.low & 0xFFFF; - - var b48 = addend.high >>> 16; - var b32 = addend.high & 0xFFFF; - var b16 = addend.low >>> 16; - var b00 = addend.low & 0xFFFF; - - var c48 = 0, c32 = 0, c16 = 0, c00 = 0; - c00 += a00 + b00; - c16 += c00 >>> 16; - c00 &= 0xFFFF; - c16 += a16 + b16; - c32 += c16 >>> 16; - c16 &= 0xFFFF; - c32 += a32 + b32; - c48 += c32 >>> 16; - c32 &= 0xFFFF; - c48 += a48 + b48; - c48 &= 0xFFFF; - return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); -}; - -/** - * Returns the difference of this and the specified Long. - * @this {!Long} - * @param {!Long|number|string} subtrahend Subtrahend - * @returns {!Long} Difference - */ -LongPrototype.subtract = function subtract(subtrahend) { - if (!isLong(subtrahend)) - subtrahend = fromValue(subtrahend); - return this.add(subtrahend.neg()); -}; - -/** - * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}. - * @function - * @param {!Long|number|string} subtrahend Subtrahend - * @returns {!Long} Difference - */ -LongPrototype.sub = LongPrototype.subtract; - -/** - * Returns the product of this and the specified Long. - * @this {!Long} - * @param {!Long|number|string} multiplier Multiplier - * @returns {!Long} Product - */ -LongPrototype.multiply = function multiply(multiplier) { - if (this.isZero()) - return this; - if (!isLong(multiplier)) - multiplier = fromValue(multiplier); - - // use wasm support if present - if (wasm) { - var low = wasm["mul"](this.low, - this.high, - multiplier.low, - multiplier.high); - return fromBits(low, wasm["get_high"](), this.unsigned); - } - - if (multiplier.isZero()) - return this.unsigned ? UZERO : ZERO; - if (this.eq(MIN_VALUE)) - return multiplier.isOdd() ? MIN_VALUE : ZERO; - if (multiplier.eq(MIN_VALUE)) - return this.isOdd() ? MIN_VALUE : ZERO; - - if (this.isNegative()) { - if (multiplier.isNegative()) - return this.neg().mul(multiplier.neg()); - else - return this.neg().mul(multiplier).neg(); - } else if (multiplier.isNegative()) - return this.mul(multiplier.neg()).neg(); - - // If both longs are small, use float multiplication - if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24)) - return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); - - // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. - // We can skip products that would overflow. - - var a48 = this.high >>> 16; - var a32 = this.high & 0xFFFF; - var a16 = this.low >>> 16; - var a00 = this.low & 0xFFFF; - - var b48 = multiplier.high >>> 16; - var b32 = multiplier.high & 0xFFFF; - var b16 = multiplier.low >>> 16; - var b00 = multiplier.low & 0xFFFF; - - var c48 = 0, c32 = 0, c16 = 0, c00 = 0; - c00 += a00 * b00; - c16 += c00 >>> 16; - c00 &= 0xFFFF; - c16 += a16 * b00; - c32 += c16 >>> 16; - c16 &= 0xFFFF; - c16 += a00 * b16; - c32 += c16 >>> 16; - c16 &= 0xFFFF; - c32 += a32 * b00; - c48 += c32 >>> 16; - c32 &= 0xFFFF; - c32 += a16 * b16; - c48 += c32 >>> 16; - c32 &= 0xFFFF; - c32 += a00 * b32; - c48 += c32 >>> 16; - c32 &= 0xFFFF; - c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; - c48 &= 0xFFFF; - return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); -}; - -/** - * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}. - * @function - * @param {!Long|number|string} multiplier Multiplier - * @returns {!Long} Product - */ -LongPrototype.mul = LongPrototype.multiply; - -/** - * Returns this Long divided by the specified. The result is signed if this Long is signed or - * unsigned if this Long is unsigned. - * @this {!Long} - * @param {!Long|number|string} divisor Divisor - * @returns {!Long} Quotient - */ -LongPrototype.divide = function divide(divisor) { - if (!isLong(divisor)) - divisor = fromValue(divisor); - if (divisor.isZero()) - throw Error('division by zero'); - - // use wasm support if present - if (wasm) { - // guard against signed division overflow: the largest - // negative number / -1 would be 1 larger than the largest - // positive number, due to two's complement. - if (!this.unsigned && - this.high === -0x80000000 && - divisor.low === -1 && divisor.high === -1) { - // be consistent with non-wasm code path - return this; - } - var low = (this.unsigned ? wasm["div_u"] : wasm["div_s"])( - this.low, - this.high, - divisor.low, - divisor.high - ); - return fromBits(low, wasm["get_high"](), this.unsigned); - } - - if (this.isZero()) - return this.unsigned ? UZERO : ZERO; - var approx, rem, res; - if (!this.unsigned) { - // This section is only relevant for signed longs and is derived from the - // closure library as a whole. - if (this.eq(MIN_VALUE)) { - if (divisor.eq(ONE) || divisor.eq(NEG_ONE)) - return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE - else if (divisor.eq(MIN_VALUE)) - return ONE; + // Do several (6) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned), + rem = this; + var result = ''; + while (true) { + var remDiv = rem.div(radixToPower), + intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0, + digits = intval.toString(radix); + rem = remDiv; + if (rem.isZero()) + return digits + result; else { - // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. - var halfThis = this.shr(1); - approx = halfThis.div(divisor).shl(1); - if (approx.eq(ZERO)) { - return divisor.isNegative() ? ONE : NEG_ONE; - } else { - rem = this.sub(divisor.mul(approx)); - res = approx.add(rem.div(divisor)); - return res; - } + while (digits.length < 6) + digits = '0' + digits; + result = '' + digits + result; } - } else if (divisor.eq(MIN_VALUE)) - return this.unsigned ? UZERO : ZERO; - if (this.isNegative()) { - if (divisor.isNegative()) - return this.neg().div(divisor.neg()); - return this.neg().div(divisor).neg(); - } else if (divisor.isNegative()) - return this.div(divisor.neg()).neg(); - res = ZERO; - } else { - // The algorithm below has not been made for unsigned longs. It's therefore - // required to take special care of the MSB prior to running it. - if (!divisor.unsigned) - divisor = divisor.toUnsigned(); - if (divisor.gt(this)) - return UZERO; - if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true - return UONE; - res = UZERO; - } + } + }; - // Repeat the following until the remainder is less than other: find a - // floating-point that approximates remainder / other *from below*, add this - // into the result, and subtract it from the remainder. It is critical that - // the approximate value is less than or equal to the real value so that the - // remainder never becomes negative. - rem = this; - while (rem.gte(divisor)) { - // Approximate the result of division. This may be a little greater or - // smaller than the actual value. - approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber())); + /** + * Gets the high 32 bits as a signed integer. + * @this {!Long} + * @returns {number} Signed high bits + */ + LongPrototype.getHighBits = function getHighBits() { + return this.high; + }; - // We will tweak the approximate result by changing it in the 48-th digit or - // the smallest non-fractional digit, whichever is larger. - var log2 = Math.ceil(Math.log(approx) / Math.LN2), - delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48), + /** + * Gets the high 32 bits as an unsigned integer. + * @this {!Long} + * @returns {number} Unsigned high bits + */ + LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() { + return this.high >>> 0; + }; - // Decrease the approximation until it is smaller than the remainder. Note - // that if it is too large, the product overflows and is negative. - approxRes = fromNumber(approx), - approxRem = approxRes.mul(divisor); - while (approxRem.isNegative() || approxRem.gt(rem)) { - approx -= delta; - approxRes = fromNumber(approx, this.unsigned); - approxRem = approxRes.mul(divisor); + /** + * Gets the low 32 bits as a signed integer. + * @this {!Long} + * @returns {number} Signed low bits + */ + LongPrototype.getLowBits = function getLowBits() { + return this.low; + }; + + /** + * Gets the low 32 bits as an unsigned integer. + * @this {!Long} + * @returns {number} Unsigned low bits + */ + LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() { + return this.low >>> 0; + }; + + /** + * Gets the number of bits needed to represent the absolute value of this Long. + * @this {!Long} + * @returns {number} + */ + LongPrototype.getNumBitsAbs = function getNumBitsAbs() { + if (this.isNegative()) // Unsigned Longs are never negative + return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); + var val = this.high != 0 ? this.high : this.low; + for (var bit = 31; bit > 0; bit--) + if ((val & (1 << bit)) != 0) + break; + return this.high != 0 ? bit + 33 : bit + 1; + }; + + /** + * Tests if this Long's value equals zero. + * @this {!Long} + * @returns {boolean} + */ + LongPrototype.isZero = function isZero() { + return this.high === 0 && this.low === 0; + }; + + /** + * Tests if this Long's value equals zero. This is an alias of {@link Long#isZero}. + * @returns {boolean} + */ + LongPrototype.eqz = LongPrototype.isZero; + + /** + * Tests if this Long's value is negative. + * @this {!Long} + * @returns {boolean} + */ + LongPrototype.isNegative = function isNegative() { + return !this.unsigned && this.high < 0; + }; + + /** + * Tests if this Long's value is positive or zero. + * @this {!Long} + * @returns {boolean} + */ + LongPrototype.isPositive = function isPositive() { + return this.unsigned || this.high >= 0; + }; + + /** + * Tests if this Long's value is odd. + * @this {!Long} + * @returns {boolean} + */ + LongPrototype.isOdd = function isOdd() { + return (this.low & 1) === 1; + }; + + /** + * Tests if this Long's value is even. + * @this {!Long} + * @returns {boolean} + */ + LongPrototype.isEven = function isEven() { + return (this.low & 1) === 0; + }; + + /** + * Tests if this Long's value equals the specified's. + * @this {!Long} + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.equals = function equals(other) { + if (!isLong(other)) + other = fromValue(other); + if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) + return false; + return this.high === other.high && this.low === other.low; + }; + + /** + * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.eq = LongPrototype.equals; + + /** + * Tests if this Long's value differs from the specified's. + * @this {!Long} + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.notEquals = function notEquals(other) { + return !this.eq(/* validates */ other); + }; + + /** + * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.neq = LongPrototype.notEquals; + + /** + * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.ne = LongPrototype.notEquals; + + /** + * Tests if this Long's value is less than the specified's. + * @this {!Long} + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.lessThan = function lessThan(other) { + return this.comp(/* validates */ other) < 0; + }; + + /** + * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.lt = LongPrototype.lessThan; + + /** + * Tests if this Long's value is less than or equal the specified's. + * @this {!Long} + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) { + return this.comp(/* validates */ other) <= 0; + }; + + /** + * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.lte = LongPrototype.lessThanOrEqual; + + /** + * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.le = LongPrototype.lessThanOrEqual; + + /** + * Tests if this Long's value is greater than the specified's. + * @this {!Long} + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.greaterThan = function greaterThan(other) { + return this.comp(/* validates */ other) > 0; + }; + + /** + * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.gt = LongPrototype.greaterThan; + + /** + * Tests if this Long's value is greater than or equal the specified's. + * @this {!Long} + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) { + return this.comp(/* validates */ other) >= 0; + }; + + /** + * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.gte = LongPrototype.greaterThanOrEqual; + + /** + * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ + LongPrototype.ge = LongPrototype.greaterThanOrEqual; + + /** + * Compares this Long's value with the specified's. + * @this {!Long} + * @param {!Long|number|string} other Other value + * @returns {number} 0 if they are the same, 1 if the this is greater and -1 + * if the given one is greater + */ + LongPrototype.compare = function compare(other) { + if (!isLong(other)) + other = fromValue(other); + if (this.eq(other)) + return 0; + var thisNeg = this.isNegative(), + otherNeg = other.isNegative(); + if (thisNeg && !otherNeg) + return -1; + if (!thisNeg && otherNeg) + return 1; + // At this point the sign bits are the same + if (!this.unsigned) + return this.sub(other).isNegative() ? -1 : 1; + // Both are positive if at least one is unsigned + return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1; + }; + + /** + * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}. + * @function + * @param {!Long|number|string} other Other value + * @returns {number} 0 if they are the same, 1 if the this is greater and -1 + * if the given one is greater + */ + LongPrototype.comp = LongPrototype.compare; + + /** + * Negates this Long's value. + * @this {!Long} + * @returns {!Long} Negated Long + */ + LongPrototype.negate = function negate() { + if (!this.unsigned && this.eq(MIN_VALUE)) + return MIN_VALUE; + return this.not().add(ONE); + }; + + /** + * Negates this Long's value. This is an alias of {@link Long#negate}. + * @function + * @returns {!Long} Negated Long + */ + LongPrototype.neg = LongPrototype.negate; + + /** + * Returns the sum of this and the specified Long. + * @this {!Long} + * @param {!Long|number|string} addend Addend + * @returns {!Long} Sum + */ + LongPrototype.add = function add(addend) { + if (!isLong(addend)) + addend = fromValue(addend); + + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + var a48 = this.high >>> 16; + var a32 = this.high & 0xFFFF; + var a16 = this.low >>> 16; + var a00 = this.low & 0xFFFF; + + var b48 = addend.high >>> 16; + var b32 = addend.high & 0xFFFF; + var b16 = addend.low >>> 16; + var b00 = addend.low & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 + b48; + c48 &= 0xFFFF; + return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); + }; + + /** + * Returns the difference of this and the specified Long. + * @this {!Long} + * @param {!Long|number|string} subtrahend Subtrahend + * @returns {!Long} Difference + */ + LongPrototype.subtract = function subtract(subtrahend) { + if (!isLong(subtrahend)) + subtrahend = fromValue(subtrahend); + return this.add(subtrahend.neg()); + }; + + /** + * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}. + * @function + * @param {!Long|number|string} subtrahend Subtrahend + * @returns {!Long} Difference + */ + LongPrototype.sub = LongPrototype.subtract; + + /** + * Returns the product of this and the specified Long. + * @this {!Long} + * @param {!Long|number|string} multiplier Multiplier + * @returns {!Long} Product + */ + LongPrototype.multiply = function multiply(multiplier) { + if (this.isZero()) + return this; + if (!isLong(multiplier)) + multiplier = fromValue(multiplier); + + // use wasm support if present + if (wasm) { + var low = wasm["mul"](this.low, + this.high, + multiplier.low, + multiplier.high); + return fromBits(low, wasm["get_high"](), this.unsigned); } - // We know the answer can't be zero... and actually, zero would cause - // infinite recursion since we would make no progress. - if (approxRes.isZero()) - approxRes = ONE; + if (multiplier.isZero()) + return this.unsigned ? UZERO : ZERO; + if (this.eq(MIN_VALUE)) + return multiplier.isOdd() ? MIN_VALUE : ZERO; + if (multiplier.eq(MIN_VALUE)) + return this.isOdd() ? MIN_VALUE : ZERO; - res = res.add(approxRes); - rem = rem.sub(approxRem); + if (this.isNegative()) { + if (multiplier.isNegative()) + return this.neg().mul(multiplier.neg()); + else + return this.neg().mul(multiplier).neg(); + } else if (multiplier.isNegative()) + return this.mul(multiplier.neg()).neg(); + + // If both longs are small, use float multiplication + if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24)) + return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); + + // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. + // We can skip products that would overflow. + + var a48 = this.high >>> 16; + var a32 = this.high & 0xFFFF; + var a16 = this.low >>> 16; + var a00 = this.low & 0xFFFF; + + var b48 = multiplier.high >>> 16; + var b32 = multiplier.high & 0xFFFF; + var b16 = multiplier.low >>> 16; + var b00 = multiplier.low & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xFFFF; + return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); + }; + + /** + * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}. + * @function + * @param {!Long|number|string} multiplier Multiplier + * @returns {!Long} Product + */ + LongPrototype.mul = LongPrototype.multiply; + + /** + * Returns this Long divided by the specified. The result is signed if this Long is signed or + * unsigned if this Long is unsigned. + * @this {!Long} + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Quotient + */ + LongPrototype.divide = function divide(divisor) { + if (!isLong(divisor)) + divisor = fromValue(divisor); + if (divisor.isZero()) + throw Error('division by zero'); + + // use wasm support if present + if (wasm) { + // guard against signed division overflow: the largest + // negative number / -1 would be 1 larger than the largest + // positive number, due to two's complement. + if (!this.unsigned && + this.high === -0x80000000 && + divisor.low === -1 && divisor.high === -1) { + // be consistent with non-wasm code path + return this; + } + var low = (this.unsigned ? wasm["div_u"] : wasm["div_s"])( + this.low, + this.high, + divisor.low, + divisor.high + ); + return fromBits(low, wasm["get_high"](), this.unsigned); + } + + if (this.isZero()) + return this.unsigned ? UZERO : ZERO; + var approx, rem, res; + if (!this.unsigned) { + // This section is only relevant for signed longs and is derived from the + // closure library as a whole. + if (this.eq(MIN_VALUE)) { + if (divisor.eq(ONE) || divisor.eq(NEG_ONE)) + return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE + else if (divisor.eq(MIN_VALUE)) + return ONE; + else { + // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. + var halfThis = this.shr(1); + approx = halfThis.div(divisor).shl(1); + if (approx.eq(ZERO)) { + return divisor.isNegative() ? ONE : NEG_ONE; + } else { + rem = this.sub(divisor.mul(approx)); + res = approx.add(rem.div(divisor)); + return res; + } + } + } else if (divisor.eq(MIN_VALUE)) + return this.unsigned ? UZERO : ZERO; + if (this.isNegative()) { + if (divisor.isNegative()) + return this.neg().div(divisor.neg()); + return this.neg().div(divisor).neg(); + } else if (divisor.isNegative()) + return this.div(divisor.neg()).neg(); + res = ZERO; + } else { + // The algorithm below has not been made for unsigned longs. It's therefore + // required to take special care of the MSB prior to running it. + if (!divisor.unsigned) + divisor = divisor.toUnsigned(); + if (divisor.gt(this)) + return UZERO; + if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true + return UONE; + res = UZERO; + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + rem = this; + while (rem.gte(divisor)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + var log2 = Math.ceil(Math.log(approx) / Math.LN2), + delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48), + + // Decrease the approximation until it is smaller than the remainder. Note + // that if it is too large, the product overflows and is negative. + approxRes = fromNumber(approx), + approxRem = approxRes.mul(divisor); + while (approxRem.isNegative() || approxRem.gt(rem)) { + approx -= delta; + approxRes = fromNumber(approx, this.unsigned); + approxRem = approxRes.mul(divisor); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) + approxRes = ONE; + + res = res.add(approxRes); + rem = rem.sub(approxRem); + } + return res; + }; + + /** + * Returns this Long divided by the specified. This is an alias of {@link Long#divide}. + * @function + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Quotient + */ + LongPrototype.div = LongPrototype.divide; + + /** + * Returns this Long modulo the specified. + * @this {!Long} + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Remainder + */ + LongPrototype.modulo = function modulo(divisor) { + if (!isLong(divisor)) + divisor = fromValue(divisor); + + // use wasm support if present + if (wasm) { + var low = (this.unsigned ? wasm["rem_u"] : wasm["rem_s"])( + this.low, + this.high, + divisor.low, + divisor.high + ); + return fromBits(low, wasm["get_high"](), this.unsigned); + } + + return this.sub(this.div(divisor).mul(divisor)); + }; + + /** + * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}. + * @function + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Remainder + */ + LongPrototype.mod = LongPrototype.modulo; + + /** + * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}. + * @function + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Remainder + */ + LongPrototype.rem = LongPrototype.modulo; + + /** + * Returns the bitwise NOT of this Long. + * @this {!Long} + * @returns {!Long} + */ + LongPrototype.not = function not() { + return fromBits(~this.low, ~this.high, this.unsigned); + }; + + /** + * Returns count leading zeros of this Long. + * @this {!Long} + * @returns {!number} + */ + LongPrototype.countLeadingZeros = function countLeadingZeros() { + return this.high ? Math.clz32(this.high) : Math.clz32(this.low) + 32; + }; + + /** + * Returns count leading zeros. This is an alias of {@link Long#countLeadingZeros}. + * @function + * @param {!Long} + * @returns {!number} + */ + LongPrototype.clz = LongPrototype.countLeadingZeros; + + /** + * Returns count trailing zeros of this Long. + * @this {!Long} + * @returns {!number} + */ + LongPrototype.countTrailingZeros = function countTrailingZeros() { + return this.low ? ctz32(this.low) : ctz32(this.high) + 32; + }; + + /** + * Returns count trailing zeros. This is an alias of {@link Long#countTrailingZeros}. + * @function + * @param {!Long} + * @returns {!number} + */ + LongPrototype.ctz = LongPrototype.countTrailingZeros; + + /** + * Returns the bitwise AND of this Long and the specified. + * @this {!Long} + * @param {!Long|number|string} other Other Long + * @returns {!Long} + */ + LongPrototype.and = function and(other) { + if (!isLong(other)) + other = fromValue(other); + return fromBits(this.low & other.low, this.high & other.high, this.unsigned); + }; + + /** + * Returns the bitwise OR of this Long and the specified. + * @this {!Long} + * @param {!Long|number|string} other Other Long + * @returns {!Long} + */ + LongPrototype.or = function or(other) { + if (!isLong(other)) + other = fromValue(other); + return fromBits(this.low | other.low, this.high | other.high, this.unsigned); + }; + + /** + * Returns the bitwise XOR of this Long and the given one. + * @this {!Long} + * @param {!Long|number|string} other Other Long + * @returns {!Long} + */ + LongPrototype.xor = function xor(other) { + if (!isLong(other)) + other = fromValue(other); + return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); + }; + + /** + * Returns this Long with bits shifted to the left by the given amount. + * @this {!Long} + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ + LongPrototype.shiftLeft = function shiftLeft(numBits) { + if (isLong(numBits)) + numBits = numBits.toInt(); + if ((numBits &= 63) === 0) + return this; + else if (numBits < 32) + return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); + else + return fromBits(0, this.low << (numBits - 32), this.unsigned); + }; + + /** + * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ + LongPrototype.shl = LongPrototype.shiftLeft; + + /** + * Returns this Long with bits arithmetically shifted to the right by the given amount. + * @this {!Long} + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ + LongPrototype.shiftRight = function shiftRight(numBits) { + if (isLong(numBits)) + numBits = numBits.toInt(); + if ((numBits &= 63) === 0) + return this; + else if (numBits < 32) + return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); + else + return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); + }; + + /** + * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ + LongPrototype.shr = LongPrototype.shiftRight; + + /** + * Returns this Long with bits logically shifted to the right by the given amount. + * @this {!Long} + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ + LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) { + if (isLong(numBits)) numBits = numBits.toInt(); + if ((numBits &= 63) === 0) return this; + if (numBits < 32) return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >>> numBits, this.unsigned); + if (numBits === 32) return fromBits(this.high, 0, this.unsigned); + return fromBits(this.high >>> (numBits - 32), 0, this.unsigned); + }; + + /** + * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ + LongPrototype.shru = LongPrototype.shiftRightUnsigned; + + /** + * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ + LongPrototype.shr_u = LongPrototype.shiftRightUnsigned; + + /** + * Returns this Long with bits rotated to the left by the given amount. + * @this {!Long} + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Rotated Long + */ + LongPrototype.rotateLeft = function rotateLeft(numBits) { + var b; + if (isLong(numBits)) numBits = numBits.toInt(); + if ((numBits &= 63) === 0) return this; + if (numBits === 32) return fromBits(this.high, this.low, this.unsigned); + if (numBits < 32) { + b = (32 - numBits); + return fromBits(((this.low << numBits) | (this.high >>> b)), ((this.high << numBits) | (this.low >>> b)), this.unsigned); + } + numBits -= 32; + b = (32 - numBits); + return fromBits(((this.high << numBits) | (this.low >>> b)), ((this.low << numBits) | (this.high >>> b)), this.unsigned); } - return res; -}; + /** + * Returns this Long with bits rotated to the left by the given amount. This is an alias of {@link Long#rotateLeft}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Rotated Long + */ + LongPrototype.rotl = LongPrototype.rotateLeft; -/** - * Returns this Long divided by the specified. This is an alias of {@link Long#divide}. - * @function - * @param {!Long|number|string} divisor Divisor - * @returns {!Long} Quotient - */ -LongPrototype.div = LongPrototype.divide; + /** + * Returns this Long with bits rotated to the right by the given amount. + * @this {!Long} + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Rotated Long + */ + LongPrototype.rotateRight = function rotateRight(numBits) { + var b; + if (isLong(numBits)) numBits = numBits.toInt(); + if ((numBits &= 63) === 0) return this; + if (numBits === 32) return fromBits(this.high, this.low, this.unsigned); + if (numBits < 32) { + b = (32 - numBits); + return fromBits(((this.high << b) | (this.low >>> numBits)), ((this.low << b) | (this.high >>> numBits)), this.unsigned); + } + numBits -= 32; + b = (32 - numBits); + return fromBits(((this.low << b) | (this.high >>> numBits)), ((this.high << b) | (this.low >>> numBits)), this.unsigned); + } + /** + * Returns this Long with bits rotated to the right by the given amount. This is an alias of {@link Long#rotateRight}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Rotated Long + */ + LongPrototype.rotr = LongPrototype.rotateRight; -/** - * Returns this Long modulo the specified. - * @this {!Long} - * @param {!Long|number|string} divisor Divisor - * @returns {!Long} Remainder - */ -LongPrototype.modulo = function modulo(divisor) { - if (!isLong(divisor)) - divisor = fromValue(divisor); + /** + * Converts this Long to signed. + * @this {!Long} + * @returns {!Long} Signed long + */ + LongPrototype.toSigned = function toSigned() { + if (!this.unsigned) + return this; + return fromBits(this.low, this.high, false); + }; - // use wasm support if present - if (wasm) { - var low = (this.unsigned ? wasm["rem_u"] : wasm["rem_s"])( - this.low, - this.high, - divisor.low, - divisor.high + /** + * Converts this Long to unsigned. + * @this {!Long} + * @returns {!Long} Unsigned long + */ + LongPrototype.toUnsigned = function toUnsigned() { + if (this.unsigned) + return this; + return fromBits(this.low, this.high, true); + }; + + /** + * Converts this Long to its byte representation. + * @param {boolean=} le Whether little or big endian, defaults to big endian + * @this {!Long} + * @returns {!Array.} Byte representation + */ + LongPrototype.toBytes = function toBytes(le) { + return le ? this.toBytesLE() : this.toBytesBE(); + }; + + /** + * Converts this Long to its little endian byte representation. + * @this {!Long} + * @returns {!Array.} Little endian byte representation + */ + LongPrototype.toBytesLE = function toBytesLE() { + var hi = this.high, + lo = this.low; + return [ + lo & 0xff, + lo >>> 8 & 0xff, + lo >>> 16 & 0xff, + lo >>> 24, + hi & 0xff, + hi >>> 8 & 0xff, + hi >>> 16 & 0xff, + hi >>> 24 + ]; + }; + + /** + * Converts this Long to its big endian byte representation. + * @this {!Long} + * @returns {!Array.} Big endian byte representation + */ + LongPrototype.toBytesBE = function toBytesBE() { + var hi = this.high, + lo = this.low; + return [ + hi >>> 24, + hi >>> 16 & 0xff, + hi >>> 8 & 0xff, + hi & 0xff, + lo >>> 24, + lo >>> 16 & 0xff, + lo >>> 8 & 0xff, + lo & 0xff + ]; + }; + + /** + * Creates a Long from its byte representation. + * @param {!Array.} bytes Byte representation + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @param {boolean=} le Whether little or big endian, defaults to big endian + * @returns {Long} The corresponding Long value + */ + Long.fromBytes = function fromBytes(bytes, unsigned, le) { + return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned); + }; + + /** + * Creates a Long from its little endian byte representation. + * @param {!Array.} bytes Little endian byte representation + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {Long} The corresponding Long value + */ + Long.fromBytesLE = function fromBytesLE(bytes, unsigned) { + return new Long( + bytes[0] | + bytes[1] << 8 | + bytes[2] << 16 | + bytes[3] << 24, + bytes[4] | + bytes[5] << 8 | + bytes[6] << 16 | + bytes[7] << 24, + unsigned ); - return fromBits(low, wasm["get_high"](), this.unsigned); - } + }; - return this.sub(this.div(divisor).mul(divisor)); -}; + /** + * Creates a Long from its big endian byte representation. + * @param {!Array.} bytes Big endian byte representation + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {Long} The corresponding Long value + */ + Long.fromBytesBE = function fromBytesBE(bytes, unsigned) { + return new Long( + bytes[4] << 24 | + bytes[5] << 16 | + bytes[6] << 8 | + bytes[7], + bytes[0] << 24 | + bytes[1] << 16 | + bytes[2] << 8 | + bytes[3], + unsigned + ); + }; -/** - * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}. - * @function - * @param {!Long|number|string} divisor Divisor - * @returns {!Long} Remainder - */ -LongPrototype.mod = LongPrototype.modulo; - -/** - * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}. - * @function - * @param {!Long|number|string} divisor Divisor - * @returns {!Long} Remainder - */ -LongPrototype.rem = LongPrototype.modulo; - -/** - * Returns the bitwise NOT of this Long. - * @this {!Long} - * @returns {!Long} - */ -LongPrototype.not = function not() { - return fromBits(~this.low, ~this.high, this.unsigned); -}; - -/** - * Returns count leading zeros of this Long. - * @this {!Long} - * @returns {!number} - */ -LongPrototype.countLeadingZeros = function countLeadingZeros() { - return this.high ? Math.clz32(this.high) : Math.clz32(this.low) + 32; -}; - -/** - * Returns count leading zeros. This is an alias of {@link Long#countLeadingZeros}. - * @function - * @param {!Long} - * @returns {!number} - */ -LongPrototype.clz = LongPrototype.countLeadingZeros; - -/** - * Returns count trailing zeros of this Long. - * @this {!Long} - * @returns {!number} - */ -LongPrototype.countTrailingZeros = function countTrailingZeros() { - return this.low ? ctz32(this.low) : ctz32(this.high) + 32; -}; - -/** - * Returns count trailing zeros. This is an alias of {@link Long#countTrailingZeros}. - * @function - * @param {!Long} - * @returns {!number} - */ -LongPrototype.ctz = LongPrototype.countTrailingZeros; - -/** - * Returns the bitwise AND of this Long and the specified. - * @this {!Long} - * @param {!Long|number|string} other Other Long - * @returns {!Long} - */ -LongPrototype.and = function and(other) { - if (!isLong(other)) - other = fromValue(other); - return fromBits(this.low & other.low, this.high & other.high, this.unsigned); -}; - -/** - * Returns the bitwise OR of this Long and the specified. - * @this {!Long} - * @param {!Long|number|string} other Other Long - * @returns {!Long} - */ -LongPrototype.or = function or(other) { - if (!isLong(other)) - other = fromValue(other); - return fromBits(this.low | other.low, this.high | other.high, this.unsigned); -}; - -/** - * Returns the bitwise XOR of this Long and the given one. - * @this {!Long} - * @param {!Long|number|string} other Other Long - * @returns {!Long} - */ -LongPrototype.xor = function xor(other) { - if (!isLong(other)) - other = fromValue(other); - return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); -}; - -/** - * Returns this Long with bits shifted to the left by the given amount. - * @this {!Long} - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Shifted Long - */ -LongPrototype.shiftLeft = function shiftLeft(numBits) { - if (isLong(numBits)) - numBits = numBits.toInt(); - if ((numBits &= 63) === 0) - return this; - else if (numBits < 32) - return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); - else - return fromBits(0, this.low << (numBits - 32), this.unsigned); -}; - -/** - * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}. - * @function - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Shifted Long - */ -LongPrototype.shl = LongPrototype.shiftLeft; - -/** - * Returns this Long with bits arithmetically shifted to the right by the given amount. - * @this {!Long} - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Shifted Long - */ -LongPrototype.shiftRight = function shiftRight(numBits) { - if (isLong(numBits)) - numBits = numBits.toInt(); - if ((numBits &= 63) === 0) - return this; - else if (numBits < 32) - return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); - else - return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); -}; - -/** - * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}. - * @function - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Shifted Long - */ -LongPrototype.shr = LongPrototype.shiftRight; - -/** - * Returns this Long with bits logically shifted to the right by the given amount. - * @this {!Long} - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Shifted Long - */ -LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) { - if (isLong(numBits)) numBits = numBits.toInt(); - if ((numBits &= 63) === 0) return this; - if (numBits < 32) return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >>> numBits, this.unsigned); - if (numBits === 32) return fromBits(this.high, 0, this.unsigned); - return fromBits(this.high >>> (numBits - 32), 0, this.unsigned); -}; - -/** - * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}. - * @function - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Shifted Long - */ -LongPrototype.shru = LongPrototype.shiftRightUnsigned; - -/** - * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}. - * @function - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Shifted Long - */ -LongPrototype.shr_u = LongPrototype.shiftRightUnsigned; - -/** - * Returns this Long with bits rotated to the left by the given amount. - * @this {!Long} - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Rotated Long - */ -LongPrototype.rotateLeft = function rotateLeft(numBits) { - var b; - if (isLong(numBits)) numBits = numBits.toInt(); - if ((numBits &= 63) === 0) return this; - if (numBits === 32) return fromBits(this.high, this.low, this.unsigned); - if (numBits < 32) { - b = (32 - numBits); - return fromBits(((this.low << numBits) | (this.high >>> b)), ((this.high << numBits) | (this.low >>> b)), this.unsigned); - } - numBits -= 32; - b = (32 - numBits); - return fromBits(((this.high << numBits) | (this.low >>> b)), ((this.low << numBits) | (this.high >>> b)), this.unsigned); -} -/** - * Returns this Long with bits rotated to the left by the given amount. This is an alias of {@link Long#rotateLeft}. - * @function - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Rotated Long - */ -LongPrototype.rotl = LongPrototype.rotateLeft; - -/** - * Returns this Long with bits rotated to the right by the given amount. - * @this {!Long} - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Rotated Long - */ -LongPrototype.rotateRight = function rotateRight(numBits) { - var b; - if (isLong(numBits)) numBits = numBits.toInt(); - if ((numBits &= 63) === 0) return this; - if (numBits === 32) return fromBits(this.high, this.low, this.unsigned); - if (numBits < 32) { - b = (32 - numBits); - return fromBits(((this.high << b) | (this.low >>> numBits)), ((this.low << b) | (this.high >>> numBits)), this.unsigned); - } - numBits -= 32; - b = (32 - numBits); - return fromBits(((this.low << b) | (this.high >>> numBits)), ((this.high << b) | (this.low >>> numBits)), this.unsigned); -} -/** - * Returns this Long with bits rotated to the right by the given amount. This is an alias of {@link Long#rotateRight}. - * @function - * @param {number|!Long} numBits Number of bits - * @returns {!Long} Rotated Long - */ -LongPrototype.rotr = LongPrototype.rotateRight; - -/** - * Converts this Long to signed. - * @this {!Long} - * @returns {!Long} Signed long - */ -LongPrototype.toSigned = function toSigned() { - if (!this.unsigned) - return this; - return fromBits(this.low, this.high, false); -}; - -/** - * Converts this Long to unsigned. - * @this {!Long} - * @returns {!Long} Unsigned long - */ -LongPrototype.toUnsigned = function toUnsigned() { - if (this.unsigned) - return this; - return fromBits(this.low, this.high, true); -}; - -/** - * Converts this Long to its byte representation. - * @param {boolean=} le Whether little or big endian, defaults to big endian - * @this {!Long} - * @returns {!Array.} Byte representation - */ -LongPrototype.toBytes = function toBytes(le) { - return le ? this.toBytesLE() : this.toBytesBE(); -}; - -/** - * Converts this Long to its little endian byte representation. - * @this {!Long} - * @returns {!Array.} Little endian byte representation - */ -LongPrototype.toBytesLE = function toBytesLE() { - var hi = this.high, - lo = this.low; - return [ - lo & 0xff, - lo >>> 8 & 0xff, - lo >>> 16 & 0xff, - lo >>> 24, - hi & 0xff, - hi >>> 8 & 0xff, - hi >>> 16 & 0xff, - hi >>> 24 - ]; -}; - -/** - * Converts this Long to its big endian byte representation. - * @this {!Long} - * @returns {!Array.} Big endian byte representation - */ -LongPrototype.toBytesBE = function toBytesBE() { - var hi = this.high, - lo = this.low; - return [ - hi >>> 24, - hi >>> 16 & 0xff, - hi >>> 8 & 0xff, - hi & 0xff, - lo >>> 24, - lo >>> 16 & 0xff, - lo >>> 8 & 0xff, - lo & 0xff - ]; -}; - -/** - * Creates a Long from its byte representation. - * @param {!Array.} bytes Byte representation - * @param {boolean=} unsigned Whether unsigned or not, defaults to signed - * @param {boolean=} le Whether little or big endian, defaults to big endian - * @returns {Long} The corresponding Long value - */ -Long.fromBytes = function fromBytes(bytes, unsigned, le) { - return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned); -}; - -/** - * Creates a Long from its little endian byte representation. - * @param {!Array.} bytes Little endian byte representation - * @param {boolean=} unsigned Whether unsigned or not, defaults to signed - * @returns {Long} The corresponding Long value - */ -Long.fromBytesLE = function fromBytesLE(bytes, unsigned) { - return new Long( - bytes[0] | - bytes[1] << 8 | - bytes[2] << 16 | - bytes[3] << 24, - bytes[4] | - bytes[5] << 8 | - bytes[6] << 16 | - bytes[7] << 24, - unsigned - ); -}; - -/** - * Creates a Long from its big endian byte representation. - * @param {!Array.} bytes Big endian byte representation - * @param {boolean=} unsigned Whether unsigned or not, defaults to signed - * @returns {Long} The corresponding Long value - */ -Long.fromBytesBE = function fromBytesBE(bytes, unsigned) { - return new Long( - bytes[4] << 24 | - bytes[5] << 16 | - bytes[6] << 8 | - bytes[7], - bytes[0] << 24 | - bytes[1] << 16 | - bytes[2] << 8 | - bytes[3], - unsigned - ); -}; + return Long; +}(); export default Long; \ No newline at end of file diff --git a/libraries/modpow.js b/libraries/modpow.js deleted file mode 100644 index ae00c45..0000000 --- a/libraries/modpow.js +++ /dev/null @@ -1,257 +0,0 @@ -// https://github.com/juanelas/bigint-mod-arith - -/** - * Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0 - * - * @param a - * - * @returns The absolute value of a - */ -function abs(a) { - return (a >= 0) ? a : -a; -} - -/** - * Returns the bitlength of a number - * - * @param a - * @returns The bit length - */ -function bitLength(a) { - if (typeof a === 'number') - a = BigInt(a); - if (a === 1n) { - return 1; - } - let bits = 1; - do { - bits++; - } while ((a >>= 1n) > 1n); - return bits; -} - -/** - * An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm. - * Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b). - * - * @param a - * @param b - * - * @throws {RangeError} - * This excepction is thrown if a or b are less than 0 - * - * @returns A triple (g, x, y), such that ax + by = g = gcd(a, b). - */ -function eGcd(a, b) { - if (typeof a === 'number') - a = BigInt(a); - if (typeof b === 'number') - b = BigInt(b); - if (a <= 0n || b <= 0n) - throw new RangeError('a and b MUST be > 0'); // a and b MUST be positive - let x = 0n; - let y = 1n; - let u = 1n; - let v = 0n; - while (a !== 0n) { - const q = b / a; - const r = b % a; - const m = x - (u * q); - const n = y - (v * q); - b = a; - a = r; - x = u; - y = v; - u = m; - v = n; - } - return { - g: b, - x: x, - y: y - }; -} - -/** - * Greatest-common divisor of two integers based on the iterative binary algorithm. - * - * @param a - * @param b - * - * @returns The greatest common divisor of a and b - */ -function gcd(a, b) { - let aAbs = (typeof a === 'number') ? BigInt(abs(a)) : abs(a); - let bAbs = (typeof b === 'number') ? BigInt(abs(b)) : abs(b); - if (aAbs === 0n) { - return bAbs; - } else if (bAbs === 0n) { - return aAbs; - } - let shift = 0n; - while (((aAbs | bAbs) & 1n) === 0n) { - aAbs >>= 1n; - bAbs >>= 1n; - shift++; - } - while ((aAbs & 1n) === 0n) - aAbs >>= 1n; - do { - while ((bAbs & 1n) === 0n) - bAbs >>= 1n; - if (aAbs > bAbs) { - const x = aAbs; - aAbs = bAbs; - bAbs = x; - } - bAbs -= aAbs; - } while (bAbs !== 0n); - // rescale - return aAbs << shift; -} - -/** - * The least common multiple computed as abs(a*b)/gcd(a,b) - * @param a - * @param b - * - * @returns The least common multiple of a and b - */ -function lcm(a, b) { - if (typeof a === 'number') - a = BigInt(a); - if (typeof b === 'number') - b = BigInt(b); - if (a === 0n && b === 0n) - return BigInt(0); - // return abs(a * b) as bigint / gcd(a, b) - return abs((a / gcd(a, b)) * b); -} - -/** - * Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b - * - * @param a - * @param b - * - * @returns Maximum of numbers a and b - */ -function max(a, b) { - return (a >= b) ? a : b; -} - -/** - * Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b - * - * @param a - * @param b - * - * @returns Minimum of numbers a and b - */ -function min(a, b) { - return (a >= b) ? b : a; -} - -/** - * Finds the smallest positive element that is congruent to a in modulo n - * - * @remarks - * a and b must be the same type, either number or bigint - * - * @param a - An integer - * @param n - The modulo - * - * @throws {RangeError} - * Excpeption thrown when n is not > 0 - * - * @returns A bigint with the smallest positive representation of a modulo n - */ -function toZn(a, n) { - if (typeof a === 'number') - a = BigInt(a); - if (typeof n === 'number') - n = BigInt(n); - if (n <= 0n) { - throw new RangeError('n must be > 0'); - } - const aZn = a % n; - return (aZn < 0n) ? aZn + n : aZn; -} - -/** - * Modular inverse. - * - * @param a The number to find an inverse for - * @param n The modulo - * - * @throws {RangeError} - * Excpeption thorwn when a does not have inverse modulo n - * - * @returns The inverse modulo n - */ -function modInv(a, n) { - const egcd = eGcd(toZn(a, n), n); - if (egcd.g !== 1n) { - throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`); // modular inverse does not exist - } else { - return toZn(egcd.x, n); - } -} - -/** - * Modular exponentiation b**e mod n. Currently using the right-to-left binary method - * - * @param b base - * @param e exponent - * @param n modulo - * - * @throws {RangeError} - * Excpeption thrown when n is not > 0 - * - * @returns b**e mod n - */ -function modPow(b, e, n) { - if (typeof b === 'number') - b = BigInt(b); - if (typeof e === 'number') - e = BigInt(e); - if (typeof n === 'number') - n = BigInt(n); - if (n <= 0n) { - throw new RangeError('n must be > 0'); - } else if (n === 1n) { - return 0n; - } - b = toZn(b, n); - if (e < 0n) { - return modInv(modPow(b, abs(e), n), n); - } - let r = 1n; - while (e > 0) { - if ((e % 2n) === 1n) { - r = r * b % n; - } - e = e / 2n; - b = b ** 2n % n; - } - return r; -} - -function bytesToBigInt(bytes) { - return BigInt("0x" + Array.from(bytes, byte => { - return ('0' + (byte & 0xFF).toString(16)).slice(-2); - }).join('')); -} - -function bigIntToBytes(bigInt) { - let hex = bigInt.toString(16); - - // Convert hex to bytes - let bytes = []; - for (let c = 0; c < hex.length; c += 2) { - bytes.push(parseInt(hex.substr(c, 2), 16)); - } - return bytes; -} - -export {abs, bitLength, eGcd, gcd, lcm, max, min, modInv, modPow, toZn, bytesToBigInt, bigIntToBytes}; \ No newline at end of file diff --git a/libraries/pako.es5.min.js b/libraries/pako.es5.min.js new file mode 100644 index 0000000..5b9a3e0 --- /dev/null +++ b/libraries/pako.es5.min.js @@ -0,0 +1,8027 @@ + +/*! pako 2.0.4 https://github.com/nodeca/pako @license (MIT AND Zlib) */ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.pako = {})); +}(this, (function (exports) { 'use strict'; + + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + + /* eslint-disable space-unary-ops */ + + /* Public constants ==========================================================*/ + + /* ===========================================================================*/ + //const Z_FILTERED = 1; + //const Z_HUFFMAN_ONLY = 2; + //const Z_RLE = 3; + + var Z_FIXED$1 = 4; //const Z_DEFAULT_STRATEGY = 0; + + /* Possible values of the data_type field (though see inflate()) */ + + var Z_BINARY = 0; + var Z_TEXT = 1; //const Z_ASCII = 1; // = Z_TEXT + + var Z_UNKNOWN$1 = 2; + /*============================================================================*/ + + function zero$1(buf) { + var len = buf.length; + + while (--len >= 0) { + buf[len] = 0; + } + } // From zutil.h + + + var STORED_BLOCK = 0; + var STATIC_TREES = 1; + var DYN_TREES = 2; + /* The three kinds of block type */ + + var MIN_MATCH$1 = 3; + var MAX_MATCH$1 = 258; + /* The minimum and maximum match lengths */ + // From deflate.h + + /* =========================================================================== + * Internal compression state. + */ + + var LENGTH_CODES$1 = 29; + /* number of length codes, not counting the special END_BLOCK code */ + + var LITERALS$1 = 256; + /* number of literal bytes 0..255 */ + + var L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1; + /* number of Literal or Length codes, including the END_BLOCK code */ + + var D_CODES$1 = 30; + /* number of distance codes */ + + var BL_CODES$1 = 19; + /* number of codes used to transfer the bit lengths */ + + var HEAP_SIZE$1 = 2 * L_CODES$1 + 1; + /* maximum heap size */ + + var MAX_BITS$1 = 15; + /* All codes must not exceed MAX_BITS bits */ + + var Buf_size = 16; + /* size of bit buffer in bi_buf */ + + /* =========================================================================== + * Constants + */ + + var MAX_BL_BITS = 7; + /* Bit length codes must not exceed MAX_BL_BITS bits */ + + var END_BLOCK = 256; + /* end of block literal code */ + + var REP_3_6 = 16; + /* repeat previous bit length 3-6 times (2 bits of repeat count) */ + + var REPZ_3_10 = 17; + /* repeat a zero length 3-10 times (3 bits of repeat count) */ + + var REPZ_11_138 = 18; + /* repeat a zero length 11-138 times (7 bits of repeat count) */ + + /* eslint-disable comma-spacing,array-bracket-spacing */ + + var extra_lbits = + /* extra bits for each length code */ + new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]); + var extra_dbits = + /* extra bits for each distance code */ + new Uint8Array([0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13]); + var extra_blbits = + /* extra bits for each bit length code */ + new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7]); + var bl_order = new Uint8Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]); + /* eslint-enable comma-spacing,array-bracket-spacing */ + + /* The lengths of the bit length codes are sent in order of decreasing + * probability, to avoid transmitting the lengths for unused bit length codes. + */ + + /* =========================================================================== + * Local data. These are initialized only once. + */ + // We pre-fill arrays with 0 to avoid uninitialized gaps + + var DIST_CODE_LEN = 512; + /* see definition of array dist_code below */ + // !!!! Use flat array instead of structure, Freq = i*2, Len = i*2+1 + + var static_ltree = new Array((L_CODES$1 + 2) * 2); + zero$1(static_ltree); + /* The static literal tree. Since the bit lengths are imposed, there is no + * need for the L_CODES extra codes used during heap construction. However + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init + * below). + */ + + var static_dtree = new Array(D_CODES$1 * 2); + zero$1(static_dtree); + /* The static distance tree. (Actually a trivial tree since all codes use + * 5 bits.) + */ + + var _dist_code = new Array(DIST_CODE_LEN); + + zero$1(_dist_code); + /* Distance codes. The first 256 values correspond to the distances + * 3 .. 258, the last 256 values correspond to the top 8 bits of + * the 15 bit distances. + */ + + var _length_code = new Array(MAX_MATCH$1 - MIN_MATCH$1 + 1); + + zero$1(_length_code); + /* length code for each normalized match length (0 == MIN_MATCH) */ + + var base_length = new Array(LENGTH_CODES$1); + zero$1(base_length); + /* First normalized length for each code (0 = MIN_MATCH) */ + + var base_dist = new Array(D_CODES$1); + zero$1(base_dist); + /* First normalized distance for each code (0 = distance of 1) */ + + function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) { + this.static_tree = static_tree; + /* static tree or NULL */ + + this.extra_bits = extra_bits; + /* extra bits for each code or NULL */ + + this.extra_base = extra_base; + /* base index for extra_bits */ + + this.elems = elems; + /* max number of elements in the tree */ + + this.max_length = max_length; + /* max bit length for the codes */ + // show if `static_tree` has data or dummy - needed for monomorphic objects + + this.has_stree = static_tree && static_tree.length; + } + + var static_l_desc; + var static_d_desc; + var static_bl_desc; + + function TreeDesc(dyn_tree, stat_desc) { + this.dyn_tree = dyn_tree; + /* the dynamic tree */ + + this.max_code = 0; + /* largest code with non zero frequency */ + + this.stat_desc = stat_desc; + /* the corresponding static tree */ + } + + var d_code = function d_code(dist) { + return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)]; + }; + /* =========================================================================== + * Output a short LSB first on the stream. + * IN assertion: there is enough room in pendingBuf. + */ + + + var put_short = function put_short(s, w) { + // put_byte(s, (uch)((w) & 0xff)); + // put_byte(s, (uch)((ush)(w) >> 8)); + s.pending_buf[s.pending++] = w & 0xff; + s.pending_buf[s.pending++] = w >>> 8 & 0xff; + }; + /* =========================================================================== + * Send a value on a given number of bits. + * IN assertion: length <= 16 and value fits in length bits. + */ + + + var send_bits = function send_bits(s, value, length) { + if (s.bi_valid > Buf_size - length) { + s.bi_buf |= value << s.bi_valid & 0xffff; + put_short(s, s.bi_buf); + s.bi_buf = value >> Buf_size - s.bi_valid; + s.bi_valid += length - Buf_size; + } else { + s.bi_buf |= value << s.bi_valid & 0xffff; + s.bi_valid += length; + } + }; + + var send_code = function send_code(s, c, tree) { + send_bits(s, tree[c * 2] + /*.Code*/ + , tree[c * 2 + 1] + /*.Len*/ + ); + }; + /* =========================================================================== + * Reverse the first len bits of a code, using straightforward code (a faster + * method would use a table) + * IN assertion: 1 <= len <= 15 + */ + + + var bi_reverse = function bi_reverse(code, len) { + var res = 0; + + do { + res |= code & 1; + code >>>= 1; + res <<= 1; + } while (--len > 0); + + return res >>> 1; + }; + /* =========================================================================== + * Flush the bit buffer, keeping at most 7 bits in it. + */ + + + var bi_flush = function bi_flush(s) { + if (s.bi_valid === 16) { + put_short(s, s.bi_buf); + s.bi_buf = 0; + s.bi_valid = 0; + } else if (s.bi_valid >= 8) { + s.pending_buf[s.pending++] = s.bi_buf & 0xff; + s.bi_buf >>= 8; + s.bi_valid -= 8; + } + }; + /* =========================================================================== + * Compute the optimal bit lengths for a tree and update the total bit length + * for the current block. + * IN assertion: the fields freq and dad are set, heap[heap_max] and + * above are the tree nodes sorted by increasing frequency. + * OUT assertions: the field len is set to the optimal bit length, the + * array bl_count contains the frequencies for each bit length. + * The length opt_len is updated; static_len is also updated if stree is + * not null. + */ + + + var gen_bitlen = function gen_bitlen(s, desc) // deflate_state *s; + // tree_desc *desc; /* the tree descriptor */ + { + var tree = desc.dyn_tree; + var max_code = desc.max_code; + var stree = desc.stat_desc.static_tree; + var has_stree = desc.stat_desc.has_stree; + var extra = desc.stat_desc.extra_bits; + var base = desc.stat_desc.extra_base; + var max_length = desc.stat_desc.max_length; + var h; + /* heap index */ + + var n, m; + /* iterate over the tree elements */ + + var bits; + /* bit length */ + + var xbits; + /* extra bits */ + + var f; + /* frequency */ + + var overflow = 0; + /* number of elements with bit length too large */ + + for (bits = 0; bits <= MAX_BITS$1; bits++) { + s.bl_count[bits] = 0; + } + /* In a first pass, compute the optimal bit lengths (which may + * overflow in the case of the bit length tree). + */ + + + tree[s.heap[s.heap_max] * 2 + 1] + /*.Len*/ + = 0; + /* root of the heap */ + + for (h = s.heap_max + 1; h < HEAP_SIZE$1; h++) { + n = s.heap[h]; + bits = tree[tree[n * 2 + 1] + /*.Dad*/ + * 2 + 1] + /*.Len*/ + + 1; + + if (bits > max_length) { + bits = max_length; + overflow++; + } + + tree[n * 2 + 1] + /*.Len*/ + = bits; + /* We overwrite tree[n].Dad which is no longer needed */ + + if (n > max_code) { + continue; + } + /* not a leaf node */ + + + s.bl_count[bits]++; + xbits = 0; + + if (n >= base) { + xbits = extra[n - base]; + } + + f = tree[n * 2] + /*.Freq*/ + ; + s.opt_len += f * (bits + xbits); + + if (has_stree) { + s.static_len += f * (stree[n * 2 + 1] + /*.Len*/ + + xbits); + } + } + + if (overflow === 0) { + return; + } // Trace((stderr,"\nbit length overflow\n")); + + /* This happens for example on obj2 and pic of the Calgary corpus */ + + /* Find the first bit length which could increase: */ + + + do { + bits = max_length - 1; + + while (s.bl_count[bits] === 0) { + bits--; + } + + s.bl_count[bits]--; + /* move one leaf down the tree */ + + s.bl_count[bits + 1] += 2; + /* move one overflow item as its brother */ + + s.bl_count[max_length]--; + /* The brother of the overflow item also moves one step up, + * but this does not affect bl_count[max_length] + */ + + overflow -= 2; + } while (overflow > 0); + /* Now recompute all bit lengths, scanning in increasing frequency. + * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all + * lengths instead of fixing only the wrong ones. This idea is taken + * from 'ar' written by Haruhiko Okumura.) + */ + + + for (bits = max_length; bits !== 0; bits--) { + n = s.bl_count[bits]; + + while (n !== 0) { + m = s.heap[--h]; + + if (m > max_code) { + continue; + } + + if (tree[m * 2 + 1] + /*.Len*/ + !== bits) { + // Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); + s.opt_len += (bits - tree[m * 2 + 1] + /*.Len*/ + ) * tree[m * 2] + /*.Freq*/ + ; + tree[m * 2 + 1] + /*.Len*/ + = bits; + } + + n--; + } + } + }; + /* =========================================================================== + * Generate the codes for a given tree and bit counts (which need not be + * optimal). + * IN assertion: the array bl_count contains the bit length statistics for + * the given tree and the field len is set for all tree elements. + * OUT assertion: the field code is set for all tree elements of non + * zero code length. + */ + + + var gen_codes = function gen_codes(tree, max_code, bl_count) // ct_data *tree; /* the tree to decorate */ + // int max_code; /* largest code with non zero frequency */ + // ushf *bl_count; /* number of codes at each bit length */ + { + var next_code = new Array(MAX_BITS$1 + 1); + /* next code value for each bit length */ + + var code = 0; + /* running code value */ + + var bits; + /* bit index */ + + var n; + /* code index */ + + /* The distribution counts are first used to generate the code values + * without bit reversal. + */ + + for (bits = 1; bits <= MAX_BITS$1; bits++) { + next_code[bits] = code = code + bl_count[bits - 1] << 1; + } + /* Check that the bit counts in bl_count are consistent. The last code + * must be all ones. + */ + //Assert (code + bl_count[MAX_BITS]-1 == (1< length code (0..28) */ + + length = 0; + + for (code = 0; code < LENGTH_CODES$1 - 1; code++) { + base_length[code] = length; + + for (n = 0; n < 1 << extra_lbits[code]; n++) { + _length_code[length++] = code; + } + } //Assert (length == 256, "tr_static_init: length != 256"); + + /* Note that the length 255 (match length 258) can be represented + * in two different ways: code 284 + 5 bits or code 285, so we + * overwrite length_code[255] to use the best encoding: + */ + + + _length_code[length - 1] = code; + /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ + + dist = 0; + + for (code = 0; code < 16; code++) { + base_dist[code] = dist; + + for (n = 0; n < 1 << extra_dbits[code]; n++) { + _dist_code[dist++] = code; + } + } //Assert (dist == 256, "tr_static_init: dist != 256"); + + + dist >>= 7; + /* from now on, all distances are divided by 128 */ + + for (; code < D_CODES$1; code++) { + base_dist[code] = dist << 7; + + for (n = 0; n < 1 << extra_dbits[code] - 7; n++) { + _dist_code[256 + dist++] = code; + } + } //Assert (dist == 256, "tr_static_init: 256+dist != 512"); + + /* Construct the codes of the static literal tree */ + + + for (bits = 0; bits <= MAX_BITS$1; bits++) { + bl_count[bits] = 0; + } + + n = 0; + + while (n <= 143) { + static_ltree[n * 2 + 1] + /*.Len*/ + = 8; + n++; + bl_count[8]++; + } + + while (n <= 255) { + static_ltree[n * 2 + 1] + /*.Len*/ + = 9; + n++; + bl_count[9]++; + } + + while (n <= 279) { + static_ltree[n * 2 + 1] + /*.Len*/ + = 7; + n++; + bl_count[7]++; + } + + while (n <= 287) { + static_ltree[n * 2 + 1] + /*.Len*/ + = 8; + n++; + bl_count[8]++; + } + /* Codes 286 and 287 do not exist, but we must include them in the + * tree construction to get a canonical Huffman tree (longest code + * all ones) + */ + + + gen_codes(static_ltree, L_CODES$1 + 1, bl_count); + /* The static distance tree is trivial: */ + + for (n = 0; n < D_CODES$1; n++) { + static_dtree[n * 2 + 1] + /*.Len*/ + = 5; + static_dtree[n * 2] + /*.Code*/ + = bi_reverse(n, 5); + } // Now data ready and we can init static trees + + + static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS$1 + 1, L_CODES$1, MAX_BITS$1); + static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES$1, MAX_BITS$1); + static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES$1, MAX_BL_BITS); //static_init_done = true; + }; + /* =========================================================================== + * Initialize a new block. + */ + + + var init_block = function init_block(s) { + var n; + /* iterates over tree elements */ + + /* Initialize the trees. */ + + for (n = 0; n < L_CODES$1; n++) { + s.dyn_ltree[n * 2] + /*.Freq*/ + = 0; + } + + for (n = 0; n < D_CODES$1; n++) { + s.dyn_dtree[n * 2] + /*.Freq*/ + = 0; + } + + for (n = 0; n < BL_CODES$1; n++) { + s.bl_tree[n * 2] + /*.Freq*/ + = 0; + } + + s.dyn_ltree[END_BLOCK * 2] + /*.Freq*/ + = 1; + s.opt_len = s.static_len = 0; + s.last_lit = s.matches = 0; + }; + /* =========================================================================== + * Flush the bit buffer and align the output on a byte boundary + */ + + + var bi_windup = function bi_windup(s) { + if (s.bi_valid > 8) { + put_short(s, s.bi_buf); + } else if (s.bi_valid > 0) { + //put_byte(s, (Byte)s->bi_buf); + s.pending_buf[s.pending++] = s.bi_buf; + } + + s.bi_buf = 0; + s.bi_valid = 0; + }; + /* =========================================================================== + * Copy a stored block, storing first the length and its + * one's complement if requested. + */ + + + var copy_block = function copy_block(s, buf, len, header) //DeflateState *s; + //charf *buf; /* the input data */ + //unsigned len; /* its length */ + //int header; /* true if block header must be written */ + { + bi_windup(s); + /* align on byte boundary */ + + if (header) { + put_short(s, len); + put_short(s, ~len); + } // while (len--) { + // put_byte(s, *buf++); + // } + + + s.pending_buf.set(s.window.subarray(buf, buf + len), s.pending); + s.pending += len; + }; + /* =========================================================================== + * Compares to subtrees, using the tree depth as tie breaker when + * the subtrees have equal frequency. This minimizes the worst case length. + */ + + + var smaller = function smaller(tree, n, m, depth) { + var _n2 = n * 2; + + var _m2 = m * 2; + + return tree[_n2] + /*.Freq*/ + < tree[_m2] + /*.Freq*/ + || tree[_n2] + /*.Freq*/ + === tree[_m2] + /*.Freq*/ + && depth[n] <= depth[m]; + }; + /* =========================================================================== + * Restore the heap property by moving down the tree starting at node k, + * exchanging a node with the smallest of its two sons if necessary, stopping + * when the heap property is re-established (each father smaller than its + * two sons). + */ + + + var pqdownheap = function pqdownheap(s, tree, k) // deflate_state *s; + // ct_data *tree; /* the tree to restore */ + // int k; /* node to move down */ + { + var v = s.heap[k]; + var j = k << 1; + /* left son of k */ + + while (j <= s.heap_len) { + /* Set j to the smallest of the two sons: */ + if (j < s.heap_len && smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) { + j++; + } + /* Exit if v is smaller than both sons */ + + + if (smaller(tree, v, s.heap[j], s.depth)) { + break; + } + /* Exchange v with the smallest son */ + + + s.heap[k] = s.heap[j]; + k = j; + /* And continue down the tree, setting j to the left son of k */ + + j <<= 1; + } + + s.heap[k] = v; + }; // inlined manually + // const SMALLEST = 1; + + /* =========================================================================== + * Send the block data compressed using the given Huffman trees + */ + + + var compress_block = function compress_block(s, ltree, dtree) // deflate_state *s; + // const ct_data *ltree; /* literal tree */ + // const ct_data *dtree; /* distance tree */ + { + var dist; + /* distance of matched string */ + + var lc; + /* match length or unmatched char (if dist == 0) */ + + var lx = 0; + /* running index in l_buf */ + + var code; + /* the code to send */ + + var extra; + /* number of extra bits to send */ + + if (s.last_lit !== 0) { + do { + dist = s.pending_buf[s.d_buf + lx * 2] << 8 | s.pending_buf[s.d_buf + lx * 2 + 1]; + lc = s.pending_buf[s.l_buf + lx]; + lx++; + + if (dist === 0) { + send_code(s, lc, ltree); + /* send a literal byte */ + //Tracecv(isgraph(lc), (stderr," '%c' ", lc)); + } else { + /* Here, lc is the match length - MIN_MATCH */ + code = _length_code[lc]; + send_code(s, code + LITERALS$1 + 1, ltree); + /* send the length code */ + + extra = extra_lbits[code]; + + if (extra !== 0) { + lc -= base_length[code]; + send_bits(s, lc, extra); + /* send the extra length bits */ + } + + dist--; + /* dist is now the match distance - 1 */ + + code = d_code(dist); //Assert (code < D_CODES, "bad d_code"); + + send_code(s, code, dtree); + /* send the distance code */ + + extra = extra_dbits[code]; + + if (extra !== 0) { + dist -= base_dist[code]; + send_bits(s, dist, extra); + /* send the extra distance bits */ + } + } + /* literal or match pair ? */ + + /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ + //Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, + // "pendingBuf overflow"); + + } while (lx < s.last_lit); + } + + send_code(s, END_BLOCK, ltree); + }; + /* =========================================================================== + * Construct one Huffman tree and assigns the code bit strings and lengths. + * Update the total bit length for the current block. + * IN assertion: the field freq is set for all tree elements. + * OUT assertions: the fields len and code are set to the optimal bit length + * and corresponding code. The length opt_len is updated; static_len is + * also updated if stree is not null. The field max_code is set. + */ + + + var build_tree = function build_tree(s, desc) // deflate_state *s; + // tree_desc *desc; /* the tree descriptor */ + { + var tree = desc.dyn_tree; + var stree = desc.stat_desc.static_tree; + var has_stree = desc.stat_desc.has_stree; + var elems = desc.stat_desc.elems; + var n, m; + /* iterate over heap elements */ + + var max_code = -1; + /* largest code with non zero frequency */ + + var node; + /* new node being created */ + + /* Construct the initial heap, with least frequent element in + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. + * heap[0] is not used. + */ + + s.heap_len = 0; + s.heap_max = HEAP_SIZE$1; + + for (n = 0; n < elems; n++) { + if (tree[n * 2] + /*.Freq*/ + !== 0) { + s.heap[++s.heap_len] = max_code = n; + s.depth[n] = 0; + } else { + tree[n * 2 + 1] + /*.Len*/ + = 0; + } + } + /* The pkzip format requires that at least one distance code exists, + * and that at least one bit should be sent even if there is only one + * possible code. So to avoid special checks later on we force at least + * two codes of non zero frequency. + */ + + + while (s.heap_len < 2) { + node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0; + tree[node * 2] + /*.Freq*/ + = 1; + s.depth[node] = 0; + s.opt_len--; + + if (has_stree) { + s.static_len -= stree[node * 2 + 1] + /*.Len*/ + ; + } + /* node is 0 or 1 so it does not have extra bits */ + + } + + desc.max_code = max_code; + /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, + * establish sub-heaps of increasing lengths: + */ + + for (n = s.heap_len >> 1 + /*int /2*/ + ; n >= 1; n--) { + pqdownheap(s, tree, n); + } + /* Construct the Huffman tree by repeatedly combining the least two + * frequent nodes. + */ + + + node = elems; + /* next internal node of the tree */ + + do { + //pqremove(s, tree, n); /* n = node of least frequency */ + + /*** pqremove ***/ + n = s.heap[1 + /*SMALLEST*/ + ]; + s.heap[1 + /*SMALLEST*/ + ] = s.heap[s.heap_len--]; + pqdownheap(s, tree, 1 + /*SMALLEST*/ + ); + /***/ + + m = s.heap[1 + /*SMALLEST*/ + ]; + /* m = node of next least frequency */ + + s.heap[--s.heap_max] = n; + /* keep the nodes sorted by frequency */ + + s.heap[--s.heap_max] = m; + /* Create a new node father of n and m */ + + tree[node * 2] + /*.Freq*/ + = tree[n * 2] + /*.Freq*/ + + tree[m * 2] + /*.Freq*/ + ; + s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1; + tree[n * 2 + 1] + /*.Dad*/ + = tree[m * 2 + 1] + /*.Dad*/ + = node; + /* and insert the new node in the heap */ + + s.heap[1 + /*SMALLEST*/ + ] = node++; + pqdownheap(s, tree, 1 + /*SMALLEST*/ + ); + } while (s.heap_len >= 2); + + s.heap[--s.heap_max] = s.heap[1 + /*SMALLEST*/ + ]; + /* At this point, the fields freq and dad are set. We can now + * generate the bit lengths. + */ + + gen_bitlen(s, desc); + /* The field len is now set, we can generate the bit codes */ + + gen_codes(tree, max_code, s.bl_count); + }; + /* =========================================================================== + * Scan a literal or distance tree to determine the frequencies of the codes + * in the bit length tree. + */ + + + var scan_tree = function scan_tree(s, tree, max_code) // deflate_state *s; + // ct_data *tree; /* the tree to be scanned */ + // int max_code; /* and its largest code of non zero frequency */ + { + var n; + /* iterates over all tree elements */ + + var prevlen = -1; + /* last emitted length */ + + var curlen; + /* length of current code */ + + var nextlen = tree[0 * 2 + 1] + /*.Len*/ + ; + /* length of next code */ + + var count = 0; + /* repeat count of the current code */ + + var max_count = 7; + /* max repeat count */ + + var min_count = 4; + /* min repeat count */ + + if (nextlen === 0) { + max_count = 138; + min_count = 3; + } + + tree[(max_code + 1) * 2 + 1] + /*.Len*/ + = 0xffff; + /* guard */ + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; + nextlen = tree[(n + 1) * 2 + 1] + /*.Len*/ + ; + + if (++count < max_count && curlen === nextlen) { + continue; + } else if (count < min_count) { + s.bl_tree[curlen * 2] + /*.Freq*/ + += count; + } else if (curlen !== 0) { + if (curlen !== prevlen) { + s.bl_tree[curlen * 2] /*.Freq*/++; + } + + s.bl_tree[REP_3_6 * 2] /*.Freq*/++; + } else if (count <= 10) { + s.bl_tree[REPZ_3_10 * 2] /*.Freq*/++; + } else { + s.bl_tree[REPZ_11_138 * 2] /*.Freq*/++; + } + + count = 0; + prevlen = curlen; + + if (nextlen === 0) { + max_count = 138; + min_count = 3; + } else if (curlen === nextlen) { + max_count = 6; + min_count = 3; + } else { + max_count = 7; + min_count = 4; + } + } + }; + /* =========================================================================== + * Send a literal or distance tree in compressed form, using the codes in + * bl_tree. + */ + + + var send_tree = function send_tree(s, tree, max_code) // deflate_state *s; + // ct_data *tree; /* the tree to be scanned */ + // int max_code; /* and its largest code of non zero frequency */ + { + var n; + /* iterates over all tree elements */ + + var prevlen = -1; + /* last emitted length */ + + var curlen; + /* length of current code */ + + var nextlen = tree[0 * 2 + 1] + /*.Len*/ + ; + /* length of next code */ + + var count = 0; + /* repeat count of the current code */ + + var max_count = 7; + /* max repeat count */ + + var min_count = 4; + /* min repeat count */ + + /* tree[max_code+1].Len = -1; */ + + /* guard already set */ + + if (nextlen === 0) { + max_count = 138; + min_count = 3; + } + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; + nextlen = tree[(n + 1) * 2 + 1] + /*.Len*/ + ; + + if (++count < max_count && curlen === nextlen) { + continue; + } else if (count < min_count) { + do { + send_code(s, curlen, s.bl_tree); + } while (--count !== 0); + } else if (curlen !== 0) { + if (curlen !== prevlen) { + send_code(s, curlen, s.bl_tree); + count--; + } //Assert(count >= 3 && count <= 6, " 3_6?"); + + + send_code(s, REP_3_6, s.bl_tree); + send_bits(s, count - 3, 2); + } else if (count <= 10) { + send_code(s, REPZ_3_10, s.bl_tree); + send_bits(s, count - 3, 3); + } else { + send_code(s, REPZ_11_138, s.bl_tree); + send_bits(s, count - 11, 7); + } + + count = 0; + prevlen = curlen; + + if (nextlen === 0) { + max_count = 138; + min_count = 3; + } else if (curlen === nextlen) { + max_count = 6; + min_count = 3; + } else { + max_count = 7; + min_count = 4; + } + } + }; + /* =========================================================================== + * Construct the Huffman tree for the bit lengths and return the index in + * bl_order of the last bit length code to send. + */ + + + var build_bl_tree = function build_bl_tree(s) { + var max_blindex; + /* index of last bit length code of non zero freq */ + + /* Determine the bit length frequencies for literal and distance trees */ + + scan_tree(s, s.dyn_ltree, s.l_desc.max_code); + scan_tree(s, s.dyn_dtree, s.d_desc.max_code); + /* Build the bit length tree: */ + + build_tree(s, s.bl_desc); + /* opt_len now includes the length of the tree representations, except + * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. + */ + + /* Determine the number of bit length codes to send. The pkzip format + * requires that at least 4 bit length codes be sent. (appnote.txt says + * 3 but the actual value used is 4.) + */ + + for (max_blindex = BL_CODES$1 - 1; max_blindex >= 3; max_blindex--) { + if (s.bl_tree[bl_order[max_blindex] * 2 + 1] + /*.Len*/ + !== 0) { + break; + } + } + /* Update opt_len to include the bit length tree and counts */ + + + s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; //Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", + // s->opt_len, s->static_len)); + + return max_blindex; + }; + /* =========================================================================== + * Send the header for a block using dynamic Huffman trees: the counts, the + * lengths of the bit length codes, the literal tree and the distance tree. + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. + */ + + + var send_all_trees = function send_all_trees(s, lcodes, dcodes, blcodes) // deflate_state *s; + // int lcodes, dcodes, blcodes; /* number of codes for each tree */ + { + var rank; + /* index in bl_order */ + //Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); + //Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, + // "too many codes"); + //Tracev((stderr, "\nbl counts: ")); + + send_bits(s, lcodes - 257, 5); + /* not +255 as stated in appnote.txt */ + + send_bits(s, dcodes - 1, 5); + send_bits(s, blcodes - 4, 4); + /* not -3 as stated in appnote.txt */ + + for (rank = 0; rank < blcodes; rank++) { + //Tracev((stderr, "\nbl code %2d ", bl_order[rank])); + send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1] + /*.Len*/ + , 3); + } //Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); + + + send_tree(s, s.dyn_ltree, lcodes - 1); + /* literal tree */ + //Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); + + send_tree(s, s.dyn_dtree, dcodes - 1); + /* distance tree */ + //Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); + }; + /* =========================================================================== + * Check if the data type is TEXT or BINARY, using the following algorithm: + * - TEXT if the two conditions below are satisfied: + * a) There are no non-portable control characters belonging to the + * "black list" (0..6, 14..25, 28..31). + * b) There is at least one printable character belonging to the + * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). + * - BINARY otherwise. + * - The following partially-portable control characters form a + * "gray list" that is ignored in this detection algorithm: + * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). + * IN assertion: the fields Freq of dyn_ltree are set. + */ + + + var detect_data_type = function detect_data_type(s) { + /* black_mask is the bit mask of black-listed bytes + * set bits 0..6, 14..25, and 28..31 + * 0xf3ffc07f = binary 11110011111111111100000001111111 + */ + var black_mask = 0xf3ffc07f; + var n; + /* Check for non-textual ("black-listed") bytes. */ + + for (n = 0; n <= 31; n++, black_mask >>>= 1) { + if (black_mask & 1 && s.dyn_ltree[n * 2] + /*.Freq*/ + !== 0) { + return Z_BINARY; + } + } + /* Check for textual ("white-listed") bytes. */ + + + if (s.dyn_ltree[9 * 2] + /*.Freq*/ + !== 0 || s.dyn_ltree[10 * 2] + /*.Freq*/ + !== 0 || s.dyn_ltree[13 * 2] + /*.Freq*/ + !== 0) { + return Z_TEXT; + } + + for (n = 32; n < LITERALS$1; n++) { + if (s.dyn_ltree[n * 2] + /*.Freq*/ + !== 0) { + return Z_TEXT; + } + } + /* There are no "black-listed" or "white-listed" bytes: + * this stream either is empty or has tolerated ("gray-listed") bytes only. + */ + + + return Z_BINARY; + }; + + var static_init_done = false; + /* =========================================================================== + * Initialize the tree data structures for a new zlib stream. + */ + + var _tr_init$1 = function _tr_init(s) { + if (!static_init_done) { + tr_static_init(); + static_init_done = true; + } + + s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc); + s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc); + s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc); + s.bi_buf = 0; + s.bi_valid = 0; + /* Initialize the first block of the first file: */ + + init_block(s); + }; + /* =========================================================================== + * Send a stored block + */ + + + var _tr_stored_block$1 = function _tr_stored_block(s, buf, stored_len, last) //DeflateState *s; + //charf *buf; /* input block */ + //ulg stored_len; /* length of input block */ + //int last; /* one if this is the last block for a file */ + { + send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3); + /* send block type */ + + copy_block(s, buf, stored_len, true); + /* with header */ + }; + /* =========================================================================== + * Send one empty static block to give enough lookahead for inflate. + * This takes 10 bits, of which 7 may remain in the bit buffer. + */ + + + var _tr_align$1 = function _tr_align(s) { + send_bits(s, STATIC_TREES << 1, 3); + send_code(s, END_BLOCK, static_ltree); + bi_flush(s); + }; + /* =========================================================================== + * Determine the best encoding for the current block: dynamic trees, static + * trees or store, and output the encoded block to the zip file. + */ + + + var _tr_flush_block$1 = function _tr_flush_block(s, buf, stored_len, last) //DeflateState *s; + //charf *buf; /* input block, or NULL if too old */ + //ulg stored_len; /* length of input block */ + //int last; /* one if this is the last block for a file */ + { + var opt_lenb, static_lenb; + /* opt_len and static_len in bytes */ + + var max_blindex = 0; + /* index of last bit length code of non zero freq */ + + /* Build the Huffman trees unless a stored block is forced */ + + if (s.level > 0) { + /* Check if the file is binary or text */ + if (s.strm.data_type === Z_UNKNOWN$1) { + s.strm.data_type = detect_data_type(s); + } + /* Construct the literal and distance trees */ + + + build_tree(s, s.l_desc); // Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, + // s->static_len)); + + build_tree(s, s.d_desc); // Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, + // s->static_len)); + + /* At this point, opt_len and static_len are the total bit lengths of + * the compressed block data, excluding the tree representations. + */ + + /* Build the bit length tree for the above two trees, and get the index + * in bl_order of the last bit length code to send. + */ + + max_blindex = build_bl_tree(s); + /* Determine the best encoding. Compute the block lengths in bytes. */ + + opt_lenb = s.opt_len + 3 + 7 >>> 3; + static_lenb = s.static_len + 3 + 7 >>> 3; // Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", + // opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, + // s->last_lit)); + + if (static_lenb <= opt_lenb) { + opt_lenb = static_lenb; + } + } else { + // Assert(buf != (char*)0, "lost buf"); + opt_lenb = static_lenb = stored_len + 5; + /* force a stored block */ + } + + if (stored_len + 4 <= opt_lenb && buf !== -1) { + /* 4: two words for the lengths */ + + /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. + * Otherwise we can't have processed more than WSIZE input bytes since + * the last block flush, because compression would have been + * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to + * transform a block into a stored block. + */ + _tr_stored_block$1(s, buf, stored_len, last); + } else if (s.strategy === Z_FIXED$1 || static_lenb === opt_lenb) { + send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3); + compress_block(s, static_ltree, static_dtree); + } else { + send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3); + send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1); + compress_block(s, s.dyn_ltree, s.dyn_dtree); + } // Assert (s->compressed_len == s->bits_sent, "bad compressed size"); + + /* The above check is made mod 2^32, for files larger than 512 MB + * and uLong implemented on 32 bits. + */ + + + init_block(s); + + if (last) { + bi_windup(s); + } // Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, + // s->compressed_len-7*last)); + + }; + /* =========================================================================== + * Save the match info and tally the frequency counts. Return true if + * the current block must be flushed. + */ + + + var _tr_tally$1 = function _tr_tally(s, dist, lc) // deflate_state *s; + // unsigned dist; /* distance of matched string */ + // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ + { + //let out_length, in_length, dcode; + s.pending_buf[s.d_buf + s.last_lit * 2] = dist >>> 8 & 0xff; + s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff; + s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff; + s.last_lit++; + + if (dist === 0) { + /* lc is the unmatched char */ + s.dyn_ltree[lc * 2] /*.Freq*/++; + } else { + s.matches++; + /* Here, lc is the match length - MIN_MATCH */ + + dist--; + /* dist = match distance - 1 */ + //Assert((ush)dist < (ush)MAX_DIST(s) && + // (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && + // (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); + + s.dyn_ltree[(_length_code[lc] + LITERALS$1 + 1) * 2] /*.Freq*/++; + s.dyn_dtree[d_code(dist) * 2] /*.Freq*/++; + } // (!) This block is disabled in zlib defaults, + // don't enable it for binary compatibility + //#ifdef TRUNCATE_BLOCK + // /* Try to guess if it is profitable to stop the current block here */ + // if ((s.last_lit & 0x1fff) === 0 && s.level > 2) { + // /* Compute an upper bound for the compressed length */ + // out_length = s.last_lit*8; + // in_length = s.strstart - s.block_start; + // + // for (dcode = 0; dcode < D_CODES; dcode++) { + // out_length += s.dyn_dtree[dcode*2]/*.Freq*/ * (5 + extra_dbits[dcode]); + // } + // out_length >>>= 3; + // //Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", + // // s->last_lit, in_length, out_length, + // // 100L - out_length*100L/in_length)); + // if (s.matches < (s.last_lit>>1)/*int /2*/ && out_length < (in_length>>1)/*int /2*/) { + // return true; + // } + // } + //#endif + + + return s.last_lit === s.lit_bufsize - 1; + /* We avoid equality with lit_bufsize because of wraparound at 64K + * on 16 bit machines and because stored blocks are restricted to + * 64K-1 bytes. + */ + }; + + var _tr_init_1 = _tr_init$1; + var _tr_stored_block_1 = _tr_stored_block$1; + var _tr_flush_block_1 = _tr_flush_block$1; + var _tr_tally_1 = _tr_tally$1; + var _tr_align_1 = _tr_align$1; + var trees = { + _tr_init: _tr_init_1, + _tr_stored_block: _tr_stored_block_1, + _tr_flush_block: _tr_flush_block_1, + _tr_tally: _tr_tally_1, + _tr_align: _tr_align_1 + }; + + // It isn't worth it to make additional optimizations as in original. + // Small size is preferable. + // (C) 1995-2013 Jean-loup Gailly and Mark Adler + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + + var adler32 = function adler32(adler, buf, len, pos) { + var s1 = adler & 0xffff | 0, + s2 = adler >>> 16 & 0xffff | 0, + n = 0; + + while (len !== 0) { + // Set limit ~ twice less than 5552, to keep + // s2 in 31-bits, because we force signed ints. + // in other case %= will fail. + n = len > 2000 ? 2000 : len; + len -= n; + + do { + s1 = s1 + buf[pos++] | 0; + s2 = s2 + s1 | 0; + } while (--n); + + s1 %= 65521; + s2 %= 65521; + } + + return s1 | s2 << 16 | 0; + }; + + var adler32_1 = adler32; + + // So write code to minimize size - no pregenerated tables + // and array tools dependencies. + // (C) 1995-2013 Jean-loup Gailly and Mark Adler + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + // Use ordinary array, since untyped makes no boost here + + var makeTable = function makeTable() { + var c, + table = []; + + for (var n = 0; n < 256; n++) { + c = n; + + for (var k = 0; k < 8; k++) { + c = c & 1 ? 0xEDB88320 ^ c >>> 1 : c >>> 1; + } + + table[n] = c; + } + + return table; + }; // Create table on load. Just 255 signed longs. Not a problem. + + + var crcTable = new Uint32Array(makeTable()); + + var crc32 = function crc32(crc, buf, len, pos) { + var t = crcTable; + var end = pos + len; + crc ^= -1; + + for (var i = pos; i < end; i++) { + crc = crc >>> 8 ^ t[(crc ^ buf[i]) & 0xFF]; + } + + return crc ^ -1; // >>> 0; + }; + + var crc32_1 = crc32; + + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + + var messages = { + 2: 'need dictionary', + + /* Z_NEED_DICT 2 */ + 1: 'stream end', + + /* Z_STREAM_END 1 */ + 0: '', + + /* Z_OK 0 */ + '-1': 'file error', + + /* Z_ERRNO (-1) */ + '-2': 'stream error', + + /* Z_STREAM_ERROR (-2) */ + '-3': 'data error', + + /* Z_DATA_ERROR (-3) */ + '-4': 'insufficient memory', + + /* Z_MEM_ERROR (-4) */ + '-5': 'buffer error', + + /* Z_BUF_ERROR (-5) */ + '-6': 'incompatible version' + /* Z_VERSION_ERROR (-6) */ + + }; + + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + + var constants$2 = { + /* Allowed flush values; see deflate() and inflate() below for details */ + Z_NO_FLUSH: 0, + Z_PARTIAL_FLUSH: 1, + Z_SYNC_FLUSH: 2, + Z_FULL_FLUSH: 3, + Z_FINISH: 4, + Z_BLOCK: 5, + Z_TREES: 6, + + /* Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ + Z_OK: 0, + Z_STREAM_END: 1, + Z_NEED_DICT: 2, + Z_ERRNO: -1, + Z_STREAM_ERROR: -2, + Z_DATA_ERROR: -3, + Z_MEM_ERROR: -4, + Z_BUF_ERROR: -5, + //Z_VERSION_ERROR: -6, + + /* compression levels */ + Z_NO_COMPRESSION: 0, + Z_BEST_SPEED: 1, + Z_BEST_COMPRESSION: 9, + Z_DEFAULT_COMPRESSION: -1, + Z_FILTERED: 1, + Z_HUFFMAN_ONLY: 2, + Z_RLE: 3, + Z_FIXED: 4, + Z_DEFAULT_STRATEGY: 0, + + /* Possible values of the data_type field (though see inflate()) */ + Z_BINARY: 0, + Z_TEXT: 1, + //Z_ASCII: 1, // = Z_TEXT (deprecated) + Z_UNKNOWN: 2, + + /* The deflate compression method */ + Z_DEFLATED: 8 //Z_NULL: null // Use -1 or null inline, depending on var type + + }; + + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + + + var _tr_init = trees._tr_init, + _tr_stored_block = trees._tr_stored_block, + _tr_flush_block = trees._tr_flush_block, + _tr_tally = trees._tr_tally, + _tr_align = trees._tr_align; + /* Public constants ==========================================================*/ + + /* ===========================================================================*/ + + var Z_NO_FLUSH$2 = constants$2.Z_NO_FLUSH, + Z_PARTIAL_FLUSH = constants$2.Z_PARTIAL_FLUSH, + Z_FULL_FLUSH$1 = constants$2.Z_FULL_FLUSH, + Z_FINISH$3 = constants$2.Z_FINISH, + Z_BLOCK$1 = constants$2.Z_BLOCK, + Z_OK$3 = constants$2.Z_OK, + Z_STREAM_END$3 = constants$2.Z_STREAM_END, + Z_STREAM_ERROR$2 = constants$2.Z_STREAM_ERROR, + Z_DATA_ERROR$2 = constants$2.Z_DATA_ERROR, + Z_BUF_ERROR$1 = constants$2.Z_BUF_ERROR, + Z_DEFAULT_COMPRESSION$1 = constants$2.Z_DEFAULT_COMPRESSION, + Z_FILTERED = constants$2.Z_FILTERED, + Z_HUFFMAN_ONLY = constants$2.Z_HUFFMAN_ONLY, + Z_RLE = constants$2.Z_RLE, + Z_FIXED = constants$2.Z_FIXED, + Z_DEFAULT_STRATEGY$1 = constants$2.Z_DEFAULT_STRATEGY, + Z_UNKNOWN = constants$2.Z_UNKNOWN, + Z_DEFLATED$2 = constants$2.Z_DEFLATED; + /*============================================================================*/ + + var MAX_MEM_LEVEL = 9; + /* Maximum value for memLevel in deflateInit2 */ + + var MAX_WBITS$1 = 15; + /* 32K LZ77 window */ + + var DEF_MEM_LEVEL = 8; + var LENGTH_CODES = 29; + /* number of length codes, not counting the special END_BLOCK code */ + + var LITERALS = 256; + /* number of literal bytes 0..255 */ + + var L_CODES = LITERALS + 1 + LENGTH_CODES; + /* number of Literal or Length codes, including the END_BLOCK code */ + + var D_CODES = 30; + /* number of distance codes */ + + var BL_CODES = 19; + /* number of codes used to transfer the bit lengths */ + + var HEAP_SIZE = 2 * L_CODES + 1; + /* maximum heap size */ + + var MAX_BITS = 15; + /* All codes must not exceed MAX_BITS bits */ + + var MIN_MATCH = 3; + var MAX_MATCH = 258; + var MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1; + var PRESET_DICT = 0x20; + var INIT_STATE = 42; + var EXTRA_STATE = 69; + var NAME_STATE = 73; + var COMMENT_STATE = 91; + var HCRC_STATE = 103; + var BUSY_STATE = 113; + var FINISH_STATE = 666; + var BS_NEED_MORE = 1; + /* block not completed, need more input or more output */ + + var BS_BLOCK_DONE = 2; + /* block flush performed */ + + var BS_FINISH_STARTED = 3; + /* finish started, need only more output at next deflate */ + + var BS_FINISH_DONE = 4; + /* finish done, accept no more input or output */ + + var OS_CODE = 0x03; // Unix :) . Don't detect, use this default. + + var err = function err(strm, errorCode) { + strm.msg = messages[errorCode]; + return errorCode; + }; + + var rank = function rank(f) { + return (f << 1) - (f > 4 ? 9 : 0); + }; + + var zero = function zero(buf) { + var len = buf.length; + + while (--len >= 0) { + buf[len] = 0; + } + }; + /* eslint-disable new-cap */ + + + var HASH_ZLIB = function HASH_ZLIB(s, prev, data) { + return (prev << s.hash_shift ^ data) & s.hash_mask; + }; // This hash causes less collisions, https://github.com/nodeca/pako/issues/135 + // But breaks binary compatibility + //let HASH_FAST = (s, prev, data) => ((prev << 8) + (prev >> 8) + (data << 4)) & s.hash_mask; + + + var HASH = HASH_ZLIB; + /* ========================================================================= + * Flush as much pending output as possible. All deflate() output goes + * through this function so some applications may wish to modify it + * to avoid allocating a large strm->output buffer and copying into it. + * (See also read_buf()). + */ + + var flush_pending = function flush_pending(strm) { + var s = strm.state; //_tr_flush_bits(s); + + var len = s.pending; + + if (len > strm.avail_out) { + len = strm.avail_out; + } + + if (len === 0) { + return; + } + + strm.output.set(s.pending_buf.subarray(s.pending_out, s.pending_out + len), strm.next_out); + strm.next_out += len; + s.pending_out += len; + strm.total_out += len; + strm.avail_out -= len; + s.pending -= len; + + if (s.pending === 0) { + s.pending_out = 0; + } + }; + + var flush_block_only = function flush_block_only(s, last) { + _tr_flush_block(s, s.block_start >= 0 ? s.block_start : -1, s.strstart - s.block_start, last); + + s.block_start = s.strstart; + flush_pending(s.strm); + }; + + var put_byte = function put_byte(s, b) { + s.pending_buf[s.pending++] = b; + }; + /* ========================================================================= + * Put a short in the pending buffer. The 16-bit value is put in MSB order. + * IN assertion: the stream state is correct and there is enough room in + * pending_buf. + */ + + + var putShortMSB = function putShortMSB(s, b) { + // put_byte(s, (Byte)(b >> 8)); + // put_byte(s, (Byte)(b & 0xff)); + s.pending_buf[s.pending++] = b >>> 8 & 0xff; + s.pending_buf[s.pending++] = b & 0xff; + }; + /* =========================================================================== + * Read a new buffer from the current input stream, update the adler32 + * and total number of bytes read. All deflate() input goes through + * this function so some applications may wish to modify it to avoid + * allocating a large strm->input buffer and copying from it. + * (See also flush_pending()). + */ + + + var read_buf = function read_buf(strm, buf, start, size) { + var len = strm.avail_in; + + if (len > size) { + len = size; + } + + if (len === 0) { + return 0; + } + + strm.avail_in -= len; // zmemcpy(buf, strm->next_in, len); + + buf.set(strm.input.subarray(strm.next_in, strm.next_in + len), start); + + if (strm.state.wrap === 1) { + strm.adler = adler32_1(strm.adler, buf, len, start); + } else if (strm.state.wrap === 2) { + strm.adler = crc32_1(strm.adler, buf, len, start); + } + + strm.next_in += len; + strm.total_in += len; + return len; + }; + /* =========================================================================== + * Set match_start to the longest match starting at the given string and + * return its length. Matches shorter or equal to prev_length are discarded, + * in which case the result is equal to prev_length and match_start is + * garbage. + * IN assertions: cur_match is the head of the hash chain for the current + * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 + * OUT assertion: the match length is not greater than s->lookahead. + */ + + + var longest_match = function longest_match(s, cur_match) { + var chain_length = s.max_chain_length; + /* max hash chain length */ + + var scan = s.strstart; + /* current string */ + + var match; + /* matched string */ + + var len; + /* length of current match */ + + var best_len = s.prev_length; + /* best match length so far */ + + var nice_match = s.nice_match; + /* stop if match long enough */ + + var limit = s.strstart > s.w_size - MIN_LOOKAHEAD ? s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0 + /*NIL*/ + ; + var _win = s.window; // shortcut + + var wmask = s.w_mask; + var prev = s.prev; + /* Stop when cur_match becomes <= limit. To simplify the code, + * we prevent matches with the string of window index 0. + */ + + var strend = s.strstart + MAX_MATCH; + var scan_end1 = _win[scan + best_len - 1]; + var scan_end = _win[scan + best_len]; + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. + * It is easy to get rid of this optimization if necessary. + */ + // Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + + /* Do not waste too much time if we already have a good match: */ + + if (s.prev_length >= s.good_match) { + chain_length >>= 2; + } + /* Do not look for matches beyond the end of the input. This is necessary + * to make deflate deterministic. + */ + + + if (nice_match > s.lookahead) { + nice_match = s.lookahead; + } // Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + + + do { + // Assert(cur_match < s->strstart, "no future"); + match = cur_match; + /* Skip to next match if the match length cannot increase + * or if the match length is less than 2. Note that the checks below + * for insufficient lookahead only occur occasionally for performance + * reasons. Therefore uninitialized memory will be accessed, and + * conditional jumps will be made that depend on those values. + * However the length of the match is limited to the lookahead, so + * the output of deflate is not affected by the uninitialized values. + */ + + if (_win[match + best_len] !== scan_end || _win[match + best_len - 1] !== scan_end1 || _win[match] !== _win[scan] || _win[++match] !== _win[scan + 1]) { + continue; + } + /* The check at best_len-1 can be removed because it will be made + * again later. (This heuristic is not always a win.) + * It is not necessary to compare scan[2] and match[2] since they + * are always equal when the other bytes match, given that + * the hash keys are equal and that HASH_BITS >= 8. + */ + + + scan += 2; + match++; // Assert(*scan == *match, "match[2]?"); + + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart+258. + */ + + do { + /*jshint noempty:false*/ + } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && _win[++scan] === _win[++match] && scan < strend); // Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + + + len = MAX_MATCH - (strend - scan); + scan = strend - MAX_MATCH; + + if (len > best_len) { + s.match_start = cur_match; + best_len = len; + + if (len >= nice_match) { + break; + } + + scan_end1 = _win[scan + best_len - 1]; + scan_end = _win[scan + best_len]; + } + } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0); + + if (best_len <= s.lookahead) { + return best_len; + } + + return s.lookahead; + }; + /* =========================================================================== + * Fill the window when the lookahead becomes insufficient. + * Updates strstart and lookahead. + * + * IN assertion: lookahead < MIN_LOOKAHEAD + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD + * At least one byte has been read, or avail_in == 0; reads are + * performed for at least two bytes (required for the zip translate_eol + * option -- not supported here). + */ + + + var fill_window = function fill_window(s) { + var _w_size = s.w_size; + var p, n, m, more, str; //Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); + + do { + more = s.window_size - s.lookahead - s.strstart; // JS ints have 32 bit, block below not needed + + /* Deal with !@#$% 64K limit: */ + //if (sizeof(int) <= 2) { + // if (more == 0 && s->strstart == 0 && s->lookahead == 0) { + // more = wsize; + // + // } else if (more == (unsigned)(-1)) { + // /* Very unlikely, but possible on 16 bit machine if + // * strstart == 0 && lookahead == 1 (input done a byte at time) + // */ + // more--; + // } + //} + + /* If the window is almost full and there is insufficient lookahead, + * move the upper half to the lower one to make room in the upper half. + */ + + if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) { + s.window.set(s.window.subarray(_w_size, _w_size + _w_size), 0); + s.match_start -= _w_size; + s.strstart -= _w_size; + /* we now have strstart >= MAX_DIST */ + + s.block_start -= _w_size; + /* Slide the hash table (could be avoided with 32 bit values + at the expense of memory usage). We slide even when level == 0 + to keep the hash table consistent if we switch back to level > 0 + later. (Using level 0 permanently is not an optimal usage of + zlib, so we don't care about this pathological case.) + */ + + n = s.hash_size; + p = n; + + do { + m = s.head[--p]; + s.head[p] = m >= _w_size ? m - _w_size : 0; + } while (--n); + + n = _w_size; + p = n; + + do { + m = s.prev[--p]; + s.prev[p] = m >= _w_size ? m - _w_size : 0; + /* If n is not on any hash chain, prev[n] is garbage but + * its value will never be used. + */ + } while (--n); + + more += _w_size; + } + + if (s.strm.avail_in === 0) { + break; + } + /* If there was no sliding: + * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && + * more == window_size - lookahead - strstart + * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) + * => more >= window_size - 2*WSIZE + 2 + * In the BIG_MEM or MMAP case (not yet supported), + * window_size == input_size + MIN_LOOKAHEAD && + * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. + * Otherwise, window_size == 2*WSIZE so more >= 2. + * If there was sliding, more >= WSIZE. So in all cases, more >= 2. + */ + //Assert(more >= 2, "more < 2"); + + + n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more); + s.lookahead += n; + /* Initialize the hash value now that we have some input: */ + + if (s.lookahead + s.insert >= MIN_MATCH) { + str = s.strstart - s.insert; + s.ins_h = s.window[str]; + /* UPDATE_HASH(s, s->ins_h, s->window[str + 1]); */ + + s.ins_h = HASH(s, s.ins_h, s.window[str + 1]); //#if MIN_MATCH != 3 + // Call update_hash() MIN_MATCH-3 more times + //#endif + + while (s.insert) { + /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */ + s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]); + s.prev[str & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = str; + str++; + s.insert--; + + if (s.lookahead + s.insert < MIN_MATCH) { + break; + } + } + } + /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, + * but this is not important since only literal bytes will be emitted. + */ + + } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0); + /* If the WIN_INIT bytes after the end of the current data have never been + * written, then zero those bytes in order to avoid memory check reports of + * the use of uninitialized (or uninitialised as Julian writes) bytes by + * the longest match routines. Update the high water mark for the next + * time through here. WIN_INIT is set to MAX_MATCH since the longest match + * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. + */ + // if (s.high_water < s.window_size) { + // const curr = s.strstart + s.lookahead; + // let init = 0; + // + // if (s.high_water < curr) { + // /* Previous high water mark below current data -- zero WIN_INIT + // * bytes or up to end of window, whichever is less. + // */ + // init = s.window_size - curr; + // if (init > WIN_INIT) + // init = WIN_INIT; + // zmemzero(s->window + curr, (unsigned)init); + // s->high_water = curr + init; + // } + // else if (s->high_water < (ulg)curr + WIN_INIT) { + // /* High water mark at or above current data, but below current data + // * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up + // * to end of window, whichever is less. + // */ + // init = (ulg)curr + WIN_INIT - s->high_water; + // if (init > s->window_size - s->high_water) + // init = s->window_size - s->high_water; + // zmemzero(s->window + s->high_water, (unsigned)init); + // s->high_water += init; + // } + // } + // + // Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, + // "not enough room for search"); + + }; + /* =========================================================================== + * Copy without compression as much as possible from the input stream, return + * the current block state. + * This function does not insert new strings in the dictionary since + * uncompressible data is probably not useful. This function is used + * only for the level=0 compression option. + * NOTE: this function should be optimized to avoid extra copying from + * window to pending_buf. + */ + + + var deflate_stored = function deflate_stored(s, flush) { + /* Stored blocks are limited to 0xffff bytes, pending_buf is limited + * to pending_buf_size, and each stored block has a 5 byte header: + */ + var max_block_size = 0xffff; + + if (max_block_size > s.pending_buf_size - 5) { + max_block_size = s.pending_buf_size - 5; + } + /* Copy as much as possible from input to output: */ + + + for (;;) { + /* Fill the window as much as possible: */ + if (s.lookahead <= 1) { + //Assert(s->strstart < s->w_size+MAX_DIST(s) || + // s->block_start >= (long)s->w_size, "slide too late"); + // if (!(s.strstart < s.w_size + (s.w_size - MIN_LOOKAHEAD) || + // s.block_start >= s.w_size)) { + // throw new Error("slide too late"); + // } + fill_window(s); + + if (s.lookahead === 0 && flush === Z_NO_FLUSH$2) { + return BS_NEED_MORE; + } + + if (s.lookahead === 0) { + break; + } + /* flush the current block */ + + } //Assert(s->block_start >= 0L, "block gone"); + // if (s.block_start < 0) throw new Error("block gone"); + + + s.strstart += s.lookahead; + s.lookahead = 0; + /* Emit a stored block if pending_buf will be full: */ + + var max_start = s.block_start + max_block_size; + + if (s.strstart === 0 || s.strstart >= max_start) { + /* strstart == 0 is possible when wraparound on 16-bit machine */ + s.lookahead = s.strstart - max_start; + s.strstart = max_start; + /*** FLUSH_BLOCK(s, 0); ***/ + + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + /* Flush if we may have to slide, otherwise block_start may become + * negative and the data will be gone: + */ + + + if (s.strstart - s.block_start >= s.w_size - MIN_LOOKAHEAD) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + } + + s.insert = 0; + + if (flush === Z_FINISH$3) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + + + return BS_FINISH_DONE; + } + + if (s.strstart > s.block_start) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + + return BS_NEED_MORE; + }; + /* =========================================================================== + * Compress as much as possible from the input stream, return the current + * block state. + * This function does not perform lazy evaluation of matches and inserts + * new strings in the dictionary only for unmatched strings or for short + * matches. It is used only for the fast compression options. + */ + + + var deflate_fast = function deflate_fast(s, flush) { + var hash_head; + /* head of the hash chain */ + + var bflush; + /* set if current block must be flushed */ + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s.lookahead < MIN_LOOKAHEAD) { + fill_window(s); + + if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$2) { + return BS_NEED_MORE; + } + + if (s.lookahead === 0) { + break; + /* flush the current block */ + } + } + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + + + hash_head = 0 + /*NIL*/ + ; + + if (s.lookahead >= MIN_MATCH) { + /*** INSERT_STRING(s, s.strstart, hash_head); ***/ + s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]); + hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = s.strstart; + /***/ + } + /* Find the longest match, discarding those <= prev_length. + * At this point we have always match_length < MIN_MATCH + */ + + + if (hash_head !== 0 + /*NIL*/ + && s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ + s.match_length = longest_match(s, hash_head); + /* longest_match() sets match_start */ + } + + if (s.match_length >= MIN_MATCH) { + // check_match(s, s.strstart, s.match_start, s.match_length); // for debug only + + /*** _tr_tally_dist(s, s.strstart - s.match_start, + s.match_length - MIN_MATCH, bflush); ***/ + bflush = _tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH); + s.lookahead -= s.match_length; + /* Insert new strings in the hash table only if the match length + * is not too large. This saves time but degrades compression. + */ + + if (s.match_length <= s.max_lazy_match + /*max_insert_length*/ + && s.lookahead >= MIN_MATCH) { + s.match_length--; + /* string at strstart already in table */ + + do { + s.strstart++; + /*** INSERT_STRING(s, s.strstart, hash_head); ***/ + + s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]); + hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = s.strstart; + /***/ + + /* strstart never exceeds WSIZE-MAX_MATCH, so there are + * always MIN_MATCH bytes ahead. + */ + } while (--s.match_length !== 0); + + s.strstart++; + } else { + s.strstart += s.match_length; + s.match_length = 0; + s.ins_h = s.window[s.strstart]; + /* UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]); */ + + s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + 1]); //#if MIN_MATCH != 3 + // Call UPDATE_HASH() MIN_MATCH-3 more times + //#endif + + /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not + * matter since it will be recomputed at next deflate call. + */ + } + } else { + /* No match, output a literal byte */ + //Tracevv((stderr,"%c", s.window[s.strstart])); + + /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ + bflush = _tr_tally(s, 0, s.window[s.strstart]); + s.lookahead--; + s.strstart++; + } + + if (bflush) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + } + + s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1; + + if (flush === Z_FINISH$3) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + + + return BS_FINISH_DONE; + } + + if (s.last_lit) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + + return BS_BLOCK_DONE; + }; + /* =========================================================================== + * Same as above, but achieves better compression. We use a lazy + * evaluation for matches: a match is finally adopted only if there is + * no better match at the next window position. + */ + + + var deflate_slow = function deflate_slow(s, flush) { + var hash_head; + /* head of hash chain */ + + var bflush; + /* set if current block must be flushed */ + + var max_insert; + /* Process the input block. */ + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s.lookahead < MIN_LOOKAHEAD) { + fill_window(s); + + if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH$2) { + return BS_NEED_MORE; + } + + if (s.lookahead === 0) { + break; + } + /* flush the current block */ + + } + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + + + hash_head = 0 + /*NIL*/ + ; + + if (s.lookahead >= MIN_MATCH) { + /*** INSERT_STRING(s, s.strstart, hash_head); ***/ + s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]); + hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = s.strstart; + /***/ + } + /* Find the longest match, discarding those <= prev_length. + */ + + + s.prev_length = s.match_length; + s.prev_match = s.match_start; + s.match_length = MIN_MATCH - 1; + + if (hash_head !== 0 + /*NIL*/ + && s.prev_length < s.max_lazy_match && s.strstart - hash_head <= s.w_size - MIN_LOOKAHEAD + /*MAX_DIST(s)*/ + ) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ + s.match_length = longest_match(s, hash_head); + /* longest_match() sets match_start */ + + if (s.match_length <= 5 && (s.strategy === Z_FILTERED || s.match_length === MIN_MATCH && s.strstart - s.match_start > 4096 + /*TOO_FAR*/ + )) { + /* If prev_match is also MIN_MATCH, match_start is garbage + * but we will ignore the current match anyway. + */ + s.match_length = MIN_MATCH - 1; + } + } + /* If there was a match at the previous step and the current + * match is not better, output the previous match: + */ + + + if (s.prev_length >= MIN_MATCH && s.match_length <= s.prev_length) { + max_insert = s.strstart + s.lookahead - MIN_MATCH; + /* Do not insert strings in hash table beyond this. */ + //check_match(s, s.strstart-1, s.prev_match, s.prev_length); + + /***_tr_tally_dist(s, s.strstart - 1 - s.prev_match, + s.prev_length - MIN_MATCH, bflush);***/ + + bflush = _tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH); + /* Insert in hash table all strings up to the end of the match. + * strstart-1 and strstart are already inserted. If there is not + * enough lookahead, the last two strings are not inserted in + * the hash table. + */ + + s.lookahead -= s.prev_length - 1; + s.prev_length -= 2; + + do { + if (++s.strstart <= max_insert) { + /*** INSERT_STRING(s, s.strstart, hash_head); ***/ + s.ins_h = HASH(s, s.ins_h, s.window[s.strstart + MIN_MATCH - 1]); + hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = s.strstart; + /***/ + } + } while (--s.prev_length !== 0); + + s.match_available = 0; + s.match_length = MIN_MATCH - 1; + s.strstart++; + + if (bflush) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + } else if (s.match_available) { + /* If there was no match at the previous position, output a + * single literal. If there was a match but the current match + * is longer, truncate the previous match to a single literal. + */ + //Tracevv((stderr,"%c", s->window[s->strstart-1])); + + /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/ + bflush = _tr_tally(s, 0, s.window[s.strstart - 1]); + + if (bflush) { + /*** FLUSH_BLOCK_ONLY(s, 0) ***/ + flush_block_only(s, false); + /***/ + } + + s.strstart++; + s.lookahead--; + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + } else { + /* There is no previous match to compare with, wait for + * the next step to decide. + */ + s.match_available = 1; + s.strstart++; + s.lookahead--; + } + } //Assert (flush != Z_NO_FLUSH, "no flush?"); + + + if (s.match_available) { + //Tracevv((stderr,"%c", s->window[s->strstart-1])); + + /*** _tr_tally_lit(s, s.window[s.strstart-1], bflush); ***/ + bflush = _tr_tally(s, 0, s.window[s.strstart - 1]); + s.match_available = 0; + } + + s.insert = s.strstart < MIN_MATCH - 1 ? s.strstart : MIN_MATCH - 1; + + if (flush === Z_FINISH$3) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + + + return BS_FINISH_DONE; + } + + if (s.last_lit) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + + return BS_BLOCK_DONE; + }; + /* =========================================================================== + * For Z_RLE, simply look for runs of bytes, generate matches only of distance + * one. Do not maintain a hash table. (It will be regenerated if this run of + * deflate switches away from Z_RLE.) + */ + + + var deflate_rle = function deflate_rle(s, flush) { + var bflush; + /* set if current block must be flushed */ + + var prev; + /* byte at distance one to match */ + + var scan, strend; + /* scan goes up to strend for length of run */ + + var _win = s.window; + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the longest run, plus one for the unrolled loop. + */ + if (s.lookahead <= MAX_MATCH) { + fill_window(s); + + if (s.lookahead <= MAX_MATCH && flush === Z_NO_FLUSH$2) { + return BS_NEED_MORE; + } + + if (s.lookahead === 0) { + break; + } + /* flush the current block */ + + } + /* See how many times the previous byte repeats */ + + + s.match_length = 0; + + if (s.lookahead >= MIN_MATCH && s.strstart > 0) { + scan = s.strstart - 1; + prev = _win[scan]; + + if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) { + strend = s.strstart + MAX_MATCH; + + do { + /*jshint noempty:false*/ + } while (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan] && scan < strend); + + s.match_length = MAX_MATCH - (strend - scan); + + if (s.match_length > s.lookahead) { + s.match_length = s.lookahead; + } + } //Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); + + } + /* Emit match if have run of MIN_MATCH or longer, else emit literal */ + + + if (s.match_length >= MIN_MATCH) { + //check_match(s, s.strstart, s.strstart - 1, s.match_length); + + /*** _tr_tally_dist(s, 1, s.match_length - MIN_MATCH, bflush); ***/ + bflush = _tr_tally(s, 1, s.match_length - MIN_MATCH); + s.lookahead -= s.match_length; + s.strstart += s.match_length; + s.match_length = 0; + } else { + /* No match, output a literal byte */ + //Tracevv((stderr,"%c", s->window[s->strstart])); + + /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ + bflush = _tr_tally(s, 0, s.window[s.strstart]); + s.lookahead--; + s.strstart++; + } + + if (bflush) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + } + + s.insert = 0; + + if (flush === Z_FINISH$3) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + + + return BS_FINISH_DONE; + } + + if (s.last_lit) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + + return BS_BLOCK_DONE; + }; + /* =========================================================================== + * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. + * (It will be regenerated if this run of deflate switches away from Huffman.) + */ + + + var deflate_huff = function deflate_huff(s, flush) { + var bflush; + /* set if current block must be flushed */ + + for (;;) { + /* Make sure that we have a literal to write. */ + if (s.lookahead === 0) { + fill_window(s); + + if (s.lookahead === 0) { + if (flush === Z_NO_FLUSH$2) { + return BS_NEED_MORE; + } + + break; + /* flush the current block */ + } + } + /* Output a literal byte */ + + + s.match_length = 0; //Tracevv((stderr,"%c", s->window[s->strstart])); + + /*** _tr_tally_lit(s, s.window[s.strstart], bflush); ***/ + + bflush = _tr_tally(s, 0, s.window[s.strstart]); + s.lookahead--; + s.strstart++; + + if (bflush) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + } + + s.insert = 0; + + if (flush === Z_FINISH$3) { + /*** FLUSH_BLOCK(s, 1); ***/ + flush_block_only(s, true); + + if (s.strm.avail_out === 0) { + return BS_FINISH_STARTED; + } + /***/ + + + return BS_FINISH_DONE; + } + + if (s.last_lit) { + /*** FLUSH_BLOCK(s, 0); ***/ + flush_block_only(s, false); + + if (s.strm.avail_out === 0) { + return BS_NEED_MORE; + } + /***/ + + } + + return BS_BLOCK_DONE; + }; + /* Values for max_lazy_match, good_match and max_chain_length, depending on + * the desired pack level (0..9). The values given below have been tuned to + * exclude worst case performance for pathological files. Better values may be + * found for specific files. + */ + + + function Config(good_length, max_lazy, nice_length, max_chain, func) { + this.good_length = good_length; + this.max_lazy = max_lazy; + this.nice_length = nice_length; + this.max_chain = max_chain; + this.func = func; + } + + var configuration_table = [ + /* good lazy nice chain */ + new Config(0, 0, 0, 0, deflate_stored), + /* 0 store only */ + new Config(4, 4, 8, 4, deflate_fast), + /* 1 max speed, no lazy matches */ + new Config(4, 5, 16, 8, deflate_fast), + /* 2 */ + new Config(4, 6, 32, 32, deflate_fast), + /* 3 */ + new Config(4, 4, 16, 16, deflate_slow), + /* 4 lazy matches */ + new Config(8, 16, 32, 32, deflate_slow), + /* 5 */ + new Config(8, 16, 128, 128, deflate_slow), + /* 6 */ + new Config(8, 32, 128, 256, deflate_slow), + /* 7 */ + new Config(32, 128, 258, 1024, deflate_slow), + /* 8 */ + new Config(32, 258, 258, 4096, deflate_slow) + /* 9 max compression */ + ]; + /* =========================================================================== + * Initialize the "longest match" routines for a new zlib stream + */ + + var lm_init = function lm_init(s) { + s.window_size = 2 * s.w_size; + /*** CLEAR_HASH(s); ***/ + + zero(s.head); // Fill with NIL (= 0); + + /* Set the default configuration parameters: + */ + + s.max_lazy_match = configuration_table[s.level].max_lazy; + s.good_match = configuration_table[s.level].good_length; + s.nice_match = configuration_table[s.level].nice_length; + s.max_chain_length = configuration_table[s.level].max_chain; + s.strstart = 0; + s.block_start = 0; + s.lookahead = 0; + s.insert = 0; + s.match_length = s.prev_length = MIN_MATCH - 1; + s.match_available = 0; + s.ins_h = 0; + }; + + function DeflateState() { + this.strm = null; + /* pointer back to this zlib stream */ + + this.status = 0; + /* as the name implies */ + + this.pending_buf = null; + /* output still pending */ + + this.pending_buf_size = 0; + /* size of pending_buf */ + + this.pending_out = 0; + /* next pending byte to output to the stream */ + + this.pending = 0; + /* nb of bytes in the pending buffer */ + + this.wrap = 0; + /* bit 0 true for zlib, bit 1 true for gzip */ + + this.gzhead = null; + /* gzip header information to write */ + + this.gzindex = 0; + /* where in extra, name, or comment */ + + this.method = Z_DEFLATED$2; + /* can only be DEFLATED */ + + this.last_flush = -1; + /* value of flush param for previous deflate call */ + + this.w_size = 0; + /* LZ77 window size (32K by default) */ + + this.w_bits = 0; + /* log2(w_size) (8..16) */ + + this.w_mask = 0; + /* w_size - 1 */ + + this.window = null; + /* Sliding window. Input bytes are read into the second half of the window, + * and move to the first half later to keep a dictionary of at least wSize + * bytes. With this organization, matches are limited to a distance of + * wSize-MAX_MATCH bytes, but this ensures that IO is always + * performed with a length multiple of the block size. + */ + + this.window_size = 0; + /* Actual size of window: 2*wSize, except when the user input buffer + * is directly used as sliding window. + */ + + this.prev = null; + /* Link to older string with same hash index. To limit the size of this + * array to 64K, this link is maintained only for the last 32K strings. + * An index in this array is thus a window index modulo 32K. + */ + + this.head = null; + /* Heads of the hash chains or NIL. */ + + this.ins_h = 0; + /* hash index of string to be inserted */ + + this.hash_size = 0; + /* number of elements in hash table */ + + this.hash_bits = 0; + /* log2(hash_size) */ + + this.hash_mask = 0; + /* hash_size-1 */ + + this.hash_shift = 0; + /* Number of bits by which ins_h must be shifted at each input + * step. It must be such that after MIN_MATCH steps, the oldest + * byte no longer takes part in the hash key, that is: + * hash_shift * MIN_MATCH >= hash_bits + */ + + this.block_start = 0; + /* Window position at the beginning of the current output block. Gets + * negative when the window is moved backwards. + */ + + this.match_length = 0; + /* length of best match */ + + this.prev_match = 0; + /* previous match */ + + this.match_available = 0; + /* set if previous match exists */ + + this.strstart = 0; + /* start of string to insert */ + + this.match_start = 0; + /* start of matching string */ + + this.lookahead = 0; + /* number of valid bytes ahead in window */ + + this.prev_length = 0; + /* Length of the best match at previous step. Matches not greater than this + * are discarded. This is used in the lazy match evaluation. + */ + + this.max_chain_length = 0; + /* To speed up deflation, hash chains are never searched beyond this + * length. A higher limit improves compression ratio but degrades the + * speed. + */ + + this.max_lazy_match = 0; + /* Attempt to find a better match only when the current match is strictly + * smaller than this value. This mechanism is used only for compression + * levels >= 4. + */ + // That's alias to max_lazy_match, don't use directly + //this.max_insert_length = 0; + + /* Insert new strings in the hash table only if the match length is not + * greater than this length. This saves time but degrades compression. + * max_insert_length is used only for compression levels <= 3. + */ + + this.level = 0; + /* compression level (1..9) */ + + this.strategy = 0; + /* favor or force Huffman coding*/ + + this.good_match = 0; + /* Use a faster search when the previous match is longer than this */ + + this.nice_match = 0; + /* Stop searching when current match exceeds this */ + + /* used by trees.c: */ + + /* Didn't use ct_data typedef below to suppress compiler warning */ + // struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ + // struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ + // struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ + // Use flat array of DOUBLE size, with interleaved fata, + // because JS does not support effective + + this.dyn_ltree = new Uint16Array(HEAP_SIZE * 2); + this.dyn_dtree = new Uint16Array((2 * D_CODES + 1) * 2); + this.bl_tree = new Uint16Array((2 * BL_CODES + 1) * 2); + zero(this.dyn_ltree); + zero(this.dyn_dtree); + zero(this.bl_tree); + this.l_desc = null; + /* desc. for literal tree */ + + this.d_desc = null; + /* desc. for distance tree */ + + this.bl_desc = null; + /* desc. for bit length tree */ + //ush bl_count[MAX_BITS+1]; + + this.bl_count = new Uint16Array(MAX_BITS + 1); + /* number of codes at each bit length for an optimal tree */ + //int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ + + this.heap = new Uint16Array(2 * L_CODES + 1); + /* heap used to build the Huffman trees */ + + zero(this.heap); + this.heap_len = 0; + /* number of elements in the heap */ + + this.heap_max = 0; + /* element of largest frequency */ + + /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. + * The same heap array is used to build all trees. + */ + + this.depth = new Uint16Array(2 * L_CODES + 1); //uch depth[2*L_CODES+1]; + + zero(this.depth); + /* Depth of each subtree used as tie breaker for trees of equal frequency + */ + + this.l_buf = 0; + /* buffer index for literals or lengths */ + + this.lit_bufsize = 0; + /* Size of match buffer for literals/lengths. There are 4 reasons for + * limiting lit_bufsize to 64K: + * - frequencies can be kept in 16 bit counters + * - if compression is not successful for the first block, all input + * data is still in the window so we can still emit a stored block even + * when input comes from standard input. (This can also be done for + * all blocks if lit_bufsize is not greater than 32K.) + * - if compression is not successful for a file smaller than 64K, we can + * even emit a stored file instead of a stored block (saving 5 bytes). + * This is applicable only for zip (not gzip or zlib). + * - creating new Huffman trees less frequently may not provide fast + * adaptation to changes in the input data statistics. (Take for + * example a binary file with poorly compressible code followed by + * a highly compressible string table.) Smaller buffer sizes give + * fast adaptation but have of course the overhead of transmitting + * trees more frequently. + * - I can't count above 4 + */ + + this.last_lit = 0; + /* running index in l_buf */ + + this.d_buf = 0; + /* Buffer index for distances. To simplify the code, d_buf and l_buf have + * the same number of elements. To use different lengths, an extra flag + * array would be necessary. + */ + + this.opt_len = 0; + /* bit length of current block with optimal trees */ + + this.static_len = 0; + /* bit length of current block with static trees */ + + this.matches = 0; + /* number of string matches in current block */ + + this.insert = 0; + /* bytes at end of window left to insert */ + + this.bi_buf = 0; + /* Output buffer. bits are inserted starting at the bottom (least + * significant bits). + */ + + this.bi_valid = 0; + /* Number of valid bits in bi_buf. All bits above the last valid bit + * are always zero. + */ + // Used for window memory init. We safely ignore it for JS. That makes + // sense only for pointers and memory check tools. + //this.high_water = 0; + + /* High water mark offset in window for initialized bytes -- bytes above + * this are set to zero in order to avoid memory check warnings when + * longest match routines access bytes past the input. This is then + * updated to the new high water mark. + */ + } + + var deflateResetKeep = function deflateResetKeep(strm) { + if (!strm || !strm.state) { + return err(strm, Z_STREAM_ERROR$2); + } + + strm.total_in = strm.total_out = 0; + strm.data_type = Z_UNKNOWN; + var s = strm.state; + s.pending = 0; + s.pending_out = 0; + + if (s.wrap < 0) { + s.wrap = -s.wrap; + /* was made negative by deflate(..., Z_FINISH); */ + } + + s.status = s.wrap ? INIT_STATE : BUSY_STATE; + strm.adler = s.wrap === 2 ? 0 // crc32(0, Z_NULL, 0) + : 1; // adler32(0, Z_NULL, 0) + + s.last_flush = Z_NO_FLUSH$2; + + _tr_init(s); + + return Z_OK$3; + }; + + var deflateReset = function deflateReset(strm) { + var ret = deflateResetKeep(strm); + + if (ret === Z_OK$3) { + lm_init(strm.state); + } + + return ret; + }; + + var deflateSetHeader = function deflateSetHeader(strm, head) { + if (!strm || !strm.state) { + return Z_STREAM_ERROR$2; + } + + if (strm.state.wrap !== 2) { + return Z_STREAM_ERROR$2; + } + + strm.state.gzhead = head; + return Z_OK$3; + }; + + var deflateInit2 = function deflateInit2(strm, level, method, windowBits, memLevel, strategy) { + if (!strm) { + // === Z_NULL + return Z_STREAM_ERROR$2; + } + + var wrap = 1; + + if (level === Z_DEFAULT_COMPRESSION$1) { + level = 6; + } + + if (windowBits < 0) { + /* suppress zlib wrapper */ + wrap = 0; + windowBits = -windowBits; + } else if (windowBits > 15) { + wrap = 2; + /* write gzip wrapper instead */ + + windowBits -= 16; + } + + if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED$2 || windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { + return err(strm, Z_STREAM_ERROR$2); + } + + if (windowBits === 8) { + windowBits = 9; + } + /* until 256-byte window bug fixed */ + + + var s = new DeflateState(); + strm.state = s; + s.strm = strm; + s.wrap = wrap; + s.gzhead = null; + s.w_bits = windowBits; + s.w_size = 1 << s.w_bits; + s.w_mask = s.w_size - 1; + s.hash_bits = memLevel + 7; + s.hash_size = 1 << s.hash_bits; + s.hash_mask = s.hash_size - 1; + s.hash_shift = ~~((s.hash_bits + MIN_MATCH - 1) / MIN_MATCH); + s.window = new Uint8Array(s.w_size * 2); + s.head = new Uint16Array(s.hash_size); + s.prev = new Uint16Array(s.w_size); // Don't need mem init magic for JS. + //s.high_water = 0; /* nothing written to s->window yet */ + + s.lit_bufsize = 1 << memLevel + 6; + /* 16K elements by default */ + + s.pending_buf_size = s.lit_bufsize * 4; //overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); + //s->pending_buf = (uchf *) overlay; + + s.pending_buf = new Uint8Array(s.pending_buf_size); // It is offset from `s.pending_buf` (size is `s.lit_bufsize * 2`) + //s->d_buf = overlay + s->lit_bufsize/sizeof(ush); + + s.d_buf = 1 * s.lit_bufsize; //s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; + + s.l_buf = (1 + 2) * s.lit_bufsize; + s.level = level; + s.strategy = strategy; + s.method = method; + return deflateReset(strm); + }; + + var deflateInit = function deflateInit(strm, level) { + return deflateInit2(strm, level, Z_DEFLATED$2, MAX_WBITS$1, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY$1); + }; + + var deflate$2 = function deflate(strm, flush) { + var beg, val; // for gzip header write only + + if (!strm || !strm.state || flush > Z_BLOCK$1 || flush < 0) { + return strm ? err(strm, Z_STREAM_ERROR$2) : Z_STREAM_ERROR$2; + } + + var s = strm.state; + + if (!strm.output || !strm.input && strm.avail_in !== 0 || s.status === FINISH_STATE && flush !== Z_FINISH$3) { + return err(strm, strm.avail_out === 0 ? Z_BUF_ERROR$1 : Z_STREAM_ERROR$2); + } + + s.strm = strm; + /* just in case */ + + var old_flush = s.last_flush; + s.last_flush = flush; + /* Write the header */ + + if (s.status === INIT_STATE) { + if (s.wrap === 2) { + // GZIP header + strm.adler = 0; //crc32(0L, Z_NULL, 0); + + put_byte(s, 31); + put_byte(s, 139); + put_byte(s, 8); + + if (!s.gzhead) { + // s->gzhead == Z_NULL + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, s.level === 9 ? 2 : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0); + put_byte(s, OS_CODE); + s.status = BUSY_STATE; + } else { + put_byte(s, (s.gzhead.text ? 1 : 0) + (s.gzhead.hcrc ? 2 : 0) + (!s.gzhead.extra ? 0 : 4) + (!s.gzhead.name ? 0 : 8) + (!s.gzhead.comment ? 0 : 16)); + put_byte(s, s.gzhead.time & 0xff); + put_byte(s, s.gzhead.time >> 8 & 0xff); + put_byte(s, s.gzhead.time >> 16 & 0xff); + put_byte(s, s.gzhead.time >> 24 & 0xff); + put_byte(s, s.level === 9 ? 2 : s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ? 4 : 0); + put_byte(s, s.gzhead.os & 0xff); + + if (s.gzhead.extra && s.gzhead.extra.length) { + put_byte(s, s.gzhead.extra.length & 0xff); + put_byte(s, s.gzhead.extra.length >> 8 & 0xff); + } + + if (s.gzhead.hcrc) { + strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending, 0); + } + + s.gzindex = 0; + s.status = EXTRA_STATE; + } + } else // DEFLATE header + { + var header = Z_DEFLATED$2 + (s.w_bits - 8 << 4) << 8; + var level_flags = -1; + + if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) { + level_flags = 0; + } else if (s.level < 6) { + level_flags = 1; + } else if (s.level === 6) { + level_flags = 2; + } else { + level_flags = 3; + } + + header |= level_flags << 6; + + if (s.strstart !== 0) { + header |= PRESET_DICT; + } + + header += 31 - header % 31; + s.status = BUSY_STATE; + putShortMSB(s, header); + /* Save the adler32 of the preset dictionary: */ + + if (s.strstart !== 0) { + putShortMSB(s, strm.adler >>> 16); + putShortMSB(s, strm.adler & 0xffff); + } + + strm.adler = 1; // adler32(0L, Z_NULL, 0); + } + } //#ifdef GZIP + + + if (s.status === EXTRA_STATE) { + if (s.gzhead.extra + /* != Z_NULL*/ + ) { + beg = s.pending; + /* start of bytes to update crc */ + + while (s.gzindex < (s.gzhead.extra.length & 0xffff)) { + if (s.pending === s.pending_buf_size) { + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg); + } + + flush_pending(strm); + beg = s.pending; + + if (s.pending === s.pending_buf_size) { + break; + } + } + + put_byte(s, s.gzhead.extra[s.gzindex] & 0xff); + s.gzindex++; + } + + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg); + } + + if (s.gzindex === s.gzhead.extra.length) { + s.gzindex = 0; + s.status = NAME_STATE; + } + } else { + s.status = NAME_STATE; + } + } + + if (s.status === NAME_STATE) { + if (s.gzhead.name + /* != Z_NULL*/ + ) { + beg = s.pending; + /* start of bytes to update crc */ + //int val; + + do { + if (s.pending === s.pending_buf_size) { + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg); + } + + flush_pending(strm); + beg = s.pending; + + if (s.pending === s.pending_buf_size) { + val = 1; + break; + } + } // JS specific: little magic to add zero terminator to end of string + + + if (s.gzindex < s.gzhead.name.length) { + val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff; + } else { + val = 0; + } + + put_byte(s, val); + } while (val !== 0); + + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg); + } + + if (val === 0) { + s.gzindex = 0; + s.status = COMMENT_STATE; + } + } else { + s.status = COMMENT_STATE; + } + } + + if (s.status === COMMENT_STATE) { + if (s.gzhead.comment + /* != Z_NULL*/ + ) { + beg = s.pending; + /* start of bytes to update crc */ + //int val; + + do { + if (s.pending === s.pending_buf_size) { + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg); + } + + flush_pending(strm); + beg = s.pending; + + if (s.pending === s.pending_buf_size) { + val = 1; + break; + } + } // JS specific: little magic to add zero terminator to end of string + + + if (s.gzindex < s.gzhead.comment.length) { + val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff; + } else { + val = 0; + } + + put_byte(s, val); + } while (val !== 0); + + if (s.gzhead.hcrc && s.pending > beg) { + strm.adler = crc32_1(strm.adler, s.pending_buf, s.pending - beg, beg); + } + + if (val === 0) { + s.status = HCRC_STATE; + } + } else { + s.status = HCRC_STATE; + } + } + + if (s.status === HCRC_STATE) { + if (s.gzhead.hcrc) { + if (s.pending + 2 > s.pending_buf_size) { + flush_pending(strm); + } + + if (s.pending + 2 <= s.pending_buf_size) { + put_byte(s, strm.adler & 0xff); + put_byte(s, strm.adler >> 8 & 0xff); + strm.adler = 0; //crc32(0L, Z_NULL, 0); + + s.status = BUSY_STATE; + } + } else { + s.status = BUSY_STATE; + } + } //#endif + + /* Flush as much pending output as possible */ + + + if (s.pending !== 0) { + flush_pending(strm); + + if (strm.avail_out === 0) { + /* Since avail_out is 0, deflate will be called again with + * more output space, but possibly with both pending and + * avail_in equal to zero. There won't be anything to do, + * but this is not an error situation so make sure we + * return OK instead of BUF_ERROR at next call of deflate: + */ + s.last_flush = -1; + return Z_OK$3; + } + /* Make sure there is something to do and avoid duplicate consecutive + * flushes. For repeated and useless calls with Z_FINISH, we keep + * returning Z_STREAM_END instead of Z_BUF_ERROR. + */ + + } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) && flush !== Z_FINISH$3) { + return err(strm, Z_BUF_ERROR$1); + } + /* User must not provide more input after the first FINISH: */ + + + if (s.status === FINISH_STATE && strm.avail_in !== 0) { + return err(strm, Z_BUF_ERROR$1); + } + /* Start a new block or continue the current one. + */ + + + if (strm.avail_in !== 0 || s.lookahead !== 0 || flush !== Z_NO_FLUSH$2 && s.status !== FINISH_STATE) { + var bstate = s.strategy === Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : s.strategy === Z_RLE ? deflate_rle(s, flush) : configuration_table[s.level].func(s, flush); + + if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) { + s.status = FINISH_STATE; + } + + if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) { + if (strm.avail_out === 0) { + s.last_flush = -1; + /* avoid BUF_ERROR next call, see above */ + } + + return Z_OK$3; + /* If flush != Z_NO_FLUSH && avail_out == 0, the next call + * of deflate should use the same flush parameter to make sure + * that the flush is complete. So we don't have to output an + * empty block here, this will be done at next call. This also + * ensures that for a very small output buffer, we emit at most + * one empty block. + */ + } + + if (bstate === BS_BLOCK_DONE) { + if (flush === Z_PARTIAL_FLUSH) { + _tr_align(s); + } else if (flush !== Z_BLOCK$1) { + /* FULL_FLUSH or SYNC_FLUSH */ + _tr_stored_block(s, 0, 0, false); + /* For a full flush, this empty block will be recognized + * as a special marker by inflate_sync(). + */ + + + if (flush === Z_FULL_FLUSH$1) { + /*** CLEAR_HASH(s); ***/ + + /* forget history */ + zero(s.head); // Fill with NIL (= 0); + + if (s.lookahead === 0) { + s.strstart = 0; + s.block_start = 0; + s.insert = 0; + } + } + } + + flush_pending(strm); + + if (strm.avail_out === 0) { + s.last_flush = -1; + /* avoid BUF_ERROR at next call, see above */ + + return Z_OK$3; + } + } + } //Assert(strm->avail_out > 0, "bug2"); + //if (strm.avail_out <= 0) { throw new Error("bug2");} + + + if (flush !== Z_FINISH$3) { + return Z_OK$3; + } + + if (s.wrap <= 0) { + return Z_STREAM_END$3; + } + /* Write the trailer */ + + + if (s.wrap === 2) { + put_byte(s, strm.adler & 0xff); + put_byte(s, strm.adler >> 8 & 0xff); + put_byte(s, strm.adler >> 16 & 0xff); + put_byte(s, strm.adler >> 24 & 0xff); + put_byte(s, strm.total_in & 0xff); + put_byte(s, strm.total_in >> 8 & 0xff); + put_byte(s, strm.total_in >> 16 & 0xff); + put_byte(s, strm.total_in >> 24 & 0xff); + } else { + putShortMSB(s, strm.adler >>> 16); + putShortMSB(s, strm.adler & 0xffff); + } + + flush_pending(strm); + /* If avail_out is zero, the application will call deflate again + * to flush the rest. + */ + + if (s.wrap > 0) { + s.wrap = -s.wrap; + } + /* write the trailer only once! */ + + + return s.pending !== 0 ? Z_OK$3 : Z_STREAM_END$3; + }; + + var deflateEnd = function deflateEnd(strm) { + if (!strm + /*== Z_NULL*/ + || !strm.state + /*== Z_NULL*/ + ) { + return Z_STREAM_ERROR$2; + } + + var status = strm.state.status; + + if (status !== INIT_STATE && status !== EXTRA_STATE && status !== NAME_STATE && status !== COMMENT_STATE && status !== HCRC_STATE && status !== BUSY_STATE && status !== FINISH_STATE) { + return err(strm, Z_STREAM_ERROR$2); + } + + strm.state = null; + return status === BUSY_STATE ? err(strm, Z_DATA_ERROR$2) : Z_OK$3; + }; + /* ========================================================================= + * Initializes the compression dictionary from the given byte + * sequence without producing any compressed output. + */ + + + var deflateSetDictionary = function deflateSetDictionary(strm, dictionary) { + var dictLength = dictionary.length; + + if (!strm + /*== Z_NULL*/ + || !strm.state + /*== Z_NULL*/ + ) { + return Z_STREAM_ERROR$2; + } + + var s = strm.state; + var wrap = s.wrap; + + if (wrap === 2 || wrap === 1 && s.status !== INIT_STATE || s.lookahead) { + return Z_STREAM_ERROR$2; + } + /* when using zlib wrappers, compute Adler-32 for provided dictionary */ + + + if (wrap === 1) { + /* adler32(strm->adler, dictionary, dictLength); */ + strm.adler = adler32_1(strm.adler, dictionary, dictLength, 0); + } + + s.wrap = 0; + /* avoid computing Adler-32 in read_buf */ + + /* if dictionary would fill window, just replace the history */ + + if (dictLength >= s.w_size) { + if (wrap === 0) { + /* already empty otherwise */ + + /*** CLEAR_HASH(s); ***/ + zero(s.head); // Fill with NIL (= 0); + + s.strstart = 0; + s.block_start = 0; + s.insert = 0; + } + /* use the tail */ + // dictionary = dictionary.slice(dictLength - s.w_size); + + + var tmpDict = new Uint8Array(s.w_size); + tmpDict.set(dictionary.subarray(dictLength - s.w_size, dictLength), 0); + dictionary = tmpDict; + dictLength = s.w_size; + } + /* insert dictionary into window and hash */ + + + var avail = strm.avail_in; + var next = strm.next_in; + var input = strm.input; + strm.avail_in = dictLength; + strm.next_in = 0; + strm.input = dictionary; + fill_window(s); + + while (s.lookahead >= MIN_MATCH) { + var str = s.strstart; + var n = s.lookahead - (MIN_MATCH - 1); + + do { + /* UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); */ + s.ins_h = HASH(s, s.ins_h, s.window[str + MIN_MATCH - 1]); + s.prev[str & s.w_mask] = s.head[s.ins_h]; + s.head[s.ins_h] = str; + str++; + } while (--n); + + s.strstart = str; + s.lookahead = MIN_MATCH - 1; + fill_window(s); + } + + s.strstart += s.lookahead; + s.block_start = s.strstart; + s.insert = s.lookahead; + s.lookahead = 0; + s.match_length = s.prev_length = MIN_MATCH - 1; + s.match_available = 0; + strm.next_in = next; + strm.input = input; + strm.avail_in = avail; + s.wrap = wrap; + return Z_OK$3; + }; + + var deflateInit_1 = deflateInit; + var deflateInit2_1 = deflateInit2; + var deflateReset_1 = deflateReset; + var deflateResetKeep_1 = deflateResetKeep; + var deflateSetHeader_1 = deflateSetHeader; + var deflate_2$1 = deflate$2; + var deflateEnd_1 = deflateEnd; + var deflateSetDictionary_1 = deflateSetDictionary; + var deflateInfo = 'pako deflate (from Nodeca project)'; + /* Not implemented + module.exports.deflateBound = deflateBound; + module.exports.deflateCopy = deflateCopy; + module.exports.deflateParams = deflateParams; + module.exports.deflatePending = deflatePending; + module.exports.deflatePrime = deflatePrime; + module.exports.deflateTune = deflateTune; + */ + + var deflate_1$2 = { + deflateInit: deflateInit_1, + deflateInit2: deflateInit2_1, + deflateReset: deflateReset_1, + deflateResetKeep: deflateResetKeep_1, + deflateSetHeader: deflateSetHeader_1, + deflate: deflate_2$1, + deflateEnd: deflateEnd_1, + deflateSetDictionary: deflateSetDictionary_1, + deflateInfo: deflateInfo + }; + + function _typeof(obj) { + "@babel/helpers - typeof"; + + if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { + _typeof = function (obj) { + return typeof obj; + }; + } else { + _typeof = function (obj) { + return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; + }; + } + + return _typeof(obj); + } + + var _has = function _has(obj, key) { + return Object.prototype.hasOwnProperty.call(obj, key); + }; + + var assign = function assign(obj + /*from1, from2, from3, ...*/ + ) { + var sources = Array.prototype.slice.call(arguments, 1); + + while (sources.length) { + var source = sources.shift(); + + if (!source) { + continue; + } + + if (_typeof(source) !== 'object') { + throw new TypeError(source + 'must be non-object'); + } + + for (var p in source) { + if (_has(source, p)) { + obj[p] = source[p]; + } + } + } + + return obj; + }; // Join array of chunks to single array. + + + var flattenChunks = function flattenChunks(chunks) { + // calculate data length + var len = 0; + + for (var i = 0, l = chunks.length; i < l; i++) { + len += chunks[i].length; + } // join chunks + + + var result = new Uint8Array(len); + + for (var _i = 0, pos = 0, _l = chunks.length; _i < _l; _i++) { + var chunk = chunks[_i]; + result.set(chunk, pos); + pos += chunk.length; + } + + return result; + }; + + var common = { + assign: assign, + flattenChunks: flattenChunks + }; + + // String encode/decode helpers + // + // - apply(Array) can fail on Android 2.2 + // - apply(Uint8Array) can fail on iOS 5.1 Safari + // + + var STR_APPLY_UIA_OK = true; + + try { + String.fromCharCode.apply(null, new Uint8Array(1)); + } catch (__) { + STR_APPLY_UIA_OK = false; + } // Table with utf8 lengths (calculated by first byte of sequence) + // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS, + // because max possible codepoint is 0x10ffff + + + var _utf8len = new Uint8Array(256); + + for (var q = 0; q < 256; q++) { + _utf8len[q] = q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1; + } + + _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start + // convert string to array (typed, when possible) + + var string2buf = function string2buf(str) { + if (typeof TextEncoder === 'function' && TextEncoder.prototype.encode) { + return new TextEncoder().encode(str); + } + + var buf, + c, + c2, + m_pos, + i, + str_len = str.length, + buf_len = 0; // count binary size + + for (m_pos = 0; m_pos < str_len; m_pos++) { + c = str.charCodeAt(m_pos); + + if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) { + c2 = str.charCodeAt(m_pos + 1); + + if ((c2 & 0xfc00) === 0xdc00) { + c = 0x10000 + (c - 0xd800 << 10) + (c2 - 0xdc00); + m_pos++; + } + } + + buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4; + } // allocate buffer + + + buf = new Uint8Array(buf_len); // convert + + for (i = 0, m_pos = 0; i < buf_len; m_pos++) { + c = str.charCodeAt(m_pos); + + if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) { + c2 = str.charCodeAt(m_pos + 1); + + if ((c2 & 0xfc00) === 0xdc00) { + c = 0x10000 + (c - 0xd800 << 10) + (c2 - 0xdc00); + m_pos++; + } + } + + if (c < 0x80) { + /* one byte */ + buf[i++] = c; + } else if (c < 0x800) { + /* two bytes */ + buf[i++] = 0xC0 | c >>> 6; + buf[i++] = 0x80 | c & 0x3f; + } else if (c < 0x10000) { + /* three bytes */ + buf[i++] = 0xE0 | c >>> 12; + buf[i++] = 0x80 | c >>> 6 & 0x3f; + buf[i++] = 0x80 | c & 0x3f; + } else { + /* four bytes */ + buf[i++] = 0xf0 | c >>> 18; + buf[i++] = 0x80 | c >>> 12 & 0x3f; + buf[i++] = 0x80 | c >>> 6 & 0x3f; + buf[i++] = 0x80 | c & 0x3f; + } + } + + return buf; + }; // Helper + + + var buf2binstring = function buf2binstring(buf, len) { + // On Chrome, the arguments in a function call that are allowed is `65534`. + // If the length of the buffer is smaller than that, we can use this optimization, + // otherwise we will take a slower path. + if (len < 65534) { + if (buf.subarray && STR_APPLY_UIA_OK) { + return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len)); + } + } + + var result = ''; + + for (var i = 0; i < len; i++) { + result += String.fromCharCode(buf[i]); + } + + return result; + }; // convert array to string + + + var buf2string = function buf2string(buf, max) { + var len = max || buf.length; + + if (typeof TextDecoder === 'function' && TextDecoder.prototype.decode) { + return new TextDecoder().decode(buf.subarray(0, max)); + } + + var i, out; // Reserve max possible length (2 words per char) + // NB: by unknown reasons, Array is significantly faster for + // String.fromCharCode.apply than Uint16Array. + + var utf16buf = new Array(len * 2); + + for (out = 0, i = 0; i < len;) { + var c = buf[i++]; // quick process ascii + + if (c < 0x80) { + utf16buf[out++] = c; + continue; + } + + var c_len = _utf8len[c]; // skip 5 & 6 byte codes + + if (c_len > 4) { + utf16buf[out++] = 0xfffd; + i += c_len - 1; + continue; + } // apply mask on first byte + + + c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07; // join the rest + + while (c_len > 1 && i < len) { + c = c << 6 | buf[i++] & 0x3f; + c_len--; + } // terminated by end of string? + + + if (c_len > 1) { + utf16buf[out++] = 0xfffd; + continue; + } + + if (c < 0x10000) { + utf16buf[out++] = c; + } else { + c -= 0x10000; + utf16buf[out++] = 0xd800 | c >> 10 & 0x3ff; + utf16buf[out++] = 0xdc00 | c & 0x3ff; + } + } + + return buf2binstring(utf16buf, out); + }; // Calculate max possible position in utf8 buffer, + // that will not break sequence. If that's not possible + // - (very small limits) return max size as is. + // + // buf[] - utf8 bytes array + // max - length limit (mandatory); + + + var utf8border = function utf8border(buf, max) { + max = max || buf.length; + + if (max > buf.length) { + max = buf.length; + } // go back from last position, until start of sequence found + + + var pos = max - 1; + + while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { + pos--; + } // Very small and broken sequence, + // return max, because we should return something anyway. + + + if (pos < 0) { + return max; + } // If we came to start of buffer - that means buffer is too small, + // return max too. + + + if (pos === 0) { + return max; + } + + return pos + _utf8len[buf[pos]] > max ? pos : max; + }; + + var strings = { + string2buf: string2buf, + buf2string: buf2string, + utf8border: utf8border + }; + + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + + function ZStream() { + /* next input byte */ + this.input = null; // JS specific, because we have no pointers + + this.next_in = 0; + /* number of bytes available at input */ + + this.avail_in = 0; + /* total number of input bytes read so far */ + + this.total_in = 0; + /* next output byte should be put there */ + + this.output = null; // JS specific, because we have no pointers + + this.next_out = 0; + /* remaining free space at output */ + + this.avail_out = 0; + /* total number of bytes output so far */ + + this.total_out = 0; + /* last error message, NULL if no error */ + + this.msg = '' + /*Z_NULL*/ + ; + /* not visible by applications */ + + this.state = null; + /* best guess about the data type: binary or text */ + + this.data_type = 2 + /*Z_UNKNOWN*/ + ; + /* adler32 value of the uncompressed data */ + + this.adler = 0; + } + + var zstream = ZStream; + + var toString$1 = Object.prototype.toString; + /* Public constants ==========================================================*/ + + /* ===========================================================================*/ + + var Z_NO_FLUSH$1 = constants$2.Z_NO_FLUSH, + Z_SYNC_FLUSH = constants$2.Z_SYNC_FLUSH, + Z_FULL_FLUSH = constants$2.Z_FULL_FLUSH, + Z_FINISH$2 = constants$2.Z_FINISH, + Z_OK$2 = constants$2.Z_OK, + Z_STREAM_END$2 = constants$2.Z_STREAM_END, + Z_DEFAULT_COMPRESSION = constants$2.Z_DEFAULT_COMPRESSION, + Z_DEFAULT_STRATEGY = constants$2.Z_DEFAULT_STRATEGY, + Z_DEFLATED$1 = constants$2.Z_DEFLATED; + /* ===========================================================================*/ + + /** + * class Deflate + * + * Generic JS-style wrapper for zlib calls. If you don't need + * streaming behaviour - use more simple functions: [[deflate]], + * [[deflateRaw]] and [[gzip]]. + **/ + + /* internal + * Deflate.chunks -> Array + * + * Chunks of output data, if [[Deflate#onData]] not overridden. + **/ + + /** + * Deflate.result -> Uint8Array + * + * Compressed result, generated by default [[Deflate#onData]] + * and [[Deflate#onEnd]] handlers. Filled after you push last chunk + * (call [[Deflate#push]] with `Z_FINISH` / `true` param). + **/ + + /** + * Deflate.err -> Number + * + * Error code after deflate finished. 0 (Z_OK) on success. + * You will not need it in real life, because deflate errors + * are possible only on wrong options or bad `onData` / `onEnd` + * custom handlers. + **/ + + /** + * Deflate.msg -> String + * + * Error message, if [[Deflate.err]] != 0 + **/ + + /** + * new Deflate(options) + * - options (Object): zlib deflate options. + * + * Creates new deflator instance with specified params. Throws exception + * on bad params. Supported options: + * + * - `level` + * - `windowBits` + * - `memLevel` + * - `strategy` + * - `dictionary` + * + * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced) + * for more information on these. + * + * Additional options, for internal needs: + * + * - `chunkSize` - size of generated data chunks (16K by default) + * - `raw` (Boolean) - do raw deflate + * - `gzip` (Boolean) - create gzip wrapper + * - `header` (Object) - custom header for gzip + * - `text` (Boolean) - true if compressed data believed to be text + * - `time` (Number) - modification time, unix timestamp + * - `os` (Number) - operation system code + * - `extra` (Array) - array of bytes with extra data (max 65536) + * - `name` (String) - file name (binary string) + * - `comment` (String) - comment (binary string) + * - `hcrc` (Boolean) - true if header crc should be added + * + * ##### Example: + * + * ```javascript + * const pako = require('pako') + * , chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9]) + * , chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]); + * + * const deflate = new pako.Deflate({ level: 3}); + * + * deflate.push(chunk1, false); + * deflate.push(chunk2, true); // true -> last chunk + * + * if (deflate.err) { throw new Error(deflate.err); } + * + * console.log(deflate.result); + * ``` + **/ + + function Deflate$1(options) { + this.options = common.assign({ + level: Z_DEFAULT_COMPRESSION, + method: Z_DEFLATED$1, + chunkSize: 16384, + windowBits: 15, + memLevel: 8, + strategy: Z_DEFAULT_STRATEGY + }, options || {}); + var opt = this.options; + + if (opt.raw && opt.windowBits > 0) { + opt.windowBits = -opt.windowBits; + } else if (opt.gzip && opt.windowBits > 0 && opt.windowBits < 16) { + opt.windowBits += 16; + } + + this.err = 0; // error code, if happens (0 = Z_OK) + + this.msg = ''; // error message + + this.ended = false; // used to avoid multiple onEnd() calls + + this.chunks = []; // chunks of compressed data + + this.strm = new zstream(); + this.strm.avail_out = 0; + var status = deflate_1$2.deflateInit2(this.strm, opt.level, opt.method, opt.windowBits, opt.memLevel, opt.strategy); + + if (status !== Z_OK$2) { + throw new Error(messages[status]); + } + + if (opt.header) { + deflate_1$2.deflateSetHeader(this.strm, opt.header); + } + + if (opt.dictionary) { + var dict; // Convert data if needed + + if (typeof opt.dictionary === 'string') { + // If we need to compress text, change encoding to utf8. + dict = strings.string2buf(opt.dictionary); + } else if (toString$1.call(opt.dictionary) === '[object ArrayBuffer]') { + dict = new Uint8Array(opt.dictionary); + } else { + dict = opt.dictionary; + } + + status = deflate_1$2.deflateSetDictionary(this.strm, dict); + + if (status !== Z_OK$2) { + throw new Error(messages[status]); + } + + this._dict_set = true; + } + } + /** + * Deflate#push(data[, flush_mode]) -> Boolean + * - data (Uint8Array|ArrayBuffer|String): input data. Strings will be + * converted to utf8 byte sequence. + * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes. + * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH. + * + * Sends input data to deflate pipe, generating [[Deflate#onData]] calls with + * new compressed chunks. Returns `true` on success. The last data block must + * have `flush_mode` Z_FINISH (or `true`). That will flush internal pending + * buffers and call [[Deflate#onEnd]]. + * + * On fail call [[Deflate#onEnd]] with error code and return false. + * + * ##### Example + * + * ```javascript + * push(chunk, false); // push one of data chunks + * ... + * push(chunk, true); // push last chunk + * ``` + **/ + + + Deflate$1.prototype.push = function (data, flush_mode) { + var strm = this.strm; + var chunkSize = this.options.chunkSize; + + var status, _flush_mode; + + if (this.ended) { + return false; + } + + if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;else _flush_mode = flush_mode === true ? Z_FINISH$2 : Z_NO_FLUSH$1; // Convert data if needed + + if (typeof data === 'string') { + // If we need to compress text, change encoding to utf8. + strm.input = strings.string2buf(data); + } else if (toString$1.call(data) === '[object ArrayBuffer]') { + strm.input = new Uint8Array(data); + } else { + strm.input = data; + } + + strm.next_in = 0; + strm.avail_in = strm.input.length; + + for (;;) { + if (strm.avail_out === 0) { + strm.output = new Uint8Array(chunkSize); + strm.next_out = 0; + strm.avail_out = chunkSize; + } // Make sure avail_out > 6 to avoid repeating markers + + + if ((_flush_mode === Z_SYNC_FLUSH || _flush_mode === Z_FULL_FLUSH) && strm.avail_out <= 6) { + this.onData(strm.output.subarray(0, strm.next_out)); + strm.avail_out = 0; + continue; + } + + status = deflate_1$2.deflate(strm, _flush_mode); // Ended => flush and finish + + if (status === Z_STREAM_END$2) { + if (strm.next_out > 0) { + this.onData(strm.output.subarray(0, strm.next_out)); + } + + status = deflate_1$2.deflateEnd(this.strm); + this.onEnd(status); + this.ended = true; + return status === Z_OK$2; + } // Flush if out buffer full + + + if (strm.avail_out === 0) { + this.onData(strm.output); + continue; + } // Flush if requested and has data + + + if (_flush_mode > 0 && strm.next_out > 0) { + this.onData(strm.output.subarray(0, strm.next_out)); + strm.avail_out = 0; + continue; + } + + if (strm.avail_in === 0) break; + } + + return true; + }; + /** + * Deflate#onData(chunk) -> Void + * - chunk (Uint8Array): output data. + * + * By default, stores data blocks in `chunks[]` property and glue + * those in `onEnd`. Override this handler, if you need another behaviour. + **/ + + + Deflate$1.prototype.onData = function (chunk) { + this.chunks.push(chunk); + }; + /** + * Deflate#onEnd(status) -> Void + * - status (Number): deflate status. 0 (Z_OK) on success, + * other if not. + * + * Called once after you tell deflate that the input stream is + * complete (Z_FINISH). By default - join collected chunks, + * free memory and fill `results` / `err` properties. + **/ + + + Deflate$1.prototype.onEnd = function (status) { + // On success - join + if (status === Z_OK$2) { + this.result = common.flattenChunks(this.chunks); + } + + this.chunks = []; + this.err = status; + this.msg = this.strm.msg; + }; + /** + * deflate(data[, options]) -> Uint8Array + * - data (Uint8Array|String): input data to compress. + * - options (Object): zlib deflate options. + * + * Compress `data` with deflate algorithm and `options`. + * + * Supported options are: + * + * - level + * - windowBits + * - memLevel + * - strategy + * - dictionary + * + * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced) + * for more information on these. + * + * Sugar (options): + * + * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify + * negative windowBits implicitly. + * + * ##### Example: + * + * ```javascript + * const pako = require('pako') + * const data = new Uint8Array([1,2,3,4,5,6,7,8,9]); + * + * console.log(pako.deflate(data)); + * ``` + **/ + + + function deflate$1(input, options) { + var deflator = new Deflate$1(options); + deflator.push(input, true); // That will never happens, if you don't cheat with options :) + + if (deflator.err) { + throw deflator.msg || messages[deflator.err]; + } + + return deflator.result; + } + /** + * deflateRaw(data[, options]) -> Uint8Array + * - data (Uint8Array|String): input data to compress. + * - options (Object): zlib deflate options. + * + * The same as [[deflate]], but creates raw data, without wrapper + * (header and adler32 crc). + **/ + + + function deflateRaw$1(input, options) { + options = options || {}; + options.raw = true; + return deflate$1(input, options); + } + /** + * gzip(data[, options]) -> Uint8Array + * - data (Uint8Array|String): input data to compress. + * - options (Object): zlib deflate options. + * + * The same as [[deflate]], but create gzip wrapper instead of + * deflate one. + **/ + + + function gzip$1(input, options) { + options = options || {}; + options.gzip = true; + return deflate$1(input, options); + } + + var Deflate_1$1 = Deflate$1; + var deflate_2 = deflate$1; + var deflateRaw_1$1 = deflateRaw$1; + var gzip_1$1 = gzip$1; + var constants$1 = constants$2; + var deflate_1$1 = { + Deflate: Deflate_1$1, + deflate: deflate_2, + deflateRaw: deflateRaw_1$1, + gzip: gzip_1$1, + constants: constants$1 + }; + + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + // See state defs from inflate.js + + var BAD$1 = 30; + /* got a data error -- remain here until reset */ + + var TYPE$1 = 12; + /* i: waiting for type bits, including last-flag bit */ + + /* + Decode literal, length, and distance codes and write out the resulting + literal and match bytes until either not enough input or output is + available, an end-of-block is encountered, or a data error is encountered. + When large enough input and output buffers are supplied to inflate(), for + example, a 16K input buffer and a 64K output buffer, more than 95% of the + inflate execution time is spent in this routine. + + Entry assumptions: + + state.mode === LEN + strm.avail_in >= 6 + strm.avail_out >= 258 + start >= strm.avail_out + state.bits < 8 + + On return, state.mode is one of: + + LEN -- ran out of enough output space or enough available input + TYPE -- reached end of block code, inflate() to interpret next block + BAD -- error in block data + + Notes: + + - The maximum input bits used by a length/distance pair is 15 bits for the + length code, 5 bits for the length extra, 15 bits for the distance code, + and 13 bits for the distance extra. This totals 48 bits, or six bytes. + Therefore if strm.avail_in >= 6, then there is enough input to avoid + checking for available input while decoding. + + - The maximum bytes that a single length/distance pair can output is 258 + bytes, which is the maximum length that can be coded. inflate_fast() + requires strm.avail_out >= 258 for each loop to avoid checking for + output space. + */ + + var inffast = function inflate_fast(strm, start) { + var _in; + /* local strm.input */ + + + var last; + /* have enough input while in < last */ + + var _out; + /* local strm.output */ + + + var beg; + /* inflate()'s initial strm.output */ + + var end; + /* while out < end, enough space available */ + //#ifdef INFLATE_STRICT + + var dmax; + /* maximum distance from zlib header */ + //#endif + + var wsize; + /* window size or zero if not using window */ + + var whave; + /* valid bytes in the window */ + + var wnext; + /* window write index */ + // Use `s_window` instead `window`, avoid conflict with instrumentation tools + + var s_window; + /* allocated sliding window, if wsize != 0 */ + + var hold; + /* local strm.hold */ + + var bits; + /* local strm.bits */ + + var lcode; + /* local strm.lencode */ + + var dcode; + /* local strm.distcode */ + + var lmask; + /* mask for first level of length codes */ + + var dmask; + /* mask for first level of distance codes */ + + var here; + /* retrieved table entry */ + + var op; + /* code bits, operation, extra bits, or */ + + /* window position, window bytes to copy */ + + var len; + /* match length, unused bytes */ + + var dist; + /* match distance */ + + var from; + /* where to copy match from */ + + var from_source; + var input, output; // JS specific, because we have no pointers + + /* copy state to local variables */ + + var state = strm.state; //here = state.here; + + _in = strm.next_in; + input = strm.input; + last = _in + (strm.avail_in - 5); + _out = strm.next_out; + output = strm.output; + beg = _out - (start - strm.avail_out); + end = _out + (strm.avail_out - 257); //#ifdef INFLATE_STRICT + + dmax = state.dmax; //#endif + + wsize = state.wsize; + whave = state.whave; + wnext = state.wnext; + s_window = state.window; + hold = state.hold; + bits = state.bits; + lcode = state.lencode; + dcode = state.distcode; + lmask = (1 << state.lenbits) - 1; + dmask = (1 << state.distbits) - 1; + /* decode literals and length/distances until end-of-block or not enough + input data or output space */ + + top: do { + if (bits < 15) { + hold += input[_in++] << bits; + bits += 8; + hold += input[_in++] << bits; + bits += 8; + } + + here = lcode[hold & lmask]; + + dolen: for (;;) { + // Goto emulation + op = here >>> 24 + /*here.bits*/ + ; + hold >>>= op; + bits -= op; + op = here >>> 16 & 0xff + /*here.op*/ + ; + + if (op === 0) { + /* literal */ + //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + // "inflate: literal '%c'\n" : + // "inflate: literal 0x%02x\n", here.val)); + output[_out++] = here & 0xffff + /*here.val*/ + ; + } else if (op & 16) { + /* length base */ + len = here & 0xffff + /*here.val*/ + ; + op &= 15; + /* number of extra bits */ + + if (op) { + if (bits < op) { + hold += input[_in++] << bits; + bits += 8; + } + + len += hold & (1 << op) - 1; + hold >>>= op; + bits -= op; + } //Tracevv((stderr, "inflate: length %u\n", len)); + + + if (bits < 15) { + hold += input[_in++] << bits; + bits += 8; + hold += input[_in++] << bits; + bits += 8; + } + + here = dcode[hold & dmask]; + + dodist: for (;;) { + // goto emulation + op = here >>> 24 + /*here.bits*/ + ; + hold >>>= op; + bits -= op; + op = here >>> 16 & 0xff + /*here.op*/ + ; + + if (op & 16) { + /* distance base */ + dist = here & 0xffff + /*here.val*/ + ; + op &= 15; + /* number of extra bits */ + + if (bits < op) { + hold += input[_in++] << bits; + bits += 8; + + if (bits < op) { + hold += input[_in++] << bits; + bits += 8; + } + } + + dist += hold & (1 << op) - 1; //#ifdef INFLATE_STRICT + + if (dist > dmax) { + strm.msg = 'invalid distance too far back'; + state.mode = BAD$1; + break top; + } //#endif + + + hold >>>= op; + bits -= op; //Tracevv((stderr, "inflate: distance %u\n", dist)); + + op = _out - beg; + /* max distance in output */ + + if (dist > op) { + /* see if copy from window */ + op = dist - op; + /* distance back in window */ + + if (op > whave) { + if (state.sane) { + strm.msg = 'invalid distance too far back'; + state.mode = BAD$1; + break top; + } // (!) This block is disabled in zlib defaults, + // don't enable it for binary compatibility + //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR + // if (len <= op - whave) { + // do { + // output[_out++] = 0; + // } while (--len); + // continue top; + // } + // len -= op - whave; + // do { + // output[_out++] = 0; + // } while (--op > whave); + // if (op === 0) { + // from = _out - dist; + // do { + // output[_out++] = output[from++]; + // } while (--len); + // continue top; + // } + //#endif + + } + + from = 0; // window index + + from_source = s_window; + + if (wnext === 0) { + /* very common case */ + from += wsize - op; + + if (op < len) { + /* some from window */ + len -= op; + + do { + output[_out++] = s_window[from++]; + } while (--op); + + from = _out - dist; + /* rest from output */ + + from_source = output; + } + } else if (wnext < op) { + /* wrap around window */ + from += wsize + wnext - op; + op -= wnext; + + if (op < len) { + /* some from end of window */ + len -= op; + + do { + output[_out++] = s_window[from++]; + } while (--op); + + from = 0; + + if (wnext < len) { + /* some from start of window */ + op = wnext; + len -= op; + + do { + output[_out++] = s_window[from++]; + } while (--op); + + from = _out - dist; + /* rest from output */ + + from_source = output; + } + } + } else { + /* contiguous in window */ + from += wnext - op; + + if (op < len) { + /* some from window */ + len -= op; + + do { + output[_out++] = s_window[from++]; + } while (--op); + + from = _out - dist; + /* rest from output */ + + from_source = output; + } + } + + while (len > 2) { + output[_out++] = from_source[from++]; + output[_out++] = from_source[from++]; + output[_out++] = from_source[from++]; + len -= 3; + } + + if (len) { + output[_out++] = from_source[from++]; + + if (len > 1) { + output[_out++] = from_source[from++]; + } + } + } else { + from = _out - dist; + /* copy direct from output */ + + do { + /* minimum length is three */ + output[_out++] = output[from++]; + output[_out++] = output[from++]; + output[_out++] = output[from++]; + len -= 3; + } while (len > 2); + + if (len) { + output[_out++] = output[from++]; + + if (len > 1) { + output[_out++] = output[from++]; + } + } + } + } else if ((op & 64) === 0) { + /* 2nd level distance code */ + here = dcode[(here & 0xffff) + (hold & (1 << op) - 1)]; + continue dodist; + } else { + strm.msg = 'invalid distance code'; + state.mode = BAD$1; + break top; + } + + break; // need to emulate goto via "continue" + } + } else if ((op & 64) === 0) { + /* 2nd level length code */ + here = lcode[(here & 0xffff) + (hold & (1 << op) - 1)]; + continue dolen; + } else if (op & 32) { + /* end-of-block */ + //Tracevv((stderr, "inflate: end of block\n")); + state.mode = TYPE$1; + break top; + } else { + strm.msg = 'invalid literal/length code'; + state.mode = BAD$1; + break top; + } + + break; // need to emulate goto via "continue" + } + } while (_in < last && _out < end); + /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ + + + len = bits >> 3; + _in -= len; + bits -= len << 3; + hold &= (1 << bits) - 1; + /* update state and return */ + + strm.next_in = _in; + strm.next_out = _out; + strm.avail_in = _in < last ? 5 + (last - _in) : 5 - (_in - last); + strm.avail_out = _out < end ? 257 + (end - _out) : 257 - (_out - end); + state.hold = hold; + state.bits = bits; + return; + }; + + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + + var MAXBITS = 15; + var ENOUGH_LENS$1 = 852; + var ENOUGH_DISTS$1 = 592; //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); + + var CODES$1 = 0; + var LENS$1 = 1; + var DISTS$1 = 2; + var lbase = new Uint16Array([ + /* Length codes 257..285 base */ + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0]); + var lext = new Uint8Array([ + /* Length codes 257..285 extra */ + 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78]); + var dbase = new Uint16Array([ + /* Distance codes 0..29 base */ + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0]); + var dext = new Uint8Array([ + /* Distance codes 0..29 extra */ + 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 64, 64]); + + var inflate_table = function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts) { + var bits = opts.bits; //here = opts.here; /* table entry for duplication */ + + var len = 0; + /* a code's length in bits */ + + var sym = 0; + /* index of code symbols */ + + var min = 0, + max = 0; + /* minimum and maximum code lengths */ + + var root = 0; + /* number of index bits for root table */ + + var curr = 0; + /* number of index bits for current table */ + + var drop = 0; + /* code bits to drop for sub-table */ + + var left = 0; + /* number of prefix codes available */ + + var used = 0; + /* code entries in table used */ + + var huff = 0; + /* Huffman code */ + + var incr; + /* for incrementing code, index */ + + var fill; + /* index for replicating entries */ + + var low; + /* low bits for current root entry */ + + var mask; + /* mask for low root bits */ + + var next; + /* next available space in table */ + + var base = null; + /* base value table to use */ + + var base_index = 0; // let shoextra; /* extra bits table to use */ + + var end; + /* use base and extra for symbol > end */ + + var count = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* number of codes of each length */ + + var offs = new Uint16Array(MAXBITS + 1); //[MAXBITS+1]; /* offsets in table for each length */ + + var extra = null; + var extra_index = 0; + var here_bits, here_op, here_val; + /* + Process a set of code lengths to create a canonical Huffman code. The + code lengths are lens[0..codes-1]. Each length corresponds to the + symbols 0..codes-1. The Huffman code is generated by first sorting the + symbols by length from short to long, and retaining the symbol order + for codes with equal lengths. Then the code starts with all zero bits + for the first code of the shortest length, and the codes are integer + increments for the same length, and zeros are appended as the length + increases. For the deflate format, these bits are stored backwards + from their more natural integer increment ordering, and so when the + decoding tables are built in the large loop below, the integer codes + are incremented backwards. + This routine assumes, but does not check, that all of the entries in + lens[] are in the range 0..MAXBITS. The caller must assure this. + 1..MAXBITS is interpreted as that code length. zero means that that + symbol does not occur in this code. + The codes are sorted by computing a count of codes for each length, + creating from that a table of starting indices for each length in the + sorted table, and then entering the symbols in order in the sorted + table. The sorted table is work[], with that space being provided by + the caller. + The length counts are used for other purposes as well, i.e. finding + the minimum and maximum length codes, determining if there are any + codes at all, checking for a valid set of lengths, and looking ahead + at length counts to determine sub-table sizes when building the + decoding tables. + */ + + /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ + + for (len = 0; len <= MAXBITS; len++) { + count[len] = 0; + } + + for (sym = 0; sym < codes; sym++) { + count[lens[lens_index + sym]]++; + } + /* bound code lengths, force root to be within code lengths */ + + + root = bits; + + for (max = MAXBITS; max >= 1; max--) { + if (count[max] !== 0) { + break; + } + } + + if (root > max) { + root = max; + } + + if (max === 0) { + /* no symbols to code at all */ + //table.op[opts.table_index] = 64; //here.op = (var char)64; /* invalid code marker */ + //table.bits[opts.table_index] = 1; //here.bits = (var char)1; + //table.val[opts.table_index++] = 0; //here.val = (var short)0; + table[table_index++] = 1 << 24 | 64 << 16 | 0; //table.op[opts.table_index] = 64; + //table.bits[opts.table_index] = 1; + //table.val[opts.table_index++] = 0; + + table[table_index++] = 1 << 24 | 64 << 16 | 0; + opts.bits = 1; + return 0; + /* no symbols, but wait for decoding to report error */ + } + + for (min = 1; min < max; min++) { + if (count[min] !== 0) { + break; + } + } + + if (root < min) { + root = min; + } + /* check for an over-subscribed or incomplete set of lengths */ + + + left = 1; + + for (len = 1; len <= MAXBITS; len++) { + left <<= 1; + left -= count[len]; + + if (left < 0) { + return -1; + } + /* over-subscribed */ + + } + + if (left > 0 && (type === CODES$1 || max !== 1)) { + return -1; + /* incomplete set */ + } + /* generate offsets into symbol table for each length for sorting */ + + + offs[1] = 0; + + for (len = 1; len < MAXBITS; len++) { + offs[len + 1] = offs[len] + count[len]; + } + /* sort symbols by length, by symbol order within each length */ + + + for (sym = 0; sym < codes; sym++) { + if (lens[lens_index + sym] !== 0) { + work[offs[lens[lens_index + sym]]++] = sym; + } + } + /* + Create and fill in decoding tables. In this loop, the table being + filled is at next and has curr index bits. The code being used is huff + with length len. That code is converted to an index by dropping drop + bits off of the bottom. For codes where len is less than drop + curr, + those top drop + curr - len bits are incremented through all values to + fill the table with replicated entries. + root is the number of index bits for the root table. When len exceeds + root, sub-tables are created pointed to by the root entry with an index + of the low root bits of huff. This is saved in low to check for when a + new sub-table should be started. drop is zero when the root table is + being filled, and drop is root when sub-tables are being filled. + When a new sub-table is needed, it is necessary to look ahead in the + code lengths to determine what size sub-table is needed. The length + counts are used for this, and so count[] is decremented as codes are + entered in the tables. + used keeps track of how many table entries have been allocated from the + provided *table space. It is checked for LENS and DIST tables against + the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in + the initial root table size constants. See the comments in inftrees.h + for more information. + sym increments through all symbols, and the loop terminates when + all codes of length max, i.e. all codes, have been processed. This + routine permits incomplete codes, so another loop after this one fills + in the rest of the decoding tables with invalid code markers. + */ + + /* set up for code type */ + // poor man optimization - use if-else instead of switch, + // to avoid deopts in old v8 + + + if (type === CODES$1) { + base = extra = work; + /* dummy value--not used */ + + end = 19; + } else if (type === LENS$1) { + base = lbase; + base_index -= 257; + extra = lext; + extra_index -= 257; + end = 256; + } else { + /* DISTS */ + base = dbase; + extra = dext; + end = -1; + } + /* initialize opts for loop */ + + + huff = 0; + /* starting code */ + + sym = 0; + /* starting code symbol */ + + len = min; + /* starting code length */ + + next = table_index; + /* current table to fill in */ + + curr = root; + /* current table index bits */ + + drop = 0; + /* current bits to drop from code for index */ + + low = -1; + /* trigger new sub-table when len > root */ + + used = 1 << root; + /* use root table entries */ + + mask = used - 1; + /* mask for comparing low */ + + /* check available table space */ + + if (type === LENS$1 && used > ENOUGH_LENS$1 || type === DISTS$1 && used > ENOUGH_DISTS$1) { + return 1; + } + /* process all codes and make table entries */ + + + for (;;) { + /* create table entry */ + here_bits = len - drop; + + if (work[sym] < end) { + here_op = 0; + here_val = work[sym]; + } else if (work[sym] > end) { + here_op = extra[extra_index + work[sym]]; + here_val = base[base_index + work[sym]]; + } else { + here_op = 32 + 64; + /* end of block */ + + here_val = 0; + } + /* replicate for those indices with low len bits equal to huff */ + + + incr = 1 << len - drop; + fill = 1 << curr; + min = fill; + /* save offset to next table */ + + do { + fill -= incr; + table[next + (huff >> drop) + fill] = here_bits << 24 | here_op << 16 | here_val | 0; + } while (fill !== 0); + /* backwards increment the len-bit code huff */ + + + incr = 1 << len - 1; + + while (huff & incr) { + incr >>= 1; + } + + if (incr !== 0) { + huff &= incr - 1; + huff += incr; + } else { + huff = 0; + } + /* go to next symbol, update count, len */ + + + sym++; + + if (--count[len] === 0) { + if (len === max) { + break; + } + + len = lens[lens_index + work[sym]]; + } + /* create new sub-table if needed */ + + + if (len > root && (huff & mask) !== low) { + /* if first time, transition to sub-tables */ + if (drop === 0) { + drop = root; + } + /* increment past last table */ + + + next += min; + /* here min is 1 << curr */ + + /* determine length of next table */ + + curr = len - drop; + left = 1 << curr; + + while (curr + drop < max) { + left -= count[curr + drop]; + + if (left <= 0) { + break; + } + + curr++; + left <<= 1; + } + /* check for enough space */ + + + used += 1 << curr; + + if (type === LENS$1 && used > ENOUGH_LENS$1 || type === DISTS$1 && used > ENOUGH_DISTS$1) { + return 1; + } + /* point entry in root table to sub-table */ + + + low = huff & mask; + /*table.op[low] = curr; + table.bits[low] = root; + table.val[low] = next - opts.table_index;*/ + + table[low] = root << 24 | curr << 16 | next - table_index | 0; + } + } + /* fill in remaining table entry if code is incomplete (guaranteed to have + at most one remaining entry, since if the code is incomplete, the + maximum code length that was allowed to get this far is one bit) */ + + + if (huff !== 0) { + //table.op[next + huff] = 64; /* invalid code marker */ + //table.bits[next + huff] = len - drop; + //table.val[next + huff] = 0; + table[next + huff] = len - drop << 24 | 64 << 16 | 0; + } + /* set return parameters */ + //opts.table_index += used; + + + opts.bits = root; + return 0; + }; + + var inftrees = inflate_table; + + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + + + var CODES = 0; + var LENS = 1; + var DISTS = 2; + /* Public constants ==========================================================*/ + + /* ===========================================================================*/ + + var Z_FINISH$1 = constants$2.Z_FINISH, + Z_BLOCK = constants$2.Z_BLOCK, + Z_TREES = constants$2.Z_TREES, + Z_OK$1 = constants$2.Z_OK, + Z_STREAM_END$1 = constants$2.Z_STREAM_END, + Z_NEED_DICT$1 = constants$2.Z_NEED_DICT, + Z_STREAM_ERROR$1 = constants$2.Z_STREAM_ERROR, + Z_DATA_ERROR$1 = constants$2.Z_DATA_ERROR, + Z_MEM_ERROR$1 = constants$2.Z_MEM_ERROR, + Z_BUF_ERROR = constants$2.Z_BUF_ERROR, + Z_DEFLATED = constants$2.Z_DEFLATED; + /* STATES ====================================================================*/ + + /* ===========================================================================*/ + + var HEAD = 1; + /* i: waiting for magic header */ + + var FLAGS = 2; + /* i: waiting for method and flags (gzip) */ + + var TIME = 3; + /* i: waiting for modification time (gzip) */ + + var OS = 4; + /* i: waiting for extra flags and operating system (gzip) */ + + var EXLEN = 5; + /* i: waiting for extra length (gzip) */ + + var EXTRA = 6; + /* i: waiting for extra bytes (gzip) */ + + var NAME = 7; + /* i: waiting for end of file name (gzip) */ + + var COMMENT = 8; + /* i: waiting for end of comment (gzip) */ + + var HCRC = 9; + /* i: waiting for header crc (gzip) */ + + var DICTID = 10; + /* i: waiting for dictionary check value */ + + var DICT = 11; + /* waiting for inflateSetDictionary() call */ + + var TYPE = 12; + /* i: waiting for type bits, including last-flag bit */ + + var TYPEDO = 13; + /* i: same, but skip check to exit inflate on new block */ + + var STORED = 14; + /* i: waiting for stored size (length and complement) */ + + var COPY_ = 15; + /* i/o: same as COPY below, but only first time in */ + + var COPY = 16; + /* i/o: waiting for input or output to copy stored block */ + + var TABLE = 17; + /* i: waiting for dynamic block table lengths */ + + var LENLENS = 18; + /* i: waiting for code length code lengths */ + + var CODELENS = 19; + /* i: waiting for length/lit and distance code lengths */ + + var LEN_ = 20; + /* i: same as LEN below, but only first time in */ + + var LEN = 21; + /* i: waiting for length/lit/eob code */ + + var LENEXT = 22; + /* i: waiting for length extra bits */ + + var DIST = 23; + /* i: waiting for distance code */ + + var DISTEXT = 24; + /* i: waiting for distance extra bits */ + + var MATCH = 25; + /* o: waiting for output space to copy string */ + + var LIT = 26; + /* o: waiting for output space to write literal */ + + var CHECK = 27; + /* i: waiting for 32-bit check value */ + + var LENGTH = 28; + /* i: waiting for 32-bit length (gzip) */ + + var DONE = 29; + /* finished check, done -- remain here until reset */ + + var BAD = 30; + /* got a data error -- remain here until reset */ + + var MEM = 31; + /* got an inflate() memory error -- remain here until reset */ + + var SYNC = 32; + /* looking for synchronization bytes to restart inflate() */ + + /* ===========================================================================*/ + + var ENOUGH_LENS = 852; + var ENOUGH_DISTS = 592; //const ENOUGH = (ENOUGH_LENS+ENOUGH_DISTS); + + var MAX_WBITS = 15; + /* 32K LZ77 window */ + + var DEF_WBITS = MAX_WBITS; + + var zswap32 = function zswap32(q) { + return (q >>> 24 & 0xff) + (q >>> 8 & 0xff00) + ((q & 0xff00) << 8) + ((q & 0xff) << 24); + }; + + function InflateState() { + this.mode = 0; + /* current inflate mode */ + + this.last = false; + /* true if processing last block */ + + this.wrap = 0; + /* bit 0 true for zlib, bit 1 true for gzip */ + + this.havedict = false; + /* true if dictionary provided */ + + this.flags = 0; + /* gzip header method and flags (0 if zlib) */ + + this.dmax = 0; + /* zlib header max distance (INFLATE_STRICT) */ + + this.check = 0; + /* protected copy of check value */ + + this.total = 0; + /* protected copy of output count */ + // TODO: may be {} + + this.head = null; + /* where to save gzip header information */ + + /* sliding window */ + + this.wbits = 0; + /* log base 2 of requested window size */ + + this.wsize = 0; + /* window size or zero if not using window */ + + this.whave = 0; + /* valid bytes in the window */ + + this.wnext = 0; + /* window write index */ + + this.window = null; + /* allocated sliding window, if needed */ + + /* bit accumulator */ + + this.hold = 0; + /* input bit accumulator */ + + this.bits = 0; + /* number of bits in "in" */ + + /* for string and stored block copying */ + + this.length = 0; + /* literal or length of data to copy */ + + this.offset = 0; + /* distance back to copy string from */ + + /* for table and code decoding */ + + this.extra = 0; + /* extra bits needed */ + + /* fixed and dynamic code tables */ + + this.lencode = null; + /* starting table for length/literal codes */ + + this.distcode = null; + /* starting table for distance codes */ + + this.lenbits = 0; + /* index bits for lencode */ + + this.distbits = 0; + /* index bits for distcode */ + + /* dynamic table building */ + + this.ncode = 0; + /* number of code length code lengths */ + + this.nlen = 0; + /* number of length code lengths */ + + this.ndist = 0; + /* number of distance code lengths */ + + this.have = 0; + /* number of code lengths in lens[] */ + + this.next = null; + /* next available space in codes[] */ + + this.lens = new Uint16Array(320); + /* temporary storage for code lengths */ + + this.work = new Uint16Array(288); + /* work area for code table building */ + + /* + because we don't have pointers in js, we use lencode and distcode directly + as buffers so we don't need codes + */ + //this.codes = new Int32Array(ENOUGH); /* space for code tables */ + + this.lendyn = null; + /* dynamic table for length/literal codes (JS specific) */ + + this.distdyn = null; + /* dynamic table for distance codes (JS specific) */ + + this.sane = 0; + /* if false, allow invalid distance too far */ + + this.back = 0; + /* bits back of last unprocessed length/lit */ + + this.was = 0; + /* initial length of match */ + } + + var inflateResetKeep = function inflateResetKeep(strm) { + if (!strm || !strm.state) { + return Z_STREAM_ERROR$1; + } + + var state = strm.state; + strm.total_in = strm.total_out = state.total = 0; + strm.msg = ''; + /*Z_NULL*/ + + if (state.wrap) { + /* to support ill-conceived Java test suite */ + strm.adler = state.wrap & 1; + } + + state.mode = HEAD; + state.last = 0; + state.havedict = 0; + state.dmax = 32768; + state.head = null + /*Z_NULL*/ + ; + state.hold = 0; + state.bits = 0; //state.lencode = state.distcode = state.next = state.codes; + + state.lencode = state.lendyn = new Int32Array(ENOUGH_LENS); + state.distcode = state.distdyn = new Int32Array(ENOUGH_DISTS); + state.sane = 1; + state.back = -1; //Tracev((stderr, "inflate: reset\n")); + + return Z_OK$1; + }; + + var inflateReset = function inflateReset(strm) { + if (!strm || !strm.state) { + return Z_STREAM_ERROR$1; + } + + var state = strm.state; + state.wsize = 0; + state.whave = 0; + state.wnext = 0; + return inflateResetKeep(strm); + }; + + var inflateReset2 = function inflateReset2(strm, windowBits) { + var wrap; + /* get the state */ + + if (!strm || !strm.state) { + return Z_STREAM_ERROR$1; + } + + var state = strm.state; + /* extract wrap request from windowBits parameter */ + + if (windowBits < 0) { + wrap = 0; + windowBits = -windowBits; + } else { + wrap = (windowBits >> 4) + 1; + + if (windowBits < 48) { + windowBits &= 15; + } + } + /* set number of window bits, free window if different */ + + + if (windowBits && (windowBits < 8 || windowBits > 15)) { + return Z_STREAM_ERROR$1; + } + + if (state.window !== null && state.wbits !== windowBits) { + state.window = null; + } + /* update state and reset the rest of it */ + + + state.wrap = wrap; + state.wbits = windowBits; + return inflateReset(strm); + }; + + var inflateInit2 = function inflateInit2(strm, windowBits) { + if (!strm) { + return Z_STREAM_ERROR$1; + } //strm.msg = Z_NULL; /* in case we return an error */ + + + var state = new InflateState(); //if (state === Z_NULL) return Z_MEM_ERROR; + //Tracev((stderr, "inflate: allocated\n")); + + strm.state = state; + state.window = null + /*Z_NULL*/ + ; + var ret = inflateReset2(strm, windowBits); + + if (ret !== Z_OK$1) { + strm.state = null + /*Z_NULL*/ + ; + } + + return ret; + }; + + var inflateInit = function inflateInit(strm) { + return inflateInit2(strm, DEF_WBITS); + }; + /* + Return state with length and distance decoding tables and index sizes set to + fixed code decoding. Normally this returns fixed tables from inffixed.h. + If BUILDFIXED is defined, then instead this routine builds the tables the + first time it's called, and returns those tables the first time and + thereafter. This reduces the size of the code by about 2K bytes, in + exchange for a little execution time. However, BUILDFIXED should not be + used for threaded applications, since the rewriting of the tables and virgin + may not be thread-safe. + */ + + + var virgin = true; + var lenfix, distfix; // We have no pointers in JS, so keep tables separate + + var fixedtables = function fixedtables(state) { + /* build fixed huffman tables if first call (may not be thread safe) */ + if (virgin) { + lenfix = new Int32Array(512); + distfix = new Int32Array(32); + /* literal/length table */ + + var sym = 0; + + while (sym < 144) { + state.lens[sym++] = 8; + } + + while (sym < 256) { + state.lens[sym++] = 9; + } + + while (sym < 280) { + state.lens[sym++] = 7; + } + + while (sym < 288) { + state.lens[sym++] = 8; + } + + inftrees(LENS, state.lens, 0, 288, lenfix, 0, state.work, { + bits: 9 + }); + /* distance table */ + + sym = 0; + + while (sym < 32) { + state.lens[sym++] = 5; + } + + inftrees(DISTS, state.lens, 0, 32, distfix, 0, state.work, { + bits: 5 + }); + /* do this just once */ + + virgin = false; + } + + state.lencode = lenfix; + state.lenbits = 9; + state.distcode = distfix; + state.distbits = 5; + }; + /* + Update the window with the last wsize (normally 32K) bytes written before + returning. If window does not exist yet, create it. This is only called + when a window is already in use, or when output has been written during this + inflate call, but the end of the deflate stream has not been reached yet. + It is also called to create a window for dictionary data when a dictionary + is loaded. + + Providing output buffers larger than 32K to inflate() should provide a speed + advantage, since only the last 32K of output is copied to the sliding window + upon return from inflate(), and since all distances after the first 32K of + output will fall in the output data, making match copies simpler and faster. + The advantage may be dependent on the size of the processor's data caches. + */ + + + var updatewindow = function updatewindow(strm, src, end, copy) { + var dist; + var state = strm.state; + /* if it hasn't been done already, allocate space for the window */ + + if (state.window === null) { + state.wsize = 1 << state.wbits; + state.wnext = 0; + state.whave = 0; + state.window = new Uint8Array(state.wsize); + } + /* copy state->wsize or less output bytes into the circular window */ + + + if (copy >= state.wsize) { + state.window.set(src.subarray(end - state.wsize, end), 0); + state.wnext = 0; + state.whave = state.wsize; + } else { + dist = state.wsize - state.wnext; + + if (dist > copy) { + dist = copy; + } //zmemcpy(state->window + state->wnext, end - copy, dist); + + + state.window.set(src.subarray(end - copy, end - copy + dist), state.wnext); + copy -= dist; + + if (copy) { + //zmemcpy(state->window, end - copy, copy); + state.window.set(src.subarray(end - copy, end), 0); + state.wnext = copy; + state.whave = state.wsize; + } else { + state.wnext += dist; + + if (state.wnext === state.wsize) { + state.wnext = 0; + } + + if (state.whave < state.wsize) { + state.whave += dist; + } + } + } + + return 0; + }; + + var inflate$2 = function inflate(strm, flush) { + var state; + var input, output; // input/output buffers + + var next; + /* next input INDEX */ + + var put; + /* next output INDEX */ + + var have, left; + /* available input and output */ + + var hold; + /* bit buffer */ + + var bits; + /* bits in bit buffer */ + + var _in, _out; + /* save starting available input and output */ + + + var copy; + /* number of stored or match bytes to copy */ + + var from; + /* where to copy match bytes from */ + + var from_source; + var here = 0; + /* current decoding table entry */ + + var here_bits, here_op, here_val; // paked "here" denormalized (JS specific) + //let last; /* parent table entry */ + + var last_bits, last_op, last_val; // paked "last" denormalized (JS specific) + + var len; + /* length to copy for repeats, bits to drop */ + + var ret; + /* return code */ + + var hbuf = new Uint8Array(4); + /* buffer for gzip header crc calculation */ + + var opts; + var n; // temporary variable for NEED_BITS + + var order = + /* permutation of code lengths */ + new Uint8Array([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]); + + if (!strm || !strm.state || !strm.output || !strm.input && strm.avail_in !== 0) { + return Z_STREAM_ERROR$1; + } + + state = strm.state; + + if (state.mode === TYPE) { + state.mode = TYPEDO; + } + /* skip check */ + //--- LOAD() --- + + + put = strm.next_out; + output = strm.output; + left = strm.avail_out; + next = strm.next_in; + input = strm.input; + have = strm.avail_in; + hold = state.hold; + bits = state.bits; //--- + + _in = have; + _out = left; + ret = Z_OK$1; + + inf_leave: // goto emulation + for (;;) { + switch (state.mode) { + case HEAD: + if (state.wrap === 0) { + state.mode = TYPEDO; + break; + } //=== NEEDBITS(16); + + + while (bits < 16) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + if (state.wrap & 2 && hold === 0x8b1f) { + /* gzip header */ + state.check = 0 + /*crc32(0L, Z_NULL, 0)*/ + ; //=== CRC2(state.check, hold); + + hbuf[0] = hold & 0xff; + hbuf[1] = hold >>> 8 & 0xff; + state.check = crc32_1(state.check, hbuf, 2, 0); //===// + //=== INITBITS(); + + hold = 0; + bits = 0; //===// + + state.mode = FLAGS; + break; + } + + state.flags = 0; + /* expect zlib header */ + + if (state.head) { + state.head.done = false; + } + + if (!(state.wrap & 1) || + /* check if zlib header allowed */ + (((hold & 0xff) << 8) + (hold >> 8)) % 31) { + strm.msg = 'incorrect header check'; + state.mode = BAD; + break; + } + + if ((hold & 0x0f) !== Z_DEFLATED) { + strm.msg = 'unknown compression method'; + state.mode = BAD; + break; + } //--- DROPBITS(4) ---// + + + hold >>>= 4; + bits -= 4; //---// + + len = (hold & 0x0f) + 8; + + if (state.wbits === 0) { + state.wbits = len; + } else if (len > state.wbits) { + strm.msg = 'invalid window size'; + state.mode = BAD; + break; + } // !!! pako patch. Force use `options.windowBits` if passed. + // Required to always use max window size by default. + + + state.dmax = 1 << state.wbits; //state.dmax = 1 << len; + //Tracev((stderr, "inflate: zlib header ok\n")); + + strm.adler = state.check = 1 + /*adler32(0L, Z_NULL, 0)*/ + ; + state.mode = hold & 0x200 ? DICTID : TYPE; //=== INITBITS(); + + hold = 0; + bits = 0; //===// + + break; + + case FLAGS: + //=== NEEDBITS(16); */ + while (bits < 16) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + state.flags = hold; + + if ((state.flags & 0xff) !== Z_DEFLATED) { + strm.msg = 'unknown compression method'; + state.mode = BAD; + break; + } + + if (state.flags & 0xe000) { + strm.msg = 'unknown header flags set'; + state.mode = BAD; + break; + } + + if (state.head) { + state.head.text = hold >> 8 & 1; + } + + if (state.flags & 0x0200) { + //=== CRC2(state.check, hold); + hbuf[0] = hold & 0xff; + hbuf[1] = hold >>> 8 & 0xff; + state.check = crc32_1(state.check, hbuf, 2, 0); //===// + } //=== INITBITS(); + + + hold = 0; + bits = 0; //===// + + state.mode = TIME; + + /* falls through */ + + case TIME: + //=== NEEDBITS(32); */ + while (bits < 32) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + if (state.head) { + state.head.time = hold; + } + + if (state.flags & 0x0200) { + //=== CRC4(state.check, hold) + hbuf[0] = hold & 0xff; + hbuf[1] = hold >>> 8 & 0xff; + hbuf[2] = hold >>> 16 & 0xff; + hbuf[3] = hold >>> 24 & 0xff; + state.check = crc32_1(state.check, hbuf, 4, 0); //=== + } //=== INITBITS(); + + + hold = 0; + bits = 0; //===// + + state.mode = OS; + + /* falls through */ + + case OS: + //=== NEEDBITS(16); */ + while (bits < 16) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + if (state.head) { + state.head.xflags = hold & 0xff; + state.head.os = hold >> 8; + } + + if (state.flags & 0x0200) { + //=== CRC2(state.check, hold); + hbuf[0] = hold & 0xff; + hbuf[1] = hold >>> 8 & 0xff; + state.check = crc32_1(state.check, hbuf, 2, 0); //===// + } //=== INITBITS(); + + + hold = 0; + bits = 0; //===// + + state.mode = EXLEN; + + /* falls through */ + + case EXLEN: + if (state.flags & 0x0400) { + //=== NEEDBITS(16); */ + while (bits < 16) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + state.length = hold; + + if (state.head) { + state.head.extra_len = hold; + } + + if (state.flags & 0x0200) { + //=== CRC2(state.check, hold); + hbuf[0] = hold & 0xff; + hbuf[1] = hold >>> 8 & 0xff; + state.check = crc32_1(state.check, hbuf, 2, 0); //===// + } //=== INITBITS(); + + + hold = 0; + bits = 0; //===// + } else if (state.head) { + state.head.extra = null + /*Z_NULL*/ + ; + } + + state.mode = EXTRA; + + /* falls through */ + + case EXTRA: + if (state.flags & 0x0400) { + copy = state.length; + + if (copy > have) { + copy = have; + } + + if (copy) { + if (state.head) { + len = state.head.extra_len - state.length; + + if (!state.head.extra) { + // Use untyped array for more convenient processing later + state.head.extra = new Uint8Array(state.head.extra_len); + } + + state.head.extra.set(input.subarray(next, // extra field is limited to 65536 bytes + // - no need for additional size check + next + copy), + /*len + copy > state.head.extra_max - len ? state.head.extra_max : copy,*/ + len); //zmemcpy(state.head.extra + len, next, + // len + copy > state.head.extra_max ? + // state.head.extra_max - len : copy); + } + + if (state.flags & 0x0200) { + state.check = crc32_1(state.check, input, copy, next); + } + + have -= copy; + next += copy; + state.length -= copy; + } + + if (state.length) { + break inf_leave; + } + } + + state.length = 0; + state.mode = NAME; + + /* falls through */ + + case NAME: + if (state.flags & 0x0800) { + if (have === 0) { + break inf_leave; + } + + copy = 0; + + do { + // TODO: 2 or 1 bytes? + len = input[next + copy++]; + /* use constant limit because in js we should not preallocate memory */ + + if (state.head && len && state.length < 65536 + /*state.head.name_max*/ + ) { + state.head.name += String.fromCharCode(len); + } + } while (len && copy < have); + + if (state.flags & 0x0200) { + state.check = crc32_1(state.check, input, copy, next); + } + + have -= copy; + next += copy; + + if (len) { + break inf_leave; + } + } else if (state.head) { + state.head.name = null; + } + + state.length = 0; + state.mode = COMMENT; + + /* falls through */ + + case COMMENT: + if (state.flags & 0x1000) { + if (have === 0) { + break inf_leave; + } + + copy = 0; + + do { + len = input[next + copy++]; + /* use constant limit because in js we should not preallocate memory */ + + if (state.head && len && state.length < 65536 + /*state.head.comm_max*/ + ) { + state.head.comment += String.fromCharCode(len); + } + } while (len && copy < have); + + if (state.flags & 0x0200) { + state.check = crc32_1(state.check, input, copy, next); + } + + have -= copy; + next += copy; + + if (len) { + break inf_leave; + } + } else if (state.head) { + state.head.comment = null; + } + + state.mode = HCRC; + + /* falls through */ + + case HCRC: + if (state.flags & 0x0200) { + //=== NEEDBITS(16); */ + while (bits < 16) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + if (hold !== (state.check & 0xffff)) { + strm.msg = 'header crc mismatch'; + state.mode = BAD; + break; + } //=== INITBITS(); + + + hold = 0; + bits = 0; //===// + } + + if (state.head) { + state.head.hcrc = state.flags >> 9 & 1; + state.head.done = true; + } + + strm.adler = state.check = 0; + state.mode = TYPE; + break; + + case DICTID: + //=== NEEDBITS(32); */ + while (bits < 32) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + strm.adler = state.check = zswap32(hold); //=== INITBITS(); + + hold = 0; + bits = 0; //===// + + state.mode = DICT; + + /* falls through */ + + case DICT: + if (state.havedict === 0) { + //--- RESTORE() --- + strm.next_out = put; + strm.avail_out = left; + strm.next_in = next; + strm.avail_in = have; + state.hold = hold; + state.bits = bits; //--- + + return Z_NEED_DICT$1; + } + + strm.adler = state.check = 1 + /*adler32(0L, Z_NULL, 0)*/ + ; + state.mode = TYPE; + + /* falls through */ + + case TYPE: + if (flush === Z_BLOCK || flush === Z_TREES) { + break inf_leave; + } + + /* falls through */ + + case TYPEDO: + if (state.last) { + //--- BYTEBITS() ---// + hold >>>= bits & 7; + bits -= bits & 7; //---// + + state.mode = CHECK; + break; + } //=== NEEDBITS(3); */ + + + while (bits < 3) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + state.last = hold & 0x01 + /*BITS(1)*/ + ; //--- DROPBITS(1) ---// + + hold >>>= 1; + bits -= 1; //---// + + switch (hold & 0x03) { + case 0: + /* stored block */ + //Tracev((stderr, "inflate: stored block%s\n", + // state.last ? " (last)" : "")); + state.mode = STORED; + break; + + case 1: + /* fixed block */ + fixedtables(state); //Tracev((stderr, "inflate: fixed codes block%s\n", + // state.last ? " (last)" : "")); + + state.mode = LEN_; + /* decode codes */ + + if (flush === Z_TREES) { + //--- DROPBITS(2) ---// + hold >>>= 2; + bits -= 2; //---// + + break inf_leave; + } + + break; + + case 2: + /* dynamic block */ + //Tracev((stderr, "inflate: dynamic codes block%s\n", + // state.last ? " (last)" : "")); + state.mode = TABLE; + break; + + case 3: + strm.msg = 'invalid block type'; + state.mode = BAD; + } //--- DROPBITS(2) ---// + + + hold >>>= 2; + bits -= 2; //---// + + break; + + case STORED: + //--- BYTEBITS() ---// /* go to byte boundary */ + hold >>>= bits & 7; + bits -= bits & 7; //---// + //=== NEEDBITS(32); */ + + while (bits < 32) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + if ((hold & 0xffff) !== (hold >>> 16 ^ 0xffff)) { + strm.msg = 'invalid stored block lengths'; + state.mode = BAD; + break; + } + + state.length = hold & 0xffff; //Tracev((stderr, "inflate: stored length %u\n", + // state.length)); + //=== INITBITS(); + + hold = 0; + bits = 0; //===// + + state.mode = COPY_; + + if (flush === Z_TREES) { + break inf_leave; + } + + /* falls through */ + + case COPY_: + state.mode = COPY; + + /* falls through */ + + case COPY: + copy = state.length; + + if (copy) { + if (copy > have) { + copy = have; + } + + if (copy > left) { + copy = left; + } + + if (copy === 0) { + break inf_leave; + } //--- zmemcpy(put, next, copy); --- + + + output.set(input.subarray(next, next + copy), put); //---// + + have -= copy; + next += copy; + left -= copy; + put += copy; + state.length -= copy; + break; + } //Tracev((stderr, "inflate: stored end\n")); + + + state.mode = TYPE; + break; + + case TABLE: + //=== NEEDBITS(14); */ + while (bits < 14) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + state.nlen = (hold & 0x1f) + 257; //--- DROPBITS(5) ---// + + hold >>>= 5; + bits -= 5; //---// + + state.ndist = (hold & 0x1f) + 1; //--- DROPBITS(5) ---// + + hold >>>= 5; + bits -= 5; //---// + + state.ncode = (hold & 0x0f) + 4; //--- DROPBITS(4) ---// + + hold >>>= 4; + bits -= 4; //---// + //#ifndef PKZIP_BUG_WORKAROUND + + if (state.nlen > 286 || state.ndist > 30) { + strm.msg = 'too many length or distance symbols'; + state.mode = BAD; + break; + } //#endif + //Tracev((stderr, "inflate: table sizes ok\n")); + + + state.have = 0; + state.mode = LENLENS; + + /* falls through */ + + case LENLENS: + while (state.have < state.ncode) { + //=== NEEDBITS(3); + while (bits < 3) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + state.lens[order[state.have++]] = hold & 0x07; //BITS(3); + //--- DROPBITS(3) ---// + + hold >>>= 3; + bits -= 3; //---// + } + + while (state.have < 19) { + state.lens[order[state.have++]] = 0; + } // We have separate tables & no pointers. 2 commented lines below not needed. + //state.next = state.codes; + //state.lencode = state.next; + // Switch to use dynamic table + + + state.lencode = state.lendyn; + state.lenbits = 7; + opts = { + bits: state.lenbits + }; + ret = inftrees(CODES, state.lens, 0, 19, state.lencode, 0, state.work, opts); + state.lenbits = opts.bits; + + if (ret) { + strm.msg = 'invalid code lengths set'; + state.mode = BAD; + break; + } //Tracev((stderr, "inflate: code lengths ok\n")); + + + state.have = 0; + state.mode = CODELENS; + + /* falls through */ + + case CODELENS: + while (state.have < state.nlen + state.ndist) { + for (;;) { + here = state.lencode[hold & (1 << state.lenbits) - 1]; + /*BITS(state.lenbits)*/ + + here_bits = here >>> 24; + here_op = here >>> 16 & 0xff; + here_val = here & 0xffff; + + if (here_bits <= bits) { + break; + } //--- PULLBYTE() ---// + + + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; //---// + } + + if (here_val < 16) { + //--- DROPBITS(here.bits) ---// + hold >>>= here_bits; + bits -= here_bits; //---// + + state.lens[state.have++] = here_val; + } else { + if (here_val === 16) { + //=== NEEDBITS(here.bits + 2); + n = here_bits + 2; + + while (bits < n) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + //--- DROPBITS(here.bits) ---// + + + hold >>>= here_bits; + bits -= here_bits; //---// + + if (state.have === 0) { + strm.msg = 'invalid bit length repeat'; + state.mode = BAD; + break; + } + + len = state.lens[state.have - 1]; + copy = 3 + (hold & 0x03); //BITS(2); + //--- DROPBITS(2) ---// + + hold >>>= 2; + bits -= 2; //---// + } else if (here_val === 17) { + //=== NEEDBITS(here.bits + 3); + n = here_bits + 3; + + while (bits < n) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + //--- DROPBITS(here.bits) ---// + + + hold >>>= here_bits; + bits -= here_bits; //---// + + len = 0; + copy = 3 + (hold & 0x07); //BITS(3); + //--- DROPBITS(3) ---// + + hold >>>= 3; + bits -= 3; //---// + } else { + //=== NEEDBITS(here.bits + 7); + n = here_bits + 7; + + while (bits < n) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + //--- DROPBITS(here.bits) ---// + + + hold >>>= here_bits; + bits -= here_bits; //---// + + len = 0; + copy = 11 + (hold & 0x7f); //BITS(7); + //--- DROPBITS(7) ---// + + hold >>>= 7; + bits -= 7; //---// + } + + if (state.have + copy > state.nlen + state.ndist) { + strm.msg = 'invalid bit length repeat'; + state.mode = BAD; + break; + } + + while (copy--) { + state.lens[state.have++] = len; + } + } + } + /* handle error breaks in while */ + + + if (state.mode === BAD) { + break; + } + /* check for end-of-block code (better have one) */ + + + if (state.lens[256] === 0) { + strm.msg = 'invalid code -- missing end-of-block'; + state.mode = BAD; + break; + } + /* build code tables -- note: do not change the lenbits or distbits + values here (9 and 6) without reading the comments in inftrees.h + concerning the ENOUGH constants, which depend on those values */ + + + state.lenbits = 9; + opts = { + bits: state.lenbits + }; + ret = inftrees(LENS, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts); // We have separate tables & no pointers. 2 commented lines below not needed. + // state.next_index = opts.table_index; + + state.lenbits = opts.bits; // state.lencode = state.next; + + if (ret) { + strm.msg = 'invalid literal/lengths set'; + state.mode = BAD; + break; + } + + state.distbits = 6; //state.distcode.copy(state.codes); + // Switch to use dynamic table + + state.distcode = state.distdyn; + opts = { + bits: state.distbits + }; + ret = inftrees(DISTS, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts); // We have separate tables & no pointers. 2 commented lines below not needed. + // state.next_index = opts.table_index; + + state.distbits = opts.bits; // state.distcode = state.next; + + if (ret) { + strm.msg = 'invalid distances set'; + state.mode = BAD; + break; + } //Tracev((stderr, 'inflate: codes ok\n')); + + + state.mode = LEN_; + + if (flush === Z_TREES) { + break inf_leave; + } + + /* falls through */ + + case LEN_: + state.mode = LEN; + + /* falls through */ + + case LEN: + if (have >= 6 && left >= 258) { + //--- RESTORE() --- + strm.next_out = put; + strm.avail_out = left; + strm.next_in = next; + strm.avail_in = have; + state.hold = hold; + state.bits = bits; //--- + + inffast(strm, _out); //--- LOAD() --- + + put = strm.next_out; + output = strm.output; + left = strm.avail_out; + next = strm.next_in; + input = strm.input; + have = strm.avail_in; + hold = state.hold; + bits = state.bits; //--- + + if (state.mode === TYPE) { + state.back = -1; + } + + break; + } + + state.back = 0; + + for (;;) { + here = state.lencode[hold & (1 << state.lenbits) - 1]; + /*BITS(state.lenbits)*/ + + here_bits = here >>> 24; + here_op = here >>> 16 & 0xff; + here_val = here & 0xffff; + + if (here_bits <= bits) { + break; + } //--- PULLBYTE() ---// + + + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; //---// + } + + if (here_op && (here_op & 0xf0) === 0) { + last_bits = here_bits; + last_op = here_op; + last_val = here_val; + + for (;;) { + here = state.lencode[last_val + ((hold & (1 << last_bits + last_op) - 1) >> last_bits)]; + here_bits = here >>> 24; + here_op = here >>> 16 & 0xff; + here_val = here & 0xffff; + + if (last_bits + here_bits <= bits) { + break; + } //--- PULLBYTE() ---// + + + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; //---// + } //--- DROPBITS(last.bits) ---// + + + hold >>>= last_bits; + bits -= last_bits; //---// + + state.back += last_bits; + } //--- DROPBITS(here.bits) ---// + + + hold >>>= here_bits; + bits -= here_bits; //---// + + state.back += here_bits; + state.length = here_val; + + if (here_op === 0) { + //Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + // "inflate: literal '%c'\n" : + // "inflate: literal 0x%02x\n", here.val)); + state.mode = LIT; + break; + } + + if (here_op & 32) { + //Tracevv((stderr, "inflate: end of block\n")); + state.back = -1; + state.mode = TYPE; + break; + } + + if (here_op & 64) { + strm.msg = 'invalid literal/length code'; + state.mode = BAD; + break; + } + + state.extra = here_op & 15; + state.mode = LENEXT; + + /* falls through */ + + case LENEXT: + if (state.extra) { + //=== NEEDBITS(state.extra); + n = state.extra; + + while (bits < n) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + state.length += hold & (1 << state.extra) - 1 + /*BITS(state.extra)*/ + ; //--- DROPBITS(state.extra) ---// + + hold >>>= state.extra; + bits -= state.extra; //---// + + state.back += state.extra; + } //Tracevv((stderr, "inflate: length %u\n", state.length)); + + + state.was = state.length; + state.mode = DIST; + + /* falls through */ + + case DIST: + for (;;) { + here = state.distcode[hold & (1 << state.distbits) - 1]; + /*BITS(state.distbits)*/ + + here_bits = here >>> 24; + here_op = here >>> 16 & 0xff; + here_val = here & 0xffff; + + if (here_bits <= bits) { + break; + } //--- PULLBYTE() ---// + + + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; //---// + } + + if ((here_op & 0xf0) === 0) { + last_bits = here_bits; + last_op = here_op; + last_val = here_val; + + for (;;) { + here = state.distcode[last_val + ((hold & (1 << last_bits + last_op) - 1) >> last_bits)]; + here_bits = here >>> 24; + here_op = here >>> 16 & 0xff; + here_val = here & 0xffff; + + if (last_bits + here_bits <= bits) { + break; + } //--- PULLBYTE() ---// + + + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; //---// + } //--- DROPBITS(last.bits) ---// + + + hold >>>= last_bits; + bits -= last_bits; //---// + + state.back += last_bits; + } //--- DROPBITS(here.bits) ---// + + + hold >>>= here_bits; + bits -= here_bits; //---// + + state.back += here_bits; + + if (here_op & 64) { + strm.msg = 'invalid distance code'; + state.mode = BAD; + break; + } + + state.offset = here_val; + state.extra = here_op & 15; + state.mode = DISTEXT; + + /* falls through */ + + case DISTEXT: + if (state.extra) { + //=== NEEDBITS(state.extra); + n = state.extra; + + while (bits < n) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + state.offset += hold & (1 << state.extra) - 1 + /*BITS(state.extra)*/ + ; //--- DROPBITS(state.extra) ---// + + hold >>>= state.extra; + bits -= state.extra; //---// + + state.back += state.extra; + } //#ifdef INFLATE_STRICT + + + if (state.offset > state.dmax) { + strm.msg = 'invalid distance too far back'; + state.mode = BAD; + break; + } //#endif + //Tracevv((stderr, "inflate: distance %u\n", state.offset)); + + + state.mode = MATCH; + + /* falls through */ + + case MATCH: + if (left === 0) { + break inf_leave; + } + + copy = _out - left; + + if (state.offset > copy) { + /* copy from window */ + copy = state.offset - copy; + + if (copy > state.whave) { + if (state.sane) { + strm.msg = 'invalid distance too far back'; + state.mode = BAD; + break; + } // (!) This block is disabled in zlib defaults, + // don't enable it for binary compatibility + //#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR + // Trace((stderr, "inflate.c too far\n")); + // copy -= state.whave; + // if (copy > state.length) { copy = state.length; } + // if (copy > left) { copy = left; } + // left -= copy; + // state.length -= copy; + // do { + // output[put++] = 0; + // } while (--copy); + // if (state.length === 0) { state.mode = LEN; } + // break; + //#endif + + } + + if (copy > state.wnext) { + copy -= state.wnext; + from = state.wsize - copy; + } else { + from = state.wnext - copy; + } + + if (copy > state.length) { + copy = state.length; + } + + from_source = state.window; + } else { + /* copy from output */ + from_source = output; + from = put - state.offset; + copy = state.length; + } + + if (copy > left) { + copy = left; + } + + left -= copy; + state.length -= copy; + + do { + output[put++] = from_source[from++]; + } while (--copy); + + if (state.length === 0) { + state.mode = LEN; + } + + break; + + case LIT: + if (left === 0) { + break inf_leave; + } + + output[put++] = state.length; + left--; + state.mode = LEN; + break; + + case CHECK: + if (state.wrap) { + //=== NEEDBITS(32); + while (bits < 32) { + if (have === 0) { + break inf_leave; + } + + have--; // Use '|' instead of '+' to make sure that result is signed + + hold |= input[next++] << bits; + bits += 8; + } //===// + + + _out -= left; + strm.total_out += _out; + state.total += _out; + + if (_out) { + strm.adler = state.check = state.flags ? crc32_1(state.check, output, _out, put - _out) : adler32_1(state.check, output, _out, put - _out); + } + + _out = left; // NB: crc32 stored as signed 32-bit int, zswap32 returns signed too + + if ((state.flags ? hold : zswap32(hold)) !== state.check) { + strm.msg = 'incorrect data check'; + state.mode = BAD; + break; + } //=== INITBITS(); + + + hold = 0; + bits = 0; //===// + //Tracev((stderr, "inflate: check matches trailer\n")); + } + + state.mode = LENGTH; + + /* falls through */ + + case LENGTH: + if (state.wrap && state.flags) { + //=== NEEDBITS(32); + while (bits < 32) { + if (have === 0) { + break inf_leave; + } + + have--; + hold += input[next++] << bits; + bits += 8; + } //===// + + + if (hold !== (state.total & 0xffffffff)) { + strm.msg = 'incorrect length check'; + state.mode = BAD; + break; + } //=== INITBITS(); + + + hold = 0; + bits = 0; //===// + //Tracev((stderr, "inflate: length matches trailer\n")); + } + + state.mode = DONE; + + /* falls through */ + + case DONE: + ret = Z_STREAM_END$1; + break inf_leave; + + case BAD: + ret = Z_DATA_ERROR$1; + break inf_leave; + + case MEM: + return Z_MEM_ERROR$1; + + case SYNC: + /* falls through */ + + default: + return Z_STREAM_ERROR$1; + } + } // inf_leave <- here is real place for "goto inf_leave", emulated via "break inf_leave" + + /* + Return from inflate(), updating the total counts and the check value. + If there was no progress during the inflate() call, return a buffer + error. Call updatewindow() to create and/or update the window state. + Note: a memory error from inflate() is non-recoverable. + */ + //--- RESTORE() --- + + + strm.next_out = put; + strm.avail_out = left; + strm.next_in = next; + strm.avail_in = have; + state.hold = hold; + state.bits = bits; //--- + + if (state.wsize || _out !== strm.avail_out && state.mode < BAD && (state.mode < CHECK || flush !== Z_FINISH$1)) { + if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) ; + } + + _in -= strm.avail_in; + _out -= strm.avail_out; + strm.total_in += _in; + strm.total_out += _out; + state.total += _out; + + if (state.wrap && _out) { + strm.adler = state.check = state.flags ? crc32_1(state.check, output, _out, strm.next_out - _out) : adler32_1(state.check, output, _out, strm.next_out - _out); + } + + strm.data_type = state.bits + (state.last ? 64 : 0) + (state.mode === TYPE ? 128 : 0) + (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0); + + if ((_in === 0 && _out === 0 || flush === Z_FINISH$1) && ret === Z_OK$1) { + ret = Z_BUF_ERROR; + } + + return ret; + }; + + var inflateEnd = function inflateEnd(strm) { + if (!strm || !strm.state + /*|| strm->zfree == (free_func)0*/ + ) { + return Z_STREAM_ERROR$1; + } + + var state = strm.state; + + if (state.window) { + state.window = null; + } + + strm.state = null; + return Z_OK$1; + }; + + var inflateGetHeader = function inflateGetHeader(strm, head) { + /* check state */ + if (!strm || !strm.state) { + return Z_STREAM_ERROR$1; + } + + var state = strm.state; + + if ((state.wrap & 2) === 0) { + return Z_STREAM_ERROR$1; + } + /* save header structure */ + + + state.head = head; + head.done = false; + return Z_OK$1; + }; + + var inflateSetDictionary = function inflateSetDictionary(strm, dictionary) { + var dictLength = dictionary.length; + var state; + var dictid; + var ret; + /* check state */ + + if (!strm + /* == Z_NULL */ + || !strm.state + /* == Z_NULL */ + ) { + return Z_STREAM_ERROR$1; + } + + state = strm.state; + + if (state.wrap !== 0 && state.mode !== DICT) { + return Z_STREAM_ERROR$1; + } + /* check for correct dictionary identifier */ + + + if (state.mode === DICT) { + dictid = 1; + /* adler32(0, null, 0)*/ + + /* dictid = adler32(dictid, dictionary, dictLength); */ + + dictid = adler32_1(dictid, dictionary, dictLength, 0); + + if (dictid !== state.check) { + return Z_DATA_ERROR$1; + } + } + /* copy dictionary to window using updatewindow(), which will amend the + existing dictionary if appropriate */ + + + ret = updatewindow(strm, dictionary, dictLength, dictLength); + + if (ret) { + state.mode = MEM; + return Z_MEM_ERROR$1; + } + + state.havedict = 1; // Tracev((stderr, "inflate: dictionary set\n")); + + return Z_OK$1; + }; + + var inflateReset_1 = inflateReset; + var inflateReset2_1 = inflateReset2; + var inflateResetKeep_1 = inflateResetKeep; + var inflateInit_1 = inflateInit; + var inflateInit2_1 = inflateInit2; + var inflate_2$1 = inflate$2; + var inflateEnd_1 = inflateEnd; + var inflateGetHeader_1 = inflateGetHeader; + var inflateSetDictionary_1 = inflateSetDictionary; + var inflateInfo = 'pako inflate (from Nodeca project)'; + /* Not implemented + module.exports.inflateCopy = inflateCopy; + module.exports.inflateGetDictionary = inflateGetDictionary; + module.exports.inflateMark = inflateMark; + module.exports.inflatePrime = inflatePrime; + module.exports.inflateSync = inflateSync; + module.exports.inflateSyncPoint = inflateSyncPoint; + module.exports.inflateUndermine = inflateUndermine; + */ + + var inflate_1$2 = { + inflateReset: inflateReset_1, + inflateReset2: inflateReset2_1, + inflateResetKeep: inflateResetKeep_1, + inflateInit: inflateInit_1, + inflateInit2: inflateInit2_1, + inflate: inflate_2$1, + inflateEnd: inflateEnd_1, + inflateGetHeader: inflateGetHeader_1, + inflateSetDictionary: inflateSetDictionary_1, + inflateInfo: inflateInfo + }; + + // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin + // + // This software is provided 'as-is', without any express or implied + // warranty. In no event will the authors be held liable for any damages + // arising from the use of this software. + // + // Permission is granted to anyone to use this software for any purpose, + // including commercial applications, and to alter it and redistribute it + // freely, subject to the following restrictions: + // + // 1. The origin of this software must not be misrepresented; you must not + // claim that you wrote the original software. If you use this software + // in a product, an acknowledgment in the product documentation would be + // appreciated but is not required. + // 2. Altered source versions must be plainly marked as such, and must not be + // misrepresented as being the original software. + // 3. This notice may not be removed or altered from any source distribution. + + function GZheader() { + /* true if compressed data believed to be text */ + this.text = 0; + /* modification time */ + + this.time = 0; + /* extra flags (not used when writing a gzip file) */ + + this.xflags = 0; + /* operating system */ + + this.os = 0; + /* pointer to extra field or Z_NULL if none */ + + this.extra = null; + /* extra field length (valid if extra != Z_NULL) */ + + this.extra_len = 0; // Actually, we don't need it in JS, + // but leave for few code modifications + // + // Setup limits is not necessary because in js we should not preallocate memory + // for inflate use constant limit in 65536 bytes + // + + /* space at extra (only when reading header) */ + // this.extra_max = 0; + + /* pointer to zero-terminated file name or Z_NULL */ + + this.name = ''; + /* space at name (only when reading header) */ + // this.name_max = 0; + + /* pointer to zero-terminated comment or Z_NULL */ + + this.comment = ''; + /* space at comment (only when reading header) */ + // this.comm_max = 0; + + /* true if there was or will be a header crc */ + + this.hcrc = 0; + /* true when done reading gzip header (not used when writing a gzip file) */ + + this.done = false; + } + + var gzheader = GZheader; + + var toString = Object.prototype.toString; + /* Public constants ==========================================================*/ + + /* ===========================================================================*/ + + var Z_NO_FLUSH = constants$2.Z_NO_FLUSH, + Z_FINISH = constants$2.Z_FINISH, + Z_OK = constants$2.Z_OK, + Z_STREAM_END = constants$2.Z_STREAM_END, + Z_NEED_DICT = constants$2.Z_NEED_DICT, + Z_STREAM_ERROR = constants$2.Z_STREAM_ERROR, + Z_DATA_ERROR = constants$2.Z_DATA_ERROR, + Z_MEM_ERROR = constants$2.Z_MEM_ERROR; + /* ===========================================================================*/ + + /** + * class Inflate + * + * Generic JS-style wrapper for zlib calls. If you don't need + * streaming behaviour - use more simple functions: [[inflate]] + * and [[inflateRaw]]. + **/ + + /* internal + * inflate.chunks -> Array + * + * Chunks of output data, if [[Inflate#onData]] not overridden. + **/ + + /** + * Inflate.result -> Uint8Array|String + * + * Uncompressed result, generated by default [[Inflate#onData]] + * and [[Inflate#onEnd]] handlers. Filled after you push last chunk + * (call [[Inflate#push]] with `Z_FINISH` / `true` param). + **/ + + /** + * Inflate.err -> Number + * + * Error code after inflate finished. 0 (Z_OK) on success. + * Should be checked if broken data possible. + **/ + + /** + * Inflate.msg -> String + * + * Error message, if [[Inflate.err]] != 0 + **/ + + /** + * new Inflate(options) + * - options (Object): zlib inflate options. + * + * Creates new inflator instance with specified params. Throws exception + * on bad params. Supported options: + * + * - `windowBits` + * - `dictionary` + * + * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced) + * for more information on these. + * + * Additional options, for internal needs: + * + * - `chunkSize` - size of generated data chunks (16K by default) + * - `raw` (Boolean) - do raw inflate + * - `to` (String) - if equal to 'string', then result will be converted + * from utf8 to utf16 (javascript) string. When string output requested, + * chunk length can differ from `chunkSize`, depending on content. + * + * By default, when no options set, autodetect deflate/gzip data format via + * wrapper header. + * + * ##### Example: + * + * ```javascript + * const pako = require('pako') + * const chunk1 = new Uint8Array([1,2,3,4,5,6,7,8,9]) + * const chunk2 = new Uint8Array([10,11,12,13,14,15,16,17,18,19]); + * + * const inflate = new pako.Inflate({ level: 3}); + * + * inflate.push(chunk1, false); + * inflate.push(chunk2, true); // true -> last chunk + * + * if (inflate.err) { throw new Error(inflate.err); } + * + * console.log(inflate.result); + * ``` + **/ + + function Inflate$1(options) { + this.options = common.assign({ + chunkSize: 1024 * 64, + windowBits: 15, + to: '' + }, options || {}); + var opt = this.options; // Force window size for `raw` data, if not set directly, + // because we have no header for autodetect. + + if (opt.raw && opt.windowBits >= 0 && opt.windowBits < 16) { + opt.windowBits = -opt.windowBits; + + if (opt.windowBits === 0) { + opt.windowBits = -15; + } + } // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate + + + if (opt.windowBits >= 0 && opt.windowBits < 16 && !(options && options.windowBits)) { + opt.windowBits += 32; + } // Gzip header has no info about windows size, we can do autodetect only + // for deflate. So, if window size not set, force it to max when gzip possible + + + if (opt.windowBits > 15 && opt.windowBits < 48) { + // bit 3 (16) -> gzipped data + // bit 4 (32) -> autodetect gzip/deflate + if ((opt.windowBits & 15) === 0) { + opt.windowBits |= 15; + } + } + + this.err = 0; // error code, if happens (0 = Z_OK) + + this.msg = ''; // error message + + this.ended = false; // used to avoid multiple onEnd() calls + + this.chunks = []; // chunks of compressed data + + this.strm = new zstream(); + this.strm.avail_out = 0; + var status = inflate_1$2.inflateInit2(this.strm, opt.windowBits); + + if (status !== Z_OK) { + throw new Error(messages[status]); + } + + this.header = new gzheader(); + inflate_1$2.inflateGetHeader(this.strm, this.header); // Setup dictionary + + if (opt.dictionary) { + // Convert data if needed + if (typeof opt.dictionary === 'string') { + opt.dictionary = strings.string2buf(opt.dictionary); + } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') { + opt.dictionary = new Uint8Array(opt.dictionary); + } + + if (opt.raw) { + //In raw mode we need to set the dictionary early + status = inflate_1$2.inflateSetDictionary(this.strm, opt.dictionary); + + if (status !== Z_OK) { + throw new Error(messages[status]); + } + } + } + } + /** + * Inflate#push(data[, flush_mode]) -> Boolean + * - data (Uint8Array|ArrayBuffer): input data + * - flush_mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE + * flush modes. See constants. Skipped or `false` means Z_NO_FLUSH, + * `true` means Z_FINISH. + * + * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with + * new output chunks. Returns `true` on success. If end of stream detected, + * [[Inflate#onEnd]] will be called. + * + * `flush_mode` is not needed for normal operation, because end of stream + * detected automatically. You may try to use it for advanced things, but + * this functionality was not tested. + * + * On fail call [[Inflate#onEnd]] with error code and return false. + * + * ##### Example + * + * ```javascript + * push(chunk, false); // push one of data chunks + * ... + * push(chunk, true); // push last chunk + * ``` + **/ + + + Inflate$1.prototype.push = function (data, flush_mode) { + var strm = this.strm; + var chunkSize = this.options.chunkSize; + var dictionary = this.options.dictionary; + + var status, _flush_mode, last_avail_out; + + if (this.ended) return false; + if (flush_mode === ~~flush_mode) _flush_mode = flush_mode;else _flush_mode = flush_mode === true ? Z_FINISH : Z_NO_FLUSH; // Convert data if needed + + if (toString.call(data) === '[object ArrayBuffer]') { + strm.input = new Uint8Array(data); + } else { + strm.input = data; + } + + strm.next_in = 0; + strm.avail_in = strm.input.length; + + for (;;) { + if (strm.avail_out === 0) { + strm.output = new Uint8Array(chunkSize); + strm.next_out = 0; + strm.avail_out = chunkSize; + } + + status = inflate_1$2.inflate(strm, _flush_mode); + + if (status === Z_NEED_DICT && dictionary) { + status = inflate_1$2.inflateSetDictionary(strm, dictionary); + + if (status === Z_OK) { + status = inflate_1$2.inflate(strm, _flush_mode); + } else if (status === Z_DATA_ERROR) { + // Replace code with more verbose + status = Z_NEED_DICT; + } + } // Skip snyc markers if more data follows and not raw mode + + + while (strm.avail_in > 0 && status === Z_STREAM_END && strm.state.wrap > 0 && data[strm.next_in] !== 0) { + inflate_1$2.inflateReset(strm); + status = inflate_1$2.inflate(strm, _flush_mode); + } + + switch (status) { + case Z_STREAM_ERROR: + case Z_DATA_ERROR: + case Z_NEED_DICT: + case Z_MEM_ERROR: + this.onEnd(status); + this.ended = true; + return false; + } // Remember real `avail_out` value, because we may patch out buffer content + // to align utf8 strings boundaries. + + + last_avail_out = strm.avail_out; + + if (strm.next_out) { + if (strm.avail_out === 0 || status === Z_STREAM_END) { + if (this.options.to === 'string') { + var next_out_utf8 = strings.utf8border(strm.output, strm.next_out); + var tail = strm.next_out - next_out_utf8; + var utf8str = strings.buf2string(strm.output, next_out_utf8); // move tail & realign counters + + strm.next_out = tail; + strm.avail_out = chunkSize - tail; + if (tail) strm.output.set(strm.output.subarray(next_out_utf8, next_out_utf8 + tail), 0); + this.onData(utf8str); + } else { + this.onData(strm.output.length === strm.next_out ? strm.output : strm.output.subarray(0, strm.next_out)); + } + } + } // Must repeat iteration if out buffer is full + + + if (status === Z_OK && last_avail_out === 0) continue; // Finalize if end of stream reached. + + if (status === Z_STREAM_END) { + status = inflate_1$2.inflateEnd(this.strm); + this.onEnd(status); + this.ended = true; + return true; + } + + if (strm.avail_in === 0) break; + } + + return true; + }; + /** + * Inflate#onData(chunk) -> Void + * - chunk (Uint8Array|String): output data. When string output requested, + * each chunk will be string. + * + * By default, stores data blocks in `chunks[]` property and glue + * those in `onEnd`. Override this handler, if you need another behaviour. + **/ + + + Inflate$1.prototype.onData = function (chunk) { + this.chunks.push(chunk); + }; + /** + * Inflate#onEnd(status) -> Void + * - status (Number): inflate status. 0 (Z_OK) on success, + * other if not. + * + * Called either after you tell inflate that the input stream is + * complete (Z_FINISH). By default - join collected chunks, + * free memory and fill `results` / `err` properties. + **/ + + + Inflate$1.prototype.onEnd = function (status) { + // On success - join + if (status === Z_OK) { + if (this.options.to === 'string') { + this.result = this.chunks.join(''); + } else { + this.result = common.flattenChunks(this.chunks); + } + } + + this.chunks = []; + this.err = status; + this.msg = this.strm.msg; + }; + /** + * inflate(data[, options]) -> Uint8Array|String + * - data (Uint8Array): input data to decompress. + * - options (Object): zlib inflate options. + * + * Decompress `data` with inflate/ungzip and `options`. Autodetect + * format via wrapper header by default. That's why we don't provide + * separate `ungzip` method. + * + * Supported options are: + * + * - windowBits + * + * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced) + * for more information. + * + * Sugar (options): + * + * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify + * negative windowBits implicitly. + * - `to` (String) - if equal to 'string', then result will be converted + * from utf8 to utf16 (javascript) string. When string output requested, + * chunk length can differ from `chunkSize`, depending on content. + * + * + * ##### Example: + * + * ```javascript + * const pako = require('pako'); + * const input = pako.deflate(new Uint8Array([1,2,3,4,5,6,7,8,9])); + * let output; + * + * try { + * output = pako.inflate(input); + * } catch (err) { + * console.log(err); + * } + * ``` + **/ + + + function inflate$1(input, options) { + var inflator = new Inflate$1(options); + inflator.push(input); // That will never happens, if you don't cheat with options :) + + if (inflator.err) throw inflator.msg || messages[inflator.err]; + return inflator.result; + } + /** + * inflateRaw(data[, options]) -> Uint8Array|String + * - data (Uint8Array): input data to decompress. + * - options (Object): zlib inflate options. + * + * The same as [[inflate]], but creates raw data, without wrapper + * (header and adler32 crc). + **/ + + + function inflateRaw$1(input, options) { + options = options || {}; + options.raw = true; + return inflate$1(input, options); + } + /** + * ungzip(data[, options]) -> Uint8Array|String + * - data (Uint8Array): input data to decompress. + * - options (Object): zlib inflate options. + * + * Just shortcut to [[inflate]], because it autodetects format + * by header.content. Done for convenience. + **/ + + + var Inflate_1$1 = Inflate$1; + var inflate_2 = inflate$1; + var inflateRaw_1$1 = inflateRaw$1; + var ungzip$1 = inflate$1; + var constants = constants$2; + var inflate_1$1 = { + Inflate: Inflate_1$1, + inflate: inflate_2, + inflateRaw: inflateRaw_1$1, + ungzip: ungzip$1, + constants: constants + }; + + var Deflate = deflate_1$1.Deflate, + deflate = deflate_1$1.deflate, + deflateRaw = deflate_1$1.deflateRaw, + gzip = deflate_1$1.gzip; + var Inflate = inflate_1$1.Inflate, + inflate = inflate_1$1.inflate, + inflateRaw = inflate_1$1.inflateRaw, + ungzip = inflate_1$1.ungzip; + var Deflate_1 = Deflate; + var deflate_1 = deflate; + var deflateRaw_1 = deflateRaw; + var gzip_1 = gzip; + var Inflate_1 = Inflate; + var inflate_1 = inflate; + var inflateRaw_1 = inflateRaw; + var ungzip_1 = ungzip; + var constants_1 = constants$2; + var pako = { + Deflate: Deflate_1, + deflate: deflate_1, + deflateRaw: deflateRaw_1, + gzip: gzip_1, + Inflate: Inflate_1, + inflate: inflate_1, + inflateRaw: inflateRaw_1, + ungzip: ungzip_1, + constants: constants_1 + }; + + exports.Deflate = Deflate_1; + exports.Inflate = Inflate_1; + exports.constants = constants_1; + exports['default'] = pako; + exports.deflate = deflate_1; + exports.deflateRaw = deflateRaw_1; + exports.gzip = gzip_1; + exports.inflate = inflate_1; + exports.inflateRaw = inflateRaw_1; + exports.ungzip = ungzip_1; + + Object.defineProperty(exports, '__esModule', { value: true }); + +}))); \ No newline at end of file diff --git a/libraries/sha1.min.js b/libraries/sha1.min.js new file mode 100644 index 0000000..e20453d --- /dev/null +++ b/libraries/sha1.min.js @@ -0,0 +1,9 @@ +/* + * [js-sha1]{@link https://github.com/emn178/js-sha1} + * + * @version 0.6.0 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2014-2017 + * @license MIT + */ +!function(){"use strict";function t(t){t?(f[0]=f[16]=f[1]=f[2]=f[3]=f[4]=f[5]=f[6]=f[7]=f[8]=f[9]=f[10]=f[11]=f[12]=f[13]=f[14]=f[15]=0,this.blocks=f):this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],this.h0=1732584193,this.h1=4023233417,this.h2=2562383102,this.h3=271733878,this.h4=3285377520,this.block=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}var h="object"==typeof window?window:{},s=!h.JS_SHA1_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;s&&(h=global);var i=!h.JS_SHA1_NO_COMMON_JS&&"object"==typeof module&&module.exports,e="function"==typeof define&&define.amd,r="0123456789abcdef".split(""),o=[-2147483648,8388608,32768,128],n=[24,16,8,0],a=["hex","array","digest","arrayBuffer"],f=[],u=function(h){return function(s){return new t(!0).update(s)[h]()}},c=function(){var h=u("hex");s&&(h=p(h)),h.create=function(){return new t},h.update=function(t){return h.create().update(t)};for(var i=0;i>2]|=t[r]<>2]|=i<>2]|=(192|i>>6)<>2]|=(128|63&i)<=57344?(a[e>>2]|=(224|i>>12)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<>2]|=(240|i>>18)<>2]|=(128|i>>12&63)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<=64?(this.block=a[16],this.start=e-64,this.hash(),this.hashed=!0):this.start=e}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296<<0,this.bytes=this.bytes%4294967296),this}},t.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,h=this.lastByteIndex;t[16]=this.block,t[h>>2]|=o[3&h],this.block=t[16],h>=56&&(this.hashed||this.hash(),t[0]=this.block,t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.hBytes<<3|this.bytes>>>29,t[15]=this.bytes<<3,this.hash()}},t.prototype.hash=function(){var t,h,s=this.h0,i=this.h1,e=this.h2,r=this.h3,o=this.h4,n=this.blocks;for(t=16;t<80;++t)h=n[t-3]^n[t-8]^n[t-14]^n[t-16],n[t]=h<<1|h>>>31;for(t=0;t<20;t+=5)s=(h=(i=(h=(e=(h=(r=(h=(o=(h=s<<5|s>>>27)+(i&e|~i&r)+o+1518500249+n[t]<<0)<<5|o>>>27)+(s&(i=i<<30|i>>>2)|~s&e)+r+1518500249+n[t+1]<<0)<<5|r>>>27)+(o&(s=s<<30|s>>>2)|~o&i)+e+1518500249+n[t+2]<<0)<<5|e>>>27)+(r&(o=o<<30|o>>>2)|~r&s)+i+1518500249+n[t+3]<<0)<<5|i>>>27)+(e&(r=r<<30|r>>>2)|~e&o)+s+1518500249+n[t+4]<<0,e=e<<30|e>>>2;for(;t<40;t+=5)s=(h=(i=(h=(e=(h=(r=(h=(o=(h=s<<5|s>>>27)+(i^e^r)+o+1859775393+n[t]<<0)<<5|o>>>27)+(s^(i=i<<30|i>>>2)^e)+r+1859775393+n[t+1]<<0)<<5|r>>>27)+(o^(s=s<<30|s>>>2)^i)+e+1859775393+n[t+2]<<0)<<5|e>>>27)+(r^(o=o<<30|o>>>2)^s)+i+1859775393+n[t+3]<<0)<<5|i>>>27)+(e^(r=r<<30|r>>>2)^o)+s+1859775393+n[t+4]<<0,e=e<<30|e>>>2;for(;t<60;t+=5)s=(h=(i=(h=(e=(h=(r=(h=(o=(h=s<<5|s>>>27)+(i&e|i&r|e&r)+o-1894007588+n[t]<<0)<<5|o>>>27)+(s&(i=i<<30|i>>>2)|s&e|i&e)+r-1894007588+n[t+1]<<0)<<5|r>>>27)+(o&(s=s<<30|s>>>2)|o&i|s&i)+e-1894007588+n[t+2]<<0)<<5|e>>>27)+(r&(o=o<<30|o>>>2)|r&s|o&s)+i-1894007588+n[t+3]<<0)<<5|i>>>27)+(e&(r=r<<30|r>>>2)|e&o|r&o)+s-1894007588+n[t+4]<<0,e=e<<30|e>>>2;for(;t<80;t+=5)s=(h=(i=(h=(e=(h=(r=(h=(o=(h=s<<5|s>>>27)+(i^e^r)+o-899497514+n[t]<<0)<<5|o>>>27)+(s^(i=i<<30|i>>>2)^e)+r-899497514+n[t+1]<<0)<<5|r>>>27)+(o^(s=s<<30|s>>>2)^i)+e-899497514+n[t+2]<<0)<<5|e>>>27)+(r^(o=o<<30|o>>>2)^s)+i-899497514+n[t+3]<<0)<<5|i>>>27)+(e^(r=r<<30|r>>>2)^o)+s-899497514+n[t+4]<<0,e=e<<30|e>>>2;this.h0=this.h0+s<<0,this.h1=this.h1+i<<0,this.h2=this.h2+e<<0,this.h3=this.h3+r<<0,this.h4=this.h4+o<<0},t.prototype.hex=function(){this.finalize();var t=this.h0,h=this.h1,s=this.h2,i=this.h3,e=this.h4;return r[t>>28&15]+r[t>>24&15]+r[t>>20&15]+r[t>>16&15]+r[t>>12&15]+r[t>>8&15]+r[t>>4&15]+r[15&t]+r[h>>28&15]+r[h>>24&15]+r[h>>20&15]+r[h>>16&15]+r[h>>12&15]+r[h>>8&15]+r[h>>4&15]+r[15&h]+r[s>>28&15]+r[s>>24&15]+r[s>>20&15]+r[s>>16&15]+r[s>>12&15]+r[s>>8&15]+r[s>>4&15]+r[15&s]+r[i>>28&15]+r[i>>24&15]+r[i>>20&15]+r[i>>16&15]+r[i>>12&15]+r[i>>8&15]+r[i>>4&15]+r[15&i]+r[e>>28&15]+r[e>>24&15]+r[e>>20&15]+r[e>>16&15]+r[e>>12&15]+r[e>>8&15]+r[e>>4&15]+r[15&e]},t.prototype.toString=t.prototype.hex,t.prototype.digest=function(){this.finalize();var t=this.h0,h=this.h1,s=this.h2,i=this.h3,e=this.h4;return[t>>24&255,t>>16&255,t>>8&255,255&t,h>>24&255,h>>16&255,h>>8&255,255&h,s>>24&255,s>>16&255,s>>8&255,255&s,i>>24&255,i>>16&255,i>>8&255,255&i,e>>24&255,e>>16&255,e>>8&255,255&e]},t.prototype.array=t.prototype.digest,t.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(20),h=new DataView(t);return h.setUint32(0,this.h0),h.setUint32(4,this.h1),h.setUint32(8,this.h2),h.setUint32(12,this.h3),h.setUint32(16,this.h4),t};var y=c();i?module.exports=y:(h.sha1=y,e&&define(function(){return y}))}(); \ No newline at end of file diff --git a/src/js/Start.js b/src/js/Start.js index 8ec2533..14a55a2 100644 --- a/src/js/Start.js +++ b/src/js/Start.js @@ -1,4 +1,5 @@ import Minecraft from './net/minecraft/client/Minecraft.js'; +import * as aesjs from '../../libraries/aes.js'; class Start { @@ -60,4 +61,8 @@ window.addEventListener('pageshow', function (event) { // Launch game new Start().launch("canvas-container"); } -}); \ No newline at end of file +}); + +export function require(module) { + return window[module]; +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/Minecraft.js b/src/js/net/minecraft/client/Minecraft.js index 414a399..8bb0617 100644 --- a/src/js/net/minecraft/client/Minecraft.js +++ b/src/js/net/minecraft/client/Minecraft.js @@ -5,7 +5,6 @@ import WorldRenderer from "./render/WorldRenderer.js"; import ScreenRenderer from "./render/gui/ScreenRenderer.js"; import ItemRenderer from "./render/gui/ItemRenderer.js"; import IngameOverlay from "./gui/overlay/IngameOverlay.js"; -import PlayerEntity from "./entity/PlayerEntity.js"; import SoundManager from "./sound/SoundManager.js"; import Block from "./world/block/Block.js"; import BoundingBox from "../util/BoundingBox.js"; @@ -22,12 +21,14 @@ import GuiContainerCreative from "./gui/screens/container/GuiContainerCreative.j import GameProfile from "../util/GameProfile.js"; import UUID from "../util/UUID.js"; import FocusStateType from "../util/FocusStateType.js"; +import Session from "../util/Session.js"; +import PlayerControllerMultiplayer from "./network/controller/PlayerControllerMultiplayer.js"; export default class Minecraft { - static VERSION = "1.1.4" + static VERSION = "1.1.5" static URL_GITHUB = "https://github.com/labystudio/js-minecraft"; - static PROTOCOL_VERSION = 758; + static PROTOCOL_VERSION = 47; //758; // TODO Add to settings static PROXY = { @@ -45,12 +46,13 @@ export default class Minecraft { this.loadingScreen = null; this.world = null; this.player = null; - + this.playerController = null; this.fps = 0; this.maxFps = 0; let username = "Player" + Math.floor(Math.random() * 100); - this.profile = new GameProfile(username, UUID.randomUUID()); + let profile = new GameProfile(username, UUID.randomUUID()); + this.session = new Session(profile, ""); // Tick timer this.timer = new Timer(20); @@ -111,7 +113,14 @@ export default class Minecraft { this.worldRenderer.reset(); this.itemRenderer.reset(); - this.world.chunks.clear(); + // Disconnect from server + if (this.playerController instanceof PlayerControllerMultiplayer) { + let networkHandler = this.playerController.getNetworkHandler(); + networkHandler.getNetworkManager().close(); + } + this.playerController = null; + + this.world.getChunkProvider().getChunks().clear(); this.world = null; this.player = null; this.loadingScreen = null; @@ -127,12 +136,11 @@ export default class Minecraft { this.worldRenderer.scene.add(this.world.group); // Create player - this.player = new PlayerEntity(this, this.world); - this.player.username = this.profile.username; + this.player = this.playerController.createPlayer(this.world); + this.player.username = this.session.getProfile().getUsername(); this.world.addEntity(this.player); // Load spawn chunks and respawn player - this.world.findSpawn(); this.world.loadSpawnChunks(); this.player.respawn(); } @@ -192,7 +200,7 @@ export default class Minecraft { onRender(partialTicks) { if (this.isInGame()) { // Player rotation - if (!this.isPaused()) { + if (this.hasInGameFocus()) { let deltaX = this.window.pullMouseMotionX(); let deltaY = this.window.pullMouseMotionY(); this.player.turn(deltaX, deltaY); @@ -283,8 +291,8 @@ export default class Minecraft { let cameraChunkZ = Math.floor(this.player.z) >> 4; let renderDistance = this.settings.viewDistance; - let requiredChunks = Math.pow(renderDistance * 2 - 1, 2); - let loadedChunks = this.world.chunks.size; + let requiredChunks = this.isSingleplayer() ? Math.pow(renderDistance * 2 - 1, 2) : 1; + let loadedChunks = this.world.getChunkProvider().getChunks().size; // Load chunks and count setTimeout(() => { @@ -448,7 +456,19 @@ export default class Minecraft { } isPaused() { - return !this.hasInGameFocus() && this.loadingScreen === null; + return !this.hasInGameFocus() && this.loadingScreen === null && this.isSingleplayer(); + } + + setSession(session) { + this.session = session; + } + + getSession() { + return this.session; + } + + isSingleplayer() { + return this.isInGame() && !(this.playerController instanceof PlayerControllerMultiplayer); } stop() { diff --git a/src/js/net/minecraft/client/entity/Entity.js b/src/js/net/minecraft/client/entity/Entity.js index c1c641b..c9caa2e 100644 --- a/src/js/net/minecraft/client/entity/Entity.js +++ b/src/js/net/minecraft/client/entity/Entity.js @@ -8,8 +8,7 @@ export default class Entity { this.minecraft = minecraft; this.world = world; this.random = new Random(); - - this.renderer = this.minecraft.worldRenderer.entityRenderManager.createEntityRendererByEntity(this); + this.renderer = null; this.x = 0; this.y = 0; @@ -48,6 +47,13 @@ export default class Entity { this.setPosition(this.x, this.y, this.z); } + initRenderer() { + this.renderer = this.minecraft.worldRenderer.entityRenderManager.createEntityRendererByEntity(this); + if (this.renderer === null) { + throw new Error("No entity renderer for entity " + this.constructor.name + " found!"); + } + } + setPosition(x, y, z) { // Update position this.x = x; diff --git a/src/js/net/minecraft/client/entity/PlayerEntityMultiplayer.js b/src/js/net/minecraft/client/entity/PlayerEntityMultiplayer.js new file mode 100644 index 0000000..25749e1 --- /dev/null +++ b/src/js/net/minecraft/client/entity/PlayerEntityMultiplayer.js @@ -0,0 +1,69 @@ +import PlayerEntity from "./PlayerEntity.js"; +import ClientPlayerMovementPacket from "../network/packet/play/client/ClientPlayerMovementPacket.js"; +import ClientPlayerRotationPacket from "../network/packet/play/client/ClientPlayerRotationPacket.js"; +import ClientPlayerPositionPacket from "../network/packet/play/client/ClientPlayerPositionPacket.js"; +import ClientPlayerPositionRotationPacket from "../network/packet/play/client/ClientPlayerPositionRotationPacket.js"; + +export default class PlayerEntityMultiplayer extends PlayerEntity { + + constructor(minecraft, world, networkHandler) { + super(minecraft, world); + + this.networkHandler = networkHandler; + + this.positionUpdateTicks = 0; + + this.lastReportedX = 0; + this.lastReportedY = 0; + this.lastReportedZ = 0; + + this.lastReportedYaw = 0; + this.lastReportedPitch = 0; + } + + onUpdate() { + super.onUpdate(); + this.onUpdateWalkingPlayer(); + } + + onUpdateWalkingPlayer() { + let movementX = this.x - this.lastReportedX; + let movementY = this.y - this.lastReportedY; + let movementZ = this.z - this.lastReportedZ; + + let movementYaw = this.rotationYaw - this.lastReportedYaw; + let movementPitch = this.rotationPitch - this.lastReportedPitch; + + let reportPosition = movementX * movementX + movementY * movementY + movementZ * movementZ > 9.0E-4 || this.positionUpdateTicks >= 20; + let reportRotation = movementYaw !== 0.0 || movementPitch !== 0.0; + + if (reportPosition && reportRotation) { + this.networkHandler.sendPacket(new ClientPlayerPositionRotationPacket(this.onGround, this.x, this.y, this.z, this.rotationYaw, this.rotationPitch)); + } else if (reportPosition) { + this.networkHandler.sendPacket(new ClientPlayerPositionPacket(this.onGround, this.x, this.y, this.z)); + } else if (reportRotation) { + this.networkHandler.sendPacket(new ClientPlayerRotationPacket(this.onGround, this.rotationYaw, this.rotationPitch)); + } else { + this.networkHandler.sendPacket(new ClientPlayerMovementPacket(this.onGround)); + } + + this.positionUpdateTicks++; + + if (reportPosition) { + this.lastReportedX = this.x; + this.lastReportedY = this.y; + this.lastReportedZ = this.z; + this.positionUpdateTicks = 0; + } + + if (reportRotation) { + this.lastReportedYaw = this.rotationYaw; + this.lastReportedPitch = this.rotationPitch; + } + } + + getNetworkHandler() { + return this.networkHandler; + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/gui/overlay/IngameOverlay.js b/src/js/net/minecraft/client/gui/overlay/IngameOverlay.js index aa0acbc..4c58799 100644 --- a/src/js/net/minecraft/client/gui/overlay/IngameOverlay.js +++ b/src/js/net/minecraft/client/gui/overlay/IngameOverlay.js @@ -129,7 +129,7 @@ export default class IngameOverlay extends Gui { let visibleChunks = 0; let loadedChunks = 0; - for (let [index, chunk] of world.chunks) { + for (let [index, chunk] of world.getChunkProvider().getChunks()) { for (let y in chunk.sections) { let chunkSection = chunk.sections[y]; if (chunkSection.group.visible) { diff --git a/src/js/net/minecraft/client/gui/screens/GuiChat.js b/src/js/net/minecraft/client/gui/screens/GuiChat.js index 46dabb6..f69e8f1 100644 --- a/src/js/net/minecraft/client/gui/screens/GuiChat.js +++ b/src/js/net/minecraft/client/gui/screens/GuiChat.js @@ -47,7 +47,7 @@ export default class GuiChat extends GuiScreen { if (message.startsWith("/")) { this.minecraft.commandHandler.handleMessage(message.substring(1)); } else { - this.minecraft.addMessageToChat("<" + this.minecraft.player.username + "> " + message); + this.minecraft.playerController.sendChatMessage(message); } return; } diff --git a/src/js/net/minecraft/client/gui/screens/GuiConnecting.js b/src/js/net/minecraft/client/gui/screens/GuiConnecting.js index 39a6891..a904bb2 100644 --- a/src/js/net/minecraft/client/gui/screens/GuiConnecting.js +++ b/src/js/net/minecraft/client/gui/screens/GuiConnecting.js @@ -34,7 +34,7 @@ export default class GuiConnecting extends GuiScreen { // Send Minecraft protocol handshake this.networkManager.sendPacket(new HandshakePacket(Minecraft.PROTOCOL_VERSION, ProtocolState.LOGIN)); - this.networkManager.sendPacket(new LoginStartPacket(this.minecraft.profile.username)); + this.networkManager.sendPacket(new LoginStartPacket(this.minecraft.getSession().getProfile().getUsername())); } init() { @@ -66,7 +66,7 @@ export default class GuiConnecting extends GuiScreen { onClose() { super.onClose(); - if (this.networkManager !== null) { + if (this.networkManager !== null && this.networkManager.getState() !== ProtocolState.PLAY) { this.networkManager.close(); } } diff --git a/src/js/net/minecraft/client/gui/screens/GuiCreateWorld.js b/src/js/net/minecraft/client/gui/screens/GuiCreateWorld.js index 0988e05..9339c47 100644 --- a/src/js/net/minecraft/client/gui/screens/GuiCreateWorld.js +++ b/src/js/net/minecraft/client/gui/screens/GuiCreateWorld.js @@ -4,6 +4,8 @@ import World from "../../world/World.js"; import GuiTextField from "../widgets/GuiTextField.js"; import Random from "../../../util/Random.js"; import Long from "../../../../../../../libraries/long.js"; +import ChunkProviderGenerate from "../../world/provider/ChunkProviderGenerate.js"; +import PlayerController from "../../network/controller/PlayerController.js"; export default class GuiCreateWorld extends GuiScreen { @@ -33,7 +35,14 @@ export default class GuiCreateWorld extends GuiScreen { } seed = Long.fromNumber(h); } - this.minecraft.loadWorld(new World(this.minecraft, seed)); + + // Load world + let world = new World(this.minecraft); + world.setChunkProvider(new ChunkProviderGenerate(world, seed)); + world.getChunkProvider().findSpawn(); + + this.minecraft.playerController = new PlayerController(this.minecraft); + this.minecraft.loadWorld(world); })); this.buttonList.push(new GuiButton("Cancel", this.width / 2 + 5, y + 110, 150, 20, () => { this.minecraft.displayScreen(this.previousScreen); diff --git a/src/js/net/minecraft/client/network/NetworkManager.js b/src/js/net/minecraft/client/network/NetworkManager.js index 1e1ed96..d4570c8 100644 --- a/src/js/net/minecraft/client/network/NetworkManager.js +++ b/src/js/net/minecraft/client/network/NetworkManager.js @@ -1,10 +1,13 @@ import ByteBuf from "./util/ByteBuf.js"; import PacketRegistry from "./PacketRegistry.js"; import ProtocolState from "./ProtocolState.js"; -import {aesjs} from "../../../../../../libraries/aes.js"; +import {require} from "../../../../Start.js"; export default class NetworkManager { + static DEBUG = false; + static MAX_COMPRESSION = 2097152; + constructor(minecraft) { this.minecraft = minecraft; this.socket = null; @@ -14,10 +17,10 @@ export default class NetworkManager { this.registry = new PacketRegistry(); this.protocolState = ProtocolState.HANDSHAKE; - this.readBuffer = new ByteBuf(); - this.expectedLength = -1; - this.queue = []; + + this.pako = require("pako"); + this.compressionThreshold = 0; } setNetworkHandler(networkHandler) { @@ -41,11 +44,10 @@ export default class NetworkManager { this.connected = true; // Send proxy handshake - let object = { - "address": this.address, + this.sendProxyPacket(0, { + "host": this.address, "port": this.port, - }; - this.socket.send(JSON.stringify(object)); + }); // Handle connect event this.networkHandler.onConnect(); @@ -54,6 +56,14 @@ export default class NetworkManager { this.flushPacketQueue(); } + sendProxyPacket(id, payload) { + let object = { + "id": id, + "payload": payload + }; + this.socket.send(JSON.stringify(object)); + } + sendPacket(packet) { if (this.connected) { this._sendPacketImmediately(packet); @@ -67,81 +77,138 @@ export default class NetworkManager { let packetState = this.registry.getPacketState(packet); if (packetState !== this.protocolState) { if (packetState === null) { - console.error("[Network] Tried to send unknown packet: " + packet); + console.error("[Network] Tried to send unknown packet: " + packet.constructor.name); return; } - console.log("[Network] Switching protocol state from " + ProtocolState.getName(this.protocolState) + " to " + ProtocolState.getName(packetState)); - this.protocolState = packetState; + this.setState(packetState); } - // Write packet to buffer + // Packet Codec let buffer = new ByteBuf(); buffer.writeByte(this.registry.getClientBoundPacketId(this.protocolState, packet)); packet.write(buffer); + buffer.setPosition(0); - // Write chunk header - let array = buffer.getArray(); + // Packet Compression + if (this.compressionThreshold !== 0) { + let length = buffer.length(); + if (length > this.compressionThreshold) { + let compressed = this.pako.deflate(buffer.getArray(), { + chunkSize: 8192 + }); + + buffer = new ByteBuf(); + buffer.writeVarInt(length); + buffer.write(compressed); + } else { + let copy = buffer.getArray(); + buffer = new ByteBuf(); + buffer.writeVarInt(0); + buffer.write(copy); + } + buffer.setPosition(0); + } + + // Packet Sizer let wrapper = new ByteBuf(); - wrapper.writeVarInt(array.length); - wrapper.write(array); - let chunk = wrapper.getArray().buffer; + wrapper.writeVarInt(buffer.length()); + wrapper.write(buffer.getArray()); - // Encrypt chunk + // Packet Compression if (this.isEncrypted) { - chunk = this.encryption.encrypt(new Uint8Array(chunk)); + wrapper = new ByteBuf(this.encryption.encrypt(wrapper.getArray())); } // Send chunk - this.socket.send(chunk); + this.socket.send(wrapper.getArray()); - console.log("[Network] [OUT] " + packet.constructor.name); + if (NetworkManager.DEBUG) { + console.log("[Network] [OUT] " + packet.constructor.name); + } } _onMessage(event) { - let chunk = new Int8Array(event.data); + try { + let data = new Uint8Array(event.data); - // Decrypt chunk - if (this.isEncrypted) { - chunk = this.encryption.decrypt(new Uint8Array(event.data)); - } + // Packet Compression + if (this.isEncrypted) { + data = this.decryption.decrypt(data); + } - // Read packet header - if (this.expectedLength === -1) { - let buf = new ByteBuf(chunk); - this.expectedLength = buf.readVarInt(); // Read packet length - this.readBuffer.setPosition(0); - chunk = chunk.slice(buf.getPosition()); - } + // Packet Sizer + let bufferIn = new ByteBuf(new Int8Array(data)); + while (bufferIn.readableBytes() > 0) { + let three = [0, 0, 0]; + for (let i = 0; i < three.length; i++) { + three[i] = bufferIn.readByte(); + if (three[i] >= 0) { + let length = new ByteBuf(three).readVarInt(); + if (length === 0) { + throw new Error("Empty Packet!"); + } - // Fill packet content - this.readBuffer.write(chunk); - - // Handle packet - if (this.readBuffer.getPosition() >= this.expectedLength) { - this.expectedLength = -1; - this.readBuffer.setPosition(0); - this.onPacketBufferReceived(this.readBuffer); + if (bufferIn.readableBytes() < length) { + break; + } else { + this.handlePacket(new ByteBuf(bufferIn.getSlicedArray(length))); + bufferIn.skipBytes(length); + } + break; + } + } + } + } catch (e) { + console.error(e); } } - onPacketBufferReceived(buffer) { + handlePacket(buffer) { + // Packet Compression + if (this.compressionThreshold !== 0) { + let uncompressedLength = buffer.readVarInt(); + + if (uncompressedLength !== 0) { + if (uncompressedLength < this.compressionThreshold) { + throw new Error("Badly compressed packet - size of " + uncompressedLength + " is below server threshold of " + this.compressionThreshold); + } + if (uncompressedLength > NetworkManager.MAX_COMPRESSION) { + throw new Error("Badly compressed packet - size of " + uncompressedLength + " is larger than protocol maximum of " + NetworkManager.MAX_COMPRESSION); + } + + // Decompress + buffer = new ByteBuf(this.pako.inflate(new Uint8Array(buffer.getSlicedArray()), { + chunkSize: 8192 + })); + + if (buffer.length() !== uncompressedLength) { + throw new Error("Badly compressed packet - decompressed size of " + buffer.length() + " is not equal to original size of " + uncompressedLength); + } + } + } + + // Packet Codec let packetId = buffer.readByte(); // Read packet id let clazz = this.registry.getServerBoundById(this.protocolState, packetId); if (clazz === null) { - console.log("[Network] Unknown packet id: " + packetId); + if (NetworkManager.DEBUG) { + console.log("[Network] [IN] Unknown packet id: " + packetId + " (0x" + packetId.toString(16) + ")"); + } return; + } else { + if (NetworkManager.DEBUG) { + console.log("[Network] [IN] " + clazz.name); + } } let packet = new clazz; - console.log("[Network] [IN] " + packet.constructor.name); - packet.read(buffer, buffer.length); packet.handle(this.networkHandler); } _onError(event) { - + console.error("[Network] Error: " + event.data); } _onClose(event) { @@ -164,6 +231,26 @@ export default class NetworkManager { enableEncryption(secretKey) { this.isEncrypted = true; - this.encryption = new aesjs.ModeOfOperation.cfb(secretKey, secretKey, 1); + this.decryption = new (require("aesjs").ModeOfOperation).cfb(secretKey, secretKey, 1); + this.encryption = new (require("aesjs").ModeOfOperation).cfb(secretKey, secretKey, 1); + } + + setState(packetState) { + console.log("[Network] Switching protocol state from " + this.protocolState.getName() + " to " + packetState.getName()); + this.protocolState = packetState; + } + + getState() { + return this.protocolState; + } + + setCompressionThreshold(threshold) { + console.log("[Network] Set compression threshold to " + threshold); + + if (threshold >= 0) { + this.compressionThreshold = threshold; + } else { + this.compressionThreshold = 0; + } } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/PacketRegistry.js b/src/js/net/minecraft/client/network/PacketRegistry.js index efd26c9..ad79794 100644 --- a/src/js/net/minecraft/client/network/PacketRegistry.js +++ b/src/js/net/minecraft/client/network/PacketRegistry.js @@ -6,6 +6,20 @@ import StatusResponsePacket from "./packet/status/server/StatusResponsePacket.js import EncryptionRequestPacket from "./packet/login/server/EncryptionRequestPacket.js"; import EncryptionResponsePacket from "./packet/login/client/EncryptionResponsePacket.js"; import LoginDisconnectPacket from "./packet/login/server/LoginDisconnectPacket.js"; +import LoginSuccessPacket from "./packet/login/server/LoginSuccessPacket.js"; +import EnableCompressionPacket from "./packet/login/server/EnableCompressionPacket.js"; +import ServerKeepAlivePacket from "./packet/play/server/ServerKeepAlivePacket.js"; +import ServerJoinGamePacket from "./packet/play/server/ServerJoinGamePacket.js"; +import ClientKeepAlivePacket from "./packet/play/client/ClientKeepAlivePacket.js"; +import ClientChatPacket from "./packet/play/client/ClientChatPacket.js"; +import ClientPlayerMovementPacket from "./packet/play/client/ClientPlayerMovementPacket.js"; +import ClientPlayerRotationPacket from "./packet/play/client/ClientPlayerRotationPacket.js"; +import ClientPlayerPositionPacket from "./packet/play/client/ClientPlayerPositionPacket.js"; +import ClientPlayerPositionRotationPacket from "./packet/play/client/ClientPlayerPositionRotationPacket.js"; +import ServerChunkDataPacket from "./packet/play/server/ServerChunkDataPacket.js"; +import ServerMultiChunkDataPacket from "./packet/play/server/ServerMultiChunkDataPacket.js"; +import ServerBlockChangePacket from "./packet/play/server/ServerBlockChangePacket.js"; +import ServerChatPacket from "./packet/play/server/ServerChatPacket.js"; export default class PacketRegistry { @@ -23,9 +37,26 @@ export default class PacketRegistry { // Register login this.registerServer(ProtocolState.LOGIN, 0x00, LoginDisconnectPacket); this.registerServer(ProtocolState.LOGIN, 0x01, EncryptionRequestPacket); + this.registerServer(ProtocolState.LOGIN, 0x02, LoginSuccessPacket); + this.registerServer(ProtocolState.LOGIN, 0x03, EnableCompressionPacket); this.registerClient(ProtocolState.LOGIN, 0x00, LoginStartPacket); this.registerClient(ProtocolState.LOGIN, 0x01, EncryptionResponsePacket); + + // Register play + this.registerServer(ProtocolState.PLAY, 0x00, ServerKeepAlivePacket); + this.registerServer(ProtocolState.PLAY, 0x01, ServerJoinGamePacket); + this.registerServer(ProtocolState.PLAY, 0x02, ServerChatPacket); + this.registerServer(ProtocolState.PLAY, 0x21, ServerChunkDataPacket); + this.registerServer(ProtocolState.PLAY, 0x23, ServerBlockChangePacket); + this.registerServer(ProtocolState.PLAY, 0x26, ServerMultiChunkDataPacket); + + this.registerClient(ProtocolState.PLAY, 0x00, ClientKeepAlivePacket); + this.registerClient(ProtocolState.PLAY, 0x01, ClientChatPacket); + this.registerClient(ProtocolState.PLAY, 0x03, ClientPlayerMovementPacket); + this.registerClient(ProtocolState.PLAY, 0x04, ClientPlayerPositionPacket); + this.registerClient(ProtocolState.PLAY, 0x05, ClientPlayerRotationPacket); + this.registerClient(ProtocolState.PLAY, 0x06, ClientPlayerPositionRotationPacket); } registerClient(state, id, packet) { @@ -37,29 +68,29 @@ export default class PacketRegistry { } _register(registry, state, id, packet) { - if (typeof registry[state] === "undefined") { - registry[state] = []; + if (typeof registry[state.getId()] === "undefined") { + registry[state.getId()] = []; } - registry[state][id] = packet; + registry[state.getId()][id] = packet; } getServerBoundById(state, id) { - if (typeof this.packetsServer[state][id] === "undefined") { + if (typeof this.packetsServer[state.getId()][id] === "undefined") { return null; } - return this.packetsServer[state][id]; + return this.packetsServer[state.getId()][id]; } getClientBoundById(state, id) { - if (typeof this.packetsClient[state][id] === "undefined") { + if (typeof this.packetsClient[state.getId()][id] === "undefined") { return null; } - return this.packetsClient[state][id]; + return this.packetsClient[state.getId()][id]; } getClientBoundPacketId(state, packet) { - for (let id in this.packetsClient[state]) { - if (this.packetsClient[state][id] === packet.constructor) { + for (let id in this.packetsClient[state.getId()]) { + if (this.packetsClient[state.getId()][id] === packet.constructor) { return id; } } @@ -68,7 +99,7 @@ export default class PacketRegistry { getServerBoundPacketId(state, packet) { for (let id in this.packetsServer[state]) { - if (this.packetsServer[state][id] === packet.constructor) { + if (this.packetsServer[state.getId()][id] === packet.constructor) { return id; } } @@ -79,14 +110,14 @@ export default class PacketRegistry { for (const [state, value] of Object.entries(this.packetsClient)) { for (let id in value) { if (value[id] === packet.constructor) { - return parseInt(state); + return ProtocolState.fromId(parseInt(state)); } } } for (const [state, value] of Object.entries(this.packetsServer)) { for (let id in value) { if (value[id] === packet.constructor) { - return parseInt(state); + return ProtocolState.fromId(parseInt(state)); } } } diff --git a/src/js/net/minecraft/client/network/ProtocolState.js b/src/js/net/minecraft/client/network/ProtocolState.js index 59cc0bb..c2a7e70 100644 --- a/src/js/net/minecraft/client/network/ProtocolState.js +++ b/src/js/net/minecraft/client/network/ProtocolState.js @@ -1,11 +1,19 @@ export default class ProtocolState { - static HANDSHAKE = -1; - static PLAY = 0; - static STATUS = 1; - static LOGIN = 2; + static HANDSHAKE = new ProtocolState(-1); + static PLAY = new ProtocolState(0); + static STATUS = new ProtocolState(1); + static LOGIN = new ProtocolState(2); - static getName(state) { - switch (state) { + constructor(id) { + this.id = id; + } + + getId() { + return this.id; + } + + getName() { + switch (this) { case ProtocolState.HANDSHAKE: return "HANDSHAKE"; case ProtocolState.LOGIN: @@ -18,4 +26,22 @@ export default class ProtocolState { return "UNKNOWN"; } } + + + static fromId(id) { + for (let state of this.values()) { + if (state.getId() === id) { + return state; + } + } + } + + static values() { + return [ + ProtocolState.HANDSHAKE, + ProtocolState.LOGIN, + ProtocolState.PLAY, + ProtocolState.STATUS + ]; + } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/controller/PlayerController.js b/src/js/net/minecraft/client/network/controller/PlayerController.js new file mode 100644 index 0000000..4c44797 --- /dev/null +++ b/src/js/net/minecraft/client/network/controller/PlayerController.js @@ -0,0 +1,16 @@ +import PlayerEntity from "../../entity/PlayerEntity.js"; + +export default class PlayerController { + + constructor(minecraft) { + this.minecraft = minecraft; + } + + createPlayer(world) { + return new PlayerEntity(this.minecraft, world); + } + + sendChatMessage(message) { + this.minecraft.addMessageToChat("<" + this.minecraft.player.username + "> " + message); + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/controller/PlayerControllerMultiplayer.js b/src/js/net/minecraft/client/network/controller/PlayerControllerMultiplayer.js new file mode 100644 index 0000000..b91c8fa --- /dev/null +++ b/src/js/net/minecraft/client/network/controller/PlayerControllerMultiplayer.js @@ -0,0 +1,24 @@ +import PlayerController from "./PlayerController.js"; +import PlayerEntityMultiplayer from "../../entity/PlayerEntityMultiplayer.js"; +import ClientChatPacket from "../packet/play/client/ClientChatPacket.js"; + +export default class PlayerControllerMultiplayer extends PlayerController { + + constructor(minecraft, networkHandler) { + super(minecraft); + + this.networkHandler = networkHandler; + } + + createPlayer(world) { + return new PlayerEntityMultiplayer(this.minecraft, world, this.networkHandler); + } + + sendChatMessage(message) { + this.networkHandler.sendPacket(new ClientChatPacket(message)); + } + + getNetworkHandler() { + return this.networkHandler; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/handler/NetworkLoginHandler.js b/src/js/net/minecraft/client/network/handler/NetworkLoginHandler.js index 0fbb72e..ef9fbfe 100644 --- a/src/js/net/minecraft/client/network/handler/NetworkLoginHandler.js +++ b/src/js/net/minecraft/client/network/handler/NetworkLoginHandler.js @@ -1,7 +1,10 @@ -import PacketHandler from "../PacketHandler.js"; +import PacketHandler from "./PacketHandler.js"; import EncryptionResponsePacket from "../packet/login/client/EncryptionResponsePacket.js"; import CryptManager from "../util/CryptManager.js"; import GuiDisconnected from "../../gui/screens/GuiDisconnected.js"; +import Authentication from "../util/Authentication.js"; +import NetworkPlayHandler from "./NetworkPlayHandler.js"; +import ProtocolState from "../ProtocolState.js"; export default class NetworkLoginHandler extends PacketHandler { @@ -9,10 +12,19 @@ export default class NetworkLoginHandler extends PacketHandler { super(); this.networkManager = networkManager; + this.authentication = new Authentication(networkManager); } handleEncryptionRequest(packet) { + // Create an AES key for the packet encryption let secretKey = CryptManager.createNewSharedKey(); + + // Send join server request to Mojang + let session = this.networkManager.minecraft.getSession(); + let serverId = this.authentication.createServerHash(packet.serverId, secretKey, packet.publicKey); + this.authentication.joinServer(session.getProfile(), session.getAccessToken(), serverId); + + // Send encryption response this.networkManager.sendPacket(new EncryptionResponsePacket(secretKey, packet.publicKey, packet.verifyToken)); // Enable encryption @@ -24,8 +36,19 @@ export default class NetworkLoginHandler extends PacketHandler { this.networkManager.minecraft.displayScreen(new GuiDisconnected(packet.message)); } - onDisconnect() { + handleLoginSuccess(packet) { + this.networkManager.setState(ProtocolState.PLAY); + this.networkManager.setNetworkHandler(new NetworkPlayHandler(this.networkManager, packet.profile)); + } + handleEnableCompression(packet) { + this.networkManager.setCompressionThreshold(packet.getCompressionThreshold()); + } + + onDisconnect() { + if (this.networkManager.minecraft.isInGame()) { + this.networkManager.minecraft.displayScreen(new GuiDisconnected("Disconnected from server")); + } } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/handler/NetworkPlayHandler.js b/src/js/net/minecraft/client/network/handler/NetworkPlayHandler.js new file mode 100644 index 0000000..b1a4342 --- /dev/null +++ b/src/js/net/minecraft/client/network/handler/NetworkPlayHandler.js @@ -0,0 +1,76 @@ +import PacketHandler from "./PacketHandler.js"; +import GuiDisconnected from "../../gui/screens/GuiDisconnected.js"; +import WorldClient from "../../world/WorldClient.js"; +import ClientKeepAlivePacket from "../packet/play/client/ClientKeepAlivePacket.js"; +import PlayerControllerMultiplayer from "../controller/PlayerControllerMultiplayer.js"; + +export default class NetworkPlayHandler extends PacketHandler { + + constructor(networkManager, profile) { + super(); + + this.minecraft = networkManager.minecraft; + this.networkManager = networkManager; + this.profile = profile; + } + + handleKeepAlive(packet) { + this.networkManager.sendPacket(new ClientKeepAlivePacket(packet.getId())); + } + + handleJoinGame(packet) { + this.minecraft.playerController = new PlayerControllerMultiplayer(this.minecraft, this); + let world = new WorldClient(this.minecraft); + this.minecraft.loadWorld(world); + } + + handleServerChat(packet) { + this.minecraft.ingameOverlay.chatOverlay.addMessage(packet.getMessage()); + } + + handleChunkData(packet) { + let provider = this.minecraft.world.getChunkProvider(); + + if (packet.isFullChunk()) { + if (packet.getDataSize() === 0) { + provider.unloadChunk(packet.getX(), packet.getZ()); + return; + } + + provider.loadChunk(packet.getX(), packet.getZ()); + } + + let chunk = this.minecraft.world.getChunkAt(packet.getX(), packet.getZ()); + chunk.fillChunk(packet.getData(), packet.getDataSize(), packet.isFullChunk()); + } + + handleMultiChunkData(packet) { + for (let chunkData of packet.getChunkData()) { + this.handleChunkData(chunkData); + } + } + + handleBlockChange(packet) { + let position = packet.getBlockPosition(); + + let blockState = packet.getBlockState(); + let typeId = blockState >> 4; + + this.minecraft.world.setBlockAt(position.getX(), position.getY(), position.getZ(), typeId); + } + + onDisconnect() { + if (this.minecraft.isInGame()) { + this.minecraft.displayScreen(new GuiDisconnected("Disconnected from server")); + } + } + + getNetworkManager() { + return this.networkManager; + } + + sendPacket(packet) { + this.networkManager.sendPacket(packet); + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/PacketHandler.js b/src/js/net/minecraft/client/network/handler/PacketHandler.js similarity index 50% rename from src/js/net/minecraft/client/network/PacketHandler.js rename to src/js/net/minecraft/client/network/handler/PacketHandler.js index 4ae9a32..cc45236 100644 --- a/src/js/net/minecraft/client/network/PacketHandler.js +++ b/src/js/net/minecraft/client/network/handler/PacketHandler.js @@ -4,14 +4,6 @@ export default class PacketHandler { } - handleStatusResponse(packet) { - - } - - handleEncryptionRequest(packet) { - - } - onDisconnect() { } diff --git a/src/js/net/minecraft/client/network/packet/handshake/client/HandshakePacket.js b/src/js/net/minecraft/client/network/packet/handshake/client/HandshakePacket.js index 157f93a..3e38827 100644 --- a/src/js/net/minecraft/client/network/packet/handshake/client/HandshakePacket.js +++ b/src/js/net/minecraft/client/network/packet/handshake/client/HandshakePacket.js @@ -13,7 +13,7 @@ export default class HandshakePacket extends Packet { buffer.writeVarInt(this.version); // Protocol version buffer.writeString("localhost"); // Server address buffer.writeShort(25565); // Server port - buffer.writeVarInt(this.nextState); // Next state + buffer.writeVarInt(this.nextState.getId()); // Next state } read(buffer) { diff --git a/src/js/net/minecraft/client/network/packet/login/server/EnableCompressionPacket.js b/src/js/net/minecraft/client/network/packet/login/server/EnableCompressionPacket.js new file mode 100644 index 0000000..d4cd834 --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/login/server/EnableCompressionPacket.js @@ -0,0 +1,26 @@ +import Packet from "../../../Packet.js"; + +export default class EnableCompressionPacket extends Packet { + + constructor() { + super(); + + this.compressionThreshold = 0; + } + + write(buffer) { + + } + + read(buffer) { + this.compressionThreshold = buffer.readVarInt(); + } + + handle(handler) { + handler.handleEnableCompression(this); + } + + getCompressionThreshold() { + return this.compressionThreshold; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/login/server/LoginDisconnectPacket.js b/src/js/net/minecraft/client/network/packet/login/server/LoginDisconnectPacket.js index ab02277..094003d 100644 --- a/src/js/net/minecraft/client/network/packet/login/server/LoginDisconnectPacket.js +++ b/src/js/net/minecraft/client/network/packet/login/server/LoginDisconnectPacket.js @@ -14,7 +14,7 @@ export default class LoginDisconnectPacket extends Packet { } read(buffer) { - this.message = format(JSON.parse(buffer.readString())); + this.message = format(JSON.parse(buffer.readString(32767))); } handle(handler) { diff --git a/src/js/net/minecraft/client/network/packet/login/server/LoginSuccessPacket.js b/src/js/net/minecraft/client/network/packet/login/server/LoginSuccessPacket.js new file mode 100644 index 0000000..d28035e --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/login/server/LoginSuccessPacket.js @@ -0,0 +1,27 @@ +import Packet from "../../../Packet.js"; +import UUID from "../../../../../util/UUID.js"; +import GameProfile from "../../../../../util/GameProfile.js"; + +export default class LoginSuccessPacket extends Packet { + + constructor() { + super(); + + this.profile = null; + } + + write(buffer) { + + } + + read(buffer) { + let uuid = UUID.fromString(buffer.readString()); + let username = buffer.readString(); + + this.profile = new GameProfile(uuid, username); + } + + handle(handler) { + handler.handleLoginSuccess(this); + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/client/ClientChatPacket.js b/src/js/net/minecraft/client/network/packet/play/client/ClientChatPacket.js new file mode 100644 index 0000000..605420d --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/client/ClientChatPacket.js @@ -0,0 +1,14 @@ +import Packet from "../../../Packet.js"; + +export default class ClientChatPacket extends Packet { + + constructor(message) { + super(); + + this.message = message; + } + + write(buffer) { + buffer.writeString(this.message); + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/client/ClientKeepAlivePacket.js b/src/js/net/minecraft/client/network/packet/play/client/ClientKeepAlivePacket.js new file mode 100644 index 0000000..75a0204 --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/client/ClientKeepAlivePacket.js @@ -0,0 +1,18 @@ +import Packet from "../../../Packet.js"; + +export default class ClientKeepAlivePacket extends Packet { + + constructor(id = 0) { + super(); + + this.id = id; + } + + write(buffer) { + buffer.writeVarInt(this.id); + } + + getId() { + return this.id; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerMovementPacket.js b/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerMovementPacket.js new file mode 100644 index 0000000..282e747 --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerMovementPacket.js @@ -0,0 +1,35 @@ +import Packet from "../../../Packet.js"; + +export default class ClientPlayerMovementPacket extends Packet { + + constructor(onGround) { + super(); + + this.position = false; + this.rotation = false; + + this.onGround = onGround; + + this.x = 0; + this.y = 0; + this.z = 0; + + this.yaw = 0; + this.pitch = 0; + } + + write(buffer) { + if (this.position) { + buffer.writeDouble(this.x); + buffer.writeDouble(this.y); + buffer.writeDouble(this.z); + } + + if (this.rotation) { + buffer.writeFloat(this.yaw); + buffer.writeFloat(this.pitch); + } + + buffer.writeBoolean(this.onGround); + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerPositionPacket.js b/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerPositionPacket.js new file mode 100644 index 0000000..ed55e28 --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerPositionPacket.js @@ -0,0 +1,14 @@ +import ClientPlayerMovementPacket from "./ClientPlayerMovementPacket.js"; + +export default class ClientPlayerPositionPacket extends ClientPlayerMovementPacket { + + constructor(onGround, x, y, z) { + super(onGround); + + this.position = true; + + this.x = x; + this.y = y; + this.z = z; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerPositionRotationPacket.js b/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerPositionRotationPacket.js new file mode 100644 index 0000000..243ed1e --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerPositionRotationPacket.js @@ -0,0 +1,18 @@ +import ClientPlayerMovementPacket from "./ClientPlayerMovementPacket.js"; + +export default class ClientPlayerPositionRotationPacket extends ClientPlayerMovementPacket { + + constructor(onGround, x, y, z, yaw, pitch) { + super(onGround); + + this.position = true; + this.rotation = true; + + this.x = x; + this.y = y; + this.z = z; + + this.yaw = yaw; + this.pitch = pitch; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerRotationPacket.js b/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerRotationPacket.js new file mode 100644 index 0000000..873c50e --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/client/ClientPlayerRotationPacket.js @@ -0,0 +1,13 @@ +import ClientPlayerMovementPacket from "./ClientPlayerMovementPacket.js"; + +export default class ClientPlayerRotationPacket extends ClientPlayerMovementPacket { + + constructor(onGround, yaw, pitch) { + super(onGround); + + this.rotation = true; + + this.yaw = yaw; + this.pitch = pitch; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/server/ServerBlockChangePacket.js b/src/js/net/minecraft/client/network/packet/play/server/ServerBlockChangePacket.js new file mode 100644 index 0000000..53c4f05 --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/server/ServerBlockChangePacket.js @@ -0,0 +1,27 @@ +import Packet from "../../../Packet.js"; + +export default class ServerBlockChangePacket extends Packet { + + constructor() { + super(); + + this.blockPosition = null; + } + + read(buffer) { + this.blockPosition = buffer.readBlockPosition(); + this.blockState = buffer.readVarInt(); + } + + handle(handler) { + handler.handleBlockChange(this); + } + + getBlockPosition() { + return this.blockPosition; + } + + getBlockState() { + return this.blockState; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/server/ServerChatPacket.js b/src/js/net/minecraft/client/network/packet/play/server/ServerChatPacket.js new file mode 100644 index 0000000..f4d6f1b --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/server/ServerChatPacket.js @@ -0,0 +1,31 @@ +import Packet from "../../../Packet.js"; +import {format} from "../../../../../../../../../libraries/chat.js"; + +export default class ServerChatPacket extends Packet { + + constructor() { + super(); + + this.message = ""; + this.type = 0; + } + + read(buffer) { + this.message = format(JSON.parse(buffer.readString(32767)), { + useAnsiCodes: true + }); + this.type = buffer.readByte(); + } + + handle(handler) { + handler.handleServerChat(this); + } + + getMessage() { + return this.message; + } + + getType() { + return this.type; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/server/ServerChunkDataPacket.js b/src/js/net/minecraft/client/network/packet/play/server/ServerChunkDataPacket.js new file mode 100644 index 0000000..68b1c29 --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/server/ServerChunkDataPacket.js @@ -0,0 +1,46 @@ +import Packet from "../../../Packet.js"; + +export default class ServerChunkDataPacket extends Packet { + + constructor() { + super(); + + this.x = 0; + this.z = 0; + this.fullChunk = false; + this.dataSize = 0; + this.data = []; + } + + read(buffer) { + this.x = buffer.readInt(); + this.z = buffer.readInt(); + this.fullChunk = buffer.readBoolean(); + this.dataSize = buffer.readShort(); + this.data = buffer.readByteArray(); + } + + handle(handler) { + handler.handleChunkData(this); + } + + getX() { + return this.x; + } + + getZ() { + return this.z; + } + + isFullChunk() { + return this.fullChunk; + } + + getDataSize() { + return this.dataSize; + } + + getData() { + return this.data; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/server/ServerJoinGamePacket.js b/src/js/net/minecraft/client/network/packet/play/server/ServerJoinGamePacket.js new file mode 100644 index 0000000..e04bd4b --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/server/ServerJoinGamePacket.js @@ -0,0 +1,37 @@ +import Packet from "../../../Packet.js"; + +export default class ServerJoinGamePacket extends Packet { + + constructor() { + super(); + + this.entityId = 0; + this.hardcoreMode = false; + this.gameType = 0; + this.dimension = 0; + this.difficulty = 0; + this.maxPlayers = 0; + this.worldType = ""; + this.reducedDebugInfo = false; + } + + write(buffer) { + + } + + read(buffer) { + this.entityId = buffer.readVarInt(); + let bits = buffer.readByte(); + this.hardcoreMode = (bits & 8) === 8; + this.gameType = bits & -9; + this.dimension = buffer.readByte(); + this.difficulty = buffer.readByte(); + this.maxPlayers = buffer.readByte(); + this.worldType = buffer.readString(); + this.reducedDebugInfo = buffer.readBoolean(); + } + + handle(handler) { + handler.handleJoinGame(this); + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/server/ServerKeepAlivePacket.js b/src/js/net/minecraft/client/network/packet/play/server/ServerKeepAlivePacket.js new file mode 100644 index 0000000..127ba53 --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/server/ServerKeepAlivePacket.js @@ -0,0 +1,22 @@ +import Packet from "../../../Packet.js"; + +export default class ServerKeepAlivePacket extends Packet { + + constructor() { + super(); + + this.id = 0; + } + + read(buffer) { + this.id = buffer.readVarInt(); + } + + handle(handler) { + handler.handleKeepAlive(this); + } + + getId() { + return this.id; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/packet/play/server/ServerMultiChunkDataPacket.js b/src/js/net/minecraft/client/network/packet/play/server/ServerMultiChunkDataPacket.js new file mode 100644 index 0000000..26b8b7d --- /dev/null +++ b/src/js/net/minecraft/client/network/packet/play/server/ServerMultiChunkDataPacket.js @@ -0,0 +1,38 @@ +import Packet from "../../../Packet.js"; +import ServerChunkDataPacket from "./ServerChunkDataPacket.js"; + +export default class ServerMultiChunkDataPacket extends Packet { + + constructor() { + super(); + + this.overworld = false; + this.chunkData = []; + } + + read(buffer) { + this.overworld = buffer.readBoolean(); + + let amount = buffer.readVarInt(); + for (let i = 0; i < amount; i++) { + let x = buffer.readInt(); + let y = buffer.readInt(); + let dataSize = buffer.readShort() & 65535; + let data = buffer.readByteArray(); + + this.chunkData.push(new ServerChunkDataPacket(x, y, dataSize, data)); + } + } + + handle(handler) { + handler.handleMultiChunkData(this); + } + + getChunkData() { + return this.chunkData; + } + + isOverworld() { + return this.overworld; + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/pinger/NetworkStatusHandler.js b/src/js/net/minecraft/client/network/pinger/NetworkStatusHandler.js index 565aba3..2d7e592 100644 --- a/src/js/net/minecraft/client/network/pinger/NetworkStatusHandler.js +++ b/src/js/net/minecraft/client/network/pinger/NetworkStatusHandler.js @@ -1,4 +1,4 @@ -import PacketHandler from "../PacketHandler.js"; +import PacketHandler from "../handler/PacketHandler.js"; import GuiDisconnected from "../../gui/screens/GuiDisconnected.js"; export default class NetworkStatusHandler extends PacketHandler { diff --git a/src/js/net/minecraft/client/network/util/Authentication.js b/src/js/net/minecraft/client/network/util/Authentication.js new file mode 100644 index 0000000..fff7101 --- /dev/null +++ b/src/js/net/minecraft/client/network/util/Authentication.js @@ -0,0 +1,50 @@ +import ByteBuf from "./ByteBuf.js"; +import {require} from "../../../../../Start.js"; + +export default class Authentication { + + constructor(networkManager) { + this.networkManager = networkManager; + } + + joinServer(profile, accessToken, serverId) { + this.networkManager.sendProxyPacket(1, { + "accessToken": accessToken, + "selectedProfile": profile.getCompactUUID(), + "serverId": serverId + }) + } + + createServerHash(serverId, secretKey, publicKey) { + // Create hash + let bytes = require("sha1").create() + .update(new TextEncoder().encode(serverId)) + .update(secretKey) + .update(new Uint8Array(publicKey)) + .digest() + + // Convert to hex string + let buffer = new ByteBuf(new Int8Array(bytes)); + let sign = ''; + + // Handle negative hashes + if (buffer.readByte() < 0) { + let carry = true + for (let pos = buffer.length() - 1; pos >= 0; --pos) { + let value = buffer.readByte(pos); + let newValue = ~value & 0xff + + if (carry) { + carry = newValue === 0xff + buffer.writeByte(carry ? 0 : newValue + 1, pos); + } else { + buffer.writeByte(newValue, pos) + } + } + sign = '-'; + } + + // Convert to hex string + return sign + buffer.toString().replace(/^0+/g, ''); + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/util/ByteBuf.js b/src/js/net/minecraft/client/network/util/ByteBuf.js index 4c6ec74..8385b55 100644 --- a/src/js/net/minecraft/client/network/util/ByteBuf.js +++ b/src/js/net/minecraft/client/network/util/ByteBuf.js @@ -1,3 +1,6 @@ +import Long from "../../../../../../../libraries/long.js"; +import BlockPosition from "../../../util/BlockPosition.js"; + export default class ByteBuf { static SEGMENT_BITS = 0x7F; @@ -24,7 +27,12 @@ export default class ByteBuf { return this.array; } - readByte() { + getSlicedArray(length = this.array.length - this.pos) { + return this.array.slice(this.pos, this.pos + length); + } + + readByte(pos = this.pos) { + this.pos = pos; return this.array[this.pos++]; } @@ -40,32 +48,42 @@ export default class ByteBuf { } readLong() { - return this.array[this.pos++] << 56 - | this.array[this.pos++] << 48 - | this.array[this.pos++] << 40 - | this.array[this.pos++] << 32 - | this.array[this.pos++] << 24 - | this.array[this.pos++] << 16 - | this.array[this.pos++] << 8 - | this.array[this.pos++]; + return Long.fromNumber(this.array[this.pos++]).shiftLeft(56) + .or(Long.fromNumber(this.array[this.pos++]).shiftLeft(48)) + .or(Long.fromNumber(this.array[this.pos++]).shiftLeft(40)) + .or(Long.fromNumber(this.array[this.pos++]).shiftLeft(32)) + .or(Long.fromNumber(this.array[this.pos++]).shiftLeft(24)) + .or(Long.fromNumber(this.array[this.pos++]).shiftLeft(16)) + .or(Long.fromNumber(this.array[this.pos++]).shiftLeft(8)) + .or(Long.fromNumber(this.array[this.pos++])); } readFloat() { - return this.readInt() / (1 << 24); + return new Float32Array(new Uint32Array([this.readInt(), 0, 0, 0, 0, 0, 0, 0]).buffer)[0]; } readDouble() { - return this.readLong() / (1 << 53); + let lng = this.readLong(); + return new Float64Array(new Uint32Array([lng.low >>> 0, lng.high >>> 0, 0, 0, 0, 0, 0, 0]).buffer)[0]; } - readString() { + readBoolean() { + return this.readByte() !== 0; + } + + readString(maxLength) { let len = this.readVarInt(); + if (len > maxLength * 4) { + throw new Error("Trying to read string longer than max length (" + len + " > " + (maxLength * 4) + ")"); + } + let array = new Uint8Array(len); this.read(array, len); return new TextDecoder().decode(array); } - writeByte(value) { + writeByte(value, pos = this.pos) { + this.pos = pos; this.extendIfNeeded(1); this.array[this.pos++] = value; } @@ -86,22 +104,24 @@ export default class ByteBuf { writeLong(value) { this.extendIfNeeded(8); - this.array[this.pos++] = value >> 56; - this.array[this.pos++] = value >> 48; - this.array[this.pos++] = value >> 40; - this.array[this.pos++] = value >> 32; - this.array[this.pos++] = value >> 24; - this.array[this.pos++] = value >> 16; - this.array[this.pos++] = value >> 8; - this.array[this.pos++] = value; + this.array[this.pos++] = value.shiftRightUnsigned(56).toInt() & 0xFF; + this.array[this.pos++] = value.shiftRightUnsigned(48).toInt() & 0xFF; + this.array[this.pos++] = value.shiftRightUnsigned(40).toInt() & 0xFF; + this.array[this.pos++] = value.shiftRightUnsigned(32).toInt() & 0xFF; + this.array[this.pos++] = value.shiftRightUnsigned(24).toInt() & 0xFF; + this.array[this.pos++] = value.shiftRightUnsigned(16).toInt() & 0xFF; + this.array[this.pos++] = value.shiftRightUnsigned(8).toInt() & 0xFF; + this.array[this.pos++] = value.toInt() & 0xFF; } writeFloat(value) { - this.writeInt(value * (1 << 24)); + let buffer = new Uint32Array(new Float32Array([value, 0, 0, 0, 0, 0, 0, 0]).buffer); + this.writeInt(buffer[0]); } writeDouble(value) { - this.writeLong(value * (1 << 53)); + let buffer = new Uint32Array(new Float64Array([value, 0, 0, 0, 0, 0, 0, 0]).buffer); + this.writeLong(Long.fromBits(buffer[0], buffer[1])); } writeString(value) { @@ -110,6 +130,10 @@ export default class ByteBuf { this.write(array); } + writeBoolean(value) { + this.writeByte(value ? 1 : 0); + } + writeVarInt(value) { while (true) { if ((value & ~ByteBuf.SEGMENT_BITS) === 0) { @@ -131,6 +155,10 @@ export default class ByteBuf { } } + skipBytes(length) { + this.pos += length; + } + extendIfNeeded(bytes) { if (this.pos + bytes > this.array.length) { let newArray = new Uint8Array(this.array.length + bytes); @@ -169,4 +197,22 @@ export default class ByteBuf { this.writeVarInt(array.length); this.write(array); } + + writeBlockPosition(blockPosition) { + this.writeLong(blockPosition.toLong()); + } + + readBlockPosition() { + return BlockPosition.fromLong(this.readLong()); + } + + readableBytes() { + return this.array.length - this.pos; + } + + toString() { + return Array.from(this.array, byte => { + return ('0' + (byte & 0xFF).toString(16)).slice(-2); + }).join(''); + } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/network/util/CryptManager.js b/src/js/net/minecraft/client/network/util/CryptManager.js index 17e2aca..432877f 100644 --- a/src/js/net/minecraft/client/network/util/CryptManager.js +++ b/src/js/net/minecraft/client/network/util/CryptManager.js @@ -1,7 +1,6 @@ import Random from "../../../util/Random.js"; import ByteBuf from "./ByteBuf.js"; -import {bigIntToBytes, bytesToBigInt, modPow} from "../../../../../../../libraries/modpow.js"; -import {parseAsn1} from "../../../../../../../libraries/asn1.js"; +import {require} from "../../../../../Start.js"; export default class CryptManager { @@ -13,11 +12,12 @@ export default class CryptManager { static encryptRSA(publicKey, data) { // Parse asn1 public key - let asn1 = parseAsn1(new Uint8Array(publicKey)) + let asn1 = require("ASN1").parse(new Uint8Array(publicKey)) + let bigintModArith = require("bigint-mod-arith"); - // Extract n an e of the public key - let n = bytesToBigInt(asn1.children[1].children[0].children[0].value); - let e = bytesToBigInt(asn1.children[1].children[0].children[1].value); + // Extract n and e of the public key + let n = bigintModArith.bytesToBigInt(asn1.children[1].children[0].children[0].value); + let e = bigintModArith.bytesToBigInt(asn1.children[1].children[0].children[1].value); // Check length of public key let length = (n.toString(2).length + 7) >> 3; @@ -44,13 +44,13 @@ export default class CryptManager { let reversed = buffer.getArray().reverse(); // Convert to bigint - let bigInt = bytesToBigInt(reversed); + let bigInt = bigintModArith.bytesToBigInt(reversed); // Encrypt - bigInt = modPow(bigInt, e, n); + bigInt = bigintModArith.modPow(bigInt, e, n); // Convert to bytes - return bigIntToBytes(bigInt); + return bigintModArith.bigIntToBytes(bigInt); } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/render/WorldRenderer.js b/src/js/net/minecraft/client/render/WorldRenderer.js index ea3c0f1..4a00495 100644 --- a/src/js/net/minecraft/client/render/WorldRenderer.js +++ b/src/js/net/minecraft/client/render/WorldRenderer.js @@ -558,7 +558,7 @@ export default class WorldRenderer { let renderDistance = this.minecraft.settings.viewDistance; // Update chunks - for (let [index, chunk] of world.chunks) { + for (let [index, chunk] of world.getChunkProvider().getChunks()) { let distanceX = Math.abs(cameraChunkX - chunk.x); let distanceZ = Math.abs(cameraChunkZ - chunk.z); @@ -599,7 +599,7 @@ export default class WorldRenderer { // TODO Implement chunk unloading //let index = chunk.x + (chunk.z << 16); - //world.chunks.delete(index); + //world.getChunkProvider().getChunks().delete(index); //world.group.remove(chunk.group); } } @@ -637,7 +637,7 @@ export default class WorldRenderer { rebuildAll() { let world = this.minecraft.world; - for (let [index, chunk] of world.chunks) { + for (let [index, chunk] of world.getChunkProvider().getChunks()) { chunk.setModifiedAllSections(); } } diff --git a/src/js/net/minecraft/client/render/entity/EntityRenderManager.js b/src/js/net/minecraft/client/render/entity/EntityRenderManager.js index dd9e0dc..5d62324 100644 --- a/src/js/net/minecraft/client/render/entity/EntityRenderManager.js +++ b/src/js/net/minecraft/client/render/entity/EntityRenderManager.js @@ -1,5 +1,6 @@ import PlayerRenderer from "./entity/PlayerRenderer.js"; import PlayerEntity from "../../entity/PlayerEntity.js"; +import PlayerEntityMultiplayer from "../../entity/PlayerEntityMultiplayer.js"; export default class EntityRenderManager { @@ -8,6 +9,7 @@ export default class EntityRenderManager { this.renderers = []; this.push(PlayerEntity, PlayerRenderer); + this.push(PlayerEntityMultiplayer, PlayerRenderer); } push(entityType, entityRenderer) { @@ -18,6 +20,6 @@ export default class EntityRenderManager { if (!(entity.constructor.name in this.renderers)) { return null; } - return new this.renderers[entity.constructor.name].prototype.constructor(this.worldRenderer); + return new this.renderers[entity.constructor.name]["prototype"]["constructor"](this.worldRenderer); } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/Chunk.js b/src/js/net/minecraft/client/world/Chunk.js index b65827d..694af82 100644 --- a/src/js/net/minecraft/client/world/Chunk.js +++ b/src/js/net/minecraft/client/world/Chunk.js @@ -300,6 +300,34 @@ export default class Chunk { return true; } + fillChunk(data, size, fullChunk) { + let i = 0; + for (let layer = 0; layer < Chunk.SECTION_AMOUNT; layer++) { + if ((size & 1 << layer) !== 0) { + let section = this.getSection(layer); + + for (let k = 0; k < ChunkSection.SIZE * ChunkSection.SIZE * ChunkSection.SIZE; k++) { + let x = k & 15; + let z = (k >> 4) & 15; + let y = (k >> 8) & 15; + + let value = (((data[i] & 0xFF) | (data[i + 1] & 0xFF) << 8)); + let typeId = value >> 4; + let meta = value & 0xF; // TODO handle meta of block + + // TODO support more blocks + if (typeId !== 0 && Block.getById(typeId) === null) { + typeId = 1; + } + + section.setBlockAt(x, y, z, typeId); + + i += 2; + } + } + } + } + getBlockID(x, y, z) { return this.getBlockAt(x, y, z); } @@ -317,7 +345,7 @@ export default class Chunk { } rebuild(renderer) { - for (let y = 0; y < this.sections.length; y++) { + for (let y = 0; y < Chunk.SECTION_AMOUNT; y++) { this.sections[y].rebuild(renderer); } } @@ -331,7 +359,7 @@ export default class Chunk { } setModifiedAllSections() { - for (let y = 0; y < this.sections.length; y++) { + for (let y = 0; y < Chunk.SECTION_AMOUNT; y++) { this.sections[y].isModified = true; } } diff --git a/src/js/net/minecraft/client/world/ChunkSection.js b/src/js/net/minecraft/client/world/ChunkSection.js index 56f85a4..b737b8f 100644 --- a/src/js/net/minecraft/client/world/ChunkSection.js +++ b/src/js/net/minecraft/client/world/ChunkSection.js @@ -66,7 +66,7 @@ export default class ChunkSection { let absoluteZ = this.z * ChunkSection.SIZE + z; let block = Block.getById(typeId); - if (block.isTranslucent() !== isTranslucentRenderPhase) { + if (block === null || block.isTranslucent() !== isTranslucentRenderPhase) { continue; } diff --git a/src/js/net/minecraft/client/world/World.js b/src/js/net/minecraft/client/world/World.js index 6568241..481b45c 100644 --- a/src/js/net/minecraft/client/world/World.js +++ b/src/js/net/minecraft/client/world/World.js @@ -1,5 +1,4 @@ import ChunkSection from "./ChunkSection.js"; -import WorldGenerator from "./generator/WorldGenerator.js"; import MathHelper from "../../util/MathHelper.js"; import BoundingBox from "../../util/BoundingBox.js"; import EnumSkyBlock from "../../util/EnumSkyBlock.js"; @@ -9,13 +8,12 @@ import Vector3 from "../../util/Vector3.js"; import Vector4 from "../../util/Vector4.js"; import MetadataChunkBlock from "../../util/MetadataChunkBlock.js"; import * as THREE from "../../../../../../libraries/three.module.js"; -import Random from "../../util/Random.js"; export default class World { static TOTAL_HEIGHT = ChunkSection.SIZE * 8 - 1; // ChunkSection.SIZE * 16 - 1; - constructor(minecraft, seed) { + constructor(minecraft) { this.minecraft = minecraft; this.entities = []; @@ -23,14 +21,12 @@ export default class World { this.group = new THREE.Object3D(); this.group.matrixAutoUpdate = false; - this.chunks = new Map(); this.lightUpdateQueue = []; + this.chunkProvider = null; this.time = 0; this.spawn = new Vector3(0, 0, 0); - this.setSeed(seed); - // Update lights async let scope = this; setInterval(function () { @@ -42,14 +38,8 @@ export default class World { }, 0); } - setSeed(seed) { - this.seed = seed; - this.generator = new WorldGenerator(this, seed); - this.random = new Random(seed); - } - - getSeed() { - return this.seed; + setChunkProvider(chunkProvider) { + this.chunkProvider = chunkProvider; } onTick() { @@ -67,44 +57,7 @@ export default class World { } getChunkAt(x, z) { - let index = x + (z << 16); - let chunk = this.chunks.get(index); - if (typeof chunk === 'undefined') { - // Generate new chunk - chunk = this.generator.newChunk(this, x, z); - - // Register and mark as loaded - chunk.loaded = true; - this.chunks.set(index, chunk); - - // Populate the chunk - if (!chunk.isTerrainPopulated && this.chunkExists(x + 1, z + 1) && this.chunkExists(x, z + 1) && this.chunkExists(x + 1, z)) { - this.populate(x, z); - } - if (this.chunkExists(x - 1, z) && !this.getChunkAt(x - 1, z).isTerrainPopulated && this.chunkExists(x - 1, z + 1) && this.chunkExists(x, z + 1) && this.chunkExists(x - 1, z)) { - this.populate(x - 1, z); - } - if (this.chunkExists(x, z - 1) && !this.getChunkAt(x, z - 1).isTerrainPopulated && this.chunkExists(x + 1, z - 1) && this.chunkExists(x, z - 1) && this.chunkExists(x + 1, z)) { - this.populate(x, z - 1); - } - if (this.chunkExists(x - 1, z - 1) && !this.getChunkAt(x - 1, z - 1).isTerrainPopulated && this.chunkExists(x - 1, z - 1) && this.chunkExists(x, z - 1) && this.chunkExists(x - 1, z)) { - this.populate(x - 1, z - 1); - } - - // Register in three.js - this.group.add(chunk.group); - } - return chunk; - } - - populate(x, z) { - let chunk = this.getChunkAt(x, z); - if (!chunk.isTerrainPopulated) { - chunk.isTerrainPopulated = true; - - // Populate chunk - this.generator.populateChunk(chunk.x, chunk.z); - } + return this.chunkProvider.getChunkAt(x, z); } getChunkAtBlock(x, y, z) { @@ -207,9 +160,7 @@ export default class World { } chunkExists(chunkX, chunkZ) { - let index = chunkX + (chunkZ << 16); - let chunk = this.chunks.get(index); - return typeof chunk !== 'undefined'; + return this.chunkProvider !== null && this.chunkProvider.chunkExists(chunkX, chunkZ); } neighborLightPropagationChanged(sourceType, x, y, z, level) { @@ -302,7 +253,12 @@ export default class World { isSolidBlockAt(x, y, z) { let typeId = this.getBlockAt(x, y, z); - return typeId !== 0 && Block.getById(typeId).isSolid(); + if (typeId === 0) { + return false; + } + + let block = Block.getById(typeId); + return block !== null && block.isSolid(); } isTranslucentBlockAt(x, y, z) { @@ -604,6 +560,7 @@ export default class World { addEntity(entity) { this.entities.push(entity); + entity.initRenderer(); this.group.add(entity.renderer.group); } @@ -631,25 +588,6 @@ export default class World { this.spawn = new Vector3(x, y + 8, z); } - findSpawn() { - if (this.spawn.y <= 0) { - this.spawn.y = 64; - } - - while (this.getBlockAboveSeaLevel(this.spawn.x, this.spawn.z) === 0) { - this.spawn.x += this.random.nextInt(8) - this.random.nextInt(8); - this.spawn.z += this.random.nextInt(8) - this.random.nextInt(8); - } - } - - getBlockAboveSeaLevel(x, z) { - let y = this.generator.seaLevel; - while (this.getBlockAt(x, y + 1, z) !== 0) { - y++; - } - return this.getBlockAt(x, y, z); - } - loadSpawnChunks() { let viewDistance = this.minecraft.settings.viewDistance; for (let x = -viewDistance; x <= viewDistance; x++) { @@ -660,4 +598,8 @@ export default class World { this.spawn.y = this.getHeightAt(this.spawn.x, this.spawn.z) + 8; } + getChunkProvider() { + return this.chunkProvider; + } + } \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/WorldClient.js b/src/js/net/minecraft/client/world/WorldClient.js new file mode 100644 index 0000000..99b2bb6 --- /dev/null +++ b/src/js/net/minecraft/client/world/WorldClient.js @@ -0,0 +1,12 @@ +import World from "./World.js"; +import ChunkProviderClient from "./provider/ChunkProviderClient.js"; + +export default class WorldClient extends World { + + constructor(minecraft) { + super(minecraft); + + // Set chunk provider to remote chunk loader + this.setChunkProvider(new ChunkProviderClient(this)) + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/block/Block.js b/src/js/net/minecraft/client/world/block/Block.js index c9edf2f..768ba9c 100644 --- a/src/js/net/minecraft/client/world/block/Block.js +++ b/src/js/net/minecraft/client/world/block/Block.js @@ -49,7 +49,12 @@ export default class Block { shouldRenderFace(world, x, y, z, face) { let typeId = world.getBlockAtFace(x, y, z, face); - return typeId === 0 || Block.getById(typeId).isTranslucent(); + if (typeId === 0) { + return true; + } + + let block = Block.getById(typeId); + return block === null || block.isTranslucent(); } getColor(world, x, y, z, face) { @@ -208,7 +213,8 @@ export default class Block { } static getById(typeId) { - return Block.blocks.get(typeId); + let block = Block.blocks.get(typeId); + return typeof block === "undefined" ? null : block; } } diff --git a/src/js/net/minecraft/client/world/block/BlockRegistry.js b/src/js/net/minecraft/client/world/block/BlockRegistry.js index df0eaf9..ce58eff 100644 --- a/src/js/net/minecraft/client/world/block/BlockRegistry.js +++ b/src/js/net/minecraft/client/world/block/BlockRegistry.js @@ -13,6 +13,7 @@ import BlockBedrock from "./type/BlockBedrock.js"; import BlockGlass from "./type/BlockGlass.js"; import SoundGlass from "./sound/SoundGlass.js"; import BlockGravel from "./type/BlockGravel.js"; +import BlockCobblestone from "./type/BlockCobblestone.js"; export class BlockRegistry { @@ -30,6 +31,7 @@ export class BlockRegistry { BlockRegistry.STONE = new BlockStone(1, 0); BlockRegistry.GRASS = new BlockGrass(2, 1); BlockRegistry.DIRT = new BlockDirt(3, 2); + BlockRegistry.COBBLE_STONE = new BlockCobblestone(4, 14); BlockRegistry.WOOD = new BlockWood(5, 10); BlockRegistry.BEDROCK = new BlockBedrock(7, 11); BlockRegistry.GRAVEL = new BlockGravel(13, 13); diff --git a/src/js/net/minecraft/client/world/block/type/BlockCobblestone.js b/src/js/net/minecraft/client/world/block/type/BlockCobblestone.js new file mode 100644 index 0000000..a22d77f --- /dev/null +++ b/src/js/net/minecraft/client/world/block/type/BlockCobblestone.js @@ -0,0 +1,9 @@ +import Block from "../Block.js"; + +export default class BlockCobblestone extends Block { + + constructor(id, textureSlotId) { + super(id, textureSlotId); + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/generator/Generator.js b/src/js/net/minecraft/client/world/generator/Generator.js index 4011e88..174ffe2 100644 --- a/src/js/net/minecraft/client/world/generator/Generator.js +++ b/src/js/net/minecraft/client/world/generator/Generator.js @@ -7,6 +7,8 @@ export default class Generator { this.world = world; this.seed = seed; this.random = new Random(seed); + + this.seaLevel = 64; } generateInChunk(chunkX, chunkZ, primer) { @@ -35,4 +37,12 @@ export default class Generator { let {seedX, seedZ} = this.generateSeedOffset(); this.setSeedOffset(chunkX, chunkZ, seedX, seedZ); } + + getSeed() { + return this.seed; + } + + getSeaLevel() { + return this.seaLevel; + } } \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/generator/WorldGenerator.js b/src/js/net/minecraft/client/world/generator/WorldGenerator.js index 8279e5d..74d8f20 100644 --- a/src/js/net/minecraft/client/world/generator/WorldGenerator.js +++ b/src/js/net/minecraft/client/world/generator/WorldGenerator.js @@ -13,8 +13,6 @@ export default class WorldGenerator extends Generator { constructor(world, seed) { super(world, seed); - this.seaLevel = 64; - this.caveGenerator = new CaveGenerator(world, seed); this.terrainGenerator4 = new NoiseGeneratorOctaves(this.random, 16); diff --git a/src/js/net/minecraft/client/world/provider/ChunkProvider.js b/src/js/net/minecraft/client/world/provider/ChunkProvider.js new file mode 100644 index 0000000..b808bef --- /dev/null +++ b/src/js/net/minecraft/client/world/provider/ChunkProvider.js @@ -0,0 +1,61 @@ +import Chunk from "../Chunk.js"; + +export default class ChunkProvider { + + constructor(world) { + this.world = world; + this.chunks = new Map(); + } + + chunkExists(x, z) { + let index = x + (z << 16); + let chunk = this.chunks.get(index); + return typeof chunk !== 'undefined'; + } + + getChunkAt(x, z) { + let index = x + (z << 16); + let chunk = this.chunks.get(index); + if (typeof chunk === 'undefined') { + chunk = this.loadChunk(x, z); + } + return chunk; + } + + generateChunk(x, z) { + let chunk = new Chunk(this.world, x, z); + chunk.generateSkylightMap(); + chunk.generateBlockLightMap(); + return chunk; + } + + populateChunk(chunk) { + + } + + loadChunk(x, z) { + let index = x + (z << 16); + let chunk = this.generateChunk(x, z) + + // Register and mark as loaded + chunk.loaded = true; + this.chunks.set(index, chunk); + + this.populateChunk(chunk); + + // Register in three.js + this.world.group.add(chunk.group); + + return chunk; + } + + unloadChunk(x, z) { + let index = x + (z << 16); + this.chunks.delete(index); + } + + getChunks() { + return this.chunks; + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/provider/ChunkProviderClient.js b/src/js/net/minecraft/client/world/provider/ChunkProviderClient.js new file mode 100644 index 0000000..fbe72cd --- /dev/null +++ b/src/js/net/minecraft/client/world/provider/ChunkProviderClient.js @@ -0,0 +1,20 @@ +import ChunkProvider from "./ChunkProvider.js"; +import Chunk from "../Chunk.js"; + +export default class ChunkProviderClient extends ChunkProvider { + + constructor(world) { + super(world); + + this.emptyChunk = new Chunk(world, 0, 0); + this.emptyChunk.generateSkylightMap(); + this.emptyChunk.generateBlockLightMap(); + } + + getChunkAt(x, z) { + let index = x + (z << 16); + let chunk = this.chunks.get(index); + return typeof chunk === 'undefined' ? this.emptyChunk : chunk; + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/provider/ChunkProviderGenerate.js b/src/js/net/minecraft/client/world/provider/ChunkProviderGenerate.js new file mode 100644 index 0000000..50632b7 --- /dev/null +++ b/src/js/net/minecraft/client/world/provider/ChunkProviderGenerate.js @@ -0,0 +1,67 @@ +import ChunkProvider from "./ChunkProvider.js"; +import WorldGenerator from "../generator/WorldGenerator.js"; +import Random from "../../../util/Random.js"; + +export default class ChunkProviderGenerate extends ChunkProvider { + + constructor(world, seed) { + super(world); + + this.generator = new WorldGenerator(world, seed); + } + + generateChunk(x, z) { + return this.generator.newChunk(this.world, x, z); + } + + populateChunk(chunk) { + let x = chunk.x; + let z = chunk.z; + + // Populate the chunk + if (!chunk.isTerrainPopulated && this.chunkExists(x + 1, z + 1) && this.chunkExists(x, z + 1) && this.chunkExists(x + 1, z)) { + this._populateChunkAt(x, z); + } + if (this.chunkExists(x - 1, z) && !this.getChunkAt(x - 1, z).isTerrainPopulated && this.chunkExists(x - 1, z + 1) && this.chunkExists(x, z + 1) && this.chunkExists(x - 1, z)) { + this._populateChunkAt(x - 1, z); + } + if (this.chunkExists(x, z - 1) && !this.getChunkAt(x, z - 1).isTerrainPopulated && this.chunkExists(x + 1, z - 1) && this.chunkExists(x, z - 1) && this.chunkExists(x + 1, z)) { + this._populateChunkAt(x, z - 1); + } + if (this.chunkExists(x - 1, z - 1) && !this.getChunkAt(x - 1, z - 1).isTerrainPopulated && this.chunkExists(x - 1, z - 1) && this.chunkExists(x, z - 1) && this.chunkExists(x - 1, z)) { + this._populateChunkAt(x - 1, z - 1); + } + } + + _populateChunkAt(x, z) { + let chunk = this.getChunkAt(x, z); + if (!chunk.isTerrainPopulated) { + chunk.isTerrainPopulated = true; + + // Populate chunk + this.generator.populateChunk(chunk.x, chunk.z); + } + } + + findSpawn() { + let spawn = this.world.spawn; + if (spawn.y <= 0) { + spawn.y = 64; + } + + let random = new Random(this.generator.getSeed()); + while (this.getBlockAboveSeaLevel(spawn.x, spawn.z) === 0) { + spawn.x += random.nextInt(8) - random.nextInt(8); + spawn.z += random.nextInt(8) - random.nextInt(8); + } + } + + getBlockAboveSeaLevel(x, z) { + let y = this.generator.getSeaLevel(); + while (this.world.getBlockAt(x, y + 1, z) !== 0) { + y++; + } + return this.world.getBlockAt(x, y, z); + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/util/BlockPosition.js b/src/js/net/minecraft/util/BlockPosition.js new file mode 100644 index 0000000..209d99d --- /dev/null +++ b/src/js/net/minecraft/util/BlockPosition.js @@ -0,0 +1,59 @@ +import Vector3 from "./Vector3.js"; +import Long from "../../../../../libraries/long.js"; + +export default class BlockPosition extends Vector3 { + + static NUM_X_BITS = 26; + static NUM_Z_BITS = BlockPosition.NUM_X_BITS; + static NUM_Y_BITS = 64 - BlockPosition.NUM_X_BITS - BlockPosition.NUM_Z_BITS; + + static Y_SHIFT = BlockPosition.NUM_Z_BITS; + static X_SHIFT = BlockPosition.Y_SHIFT + BlockPosition.NUM_Y_BITS; + + static X_MASK = Long.fromNumber(1).shiftLeft(BlockPosition.NUM_X_BITS).subtract(1); + static Y_MASK = Long.fromNumber(1).shiftLeft(BlockPosition.NUM_Y_BITS).subtract(1); + static Z_MASK = Long.fromNumber(1).shiftLeft(BlockPosition.NUM_Z_BITS).subtract(1); + + constructor(x, y, z) { + super(Math.floor(x), Math.floor(y), Math.floor(z)); + } + + getX() { + return Math.floor(this.x); + } + + getY() { + return Math.floor(this.y); + } + + getZ() { + return Math.floor(this.z); + } + + getChunkX() { + return this.getX() >> 4; + } + + getChunkY() { + return this.getY() >> 4; + } + + getChunkZ() { + return this.getZ() >> 4; + } + + static fromLong(serialized) { + let x = serialized.shiftLeft(64 - BlockPosition.X_SHIFT - BlockPosition.NUM_X_BITS).shiftRight(64 - BlockPosition.NUM_X_BITS).toNumber(); + let y = serialized.shiftLeft(64 - BlockPosition.Y_SHIFT - BlockPosition.NUM_Y_BITS).shiftRight(64 - BlockPosition.NUM_Y_BITS).toNumber(); + let z = serialized.shiftLeft(64 - BlockPosition.NUM_Z_BITS).shiftRight(64 - BlockPosition.NUM_Z_BITS).toNumber(); + return new BlockPosition(x, y, z); + } + + toLong() { + return Long.fromNumber(this.getX()).and(BlockPosition.X_MASK).shiftLeft(BlockPosition.X_SHIFT) + .or(Long.fromNumber(this.getY()).and(BlockPosition.Y_MASK).shiftLeft(BlockPosition.Y_SHIFT)) + .or(Long.fromNumber(this.getZ()).and(BlockPosition.Z_MASK).shiftLeft(0)); + } + + +} \ No newline at end of file diff --git a/src/js/net/minecraft/util/ChatLine.js b/src/js/net/minecraft/util/ChatLine.js index c9f7ab3..93180ea 100644 --- a/src/js/net/minecraft/util/ChatLine.js +++ b/src/js/net/minecraft/util/ChatLine.js @@ -1,6 +1,11 @@ export default class ChatLine { + constructor(message) { this.message = message; this.updateCounter = 0; } + + getMessage() { + return this.message; + } } \ No newline at end of file diff --git a/src/js/net/minecraft/util/GameProfile.js b/src/js/net/minecraft/util/GameProfile.js index c35770b..16d24a3 100644 --- a/src/js/net/minecraft/util/GameProfile.js +++ b/src/js/net/minecraft/util/GameProfile.js @@ -5,4 +5,16 @@ export default class GameProfile { this.uuid = uuid; } + getCompactUUID() { + return this.uuid.toString().replace(/-/g, ""); + } + + getId() { + return this.uuid; + } + + getUsername() { + return this.username; + } + } \ No newline at end of file diff --git a/src/js/net/minecraft/util/MetadataChunkBlock.js b/src/js/net/minecraft/util/MetadataChunkBlock.js index 5b6ed7f..8ecb244 100644 --- a/src/js/net/minecraft/util/MetadataChunkBlock.js +++ b/src/js/net/minecraft/util/MetadataChunkBlock.js @@ -43,7 +43,7 @@ export default class MetadataChunkBlock { let newLevel = 0; let typeId = world.getBlockAt(x, y, z); let block = Block.getById(typeId); - let opacity = typeId === 0 ? 0 : Math.round(block.getOpacity() * 255); + let opacity = block === null || typeId === 0 ? 0 : Math.round(block.getOpacity() * 255); if (opacity === 0) { opacity = 1; diff --git a/src/js/net/minecraft/util/Session.js b/src/js/net/minecraft/util/Session.js new file mode 100644 index 0000000..69a28a4 --- /dev/null +++ b/src/js/net/minecraft/util/Session.js @@ -0,0 +1,16 @@ +export default class Session { + + constructor(profile, accessToken) { + this.profile = profile; + this.accessToken = accessToken; + } + + getProfile() { + return this.profile; + } + + getAccessToken() { + return this.accessToken; + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/util/UUID.js b/src/js/net/minecraft/util/UUID.js index 9d9252c..bdbe5d8 100644 --- a/src/js/net/minecraft/util/UUID.js +++ b/src/js/net/minecraft/util/UUID.js @@ -3,15 +3,7 @@ import Random from "./Random.js"; export default class UUID { - constructor(data) { - let msb = Long.fromNumber(0); - let lsb = Long.fromNumber(0); - for (let i = 0; i < 8; i++) { - msb = msb.shiftLeft(8).or(Long.fromInt(data[i] & 0xff)); - } - for (let i = 8; i < 16; i++) { - lsb = lsb.shiftLeft(8).or(Long.fromInt(data[i] & 0xff)); - } + constructor(msb, lsb) { this.mostSigBits = msb; this.leastSigBits = lsb; } @@ -33,7 +25,7 @@ export default class UUID { randomBytes[6] |= 0x40; /* set to version 4 */ randomBytes[8] &= 0x3f; /* clear variant */ randomBytes[8] |= 0x80; /* set to IETF variant */ - return new UUID(randomBytes); + return UUID.fromBytes(randomBytes); } static digits(val, digits) { @@ -41,4 +33,39 @@ export default class UUID { let num = hi.or(val.and(hi.add(Long.fromInt(-1)))); return num.toString(16).substr(1); } + + static fromString(string) { + let components = string.split("-"); + if (components.length !== 5) { + throw new Error("Invalid UUID string: " + string); + } + + for (let i = 0; i < 5; i++) { + components[i] = parseInt(components[i], 16); + } + + let mostSigBits = Long.fromNumber(components[0]); + mostSigBits = mostSigBits.shiftLeft(16); + mostSigBits = mostSigBits.or(Long.fromNumber(components[1])); + mostSigBits = mostSigBits.shiftLeft(16); + mostSigBits = mostSigBits.or(Long.fromNumber(components[2])); + + let leastSigBits = Long.fromInt(components[3]); + leastSigBits = leastSigBits.shiftLeft(48); + leastSigBits = leastSigBits.or(Long.fromNumber(components[4])); + + return new UUID(mostSigBits, leastSigBits); + } + + static fromBytes(data) { + let msb = Long.fromNumber(0); + let lsb = Long.fromNumber(0); + for (let i = 0; i < 8; i++) { + msb = msb.shiftLeft(8).or(Long.fromInt(data[i] & 0xff)); + } + for (let i = 8; i < 16; i++) { + lsb = lsb.shiftLeft(8).or(Long.fromInt(data[i] & 0xff)); + } + return new UUID(msb, lsb); + } } \ No newline at end of file diff --git a/src/resources/terrain/terrain.png b/src/resources/terrain/terrain.png index 33634fb9b8292a2bb780322e55d208bcefb3baea..ff8d6bdc48bd457644d9f9fc4c52255d9691334b 100644 GIT binary patch delta 7838 zcmV;P9%13FL+3`2RVO0+Nkl%1yVb3&Wl6RrS-WIgmgTWC zc9DD>f080qc&L!XVY5AU#%2s2yh@fWOO_=|vMgEF+LC(T)ZMSU-^=fFo~~$nBtg-C zq-xIhBi*{|IrrRi&hLAEzjJTD)VII=?NU0Owz07>OQlkl$z*J1X2zzcr>#&ZSXEV( zH8(eh^Xv}|4OvZ1ja5`sgzFm`8mz9a&MsWIe-N&zuCBIxK5soeJt41LE*GvPKi1^U zx>zjQ@#Dv>rS~-(7@Y`da;X_>Y;3gtp%I&yn6TD{S{oU=V3Wmk$iJbk##-uYY@#q_ zBNNl%zWx8KG^hLb{m)Tbv#iB(nF?#lmh8#b&e?|R+HCov7W?V1579V!GreO-~RUE5wkn* zywjd~>Z!0jx&Ddg^HXNi`J$C3%qp^Gf5mjsvZaho`th_sQ#ze8o0_)iii}m1e6wg? z*JdUE_wavsU|UVL-gBz4-hmU=+SKgZp3pK@C{9`~%{lfbtuQm?&t72_>5^qL4K{5P z*0tc@dQN{l|KnR&eNWU26=!Vu z{ANpgEGLZ)vvIkDj!%eS=o?rYihP7lk?8|E(XK4i(YWnzbX{ z7wpEB^DUJzTij4>fBVX5TRPu`$?v`Y?O|KDw82&^TWAmd-9G#CPv2vY{Cc|^Orza> z=cZuH)DNN?T>!+qxRr~4~7m++Tr10d*+#E!nvs%{_pb@6)87v&)c$AG3|Adp0><%#-`jz zve=PVR56nZ>$I1XYyFJ*A$#aA{`St=G}G->)jy z%AI`4`0>+zywm%{pp8yU+S*nBeog}Lz`y%9CE9A*jUJphrWm?{p2S|Px zjmmX^S{BBJtgbR+eS^c++*ohTH91?hpv@ZVYJJqLx4h?9op+0LQE+*-w~igN2^XZc zrutBs$^8fZ%gJ}#|IBv|U%LO`4^Cb2{@2dShV%YR-yvItJv`u*X zWT(B1ZWuFERVi)7RKc%xWB0=|#gtc{871AAoRw15UjH3# z{0)9$(2abt4UY`iw3ln9IO3zddtx@`$IBi(9Xmf3|nf z`rHc~aPP0Xr@uVtOcuOvy7%T{Y;OF;nWB4r;}>(WP+u573qrv1`Okm;()x}&?g;%g zG5*9Of0&K;jZ35d?=$`cMqvgVYywXJBc;53eSKjB->_jrxQ6XBXU>Gte*gacq5T=% z>7Vo`ygZ(zvJ;fKi@8Q2;u0qFlH(fZEVN zT*LJggoT2#ARhDQ&ksh$aSBam#puYJ3Lsx9e+rS|KAg)`)!CVz0o$~4sWs6NPVQ26L8XB9-{SZGs9`fVfL;tlriYNCU{%>cjtG&s(|ME>c^`8y} zL8g^k_t!o!sQ%8$E`0%en*(>uoTfAg}Wqgf+j=N7?qVst^ ze@A=D^|R%M1()igp{_D4P3}F^xsWwC)`b1~+Pu|PXRW3x6ZVrb?0Vx|$O91C1Ue7T za^S!L`_{LV+kpHHmj?ubzSl2_Vjtr)#b#q zr7rqC3@DdtF6?}DzdgO<729**4cq?8UVCBJtG@rTJ^s`)wsZGuetus#_ws88Z0DYR z_Uz8xwtN4fa4qeSkQn+efB8#$|NGw`7GV9@V~<@55il-7ghVhBF#g$|!0$7De+1$) zx2;IgPZDU;&d$zYJ9ue?r5O8l*IgI-7=47}zyQerOBnjX3onFi24O~a zAdYh_3+@m`l!ycAFl_aDetg5EghP+IR+Z$}e4{PL`ikna7v2>3BSo^l_*nDdapEuYdA-G0+<%m{&b*bw3aAK5QVO}NOJjd*?6 zRn~f&`>1atHZ?P^=U@;bY9^ZKxCyJ@}MwDtzucH?@xeaq$$(*p+m z{qKK2EQCrJJ+S5)+XfAZy_ooIL3jgfxi z#*JZcV!npK@%7hV5BFcWa-}U@x-^8M*wCg;n}U!t!ZR;{Jt8F73)gcUo-BD`d)O!Q z2NVl&nJd8q;Mp;DR0hRauwX$@7u1SQM}g>Ys1ruc!Z`{-N9O@~cnZkqhG9@p&L^V4 zh(?Fyp>SOg2-3`Jf3CNsoh{bUQg6r4_E|%7t97)uzC#d(28XO~yyzoS%7)#T55C=P zoeLL-3cO+2qDunO)mCr)-#=j+S1mWDjFz|8dfeV&D;BodwOtEr^oM6{xnH}Wy~V1l zDs5hCvt1aSw5}yxAusA-%8hemXv}(C1mEtSv;iNDE8U2xe}k#Ql%41s4&@#h84cIp zxT2$c&V`aS&rAq_t3Na{Vdr~?g3C`xkO(Gg>WF;cBOm#Qefra%4%>hmnT%M}Bb|%M zpI`jq7onW6b?a8^?(PmF`wT{(E?Oz;7Ve{aD(x}9yZ?T~jdd#6x}Ua+DfbMf{hm`U z48G*OYZquRg*!0{3H2=X@1lN=2mf9#%P zm#$A@ybpZf17U(E0U@SdF@C_sg(shU(%ReGZQAvKQ9YGSxpDjbT>#SV5>(8j+*rL6 z&t&oTeVq5}m`L+M(2MF`K#C~FI?-ci&UIVX z^BtYIU}G0J@0}7?!@aVrQJPv@e=6C^zV-8IhMQ);M|KZz8FFTX3t4OJt6{r@5>rLhJLwo z?+y)U14efYpR&_-7(WB)OE0|?j27EK6d3)v29E);K#T|hn9IR7$cO6@e*{E>^Yk0y zeQXk&B7Zyvik5gZb0Lk3fy<9EVh|WRH$|;5dJ0BkFxpTkZcJy#CE++7ltR;xC=&a4 z0;FS~<8<)EK-eZN0&wEQ2{*QL_S;>r`=~U2X+B`b{-f^@gpmub?CzFUy9=Iox0Ony zlIz_d=bHZ2BLi07&>YHEe<)4aaPLSkxG5J5$~@>JJp&!(wsby}dr{|nyKYHqNW(Qe zWAc2|3EM2=69t?0pq%?rN93Da&vozHwmQUz)D=Ptw86(d_A&d)SH2SV-+1GVP+ynh zM?dpyyQ=5&94|sifSdd1zvR3PD+E9DF)mPV9uCm%k>=CP~Z1B2VKryaN=6}r#Fae>@>Mo(KcRM0kMvpPa)3@b(A-g~5jlo!O)O?*rSRPh4}&HNoA- zI2quQHl=;>1ek;-B%U{Yh{;6kjC>I+!UWFaF<__kBd$#*gXcn; z=ck}_aKr$k^gtB)wmW{ZfjhXVY@$WovCZwefdH!U`3>~oxLMzeeFxhEX^+Sk5jU;N@1!#2Zww#fT)ck6w( z!XLIcoepyXSbWJ1opiO4+C8+G zwtR()Wi4|R!!Bmi?yVKA$?MzQ1-no<>FX==-9LZPfBx#v{^H`^+QCiHil)(hSeVLZwU1n<^VkQzjgCE+q#ME4Ht7M^NnwmUx!i8xX*|J zYZm(JY!85Q3GX)eKh*evzK{FQgzYP@yb|Ja`VDQ)C{I}uo1(uk;sf?Xzd`INU($b) zvU5Fse}}$6-{Tb`_h3?ouBJ@IId%#yy*DNbyeX*AJR6}R@&0}t-*Mi>>~~F{rVM4t=41q zoGaq6r)z6&2yyMC=QG#zG(tLC4ui7pIuCmMf4_3=x=v z#}B`L*lt+0GIYx29uBako>-F)^+&xnH#JyIb=ghhIOU<7oFgCVmcPw-Tmk72Le}_srKiZvEi|PH}@f{^7g9A z)OddF+}hjk>aPKldb%3aN80PFqt#<(4*@#cTfANf8wsaUmkbAEpw3zwJXC0I4BrcM zw3a7PF__uw>uv^)KWeb#shd=!QUnAjQl;|(y0$XfyisI}fZX2Di;b%y+s3Bu{|(Tn%vBbd?P4lV6kXeWDy=A546IWHq(H#t?bY%Xp4 z_wEm69(NI;YxJDy3FRLg9WNVhW!?@R*ca-hd-Qj&L`g$_7&{YyJ$=JrLhyncQBr4# zKydBLht7s~!Lwnsr*kAAnl$$Ff6qT3_CNmdj|VYII&d~!w7d`P%*oGqD3F_SW0^>K zwBlk))okMl#r(Io}uR8n(e96~+tY^OwKo}6xWSRZdpx^(p#($+b059V^j$^R&dyI4E z&YeMYI87O7H~Ki|e)F5(gmN%vfB{ox`VZ|&pCAuH7{r1>^RBz@3fIzS2o;#~VE|!X zj{eKQK|RftbQl4>Gcd}de`FL2*BoU-VR#q}mXRNoqOceoDhIPWH2Gjigavd`7V^i{ z#c+5y-U`s*948%>v$)eek6IsveI)LiC|7EGQ{ANr!V`UCR#{yW+|;$#b=kFxI;^E8 zXUi9N&N)5Lv2*Vc+qk^VNB=%+X=}07YgUJ6DNMM*`_OrM*lgX>e->MD{k5S%de8NF z*~?!O^4!V7^KhO?KPLb97JzR8@;(Gt=E|cP`6dh3@@53+n4kUZXT$dF4w|@yKnOrR z{`99m4f{|kxcA|%`Ggw`mOhnt5zD))&KSkCg?CkqJA?@C&htee(E%6uKx@O}%p^Cu zNDaD2G+~YI`j>3Ne@FjH%ct9{XW*PQH!Qcov9oUcZiIf1vFTnLDNKg%2Cha@Pgi5d zp4U8{-xXr~$DiG4kN@UbZ;$;R-|w~;UMB9}Yk&X3OE%%+9pe6%ciZ+?UbQEmej$7p z02mGyq+q@BA0j^_CfJs1ax6Zod!{q}ZTzalSDDdjAGhSEn)`BwAKa#g-z%jlQ#IBeWq%X{s zG#w0O#c1h_WX_B<9h&0^1BFL@5Ct9}F<>5+Z45m5T#-DI_=Lif4_TKltH*7!jf6zmh<^w1zBAvh+gJPEN3(_9a zxbt32x#R9(K*{s;?P(vZ3v}&p9H-t#(>cX7D)kx~;YHV4*#%}h7ZvbeQd+xg3 zwr*VKf5%pPoWIU)TDRJ6yJ?gCs}J34_uX@+-M(d$-MV>$-F5p`yLsK3kOz}_jIJz< z_z4&~v;Bxd@*MzP0bP6x3YTy8t)7tugF#9kJmJ z%a?@LdTot0);Buo@#we>PE1>AX3EZ->z#9Y1TSBeb3rN_7 z2@R+Zgn$o$lF^+%?n(L=E+8E$;S-1u?yzpcq56@Lc)5Xjqrw!`h`8-R}12Ae;x z#WH@LyR|jehuFW}*WtiLi1iuOlh>q>d<(#WSH*dj6EZ876!8YmGr(Yo=%=lwhv^nX?kM&#$lYn$Q zIyp0wbVxcm#>>qasd)gxg2aF+82hBd;3*W>lAiNa1Ra=T7(407k99HvhLP9rlg^Z1 ziRJSiWqRd}xnd8rV<}>!CFMa9a5eq_qf6G}cW{tm* zDDv@-n`L;nPu9L>w4e5Ez6&UMyw4v3xnFM;t_pmAUvMvgb}av;-?)1V6E;;b?RA>5 z>a2T%RrCDZL@@qRY0O1o$i1ogHc>3t(Aa=Yq6t?asi&*aKRjmL9@k@C{M90DK)v(k z;_UeWkNMqje8hBTE_^I>f4+avPM>pw87zN@mdxfevNJD|cmNp9yBFRPB;ghH%;I^V zWq%ZS1<63jOg{$6z?@m~nvb*$xJ>fm2_yq4 zX|XFN2I&vv&0i4CmDD%C`OWYU7&7iV1>r`(ac)i+K%sb0u0s?MlR_I6e|QMaaU26> zd-&zQv8rr^)m3>q@2%r}V(Oi<+Wp7I+DebhhpoHc^Qft|v5E3$tP5M}EaNdPe-g~S z=aRms&})_X8XvX$JS-Ts4NKc>U~s^4`HUUwow2Uwv^CT>*u+%97PVH}Q+s=C%{7fa zy8E-b=;Tu64}6mE`q|!ie{K2o;(?PRVU%aG522vGuIsD{qXhfZi_O379Wg&7ZY3}; zKu4hNXTL3A#3Ocm?9{)0p1<+M2jc5Kw{joMecxU0F!ATtX8hQUt3q)m->pdK%Go{)g!AO4SjZKsaDaq;NyV033qoF5K29GktraM|_t>;L?P(AF3) z{U5_mycq0;{?98sSpKq;=V2C~(Ve*~+7;tRAP@=+o7cePgJ;1!0eLf6A{N*WeTD&; z{F#$G{dbdd`fI?{=WhS{r3Vs1#6=K*xhTjFp8m$16iDfnUg`h;rT;zixI&13%#-0D w7n7YHC6o9dowFVvcLK8>A3FgVqQ&h00Y_*_s?(@6u>b%707*qoM6N<$f`v0T(EtDd delta 7414 zcmV%1yVb3&wb_iyzn9kBtfA+ zRF(7nNVo2K&OP^>^ZTCP@7&uj^{sDxtCUWsZG3#(QmK?>G8vnlowb>n87mYDR#jDH z&CSi>Jp04L!&XyMV-*z@;rfP#2CJ*9vkMn4e}rqQtE(-a&s%SAZ^$c`%Y|#nk2QI- zE*6V+;=~DS>3hWn$0kFXTx!-D8yjt4c+@5*C#|)i)<(xK*i{QnZ-0C7h}m6t z-DOWa@kH33y6KnC=BLeO@26{`)t-P%3#_w0K)nN2!Xm&3akWzU>v{|Glu5wv;~uH|iT24Q=RMf6tzJ z{w14jUFq$z%zpK&UxobOOJDj@Sby+?AGEDow}$Q2t5=8hkACzc`{XA-X@?IV4*NTI z?hI@2{+#kQ@f!N$c&yv((Es|5z5e>^ws`SktF5gKwz7BcUR$|xr7c*nz)qh&Z7;w4 zvaMUUF7)Z1o}SPzXzL?Kj#%A^f8`f*yEdf0@|CZYDl02(XlN*OPz(se!9XY|>#3=! zAOvh976<``#X@7Ww6p}{8yg!7=P^jmVF(x_4U14rOiYC9xDWYZq=myzhx}UVtL@ae zzHq!$oV8U8n=S2)-!nX6!xyH!160_WrES*U(rm9DJ7wqkhpgyLRrr@Ke+pM||2MvW zCR9XMYu1i-U$9%&EVNX{Y-vNa{liOVY{fzsCcpQAH%DyaiUwP~afc2yS4;lrhX8`(2;#j-C*n-r;gdDZh2nL|NPOXZQ;%TA_%}IKJkfg?tup$ z2<7>}2R>jAKm2gm{>Vo@e_{_l_+aSJq#YRc4HTtz6)BIbcIx*ST!QYHK=eY~q~N zdL2#p{i<@U+{u@WA3x*AJH1Z~+1TWiZCLy7uSx(O_;>%NL|e_cf6-&q?ad8d4;5Z- zv!PBaTo8J^t<$~+>e6*92*S$;kJv!AEgT1g;K?VSytsFHFhscf?z`=4U;A1x{BYoH z{KMH&>E4BxJ_kQ|=5T56|NNU^Cu`TP4K{?)v!!=K6~fa!!>^jf8T%d>sOWDg|;FTwzai27(Wk_bX*K-aBwhmRvwCDR31jfHEh#( zqobqYflxp?I{C4WVPbd~+Z%7Z5z^DBTnDISVSL!?Dl^tUG-Az-_10XIvz3e5tg)`v zN8NhMdw$hZLn`|p1DyRDQ-+oZQocE-!-hA~T3mC{yB75rK^c0W8@OnLRWG5K>8 zd^B}mBnXAg6fKj^dAWM5l&bdn?{MR9@DoFBUcb+$BE zO|If%V@>%|2>0t++!?%qhQ=myKg5qug#5Vo@ZXk4@#Ov^|MjeOwKrM!U%zgr|I^_h zC>8&^e0_3%&T;>`#+J}Pef@*BZNpM)>S(bCe;@u;D0^i-XG@nYvW%}W&MFz1%k9)Qp$(0O>4 zg9i`VH^2GKIl=1c>I%>I-S2)k>?3Y7?j=>Zfls>IGVSGZJGWe@Ay)#7+z0NI*J~OP zfADi|KHiSLktWP5S!J%>%T{j%51|T07pKYbkS8vGeO;T?Rn@w#_;Y*uyyxn2;@MIc z10DvH%XKyEdij7ox$`C4d+;^e@zOqfZuiT+|Drwi#8b9w&ntd@e>nHzD+g`Y-u?FU zu06Krz~OK$?U0Zd`p+W-WL{NfBlPJ{9-OdV1OV*NCYDRJq})kPNWbnlv2`qh^Gs;B+uX8o+2V65t$$ z%!AM&8F_hl>MRkDbLY;5>p0HCahz*eaECCWL>x$mVKZ8w;k)e?HY~ zRW-FX>}6Wo-e7B%FLsx9!fHIe9lqf4_Y!xre1ssMbKg1YE^5Z!yKSSr`juUC!bN<$ za6wx*H#}ZeeeRE^K$;~BJHj^Yci$b?+p8yst>=7?EpKbG?_x#A8}X5f_1>d>e{*7&kccGDpFA)3<{H*KGi+Gi6xQ(AV~<_jDvK@G z0rBMLKmU2Vndx#&3)9jQ5zohHZOQ+k*>0Ay3#$be{sLQ%B98g zFM610<$Apu-uLI*?7sK@6JKw)KYPa(yKVCZf3_NX&z;+C>!uBM%Z7D!)0*q;oj2bU zmhHFRWVdd((YD`mlijiP<`B~Z2K~M7eJ?D8N*Fz`<{I36j34&z-~X0y!Q1C8#y=m< zUEG{wzIVXrziQR0&`z{Ff9=Ldzj^cKFgP(^!{GSptFMOpuUWIkR;*YNLQ!n!S`$0~ zo*iQ+P9bhyv}jS7yh5$$bSN7gjtM%9oP~4L4IP~an)?3XEuUSSo>u z5d?xX3!3X~MQ4k3wA9;)bN$xP+-e=|t#1*8;h|ycpD6msl(G>wf96APc3bD-rJ(|E zT)AXUK)TxMZQy$+ZS&ey#+1>j_F9kIJ8bphHoLBCk&XS}oUQU}7qz!obycM;Xl=F& zV^h|(tSjV2JxsfCjt-AouZ!TD-BUK`qj9AhF?BFqn6{JsBca@*qhsOvTUK|J&$&>t z=9viraP@~rC+&RSe{gX52?-LxWKA8B4}AE;AGS|@>QiAGa3hlui+ZGUwY9a`&wlo^ zxlXff+cxX&?hYgSEJmL$S}E%m?xTAu?J>W*|9-@cbvoF(pSFr=_Y7wIp3^Q2zT~}Y z7j44NxhPG#H`Lr>%U&?X$)zH&F zZ084t-R<{q!|N}p8`jimhkJ6(^)+FVkg%_%-n|Zgo}#N5CYVgxHU8)#!lWz z{9hbPtYtzn-;$VfQM$0vX-TcApD* zlD=z!i%qsNq{rRAT>2$#jlY*ZmrvQ=-MfQf*5s>w9VATKzT*Sd@6wFHrZ=ud%7*_`HoFqu<;9=PrFCps^ebSl_-BrFD;dJZu`9vbUuh6 zb=%rl7Yv*cbjsV2`pIUQ8yd6T!4d2Cw(s#UjX8p%U&BNp#)ONHOaJzmo@2?|0M0%C z{PQ71SigRKNJBj$0)FpH8b5}9sdH}+4QK;KcMPAh({>m?1L+GdybuH%+dvc;{kaB@ z0kJ@gt~F^~5nO+Mj1hyt*y*gO6-JNI(in_3C>eD^XUDzaI31LSq#^MN*vAtf9s3-o zgC_>UHfa%nlP6EQvGv$*cfabR(!|_+z|I55-XaL27hKuhEvk zvrJ4DY{r9f?nfPwZ*o1?y?6V%5Fb)k2rbYCAN}Y@?aN>Oa@c?Ewbw#@U5X$6@Q3dD zyJwV12O}r!ck{PYMKQbq2;P7jdC}MTOfiH2C67Khmho$f?)Gz8nsC95Etgwhtxat< zIx*<=v9prxICHr&9?sa#hpt-yia`p z%cpnQuXpUW-TMz&Rdtp9<8!<0spogwj@|og=bl%>+`!H~`)%jm{l2}=p5FD6J@dle z;Nr93(qrTa$?-%OFgC&i-2dbp9)Pz;5GV{jTJbZE&|A4`aST6SHRb)*)S9Yu%)HRj&%>%Ki|I0_8mSSuBpmb+M%OoL)kkU%SPPL#!PwH_;(#V z8`4sTJb$ua;FOJp#-@IF=HxtOr8C@m>#d=#k~sk057UVrdE}9>&vCXH&F;D9o^b4| zU;V0m;R{~~+YIyBBJa=Lt@qsuf7s$oI?R6wVDTk4bl$0#@CaN0!sEK(dkHN&W!8JX z&+CWqhIxrm&n{(!sab1nX!p=!#_|;|mbJ`PjJTN1xVKicCa-UI7wkgel&>$(cmDEu z`f1{2jH>)J8ys9 zXxp~1z3E~uWxoFP^6N0_88;tMV9i2*o$mo~F5&G4|Hm3X(D!lwnXrB7rI*5_AN__l zXOyQbiA~X8`1S;_C;H8`CQV1AQ)37y6$^#s0qCey9HTl)hX;U20LD)`)DG|#ctDP! zc8pqdWGaWw&-GLW*RfAtbo}PJs_=iI4{4ifD{aNX)?mC$_K}A8{-)I{tkz@poGa~! zr)z6&2yyL{=X15`X@qpP90q0GbsqBgf6a!Cp+4^Zv$g&VwU%QR-^*Q{ch}mFA9?kN z-MDs5=#;BG9AHg7u_hntk9uuxYOtE>^28IzDG%l39Qjbkyk+^xPks`v0p5QA;Dzy# z2+&zS``OQi*GGv+Fek8Q&z`W2Aog*uK`J>1>Q%JQD?DjS(jaNa^Uot^BGck7YetZNo z7UHWFU5o8h@9NZ)L9}BT>IkTbHDd4n9-ijk$`9(J9f;TefHU~ z|FMsKEQnFkfwSqN<$Y*pPJY%yf!wqk%Vf%<6<_d~-5YRw4`ocbILvr_FzegX z?m3_kmFY^C1j4$!jSdf3byb5+&hP~$o=v^iWy*5KsGo9qVAOx|u*lPS&_EF`OHxl) zA{lJXp6?HJa;~>Ov|WmFhkirY$b?{dz~LK#^Zi3MFgkAM2g`4K=zmP|C13Zkp8rMw zVL(WeW&T%#e*a4v|K;WYyo_%-&Ok=L$M|>c+7(2H)0BaBqmOg$H^2E!CpP{AUxSWZk5$F!A;$8eV1Lgq{CWja<*z| z=T)cYId<(kYMWQJ`RLzoEp08fZvDFOEQLuocpo~?csqY>T+w2yZ@4ZPabHirm%aQo zAqT z{Nu0>rGk4O?wU`!!C>jrc^9#~yXuTlOj~$Y#kfO=;O;zM1QH!^fe*AcGC?P7bdefz zk7&{w-SvMj*`$yDm6lJpS?^$vH8-rXq49HW{BDGPkMWs48!b$Q?*^_!QcqW6=iXO5 zp5Gl}{KuZ&Wsm*lX>X4M9^db==Uyc4-)H~$+zU48;vM4t7x&nXmtMBVpL{NS7XZZm zjPNYU#2*I6l5F!vfCadYIRPdCz1>RlLKw$KK*alu{LI@h zflhzN8{WNpcjzmG3oHm69|#dPTx-&FFq9Rer88o*%t+IrIgT(;c-&V+fd@zon1^K> z17|{mj!GVk@`v4a$j}Uz$o(B_iMY}M5!CpOnHn_i6o1SvI>3HW(*`d?rzjt{2$z8T>;}RE?N)MyU zqBb%zY^P712-`U~oVyz;PDJ!~J<);Q2YmvvYm2&-p|!fsuiUR`}3|K4kBC z&wIl5Lk~SPHy=P*5$Ocp7!2N)1u`2f#sw?WO zK35m!6s|;4Pgi2=P3!F)zVHnI?Q+k%?yzl}H~O)49_MegTQ{z=+i%@s|LTMH+I@fb z++}xc-D2;&d6T{Cj&1gijq5`mOy)7VvM}N&VCc;DBMQlP0C)w6Eh9DQ_@4jm#(pW| zznn)vK74=_U+rlb+j@4fIfz3asFBpLI4WINO=BydEyI0rxODm zH3a9|!>-0wtq6f83MwFXm0?PzfLZ_{YQgx#ymock+uSp^K7Jvn>it{YVG0y+76MmPvjyV7ZOKgCF zEqPteD_X#g7#I`pm*-+|WIl~`GI(4|!XzLak510aBps4Yj`7l|8L5AH0K$UAfDsG! zNr%DH5xJK1oTnn_z#PNaNl$*PlMyhCyndf_ru<4QpZ6%!D{ssV8uyiB1$Q$Ujk12f z<0sG9*hIl&)08dojH-q-}#|Xz6$Snvz|vyZCxlQqvi&8|M?5G zzyIa4q5QQCb$0B;X?uTk*NO0ah#{Z(CZGP|{_`CGu0QM6X0H!lb4~I|a5C~IAA>M@ zQFn}1^SuFJ=J?Z}{k+zX%`%YW%N;oiceO;^l#oo1{$>)v410zZE@8H~SF8h23`c5iB- zO%@9_JU(bsXu=gp>gh@hjEq~i$Mskjf3-*(Q186CICp-~V}3UrA2HpT3m*%e9~iPT zJ-yaDRQ?bxnayWpXI>=n0Fsbk{=!>=B)p=YSv(K4>`wx(AQ=dm>Bq2n#Ydgqa?34Y zA2ClR#km!8IedQ;z-QgG^R*`RjcKnMfy0(elaLlh7UjGV?o z0TB*d8;;YFxeirCG`OC;r~uA!90O&0YcON{l~`IN{`D& ztb4%osHwK`$?|8ci(Bd}<1sCN63o4?CVg+A&nojZK5BpWdsr}Ln^v^h;LxDu@)!3BL2)-6qyYOQ-F)E@aMpP@!`!S>gflM9JuT?_uuy}rYOAs_&vSOrY5Ic z9DnD1^2J}qMV@=IO~1iYpzqMvTGnn0;+aeUCM17z0o3EA&Jz-F{6qio@9gxk*DfCY z9gOa*iSxq&hhy{i7q7g*e*K?6AKDrrr~hO4i5G+2(Es@>K>o6m=h?DlOYjPqyP{pk zp0B*7e+`)W>>Xd5dmtf1Tm%8QmI|cwO0V?SF8v>v#}z{S|BM7ch=`E@2oW(703jkq o0wA*wAU^}M*C0Fr7+TE!AGRXR1d+s&v;Y7A07*qoM6N<$f=h5H{{R30