2014-06-16 21:27:47 +00:00
|
|
|
/* charCodeAt is the best approach for binary strings */
|
2014-06-17 23:20:41 +00:00
|
|
|
var use_buffer = typeof Buffer !== 'undefined';
|
2015-05-06 21:47:18 +00:00
|
|
|
function crc32_bstr(bstr/*:string*/)/*:CRC32Type*/ {
|
|
|
|
if(bstr.length > 32768) if(use_buffer) return crc32_buf_8(new Buffer(bstr));
|
2014-06-17 23:20:41 +00:00
|
|
|
var crc = -1, L = bstr.length - 1;
|
|
|
|
for(var i = 0; i < L;) {
|
|
|
|
crc = table[(crc ^ bstr.charCodeAt(i++)) & 0xFF] ^ (crc >>> 8);
|
|
|
|
crc = table[(crc ^ bstr.charCodeAt(i++)) & 0xFF] ^ (crc >>> 8);
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
2014-06-17 23:20:41 +00:00
|
|
|
if(i === L) crc = (crc >>> 8) ^ table[(crc ^ bstr.charCodeAt(i)) & 0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
return crc ^ -1;
|
|
|
|
}
|
|
|
|
|
2015-05-06 21:47:18 +00:00
|
|
|
function crc32_buf(buf/*:ABuf*/)/*:CRC32Type*/ {
|
2014-06-21 02:15:23 +00:00
|
|
|
if(buf.length > 10000) return crc32_buf_8(buf);
|
2014-06-17 23:20:41 +00:00
|
|
|
for(var crc = -1, i = 0, L=buf.length-3; i < L;) {
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
2014-06-21 02:15:23 +00:00
|
|
|
while(i < L+3) crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
return crc ^ -1;
|
|
|
|
}
|
|
|
|
|
2015-05-06 21:47:18 +00:00
|
|
|
function crc32_buf_8(buf/*:ABuf*/)/*:CRC32Type*/ {
|
2014-06-21 02:15:23 +00:00
|
|
|
for(var crc = -1, i = 0, L=buf.length-7; i < L;) {
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
2014-06-17 23:20:41 +00:00
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
2014-06-21 02:15:23 +00:00
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
|
|
|
}
|
|
|
|
while(i < L+7) crc = (crc >>> 8) ^ table[(crc^buf[i++])&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
return crc ^ -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* much much faster to intertwine utf8 and crc */
|
2015-05-06 21:47:18 +00:00
|
|
|
function crc32_str(str/*:string*/)/*:CRC32Type*/ {
|
2014-06-16 21:27:47 +00:00
|
|
|
for(var crc = -1, i = 0, L=str.length, c, d; i < L;) {
|
|
|
|
c = str.charCodeAt(i++);
|
|
|
|
if(c < 0x80) {
|
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ c) & 0xFF];
|
|
|
|
} else if(c < 0x800) {
|
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ (192|((c>>6)&31))) & 0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
|
|
|
|
} else if(c >= 0xD800 && c < 0xE000) {
|
|
|
|
c = (c&1023)+64; d = str.charCodeAt(i++) & 1023;
|
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ (240|((c>>8)&7))) & 0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>2)&63))) & 0xFF];
|
2016-01-13 03:30:35 +00:00
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ (128|((d>>6)&15)|((c&3)<<4))) & 0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ (128|(d&63))) & 0xFF];
|
|
|
|
} else {
|
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ (224|((c>>12)&15))) & 0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ (128|((c>>6)&63))) & 0xFF];
|
|
|
|
crc = (crc >>> 8) ^ table[(crc ^ (128|(c&63))) & 0xFF];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return crc ^ -1;
|
|
|
|
}
|