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