crc.js 853 B

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