forked from devgru/node-tarantool
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
51 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
### | ||
BEB128 implementation. Tarantool uses BEB128 and calls it LEB128 somewhy. | ||
Big-endian base-128 uses 7 bits for value and most significant bit (MSB) for flag | ||
If flag is 1 — there is more bytes, continue reading. | ||
So, 127 is 0x7f, 128 is 0x81 0x00, 129 is 0x81 0x01 and so on. | ||
### | ||
|
||
encode = (value) -> | ||
bytes = [] | ||
|
||
bytes.push value >> 28 | 0x80 if value >= 1 << 28 | ||
bytes.push value >> 21 | 0x80 if value >= 1 << 21 | ||
bytes.push value >> 14 | 0x80 if value >= 1 << 14 | ||
bytes.push value >> 7 | 0x80 if value >= 1 << 7 | ||
bytes.push value & 0x7F | ||
|
||
new Buffer bytes | ||
|
||
decode = (buffer, position = 0) -> | ||
value = 0 | ||
loop # stops after reading byte with MSB=0 | ||
byte = buffer[position++] | ||
value = (value << 7) + (byte & 0x7F) | ||
break unless byte & 0x80 | ||
|
||
value: value | ||
nextIndex: position | ||
|
||
module.exports = | ||
encode: encode | ||
decode: decode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
beb = require '../src/beb' | ||
|
||
exports['encode'] = (test) -> | ||
test.equalBuffer = (b1, b2) -> | ||
@equal b1.toString('hex'), b2.toString('hex') | ||
test.equalBuffer (new Buffer [0x80 | 1, 0]), beb.encode 128 | ||
test.equalBuffer (new Buffer [0x80 | 1, 1]), beb.encode 129 | ||
do test.done | ||
|
||
exports['fuzz'] = (test) -> | ||
for i in [0...(1<<15)] by 1<<5 | ||
test.equal i, beb.decode(beb.encode(i)).value | ||
do test.done | ||
|