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.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 '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); default : { throw new Error('Unknown encoding') } } } ; 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 '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); default : { throw new Error('Unknown encoding') } } } ; SlowBuffer.prototype.slice = function (start, end){ 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 need to be an 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 (Array.isArray(subject)) { for (var i = 0; i < _AN_Read_length("length", this); i++ ){ this.parent[i + this.offset] = subject[i]; } } else if (type == 'string') { _AN_Call_write('write', this, subject, 0, encoding); } } SlowBuffer.makeFastBuffer(this.parent, this, this.offset, _AN_Read_length('length', this)); } 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); 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 write(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 '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 ; 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 '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); default : { throw new Error('Unknown encoding') } } } ; Buffer.byteLength = SlowBuffer.byteLength; Buffer.prototype.copy = function copy(target, target_start, start, end){ var source = this; start || (start = 0); end || (end = _AN_Read_length('length', this)); 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) 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); } ;