reader.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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('Invalid wire type');
  248. this.error_ = true;
  249. return false;
  250. }
  251. this.nextField_ = nextField;
  252. this.nextWireType_ = nextWireType;
  253. return true;
  254. };
  255. /**
  256. * Winds the reader back to just before this field's header.
  257. */
  258. jspb.BinaryReader.prototype.unskipHeader = function() {
  259. this.decoder_.unskipVarint((this.nextField_ << 3) | this.nextWireType_);
  260. };
  261. /**
  262. * Skips all contiguous fields whose header matches the one we just read.
  263. */
  264. jspb.BinaryReader.prototype.skipMatchingFields = function() {
  265. var field = this.nextField_;
  266. this.unskipHeader();
  267. while (this.nextField() && (this.getFieldNumber() == field)) {
  268. this.skipField();
  269. }
  270. if (!this.decoder_.atEnd()) {
  271. this.unskipHeader();
  272. }
  273. };
  274. /**
  275. * Skips over the next varint field in the binary stream.
  276. */
  277. jspb.BinaryReader.prototype.skipVarintField = function() {
  278. if (this.nextWireType_ != jspb.BinaryConstants.WireType.VARINT) {
  279. goog.asserts.fail('Invalid wire type for skipVarintField');
  280. this.skipField();
  281. return;
  282. }
  283. this.decoder_.skipVarint();
  284. };
  285. /**
  286. * Skips over the next delimited field in the binary stream.
  287. */
  288. jspb.BinaryReader.prototype.skipDelimitedField = function() {
  289. if (this.nextWireType_ != jspb.BinaryConstants.WireType.DELIMITED) {
  290. goog.asserts.fail('Invalid wire type for skipDelimitedField');
  291. this.skipField();
  292. return;
  293. }
  294. var length = this.decoder_.readUnsignedVarint32();
  295. this.decoder_.advance(length);
  296. };
  297. /**
  298. * Skips over the next fixed32 field in the binary stream.
  299. */
  300. jspb.BinaryReader.prototype.skipFixed32Field = function() {
  301. if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED32) {
  302. goog.asserts.fail('Invalid wire type for skipFixed32Field');
  303. this.skipField();
  304. return;
  305. }
  306. this.decoder_.advance(4);
  307. };
  308. /**
  309. * Skips over the next fixed64 field in the binary stream.
  310. */
  311. jspb.BinaryReader.prototype.skipFixed64Field = function() {
  312. if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED64) {
  313. goog.asserts.fail('Invalid wire type for skipFixed64Field');
  314. this.skipField();
  315. return;
  316. }
  317. this.decoder_.advance(8);
  318. };
  319. /**
  320. * Skips over the next group field in the binary stream.
  321. */
  322. jspb.BinaryReader.prototype.skipGroup = function() {
  323. // Keep a stack of start-group tags that must be matched by end-group tags.
  324. var nestedGroups = [this.nextField_];
  325. do {
  326. if (!this.nextField()) {
  327. goog.asserts.fail('Unmatched start-group tag: stream EOF');
  328. this.error_ = true;
  329. return;
  330. }
  331. if (this.nextWireType_ ==
  332. jspb.BinaryConstants.WireType.START_GROUP) {
  333. // Nested group start.
  334. nestedGroups.push(this.nextField_);
  335. } else if (this.nextWireType_ ==
  336. jspb.BinaryConstants.WireType.END_GROUP) {
  337. // Group end: check that it matches top-of-stack.
  338. if (this.nextField_ != nestedGroups.pop()) {
  339. goog.asserts.fail('Unmatched end-group tag');
  340. this.error_ = true;
  341. return;
  342. }
  343. }
  344. } while (nestedGroups.length > 0);
  345. };
  346. /**
  347. * Skips over the next field in the binary stream - this is useful if we're
  348. * decoding a message that contain unknown fields.
  349. */
  350. jspb.BinaryReader.prototype.skipField = function() {
  351. switch (this.nextWireType_) {
  352. case jspb.BinaryConstants.WireType.VARINT:
  353. this.skipVarintField();
  354. break;
  355. case jspb.BinaryConstants.WireType.FIXED64:
  356. this.skipFixed64Field();
  357. break;
  358. case jspb.BinaryConstants.WireType.DELIMITED:
  359. this.skipDelimitedField();
  360. break;
  361. case jspb.BinaryConstants.WireType.FIXED32:
  362. this.skipFixed32Field();
  363. break;
  364. case jspb.BinaryConstants.WireType.START_GROUP:
  365. this.skipGroup();
  366. break;
  367. default:
  368. goog.asserts.fail('Invalid wire encoding for field.');
  369. }
  370. };
  371. /**
  372. * Registers a user-defined read callback.
  373. * @param {string} callbackName
  374. * @param {function(!jspb.BinaryReader):*} callback
  375. */
  376. jspb.BinaryReader.prototype.registerReadCallback =
  377. function(callbackName, callback) {
  378. if (goog.isNull(this.readCallbacks_)) {
  379. this.readCallbacks_ = {};
  380. }
  381. goog.asserts.assert(!this.readCallbacks_[callbackName]);
  382. this.readCallbacks_[callbackName] = callback;
  383. };
  384. /**
  385. * Runs a registered read callback.
  386. * @param {string} callbackName The name the callback is registered under.
  387. * @return {*} The value returned by the callback.
  388. */
  389. jspb.BinaryReader.prototype.runReadCallback = function(callbackName) {
  390. goog.asserts.assert(!goog.isNull(this.readCallbacks_));
  391. var callback = this.readCallbacks_[callbackName];
  392. goog.asserts.assert(callback);
  393. return callback(this);
  394. };
  395. /**
  396. * Reads a field of any valid non-message type from the binary stream.
  397. * @param {jspb.BinaryConstants.FieldType} fieldType
  398. * @return {jspb.AnyFieldType}
  399. */
  400. jspb.BinaryReader.prototype.readAny = function(fieldType) {
  401. this.nextWireType_ = jspb.BinaryConstants.FieldTypeToWireType(fieldType);
  402. var fieldTypes = jspb.BinaryConstants.FieldType;
  403. switch (fieldType) {
  404. case fieldTypes.DOUBLE:
  405. return this.readDouble();
  406. case fieldTypes.FLOAT:
  407. return this.readFloat();
  408. case fieldTypes.INT64:
  409. return this.readInt64();
  410. case fieldTypes.UINT64:
  411. return this.readUint64();
  412. case fieldTypes.INT32:
  413. return this.readInt32();
  414. case fieldTypes.FIXED64:
  415. return this.readFixed64();
  416. case fieldTypes.FIXED32:
  417. return this.readFixed32();
  418. case fieldTypes.BOOL:
  419. return this.readBool();
  420. case fieldTypes.STRING:
  421. return this.readString();
  422. case fieldTypes.GROUP:
  423. goog.asserts.fail('Group field type not supported in readAny()');
  424. case fieldTypes.MESSAGE:
  425. goog.asserts.fail('Message field type not supported in readAny()');
  426. case fieldTypes.BYTES:
  427. return this.readBytes();
  428. case fieldTypes.UINT32:
  429. return this.readUint32();
  430. case fieldTypes.ENUM:
  431. return this.readEnum();
  432. case fieldTypes.SFIXED32:
  433. return this.readSfixed32();
  434. case fieldTypes.SFIXED64:
  435. return this.readSfixed64();
  436. case fieldTypes.SINT32:
  437. return this.readSint32();
  438. case fieldTypes.SINT64:
  439. return this.readSint64();
  440. case fieldTypes.FHASH64:
  441. return this.readFixedHash64();
  442. case fieldTypes.VHASH64:
  443. return this.readVarintHash64();
  444. default:
  445. goog.asserts.fail('Invalid field type in readAny()');
  446. }
  447. return 0;
  448. };
  449. /**
  450. * Deserialize a proto into the provided message object using the provided
  451. * reader function. This function is templated as we currently have one client
  452. * who is using manual deserialization instead of the code-generated versions.
  453. * @template T
  454. * @param {T} message
  455. * @param {function(T, !jspb.BinaryReader)} reader
  456. */
  457. jspb.BinaryReader.prototype.readMessage = function(message, reader) {
  458. goog.asserts.assert(
  459. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  460. // Save the current endpoint of the decoder and move it to the end of the
  461. // embedded message.
  462. var oldEnd = this.decoder_.getEnd();
  463. var length = this.decoder_.readUnsignedVarint32();
  464. var newEnd = this.decoder_.getCursor() + length;
  465. this.decoder_.setEnd(newEnd);
  466. // Deserialize the embedded message.
  467. reader(message, this);
  468. // Advance the decoder past the embedded message and restore the endpoint.
  469. this.decoder_.setCursor(newEnd);
  470. this.decoder_.setEnd(oldEnd);
  471. };
  472. /**
  473. * Deserialize a proto into the provided message object using the provided
  474. * reader function, assuming that the message is serialized as a group
  475. * with the given tag.
  476. * @template T
  477. * @param {number} field
  478. * @param {T} message
  479. * @param {function(T, !jspb.BinaryReader)} reader
  480. */
  481. jspb.BinaryReader.prototype.readGroup =
  482. function(field, message, reader) {
  483. // Ensure that the wire type is correct.
  484. goog.asserts.assert(
  485. this.nextWireType_ == jspb.BinaryConstants.WireType.START_GROUP);
  486. // Ensure that the field number is correct.
  487. goog.asserts.assert(this.nextField_ == field);
  488. // Deserialize the message. The deserialization will stop at an END_GROUP tag.
  489. reader(message, this);
  490. if (!this.error_ &&
  491. this.nextWireType_ != jspb.BinaryConstants.WireType.END_GROUP) {
  492. goog.asserts.fail('Group submessage did not end with an END_GROUP tag');
  493. this.error_ = true;
  494. }
  495. };
  496. /**
  497. * Return a decoder that wraps the current delimited field.
  498. * @return {!jspb.BinaryDecoder}
  499. */
  500. jspb.BinaryReader.prototype.getFieldDecoder = function() {
  501. goog.asserts.assert(
  502. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  503. var length = this.decoder_.readUnsignedVarint32();
  504. var start = this.decoder_.getCursor();
  505. var end = start + length;
  506. var innerDecoder =
  507. jspb.BinaryDecoder.alloc(this.decoder_.getBuffer(), start, length);
  508. this.decoder_.setCursor(end);
  509. return innerDecoder;
  510. };
  511. /**
  512. * Reads a signed 32-bit integer field from the binary stream, or throws an
  513. * error if the next field in the stream is not of the correct wire type.
  514. *
  515. * @return {number} The value of the signed 32-bit integer field.
  516. */
  517. jspb.BinaryReader.prototype.readInt32 = function() {
  518. goog.asserts.assert(
  519. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  520. return this.decoder_.readSignedVarint32();
  521. };
  522. /**
  523. * Reads a signed 32-bit integer field from the binary stream, or throws an
  524. * error if the next field in the stream is not of the correct wire type.
  525. *
  526. * Returns the value as a string.
  527. *
  528. * @return {string} The value of the signed 32-bit integer field as a decimal
  529. * string.
  530. */
  531. jspb.BinaryReader.prototype.readInt32String = function() {
  532. goog.asserts.assert(
  533. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  534. return this.decoder_.readSignedVarint32String();
  535. };
  536. /**
  537. * Reads a signed 64-bit integer field from the binary stream, or throws an
  538. * error if the next field in the stream is not of the correct wire type.
  539. *
  540. * @return {number} The value of the signed 64-bit integer field.
  541. */
  542. jspb.BinaryReader.prototype.readInt64 = function() {
  543. goog.asserts.assert(
  544. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  545. return this.decoder_.readSignedVarint64();
  546. };
  547. /**
  548. * Reads a signed 64-bit integer field from the binary stream, or throws an
  549. * error if the next field in the stream is not of the correct wire type.
  550. *
  551. * Returns the value as a string.
  552. *
  553. * @return {string} The value of the signed 64-bit integer field as a decimal
  554. * string.
  555. */
  556. jspb.BinaryReader.prototype.readInt64String = function() {
  557. goog.asserts.assert(
  558. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  559. return this.decoder_.readSignedVarint64String();
  560. };
  561. /**
  562. * Reads an unsigned 32-bit integer field from the binary stream, or throws an
  563. * error if the next field in the stream is not of the correct wire type.
  564. *
  565. * @return {number} The value of the unsigned 32-bit integer field.
  566. */
  567. jspb.BinaryReader.prototype.readUint32 = function() {
  568. goog.asserts.assert(
  569. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  570. return this.decoder_.readUnsignedVarint32();
  571. };
  572. /**
  573. * Reads an unsigned 32-bit integer field from the binary stream, or throws an
  574. * error if the next field in the stream is not of the correct wire type.
  575. *
  576. * Returns the value as a string.
  577. *
  578. * @return {string} The value of the unsigned 32-bit integer field as a decimal
  579. * string.
  580. */
  581. jspb.BinaryReader.prototype.readUint32String = function() {
  582. goog.asserts.assert(
  583. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  584. return this.decoder_.readUnsignedVarint32String();
  585. };
  586. /**
  587. * Reads an unsigned 64-bit integer field from the binary stream, or throws an
  588. * error if the next field in the stream is not of the correct wire type.
  589. *
  590. * @return {number} The value of the unsigned 64-bit integer field.
  591. */
  592. jspb.BinaryReader.prototype.readUint64 = function() {
  593. goog.asserts.assert(
  594. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  595. return this.decoder_.readUnsignedVarint64();
  596. };
  597. /**
  598. * Reads an unsigned 64-bit integer field from the binary stream, or throws an
  599. * error if the next field in the stream is not of the correct wire type.
  600. *
  601. * Returns the value as a string.
  602. *
  603. * @return {string} The value of the unsigned 64-bit integer field as a decimal
  604. * string.
  605. */
  606. jspb.BinaryReader.prototype.readUint64String = function() {
  607. goog.asserts.assert(
  608. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  609. return this.decoder_.readUnsignedVarint64String();
  610. };
  611. /**
  612. * Reads a signed zigzag-encoded 32-bit integer field from the binary stream,
  613. * or throws an error if the next field in the stream is not of the correct
  614. * wire type.
  615. *
  616. * @return {number} The value of the signed 32-bit integer field.
  617. */
  618. jspb.BinaryReader.prototype.readSint32 = function() {
  619. goog.asserts.assert(
  620. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  621. return this.decoder_.readZigzagVarint32();
  622. };
  623. /**
  624. * Reads a signed zigzag-encoded 64-bit integer field from the binary stream,
  625. * or throws an error if the next field in the stream is not of the correct
  626. * wire type.
  627. *
  628. * @return {number} The value of the signed 64-bit integer field.
  629. */
  630. jspb.BinaryReader.prototype.readSint64 = function() {
  631. goog.asserts.assert(
  632. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  633. return this.decoder_.readZigzagVarint64();
  634. };
  635. /**
  636. * Reads an unsigned 32-bit fixed-length integer fiield from the binary stream,
  637. * or throws an error if the next field in the stream is not of the correct
  638. * wire type.
  639. *
  640. * @return {number} The value of the double field.
  641. */
  642. jspb.BinaryReader.prototype.readFixed32 = function() {
  643. goog.asserts.assert(
  644. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  645. return this.decoder_.readUint32();
  646. };
  647. /**
  648. * Reads an unsigned 64-bit fixed-length integer fiield from the binary stream,
  649. * or throws an error if the next field in the stream is not of the correct
  650. * wire type.
  651. *
  652. * @return {number} The value of the float field.
  653. */
  654. jspb.BinaryReader.prototype.readFixed64 = function() {
  655. goog.asserts.assert(
  656. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  657. return this.decoder_.readUint64();
  658. };
  659. /**
  660. * Reads a signed 32-bit fixed-length integer fiield from the binary stream, or
  661. * throws an error if the next field in the stream is not of the correct wire
  662. * type.
  663. *
  664. * @return {number} The value of the double field.
  665. */
  666. jspb.BinaryReader.prototype.readSfixed32 = function() {
  667. goog.asserts.assert(
  668. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  669. return this.decoder_.readInt32();
  670. };
  671. /**
  672. * Reads a signed 64-bit fixed-length integer fiield from the binary stream, or
  673. * throws an error if the next field in the stream is not of the correct wire
  674. * type.
  675. *
  676. * @return {number} The value of the float field.
  677. */
  678. jspb.BinaryReader.prototype.readSfixed64 = function() {
  679. goog.asserts.assert(
  680. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  681. return this.decoder_.readInt64();
  682. };
  683. /**
  684. * Reads a 32-bit floating-point field from the binary stream, or throws an
  685. * error if the next field in the stream is not of the correct wire type.
  686. *
  687. * @return {number} The value of the float field.
  688. */
  689. jspb.BinaryReader.prototype.readFloat = function() {
  690. goog.asserts.assert(
  691. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  692. return this.decoder_.readFloat();
  693. };
  694. /**
  695. * Reads a 64-bit floating-point field from the binary stream, or throws an
  696. * error if the next field in the stream is not of the correct wire type.
  697. *
  698. * @return {number} The value of the double field.
  699. */
  700. jspb.BinaryReader.prototype.readDouble = function() {
  701. goog.asserts.assert(
  702. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  703. return this.decoder_.readDouble();
  704. };
  705. /**
  706. * Reads a boolean field from the binary stream, or throws an error if the next
  707. * field in the stream is not of the correct wire type.
  708. *
  709. * @return {boolean} The value of the boolean field.
  710. */
  711. jspb.BinaryReader.prototype.readBool = function() {
  712. goog.asserts.assert(
  713. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  714. return !!this.decoder_.readUnsignedVarint32();
  715. };
  716. /**
  717. * Reads an enum field from the binary stream, or throws an error if the next
  718. * field in the stream is not of the correct wire type.
  719. *
  720. * @return {number} The value of the enum field.
  721. */
  722. jspb.BinaryReader.prototype.readEnum = function() {
  723. goog.asserts.assert(
  724. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  725. return this.decoder_.readSignedVarint64();
  726. };
  727. /**
  728. * Reads a string field from the binary stream, or throws an error if the next
  729. * field in the stream is not of the correct wire type.
  730. *
  731. * @return {string} The value of the string field.
  732. */
  733. jspb.BinaryReader.prototype.readString = function() {
  734. goog.asserts.assert(
  735. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  736. var length = this.decoder_.readUnsignedVarint32();
  737. return this.decoder_.readString(length);
  738. };
  739. /**
  740. * Reads a length-prefixed block of bytes from the binary stream, or returns
  741. * null if the next field in the stream has an invalid length value.
  742. *
  743. * @return {!Uint8Array} The block of bytes.
  744. */
  745. jspb.BinaryReader.prototype.readBytes = function() {
  746. goog.asserts.assert(
  747. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  748. var length = this.decoder_.readUnsignedVarint32();
  749. return this.decoder_.readBytes(length);
  750. };
  751. /**
  752. * Reads a 64-bit varint or fixed64 field from the stream and returns it as a
  753. * 8-character Unicode string for use as a hash table key, or throws an error
  754. * if the next field in the stream is not of the correct wire type.
  755. *
  756. * @return {string} The hash value.
  757. */
  758. jspb.BinaryReader.prototype.readVarintHash64 = function() {
  759. goog.asserts.assert(
  760. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  761. return this.decoder_.readVarintHash64();
  762. };
  763. /**
  764. * Reads a 64-bit varint or fixed64 field from the stream and returns it as a
  765. * 8-character Unicode string for use as a hash table key, or throws an error
  766. * if the next field in the stream is not of the correct wire type.
  767. *
  768. * @return {string} The hash value.
  769. */
  770. jspb.BinaryReader.prototype.readFixedHash64 = function() {
  771. goog.asserts.assert(
  772. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  773. return this.decoder_.readFixedHash64();
  774. };
  775. /**
  776. * Reads a packed scalar field using the supplied raw reader function.
  777. * @param {function()} decodeMethod
  778. * @return {!Array}
  779. * @private
  780. */
  781. jspb.BinaryReader.prototype.readPackedField_ = function(decodeMethod) {
  782. goog.asserts.assert(
  783. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  784. var length = this.decoder_.readUnsignedVarint32();
  785. var end = this.decoder_.getCursor() + length;
  786. var result = [];
  787. while (this.decoder_.getCursor() < end) {
  788. // TODO(aappleby): .call is slow
  789. result.push(decodeMethod.call(this.decoder_));
  790. }
  791. return result;
  792. };
  793. /**
  794. * Reads a packed int32 field, which consists of a length header and a list of
  795. * signed varints.
  796. * @return {!Array.<number>}
  797. */
  798. jspb.BinaryReader.prototype.readPackedInt32 = function() {
  799. return this.readPackedField_(this.decoder_.readSignedVarint32);
  800. };
  801. /**
  802. * Reads a packed int32 field, which consists of a length header and a list of
  803. * signed varints. Returns a list of strings.
  804. * @return {!Array.<string>}
  805. */
  806. jspb.BinaryReader.prototype.readPackedInt32String = function() {
  807. return this.readPackedField_(this.decoder_.readSignedVarint32String);
  808. };
  809. /**
  810. * Reads a packed int64 field, which consists of a length header and a list of
  811. * signed varints.
  812. * @return {!Array.<number>}
  813. */
  814. jspb.BinaryReader.prototype.readPackedInt64 = function() {
  815. return this.readPackedField_(this.decoder_.readSignedVarint64);
  816. };
  817. /**
  818. * Reads a packed int64 field, which consists of a length header and a list of
  819. * signed varints. Returns a list of strings.
  820. * @return {!Array.<string>}
  821. */
  822. jspb.BinaryReader.prototype.readPackedInt64String = function() {
  823. return this.readPackedField_(this.decoder_.readSignedVarint64String);
  824. };
  825. /**
  826. * Reads a packed uint32 field, which consists of a length header and a list of
  827. * unsigned varints.
  828. * @return {!Array.<number>}
  829. */
  830. jspb.BinaryReader.prototype.readPackedUint32 = function() {
  831. return this.readPackedField_(this.decoder_.readUnsignedVarint32);
  832. };
  833. /**
  834. * Reads a packed uint32 field, which consists of a length header and a list of
  835. * unsigned varints. Returns a list of strings.
  836. * @return {!Array.<string>}
  837. */
  838. jspb.BinaryReader.prototype.readPackedUint32String = function() {
  839. return this.readPackedField_(this.decoder_.readUnsignedVarint32String);
  840. };
  841. /**
  842. * Reads a packed uint64 field, which consists of a length header and a list of
  843. * unsigned varints.
  844. * @return {!Array.<number>}
  845. */
  846. jspb.BinaryReader.prototype.readPackedUint64 = function() {
  847. return this.readPackedField_(this.decoder_.readUnsignedVarint64);
  848. };
  849. /**
  850. * Reads a packed uint64 field, which consists of a length header and a list of
  851. * unsigned varints. Returns a list of strings.
  852. * @return {!Array.<string>}
  853. */
  854. jspb.BinaryReader.prototype.readPackedUint64String = function() {
  855. return this.readPackedField_(this.decoder_.readUnsignedVarint64String);
  856. };
  857. /**
  858. * Reads a packed sint32 field, which consists of a length header and a list of
  859. * zigzag varints.
  860. * @return {!Array.<number>}
  861. */
  862. jspb.BinaryReader.prototype.readPackedSint32 = function() {
  863. return this.readPackedField_(this.decoder_.readZigzagVarint32);
  864. };
  865. /**
  866. * Reads a packed sint64 field, which consists of a length header and a list of
  867. * zigzag varints.
  868. * @return {!Array.<number>}
  869. */
  870. jspb.BinaryReader.prototype.readPackedSint64 = function() {
  871. return this.readPackedField_(this.decoder_.readZigzagVarint64);
  872. };
  873. /**
  874. * Reads a packed fixed32 field, which consists of a length header and a list
  875. * of unsigned 32-bit ints.
  876. * @return {!Array.<number>}
  877. */
  878. jspb.BinaryReader.prototype.readPackedFixed32 = function() {
  879. return this.readPackedField_(this.decoder_.readUint32);
  880. };
  881. /**
  882. * Reads a packed fixed64 field, which consists of a length header and a list
  883. * of unsigned 64-bit ints.
  884. * @return {!Array.<number>}
  885. */
  886. jspb.BinaryReader.prototype.readPackedFixed64 = function() {
  887. return this.readPackedField_(this.decoder_.readUint64);
  888. };
  889. /**
  890. * Reads a packed sfixed32 field, which consists of a length header and a list
  891. * of 32-bit ints.
  892. * @return {!Array.<number>}
  893. */
  894. jspb.BinaryReader.prototype.readPackedSfixed32 = function() {
  895. return this.readPackedField_(this.decoder_.readInt32);
  896. };
  897. /**
  898. * Reads a packed sfixed64 field, which consists of a length header and a list
  899. * of 64-bit ints.
  900. * @return {!Array.<number>}
  901. */
  902. jspb.BinaryReader.prototype.readPackedSfixed64 = function() {
  903. return this.readPackedField_(this.decoder_.readInt64);
  904. };
  905. /**
  906. * Reads a packed float field, which consists of a length header and a list of
  907. * floats.
  908. * @return {!Array.<number>}
  909. */
  910. jspb.BinaryReader.prototype.readPackedFloat = function() {
  911. return this.readPackedField_(this.decoder_.readFloat);
  912. };
  913. /**
  914. * Reads a packed double field, which consists of a length header and a list of
  915. * doubles.
  916. * @return {!Array.<number>}
  917. */
  918. jspb.BinaryReader.prototype.readPackedDouble = function() {
  919. return this.readPackedField_(this.decoder_.readDouble);
  920. };
  921. /**
  922. * Reads a packed bool field, which consists of a length header and a list of
  923. * unsigned varints.
  924. * @return {!Array.<boolean>}
  925. */
  926. jspb.BinaryReader.prototype.readPackedBool = function() {
  927. return this.readPackedField_(this.decoder_.readBool);
  928. };
  929. /**
  930. * Reads a packed enum field, which consists of a length header and a list of
  931. * unsigned varints.
  932. * @return {!Array.<number>}
  933. */
  934. jspb.BinaryReader.prototype.readPackedEnum = function() {
  935. return this.readPackedField_(this.decoder_.readEnum);
  936. };
  937. /**
  938. * Reads a packed varint hash64 field, which consists of a length header and a
  939. * list of varint hash64s.
  940. * @return {!Array.<string>}
  941. */
  942. jspb.BinaryReader.prototype.readPackedVarintHash64 = function() {
  943. return this.readPackedField_(this.decoder_.readVarintHash64);
  944. };
  945. /**
  946. * Reads a packed fixed hash64 field, which consists of a length header and a
  947. * list of fixed hash64s.
  948. * @return {!Array.<string>}
  949. */
  950. jspb.BinaryReader.prototype.readPackedFixedHash64 = function() {
  951. return this.readPackedField_(this.decoder_.readFixedHash64);
  952. };