protobuf.js 42 KB

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