UNPKG

54.4 kBJavaScriptView Raw
1var lookup = [];
2var revLookup = [];
3var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
4var inited = false;
5function init () {
6 inited = true;
7 var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
8 for (var i = 0, len = code.length; i < len; ++i) {
9 lookup[i] = code[i];
10 revLookup[code.charCodeAt(i)] = i;
11 }
12
13 revLookup['-'.charCodeAt(0)] = 62;
14 revLookup['_'.charCodeAt(0)] = 63;
15}
16
17function toByteArray (b64) {
18 if (!inited) {
19 init();
20 }
21 var i, j, l, tmp, placeHolders, arr;
22 var len = b64.length;
23
24 if (len % 4 > 0) {
25 throw new Error('Invalid string. Length must be a multiple of 4')
26 }
27
28 // the number of equal signs (place holders)
29 // if there are two placeholders, than the two characters before it
30 // represent one byte
31 // if there is only one, then the three characters before it represent 2 bytes
32 // this is just a cheap hack to not do indexOf twice
33 placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0;
34
35 // base64 is 4/3 + up to two characters of the original data
36 arr = new Arr(len * 3 / 4 - placeHolders);
37
38 // if there are placeholders, only get up to the last complete 4 chars
39 l = placeHolders > 0 ? len - 4 : len;
40
41 var L = 0;
42
43 for (i = 0, j = 0; i < l; i += 4, j += 3) {
44 tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)];
45 arr[L++] = (tmp >> 16) & 0xFF;
46 arr[L++] = (tmp >> 8) & 0xFF;
47 arr[L++] = tmp & 0xFF;
48 }
49
50 if (placeHolders === 2) {
51 tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4);
52 arr[L++] = tmp & 0xFF;
53 } else if (placeHolders === 1) {
54 tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2);
55 arr[L++] = (tmp >> 8) & 0xFF;
56 arr[L++] = tmp & 0xFF;
57 }
58
59 return arr
60}
61
62function tripletToBase64 (num) {
63 return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
64}
65
66function encodeChunk (uint8, start, end) {
67 var tmp;
68 var output = [];
69 for (var i = start; i < end; i += 3) {
70 tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
71 output.push(tripletToBase64(tmp));
72 }
73 return output.join('')
74}
75
76function fromByteArray (uint8) {
77 if (!inited) {
78 init();
79 }
80 var tmp;
81 var len = uint8.length;
82 var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
83 var output = '';
84 var parts = [];
85 var maxChunkLength = 16383; // must be multiple of 3
86
87 // go through the array every three bytes, we'll deal with trailing stuff later
88 for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
89 parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)));
90 }
91
92 // pad the end with zeros, but make sure to not forget the extra bytes
93 if (extraBytes === 1) {
94 tmp = uint8[len - 1];
95 output += lookup[tmp >> 2];
96 output += lookup[(tmp << 4) & 0x3F];
97 output += '==';
98 } else if (extraBytes === 2) {
99 tmp = (uint8[len - 2] << 8) + (uint8[len - 1]);
100 output += lookup[tmp >> 10];
101 output += lookup[(tmp >> 4) & 0x3F];
102 output += lookup[(tmp << 2) & 0x3F];
103 output += '=';
104 }
105
106 parts.push(output);
107
108 return parts.join('')
109}
110
111function read (buffer, offset, isLE, mLen, nBytes) {
112 var e, m;
113 var eLen = nBytes * 8 - mLen - 1;
114 var eMax = (1 << eLen) - 1;
115 var eBias = eMax >> 1;
116 var nBits = -7;
117 var i = isLE ? (nBytes - 1) : 0;
118 var d = isLE ? -1 : 1;
119 var s = buffer[offset + i];
120
121 i += d;
122
123 e = s & ((1 << (-nBits)) - 1);
124 s >>= (-nBits);
125 nBits += eLen;
126 for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
127
128 m = e & ((1 << (-nBits)) - 1);
129 e >>= (-nBits);
130 nBits += mLen;
131 for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
132
133 if (e === 0) {
134 e = 1 - eBias;
135 } else if (e === eMax) {
136 return m ? NaN : ((s ? -1 : 1) * Infinity)
137 } else {
138 m = m + Math.pow(2, mLen);
139 e = e - eBias;
140 }
141 return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
142}
143
144function write (buffer, value, offset, isLE, mLen, nBytes) {
145 var e, m, c;
146 var eLen = nBytes * 8 - mLen - 1;
147 var eMax = (1 << eLen) - 1;
148 var eBias = eMax >> 1;
149 var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
150 var i = isLE ? 0 : (nBytes - 1);
151 var d = isLE ? 1 : -1;
152 var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
153
154 value = Math.abs(value);
155
156 if (isNaN(value) || value === Infinity) {
157 m = isNaN(value) ? 1 : 0;
158 e = eMax;
159 } else {
160 e = Math.floor(Math.log(value) / Math.LN2);
161 if (value * (c = Math.pow(2, -e)) < 1) {
162 e--;
163 c *= 2;
164 }
165 if (e + eBias >= 1) {
166 value += rt / c;
167 } else {
168 value += rt * Math.pow(2, 1 - eBias);
169 }
170 if (value * c >= 2) {
171 e++;
172 c /= 2;
173 }
174
175 if (e + eBias >= eMax) {
176 m = 0;
177 e = eMax;
178 } else if (e + eBias >= 1) {
179 m = (value * c - 1) * Math.pow(2, mLen);
180 e = e + eBias;
181 } else {
182 m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
183 e = 0;
184 }
185 }
186
187 for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
188
189 e = (e << mLen) | m;
190 eLen += mLen;
191 for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
192
193 buffer[offset + i - d] |= s * 128;
194}
195
196var toString = {}.toString;
197
198var isArray = Array.isArray || function (arr) {
199 return toString.call(arr) == '[object Array]';
200};
201
202/*!
203 * The buffer module from node.js, for the browser.
204 *
205 * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
206 * @license MIT
207 */
208
209var INSPECT_MAX_BYTES = 50;
210
211/**
212 * If `Buffer.TYPED_ARRAY_SUPPORT`:
213 * === true Use Uint8Array implementation (fastest)
214 * === false Use Object implementation (most compatible, even IE6)
215 *
216 * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
217 * Opera 11.6+, iOS 4.2+.
218 *
219 * Due to various browser bugs, sometimes the Object implementation will be used even
220 * when the browser supports typed arrays.
221 *
222 * Note:
223 *
224 * - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
225 * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
226 *
227 * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
228 *
229 * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
230 * incorrect length in some situations.
231
232 * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
233 * get the Object implementation, which is slower but behaves correctly.
234 */
235Buffer.TYPED_ARRAY_SUPPORT = globalThis.TYPED_ARRAY_SUPPORT !== undefined
236 ? globalThis.TYPED_ARRAY_SUPPORT
237 : true;
238
239/*
240 * Export kMaxLength after typed array support is determined.
241 */
242var _kMaxLength = kMaxLength();
243
244function kMaxLength () {
245 return Buffer.TYPED_ARRAY_SUPPORT
246 ? 0x7fffffff
247 : 0x3fffffff
248}
249
250function createBuffer (that, length) {
251 if (kMaxLength() < length) {
252 throw new RangeError('Invalid typed array length')
253 }
254 if (Buffer.TYPED_ARRAY_SUPPORT) {
255 // Return an augmented `Uint8Array` instance, for best performance
256 that = new Uint8Array(length);
257 that.__proto__ = Buffer.prototype;
258 } else {
259 // Fallback: Return an object instance of the Buffer class
260 if (that === null) {
261 that = new Buffer(length);
262 }
263 that.length = length;
264 }
265
266 return that
267}
268
269/**
270 * The Buffer constructor returns instances of `Uint8Array` that have their
271 * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
272 * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
273 * and the `Uint8Array` methods. Square bracket notation works as expected -- it
274 * returns a single octet.
275 *
276 * The `Uint8Array` prototype remains unmodified.
277 */
278
279function Buffer (arg, encodingOrOffset, length) {
280 if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
281 return new Buffer(arg, encodingOrOffset, length)
282 }
283
284 // Common case.
285 if (typeof arg === 'number') {
286 if (typeof encodingOrOffset === 'string') {
287 throw new Error(
288 'If encoding is specified then the first argument must be a string'
289 )
290 }
291 return allocUnsafe(this, arg)
292 }
293 return from(this, arg, encodingOrOffset, length)
294}
295
296Buffer.poolSize = 8192; // not used by this implementation
297
298// TODO: Legacy, not needed anymore. Remove in next major version.
299Buffer._augment = function (arr) {
300 arr.__proto__ = Buffer.prototype;
301 return arr
302};
303
304function from (that, value, encodingOrOffset, length) {
305 if (typeof value === 'number') {
306 throw new TypeError('"value" argument must not be a number')
307 }
308
309 if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
310 return fromArrayBuffer(that, value, encodingOrOffset, length)
311 }
312
313 if (typeof value === 'string') {
314 return fromString(that, value, encodingOrOffset)
315 }
316
317 return fromObject(that, value)
318}
319
320/**
321 * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
322 * if value is a number.
323 * Buffer.from(str[, encoding])
324 * Buffer.from(array)
325 * Buffer.from(buffer)
326 * Buffer.from(arrayBuffer[, byteOffset[, length]])
327 **/
328Buffer.from = function (value, encodingOrOffset, length) {
329 return from(null, value, encodingOrOffset, length)
330};
331
332if (Buffer.TYPED_ARRAY_SUPPORT) {
333 Buffer.prototype.__proto__ = Uint8Array.prototype;
334 Buffer.__proto__ = Uint8Array;
335}
336
337function assertSize (size) {
338 if (typeof size !== 'number') {
339 throw new TypeError('"size" argument must be a number')
340 } else if (size < 0) {
341 throw new RangeError('"size" argument must not be negative')
342 }
343}
344
345function alloc (that, size, fill, encoding) {
346 assertSize(size);
347 if (size <= 0) {
348 return createBuffer(that, size)
349 }
350 if (fill !== undefined) {
351 // Only pay attention to encoding if it's a string. This
352 // prevents accidentally sending in a number that would
353 // be interpretted as a start offset.
354 return typeof encoding === 'string'
355 ? createBuffer(that, size).fill(fill, encoding)
356 : createBuffer(that, size).fill(fill)
357 }
358 return createBuffer(that, size)
359}
360
361/**
362 * Creates a new filled Buffer instance.
363 * alloc(size[, fill[, encoding]])
364 **/
365Buffer.alloc = function (size, fill, encoding) {
366 return alloc(null, size, fill, encoding)
367};
368
369function allocUnsafe (that, size) {
370 assertSize(size);
371 that = createBuffer(that, size < 0 ? 0 : checked(size) | 0);
372 if (!Buffer.TYPED_ARRAY_SUPPORT) {
373 for (var i = 0; i < size; ++i) {
374 that[i] = 0;
375 }
376 }
377 return that
378}
379
380/**
381 * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
382 * */
383Buffer.allocUnsafe = function (size) {
384 return allocUnsafe(null, size)
385};
386/**
387 * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
388 */
389Buffer.allocUnsafeSlow = function (size) {
390 return allocUnsafe(null, size)
391};
392
393function fromString (that, string, encoding) {
394 if (typeof encoding !== 'string' || encoding === '') {
395 encoding = 'utf8';
396 }
397
398 if (!Buffer.isEncoding(encoding)) {
399 throw new TypeError('"encoding" must be a valid string encoding')
400 }
401
402 var length = byteLength(string, encoding) | 0;
403 that = createBuffer(that, length);
404
405 var actual = that.write(string, encoding);
406
407 if (actual !== length) {
408 // Writing a hex string, for example, that contains invalid characters will
409 // cause everything after the first invalid character to be ignored. (e.g.
410 // 'abxxcd' will be treated as 'ab')
411 that = that.slice(0, actual);
412 }
413
414 return that
415}
416
417function fromArrayLike (that, array) {
418 var length = array.length < 0 ? 0 : checked(array.length) | 0;
419 that = createBuffer(that, length);
420 for (var i = 0; i < length; i += 1) {
421 that[i] = array[i] & 255;
422 }
423 return that
424}
425
426function fromArrayBuffer (that, array, byteOffset, length) {
427 array.byteLength; // this throws if `array` is not a valid ArrayBuffer
428
429 if (byteOffset < 0 || array.byteLength < byteOffset) {
430 throw new RangeError('\'offset\' is out of bounds')
431 }
432
433 if (array.byteLength < byteOffset + (length || 0)) {
434 throw new RangeError('\'length\' is out of bounds')
435 }
436
437 if (byteOffset === undefined && length === undefined) {
438 array = new Uint8Array(array);
439 } else if (length === undefined) {
440 array = new Uint8Array(array, byteOffset);
441 } else {
442 array = new Uint8Array(array, byteOffset, length);
443 }
444
445 if (Buffer.TYPED_ARRAY_SUPPORT) {
446 // Return an augmented `Uint8Array` instance, for best performance
447 that = array;
448 that.__proto__ = Buffer.prototype;
449 } else {
450 // Fallback: Return an object instance of the Buffer class
451 that = fromArrayLike(that, array);
452 }
453 return that
454}
455
456function fromObject (that, obj) {
457 if (internalIsBuffer(obj)) {
458 var len = checked(obj.length) | 0;
459 that = createBuffer(that, len);
460
461 if (that.length === 0) {
462 return that
463 }
464
465 obj.copy(that, 0, 0, len);
466 return that
467 }
468
469 if (obj) {
470 if ((typeof ArrayBuffer !== 'undefined' &&
471 obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
472 if (typeof obj.length !== 'number' || isnan(obj.length)) {
473 return createBuffer(that, 0)
474 }
475 return fromArrayLike(that, obj)
476 }
477
478 if (obj.type === 'Buffer' && isArray(obj.data)) {
479 return fromArrayLike(that, obj.data)
480 }
481 }
482
483 throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
484}
485
486function checked (length) {
487 // Note: cannot use `length < kMaxLength()` here because that fails when
488 // length is NaN (which is otherwise coerced to zero.)
489 if (length >= kMaxLength()) {
490 throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
491 'size: 0x' + kMaxLength().toString(16) + ' bytes')
492 }
493 return length | 0
494}
495
496function SlowBuffer (length) {
497 if (+length != length) { // eslint-disable-line eqeqeq
498 length = 0;
499 }
500 return Buffer.alloc(+length)
501}
502Buffer.isBuffer = isBuffer;
503function internalIsBuffer (b) {
504 return !!(b != null && b._isBuffer)
505}
506
507Buffer.compare = function compare (a, b) {
508 if (!internalIsBuffer(a) || !internalIsBuffer(b)) {
509 throw new TypeError('Arguments must be Buffers')
510 }
511
512 if (a === b) return 0
513
514 var x = a.length;
515 var y = b.length;
516
517 for (var i = 0, len = Math.min(x, y); i < len; ++i) {
518 if (a[i] !== b[i]) {
519 x = a[i];
520 y = b[i];
521 break
522 }
523 }
524
525 if (x < y) return -1
526 if (y < x) return 1
527 return 0
528};
529
530Buffer.isEncoding = function isEncoding (encoding) {
531 switch (String(encoding).toLowerCase()) {
532 case 'hex':
533 case 'utf8':
534 case 'utf-8':
535 case 'ascii':
536 case 'latin1':
537 case 'binary':
538 case 'base64':
539 case 'ucs2':
540 case 'ucs-2':
541 case 'utf16le':
542 case 'utf-16le':
543 return true
544 default:
545 return false
546 }
547};
548
549Buffer.concat = function concat (list, length) {
550 if (!isArray(list)) {
551 throw new TypeError('"list" argument must be an Array of Buffers')
552 }
553
554 if (list.length === 0) {
555 return Buffer.alloc(0)
556 }
557
558 var i;
559 if (length === undefined) {
560 length = 0;
561 for (i = 0; i < list.length; ++i) {
562 length += list[i].length;
563 }
564 }
565
566 var buffer = Buffer.allocUnsafe(length);
567 var pos = 0;
568 for (i = 0; i < list.length; ++i) {
569 var buf = list[i];
570 if (!internalIsBuffer(buf)) {
571 throw new TypeError('"list" argument must be an Array of Buffers')
572 }
573 buf.copy(buffer, pos);
574 pos += buf.length;
575 }
576 return buffer
577};
578
579function byteLength (string, encoding) {
580 if (internalIsBuffer(string)) {
581 return string.length
582 }
583 if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
584 (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
585 return string.byteLength
586 }
587 if (typeof string !== 'string') {
588 string = '' + string;
589 }
590
591 var len = string.length;
592 if (len === 0) return 0
593
594 // Use a for loop to avoid recursion
595 var loweredCase = false;
596 for (;;) {
597 switch (encoding) {
598 case 'ascii':
599 case 'latin1':
600 case 'binary':
601 return len
602 case 'utf8':
603 case 'utf-8':
604 case undefined:
605 return utf8ToBytes(string).length
606 case 'ucs2':
607 case 'ucs-2':
608 case 'utf16le':
609 case 'utf-16le':
610 return len * 2
611 case 'hex':
612 return len >>> 1
613 case 'base64':
614 return base64ToBytes(string).length
615 default:
616 if (loweredCase) return utf8ToBytes(string).length // assume utf8
617 encoding = ('' + encoding).toLowerCase();
618 loweredCase = true;
619 }
620 }
621}
622Buffer.byteLength = byteLength;
623
624function slowToString (encoding, start, end) {
625 var loweredCase = false;
626
627 // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
628 // property of a typed array.
629
630 // This behaves neither like String nor Uint8Array in that we set start/end
631 // to their upper/lower bounds if the value passed is out of range.
632 // undefined is handled specially as per ECMA-262 6th Edition,
633 // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
634 if (start === undefined || start < 0) {
635 start = 0;
636 }
637 // Return early if start > this.length. Done here to prevent potential uint32
638 // coercion fail below.
639 if (start > this.length) {
640 return ''
641 }
642
643 if (end === undefined || end > this.length) {
644 end = this.length;
645 }
646
647 if (end <= 0) {
648 return ''
649 }
650
651 // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
652 end >>>= 0;
653 start >>>= 0;
654
655 if (end <= start) {
656 return ''
657 }
658
659 if (!encoding) encoding = 'utf8';
660
661 while (true) {
662 switch (encoding) {
663 case 'hex':
664 return hexSlice(this, start, end)
665
666 case 'utf8':
667 case 'utf-8':
668 return utf8Slice(this, start, end)
669
670 case 'ascii':
671 return asciiSlice(this, start, end)
672
673 case 'latin1':
674 case 'binary':
675 return latin1Slice(this, start, end)
676
677 case 'base64':
678 return base64Slice(this, start, end)
679
680 case 'ucs2':
681 case 'ucs-2':
682 case 'utf16le':
683 case 'utf-16le':
684 return utf16leSlice(this, start, end)
685
686 default:
687 if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
688 encoding = (encoding + '').toLowerCase();
689 loweredCase = true;
690 }
691 }
692}
693
694// The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect
695// Buffer instances.
696Buffer.prototype._isBuffer = true;
697
698function swap (b, n, m) {
699 var i = b[n];
700 b[n] = b[m];
701 b[m] = i;
702}
703
704Buffer.prototype.swap16 = function swap16 () {
705 var len = this.length;
706 if (len % 2 !== 0) {
707 throw new RangeError('Buffer size must be a multiple of 16-bits')
708 }
709 for (var i = 0; i < len; i += 2) {
710 swap(this, i, i + 1);
711 }
712 return this
713};
714
715Buffer.prototype.swap32 = function swap32 () {
716 var len = this.length;
717 if (len % 4 !== 0) {
718 throw new RangeError('Buffer size must be a multiple of 32-bits')
719 }
720 for (var i = 0; i < len; i += 4) {
721 swap(this, i, i + 3);
722 swap(this, i + 1, i + 2);
723 }
724 return this
725};
726
727Buffer.prototype.swap64 = function swap64 () {
728 var len = this.length;
729 if (len % 8 !== 0) {
730 throw new RangeError('Buffer size must be a multiple of 64-bits')
731 }
732 for (var i = 0; i < len; i += 8) {
733 swap(this, i, i + 7);
734 swap(this, i + 1, i + 6);
735 swap(this, i + 2, i + 5);
736 swap(this, i + 3, i + 4);
737 }
738 return this
739};
740
741Buffer.prototype.toString = function toString () {
742 var length = this.length | 0;
743 if (length === 0) return ''
744 if (arguments.length === 0) return utf8Slice(this, 0, length)
745 return slowToString.apply(this, arguments)
746};
747
748Buffer.prototype.equals = function equals (b) {
749 if (!internalIsBuffer(b)) throw new TypeError('Argument must be a Buffer')
750 if (this === b) return true
751 return Buffer.compare(this, b) === 0
752};
753
754Buffer.prototype.inspect = function inspect () {
755 var str = '';
756 var max = INSPECT_MAX_BYTES;
757 if (this.length > 0) {
758 str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
759 if (this.length > max) str += ' ... ';
760 }
761 return '<Buffer ' + str + '>'
762};
763
764Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
765 if (!internalIsBuffer(target)) {
766 throw new TypeError('Argument must be a Buffer')
767 }
768
769 if (start === undefined) {
770 start = 0;
771 }
772 if (end === undefined) {
773 end = target ? target.length : 0;
774 }
775 if (thisStart === undefined) {
776 thisStart = 0;
777 }
778 if (thisEnd === undefined) {
779 thisEnd = this.length;
780 }
781
782 if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
783 throw new RangeError('out of range index')
784 }
785
786 if (thisStart >= thisEnd && start >= end) {
787 return 0
788 }
789 if (thisStart >= thisEnd) {
790 return -1
791 }
792 if (start >= end) {
793 return 1
794 }
795
796 start >>>= 0;
797 end >>>= 0;
798 thisStart >>>= 0;
799 thisEnd >>>= 0;
800
801 if (this === target) return 0
802
803 var x = thisEnd - thisStart;
804 var y = end - start;
805 var len = Math.min(x, y);
806
807 var thisCopy = this.slice(thisStart, thisEnd);
808 var targetCopy = target.slice(start, end);
809
810 for (var i = 0; i < len; ++i) {
811 if (thisCopy[i] !== targetCopy[i]) {
812 x = thisCopy[i];
813 y = targetCopy[i];
814 break
815 }
816 }
817
818 if (x < y) return -1
819 if (y < x) return 1
820 return 0
821};
822
823// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
824// OR the last index of `val` in `buffer` at offset <= `byteOffset`.
825//
826// Arguments:
827// - buffer - a Buffer to search
828// - val - a string, Buffer, or number
829// - byteOffset - an index into `buffer`; will be clamped to an int32
830// - encoding - an optional encoding, relevant is val is a string
831// - dir - true for indexOf, false for lastIndexOf
832function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
833 // Empty buffer means no match
834 if (buffer.length === 0) return -1
835
836 // Normalize byteOffset
837 if (typeof byteOffset === 'string') {
838 encoding = byteOffset;
839 byteOffset = 0;
840 } else if (byteOffset > 0x7fffffff) {
841 byteOffset = 0x7fffffff;
842 } else if (byteOffset < -0x80000000) {
843 byteOffset = -0x80000000;
844 }
845 byteOffset = +byteOffset; // Coerce to Number.
846 if (isNaN(byteOffset)) {
847 // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
848 byteOffset = dir ? 0 : (buffer.length - 1);
849 }
850
851 // Normalize byteOffset: negative offsets start from the end of the buffer
852 if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
853 if (byteOffset >= buffer.length) {
854 if (dir) return -1
855 else byteOffset = buffer.length - 1;
856 } else if (byteOffset < 0) {
857 if (dir) byteOffset = 0;
858 else return -1
859 }
860
861 // Normalize val
862 if (typeof val === 'string') {
863 val = Buffer.from(val, encoding);
864 }
865
866 // Finally, search either indexOf (if dir is true) or lastIndexOf
867 if (internalIsBuffer(val)) {
868 // Special case: looking for empty string/buffer always fails
869 if (val.length === 0) {
870 return -1
871 }
872 return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
873 } else if (typeof val === 'number') {
874 val = val & 0xFF; // Search for a byte value [0-255]
875 if (Buffer.TYPED_ARRAY_SUPPORT &&
876 typeof Uint8Array.prototype.indexOf === 'function') {
877 if (dir) {
878 return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
879 } else {
880 return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
881 }
882 }
883 return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
884 }
885
886 throw new TypeError('val must be string, number or Buffer')
887}
888
889function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
890 var indexSize = 1;
891 var arrLength = arr.length;
892 var valLength = val.length;
893
894 if (encoding !== undefined) {
895 encoding = String(encoding).toLowerCase();
896 if (encoding === 'ucs2' || encoding === 'ucs-2' ||
897 encoding === 'utf16le' || encoding === 'utf-16le') {
898 if (arr.length < 2 || val.length < 2) {
899 return -1
900 }
901 indexSize = 2;
902 arrLength /= 2;
903 valLength /= 2;
904 byteOffset /= 2;
905 }
906 }
907
908 function read (buf, i) {
909 if (indexSize === 1) {
910 return buf[i]
911 } else {
912 return buf.readUInt16BE(i * indexSize)
913 }
914 }
915
916 var i;
917 if (dir) {
918 var foundIndex = -1;
919 for (i = byteOffset; i < arrLength; i++) {
920 if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
921 if (foundIndex === -1) foundIndex = i;
922 if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
923 } else {
924 if (foundIndex !== -1) i -= i - foundIndex;
925 foundIndex = -1;
926 }
927 }
928 } else {
929 if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
930 for (i = byteOffset; i >= 0; i--) {
931 var found = true;
932 for (var j = 0; j < valLength; j++) {
933 if (read(arr, i + j) !== read(val, j)) {
934 found = false;
935 break
936 }
937 }
938 if (found) return i
939 }
940 }
941
942 return -1
943}
944
945Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
946 return this.indexOf(val, byteOffset, encoding) !== -1
947};
948
949Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
950 return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
951};
952
953Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
954 return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
955};
956
957function hexWrite (buf, string, offset, length) {
958 offset = Number(offset) || 0;
959 var remaining = buf.length - offset;
960 if (!length) {
961 length = remaining;
962 } else {
963 length = Number(length);
964 if (length > remaining) {
965 length = remaining;
966 }
967 }
968
969 // must be an even number of digits
970 var strLen = string.length;
971 if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
972
973 if (length > strLen / 2) {
974 length = strLen / 2;
975 }
976 for (var i = 0; i < length; ++i) {
977 var parsed = parseInt(string.substr(i * 2, 2), 16);
978 if (isNaN(parsed)) return i
979 buf[offset + i] = parsed;
980 }
981 return i
982}
983
984function utf8Write (buf, string, offset, length) {
985 return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
986}
987
988function asciiWrite (buf, string, offset, length) {
989 return blitBuffer(asciiToBytes(string), buf, offset, length)
990}
991
992function latin1Write (buf, string, offset, length) {
993 return asciiWrite(buf, string, offset, length)
994}
995
996function base64Write (buf, string, offset, length) {
997 return blitBuffer(base64ToBytes(string), buf, offset, length)
998}
999
1000function ucs2Write (buf, string, offset, length) {
1001 return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
1002}
1003
1004Buffer.prototype.write = function write (string, offset, length, encoding) {
1005 // Buffer#write(string)
1006 if (offset === undefined) {
1007 encoding = 'utf8';
1008 length = this.length;
1009 offset = 0;
1010 // Buffer#write(string, encoding)
1011 } else if (length === undefined && typeof offset === 'string') {
1012 encoding = offset;
1013 length = this.length;
1014 offset = 0;
1015 // Buffer#write(string, offset[, length][, encoding])
1016 } else if (isFinite(offset)) {
1017 offset = offset | 0;
1018 if (isFinite(length)) {
1019 length = length | 0;
1020 if (encoding === undefined) encoding = 'utf8';
1021 } else {
1022 encoding = length;
1023 length = undefined;
1024 }
1025 // legacy write(string, encoding, offset, length) - remove in v0.13
1026 } else {
1027 throw new Error(
1028 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
1029 )
1030 }
1031
1032 var remaining = this.length - offset;
1033 if (length === undefined || length > remaining) length = remaining;
1034
1035 if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
1036 throw new RangeError('Attempt to write outside buffer bounds')
1037 }
1038
1039 if (!encoding) encoding = 'utf8';
1040
1041 var loweredCase = false;
1042 for (;;) {
1043 switch (encoding) {
1044 case 'hex':
1045 return hexWrite(this, string, offset, length)
1046
1047 case 'utf8':
1048 case 'utf-8':
1049 return utf8Write(this, string, offset, length)
1050
1051 case 'ascii':
1052 return asciiWrite(this, string, offset, length)
1053
1054 case 'latin1':
1055 case 'binary':
1056 return latin1Write(this, string, offset, length)
1057
1058 case 'base64':
1059 // Warning: maxLength not taken into account in base64Write
1060 return base64Write(this, string, offset, length)
1061
1062 case 'ucs2':
1063 case 'ucs-2':
1064 case 'utf16le':
1065 case 'utf-16le':
1066 return ucs2Write(this, string, offset, length)
1067
1068 default:
1069 if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
1070 encoding = ('' + encoding).toLowerCase();
1071 loweredCase = true;
1072 }
1073 }
1074};
1075
1076Buffer.prototype.toJSON = function toJSON () {
1077 return {
1078 type: 'Buffer',
1079 data: Array.prototype.slice.call(this._arr || this, 0)
1080 }
1081};
1082
1083function base64Slice (buf, start, end) {
1084 if (start === 0 && end === buf.length) {
1085 return fromByteArray(buf)
1086 } else {
1087 return fromByteArray(buf.slice(start, end))
1088 }
1089}
1090
1091function utf8Slice (buf, start, end) {
1092 end = Math.min(buf.length, end);
1093 var res = [];
1094
1095 var i = start;
1096 while (i < end) {
1097 var firstByte = buf[i];
1098 var codePoint = null;
1099 var bytesPerSequence = (firstByte > 0xEF) ? 4
1100 : (firstByte > 0xDF) ? 3
1101 : (firstByte > 0xBF) ? 2
1102 : 1;
1103
1104 if (i + bytesPerSequence <= end) {
1105 var secondByte, thirdByte, fourthByte, tempCodePoint;
1106
1107 switch (bytesPerSequence) {
1108 case 1:
1109 if (firstByte < 0x80) {
1110 codePoint = firstByte;
1111 }
1112 break
1113 case 2:
1114 secondByte = buf[i + 1];
1115 if ((secondByte & 0xC0) === 0x80) {
1116 tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
1117 if (tempCodePoint > 0x7F) {
1118 codePoint = tempCodePoint;
1119 }
1120 }
1121 break
1122 case 3:
1123 secondByte = buf[i + 1];
1124 thirdByte = buf[i + 2];
1125 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
1126 tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
1127 if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
1128 codePoint = tempCodePoint;
1129 }
1130 }
1131 break
1132 case 4:
1133 secondByte = buf[i + 1];
1134 thirdByte = buf[i + 2];
1135 fourthByte = buf[i + 3];
1136 if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
1137 tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
1138 if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
1139 codePoint = tempCodePoint;
1140 }
1141 }
1142 }
1143 }
1144
1145 if (codePoint === null) {
1146 // we did not generate a valid codePoint so insert a
1147 // replacement char (U+FFFD) and advance only 1 byte
1148 codePoint = 0xFFFD;
1149 bytesPerSequence = 1;
1150 } else if (codePoint > 0xFFFF) {
1151 // encode to utf16 (surrogate pair dance)
1152 codePoint -= 0x10000;
1153 res.push(codePoint >>> 10 & 0x3FF | 0xD800);
1154 codePoint = 0xDC00 | codePoint & 0x3FF;
1155 }
1156
1157 res.push(codePoint);
1158 i += bytesPerSequence;
1159 }
1160
1161 return decodeCodePointsArray(res)
1162}
1163
1164// Based on http://stackoverflow.com/a/22747272/680742, the browser with
1165// the lowest limit is Chrome, with 0x10000 args.
1166// We go 1 magnitude less, for safety
1167var MAX_ARGUMENTS_LENGTH = 0x1000;
1168
1169function decodeCodePointsArray (codePoints) {
1170 var len = codePoints.length;
1171 if (len <= MAX_ARGUMENTS_LENGTH) {
1172 return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
1173 }
1174
1175 // Decode in chunks to avoid "call stack size exceeded".
1176 var res = '';
1177 var i = 0;
1178 while (i < len) {
1179 res += String.fromCharCode.apply(
1180 String,
1181 codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
1182 );
1183 }
1184 return res
1185}
1186
1187function asciiSlice (buf, start, end) {
1188 var ret = '';
1189 end = Math.min(buf.length, end);
1190
1191 for (var i = start; i < end; ++i) {
1192 ret += String.fromCharCode(buf[i] & 0x7F);
1193 }
1194 return ret
1195}
1196
1197function latin1Slice (buf, start, end) {
1198 var ret = '';
1199 end = Math.min(buf.length, end);
1200
1201 for (var i = start; i < end; ++i) {
1202 ret += String.fromCharCode(buf[i]);
1203 }
1204 return ret
1205}
1206
1207function hexSlice (buf, start, end) {
1208 var len = buf.length;
1209
1210 if (!start || start < 0) start = 0;
1211 if (!end || end < 0 || end > len) end = len;
1212
1213 var out = '';
1214 for (var i = start; i < end; ++i) {
1215 out += toHex(buf[i]);
1216 }
1217 return out
1218}
1219
1220function utf16leSlice (buf, start, end) {
1221 var bytes = buf.slice(start, end);
1222 var res = '';
1223 for (var i = 0; i < bytes.length; i += 2) {
1224 res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256);
1225 }
1226 return res
1227}
1228
1229Buffer.prototype.slice = function slice (start, end) {
1230 var len = this.length;
1231 start = ~~start;
1232 end = end === undefined ? len : ~~end;
1233
1234 if (start < 0) {
1235 start += len;
1236 if (start < 0) start = 0;
1237 } else if (start > len) {
1238 start = len;
1239 }
1240
1241 if (end < 0) {
1242 end += len;
1243 if (end < 0) end = 0;
1244 } else if (end > len) {
1245 end = len;
1246 }
1247
1248 if (end < start) end = start;
1249
1250 var newBuf;
1251 if (Buffer.TYPED_ARRAY_SUPPORT) {
1252 newBuf = this.subarray(start, end);
1253 newBuf.__proto__ = Buffer.prototype;
1254 } else {
1255 var sliceLen = end - start;
1256 newBuf = new Buffer(sliceLen, undefined);
1257 for (var i = 0; i < sliceLen; ++i) {
1258 newBuf[i] = this[i + start];
1259 }
1260 }
1261
1262 return newBuf
1263};
1264
1265/*
1266 * Need to make sure that buffer isn't trying to write out of bounds.
1267 */
1268function checkOffset (offset, ext, length) {
1269 if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
1270 if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
1271}
1272
1273Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
1274 offset = offset | 0;
1275 byteLength = byteLength | 0;
1276 if (!noAssert) checkOffset(offset, byteLength, this.length);
1277
1278 var val = this[offset];
1279 var mul = 1;
1280 var i = 0;
1281 while (++i < byteLength && (mul *= 0x100)) {
1282 val += this[offset + i] * mul;
1283 }
1284
1285 return val
1286};
1287
1288Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
1289 offset = offset | 0;
1290 byteLength = byteLength | 0;
1291 if (!noAssert) {
1292 checkOffset(offset, byteLength, this.length);
1293 }
1294
1295 var val = this[offset + --byteLength];
1296 var mul = 1;
1297 while (byteLength > 0 && (mul *= 0x100)) {
1298 val += this[offset + --byteLength] * mul;
1299 }
1300
1301 return val
1302};
1303
1304Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
1305 if (!noAssert) checkOffset(offset, 1, this.length);
1306 return this[offset]
1307};
1308
1309Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
1310 if (!noAssert) checkOffset(offset, 2, this.length);
1311 return this[offset] | (this[offset + 1] << 8)
1312};
1313
1314Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
1315 if (!noAssert) checkOffset(offset, 2, this.length);
1316 return (this[offset] << 8) | this[offset + 1]
1317};
1318
1319Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
1320 if (!noAssert) checkOffset(offset, 4, this.length);
1321
1322 return ((this[offset]) |
1323 (this[offset + 1] << 8) |
1324 (this[offset + 2] << 16)) +
1325 (this[offset + 3] * 0x1000000)
1326};
1327
1328Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
1329 if (!noAssert) checkOffset(offset, 4, this.length);
1330
1331 return (this[offset] * 0x1000000) +
1332 ((this[offset + 1] << 16) |
1333 (this[offset + 2] << 8) |
1334 this[offset + 3])
1335};
1336
1337Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
1338 offset = offset | 0;
1339 byteLength = byteLength | 0;
1340 if (!noAssert) checkOffset(offset, byteLength, this.length);
1341
1342 var val = this[offset];
1343 var mul = 1;
1344 var i = 0;
1345 while (++i < byteLength && (mul *= 0x100)) {
1346 val += this[offset + i] * mul;
1347 }
1348 mul *= 0x80;
1349
1350 if (val >= mul) val -= Math.pow(2, 8 * byteLength);
1351
1352 return val
1353};
1354
1355Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
1356 offset = offset | 0;
1357 byteLength = byteLength | 0;
1358 if (!noAssert) checkOffset(offset, byteLength, this.length);
1359
1360 var i = byteLength;
1361 var mul = 1;
1362 var val = this[offset + --i];
1363 while (i > 0 && (mul *= 0x100)) {
1364 val += this[offset + --i] * mul;
1365 }
1366 mul *= 0x80;
1367
1368 if (val >= mul) val -= Math.pow(2, 8 * byteLength);
1369
1370 return val
1371};
1372
1373Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
1374 if (!noAssert) checkOffset(offset, 1, this.length);
1375 if (!(this[offset] & 0x80)) return (this[offset])
1376 return ((0xff - this[offset] + 1) * -1)
1377};
1378
1379Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
1380 if (!noAssert) checkOffset(offset, 2, this.length);
1381 var val = this[offset] | (this[offset + 1] << 8);
1382 return (val & 0x8000) ? val | 0xFFFF0000 : val
1383};
1384
1385Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
1386 if (!noAssert) checkOffset(offset, 2, this.length);
1387 var val = this[offset + 1] | (this[offset] << 8);
1388 return (val & 0x8000) ? val | 0xFFFF0000 : val
1389};
1390
1391Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
1392 if (!noAssert) checkOffset(offset, 4, this.length);
1393
1394 return (this[offset]) |
1395 (this[offset + 1] << 8) |
1396 (this[offset + 2] << 16) |
1397 (this[offset + 3] << 24)
1398};
1399
1400Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
1401 if (!noAssert) checkOffset(offset, 4, this.length);
1402
1403 return (this[offset] << 24) |
1404 (this[offset + 1] << 16) |
1405 (this[offset + 2] << 8) |
1406 (this[offset + 3])
1407};
1408
1409Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
1410 if (!noAssert) checkOffset(offset, 4, this.length);
1411 return read(this, offset, true, 23, 4)
1412};
1413
1414Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
1415 if (!noAssert) checkOffset(offset, 4, this.length);
1416 return read(this, offset, false, 23, 4)
1417};
1418
1419Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
1420 if (!noAssert) checkOffset(offset, 8, this.length);
1421 return read(this, offset, true, 52, 8)
1422};
1423
1424Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
1425 if (!noAssert) checkOffset(offset, 8, this.length);
1426 return read(this, offset, false, 52, 8)
1427};
1428
1429function checkInt (buf, value, offset, ext, max, min) {
1430 if (!internalIsBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
1431 if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
1432 if (offset + ext > buf.length) throw new RangeError('Index out of range')
1433}
1434
1435Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
1436 value = +value;
1437 offset = offset | 0;
1438 byteLength = byteLength | 0;
1439 if (!noAssert) {
1440 var maxBytes = Math.pow(2, 8 * byteLength) - 1;
1441 checkInt(this, value, offset, byteLength, maxBytes, 0);
1442 }
1443
1444 var mul = 1;
1445 var i = 0;
1446 this[offset] = value & 0xFF;
1447 while (++i < byteLength && (mul *= 0x100)) {
1448 this[offset + i] = (value / mul) & 0xFF;
1449 }
1450
1451 return offset + byteLength
1452};
1453
1454Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
1455 value = +value;
1456 offset = offset | 0;
1457 byteLength = byteLength | 0;
1458 if (!noAssert) {
1459 var maxBytes = Math.pow(2, 8 * byteLength) - 1;
1460 checkInt(this, value, offset, byteLength, maxBytes, 0);
1461 }
1462
1463 var i = byteLength - 1;
1464 var mul = 1;
1465 this[offset + i] = value & 0xFF;
1466 while (--i >= 0 && (mul *= 0x100)) {
1467 this[offset + i] = (value / mul) & 0xFF;
1468 }
1469
1470 return offset + byteLength
1471};
1472
1473Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
1474 value = +value;
1475 offset = offset | 0;
1476 if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
1477 if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
1478 this[offset] = (value & 0xff);
1479 return offset + 1
1480};
1481
1482function objectWriteUInt16 (buf, value, offset, littleEndian) {
1483 if (value < 0) value = 0xffff + value + 1;
1484 for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; ++i) {
1485 buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
1486 (littleEndian ? i : 1 - i) * 8;
1487 }
1488}
1489
1490Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
1491 value = +value;
1492 offset = offset | 0;
1493 if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
1494 if (Buffer.TYPED_ARRAY_SUPPORT) {
1495 this[offset] = (value & 0xff);
1496 this[offset + 1] = (value >>> 8);
1497 } else {
1498 objectWriteUInt16(this, value, offset, true);
1499 }
1500 return offset + 2
1501};
1502
1503Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
1504 value = +value;
1505 offset = offset | 0;
1506 if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
1507 if (Buffer.TYPED_ARRAY_SUPPORT) {
1508 this[offset] = (value >>> 8);
1509 this[offset + 1] = (value & 0xff);
1510 } else {
1511 objectWriteUInt16(this, value, offset, false);
1512 }
1513 return offset + 2
1514};
1515
1516function objectWriteUInt32 (buf, value, offset, littleEndian) {
1517 if (value < 0) value = 0xffffffff + value + 1;
1518 for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; ++i) {
1519 buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff;
1520 }
1521}
1522
1523Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
1524 value = +value;
1525 offset = offset | 0;
1526 if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
1527 if (Buffer.TYPED_ARRAY_SUPPORT) {
1528 this[offset + 3] = (value >>> 24);
1529 this[offset + 2] = (value >>> 16);
1530 this[offset + 1] = (value >>> 8);
1531 this[offset] = (value & 0xff);
1532 } else {
1533 objectWriteUInt32(this, value, offset, true);
1534 }
1535 return offset + 4
1536};
1537
1538Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
1539 value = +value;
1540 offset = offset | 0;
1541 if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
1542 if (Buffer.TYPED_ARRAY_SUPPORT) {
1543 this[offset] = (value >>> 24);
1544 this[offset + 1] = (value >>> 16);
1545 this[offset + 2] = (value >>> 8);
1546 this[offset + 3] = (value & 0xff);
1547 } else {
1548 objectWriteUInt32(this, value, offset, false);
1549 }
1550 return offset + 4
1551};
1552
1553Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
1554 value = +value;
1555 offset = offset | 0;
1556 if (!noAssert) {
1557 var limit = Math.pow(2, 8 * byteLength - 1);
1558
1559 checkInt(this, value, offset, byteLength, limit - 1, -limit);
1560 }
1561
1562 var i = 0;
1563 var mul = 1;
1564 var sub = 0;
1565 this[offset] = value & 0xFF;
1566 while (++i < byteLength && (mul *= 0x100)) {
1567 if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
1568 sub = 1;
1569 }
1570 this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
1571 }
1572
1573 return offset + byteLength
1574};
1575
1576Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
1577 value = +value;
1578 offset = offset | 0;
1579 if (!noAssert) {
1580 var limit = Math.pow(2, 8 * byteLength - 1);
1581
1582 checkInt(this, value, offset, byteLength, limit - 1, -limit);
1583 }
1584
1585 var i = byteLength - 1;
1586 var mul = 1;
1587 var sub = 0;
1588 this[offset + i] = value & 0xFF;
1589 while (--i >= 0 && (mul *= 0x100)) {
1590 if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
1591 sub = 1;
1592 }
1593 this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
1594 }
1595
1596 return offset + byteLength
1597};
1598
1599Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
1600 value = +value;
1601 offset = offset | 0;
1602 if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80);
1603 if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
1604 if (value < 0) value = 0xff + value + 1;
1605 this[offset] = (value & 0xff);
1606 return offset + 1
1607};
1608
1609Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
1610 value = +value;
1611 offset = offset | 0;
1612 if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
1613 if (Buffer.TYPED_ARRAY_SUPPORT) {
1614 this[offset] = (value & 0xff);
1615 this[offset + 1] = (value >>> 8);
1616 } else {
1617 objectWriteUInt16(this, value, offset, true);
1618 }
1619 return offset + 2
1620};
1621
1622Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
1623 value = +value;
1624 offset = offset | 0;
1625 if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
1626 if (Buffer.TYPED_ARRAY_SUPPORT) {
1627 this[offset] = (value >>> 8);
1628 this[offset + 1] = (value & 0xff);
1629 } else {
1630 objectWriteUInt16(this, value, offset, false);
1631 }
1632 return offset + 2
1633};
1634
1635Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
1636 value = +value;
1637 offset = offset | 0;
1638 if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
1639 if (Buffer.TYPED_ARRAY_SUPPORT) {
1640 this[offset] = (value & 0xff);
1641 this[offset + 1] = (value >>> 8);
1642 this[offset + 2] = (value >>> 16);
1643 this[offset + 3] = (value >>> 24);
1644 } else {
1645 objectWriteUInt32(this, value, offset, true);
1646 }
1647 return offset + 4
1648};
1649
1650Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
1651 value = +value;
1652 offset = offset | 0;
1653 if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
1654 if (value < 0) value = 0xffffffff + value + 1;
1655 if (Buffer.TYPED_ARRAY_SUPPORT) {
1656 this[offset] = (value >>> 24);
1657 this[offset + 1] = (value >>> 16);
1658 this[offset + 2] = (value >>> 8);
1659 this[offset + 3] = (value & 0xff);
1660 } else {
1661 objectWriteUInt32(this, value, offset, false);
1662 }
1663 return offset + 4
1664};
1665
1666function checkIEEE754 (buf, value, offset, ext, max, min) {
1667 if (offset + ext > buf.length) throw new RangeError('Index out of range')
1668 if (offset < 0) throw new RangeError('Index out of range')
1669}
1670
1671function writeFloat (buf, value, offset, littleEndian, noAssert) {
1672 if (!noAssert) {
1673 checkIEEE754(buf, value, offset, 4);
1674 }
1675 write(buf, value, offset, littleEndian, 23, 4);
1676 return offset + 4
1677}
1678
1679Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
1680 return writeFloat(this, value, offset, true, noAssert)
1681};
1682
1683Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
1684 return writeFloat(this, value, offset, false, noAssert)
1685};
1686
1687function writeDouble (buf, value, offset, littleEndian, noAssert) {
1688 if (!noAssert) {
1689 checkIEEE754(buf, value, offset, 8);
1690 }
1691 write(buf, value, offset, littleEndian, 52, 8);
1692 return offset + 8
1693}
1694
1695Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
1696 return writeDouble(this, value, offset, true, noAssert)
1697};
1698
1699Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
1700 return writeDouble(this, value, offset, false, noAssert)
1701};
1702
1703// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
1704Buffer.prototype.copy = function copy (target, targetStart, start, end) {
1705 if (!start) start = 0;
1706 if (!end && end !== 0) end = this.length;
1707 if (targetStart >= target.length) targetStart = target.length;
1708 if (!targetStart) targetStart = 0;
1709 if (end > 0 && end < start) end = start;
1710
1711 // Copy 0 bytes; we're done
1712 if (end === start) return 0
1713 if (target.length === 0 || this.length === 0) return 0
1714
1715 // Fatal error conditions
1716 if (targetStart < 0) {
1717 throw new RangeError('targetStart out of bounds')
1718 }
1719 if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
1720 if (end < 0) throw new RangeError('sourceEnd out of bounds')
1721
1722 // Are we oob?
1723 if (end > this.length) end = this.length;
1724 if (target.length - targetStart < end - start) {
1725 end = target.length - targetStart + start;
1726 }
1727
1728 var len = end - start;
1729 var i;
1730
1731 if (this === target && start < targetStart && targetStart < end) {
1732 // descending copy from end
1733 for (i = len - 1; i >= 0; --i) {
1734 target[i + targetStart] = this[i + start];
1735 }
1736 } else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
1737 // ascending copy from start
1738 for (i = 0; i < len; ++i) {
1739 target[i + targetStart] = this[i + start];
1740 }
1741 } else {
1742 Uint8Array.prototype.set.call(
1743 target,
1744 this.subarray(start, start + len),
1745 targetStart
1746 );
1747 }
1748
1749 return len
1750};
1751
1752// Usage:
1753// buffer.fill(number[, offset[, end]])
1754// buffer.fill(buffer[, offset[, end]])
1755// buffer.fill(string[, offset[, end]][, encoding])
1756Buffer.prototype.fill = function fill (val, start, end, encoding) {
1757 // Handle string cases:
1758 if (typeof val === 'string') {
1759 if (typeof start === 'string') {
1760 encoding = start;
1761 start = 0;
1762 end = this.length;
1763 } else if (typeof end === 'string') {
1764 encoding = end;
1765 end = this.length;
1766 }
1767 if (val.length === 1) {
1768 var code = val.charCodeAt(0);
1769 if (code < 256) {
1770 val = code;
1771 }
1772 }
1773 if (encoding !== undefined && typeof encoding !== 'string') {
1774 throw new TypeError('encoding must be a string')
1775 }
1776 if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
1777 throw new TypeError('Unknown encoding: ' + encoding)
1778 }
1779 } else if (typeof val === 'number') {
1780 val = val & 255;
1781 }
1782
1783 // Invalid ranges are not set to a default, so can range check early.
1784 if (start < 0 || this.length < start || this.length < end) {
1785 throw new RangeError('Out of range index')
1786 }
1787
1788 if (end <= start) {
1789 return this
1790 }
1791
1792 start = start >>> 0;
1793 end = end === undefined ? this.length : end >>> 0;
1794
1795 if (!val) val = 0;
1796
1797 var i;
1798 if (typeof val === 'number') {
1799 for (i = start; i < end; ++i) {
1800 this[i] = val;
1801 }
1802 } else {
1803 var bytes = internalIsBuffer(val)
1804 ? val
1805 : utf8ToBytes(new Buffer(val, encoding).toString());
1806 var len = bytes.length;
1807 for (i = 0; i < end - start; ++i) {
1808 this[i + start] = bytes[i % len];
1809 }
1810 }
1811
1812 return this
1813};
1814
1815// HELPER FUNCTIONS
1816// ================
1817
1818var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g;
1819
1820function base64clean (str) {
1821 // Node strips out invalid characters like \n and \t from the string, base64-js does not
1822 str = stringtrim(str).replace(INVALID_BASE64_RE, '');
1823 // Node converts strings with length < 2 to ''
1824 if (str.length < 2) return ''
1825 // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
1826 while (str.length % 4 !== 0) {
1827 str = str + '=';
1828 }
1829 return str
1830}
1831
1832function stringtrim (str) {
1833 if (str.trim) return str.trim()
1834 return str.replace(/^\s+|\s+$/g, '')
1835}
1836
1837function toHex (n) {
1838 if (n < 16) return '0' + n.toString(16)
1839 return n.toString(16)
1840}
1841
1842function utf8ToBytes (string, units) {
1843 units = units || Infinity;
1844 var codePoint;
1845 var length = string.length;
1846 var leadSurrogate = null;
1847 var bytes = [];
1848
1849 for (var i = 0; i < length; ++i) {
1850 codePoint = string.charCodeAt(i);
1851
1852 // is surrogate component
1853 if (codePoint > 0xD7FF && codePoint < 0xE000) {
1854 // last char was a lead
1855 if (!leadSurrogate) {
1856 // no lead yet
1857 if (codePoint > 0xDBFF) {
1858 // unexpected trail
1859 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
1860 continue
1861 } else if (i + 1 === length) {
1862 // unpaired lead
1863 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
1864 continue
1865 }
1866
1867 // valid lead
1868 leadSurrogate = codePoint;
1869
1870 continue
1871 }
1872
1873 // 2 leads in a row
1874 if (codePoint < 0xDC00) {
1875 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
1876 leadSurrogate = codePoint;
1877 continue
1878 }
1879
1880 // valid surrogate pair
1881 codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
1882 } else if (leadSurrogate) {
1883 // valid bmp char, but last char was a lead
1884 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
1885 }
1886
1887 leadSurrogate = null;
1888
1889 // encode utf8
1890 if (codePoint < 0x80) {
1891 if ((units -= 1) < 0) break
1892 bytes.push(codePoint);
1893 } else if (codePoint < 0x800) {
1894 if ((units -= 2) < 0) break
1895 bytes.push(
1896 codePoint >> 0x6 | 0xC0,
1897 codePoint & 0x3F | 0x80
1898 );
1899 } else if (codePoint < 0x10000) {
1900 if ((units -= 3) < 0) break
1901 bytes.push(
1902 codePoint >> 0xC | 0xE0,
1903 codePoint >> 0x6 & 0x3F | 0x80,
1904 codePoint & 0x3F | 0x80
1905 );
1906 } else if (codePoint < 0x110000) {
1907 if ((units -= 4) < 0) break
1908 bytes.push(
1909 codePoint >> 0x12 | 0xF0,
1910 codePoint >> 0xC & 0x3F | 0x80,
1911 codePoint >> 0x6 & 0x3F | 0x80,
1912 codePoint & 0x3F | 0x80
1913 );
1914 } else {
1915 throw new Error('Invalid code point')
1916 }
1917 }
1918
1919 return bytes
1920}
1921
1922function asciiToBytes (str) {
1923 var byteArray = [];
1924 for (var i = 0; i < str.length; ++i) {
1925 // Node's code seems to be doing this and not & 0x7F..
1926 byteArray.push(str.charCodeAt(i) & 0xFF);
1927 }
1928 return byteArray
1929}
1930
1931function utf16leToBytes (str, units) {
1932 var c, hi, lo;
1933 var byteArray = [];
1934 for (var i = 0; i < str.length; ++i) {
1935 if ((units -= 2) < 0) break
1936
1937 c = str.charCodeAt(i);
1938 hi = c >> 8;
1939 lo = c % 256;
1940 byteArray.push(lo);
1941 byteArray.push(hi);
1942 }
1943
1944 return byteArray
1945}
1946
1947
1948function base64ToBytes (str) {
1949 return toByteArray(base64clean(str))
1950}
1951
1952function blitBuffer (src, dst, offset, length) {
1953 for (var i = 0; i < length; ++i) {
1954 if ((i + offset >= dst.length) || (i >= src.length)) break
1955 dst[i + offset] = src[i];
1956 }
1957 return i
1958}
1959
1960function isnan (val) {
1961 return val !== val // eslint-disable-line no-self-compare
1962}
1963
1964
1965// the following is from is-buffer, also by Feross Aboukhadijeh and with same lisence
1966// The _isBuffer check is for Safari 5-7 support, because it's missing
1967// Object.prototype.constructor. Remove this eventually
1968function isBuffer(obj) {
1969 return obj != null && (!!obj._isBuffer || isFastBuffer(obj) || isSlowBuffer(obj))
1970}
1971
1972function isFastBuffer (obj) {
1973 return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
1974}
1975
1976// For Node v0.10 support. Remove this eventually.
1977function isSlowBuffer (obj) {
1978 return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isFastBuffer(obj.slice(0, 0))
1979}
1980
1981export { Buffer, INSPECT_MAX_BYTES, SlowBuffer, isBuffer, _kMaxLength as kMaxLength };
1982export default {Buffer};
\No newline at end of file