protobuf.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. import * as text from './text.js';
  2. const protobuf = {};
  3. protobuf.BinaryReader = class {
  4. static open(data, offset) {
  5. offset = offset || 0;
  6. if (data instanceof Uint8Array) {
  7. return new protobuf.BufferReader(data, offset);
  8. }
  9. if (data.length < 0x20000000) {
  10. data = data.peek();
  11. return new protobuf.BufferReader(data, offset);
  12. }
  13. return new protobuf.StreamReader(data, offset);
  14. }
  15. constructor() {
  16. this._utf8Decoder = new TextDecoder('utf-8');
  17. }
  18. signature() {
  19. const tags = new Map();
  20. this._position = 0;
  21. try {
  22. if (this._length > 0) {
  23. const type = this.byte() & 7;
  24. this.skip(-1);
  25. if (type !== 4 && type !== 6 && type !== 7) {
  26. const length = this.length;
  27. while (this._position < length) {
  28. const tag = this.uint32();
  29. const field = tag >>> 3;
  30. const type = tag & 7;
  31. if (type > 5 || field === 0) {
  32. tags.clear();
  33. break;
  34. }
  35. tags.set(field, type);
  36. if (!this._skipType(type)) {
  37. tags.clear();
  38. break;
  39. }
  40. }
  41. }
  42. }
  43. } catch {
  44. tags.clear();
  45. }
  46. this._position = 0;
  47. return tags;
  48. }
  49. decode() {
  50. let tags = {};
  51. this._position = 0;
  52. try {
  53. const decodeMessage = (max) => {
  54. const length = this._uint32();
  55. if (length === undefined) {
  56. return undefined;
  57. }
  58. if (length === 0) {
  59. // return 2;
  60. }
  61. const end = this.position + length;
  62. if (end > max) {
  63. return undefined;
  64. }
  65. try {
  66. const tags = {};
  67. while (this.position < end) {
  68. const tag = this._uint32();
  69. if (tag === undefined) {
  70. this.seek(end);
  71. return 2;
  72. }
  73. const field = tag >>> 3;
  74. const type = tag & 7;
  75. if (type > 5 || field === 0) {
  76. this.seek(end);
  77. return 2;
  78. }
  79. if (type === 2) {
  80. const type = tags[field];
  81. if (type !== 2) {
  82. const inner = decodeMessage(end);
  83. if (this.position > end) {
  84. this.seek(end);
  85. return 2;
  86. }
  87. if (inner === undefined) {
  88. this.seek(end);
  89. return 2;
  90. }
  91. if (inner === 2) {
  92. tags[field] = inner;
  93. } else if (type) {
  94. for (const [key, value] of Object.entries(inner)) {
  95. if (type[key] === 2 && value !== 2) {
  96. continue;
  97. }
  98. type[key] = value;
  99. }
  100. } else {
  101. tags[field] = inner;
  102. }
  103. continue;
  104. }
  105. }
  106. tags[field] = type;
  107. if (!this._skipType(type)) {
  108. this.seek(end);
  109. return 2;
  110. }
  111. }
  112. if (this.position === end) {
  113. return tags;
  114. }
  115. } catch {
  116. // continue regardless of error
  117. }
  118. this.seek(end);
  119. return 2;
  120. };
  121. if (this._length > 0) {
  122. const type = this.byte() & 7;
  123. this.skip(-1);
  124. if (type !== 4 && type !== 6 && type !== 7) {
  125. const length = this.length;
  126. while (this.position < length) {
  127. const tag = this.uint32();
  128. const field = tag >>> 3;
  129. const type = tag & 7;
  130. if (type > 5 || field === 0) {
  131. tags = {};
  132. break;
  133. }
  134. if (type === 2) {
  135. const type = tags[field];
  136. if (type !== 2) {
  137. const inner = decodeMessage(length);
  138. if (inner === undefined) {
  139. tags = {};
  140. break;
  141. }
  142. if (inner === 2) {
  143. tags[field] = inner;
  144. } else if (type) {
  145. for (const [name, value] of Object.entries(inner)) {
  146. if (type[name] === 2 && value !== 2) {
  147. continue;
  148. }
  149. type[name] = value;
  150. }
  151. } else {
  152. tags[field] = inner;
  153. }
  154. continue;
  155. }
  156. }
  157. tags[field] = type;
  158. if (!this._skipType(type)) {
  159. tags = {};
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. } catch {
  166. tags = {};
  167. }
  168. this._position = 0;
  169. return tags;
  170. }
  171. get length() {
  172. return this._length;
  173. }
  174. get position() {
  175. return this._position;
  176. }
  177. seek(position) {
  178. this._position = position >= 0 ? position : this._length + position;
  179. }
  180. bytes() {
  181. const length = this.uint32();
  182. return this.read(length);
  183. }
  184. string() {
  185. const buffer = this.bytes();
  186. return this._utf8Decoder.decode(buffer);
  187. }
  188. bool() {
  189. return this.uint32() !== 0;
  190. }
  191. uint32() {
  192. let c = this.byte();
  193. let value = (c & 127) >>> 0;
  194. if (c < 128) {
  195. return value;
  196. }
  197. c = this.byte();
  198. value = (value | (c & 127) << 7) >>> 0;
  199. if (c < 128) {
  200. return value;
  201. }
  202. c = this.byte();
  203. value = (value | (c & 127) << 14) >>> 0;
  204. if (c < 128) {
  205. return value;
  206. }
  207. c = this.byte();
  208. value = (value | (c & 127) << 21) >>> 0;
  209. if (c < 128) {
  210. return value;
  211. }
  212. c = this.byte();
  213. value += (c & 127) * 0x10000000;
  214. if (c < 128) {
  215. return value;
  216. }
  217. if (this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 1) {
  218. throw new protobuf.Error('Varint is not 32-bit.');
  219. }
  220. return value;
  221. }
  222. _uint32() {
  223. if (this._position < this._length) {
  224. let c = this.byte();
  225. let value = (c & 127) >>> 0;
  226. if (c < 128) {
  227. return value;
  228. }
  229. if (this._position < this._length) {
  230. c = this.byte();
  231. value = (value | (c & 127) << 7) >>> 0;
  232. if (c < 128) {
  233. return value;
  234. }
  235. if (this._position < this._length) {
  236. c = this.byte();
  237. value = (value | (c & 127) << 14) >>> 0;
  238. if (c < 128) {
  239. return value;
  240. }
  241. if (this._position < this._length) {
  242. c = this.byte();
  243. value = (value | (c & 127) << 21) >>> 0;
  244. if (c < 128) {
  245. return value;
  246. }
  247. if (this._position < this._length) {
  248. c = this.byte();
  249. value += (c & 127) * 0x10000000;
  250. if (c < 128) {
  251. return value;
  252. }
  253. if (this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 1) {
  254. return undefined;
  255. }
  256. return value;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. return undefined;
  263. }
  264. int32() {
  265. return this.uint32() | 0;
  266. }
  267. sint32() {
  268. const value = this.uint32();
  269. return value >>> 1 ^ -(value & 1) | 0;
  270. }
  271. int64() {
  272. return BigInt.asIntN(64, this.varint());
  273. }
  274. uint64() {
  275. return this.varint();
  276. }
  277. sint64() {
  278. const value = this.varint();
  279. return (value >> 1n) ^ (-(value & 1n)); // ZigZag decode
  280. }
  281. array(obj, item, tag) {
  282. if ((tag & 7) === 2) {
  283. const end = this.uint32() + this._position;
  284. while (this._position < end) {
  285. obj.push(item());
  286. }
  287. } else {
  288. obj.push(item());
  289. }
  290. return obj;
  291. }
  292. floats(obj, tag) {
  293. if ((tag & 7) === 2) {
  294. if (obj && obj.length > 0) {
  295. throw new protobuf.Error('Invalid packed float array.');
  296. }
  297. const size = this.uint32();
  298. const end = this._position + size;
  299. if (end > this._length) {
  300. this._unexpected();
  301. }
  302. const length = size >>> 2;
  303. obj = size > 0x100000 ? new Float32Array(length) : new Array(length);
  304. for (let i = 0; i < length; i++) {
  305. obj[i] = this.float();
  306. }
  307. this._position = end;
  308. } else if (obj !== undefined && obj.length <= 0x4000000) {
  309. obj.push(this.float());
  310. } else {
  311. obj = undefined;
  312. this.float();
  313. }
  314. return obj;
  315. }
  316. doubles(obj, tag) {
  317. if ((tag & 7) === 2) {
  318. if (obj && obj.length > 0) {
  319. throw new protobuf.Error('Invalid packed float array.');
  320. }
  321. const size = this.uint32();
  322. const end = this._position + size;
  323. if (end > this._length) {
  324. this._unexpected();
  325. }
  326. const length = size >>> 3;
  327. obj = size > 1048576 ? new Float64Array(length) : new Array(length);
  328. for (let i = 0; i < length; i++) {
  329. obj[i] = this.double();
  330. }
  331. this._position = end;
  332. } else if (obj !== undefined && obj.length < 1000000) {
  333. obj.push(this.double());
  334. } else {
  335. obj = undefined;
  336. this.double();
  337. }
  338. return obj;
  339. }
  340. skip(offset) {
  341. this._position += offset;
  342. if (this._position > this._length) {
  343. this._unexpected();
  344. }
  345. }
  346. skipType(type) {
  347. switch (type) {
  348. case 0:
  349. this.skipVarint();
  350. break;
  351. case 1:
  352. this.skip(8);
  353. break;
  354. case 2:
  355. this.skip(this.uint32());
  356. break;
  357. case 3:
  358. while ((type = this.uint32() & 7) !== 4) {
  359. this.skipType(type);
  360. }
  361. break;
  362. case 5:
  363. this.skip(4);
  364. break;
  365. default:
  366. throw new protobuf.Error(`Invalid type '${type}' at offset ${this._position}.`);
  367. }
  368. }
  369. _skipType(type) {
  370. switch (type) {
  371. case 0: {
  372. // const max = this._position + 9;
  373. do {
  374. if (this._position >= this._length /* || this._position > max */) {
  375. return false;
  376. }
  377. }
  378. while (this.byte() & 128);
  379. break;
  380. }
  381. case 1: {
  382. if (this._position + 8 >= this._length) {
  383. return false;
  384. }
  385. this._position += 8;
  386. break;
  387. }
  388. case 2: {
  389. const length = this.uint32();
  390. if (length === undefined) {
  391. return false;
  392. }
  393. if (this._position + length > this._end) {
  394. return false;
  395. }
  396. this._position += length;
  397. break;
  398. }
  399. case 3: {
  400. for (;;) {
  401. const tag = this.uint32();
  402. if (tag === undefined) {
  403. return false;
  404. }
  405. const wireType = tag & 7;
  406. if (wireType === 4) {
  407. break;
  408. }
  409. if (!this._skipType(wireType)) {
  410. return false;
  411. }
  412. }
  413. break;
  414. }
  415. case 5: {
  416. this._position += 4;
  417. if (this._position > this._length) {
  418. return false;
  419. }
  420. break;
  421. }
  422. default: {
  423. return false;
  424. }
  425. }
  426. return true;
  427. }
  428. entry(obj, key, value) {
  429. this.skipVarint();
  430. this.skip(1);
  431. let k = key();
  432. if (!Number.isInteger(k) && typeof k !== 'string') {
  433. k = Number(k);
  434. }
  435. this.skip(1);
  436. const v = value();
  437. obj[k] = v;
  438. }
  439. _unexpected() {
  440. throw new RangeError('Unexpected end of file.');
  441. }
  442. };
  443. protobuf.BufferReader = class extends protobuf.BinaryReader {
  444. constructor(buffer, offset) {
  445. super();
  446. this._buffer = buffer;
  447. this._length = buffer.length;
  448. this._position = offset || 0;
  449. this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  450. }
  451. skipVarint() {
  452. do {
  453. if (this._position >= this._length) {
  454. this._unexpected();
  455. }
  456. }
  457. while (this._buffer[this._position++] & 128);
  458. }
  459. read(length) {
  460. const position = this._position;
  461. this.skip(length);
  462. return this._buffer.slice(position, this._position);
  463. }
  464. byte() {
  465. if (this._position < this._length) {
  466. return this._buffer[this._position++];
  467. }
  468. throw new RangeError('Unexpected end of file.');
  469. }
  470. fixed64() {
  471. const position = this._position;
  472. this.skip(8);
  473. return this._view.getBigUint64(position, true);
  474. }
  475. sfixed64() {
  476. const position = this._position;
  477. this.skip(8);
  478. return this._view.getBigInt64(position, true);
  479. }
  480. fixed32() {
  481. const position = this._position;
  482. this.skip(4);
  483. return this._view.getUint32(position, true);
  484. }
  485. sfixed32() {
  486. const position = this._position;
  487. this.skip(4);
  488. return this._view.getInt32(position, true);
  489. }
  490. float() {
  491. const position = this._position;
  492. this.skip(4);
  493. return this._view.getFloat32(position, true);
  494. }
  495. double() {
  496. const position = this._position;
  497. this.skip(8);
  498. return this._view.getFloat64(position, true);
  499. }
  500. varint() {
  501. let value = 0n;
  502. let shift = 0n;
  503. for (let i = 0; i < 10 && this._position < this._length; i++) {
  504. const byte = this._buffer[this._position++];
  505. value |= BigInt(byte & 0x7F) << shift;
  506. if ((byte & 0x80) === 0) {
  507. return value;
  508. }
  509. shift += 7n;
  510. }
  511. throw new protobuf.Error('Invalid varint value.');
  512. }
  513. };
  514. protobuf.StreamReader = class extends protobuf.BinaryReader {
  515. constructor(stream, offset) {
  516. super(new Uint8Array(0));
  517. this._stream = stream;
  518. this._length = stream.length;
  519. this.seek(offset || 0);
  520. }
  521. skipVarint() {
  522. do {
  523. if (this._position >= this._length) {
  524. this._unexpected();
  525. }
  526. }
  527. while (this.byte() & 128);
  528. }
  529. read(length) {
  530. this._stream.seek(this._position);
  531. this.skip(length);
  532. return this._stream.read(length);
  533. }
  534. byte() {
  535. const position = this._fill(1);
  536. return this._view.getUint8(position);
  537. }
  538. fixed64() {
  539. const position = this._fill(8);
  540. return this._view.getBigUint64(position, true);
  541. }
  542. sfixed64() {
  543. const position = this._fill(8);
  544. return this._view.getBigInt64(position, true);
  545. }
  546. fixed32() {
  547. const position = this._fill(4);
  548. return this._view.getUint32(position, true);
  549. }
  550. sfixed32() {
  551. const position = this._fill(4);
  552. return this._view.getInt32(position, true);
  553. }
  554. float() {
  555. const position = this._fill(4);
  556. return this._view.getFloat32(position, true);
  557. }
  558. double() {
  559. const position = this._fill(8);
  560. return this._view.getFloat64(position, true);
  561. }
  562. varint() {
  563. let value = 0n;
  564. let shift = 0n;
  565. for (let i = 0; i < 10 && this._position < this._length; i++) {
  566. const byte = this.byte();
  567. value |= BigInt(byte & 0x7F) << shift;
  568. if ((byte & 0x80) === 0) {
  569. return value;
  570. }
  571. shift += 7n;
  572. }
  573. throw new protobuf.Error('Invalid varint value.');
  574. }
  575. _fill(length) {
  576. if (this._position + length > this._length) {
  577. throw new Error(`Expected ${this._position + length - this._length} more bytes. The file might be corrupted. Unexpected end of file.`);
  578. }
  579. if (!this._buffer || this._position < this._offset || this._position + length > this._offset + this._buffer.length) {
  580. this._offset = this._position;
  581. this._stream.seek(this._offset);
  582. const size = Math.min(0x10000000, this._length - this._offset);
  583. this._buffer = this._stream.read(size);
  584. this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);
  585. }
  586. const position = this._position;
  587. this._position += length;
  588. return position - this._offset;
  589. }
  590. };
  591. protobuf.TextReader = class {
  592. static open(data) {
  593. if (data) {
  594. const buffer = data instanceof Uint8Array ? data : data.peek();
  595. const decoder = text.Decoder.open(buffer);
  596. let first = true;
  597. for (let i = 0; i < 0x100; i++) {
  598. const c = decoder.decode();
  599. if (c === undefined) {
  600. if (i === 0) {
  601. return null;
  602. }
  603. break;
  604. }
  605. if (c === '\0') {
  606. return null;
  607. }
  608. const whitespace = c === ' ' || c === '\n' || c === '\r' || c === '\t';
  609. if (c < ' ' && !whitespace) {
  610. return null;
  611. }
  612. if (first && !whitespace) {
  613. first = false;
  614. if (c === '#') {
  615. let c = '';
  616. do {
  617. c = decoder.decode();
  618. }
  619. while (c !== undefined && c !== '\n');
  620. if (c === undefined) {
  621. break;
  622. }
  623. continue;
  624. }
  625. if (c === '[') {
  626. continue;
  627. }
  628. if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
  629. continue;
  630. }
  631. return null;
  632. }
  633. }
  634. return new protobuf.TextReader(buffer);
  635. }
  636. return null;
  637. }
  638. constructor(buffer) {
  639. this._decoder = text.Decoder.open(buffer);
  640. this.reset();
  641. }
  642. signature() {
  643. const tags = new Map();
  644. this.reset();
  645. try {
  646. this.start(false);
  647. while (!this.end()) {
  648. const tag = this.tag();
  649. if (this.token() === '{') {
  650. this.start();
  651. tags.set(tag, true);
  652. while (!this.end()) {
  653. const subtag = this.tag();
  654. tags.set(`${tag}.${subtag}`, true);
  655. this.skip();
  656. this.match(',');
  657. }
  658. } else {
  659. this.skip();
  660. tags.set(tag, true);
  661. }
  662. }
  663. } catch {
  664. if (tags.has('[')) {
  665. tags.clear();
  666. }
  667. }
  668. this.reset();
  669. return tags;
  670. }
  671. reset() {
  672. this._decoder.position = 0;
  673. this._position = 0;
  674. this._token = undefined;
  675. this._depth = 0;
  676. this._arrayDepth = 0;
  677. this._token = '';
  678. this.next();
  679. }
  680. start() {
  681. if (this._depth > 0) {
  682. this.expect('{');
  683. }
  684. this._depth++;
  685. }
  686. end() {
  687. if (this._depth <= 0) {
  688. throw new protobuf.Error(`Invalid depth ${this.location()}`);
  689. }
  690. if (this._token === '}') {
  691. this.expect('}');
  692. this.match(';');
  693. this._depth--;
  694. return true;
  695. }
  696. if (this._token === undefined) {
  697. if (this._depth !== 1) {
  698. throw new protobuf.Error(`Unexpected end of input ${this.location()}`);
  699. }
  700. this._depth--;
  701. return true;
  702. }
  703. return false;
  704. }
  705. tag() {
  706. const name = this._token;
  707. this.next();
  708. if (this._token !== '[' && this._token !== '{') {
  709. this.expect(':');
  710. }
  711. return name;
  712. }
  713. integer() {
  714. const token = this._token;
  715. const value = Number.parseInt(token, 10);
  716. if (Number.isNaN(token - value)) {
  717. throw new protobuf.Error(`Couldn't parse integer '${token}' ${this.location()}`);
  718. }
  719. this.next();
  720. this.semicolon();
  721. return value;
  722. }
  723. double() {
  724. let value = 0;
  725. let token = this._token;
  726. switch (token) {
  727. case 'nan': value = NaN; break;
  728. case 'inf': value = Infinity; break;
  729. case '-inf': value = -Infinity; break;
  730. default:
  731. if (token.endsWith('f')) {
  732. token = token.substring(0, token.length - 1);
  733. }
  734. value = Number.parseFloat(token);
  735. if (Number.isNaN(token - value)) {
  736. throw new protobuf.Error(`Couldn't parse float '${token}' ${this.location()}`);
  737. }
  738. break;
  739. }
  740. this.next();
  741. this.semicolon();
  742. return value;
  743. }
  744. float() {
  745. return this.double();
  746. }
  747. uint32() {
  748. return this.integer();
  749. }
  750. int32() {
  751. return this.integer();
  752. }
  753. sint32() {
  754. return this.integer();
  755. }
  756. int64() {
  757. return BigInt(this.integer());
  758. }
  759. uint64() {
  760. return BigInt.asUintN(64, BigInt(this.integer()));
  761. }
  762. sint64() {
  763. return BigInt(this.integer());
  764. }
  765. fixed64() {
  766. return BigInt.asUintN(64, BigInt(this.integer()));
  767. }
  768. sfixed64() {
  769. return BigInt(this.integer());
  770. }
  771. fixed32() {
  772. return this.integer();
  773. }
  774. sfixed32() {
  775. return this.integer();
  776. }
  777. string() {
  778. const token = this._token;
  779. if (token.length < 2) {
  780. throw new protobuf.Error(`String is too short ${this.location()}`);
  781. }
  782. const quote = token.substring(0, 1);
  783. if (quote !== "'" && quote !== '"') {
  784. throw new protobuf.Error(`String is not in quotes ${this.location()}`);
  785. }
  786. if (quote !== token.substring(token.length - 1)) {
  787. throw new protobuf.Error(`String quotes do not match ${this.location()}`);
  788. }
  789. const value = token.substring(1, token.length - 1);
  790. this.next();
  791. this.semicolon();
  792. return value;
  793. }
  794. bool() {
  795. const token = this._token;
  796. switch (token) {
  797. case 'true':
  798. case 'True':
  799. case '1':
  800. this.next();
  801. this.semicolon();
  802. return true;
  803. case 'false':
  804. case 'False':
  805. case '0':
  806. this.next();
  807. this.semicolon();
  808. return false;
  809. default:
  810. throw new protobuf.Error(`Couldn't parse boolean '${token}' ${this.location()}`);
  811. }
  812. }
  813. bytes() {
  814. const token = this.string();
  815. const length = token.length;
  816. const array = new Uint8Array(length);
  817. for (let i = 0; i < length; i++) {
  818. array[i] = token.charCodeAt(i);
  819. }
  820. return array;
  821. }
  822. enum(type) {
  823. const token = this._token;
  824. let value = 0;
  825. if (Object.prototype.hasOwnProperty.call(type, token)) {
  826. value = type[token];
  827. } else {
  828. value = Number.parseInt(token, 10);
  829. if (Number.isNaN(token - value)) {
  830. throw new protobuf.Error(`Couldn't parse enum '${token === undefined ? '' : token}' ${this.location()}`);
  831. }
  832. }
  833. this.next();
  834. this.semicolon();
  835. return value;
  836. }
  837. any(type) {
  838. this.start();
  839. const message = type();
  840. if (this._token.startsWith('[') && this._token.endsWith(']')) {
  841. message.type_url = this._token.substring(1, this._token.length - 1).trim();
  842. this.next();
  843. this.match(':');
  844. message.value = this.read();
  845. this.match(';');
  846. if (!this.end()) {
  847. this.expect('}');
  848. }
  849. } else {
  850. while (!this.end()) {
  851. const tag = this.tag();
  852. switch (tag) {
  853. case "type_url":
  854. message.type_url = this.string();
  855. break;
  856. case "value":
  857. message.value = this.bytes();
  858. break;
  859. default:
  860. this.field(tag, message);
  861. break;
  862. }
  863. }
  864. }
  865. return message;
  866. }
  867. anyarray(obj, type) {
  868. this.start();
  869. if (this._token.startsWith('[') && this._token.endsWith(']')) {
  870. while (!this.end()) {
  871. if (this._token.startsWith('[') && this._token.endsWith(']')) {
  872. const message = type();
  873. message.type_url = this._token.substring(1, this._token.length - 1).trim();
  874. this.next();
  875. this.match(':');
  876. message.value = this.read();
  877. this.match(';');
  878. obj.push(message);
  879. continue;
  880. }
  881. this.expect('[');
  882. }
  883. } else {
  884. const message = type();
  885. while (!this.end()) {
  886. const tag = this.tag();
  887. switch (tag) {
  888. case "type_url":
  889. message.type_url = this.string();
  890. break;
  891. case "value":
  892. message.value = this.bytes();
  893. break;
  894. default:
  895. this.field(tag, message);
  896. break;
  897. }
  898. }
  899. obj.push(message);
  900. }
  901. }
  902. entry(obj, key, value) {
  903. this.start();
  904. let k = '';
  905. let v = null;
  906. while (!this.end()) {
  907. const tag = this.tag();
  908. switch (tag) {
  909. case 'key':
  910. k = key();
  911. break;
  912. case 'value':
  913. v = value();
  914. break;
  915. default:
  916. throw new protobuf.Error(`Unsupported entry tag '${tag}'.`);
  917. }
  918. }
  919. obj[k] = v;
  920. }
  921. array(obj, item) {
  922. if (this.first()) {
  923. while (!this.last()) {
  924. obj.push(item());
  925. switch (this._token) {
  926. case ',':
  927. this.next();
  928. break;
  929. case ']':
  930. break;
  931. default:
  932. this.handle(this._token);
  933. break;
  934. }
  935. }
  936. } else {
  937. obj.push(item());
  938. }
  939. }
  940. first() {
  941. if (this.match('[')) {
  942. this._arrayDepth++;
  943. return true;
  944. }
  945. return false;
  946. }
  947. last() {
  948. if (this.match(']')) {
  949. this._arrayDepth--;
  950. return true;
  951. }
  952. return false;
  953. }
  954. read() {
  955. const start = this._position;
  956. this.skip();
  957. const end = this._position;
  958. const position = this._decoder.position;
  959. this._decoder.position = start;
  960. let content = '';
  961. while (this._decoder.position < end) {
  962. content += this._decoder.decode();
  963. }
  964. this._decoder.position = position;
  965. return content;
  966. }
  967. skip() {
  968. switch (this._token) {
  969. case '{': {
  970. const depth = this._depth;
  971. this.start();
  972. while (!this.end() || depth < this._depth) {
  973. if (this._token === '{') {
  974. this.start();
  975. } else if (this._token !== '}') {
  976. this.next();
  977. this.match(';');
  978. }
  979. }
  980. break;
  981. }
  982. case '[': {
  983. const depth = this._arrayDepth;
  984. this.first();
  985. while (!this.last() || depth < this._arrayDepth) {
  986. this.next();
  987. if (this._token === '[') {
  988. this.first();
  989. } else if (this._token === undefined) {
  990. this.handle(this._token);
  991. }
  992. }
  993. break;
  994. }
  995. default: {
  996. this.next();
  997. this.semicolon();
  998. break;
  999. }
  1000. }
  1001. }
  1002. handle(token) {
  1003. throw new protobuf.Error(`Unexpected token '${token}' ${this.location()}`);
  1004. }
  1005. field(token /*, module */) {
  1006. throw new protobuf.Error(`Unsupported field '${token}' ${this.location()}`);
  1007. }
  1008. token() {
  1009. return this._token;
  1010. }
  1011. next() {
  1012. if (this._token === undefined) {
  1013. throw new protobuf.Error(`Unexpected end of input ${this.location()}`);
  1014. }
  1015. this._position = this._decoder.position;
  1016. let c = this._decoder.decode();
  1017. for (;;) {
  1018. switch (c) {
  1019. case ' ':
  1020. case '\n':
  1021. case '\r':
  1022. case '\t':
  1023. this._position = this._decoder.position;
  1024. c = this._decoder.decode();
  1025. continue;
  1026. case '#':
  1027. do {
  1028. c = this._decoder.decode();
  1029. if (c === undefined) {
  1030. this._token = undefined;
  1031. return;
  1032. }
  1033. }
  1034. while (c !== '\n');
  1035. this._position = this._decoder.position;
  1036. c = this._decoder.decode();
  1037. continue;
  1038. default:
  1039. break;
  1040. }
  1041. break;
  1042. }
  1043. if (c === undefined) {
  1044. this._token = undefined;
  1045. return;
  1046. }
  1047. if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c === '_' || c === '$') {
  1048. let token = c;
  1049. let position = this._decoder.position;
  1050. for (;;) {
  1051. c = this._decoder.decode();
  1052. if (c === undefined || c === '\n') {
  1053. break;
  1054. }
  1055. if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c === '_' || c === '+' || c === '-') {
  1056. token += c;
  1057. position = this._decoder.position;
  1058. continue;
  1059. }
  1060. break;
  1061. }
  1062. this._decoder.position = position;
  1063. this._token = token;
  1064. return;
  1065. }
  1066. switch (c) {
  1067. case '{':
  1068. case '}':
  1069. case ':':
  1070. case ',':
  1071. case ']':
  1072. case ';':
  1073. this._token = c;
  1074. return;
  1075. case '[': {
  1076. let token = c;
  1077. let position = this._decoder.position;
  1078. let x = this._decoder.decode();
  1079. if ((x !== undefined) && x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z') {
  1080. token += x;
  1081. for (;;) {
  1082. x = this._decoder.decode();
  1083. if (x === undefined || x === '\n') {
  1084. break;
  1085. }
  1086. if (x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z' || x >= '0' && x <= '9' || x === '.' || x === '/') {
  1087. token += x;
  1088. position = this._decoder.position;
  1089. continue;
  1090. }
  1091. if (x === ']') {
  1092. this._token = token + x;
  1093. return;
  1094. }
  1095. }
  1096. }
  1097. this._decoder.position = position;
  1098. this._token = '[';
  1099. return;
  1100. }
  1101. case '"':
  1102. case "'": {
  1103. const quote = c;
  1104. let content = c;
  1105. for (;;) {
  1106. c = this._decoder.decode();
  1107. if (c === undefined || c === '\n') {
  1108. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1109. }
  1110. if (c === '\\') {
  1111. c = this._decoder.decode();
  1112. if (c === undefined || c === '\n') {
  1113. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1114. }
  1115. switch (c) {
  1116. case '\\': c = '\\'; break;
  1117. case "'": c = "'"; break;
  1118. case '"': c = '"'; break;
  1119. case 'r': c = '\r'; break;
  1120. case 'n': c = '\n'; break;
  1121. case 't': c = '\t'; break;
  1122. case 'b': c = '\b'; break;
  1123. case 'x':
  1124. case 'X': {
  1125. let value = 0;
  1126. for (let xi = 0; xi < 2; xi++) {
  1127. let c = this._decoder.decode();
  1128. if (c === undefined) {
  1129. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1130. }
  1131. c = c.charCodeAt(0);
  1132. if (c >= 65 && c <= 70) {
  1133. c -= 55;
  1134. } else if (c >= 97 && c <= 102) {
  1135. c -= 87;
  1136. } else if (c >= 48 && c <= 57) {
  1137. c -= 48;
  1138. } else {
  1139. c = -1;
  1140. }
  1141. if (c === -1) {
  1142. throw new protobuf.Error(`Unexpected hex digit '${c}' in bytes string ${this.location()}`);
  1143. }
  1144. value = value << 4 | c;
  1145. }
  1146. c = String.fromCharCode(value);
  1147. break;
  1148. }
  1149. default: {
  1150. if (c < '0' || c > '9') {
  1151. throw new protobuf.Error(`Unexpected character '${c}' in string ${this.location()}`);
  1152. }
  1153. let value = 0;
  1154. let od = c;
  1155. if (od < '0' || od > '9') {
  1156. throw new protobuf.Error(`Unexpected octal digit '${od}' in bytes string ${this.location()}`);
  1157. }
  1158. od = od.charCodeAt(0);
  1159. value = value << 3 | od - 48;
  1160. od = this._decoder.decode();
  1161. if (od === undefined) {
  1162. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1163. }
  1164. if (od < '0' || od > '9') {
  1165. throw new protobuf.Error(`Unexpected octal digit '${od}' in bytes string ${this.location()}`);
  1166. }
  1167. od = od.charCodeAt(0);
  1168. value = value << 3 | od - 48;
  1169. od = this._decoder.decode();
  1170. if (od === undefined) {
  1171. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1172. }
  1173. if (od < '0' || od > '9') {
  1174. throw new protobuf.Error(`Unexpected octal digit '${od}' in bytes string ${this.location()}`);
  1175. }
  1176. od = od.charCodeAt(0);
  1177. value = value << 3 | od - 48;
  1178. c = String.fromCharCode(value);
  1179. break;
  1180. }
  1181. }
  1182. content += c;
  1183. continue;
  1184. } else {
  1185. content += c;
  1186. if (c === quote) {
  1187. break;
  1188. }
  1189. }
  1190. }
  1191. this._token = content;
  1192. return;
  1193. }
  1194. case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  1195. case '-': case '+': case '.': {
  1196. let token = c;
  1197. let position = this._decoder.position;
  1198. for (;;) {
  1199. c = this._decoder.decode();
  1200. if (c === undefined || c === '\n') {
  1201. break;
  1202. }
  1203. if ((c >= '0' && c <= '9') || c === '_' || c === '+' || c === '-' || c === '.' || c === 'e' || c === 'E') {
  1204. token += c;
  1205. position = this._decoder.position;
  1206. continue;
  1207. }
  1208. break;
  1209. }
  1210. if (token === '-' && c === 'i' && this._decoder.decode() === 'n' && this._decoder.decode() === 'f') {
  1211. token = '-inf';
  1212. position = this._decoder.position;
  1213. }
  1214. if (token === '-' || token === '+' || token === '.') {
  1215. throw new protobuf.Error(`Unexpected token '${token}' ${this.location()}`);
  1216. }
  1217. this._decoder.position = position;
  1218. this._token = token;
  1219. return;
  1220. }
  1221. default: {
  1222. throw new protobuf.Error(`Unexpected token '${c}' ${this.location()}`);
  1223. }
  1224. }
  1225. }
  1226. expect(value) {
  1227. if (this._token !== value) {
  1228. throw new protobuf.Error(`Unexpected '${this._token}' instead of '${value}' ${this.location()}`);
  1229. }
  1230. this.next();
  1231. }
  1232. match(value) {
  1233. if (value === this._token) {
  1234. this.next();
  1235. return true;
  1236. }
  1237. return false;
  1238. }
  1239. location() {
  1240. let line = 1;
  1241. let column = 1;
  1242. this._decoder.position = 0;
  1243. let c = '';
  1244. do {
  1245. if (this._decoder.position === this._position) {
  1246. return `at ${line}:${column}.`;
  1247. }
  1248. c = this._decoder.decode();
  1249. if (c === '\n') {
  1250. line++;
  1251. column = 1;
  1252. } else {
  1253. column++;
  1254. }
  1255. }
  1256. while (c !== undefined);
  1257. return `at ${line}:${column}.`;
  1258. }
  1259. semicolon() {
  1260. if (this._arrayDepth === 0) {
  1261. this.match(';');
  1262. }
  1263. }
  1264. };
  1265. protobuf.Error = class extends Error {
  1266. constructor(message) {
  1267. super(message);
  1268. this.name = 'Protocol Buffers Error';
  1269. this.message = message;
  1270. }
  1271. };
  1272. export const BinaryReader = protobuf.BinaryReader;
  1273. export const TextReader = protobuf.TextReader;