From 724944e795005907af7121e646f779d006c40fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Wed, 19 Jul 2023 15:02:24 +0200 Subject: [PATCH] Update buffercursor.js --- buffercursor.js | 598 ++++++++++++++++++++++++------------------------ 1 file changed, 303 insertions(+), 295 deletions(-) diff --git a/buffercursor.js b/buffercursor.js index 8c58349..bd9bd70 100644 --- a/buffercursor.js +++ b/buffercursor.js @@ -18,371 +18,379 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE -var util = require('util'); var VError = require('verror'); -var BufferCursor = module.exports = function(buff, noAssert) { - if (!(this instanceof BufferCursor)) - return new BufferCursor(buff, noAssert); - - this._pos = 0; - - this._noAssert = noAssert; +class BCO extends VError { + constructor (length, pos, size) { + super( + 'BufferCursorOverflow: length %d, position %d, size %d', + length, + pos, + size + ); + this.kind = 'BufferCursorOverflow'; + this.length = length; + this.position = pos; + this.size = size; + } +}; - if (this._noAssert === undefined) - this._noAssert = true; +class BufferCursor { + static BufferCursorOverflow = BCO; - this.buffer = buff; - this.length = buff.length; -}; + #pos = 0; -var BCO = BufferCursor.BufferCursorOverflow = function(length, pos, size) { - this.kind = 'BufferCursorOverflow'; - this.length = length; - this.position = pos; - this.size = size; - VError.call(this, - 'BufferCursorOverflow: length %d, position %d, size %d', - length, - pos, - size); -}; -util.inherits(BCO, VError); + /** + * @param {Buffer} buff + */ + constructor (buff) { + this.buffer = buff; + this.length = buff.length; + } -BufferCursor.prototype._move = function(step) { - this._checkWrite(step); - this._pos += step; -}; + /** + * @param {number} step + */ + #move (step) { + this.#checkWrite(step); + this.#pos += step; + return this; + } -BufferCursor.prototype._checkWrite = function(size) { - var shouldThrow = false; + /** + * @param {number} size + */ + #checkWrite (size) { + var shouldThrow = false; - var length = this.length; - var pos = this._pos; + var length = this.length; + var pos = this.#pos; - if (size > length) - shouldThrow = true; + if (size > length) + shouldThrow = true; - if (length - pos < size) - shouldThrow = true; + if (length - pos < size) + shouldThrow = true; - if (shouldThrow) { - var bco = new BCO(length, - pos, - size); - throw bco; + if (shouldThrow) { + var bco = new BCO(length, + pos, + size); + throw bco; + } } -} - -BufferCursor.prototype.seek = function(pos) { - if (pos < 0) - throw new VError(new RangeError('Cannot seek before start of buffer'), - 'Negative seek values not allowed: %d', pos); - if (pos > this.length) - throw new VError(new RangeError('Trying to seek beyond buffer'), - 'Requested %d position is beyond length %d', - pos, this.length); + /** + * @param {number} pos + */ + seek (pos) { + if (pos < 0) + throw new VError(new RangeError('Cannot seek before start of buffer'), + 'Negative seek values not allowed: %d', pos); + + if (pos > this.length) + throw new VError(new RangeError('Trying to seek beyond buffer'), + 'Requested %d position is beyond length %d', + pos, this.length); + + this.#pos = pos; + return this; + } - this._pos = pos; - return this; -}; + eof () { + return this.#pos === this.length; + } -BufferCursor.prototype.eof = function() { - return this._pos == this.length; -}; + toByteArray (method = 'readUInt8') { + var arr = [], i, part = 1; -BufferCursor.prototype.toByteArray = function(method) { - var arr = [], i, part, count; + if (method.indexOf('16') > 0) + part = 2; + else if (method.indexOf('32') > 0) + part = 4; - if (!method) { - method = 'readUInt8'; - part = 1; + for (i = 0; i < this.buffer.length; i += part) { + arr.push(this.buffer[method](i)); + } + return arr; } - if (method.indexOf('16') > 0) - part = 2; - else if (method.indexOf('32') > 0) - part = 4; - - count = this.length / part; - - for (i = 0; i < this.buffer.length; i += part) { - arr.push(this.buffer[method](i)); + tell () { + return this.#pos; } - return arr; -}; -BufferCursor.prototype.tell = function() { - return this._pos; -}; + /** + * @param {number} [length] + */ + slice (length) { + var end, b; -BufferCursor.prototype.slice = function(length) { - var end, b; + if (length === undefined) { + end = this.length; + } else { + end = this.#pos + length; + } - if (length === undefined) { - end = this.length; - } else { - end = this._pos + length; - } + b = new BufferCursor(this.buffer.subarray(this.#pos, end)); + this.seek(end); - b = new BufferCursor(this.buffer.slice(this._pos, end)); - this.seek(end); + return b; + } - return b; -}; + toString (encoding = 'utf8', length) { + var end, ret; -BufferCursor.prototype.toString = function(encoding, length) { - var end, ret; + if (length === undefined) { + end = this.length; + } else { + end = this.#pos + length; + } - if (length === undefined) { - end = this.length; - } else { - end = this._pos + length; + ret = this.buffer.toString(encoding, this.#pos, end); + this.seek(end); + return ret; } - if (!encoding) { - encoding = 'utf8'; + // This method doesn't need to _checkWrite because Buffer implicitly truncates + // to the length of the buffer, it's the only method in Node core that behaves + // this way by default + /** + * @param {string} value + * @param {number} length + * @param {string} [encoding] + */ + write (value, length, encoding) { + var ret; + + ret = this.buffer.write(value, this.#pos, length, encoding); + return this.#move(ret); } - ret = this.buffer.toString(encoding, this._pos, end); - this.seek(end); - return ret; -}; - -// This method doesn't need to _checkWrite because Buffer implicitly truncates -// to the length of the buffer, it's the only method in Node core that behaves -// this way by default -BufferCursor.prototype.write = function(value, length, encoding) { - var end, ret; + fill (value, length) { + var end; - ret = this.buffer.write(value, this._pos, length, encoding); - this._move(ret); - return this; -}; + if (length === undefined) { + end = this.length; + } else { + end = this.#pos + length; + } -BufferCursor.prototype.fill = function(value, length) { - var end; + this.#checkWrite(end - this.#pos); - if (length === undefined) { - end = this.length; - } else { - end = this._pos + length; + this.buffer.fill(value, this.#pos, end); + this.seek(end); + return this; } - this._checkWrite(end - this._pos); + // This prototype is not entirely like the upstream Buffer.copy, instead it + // is the target buffer, and accepts the source buffer -- since the target + // buffer knows its starting position + copy (source, sourceStart, sourceEnd) { + var sBC = source instanceof BufferCursor; - this.buffer.fill(value, this._pos, end); - this.seek(end); - return this; -}; + if (isNaN(sourceEnd)) + sourceEnd = source.length; -// This prototype is not entirely like the upstream Buffer.copy, instead it -// is the target buffer, and accepts the source buffer -- since the target -// buffer knows its starting position -BufferCursor.prototype.copy = function copy(source, sourceStart, sourceEnd) { - var sBC = source instanceof BufferCursor; + if (isNaN(sourceStart)) { + if (sBC) + sourceStart = source._pos; - if (isNaN(sourceEnd)) - sourceEnd = source.length; + else + sourceStart = 0; + } - if (isNaN(sourceStart)) { - if (sBC) - sourceStart = source._pos; - else - sourceStart = 0; - } + var length = sourceEnd - sourceStart; - var length = sourceEnd - sourceStart; + this.#checkWrite(length); - this._checkWrite(length); + var buf = sBC ? source.buffer : source; - var buf = sBC ? source.buffer : source; + buf.copy(this.buffer, this.#pos, sourceStart, sourceEnd); - buf.copy(this.buffer, this._pos, sourceStart, sourceEnd); + return this.#move(length); + } - this._move(length); - return this; -}; + readUInt8 () { + var ret = this.buffer.readUInt8(this.#pos); + this.#move(1); + return ret; + } -BufferCursor.prototype.readUInt8 = function() { - var ret = this.buffer.readUInt8(this._pos, this._noAssert); - this._move(1); - return ret; -}; + readInt8 () { + var ret = this.buffer.readInt8(this.#pos); + this.#move(1); + return ret; + } -BufferCursor.prototype.readInt8 = function() { - var ret = this.buffer.readInt8(this._pos, this._noAssert); - this._move(1); - return ret; -}; + readInt16BE () { + var ret = this.buffer.readInt16BE(this.#pos); + this.#move(2); + return ret; + } -BufferCursor.prototype.readInt16BE = function() { - var ret = this.buffer.readInt16BE(this._pos, this._noAssert); - this._move(2); - return ret; -}; + readInt16LE () { + var ret = this.buffer.readInt16LE(this.#pos); + this.#move(2); + return ret; + } -BufferCursor.prototype.readInt16LE = function() { - var ret = this.buffer.readInt16LE(this._pos, this._noAssert); - this._move(2); - return ret; -}; + readUInt16BE () { + var ret = this.buffer.readUInt16BE(this.#pos); + this.#move(2); + return ret; + } -BufferCursor.prototype.readUInt16BE = function() { - var ret = this.buffer.readUInt16BE(this._pos, this._noAssert); - this._move(2); - return ret; -}; + readUInt16LE () { + var ret = this.buffer.readUInt16LE(this.#pos); + this.#move(2); + return ret; + } -BufferCursor.prototype.readUInt16LE = function() { - var ret = this.buffer.readUInt16LE(this._pos, this._noAssert); - this._move(2); - return ret; -}; + readUInt32LE () { + var ret = this.buffer.readUInt32LE(this.#pos); + this.#move(4); + return ret; + } -BufferCursor.prototype.readUInt32LE = function() { - var ret = this.buffer.readUInt32LE(this._pos, this._noAssert); - this._move(4); - return ret; -}; + readUInt32BE () { + var ret = this.buffer.readUInt32BE(this.#pos); + this.#move(4); + return ret; + } -BufferCursor.prototype.readUInt32BE = function() { - var ret = this.buffer.readUInt32BE(this._pos, this._noAssert); - this._move(4); - return ret; -}; + readInt32LE () { + var ret = this.buffer.readInt32LE(this.#pos); + this.#move(4); + return ret; + } -BufferCursor.prototype.readInt32LE = function() { - var ret = this.buffer.readInt32LE(this._pos, this._noAssert); - this._move(4); - return ret; -}; + readInt32BE () { + var ret = this.buffer.readInt32BE(this.#pos); + this.#move(4); + return ret; + } -BufferCursor.prototype.readInt32BE = function() { - var ret = this.buffer.readInt32BE(this._pos, this._noAssert); - this._move(4); - return ret; -}; + readFloatBE () { + var ret = this.buffer.readFloatBE(this.#pos); + this.#move(4); + return ret; + } -BufferCursor.prototype.readFloatBE = function() { - var ret = this.buffer.readFloatBE(this._pos, this._noAssert); - this._move(4); - return ret; -}; + readFloatLE () { + var ret = this.buffer.readFloatLE(this.#pos); + this.#move(4); + return ret; + } -BufferCursor.prototype.readFloatLE = function() { - var ret = this.buffer.readFloatLE(this._pos, this._noAssert); - this._move(4); - return ret; -}; + readDoubleBE () { + var ret = this.buffer.readDoubleBE(this.#pos); + this.#move(8); + return ret; + } -BufferCursor.prototype.readDoubleBE = function() { - var ret = this.buffer.readDoubleBE(this._pos, this._noAssert); - this._move(8); - return ret; -}; + readDoubleLE () { + var ret = this.buffer.readDoubleLE(this.#pos); + this.#move(8); + return ret; + } -BufferCursor.prototype.readDoubleLE = function() { - var ret = this.buffer.readDoubleLE(this._pos, this._noAssert); - this._move(8); - return ret; -}; + writeUInt8 (value) { + this.#checkWrite(1); + this.buffer.writeUInt8(value, this.#pos); + this.#move(1); + return this; + } -BufferCursor.prototype.writeUInt8 = function(value) { - this._checkWrite(1); - this.buffer.writeUInt8(value, this._pos, this._noAssert); - this._move(1); - return this; -}; + writeInt8 (value) { + this.#checkWrite(1); + this.buffer.writeInt8(value, this.#pos); + this.#move(1); + return this; + } -BufferCursor.prototype.writeInt8 = function(value) { - this._checkWrite(1); - this.buffer.writeInt8(value, this._pos, this._noAssert); - this._move(1); - return this; -}; + writeUInt16BE (value) { + this.#checkWrite(2); + this.buffer.writeUInt16BE(value, this.#pos); + this.#move(2); + return this; + } -BufferCursor.prototype.writeUInt16BE = function(value) { - this._checkWrite(2); - this.buffer.writeUInt16BE(value, this._pos, this._noAssert); - this._move(2); - return this; -}; + writeUInt16LE (value) { + this.#checkWrite(2); + this.buffer.writeUInt16LE(value, this.#pos); + this.#move(2); + return this; + } -BufferCursor.prototype.writeUInt16LE = function(value) { - this._checkWrite(2); - this.buffer.writeUInt16LE(value, this._pos, this._noAssert); - this._move(2); - return this; -}; + writeInt16BE (value) { + this.#checkWrite(2); + this.buffer.writeInt16BE(value, this.#pos); + this.#move(2); + return this; + } -BufferCursor.prototype.writeInt16BE = function(value) { - this._checkWrite(2); - this.buffer.writeInt16BE(value, this._pos, this._noAssert); - this._move(2); - return this; -}; + writeInt16LE (value) { + this.#checkWrite(2); + this.buffer.writeInt16LE(value, this.#pos); + this.#move(2); + return this; + } -BufferCursor.prototype.writeInt16LE = function(value) { - this._checkWrite(2); - this.buffer.writeInt16LE(value, this._pos, this._noAssert); - this._move(2); - return this; -}; + writeUInt32BE (value) { + this.#checkWrite(4); + this.buffer.writeUInt32BE(value, this.#pos); + this.#move(4); + return this; + } -BufferCursor.prototype.writeUInt32BE = function(value) { - this._checkWrite(4); - this.buffer.writeUInt32BE(value, this._pos, this._noAssert); - this._move(4); - return this; -}; + writeUInt32LE (value) { + this.#checkWrite(4); + this.buffer.writeUInt32LE(value, this.#pos); + this.#move(4); + return this; + } -BufferCursor.prototype.writeUInt32LE = function(value) { - this._checkWrite(4); - this.buffer.writeUInt32LE(value, this._pos, this._noAssert); - this._move(4); - return this; -}; + writeInt32BE (value) { + this.#checkWrite(4); + this.buffer.writeInt32BE(value, this.#pos); + this.#move(4); + return this; + } -BufferCursor.prototype.writeInt32BE = function(value) { - this._checkWrite(4); - this.buffer.writeInt32BE(value, this._pos, this._noAssert); - this._move(4); - return this; -}; + writeInt32LE (value) { + this.#checkWrite(4); + this.buffer.writeInt32LE(value, this.#pos); + this.#move(4); + return this; + } -BufferCursor.prototype.writeInt32LE = function(value) { - this._checkWrite(4); - this.buffer.writeInt32LE(value, this._pos, this._noAssert); - this._move(4); - return this; -}; + writeFloatBE (value) { + this.#checkWrite(4); + this.buffer.writeFloatBE(value, this.#pos); + this.#move(4); + return this; + } -BufferCursor.prototype.writeFloatBE = function(value) { - this._checkWrite(4); - this.buffer.writeFloatBE(value, this._pos, this._noAssert); - this._move(4); - return this; -}; + writeFloatLE (value) { + this.#checkWrite(4); + this.buffer.writeFloatLE(value, this.#pos); + this.#move(4); + return this; + } -BufferCursor.prototype.writeFloatLE = function(value) { - this._checkWrite(4); - this.buffer.writeFloatLE(value, this._pos, this._noAssert); - this._move(4); - return this; -}; + writeDoubleBE (value) { + this.#checkWrite(8); + this.buffer.writeDoubleBE(value, this.#pos); + this.#move(8); + return this; + } -BufferCursor.prototype.writeDoubleBE = function(value) { - this._checkWrite(8); - this.buffer.writeDoubleBE(value, this._pos, this._noAssert); - this._move(8); - return this; -}; + writeDoubleLE (value) { + this.#checkWrite(8); + this.buffer.writeDoubleLE(value, this.#pos); + this.#move(8); + return this; + } +} -BufferCursor.prototype.writeDoubleLE = function(value) { - this._checkWrite(8); - this.buffer.writeDoubleLE(value, this._pos, this._noAssert); - this._move(8); - return this; -}; +module.exports = BufferCursor;