From 164d31ee3d05c48973285885b1d646e583f130f8 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 25 Mar 2014 17:23:01 +1100 Subject: [PATCH 1/2] VarInt now uses correct prefix for 64-bit --- src/convert.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/convert.js b/src/convert.js index d09942a..ddf4282 100644 --- a/src/convert.js +++ b/src/convert.js @@ -131,7 +131,7 @@ function numToVarInt(num) { if (num < 253) return [num]; if (num < 65536) return [253].concat(numToBytes(num, 2)); if (num < 4294967296) return [254].concat(numToBytes(num, 4)); - return [253].concat(numToBytes(num, 8)); + return [255].concat(numToBytes(num, 8)); } function bytesToWords(bytes) { From baa50d6aa26f00bd18f6700501f989229f6437a0 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 26 Mar 2014 05:03:27 +1100 Subject: [PATCH 2/2] Adds simple test vectors for numToVarInt --- test/convert.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/convert.js b/test/convert.js index b1c6001..efa4a82 100644 --- a/test/convert.js +++ b/test/convert.js @@ -71,4 +71,26 @@ describe('convert', function() { }) }) }) + + describe('numToVarInt', function() { + describe('works', function() { + var data = [ + 0, 128, 252, // 8-bit + 256, 512, 1024, // 16-bit + 65541, // 32-bit + 4294967299, // 64-bit + ] + var expected = [ + [0], [128], [252], // 8-bit + [253, 0, 1], [253, 0, 2], [253, 0, 4], // 16-bit + [254, 5, 0, 1, 0], // 32-bit + [255, 3, 0, 0, 0, 1, 0, 0, 0] // 64-bit + ] + + for (var i = 0; i < data.length; ++i) { + var actual = convert.numToVarInt(data[i]) + assert.deepEqual(actual, expected[i]) + } + }) + }) })