From 8c42c841d69be835a1697946a1800ed9fe64b698 Mon Sep 17 00:00:00 2001 From: Max Ogden Date: Wed, 9 Oct 2013 23:11:37 -0700 Subject: [PATCH 1/3] switch out the accumulator reader for a faster buffer reader --- README.md | 23 +++++-------------- decode.js | 44 +++++++++++++----------------------- encode.js | 2 +- index.js | 24 +++----------------- package.json | 2 +- test.js | 63 ++++++++++++++++++---------------------------------- 6 files changed, 48 insertions(+), 110 deletions(-) diff --git a/README.md b/README.md index 64bea04..be59be7 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,12 @@ # varint -decode [protobuf-style varint bytes](https://developers.google.com/protocol-buffers/docs/encoding#varints) and emit whole integers when enough have been accumulated that a number can be constructed; also encode whole numbers to an array of varint style bytes. +encode whole numbers to an array of [protobuf-style varint bytes](https://developers.google.com/protocol-buffers/docs/encoding#varints) and also decode them. ```javascript var varint = require('varint') var bytes = varint.encode(300) // === [0xAC, 0x02] - , vi = varint() - -vi.once('data', function(num) { - console.log('got', num) -}) - -vi.write(bytes[0]) -vi.write(bytes[1]) // "got 300" - +varint.decode(bytes) // 300 ``` ## api @@ -24,16 +16,11 @@ vi.write(bytes[1]) // "got 300" ### varint.encode(num[, output=[], offset=0]) -> array encodes `num` into either the array given by `offset` or a new array at `offset` -and returns that array. - -### vi = varint() -> EventEmitter - -return a new `varint` instance. +and returns that array filled with integers. -### vi.write(byte) -> undefined +### varint.decode(data[, offset=0]) -> number -write a byte to the varint. if the byte is "final" (i.e., does not have the bit at `0x80` set), -it will attempt to compile the number and emit it as a `data` event. +decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded integer # License diff --git a/decode.js b/decode.js index 7e2530f..3ed365e 100644 --- a/decode.js +++ b/decode.js @@ -1,33 +1,21 @@ -module.exports = Decoder +module.exports = read var MSB = 0x80 , REST = 0x7F - -function Decoder() { - this.accum = [] +function read(buf, offset) { + var res = 0 + , offset = offset || 0 + , shift = 0 + , b + + do { + b = buf[offset++] + res += shift < 28 + ? (b & REST) << shift + : (b & REST) * Math.pow(2, shift) + shift += 7 + } while (b >= MSB) + + return res } -Decoder.prototype.write = write; - -function write(byte) { - var msb = byte & MSB - , accum = this.accum - , len - , out - - accum[accum.length] = byte & REST - if(msb) { - return - } - - len = accum.length - out = 0 - - for(var i = 0; i < len; ++i) { - out |= accum[i] << (7 * i) - } - - accum.length = 0 - this.ondata(out) - return -} \ No newline at end of file diff --git a/encode.js b/encode.js index 3e68c02..e2e2e36 100644 --- a/encode.js +++ b/encode.js @@ -14,4 +14,4 @@ function encode(num, out, offset) { } out[offset] = num return out -} \ No newline at end of file +} diff --git a/index.js b/index.js index 68483bd..584cabf 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,4 @@ -module.exports = varint - -varint.encode = require('./encode.js'); - -var EE = require('events').EventEmitter - , Decoder = require('./decode.js') - -function varint() { - var ee = new EE - , dec = new Decoder - - dec.ondata = function (item) { - ee.emit("data", item) - } - - ee.write = function (item) { - dec.write(item); - } - - return ee +module.exports = { + encode: require('./encode.js'), + decode: require('./decode.js') } - diff --git a/package.json b/package.json index a7569cf..547046b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "varint", "version": "0.0.3", - "description": "use msb to create integer values of varying sizes", + "description": "protobuf-style varint bytes - use msb to create integer values of varying sizes", "main": "index.js", "scripts": { "test": "node test.js" diff --git a/test.js b/test.js index 812733b..2acb9ff 100644 --- a/test.js +++ b/test.js @@ -2,39 +2,25 @@ var varint = require('./index') , test = require('tape') test('fuzz test', function(assert) { - var vi = varint() - , expect + var expect , encoded - vi.on('data', function(data) { - assert.equal(expect, data, 'fuzz test: '+expect.toString(16)) - }) - for(var i = 0, len = 100; i < len; ++i) { expect = randint(0x7FFFFFFF) encoded = varint.encode(expect) - - for(var x = 0; x < encoded.length; ++x) { - vi.write(encoded[x]) - } + var data = varint.decode(encoded) + assert.equal(expect, data, 'fuzz test: '+expect.toString()) } assert.end() }) test('test single byte works as expected', function(assert) { - var num = [0xAC, 0x02] - var acc = varint() - - acc.on('data', function(data) { - assert.equal(data, 300, 'should equal 300 every time') - }) - - for(var i = 0, len = 10; i < len; ++i) { - acc.write(0xAC) - acc.write(0x02) - } - + var buf = new Uint8Array(2) + buf[0] = 172 + buf[1] = 2 + var data = varint.decode(buf) + assert.equal(data, 300, 'should equal 300') assert.end() }) @@ -47,28 +33,22 @@ test('test encode works as expected', function(assert) { }) test('test decode single bytes', function(assert) { - var vi = varint() - , expected = randint(parseInt('1111111', '2')) - - vi.once('data', function(data) { - assert.equal(data, expected) - assert.end() - }) - - vi.write(expected) + var expected = randint(parseInt('1111111', '2')) + var buf = new Uint8Array(1) + buf[0] = expected + var data = varint.decode(buf) + assert.equal(data, expected) + assert.end() }) test('test decode multiple bytes with zero', function(assert) { - var vi = varint() - , expected = randint(parseInt('1111111', '2')) - - vi.once('data', function(data) { - assert.equal(data, expected << 7) - assert.end() - }) - - vi.write(0x80) - vi.write(expected) + var expected = randint(parseInt('1111111', '2')) + var buf = new Uint8Array(2) + buf[0] = 128 + buf[1] = expected + var data = varint.decode(buf) + assert.equal(data, expected << 7) + assert.end() }) test('encode single byte', function(assert) { @@ -80,6 +60,7 @@ test('encode single byte', function(assert) { test('encode multiple byte with zero first byte', function(assert) { var expected = 0x0F00 assert.deepEqual(varint.encode(expected), [0x80, 0x1E]) + assert.end() }) function randint(range) { From 50ae23c9e994ca69346a9d4f4cb9e815ebe93b00 Mon Sep 17 00:00:00 2001 From: Max Ogden Date: Thu, 10 Oct 2013 08:46:46 -0700 Subject: [PATCH 2/3] also return length from decode --- README.md | 6 +++--- decode.js | 5 +++-- test.js | 12 ++++++++---- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index be59be7..1b84062 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ encode whole numbers to an array of [protobuf-style varint bytes](https://develo var varint = require('varint') var bytes = varint.encode(300) // === [0xAC, 0x02] -varint.decode(bytes) // 300 +varint.decode(bytes) // [300, 2] ``` ## api @@ -18,9 +18,9 @@ varint.decode(bytes) // 300 encodes `num` into either the array given by `offset` or a new array at `offset` and returns that array filled with integers. -### varint.decode(data[, offset=0]) -> number +### varint.decode(data[, offset=0]) -> [number, length] -decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded integer +decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns an array containing `[number, length]` where number is the original integer and length is the amount of bytes that were consumed in order to decode the number # License diff --git a/decode.js b/decode.js index 3ed365e..8a5fb17 100644 --- a/decode.js +++ b/decode.js @@ -7,15 +7,16 @@ function read(buf, offset) { var res = 0 , offset = offset || 0 , shift = 0 + , counter = offset , b do { - b = buf[offset++] + b = buf[counter++] res += shift < 28 ? (b & REST) << shift : (b & REST) * Math.pow(2, shift) shift += 7 } while (b >= MSB) - return res + return [res, counter - offset] } diff --git a/test.js b/test.js index 2acb9ff..9427952 100644 --- a/test.js +++ b/test.js @@ -9,7 +9,8 @@ test('fuzz test', function(assert) { expect = randint(0x7FFFFFFF) encoded = varint.encode(expect) var data = varint.decode(encoded) - assert.equal(expect, data, 'fuzz test: '+expect.toString()) + assert.equal(expect, data[0], 'fuzz test: '+expect.toString()) + assert.equal(data[1], encoded.length) } assert.end() @@ -20,7 +21,8 @@ test('test single byte works as expected', function(assert) { buf[0] = 172 buf[1] = 2 var data = varint.decode(buf) - assert.equal(data, 300, 'should equal 300') + assert.equal(data[0], 300, 'should equal 300') + assert.equal(data[1], 2) assert.end() }) @@ -37,7 +39,8 @@ test('test decode single bytes', function(assert) { var buf = new Uint8Array(1) buf[0] = expected var data = varint.decode(buf) - assert.equal(data, expected) + assert.equal(data[0], expected) + assert.equal(data[1], 1) assert.end() }) @@ -47,7 +50,8 @@ test('test decode multiple bytes with zero', function(assert) { buf[0] = 128 buf[1] = expected var data = varint.decode(buf) - assert.equal(data, expected << 7) + assert.equal(data[0], expected << 7) + assert.equal(data[1], 2) assert.end() }) From a058f9360f85dd75670a0752e907b04f68565d86 Mon Sep 17 00:00:00 2001 From: Max Ogden Date: Thu, 10 Oct 2013 13:36:02 -0700 Subject: [PATCH 3/3] add .bytesRead property to varint.decode --- README.md | 18 +++++++++++++++--- decode.js | 4 +++- index.js | 6 +++--- test.js | 34 ++++++++++++++++++---------------- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 1b84062..78da997 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ encode whole numbers to an array of [protobuf-style varint bytes](https://develo var varint = require('varint') var bytes = varint.encode(300) // === [0xAC, 0x02] -varint.decode(bytes) // [300, 2] +varint.decode(bytes) // 300 +varint.decode.bytesRead // 2 (the last decode() call required 2 bytes) ``` ## api @@ -18,9 +19,20 @@ varint.decode(bytes) // [300, 2] encodes `num` into either the array given by `offset` or a new array at `offset` and returns that array filled with integers. -### varint.decode(data[, offset=0]) -> [number, length] +### varint.decode(data[, offset=0]) -> number -decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns an array containing `[number, length]` where number is the original integer and length is the amount of bytes that were consumed in order to decode the number +decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded original integer. + +### varint.decode.bytesRead + +if you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytesRead`. this is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode. + +## usage notes + +if you are using this to decode buffers from a streaming source it's up to you to make sure that you send 'complete' buffers into `varint.decode`. the maximum number of bytes that varint will need to decode is 8, so all you have to do is make sure you are sending buffers that are at least 8 bytes long from the point at which you know a varint range begins. + +for example, if you are reading buffers from a `fs.createReadStream`, +imagine the first buffer contains one full varint range and half of a second one, and the second buffer contains the second half of the second varint range. in order to be safe across the buffer boundaries you'd just have to make sure the buffer you give to `varint.decode` contains the full varint range (8 bytes), otherwise you'll get an error. # License diff --git a/decode.js b/decode.js index 8a5fb17..6269e73 100644 --- a/decode.js +++ b/decode.js @@ -18,5 +18,7 @@ function read(buf, offset) { shift += 7 } while (b >= MSB) - return [res, counter - offset] + read.bytesRead = counter - offset + + return res } diff --git a/index.js b/index.js index 584cabf..1c4e803 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ module.exports = { - encode: require('./encode.js'), - decode: require('./decode.js') -} + encode: require('./encode.js') + , decode: require('./decode.js') +} \ No newline at end of file diff --git a/test.js b/test.js index 9427952..58bd670 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,7 @@ var varint = require('./index') , test = require('tape') + , decode = varint.decode + , encode = varint.encode test('fuzz test', function(assert) { var expect @@ -7,10 +9,10 @@ test('fuzz test', function(assert) { for(var i = 0, len = 100; i < len; ++i) { expect = randint(0x7FFFFFFF) - encoded = varint.encode(expect) - var data = varint.decode(encoded) - assert.equal(expect, data[0], 'fuzz test: '+expect.toString()) - assert.equal(data[1], encoded.length) + encoded = encode(expect) + var data = decode(encoded) + assert.equal(expect, data, 'fuzz test: ' + expect.toString()) + assert.equal(decode.bytesRead, encoded.length) } assert.end() @@ -20,16 +22,16 @@ test('test single byte works as expected', function(assert) { var buf = new Uint8Array(2) buf[0] = 172 buf[1] = 2 - var data = varint.decode(buf) - assert.equal(data[0], 300, 'should equal 300') - assert.equal(data[1], 2) + var data = decode(buf) + assert.equal(data, 300, 'should equal 300') + assert.equal(decode.bytesRead, 2) assert.end() }) test('test encode works as expected', function(assert) { var out = [] - assert.deepEqual(varint.encode(300), [0xAC, 0x02]) + assert.deepEqual(encode(300), [0xAC, 0x02]) assert.end() }) @@ -38,9 +40,9 @@ test('test decode single bytes', function(assert) { var expected = randint(parseInt('1111111', '2')) var buf = new Uint8Array(1) buf[0] = expected - var data = varint.decode(buf) - assert.equal(data[0], expected) - assert.equal(data[1], 1) + var data = decode(buf) + assert.equal(data, expected) + assert.equal(decode.bytesRead, 1) assert.end() }) @@ -49,21 +51,21 @@ test('test decode multiple bytes with zero', function(assert) { var buf = new Uint8Array(2) buf[0] = 128 buf[1] = expected - var data = varint.decode(buf) - assert.equal(data[0], expected << 7) - assert.equal(data[1], 2) + var data = decode(buf) + assert.equal(data, expected << 7) + assert.equal(decode.bytesRead, 2) assert.end() }) test('encode single byte', function(assert) { var expected = randint(parseInt('1111111', '2')) - assert.deepEqual(varint.encode(expected), [expected]) + assert.deepEqual(encode(expected), [expected]) assert.end() }) test('encode multiple byte with zero first byte', function(assert) { var expected = 0x0F00 - assert.deepEqual(varint.encode(expected), [0x80, 0x1E]) + assert.deepEqual(encode(expected), [0x80, 0x1E]) assert.end() })