Skip to content

Instantly share code, notes, and snippets.

@chp-io
Created March 30, 2015 05:01
Show Gist options
  • Save chp-io/c67e2685f4f681a197e1 to your computer and use it in GitHub Desktop.
Save chp-io/c67e2685f4f681a197e1 to your computer and use it in GitHub Desktop.
crc32
// from https://github.com/gildas-lormeau/zip.js/blob/master/WebContent/zip.js
function Crc32() {
this.crc = -1;
}
Crc32.prototype.append = function append(data) {
var crc = this.crc | 0, table = this.table;
for (var offset = 0, len = data.length | 0; offset < len; offset++)
crc = (crc >>> 8) ^ table[(crc ^ data[offset]) & 0xFF];
this.crc = crc;
};
Crc32.prototype.get = function get() {
return ~this.crc;
};
Crc32.prototype.table = (function() {
var i, j, t, table = []; // Uint32Array is actually slower than []
for (i = 0; i < 256; i++) {
t = i;
for (j = 0; j < 8; j++)
if (t & 1)
t = (t >>> 1) ^ 0xEDB88320;
else
t = t >>> 1;
table[i] = t;
}
return table;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment