Skip to content

Commit 9f97346

Browse files
committed
CRC32C
1 parent 49af363 commit 9f97346

File tree

5 files changed

+256
-4
lines changed

5 files changed

+256
-4
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
LIB=crc32
22
REQS=
33
ADDONS=
4-
AUXTARGETS=demo/browser.js
4+
AUXTARGETS=crc32c.js demo/browser.js
55
CMDS=bin/crc32.njs
66
HTMLLINT=index.html
77

@@ -30,6 +30,9 @@ bits/01_version.js: package.json
3030
clean: clean-baseline ## Remove targets and build artifacts
3131
rm -f $(TARGET) $(FLOWTARGET)
3232

33+
crc32c.flow.js: crc32.flow.js
34+
cat $^ | sed 's/-306674912/-2097792136/g' > $@
35+
3336
## Testing
3437

3538
.PHONY: test mocha

bin/crc32.njs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function help()/*:number*/ {
2020
" -u, --unsigned print result with format `%u`",
2121
" -x, --hex print result with format `%0.8x`",
2222
" -X, --HEX print result with format `%0.8X`",
23+
" -c, --crc32c use CRC32C (Castagnoli)",
2324
" -F, --format=<s> use specified printf format",
2425
"",
2526
"Set filename = '-' or pipe data into crc32 to read from stdin",
@@ -51,6 +52,8 @@ for(var i = 0; i < args.length; ++i) {
5152
case "--help": case "-h": process.exit(help()); break;
5253
case "--version": case "-V": process.exit(version()); break;
5354

55+
case "--crc32c": case "-c": try { X = require('../crc32c'); } catch(e) { X = require('crc-32/crc32c'); } break;
56+
5457
case "--signed": case "-d": fmt = "%d"; break;
5558
case "--unsigned": case "-u": fmt = "%u"; break;
5659
case "--hex": case "-x": fmt = "%0.8x"; break;

crc32c.flow.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
2+
/* vim: set ts=2: */
3+
/*exported CRC32 */
4+
/*:: declare var DO_NOT_EXPORT_CRC:?boolean; */
5+
/*:: declare function define(cb:()=>any):void; */
6+
var CRC32/*:CRC32Module*/;
7+
(function (factory/*:(a:any)=>void*/)/*:void*/ {
8+
/*jshint ignore:start */
9+
/*eslint-disable */
10+
if(typeof DO_NOT_EXPORT_CRC === 'undefined') {
11+
if('object' === typeof exports) {
12+
factory(exports);
13+
} else if ('function' === typeof define && define.amd) {
14+
define(function () {
15+
var module/*:CRC32Module*/ = /*::(*/{}/*:: :any)*/;
16+
factory(module);
17+
return module;
18+
});
19+
} else {
20+
factory(CRC32 = /*::(*/{}/*:: :any)*/);
21+
}
22+
} else {
23+
factory(CRC32 = /*::(*/{}/*:: :any)*/);
24+
}
25+
/*eslint-enable */
26+
/*jshint ignore:end */
27+
}(function(CRC32/*:CRC32Module*/) {
28+
CRC32.version = '1.2.0';
29+
/*::
30+
type CRC32Type = number;
31+
type ABuf = Array<number> | Buffer | Uint8Array;
32+
type CRC32TableType = Array<number> | Int32Array;
33+
*/
34+
/* see perf/crc32table.js */
35+
/*global Int32Array */
36+
function signed_crc_table()/*:CRC32TableType*/ {
37+
var c = 0, table/*:Array<number>*/ = new Array(256);
38+
39+
for(var n =0; n != 256; ++n){
40+
c = n;
41+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
42+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
43+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
44+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
45+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
46+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
47+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
48+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
49+
table[n] = c;
50+
}
51+
52+
return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
53+
}
54+
55+
var T = signed_crc_table();
56+
/*# charCodeAt is the best approach for binary strings */
57+
function crc32_bstr(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
58+
var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 1;
59+
for(var i = 0; i < L;) {
60+
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
61+
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
62+
}
63+
if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF];
64+
return C ^ -1;
65+
}
66+
67+
function crc32_buf(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
68+
if(buf.length > 10000) return crc32_buf_8(buf, seed);
69+
var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 3;
70+
for(var i = 0; i < L;) {
71+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
72+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
73+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
74+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
75+
}
76+
while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
77+
return C ^ -1;
78+
}
79+
80+
function crc32_buf_8(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
81+
var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 7;
82+
for(var i = 0; i < L;) {
83+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
84+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
85+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
86+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
87+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
88+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
89+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
90+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
91+
}
92+
while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
93+
return C ^ -1;
94+
}
95+
96+
/*# much much faster to intertwine utf8 and crc */
97+
function crc32_str(str/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
98+
var C = seed/*:: ? 0 : 0 */ ^ -1;
99+
for(var i = 0, L=str.length, c, d; i < L;) {
100+
c = str.charCodeAt(i++);
101+
if(c < 0x80) {
102+
C = (C>>>8) ^ T[(C ^ c)&0xFF];
103+
} else if(c < 0x800) {
104+
C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
105+
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
106+
} else if(c >= 0xD800 && c < 0xE000) {
107+
c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
108+
C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
109+
C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
110+
C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
111+
C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
112+
} else {
113+
C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
114+
C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
115+
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
116+
}
117+
}
118+
return C ^ -1;
119+
}
120+
CRC32.table = T;
121+
// $FlowIgnore
122+
CRC32.bstr = crc32_bstr;
123+
// $FlowIgnore
124+
CRC32.buf = crc32_buf;
125+
// $FlowIgnore
126+
CRC32.str = crc32_str;
127+
}));

crc32c.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
2+
/* vim: set ts=2: */
3+
/*exported CRC32 */
4+
var CRC32;
5+
(function (factory) {
6+
/*jshint ignore:start */
7+
/*eslint-disable */
8+
if(typeof DO_NOT_EXPORT_CRC === 'undefined') {
9+
if('object' === typeof exports) {
10+
factory(exports);
11+
} else if ('function' === typeof define && define.amd) {
12+
define(function () {
13+
var module = {};
14+
factory(module);
15+
return module;
16+
});
17+
} else {
18+
factory(CRC32 = {});
19+
}
20+
} else {
21+
factory(CRC32 = {});
22+
}
23+
/*eslint-enable */
24+
/*jshint ignore:end */
25+
}(function(CRC32) {
26+
CRC32.version = '1.2.0';
27+
/* see perf/crc32table.js */
28+
/*global Int32Array */
29+
function signed_crc_table() {
30+
var c = 0, table = new Array(256);
31+
32+
for(var n =0; n != 256; ++n){
33+
c = n;
34+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
35+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
36+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
37+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
38+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
39+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
40+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
41+
c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1));
42+
table[n] = c;
43+
}
44+
45+
return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
46+
}
47+
48+
var T = signed_crc_table();
49+
function crc32_bstr(bstr, seed) {
50+
var C = seed ^ -1, L = bstr.length - 1;
51+
for(var i = 0; i < L;) {
52+
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
53+
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
54+
}
55+
if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF];
56+
return C ^ -1;
57+
}
58+
59+
function crc32_buf(buf, seed) {
60+
if(buf.length > 10000) return crc32_buf_8(buf, seed);
61+
var C = seed ^ -1, L = buf.length - 3;
62+
for(var i = 0; i < L;) {
63+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
64+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
65+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
66+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
67+
}
68+
while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
69+
return C ^ -1;
70+
}
71+
72+
function crc32_buf_8(buf, seed) {
73+
var C = seed ^ -1, L = buf.length - 7;
74+
for(var i = 0; i < L;) {
75+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
76+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
77+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
78+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
79+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
80+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
81+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
82+
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
83+
}
84+
while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
85+
return C ^ -1;
86+
}
87+
88+
function crc32_str(str, seed) {
89+
var C = seed ^ -1;
90+
for(var i = 0, L=str.length, c, d; i < L;) {
91+
c = str.charCodeAt(i++);
92+
if(c < 0x80) {
93+
C = (C>>>8) ^ T[(C ^ c)&0xFF];
94+
} else if(c < 0x800) {
95+
C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
96+
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
97+
} else if(c >= 0xD800 && c < 0xE000) {
98+
c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
99+
C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
100+
C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
101+
C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
102+
C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
103+
} else {
104+
C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
105+
C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
106+
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
107+
}
108+
}
109+
return C ^ -1;
110+
}
111+
CRC32.table = T;
112+
// $FlowIgnore
113+
CRC32.bstr = crc32_bstr;
114+
// $FlowIgnore
115+
CRC32.buf = crc32_buf;
116+
// $FlowIgnore
117+
CRC32.str = crc32_str;
118+
}));

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
"description": "Pure-JS CRC-32",
66
"keywords": [ "crc", "crc32", "checksum" ],
77
"bin": {
8-
"crc32": "./bin/crc32.njs"
8+
"crc32": "bin/crc32.njs"
99
},
1010
"main": "crc32.js",
1111
"types": "types/index.d.ts",
12+
"typesVersions": { "*": { "*": ["types/index.d.ts" ] } },
1213
"dependencies": {
13-
"printj": "~1.1.0",
14+
"printj": "~1.3.1",
1415
"exit-on-epipe": "~1.0.1"
1516
},
1617
"devDependencies": {
@@ -35,7 +36,7 @@
3536
}
3637
},
3738
"homepage": "http://sheetjs.com/opensource",
38-
"files": ["crc32.js", "bin/crc32.njs", "LICENSE", "README.md", "types/index.d.ts", "types/*.json"],
39+
"files": ["crc32.js", "crc32c.js", "bin/crc32.njs", "LICENSE", "README.md", "types/index.d.ts", "types/*.json"],
3940
"bugs": { "url": "https://github.com/SheetJS/js-crc32/issues" },
4041
"license": "Apache-2.0",
4142
"engines": { "node": ">=0.8" }

0 commit comments

Comments
 (0)