adler32.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  3. // It isn't worth it to make additional optimizations as in original.
  4. // Small size is preferable.
  5. // (C) 1995-2013 Jean-loup Gailly and Mark Adler
  6. // (C) 2014-2017 Vitaly Puzrin and Andrey Tupitsin
  7. //
  8. // This software is provided 'as-is', without any express or implied
  9. // warranty. In no event will the authors be held liable for any damages
  10. // arising from the use of this software.
  11. //
  12. // Permission is granted to anyone to use this software for any purpose,
  13. // including commercial applications, and to alter it and redistribute it
  14. // freely, subject to the following restrictions:
  15. //
  16. // 1. The origin of this software must not be misrepresented; you must not
  17. // claim that you wrote the original software. If you use this software
  18. // in a product, an acknowledgment in the product documentation would be
  19. // appreciated but is not required.
  20. // 2. Altered source versions must be plainly marked as such, and must not be
  21. // misrepresented as being the original software.
  22. // 3. This notice may not be removed or altered from any source distribution.
  23. function adler32(adler, buf, len, pos) {
  24. var s1 = (adler & 0xffff) |0,
  25. s2 = ((adler >>> 16) & 0xffff) |0,
  26. n = 0;
  27. while (len !== 0) {
  28. // Set limit ~ twice less than 5552, to keep
  29. // s2 in 31-bits, because we force signed ints.
  30. // in other case %= will fail.
  31. n = len > 2000 ? 2000 : len;
  32. len -= n;
  33. do {
  34. s1 = (s1 + buf[pos++]) |0;
  35. s2 = (s2 + s1) |0;
  36. } while (--n);
  37. s1 %= 65521;
  38. s2 %= 65521;
  39. }
  40. return (s1 | (s2 << 16)) |0;
  41. }
  42. module.exports = adler32;