crc16xmodem.js 669 B

123456789101112131415161718192021222324252627
  1. import { Buffer } from 'buffer';
  2. import createBuffer from './create_buffer';
  3. import defineCrc from './define_crc';
  4. const crc16xmodem = defineCrc('xmodem', function(buf, previous) {
  5. if (!Buffer.isBuffer(buf)) buf = createBuffer(buf);
  6. let crc = typeof previous !== 'undefined' ? ~~previous : 0x0;
  7. for (let index = 0; index < buf.length; index++) {
  8. const byte = buf[index];
  9. let code = (crc >>> 8) & 0xff;
  10. code ^= byte & 0xff;
  11. code ^= code >>> 4;
  12. crc = (crc << 8) & 0xffff;
  13. crc ^= code;
  14. code = (code << 5) & 0xffff;
  15. crc ^= code;
  16. code = (code << 7) & 0xffff;
  17. crc ^= code;
  18. }
  19. return crc;
  20. });
  21. export default crc16xmodem;