react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / buffer / index.js
80713 views/*!1* The buffer module from node.js, for the browser.2*3* @author Feross Aboukhadijeh <[email protected]> <http://feross.org>4* @license MIT5*/67var base64 = require('base64-js')8var ieee754 = require('ieee754')9var isArray = require('is-array')1011exports.Buffer = Buffer12exports.SlowBuffer = Buffer13exports.INSPECT_MAX_BYTES = 5014Buffer.poolSize = 8192 // not used by this implementation1516var kMaxLength = 0x3fffffff1718/**19* If `Buffer.TYPED_ARRAY_SUPPORT`:20* === true Use Uint8Array implementation (fastest)21* === false Use Object implementation (most compatible, even IE6)22*23* Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,24* Opera 11.6+, iOS 4.2+.25*26* Note:27*28* - Implementation must support adding new properties to `Uint8Array` instances.29* Firefox 4-29 lacked support, fixed in Firefox 30+.30* See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.31*32* - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.33*34* - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of35* incorrect length in some situations.36*37* We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will38* get the Object implementation, which is slower but will work correctly.39*/40Buffer.TYPED_ARRAY_SUPPORT = (function () {41try {42var buf = new ArrayBuffer(0)43var arr = new Uint8Array(buf)44arr.foo = function () { return 42 }45return 42 === arr.foo() && // typed array instances can be augmented46typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`47new Uint8Array(1).subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`48} catch (e) {49return false50}51})()5253/**54* Class: Buffer55* =============56*57* The Buffer constructor returns instances of `Uint8Array` that are augmented58* with function properties for all the node `Buffer` API functions. We use59* `Uint8Array` so that square bracket notation works as expected -- it returns60* a single octet.61*62* By augmenting the instances, we can avoid modifying the `Uint8Array`63* prototype.64*/65function Buffer (subject, encoding, noZero) {66if (!(this instanceof Buffer))67return new Buffer(subject, encoding, noZero)6869var type = typeof subject7071// Find the length72var length73if (type === 'number')74length = subject > 0 ? subject >>> 0 : 075else if (type === 'string') {76if (encoding === 'base64')77subject = base64clean(subject)78length = Buffer.byteLength(subject, encoding)79} else if (type === 'object' && subject !== null) { // assume object is array-like80if (subject.type === 'Buffer' && isArray(subject.data))81subject = subject.data82length = +subject.length > 0 ? Math.floor(+subject.length) : 083} else84throw new TypeError('must start with number, buffer, array or string')8586if (this.length > kMaxLength)87throw new RangeError('Attempt to allocate Buffer larger than maximum ' +88'size: 0x' + kMaxLength.toString(16) + ' bytes')8990var buf91if (Buffer.TYPED_ARRAY_SUPPORT) {92// Preferred: Return an augmented `Uint8Array` instance for best performance93buf = Buffer._augment(new Uint8Array(length))94} else {95// Fallback: Return THIS instance of Buffer (created by `new`)96buf = this97buf.length = length98buf._isBuffer = true99}100101var i102if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') {103// Speed optimization -- use set if we're copying from a typed array104buf._set(subject)105} else if (isArrayish(subject)) {106// Treat array-ish objects as a byte array107if (Buffer.isBuffer(subject)) {108for (i = 0; i < length; i++)109buf[i] = subject.readUInt8(i)110} else {111for (i = 0; i < length; i++)112buf[i] = ((subject[i] % 256) + 256) % 256113}114} else if (type === 'string') {115buf.write(subject, 0, encoding)116} else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) {117for (i = 0; i < length; i++) {118buf[i] = 0119}120}121122return buf123}124125Buffer.isBuffer = function (b) {126return !!(b != null && b._isBuffer)127}128129Buffer.compare = function (a, b) {130if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b))131throw new TypeError('Arguments must be Buffers')132133var x = a.length134var y = b.length135for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {}136if (i !== len) {137x = a[i]138y = b[i]139}140if (x < y) return -1141if (y < x) return 1142return 0143}144145Buffer.isEncoding = function (encoding) {146switch (String(encoding).toLowerCase()) {147case 'hex':148case 'utf8':149case 'utf-8':150case 'ascii':151case 'binary':152case 'base64':153case 'raw':154case 'ucs2':155case 'ucs-2':156case 'utf16le':157case 'utf-16le':158return true159default:160return false161}162}163164Buffer.concat = function (list, totalLength) {165if (!isArray(list)) throw new TypeError('Usage: Buffer.concat(list[, length])')166167if (list.length === 0) {168return new Buffer(0)169} else if (list.length === 1) {170return list[0]171}172173var i174if (totalLength === undefined) {175totalLength = 0176for (i = 0; i < list.length; i++) {177totalLength += list[i].length178}179}180181var buf = new Buffer(totalLength)182var pos = 0183for (i = 0; i < list.length; i++) {184var item = list[i]185item.copy(buf, pos)186pos += item.length187}188return buf189}190191Buffer.byteLength = function (str, encoding) {192var ret193str = str + ''194switch (encoding || 'utf8') {195case 'ascii':196case 'binary':197case 'raw':198ret = str.length199break200case 'ucs2':201case 'ucs-2':202case 'utf16le':203case 'utf-16le':204ret = str.length * 2205break206case 'hex':207ret = str.length >>> 1208break209case 'utf8':210case 'utf-8':211ret = utf8ToBytes(str).length212break213case 'base64':214ret = base64ToBytes(str).length215break216default:217ret = str.length218}219return ret220}221222// pre-set for values that may exist in the future223Buffer.prototype.length = undefined224Buffer.prototype.parent = undefined225226// toString(encoding, start=0, end=buffer.length)227Buffer.prototype.toString = function (encoding, start, end) {228var loweredCase = false229230start = start >>> 0231end = end === undefined || end === Infinity ? this.length : end >>> 0232233if (!encoding) encoding = 'utf8'234if (start < 0) start = 0235if (end > this.length) end = this.length236if (end <= start) return ''237238while (true) {239switch (encoding) {240case 'hex':241return hexSlice(this, start, end)242243case 'utf8':244case 'utf-8':245return utf8Slice(this, start, end)246247case 'ascii':248return asciiSlice(this, start, end)249250case 'binary':251return binarySlice(this, start, end)252253case 'base64':254return base64Slice(this, start, end)255256case 'ucs2':257case 'ucs-2':258case 'utf16le':259case 'utf-16le':260return utf16leSlice(this, start, end)261262default:263if (loweredCase)264throw new TypeError('Unknown encoding: ' + encoding)265encoding = (encoding + '').toLowerCase()266loweredCase = true267}268}269}270271Buffer.prototype.equals = function (b) {272if(!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')273return Buffer.compare(this, b) === 0274}275276Buffer.prototype.inspect = function () {277var str = ''278var max = exports.INSPECT_MAX_BYTES279if (this.length > 0) {280str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')281if (this.length > max)282str += ' ... '283}284return '<Buffer ' + str + '>'285}286287Buffer.prototype.compare = function (b) {288if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')289return Buffer.compare(this, b)290}291292// `get` will be removed in Node 0.13+293Buffer.prototype.get = function (offset) {294console.log('.get() is deprecated. Access using array indexes instead.')295return this.readUInt8(offset)296}297298// `set` will be removed in Node 0.13+299Buffer.prototype.set = function (v, offset) {300console.log('.set() is deprecated. Access using array indexes instead.')301return this.writeUInt8(v, offset)302}303304function hexWrite (buf, string, offset, length) {305offset = Number(offset) || 0306var remaining = buf.length - offset307if (!length) {308length = remaining309} else {310length = Number(length)311if (length > remaining) {312length = remaining313}314}315316// must be an even number of digits317var strLen = string.length318if (strLen % 2 !== 0) throw new Error('Invalid hex string')319320if (length > strLen / 2) {321length = strLen / 2322}323for (var i = 0; i < length; i++) {324var byte = parseInt(string.substr(i * 2, 2), 16)325if (isNaN(byte)) throw new Error('Invalid hex string')326buf[offset + i] = byte327}328return i329}330331function utf8Write (buf, string, offset, length) {332var charsWritten = blitBuffer(utf8ToBytes(string), buf, offset, length)333return charsWritten334}335336function asciiWrite (buf, string, offset, length) {337var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length)338return charsWritten339}340341function binaryWrite (buf, string, offset, length) {342return asciiWrite(buf, string, offset, length)343}344345function base64Write (buf, string, offset, length) {346var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length)347return charsWritten348}349350function utf16leWrite (buf, string, offset, length) {351var charsWritten = blitBuffer(utf16leToBytes(string), buf, offset, length, 2)352return charsWritten353}354355Buffer.prototype.write = function (string, offset, length, encoding) {356// Support both (string, offset, length, encoding)357// and the legacy (string, encoding, offset, length)358if (isFinite(offset)) {359if (!isFinite(length)) {360encoding = length361length = undefined362}363} else { // legacy364var swap = encoding365encoding = offset366offset = length367length = swap368}369370offset = Number(offset) || 0371var remaining = this.length - offset372if (!length) {373length = remaining374} else {375length = Number(length)376if (length > remaining) {377length = remaining378}379}380encoding = String(encoding || 'utf8').toLowerCase()381382var ret383switch (encoding) {384case 'hex':385ret = hexWrite(this, string, offset, length)386break387case 'utf8':388case 'utf-8':389ret = utf8Write(this, string, offset, length)390break391case 'ascii':392ret = asciiWrite(this, string, offset, length)393break394case 'binary':395ret = binaryWrite(this, string, offset, length)396break397case 'base64':398ret = base64Write(this, string, offset, length)399break400case 'ucs2':401case 'ucs-2':402case 'utf16le':403case 'utf-16le':404ret = utf16leWrite(this, string, offset, length)405break406default:407throw new TypeError('Unknown encoding: ' + encoding)408}409return ret410}411412Buffer.prototype.toJSON = function () {413return {414type: 'Buffer',415data: Array.prototype.slice.call(this._arr || this, 0)416}417}418419function base64Slice (buf, start, end) {420if (start === 0 && end === buf.length) {421return base64.fromByteArray(buf)422} else {423return base64.fromByteArray(buf.slice(start, end))424}425}426427function utf8Slice (buf, start, end) {428var res = ''429var tmp = ''430end = Math.min(buf.length, end)431432for (var i = start; i < end; i++) {433if (buf[i] <= 0x7F) {434res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i])435tmp = ''436} else {437tmp += '%' + buf[i].toString(16)438}439}440441return res + decodeUtf8Char(tmp)442}443444function asciiSlice (buf, start, end) {445var ret = ''446end = Math.min(buf.length, end)447448for (var i = start; i < end; i++) {449ret += String.fromCharCode(buf[i])450}451return ret452}453454function binarySlice (buf, start, end) {455return asciiSlice(buf, start, end)456}457458function hexSlice (buf, start, end) {459var len = buf.length460461if (!start || start < 0) start = 0462if (!end || end < 0 || end > len) end = len463464var out = ''465for (var i = start; i < end; i++) {466out += toHex(buf[i])467}468return out469}470471function utf16leSlice (buf, start, end) {472var bytes = buf.slice(start, end)473var res = ''474for (var i = 0; i < bytes.length; i += 2) {475res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)476}477return res478}479480Buffer.prototype.slice = function (start, end) {481var len = this.length482start = ~~start483end = end === undefined ? len : ~~end484485if (start < 0) {486start += len;487if (start < 0)488start = 0489} else if (start > len) {490start = len491}492493if (end < 0) {494end += len495if (end < 0)496end = 0497} else if (end > len) {498end = len499}500501if (end < start)502end = start503504if (Buffer.TYPED_ARRAY_SUPPORT) {505return Buffer._augment(this.subarray(start, end))506} else {507var sliceLen = end - start508var newBuf = new Buffer(sliceLen, undefined, true)509for (var i = 0; i < sliceLen; i++) {510newBuf[i] = this[i + start]511}512return newBuf513}514}515516/*517* Need to make sure that buffer isn't trying to write out of bounds.518*/519function checkOffset (offset, ext, length) {520if ((offset % 1) !== 0 || offset < 0)521throw new RangeError('offset is not uint')522if (offset + ext > length)523throw new RangeError('Trying to access beyond buffer length')524}525526Buffer.prototype.readUInt8 = function (offset, noAssert) {527if (!noAssert)528checkOffset(offset, 1, this.length)529return this[offset]530}531532Buffer.prototype.readUInt16LE = function (offset, noAssert) {533if (!noAssert)534checkOffset(offset, 2, this.length)535return this[offset] | (this[offset + 1] << 8)536}537538Buffer.prototype.readUInt16BE = function (offset, noAssert) {539if (!noAssert)540checkOffset(offset, 2, this.length)541return (this[offset] << 8) | this[offset + 1]542}543544Buffer.prototype.readUInt32LE = function (offset, noAssert) {545if (!noAssert)546checkOffset(offset, 4, this.length)547548return ((this[offset]) |549(this[offset + 1] << 8) |550(this[offset + 2] << 16)) +551(this[offset + 3] * 0x1000000)552}553554Buffer.prototype.readUInt32BE = function (offset, noAssert) {555if (!noAssert)556checkOffset(offset, 4, this.length)557558return (this[offset] * 0x1000000) +559((this[offset + 1] << 16) |560(this[offset + 2] << 8) |561this[offset + 3])562}563564Buffer.prototype.readInt8 = function (offset, noAssert) {565if (!noAssert)566checkOffset(offset, 1, this.length)567if (!(this[offset] & 0x80))568return (this[offset])569return ((0xff - this[offset] + 1) * -1)570}571572Buffer.prototype.readInt16LE = function (offset, noAssert) {573if (!noAssert)574checkOffset(offset, 2, this.length)575var val = this[offset] | (this[offset + 1] << 8)576return (val & 0x8000) ? val | 0xFFFF0000 : val577}578579Buffer.prototype.readInt16BE = function (offset, noAssert) {580if (!noAssert)581checkOffset(offset, 2, this.length)582var val = this[offset + 1] | (this[offset] << 8)583return (val & 0x8000) ? val | 0xFFFF0000 : val584}585586Buffer.prototype.readInt32LE = function (offset, noAssert) {587if (!noAssert)588checkOffset(offset, 4, this.length)589590return (this[offset]) |591(this[offset + 1] << 8) |592(this[offset + 2] << 16) |593(this[offset + 3] << 24)594}595596Buffer.prototype.readInt32BE = function (offset, noAssert) {597if (!noAssert)598checkOffset(offset, 4, this.length)599600return (this[offset] << 24) |601(this[offset + 1] << 16) |602(this[offset + 2] << 8) |603(this[offset + 3])604}605606Buffer.prototype.readFloatLE = function (offset, noAssert) {607if (!noAssert)608checkOffset(offset, 4, this.length)609return ieee754.read(this, offset, true, 23, 4)610}611612Buffer.prototype.readFloatBE = function (offset, noAssert) {613if (!noAssert)614checkOffset(offset, 4, this.length)615return ieee754.read(this, offset, false, 23, 4)616}617618Buffer.prototype.readDoubleLE = function (offset, noAssert) {619if (!noAssert)620checkOffset(offset, 8, this.length)621return ieee754.read(this, offset, true, 52, 8)622}623624Buffer.prototype.readDoubleBE = function (offset, noAssert) {625if (!noAssert)626checkOffset(offset, 8, this.length)627return ieee754.read(this, offset, false, 52, 8)628}629630function checkInt (buf, value, offset, ext, max, min) {631if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance')632if (value > max || value < min) throw new TypeError('value is out of bounds')633if (offset + ext > buf.length) throw new TypeError('index out of range')634}635636Buffer.prototype.writeUInt8 = function (value, offset, noAssert) {637value = +value638offset = offset >>> 0639if (!noAssert)640checkInt(this, value, offset, 1, 0xff, 0)641if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)642this[offset] = value643return offset + 1644}645646function objectWriteUInt16 (buf, value, offset, littleEndian) {647if (value < 0) value = 0xffff + value + 1648for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) {649buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>650(littleEndian ? i : 1 - i) * 8651}652}653654Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) {655value = +value656offset = offset >>> 0657if (!noAssert)658checkInt(this, value, offset, 2, 0xffff, 0)659if (Buffer.TYPED_ARRAY_SUPPORT) {660this[offset] = value661this[offset + 1] = (value >>> 8)662} else objectWriteUInt16(this, value, offset, true)663return offset + 2664}665666Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) {667value = +value668offset = offset >>> 0669if (!noAssert)670checkInt(this, value, offset, 2, 0xffff, 0)671if (Buffer.TYPED_ARRAY_SUPPORT) {672this[offset] = (value >>> 8)673this[offset + 1] = value674} else objectWriteUInt16(this, value, offset, false)675return offset + 2676}677678function objectWriteUInt32 (buf, value, offset, littleEndian) {679if (value < 0) value = 0xffffffff + value + 1680for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) {681buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff682}683}684685Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) {686value = +value687offset = offset >>> 0688if (!noAssert)689checkInt(this, value, offset, 4, 0xffffffff, 0)690if (Buffer.TYPED_ARRAY_SUPPORT) {691this[offset + 3] = (value >>> 24)692this[offset + 2] = (value >>> 16)693this[offset + 1] = (value >>> 8)694this[offset] = value695} else objectWriteUInt32(this, value, offset, true)696return offset + 4697}698699Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) {700value = +value701offset = offset >>> 0702if (!noAssert)703checkInt(this, value, offset, 4, 0xffffffff, 0)704if (Buffer.TYPED_ARRAY_SUPPORT) {705this[offset] = (value >>> 24)706this[offset + 1] = (value >>> 16)707this[offset + 2] = (value >>> 8)708this[offset + 3] = value709} else objectWriteUInt32(this, value, offset, false)710return offset + 4711}712713Buffer.prototype.writeInt8 = function (value, offset, noAssert) {714value = +value715offset = offset >>> 0716if (!noAssert)717checkInt(this, value, offset, 1, 0x7f, -0x80)718if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)719if (value < 0) value = 0xff + value + 1720this[offset] = value721return offset + 1722}723724Buffer.prototype.writeInt16LE = function (value, offset, noAssert) {725value = +value726offset = offset >>> 0727if (!noAssert)728checkInt(this, value, offset, 2, 0x7fff, -0x8000)729if (Buffer.TYPED_ARRAY_SUPPORT) {730this[offset] = value731this[offset + 1] = (value >>> 8)732} else objectWriteUInt16(this, value, offset, true)733return offset + 2734}735736Buffer.prototype.writeInt16BE = function (value, offset, noAssert) {737value = +value738offset = offset >>> 0739if (!noAssert)740checkInt(this, value, offset, 2, 0x7fff, -0x8000)741if (Buffer.TYPED_ARRAY_SUPPORT) {742this[offset] = (value >>> 8)743this[offset + 1] = value744} else objectWriteUInt16(this, value, offset, false)745return offset + 2746}747748Buffer.prototype.writeInt32LE = function (value, offset, noAssert) {749value = +value750offset = offset >>> 0751if (!noAssert)752checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)753if (Buffer.TYPED_ARRAY_SUPPORT) {754this[offset] = value755this[offset + 1] = (value >>> 8)756this[offset + 2] = (value >>> 16)757this[offset + 3] = (value >>> 24)758} else objectWriteUInt32(this, value, offset, true)759return offset + 4760}761762Buffer.prototype.writeInt32BE = function (value, offset, noAssert) {763value = +value764offset = offset >>> 0765if (!noAssert)766checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)767if (value < 0) value = 0xffffffff + value + 1768if (Buffer.TYPED_ARRAY_SUPPORT) {769this[offset] = (value >>> 24)770this[offset + 1] = (value >>> 16)771this[offset + 2] = (value >>> 8)772this[offset + 3] = value773} else objectWriteUInt32(this, value, offset, false)774return offset + 4775}776777function checkIEEE754 (buf, value, offset, ext, max, min) {778if (value > max || value < min) throw new TypeError('value is out of bounds')779if (offset + ext > buf.length) throw new TypeError('index out of range')780}781782function writeFloat (buf, value, offset, littleEndian, noAssert) {783if (!noAssert)784checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)785ieee754.write(buf, value, offset, littleEndian, 23, 4)786return offset + 4787}788789Buffer.prototype.writeFloatLE = function (value, offset, noAssert) {790return writeFloat(this, value, offset, true, noAssert)791}792793Buffer.prototype.writeFloatBE = function (value, offset, noAssert) {794return writeFloat(this, value, offset, false, noAssert)795}796797function writeDouble (buf, value, offset, littleEndian, noAssert) {798if (!noAssert)799checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)800ieee754.write(buf, value, offset, littleEndian, 52, 8)801return offset + 8802}803804Buffer.prototype.writeDoubleLE = function (value, offset, noAssert) {805return writeDouble(this, value, offset, true, noAssert)806}807808Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) {809return writeDouble(this, value, offset, false, noAssert)810}811812// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)813Buffer.prototype.copy = function (target, target_start, start, end) {814var source = this815816if (!start) start = 0817if (!end && end !== 0) end = this.length818if (!target_start) target_start = 0819820// Copy 0 bytes; we're done821if (end === start) return822if (target.length === 0 || source.length === 0) return823824// Fatal error conditions825if (end < start) throw new TypeError('sourceEnd < sourceStart')826if (target_start < 0 || target_start >= target.length)827throw new TypeError('targetStart out of bounds')828if (start < 0 || start >= source.length) throw new TypeError('sourceStart out of bounds')829if (end < 0 || end > source.length) throw new TypeError('sourceEnd out of bounds')830831// Are we oob?832if (end > this.length)833end = this.length834if (target.length - target_start < end - start)835end = target.length - target_start + start836837var len = end - start838839if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {840for (var i = 0; i < len; i++) {841target[i + target_start] = this[i + start]842}843} else {844target._set(this.subarray(start, start + len), target_start)845}846}847848// fill(value, start=0, end=buffer.length)849Buffer.prototype.fill = function (value, start, end) {850if (!value) value = 0851if (!start) start = 0852if (!end) end = this.length853854if (end < start) throw new TypeError('end < start')855856// Fill 0 bytes; we're done857if (end === start) return858if (this.length === 0) return859860if (start < 0 || start >= this.length) throw new TypeError('start out of bounds')861if (end < 0 || end > this.length) throw new TypeError('end out of bounds')862863var i864if (typeof value === 'number') {865for (i = start; i < end; i++) {866this[i] = value867}868} else {869var bytes = utf8ToBytes(value.toString())870var len = bytes.length871for (i = start; i < end; i++) {872this[i] = bytes[i % len]873}874}875876return this877}878879/**880* Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance.881* Added in Node 0.12. Only available in browsers that support ArrayBuffer.882*/883Buffer.prototype.toArrayBuffer = function () {884if (typeof Uint8Array !== 'undefined') {885if (Buffer.TYPED_ARRAY_SUPPORT) {886return (new Buffer(this)).buffer887} else {888var buf = new Uint8Array(this.length)889for (var i = 0, len = buf.length; i < len; i += 1) {890buf[i] = this[i]891}892return buf.buffer893}894} else {895throw new TypeError('Buffer.toArrayBuffer not supported in this browser')896}897}898899// HELPER FUNCTIONS900// ================901902var BP = Buffer.prototype903904/**905* Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods906*/907Buffer._augment = function (arr) {908arr.constructor = Buffer909arr._isBuffer = true910911// save reference to original Uint8Array get/set methods before overwriting912arr._get = arr.get913arr._set = arr.set914915// deprecated, will be removed in node 0.13+916arr.get = BP.get917arr.set = BP.set918919arr.write = BP.write920arr.toString = BP.toString921arr.toLocaleString = BP.toString922arr.toJSON = BP.toJSON923arr.equals = BP.equals924arr.compare = BP.compare925arr.copy = BP.copy926arr.slice = BP.slice927arr.readUInt8 = BP.readUInt8928arr.readUInt16LE = BP.readUInt16LE929arr.readUInt16BE = BP.readUInt16BE930arr.readUInt32LE = BP.readUInt32LE931arr.readUInt32BE = BP.readUInt32BE932arr.readInt8 = BP.readInt8933arr.readInt16LE = BP.readInt16LE934arr.readInt16BE = BP.readInt16BE935arr.readInt32LE = BP.readInt32LE936arr.readInt32BE = BP.readInt32BE937arr.readFloatLE = BP.readFloatLE938arr.readFloatBE = BP.readFloatBE939arr.readDoubleLE = BP.readDoubleLE940arr.readDoubleBE = BP.readDoubleBE941arr.writeUInt8 = BP.writeUInt8942arr.writeUInt16LE = BP.writeUInt16LE943arr.writeUInt16BE = BP.writeUInt16BE944arr.writeUInt32LE = BP.writeUInt32LE945arr.writeUInt32BE = BP.writeUInt32BE946arr.writeInt8 = BP.writeInt8947arr.writeInt16LE = BP.writeInt16LE948arr.writeInt16BE = BP.writeInt16BE949arr.writeInt32LE = BP.writeInt32LE950arr.writeInt32BE = BP.writeInt32BE951arr.writeFloatLE = BP.writeFloatLE952arr.writeFloatBE = BP.writeFloatBE953arr.writeDoubleLE = BP.writeDoubleLE954arr.writeDoubleBE = BP.writeDoubleBE955arr.fill = BP.fill956arr.inspect = BP.inspect957arr.toArrayBuffer = BP.toArrayBuffer958959return arr960}961962var INVALID_BASE64_RE = /[^+\/0-9A-z]/g963964function base64clean (str) {965// Node strips out invalid characters like \n and \t from the string, base64-js does not966str = stringtrim(str).replace(INVALID_BASE64_RE, '')967// Node allows for non-padded base64 strings (missing trailing ===), base64-js does not968while (str.length % 4 !== 0) {969str = str + '='970}971return str972}973974function stringtrim (str) {975if (str.trim) return str.trim()976return str.replace(/^\s+|\s+$/g, '')977}978979function isArrayish (subject) {980return isArray(subject) || Buffer.isBuffer(subject) ||981subject && typeof subject === 'object' &&982typeof subject.length === 'number'983}984985function toHex (n) {986if (n < 16) return '0' + n.toString(16)987return n.toString(16)988}989990function utf8ToBytes (str) {991var byteArray = []992for (var i = 0; i < str.length; i++) {993var b = str.charCodeAt(i)994if (b <= 0x7F) {995byteArray.push(b)996} else {997var start = i998if (b >= 0xD800 && b <= 0xDFFF) i++999var h = encodeURIComponent(str.slice(start, i+1)).substr(1).split('%')1000for (var j = 0; j < h.length; j++) {1001byteArray.push(parseInt(h[j], 16))1002}1003}1004}1005return byteArray1006}10071008function asciiToBytes (str) {1009var byteArray = []1010for (var i = 0; i < str.length; i++) {1011// Node's code seems to be doing this and not & 0x7F..1012byteArray.push(str.charCodeAt(i) & 0xFF)1013}1014return byteArray1015}10161017function utf16leToBytes (str) {1018var c, hi, lo1019var byteArray = []1020for (var i = 0; i < str.length; i++) {1021c = str.charCodeAt(i)1022hi = c >> 81023lo = c % 2561024byteArray.push(lo)1025byteArray.push(hi)1026}10271028return byteArray1029}10301031function base64ToBytes (str) {1032return base64.toByteArray(str)1033}10341035function blitBuffer (src, dst, offset, length, unitSize) {1036if (unitSize) length -= length % unitSize;1037for (var i = 0; i < length; i++) {1038if ((i + offset >= dst.length) || (i >= src.length))1039break1040dst[i + offset] = src[i]1041}1042return i1043}10441045function decodeUtf8Char (str) {1046try {1047return decodeURIComponent(str)1048} catch (err) {1049return String.fromCharCode(0xFFFD) // UTF 8 invalid char1050}1051}105210531054