Skip to content

Commit

Permalink
Merge pull request #48 from acrisci/big-endian
Browse files Browse the repository at this point in the history
add support for big endian
  • Loading branch information
Tony Crisci authored Feb 6, 2020
2 parents 4a63ddb + 96833b8 commit 609c7d8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
34 changes: 28 additions & 6 deletions lib/dbus-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const {parseSignature} = require('./signature');
const { getBigIntCompat } = require('./library-options');
const JSBI = require('jsbi');
const Long = require('long');
const LE = require('./constants').endianness.le;

// Buffer + position + global start position ( used in alignment )
function DBusBuffer(buffer, startPos, options) {
function DBusBuffer(buffer, startPos, endian, options) {
if (typeof options !== 'object') {
options = { ayBuffer: true };
} else if (options.ayBuffer === undefined) {
Expand All @@ -13,6 +14,7 @@ function DBusBuffer(buffer, startPos, options) {
}
this.options = options;
this.buffer = buffer;
this.endian = endian;
(this.startPos = startPos ? startPos : 0), (this.pos = 0);
}

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

DBusBuffer.prototype.readSInt16 = function() {
this.align(1);
var res = this.buffer.readInt16LE(this.pos);

var res = (this.endian === LE ?
this.buffer.readInt16LE(this.pos) :
this.buffer.readInt16BE(this.pos));

this.pos += 2;
return res;
};

DBusBuffer.prototype.readInt16 = function() {
this.align(1);
var res = this.buffer.readUInt16LE(this.pos);

var res = (this.endian === LE ?
this.buffer.readUInt16LE(this.pos) :
this.buffer.readUInt16BE(this.pos));

this.pos += 2;
return res;
};

DBusBuffer.prototype.readSInt32 = function() {
this.align(2);
var res = this.buffer.readInt32LE(this.pos);

var res = (this.endian === LE ?
this.buffer.readInt32LE(this.pos) :
this.buffer.readInt32BE(this.pos));

this.pos += 4;
return res;
};

DBusBuffer.prototype.readInt32 = function() {
this.align(2);
var res = this.buffer.readUInt32LE(this.pos);

var res = (this.endian === LE ?
this.buffer.readUInt32LE(this.pos) :
this.buffer.readUInt32BE(this.pos));

this.pos += 4;
return res;
};

DBusBuffer.prototype.readDouble = function() {
this.align(3);
var res = this.buffer.readDoubleLE(this.pos);

var res = (this.endian === LE ?
this.buffer.readDoubleLE(this.pos) :
this.buffer.readDoubleBE(this.pos));

this.pos += 8;
return res;
};
Expand Down
23 changes: 16 additions & 7 deletions lib/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,39 @@ module.exports.unmarshalMessages = function messageParser(
var fieldsLength, fieldsLengthPadded;
var fieldsAndBodyLength = 0;
var bodyLength = 0;
var endian = 0;
const LE = constants.endianness.le;
stream.on('readable', function() {
while (1) {
if (state === 0) {
header = stream.read(16);
if (!header) break;
if (!header) {
break;
}
state = 1;

fieldsLength = header.readUInt32LE(12);
endian = header.readUInt8();

fieldsLength = (endian === LE ? header.readUInt32LE(12) : header.readUInt32BE(12));
fieldsLengthPadded = ((fieldsLength + 7) >> 3) << 3;
bodyLength = header.readUInt32LE(4);
bodyLength = (endian === LE ? header.readUInt32LE(4) : header.readUInt32BE(4));
fieldsAndBodyLength = fieldsLengthPadded + bodyLength;
} else {
fieldsAndBody = stream.read(fieldsAndBodyLength);
if (!fieldsAndBody) break;
if (!fieldsAndBody) {
break;
}
state = 0;

var messageBuffer = new DBusBuffer(fieldsAndBody, undefined, opts);
var messageBuffer = new DBusBuffer(fieldsAndBody, 0, endian, opts);
var unmarshalledHeader = messageBuffer.readArray(
headerSignature[0].child[0],
fieldsLength
);
messageBuffer.align(3);
var headerName;
var message = {};
message.serial = header.readUInt32LE(8);
message.serial = (endian === LE ? header.readUInt32LE(8) : header.readUInt32BE(8));

for (var i = 0; i < unmarshalledHeader.length; ++i) {
headerName = constants.headerTypeName[unmarshalledHeader[i][0]];
Expand All @@ -61,7 +69,8 @@ module.exports.unmarshalMessages = function messageParser(
// given buffer which contains entire message deserialise it
// TODO: factor out common code
module.exports.unmarshall = function unmarshall(buff, opts) {
var msgBuf = new DBusBuffer(buff, undefined, opts);
var endian = buff.readUInt8();
var msgBuf = new DBusBuffer(buff, 0, endian, opts);
var headers = msgBuf.read('yyyyuua(yv)');
var message = {};
for (var i = 0; i < headers[6].length; ++i) {
Expand Down
9 changes: 0 additions & 9 deletions lib/unmarshall.js

This file was deleted.

0 comments on commit 609c7d8

Please sign in to comment.