853 BJavaScriptView Raw
1"use strict";
2
3let crcTable = [];
4
5(function () {
6 for (let i = 0; i < 256; i++) {
7 let currentCrc = i;
8 for (let j = 0; j < 8; j++) {
9 if (currentCrc & 1) {
10 currentCrc = 0xedb88320 ^ (currentCrc >>> 1);
11 } else {
12 currentCrc = currentCrc >>> 1;
13 }
14 }
15 crcTable[i] = currentCrc;
16 }
17})();
18
19let CrcCalculator = (module.exports = function () {
20 this._crc = -1;
21});
22
23CrcCalculator.prototype.write = function (data) {
24 for (let i = 0; i < data.length; i++) {
25 this._crc = crcTable[(this._crc ^ data[i]) & 0xff] ^ (this._crc >>> 8);
26 }
27 return true;
28};
29
30CrcCalculator.prototype.crc32 = function () {
31 return this._crc ^ -1;
32};
33
34CrcCalculator.crc32 = function (buf) {
35 let crc = -1;
36 for (let i = 0; i < buf.length; i++) {
37 crc = crcTable[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8);
38 }
39 return crc ^ -1;
40};