adler32.js 678 B

12345678910111213141516171819202122232425262728293031
  1. // Note: adler32 takes 12% for level 0 and 2% for level 6.
  2. // It doesn't worth to make additional optimizationa as in original.
  3. // Small size is preferable.
  4. function adler32(adler, buf, len, pos) {
  5. var s1 = (adler & 0xffff) |0,
  6. s2 = ((adler >>> 16) & 0xffff) |0,
  7. n = 0;
  8. while (len !== 0) {
  9. // Set limit ~ twice less than 5552, to keep
  10. // s2 in 31-bits, because we force signed ints.
  11. // in other case %= will fail.
  12. n = len > 2000 ? 2000 : len;
  13. len -= n;
  14. do {
  15. s1 = (s1 + buf[pos++]) |0;
  16. s2 = (s2 + s1) |0;
  17. } while (--n);
  18. s1 %= 65521;
  19. s2 %= 65521;
  20. }
  21. return (s1 | (s2 << 16)) |0;
  22. }
  23. export default adler32;