var SlowBuffer = process.binding('buffer').SlowBuffer; function toHex(n){ if (n < 16) return '0' + n.toString(16); return n.toString(16); } SlowBuffer.prototype.inspect = function (){ var out = [] , len = _AN_Read_length('length', this); for (var i = 0; i < len; i++ ){ out[i] = toHex(this[i]); } return ''; } ; 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': return this.ucs2Slice(start, end); default : { throw new Error('Unknown encoding') } } } ; SlowBuffer.prototype.hexWrite = function (string, offset){ var len = _AN_Read_length('length', string); offset = + offset || 0; if (len % 2) { throw new Error('Invalid hex string') } for (var i = 0; i < len / 2; i++ ){ var byte = parseInt(string.substr(i * 2, 2), 16); if (isNaN(byte)) throw new Error('Invalid hex string') this[offset + i] = byte; } return i; } ; SlowBuffer.prototype.write = function (string, offset, encoding){ if (!isFinite(offset)) { var swap = encoding; encoding = offset; offset = swap; } offset = + offset || 0; encoding = String(encoding || 'utf8').toLowerCase(); switch (encoding){ case 'hex': return this.hexWrite(string, offset); case 'utf8': case 'utf-8': return this.utf8Write(string, offset); case 'ascii': return this.asciiWrite(string, offset); case 'binary': return this.binaryWrite(string, offset); case 'base64': return this.base64Write(string, offset); case 'ucs2': case 'ucs-2': return this.ucs2Write(start, end); 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 Buffer(subject, encoding, offset){ if (!(this instanceof Buffer)) { return new Buffer(subject, encoding, offset); } var type; if (typeof offset === 'number') { this.length = encoding; this.parent = subject; this.offset = offset; } else { switch (type = typeof subject){ case 'number': this.length = subject; break ; case 'string': this.length = Buffer.byteLength(subject, encoding); break ; case 'object': this.length = _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 (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 || b instanceof SlowBuffer; } ; Buffer.prototype.inspect = function inspect(){ var out = [] , len = _AN_Read_length('length', this); for (var i = 0; i < len; i++ ){ out[i] = toHex(this.parent[i + this.offset]); } return ''; } ; 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, encoding){ if (!isFinite(offset)) { var swap = encoding; encoding = offset; offset = swap; } offset = + offset || 0; encoding = String(encoding || 'utf8').toLowerCase(); var maxLength = _AN_Read_length('length', this) - offset; var ret; switch (encoding){ case 'hex': ret = this.parent.hexWrite(string, this.offset + offset, maxLength); break ; case 'utf8': case 'utf-8': ret = this.parent.utf8Write(string, this.offset + offset, maxLength); break ; case 'ascii': ret = this.parent.asciiWrite(string, this.offset + offset, maxLength); break ; case 'binary': ret = this.parent.binaryWrite(string, this.offset + offset, maxLength); break ; case 'base64': ret = this.parent.base64Write(string, this.offset + offset, maxLength); break ; case 'ucs2': case 'ucs-2': ret = this.parent.ucs2Write(string, this.offset + offset, maxLength); 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': return this.parent.ucs2Slice(start, end); default : { throw new Error('Unknown encoding') } } } ; Buffer.byteLength = SlowBuffer.byteLength; 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'); } ;