js-crc32/crc32.flow.js

128 lines
4.0 KiB
JavaScript
Raw Normal View History

/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
2015-05-06 21:47:18 +00:00
/* vim: set ts=2: */
/*exported CRC32 */
2018-01-17 21:27:59 +00:00
/*:: declare var DO_NOT_EXPORT_CRC:?boolean; */
/*:: declare function define(cb:()=>any):void; */
var CRC32/*:CRC32Module*/;
(function (factory/*:(a:any)=>void*/)/*:void*/ {
/*jshint ignore:start */
2018-01-17 21:27:59 +00:00
/*eslint-disable */
2015-05-06 22:27:03 +00:00
if(typeof DO_NOT_EXPORT_CRC === 'undefined') {
if('object' === typeof exports) {
factory(exports);
} else if ('function' === typeof define && define.amd) {
define(function () {
2018-01-17 21:27:59 +00:00
var module/*:CRC32Module*/ = /*::(*/{}/*:: :any)*/;
2015-05-06 22:27:03 +00:00
factory(module);
return module;
});
} else {
2018-01-17 21:27:59 +00:00
factory(CRC32 = /*::(*/{}/*:: :any)*/);
2015-05-06 22:27:03 +00:00
}
} else {
2018-01-17 21:27:59 +00:00
factory(CRC32 = /*::(*/{}/*:: :any)*/);
2015-05-06 22:27:03 +00:00
}
2018-01-17 21:27:59 +00:00
/*eslint-enable */
/*jshint ignore:end */
2018-01-17 21:27:59 +00:00
}(function(CRC32/*:CRC32Module*/) {
CRC32.version = '1.2.0';
2015-05-06 21:47:18 +00:00
/*::
type CRC32Type = number;
2018-01-17 21:27:59 +00:00
type ABuf = Array<number> | Buffer | Uint8Array;
2015-05-06 21:47:18 +00:00
type CRC32TableType = Array<number> | Int32Array;
*/
/* see perf/crc32table.js */
/*global Int32Array */
2015-05-06 21:47:18 +00:00
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) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
table[n] = c;
}
return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
}
var T = signed_crc_table();
/*# charCodeAt is the best approach for binary strings */
function crc32_bstr(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 1;
2015-05-06 21:47:18 +00:00
for(var i = 0; i < L;) {
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
2015-05-06 21:47:18 +00:00
}
if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF];
return C ^ -1;
2015-05-06 21:47:18 +00:00
}
function crc32_buf(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
if(buf.length > 10000) return crc32_buf_8(buf, seed);
var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 3;
for(var i = 0; i < L;) {
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
2015-05-06 21:47:18 +00:00
}
while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
return C ^ -1;
2015-05-06 21:47:18 +00:00
}
function crc32_buf_8(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 7;
for(var i = 0; i < L;) {
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
2015-05-06 21:47:18 +00:00
}
while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
return C ^ -1;
2015-05-06 21:47:18 +00:00
}
/*# much much faster to intertwine utf8 and crc */
function crc32_str(str/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
var C = seed/*:: ? 0 : 0 */ ^ -1;
for(var i = 0, L=str.length, c, d; i < L;) {
2015-05-06 21:47:18 +00:00
c = str.charCodeAt(i++);
if(c < 0x80) {
C = (C>>>8) ^ T[(C ^ c)&0xFF];
2015-05-06 21:47:18 +00:00
} else if(c < 0x800) {
C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
2015-05-06 21:47:18 +00:00
} else if(c >= 0xD800 && c < 0xE000) {
c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
2015-05-06 21:47:18 +00:00
} else {
C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
2015-05-06 21:47:18 +00:00
}
}
return C ^ -1;
2015-05-06 21:47:18 +00:00
}
CRC32.table = T;
2018-01-17 21:27:59 +00:00
// $FlowIgnore
2015-05-06 21:47:18 +00:00
CRC32.bstr = crc32_bstr;
2018-01-17 21:27:59 +00:00
// $FlowIgnore
2015-05-06 21:47:18 +00:00
CRC32.buf = crc32_buf;
2018-01-17 21:27:59 +00:00
// $FlowIgnore
2015-05-06 21:47:18 +00:00
CRC32.str = crc32_str;
2015-05-06 22:27:03 +00:00
}));