|
| 1 | +/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ |
| 2 | +/* vim: set ts=2: */ |
| 3 | +/*exported CRC32 */ |
| 4 | +/*:: declare var DO_NOT_EXPORT_CRC:?boolean; */ |
| 5 | +/*:: declare function define(cb:()=>any):void; */ |
| 6 | +var CRC32/*:CRC32Module*/; |
| 7 | +(function (factory/*:(a:any)=>void*/)/*:void*/ { |
| 8 | +/*jshint ignore:start */ |
| 9 | +/*eslint-disable */ |
| 10 | +if(typeof DO_NOT_EXPORT_CRC === 'undefined') { |
| 11 | +if('object' === typeof exports) { |
| 12 | +factory(exports); |
| 13 | +} else if ('function' === typeof define && define.amd) { |
| 14 | +define(function () { |
| 15 | +var module/*:CRC32Module*/ = /*::(*/{}/*:: :any)*/; |
| 16 | +factory(module); |
| 17 | +return module; |
| 18 | +}); |
| 19 | +} else { |
| 20 | +factory(CRC32 = /*::(*/{}/*:: :any)*/); |
| 21 | +} |
| 22 | +} else { |
| 23 | +factory(CRC32 = /*::(*/{}/*:: :any)*/); |
| 24 | +} |
| 25 | +/*eslint-enable */ |
| 26 | +/*jshint ignore:end */ |
| 27 | +}(function(CRC32/*:CRC32Module*/) { |
| 28 | +CRC32.version = '1.2.0'; |
| 29 | +/*:: |
| 30 | +type CRC32Type = number; |
| 31 | +type ABuf = Array<number> | Buffer | Uint8Array; |
| 32 | +type CRC32TableType = Array<number> | Int32Array; |
| 33 | +*/ |
| 34 | +/* see perf/crc32table.js */ |
| 35 | +/*global Int32Array */ |
| 36 | +function signed_crc_table()/*:CRC32TableType*/ { |
| 37 | +var c = 0, table/*:Array<number>*/ = new Array(256); |
| 38 | + |
| 39 | +for(var n =0; n != 256; ++n){ |
| 40 | +c = n; |
| 41 | +c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1)); |
| 42 | +c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1)); |
| 43 | +c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1)); |
| 44 | +c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1)); |
| 45 | +c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1)); |
| 46 | +c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1)); |
| 47 | +c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1)); |
| 48 | +c = ((c&1) ? (-2097792136 ^ (c >>> 1)) : (c >>> 1)); |
| 49 | +table[n] = c; |
| 50 | +} |
| 51 | + |
| 52 | +return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; |
| 53 | +} |
| 54 | + |
| 55 | +var T = signed_crc_table(); |
| 56 | +/*# charCodeAt is the best approach for binary strings */ |
| 57 | +function crc32_bstr(bstr/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { |
| 58 | +var C = seed/*:: ? 0 : 0 */ ^ -1, L = bstr.length - 1; |
| 59 | +for(var i = 0; i < L;) { |
| 60 | +C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; |
| 61 | +C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; |
| 62 | +} |
| 63 | +if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; |
| 64 | +return C ^ -1; |
| 65 | +} |
| 66 | + |
| 67 | +function crc32_buf(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { |
| 68 | +if(buf.length > 10000) return crc32_buf_8(buf, seed); |
| 69 | +var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 3; |
| 70 | +for(var i = 0; i < L;) { |
| 71 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 72 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 73 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 74 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 75 | +} |
| 76 | +while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 77 | +return C ^ -1; |
| 78 | +} |
| 79 | + |
| 80 | +function crc32_buf_8(buf/*:ABuf*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { |
| 81 | +var C = seed/*:: ? 0 : 0 */ ^ -1, L = buf.length - 7; |
| 82 | +for(var i = 0; i < L;) { |
| 83 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 84 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 85 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 86 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 87 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 88 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 89 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 90 | +C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 91 | +} |
| 92 | +while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; |
| 93 | +return C ^ -1; |
| 94 | +} |
| 95 | + |
| 96 | +/*# much much faster to intertwine utf8 and crc */ |
| 97 | +function crc32_str(str/*:string*/, seed/*:?CRC32Type*/)/*:CRC32Type*/ { |
| 98 | +var C = seed/*:: ? 0 : 0 */ ^ -1; |
| 99 | +for(var i = 0, L=str.length, c, d; i < L;) { |
| 100 | +c = str.charCodeAt(i++); |
| 101 | +if(c < 0x80) { |
| 102 | +C = (C>>>8) ^ T[(C ^ c)&0xFF]; |
| 103 | +} else if(c < 0x800) { |
| 104 | +C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; |
| 105 | +C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; |
| 106 | +} else if(c >= 0xD800 && c < 0xE000) { |
| 107 | +c = (c&1023)+64; d = str.charCodeAt(i++)&1023; |
| 108 | +C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF]; |
| 109 | +C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF]; |
| 110 | +C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF]; |
| 111 | +C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF]; |
| 112 | +} else { |
| 113 | +C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF]; |
| 114 | +C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF]; |
| 115 | +C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; |
| 116 | +} |
| 117 | +} |
| 118 | +return C ^ -1; |
| 119 | +} |
| 120 | +CRC32.table = T; |
| 121 | +// $FlowIgnore |
| 122 | +CRC32.bstr = crc32_bstr; |
| 123 | +// $FlowIgnore |
| 124 | +CRC32.buf = crc32_buf; |
| 125 | +// $FlowIgnore |
| 126 | +CRC32.str = crc32_str; |
| 127 | +})); |
0 commit comments