UNPKG

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