summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTrevor Norris <trev.norris@gmail.com>2013-11-14 15:40:56 -0800
committerTrevor Norris <trev.norris@gmail.com>2013-11-15 11:48:09 -0800
commitd6df1b91579e95283768935ec3f43eb34e2dc818 (patch)
tree5d287359294945e0435f72966fc2ff17ed3e5786 /lib
parente5346932bcbc523489c9418b82fde31cb666ee99 (diff)
downloadnodejs-d6df1b91579e95283768935ec3f43eb34e2dc818.tar.gz
nodejs-d6df1b91579e95283768935ec3f43eb34e2dc818.tar.bz2
nodejs-d6df1b91579e95283768935ec3f43eb34e2dc818.zip
buffer: convert values to uint, not int
In many cases values expected to be unsigned were converted to a signed integer. Also include some small code cleanup.
Diffstat (limited to 'lib')
-rw-r--r--lib/buffer.js84
1 files changed, 40 insertions, 44 deletions
diff --git a/lib/buffer.js b/lib/buffer.js
index bfaee2c5e..b43363024 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -33,9 +33,7 @@ exports.INSPECT_MAX_BYTES = 50;
Buffer.poolSize = 8 * 1024;
-var poolSize = Buffer.poolSize;
-var poolOffset = 0;
-var allocPool = alloc({}, poolSize);
+var poolSize, poolOffset, allocPool;
function createPool() {
@@ -43,6 +41,7 @@ function createPool() {
allocPool = alloc({}, poolSize);
poolOffset = 0;
}
+createPool();
function Buffer(subject, encoding) {
@@ -50,7 +49,7 @@ function Buffer(subject, encoding) {
return new Buffer(subject, encoding);
if (util.isNumber(subject))
- this.length = subject > 0 ? Math.floor(subject) : 0;
+ this.length = subject > 0 ? subject >>> 0 : 0;
else if (util.isString(subject))
this.length = Buffer.byteLength(subject, encoding = encoding || 'utf8');
else if (util.isObject(subject))
@@ -63,7 +62,7 @@ function Buffer(subject, encoding) {
'size: 0x' + kMaxLength.toString(16) + ' bytes');
}
- if (this.length < Buffer.poolSize / 2 && this.length > 0) {
+ if (this.length <= (Buffer.poolSize >>> 1) && this.length > 0) {
if (this.length > poolSize - poolOffset)
createPool();
this.parent = sliceOnto(allocPool,
@@ -154,11 +153,9 @@ Buffer.concat = function(list, length) {
for (var i = 0; i < list.length; i++)
length += list[i].length;
} else {
- length = ~~length;
+ length = length >>> 0;
}
- if (length < 0) length = 0;
-
if (list.length === 0)
return new Buffer(0);
else if (list.length === 1)
@@ -291,19 +288,21 @@ var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
Buffer.prototype.write = function(string, offset, length, encoding) {
// Buffer#write(string);
if (util.isUndefined(offset)) {
- offset = 0;
encoding = 'utf8';
+ length = this.length;
+ offset = 0;
// Buffer#write(string, encoding)
} else if (util.isUndefined(length) && util.isString(offset)) {
encoding = offset;
+ length = this.length;
offset = 0;
// Buffer#write(string, offset[, length][, encoding])
} else if (isFinite(offset)) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (isFinite(length)) {
- length = ~~length;
+ length = length >>> 0;
if (util.isUndefined(encoding))
encoding = 'utf8';
} else {
@@ -325,7 +324,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
var swap = encoding;
encoding = offset;
- offset = ~~length;
+ offset = length >>> 0;
length = swap;
}
@@ -336,7 +335,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
if (string.length > 0 && (length < 0 || offset < 0))
- throw new RangeError('attempt to write beyond buffer bounds');
+ throw new RangeError('attempt to write outside buffer bounds');
var ret;
switch (encoding) {
@@ -422,13 +421,13 @@ Buffer.prototype.slice = function(start, end) {
function checkOffset(offset, ext, length) {
- if (offset < 0 || offset + ext > length)
+ if (offset + ext > length)
throw new RangeError('index out of range');
}
Buffer.prototype.readUInt8 = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 1, this.length);
return this[offset];
@@ -449,7 +448,7 @@ function readUInt16(buffer, offset, isBigEndian) {
Buffer.prototype.readUInt16LE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return readUInt16(this, offset, false, noAssert);
@@ -457,7 +456,7 @@ Buffer.prototype.readUInt16LE = function(offset, noAssert) {
Buffer.prototype.readUInt16BE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return readUInt16(this, offset, true, noAssert);
@@ -482,7 +481,7 @@ function readUInt32(buffer, offset, isBigEndian) {
Buffer.prototype.readUInt32LE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return readUInt32(this, offset, false);
@@ -490,7 +489,7 @@ Buffer.prototype.readUInt32LE = function(offset, noAssert) {
Buffer.prototype.readUInt32BE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return readUInt32(this, offset, true);
@@ -544,25 +543,22 @@ Buffer.prototype.readUInt32BE = function(offset, noAssert) {
*/
Buffer.prototype.readInt8 = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 1, this.length);
- if (!(this[offset] & 0x80))
- return (this[offset]);
- return ((0xff - this[offset] + 1) * -1);
+ var val = this[offset];
+ return !(val & 0x80) ? val : (0xff - val + 1) * -1;
};
function readInt16(buffer, offset, isBigEndian) {
var val = readUInt16(buffer, offset, isBigEndian);
- if (!(val & 0x8000))
- return val;
- return (0xffff - val + 1) * -1;
+ return !(val & 0x8000) ? val : (0xffff - val + 1) * -1;
}
Buffer.prototype.readInt16LE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return readInt16(this, offset, false);
@@ -570,7 +566,7 @@ Buffer.prototype.readInt16LE = function(offset, noAssert) {
Buffer.prototype.readInt16BE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return readInt16(this, offset, true);
@@ -579,14 +575,12 @@ Buffer.prototype.readInt16BE = function(offset, noAssert) {
function readInt32(buffer, offset, isBigEndian) {
var val = readUInt32(buffer, offset, isBigEndian);
- if (!(val & 0x80000000))
- return (val);
- return (0xffffffff - val + 1) * -1;
+ return !(val & 0x80000000) ? val : (0xffffffff - val + 1) * -1;
}
Buffer.prototype.readInt32LE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return readInt32(this, offset, false);
@@ -594,7 +588,7 @@ Buffer.prototype.readInt32LE = function(offset, noAssert) {
Buffer.prototype.readInt32BE = function(offset, noAssert) {
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return readInt32(this, offset, true);
@@ -602,16 +596,18 @@ Buffer.prototype.readInt32BE = function(offset, noAssert) {
function checkInt(buffer, value, offset, ext, max, min) {
+ if (!(buffer instanceof Buffer))
+ throw new TypeError('buffer must be a Buffer instance');
if (value > max || value < min)
throw new TypeError('value is out of bounds');
- if (offset < 0 || offset + ext > buffer.length || buffer.length + offset < 0)
+ if (offset + ext > buffer.length)
throw new RangeError('index out of range');
}
Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 1, 0xff, 0);
this[offset] = value;
@@ -633,7 +629,7 @@ function writeUInt16(buffer, value, offset, isBigEndian) {
Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0xffff, 0);
return writeUInt16(this, value, offset, false);
@@ -642,7 +638,7 @@ Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0xffff, 0);
return writeUInt16(this, value, offset, true);
@@ -667,7 +663,7 @@ function writeUInt32(buffer, value, offset, isBigEndian) {
Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0xffffffff, 0);
return writeUInt32(this, value, offset, false);
@@ -676,7 +672,7 @@ Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0xffffffff, 0);
return writeUInt32(this, value, offset, true);
@@ -722,7 +718,7 @@ Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 1, 0x7f, -0x80);
if (value < 0) value = 0xff + value + 1;
@@ -733,7 +729,7 @@ Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
if (value < 0) value = 0xffff + value + 1;
@@ -743,7 +739,7 @@ Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
if (value < 0) value = 0xffff + value + 1;
@@ -753,7 +749,7 @@ Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
if (value < 0) value = 0xffffffff + value + 1;
@@ -763,7 +759,7 @@ Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
value = +value;
- offset = ~~offset;
+ offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
if (value < 0) value = 0xffffffff + value + 1;