index.js 47 KB

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