index.js 48 KB

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