From ca3dea3470ad5045f8ab8934f8c8ef847365add8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 16 Jan 2017 17:22:05 -0800 Subject: [PATCH] writer: better failsafes. optimize. --- lib/coins/coins.js | 6 +--- lib/utils/staticwriter.js | 58 +++++++++++++++++++++++++++------------ lib/utils/writer.js | 41 +++++++++++++++++---------- 3 files changed, 68 insertions(+), 37 deletions(-) diff --git a/lib/coins/coins.js b/lib/coins/coins.js index 21d97b1d..5615e81c 100644 --- a/lib/coins/coins.js +++ b/lib/coins/coins.js @@ -700,8 +700,6 @@ CoinEntry.prototype.getSize = function getSize() { */ CoinEntry.prototype.toWriter = function toWriter(bw) { - var raw; - if (!this.raw) { assert(this.output); compress.output(this.output, bw); @@ -712,9 +710,7 @@ CoinEntry.prototype.toWriter = function toWriter(bw) { // didn't use it, it's still in its // compressed form. Just write it back // as a buffer for speed. - raw = this.raw.slice(this.offset, this.offset + this.size); - - bw.writeBytes(raw); + bw.copy(this.raw, this.offset, this.offset + this.size); return bw; }; diff --git a/lib/utils/staticwriter.js b/lib/utils/staticwriter.js index de4f3625..0609aa3b 100644 --- a/lib/utils/staticwriter.js +++ b/lib/utils/staticwriter.js @@ -323,58 +323,80 @@ StaticWriter.prototype.writeBytes = function writeBytes(value) { StaticWriter.prototype.writeVarBytes = function writeVarBytes(value) { this.writeVarint(value.length); - - if (value.length === 0) - return; - this.writeBytes(value); }; +/** + * Copy bytes. + * @param {Buffer} value + * @param {Number} start + * @param {Number} end + */ + +StaticWriter.prototype.copy = function copy(value, start, end) { + var len = end - start; + + if (len === 0) + return; + + value.copy(this.data, this.written, start, end); + this.written += len; +}; + /** * Write string to buffer. - * @param {String|Buffer} value + * @param {String} value * @param {String?} enc - Any buffer-supported encoding. */ StaticWriter.prototype.writeString = function writeString(value, enc) { - if (typeof value !== 'string') - return this.writeBytes(value); + var size; if (value.length === 0) return; - this.written += this.data.write(value, this.written, enc); + size = Buffer.byteLength(value, enc); + + this.data.write(value, this.written, enc); + + this.written += size; }; /** - * Write a hash/hex-string. - * @param {Hash|Buffer} + * Write a 32 byte hash. + * @param {Hash} value */ StaticWriter.prototype.writeHash = function writeHash(value) { - this.writeString(value, 'hex'); + if (typeof value !== 'string') { + assert(value.length === 32); + return this.writeBytes(value); + } + assert(value.length === 64); + this.data.write(value, this.written, 'hex'); + this.written += 32; }; /** * Write a string with a varint length before it. - * @param {String|Buffer} + * @param {String} * @param {String?} enc - Any buffer-supported encoding. */ StaticWriter.prototype.writeVarString = function writeVarString(value, enc) { var size; - if (typeof value !== 'string') - return this.writeVarBytes(value); + if (value.length === 0) { + this.writeVarint(0); + return; + } size = Buffer.byteLength(value, enc); this.writeVarint(size); + this.data.write(value, this.written, enc); - if (value.length === 0) - return; - - this.writeString(value, enc); + this.written += size; }; /** diff --git a/lib/utils/writer.js b/lib/utils/writer.js index 6355aa73..6d94aee5 100644 --- a/lib/utils/writer.js +++ b/lib/utils/writer.js @@ -433,44 +433,60 @@ BufferWriter.prototype.writeVarBytes = function writeVarBytes(value) { this.ops.push(new WriteOp(BYTES, value)); }; +/** + * Copy bytes. + * @param {Buffer} value + * @param {Number} start + * @param {Number} end + */ + +BufferWriter.prototype.copy = function copy(value, start, end) { + assert(end >= start); + value = value.slice(start, end); + this.writeBytes(value); +}; + /** * Write string to buffer. - * @param {String|Buffer} value + * @param {String} value * @param {String?} enc - Any buffer-supported encoding. */ BufferWriter.prototype.writeString = function writeString(value, enc) { - if (typeof value !== 'string') - return this.writeBytes(value); - - this.written += Buffer.byteLength(value, enc); - if (value.length === 0) return; + this.written += Buffer.byteLength(value, enc); this.ops.push(new WriteOp(STR, value, enc)); }; /** - * Write a hash/hex-string. - * @param {Hash|Buffer} + * Write a 32 byte hash. + * @param {Hash} value */ BufferWriter.prototype.writeHash = function writeHash(value) { + if (typeof value !== 'string') { + assert(value.length === 32); + return this.writeBytes(value); + } + assert(value.length === 64); this.writeString(value, 'hex'); }; /** * Write a string with a varint length before it. - * @param {String|Buffer} + * @param {String} * @param {String?} enc - Any buffer-supported encoding. */ BufferWriter.prototype.writeVarString = function writeVarString(value, enc) { var size; - if (typeof value !== 'string') - return this.writeVarBytes(value); + if (value.length === 0) { + this.ops.push(new WriteOp(VARINT, 0)); + return; + } size = Buffer.byteLength(value, enc); @@ -479,9 +495,6 @@ BufferWriter.prototype.writeVarString = function writeVarString(value, enc) { this.ops.push(new WriteOp(VARINT, size)); - if (value.length === 0) - return; - this.ops.push(new WriteOp(STR, value, enc)); };