index.js 49 KB

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