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) {