Permalink
Browse files

calculate correct initial offset for array buffer

  • Loading branch information...
1 parent 6bc74d4 commit f6946d94eef900651276425ffd01ccef54431e4d @sidorares committed Jan 2, 2013
Showing with 20 additions and 15 deletions.
  1. +11 −4 lib/marshall.js
  2. +9 −11 test/unmarshall-basic.js
View
15 lib/marshall.js
@@ -3,15 +3,17 @@ var assert = require('assert');
var parseSignature = require('./signature');
var put = require('put');
-var marshall = module.exports = function(signature, data) {
+var marshall = module.exports = function(signature, data, offset) {
+ if (typeof(offset) == 'undefined')
+ offset = 0;
var tree = parseSignature(signature);
if (!Array.isArray(data) || data.length !== tree.length)
{
console.log(tree, data);
throw new Error('message body does not match message signature. Body:' + JSON.stringify(data) + ", signature:" + signature);
}
var putstream = put();
- putstream._offset = 0;
+ putstream._offset = offset;
var buf = writeStruct(putstream, tree, data).buffer();
//console.log(buf, buf.toString('hex'));
return buf;
@@ -34,8 +36,13 @@ function write(ps, ele, data) {
break;
case 'a':
var arrPut = put();
- arrPut._offset = 0; //ps._offset;
- //arrPut._offset = ps._offset + 4; // account for 'u' written later
+ console.log('PUT ARRAY: current offset - ', ps._offset);
+ var arrLenPad = 4 - ps._offset % 4;
+ if (arrLenPad === 4)
+ arrLenPad = 0;
+ console.log('ARRPUT pad:', arrLenPad);
+ arrPut._offset = ps._offset + arrLenPad + 4;
+ console.log('PUT ARRAY BUFF OFFSET: ', arrPut._offset);
for(var i=0; i < data.length; ++i)
write(arrPut, ele.child[0], data[i]);
var arrBuff = arrPut.buffer();
View
20 test/unmarshall-basic.js
@@ -9,6 +9,7 @@ function testOnly() {};
function test(signature, data, callback) {
console.log(signature, data);
var marshalledBuffer = marshall(signature, data);
+ console.error('============= ', signature);
console.error(hexy(marshalledBuffer, {prefix: '===='}));
var stream = binary(marshalledBuffer);
unmarshall.call(stream, signature, 0, function(err, result) {
@@ -75,7 +76,14 @@ describe('marshall/unmarshall', function() {
'compound types': [
['iyai', [10, 100, [1, 2, 3, 4, 5, 6]]],
// TODO: fix 'array of structs offset problem
- //['a(iyai)', [[[10, 100, [1, 2, 3, 4, 5, 6]], [11, 200, [15, 4, 5, 6]]]] ]
+ ['a(iyai)', [[[10, 100, [1, 2, 3, 4, 5, 6]], [11, 200, [15, 4, 5, 6]]]] ],
+ ['sa(iyai)', ['test test test test', [[10, 100, [1, 2, 3, 4, 5, 6]], [11, 200, [15, 4, 5, 6]]]]],
+ ['a(iyai)', [[[10, 100, [1, 2, 3, 4, 5, 6]], [11, 200, [15, 4, 5, 6]]]]],
+ ['a(yai)', [[[100, [1, 2, 3, 4, 5, 6]], [200, [15, 4, 5, 6]]]]],
+ ['ai', [[1, 2, 3, 4, 5, 6]]],
+ ['aii', [[1, 2, 3, 4, 5, 6], 10]],
+ ['a(ai)', [[ [[1, 2, 3, 4, 5, 6]], [[15, 4, 5, 6]] ]]],
+ ['aai', [[[1, 2, 3, 4, 5, 6], [15, 4, 5, 6]]]],
]
};
@@ -126,15 +134,5 @@ test('aai', [[[]]]);
// TODO: epsilon-test floats
// test('bdsai', [0, 3.141590118408203, 'test string', [1, 2, 3, 0, 0, 0, 4, 5, 6, 7]]);
-// test('sa(iyai)', ['test test test test', [[10, 100, [1, 2, 3, 4, 5, 6]], [11, 200, [15, 4, 5, 6]]]]);
-// np test('a(iyai)', [[[10, 100, [1, 2, 3, 4, 5, 6]], [11, 200, [15, 4, 5, 6]]]]);
-// np test('a(yai)', [[[100, [1, 2, 3, 4, 5, 6]], [200, [15, 4, 5, 6]]]]);
-
-// pass test('ai', [[1, 2, 3, 4, 5, 6]]);
-// pass test('aii', [[1, 2, 3, 4, 5, 6], 10]);
-
-// np test('a(ai)', [[ [[1, 2, 3, 4, 5, 6]], [[15, 4, 5, 6]] ]]);
-// pass test('aai', [[[1, 2, 3, 4, 5, 6], [15, 4, 5, 6]]]);
-
*/

0 comments on commit f6946d9

Please sign in to comment.