script and reader.

This commit is contained in:
Christopher Jeffrey 2016-04-21 23:11:14 -07:00
parent 381e2c52b9
commit 6e2b58c16d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 21 additions and 5 deletions

View File

@ -447,12 +447,13 @@ BufferReader.prototype.readNullString = function readNullString(enc) {
/**
* Read a varint.
* @param {Boolean?} big - Whether to read as a big number.
* @returns {Number}
*/
BufferReader.prototype.readVarint = function readVarint() {
BufferReader.prototype.readVarint = function readVarint(big) {
assert(this.offset + 1 <= this.data.length);
var result = utils.readVarint(this.data, this.offset);
var result = utils.readVarint(this.data, this.offset, big);
assert(result.off <= this.data.length);
assert(result.r >= 0);
this.offset = result.off;

View File

@ -3366,7 +3366,7 @@ Script.format = function format(code) {
}
}
size = chunk.length.toString(16);
if (size.length < 2)
while (size.length % 2 !== 0)
size = '0' + size;
if (!constants.opcodesByVal[op]) {
op = op.toString(16);

View File

@ -2003,25 +2003,40 @@ utils.write64BE = function write64BE(dst, num, off) {
* Read a varint.
* @param {Buffer} arr
* @param {Number} off
* @param {Boolean?} big - Whether to read as a big number.
* @returns {Number}
*/
utils.readVarint = function readVarint(arr, off) {
utils.readVarint = function readVarint(arr, off, big) {
var r, bytes;
off = off >>> 0;
assert(off < arr.length);
if (arr[off] < 0xfd) {
r = arr[off];
if (big)
r = new bn(r);
bytes = 1;
} else if (arr[off] === 0xfd) {
assert(off + 2 < arr.length);
r = arr[off + 1] | (arr[off + 2] << 8);
if (big)
r = new bn(r);
bytes = 3;
} else if (arr[off] === 0xfe) {
assert(off + 4 < arr.length);
r = utils.readU32(arr, off + 1);
if (big)
r = new bn(r);
bytes = 5;
} else if (arr[off] === 0xff) {
r = utils.readU64N(arr, off + 1);
assert(off + 8 < arr.length);
if (big)
r = utils.readU64(arr, off + 1);
else
r = utils.readU64N(arr, off + 1);
bytes = 9;
} else {
assert(false, 'Malformed varint.');