UNPKG

1.19 kBJavaScriptView Raw
1/**
2 * Copyright 2013-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 *
10 */
11
12'use strict';
13
14var MOD = 65521;
15
16// adler32 is not cryptographically strong, and is only used to sanity check that
17// markup generated on the server matches the markup generated on the client.
18// This implementation (a modified version of the SheetJS version) has been optimized
19// for our use case, at the expense of conforming to the adler32 specification
20// for non-ascii inputs.
21function adler32(data) {
22 var a = 1;
23 var b = 0;
24 var i = 0;
25 var l = data.length;
26 var m = l & ~0x3;
27 while (i < m) {
28 var n = Math.min(i + 4096, m);
29 for (; i < n; i += 4) {
30 b += (a += data.charCodeAt(i)) + (a += data.charCodeAt(i + 1)) + (a += data.charCodeAt(i + 2)) + (a += data.charCodeAt(i + 3));
31 }
32 a %= MOD;
33 b %= MOD;
34 }
35 for (; i < l; i++) {
36 b += a += data.charCodeAt(i);
37 }
38 a %= MOD;
39 b %= MOD;
40 return a | b << 16;
41}
42
43module.exports = adler32;
\No newline at end of file