2018-08-14 12:11:49 +00:00
|
|
|
// Note: we can't get significant speed boost here.
|
|
|
|
// So write code to minimize size - no pregenerated tables
|
|
|
|
// and array tools dependencies.
|
|
|
|
|
|
|
|
|
|
|
|
// Use ordinary array, since untyped makes no boost here
|
2018-08-27 12:30:50 +00:00
|
|
|
export default function makeTable() {
|
|
|
|
var c, table = [];
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
for (var n = 0; n < 256; n++) {
|
|
|
|
c = n;
|
|
|
|
for (var k = 0; k < 8; k++) {
|
2018-08-27 12:30:50 +00:00
|
|
|
c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
table[n] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create table on load. Just 255 signed longs. Not a problem.
|
|
|
|
var crcTable = makeTable();
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
|
2018-08-14 12:11:49 +00:00
|
|
|
function crc32(crc, buf, len, pos) {
|
|
|
|
var t = crcTable,
|
|
|
|
end = pos + len;
|
|
|
|
|
|
|
|
crc ^= -1;
|
|
|
|
|
|
|
|
for (var i = pos; i < end; i++) {
|
2018-08-27 12:30:50 +00:00
|
|
|
crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & 0xFF];
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
return (crc ^ (-1)); // >>> 0;
|
|
|
|
}
|