crc32.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
  2. /* vim: set ts=2: */
  3. /*exported CRC32 */
  4. var CRC32;
  5. (function (factory) {
  6. /*jshint ignore:start */
  7. /*eslint-disable */
  8. if(typeof DO_NOT_EXPORT_CRC === 'undefined') {
  9. if('object' === typeof exports) {
  10. factory(exports);
  11. } else if ('function' === typeof define && define.amd) {
  12. define(function () {
  13. var module = {};
  14. factory(module);
  15. return module;
  16. });
  17. } else {
  18. factory(CRC32 = {});
  19. }
  20. } else {
  21. factory(CRC32 = {});
  22. }
  23. /*eslint-enable */
  24. /*jshint ignore:end */
  25. }(function(CRC32) {
  26. CRC32.version = '1.2.3';
  27. /*global Int32Array */
  28. function signed_crc_table() {
  29. var c = 0, table = new Array(256);
  30. for(var n =0; n != 256; ++n){
  31. c = n;
  32. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  33. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  34. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  35. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  36. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  37. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  38. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  39. c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
  40. table[n] = c;
  41. }
  42. return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
  43. }
  44. var T0 = signed_crc_table();
  45. function slice_by_16_tables(T) {
  46. var c = 0, v = 0, n = 0, table = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ;
  47. for(n = 0; n != 256; ++n) table[n] = T[n];
  48. for(n = 0; n != 256; ++n) {
  49. v = T[n];
  50. for(c = 256 + n; c < 4096; c += 256) v = table[c] = (v >>> 8) ^ T[v & 0xFF];
  51. }
  52. var out = [];
  53. for(n = 1; n != 16; ++n) out[n - 1] = typeof Int32Array !== 'undefined' ? table.subarray(n * 256, n * 256 + 256) : table.slice(n * 256, n * 256 + 256);
  54. return out;
  55. }
  56. var TT = slice_by_16_tables(T0);
  57. var T1 = TT[0], T2 = TT[1], T3 = TT[2], T4 = TT[3], T5 = TT[4];
  58. var T6 = TT[5], T7 = TT[6], T8 = TT[7], T9 = TT[8], Ta = TT[9];
  59. var Tb = TT[10], Tc = TT[11], Td = TT[12], Te = TT[13], Tf = TT[14];
  60. function crc32_bstr(bstr, seed) {
  61. var C = seed ^ -1;
  62. for(var i = 0, L = bstr.length; i < L;) C = (C>>>8) ^ T0[(C^bstr.charCodeAt(i++))&0xFF];
  63. return ~C;
  64. }
  65. function crc32_buf(B, seed) {
  66. var C = seed ^ -1, L = B.length - 15, i = 0;
  67. for(; i < L;) C =
  68. Tf[B[i++] ^ (C & 255)] ^
  69. Te[B[i++] ^ ((C >> 8) & 255)] ^
  70. Td[B[i++] ^ ((C >> 16) & 255)] ^
  71. Tc[B[i++] ^ (C >>> 24)] ^
  72. Tb[B[i++]] ^ Ta[B[i++]] ^ T9[B[i++]] ^ T8[B[i++]] ^
  73. T7[B[i++]] ^ T6[B[i++]] ^ T5[B[i++]] ^ T4[B[i++]] ^
  74. T3[B[i++]] ^ T2[B[i++]] ^ T1[B[i++]] ^ T0[B[i++]];
  75. L += 15;
  76. while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF];
  77. return ~C;
  78. }
  79. function crc32_str(str, seed) {
  80. var C = seed ^ -1;
  81. for(var i = 0, L = str.length, c = 0, d = 0; i < L;) {
  82. c = str.charCodeAt(i++);
  83. if(c < 0x80) {
  84. C = (C>>>8) ^ T0[(C^c)&0xFF];
  85. } else if(c < 0x800) {
  86. C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF];
  87. C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF];
  88. } else if(c >= 0xD800 && c < 0xE000) {
  89. c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
  90. C = (C>>>8) ^ T0[(C ^ (240|((c>>8)&7)))&0xFF];
  91. C = (C>>>8) ^ T0[(C ^ (128|((c>>2)&63)))&0xFF];
  92. C = (C>>>8) ^ T0[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
  93. C = (C>>>8) ^ T0[(C ^ (128|(d&63)))&0xFF];
  94. } else {
  95. C = (C>>>8) ^ T0[(C ^ (224|((c>>12)&15)))&0xFF];
  96. C = (C>>>8) ^ T0[(C ^ (128|((c>>6)&63)))&0xFF];
  97. C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF];
  98. }
  99. }
  100. return ~C;
  101. }
  102. CRC32.table = T0;
  103. // $FlowIgnore
  104. CRC32.bstr = crc32_bstr;
  105. // $FlowIgnore
  106. CRC32.buf = crc32_buf;
  107. // $FlowIgnore
  108. CRC32.str = crc32_str;
  109. }));