buffer

This is the userscript port for https://github.com/feross/buffer/

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/482519/1297748/buffer.js

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址