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