wuhkuh / protocol

Protocol generator / parser for Node.JS
MIT License
3 stars 3 forks source link

BUG in generate.js! #10

Open rex-farmer opened 7 years ago

rex-farmer commented 7 years ago

Hi buddy: I found a BUG which in generate.js file from line:128, that can make a critical running error!

here's the report: bytes.push(parseInt(bits.substr(i 8, i 8 + 8), 2)) ; // BUG SHOULD BE :bytes.push(parseInt(bits.substr(i * 8, 8), 2)) ; //this will works well!

here the Syntax: str.substr(start[, length])

wuhkuh commented 7 years ago

I will be checking this issue out this weekend, as I'm currently very busy.

Can you supply me with a failing generation (a wrongly generated packet) in the meantime, so I can make sure the issue is fixed properly?

Thanks for reporting this!

rex-farmer commented 7 years ago
let Protocol = require('protocol');
let schema = {
    startFlag: {bitLength: 8},
    controlNum: {bitLength: 8},
    infoCode: {bitLength: 8},
    infoLength: {bitLength: 16},
    infoBody: {byteLength: 'infoLength'},
    crc16: {bitLength: 16},
    endFlag: {bitLength: 8}
};
let myProtocol = new Protocol(schema);
let out = myProtocol.generate({
    startFlag: 0xfe,
    controlNum: 0x11,
    infoCode: 0x11,
    infoLength: 0x0004,
    infoBody: 'abcd',
    crc16: 0x1234,
    endFlag: 0xff
});
console.log(out);

'output:  <Buffer fe 11 11 00 04 61 62 63 64 12 34 ff>'

with:generate.js line:128 like this bytes.push(parseInt(bits.substr(i * 8, 8), 2));


'output:  <Buffer fe 11 04 04 04 61 62 63 64 12 ff ff>'

with:generate.js line:128 like this bytes.push(parseInt(bits.substr(i * 8, i * 8 + 8), 2));