Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- //*********************************//
- // Native Feature extensions //
- //*********************************//
- Array.fromArguments = (function (slice) {
- return function (args) {
- return slice.call(args);
- };
- }(Array.prototype.slice));
- Array.isArray = (function (toString) {
- return function (arr) {
- return "[object Array]" === toString.call(arr);
- };
- }(Object.prototype.toString));
- Array.prototype.forEach = function (callback, thisArg) {
- var index = 0, length = this.length;
- for (; index < length; index += 1) {
- callback.call(thisArg, this[index], index, this);
- }
- };
- Array.prototype.find = function (callback, thisArg) {
- var index = 0, length = this.length;
- for (; index < length; index += 1) {
- if (callback.call(thisArg, this[index], index, this)) {
- return this[index];
- }
- }
- };
- Array.prototype.reduce = function (callback, value) {
- var index = 0, length = this.length;
- if (len > 0) {
- if (arguments.length < 2) {
- value = this[0];
- index = 1;
- }
- for (; index < length; index += 1) {
- value = callback(value, this[index], index, this);
- }
- }
- return value;
- };
- Object.hasOwnProperty = (function (hasOwnProperty) {
- return function (obj, prop) {
- return hasOwnProperty.call(obj, prop)
- };
- }(Object.prototype.hasOwnProperty));
- Object.type = (function (toString) {
- return function (input) {
- if (input === undefined) {
- return 'undefined';
- } else if (input === null) {
- return 'null';
- } else {
- var res = toString.call(input).replace(/^\[(?:object )?(.+)\]$/, '$1');
- if (typeof input === 'unknown' && res.toLowerCase() === 'Object') {
- return 'unknown';
- } else if (/ /.test(res)) {
- return res;
- } else {
- return res.toLowerCase();
- }
- }
- }
- }(Object.prototype.toString));
- Object.keys = function (obj) {
- var keys = [], prop;
- for (prop in obj) {
- if (Object.hasOwnProperty(obj, prop)) {
- keys.push(prop);
- }
- }
- return keys;
- };
- Function.prototype.bind = (function (argumentsToArray, concat) {
- return function () {
- var self = this, bindArgs = argumentsToArray(arguments.length ? arguments : [undefined]), thisArg = bindArgs.shift();
- return function () {
- self.apply(thisArg, concat.call(bindArgs, argumentsToArray(arguments)));
- };
- };
- }(Array.fromArguments, Array.prototype.concat));
- JSON = {
- stringify: function (value) {
- var type = Object.type(value);
- if (value === null) {
- return 'null';
- }
- if (type === 'boolean') {
- return value.toString();
- }
- if (type === 'number') {
- return isFinite(value) ? value.toString() : 'null';
- }
- if (type === 'string') {
- return '"' + value.replace(/[\\"\u0000-\u001F\u2028\u2029]/g, function(chr) {
- return {'"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'}[chr] || '\\u' + (chr.charCodeAt(0) + 0x10000).toString(16).substr(1);
- }) + '"';
- }
- if (type === 'array') {
- return '[' + value.reduce(function (val, item) {
- item = JSON.stringify(item);
- if (item) {
- return val + (val ? ',' : '') + item;
- }
- }, '') + ']';
- }
- if (type !== 'function' && type !== 'undefined' && type !== 'unknown') {
- return '{' + Object.keys(value).reduce(function (val, item) {
- var res = JSON.stringify(value[key]);
- if (res) {
- return val + (val ? ',' : '') + JSON.stringify(key) + ':' + val;
- }
- }, '') + '}';
- }
- },
- parse: function (value) {
- value = String(value).replace(/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, function(chr) {
- return '\\u' + ('0000' + chr.charCodeAt(0).toString(16)).slice(-4);
- });
- if (!/^[\],:{}\s]*$/.test(value.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
- throw new Error("INVALID_JSON");
- }
- try {
- return eval('(' + value + ')');
- } catch (e) {
- throw new Error("INVALID_JSON");
- }
- }
- };
- //**************************//
- // Binary Array Class //
- //**************************//
- BinaryArray = function (fromCodepageMap, toCodepageMap) {
- function BinaryArray(input) {
- var output = [], type = Object.type(input), index = 0, charCode, nextCharCode, length, stream;
- output.__proto__ = BinaryArray.prototype;
- if (type === 'array') {
- input.forEach(function (item, idx) {
- if (Object.type(item) !== 'number' || item < 0 || parseInt(item, 10) !== item || item > 255) {
- throw TypeError("array must consist of only unsigned 8bit integer values");
- }
- output[idx] = item;
- });
- } else if (type === 'string' || type === 'unknown') {
- stream = new ActiveXObject("ADODB.Stream");
- stream.mode = 3;
- stream.type = 1;
- stream.open();
- if (typeof input === 'string') {
- stream.charSet = 'utf-8';
- stream.type = 2;
- stream.writeText(input);
- } else {
- try {
- stream.write(input);
- } catch (ignore) {
- throw new TypeError("Cannot convert unknown to BinaryArray");
- }
- }
- stream.position = 0;
- stream.type = 2;
- stream.charSet = '437';
- input = stream.readText();
- stream.close();
- for (length = input.length; index < length; index += 1) {
- charCode = input.charCodeAt(index);
- if (charCode > 127) {
- charCode = fromCodepageMap[charCode];
- }
- output.push(charCode);
- }
- } else if (input !== undefined && input !== null) {
- throw new TypeError("Cannot convert " + type + " to BinaryArray");
- }
- return output;
- }
- BinaryArray.prototype = new Array;
- BinaryArray.toString = function () {
- var output, stream = new ActiveXObject("ADODB.Stream");
- stream.mode = 3;
- stream.type = 2;
- stream.charSet = '437';
- stream.open();
- this.forEach(function (octet) {
- octet > 127 ? toCodepageMap[octet] : octet;
- stream.writeText(String.fromCharCode(octet));
- });
- stream.position = 0;
- stream.charSet = 'utf-8';
- output = stream.readText();
- stream.close();
- return output;
- };
- BinaryArray.toSafearray = function () {
- var output, stream = new ActiveXObject("ADODB.Stream");
- stream.mode = 3;
- stream.type = 2;
- stream.charSet = '437';
- stream.open();
- this.forEach(function (octet) {
- octet > 127 ? toCodepageMap[octet] : octet;
- stream.writeText(String.fromCharCode(octet));
- });
- stream.position = 0;
- stream.type = 1;
- output = stream.read();
- stream.close();
- return output;
- };
- }({
- '199': 128, '252': 129, '233': 130, '226': 131, '228': 132, '224': 133, '229': 134, '231': 135, '234': 136, '235': 137,
- '232': 138, '239': 139, '238': 140, '236': 141, '196': 142, '197': 143, '201': 144, '230': 145, '198': 146, '244': 147,
- '246': 148, '242': 149, '251': 150, '249': 151, '255': 152, '214': 153, '220': 154, '162': 155, '163': 156, '165': 157,
- '8359':158, '402': 159, '225': 160, '237': 161, '243': 162, '250': 163, '241': 164, '209': 165, '170': 166, '186': 167,
- '191': 168, '8976':169, '172': 170, '189': 171, '188': 172, '161': 173, '171': 174, '187': 175, '9617':176, '9618':177,
- '9619':178, '9474':179, '9508':180, '9569':181, '9570':182, '9558':183, '9557':184, '9571':185, '9553':186, '9559':187,
- '9565':188, '9564':189, '9563':190, '9488':191, '9492':192, '9524':193, '9516':194, '9500':195, '9472':196, '9532':197,
- '9566':198, '9567':199, '9562':200, '9556':201, '9577':202, '9574':203, '9568':204, '9552':205, '9580':206, '9575':207,
- '9576':208, '9572':209, '9573':210, '9561':211, '9560':212, '9554':213, '9555':214, '9579':215, '9578':216, '9496':217,
- '9484':218, '9608':219, '9604':220, '9612':221, '9616':222, '9600':223, '945': 224, '223': 225, '915': 226, '960': 227,
- '931': 228, '963': 229, '181': 230, '964': 231, '934': 232, '920': 233, '937': 234, '948': 235, '8734':236, '966': 237,
- '949': 238, '8745':239, '8801':240, '177': 241, '8805':242, '8804':243, '8992':244, '8993':245, '247': 246, '8776':247,
- '176': 248, '8729':249, '183': 250, '8730':251, '8319':252, '178': 253, '9632':254, '160': 255
- },{
- '128': 199,'129': 252,'130': 233,'131': 226,'132': 228,'133': 224,'134': 229,'135': 231,'136': 234,'137': 235,
- '138': 232,'139': 239,'140': 238,'141': 236,'142': 196,'143': 197,'144': 201,'145': 230,'146': 198,'147': 244,
- '148': 246,'149': 242,'150': 251,'151': 249,'152': 255,'153': 214,'154': 220,'155': 162,'156': 163,'157': 165,
- '158': 8359,'159': 402,'160': 225,'161': 237,'162': 243,'163': 250,'164': 241,'165': 209,'166': 170,'167': 186,
- '168': 191,'169': 8976,'170': 172,'171': 189,'172': 188,'173': 161,'174': 171,'175': 187,'176': 9617,'177': 9618,
- '178': 9619,'179': 9474,'180': 9508,'181': 9569,'182': 9570,'183': 9558,'184': 9557,'185': 9571,'186': 9553,'187': 9559,
- '188': 9565,'189': 9564,'190': 9563,'191': 9488,'192': 9492,'193': 9524,'194': 9516,'195': 9500,'196': 9472,'197': 9532,
- '198': 9566,'199': 9567,'200': 9562,'201': 9556,'202': 9577,'203': 9574,'204': 9568,'205': 9552,'206': 9580,'207': 9575,
- '208': 9576,'209': 9572,'210': 9573,'211': 9561,'212': 9560,'213': 9554,'214': 9555,'215': 9579,'216': 9578,'217': 9496,
- '218': 9484,'219': 9608,'220': 9604,'221': 9612,'222': 9616,'223': 9600,'224': 945,'225': 223,'226': 915,'227': 960,
- '228': 931,'229': 963,'230': 181,'231': 964,'232': 934,'233': 920,'234': 937,'235': 948,'236': 8734,'237': 966,
- '238': 949,'239': 8745,'240': 8801,'241': 177,'242': 8805,'243': 8804,'244': 8992,'245': 8993,'246': 247,'247': 8776,
- '248': 176,'249': 8729,'250': 183,'251': 8730,'252': 8319,'253': 178,'254': 9632,'255': 160
- });
- //**************************//
- // GZIP Decompression //
- //**************************//
- gunzip = (function () {
- var ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
- var LENGTHCODETABLE = [0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000d, 0x000f, 0x0011, 0x0013, 0x0017, 0x001b, 0x001f, 0x0023, 0x002b, 0x0033, 0x003b, 0x0043, 0x0053, 0x0063, 0x0073, 0x0083, 0x00a3, 0x00c3, 0x00e3, 0x0102, 0x0102, 0x0102];
- var LENGTHEXTRATABLE = [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, 0, 0];
- var DISTCODETABLE = [0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0007, 0x0009, 0x000d, 0x0011, 0x0019, 0x0021, 0x0031, 0x0041, 0x0061, 0x0081, 0x00c1, 0x0101, 0x0181, 0x0201, 0x0301, 0x0401, 0x0601, 0x0801, 0x0c01, 0x1001, 0x1801, 0x2001, 0x3001, 0x4001, 0x6001];
- var DISTEXTRATABLE = [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 FIXEDLITERALLENGTHTABLE = [[459008,524368,524304,524568,459024,524400,524336,590016,459016,524384,524320,589984,524288,524416,524352,590048,459012,524376,524312,589968,459028,524408,524344,590032,459020,524392,524328,590000,524296,524424,524360,590064,459010,524372,524308,524572,459026,524404,524340,590024,459018,524388,524324,589992,524292,524420,524356,590056,459014,524380,524316,589976,459030,524412,524348,590040,459022,524396,524332,590008,524300,524428,524364,590072,459009,524370,524306,524570,459025,524402,524338,590020,459017,524386,524322,589988,524290,524418,524354,590052,459013,524378,524314,589972,459029,524410,524346,590036,459021,524394,524330,590004,524298,524426,524362,590068,459011,524374,524310,524574,459027,524406,524342,590028,459019,524390,524326,589996,524294,524422,524358,590060,459015,524382,524318,589980,459031,524414,524350,590044,459023,524398,524334,590012,524302,524430,524366,590076,459008,524369,524305,524569,459024,524401,524337,590018,459016,524385,524321,589986,524289,524417,524353,590050,459012,524377,524313,589970,459028,524409,524345,590034,459020,524393,524329,590002,524297,524425,524361,590066,459010,524373,524309,524573,459026,524405,524341,590026,459018,524389,524325,589994,524293,524421,524357,590058,459014,524381,524317,589978,459030,524413,524349,590042,459022,524397,524333,590010,524301,524429,524365,590074,459009,524371,524307,524571,459025,524403,524339,590022,459017,524387,524323,589990,524291,524419,524355,590054,459013,524379,524315,589974,459029,524411,524347,590038,459021,524395,524331,590006,524299,524427,524363,590070,459011,524375,524311,524575,459027,524407,524343,590030,459019,524391,524327,589998,524295,524423,524359,590062,459015,524383,524319,589982,459031,524415,524351,590046,459023,524399,524335,590014,524303,524431,524367,590078,459008,524368,524304,524568,459024,524400,524336,590017,459016,524384,524320,589985,524288,524416,524352,590049,459012,524376,524312,589969,459028,524408,524344,590033,459020,524392,524328,590001,524296,524424,524360,590065,459010,524372,524308,524572,459026,524404,524340,590025,459018,524388,524324,589993,524292,524420,524356,590057,459014,524380,524316,589977,459030,524412,524348,590041,459022,524396,524332,590009,524300,524428,524364,590073,459009,524370,524306,524570,459025,524402,524338,590021,459017,524386,524322,589989,524290,524418,524354,590053,459013,524378,524314,589973,459029,524410,524346,590037,459021,524394,524330,590005,524298,524426,524362,590069,459011,524374,524310,524574,459027,524406,524342,590029,459019,524390,524326,589997,524294,524422,524358,590061,459015,524382,524318,589981,459031,524414,524350,590045,459023,524398,524334,590013,524302,524430,524366,590077,459008,524369,524305,524569,459024,524401,524337,590019,459016,524385,524321,589987,524289,524417,524353,590051,459012,524377,524313,589971,459028,524409,524345,590035,459020,524393,524329,590003,524297,524425,524361,590067,459010,524373,524309,524573,459026,524405,524341,590027,459018,524389,524325,589995,524293,524421,524357,590059,459014,524381,524317,589979,459030,524413,524349,590043,459022,524397,524333,590011,524301,524429,524365,590075,459009,524371,524307,524571,459025,524403,524339,590023,459017,524387,524323,589991,524291,524419,524355,590055,459013,524379,524315,589975,459029,524411,524347,590039,459021,524395,524331,590007,524299,524427,524363,590071,459011,524375,524311,524575,459027,524407,524343,590031,459019,524391,524327,589999,524295,524423,524359,590063,459015,524383,524319,589983,459031,524415,524351,590047,459023,524399,524335,590015,524303,524431,524367,590079],9,7]
- var FIXEDISTANCETABLE = [[327680,327696,327688,327704,327684,327700,327692,327708,327682,327698,327690,327706,327686,327702,327694,null,327681,327697,327689,327705,327685,327701,327693,327709,327683,327699,327691,327707,327687,327703,327695,null],5,5];
- var CRC32TABLE = [0,1996959894,3993919788,2567524794,124634137,1886057615,3915621685,2657392035,249268274,2044508324,3772115230,2547177864,162941995,2125561021,3887607047,2428444049,498536548,1789927666,4089016648,2227061214,450548861,1843258603,4107580753,2211677639,325883990,1684777152,4251122042,2321926636,335633487,1661365465,4195302755,2366115317,997073096,1281953886,3579855332,2724688242,1006888145,1258607687,3524101629,2768942443,901097722,1119000684,3686517206,2898065728,853044451,1172266101,3705015759,2882616665,651767980,1373503546,3369554304,3218104598,565507253,1454621731,3485111705,3099436303,671266974,1594198024,3322730930,2970347812,795835527,1483230225,3244367275,3060149565,1994146192,31158534,2563907772,4023717930,1907459465,112637215,2680153253,3904427059,2013776290,251722036,2517215374,3775830040,2137656763,141376813,2439277719,3865271297,1802195444,476864866,2238001368,4066508878,1812370925,453092731,2181625025,4111451223,1706088902,314042704,2344532202,4240017532,1658658271,366619977,2362670323,4224994405,1303535960,984961486,2747007092,3569037538,1256170817,1037604311,2765210733,3554079995,1131014506,879679996,2909243462,3663771856,1141124467,855842277,2852801631,3708648649,1342533948,654459306,3188396048,3373015174,1466479909,544179635,3110523913,3462522015,1591671054,702138776,2966460450,3352799412,1504918807,783551873,3082640443,3233442989,3988292384,2596254646,62317068,1957810842,3939845945,2647816111,81470997,1943803523,3814918930,2489596804,225274430,2053790376,3826175755,2466906013,167816743,2097651377,4027552580,2265490386,503444072,1762050814,4150417245,2154129355,426522225,1852507879,4275313526,2312317920,282753626,1742555852,4189708143,2394877945,397917763,1622183637,3604390888,2714866558,953729732,1340076626,3518719985,2797360999,1068828381,1219638859,3624741850,2936675148,906185462,1090812512,3747672003,2825379669,829329135,1181335161,3412177804,3160834842,628085408,1382605366,3423369109,3138078467,570562233,1426400815,3317316542,2998733608,733239954,1555261956,3268935591,3050360625,752459403,1541320221,2607071920,3965973030,1969922972,40735498,2617837225,3943577151,1913087877,83908371,2512341634,3803740692,2075208622,213261112,2463272603,3855990285,2094854071,198958881,2262029012,4057260610,1759359992,534414190,2176718541,4139329115,1873836001,414664567,2282248934,4279200368,1711684554,285281116,2405801727,4167216745,1634467795,376229701,2685067896,3608007406,1308918612,956543938,2808555105,3495958263,1231636301,1047427035,2932959818,3654703836,1088359270,936918000,2847714899,3736837829,1202900863,817233897,3183342108,3401237130,1404277552,615818150,3134207493,3453421203,1423857449,601450431,3009837614,3294710456,1567103746,711928724,3020668471,3272380065,1510334235,755167117];
- function buildHuffmanTable(lengths) {
- var listSize = lengths.length,
- maxCodeLength = 0,
- minCodeLength = Number.POSITIVE_INFINITY,
- size, table, bitLength, code, skip, reversed, rtemp, i, il, j, value;
- for (i = 0, il = listSize; i < il; ++i) {
- if (lengths[i] > maxCodeLength) {
- maxCodeLength = lengths[i];
- }
- if (lengths[i] < minCodeLength) {
- minCodeLength = lengths[i];
- }
- }
- size = 1 << maxCodeLength;
- table = new Array(size);
- for (bitLength = 1, code = 0, skip = 2; bitLength <= maxCodeLength;) {
- for (i = 0; i < listSize; ++i) {
- if (lengths[i] === bitLength) {
- for (reversed = 0, rtemp = code, j = 0; j < bitLength; ++j) {
- reversed = (reversed << 1) | (rtemp & 1);
- rtemp >>= 1;
- }
- value = (bitLength << 16) | i;
- for (j = reversed; j < size; j += skip) {
- table[j] = value;
- }
- ++code;
- }
- }
- ++bitLength;
- code <<= 1;
- skip <<= 1;
- }
- return [table, maxCodeLength, minCodeLength];
- }
- function calcCRC32(data, pos, length) {
- var i = (typeof pos === 'number') ? pos : (pos = 0),
- il = (typeof length === 'number') ? length : data.length,
- crc = -1;
- for (i = il & 7; i--; ++pos) {
- crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos]) & 0xff];
- }
- for (i = il >> 3; i--; pos += 8) {
- crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos ]) & 0xff];
- crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 1]) & 0xff];
- crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 2]) & 0xff];
- crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 3]) & 0xff];
- crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 4]) & 0xff];
- crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 5]) & 0xff];
- crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 6]) & 0xff];
- crc = (crc >>> 8) ^ CRC32TABLE[(crc ^ data[pos + 7]) & 0xff];
- }
- return (crc ^ 4294967295) >>> 0;
- }
- function RawInflate(input, options) {
- this.buffer;
- this.blocks = [];
- this.bufferSize = options.bufferSize || 32768;
- this.totalpos = 0;
- this.ip = options.index;
- this.bitsbuf = 0;
- this.bitsbuflen = 0;
- this.input = input;
- this.output = new Array(this.bufferSize);
- this.op = 0;
- this.bfinal = false;
- this.bufferType = 1;
- this.resize = false;
- };
- RawInflate.prototype.readBits = function(length) {
- var bitsbuf = this.bitsbuf,
- bitsbuflen = this.bitsbuflen,
- input = this.input,
- ip = this.ip,
- inputLength = input.length,
- octet;
- if (ip + ((length - bitsbuflen + 7) >> 3) >= inputLength) {
- throw new Error('input buffer is broken');
- }
- while (bitsbuflen < length) {
- bitsbuf |= input[ip++] << bitsbuflen;
- bitsbuflen += 8;
- }
- octet = bitsbuf & ((1 << length) - 1);
- bitsbuf >>>= length;
- bitsbuflen -= length;
- this.bitsbuf = bitsbuf;
- this.bitsbuflen = bitsbuflen;
- this.ip = ip;
- return octet;
- };
- RawInflate.prototype.readCodeByTable = function(table) {
- var bitsbuf = this.bitsbuf,
- bitsbuflen = this.bitsbuflen,
- input = this.input,
- ip = this.ip,
- inputLength = input.length,
- codeTable = table[0],
- maxCodeLength = table[1],
- codeWithLength, codeLength;
- while (bitsbuflen < maxCodeLength) {
- if (ip >= inputLength) {
- break;
- }
- bitsbuf |= input[ip++] << bitsbuflen;
- bitsbuflen += 8;
- }
- codeWithLength = codeTable[bitsbuf & ((1 << maxCodeLength) - 1)];
- codeLength = codeWithLength >>> 16;
- if (codeLength > bitsbuflen) {
- throw new Error('invalid code length: ' + codeLength);
- }
- this.bitsbuf = bitsbuf >> codeLength;
- this.bitsbuflen = bitsbuflen - codeLength;
- this.ip = ip;
- return codeWithLength & 0xffff;
- };
- RawInflate.prototype.decodeHuffmanAdaptive = function(litlen, dist) {
- var output = this.output,
- op = this.op,
- olength = output.length,
- lengthCodeTable = LENGTHCODETABLE,
- lengthExtraTable = LENGTHEXTRATABLE,
- distCodeTable = DISTCODETABLE,
- distExtraTable = DISTEXTRATABLE,
- code, ti, codeDist, codeLength;
- this.currentLitlenTable = litlen;
- while ((code = this.readCodeByTable(litlen)) !== 256) {
- if (code < 256) {
- if (op >= olength) {
- output = this.expandBufferAdaptive();
- olength = output.length;
- }
- output[op++] = code;
- continue;
- }
- ti = code - 257;
- codeLength = lengthCodeTable[ti];
- if (lengthExtraTable[ti] > 0) {
- codeLength += this.readBits(lengthExtraTable[ti]);
- }
- code = this.readCodeByTable(dist);
- codeDist = distCodeTable[code];
- if (distExtraTable[code] > 0) {
- codeDist += this.readBits(distExtraTable[code]);
- }
- if (op + codeLength > olength) {
- output = this.expandBufferAdaptive();
- olength = output.length;
- }
- while (codeLength--) {
- output[op] = output[(op++) - codeDist];
- }
- }
- while (this.bitsbuflen >= 8) {
- this.bitsbuflen -= 8;
- this.ip--;
- }
- this.op = op;
- };
- RawInflate.prototype.expandBufferAdaptive = function(opt_param) {
- var ratio = (this.input.length / this.ip + 1) | 0,
- input = this.input,
- output = this.output,
- maxHuffCode, newSize, maxInflateSize;
- if (opt_param) {
- if (typeof opt_param.fixRatio === 'number') {
- ratio = opt_param.fixRatio;
- }
- if (typeof opt_param.addRatio === 'number') {
- ratio += opt_param.addRatio;
- }
- }
- if (ratio < 2) {
- maxHuffCode = (input.length - this.ip) / this.currentLitlenTable[2];
- maxInflateSize = (maxHuffCode / 2 * 258) | 0;
- newSize = maxInflateSize < output.length ?
- output.length + maxInflateSize :
- output.length << 1;
- } else {
- newSize = output.length * ratio;
- }
- return this.output = output;
- };
- RawInflate.prototype.decompress = function () {
- var input = this.input,
- inputLength = this.input.length,
- ip = this.ip,
- output = this.output,
- olength = this.output.length,
- op = this.op,
- hdr, len, nlen, hlit, hdist, hclen, codeLengths, codeLengthsTable, litlenTable, distTable, lengthTable, code, prev, repeat, i, il;
- while (!this.bfinal) {
- hdr = this.readBits(3);
- if (hdr & 0x1) {
- this.bfinal = true;
- }
- hdr >>>= 1;
- switch (hdr) {
- case 0: // uncompressed
- this.bitsbuf = 0;
- this.bitsbuflen = 0;
- if (ip + 1 >= inputLength) {
- throw new Error('invalid uncompressed block header: LEN');
- }
- len = input[ip++] | (input[ip++] << 8);
- if (ip + 1 >= inputLength) {
- throw new Error('invalid uncompressed block header: NLEN');
- }
- nlen = input[ip++] | (input[ip++] << 8);
- if (len === ~nlen) {
- throw new Error('invalid uncompressed block header: length verify');
- }
- if (ip + len > input.length) { throw new Error('input buffer is broken'); }
- while (op + len > output.length) {
- output = this.expandBufferAdaptive({fixRatio: 2});
- }
- while (len--) {
- output[op++] = input[ip++];
- }
- this.ip = ip;
- this.op = op;
- this.output = output;
- break;
- case 1: // fixed huffman
- this.decodeHuffmanAdaptive(FIXEDLITERALLENGTHTABLE, FIXEDDISTANCETABLE);
- break;
- case 2: // dynamic huffman
- hlit = this.readBits(5) + 257;
- hdist = this.readBits(5) + 1;
- hclen = this.readBits(4) + 4;
- codeLengths = new Array(ORDER.length);
- for (i = 0; i < hclen; ++i) {
- codeLengths[ORDER[i]] = this.readBits(3);
- }
- for (i = hclen, hclen = codeLengths.length; i < hclen; ++i) {
- codeLengths[ORDER[i]] = 0;
- }
- codeLengthsTable = buildHuffmanTable(codeLengths);
- lengthTable = new Array(hlit + hdist);
- for (i = 0, il = hlit + hdist; i < il;) {
- code = this.readCodeByTable(codeLengthsTable);
- switch (code) {
- case 16:
- repeat = 3 + this.readBits(2);
- while (repeat--) { lengthTable[i++] = prev; }
- break;
- case 17:
- repeat = 3 + this.readBits(3);
- while (repeat--) { lengthTable[i++] = 0; }
- prev = 0;
- break;
- case 18:
- repeat = 11 + this.readBits(7);
- while (repeat--) { lengthTable[i++] = 0; }
- prev = 0;
- break;
- default:
- lengthTable[i++] = code;
- prev = code;
- break;
- }
- }
- litlenTable = buildHuffmanTable(lengthTable.slice(0, hlit));
- distTable = buildHuffmanTable(lengthTable.slice(hlit));
- this.decodeHuffmanAdaptive(litlenTable, distTable);
- break;
- default:
- throw new Error('unknown BTYPE: ' + hdr);
- }
- }
- if (this.output.length > op) {
- this.output.length = op;
- }
- return this.buffer = this.output;
- };
- return function gunzip(input) {
- input = new BinaryArray(input);
- var members = [], len = input.length, index = 0, flags, isize, inflated, data;
- while (index < len) {
- if (input[index++] !== 0x1f || input[index++] !== 0x8b) {
- throw new Error('invalid signature');
- }
- if (input[index++] !== 8) {
- throw new Error("unknown compression method: " + input[index]);
- }
- flags = input[index++];
- index += 6;
- if ((flags & 4) > 0) {
- index += input[index++] | (input[index++] << 8);
- }
- if ((flags & 8) > 0) {
- index += 1;
- while (input[index] > 0) {
- index += 1;
- }
- }
- if (0 < (flags & 16)) {
- index += 1;
- while (input[index] > 0) {
- index += 1;
- }
- }
- if (0 < (flags & 2)) {
- if ((calcCRC32(input, 0, index) & 65535) !== (input[index++] | input[index++] << 8)) {
- throw new Error("invalid header crc16");
- }
- }
- isize = input[input.length - 4] | input[input.length - 3] << 8 | input[input.length - 2] << 16 | input[input.length - 1] << 24;
- inflated = new RawInflate(input, {index: index, bufferSize: input.length - index - 8 < 512 * isize ? isize : undefined});
- data = inflated.decompress();
- index = inflated.ip;
- crc = (input[index++] | input[index++]<<8 | input[index++]<<16 | input[index++]<<24) >>> 0;
- if (calcCRC32(data) !== crc) {
- throw new Error("invalid CRC-32 checksum");
- }
- isize = (input[index++] | input[index++]<<8 | input[index++]<<16 | input[index++] << 24) >>> 0;
- if ((data.length & 4294967295) !== isize) {
- throw new Error("invalid input size");
- }
- members.concat(data);
- }
- return new BinaryArray(members);
- };
- }());
- //**************************//
- // HTTP Request Class //
- //**************************//
- HttpRequest = (function (xhr) {
- if (!(xhr = xhr.find(function (testXHR, idx) {
- try {
- new ActiveXObject(testXHR);
- return true;
- } catch (e) {}
- }))) {
- return function () {
- throw new Error("HTTP Requester not found");
- }
- }
- function forceHttpInit(self) {
- if (self.reasyState !== 0) {
- throw new Error("HTTP not initializing");
- }
- return self;
- }
- function forceHttpDone(self) {
- if (self.readyState !== 4) {
- throw new Error("HTTP request not completed");
- }
- return self;
- }
- function prepareHttp(async) {
- var setDefHeaders = this.requestData ? !0 : !1,
- typeHeaderSet = false,
- lenHeadrSert = false;
- this.request.open(this.requestMethod, this.requestUrl, async);
- this.requestHeaders.forEach(function (header) {
- var name = header[0];
- this.request.setRequestHeader(name, header[2]);
- if (name === "content-type") {
- typeHeaderSet = true;
- }
- if (name === "content-length") {
- lenHeaderSet = true;
- }
- });
- if (setDefHeaders) {
- if (!typeHeaderSet) {
- this.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- }
- if (!lenHeaderSet) {
- this.setRequestHeader("Content-Length", this.requestData.length);
- }
- }
- }
- function onReadyStateChange() {
- var err, cb = this.callback || function(){};
- this.readyState = this.request.readyState;
- if (this.readyState === 4) {
- try {
- this.responseStatusCode = this.request.status;
- this.responseStatusText = this.request.statusText;
- this.responseHeaders = this.request.getAllResponseHeaders().split('\r\n');
- this.responseBody = new BinaryArray(this.request.responseBody);
- this.getResponseHeader("content-encoding").find(function (item) {
- if (/\x20*gzip\x20*/.test(item)) {
- this.responseBody = gunzip(this.responseBody);
- return true;
- }
- });
- this.responseText = this.responseBody.toString();
- this.request.onreadystatechange = null;
- delete this.request;
- } catch (e) {
- err = e;
- }
- cb(err);
- }
- }
- function HttpRequest(method, url, wait, async, timeout) {
- this.wait = wait || false;
- this.async = async || false;
- this.timeout = timeout || 0;
- this.error = null;
- this.readyState = 0;
- this.requestUrl = url;
- this.requestMethod = method || "GET";
- this.requestHeaders = [];
- this.requestData = null;
- this.responseStatusCode = 0;
- this.responseStatusText = '';
- this.responseHeaders = [];
- this.responseBody = null;
- this.responseText = '';
- if (wait !== true) {
- this.fetch();
- }
- }
- HttpRequest.REQUESTER = xhr;
- HttpRequest.prototype.getAsync = function () {
- return this.async;
- };
- HttpRequest.prototype.setAsync = function (state) {
- forceHttpInit(this).async = state;
- return this;
- };
- HttpRequest.prototype.getTimeout = function () {
- return this.timeout;
- };
- HttpRequest.prototype.setTimeout = function (timeout) {
- forceHttpInit(this).timeout = (timeout === infinity || isNaN(timeout)) ? 0 : parseInt(timeout, 10);
- };
- HttpRequest.prototype.getReadyState = function () {
- return this.readyState;
- };
- HttpRequest.prototype.getRequestUrl = function () {
- return this.requestUrl;
- }
- HttpRequest.prototype.getRequestMethod = function () {
- return this.requestMethod;
- };
- HttpRequest.prototype.setRequestMethod = function (method) {
- forceHttpInit(this).requestMethod = method;
- };
- HttpRequest.prototype.getRequestHeaders = function () {
- return this.requestHeaders;
- };
- HttpRequest.prototype.addRequestHeader = function (name, value) {
- forceHttpInit(this).requestHeaders.push([name, value]);
- };
- HttpRequest.prototype.getRequestData = function () {
- return this.requestData;
- };
- HttpRequest.prototype.setRequestData = function (data) {
- forceHttpInit(this);
- if (data === null && data === undefined) {
- this.requestData = null;
- }
- forceHttpInit(this).requestData = new BinaryArray(data);
- };
- HttpRequest.prototype.getResponseStatusCode = function () {
- return forceHttpDone(this).responseStatusCode
- };
- HttpRequest.prototype.getResponseStatusCode = function () {
- return forceHttpDone(this).responseStatusText
- };
- HttpRequest.prototype.getResponseHeaders = function () {
- return forceHttpDone(this).responseHeaders.join('\r\n');
- };
- HttpRequest.prototype.getResponseHeader = function (name, index) {
- var matchName = name.toLowerCase().replace(/(?:^\x20+)|(\x20+$)/g),
- headers = forceHttpDone(this).responseHeaders.reduce(function (val, header) {
- header = header.split(/\s*:\s*/);
- var hName = String(header.shift()).toLowerCase();
- if (hName === matchName) {
- val.push(val);
- }
- return val;
- }, []);
- if (!headers.length) {
- return [];
- }
- if (typeof index === 'number') {
- if (index < headers.length) {
- return [headers[index]];
- }
- }
- return headers;
- };
- HttpRequest.prototype.getResponseHead = function () {
- return this.getResponseStatusCode() + ' ' + this.getResponseStatusText() + '\r\n' + this.getResponseHeaders();
- };
- HttpRequest.prototype.getResponseBody = function () {
- return forceHttpDone(this).responseBody;
- };
- HttpRequest.prototype.getResponseText = function () {
- forceHttpDone(this);
- if (this.responseBody === null) {
- return '';
- }
- return this.responseBody.toString();
- };
- HttpRequest.prototype.getResponse = function () {
- return this.getResponseHead() + '\r\n' + this.getResponseText;
- };
- HttpRequest.prototype.fetch = function (callback) {
- var timeout;
- this.callback = callback || function () {};
- this.request = new ActiveXObject(HttpRequest.REQUESTER);
- this.request.setTimeouts(Infinity, Infinity, Infinity, Infinity);
- if (!this.timeout || this.timeout < 1) {
- prepaseHttp.call(this, this.async);
- if (this.async) {
- this.request.onreadystatechange = onreadystatechange.bind(this);
- this.request.send(this.requestData ? this.requestData.toSafearray() : null);
- } else {
- this.request.send(this.requestData ? this.requestData.toSafearray() : null);
- onreadystatechange.call(this);
- }
- } else if (this.ascync === false) {
- timeout = (new Date()).getTime() = this.timeout;
- prepareHttp.call(this, true);
- this.request.send(this.requestData ? this.requestData.toSafearray() : null);
- do {
- if (this.request.readyState === 4) {
- break;
- }
- if (timeout <= (new Date()).getTime()) {
- this.readyState = 4;
- this.error = "HTTP_TIMEOUT"
- throw new Error("HTTP_TIMEOUT");
- }
- this.request.waitForResponse(1);
- } while (1);
- onreadystatechange.call(this);
- } else {
- // TO-DO: async w/ timeout
- }
- };
- return HttpRequest;
- }(['MSXML2.SERVERXMLHTTP.6.0', 'MSXML2.SERVERXMLHTTP.3.0', 'MSXML2.SERVERXMLHTTP']));
- //***************************//
- // JSON Instance Class //
- //***************************//
- // todo
- //*******************************//
- // HTML/XML Instance Class //
- //*******************************//
- // todo
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement