varint. misc.

This commit is contained in:
Christopher Jeffrey 2016-05-17 15:17:39 -07:00
parent ec21986062
commit 559cb62889
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 22 additions and 24 deletions

View File

@ -474,13 +474,9 @@ BufferReader.prototype.readDoubleBE = function readDoubleBE() {
*/
BufferReader.prototype.readVarint = function readVarint(big) {
var result;
assert(this.offset + 1 <= this.data.length);
result = utils.readVarint(this.data, this.offset, big);
assert(result.off <= this.data.length);
assert(result.r >= 0);
this.offset = result.off;
return result.r;
var result = utils.readVarint(this.data, this.offset, big);
this.offset += result.size;
return result.value;
};
/**

View File

@ -2018,41 +2018,41 @@ utils.write64BE = function write64BE(dst, num, off) {
*/
utils.readVarint = function readVarint(data, off, big) {
var r, bytes;
var value, size;
off = off >>> 0;
assert(off < data.length);
if (data[off] < 0xfd) {
r = data[off];
size = 1;
value = data[off];
if (big)
r = new bn(r);
bytes = 1;
value = new bn(value);
} else if (data[off] === 0xfd) {
assert(off + 2 < data.length);
r = data[off + 1] | (data[off + 2] << 8);
size = 3;
assert(off + size <= data.length);
value = data[off + 1] | (data[off + 2] << 8);
if (big)
r = new bn(r);
bytes = 3;
value = new bn(value);
} else if (data[off] === 0xfe) {
assert(off + 4 < data.length);
r = utils.readU32(data, off + 1);
size = 5;
assert(off + size <= data.length);
value = utils.readU32(data, off + 1);
if (big)
r = new bn(r);
bytes = 5;
value = new bn(value);
} else if (data[off] === 0xff) {
assert(off + 8 < data.length);
size = 9;
assert(off + size <= data.length);
if (big)
r = utils.readU64(data, off + 1);
value = utils.readU64(data, off + 1);
else
r = utils.readU64N(data, off + 1);
bytes = 9;
value = utils.readU64N(data, off + 1);
} else {
assert(false, 'Malformed varint.');
}
return { off: off + bytes, r: r };
return { size: size, value: value };
};
/**

View File

@ -112,6 +112,8 @@ BufferWriter.prototype.render = function render(keep) {
}
}
assert(off === data.length);
if (!keep)
this.destroy();