You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1011 B
42 lines
1011 B
6 years ago
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
|
||
|
var _buffer = require('buffer');
|
||
|
|
||
|
var _create_buffer = require('./create_buffer');
|
||
|
|
||
|
var _create_buffer2 = _interopRequireDefault(_create_buffer);
|
||
|
|
||
|
var _define_crc = require('./define_crc');
|
||
|
|
||
|
var _define_crc2 = _interopRequireDefault(_define_crc);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
var crc16xmodem = (0, _define_crc2.default)('xmodem', function (buf, previous) {
|
||
|
if (!_buffer.Buffer.isBuffer(buf)) buf = (0, _create_buffer2.default)(buf);
|
||
|
|
||
|
var crc = typeof previous !== 'undefined' ? ~~previous : 0x0;
|
||
|
|
||
|
for (var index = 0; index < buf.length; index++) {
|
||
|
var byte = buf[index];
|
||
|
var code = crc >>> 8 & 0xff;
|
||
|
|
||
|
code ^= byte & 0xff;
|
||
|
code ^= code >>> 4;
|
||
|
crc = crc << 8 & 0xffff;
|
||
|
crc ^= code;
|
||
|
code = code << 5 & 0xffff;
|
||
|
crc ^= code;
|
||
|
code = code << 7 & 0xffff;
|
||
|
crc ^= code;
|
||
|
}
|
||
|
|
||
|
return crc;
|
||
|
});
|
||
|
|
||
|
exports.default = crc16xmodem;
|