2016-06-16 20:49:46 +00:00
|
|
|
/*# charCodeAt is the best approach for binary strings */
|
2016-10-08 18:48:03 +00:00
|
|
|
function crc32_bstr(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
|
|
|
|
var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 1;
|
2014-06-17 23:20:41 +00:00
|
|
|
for(var i = 0; i < L;) {
|
2016-06-16 20:49:46 +00:00
|
|
|
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
2016-06-16 20:49:46 +00:00
|
|
|
if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF];
|
|
|
|
return C ^ -1;
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:48:03 +00:00
|
|
|
function crc32_buf(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
|
|
|
|
if(buf.length > 10000) return crc32_buf_8(buf, seed);
|
|
|
|
var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 3;
|
2016-06-16 20:49:46 +00:00
|
|
|
for(var i = 0; i < L;) {
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
2016-06-16 20:49:46 +00:00
|
|
|
while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
return C ^ -1;
|
2014-06-21 02:15:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:48:03 +00:00
|
|
|
function crc32_buf_8(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
|
|
|
|
var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 7;
|
2016-06-16 20:49:46 +00:00
|
|
|
for(var i = 0; i < L;) {
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
2014-06-21 02:15:23 +00:00
|
|
|
}
|
2016-06-16 20:49:46 +00:00
|
|
|
while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
|
|
|
|
return C ^ -1;
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 19:44:45 +00:00
|
|
|
/*# much much faster to intertwine utf8 and crc */
|
2016-10-08 18:48:03 +00:00
|
|
|
function crc32_str(str/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ {
|
|
|
|
var C = seed/*:: ? 0 : 0 */ ^ -1;
|
2016-06-16 20:49:46 +00:00
|
|
|
for(var i = 0, L=str.length, c, d; i < L;) {
|
2014-06-16 21:27:47 +00:00
|
|
|
c = str.charCodeAt(i++);
|
|
|
|
if(c < 0x80) {
|
2016-06-16 20:49:46 +00:00
|
|
|
C = (C>>>8) ^ T[(C ^ c)&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
} else if(c < 0x800) {
|
2016-06-16 20:49:46 +00:00
|
|
|
C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
} else if(c >= 0xD800 && c < 0xE000) {
|
2016-06-16 20:49:46 +00:00
|
|
|
c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
|
|
|
|
C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
} else {
|
2016-06-16 20:49:46 +00:00
|
|
|
C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 20:49:46 +00:00
|
|
|
return C ^ -1;
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|