Skip to content

Commit 35003c5

Browse files
committed
Initial commit
0 parents  commit 35003c5

29 files changed

+6981
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
misc/coverage.html

.jscs.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"requireCommaBeforeLineBreak": true,
3+
"disallowTrailingWhitespace": true,
4+
"disallowTrailingComma": true
5+
}
6+

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: node_js
2+
node_js:
3+
- "0.11"
4+
- "0.10"
5+
- "0.8"
6+
before_install:
7+
- "npm install -g mocha"
8+
- "npm install blanket"
9+
- "npm install coveralls mocha-lcov-reporter"
10+
after_success:
11+
- "make coveralls-spin"

LICENSE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Copyright (C) 2014 SheetJS
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+

Makefile

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
LIB=crc32
2+
DEPS=$(sort $(wildcard bits/*.js))
3+
TARGET=$(LIB).js
4+
5+
$(TARGET): $(DEPS)
6+
cat $^ | tr -d '\15\32' > $@
7+
cp -f $@ ctest/
8+
9+
bits/01_version.js: package.json
10+
echo "CRC32.version = '"`grep version package.json | awk '{gsub(/[^0-9a-z\.-]/,"",$$2); print $$2}'`"';" > $@
11+
12+
.PHONY: clean
13+
clean:
14+
rm -f $(TARGET)
15+
16+
.PHONY: test mocha
17+
test mocha: test.js
18+
mocha -R spec
19+
20+
.PHONY: ctest
21+
ctest:
22+
cat misc/*.js > ctest/fixtures.js
23+
cp -f test.js ctest/test.js
24+
cp -f $(TARGET) ctest/
25+
26+
.PHONY: lint
27+
lint: $(TARGET)
28+
jshint --show-non-errors $(TARGET)
29+
jscs $(TARGET)
30+
31+
.PHONY: cov cov-spin
32+
cov: misc/coverage.html
33+
cov-spin:
34+
make cov & bash misc/spin.sh $$!
35+
36+
COVFMT=$(patsubst %,cov_%,$(FMT))
37+
.PHONY: $(COVFMT)
38+
$(COVFMT): cov_%:
39+
FMTS=$* make cov
40+
41+
misc/coverage.html: $(TARGET) test.js
42+
mocha --require blanket -R html-cov > $@
43+
44+
.PHONY: coveralls coveralls-spin
45+
coveralls:
46+
mocha --require blanket --reporter mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
47+
48+
coveralls-spin:
49+
make coveralls & bash misc/spin.sh $$!
50+
51+
.PHONY: perf
52+
perf:
53+
bash perf/perf.sh

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# crc32
2+
3+
Standard CRC-32 algorithm implementation in JS (for the browser and nodejs).
4+
Emphasis on correctness and performance.
5+
6+
## Installation
7+
8+
In [nodejs](https://www.npmjs.org/package/crc-32):
9+
10+
npm install crc-32
11+
12+
In the browser:
13+
14+
<script lang="javascript" src="crc32.js"></script>
15+
16+
The browser exposes a variable CRC32
17+
18+
## Usage
19+
20+
- `CRC32.buf(byte array or buffer)` assumes the argument is a set of 8 bit
21+
unsigned integers (e.g. nodejs `Buffer` or simple array of ints)
22+
23+
- `CRC32.bstr(binary string)` interprets the argument as a binary string where
24+
the `i`-th byte is `str.charCodeAt(i)`
25+
26+
- `CRC32.str(string)` interprets the argument as a standard JS string
27+
28+
## Testing
29+
30+
`make test` will run the nodejs-based test. To run the in-browser tests, run a
31+
local server and go to the `ctest` directory. To update the browser artifacts,
32+
run `make ctest`.
33+
34+
## Performance
35+
36+
`make perf` will run performance tests.
37+
38+
## In the future ...
39+
40+
- Specifying an arbitrary initial CRC value
41+
42+
- Supporting different polynomials (e.g. CRC32C)
43+
44+
## License
45+
46+
Please consult the attached LICENSE file for details. All rights not explicitly
47+
granted by the Apache 2.0 license are reserved by the Original Author.
48+
49+
[![Build Status](https://travis-ci.org/SheetJS/js-crc32.png?branch=master)](https://travis-ci.org/SheetJS/js-crc32)
50+
51+
[![Coverage Status](https://coveralls.io/repos/SheetJS/js-crc32/badge.png?branch=master)](https://coveralls.io/r/SheetJS/js-crc32?branch=master)
52+
53+
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/ee0e89f8b1d5b861ffbf264b8ce329a6 "githalytics.com")](http://githalytics.com/SheetJS/js-crc32)
54+

bits/00_header.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* crc32.js (C) 2014 SheetJS -- http://sheetjs.com */
2+
/* vim: set ts=2: */
3+
var CRC32 = {};
4+
(function(CRC32) {

bits/01_version.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CRC32.version = '0.1.0';

bits/20_crctable.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* see perf/crc32table.js */
2+
function signed_crc_table() {
3+
var c, table = new Array(256);
4+
5+
for(var n =0; n != 256; ++n){
6+
c = n;
7+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
8+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
9+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
10+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
11+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
12+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
13+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
14+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
15+
table[n] = c;
16+
}
17+
18+
return table;
19+
}
20+
21+
var table = signed_crc_table();

bits/40_crc.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* charCodeAt is the best approach for binary strings */
2+
function crc32_bstr(bstr) {
3+
for(var crc = -1, i = 0, L=bstr.length-1; i < L;) {
4+
crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i++)) & 0xFF];
5+
crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i++)) & 0xFF];
6+
}
7+
if(i === L) crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i++)) & 0xFF];
8+
return crc ^ -1;
9+
}
10+
11+
function crc32_buf(buf) {
12+
for(var crc = -1, i = 0; i != buf.length; ++i) {
13+
crc = (crc >>> 8) ^ table[(crc ^ buf[i]) & 0xFF];
14+
}
15+
return crc ^ -1;
16+
}
17+
18+
/* much much faster to intertwine utf8 and crc */
19+
function crc32_str(str) {
20+
for(var crc = -1, i = 0, L=str.length, c, d; i < L;) {
21+
c = str.charCodeAt(i++);
22+
if(c < 0x80) {
23+
crc = (crc >>> 8) ^ table[(crc ^ c) & 0xFF];
24+
} else if(c < 0x800) {
25+
crc = (crc >>> 8) ^ table[(crc ^ (192|((c>>6)&31))) & 0xFF];
26+
crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
27+
} else if(c >= 0xD800 && c < 0xE000) {
28+
c = (c&1023)+64; d = str.charCodeAt(i++) & 1023;
29+
crc = (crc >>> 8) ^ table[(crc ^ (240|((c>>8)&7))) & 0xFF];
30+
crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>2)&63))) & 0xFF];
31+
crc = (crc >>> 8) ^ table[(crc ^ (128|((d>>6)&15)|(c&3))) & 0xFF];
32+
crc = (crc >>> 8) ^ table[(crc ^ (128|(d&63))) & 0xFF];
33+
} else {
34+
crc = (crc >>> 8) ^ table[(crc ^ (224|((c>>12)&15))) & 0xFF];
35+
crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>6)&63))) & 0xFF];
36+
crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
37+
}
38+
}
39+
return crc ^ -1;
40+
}

