buffer.js 68 KB

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