-
Notifications
You must be signed in to change notification settings - Fork 2
/
crc.js
106 lines (99 loc) · 3.02 KB
/
crc.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
/*
* js-crc v0.1.0
* https://github.com/emn178/js-crc
*
* Copyright 2015, [email protected]
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
;(function(root, undefined) {
'use strict';
var NODE_JS = typeof(module) != 'undefined';
if(NODE_JS) {
root = global;
}
var HEX_CHARS = '0123456789abcdef'.split('');
var Modules = [
{
name: 'crc32',
polynom: 0xEDB88320,
initValue: -1,
bytes: 4
},
{
name: 'crc16',
polynom: 0xA001,
initValue: 0,
bytes: 2
}
];
var i, j, k, b;
for(i = 0;i < Modules.length;++i) {
var m = Modules[i];
m.method = (function(m) {
return function(message) {
return crc(message, m);
};
})(m);
m.table = [];
for(j = 0;j < 256;++j) {
b = j;
for(k = 0;k < 8;++k) {
b = b & 1 ? m.polynom ^ (b >>> 1) : b >>> 1;
}
m.table[j] = b >>> 0;
}
}
var crc = function(message, module) {
var notString = typeof(message) != 'string';
if(notString && message.constructor == ArrayBuffer) {
message = new Uint8Array(message);
}
var crc = module.initValue, code, i, length = message.length, table = module.table;
if(notString) {
for(i = 0;i < length;++i) {
crc = table[(crc ^ message[i]) & 0xFF] ^ (crc >>> 8);
}
} else {
for(i = 0;i < length;++i) {
code = message.charCodeAt(i);
if (code < 0x80) {
crc = table[(crc ^ code) & 0xFF] ^ (crc >>> 8);
} else if (code < 0x800) {
crc = table[(crc ^ (0xc0 | (code >> 6))) & 0xFF] ^ (crc >>> 8);
crc = table[(crc ^ (0x80 | (code & 0x3f))) & 0xFF] ^ (crc >>> 8);
} else if (code < 0xd800 || code >= 0xe000) {
crc = table[(crc ^ (0xe0 | (code >> 12))) & 0xFF] ^ (crc >>> 8);
crc = table[(crc ^ (0x80 | ((code >> 6) & 0x3f))) & 0xFF] ^ (crc >>> 8);
crc = table[(crc ^ (0x80 | (code & 0x3f))) & 0xFF] ^ (crc >>> 8);
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++i) & 0x3ff));
crc = table[(crc ^ (0xf0 | (code >> 18))) & 0xFF] ^ (crc >>> 8);
crc = table[(crc ^ (0x80 | ((code >> 12) & 0x3f))) & 0xFF] ^ (crc >>> 8);
crc = table[(crc ^ (0x80 | ((code >> 6) & 0x3f))) & 0xFF] ^ (crc >>> 8);
crc = table[(crc ^ (0x80 | (code & 0x3f))) & 0xFF] ^ (crc >>> 8);
}
}
}
crc ^= module.initValue;
var hex = '';
if(module.bytes > 2) {
hex += HEX_CHARS[(crc >> 28) & 0x0F] + HEX_CHARS[(crc >> 24) & 0x0F] +
HEX_CHARS[(crc >> 20) & 0x0F] + HEX_CHARS[(crc >> 16) & 0x0F];
}
hex += HEX_CHARS[(crc >> 12) & 0x0F] + HEX_CHARS[(crc >> 8) & 0x0F] +
HEX_CHARS[(crc >> 4) & 0x0F] + HEX_CHARS[crc & 0x0F];
return hex;
};
var exports;
if(!root.HI_CRC32_TEST && NODE_JS) {
exports = module.exports = {};
} else if(root) {
exports = root;
}
for(i = 0;i < Modules.length;++i) {
var m = Modules[i];
exports[m.name] = m.method;
}
}(this));