Skip to content

Commit 42c9584

Browse files
committed
fix int64 decode/encode bug
1 parent b506830 commit 42c9584

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ Nodejs code:
128128
console.log( msg.encode());
129129
```
130130
## Changelog
131+
### 1.0.3
132+
1. fix int64 decode/encode error( Works only for numbers <= Number.MAX_SAFE_INTEGER ).
133+
2. fix object decode error.
131134
### 1.0.2
132135
1. merge darnold79 change,add array support.
133136

cppMsg.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ function readFunc(f, arrayLen, off, datalen) {
403403
off += datalen;
404404
}
405405
return res;
406-
d
407406
}
408407

409408
function decodeObject(buf, offset, dsDecode) {
@@ -415,7 +414,7 @@ function decodeObject(buf, offset, dsDecode) {
415414
for (i = 0; i < len; i++) {
416415
info = dsDecode[i];
417416
off = info[0] + offset;
418-
key = info[3];
417+
var key = info[3];
419418
var arrayLen = info[5];
420419
var values = [];
421420
for (var arri = 0; arri < arrayLen; arri++) {
@@ -433,7 +432,7 @@ function decodeObject(buf, offset, dsDecode) {
433432
case DataType.int64:
434433
var high = buf.readUInt32LE(off);
435434
var low = buf.readUInt32LE(off + 4);
436-
values.push((high << 8) | low);
435+
values.push(low * 0x100000000 + high);
437436
break;
438437
case DataType.uint8:
439438
values.push(buf.readUInt8(off));
@@ -456,7 +455,8 @@ function decodeObject(buf, offset, dsDecode) {
456455
break;
457456
case DataType.string: {
458457
//values = buf.toString(undefined, off, off+info[1]-1 );
459-
values.push(iconv.decode(buf.slice(off, off + info[1] - 1), 'gb2312'));
458+
var val = iconv.decode(buf.slice(off, off + info[1] - 1), info[4]);
459+
values.push(val.replace(/\0[\s\S]*/g, ''));
460460
}
461461
break;
462462
case DataType.object: {
@@ -499,9 +499,11 @@ function encodeObject(data, dsLen, dsEncode) {
499499
msgBuf.writeInt32LE(x, off);
500500
break;
501501
case DataType.int64:
502-
msgBuf.writeUInt32LE(x >> 8, off); //write the high order bits (shifted over)
503-
off += 4;
504-
msgBuf.writeUInt32LE(x & 0x00ff, off); //write the low order bits
502+
let high = ~~(x / 0xFFFFFFFF);
503+
let low = (x % 0xFFFFFFFF) - high;
504+
505+
msgBuf.writeUInt32LE(low, off);
506+
msgBuf.writeUInt32LE(high, (off+4));
505507
break;
506508
case DataType.uint8:
507509
msgBuf.writeUInt8(x, off);
@@ -544,8 +546,3 @@ function encodeObject(data, dsLen, dsEncode) {
544546

545547
return msgBuf;
546548
}
547-
548-
549-
550-
551-

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cppmsg",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "C++ stuct encoding/decoding with support for (dynamic) arrays.",
55
"main": "cppMsg.js",
66
"scripts": {

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var buff = msg.encodeMsg({
5656
subType: 0x0A0B0C0D
5757
},
5858

59-
testint64: 0xCDEF,
59+
testint64: 8888321499136,
6060
floatArray3: [1.1, 2.2, 9.7],
6161
alert: ['quick fox', 'lazy dog']
6262
});

0 commit comments

Comments
 (0)