index.js 48 KB

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