crc16_xmodem.js 790 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var _buffer = require('buffer');
  3. var _define_crc = require('./define_crc');
  4. var _define_crc2 = _interopRequireDefault(_define_crc);
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6. module.exports = (0, _define_crc2.default)('xmodem', function (buf, previous) {
  7. if (!_buffer.Buffer.isBuffer(buf)) buf = (0, _buffer.Buffer)(buf);
  8. var crc = typeof previous !== 'undefined' ? ~ ~previous : 0x0;
  9. for (var index = 0; index < buf.length; index++) {
  10. var byte = buf[index];
  11. var code = crc >>> 8 & 0xFF;
  12. code ^= byte & 0xFF;
  13. code ^= code >>> 4;
  14. crc = crc << 8 & 0xFFFF;
  15. crc ^= code;
  16. code = code << 5 & 0xFFFF;
  17. crc ^= code;
  18. code = code << 7 & 0xFFFF;
  19. crc ^= code;
  20. }
  21. return crc;
  22. });