|
@ -1,5 +1,3 @@ |
|
|
math = require("math") |
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
module.exports = { |
|
|
decode_string: decode_string, |
|
|
decode_string: decode_string, |
|
|
decode_varint: decode_varint, |
|
|
decode_varint: decode_varint, |
|
@ -37,14 +35,14 @@ function encode_big_endian(i, size){ |
|
|
if (size == 0){ |
|
|
if (size == 0){ |
|
|
return new Buffer(0); |
|
|
return new Buffer(0); |
|
|
} |
|
|
} |
|
|
b = encode_big_endian(math.floor(i/256), size-1); |
|
|
|
|
|
|
|
|
b = encode_big_endian(Math.floor(i/256), size-1); |
|
|
return Buffer.concat([b, new Buffer([i%256])]); |
|
|
return Buffer.concat([b, new Buffer([i%256])]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function decode_big_endian(reader, size){ |
|
|
function decode_big_endian(reader, size){ |
|
|
if (size == 0){ return 0 } |
|
|
if (size == 0){ return 0 } |
|
|
firstByte = reader.read(1)[0]; |
|
|
firstByte = reader.read(1)[0]; |
|
|
return firstByte*(math.pow(256, size-1)) + decode_big_endian(reader, size-1) |
|
|
|
|
|
|
|
|
return firstByte*(Math.pow(256, size-1)) + decode_big_endian(reader, size-1) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function encode_string(s){ |
|
|
function encode_string(s){ |
|
@ -70,7 +68,7 @@ function encode_varint(i){ |
|
|
|
|
|
|
|
|
big_end = encode_big_endian(i, size); |
|
|
big_end = encode_big_endian(i, size); |
|
|
if (negate){ size += 0xF0 } |
|
|
if (negate){ size += 0xF0 } |
|
|
var buf = new Buffer([1]); |
|
|
|
|
|
|
|
|
var buf = new Buffer([size]); |
|
|
return Buffer.concat([buf, big_end]) |
|
|
return Buffer.concat([buf, big_end]) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|