+var assert = require('assert');
exports.Buffer = Buffer;
+exports.INSPECT_MAX_BYTES = 50;
function Buffer(subject, encoding, offset) {
if (!(this instanceof Buffer)) {
}
}
-var assert = require('assert');
-
-exports.INSPECT_MAX_BYTES = 50;
-
-
-function toHex(n) {
- if (n < 16) return '0' + n.toString(16);
- return n.toString(16);
-}
-
-function utf8ToBytes(str) {
- var byteArray = [];
- for (var i = 0; i < str.length; i++)
- if (str.charCodeAt(i) <= 0x7F)
- byteArray.push(str.charCodeAt(i));
- else {
- var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
- for (var j = 0; j < h.length; j++)
- byteArray.push(parseInt(h[j], 16));
- }
-
- return byteArray;
-}
-
-function asciiToBytes(str) {
- var byteArray = []
- for (var i = 0; i < str.length; i++ )
- // Node's code seems to be doing this and not & 0x7F..
- byteArray.push( str.charCodeAt(i) & 0xFF );
-
- return byteArray;
-}
+Buffer.prototype.get = function get(i) {
+ if (i < 0 || i >= this.length) throw new Error('oob');
+ return this[this.offset + i];
+};
-function base64ToBytes(str) {
- return require("base64-js").toByteArray(str);
-}
+Buffer.prototype.set = function set(i, v) {
+ if (i < 0 || i >= this.length) throw new Error('oob');
+ return this[this.offset + i] = v;
+};
Buffer.byteLength = function (str, encoding) {
switch (encoding || "utf8") {
}
};
-function blitBuffer(src, dst, offset, length) {
- var pos, i = 0;
- while (i < length) {
- if ((i+offset >= dst.length) || (i >= src.length))
- break;
-
- dst[i + offset] = src[i];
- i++;
- }
- return i;
-}
-
Buffer.prototype.utf8Write = function (string, offset, length) {
var bytes, pos;
return Buffer._charsWritten = blitBuffer(utf8ToBytes(string), this, offset, length);
Buffer.prototype.base64Slice = function (start, end) {
var bytes = Array.prototype.slice.apply(this, arguments)
return require("base64-js").fromByteArray(bytes);
-}
-
-function decodeUtf8Char(str) {
- try {
- return decodeURIComponent(str);
- } catch (err) {
- return String.fromCharCode(0xFFFD); // UTF 8 invalid char
- }
-}
+};
Buffer.prototype.utf8Slice = function () {
var bytes = Array.prototype.slice.apply(this, arguments);
subject && typeof subject === 'object' &&
typeof subject.length === 'number';
}
+
+function toHex(n) {
+ if (n < 16) return '0' + n.toString(16);
+ return n.toString(16);
+}
+
+function utf8ToBytes(str) {
+ var byteArray = [];
+ for (var i = 0; i < str.length; i++)
+ if (str.charCodeAt(i) <= 0x7F)
+ byteArray.push(str.charCodeAt(i));
+ else {
+ var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
+ for (var j = 0; j < h.length; j++)
+ byteArray.push(parseInt(h[j], 16));
+ }
+
+ return byteArray;
+}
+
+function asciiToBytes(str) {
+ var byteArray = []
+ for (var i = 0; i < str.length; i++ )
+ // Node's code seems to be doing this and not & 0x7F..
+ byteArray.push( str.charCodeAt(i) & 0xFF );
+
+ return byteArray;
+}
+
+function base64ToBytes(str) {
+ return require("base64-js").toByteArray(str);
+}
+
+function blitBuffer(src, dst, offset, length) {
+ var pos, i = 0;
+ while (i < length) {
+ if ((i+offset >= dst.length) || (i >= src.length))
+ break;
+
+ dst[i + offset] = src[i];
+ i++;
+ }
+ return i;
+}
+
+function decodeUtf8Char(str) {
+ try {
+ return decodeURIComponent(str);
+ } catch (err) {
+ return String.fromCharCode(0xFFFD); // UTF 8 invalid char
+ }
+}
-var assert = require('assert');
-var SlowBuffer = require('./slow_buffer');
-
-exports.INSPECT_MAX_BYTES = 50;
-
-function coerce(length) {
- // Coerce length to a number (possibly NaN), round up
- // in case it's fractional (e.g. 123.456) then do a
- // double negate to coerce a NaN to 0. Easy, right?
- length = ~~Math.ceil(+length);
- return length < 0 ? 0 : length;
-}
-
-function toHex(n) {
- if (n < 16) return '0' + n.toString(16);
- return n.toString(16);
-}
-
-// Buffer
-
-function Buffer(subject, encoding, offset) {
- if (!(this instanceof Buffer)) {
- return new Buffer(subject, encoding, offset);
- }
-
- var type;
-
- // Are we slicing?
- if (typeof offset === 'number') {
- this.length = coerce(encoding);
- this.parent = subject;
- this.offset = offset;
- } else {
- // Find the length
- switch (type = typeof subject) {
- case 'number':
- this.length = coerce(subject);
- break;
-
- case 'string':
- this.length = Buffer.byteLength(subject, encoding);
- break;
-
- case 'object': // Assume object is an array
- this.length = coerce(subject.length);
- break;
-
- default:
- throw new Error('First argument needs to be a number, ' +
- 'array or string.');
- }
-
- if (this.length > Buffer.poolSize) {
- // Big buffer, just alloc one.
- this.parent = new SlowBuffer(this.length);
- this.offset = 0;
-
- } else {
- // Small buffer.
- if (!pool || pool.length - pool.used < this.length) allocPool();
- this.parent = pool;
- this.offset = pool.used;
- pool.used += this.length;
- }
-
- // Treat array-ish objects as a byte array.
- if (isArrayIsh(subject)) {
- for (var i = 0; i < this.length; i++) {
- if (subject instanceof Buffer) {
- this.parent[i + this.offset] = subject.readUInt8(i);
- this[i] = subject.readUInt8(i);
- }
- else {
- this.parent[i + this.offset] = subject[i];
- this[i] = subject[i];
- }
- }
- } else if (type == 'string') {
- // We are a string
- this.length = this.write(subject, 0, encoding);
- for (var i = 0; i < this.length; i++) {
- this[i] = this.parent[i];
- }
- }
- }
-
-}
-
-function isArrayIsh(subject) {
- return Array.isArray(subject) || Buffer.isBuffer(subject) ||
- subject && typeof subject === 'object' &&
- typeof subject.length === 'number';
-}
-
-exports.SlowBuffer = SlowBuffer;
-exports.Buffer = Buffer;
-
-Buffer.poolSize = 8 * 1024;
-var pool;
-
-function allocPool() {
- pool = new SlowBuffer(Buffer.poolSize);
- pool.used = 0;
-}
-
-
-// Static methods
-Buffer.isBuffer = function isBuffer(b) {
- return b instanceof Buffer || b instanceof SlowBuffer;
-};
-
-Buffer.concat = function (list, totalLength) {
- if (!Array.isArray(list)) {
- throw new Error("Usage: Buffer.concat(list, [totalLength])\n \
- list should be an Array.");
- }
-
- if (list.length === 0) {
- return new Buffer(0);
- } else if (list.length === 1) {
- return list[0];
- }
-
- if (typeof totalLength !== 'number') {
- totalLength = 0;
- for (var i = 0; i < list.length; i++) {
- var buf = list[i];
- totalLength += buf.length;
- }
- }
-
- var buffer = new Buffer(totalLength);
- var pos = 0;
- for (var i = 0; i < list.length; i++) {
- var buf = list[i];
- buf.copy(buffer, pos);
- pos += buf.length;
- }
- return buffer;
-};
-
-// Inspect
-Buffer.prototype.inspect = function inspect() {
- var out = [],
- len = this.length;
-
- for (var i = 0; i < len; i++) {
- out[i] = toHex(this.parent[i + this.offset]);
- if (i == exports.INSPECT_MAX_BYTES) {
- out[i + 1] = '...';
- break;
- }
- }
-
- return '<Buffer ' + out.join(' ') + '>';
-};
-
-
-Buffer.prototype.get = function get(i) {
- if (i < 0 || i >= this.length) throw new Error('oob');
- return this.parent[this.offset + i];
-};
-
-
-Buffer.prototype.set = function set(i, v) {
- if (i < 0 || i >= this.length) throw new Error('oob');
- return this.parent[this.offset + i] = v;
-};
-
-
-// write(string, offset = 0, length = buffer.length-offset, encoding = 'utf8')
-Buffer.prototype.write = function(string, offset, length, encoding) {
- // Support both (string, offset, length, encoding)
- // and the legacy (string, encoding, offset, length)
- if (isFinite(offset)) {
- if (!isFinite(length)) {
- encoding = length;
- length = undefined;
- }
- } else { // legacy
- var swap = encoding;
- encoding = offset;
- offset = length;
- length = swap;
- }
-
- offset = +offset || 0;
- var remaining = this.length - 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':
- // Warning: maxLength not taken into account in base64Write
- ret = this.parent.base64Write(string, this.offset + offset, length);
- break;
-
- case 'ucs2':
- case 'ucs-2':
- ret = this.parent.ucs2Write(string, this.offset + offset, length);
- break;
-
- default:
- throw new Error('Unknown encoding');
- }
-
- Buffer._charsWritten = SlowBuffer._charsWritten;
-
- return ret;
-};
-
// toString(encoding, start=0, end=buffer.length)
Buffer.prototype.toString = function(encoding, start, end) {