bits/90_exports.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CRC32.table = table;
2+
CRC32.bstr = crc32_bstr;
3+
CRC32.buf = crc32_buf;
4+
CRC32.str = crc32_str;

bits/99_footer.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
})(typeof exports !== "undefined" && typeof DO_NOT_EXPORT_CRC === 'undefined' ? exports : CRC32);

crc32.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* crc32.js (C) 2014 SheetJS -- http://sheetjs.com */
2+
/* vim: set ts=2: */
3+
var CRC32 = {};
4+
(function(CRC32) {
5+
CRC32.version = '0.1.0';
6+
/* see perf/crc32table.js */
7+
function signed_crc_table() {
8+
var c, table = new Array(256);
9+
10+
for(var n =0; n != 256; ++n){
11+
c = n;
12+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
13+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
14+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
15+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
16+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
17+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
18+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
19+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
20+
table[n] = c;
21+
}
22+
23+
return table;
24+
}
25+
26+
var table = signed_crc_table();
27+
/* charCodeAt is the best approach for binary strings */
28+
function crc32_bstr(bstr) {
29+
for(var crc = -1, i = 0, L=bstr.length-1; i < L;) {
30+
crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i++)) & 0xFF];
31+
crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i++)) & 0xFF];
32+
}
33+
if(i === L) crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i++)) & 0xFF];
34+
return crc ^ -1;
35+
}
36+
37+
function crc32_buf(buf) {
38+
for(var crc = -1, i = 0; i != buf.length; ++i) {
39+
crc = (crc >>> 8) ^ table[(crc ^ buf[i]) & 0xFF];
40+
}
41+
return crc ^ -1;
42+
}
43+
44+
/* much much faster to intertwine utf8 and crc */
45+
function crc32_str(str) {
46+
for(var crc = -1, i = 0, L=str.length, c, d; i < L;) {
47+
c = str.charCodeAt(i++);
48+
if(c < 0x80) {
49+
crc = (crc >>> 8) ^ table[(crc ^ c) & 0xFF];
50+
} else if(c < 0x800) {
51+
crc = (crc >>> 8) ^ table[(crc ^ (192|((c>>6)&31))) & 0xFF];
52+
crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
53+
} else if(c >= 0xD800 && c < 0xE000) {
54+
c = (c&1023)+64; d = str.charCodeAt(i++) & 1023;
55+
crc = (crc >>> 8) ^ table[(crc ^ (240|((c>>8)&7))) & 0xFF];
56+
crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>2)&63))) & 0xFF];
57+
crc = (crc >>> 8) ^ table[(crc ^ (128|((d>>6)&15)|(c&3))) & 0xFF];
58+
crc = (crc >>> 8) ^ table[(crc ^ (128|(d&63))) & 0xFF];
59+
} else {
60+
crc = (crc >>> 8) ^ table[(crc ^ (224|((c>>12)&15))) & 0xFF];
61+
crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>6)&63))) & 0xFF];
62+
crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
63+
}
64+
}
65+
return crc ^ -1;
66+
}
67+
CRC32.table = table;
68+
CRC32.bstr = crc32_bstr;
69+
CRC32.buf = crc32_buf;
70+
CRC32.str = crc32_str;
71+
})(typeof exports !== "undefined" && typeof DO_NOT_EXPORT_CRC === 'undefined' ? exports : CRC32);

