UNPKG

ndn-js

Version:

A JavaScript client library for Named Data Networking

1,162 lines (999 loc) 33.1 kB
/** * The buffer module from node.js, for the browser. * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org> * * Copyright (C) 2013 Feross Aboukhadijeh, and other contributors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * @license MIT */ var base64 = base64js; var ieee754 = require('./ieee754.js').ieee exports.Buffer = Buffer exports.SlowBuffer = Buffer exports.INSPECT_MAX_BYTES = 50 Buffer.poolSize = 8192 /** * If `Buffer._useTypedArrays`: * === true Use Uint8Array implementation (fastest) * === false Use Object implementation (compatible down to IE6) */ Buffer._useTypedArrays = (function () { // Detect if browser supports Typed Arrays. Supported browsers are IE 10+, Firefox 4+, // Chrome 7+, Safari 5.1+, Opera 11.6+, iOS 4.2+. If the browser does not support adding // properties to `Uint8Array` instances, then that's the same as no `Uint8Array` support // because we need to be able to add all the node Buffer API methods. This is an issue // in Firefox 4-29. Now fixed: https://bugzilla.mozilla.org/show_bug.cgi?id=695438 try { var buf = new ArrayBuffer(0) var arr = new Uint8Array(buf) arr.foo = function () { return 42 } return 42 === arr.foo() && typeof arr.subarray === 'function' // Chrome 9-10 lack `subarray` } catch (e) { return false } })() /** * Class: Buffer * ============= * * The Buffer constructor returns instances of `Uint8Array` that are augmented * with function properties for all the node `Buffer` API functions. We use * `Uint8Array` so that square bracket notation works as expected -- it returns * a single octet. * * By augmenting the instances, we can avoid modifying the `Uint8Array` * prototype. */ function Buffer (subject, encoding, noZero) { if (!(this instanceof Buffer)) return new Buffer(subject, encoding, noZero) var type = typeof subject // Workaround: node's base64 implementation allows for non-padded strings // while base64-js does not. if (encoding === 'base64' && type === 'string') { subject = Buffer.stringtrim(subject) while (subject.length % 4 !== 0) { subject = subject + '=' } } // Find the length var length if (type === 'number') length = Buffer.coerce(subject) else if (type === 'string') length = Buffer.byteLength(subject, encoding) else if (type === 'object') length = Buffer.coerce(subject.length) // assume that object is array-like else throw new Error('First argument needs to be a number, array or string.') var buf if (Buffer._useTypedArrays) { // Preferred: Return an augmented `Uint8Array` instance for best performance buf = Buffer._augment(new Uint8Array(length)) } else { // Fallback: Return THIS instance of Buffer (created by `new`) buf = this buf.length = length buf._isBuffer = true } var i if (Buffer._useTypedArrays && typeof subject.byteLength === 'number') { // Speed optimization -- use set if we're copying from a typed array buf._set(subject) } else if (Buffer.isArrayish(subject)) { // Treat array-ish objects as a byte array if (Buffer.isBuffer(subject)) { for (i = 0; i < length; i++) buf[i] = subject.readUInt8(i) } else { for (i = 0; i < length; i++) buf[i] = ((subject[i] % 256) + 256) % 256 } } else if (type === 'string') { buf.write(subject, 0, encoding) } else if (type === 'number' && !Buffer._useTypedArrays && !noZero) { for (i = 0; i < length; i++) { buf[i] = 0 } } return buf } // STATIC METHODS // ============== Buffer.isEncoding = function (encoding) { switch (String(encoding).toLowerCase()) { case 'hex': case 'utf8': case 'utf-8': case 'ascii': case 'binary': case 'base64': case 'raw': case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': return true default: return false } } Buffer.isBuffer = function (b) { return !!(b !== null && b !== undefined && b._isBuffer) } Buffer.byteLength = function (str, encoding) { var ret str = str.toString() switch (encoding || 'utf8') { case 'hex': ret = str.length / 2 break case 'utf8': case 'utf-8': ret = Buffer.utf8ToBytes(str).length break case 'ascii': case 'binary': case 'raw': ret = str.length break case 'base64': ret = Buffer.base64ToBytes(str).length break case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': ret = str.length * 2 break default: throw new Error('Unknown encoding') } return ret } Buffer.concat = function (list, totalLength) { Buffer.assert(Buffer.isArray(list), 'Usage: Buffer.concat(list[, length])') if (list.length === 0) { return new Buffer(0) } else if (list.length === 1) { return list[0] } var i if (totalLength === undefined) { totalLength = 0 for (i = 0; i < list.length; i++) { totalLength += list[i].length } } var buf = new Buffer(totalLength) var pos = 0 for (i = 0; i < list.length; i++) { var item = list[i] item.copy(buf, pos) pos += item.length } return buf } Buffer.compare = function (a, b) { Buffer.assert(Buffer.isBuffer(a) && Buffer.isBuffer(b), 'Arguments must be Buffers') var x = a.length var y = b.length for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {} if (i !== len) { x = a[i] y = b[i] } if (x < y) { return -1 } if (y < x) { return 1 } return 0 } // BUFFER INSTANCE METHODS // ======================= Buffer.hexWrite = function(buf, string, offset, length) { offset = Number(offset) || 0 var remaining = buf.length - offset if (!length) { length = remaining } else { length = Number(length) if (length > remaining) { length = remaining } } // must be an even number of digits var strLen = string.length Buffer.assert(strLen % 2 === 0, 'Invalid hex string') if (length > strLen / 2) { length = strLen / 2 } for (var i = 0; i < length; i++) { var b = parseInt(string.substr(i * 2, 2), 16) Buffer.assert(!isNaN(b), 'Invalid hex string') buf[offset + i] = b } return i } Buffer.utf8Write = function(buf, string, offset, length) { var charsWritten = Buffer.blitBuffer(Buffer.utf8ToBytes(string), buf, offset, length) return charsWritten } Buffer.asciiWrite = function(buf, string, offset, length) { var charsWritten = Buffer.blitBuffer(Buffer.asciiToBytes(string), buf, offset, length) return charsWritten } Buffer.binaryWrite = function(buf, string, offset, length) { return Buffer.asciiWrite(buf, string, offset, length) } Buffer.base64Write = function(buf, string, offset, length) { var charsWritten = Buffer.blitBuffer(Buffer.base64ToBytes(string), buf, offset, length) return charsWritten } Buffer.utf16leWrite = function(buf, string, offset, length) { var charsWritten = Buffer.blitBuffer(Buffer.utf16leToBytes(string), buf, offset, length) return charsWritten } 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 = Number(offset) || 0 var remaining = this.length - offset if (!length) { length = remaining } else { length = Number(length) if (length > remaining) { length = remaining } } encoding = String(encoding || 'utf8').toLowerCase() var ret switch (encoding) { case 'hex': ret = Buffer.hexWrite(this, string, offset, length) break case 'utf8': case 'utf-8': ret = Buffer.utf8Write(this, string, offset, length) break case 'ascii': ret = Buffer.asciiWrite(this, string, offset, length) break case 'binary': ret = Buffer.binaryWrite(this, string, offset, length) break case 'base64': ret = Buffer.base64Write(this, string, offset, length) break case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': ret = Buffer.utf16leWrite(this, string, offset, length) break default: throw new Error('Unknown encoding') } return ret } Buffer.prototype.toString = function (encoding, start, end) { var self = this encoding = String(encoding || 'utf8').toLowerCase() start = Number(start) || 0 end = (end === undefined) ? self.length : Number(end) // Fastpath empty strings if (end === start) return '' var ret switch (encoding) { case 'hex': ret = Buffer.hexSlice(self, start, end) break case 'utf8': case 'utf-8': ret = Buffer.utf8Slice(self, start, end) break case 'ascii': ret = Buffer.asciiSlice(self, start, end) break case 'binary': ret = Buffer.binarySlice(self, start, end) break case 'base64': ret = Buffer.base64Slice(self, start, end) break case 'ucs2': case 'ucs-2': case 'utf16le': case 'utf-16le': ret = utf16leSlice(self, start, end) break default: throw new Error('Unknown encoding') } return ret } Buffer.prototype.toJSON = function () { return { type: 'Buffer', data: Array.prototype.slice.call(this._arr || this, 0) } } Buffer.prototype.equals = function (b) { Buffer.assert(Buffer.isBuffer(b), 'Argument must be a Buffer') return Buffer.compare(this, b) === 0 } Buffer.prototype.compare = function (b) { Buffer.assert(Buffer.isBuffer(b), 'Argument must be a Buffer') return Buffer.compare(this, b) } // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) Buffer.prototype.copy = function (target, target_start, start, end) { var source = this if (!start) start = 0 if (!end && end !== 0) end = this.length if (!target_start) target_start = 0 // Copy 0 bytes; we're done if (end === start) return if (target.length === 0 || source.length === 0) return // Fatal error conditions Buffer.assert(end >= start, 'sourceEnd < sourceStart') Buffer.assert(target_start >= 0 && target_start < target.length, 'targetStart out of bounds') Buffer.assert(start >= 0 && start < source.length, 'sourceStart out of bounds') Buffer.assert(end >= 0 && end <= source.length, 'sourceEnd out of bounds') // Are we oob? if (end > this.length) end = this.length if (target.length - target_start < end - start) end = target.length - target_start + start var len = end - start if (len < 100 || !Buffer._useTypedArrays) { for (var i = 0; i < len; i++) { target[i + target_start] = this[i + start] } } else { target._set(this.subarray(start, start + len), target_start) } } Buffer.base64Slice = function(buf, start, end) { if (start === 0 && end === buf.length) { return base64.fromByteArray(buf) } else { return base64.fromByteArray(buf.slice(start, end)) } } Buffer.utf8Slice = function(buf, start, end) { var res = '' var tmp = '' end = Math.min(buf.length, end) for (var i = start; i < end; i++) { if (buf[i] <= 0x7F) { res += Buffer.decodeUtf8Char(tmp) + String.fromCharCode(buf[i]) tmp = '' } else { tmp += '%' + buf[i].toString(16) } } return res + Buffer.decodeUtf8Char(tmp) } Buffer.asciiSlice = function(buf, start, end) { var ret = '' end = Math.min(buf.length, end) for (var i = start; i < end; i++) { ret += String.fromCharCode(buf[i]) } return ret } Buffer.binarySlice = function(buf, start, end) { return Buffer.asciiSlice(buf, start, end) } Buffer.hexSlice = function(buf, start, end) { var len = buf.length if (!start || start < 0) start = 0 if (!end || end < 0 || end > len) end = len var out = '' for (var i = start; i < end; i++) { out += Buffer.toHex(buf[i]) } return out } function utf16leSlice (buf, start, end) { var bytes = buf.slice(start, end) var res = '' for (var i = 0; i < bytes.length; i += 2) { res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256) } return res } Buffer.prototype.slice = function (start, end) { var len = this.length start = Buffer.clamp(start, len, 0) end = Buffer.clamp(end, len, len) if (Buffer._useTypedArrays) { return Buffer._augment(this.subarray(start, end)) } else { var sliceLen = end - start var newBuf = new Buffer(sliceLen, undefined, true) for (var i = 0; i < sliceLen; i++) { newBuf[i] = this[i + start] } return newBuf } } // `get` will be removed in Node 0.13+ Buffer.prototype.get = function (offset) { console.log('.get() is deprecated. Access using array indexes instead.') return this.readUInt8(offset) } // `set` will be removed in Node 0.13+ Buffer.prototype.set = function (v, offset) { console.log('.set() is deprecated. Access using array indexes instead.') return this.writeUInt8(v, offset) } Buffer.prototype.readUInt8 = function (offset, noAssert) { if (!noAssert) { Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset < this.length, 'Trying to read beyond buffer length') } if (offset >= this.length) return return this[offset] } Buffer.readUInt16 = function(buf, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') } var len = buf.length if (offset >= len) return var val if (littleEndian) { val = buf[offset] if (offset + 1 < len) val |= buf[offset + 1] << 8 } else { val = buf[offset] << 8 if (offset + 1 < len) val |= buf[offset + 1] } return val } Buffer.prototype.readUInt16LE = function (offset, noAssert) { return Buffer.readUInt16(this, offset, true, noAssert) } Buffer.prototype.readUInt16BE = function (offset, noAssert) { return Buffer.readUInt16(this, offset, false, noAssert) } Buffer.readUInt32 = function(buf, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') } var len = buf.length if (offset >= len) return var val if (littleEndian) { if (offset + 2 < len) val = buf[offset + 2] << 16 if (offset + 1 < len) val |= buf[offset + 1] << 8 val |= buf[offset] if (offset + 3 < len) val = val + (buf[offset + 3] << 24 >>> 0) } else { if (offset + 1 < len) val = buf[offset + 1] << 16 if (offset + 2 < len) val |= buf[offset + 2] << 8 if (offset + 3 < len) val |= buf[offset + 3] val = val + (buf[offset] << 24 >>> 0) } return val } Buffer.prototype.readUInt32LE = function (offset, noAssert) { return Buffer.readUInt32(this, offset, true, noAssert) } Buffer.prototype.readUInt32BE = function (offset, noAssert) { return Buffer.readUInt32(this, offset, false, noAssert) } Buffer.prototype.readInt8 = function (offset, noAssert) { if (!noAssert) { Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset < this.length, 'Trying to read beyond buffer length') } if (offset >= this.length) return var neg = this[offset] & 0x80 if (neg) return (0xff - this[offset] + 1) * -1 else return this[offset] } Buffer.readInt16 = function(buf, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') } var len = buf.length if (offset >= len) return var val = Buffer.readUInt16(buf, offset, littleEndian, true) var neg = val & 0x8000 if (neg) return (0xffff - val + 1) * -1 else return val } Buffer.prototype.readInt16LE = function (offset, noAssert) { return Buffer.readInt16(this, offset, true, noAssert) } Buffer.prototype.readInt16BE = function (offset, noAssert) { return Buffer.readInt16(this, offset, false, noAssert) } Buffer.readInt32 = function(buf, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') } var len = buf.length if (offset >= len) return var val = Buffer.readUInt32(buf, offset, littleEndian, true) var neg = val & 0x80000000 if (neg) return (0xffffffff - val + 1) * -1 else return val } Buffer.prototype.readInt32LE = function (offset, noAssert) { return Buffer.readInt32(this, offset, true, noAssert) } Buffer.prototype.readInt32BE = function (offset, noAssert) { return Buffer.readInt32(this, offset, false, noAssert) } Buffer.readFloat = function(buf, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') } return ieee754.read(buf, offset, littleEndian, 23, 4) } Buffer.prototype.readFloatLE = function (offset, noAssert) { return Buffer.readFloat(this, offset, true, noAssert) } Buffer.prototype.readFloatBE = function (offset, noAssert) { return Buffer.readFloat(this, offset, false, noAssert) } Buffer.readDouble = function(buf, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset + 7 < buf.length, 'Trying to read beyond buffer length') } return ieee754.read(buf, offset, littleEndian, 52, 8) } Buffer.prototype.readDoubleLE = function (offset, noAssert) { return Buffer.readDouble(this, offset, true, noAssert) } Buffer.prototype.readDoubleBE = function (offset, noAssert) { return Buffer.readDouble(this, offset, false, noAssert) } Buffer.prototype.writeUInt8 = function (value, offset, noAssert) { if (!noAssert) { Buffer.assert(value !== undefined && value !== null, 'missing value') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset < this.length, 'trying to write beyond buffer length') Buffer.verifuint(value, 0xff) } if (offset >= this.length) return this[offset] = value return offset + 1 } Buffer.writeUInt16 = function(buf, value, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(value !== undefined && value !== null, 'missing value') Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 1 < buf.length, 'trying to write beyond buffer length') Buffer.verifuint(value, 0xffff) } var len = buf.length if (offset >= len) return for (var i = 0, j = Math.min(len - offset, 2); i < j; i++) { buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> (littleEndian ? i : 1 - i) * 8 } return offset + 2 } Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) { return Buffer.writeUInt16(this, value, offset, true, noAssert) } Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) { return Buffer.writeUInt16(this, value, offset, false, noAssert) } Buffer.writeUInt32 = function(buf, value, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(value !== undefined && value !== null, 'missing value') Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 3 < buf.length, 'trying to write beyond buffer length') Buffer.verifuint(value, 0xffffffff) } var len = buf.length if (offset >= len) return for (var i = 0, j = Math.min(len - offset, 4); i < j; i++) { buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff } return offset + 4 } Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) { return Buffer.writeUInt32(this, value, offset, true, noAssert) } Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) { return Buffer.writeUInt32(this, value, offset, false, noAssert) } Buffer.prototype.writeInt8 = function (value, offset, noAssert) { if (!noAssert) { Buffer.assert(value !== undefined && value !== null, 'missing value') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset < this.length, 'Trying to write beyond buffer length') Buffer.verifsint(value, 0x7f, -0x80) } if (offset >= this.length) return if (value >= 0) this.writeUInt8(value, offset, noAssert) else this.writeUInt8(0xff + value + 1, offset, noAssert) return offset + 1 } Buffer.writeInt16 = function(buf, value, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(value !== undefined && value !== null, 'missing value') Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 1 < buf.length, 'Trying to write beyond buffer length') Buffer.verifsint(value, 0x7fff, -0x8000) } var len = buf.length if (offset >= len) return if (value >= 0) Buffer.writeUInt16(buf, value, offset, littleEndian, noAssert) else Buffer.writeUInt16(buf, 0xffff + value + 1, offset, littleEndian, noAssert) return offset + 2 } Buffer.prototype.writeInt16LE = function (value, offset, noAssert) { return Buffer.writeInt16(this, value, offset, true, noAssert) } Buffer.prototype.writeInt16BE = function (value, offset, noAssert) { return Buffer.writeInt16(this, value, offset, false, noAssert) } Buffer.writeInt32 = function(buf, value, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(value !== undefined && value !== null, 'missing value') Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') Buffer.verifsint(value, 0x7fffffff, -0x80000000) } var len = buf.length if (offset >= len) return if (value >= 0) Buffer.writeUInt32(buf, value, offset, littleEndian, noAssert) else Buffer.writeUInt32(buf, 0xffffffff + value + 1, offset, littleEndian, noAssert) return offset + 4 } Buffer.prototype.writeInt32LE = function (value, offset, noAssert) { return Buffer.writeInt32(this, value, offset, true, noAssert) } Buffer.prototype.writeInt32BE = function (value, offset, noAssert) { return Buffer.writeInt32(this, value, offset, false, noAssert) } Buffer.writeFloat = function(buf, value, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(value !== undefined && value !== null, 'missing value') Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') Buffer.verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38) } var len = buf.length if (offset >= len) return ieee754.write(buf, value, offset, littleEndian, 23, 4) return offset + 4 } Buffer.prototype.writeFloatLE = function (value, offset, noAssert) { return Buffer.writeFloat(this, value, offset, true, noAssert) } Buffer.prototype.writeFloatBE = function (value, offset, noAssert) { return Buffer.writeFloat(this, value, offset, false, noAssert) } Buffer.writeDouble = function(buf, value, offset, littleEndian, noAssert) { if (!noAssert) { Buffer.assert(value !== undefined && value !== null, 'missing value') Buffer.assert(typeof littleEndian === 'boolean', 'missing or invalid endian') Buffer.assert(offset !== undefined && offset !== null, 'missing offset') Buffer.assert(offset + 7 < buf.length, 'Trying to write beyond buffer length') Buffer.verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308) } var len = buf.length if (offset >= len) return ieee754.write(buf, value, offset, littleEndian, 52, 8) return offset + 8 } Buffer.prototype.writeDoubleLE = function (value, offset, noAssert) { return Buffer.writeDouble(this, value, offset, true, noAssert) } Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) { return Buffer.writeDouble(this, value, offset, false, noAssert) } // fill(value, start=0, end=buffer.length) Buffer.prototype.fill = function (value, start, end) { if (!value) value = 0 if (!start) start = 0 if (!end) end = this.length Buffer.assert(end >= start, 'end < start') // Fill 0 bytes; we're done if (end === start) return if (this.length === 0) return Buffer.assert(start >= 0 && start < this.length, 'start out of bounds') Buffer.assert(end >= 0 && end <= this.length, 'end out of bounds') var i if (typeof value === 'number') { for (i = start; i < end; i++) { this[i] = value } } else { var bytes = Buffer.utf8ToBytes(value.toString()) var len = bytes.length for (i = start; i < end; i++) { this[i] = bytes[i % len] } } return this } Buffer.prototype.inspect = function () { var out = [] var len = this.length for (var i = 0; i < len; i++) { out[i] = Buffer.toHex(this[i]) if (i === exports.INSPECT_MAX_BYTES) { out[i + 1] = '...' break } } return '<Buffer ' + out.join(' ') + '>' } /** * Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance. * Added in Node 0.12. Only available in browsers that support ArrayBuffer. */ Buffer.prototype.toArrayBuffer = function () { if (typeof Uint8Array !== 'undefined') { if (Buffer._useTypedArrays) { return (new Buffer(this)).buffer } else { var buf = new Uint8Array(this.length) for (var i = 0, len = buf.length; i < len; i += 1) { buf[i] = this[i] } return buf.buffer } } else { throw new Error('Buffer.toArrayBuffer not supported in this browser') } } // HELPER FUNCTIONS // ================ var BP = Buffer.prototype /** * Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods */ Buffer._augment = function (arr) { arr._isBuffer = true // save reference to original Uint8Array get/set methods before overwriting arr._get = arr.get arr._set = arr.set // deprecated, will be removed in node 0.13+ arr.get = BP.get arr.set = BP.set arr.write = BP.write arr.toString = BP.toString arr.toLocaleString = BP.toString arr.toJSON = BP.toJSON arr.equals = BP.equals arr.compare = BP.compare arr.copy = BP.copy arr.slice = BP.slice arr.readUInt8 = BP.readUInt8 arr.readUInt16LE = BP.readUInt16LE arr.readUInt16BE = BP.readUInt16BE arr.readUInt32LE = BP.readUInt32LE arr.readUInt32BE = BP.readUInt32BE arr.readInt8 = BP.readInt8 arr.readInt16LE = BP.readInt16LE arr.readInt16BE = BP.readInt16BE arr.readInt32LE = BP.readInt32LE arr.readInt32BE = BP.readInt32BE arr.readFloatLE = BP.readFloatLE arr.readFloatBE = BP.readFloatBE arr.readDoubleLE = BP.readDoubleLE arr.readDoubleBE = BP.readDoubleBE arr.writeUInt8 = BP.writeUInt8 arr.writeUInt16LE = BP.writeUInt16LE arr.writeUInt16BE = BP.writeUInt16BE arr.writeUInt32LE = BP.writeUInt32LE arr.writeUInt32BE = BP.writeUInt32BE arr.writeInt8 = BP.writeInt8 arr.writeInt16LE = BP.writeInt16LE arr.writeInt16BE = BP.writeInt16BE arr.writeInt32LE = BP.writeInt32LE arr.writeInt32BE = BP.writeInt32BE arr.writeFloatLE = BP.writeFloatLE arr.writeFloatBE = BP.writeFloatBE arr.writeDoubleLE = BP.writeDoubleLE arr.writeDoubleBE = BP.writeDoubleBE arr.fill = BP.fill arr.inspect = BP.inspect arr.toArrayBuffer = BP.toArrayBuffer return arr } Buffer.stringtrim = function(str) { if (str.trim) return str.trim() return str.replace(/^\s+|\s+$/g, '') } // slice(start, end) Buffer.clamp = function(index, len, defaultValue) { if (typeof index !== 'number') return defaultValue index = ~~index; // Coerce to integer. if (index >= len) return len if (index >= 0) return index index += len if (index >= 0) return index return 0 } Buffer.coerce = function(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 } Buffer.isArray = function(subject) { return (Array.isArray || function (subject) { return Object.prototype.toString.call(subject) === '[object Array]' })(subject) } Buffer.isArrayish = function(subject) { return Buffer.isArray(subject) || Buffer.isBuffer(subject) || subject && typeof subject === 'object' && typeof subject.length === 'number' } Buffer.toHex = function(n) { if (n < 16) return '0' + n.toString(16) return n.toString(16) } Buffer.utf8ToBytes = function(str) { var byteArray = [] for (var i = 0; i < str.length; i++) { var b = str.charCodeAt(i) if (b <= 0x7F) { byteArray.push(b) } else { var start = i if (b >= 0xD800 && b <= 0xDFFF) i++ var h = encodeURIComponent(str.slice(start, i+1)).substr(1).split('%') for (var j = 0; j < h.length; j++) { byteArray.push(parseInt(h[j], 16)) } } } return byteArray } Buffer.asciiToBytes = function(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.utf16leToBytes = function(str) { var c, hi, lo var byteArray = [] for (var i = 0; i < str.length; i++) { c = str.charCodeAt(i) hi = c >> 8 lo = c % 256 byteArray.push(lo) byteArray.push(hi) } return byteArray } Buffer.base64ToBytes = function(str) { return base64.toByteArray(str) } Buffer.blitBuffer = function(src, dst, offset, length) { for (var i = 0; i < length; i++) { if ((i + offset >= dst.length) || (i >= src.length)) break dst[i + offset] = src[i] } return i } Buffer.decodeUtf8Char = function(str) { try { return decodeURIComponent(str) } catch (err) { return String.fromCharCode(0xFFFD) // UTF 8 invalid char } } /* * We have to make sure that the value is a valid integer. This means that it * is non-negative. It has no fractional component and that it does not * exceed the maximum allowed value. */ Buffer.verifuint = function(value, max) { Buffer.assert(typeof value === 'number', 'cannot write a non-number as a number') Buffer.assert(value >= 0, 'specified a negative value for writing an unsigned value') Buffer.assert(value <= max, 'value is larger than maximum value for type') Buffer.assert(Math.floor(value) === value, 'value has a fractional component') } Buffer.verifsint = function(value, max, min) { Buffer.assert(typeof value === 'number', 'cannot write a non-number as a number') Buffer.assert(value <= max, 'value larger than maximum allowed value') Buffer.assert(value >= min, 'value smaller than minimum allowed value') Buffer.assert(Math.floor(value) === value, 'value has a fractional component') } Buffer.verifIEEE754 = function(value, max, min) { Buffer.assert(typeof value === 'number', 'cannot write a non-number as a number') Buffer.assert(value <= max, 'value larger than maximum allowed value') Buffer.assert(value >= min, 'value smaller than minimum allowed value') } Buffer.assert = function(test, message) { if (!test) throw new Error(message || 'Failed assertion') }