From 05ddca85130bd7b233d8d98831fa8eee8f380301 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 12 Sep 2014 00:54:21 +0100 Subject: [PATCH] make read functions match node --- index.js | 216 +++++++++++++++++--------------------------------- package.json | 8 +- test/write.js | 18 +++-- 3 files changed, 85 insertions(+), 157 deletions(-) diff --git a/index.js b/index.js index ad3f764..771ca0a 100644 --- a/index.js +++ b/index.js @@ -532,193 +532,119 @@ Buffer.prototype.set = function (v, offset) { return this.writeUInt8(v, offset) } -Buffer.prototype.readUInt8 = function (offset, noAssert) { - if (!noAssert) { - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset < this.length, 'Trying to read beyond buffer length') - } - - if (offset >= this.length) - return - - return this[offset] +/* + * Need to make sure that buffer isn't trying to write out of bounds. + */ +function checkOffset (offset, ext, length) { + if ((offset % 1) !== 0 || offset < 0) + throw new RangeError('offset is not uint') + if (offset + ext > length) + throw new RangeError('Trying to access beyond buffer length') + console.log('suces') } -function readUInt16 (buf, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') - } - - var len = buf.length - if (offset >= len) - return - - var val - if (littleEndian) { - val = buf[offset] - if (offset + 1 < len) - val |= buf[offset + 1] << 8 - } else { - val = buf[offset] << 8 - if (offset + 1 < len) - val |= buf[offset + 1] - } - return val +Buffer.prototype.readUInt8 = function (offset, noAssert) { + if (!noAssert) + checkOffset(offset, 1, this.length) + return this[offset] } Buffer.prototype.readUInt16LE = function (offset, noAssert) { - return readUInt16(this, offset, true, noAssert) + if (!noAssert) + checkOffset(offset, 2, this.length) + return this[offset] | (this[offset + 1] << 8) } Buffer.prototype.readUInt16BE = function (offset, noAssert) { - return readUInt16(this, offset, false, noAssert) -} - -function readUInt32 (buf, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') - } - - var len = buf.length - if (offset >= len) - return - - var val - if (littleEndian) { - if (offset + 2 < len) - val = buf[offset + 2] << 16 - if (offset + 1 < len) - val |= buf[offset + 1] << 8 - val |= buf[offset] - if (offset + 3 < len) - val = val + (buf[offset + 3] << 24 >>> 0) - } else { - if (offset + 1 < len) - val = buf[offset + 1] << 16 - if (offset + 2 < len) - val |= buf[offset + 2] << 8 - if (offset + 3 < len) - val |= buf[offset + 3] - val = val + (buf[offset] << 24 >>> 0) - } - return val + if (!noAssert) + checkOffset(offset, 2, this.length) + return (this[offset] << 8) | this[offset + 1] } Buffer.prototype.readUInt32LE = function (offset, noAssert) { - return readUInt32(this, offset, true, noAssert) -} + if (!noAssert) + checkOffset(offset, 4, this.length) -Buffer.prototype.readUInt32BE = function (offset, noAssert) { - return readUInt32(this, offset, false, noAssert) + return ((this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16)) + + (this[offset + 3] * 0x1000000) } -Buffer.prototype.readInt8 = function (offset, noAssert) { - if (!noAssert) { - assert(offset !== undefined && offset !== null, - 'missing offset') - assert(offset < this.length, 'Trying to read beyond buffer length') - } - - if (offset >= this.length) - return +Buffer.prototype.readUInt32BE = function (offset, noAssert) { + if (!noAssert) + checkOffset(offset, 4, this.length) - var neg = this[offset] & 0x80 - if (neg) - return (0xff - this[offset] + 1) * -1 - else - return this[offset] + return (this[offset] * 0x1000000) + + ((this[offset + 1] << 16) | + (this[offset + 2] << 8) | + this[offset + 3]) } -function readInt16 (buf, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') - } - - var len = buf.length - if (offset >= len) - return - - var val = readUInt16(buf, offset, littleEndian, true) - var neg = val & 0x8000 - if (neg) - return (0xffff - val + 1) * -1 - else - return val +Buffer.prototype.readInt8 = function (offset, noAssert) { + if (!noAssert) + checkOffset(offset, 1, this.length) + if (!(this[offset] & 0x80)) + return (this[offset]) + return ((0xff - this[offset] + 1) * -1) } Buffer.prototype.readInt16LE = function (offset, noAssert) { - return readInt16(this, offset, true, noAssert) + if (!noAssert) + checkOffset(offset, 2, this.length) + var val = this[offset] | (this[offset + 1] << 8) + return (val & 0x8000) ? val | 0xFFFF0000 : val } Buffer.prototype.readInt16BE = function (offset, noAssert) { - return readInt16(this, offset, false, noAssert) -} - -function readInt32 (buf, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset !== undefined && offset !== null, 'missing offset') - assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') - } - - var len = buf.length - if (offset >= len) - return - - var val = readUInt32(buf, offset, littleEndian, true) - var neg = val & 0x80000000 - if (neg) - return (0xffffffff - val + 1) * -1 - else - return val + if (!noAssert) + checkOffset(offset, 2, this.length) + var val = this[offset + 1] | (this[offset] << 8) + return (val & 0x8000) ? val | 0xFFFF0000 : val } Buffer.prototype.readInt32LE = function (offset, noAssert) { - return readInt32(this, offset, true, noAssert) -} + if (!noAssert) + checkOffset(offset, 4, this.length) -Buffer.prototype.readInt32BE = function (offset, noAssert) { - return readInt32(this, offset, false, noAssert) + return (this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16) | + (this[offset + 3] << 24) } -function readFloat (buf, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') - } +Buffer.prototype.readInt32BE = function (offset, noAssert) { + if (!noAssert) + checkOffset(offset, 4, this.length) - return ieee754.read(buf, offset, littleEndian, 23, 4) + return (this[offset] << 24) | + (this[offset + 1] << 16) | + (this[offset + 2] << 8) | + (this[offset + 3]) } Buffer.prototype.readFloatLE = function (offset, noAssert) { - return readFloat(this, offset, true, noAssert) + if (!noAssert) + checkOffset(offset, 4, this.length) + return ieee754.read(this, offset, true, 23, 4) } Buffer.prototype.readFloatBE = function (offset, noAssert) { - return readFloat(this, offset, false, noAssert) -} - -function readDouble (buf, offset, littleEndian, noAssert) { - if (!noAssert) { - assert(typeof littleEndian === 'boolean', 'missing or invalid endian') - assert(offset + 7 < buf.length, 'Trying to read beyond buffer length') - } - - return ieee754.read(buf, offset, littleEndian, 52, 8) + if (!noAssert) + checkOffset(offset, 4, this.length) + return ieee754.read(this, offset, false, 23, 4) } Buffer.prototype.readDoubleLE = function (offset, noAssert) { - return readDouble(this, offset, true, noAssert) + if (!noAssert) + checkOffset(offset, 8, this.length) + return ieee754.read(this, offset, true, 52, 8) } Buffer.prototype.readDoubleBE = function (offset, noAssert) { - return readDouble(this, offset, false, noAssert) + if (!noAssert) + checkOffset(offset, 8, this.length) + return ieee754.read(this, offset, false, 52, 8) } function checkInt (buf, value, offset, ext, max, min) { diff --git a/package.json b/package.json index 3510d89..5d43cfe 100644 --- a/package.json +++ b/package.json @@ -15,13 +15,13 @@ "James Halliday " ], "dependencies": { - "base64-js": "~0.0.4", - "ieee754": "~1.1.1" + "base64-js": "0.0.7", + "ieee754": "^1.1.4" }, "devDependencies": { "benchmark": "^1.0.0", - "browserify": "^3.46.0", - "tape": "^2.12.3" + "browserify": "^5.11.1", + "tape": "^2.14.0" }, "homepage": "https://github.com/feross/buffer", "keywords": [ diff --git a/test/write.js b/test/write.js index 18ecb8b..c456b61 100644 --- a/test/write.js +++ b/test/write.js @@ -1,4 +1,4 @@ -var B = require('../').Buffer +var B = require('buffer').Buffer var test = require('tape') if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false @@ -48,14 +48,12 @@ test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) { var v1 = new B(y / 8) var writefn = 'write' + x + y + z var val = (x === 'Int') ? -3 : 3 - console.log(writefn, val) v1[writefn](val, 0) t.equal( v1.toString('hex'), hex.shift() ) var readfn = 'read' + x + y + z - console.log(v1[0], v1[readfn](0)) t.equal( v1[readfn](0), reads.shift() @@ -73,7 +71,7 @@ test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { '', 'fd', 'ff', 'fdffff', 'ffffff' ] var reads = [ - undefined, 3, 0, 3, 0, + undefined, 3, 0, NaN, 0, undefined, 253, -256, 16777213, -256 ] var xs = ['UInt','Int'] @@ -101,10 +99,14 @@ test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { // check that no bytes are read from next buffer. next.writeInt32BE(~0, 0) var readfn = 'read' + x + y + z - t.equal( - v1[readfn](0, true), - reads.shift() - ) + var r = reads.shift() + if (Number.isNaN(r)) + t.pass('equal') + else + t.equal( + v1[readfn](0, true), + r + ) } } } -- 2.34.1