ctest/crc32.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* crc32.js (C) 2014 SheetJS -- http://sheetjs.com */
2+
/* vim: set ts=2: */
3+
var CRC32 = {};
4+
(function(CRC32) {
5+
CRC32.version = '0.1.0';
6+
/* see perf/crc32table.js */
7+
function signed_crc_table() {
8+
var c, table = new Array(256);
9+
10+
for(var n =0; n != 256; ++n){
11+
c = n;
12+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
13+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
14+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
15+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
16+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
17+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
18+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
19+
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
20+
table[n] = c;
21+
}
22+
23+
return table;
24+
}
25+
26+
var table = signed_crc_table();
27+
/* charCodeAt is the best approach for binary strings */
28+
function crc32_bstr(bstr) {
29+
for(var crc = -1, i = 0, L=bstr.length-1; i < L;) {
30+
crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i++)) & 0xFF];
31+
crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i++)) & 0xFF];
32+
}
33+
if(i === L) crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i++)) & 0xFF];
34+
return crc ^ -1;
35+
}
36+
37+
function crc32_buf(buf) {
38+
for(var crc = -1, i = 0; i != buf.length; ++i) {
39+
crc = (crc >>> 8) ^ table[(crc ^ buf[i]) & 0xFF];
40+
}
41+
return crc ^ -1;
42+
}
43+
44+
/* much much faster to intertwine utf8 and crc */
45+
function crc32_str(str) {
46+
for(var crc = -1, i = 0, L=str.length, c, d; i < L;) {
47+
c = str.charCodeAt(i++);
48+
if(c < 0x80) {
49+
crc = (crc >>> 8) ^ table[(crc ^ c) & 0xFF];
50+
} else if(c < 0x800) {
51+
crc = (crc >>> 8) ^ table[(crc ^ (192|((c>>6)&31))) & 0xFF];
52+
crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
53+
} else if(c >= 0xD800 && c < 0xE000) {
54+
c = (c&1023)+64; d = str.charCodeAt(i++) & 1023;
55+
crc = (crc >>> 8) ^ table[(crc ^ (240|((c>>8)&7))) & 0xFF];
56+
crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>2)&63))) & 0xFF];
57+
crc = (crc >>> 8) ^ table[(crc ^ (128|((d>>6)&15)|(c&3))) & 0xFF];
58+
crc = (crc >>> 8) ^ table[(crc ^ (128|(d&63))) & 0xFF];
59+
} else {
60+
crc = (crc >>> 8) ^ table[(crc ^ (224|((c>>12)&15))) & 0xFF];
61+
crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>6)&63))) & 0xFF];
62+
crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
63+
}
64+
}
65+
return crc ^ -1;
66+
}
67+
CRC32.table = table;
68+
CRC32.bstr = crc32_bstr;
69+
CRC32.buf = crc32_buf;
70+
CRC32.str = crc32_str;
71+
})(typeof exports !== "undefined" && typeof DO_NOT_EXPORT_CRC === 'undefined' ? exports : CRC32);

ctest/fakeassert.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var assert = {};
2+
assert.equal = function(x,y) { if(x !== y) throw x + " !== " + y; };

0 commit comments

Comments
 (0)