2014-12-18 17:47:18 +01:00
|
|
|
/**
|
|
|
|
* Implement `var_int` encoding/decoding.
|
|
|
|
* @module bitmessage/varint
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// TODO(Kagami): Since `node-int64` and `int64-native` APIs are slightly
|
|
|
|
// differ, there might be need in platform-dependent wrapper. Also think
|
|
|
|
// that to do with 64bit arithmetic since `node-int64` doesn't implement
|
|
|
|
// it.
|
|
|
|
var Int64 = require("int64-native");
|
|
|
|
var assert = require("./utils").assert;
|
|
|
|
|
|
|
|
exports.decode = function(buf) {
|
|
|
|
assert(buf.length > 0, "Empty buffer");
|
|
|
|
var value, length;
|
|
|
|
switch (buf[0]) {
|
|
|
|
case 253:
|
|
|
|
value = buf.readUInt16BE(1);
|
2014-12-18 18:16:21 +01:00
|
|
|
assert(value >= 253, "Impractical var_int");
|
2014-12-18 17:47:18 +01:00
|
|
|
length = 3;
|
|
|
|
break;
|
|
|
|
case 254:
|
|
|
|
value = buf.readUInt32BE(1);
|
2014-12-18 18:16:21 +01:00
|
|
|
assert(value >= 65536, "Impractical var_int");
|
2014-12-18 17:47:18 +01:00
|
|
|
length = 5;
|
|
|
|
break;
|
|
|
|
case 255:
|
|
|
|
var hi = buf.readUInt32BE(1);
|
|
|
|
var lo = buf.readUInt32BE(5);
|
|
|
|
value = new Int64(hi, lo);
|
2014-12-18 18:16:21 +01:00
|
|
|
assert(value >= 4294967296, "Impractical var_int");
|
2014-12-18 17:47:18 +01:00
|
|
|
length = 9;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = buf[0];
|
|
|
|
length = 1;
|
|
|
|
}
|
|
|
|
var rest = buf.slice(length);
|
|
|
|
return {value: value, length: length, rest: rest};
|
|
|
|
};
|