Skip to content

Commit

Permalink
Convert offsets to unsigned int consistently
Browse files Browse the repository at this point in the history
As pointed out by @jdalton
  • Loading branch information
feross committed Sep 27, 2016
1 parent fe54a78 commit 99491d2
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ Buffer.prototype.swap64 = function swap64 () {
}

Buffer.prototype.toString = function toString () {
var length = this.length | 0
var length = this.length
if (length === 0) return ''
if (arguments.length === 0) return utf8Slice(this, 0, length)
return slowToString.apply(this, arguments)
Expand Down Expand Up @@ -801,9 +801,9 @@ Buffer.prototype.write = function write (string, offset, length, encoding) {
offset = 0
// Buffer#write(string, offset[, length][, encoding])
} else if (isFinite(offset)) {
offset = offset | 0
offset = offset >>> 0
if (isFinite(length)) {
length = length | 0
length = length >>> 0
if (encoding === undefined) encoding = 'utf8'
} else {
encoding = length
Expand Down Expand Up @@ -1048,8 +1048,8 @@ function checkOffset (offset, ext, length) {
}

Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
offset = offset | 0
byteLength = byteLength | 0
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert) checkOffset(offset, byteLength, this.length)

var val = this[offset]
Expand All @@ -1063,8 +1063,8 @@ Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert)
}

Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
offset = offset | 0
byteLength = byteLength | 0
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert) {
checkOffset(offset, byteLength, this.length)
}
Expand All @@ -1079,21 +1079,25 @@ Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert)
}

Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 1, this.length)
return this[offset]
}

Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 2, this.length)
return this[offset] | (this[offset + 1] << 8)
}

Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 2, this.length)
return (this[offset] << 8) | this[offset + 1]
}

Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 4, this.length)

return ((this[offset]) |
Expand All @@ -1103,6 +1107,7 @@ Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
}

Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 4, this.length)

return (this[offset] * 0x1000000) +
Expand All @@ -1112,8 +1117,8 @@ Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
}

Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
offset = offset | 0
byteLength = byteLength | 0
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert) checkOffset(offset, byteLength, this.length)

var val = this[offset]
Expand All @@ -1130,8 +1135,8 @@ Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
}

Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
offset = offset | 0
byteLength = byteLength | 0
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert) checkOffset(offset, byteLength, this.length)

var i = byteLength
Expand All @@ -1148,24 +1153,28 @@ Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
}

Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 1, this.length)
if (!(this[offset] & 0x80)) return (this[offset])
return ((0xff - this[offset] + 1) * -1)
}

Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
offset = offset >>> 0
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 readInt16BE (offset, noAssert) {
offset = offset >>> 0
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 readInt32LE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 4, this.length)

return (this[offset]) |
Expand All @@ -1175,6 +1184,7 @@ Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
}

Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 4, this.length)

return (this[offset] << 24) |
Expand All @@ -1184,21 +1194,25 @@ Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
}

Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 4, this.length)
return ieee754.read(this, offset, true, 23, 4)
}

Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 4, this.length)
return ieee754.read(this, offset, false, 23, 4)
}

Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 8, this.length)
return ieee754.read(this, offset, true, 52, 8)
}

Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 8, this.length)
return ieee754.read(this, offset, false, 52, 8)
}
Expand All @@ -1211,8 +1225,8 @@ function checkInt (buf, value, offset, ext, max, min) {

Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
value = +value
offset = offset | 0
byteLength = byteLength | 0
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert) {
var maxBytes = Math.pow(2, 8 * byteLength) - 1
checkInt(this, value, offset, byteLength, maxBytes, 0)
Expand All @@ -1230,8 +1244,8 @@ Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength,

Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
value = +value
offset = offset | 0
byteLength = byteLength | 0
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert) {
var maxBytes = Math.pow(2, 8 * byteLength) - 1
checkInt(this, value, offset, byteLength, maxBytes, 0)
Expand All @@ -1249,15 +1263,15 @@ Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength,

Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
this[offset] = (value & 0xff)
return offset + 1
}

Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
this[offset] = (value & 0xff)
this[offset + 1] = (value >>> 8)
Expand All @@ -1266,7 +1280,7 @@ Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert

Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
this[offset] = (value >>> 8)
this[offset + 1] = (value & 0xff)
Expand All @@ -1275,7 +1289,7 @@ Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert

Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
this[offset + 3] = (value >>> 24)
this[offset + 2] = (value >>> 16)
Expand All @@ -1286,7 +1300,7 @@ Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert

Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
this[offset] = (value >>> 24)
this[offset + 1] = (value >>> 16)
Expand All @@ -1297,7 +1311,7 @@ Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert

Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) {
var limit = Math.pow(2, 8 * byteLength - 1)

Expand All @@ -1320,7 +1334,7 @@ Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, no

Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) {
var limit = Math.pow(2, 8 * byteLength - 1)

Expand All @@ -1343,7 +1357,7 @@ Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, no

Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
if (value < 0) value = 0xff + value + 1
this[offset] = (value & 0xff)
Expand All @@ -1352,7 +1366,7 @@ Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {

Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
this[offset] = (value & 0xff)
this[offset + 1] = (value >>> 8)
Expand All @@ -1361,7 +1375,7 @@ Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert)

Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
this[offset] = (value >>> 8)
this[offset + 1] = (value & 0xff)
Expand All @@ -1370,7 +1384,7 @@ Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert)

Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
this[offset] = (value & 0xff)
this[offset + 1] = (value >>> 8)
Expand All @@ -1381,7 +1395,7 @@ Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert)

Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
value = +value
offset = offset | 0
offset = offset >>> 0
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
if (value < 0) value = 0xffffffff + value + 1
this[offset] = (value >>> 24)
Expand All @@ -1397,6 +1411,8 @@ function checkIEEE754 (buf, value, offset, ext, max, min) {
}

function writeFloat (buf, value, offset, littleEndian, noAssert) {
value = +value
offset = offset >>> 0
if (!noAssert) {
checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
}
Expand All @@ -1413,6 +1429,8 @@ Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert)
}

function writeDouble (buf, value, offset, littleEndian, noAssert) {
value = +value
offset = offset >>> 0
if (!noAssert) {
checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
}
Expand Down

0 comments on commit 99491d2

Please sign in to comment.