-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrc32.js
62 lines (57 loc) · 1.68 KB
/
crc32.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
(function(window, undefined) {
var arrayType = Array,
functionStr = 'function',
objectStr = 'object',
CRC32_TABLE;
if (typeof Uint32Array === functionStr) {
arrayType = Uint32Array;
}
function stringToArray(string) {
var l = string.length,
bytes = new arrayType(l),
i = 0;
for (; i < l; i++) {
bytes[i] = string.charCodeAt(i);
}
return bytes;
}
function genCRC32Table() {
var i = 0, c = 0, b = 0;
CRC32_TABLE = new arrayType(256);
for (; i < 256; i++) {
c = i;
b = 8;
while (b--) {
c = (c >>> 1) ^ ((c & 1) ? 0xEDB88320 : 0);
}
CRC32_TABLE[i] = c;
}
}
function crc32(arr) {
var values = typeof arr === 'string' ? stringToArray(arr) : (arr.models || arr),
crc = -1,
i = 0,
l = values.length,
isObjects = typeof values[0] === objectStr,
id = 0;
if (CRC32_TABLE === undefined) {
genCRC32Table();
}
for (; i < l; i++) {
id = isObjects ? (values[i].id >>> 0) : values[i];
crc = CRC32_TABLE[(crc ^ id) & 0xFF] ^ (crc >>> 8);
}
//bitflip then cast to 32-bit unsigned
return (~crc >>> 0).toString(16);
}
if (typeof module !== 'undefined') {
module.exports = crc32;
} else {
window.crc32 = crc32;
if (typeof define === functionStr && typeof define.amd === objectStr && define.amd) {
define('crc32', function() {
return crc32;
});
}
}
}(this));