adler32.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* adler32.js (C) 2014-present SheetJS -- http://sheetjs.com */
  2. /* vim: set ts=2: */
  3. /*exported ADLER32 */
  4. var ADLER32;
  5. (function (factory) {
  6. /*jshint ignore:start */
  7. /*eslint-disable */
  8. if(typeof DO_NOT_EXPORT_ADLER === '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(ADLER32 = {});
  19. }
  20. } else {
  21. factory(ADLER32 = {});
  22. }
  23. /*eslint-enable */
  24. /*jshint ignore:end */
  25. }(function(ADLER32) {
  26. ADLER32.version = '1.3.1';
  27. function adler32_bstr(bstr, seed) {
  28. var a = 1, b = 0, L = bstr.length, M = 0;
  29. if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
  30. for(var i = 0; i < L;) {
  31. M = Math.min(L-i, 2654)+i;
  32. for(;i<M;i++) {
  33. a += bstr.charCodeAt(i)&0xFF;
  34. b += a;
  35. }
  36. a = (15*(a>>>16)+(a&65535));
  37. b = (15*(b>>>16)+(b&65535));
  38. }
  39. return ((b%65521) << 16) | (a%65521);
  40. }
  41. function adler32_buf(buf, seed) {
  42. var a = 1, b = 0, L = buf.length, M = 0;
  43. if(typeof seed === 'number') { a = seed & 0xFFFF; b = (seed >>> 16) & 0xFFFF; }
  44. for(var i = 0; i < L;) {
  45. M = Math.min(L-i, 2654)+i;
  46. for(;i<M;i++) {
  47. a += buf[i]&0xFF;
  48. b += a;
  49. }
  50. a = (15*(a>>>16)+(a&65535));
  51. b = (15*(b>>>16)+(b&65535));
  52. }
  53. return ((b%65521) << 16) | (a%65521);
  54. }
  55. function adler32_str(str, seed) {
  56. var a = 1, b = 0, L = str.length, M = 0, c = 0, d = 0;
  57. if(typeof seed === 'number') { a = seed & 0xFFFF; b = seed >>> 16; }
  58. for(var i = 0; i < L;) {
  59. M = Math.min(L-i, 2918);
  60. while(M>0) {
  61. c = str.charCodeAt(i++);
  62. if(c < 0x80) { a += c; }
  63. else if(c < 0x800) {
  64. a += 192|((c>>6)&31); b += a; --M;
  65. a += 128|(c&63);
  66. } else if(c >= 0xD800 && c < 0xE000) {
  67. c = (c&1023)+64; d = str.charCodeAt(i++) & 1023;
  68. a += 240|((c>>8)&7); b += a; --M;
  69. a += 128|((c>>2)&63); b += a; --M;
  70. a += 128|((d>>6)&15)|((c&3)<<4); b += a; --M;
  71. a += 128|(d&63);
  72. } else {
  73. a += 224|((c>>12)&15); b += a; --M;
  74. a += 128|((c>>6)&63); b += a; --M;
  75. a += 128|(c&63);
  76. }
  77. b += a; --M;
  78. }
  79. a = (15*(a>>>16)+(a&65535));
  80. b = (15*(b>>>16)+(b&65535));
  81. }
  82. return ((b%65521) << 16) | (a%65521);
  83. }
  84. // $FlowIgnore
  85. ADLER32.bstr = adler32_bstr;
  86. // $FlowIgnore
  87. ADLER32.buf = adler32_buf;
  88. // $FlowIgnore
  89. ADLER32.str = adler32_str;
  90. }));