Buffer.js 59 KB

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