crc16xmodem.js 1011 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _buffer = require('buffer');
  6. var _create_buffer = require('./create_buffer');
  7. var _create_buffer2 = _interopRequireDefault(_create_buffer);
  8. var _define_crc = require('./define_crc');
  9. var _define_crc2 = _interopRequireDefault(_define_crc);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var crc16xmodem = (0, _define_crc2.default)('xmodem', function (buf, previous) {
  12. if (!_buffer.Buffer.isBuffer(buf)) buf = (0, _create_buffer2.default)(buf);
  13. var crc = typeof previous !== 'undefined' ? ~~previous : 0x0;
  14. for (var index = 0; index < buf.length; index++) {
  15. var byte = buf[index];
  16. var code = crc >>> 8 & 0xff;
  17. code ^= byte & 0xff;
  18. code ^= code >>> 4;
  19. crc = crc << 8 & 0xffff;
  20. crc ^= code;
  21. code = code << 5 & 0xffff;
  22. crc ^= code;
  23. code = code << 7 & 0xffff;
  24. crc ^= code;
  25. }
  26. return crc;
  27. });
  28. exports.default = crc16xmodem;