var SlowBuffer = process.binding('buffer').SlowBuffer; var assert = require('assert'); exports.INSPECT_MAX_BYTES = 50; SlowBuffer.prototype.__proto__ = Buffer.prototype; function toHex(n){ if (n < 16) return '0' + n.toString(16); return n.toString(16); } SlowBuffer.prototype.hexSlice = function (start, end){ var len = _AN_Read_length('length', this); if (!start || start < 0) start = 0; if (!end || end < 0 || end > len) end = len; var out = ''; for (var i = start; i < end; i++ ){ out += toHex(this[i]); } return out; } ; SlowBuffer.prototype.toString = function (encoding, start, end){ encoding = String(encoding || 'utf8').toLowerCase(); start = + start || 0; if (typeof end == 'undefined') end = _AN_Read_length('length', this); if (+ end == start) { return ''; } switch (encoding){ case 'hex': return this.hexSlice(start, end); case 'utf8': case 'utf-8': return this.utf8Slice(start, end); case 'ascii': return this.asciiSlice(start, end); case 'binary': return this.binarySlice(start, end); case 'base64': return this.base64Slice(start, end); case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return this.ucs2Slice(start, end); default : { throw new Error('Unknown encoding') } } } ; SlowBuffer.prototype.hexWrite = function (string, offset, length){ offset = + offset || 0; var remaining = _AN_Read_length('length', this) - offset; if (!length) { length = remaining; } else { length = + length; if (length > remaining) { length = remaining; } } var strLen = _AN_Read_length('length', string); if (strLen % 2) { throw new Error('Invalid hex string') } if (length > strLen / 2) { length = strLen / 2; } for (var i = 0; i < length; i++ ){ var byte = parseInt(string.substr(i * 2, 2), 16); if (isNaN(byte)) throw new Error('Invalid hex string') this[offset + i] = byte; } SlowBuffer._charsWritten = i * 2; return i; } ; SlowBuffer.prototype.write = function (string, offset, length, encoding){ if (isFinite(offset)) { if (!isFinite(length)) { encoding = length; length = undefined; } } else { var swap = encoding; encoding = offset; offset = length; length = swap; } offset = + offset || 0; var remaining = _AN_Read_length('length', this) - offset; if (!length) { length = remaining; } else { length = + length; if (length > remaining) { length = remaining; } } encoding = String(encoding || 'utf8').toLowerCase(); switch (encoding){ case 'hex': return this.hexWrite(string, offset, length); case 'utf8': case 'utf-8': return this.utf8Write(string, offset, length); case 'ascii': return this.asciiWrite(string, offset, length); case 'binary': return this.binaryWrite(string, offset, length); case 'base64': return this.base64Write(string, offset, length); case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return this.ucs2Write(string, offset, length); default : { throw new Error('Unknown encoding') } } } ; SlowBuffer.prototype.slice = function (start, end){ if (end === undefined) end = _AN_Read_length('length', this); if (end > _AN_Read_length('length', this)) { throw new Error('oob') } if (start > end) { throw new Error('oob') } return new Buffer(this, end - start, + start); } ; function coerce(length){ length = ~~Math.ceil(+ length); return length < 0? 0: length; } function Buffer(subject, encoding, offset){ if (!(this instanceof Buffer)) { return new Buffer(subject, encoding, offset); } var type; if (typeof offset === 'number') { if (!Buffer.isBuffer(subject)) { throw new Error('First argument must be a Buffer when slicing') } this.length = coerce(encoding); this.parent = subject.parent? subject.parent: subject; this.offset = offset; } else { switch (type = typeof subject){ case 'number': this.length = coerce(subject); break ; case 'string': this.length = Buffer.byteLength(subject, encoding); break ; case 'object': this.length = coerce(_AN_Read_length('length', subject)); break ; default : { throw new Error('First argument needs to be a number, ' + 'array or string.') } } if (_AN_Read_length('length', this) > Buffer.poolSize) { this.parent = new SlowBuffer((_AN_Read_length('length', this))); this.offset = 0; } else { if (!pool || _AN_Read_length('length', pool) - pool.used < _AN_Read_length('length', this)) allocPool(); this.parent = pool; this.offset = pool.used; pool.used += _AN_Read_length('length', this); if (pool.used & 7) pool.used = (pool.used + 8) & -8; } if (isArrayIsh(subject)) { for (var i = 0; i < _AN_Read_length('length', this); i++ ){ this.parent[i + this.offset] = subject[i]; } } else if (type == 'string') { this.length = _AN_Call_write('write', this, subject, 0, encoding); } } SlowBuffer.makeFastBuffer(this.parent, this, this.offset, _AN_Read_length('length', this)); } function isArrayIsh(subject){ return Array.isArray(subject) || Buffer.isBuffer(subject) || subject && typeof subject === 'object' && typeof _AN_Read_length('length', subject) === 'number'; } exports.SlowBuffer = SlowBuffer; exports.Buffer = Buffer; Buffer.poolSize = 8 * 1024; var pool; function allocPool(){ pool = new SlowBuffer(Buffer.poolSize); pool.used = 0; } Buffer.isBuffer = function isBuffer(b){ return b instanceof Buffer; } ; Buffer.prototype.inspect = function inspect(){ var out = [] , len = _AN_Read_length('length', this), name = this.constructor.name; for (var i = 0; i < len; i++ ){ out[i] = toHex(this[i]); if (i == exports.INSPECT_MAX_BYTES) { out[i + 1] = '...'; break ; } } return '<' + name + ' ' + out.join(' ') + '>'; } ; Buffer.prototype.get = function get(i){ if (i < 0 || i >= _AN_Read_length('length', this)) throw new Error('oob') return this.parent[this.offset + i]; } ; Buffer.prototype.set = function set(i, v){ if (i < 0 || i >= _AN_Read_length('length', this)) throw new Error('oob') return this.parent[this.offset + i] = v; } ; Buffer.prototype.write = function (string, offset, length, encoding){ if (isFinite(offset)) { if (!isFinite(length)) { encoding = length; length = undefined; } } else { var swap = encoding; encoding = offset; offset = length; length = swap; } offset = + offset || 0; var remaining = _AN_Read_length('length', this) - offset; if (!length) { length = remaining; } else { length = + length; if (length > remaining) { length = remaining; } } encoding = String(encoding || 'utf8').toLowerCase(); var ret; switch (encoding){ case 'hex': ret = this.parent.hexWrite(string, this.offset + offset, length); break ; case 'utf8': case 'utf-8': ret = this.parent.utf8Write(string, this.offset + offset, length); break ; case 'ascii': ret = this.parent.asciiWrite(string, this.offset + offset, length); break ; case 'binary': ret = this.parent.binaryWrite(string, this.offset + offset, length); break ; case 'base64': ret = this.parent.base64Write(string, this.offset + offset, length); break ; case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': ret = this.parent.ucs2Write(string, this.offset + offset, length); break ; default : { throw new Error('Unknown encoding') } } Buffer._charsWritten = SlowBuffer._charsWritten; return ret; } ; Buffer.prototype.toString = function (encoding, start, end){ encoding = String(encoding || 'utf8').toLowerCase(); if (typeof start == 'undefined' || start < 0) { start = 0; } else if (start > _AN_Read_length('length', this)) { start = _AN_Read_length('length', this); } if (typeof end == 'undefined' || end > _AN_Read_length('length', this)) { end = _AN_Read_length('length', this); } else if (end < 0) { end = 0; } start = start + this.offset; end = end + this.offset; switch (encoding){ case 'hex': return this.parent.hexSlice(start, end); case 'utf8': case 'utf-8': return this.parent.utf8Slice(start, end); case 'ascii': return this.parent.asciiSlice(start, end); case 'binary': return this.parent.binarySlice(start, end); case 'base64': return this.parent.base64Slice(start, end); case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return this.parent.ucs2Slice(start, end); default : { throw new Error('Unknown encoding') } } } ; Buffer.byteLength = SlowBuffer.byteLength; Buffer.prototype.fill = function fill(value, start, end){ value || (value = 0); start || (start = 0); end || (end = _AN_Read_length('length', this)); if (typeof value === 'string') { value = value.charCodeAt(0); } if (!(typeof value === 'number') || isNaN(value)) { throw new Error('value is not a number') } if (end < start) throw new Error('end < start') if (end === start) return 0; if (_AN_Read_length('length', this) == 0) return 0; if (start < 0 || start >= _AN_Read_length('length', this)) { throw new Error('start out of bounds') } if (end < 0 || end > _AN_Read_length('length', this)) { throw new Error('end out of bounds') } return this.parent.fill(value, start + this.offset, end + this.offset); } ; Buffer.concat = function (list, length){ if (!Array.isArray(list)) { throw new Error('Usage: Buffer.concat(list, [length])') } if (_AN_Read_length('length', list) === 0) { return new Buffer(0); } else if (_AN_Read_length('length', list) === 1) { return list[0]; } if (typeof length !== 'number') { length = 0; for (var i = 0; i < _AN_Read_length('length', list); i++ ){ var buf = list[i]; length += _AN_Read_length('length', buf); } } var buffer = new Buffer(length); var pos = 0; for (var i = 0; i < _AN_Read_length('length', list); i++ ){ var buf = list[i]; buf.copy(buffer, pos); pos += _AN_Read_length('length', buf); } return buffer; } ; Buffer.prototype.copy = function (target, target_start, start, end){ var source = this; start || (start = 0); end || (end = _AN_Read_length('length', this)); target_start || (target_start = 0); if (end < start) throw new Error('sourceEnd < sourceStart') if (end === start) return 0; if (_AN_Read_length('length', target) == 0 || _AN_Read_length('length', source) == 0) return 0; if (target_start < 0 || target_start >= _AN_Read_length('length', target)) { throw new Error('targetStart out of bounds') } if (start < 0 || start >= _AN_Read_length('length', source)) { throw new Error('sourceStart out of bounds') } if (end < 0 || end > _AN_Read_length('length', source)) { throw new Error('sourceEnd out of bounds') } if (end > _AN_Read_length('length', this)) { end = _AN_Read_length('length', this); } if (_AN_Read_length('length', target) - target_start < end - start) { end = _AN_Read_length('length', target) - target_start + start; } return this.parent.copy(target.parent, target_start + target.offset, start + this.offset, end + this.offset); } ; Buffer.prototype.slice = function (start, end){ if (end === undefined) end = _AN_Read_length('length', this); if (end > _AN_Read_length('length', this)) throw new Error('oob') if (start > end) throw new Error('oob') return new Buffer(this.parent, end - start, + start + this.offset); } ; Buffer.prototype.utf8Slice = function (start, end){ return this.toString('utf8', start, end); } ; Buffer.prototype.binarySlice = function (start, end){ return this.toString('binary', start, end); } ; Buffer.prototype.asciiSlice = function (start, end){ return this.toString('ascii', start, end); } ; Buffer.prototype.utf8Write = function (string, offset){ return _AN_Call_write('write', this, string, offset, 'utf8'); } ; Buffer.prototype.binaryWrite = function (string, offset){ return _AN_Call_write('write', this, string, offset, 'binary'); } ; Buffer.prototype.asciiWrite = function (string, offset){ return _AN_Call_write('write', this, string, offset, 'ascii'); } ; Buffer.prototype.readUInt8 = function (offset, noAssert){ var buffer = this; if (!noAssert) { assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset < _AN_Read_length('length', buffer), 'Trying to read beyond buffer length'); } return buffer[offset]; } ; function readUInt16(buffer, offset, isBigEndian, noAssert){ var val = 0; if (!noAssert) { assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 1 < _AN_Read_length('length', buffer), 'Trying to read beyond buffer length'); } if (isBigEndian) { val = buffer[offset] << 8; val |= buffer[offset + 1]; } else { val = buffer[offset]; val |= buffer[offset + 1] << 8; } return val; } Buffer.prototype.readUInt16LE = function (offset, noAssert){ return readUInt16(this, offset, false , noAssert); } ; Buffer.prototype.readUInt16BE = function (offset, noAssert){ return readUInt16(this, offset, true , noAssert); } ; function readUInt32(buffer, offset, isBigEndian, noAssert){ var val = 0; if (!noAssert) { assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 3 < _AN_Read_length('length', buffer), 'Trying to read beyond buffer length'); } if (isBigEndian) { val = buffer[offset + 1] << 16; val |= buffer[offset + 2] << 8; val |= buffer[offset + 3]; val = val + (buffer[offset] << 24 >>> 0); } else { val = buffer[offset + 2] << 16; val |= buffer[offset + 1] << 8; val |= buffer[offset]; val = val + (buffer[offset + 3] << 24 >>> 0); } return val; } Buffer.prototype.readUInt32LE = function (offset, noAssert){ return readUInt32(this, offset, false , noAssert); } ; Buffer.prototype.readUInt32BE = function (offset, noAssert){ return readUInt32(this, offset, true , noAssert); } ; Buffer.prototype.readInt8 = function (offset, noAssert){ var buffer = this; var neg; if (!noAssert) { assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset < _AN_Read_length('length', buffer), 'Trying to read beyond buffer length'); } neg = buffer[offset] & 128; if (!neg) { return (buffer[offset]); } return ((255 - buffer[offset] + 1) * -1); } ; function readInt16(buffer, offset, isBigEndian, noAssert){ var neg, val; if (!noAssert) { assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 1 < _AN_Read_length('length', buffer), 'Trying to read beyond buffer length'); } val = readUInt16(buffer, offset, isBigEndian, noAssert); neg = val & 32768; if (!neg) { return val; } return (65535 - val + 1) * -1; } Buffer.prototype.readInt16LE = function (offset, noAssert){ return readInt16(this, offset, false , noAssert); } ; Buffer.prototype.readInt16BE = function (offset, noAssert){ return readInt16(this, offset, true , noAssert); } ; function readInt32(buffer, offset, isBigEndian, noAssert){ var neg, val; if (!noAssert) { assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 3 < _AN_Read_length('length', buffer), 'Trying to read beyond buffer length'); } val = readUInt32(buffer, offset, isBigEndian, noAssert); neg = val & 2147483648; if (!neg) { return (val); } return (4294967295 - val + 1) * -1; } Buffer.prototype.readInt32LE = function (offset, noAssert){ return readInt32(this, offset, false , noAssert); } ; Buffer.prototype.readInt32BE = function (offset, noAssert){ return readInt32(this, offset, true , noAssert); } ; function readFloat(buffer, offset, isBigEndian, noAssert){ if (!noAssert) { assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset + 3 < _AN_Read_length('length', buffer), 'Trying to read beyond buffer length'); } return require('buffer_ieee754').readIEEE754(buffer, offset, isBigEndian, 23, 4); } Buffer.prototype.readFloatLE = function (offset, noAssert){ return readFloat(this, offset, false , noAssert); } ; Buffer.prototype.readFloatBE = function (offset, noAssert){ return readFloat(this, offset, true , noAssert); } ; function readDouble(buffer, offset, isBigEndian, noAssert){ if (!noAssert) { assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset + 7 < _AN_Read_length('length', buffer), 'Trying to read beyond buffer length'); } return require('buffer_ieee754').readIEEE754(buffer, offset, isBigEndian, 52, 8); } Buffer.prototype.readDoubleLE = function (offset, noAssert){ return readDouble(this, offset, false , noAssert); } ; Buffer.prototype.readDoubleBE = function (offset, noAssert){ return readDouble(this, offset, true , noAssert); } ; function verifuint(value, max){ assert.ok(typeof (value) == 'number', 'cannot write a non-number as a number'); assert.ok(value >= 0, 'specified a negative value for writing an unsigned value'); assert.ok(value <= max, 'value is larger than maximum value for type'); assert.ok(Math.floor(value) === value, 'value has a fractional component'); } Buffer.prototype.writeUInt8 = function (value, offset, noAssert){ var buffer = this; if (!noAssert) { assert.ok(value !== undefined && value !== null , 'missing value'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset < _AN_Read_length('length', buffer), 'trying to write beyond buffer length'); verifuint(value, 255); } buffer[offset] = value; } ; function writeUInt16(buffer, value, offset, isBigEndian, noAssert){ if (!noAssert) { assert.ok(value !== undefined && value !== null , 'missing value'); assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 1 < _AN_Read_length('length', buffer), 'trying to write beyond buffer length'); verifuint(value, 65535); } if (isBigEndian) { buffer[offset] = (value & 65280) >>> 8; buffer[offset + 1] = value & 255; } else { buffer[offset + 1] = (value & 65280) >>> 8; buffer[offset] = value & 255; } } Buffer.prototype.writeUInt16LE = function (value, offset, noAssert){ writeUInt16(this, value, offset, false , noAssert); } ; Buffer.prototype.writeUInt16BE = function (value, offset, noAssert){ writeUInt16(this, value, offset, true , noAssert); } ; function writeUInt32(buffer, value, offset, isBigEndian, noAssert){ if (!noAssert) { assert.ok(value !== undefined && value !== null , 'missing value'); assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 3 < _AN_Read_length('length', buffer), 'trying to write beyond buffer length'); verifuint(value, 4294967295); } if (isBigEndian) { buffer[offset] = (value >>> 24) & 255; buffer[offset + 1] = (value >>> 16) & 255; buffer[offset + 2] = (value >>> 8) & 255; buffer[offset + 3] = value & 255; } else { buffer[offset + 3] = (value >>> 24) & 255; buffer[offset + 2] = (value >>> 16) & 255; buffer[offset + 1] = (value >>> 8) & 255; buffer[offset] = value & 255; } } Buffer.prototype.writeUInt32LE = function (value, offset, noAssert){ writeUInt32(this, value, offset, false , noAssert); } ; Buffer.prototype.writeUInt32BE = function (value, offset, noAssert){ writeUInt32(this, value, offset, true , noAssert); } ; function verifsint(value, max, min){ assert.ok(typeof (value) == 'number', 'cannot write a non-number as a number'); assert.ok(value <= max, 'value larger than maximum allowed value'); assert.ok(value >= min, 'value smaller than minimum allowed value'); assert.ok(Math.floor(value) === value, 'value has a fractional component'); } function verifIEEE754(value, max, min){ assert.ok(typeof (value) == 'number', 'cannot write a non-number as a number'); assert.ok(value <= max, 'value larger than maximum allowed value'); assert.ok(value >= min, 'value smaller than minimum allowed value'); } Buffer.prototype.writeInt8 = function (value, offset, noAssert){ var buffer = this; if (!noAssert) { assert.ok(value !== undefined && value !== null , 'missing value'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset < _AN_Read_length('length', buffer), 'Trying to write beyond buffer length'); verifsint(value, 127, -128); } if (value >= 0) { buffer.writeUInt8(value, offset, noAssert); } else { buffer.writeUInt8(255 + value + 1, offset, noAssert); } } ; function writeInt16(buffer, value, offset, isBigEndian, noAssert){ if (!noAssert) { assert.ok(value !== undefined && value !== null , 'missing value'); assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 1 < _AN_Read_length('length', buffer), 'Trying to write beyond buffer length'); verifsint(value, 32767, -32768); } if (value >= 0) { writeUInt16(buffer, value, offset, isBigEndian, noAssert); } else { writeUInt16(buffer, 65535 + value + 1, offset, isBigEndian, noAssert); } } Buffer.prototype.writeInt16LE = function (value, offset, noAssert){ writeInt16(this, value, offset, false , noAssert); } ; Buffer.prototype.writeInt16BE = function (value, offset, noAssert){ writeInt16(this, value, offset, true , noAssert); } ; function writeInt32(buffer, value, offset, isBigEndian, noAssert){ if (!noAssert) { assert.ok(value !== undefined && value !== null , 'missing value'); assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 3 < _AN_Read_length('length', buffer), 'Trying to write beyond buffer length'); verifsint(value, 2147483647, -2147483648); } if (value >= 0) { writeUInt32(buffer, value, offset, isBigEndian, noAssert); } else { writeUInt32(buffer, 4294967295 + value + 1, offset, isBigEndian, noAssert); } } Buffer.prototype.writeInt32LE = function (value, offset, noAssert){ writeInt32(this, value, offset, false , noAssert); } ; Buffer.prototype.writeInt32BE = function (value, offset, noAssert){ writeInt32(this, value, offset, true , noAssert); } ; function writeFloat(buffer, value, offset, isBigEndian, noAssert){ if (!noAssert) { assert.ok(value !== undefined && value !== null , 'missing value'); assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 3 < _AN_Read_length('length', buffer), 'Trying to write beyond buffer length'); verifIEEE754(value, 3.402823466385289e+38, -3.402823466385289e+38); } require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian, 23, 4); } Buffer.prototype.writeFloatLE = function (value, offset, noAssert){ writeFloat(this, value, offset, false , noAssert); } ; Buffer.prototype.writeFloatBE = function (value, offset, noAssert){ writeFloat(this, value, offset, true , noAssert); } ; function writeDouble(buffer, value, offset, isBigEndian, noAssert){ if (!noAssert) { assert.ok(value !== undefined && value !== null , 'missing value'); assert.ok(typeof (isBigEndian) === 'boolean', 'missing or invalid endian'); assert.ok(offset !== undefined && offset !== null , 'missing offset'); assert.ok(offset + 7 < _AN_Read_length('length', buffer), 'Trying to write beyond buffer length'); verifIEEE754(value, 1.797693134862316e+308, -1.797693134862316e+308); } require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian, 52, 8); } Buffer.prototype.writeDoubleLE = function (value, offset, noAssert){ writeDouble(this, value, offset, false , noAssert); } ; Buffer.prototype.writeDoubleBE = function (value, offset, noAssert){ writeDouble(this, value, offset, true , noAssert); } ;