buffer writer. buffer reader.
This commit is contained in:
parent
d0a5ae7aa4
commit
bf8475afe1
@ -138,6 +138,7 @@ Peer.prototype._init = function init() {
|
||||
});
|
||||
|
||||
this.parser.on('error', function(err) {
|
||||
utils.debug(err.stack + '');
|
||||
self._error(err);
|
||||
// Something is wrong here.
|
||||
// Ignore this peer.
|
||||
|
||||
@ -818,6 +818,206 @@ Framer.mempool = function mempool() {
|
||||
return new Buffer([]);
|
||||
};
|
||||
|
||||
function BufferWriter(size, offset) {
|
||||
this.buffers = [];
|
||||
if (Buffer.isBuffer(size)) {
|
||||
this.offset = offset || 0;
|
||||
this.size = 100;
|
||||
this.buffers.push(size);
|
||||
} else {
|
||||
this.offset = 0;
|
||||
this.size = size || 100;
|
||||
}
|
||||
this.data = new Buffer(this.size);
|
||||
}
|
||||
|
||||
BufferWriter.prototype.render = function render() {
|
||||
this.buffers.push(this.data.slice(this.offset));
|
||||
var ret = Buffer.concat(this.buffers);
|
||||
this.buffers.length = 0;
|
||||
delete this.size;
|
||||
delete this.offset;
|
||||
delete this.buffers;
|
||||
delete this.data;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype._realloc = function _realloc(size) {
|
||||
if (this.data.length >= size)
|
||||
return;
|
||||
|
||||
var len = this.data.length;
|
||||
var newSize = len + (len / 2 | 0);
|
||||
|
||||
if (size > newSize)
|
||||
newSize = size + (size / 2 | 0);
|
||||
|
||||
this.buffers.push(this.data.slice(this.offset));
|
||||
this.data = new Buffer(newSize);
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeU8 = function writeU8(value) {
|
||||
this._realloc(this.offset + 1);
|
||||
var ret = utils.writeU8(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeU16 = function writeU16(value) {
|
||||
this._realloc(this.offset + 2);
|
||||
var ret = utils.writeU16(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeU16BE = function writeU16BE(value) {
|
||||
this._realloc(this.offset + 2);
|
||||
var ret = utils.writeU16BE(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeU32 = function writeU32(value) {
|
||||
this._realloc(this.offset + 4);
|
||||
var ret = utils.writeU32(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeU32BE = function writeU32BE(value) {
|
||||
this._realloc(this.offset + 4);
|
||||
var ret = utils.writeU32BE(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeU64 = function writeU64(value) {
|
||||
this._realloc(this.offset + 8);
|
||||
var ret = utils.writeU64(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeU64BE = function writeU64BE(value) {
|
||||
this._realloc(this.offset + 8);
|
||||
var ret = utils.writeU64BE(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.write8 = function write8(value) {
|
||||
this._realloc(this.offset + 1);
|
||||
var ret = utils.write8(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.write16 = function write16(value) {
|
||||
this._realloc(this.offset + 2);
|
||||
var ret = utils.write16(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.write16BE = function write16BE(value) {
|
||||
this._realloc(this.offset + 2);
|
||||
var ret = utils.write16BE(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.write32 = function write32(value) {
|
||||
this._realloc(this.offset + 4);
|
||||
var ret = utils.write32(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.write32BE = function write32BE(value) {
|
||||
this._realloc(this.offset + 4);
|
||||
var ret = utils.write32BE(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.write64 = function write64(value) {
|
||||
this._realloc(this.offset + 8);
|
||||
var ret = utils.write64(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.write64BE = function write64BE(value) {
|
||||
this._realloc(this.offset + 8);
|
||||
var ret = utils.write64BE(this.data, value, this.offset);
|
||||
this.offset += ret;
|
||||
return ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeBytes = function writeBytes(value) {
|
||||
assert(Buffer.isBuffer(value));
|
||||
if (this.left() > value.length)
|
||||
return utils.copy(value, this.data, 0);
|
||||
this.buffers.push(this.data.slice(this.offset));
|
||||
this.buffers.push(value);
|
||||
this.data = new Buffer(100);
|
||||
this.offset += value.length;
|
||||
return value.length;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.left = function left() {
|
||||
return this.data.length - this.offset;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.getSize = function getSize() {
|
||||
return this.offset;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeString = function writeString(value, enc) {
|
||||
if (typeof value === 'string')
|
||||
value = new Buffer(value, enc);
|
||||
return this.writeBytes(value);
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeHash = function writeHash(value) {
|
||||
if (typeof value === 'string')
|
||||
value = new Buffer(value, 'hex');
|
||||
assert(value.length === 32 || value.length === 20);
|
||||
return this.writeBytes(value);
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeVarString = function writeVarString(value, enc) {
|
||||
var len = typeof value === 'string'
|
||||
? Buffer.byteLength(value, enc)
|
||||
: value.length;
|
||||
var ret = this.writeUIntv(len);
|
||||
return this.writeString(value, enc) + ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeVarBytes = function writeVarBytes(value) {
|
||||
var ret = this.writeUIntv(value.length);
|
||||
return this.writeBytes(value) + ret;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeNullString = function writeNullString(value, enc) {
|
||||
var ret = this.writeString(value, enc);
|
||||
this.writeU8(0);
|
||||
return ret + 1;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeIntv = function writeIntv(value) {
|
||||
this._realloc(this.offset + utils.sizeIntv(value));
|
||||
return utils.writeIntv(this.data, value, this.offset);
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeUIntv = function writeUIntv(value) {
|
||||
if (value instanceof bn)
|
||||
assert(value.cmpn(0) >= 0);
|
||||
else
|
||||
assert(value >= 0);
|
||||
return this.writeIntv(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Expose
|
||||
*/
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -28,7 +28,7 @@ script.decode = function decode(buf) {
|
||||
|
||||
// Next `b` bytes should be pushed to stack
|
||||
if (b >= 0x01 && b <= 0x4b) {
|
||||
opcodes.push(buf.slice(i, i + b));
|
||||
opcodes.push(utils.slice(buf, i, i + b));
|
||||
i += b;
|
||||
utils.hidden(opcodes[opcodes.length - 1], 'pushdata', {
|
||||
opcode: null,
|
||||
@ -63,7 +63,7 @@ script.decode = function decode(buf) {
|
||||
if (opcode === 'pushdata1') {
|
||||
len = buf[i];
|
||||
i += 1;
|
||||
opcodes.push(buf.slice(i, i + len));
|
||||
opcodes.push(utils.slice(buf, i, i + len));
|
||||
i += len;
|
||||
utils.hidden(opcodes[opcodes.length - 1], 'pushdata', {
|
||||
opcode: opcode,
|
||||
@ -72,7 +72,7 @@ script.decode = function decode(buf) {
|
||||
} else if (opcode === 'pushdata2') {
|
||||
len = utils.readU16(buf, i);
|
||||
i += 2;
|
||||
opcodes.push(buf.slice(i, i + len));
|
||||
opcodes.push(utils.slice(buf, i, i + len));
|
||||
i += len;
|
||||
utils.hidden(opcodes[opcodes.length - 1], 'pushdata', {
|
||||
opcode: opcode,
|
||||
@ -81,7 +81,7 @@ script.decode = function decode(buf) {
|
||||
} else if (opcode === 'pushdata4') {
|
||||
len = utils.readU32(buf, i);
|
||||
i += 4;
|
||||
opcodes.push(buf.slice(i, i + len));
|
||||
opcodes.push(utils.slice(buf, i, i + len));
|
||||
i += len;
|
||||
utils.hidden(opcodes[opcodes.length - 1], 'pushdata', {
|
||||
opcode: opcode,
|
||||
@ -92,7 +92,7 @@ script.decode = function decode(buf) {
|
||||
}
|
||||
}
|
||||
|
||||
utils.hidden(opcodes, '_raw', buf);
|
||||
utils.hidden(opcodes, '_raw', utils.slice(buf));
|
||||
|
||||
return opcodes;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user