protobuf.js 37 KB

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