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