diff --git a/Makefile b/Makefile index eb26e01..14d5128 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ clean: clean-baseline ## Remove targets and build artifacts rm -f $(TARGET) $(FLOWTARGET) crc32c.flow.js: crc32.flow.js - cat $^ | sed 's/-306674912/-2097792136/g' > $@ + cat $^ | sed 's/-306674912/-2097792136/g; s/CRC32\([ \/\.]\)/CRC32C\1/g' > $@ ## Testing diff --git a/README.md b/README.md index 38445de..082127a 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,57 @@ With [npm](https://www.npmjs.org/package/crc-32): $ npm install crc-32 ``` -In the browser: +When installed globally, npm installs a script `crc32` that computes the +checksum for a specified file or standard input. + +
+ CDN Availability (click to show) + +| CDN | URL | +|-----------:|:-------------------------------------------| +| `unpkg` | | +| `jsDelivr` | | +| `CDNjs` | | + +
+ + +## Integration + +Using NodeJS or a bundler: + +```js +var CRC32 = require("crc-32"); +``` + +In the browser, the `crc32.js` script can be loaded directly: ```html ``` -The browser exposes a variable `CRC32`. +The browser script exposes a variable `CRC32`. -When installed globally, npm installs a script `crc32` that computes the -checksum for a specified file or standard input. +The script will manipulate `module.exports` if available . This is not always +desirable. To prevent the behavior, define `DO_NOT_EXPORT_CRC`. + +### CRC32C (Castagnoli) + +The module and CDNs also include a parallel script for CRC32C calculations. + +Using NodeJS or a bundler: + +```js +var CRC32C = require("crc-32/crc32c"); +``` + +In the browser, the `crc32c.js` script can be loaded directly: + +```html + +``` + +The browser exposes a variable `CRC32C`. The script will manipulate `module.exports` if available . This is not always desirable. To prevent the behavior, define `DO_NOT_EXPORT_CRC`. @@ -44,20 +85,51 @@ The return value is a signed 32-bit integer. For example: ```js -// var CRC32 = require('crc-32'); // uncomment this line if in node -CRC32.str("SheetJS") // -1647298270 -CRC32.bstr("SheetJS") // -1647298270 -CRC32.buf([ 83, 104, 101, 101, 116, 74, 83 ]) // -1647298270 +// var CRC32 = require('crc-32'); // uncomment this line if in node +CRC32.str("SheetJS") // -1647298270 +CRC32.bstr("SheetJS") // -1647298270 +CRC32.buf([ 83, 104, 101, 101, 116, 74, 83 ]) // -1647298270 -crc32 = CRC32.buf([83, 104]) // -1826163454 "Sh" -crc32 = CRC32.str("eet", crc32) // 1191034598 "Sheet" -CRC32.bstr("JS", crc32) // -1647298270 "SheetJS" +crc32 = CRC32.buf([83, 104]) // -1826163454 "Sh" +crc32 = CRC32.str("eet", crc32) // 1191034598 "Sheet" +CRC32.bstr("JS", crc32) // -1647298270 "SheetJS" -[CRC32.str("\u2603"), CRC32.str("\u0003")] // [ -1743909036, 1259060791 ] -[CRC32.bstr("\u2603"), CRC32.bstr("\u0003")] // [ 1259060791, 1259060791 ] -[CRC32.buf([0x2603]), CRC32.buf([0x0003])] // [ 1259060791, 1259060791 ] +[CRC32.str("\u2603"), CRC32.str("\u0003")] // [ -1743909036, 1259060791 ] +[CRC32.bstr("\u2603"), CRC32.bstr("\u0003")] // [ 1259060791, 1259060791 ] +[CRC32.buf([0x2603]), CRC32.buf([0x0003])] // [ 1259060791, 1259060791 ] + +// var CRC32C = require('crc-32/crc32c'); // uncomment this line if in node +CRC32C.str("SheetJS") // -284764294 +CRC32C.bstr("SheetJS") // -284764294 +CRC32C.buf([ 83, 104, 101, 101, 116, 74, 83 ]) // -284764294 + +crc32c = CRC32C.buf([83, 104]) // -297065629 "Sh" +crc32c = CRC32C.str("eet", crc32c) // 1241364256 "Sheet" +CRC32C.bstr("JS", crc32c) // -284764294 "SheetJS" + +[CRC32C.str("\u2603"), CRC32C.str("\u0003")] // [ 1253703093, 1093509285 ] +[CRC32C.bstr("\u2603"), CRC32C.bstr("\u0003")] // [ 1093509285, 1093509285 ] +[CRC32C.buf([0x2603]), CRC32C.buf([0x0003])] // [ 1093509285, 1093509285 ] ``` +### Best Practices + +Even though the initial seed is optional, for performance reasons it is highly +recommended to explicitly pass the default seed 0. + +In NodeJS with the native Buffer implementation, it is oftentimes faster to +convert binary strings with `Buffer.from(bstr, "binary")` first: + +```js +/* Frequently slower in NodeJS */ +crc32 = CRC32.bstr(bstr, 0); +/* Frequently faster in NodeJS */ +crc32 = CRC32.buf(Buffer.from(bstr, "binary"), 0); +``` + +This does not apply to browser `Buffer` shims, and thus is not implemented in +the library directly. + ## Testing `make test` will run the nodejs-based test. diff --git a/bits/00_header.js b/bits/00_header.js index 102cdad..c97b148 100644 --- a/bits/00_header.js +++ b/bits/00_header.js @@ -1,4 +1,4 @@ -/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ +/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2: */ /*exported CRC32 */ /*:: declare var DO_NOT_EXPORT_CRC:?boolean; */ diff --git a/bits/01_version.js b/bits/01_version.js index 782ea1d..45bc6f8 100644 --- a/bits/01_version.js +++ b/bits/01_version.js @@ -1 +1 @@ -CRC32.version = '1.2.0'; +CRC32.version = '1.2.1'; diff --git a/bits/20_crctable.js b/bits/20_crctable.js index c0236a1..b187020 100644 --- a/bits/20_crctable.js +++ b/bits/20_crctable.js @@ -1,4 +1,3 @@ -/* see perf/crc32table.js */ /*global Int32Array */ function signed_crc_table()/*:CRC32TableType*/ { var c = 0, table/*:Array*/ = new Array(256); @@ -19,8 +18,7 @@ function signed_crc_table()/*:CRC32TableType*/ { return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; } -var T = signed_crc_table(); -/*# slice by 16 tables */ +var T0 = signed_crc_table(); function slice_by_16_tables(T) { var c = 0, v = 0, n = 0, table/*:Array*/ = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ; @@ -33,7 +31,7 @@ function slice_by_16_tables(T) { 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; } -var TT = slice_by_16_tables(T); +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]; diff --git a/bits/40_crc.js b/bits/40_crc.js index 0e55946..1a872ab 100644 --- a/bits/40_crc.js +++ b/bits/40_crc.js @@ -1,96 +1,44 @@ -/*# charCodeAt is the best approach for binary strings */ function crc32_bstr(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - if(bstr.length > 10000) return crc32_bstr_16(bstr, seed); - var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 1; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - } - if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; - return C ^ -1; + 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; } -function crc32_bstr_16(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 15; - for(var i = 0; i < L;) C = - Tf[bstr.charCodeAt(i++) ^ (C & 255)] ^ - Te[bstr.charCodeAt(i++) ^ ((C >> 8) & 255)] ^ - Td[bstr.charCodeAt(i++) ^ ((C >> 16) & 255)] ^ - Tc[bstr.charCodeAt(i++) ^ (C >>> 24)] ^ - Tb[bstr.charCodeAt(i++)] ^ - Ta[bstr.charCodeAt(i++)] ^ - T9[bstr.charCodeAt(i++)] ^ - T8[bstr.charCodeAt(i++)] ^ - T7[bstr.charCodeAt(i++)] ^ - T6[bstr.charCodeAt(i++)] ^ - T5[bstr.charCodeAt(i++)] ^ - T4[bstr.charCodeAt(i++)] ^ - T3[bstr.charCodeAt(i++)] ^ - T2[bstr.charCodeAt(i++)] ^ - T1[bstr.charCodeAt(i++)] ^ - T[bstr.charCodeAt(i++)]; + +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; - while(i < L) C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - return C ^ -1; + while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF]; + return ~C; } -function crc32_buf(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - if(buf.length > 2000) return crc32_buf_16(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]; - } - while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} -function crc32_buf_16(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 15; - for(var i = 0; i < L;) C = - Tf[buf[i++] ^ (C & 255)] ^ - Te[buf[i++] ^ ((C >> 8) & 255)] ^ - Td[buf[i++] ^ ((C >> 16) & 255)] ^ - Tc[buf[i++] ^ (C >>> 24)] ^ - Tb[buf[i++]] ^ - Ta[buf[i++]] ^ - T9[buf[i++]] ^ - T8[buf[i++]] ^ - T7[buf[i++]] ^ - T6[buf[i++]] ^ - T5[buf[i++]] ^ - T4[buf[i++]] ^ - T3[buf[i++]] ^ - T2[buf[i++]] ^ - T1[buf[i++]] ^ - T[buf[i++]]; - L += 15; - while(i < L) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} - - -/*# much much faster to intertwine utf8 and crc, slower to slice by 8 or 16 */ 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;) { c = str.charCodeAt(i++); if(c < 0x80) { - C = (C>>>8) ^ T[(C ^ c)&0xFF]; + C = (C>>>8) ^ T0[(C^c)&0xFF]; } else if(c < 0x800) { - C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } 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]; + 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]; } 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]; + 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]; } } - return C ^ -1; + return ~C; } diff --git a/bits/90_exports.js b/bits/90_exports.js index 76e4ccf..90570b1 100644 --- a/bits/90_exports.js +++ b/bits/90_exports.js @@ -1,4 +1,4 @@ -CRC32.table = T; +CRC32.table = T0; // $FlowIgnore CRC32.bstr = crc32_bstr; // $FlowIgnore diff --git a/crc32.flow.js b/crc32.flow.js index 18e5725..3a80772 100644 --- a/crc32.flow.js +++ b/crc32.flow.js @@ -1,4 +1,4 @@ -/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ +/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2: */ /*exported CRC32 */ /*:: declare var DO_NOT_EXPORT_CRC:?boolean; */ @@ -25,13 +25,12 @@ var CRC32/*:CRC32Module*/; /*eslint-enable */ /*jshint ignore:end */ }(function(CRC32/*:CRC32Module*/) { -CRC32.version = '1.2.0'; +CRC32.version = '1.2.1'; /*:: type CRC32Type = number; type ABuf = Array | Buffer | Uint8Array; type CRC32TableType = Array | Int32Array; */ -/* see perf/crc32table.js */ /*global Int32Array */ function signed_crc_table()/*:CRC32TableType*/ { var c = 0, table/*:Array*/ = new Array(256); @@ -52,8 +51,7 @@ function signed_crc_table()/*:CRC32TableType*/ { return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; } -var T = signed_crc_table(); -/*# slice by 16 tables */ +var T0 = signed_crc_table(); function slice_by_16_tables(T) { var c = 0, v = 0, n = 0, table/*:Array*/ = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ; @@ -66,107 +64,55 @@ function slice_by_16_tables(T) { 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; } -var TT = slice_by_16_tables(T); +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]; -/*# charCodeAt is the best approach for binary strings */ function crc32_bstr(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - if(bstr.length > 10000) return crc32_bstr_16(bstr, seed); - var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 1; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - } - if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; - return C ^ -1; + 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; } -function crc32_bstr_16(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 15; - for(var i = 0; i < L;) C = - Tf[bstr.charCodeAt(i++) ^ (C & 255)] ^ - Te[bstr.charCodeAt(i++) ^ ((C >> 8) & 255)] ^ - Td[bstr.charCodeAt(i++) ^ ((C >> 16) & 255)] ^ - Tc[bstr.charCodeAt(i++) ^ (C >>> 24)] ^ - Tb[bstr.charCodeAt(i++)] ^ - Ta[bstr.charCodeAt(i++)] ^ - T9[bstr.charCodeAt(i++)] ^ - T8[bstr.charCodeAt(i++)] ^ - T7[bstr.charCodeAt(i++)] ^ - T6[bstr.charCodeAt(i++)] ^ - T5[bstr.charCodeAt(i++)] ^ - T4[bstr.charCodeAt(i++)] ^ - T3[bstr.charCodeAt(i++)] ^ - T2[bstr.charCodeAt(i++)] ^ - T1[bstr.charCodeAt(i++)] ^ - T[bstr.charCodeAt(i++)]; + +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; - while(i < L) C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - return C ^ -1; + while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF]; + return ~C; } -function crc32_buf(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - if(buf.length > 2000) return crc32_buf_16(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]; - } - while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} -function crc32_buf_16(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 15; - for(var i = 0; i < L;) C = - Tf[buf[i++] ^ (C & 255)] ^ - Te[buf[i++] ^ ((C >> 8) & 255)] ^ - Td[buf[i++] ^ ((C >> 16) & 255)] ^ - Tc[buf[i++] ^ (C >>> 24)] ^ - Tb[buf[i++]] ^ - Ta[buf[i++]] ^ - T9[buf[i++]] ^ - T8[buf[i++]] ^ - T7[buf[i++]] ^ - T6[buf[i++]] ^ - T5[buf[i++]] ^ - T4[buf[i++]] ^ - T3[buf[i++]] ^ - T2[buf[i++]] ^ - T1[buf[i++]] ^ - T[buf[i++]]; - L += 15; - while(i < L) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} - - -/*# much much faster to intertwine utf8 and crc, slower to slice by 8 or 16 */ 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;) { c = str.charCodeAt(i++); if(c < 0x80) { - C = (C>>>8) ^ T[(C ^ c)&0xFF]; + C = (C>>>8) ^ T0[(C^c)&0xFF]; } else if(c < 0x800) { - C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } 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]; + 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]; } 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]; + 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]; } } - return C ^ -1; + return ~C; } -CRC32.table = T; +CRC32.table = T0; // $FlowIgnore CRC32.bstr = crc32_bstr; // $FlowIgnore diff --git a/crc32.js b/crc32.js index b310fb9..af85913 100644 --- a/crc32.js +++ b/crc32.js @@ -1,4 +1,4 @@ -/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ +/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2: */ /*exported CRC32 */ var CRC32; @@ -23,8 +23,7 @@ var CRC32; /*eslint-enable */ /*jshint ignore:end */ }(function(CRC32) { -CRC32.version = '1.2.0'; -/* see perf/crc32table.js */ +CRC32.version = '1.2.1'; /*global Int32Array */ function signed_crc_table() { var c = 0, table = new Array(256); @@ -45,7 +44,7 @@ function signed_crc_table() { return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; } -var T = signed_crc_table(); +var T0 = signed_crc_table(); function slice_by_16_tables(T) { var c = 0, v = 0, n = 0, table = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ; @@ -58,105 +57,55 @@ function slice_by_16_tables(T) { 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; } -var TT = slice_by_16_tables(T); +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]; function crc32_bstr(bstr, seed) { - if(bstr.length > 10000) return crc32_bstr_16(bstr, seed); - var C = seed ^ -1, L = bstr.length - 1; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - } - if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; - return C ^ -1; -} -function crc32_bstr_16(bstr, seed) { - var C = seed ^ -1, L = bstr.length - 15; - for(var i = 0; i < L;) C = - Tf[bstr.charCodeAt(i++) ^ (C & 255)] ^ - Te[bstr.charCodeAt(i++) ^ ((C >> 8) & 255)] ^ - Td[bstr.charCodeAt(i++) ^ ((C >> 16) & 255)] ^ - Tc[bstr.charCodeAt(i++) ^ (C >>> 24)] ^ - Tb[bstr.charCodeAt(i++)] ^ - Ta[bstr.charCodeAt(i++)] ^ - T9[bstr.charCodeAt(i++)] ^ - T8[bstr.charCodeAt(i++)] ^ - T7[bstr.charCodeAt(i++)] ^ - T6[bstr.charCodeAt(i++)] ^ - T5[bstr.charCodeAt(i++)] ^ - T4[bstr.charCodeAt(i++)] ^ - T3[bstr.charCodeAt(i++)] ^ - T2[bstr.charCodeAt(i++)] ^ - T1[bstr.charCodeAt(i++)] ^ - T[bstr.charCodeAt(i++)]; - L += 15; - while(i < L) C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - return C ^ -1; + var C = seed ^ -1; + for(var i = 0, L = bstr.length; i < L;) C = (C>>>8) ^ T0[(C^bstr.charCodeAt(i++))&0xFF]; + return ~C; } -function crc32_buf(buf, seed) { - if(buf.length > 2000) return crc32_buf_16(buf, seed); - var C = seed ^ -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]; - } - while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} -function crc32_buf_16(buf, seed) { - var C = seed ^ -1, L = buf.length - 15; - for(var i = 0; i < L;) C = - Tf[buf[i++] ^ (C & 255)] ^ - Te[buf[i++] ^ ((C >> 8) & 255)] ^ - Td[buf[i++] ^ ((C >> 16) & 255)] ^ - Tc[buf[i++] ^ (C >>> 24)] ^ - Tb[buf[i++]] ^ - Ta[buf[i++]] ^ - T9[buf[i++]] ^ - T8[buf[i++]] ^ - T7[buf[i++]] ^ - T6[buf[i++]] ^ - T5[buf[i++]] ^ - T4[buf[i++]] ^ - T3[buf[i++]] ^ - T2[buf[i++]] ^ - T1[buf[i++]] ^ - T[buf[i++]]; +function crc32_buf(B, seed) { + var C = seed ^ -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; - while(i < L) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; + while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF]; + return ~C; } - function crc32_str(str, seed) { var C = seed ^ -1; for(var i = 0, L = str.length, c = 0, d = 0; i < L;) { c = str.charCodeAt(i++); if(c < 0x80) { - C = (C>>>8) ^ T[(C ^ c)&0xFF]; + C = (C>>>8) ^ T0[(C^c)&0xFF]; } else if(c < 0x800) { - C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } 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]; + 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]; } 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]; + 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]; } } - return C ^ -1; + return ~C; } -CRC32.table = T; +CRC32.table = T0; // $FlowIgnore CRC32.bstr = crc32_bstr; // $FlowIgnore diff --git a/crc32c.flow.js b/crc32c.flow.js index fae81b1..ea7872c 100644 --- a/crc32c.flow.js +++ b/crc32c.flow.js @@ -1,9 +1,9 @@ -/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ +/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2: */ -/*exported CRC32 */ +/*exported CRC32C */ /*:: declare var DO_NOT_EXPORT_CRC:?boolean; */ /*:: declare function define(cb:()=>any):void; */ -var CRC32/*:CRC32Module*/; +var CRC32C/*:CRC32Module*/; (function (factory/*:(a:any)=>void*/)/*:void*/ { /*jshint ignore:start */ /*eslint-disable */ @@ -17,21 +17,20 @@ var CRC32/*:CRC32Module*/; return module; }); } else { - factory(CRC32 = /*::(*/{}/*:: :any)*/); + factory(CRC32C = /*::(*/{}/*:: :any)*/); } } else { - factory(CRC32 = /*::(*/{}/*:: :any)*/); + factory(CRC32C = /*::(*/{}/*:: :any)*/); } /*eslint-enable */ /*jshint ignore:end */ -}(function(CRC32/*:CRC32Module*/) { -CRC32.version = '1.2.0'; +}(function(CRC32C/*:CRC32Module*/) { +CRC32C.version = '1.2.1'; /*:: type CRC32Type = number; type ABuf = Array | Buffer | Uint8Array; type CRC32TableType = Array | Int32Array; */ -/* see perf/crc32table.js */ /*global Int32Array */ function signed_crc_table()/*:CRC32TableType*/ { var c = 0, table/*:Array*/ = new Array(256); @@ -52,8 +51,7 @@ function signed_crc_table()/*:CRC32TableType*/ { return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; } -var T = signed_crc_table(); -/*# slice by 16 tables */ +var T0 = signed_crc_table(); function slice_by_16_tables(T) { var c = 0, v = 0, n = 0, table/*:Array*/ = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ; @@ -66,111 +64,59 @@ function slice_by_16_tables(T) { 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; } -var TT = slice_by_16_tables(T); +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]; -/*# charCodeAt is the best approach for binary strings */ function crc32_bstr(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - if(bstr.length > 10000) return crc32_bstr_16(bstr, seed); - var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 1; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - } - if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; - return C ^ -1; + 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; } -function crc32_bstr_16(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 15; - for(var i = 0; i < L;) C = - Tf[bstr.charCodeAt(i++) ^ (C & 255)] ^ - Te[bstr.charCodeAt(i++) ^ ((C >> 8) & 255)] ^ - Td[bstr.charCodeAt(i++) ^ ((C >> 16) & 255)] ^ - Tc[bstr.charCodeAt(i++) ^ (C >>> 24)] ^ - Tb[bstr.charCodeAt(i++)] ^ - Ta[bstr.charCodeAt(i++)] ^ - T9[bstr.charCodeAt(i++)] ^ - T8[bstr.charCodeAt(i++)] ^ - T7[bstr.charCodeAt(i++)] ^ - T6[bstr.charCodeAt(i++)] ^ - T5[bstr.charCodeAt(i++)] ^ - T4[bstr.charCodeAt(i++)] ^ - T3[bstr.charCodeAt(i++)] ^ - T2[bstr.charCodeAt(i++)] ^ - T1[bstr.charCodeAt(i++)] ^ - T[bstr.charCodeAt(i++)]; + +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; - while(i < L) C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - return C ^ -1; + while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF]; + return ~C; } -function crc32_buf(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - if(buf.length > 2000) return crc32_buf_16(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]; - } - while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} -function crc32_buf_16(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { - var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 15; - for(var i = 0; i < L;) C = - Tf[buf[i++] ^ (C & 255)] ^ - Te[buf[i++] ^ ((C >> 8) & 255)] ^ - Td[buf[i++] ^ ((C >> 16) & 255)] ^ - Tc[buf[i++] ^ (C >>> 24)] ^ - Tb[buf[i++]] ^ - Ta[buf[i++]] ^ - T9[buf[i++]] ^ - T8[buf[i++]] ^ - T7[buf[i++]] ^ - T6[buf[i++]] ^ - T5[buf[i++]] ^ - T4[buf[i++]] ^ - T3[buf[i++]] ^ - T2[buf[i++]] ^ - T1[buf[i++]] ^ - T[buf[i++]]; - L += 15; - while(i < L) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} - - -/*# much much faster to intertwine utf8 and crc, slower to slice by 8 or 16 */ 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;) { c = str.charCodeAt(i++); if(c < 0x80) { - C = (C>>>8) ^ T[(C ^ c)&0xFF]; + C = (C>>>8) ^ T0[(C^c)&0xFF]; } else if(c < 0x800) { - C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } 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]; + 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]; } 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]; + 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]; } } - return C ^ -1; + return ~C; } -CRC32.table = T; +CRC32C.table = T0; // $FlowIgnore -CRC32.bstr = crc32_bstr; +CRC32C.bstr = crc32_bstr; // $FlowIgnore -CRC32.buf = crc32_buf; +CRC32C.buf = crc32_buf; // $FlowIgnore -CRC32.str = crc32_str; +CRC32C.str = crc32_str; })); diff --git a/crc32c.js b/crc32c.js index 7b471c0..6247199 100644 --- a/crc32c.js +++ b/crc32c.js @@ -1,7 +1,7 @@ -/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ +/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2: */ -/*exported CRC32 */ -var CRC32; +/*exported CRC32C */ +var CRC32C; (function (factory) { /*jshint ignore:start */ /*eslint-disable */ @@ -15,16 +15,15 @@ var CRC32; return module; }); } else { - factory(CRC32 = {}); + factory(CRC32C = {}); } } else { - factory(CRC32 = {}); + factory(CRC32C = {}); } /*eslint-enable */ /*jshint ignore:end */ -}(function(CRC32) { -CRC32.version = '1.2.0'; -/* see perf/crc32table.js */ +}(function(CRC32C) { +CRC32C.version = '1.2.1'; /*global Int32Array */ function signed_crc_table() { var c = 0, table = new Array(256); @@ -45,7 +44,7 @@ function signed_crc_table() { return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; } -var T = signed_crc_table(); +var T0 = signed_crc_table(); function slice_by_16_tables(T) { var c = 0, v = 0, n = 0, table = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ; @@ -58,109 +57,59 @@ function slice_by_16_tables(T) { 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; } -var TT = slice_by_16_tables(T); +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]; function crc32_bstr(bstr, seed) { - if(bstr.length > 10000) return crc32_bstr_16(bstr, seed); - var C = seed ^ -1, L = bstr.length - 1; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - } - if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; - return C ^ -1; -} -function crc32_bstr_16(bstr, seed) { - var C = seed ^ -1, L = bstr.length - 15; - for(var i = 0; i < L;) C = - Tf[bstr.charCodeAt(i++) ^ (C & 255)] ^ - Te[bstr.charCodeAt(i++) ^ ((C >> 8) & 255)] ^ - Td[bstr.charCodeAt(i++) ^ ((C >> 16) & 255)] ^ - Tc[bstr.charCodeAt(i++) ^ (C >>> 24)] ^ - Tb[bstr.charCodeAt(i++)] ^ - Ta[bstr.charCodeAt(i++)] ^ - T9[bstr.charCodeAt(i++)] ^ - T8[bstr.charCodeAt(i++)] ^ - T7[bstr.charCodeAt(i++)] ^ - T6[bstr.charCodeAt(i++)] ^ - T5[bstr.charCodeAt(i++)] ^ - T4[bstr.charCodeAt(i++)] ^ - T3[bstr.charCodeAt(i++)] ^ - T2[bstr.charCodeAt(i++)] ^ - T1[bstr.charCodeAt(i++)] ^ - T[bstr.charCodeAt(i++)]; - L += 15; - while(i < L) C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - return C ^ -1; + var C = seed ^ -1; + for(var i = 0, L = bstr.length; i < L;) C = (C>>>8) ^ T0[(C^bstr.charCodeAt(i++))&0xFF]; + return ~C; } -function crc32_buf(buf, seed) { - if(buf.length > 2000) return crc32_buf_16(buf, seed); - var C = seed ^ -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]; - } - while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} -function crc32_buf_16(buf, seed) { - var C = seed ^ -1, L = buf.length - 15; - for(var i = 0; i < L;) C = - Tf[buf[i++] ^ (C & 255)] ^ - Te[buf[i++] ^ ((C >> 8) & 255)] ^ - Td[buf[i++] ^ ((C >> 16) & 255)] ^ - Tc[buf[i++] ^ (C >>> 24)] ^ - Tb[buf[i++]] ^ - Ta[buf[i++]] ^ - T9[buf[i++]] ^ - T8[buf[i++]] ^ - T7[buf[i++]] ^ - T6[buf[i++]] ^ - T5[buf[i++]] ^ - T4[buf[i++]] ^ - T3[buf[i++]] ^ - T2[buf[i++]] ^ - T1[buf[i++]] ^ - T[buf[i++]]; +function crc32_buf(B, seed) { + var C = seed ^ -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; - while(i < L) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; + while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF]; + return ~C; } - function crc32_str(str, seed) { var C = seed ^ -1; for(var i = 0, L = str.length, c = 0, d = 0; i < L;) { c = str.charCodeAt(i++); if(c < 0x80) { - C = (C>>>8) ^ T[(C ^ c)&0xFF]; + C = (C>>>8) ^ T0[(C^c)&0xFF]; } else if(c < 0x800) { - C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } 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]; + 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]; } 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]; + 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]; } } - return C ^ -1; + return ~C; } -CRC32.table = T; +CRC32C.table = T0; // $FlowIgnore -CRC32.bstr = crc32_bstr; +CRC32C.bstr = crc32_bstr; // $FlowIgnore -CRC32.buf = crc32_buf; +CRC32C.buf = crc32_buf; // $FlowIgnore -CRC32.str = crc32_str; +CRC32C.str = crc32_str; })); diff --git a/ctest/crc32.js b/ctest/crc32.js index 6bcecef..af85913 100644 --- a/ctest/crc32.js +++ b/ctest/crc32.js @@ -1,4 +1,4 @@ -/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ +/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2: */ /*exported CRC32 */ var CRC32; @@ -23,8 +23,7 @@ var CRC32; /*eslint-enable */ /*jshint ignore:end */ }(function(CRC32) { -CRC32.version = '1.2.0'; -/* see perf/crc32table.js */ +CRC32.version = '1.2.1'; /*global Int32Array */ function signed_crc_table() { var c = 0, table = new Array(256); @@ -45,70 +44,68 @@ function signed_crc_table() { return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; } -var T = signed_crc_table(); +var T0 = signed_crc_table(); +function slice_by_16_tables(T) { + var c = 0, v = 0, n = 0, table = 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; +} +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]; function crc32_bstr(bstr, seed) { - var C = seed ^ -1, L = bstr.length - 1; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - } - if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; - return C ^ -1; + var C = seed ^ -1; + for(var i = 0, L = bstr.length; i < L;) C = (C>>>8) ^ T0[(C^bstr.charCodeAt(i++))&0xFF]; + return ~C; } -function crc32_buf(buf, seed) { - if(buf.length > 10000) return crc32_buf_8(buf, seed); - var C = seed ^ -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]; - } - while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} - -function crc32_buf_8(buf, seed) { - var C = seed ^ -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]; - } - while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; +function crc32_buf(B, seed) { + var C = seed ^ -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; + while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF]; + return ~C; } function crc32_str(str, seed) { var C = seed ^ -1; - for(var i = 0, L=str.length, c, d; i < L;) { + for(var i = 0, L = str.length, c = 0, d = 0; i < L;) { c = str.charCodeAt(i++); if(c < 0x80) { - C = (C>>>8) ^ T[(C ^ c)&0xFF]; + C = (C>>>8) ^ T0[(C^c)&0xFF]; } else if(c < 0x800) { - C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } 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]; + 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]; } 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]; + 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]; } } - return C ^ -1; + return ~C; } -CRC32.table = T; +CRC32.table = T0; // $FlowIgnore CRC32.bstr = crc32_bstr; // $FlowIgnore diff --git a/ctest/fixtures.js b/ctest/fixtures.js index 375d2b8..002840a 100644 --- a/ctest/fixtures.js +++ b/ctest/fixtures.js @@ -35,8 +35,8 @@ type _CB = {(data:Buffer):void;}; declare module 'concat-stream' {declare function exports(f:_CB):stream$Duplex;}; declare module 'exit-on-epipe' {}; -declare module 'crc-32' { declare var exports:CRC32Module; }; -declare module '../' { declare var exports:CRC32Module; }; +declare module 'crc-32' { declare module.exports:CRC32Module; }; +declare module '../' { declare module.exports:CRC32Module; }; declare module 'printj' { declare function sprintf(fmt:string, ...args:any):string; diff --git a/ctest/test.js b/ctest/test.js index 4b0f0a4..9f6b546 100644 --- a/ctest/test.js +++ b/ctest/test.js @@ -20,6 +20,14 @@ function msieversion() return parseInt (ua.substring (msie+5, ua.indexOf (".", msie ))); } +var Buffer_from = function(){}; + +if(typeof Buffer !== 'undefined') { + var nbfs = !Buffer.from; + if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; } + Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer); +} + describe('crc32 table', function() { it('should match fixed table', function() { var overflow = 0; @@ -40,7 +48,7 @@ describe('crc32 bits', function() { it(msg, function() { if(i[2] === 1) assert.equal(X.bstr(i[0]), L); assert.equal(X.str(i[0]), i[1]|0); - if(typeof Buffer !== 'undefined') assert.equal(X.buf(new Buffer(i[0])), L); + if(typeof Buffer !== 'undefined') assert.equal(X.buf(Buffer_from(i[0])), L); var len = i[0].length, step = len < 20000 ? 1 : len < 50000 ? Math.ceil(len / 20000) : Math.ceil(len / 2000); for(var x = 0; x < len; x += step) { if(i[0].charCodeAt(x) >= 0xD800 && i[0].charCodeAt(x) < 0xE000) continue; @@ -51,7 +59,7 @@ describe('crc32 bits', function() { var strcrc = X.str(i[0].substr(x), X.str(i[0].substr(0, x))); assert.equal(strcrc, i[1]|0); if(typeof Buffer !== 'undefined') { - var buf = new Buffer(i[0]); + var buf = Buffer_from(i[0]); var bufcrc = X.buf(buf.slice(x), X.buf(buf.slice(0, x))); assert.equal(bufcrc, L); } @@ -74,9 +82,9 @@ if(typeof require !== 'undefined') describe("unicode", function() { var cc = corpus[ucidx], dd = X.str(c); assert.equal(dd, cc, ":" + ucidx + ":" + c + ":" + cc + ":" + dd); if(typeof Buffer !== 'undefined') { - var ee = X.buf(new Buffer(c, "utf8")); + var ee = X.buf(Buffer_from(c, "utf8")); assert.equal(ee, cc, ":" + ucidx + ":" + c + ":" + cc + ":" + ee); - var ff = X.bstr(String.fromCharCode.apply(null, new Buffer(c, "utf8"))); + var ff = X.bstr(String.fromCharCode.apply(null, Buffer_from(c, "utf8"))); assert.equal(ff, cc, ":" + ucidx + ":" + c + ":" + cc + ":" + ff); } }; diff --git a/package.json b/package.json index 80de338..b00267a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crc-32", - "version": "1.2.0", + "version": "1.2.1", "author": "sheetjs", "description": "Pure-JS CRC-32", "keywords": [ "crc", "crc32", "checksum" ], @@ -35,7 +35,7 @@ "pattern": "crc32.js" } }, - "homepage": "http://sheetjs.com/opensource", + "homepage": "https://sheetjs.com/", "files": ["crc32.js", "crc32c.js", "bin/crc32.njs", "LICENSE", "README.md", "types/index.d.ts", "types/*.json"], "bugs": { "url": "https://github.com/SheetJS/js-crc32/issues" }, "license": "Apache-2.0",