protobuf.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. const position = this._position + 8;
  383. if (position > this._length) {
  384. return false;
  385. }
  386. this._position = position;
  387. break;
  388. }
  389. case 2: {
  390. const length = this.uint32();
  391. if (length === undefined) {
  392. return false;
  393. }
  394. const position = this._position + length;
  395. if (position > this._end) {
  396. return false;
  397. }
  398. this._position = position;
  399. break;
  400. }
  401. case 3: {
  402. for (;;) {
  403. const tag = this.uint32();
  404. if (tag === undefined) {
  405. return false;
  406. }
  407. const wireType = tag & 7;
  408. if (wireType === 4) {
  409. break;
  410. }
  411. if (!this._skipType(wireType)) {
  412. return false;
  413. }
  414. }
  415. break;
  416. }
  417. case 5: {
  418. const position = this._position + 4;
  419. if (position > this._length) {
  420. return false;
  421. }
  422. this._position = position;
  423. break;
  424. }
  425. default: {
  426. return false;
  427. }
  428. }
  429. return true;
  430. }
  431. entry(obj, key, value) {
  432. this.skipVarint();
  433. this.skip(1);
  434. let k = key();
  435. if (!Number.isInteger(k) && typeof k !== 'string') {
  436. k = Number(k);
  437. }
  438. this.skip(1);
  439. const v = value();
  440. obj[k] = v;
  441. }
  442. _unexpected() {
  443. throw new RangeError('Unexpected end of file.');
  444. }
  445. };
  446. protobuf.BufferReader = class extends protobuf.BinaryReader {
  447. constructor(buffer, offset = 0) {
  448. super();
  449. this._buffer = buffer;
  450. this._length = buffer.length;
  451. this._position = offset;
  452. this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  453. }
  454. skipVarint() {
  455. do {
  456. if (this._position >= this._length) {
  457. this._unexpected();
  458. }
  459. }
  460. while (this._buffer[this._position++] & 128);
  461. }
  462. read(length) {
  463. const position = this._position;
  464. this.skip(length);
  465. return this._buffer.slice(position, this._position);
  466. }
  467. byte() {
  468. if (this._position < this._length) {
  469. return this._buffer[this._position++];
  470. }
  471. throw new RangeError('Unexpected end of file.');
  472. }
  473. fixed64() {
  474. const position = this._position;
  475. this.skip(8);
  476. return this._view.getBigUint64(position, true);
  477. }
  478. sfixed64() {
  479. const position = this._position;
  480. this.skip(8);
  481. return this._view.getBigInt64(position, true);
  482. }
  483. fixed32() {
  484. const position = this._position;
  485. this.skip(4);
  486. return this._view.getUint32(position, true);
  487. }
  488. sfixed32() {
  489. const position = this._position;
  490. this.skip(4);
  491. return this._view.getInt32(position, true);
  492. }
  493. float() {
  494. const position = this._position;
  495. this.skip(4);
  496. return this._view.getFloat32(position, true);
  497. }
  498. double() {
  499. const position = this._position;
  500. this.skip(8);
  501. return this._view.getFloat64(position, true);
  502. }
  503. varint() {
  504. let value = 0n;
  505. let shift = 0n;
  506. for (let i = 0; i < 10 && this._position < this._length; i++) {
  507. const byte = this._buffer[this._position++];
  508. value |= BigInt(byte & 0x7F) << shift;
  509. if ((byte & 0x80) === 0) {
  510. return value;
  511. }
  512. shift += 7n;
  513. }
  514. throw new protobuf.Error('Invalid varint value.');
  515. }
  516. };
  517. protobuf.StreamReader = class extends protobuf.BinaryReader {
  518. constructor(stream, offset) {
  519. super(new Uint8Array(0));
  520. this._stream = stream;
  521. this._length = stream.length;
  522. this.seek(offset || 0);
  523. }
  524. skipVarint() {
  525. do {
  526. if (this._position >= this._length) {
  527. this._unexpected();
  528. }
  529. }
  530. while (this.byte() & 128);
  531. }
  532. read(length) {
  533. this._stream.seek(this._position);
  534. this.skip(length);
  535. return this._stream.read(length);
  536. }
  537. byte() {
  538. const position = this._fill(1);
  539. return this._view.getUint8(position);
  540. }
  541. fixed64() {
  542. const position = this._fill(8);
  543. return this._view.getBigUint64(position, true);
  544. }
  545. sfixed64() {
  546. const position = this._fill(8);
  547. return this._view.getBigInt64(position, true);
  548. }
  549. fixed32() {
  550. const position = this._fill(4);
  551. return this._view.getUint32(position, true);
  552. }
  553. sfixed32() {
  554. const position = this._fill(4);
  555. return this._view.getInt32(position, true);
  556. }
  557. float() {
  558. const position = this._fill(4);
  559. return this._view.getFloat32(position, true);
  560. }
  561. double() {
  562. const position = this._fill(8);
  563. return this._view.getFloat64(position, true);
  564. }
  565. varint() {
  566. let value = 0n;
  567. let shift = 0n;
  568. for (let i = 0; i < 10 && this._position < this._length; i++) {
  569. const byte = this.byte();
  570. value |= BigInt(byte & 0x7F) << shift;
  571. if ((byte & 0x80) === 0) {
  572. return value;
  573. }
  574. shift += 7n;
  575. }
  576. throw new protobuf.Error('Invalid varint value.');
  577. }
  578. _fill(length) {
  579. if (this._position + length > this._length) {
  580. throw new Error(`Expected ${this._position + length - this._length} more bytes. The file might be corrupted. Unexpected end of file.`);
  581. }
  582. if (!this._buffer || this._position < this._offset || this._position + length > this._offset + this._buffer.length) {
  583. this._offset = this._position;
  584. this._stream.seek(this._offset);
  585. const size = Math.min(0x10000000, this._length - this._offset);
  586. this._buffer = this._stream.read(size);
  587. this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);
  588. }
  589. const position = this._position;
  590. this._position += length;
  591. return position - this._offset;
  592. }
  593. };
  594. protobuf.TextReader = class {
  595. static open(data) {
  596. if (data) {
  597. const buffer = data instanceof Uint8Array ? data : data.peek();
  598. const decoder = text.Decoder.open(buffer);
  599. let first = true;
  600. for (let i = 0; i < 0x100; i++) {
  601. const c = decoder.decode();
  602. if (c === undefined) {
  603. if (i === 0) {
  604. return null;
  605. }
  606. break;
  607. }
  608. if (c === '\0') {
  609. return null;
  610. }
  611. const whitespace = c === ' ' || c === '\n' || c === '\r' || c === '\t';
  612. if (c < ' ' && !whitespace) {
  613. return null;
  614. }
  615. if (first && !whitespace) {
  616. first = false;
  617. if (c === '#') {
  618. let c = '';
  619. do {
  620. c = decoder.decode();
  621. }
  622. while (c !== undefined && c !== '\n');
  623. if (c === undefined) {
  624. break;
  625. }
  626. continue;
  627. }
  628. if (c === '[') {
  629. continue;
  630. }
  631. if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
  632. continue;
  633. }
  634. return null;
  635. }
  636. }
  637. return new protobuf.TextReader(buffer);
  638. }
  639. return null;
  640. }
  641. constructor(buffer) {
  642. this._decoder = text.Decoder.open(buffer);
  643. this.reset();
  644. }
  645. signature() {
  646. const tags = new Map();
  647. this.reset();
  648. try {
  649. this.start(false);
  650. while (!this.end()) {
  651. const tag = this.tag();
  652. if (this.token() === '{') {
  653. this.start();
  654. tags.set(tag, true);
  655. while (!this.end()) {
  656. const subtag = this.tag();
  657. tags.set(`${tag}.${subtag}`, true);
  658. this.skip();
  659. this.match(',');
  660. }
  661. } else {
  662. this.skip();
  663. tags.set(tag, true);
  664. }
  665. }
  666. } catch {
  667. if (tags.has('[')) {
  668. tags.clear();
  669. }
  670. }
  671. this.reset();
  672. return tags;
  673. }
  674. reset() {
  675. this._decoder.position = 0;
  676. this._position = 0;
  677. this._depth = 0;
  678. this._arrayDepth = 0;
  679. this._token = '';
  680. this.next();
  681. }
  682. start() {
  683. if (this._depth > 0) {
  684. this.expect('{');
  685. }
  686. this._depth++;
  687. }
  688. end() {
  689. if (this._depth <= 0) {
  690. throw new protobuf.Error(`Invalid depth ${this.location()}`);
  691. }
  692. if (this._token === '}') {
  693. this.expect('}');
  694. this.match(';');
  695. this._depth--;
  696. return true;
  697. }
  698. if (this._token === undefined) {
  699. if (this._depth !== 1) {
  700. throw new protobuf.Error(`Unexpected end of input ${this.location()}`);
  701. }
  702. this._depth--;
  703. return true;
  704. }
  705. return false;
  706. }
  707. tag() {
  708. const name = this._token;
  709. this.next();
  710. if (this._token !== '[' && this._token !== '{') {
  711. this.expect(':');
  712. }
  713. return name;
  714. }
  715. integer() {
  716. const token = this._token;
  717. const value = Number.parseInt(token, 10);
  718. if (Number.isNaN(token - value)) {
  719. throw new protobuf.Error(`Couldn't parse integer '${token}' ${this.location()}`);
  720. }
  721. this.next();
  722. this.semicolon();
  723. return value;
  724. }
  725. double() {
  726. let value = 0;
  727. let token = this._token;
  728. switch (token) {
  729. case 'nan': value = NaN; break;
  730. case 'inf': value = Infinity; break;
  731. case '-inf': value = -Infinity; break;
  732. default:
  733. if (token.endsWith('f')) {
  734. token = token.substring(0, token.length - 1);
  735. }
  736. value = Number.parseFloat(token);
  737. if (Number.isNaN(token - value)) {
  738. throw new protobuf.Error(`Couldn't parse float '${token}' ${this.location()}`);
  739. }
  740. break;
  741. }
  742. this.next();
  743. this.semicolon();
  744. return value;
  745. }
  746. float() {
  747. return this.double();
  748. }
  749. uint32() {
  750. return this.integer();
  751. }
  752. int32() {
  753. return this.integer();
  754. }
  755. sint32() {
  756. return this.integer();
  757. }
  758. int64() {
  759. return BigInt(this.integer());
  760. }
  761. uint64() {
  762. return BigInt.asUintN(64, BigInt(this.integer()));
  763. }
  764. sint64() {
  765. return BigInt(this.integer());
  766. }
  767. fixed64() {
  768. return BigInt.asUintN(64, BigInt(this.integer()));
  769. }
  770. sfixed64() {
  771. return BigInt(this.integer());
  772. }
  773. fixed32() {
  774. return this.integer();
  775. }
  776. sfixed32() {
  777. return this.integer();
  778. }
  779. string() {
  780. const token = this._token;
  781. if (token.length < 2) {
  782. throw new protobuf.Error(`String is too short ${this.location()}`);
  783. }
  784. const quote = token.substring(0, 1);
  785. if (quote !== "'" && quote !== '"') {
  786. throw new protobuf.Error(`String is not in quotes ${this.location()}`);
  787. }
  788. if (quote !== token.substring(token.length - 1)) {
  789. throw new protobuf.Error(`String quotes do not match ${this.location()}`);
  790. }
  791. const value = token.substring(1, token.length - 1);
  792. this.next();
  793. this.semicolon();
  794. return value;
  795. }
  796. bool() {
  797. const token = this._token;
  798. switch (token) {
  799. case 'true':
  800. case 'True':
  801. case '1':
  802. this.next();
  803. this.semicolon();
  804. return true;
  805. case 'false':
  806. case 'False':
  807. case '0':
  808. this.next();
  809. this.semicolon();
  810. return false;
  811. default:
  812. throw new protobuf.Error(`Couldn't parse boolean '${token}' ${this.location()}`);
  813. }
  814. }
  815. bytes() {
  816. const token = this.string();
  817. const length = token.length;
  818. const array = new Uint8Array(length);
  819. for (let i = 0; i < length; i++) {
  820. array[i] = token.charCodeAt(i);
  821. }
  822. return array;
  823. }
  824. enum(type) {
  825. const token = this._token;
  826. let value = 0;
  827. if (Object.prototype.hasOwnProperty.call(type, token)) {
  828. value = type[token];
  829. } else {
  830. value = Number.parseInt(token, 10);
  831. if (Number.isNaN(token - value)) {
  832. throw new protobuf.Error(`Couldn't parse enum '${token === undefined ? '' : token}' ${this.location()}`);
  833. }
  834. }
  835. this.next();
  836. this.semicolon();
  837. return value;
  838. }
  839. any(type) {
  840. this.start();
  841. const message = type();
  842. if (this._token.startsWith('[') && this._token.endsWith(']')) {
  843. message.type_url = this._token.substring(1, this._token.length - 1).trim();
  844. this.next();
  845. this.match(':');
  846. message.value = this.read();
  847. this.match(';');
  848. if (!this.end()) {
  849. this.expect('}');
  850. }
  851. } else {
  852. while (!this.end()) {
  853. const tag = this.tag();
  854. switch (tag) {
  855. case "type_url":
  856. message.type_url = this.string();
  857. break;
  858. case "value":
  859. message.value = this.bytes();
  860. break;
  861. default:
  862. this.field(tag, message);
  863. break;
  864. }
  865. }
  866. }
  867. return message;
  868. }
  869. anyarray(obj, type) {
  870. this.start();
  871. if (this._token.startsWith('[') && this._token.endsWith(']')) {
  872. while (!this.end()) {
  873. if (this._token.startsWith('[') && this._token.endsWith(']')) {
  874. const message = type();
  875. message.type_url = this._token.substring(1, this._token.length - 1).trim();
  876. this.next();
  877. this.match(':');
  878. message.value = this.read();
  879. this.match(';');
  880. obj.push(message);
  881. continue;
  882. }
  883. this.expect('[');
  884. }
  885. } else {
  886. const message = type();
  887. while (!this.end()) {
  888. const tag = this.tag();
  889. switch (tag) {
  890. case "type_url":
  891. message.type_url = this.string();
  892. break;
  893. case "value":
  894. message.value = this.bytes();
  895. break;
  896. default:
  897. this.field(tag, message);
  898. break;
  899. }
  900. }
  901. obj.push(message);
  902. }
  903. }
  904. entry(obj, key, value) {
  905. this.start();
  906. let k = '';
  907. let v = null;
  908. while (!this.end()) {
  909. const tag = this.tag();
  910. switch (tag) {
  911. case 'key':
  912. k = key();
  913. break;
  914. case 'value':
  915. v = value();
  916. break;
  917. default:
  918. throw new protobuf.Error(`Unsupported entry tag '${tag}'.`);
  919. }
  920. }
  921. obj[k] = v;
  922. }
  923. array(obj, item) {
  924. if (this.first()) {
  925. while (!this.last()) {
  926. obj.push(item());
  927. switch (this._token) {
  928. case ',':
  929. this.next();
  930. break;
  931. case ']':
  932. break;
  933. default:
  934. this.handle(this._token);
  935. break;
  936. }
  937. }
  938. } else {
  939. obj.push(item());
  940. }
  941. }
  942. first() {
  943. if (this.match('[')) {
  944. this._arrayDepth++;
  945. return true;
  946. }
  947. return false;
  948. }
  949. last() {
  950. if (this.match(']')) {
  951. this._arrayDepth--;
  952. return true;
  953. }
  954. return false;
  955. }
  956. read() {
  957. const start = this._position;
  958. this.skip();
  959. const end = this._position;
  960. const position = this._decoder.position;
  961. this._decoder.position = start;
  962. let content = '';
  963. while (this._decoder.position < end) {
  964. content += this._decoder.decode();
  965. }
  966. this._decoder.position = position;
  967. return content;
  968. }
  969. skip() {
  970. switch (this._token) {
  971. case '{': {
  972. const depth = this._depth;
  973. this.start();
  974. while (!this.end() || depth < this._depth) {
  975. if (this._token === '{') {
  976. this.start();
  977. } else if (this._token !== '}') {
  978. this.next();
  979. this.match(';');
  980. }
  981. }
  982. break;
  983. }
  984. case '[': {
  985. const depth = this._arrayDepth;
  986. this.first();
  987. while (!this.last() || depth < this._arrayDepth) {
  988. this.next();
  989. if (this._token === '[') {
  990. this.first();
  991. } else if (this._token === undefined) {
  992. this.handle(this._token);
  993. }
  994. }
  995. break;
  996. }
  997. default: {
  998. this.next();
  999. this.semicolon();
  1000. break;
  1001. }
  1002. }
  1003. }
  1004. handle(token) {
  1005. throw new protobuf.Error(`Unexpected token '${token}' ${this.location()}`);
  1006. }
  1007. field(token /*, module */) {
  1008. throw new protobuf.Error(`Unsupported field '${token}' ${this.location()}`);
  1009. }
  1010. token() {
  1011. return this._token;
  1012. }
  1013. next() {
  1014. if (this._token === undefined) {
  1015. throw new protobuf.Error(`Unexpected end of input ${this.location()}`);
  1016. }
  1017. this._position = this._decoder.position;
  1018. let c = this._decoder.decode();
  1019. for (;;) {
  1020. switch (c) {
  1021. case ' ':
  1022. case '\n':
  1023. case '\r':
  1024. case '\t':
  1025. this._position = this._decoder.position;
  1026. c = this._decoder.decode();
  1027. continue;
  1028. case '#':
  1029. do {
  1030. c = this._decoder.decode();
  1031. if (c === undefined) {
  1032. this._token = undefined;
  1033. return;
  1034. }
  1035. }
  1036. while (c !== '\n');
  1037. this._position = this._decoder.position;
  1038. c = this._decoder.decode();
  1039. continue;
  1040. default:
  1041. break;
  1042. }
  1043. break;
  1044. }
  1045. if (c === undefined) {
  1046. this._token = undefined;
  1047. return;
  1048. }
  1049. if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c === '_' || c === '$') {
  1050. let token = c;
  1051. let position = this._decoder.position;
  1052. for (;;) {
  1053. c = this._decoder.decode();
  1054. if (c === undefined || c === '\n') {
  1055. break;
  1056. }
  1057. if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c === '_' || c === '+' || c === '-') {
  1058. token += c;
  1059. position = this._decoder.position;
  1060. continue;
  1061. }
  1062. break;
  1063. }
  1064. this._decoder.position = position;
  1065. this._token = token;
  1066. return;
  1067. }
  1068. switch (c) {
  1069. case '{':
  1070. case '}':
  1071. case ':':
  1072. case ',':
  1073. case ']':
  1074. case ';':
  1075. this._token = c;
  1076. return;
  1077. case '[': {
  1078. let token = c;
  1079. let position = this._decoder.position;
  1080. let x = this._decoder.decode();
  1081. if ((x !== undefined) && x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z') {
  1082. token += x;
  1083. for (;;) {
  1084. x = this._decoder.decode();
  1085. if (x === undefined || x === '\n') {
  1086. break;
  1087. }
  1088. if (x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z' || x >= '0' && x <= '9' || x === '.' || x === '/') {
  1089. token += x;
  1090. position = this._decoder.position;
  1091. continue;
  1092. }
  1093. if (x === ']') {
  1094. this._token = token + x;
  1095. return;
  1096. }
  1097. }
  1098. }
  1099. this._decoder.position = position;
  1100. this._token = '[';
  1101. return;
  1102. }
  1103. case '"':
  1104. case "'": {
  1105. const quote = c;
  1106. let content = c;
  1107. for (;;) {
  1108. c = this._decoder.decode();
  1109. if (c === undefined || c === '\n') {
  1110. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1111. }
  1112. if (c === '\\') {
  1113. c = this._decoder.decode();
  1114. if (c === undefined || c === '\n') {
  1115. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1116. }
  1117. switch (c) {
  1118. case '\\': c = '\\'; break;
  1119. case "'": c = "'"; break;
  1120. case '"': c = '"'; break;
  1121. case 'r': c = '\r'; break;
  1122. case 'n': c = '\n'; break;
  1123. case 't': c = '\t'; break;
  1124. case 'b': c = '\b'; break;
  1125. case 'x':
  1126. case 'X': {
  1127. let value = 0;
  1128. for (let xi = 0; xi < 2; xi++) {
  1129. let c = this._decoder.decode();
  1130. if (c === undefined) {
  1131. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1132. }
  1133. c = c.charCodeAt(0);
  1134. if (c >= 65 && c <= 70) {
  1135. c -= 55;
  1136. } else if (c >= 97 && c <= 102) {
  1137. c -= 87;
  1138. } else if (c >= 48 && c <= 57) {
  1139. c -= 48;
  1140. } else {
  1141. c = -1;
  1142. }
  1143. if (c === -1) {
  1144. throw new protobuf.Error(`Unexpected hex digit '${c}' in bytes string ${this.location()}`);
  1145. }
  1146. value = value << 4 | c;
  1147. }
  1148. c = String.fromCharCode(value);
  1149. break;
  1150. }
  1151. default: {
  1152. if (c < '0' || c > '9') {
  1153. throw new protobuf.Error(`Unexpected character '${c}' in string ${this.location()}`);
  1154. }
  1155. let value = 0;
  1156. let od = c;
  1157. if (od < '0' || od > '9') {
  1158. throw new protobuf.Error(`Unexpected octal digit '${od}' in bytes string ${this.location()}`);
  1159. }
  1160. od = od.charCodeAt(0);
  1161. value = value << 3 | od - 48;
  1162. od = this._decoder.decode();
  1163. if (od === undefined) {
  1164. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1165. }
  1166. if (od < '0' || od > '9') {
  1167. throw new protobuf.Error(`Unexpected octal digit '${od}' in bytes string ${this.location()}`);
  1168. }
  1169. od = od.charCodeAt(0);
  1170. value = value << 3 | od - 48;
  1171. od = this._decoder.decode();
  1172. if (od === undefined) {
  1173. throw new protobuf.Error(`Unexpected end of string ${this.location()}`);
  1174. }
  1175. if (od < '0' || od > '9') {
  1176. throw new protobuf.Error(`Unexpected octal digit '${od}' in bytes string ${this.location()}`);
  1177. }
  1178. od = od.charCodeAt(0);
  1179. value = value << 3 | od - 48;
  1180. c = String.fromCharCode(value);
  1181. break;
  1182. }
  1183. }
  1184. content += c;
  1185. continue;
  1186. } else {
  1187. content += c;
  1188. if (c === quote) {
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. this._token = content;
  1194. return;
  1195. }
  1196. case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  1197. case '-': case '+': case '.': {
  1198. let token = c;
  1199. let position = this._decoder.position;
  1200. for (;;) {
  1201. c = this._decoder.decode();
  1202. if (c === undefined || c === '\n') {
  1203. break;
  1204. }
  1205. if ((c >= '0' && c <= '9') || c === '_' || c === '+' || c === '-' || c === '.' || c === 'e' || c === 'E') {
  1206. token += c;
  1207. position = this._decoder.position;
  1208. continue;
  1209. }
  1210. break;
  1211. }
  1212. if (token === '-' && c === 'i' && this._decoder.decode() === 'n' && this._decoder.decode() === 'f') {
  1213. token = '-inf';
  1214. position = this._decoder.position;
  1215. }
  1216. if (token === '-' || token === '+' || token === '.') {
  1217. throw new protobuf.Error(`Unexpected token '${token}' ${this.location()}`);
  1218. }
  1219. this._decoder.position = position;
  1220. this._token = token;
  1221. return;
  1222. }
  1223. default: {
  1224. throw new protobuf.Error(`Unexpected token '${c}' ${this.location()}`);
  1225. }
  1226. }
  1227. }
  1228. expect(value) {
  1229. if (this._token !== value) {
  1230. throw new protobuf.Error(`Unexpected '${this._token}' instead of '${value}' ${this.location()}`);
  1231. }
  1232. this.next();
  1233. }
  1234. match(value) {
  1235. if (value === this._token) {
  1236. this.next();
  1237. return true;
  1238. }
  1239. return false;
  1240. }
  1241. location() {
  1242. let line = 1;
  1243. let column = 1;
  1244. this._decoder.position = 0;
  1245. let c = '';
  1246. do {
  1247. if (this._decoder.position === this._position) {
  1248. return `at ${line}:${column}.`;
  1249. }
  1250. c = this._decoder.decode();
  1251. if (c === '\n') {
  1252. line++;
  1253. column = 1;
  1254. } else {
  1255. column++;
  1256. }
  1257. }
  1258. while (c !== undefined);
  1259. return `at ${line}:${column}.`;
  1260. }
  1261. semicolon() {
  1262. if (this._arrayDepth === 0) {
  1263. this.match(';');
  1264. }
  1265. }
  1266. };
  1267. protobuf.Error = class extends Error {
  1268. constructor(message) {
  1269. super(message);
  1270. this.name = 'Protocol Buffers Error';
  1271. this.message = message;
  1272. }
  1273. };
  1274. export const BinaryReader = protobuf.BinaryReader;
  1275. export const TextReader = protobuf.TextReader;