js-crc32/crc32c.flow.js

123 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-01-25 05:48:10 +00:00
/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
2022-01-24 08:14:46 +00:00
/* vim: set ts=2: */
2022-01-25 05:48:10 +00:00
/*exported CRC32C */
2022-01-24 08:14:46 +00:00
/*:: declare var DO_NOT_EXPORT_CRC:?boolean; */
/*:: declare function define(cb:()=>any):void; */
2022-01-25 05:48:10 +00:00
var CRC32C/*:CRC32Module*/;
2022-01-24 08:14:46 +00:00
(function (factory/*:(a:any)=>void*/)/*:void*/ {
/*jshint ignore:start */
/*eslint-disable */
if(typeof DO_NOT_EXPORT_CRC === 'undefined') {
if('object' === typeof exports) {
factory(exports);
} else if ('function' === typeof define && define.amd) {
define(function () {
var module/*:CRC32Module*/ = /*::(*/{}/*:: :any)*/;
factory(module);
return module;
});
} else {
2022-01-25 05:48:10 +00:00
factory(CRC32C = /*::(*/{}/*:: :any)*/);
2022-01-24 08:14:46 +00:00
}
} else {
2022-01-25 05:48:10 +00:00
factory(CRC32C = /*::(*/{}/*:: :any)*/);
2022-01-24 08:14:46 +00:00
}
/*eslint-enable */
/*jshint ignore:end */
2022-01-25 05:48:10 +00:00
}(function(CRC32C/*:CRC32Module*/) {
2022-04-04 22:53:34 +00:00
CRC32C.version = '1.2.2';
2022-01-24 08:14:46 +00:00
/*::
type CRC32Type = number;
type ABuf = Array<number> | Buffer | Uint8Array;
type CRC32TableType = Array<number> | Int32Array;
*/
/*global Int32Array */
function signed_crc_table()/*:CRC32TableType*/ {
var c = 0, table/*:Array<number>*/ = new Array(256);
for(var n =0; n != 256; ++n){
c = n;
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
table[n] = c;
}
return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
}
2022-01-25 05:48:10 +00:00
var T0 = signed_crc_table();
function slice_by_16_tables(T) {
var c = 0, v = 0, n = 0, table/*:Array<number>*/ = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ;
for(n = 0; n != 256; ++n) table[n] = T[n];
for(n = 0; n != 256; ++n) {
v = T[n];
for(c = 256 + n; c < 4096; c += 256) v = table[c] = (v >>> 8) ^ T[v & 0xFF];
}
var out = [];
for(n = 1; n != 16; ++n) out[n - 1] = typeof Int32Array !== 'undefined' ? table.subarray(n * 256, n * 256 + 256) : table.slice(n * 256, n * 256 + 256);
return out;
}
2022-01-25 05:48:10 +00:00
var TT = slice_by_16_tables(T0);
var T1 = TT[0], T2 = TT[1], T3 = TT[2], T4 = TT[3], T5 = TT[4];
var T6 = TT[5], T7 = TT[6], T8 = TT[7], T9 = TT[8], Ta = TT[9];
var Tb = TT[10], Tc = TT[11], Td = TT[12], Te = TT[13], Tf = TT[14];
2022-01-24 08:14:46 +00:00
function crc32_bstr(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
2022-01-25 05:48:10 +00:00
var C = seed/*:: ? 0 : 0 */ ^ -1;
for(var i = 0, L = bstr.length; i < L;) C = (C>>>8) ^ T0[(C^bstr.charCodeAt(i++))&0xFF];
return ~C;
}
2022-01-24 08:14:46 +00:00
2022-01-25 05:48:10 +00:00
function crc32_buf(B/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
var C = seed/*:: ? 0 : 0 */ ^ -1, L = B.length - 15, i = 0;
for(; i < L;) C =
Tf[B[i++] ^ (C & 255)] ^
Te[B[i++] ^ ((C >> 8) & 255)] ^
Td[B[i++] ^ ((C >> 16) & 255)] ^
Tc[B[i++] ^ (C >>> 24)] ^
Tb[B[i++]] ^ Ta[B[i++]] ^ T9[B[i++]] ^ T8[B[i++]] ^
T7[B[i++]] ^ T6[B[i++]] ^ T5[B[i++]] ^ T4[B[i++]] ^
T3[B[i++]] ^ T2[B[i++]] ^ T1[B[i++]] ^ T0[B[i++]];
L += 15;
2022-01-25 05:48:10 +00:00
while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF];
return ~C;
2022-01-24 08:14:46 +00:00
}
function crc32_str(str/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
var C = seed/*:: ? 0 : 0 */ ^ -1;
for(var i = 0, L = str.length, c = 0, d = 0; i < L;) {
2022-01-24 08:14:46 +00:00
c = str.charCodeAt(i++);
if(c < 0x80) {
2022-01-25 05:48:10 +00:00
C = (C>>>8) ^ T0[(C^c)&0xFF];
2022-01-24 08:14:46 +00:00
} else if(c < 0x800) {
2022-01-25 05:48:10 +00:00
C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF];
C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF];
2022-01-24 08:14:46 +00:00
} else if(c >= 0xD800 && c < 0xE000) {
c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
2022-01-25 05:48:10 +00:00
C = (C>>>8) ^ T0[(C ^ (240|((c>>8)&7)))&0xFF];
C = (C>>>8) ^ T0[(C ^ (128|((c>>2)&63)))&0xFF];
C = (C>>>8) ^ T0[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
C = (C>>>8) ^ T0[(C ^ (128|(d&63)))&0xFF];
2022-01-24 08:14:46 +00:00
} else {
2022-01-25 05:48:10 +00:00
C = (C>>>8) ^ T0[(C ^ (224|((c>>12)&15)))&0xFF];
C = (C>>>8) ^ T0[(C ^ (128|((c>>6)&63)))&0xFF];
C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF];
2022-01-24 08:14:46 +00:00
}
}
2022-01-25 05:48:10 +00:00
return ~C;
2022-01-24 08:14:46 +00:00
}
2022-01-25 05:48:10 +00:00
CRC32C.table = T0;
2022-01-24 08:14:46 +00:00
// $FlowIgnore
2022-01-25 05:48:10 +00:00
CRC32C.bstr = crc32_bstr;
2022-01-24 08:14:46 +00:00
// $FlowIgnore
2022-01-25 05:48:10 +00:00
CRC32C.buf = crc32_buf;
2022-01-24 08:14:46 +00:00
// $FlowIgnore
2022-01-25 05:48:10 +00:00
CRC32C.str = crc32_str;
2022-01-24 08:14:46 +00:00
}));