Skip to content

Commit 96833b8

Browse files
author
Tony Crisci
committed
add support for big endian
fixes #36
1 parent 4a63ddb commit 96833b8

3 files changed

Lines changed: 44 additions & 22 deletions

File tree

lib/dbus-buffer.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ const {parseSignature} = require('./signature');
22
const { getBigIntCompat } = require('./library-options');
33
const JSBI = require('jsbi');
44
const Long = require('long');
5+
const LE = require('./constants').endianness.le;
56

67
// Buffer + position + global start position ( used in alignment )
7-
function DBusBuffer(buffer, startPos, options) {
8+
function DBusBuffer(buffer, startPos, endian, options) {
89
if (typeof options !== 'object') {
910
options = { ayBuffer: true };
1011
} else if (options.ayBuffer === undefined) {
@@ -13,6 +14,7 @@ function DBusBuffer(buffer, startPos, options) {
1314
}
1415
this.options = options;
1516
this.buffer = buffer;
17+
this.endian = endian;
1618
(this.startPos = startPos ? startPos : 0), (this.pos = 0);
1719
}
1820

@@ -29,35 +31,55 @@ DBusBuffer.prototype.readInt8 = function() {
2931

3032
DBusBuffer.prototype.readSInt16 = function() {
3133
this.align(1);
32-
var res = this.buffer.readInt16LE(this.pos);
34+
35+
var res = (this.endian === LE ?
36+
this.buffer.readInt16LE(this.pos) :
37+
this.buffer.readInt16BE(this.pos));
38+
3339
this.pos += 2;
3440
return res;
3541
};
3642

3743
DBusBuffer.prototype.readInt16 = function() {
3844
this.align(1);
39-
var res = this.buffer.readUInt16LE(this.pos);
45+
46+
var res = (this.endian === LE ?
47+
this.buffer.readUInt16LE(this.pos) :
48+
this.buffer.readUInt16BE(this.pos));
49+
4050
this.pos += 2;
4151
return res;
4252
};
4353

4454
DBusBuffer.prototype.readSInt32 = function() {
4555
this.align(2);
46-
var res = this.buffer.readInt32LE(this.pos);
56+
57+
var res = (this.endian === LE ?
58+
this.buffer.readInt32LE(this.pos) :
59+
this.buffer.readInt32BE(this.pos));
60+
4761
this.pos += 4;
4862
return res;
4963
};
5064

5165
DBusBuffer.prototype.readInt32 = function() {
5266
this.align(2);
53-
var res = this.buffer.readUInt32LE(this.pos);
67+
68+
var res = (this.endian === LE ?
69+
this.buffer.readUInt32LE(this.pos) :
70+
this.buffer.readUInt32BE(this.pos));
71+
5472
this.pos += 4;
5573
return res;
5674
};
5775

5876
DBusBuffer.prototype.readDouble = function() {
5977
this.align(3);
60-
var res = this.buffer.readDoubleLE(this.pos);
78+
79+
var res = (this.endian === LE ?
80+
this.buffer.readDoubleLE(this.pos) :
81+
this.buffer.readDoubleBE(this.pos));
82+
6183
this.pos += 8;
6284
return res;
6385
};

lib/message.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,39 @@ module.exports.unmarshalMessages = function messageParser(
1515
var fieldsLength, fieldsLengthPadded;
1616
var fieldsAndBodyLength = 0;
1717
var bodyLength = 0;
18+
var endian = 0;
19+
const LE = constants.endianness.le;
1820
stream.on('readable', function() {
1921
while (1) {
2022
if (state === 0) {
2123
header = stream.read(16);
22-
if (!header) break;
24+
if (!header) {
25+
break;
26+
}
2327
state = 1;
2428

25-
fieldsLength = header.readUInt32LE(12);
29+
endian = header.readUInt8();
30+
31+
fieldsLength = (endian === LE ? header.readUInt32LE(12) : header.readUInt32BE(12));
2632
fieldsLengthPadded = ((fieldsLength + 7) >> 3) << 3;
27-
bodyLength = header.readUInt32LE(4);
33+
bodyLength = (endian === LE ? header.readUInt32LE(4) : header.readUInt32BE(4));
2834
fieldsAndBodyLength = fieldsLengthPadded + bodyLength;
2935
} else {
3036
fieldsAndBody = stream.read(fieldsAndBodyLength);
31-
if (!fieldsAndBody) break;
37+
if (!fieldsAndBody) {
38+
break;
39+
}
3240
state = 0;
3341

34-
var messageBuffer = new DBusBuffer(fieldsAndBody, undefined, opts);
42+
var messageBuffer = new DBusBuffer(fieldsAndBody, 0, endian, opts);
3543
var unmarshalledHeader = messageBuffer.readArray(
3644
headerSignature[0].child[0],
3745
fieldsLength
3846
);
3947
messageBuffer.align(3);
4048
var headerName;
4149
var message = {};
42-
message.serial = header.readUInt32LE(8);
50+
message.serial = (endian === LE ? header.readUInt32LE(8) : header.readUInt32BE(8));
4351

4452
for (var i = 0; i < unmarshalledHeader.length; ++i) {
4553
headerName = constants.headerTypeName[unmarshalledHeader[i][0]];
@@ -61,7 +69,8 @@ module.exports.unmarshalMessages = function messageParser(
6169
// given buffer which contains entire message deserialise it
6270
// TODO: factor out common code
6371
module.exports.unmarshall = function unmarshall(buff, opts) {
64-
var msgBuf = new DBusBuffer(buff, undefined, opts);
72+
var endian = buff.readUInt8();
73+
var msgBuf = new DBusBuffer(buff, 0, endian, opts);
6574
var headers = msgBuf.read('yyyyuua(yv)');
6675
var message = {};
6776
for (var i = 0; i < headers[6].length; ++i) {

lib/unmarshall.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)