forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadler32.js
More file actions
49 lines (45 loc) · 1.26 KB
/
adler32.js
File metadata and controls
49 lines (45 loc) · 1.26 KB
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
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule adler32
* @flow
*/
'use strict';
var MOD = 65521;
// adler32 is not cryptographically strong, and is only used to sanity check that
// markup generated on the server matches the markup generated on the client.
// This implementation (a modified version of the SheetJS version) has been optimized
// for our use case, at the expense of conforming to the adler32 specification
// for non-ascii inputs.
function adler32(data: string): number {
var a = 1;
var b = 0;
var i = 0;
var l = data.length;
var m = l & ~0x3;
while (i < m) {
var n = Math.min(i + 4096, m);
for (; i < n; i += 4) {
b += (
(a += data.charCodeAt(i)) +
(a += data.charCodeAt(i + 1)) +
(a += data.charCodeAt(i + 2)) +
(a += data.charCodeAt(i + 3))
);
}
a %= MOD;
b %= MOD;
}
for (; i < l; i++) {
b += (a += data.charCodeAt(i));
}
a %= MOD;
b %= MOD;
return a | (b << 16);
}
module.exports = adler32;