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