Created
March 30, 2015 05:01
-
-
Save chp-io/c67e2685f4f681a197e1 to your computer and use it in GitHub Desktop.
crc32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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