reader.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. /**
  31. * @fileoverview This file contains utilities for converting binary,
  32. * wire-format protocol buffers into Javascript data structures.
  33. *
  34. * jspb's BinaryReader class wraps the BinaryDecoder class to add methods
  35. * that understand the protocol buffer syntax and can do the type checking and
  36. * bookkeeping necessary to parse trees of nested messages.
  37. *
  38. * Major caveat - Users of this library _must_ keep their Javascript proto
  39. * parsing code in sync with the original .proto file - presumably you'll be
  40. * using the typed jspb code generator, but if you bypass that you'll need
  41. * to keep things in sync by hand.
  42. *
  43. * @author aappleby@google.com (Austin Appleby)
  44. */
  45. goog.provide('jspb.BinaryReader');
  46. goog.require('goog.asserts');
  47. goog.require('jspb.BinaryConstants');
  48. goog.require('jspb.BinaryDecoder');
  49. /**
  50. * BinaryReader implements the decoders for all the wire types specified in
  51. * https://developers.google.com/protocol-buffers/docs/encoding.
  52. *
  53. * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from.
  54. * @param {number=} opt_start The optional offset to start reading at.
  55. * @param {number=} opt_length The optional length of the block to read -
  56. * we'll throw an assertion if we go off the end of the block.
  57. * @constructor
  58. * @struct
  59. */
  60. jspb.BinaryReader = function(opt_bytes, opt_start, opt_length) {
  61. /**
  62. * Wire-format decoder.
  63. * @private {!jspb.BinaryDecoder}
  64. */
  65. this.decoder_ = jspb.BinaryDecoder.alloc(opt_bytes, opt_start, opt_length);
  66. /**
  67. * Cursor immediately before the field tag.
  68. * @private {number}
  69. */
  70. this.fieldCursor_ = this.decoder_.getCursor();
  71. /**
  72. * Field number of the next field in the buffer, filled in by nextField().
  73. * @private {number}
  74. */
  75. this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER;
  76. /**
  77. * Wire type of the next proto field in the buffer, filled in by
  78. * nextField().
  79. * @private {jspb.BinaryConstants.WireType}
  80. */
  81. this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID;
  82. /**
  83. * Set to true if this reader encountered an error due to corrupt data.
  84. * @private {boolean}
  85. */
  86. this.error_ = false;
  87. /**
  88. * User-defined reader callbacks.
  89. * @private {Object<string, function(!jspb.BinaryReader):*>}
  90. */
  91. this.readCallbacks_ = null;
  92. };
  93. /**
  94. * Global pool of BinaryReader instances.
  95. * @private {!Array<!jspb.BinaryReader>}
  96. */
  97. jspb.BinaryReader.instanceCache_ = [];
  98. /**
  99. * Pops an instance off the instance cache, or creates one if the cache is
  100. * empty.
  101. * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from.
  102. * @param {number=} opt_start The optional offset to start reading at.
  103. * @param {number=} opt_length The optional length of the block to read -
  104. * we'll throw an assertion if we go off the end of the block.
  105. * @return {!jspb.BinaryReader}
  106. */
  107. jspb.BinaryReader.alloc =
  108. function(opt_bytes, opt_start, opt_length) {
  109. if (jspb.BinaryReader.instanceCache_.length) {
  110. var newReader = jspb.BinaryReader.instanceCache_.pop();
  111. if (opt_bytes) {
  112. newReader.decoder_.setBlock(opt_bytes, opt_start, opt_length);
  113. }
  114. return newReader;
  115. } else {
  116. return new jspb.BinaryReader(opt_bytes, opt_start, opt_length);
  117. }
  118. };
  119. /**
  120. * Alias for the above method.
  121. * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from.
  122. * @param {number=} opt_start The optional offset to start reading at.
  123. * @param {number=} opt_length The optional length of the block to read -
  124. * we'll throw an assertion if we go off the end of the block.
  125. * @return {!jspb.BinaryReader}
  126. */
  127. jspb.BinaryReader.prototype.alloc = jspb.BinaryReader.alloc;
  128. /**
  129. * Puts this instance back in the instance cache.
  130. */
  131. jspb.BinaryReader.prototype.free = function() {
  132. this.decoder_.clear();
  133. this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER;
  134. this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID;
  135. this.error_ = false;
  136. this.readCallbacks_ = null;
  137. if (jspb.BinaryReader.instanceCache_.length < 100) {
  138. jspb.BinaryReader.instanceCache_.push(this);
  139. }
  140. };
  141. /**
  142. * Returns the cursor immediately before the current field's tag.
  143. * @return {number} The internal read cursor.
  144. */
  145. jspb.BinaryReader.prototype.getFieldCursor = function() {
  146. return this.fieldCursor_;
  147. };
  148. /**
  149. * Returns the internal read cursor.
  150. * @return {number} The internal read cursor.
  151. */
  152. jspb.BinaryReader.prototype.getCursor = function() {
  153. return this.decoder_.getCursor();
  154. };
  155. /**
  156. * Returns the raw buffer.
  157. * @return {?Uint8Array} The raw buffer.
  158. */
  159. jspb.BinaryReader.prototype.getBuffer = function() {
  160. return this.decoder_.getBuffer();
  161. };
  162. /**
  163. * @return {number} The field number of the next field in the buffer, or
  164. * INVALID_FIELD_NUMBER if there is no next field.
  165. */
  166. jspb.BinaryReader.prototype.getFieldNumber = function() {
  167. return this.nextField_;
  168. };
  169. /**
  170. * @return {jspb.BinaryConstants.WireType} The wire type of the next field
  171. * in the stream, or WireType.INVALID if there is no next field.
  172. */
  173. jspb.BinaryReader.prototype.getWireType = function() {
  174. return this.nextWireType_;
  175. };
  176. /**
  177. * @return {boolean} Whether the current wire type is an end-group tag. Used as
  178. * an exit condition in decoder loops in generated code.
  179. */
  180. jspb.BinaryReader.prototype.isEndGroup = function() {
  181. return this.nextWireType_ == jspb.BinaryConstants.WireType.END_GROUP;
  182. };
  183. /**
  184. * Returns true if this reader hit an error due to corrupt data.
  185. * @return {boolean}
  186. */
  187. jspb.BinaryReader.prototype.getError = function() {
  188. return this.error_ || this.decoder_.getError();
  189. };
  190. /**
  191. * Points this reader at a new block of bytes.
  192. * @param {!Uint8Array} bytes The block of bytes we're reading from.
  193. * @param {number} start The offset to start reading at.
  194. * @param {number} length The length of the block to read.
  195. */
  196. jspb.BinaryReader.prototype.setBlock = function(bytes, start, length) {
  197. this.decoder_.setBlock(bytes, start, length);
  198. this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER;
  199. this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID;
  200. };
  201. /**
  202. * Rewinds the stream cursor to the beginning of the buffer and resets all
  203. * internal state.
  204. */
  205. jspb.BinaryReader.prototype.reset = function() {
  206. this.decoder_.reset();
  207. this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER;
  208. this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID;
  209. };
  210. /**
  211. * Advances the stream cursor by the given number of bytes.
  212. * @param {number} count The number of bytes to advance by.
  213. */
  214. jspb.BinaryReader.prototype.advance = function(count) {
  215. this.decoder_.advance(count);
  216. };
  217. /**
  218. * Reads the next field header in the stream if there is one, returns true if
  219. * we saw a valid field header or false if we've read the whole stream.
  220. * Throws an error if we encountered a deprecated START_GROUP/END_GROUP field.
  221. * @return {boolean} True if the stream contains more fields.
  222. */
  223. jspb.BinaryReader.prototype.nextField = function() {
  224. // If we're at the end of the block, there are no more fields.
  225. if (this.decoder_.atEnd()) {
  226. return false;
  227. }
  228. // If we hit an error decoding the previous field, stop now before we
  229. // try to decode anything else
  230. if (this.getError()) {
  231. goog.asserts.fail('Decoder hit an error');
  232. return false;
  233. }
  234. // Otherwise just read the header of the next field.
  235. this.fieldCursor_ = this.decoder_.getCursor();
  236. var header = this.decoder_.readUnsignedVarint32();
  237. var nextField = header >>> 3;
  238. var nextWireType = /** @type {jspb.BinaryConstants.WireType} */
  239. (header & 0x7);
  240. // If the wire type isn't one of the valid ones, something's broken.
  241. if (nextWireType != jspb.BinaryConstants.WireType.VARINT &&
  242. nextWireType != jspb.BinaryConstants.WireType.FIXED32 &&
  243. nextWireType != jspb.BinaryConstants.WireType.FIXED64 &&
  244. nextWireType != jspb.BinaryConstants.WireType.DELIMITED &&
  245. nextWireType != jspb.BinaryConstants.WireType.START_GROUP &&
  246. nextWireType != jspb.BinaryConstants.WireType.END_GROUP) {
  247. goog.asserts.fail(
  248. 'Invalid wire type: %s (at position %s)', nextWireType,
  249. this.fieldCursor_);
  250. this.error_ = true;
  251. return false;
  252. }
  253. this.nextField_ = nextField;
  254. this.nextWireType_ = nextWireType;
  255. return true;
  256. };
  257. /**
  258. * Winds the reader back to just before this field's header.
  259. */
  260. jspb.BinaryReader.prototype.unskipHeader = function() {
  261. this.decoder_.unskipVarint((this.nextField_ << 3) | this.nextWireType_);
  262. };
  263. /**
  264. * Skips all contiguous fields whose header matches the one we just read.
  265. */
  266. jspb.BinaryReader.prototype.skipMatchingFields = function() {
  267. var field = this.nextField_;
  268. this.unskipHeader();
  269. while (this.nextField() && (this.getFieldNumber() == field)) {
  270. this.skipField();
  271. }
  272. if (!this.decoder_.atEnd()) {
  273. this.unskipHeader();
  274. }
  275. };
  276. /**
  277. * Skips over the next varint field in the binary stream.
  278. */
  279. jspb.BinaryReader.prototype.skipVarintField = function() {
  280. if (this.nextWireType_ != jspb.BinaryConstants.WireType.VARINT) {
  281. goog.asserts.fail('Invalid wire type for skipVarintField');
  282. this.skipField();
  283. return;
  284. }
  285. this.decoder_.skipVarint();
  286. };
  287. /**
  288. * Skips over the next delimited field in the binary stream.
  289. */
  290. jspb.BinaryReader.prototype.skipDelimitedField = function() {
  291. if (this.nextWireType_ != jspb.BinaryConstants.WireType.DELIMITED) {
  292. goog.asserts.fail('Invalid wire type for skipDelimitedField');
  293. this.skipField();
  294. return;
  295. }
  296. var length = this.decoder_.readUnsignedVarint32();
  297. this.decoder_.advance(length);
  298. };
  299. /**
  300. * Skips over the next fixed32 field in the binary stream.
  301. */
  302. jspb.BinaryReader.prototype.skipFixed32Field = function() {
  303. if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED32) {
  304. goog.asserts.fail('Invalid wire type for skipFixed32Field');
  305. this.skipField();
  306. return;
  307. }
  308. this.decoder_.advance(4);
  309. };
  310. /**
  311. * Skips over the next fixed64 field in the binary stream.
  312. */
  313. jspb.BinaryReader.prototype.skipFixed64Field = function() {
  314. if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED64) {
  315. goog.asserts.fail('Invalid wire type for skipFixed64Field');
  316. this.skipField();
  317. return;
  318. }
  319. this.decoder_.advance(8);
  320. };
  321. /**
  322. * Skips over the next group field in the binary stream.
  323. */
  324. jspb.BinaryReader.prototype.skipGroup = function() {
  325. var previousField = this.nextField_;
  326. do {
  327. if (!this.nextField()) {
  328. goog.asserts.fail('Unmatched start-group tag: stream EOF');
  329. this.error_ = true;
  330. return;
  331. }
  332. if (this.nextWireType_ ==
  333. jspb.BinaryConstants.WireType.END_GROUP) {
  334. // Group end: check that it matches top-of-stack.
  335. if (this.nextField_ != previousField) {
  336. goog.asserts.fail('Unmatched end-group tag');
  337. this.error_ = true;
  338. return;
  339. }
  340. return;
  341. }
  342. this.skipField();
  343. } while (true);
  344. };
  345. /**
  346. * Skips over the next field in the binary stream - this is useful if we're
  347. * decoding a message that contain unknown fields.
  348. */
  349. jspb.BinaryReader.prototype.skipField = function() {
  350. switch (this.nextWireType_) {
  351. case jspb.BinaryConstants.WireType.VARINT:
  352. this.skipVarintField();
  353. break;
  354. case jspb.BinaryConstants.WireType.FIXED64:
  355. this.skipFixed64Field();
  356. break;
  357. case jspb.BinaryConstants.WireType.DELIMITED:
  358. this.skipDelimitedField();
  359. break;
  360. case jspb.BinaryConstants.WireType.FIXED32:
  361. this.skipFixed32Field();
  362. break;
  363. case jspb.BinaryConstants.WireType.START_GROUP:
  364. this.skipGroup();
  365. break;
  366. default:
  367. goog.asserts.fail('Invalid wire encoding for field.');
  368. }
  369. };
  370. /**
  371. * Registers a user-defined read callback.
  372. * @param {string} callbackName
  373. * @param {function(!jspb.BinaryReader):*} callback
  374. */
  375. jspb.BinaryReader.prototype.registerReadCallback =
  376. function(callbackName, callback) {
  377. if (goog.isNull(this.readCallbacks_)) {
  378. this.readCallbacks_ = {};
  379. }
  380. goog.asserts.assert(!this.readCallbacks_[callbackName]);
  381. this.readCallbacks_[callbackName] = callback;
  382. };
  383. /**
  384. * Runs a registered read callback.
  385. * @param {string} callbackName The name the callback is registered under.
  386. * @return {*} The value returned by the callback.
  387. */
  388. jspb.BinaryReader.prototype.runReadCallback = function(callbackName) {
  389. goog.asserts.assert(!goog.isNull(this.readCallbacks_));
  390. var callback = this.readCallbacks_[callbackName];
  391. goog.asserts.assert(callback);
  392. return callback(this);
  393. };
  394. /**
  395. * Reads a field of any valid non-message type from the binary stream.
  396. * @param {jspb.BinaryConstants.FieldType} fieldType
  397. * @return {jspb.AnyFieldType}
  398. */
  399. jspb.BinaryReader.prototype.readAny = function(fieldType) {
  400. this.nextWireType_ = jspb.BinaryConstants.FieldTypeToWireType(fieldType);
  401. var fieldTypes = jspb.BinaryConstants.FieldType;
  402. switch (fieldType) {
  403. case fieldTypes.DOUBLE:
  404. return this.readDouble();
  405. case fieldTypes.FLOAT:
  406. return this.readFloat();
  407. case fieldTypes.INT64:
  408. return this.readInt64();
  409. case fieldTypes.UINT64:
  410. return this.readUint64();
  411. case fieldTypes.INT32:
  412. return this.readInt32();
  413. case fieldTypes.FIXED64:
  414. return this.readFixed64();
  415. case fieldTypes.FIXED32:
  416. return this.readFixed32();
  417. case fieldTypes.BOOL:
  418. return this.readBool();
  419. case fieldTypes.STRING:
  420. return this.readString();
  421. case fieldTypes.GROUP:
  422. goog.asserts.fail('Group field type not supported in readAny()');
  423. case fieldTypes.MESSAGE:
  424. goog.asserts.fail('Message field type not supported in readAny()');
  425. case fieldTypes.BYTES:
  426. return this.readBytes();
  427. case fieldTypes.UINT32:
  428. return this.readUint32();
  429. case fieldTypes.ENUM:
  430. return this.readEnum();
  431. case fieldTypes.SFIXED32:
  432. return this.readSfixed32();
  433. case fieldTypes.SFIXED64:
  434. return this.readSfixed64();
  435. case fieldTypes.SINT32:
  436. return this.readSint32();
  437. case fieldTypes.SINT64:
  438. return this.readSint64();
  439. case fieldTypes.FHASH64:
  440. return this.readFixedHash64();
  441. case fieldTypes.VHASH64:
  442. return this.readVarintHash64();
  443. default:
  444. goog.asserts.fail('Invalid field type in readAny()');
  445. }
  446. return 0;
  447. };
  448. /**
  449. * Deserialize a proto into the provided message object using the provided
  450. * reader function. This function is templated as we currently have one client
  451. * who is using manual deserialization instead of the code-generated versions.
  452. * @template T
  453. * @param {T} message
  454. * @param {function(T, !jspb.BinaryReader)} reader
  455. */
  456. jspb.BinaryReader.prototype.readMessage = function(message, reader) {
  457. goog.asserts.assert(
  458. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  459. // Save the current endpoint of the decoder and move it to the end of the
  460. // embedded message.
  461. var oldEnd = this.decoder_.getEnd();
  462. var length = this.decoder_.readUnsignedVarint32();
  463. var newEnd = this.decoder_.getCursor() + length;
  464. this.decoder_.setEnd(newEnd);
  465. // Deserialize the embedded message.
  466. reader(message, this);
  467. // Advance the decoder past the embedded message and restore the endpoint.
  468. this.decoder_.setCursor(newEnd);
  469. this.decoder_.setEnd(oldEnd);
  470. };
  471. /**
  472. * Deserialize a proto into the provided message object using the provided
  473. * reader function, assuming that the message is serialized as a group
  474. * with the given tag.
  475. * @template T
  476. * @param {number} field
  477. * @param {T} message
  478. * @param {function(T, !jspb.BinaryReader)} reader
  479. */
  480. jspb.BinaryReader.prototype.readGroup =
  481. function(field, message, reader) {
  482. // Ensure that the wire type is correct.
  483. goog.asserts.assert(
  484. this.nextWireType_ == jspb.BinaryConstants.WireType.START_GROUP);
  485. // Ensure that the field number is correct.
  486. goog.asserts.assert(this.nextField_ == field);
  487. // Deserialize the message. The deserialization will stop at an END_GROUP tag.
  488. reader(message, this);
  489. if (!this.error_ &&
  490. this.nextWireType_ != jspb.BinaryConstants.WireType.END_GROUP) {
  491. goog.asserts.fail('Group submessage did not end with an END_GROUP tag');
  492. this.error_ = true;
  493. }
  494. };
  495. /**
  496. * Return a decoder that wraps the current delimited field.
  497. * @return {!jspb.BinaryDecoder}
  498. */
  499. jspb.BinaryReader.prototype.getFieldDecoder = function() {
  500. goog.asserts.assert(
  501. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  502. var length = this.decoder_.readUnsignedVarint32();
  503. var start = this.decoder_.getCursor();
  504. var end = start + length;
  505. var innerDecoder =
  506. jspb.BinaryDecoder.alloc(this.decoder_.getBuffer(), start, length);
  507. this.decoder_.setCursor(end);
  508. return innerDecoder;
  509. };
  510. /**
  511. * Reads a signed 32-bit integer field from the binary stream, or throws an
  512. * error if the next field in the stream is not of the correct wire type.
  513. *
  514. * @return {number} The value of the signed 32-bit integer field.
  515. */
  516. jspb.BinaryReader.prototype.readInt32 = function() {
  517. goog.asserts.assert(
  518. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  519. return this.decoder_.readSignedVarint32();
  520. };
  521. /**
  522. * Reads a signed 32-bit integer field from the binary stream, or throws an
  523. * error if the next field in the stream is not of the correct wire type.
  524. *
  525. * Returns the value as a string.
  526. *
  527. * @return {string} The value of the signed 32-bit integer field as a decimal
  528. * string.
  529. */
  530. jspb.BinaryReader.prototype.readInt32String = function() {
  531. goog.asserts.assert(
  532. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  533. return this.decoder_.readSignedVarint32String();
  534. };
  535. /**
  536. * Reads a signed 64-bit integer field from the binary stream, or throws an
  537. * error if the next field in the stream is not of the correct wire type.
  538. *
  539. * @return {number} The value of the signed 64-bit integer field.
  540. */
  541. jspb.BinaryReader.prototype.readInt64 = function() {
  542. goog.asserts.assert(
  543. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  544. return this.decoder_.readSignedVarint64();
  545. };
  546. /**
  547. * Reads a signed 64-bit integer field from the binary stream, or throws an
  548. * error if the next field in the stream is not of the correct wire type.
  549. *
  550. * Returns the value as a string.
  551. *
  552. * @return {string} The value of the signed 64-bit integer field as a decimal
  553. * string.
  554. */
  555. jspb.BinaryReader.prototype.readInt64String = function() {
  556. goog.asserts.assert(
  557. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  558. return this.decoder_.readSignedVarint64String();
  559. };
  560. /**
  561. * Reads an unsigned 32-bit integer field from the binary stream, or throws an
  562. * error if the next field in the stream is not of the correct wire type.
  563. *
  564. * @return {number} The value of the unsigned 32-bit integer field.
  565. */
  566. jspb.BinaryReader.prototype.readUint32 = function() {
  567. goog.asserts.assert(
  568. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  569. return this.decoder_.readUnsignedVarint32();
  570. };
  571. /**
  572. * Reads an unsigned 32-bit integer field from the binary stream, or throws an
  573. * error if the next field in the stream is not of the correct wire type.
  574. *
  575. * Returns the value as a string.
  576. *
  577. * @return {string} The value of the unsigned 32-bit integer field as a decimal
  578. * string.
  579. */
  580. jspb.BinaryReader.prototype.readUint32String = function() {
  581. goog.asserts.assert(
  582. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  583. return this.decoder_.readUnsignedVarint32String();
  584. };
  585. /**
  586. * Reads an unsigned 64-bit integer field from the binary stream, or throws an
  587. * error if the next field in the stream is not of the correct wire type.
  588. *
  589. * @return {number} The value of the unsigned 64-bit integer field.
  590. */
  591. jspb.BinaryReader.prototype.readUint64 = function() {
  592. goog.asserts.assert(
  593. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  594. return this.decoder_.readUnsignedVarint64();
  595. };
  596. /**
  597. * Reads an unsigned 64-bit integer field from the binary stream, or throws an
  598. * error if the next field in the stream is not of the correct wire type.
  599. *
  600. * Returns the value as a string.
  601. *
  602. * @return {string} The value of the unsigned 64-bit integer field as a decimal
  603. * string.
  604. */
  605. jspb.BinaryReader.prototype.readUint64String = function() {
  606. goog.asserts.assert(
  607. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  608. return this.decoder_.readUnsignedVarint64String();
  609. };
  610. /**
  611. * Reads a signed zigzag-encoded 32-bit integer field from the binary stream,
  612. * or throws an error if the next field in the stream is not of the correct
  613. * wire type.
  614. *
  615. * @return {number} The value of the signed 32-bit integer field.
  616. */
  617. jspb.BinaryReader.prototype.readSint32 = function() {
  618. goog.asserts.assert(
  619. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  620. return this.decoder_.readZigzagVarint32();
  621. };
  622. /**
  623. * Reads a signed zigzag-encoded 64-bit integer field from the binary stream,
  624. * or throws an error if the next field in the stream is not of the correct
  625. * wire type.
  626. *
  627. * @return {number} The value of the signed 64-bit integer field.
  628. */
  629. jspb.BinaryReader.prototype.readSint64 = function() {
  630. goog.asserts.assert(
  631. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  632. return this.decoder_.readZigzagVarint64();
  633. };
  634. /**
  635. * Reads a signed zigzag-encoded 64-bit integer field from the binary stream,
  636. * or throws an error if the next field in the stream is not of the correct
  637. * wire type.
  638. *
  639. * @return {string} The value of the signed 64-bit integer field as a decimal string.
  640. */
  641. jspb.BinaryReader.prototype.readSint64String = function() {
  642. goog.asserts.assert(
  643. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  644. return this.decoder_.readZigzagVarint64String();
  645. };
  646. /**
  647. * Reads an unsigned 32-bit fixed-length integer fiield from the binary stream,
  648. * or throws an error if the next field in the stream is not of the correct
  649. * wire type.
  650. *
  651. * @return {number} The value of the double field.
  652. */
  653. jspb.BinaryReader.prototype.readFixed32 = function() {
  654. goog.asserts.assert(
  655. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  656. return this.decoder_.readUint32();
  657. };
  658. /**
  659. * Reads an unsigned 64-bit fixed-length integer fiield from the binary stream,
  660. * or throws an error if the next field in the stream is not of the correct
  661. * wire type.
  662. *
  663. * @return {number} The value of the float field.
  664. */
  665. jspb.BinaryReader.prototype.readFixed64 = function() {
  666. goog.asserts.assert(
  667. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  668. return this.decoder_.readUint64();
  669. };
  670. /**
  671. * Reads a signed 64-bit integer field from the binary stream as a string, or
  672. * throws an error if the next field in the stream is not of the correct wire
  673. * type.
  674. *
  675. * Returns the value as a string.
  676. *
  677. * @return {string} The value of the unsigned 64-bit integer field as a decimal
  678. * string.
  679. */
  680. jspb.BinaryReader.prototype.readFixed64String = function() {
  681. goog.asserts.assert(
  682. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  683. return this.decoder_.readUint64String();
  684. };
  685. /**
  686. * Reads a signed 32-bit fixed-length integer fiield from the binary stream, or
  687. * throws an error if the next field in the stream is not of the correct wire
  688. * type.
  689. *
  690. * @return {number} The value of the signed 32-bit integer field.
  691. */
  692. jspb.BinaryReader.prototype.readSfixed32 = function() {
  693. goog.asserts.assert(
  694. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  695. return this.decoder_.readInt32();
  696. };
  697. /**
  698. * Reads a signed 32-bit fixed-length integer fiield from the binary stream, or
  699. * throws an error if the next field in the stream is not of the correct wire
  700. * type.
  701. *
  702. * @return {string} The value of the signed 32-bit integer field as a decimal
  703. * string.
  704. */
  705. jspb.BinaryReader.prototype.readSfixed32String = function() {
  706. goog.asserts.assert(
  707. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  708. return this.decoder_.readInt32().toString();
  709. };
  710. /**
  711. * Reads a signed 64-bit fixed-length integer fiield from the binary stream, or
  712. * throws an error if the next field in the stream is not of the correct wire
  713. * type.
  714. *
  715. * @return {number} The value of the sfixed64 field.
  716. */
  717. jspb.BinaryReader.prototype.readSfixed64 = function() {
  718. goog.asserts.assert(
  719. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  720. return this.decoder_.readInt64();
  721. };
  722. /**
  723. * Reads a signed 64-bit fixed-length integer fiield from the binary stream, or
  724. * throws an error if the next field in the stream is not of the correct wire
  725. * type.
  726. *
  727. * Returns the value as a string.
  728. *
  729. * @return {string} The value of the sfixed64 field as a decimal string.
  730. */
  731. jspb.BinaryReader.prototype.readSfixed64String = function() {
  732. goog.asserts.assert(
  733. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  734. return this.decoder_.readInt64String();
  735. };
  736. /**
  737. * Reads a 32-bit floating-point field from the binary stream, or throws an
  738. * error if the next field in the stream is not of the correct wire type.
  739. *
  740. * @return {number} The value of the float field.
  741. */
  742. jspb.BinaryReader.prototype.readFloat = function() {
  743. goog.asserts.assert(
  744. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  745. return this.decoder_.readFloat();
  746. };
  747. /**
  748. * Reads a 64-bit floating-point field from the binary stream, or throws an
  749. * error if the next field in the stream is not of the correct wire type.
  750. *
  751. * @return {number} The value of the double field.
  752. */
  753. jspb.BinaryReader.prototype.readDouble = function() {
  754. goog.asserts.assert(
  755. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  756. return this.decoder_.readDouble();
  757. };
  758. /**
  759. * Reads a boolean field from the binary stream, or throws an error if the next
  760. * field in the stream is not of the correct wire type.
  761. *
  762. * @return {boolean} The value of the boolean field.
  763. */
  764. jspb.BinaryReader.prototype.readBool = function() {
  765. goog.asserts.assert(
  766. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  767. return !!this.decoder_.readUnsignedVarint32();
  768. };
  769. /**
  770. * Reads an enum field from the binary stream, or throws an error if the next
  771. * field in the stream is not of the correct wire type.
  772. *
  773. * @return {number} The value of the enum field.
  774. */
  775. jspb.BinaryReader.prototype.readEnum = function() {
  776. goog.asserts.assert(
  777. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  778. return this.decoder_.readSignedVarint64();
  779. };
  780. /**
  781. * Reads a string field from the binary stream, or throws an error if the next
  782. * field in the stream is not of the correct wire type.
  783. *
  784. * @return {string} The value of the string field.
  785. */
  786. jspb.BinaryReader.prototype.readString = function() {
  787. goog.asserts.assert(
  788. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  789. var length = this.decoder_.readUnsignedVarint32();
  790. return this.decoder_.readString(length);
  791. };
  792. /**
  793. * Reads a length-prefixed block of bytes from the binary stream, or returns
  794. * null if the next field in the stream has an invalid length value.
  795. *
  796. * @return {!Uint8Array} The block of bytes.
  797. */
  798. jspb.BinaryReader.prototype.readBytes = function() {
  799. goog.asserts.assert(
  800. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  801. var length = this.decoder_.readUnsignedVarint32();
  802. return this.decoder_.readBytes(length);
  803. };
  804. /**
  805. * Reads a 64-bit varint or fixed64 field from the stream and returns it as a
  806. * 8-character Unicode string for use as a hash table key, or throws an error
  807. * if the next field in the stream is not of the correct wire type.
  808. *
  809. * @return {string} The hash value.
  810. */
  811. jspb.BinaryReader.prototype.readVarintHash64 = function() {
  812. goog.asserts.assert(
  813. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  814. return this.decoder_.readVarintHash64();
  815. };
  816. /**
  817. * Reads a 64-bit varint or fixed64 field from the stream and returns it as a
  818. * 8-character Unicode string for use as a hash table key, or throws an error
  819. * if the next field in the stream is not of the correct wire type.
  820. *
  821. * @return {string} The hash value.
  822. */
  823. jspb.BinaryReader.prototype.readFixedHash64 = function() {
  824. goog.asserts.assert(
  825. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  826. return this.decoder_.readFixedHash64();
  827. };
  828. /**
  829. * Reads a packed scalar field using the supplied raw reader function.
  830. * @param {function(this:jspb.BinaryDecoder)} decodeMethod
  831. * @return {!Array}
  832. * @private
  833. */
  834. jspb.BinaryReader.prototype.readPackedField_ = function(decodeMethod) {
  835. goog.asserts.assert(
  836. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  837. var length = this.decoder_.readUnsignedVarint32();
  838. var end = this.decoder_.getCursor() + length;
  839. var result = [];
  840. while (this.decoder_.getCursor() < end) {
  841. // TODO(aappleby): .call is slow
  842. result.push(decodeMethod.call(this.decoder_));
  843. }
  844. return result;
  845. };
  846. /**
  847. * Reads a packed int32 field, which consists of a length header and a list of
  848. * signed varints.
  849. * @return {!Array<number>}
  850. */
  851. jspb.BinaryReader.prototype.readPackedInt32 = function() {
  852. return this.readPackedField_(this.decoder_.readSignedVarint32);
  853. };
  854. /**
  855. * Reads a packed int32 field, which consists of a length header and a list of
  856. * signed varints. Returns a list of strings.
  857. * @return {!Array<string>}
  858. */
  859. jspb.BinaryReader.prototype.readPackedInt32String = function() {
  860. return this.readPackedField_(this.decoder_.readSignedVarint32String);
  861. };
  862. /**
  863. * Reads a packed int64 field, which consists of a length header and a list of
  864. * signed varints.
  865. * @return {!Array<number>}
  866. */
  867. jspb.BinaryReader.prototype.readPackedInt64 = function() {
  868. return this.readPackedField_(this.decoder_.readSignedVarint64);
  869. };
  870. /**
  871. * Reads a packed int64 field, which consists of a length header and a list of
  872. * signed varints. Returns a list of strings.
  873. * @return {!Array<string>}
  874. */
  875. jspb.BinaryReader.prototype.readPackedInt64String = function() {
  876. return this.readPackedField_(this.decoder_.readSignedVarint64String);
  877. };
  878. /**
  879. * Reads a packed uint32 field, which consists of a length header and a list of
  880. * unsigned varints.
  881. * @return {!Array<number>}
  882. */
  883. jspb.BinaryReader.prototype.readPackedUint32 = function() {
  884. return this.readPackedField_(this.decoder_.readUnsignedVarint32);
  885. };
  886. /**
  887. * Reads a packed uint32 field, which consists of a length header and a list of
  888. * unsigned varints. Returns a list of strings.
  889. * @return {!Array<string>}
  890. */
  891. jspb.BinaryReader.prototype.readPackedUint32String = function() {
  892. return this.readPackedField_(this.decoder_.readUnsignedVarint32String);
  893. };
  894. /**
  895. * Reads a packed uint64 field, which consists of a length header and a list of
  896. * unsigned varints.
  897. * @return {!Array<number>}
  898. */
  899. jspb.BinaryReader.prototype.readPackedUint64 = function() {
  900. return this.readPackedField_(this.decoder_.readUnsignedVarint64);
  901. };
  902. /**
  903. * Reads a packed uint64 field, which consists of a length header and a list of
  904. * unsigned varints. Returns a list of strings.
  905. * @return {!Array<string>}
  906. */
  907. jspb.BinaryReader.prototype.readPackedUint64String = function() {
  908. return this.readPackedField_(this.decoder_.readUnsignedVarint64String);
  909. };
  910. /**
  911. * Reads a packed sint32 field, which consists of a length header and a list of
  912. * zigzag varints.
  913. * @return {!Array<number>}
  914. */
  915. jspb.BinaryReader.prototype.readPackedSint32 = function() {
  916. return this.readPackedField_(this.decoder_.readZigzagVarint32);
  917. };
  918. /**
  919. * Reads a packed sint64 field, which consists of a length header and a list of
  920. * zigzag varints.
  921. * @return {!Array<number>}
  922. */
  923. jspb.BinaryReader.prototype.readPackedSint64 = function() {
  924. return this.readPackedField_(this.decoder_.readZigzagVarint64);
  925. };
  926. /**
  927. * Reads a packed sint64 field, which consists of a length header and a list of
  928. * zigzag varints. Returns a list of strings.
  929. * @return {!Array<string>}
  930. */
  931. jspb.BinaryReader.prototype.readPackedSint64String = function() {
  932. return this.readPackedField_(this.decoder_.readZigzagVarint64String);
  933. };
  934. /**
  935. * Reads a packed fixed32 field, which consists of a length header and a list
  936. * of unsigned 32-bit ints.
  937. * @return {!Array<number>}
  938. */
  939. jspb.BinaryReader.prototype.readPackedFixed32 = function() {
  940. return this.readPackedField_(this.decoder_.readUint32);
  941. };
  942. /**
  943. * Reads a packed fixed64 field, which consists of a length header and a list
  944. * of unsigned 64-bit ints.
  945. * @return {!Array<number>}
  946. */
  947. jspb.BinaryReader.prototype.readPackedFixed64 = function() {
  948. return this.readPackedField_(this.decoder_.readUint64);
  949. };
  950. /**
  951. * Reads a packed fixed64 field, which consists of a length header and a list
  952. * of unsigned 64-bit ints. Returns a list of strings.
  953. * @return {!Array<number>}
  954. */
  955. jspb.BinaryReader.prototype.readPackedFixed64String = function() {
  956. return this.readPackedField_(this.decoder_.readUint64String);
  957. };
  958. /**
  959. * Reads a packed sfixed32 field, which consists of a length header and a list
  960. * of 32-bit ints.
  961. * @return {!Array<number>}
  962. */
  963. jspb.BinaryReader.prototype.readPackedSfixed32 = function() {
  964. return this.readPackedField_(this.decoder_.readInt32);
  965. };
  966. /**
  967. * Reads a packed sfixed64 field, which consists of a length header and a list
  968. * of 64-bit ints.
  969. * @return {!Array<number>}
  970. */
  971. jspb.BinaryReader.prototype.readPackedSfixed64 = function() {
  972. return this.readPackedField_(this.decoder_.readInt64);
  973. };
  974. /**
  975. * Reads a packed sfixed64 field, which consists of a length header and a list
  976. * of 64-bit ints. Returns a list of strings.
  977. * @return {!Array<string>}
  978. */
  979. jspb.BinaryReader.prototype.readPackedSfixed64String = function() {
  980. return this.readPackedField_(this.decoder_.readInt64String);
  981. };
  982. /**
  983. * Reads a packed float field, which consists of a length header and a list of
  984. * floats.
  985. * @return {!Array<number>}
  986. */
  987. jspb.BinaryReader.prototype.readPackedFloat = function() {
  988. return this.readPackedField_(this.decoder_.readFloat);
  989. };
  990. /**
  991. * Reads a packed double field, which consists of a length header and a list of
  992. * doubles.
  993. * @return {!Array<number>}
  994. */
  995. jspb.BinaryReader.prototype.readPackedDouble = function() {
  996. return this.readPackedField_(this.decoder_.readDouble);
  997. };
  998. /**
  999. * Reads a packed bool field, which consists of a length header and a list of
  1000. * unsigned varints.
  1001. * @return {!Array<boolean>}
  1002. */
  1003. jspb.BinaryReader.prototype.readPackedBool = function() {
  1004. return this.readPackedField_(this.decoder_.readBool);
  1005. };
  1006. /**
  1007. * Reads a packed enum field, which consists of a length header and a list of
  1008. * unsigned varints.
  1009. * @return {!Array<number>}
  1010. */
  1011. jspb.BinaryReader.prototype.readPackedEnum = function() {
  1012. return this.readPackedField_(this.decoder_.readEnum);
  1013. };
  1014. /**
  1015. * Reads a packed varint hash64 field, which consists of a length header and a
  1016. * list of varint hash64s.
  1017. * @return {!Array<string>}
  1018. */
  1019. jspb.BinaryReader.prototype.readPackedVarintHash64 = function() {
  1020. return this.readPackedField_(this.decoder_.readVarintHash64);
  1021. };
  1022. /**
  1023. * Reads a packed fixed hash64 field, which consists of a length header and a
  1024. * list of fixed hash64s.
  1025. * @return {!Array<string>}
  1026. */
  1027. jspb.BinaryReader.prototype.readPackedFixedHash64 = function() {
  1028. return this.readPackedField_(this.decoder_.readFixedHash64);
  1029. };