Buffer.poolSize = 8192
/**
- * If `TYPED_ARRAY_SUPPORT`:
+ * If `Buffer.TYPED_ARRAY_SUPPORT`:
* === true Use Uint8Array implementation (fastest)
* === false Use Object implementation (most compatible, even IE6)
*
* - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
* incorrect length in some situations.
*
- * We detect these buggy browsers and set `TYPED_ARRAY_SUPPORT` to `false` so they will
+ * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will
* get the Object implementation, which is slower but will work correctly.
*/
-var TYPED_ARRAY_SUPPORT = Buffer.TYPED_ARRAY_SUPPORT = (function () {
+Buffer.TYPED_ARRAY_SUPPORT = (function () {
try {
var buf = new ArrayBuffer(0)
var arr = new Uint8Array(buf)
throw new Error('First argument needs to be a number, array or string.')
var buf
- if (TYPED_ARRAY_SUPPORT) {
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
// Preferred: Return an augmented `Uint8Array` instance for best performance
buf = Buffer._augment(new Uint8Array(length))
} else {
}
var i
- if (TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') {
+ if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') {
// Speed optimization -- use set if we're copying from a typed array
buf._set(subject)
} else if (isArrayish(subject)) {
}
} else if (type === 'string') {
buf.write(subject, 0, encoding)
- } else if (type === 'number' && !TYPED_ARRAY_SUPPORT && !noZero) {
+ } else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) {
for (i = 0; i < length; i++) {
buf[i] = 0
}
var len = end - start
- if (len < 100 || !TYPED_ARRAY_SUPPORT) {
+ if (len < 100 || !Buffer.TYPED_ARRAY_SUPPORT) {
for (var i = 0; i < len; i++) {
target[i + target_start] = this[i + start]
}
if (end < start)
end = start
- if (TYPED_ARRAY_SUPPORT) {
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
return Buffer._augment(this.subarray(start, end))
} else {
var sliceLen = end - start
return readDouble(this, offset, false, noAssert)
}
-Buffer.prototype.writeUInt8 = function (value, offset, noAssert) {
- if (!noAssert) {
- assert(value !== undefined && value !== null, 'missing value')
- assert(offset !== undefined && offset !== null, 'missing offset')
- assert(offset < this.length, 'trying to write beyond buffer length')
- verifuint(value, 0xff)
- }
-
- if (offset >= this.length) return
+function checkInt (buf, value, offset, ext, max, min) {
+ assert(Buffer.isBuffer(buf), 'buffer must be a Buffer instance')
+ assert(value <= max && value >= min, 'value is out of bounds')
+ assert(offset + ext <= buf.length, 'index out of range')
+}
+Buffer.prototype.writeUInt8 = function (value, offset, noAssert) {
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 1, 0xff, 0)
+ if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
this[offset] = value
return offset + 1
}
-function writeUInt16 (buf, value, offset, littleEndian, noAssert) {
- if (!noAssert) {
- assert(value !== undefined && value !== null, 'missing value')
- assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
- assert(offset !== undefined && offset !== null, 'missing offset')
- assert(offset + 1 < buf.length, 'trying to write beyond buffer length')
- verifuint(value, 0xffff)
- }
-
- var len = buf.length
- if (offset >= len)
- return
-
- for (var i = 0, j = Math.min(len - offset, 2); i < j; i++) {
- buf[offset + i] =
- (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
- (littleEndian ? i : 1 - i) * 8
+function objectWriteUInt16 (buf, value, offset, littleEndian) {
+ if (value < 0) value = 0xffff + value + 1
+ for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) {
+ buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
+ (littleEndian ? i : 1 - i) * 8
}
- return offset + 2
}
Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) {
- return writeUInt16(this, value, offset, true, noAssert)
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 2, 0xffff, 0)
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
+ this[offset] = value
+ this[offset + 1] = (value >>> 8)
+ } else objectWriteUInt16(this, value, offset, true)
+ return offset + 2
}
Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) {
- return writeUInt16(this, value, offset, false, noAssert)
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 2, 0xffff, 0)
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
+ this[offset] = (value >>> 8)
+ this[offset + 1] = value
+ } else objectWriteUInt16(this, value, offset, false)
+ return offset + 2
}
-function writeUInt32 (buf, value, offset, littleEndian, noAssert) {
- if (!noAssert) {
- assert(value !== undefined && value !== null, 'missing value')
- assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
- assert(offset !== undefined && offset !== null, 'missing offset')
- assert(offset + 3 < buf.length, 'trying to write beyond buffer length')
- verifuint(value, 0xffffffff)
- }
-
- var len = buf.length
- if (offset >= len)
- return
-
- for (var i = 0, j = Math.min(len - offset, 4); i < j; i++) {
- buf[offset + i] =
- (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
+function objectWriteUInt32 (buf, value, offset, littleEndian) {
+ if (value < 0) value = 0xffffffff + value + 1
+ for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) {
+ buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
}
- return offset + 4
}
Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) {
- return writeUInt32(this, value, offset, true, noAssert)
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 4, 0xffffffff, 0)
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
+ this[offset + 3] = (value >>> 24)
+ this[offset + 2] = (value >>> 16)
+ this[offset + 1] = (value >>> 8)
+ this[offset] = value
+ } else objectWriteUInt32(this, value, offset, true)
+ return offset + 4
}
Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) {
- return writeUInt32(this, value, offset, false, noAssert)
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 4, 0xffffffff, 0)
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
+ this[offset] = (value >>> 24)
+ this[offset + 1] = (value >>> 16)
+ this[offset + 2] = (value >>> 8)
+ this[offset + 3] = value
+ } else objectWriteUInt32(this, value, offset, false)
+ return offset + 4
}
Buffer.prototype.writeInt8 = function (value, offset, noAssert) {
- if (!noAssert) {
- assert(value !== undefined && value !== null, 'missing value')
- assert(offset !== undefined && offset !== null, 'missing offset')
- assert(offset < this.length, 'Trying to write beyond buffer length')
- verifsint(value, 0x7f, -0x80)
- }
-
- if (offset >= this.length)
- return
-
- if (value >= 0)
- this.writeUInt8(value, offset, noAssert)
- else
- this.writeUInt8(0xff + value + 1, offset, noAssert)
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 1, 0x7f, -0x80)
+ if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
+ if (value < 0) value = 0xff + value + 1
+ this[offset] = value
return offset + 1
}
-function writeInt16 (buf, value, offset, littleEndian, noAssert) {
- if (!noAssert) {
- assert(value !== undefined && value !== null, 'missing value')
- assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
- assert(offset !== undefined && offset !== null, 'missing offset')
- assert(offset + 1 < buf.length, 'Trying to write beyond buffer length')
- verifsint(value, 0x7fff, -0x8000)
- }
-
- var len = buf.length
- if (offset >= len)
- return
-
- if (value >= 0)
- writeUInt16(buf, value, offset, littleEndian, noAssert)
- else
- writeUInt16(buf, 0xffff + value + 1, offset, littleEndian, noAssert)
- return offset + 2
-}
-
Buffer.prototype.writeInt16LE = function (value, offset, noAssert) {
- return writeInt16(this, value, offset, true, noAssert)
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 2, 0x7fff, -0x8000)
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
+ this[offset] = value
+ this[offset + 1] = (value >>> 8)
+ } else objectWriteUInt16(this, value, offset, true)
+ return offset + 2
}
Buffer.prototype.writeInt16BE = function (value, offset, noAssert) {
- return writeInt16(this, value, offset, false, noAssert)
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 2, 0x7fff, -0x8000)
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
+ this[offset] = (value >>> 8)
+ this[offset + 1] = value
+ } else objectWriteUInt16(this, value, offset, false)
+ return offset + 2
}
-function writeInt32 (buf, value, offset, littleEndian, noAssert) {
- if (!noAssert) {
- assert(value !== undefined && value !== null, 'missing value')
- assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
- assert(offset !== undefined && offset !== null, 'missing offset')
- assert(offset + 3 < buf.length, 'Trying to write beyond buffer length')
- verifsint(value, 0x7fffffff, -0x80000000)
- }
-
- var len = buf.length
- if (offset >= len)
- return
-
- if (value >= 0)
- writeUInt32(buf, value, offset, littleEndian, noAssert)
- else
- writeUInt32(buf, 0xffffffff + value + 1, offset, littleEndian, noAssert)
+Buffer.prototype.writeInt32LE = function (value, offset, noAssert) {
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
+ this[offset] = value
+ this[offset + 1] = (value >>> 8)
+ this[offset + 2] = (value >>> 16)
+ this[offset + 3] = (value >>> 24)
+ } else objectWriteUInt32(this, value, offset, true)
return offset + 4
}
-Buffer.prototype.writeInt32LE = function (value, offset, noAssert) {
- return writeInt32(this, value, offset, true, noAssert)
+Buffer.prototype.writeInt32BE = function (value, offset, noAssert) {
+ value = +value
+ offset = offset >>> 0
+ if (!noAssert)
+ checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
+ if (value < 0) value = 0xffffffff + value + 1
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
+ this[offset] = (value >>> 24)
+ this[offset + 1] = (value >>> 16)
+ this[offset + 2] = (value >>> 8)
+ this[offset + 3] = value
+ } else objectWriteUInt32(this, value, offset, false)
+ return offset + 4
}
-Buffer.prototype.writeInt32BE = function (value, offset, noAssert) {
- return writeInt32(this, value, offset, false, noAssert)
+function checkIEEE754 (buf, value, offset, ext, max, min) {
+ assert(value <= max && value >= min, 'value is out of bounds')
+ assert(offset + ext <= buf.length, 'index out of range')
}
function writeFloat (buf, value, offset, littleEndian, noAssert) {
- if (!noAssert) {
- assert(value !== undefined && value !== null, 'missing value')
- assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
- assert(offset !== undefined && offset !== null, 'missing offset')
- assert(offset + 3 < buf.length, 'Trying to write beyond buffer length')
- verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38)
- }
-
- var len = buf.length
- if (offset >= len)
- return
-
+ if (!noAssert)
+ checkIEEE754(this, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
ieee754.write(buf, value, offset, littleEndian, 23, 4)
return offset + 4
}
}
function writeDouble (buf, value, offset, littleEndian, noAssert) {
- if (!noAssert) {
- assert(value !== undefined && value !== null, 'missing value')
- assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
- assert(offset !== undefined && offset !== null, 'missing offset')
- assert(offset + 7 < buf.length,
- 'Trying to write beyond buffer length')
- verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308)
- }
-
- var len = buf.length
- if (offset >= len)
- return
-
+ if (!noAssert)
+ checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
ieee754.write(buf, value, offset, littleEndian, 52, 8)
return offset + 8
}
*/
Buffer.prototype.toArrayBuffer = function () {
if (typeof Uint8Array !== 'undefined') {
- if (TYPED_ARRAY_SUPPORT) {
+ if (Buffer.TYPED_ARRAY_SUPPORT) {
return (new Buffer(this)).buffer
} else {
var buf = new Uint8Array(this.length)
}
}
-/*
- * We have to make sure that the value is a valid integer. This means that it
- * is non-negative. It has no fractional component and that it does not
- * exceed the maximum allowed value.
- */
-function verifuint (value, max) {
- assert(typeof value === 'number', 'cannot write a non-number as a number')
- assert(value >= 0, 'specified a negative value for writing an unsigned value')
- assert(value <= max, 'value is larger than maximum value for type')
- assert(Math.floor(value) === value, 'value has a fractional component')
-}
-
-function verifsint (value, max, min) {
- assert(typeof value === 'number', 'cannot write a non-number as a number')
- assert(value <= max, 'value larger than maximum allowed value')
- assert(value >= min, 'value smaller than minimum allowed value')
- assert(Math.floor(value) === value, 'value has a fractional component')
-}
-
-function verifIEEE754 (value, max, min) {
- assert(typeof value === 'number', 'cannot write a non-number as a number')
- assert(value <= max, 'value larger than maximum allowed value')
- assert(value >= min, 'value smaller than minimum allowed value')
-}
-
function assert (test, message) {
if (!test) throw new Error(message || 'Failed assertion')
}
--- /dev/null
+var B = require('../').Buffer
+var test = require('tape')
+if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
+
+test('buffer.write string should get parsed as number', function (t) {
+ var b = new B(64)
+ b.writeUInt16LE('1003', 0)
+ t.equal(b.readUInt16LE(0), 1003)
+ t.end()
+})
+
+test('buffer.writeUInt8 a fractional number will get Math.floored', function (t) {
+ // Some extra work is necessary to make this test pass with the Object implementation
+
+ var b = new B(1)
+ b.writeInt8(5.5, 0)
+ t.equal(b[0], 5)
+ t.end()
+})
+
+test('writeUint8 with a negative number throws', function (t) {
+ var buf = new B(1)
+
+ t.throws(function () {
+ buf.writeUInt8(-3, 0)
+ })
+
+ t.end()
+})
+
+test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) {
+ t.plan(2 * (2 * 2 * 2 + 2))
+ var hex = [
+ '03', '0300', '0003', '03000000', '00000003',
+ 'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd'
+ ]
+ var reads = [ 3, 3, 3, 3, 3, -3, -3, -3, -3, -3 ]
+ var xs = ['UInt','Int']
+ var ys = [8,16,32]
+ for (var i = 0; i < xs.length; i++) {
+ var x = xs[i]
+ for (var j = 0; j < ys.length; j++) {
+ var y = ys[j]
+ var endianesses = (y === 8) ? [''] : ['LE','BE']
+ for (var k = 0; k < endianesses.length; k++) {
+ var z = endianesses[k]
+
+ 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()
+ )
+ }
+ }
+ }
+ t.end()
+})
+
+test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) {
+ t.plan(3 * (2 * 2 * 2 + 2))
+ var hex = [
+ '', '03', '00', '030000', '000000',
+ '', 'fd', 'ff', 'fdffff', 'ffffff'
+ ]
+ var reads = [
+ undefined, 3, 0, 3, 0,
+ undefined, 253, -256, 16777213, -256
+ ]
+ var xs = ['UInt','Int']
+ var ys = [8,16,32]
+ for (var i = 0; i < xs.length; i++) {
+ var x = xs[i]
+ for (var j = 0; j < ys.length; j++) {
+ var y = ys[j]
+ var endianesses = (y === 8) ? [''] : ['LE','BE']
+ for (var k = 0; k < endianesses.length; k++) {
+ var z = endianesses[k]
+
+ var v1 = new B(y / 8 - 1)
+ var next = new B(4)
+ next.writeUInt32BE(0, 0)
+ var writefn = 'write' + x + y + z
+ var val = (x === 'Int') ? -3 : 3
+ v1[writefn](val, 0, true)
+ t.equal(
+ v1.toString('hex'),
+ hex.shift()
+ )
+ // check that nothing leaked to next buffer.
+ t.equal(next.readUInt32BE(0), 0)
+ // 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()
+ )
+ }
+ }
+ }
+ t.end()
+})