crc.js 855 B

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