forked from Ylianst/MeshCommander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlib-adler32.js
279 lines (246 loc) · 9.99 KB
/
zlib-adler32.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* zlib-adler32.js -- JavaScript implementation for the zlib adler32.
Version: 0.2.0
LastModified: Apr 12 2012
Copyright (C) 2012 Masanao Izumo <[email protected]>
API documentation
==============================================================================
Usage: adler = ZLIB.adler32(adler, buf, offset, len);
Update a running Adler-32 checksum with the bytes buf[offset..offset+len-1] and
return the updated checksum. If buf is null, this function returns the
required initial value for the checksum.
An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
much faster.
Usage example:
var adler = ZLIB.adler32(0, null, 0, 0);
while (read_buffer(buffer, length) != EOF) {
adler = ZLIB.adler32(adler, buffer, 0, length);
}
if (adler != original_adler) error();
==============================================================================
Usage: adler = ZLIB.adler32_combine(adler1, adler2, len2);
Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note
that the z_off_t type (like off_t) is a signed integer. If len2 is
negative, the result has no meaning or utility.
*/
if( typeof ZLIB === 'undefined' ) {
alert('ZLIB is not defined. SRC zlib.js before zlib-adler32.js')
}
(function() {
/* adler32.c -- compute the Adler-32 checksum of a data stream
* Copyright (C) 1995-2011 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
var BASE = 65521; /* largest prime smaller than 65536 */
var NMAX = 5552;
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
/* ========================================================================= */
function adler32_string(adler, buf, offset, len)
{
var sum2;
var n;
/* split Adler-32 into component sums */
sum2 = (adler >>> 16) & 0xffff;
adler &= 0xffff;
/* in case user likes doing a byte at a time, keep it fast */
if (len == 1) {
adler += buf.charCodeAt(offset) & 0xff;
if (adler >= BASE)
adler -= BASE;
sum2 += adler;
if (sum2 >= BASE)
sum2 -= BASE;
return adler | (sum2 << 16);
}
/* initial Adler-32 value (deferred check for len == 1 speed) */
if (buf === null)
return 1;
/* in case short lengths are provided, keep it somewhat fast */
if (len < 16) {
while (len--) {
adler += buf.charCodeAt(offset++) & 0xff;
sum2 += adler;
}
if (adler >= BASE)
adler -= BASE;
sum2 %= BASE; /* only added so many BASE's */
return adler | (sum2 << 16);
}
/* do length NMAX blocks -- requires just one modulo operation */
while (len >= NMAX) {
len -= NMAX;
n = NMAX >> 4; /* NMAX is divisible by 16 */
do {
/* 16 sums unrolled */
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
} while (--n);
adler %= BASE;
sum2 %= BASE;
}
/* do remaining bytes (less than NMAX, still just one modulo) */
if (len) { /* avoid modulos if none remaining */
while (len >= 16) {
len -= 16;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
}
while (len--) {
adler += buf.charCodeAt(offset++) & 0xff; sum2 += adler;
}
adler %= BASE;
sum2 %= BASE;
}
/* return recombined sums */
return adler | (sum2 << 16);
}
/* ========================================================================= */
function adler32_array(adler, buf, offset, len)
{
var sum2;
var n;
/* split Adler-32 into component sums */
sum2 = (adler >>> 16) & 0xffff;
adler &= 0xffff;
/* in case user likes doing a byte at a time, keep it fast */
if (len == 1) {
adler += buf[offset];
if (adler >= BASE)
adler -= BASE;
sum2 += adler;
if (sum2 >= BASE)
sum2 -= BASE;
return adler | (sum2 << 16);
}
/* initial Adler-32 value (deferred check for len == 1 speed) */
if (buf === null)
return 1;
/* in case short lengths are provided, keep it somewhat fast */
if (len < 16) {
while (len--) {
adler += buf[offset++];
sum2 += adler;
}
if (adler >= BASE)
adler -= BASE;
sum2 %= BASE; /* only added so many BASE's */
return adler | (sum2 << 16);
}
/* do length NMAX blocks -- requires just one modulo operation */
while (len >= NMAX) {
len -= NMAX;
n = NMAX >> 4; /* NMAX is divisible by 16 */
do {
/* 16 sums unrolled */
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
} while (--n);
adler %= BASE;
sum2 %= BASE;
}
/* do remaining bytes (less than NMAX, still just one modulo) */
if (len) { /* avoid modulos if none remaining */
while (len >= 16) {
len -= 16;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
adler += buf[offset++]; sum2 += adler;
}
while (len--) {
adler += buf[offset++]; sum2 += adler;
}
adler %= BASE;
sum2 %= BASE;
}
/* return recombined sums */
return adler | (sum2 << 16);
}
/* ========================================================================= */
ZLIB.adler32 = function(adler, buf, offset, len)
{
if(typeof buf === 'string') {
return adler32_string(adler, buf, offset, len);
} else {
return adler32_array(adler, buf, offset, len);
}
};
ZLIB.adler32_combine = function(adler1, adler2, len2)
{
var sum1;
var sum2;
var rem;
/* for negative len, return invalid adler32 as a clue for debugging */
if (len2 < 0)
return 0xffffffff;
/* the derivation of this formula is left as an exercise for the reader */
len2 %= BASE; /* assumes len2 >= 0 */
rem = len2;
sum1 = adler1 & 0xffff;
sum2 = rem * sum1;
sum2 %= BASE;
sum1 += (adler2 & 0xffff) + BASE - 1;
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
if (sum1 >= BASE) sum1 -= BASE;
if (sum1 >= BASE) sum1 -= BASE;
if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
if (sum2 >= BASE) sum2 -= BASE;
return sum1 | (sum2 << 16);
}
}());