reader.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. goog.require('jspb.utils');
  50. /**
  51. * BinaryReader implements the decoders for all the wire types specified in
  52. * https://developers.google.com/protocol-buffers/docs/encoding.
  53. *
  54. * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from.
  55. * @param {number=} opt_start The optional offset to start reading at.
  56. * @param {number=} opt_length The optional length of the block to read -
  57. * we'll throw an assertion if we go off the end of the block.
  58. * @constructor
  59. * @struct
  60. */
  61. jspb.BinaryReader = function(opt_bytes, opt_start, opt_length) {
  62. /**
  63. * Wire-format decoder.
  64. * @private {!jspb.BinaryDecoder}
  65. */
  66. this.decoder_ = jspb.BinaryDecoder.alloc(opt_bytes, opt_start, opt_length);
  67. /**
  68. * Cursor immediately before the field tag.
  69. * @private {number}
  70. */
  71. this.fieldCursor_ = this.decoder_.getCursor();
  72. /**
  73. * Field number of the next field in the buffer, filled in by nextField().
  74. * @private {number}
  75. */
  76. this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER;
  77. /**
  78. * Wire type of the next proto field in the buffer, filled in by
  79. * nextField().
  80. * @private {jspb.BinaryConstants.WireType}
  81. */
  82. this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID;
  83. /**
  84. * Set to true if this reader encountered an error due to corrupt data.
  85. * @private {boolean}
  86. */
  87. this.error_ = false;
  88. /**
  89. * User-defined reader callbacks.
  90. * @private {?Object<string, function(!jspb.BinaryReader):*>}
  91. */
  92. this.readCallbacks_ = null;
  93. };
  94. /**
  95. * Global pool of BinaryReader instances.
  96. * @private {!Array<!jspb.BinaryReader>}
  97. */
  98. jspb.BinaryReader.instanceCache_ = [];
  99. /**
  100. * Pops an instance off the instance cache, or creates one if the cache is
  101. * empty.
  102. * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from.
  103. * @param {number=} opt_start The optional offset to start reading at.
  104. * @param {number=} opt_length The optional length of the block to read -
  105. * we'll throw an assertion if we go off the end of the block.
  106. * @return {!jspb.BinaryReader}
  107. */
  108. jspb.BinaryReader.alloc =
  109. function(opt_bytes, opt_start, opt_length) {
  110. if (jspb.BinaryReader.instanceCache_.length) {
  111. var newReader = jspb.BinaryReader.instanceCache_.pop();
  112. if (opt_bytes) {
  113. newReader.decoder_.setBlock(opt_bytes, opt_start, opt_length);
  114. }
  115. return newReader;
  116. } else {
  117. return new jspb.BinaryReader(opt_bytes, opt_start, opt_length);
  118. }
  119. };
  120. /**
  121. * Alias for the above method.
  122. * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from.
  123. * @param {number=} opt_start The optional offset to start reading at.
  124. * @param {number=} opt_length The optional length of the block to read -
  125. * we'll throw an assertion if we go off the end of the block.
  126. * @return {!jspb.BinaryReader}
  127. */
  128. jspb.BinaryReader.prototype.alloc = jspb.BinaryReader.alloc;
  129. /**
  130. * Puts this instance back in the instance cache.
  131. */
  132. jspb.BinaryReader.prototype.free = function() {
  133. this.decoder_.clear();
  134. this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER;
  135. this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID;
  136. this.error_ = false;
  137. this.readCallbacks_ = null;
  138. if (jspb.BinaryReader.instanceCache_.length < 100) {
  139. jspb.BinaryReader.instanceCache_.push(this);
  140. }
  141. };
  142. /**
  143. * Returns the cursor immediately before the current field's tag.
  144. * @return {number} The internal read cursor.
  145. */
  146. jspb.BinaryReader.prototype.getFieldCursor = function() {
  147. return this.fieldCursor_;
  148. };
  149. /**
  150. * Returns the internal read cursor.
  151. * @return {number} The internal read cursor.
  152. */
  153. jspb.BinaryReader.prototype.getCursor = function() {
  154. return this.decoder_.getCursor();
  155. };
  156. /**
  157. * Returns the raw buffer.
  158. * @return {?Uint8Array} The raw buffer.
  159. */
  160. jspb.BinaryReader.prototype.getBuffer = function() {
  161. return this.decoder_.getBuffer();
  162. };
  163. /**
  164. * @return {number} The field number of the next field in the buffer, or
  165. * INVALID_FIELD_NUMBER if there is no next field.
  166. */
  167. jspb.BinaryReader.prototype.getFieldNumber = function() {
  168. return this.nextField_;
  169. };
  170. /**
  171. * @return {jspb.BinaryConstants.WireType} The wire type of the next field
  172. * in the stream, or WireType.INVALID if there is no next field.
  173. */
  174. jspb.BinaryReader.prototype.getWireType = function() {
  175. return this.nextWireType_;
  176. };
  177. /**
  178. * @return {boolean} Whether the current wire type is an end-group tag. Used as
  179. * an exit condition in decoder loops in generated code.
  180. */
  181. jspb.BinaryReader.prototype.isEndGroup = function() {
  182. return this.nextWireType_ == jspb.BinaryConstants.WireType.END_GROUP;
  183. };
  184. /**
  185. * Returns true if this reader hit an error due to corrupt data.
  186. * @return {boolean}
  187. */
  188. jspb.BinaryReader.prototype.getError = function() {
  189. return this.error_ || this.decoder_.getError();
  190. };
  191. /**
  192. * Points this reader at a new block of bytes.
  193. * @param {!Uint8Array} bytes The block of bytes we're reading from.
  194. * @param {number} start The offset to start reading at.
  195. * @param {number} length The length of the block to read.
  196. */
  197. jspb.BinaryReader.prototype.setBlock = function(bytes, start, length) {
  198. this.decoder_.setBlock(bytes, start, length);
  199. this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER;
  200. this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID;
  201. };
  202. /**
  203. * Rewinds the stream cursor to the beginning of the buffer and resets all
  204. * internal state.
  205. */
  206. jspb.BinaryReader.prototype.reset = function() {
  207. this.decoder_.reset();
  208. this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER;
  209. this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID;
  210. };
  211. /**
  212. * Advances the stream cursor by the given number of bytes.
  213. * @param {number} count The number of bytes to advance by.
  214. */
  215. jspb.BinaryReader.prototype.advance = function(count) {
  216. this.decoder_.advance(count);
  217. };
  218. /**
  219. * Reads the next field header in the stream if there is one, returns true if
  220. * we saw a valid field header or false if we've read the whole stream.
  221. * Throws an error if we encountered a deprecated START_GROUP/END_GROUP field.
  222. * @return {boolean} True if the stream contains more fields.
  223. */
  224. jspb.BinaryReader.prototype.nextField = function() {
  225. // If we're at the end of the block, there are no more fields.
  226. if (this.decoder_.atEnd()) {
  227. return false;
  228. }
  229. // If we hit an error decoding the previous field, stop now before we
  230. // try to decode anything else
  231. if (this.getError()) {
  232. goog.asserts.fail('Decoder hit an error');
  233. return false;
  234. }
  235. // Otherwise just read the header of the next field.
  236. this.fieldCursor_ = this.decoder_.getCursor();
  237. var header = this.decoder_.readUnsignedVarint32();
  238. var nextField = header >>> 3;
  239. var nextWireType = /** @type {jspb.BinaryConstants.WireType} */
  240. (header & 0x7);
  241. // If the wire type isn't one of the valid ones, something's broken.
  242. if (nextWireType != jspb.BinaryConstants.WireType.VARINT &&
  243. nextWireType != jspb.BinaryConstants.WireType.FIXED32 &&
  244. nextWireType != jspb.BinaryConstants.WireType.FIXED64 &&
  245. nextWireType != jspb.BinaryConstants.WireType.DELIMITED &&
  246. nextWireType != jspb.BinaryConstants.WireType.START_GROUP &&
  247. nextWireType != jspb.BinaryConstants.WireType.END_GROUP) {
  248. goog.asserts.fail(
  249. 'Invalid wire type: %s (at position %s)', nextWireType,
  250. this.fieldCursor_);
  251. this.error_ = true;
  252. return false;
  253. }
  254. this.nextField_ = nextField;
  255. this.nextWireType_ = nextWireType;
  256. return true;
  257. };
  258. /**
  259. * Winds the reader back to just before this field's header.
  260. */
  261. jspb.BinaryReader.prototype.unskipHeader = function() {
  262. this.decoder_.unskipVarint((this.nextField_ << 3) | this.nextWireType_);
  263. };
  264. /**
  265. * Skips all contiguous fields whose header matches the one we just read.
  266. */
  267. jspb.BinaryReader.prototype.skipMatchingFields = function() {
  268. var field = this.nextField_;
  269. this.unskipHeader();
  270. while (this.nextField() && (this.getFieldNumber() == field)) {
  271. this.skipField();
  272. }
  273. if (!this.decoder_.atEnd()) {
  274. this.unskipHeader();
  275. }
  276. };
  277. /**
  278. * Skips over the next varint field in the binary stream.
  279. */
  280. jspb.BinaryReader.prototype.skipVarintField = function() {
  281. if (this.nextWireType_ != jspb.BinaryConstants.WireType.VARINT) {
  282. goog.asserts.fail('Invalid wire type for skipVarintField');
  283. this.skipField();
  284. return;
  285. }
  286. this.decoder_.skipVarint();
  287. };
  288. /**
  289. * Skips over the next delimited field in the binary stream.
  290. */
  291. jspb.BinaryReader.prototype.skipDelimitedField = function() {
  292. if (this.nextWireType_ != jspb.BinaryConstants.WireType.DELIMITED) {
  293. goog.asserts.fail('Invalid wire type for skipDelimitedField');
  294. this.skipField();
  295. return;
  296. }
  297. var length = this.decoder_.readUnsignedVarint32();
  298. this.decoder_.advance(length);
  299. };
  300. /**
  301. * Skips over the next fixed32 field in the binary stream.
  302. */
  303. jspb.BinaryReader.prototype.skipFixed32Field = function() {
  304. if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED32) {
  305. goog.asserts.fail('Invalid wire type for skipFixed32Field');
  306. this.skipField();
  307. return;
  308. }
  309. this.decoder_.advance(4);
  310. };
  311. /**
  312. * Skips over the next fixed64 field in the binary stream.
  313. */
  314. jspb.BinaryReader.prototype.skipFixed64Field = function() {
  315. if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED64) {
  316. goog.asserts.fail('Invalid wire type for skipFixed64Field');
  317. this.skipField();
  318. return;
  319. }
  320. this.decoder_.advance(8);
  321. };
  322. /**
  323. * Skips over the next group field in the binary stream.
  324. */
  325. jspb.BinaryReader.prototype.skipGroup = function() {
  326. var previousField = this.nextField_;
  327. do {
  328. if (!this.nextField()) {
  329. goog.asserts.fail('Unmatched start-group tag: stream EOF');
  330. this.error_ = true;
  331. return;
  332. }
  333. if (this.nextWireType_ ==
  334. jspb.BinaryConstants.WireType.END_GROUP) {
  335. // Group end: check that it matches top-of-stack.
  336. if (this.nextField_ != previousField) {
  337. goog.asserts.fail('Unmatched end-group tag');
  338. this.error_ = true;
  339. return;
  340. }
  341. return;
  342. }
  343. this.skipField();
  344. } while (true);
  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 = function(
  377. callbackName, callback) {
  378. if (this.readCallbacks_ === null) {
  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(this.readCallbacks_ !== null);
  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 a signed zigzag-encoded 64-bit integer field 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 {string} The value of the signed 64-bit integer field as a decimal string.
  641. */
  642. jspb.BinaryReader.prototype.readSint64String = function() {
  643. goog.asserts.assert(
  644. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  645. return this.decoder_.readZigzagVarint64String();
  646. };
  647. /**
  648. * Reads an unsigned 32-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 double field.
  653. */
  654. jspb.BinaryReader.prototype.readFixed32 = function() {
  655. goog.asserts.assert(
  656. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  657. return this.decoder_.readUint32();
  658. };
  659. /**
  660. * Reads an unsigned 64-bit fixed-length integer fiield from the binary stream,
  661. * or throws an error if the next field in the stream is not of the correct
  662. * wire type.
  663. *
  664. * @return {number} The value of the float field.
  665. */
  666. jspb.BinaryReader.prototype.readFixed64 = function() {
  667. goog.asserts.assert(
  668. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  669. return this.decoder_.readUint64();
  670. };
  671. /**
  672. * Reads a signed 64-bit integer field from the binary stream as a string, or
  673. * throws an error if the next field in the stream is not of the correct wire
  674. * type.
  675. *
  676. * Returns the value as a string.
  677. *
  678. * @return {string} The value of the unsigned 64-bit integer field as a decimal
  679. * string.
  680. */
  681. jspb.BinaryReader.prototype.readFixed64String = function() {
  682. goog.asserts.assert(
  683. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  684. return this.decoder_.readUint64String();
  685. };
  686. /**
  687. * Reads a signed 32-bit fixed-length integer fiield from the binary stream, or
  688. * throws an error if the next field in the stream is not of the correct wire
  689. * type.
  690. *
  691. * @return {number} The value of the signed 32-bit integer field.
  692. */
  693. jspb.BinaryReader.prototype.readSfixed32 = function() {
  694. goog.asserts.assert(
  695. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  696. return this.decoder_.readInt32();
  697. };
  698. /**
  699. * Reads a signed 32-bit fixed-length integer fiield from the binary stream, or
  700. * throws an error if the next field in the stream is not of the correct wire
  701. * type.
  702. *
  703. * @return {string} The value of the signed 32-bit integer field as a decimal
  704. * string.
  705. */
  706. jspb.BinaryReader.prototype.readSfixed32String = function() {
  707. goog.asserts.assert(
  708. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  709. return this.decoder_.readInt32().toString();
  710. };
  711. /**
  712. * Reads a signed 64-bit fixed-length integer fiield from the binary stream, or
  713. * throws an error if the next field in the stream is not of the correct wire
  714. * type.
  715. *
  716. * @return {number} The value of the sfixed64 field.
  717. */
  718. jspb.BinaryReader.prototype.readSfixed64 = function() {
  719. goog.asserts.assert(
  720. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  721. return this.decoder_.readInt64();
  722. };
  723. /**
  724. * Reads a signed 64-bit fixed-length integer fiield from the binary stream, or
  725. * throws an error if the next field in the stream is not of the correct wire
  726. * type.
  727. *
  728. * Returns the value as a string.
  729. *
  730. * @return {string} The value of the sfixed64 field as a decimal string.
  731. */
  732. jspb.BinaryReader.prototype.readSfixed64String = function() {
  733. goog.asserts.assert(
  734. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  735. return this.decoder_.readInt64String();
  736. };
  737. /**
  738. * Reads a 32-bit floating-point field from the binary stream, or throws an
  739. * error if the next field in the stream is not of the correct wire type.
  740. *
  741. * @return {number} The value of the float field.
  742. */
  743. jspb.BinaryReader.prototype.readFloat = function() {
  744. goog.asserts.assert(
  745. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32);
  746. return this.decoder_.readFloat();
  747. };
  748. /**
  749. * Reads a 64-bit floating-point field from the binary stream, or throws an
  750. * error if the next field in the stream is not of the correct wire type.
  751. *
  752. * @return {number} The value of the double field.
  753. */
  754. jspb.BinaryReader.prototype.readDouble = function() {
  755. goog.asserts.assert(
  756. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  757. return this.decoder_.readDouble();
  758. };
  759. /**
  760. * Reads a boolean field from the binary stream, or throws an error if the next
  761. * field in the stream is not of the correct wire type.
  762. *
  763. * @return {boolean} The value of the boolean field.
  764. */
  765. jspb.BinaryReader.prototype.readBool = function() {
  766. goog.asserts.assert(
  767. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  768. return !!this.decoder_.readUnsignedVarint32();
  769. };
  770. /**
  771. * Reads an enum field from the binary stream, or throws an error if the next
  772. * field in the stream is not of the correct wire type.
  773. *
  774. * @return {number} The value of the enum field.
  775. */
  776. jspb.BinaryReader.prototype.readEnum = function() {
  777. goog.asserts.assert(
  778. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  779. return this.decoder_.readSignedVarint64();
  780. };
  781. /**
  782. * Reads a string field from the binary stream, or throws an error if the next
  783. * field in the stream is not of the correct wire type.
  784. *
  785. * @return {string} The value of the string field.
  786. */
  787. jspb.BinaryReader.prototype.readString = function() {
  788. goog.asserts.assert(
  789. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  790. var length = this.decoder_.readUnsignedVarint32();
  791. return this.decoder_.readString(length);
  792. };
  793. /**
  794. * Reads a length-prefixed block of bytes from the binary stream, or returns
  795. * null if the next field in the stream has an invalid length value.
  796. *
  797. * @return {!Uint8Array} The block of bytes.
  798. */
  799. jspb.BinaryReader.prototype.readBytes = function() {
  800. goog.asserts.assert(
  801. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  802. var length = this.decoder_.readUnsignedVarint32();
  803. return this.decoder_.readBytes(length);
  804. };
  805. /**
  806. * Reads a 64-bit varint or fixed64 field from the stream and returns it as an
  807. * 8-character Unicode string for use as a hash table key, or throws an error
  808. * if the next field in the stream is not of the correct wire type.
  809. *
  810. * @return {string} The hash value.
  811. */
  812. jspb.BinaryReader.prototype.readVarintHash64 = function() {
  813. goog.asserts.assert(
  814. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  815. return this.decoder_.readVarintHash64();
  816. };
  817. /**
  818. * Reads an sint64 field from the stream and returns it as an 8-character
  819. * Unicode string for use as a hash table key, or throws an error if the next
  820. * field in the stream is not of the correct wire type.
  821. *
  822. * @return {string} The hash value.
  823. */
  824. jspb.BinaryReader.prototype.readSintHash64 = function() {
  825. goog.asserts.assert(
  826. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  827. return this.decoder_.readZigzagVarintHash64();
  828. };
  829. /**
  830. * Reads a 64-bit varint field from the stream and invokes `convert` to produce
  831. * the return value, or throws an error if the next field in the stream is not
  832. * of the correct wire type.
  833. *
  834. * @param {function(number, number): T} convert Conversion function to produce
  835. * the result value, takes parameters (lowBits, highBits).
  836. * @return {T}
  837. * @template T
  838. */
  839. jspb.BinaryReader.prototype.readSplitVarint64 = function(convert) {
  840. goog.asserts.assert(
  841. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  842. return this.decoder_.readSplitVarint64(convert);
  843. };
  844. /**
  845. * Reads a 64-bit zig-zag varint field from the stream and invokes `convert` to
  846. * produce the return value, or throws an error if the next field in the stream
  847. * is not of the correct wire type.
  848. *
  849. * @param {function(number, number): T} convert Conversion function to produce
  850. * the result value, takes parameters (lowBits, highBits).
  851. * @return {T}
  852. * @template T
  853. */
  854. jspb.BinaryReader.prototype.readSplitZigzagVarint64 = function(convert) {
  855. goog.asserts.assert(
  856. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  857. return this.decoder_.readSplitVarint64(function(lowBits, highBits) {
  858. return jspb.utils.fromZigzag64(lowBits, highBits, convert);
  859. });
  860. };
  861. /**
  862. * Reads a 64-bit varint or fixed64 field from the stream and returns it as a
  863. * 8-character Unicode string for use as a hash table key, or throws an error
  864. * if the next field in the stream is not of the correct wire type.
  865. *
  866. * @return {string} The hash value.
  867. */
  868. jspb.BinaryReader.prototype.readFixedHash64 = function() {
  869. goog.asserts.assert(
  870. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  871. return this.decoder_.readFixedHash64();
  872. };
  873. /**
  874. * Reads a 64-bit fixed64 field from the stream and invokes `convert`
  875. * to produce the return value, or throws an error if the next field in the
  876. * stream is not of the correct wire type.
  877. *
  878. * @param {function(number, number): T} convert Conversion function to produce
  879. * the result value, takes parameters (lowBits, highBits).
  880. * @return {T}
  881. * @template T
  882. */
  883. jspb.BinaryReader.prototype.readSplitFixed64 = function(convert) {
  884. goog.asserts.assert(
  885. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  886. return this.decoder_.readSplitFixed64(convert);
  887. };
  888. /**
  889. * Reads a packed scalar field using the supplied raw reader function.
  890. * @param {function(this:jspb.BinaryDecoder)} decodeMethod
  891. * @return {!Array}
  892. * @private
  893. */
  894. jspb.BinaryReader.prototype.readPackedField_ = function(decodeMethod) {
  895. goog.asserts.assert(
  896. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  897. var length = this.decoder_.readUnsignedVarint32();
  898. var end = this.decoder_.getCursor() + length;
  899. var result = [];
  900. while (this.decoder_.getCursor() < end) {
  901. // TODO(aappleby): .call is slow
  902. result.push(decodeMethod.call(this.decoder_));
  903. }
  904. return result;
  905. };
  906. /**
  907. * Reads a packed int32 field, which consists of a length header and a list of
  908. * signed varints.
  909. * @return {!Array<number>}
  910. */
  911. jspb.BinaryReader.prototype.readPackedInt32 = function() {
  912. return this.readPackedField_(this.decoder_.readSignedVarint32);
  913. };
  914. /**
  915. * Reads a packed int32 field, which consists of a length header and a list of
  916. * signed varints. Returns a list of strings.
  917. * @return {!Array<string>}
  918. */
  919. jspb.BinaryReader.prototype.readPackedInt32String = function() {
  920. return this.readPackedField_(this.decoder_.readSignedVarint32String);
  921. };
  922. /**
  923. * Reads a packed int64 field, which consists of a length header and a list of
  924. * signed varints.
  925. * @return {!Array<number>}
  926. */
  927. jspb.BinaryReader.prototype.readPackedInt64 = function() {
  928. return this.readPackedField_(this.decoder_.readSignedVarint64);
  929. };
  930. /**
  931. * Reads a packed int64 field, which consists of a length header and a list of
  932. * signed varints. Returns a list of strings.
  933. * @return {!Array<string>}
  934. */
  935. jspb.BinaryReader.prototype.readPackedInt64String = function() {
  936. return this.readPackedField_(this.decoder_.readSignedVarint64String);
  937. };
  938. /**
  939. * Reads a packed uint32 field, which consists of a length header and a list of
  940. * unsigned varints.
  941. * @return {!Array<number>}
  942. */
  943. jspb.BinaryReader.prototype.readPackedUint32 = function() {
  944. return this.readPackedField_(this.decoder_.readUnsignedVarint32);
  945. };
  946. /**
  947. * Reads a packed uint32 field, which consists of a length header and a list of
  948. * unsigned varints. Returns a list of strings.
  949. * @return {!Array<string>}
  950. */
  951. jspb.BinaryReader.prototype.readPackedUint32String = function() {
  952. return this.readPackedField_(this.decoder_.readUnsignedVarint32String);
  953. };
  954. /**
  955. * Reads a packed uint64 field, which consists of a length header and a list of
  956. * unsigned varints.
  957. * @return {!Array<number>}
  958. */
  959. jspb.BinaryReader.prototype.readPackedUint64 = function() {
  960. return this.readPackedField_(this.decoder_.readUnsignedVarint64);
  961. };
  962. /**
  963. * Reads a packed uint64 field, which consists of a length header and a list of
  964. * unsigned varints. Returns a list of strings.
  965. * @return {!Array<string>}
  966. */
  967. jspb.BinaryReader.prototype.readPackedUint64String = function() {
  968. return this.readPackedField_(this.decoder_.readUnsignedVarint64String);
  969. };
  970. /**
  971. * Reads a packed sint32 field, which consists of a length header and a list of
  972. * zigzag varints.
  973. * @return {!Array<number>}
  974. */
  975. jspb.BinaryReader.prototype.readPackedSint32 = function() {
  976. return this.readPackedField_(this.decoder_.readZigzagVarint32);
  977. };
  978. /**
  979. * Reads a packed sint64 field, which consists of a length header and a list of
  980. * zigzag varints.
  981. * @return {!Array<number>}
  982. */
  983. jspb.BinaryReader.prototype.readPackedSint64 = function() {
  984. return this.readPackedField_(this.decoder_.readZigzagVarint64);
  985. };
  986. /**
  987. * Reads a packed sint64 field, which consists of a length header and a list of
  988. * zigzag varints. Returns a list of strings.
  989. * @return {!Array<string>}
  990. */
  991. jspb.BinaryReader.prototype.readPackedSint64String = function() {
  992. return this.readPackedField_(this.decoder_.readZigzagVarint64String);
  993. };
  994. /**
  995. * Reads a packed fixed32 field, which consists of a length header and a list
  996. * of unsigned 32-bit ints.
  997. * @return {!Array<number>}
  998. */
  999. jspb.BinaryReader.prototype.readPackedFixed32 = function() {
  1000. return this.readPackedField_(this.decoder_.readUint32);
  1001. };
  1002. /**
  1003. * Reads a packed fixed64 field, which consists of a length header and a list
  1004. * of unsigned 64-bit ints.
  1005. * @return {!Array<number>}
  1006. */
  1007. jspb.BinaryReader.prototype.readPackedFixed64 = function() {
  1008. return this.readPackedField_(this.decoder_.readUint64);
  1009. };
  1010. /**
  1011. * Reads a packed fixed64 field, which consists of a length header and a list
  1012. * of unsigned 64-bit ints. Returns a list of strings.
  1013. * @return {!Array<number>}
  1014. */
  1015. jspb.BinaryReader.prototype.readPackedFixed64String = function() {
  1016. return this.readPackedField_(this.decoder_.readUint64String);
  1017. };
  1018. /**
  1019. * Reads a packed sfixed32 field, which consists of a length header and a list
  1020. * of 32-bit ints.
  1021. * @return {!Array<number>}
  1022. */
  1023. jspb.BinaryReader.prototype.readPackedSfixed32 = function() {
  1024. return this.readPackedField_(this.decoder_.readInt32);
  1025. };
  1026. /**
  1027. * Reads a packed sfixed64 field, which consists of a length header and a list
  1028. * of 64-bit ints.
  1029. * @return {!Array<number>}
  1030. */
  1031. jspb.BinaryReader.prototype.readPackedSfixed64 = function() {
  1032. return this.readPackedField_(this.decoder_.readInt64);
  1033. };
  1034. /**
  1035. * Reads a packed sfixed64 field, which consists of a length header and a list
  1036. * of 64-bit ints. Returns a list of strings.
  1037. * @return {!Array<string>}
  1038. */
  1039. jspb.BinaryReader.prototype.readPackedSfixed64String = function() {
  1040. return this.readPackedField_(this.decoder_.readInt64String);
  1041. };
  1042. /**
  1043. * Reads a packed float field, which consists of a length header and a list of
  1044. * floats.
  1045. * @return {!Array<number>}
  1046. */
  1047. jspb.BinaryReader.prototype.readPackedFloat = function() {
  1048. return this.readPackedField_(this.decoder_.readFloat);
  1049. };
  1050. /**
  1051. * Reads a packed double field, which consists of a length header and a list of
  1052. * doubles.
  1053. * @return {!Array<number>}
  1054. */
  1055. jspb.BinaryReader.prototype.readPackedDouble = function() {
  1056. return this.readPackedField_(this.decoder_.readDouble);
  1057. };
  1058. /**
  1059. * Reads a packed bool field, which consists of a length header and a list of
  1060. * unsigned varints.
  1061. * @return {!Array<boolean>}
  1062. */
  1063. jspb.BinaryReader.prototype.readPackedBool = function() {
  1064. return this.readPackedField_(this.decoder_.readBool);
  1065. };
  1066. /**
  1067. * Reads a packed enum field, which consists of a length header and a list of
  1068. * unsigned varints.
  1069. * @return {!Array<number>}
  1070. */
  1071. jspb.BinaryReader.prototype.readPackedEnum = function() {
  1072. return this.readPackedField_(this.decoder_.readEnum);
  1073. };
  1074. /**
  1075. * Reads a packed varint hash64 field, which consists of a length header and a
  1076. * list of varint hash64s.
  1077. * @return {!Array<string>}
  1078. */
  1079. jspb.BinaryReader.prototype.readPackedVarintHash64 = function() {
  1080. return this.readPackedField_(this.decoder_.readVarintHash64);
  1081. };
  1082. /**
  1083. * Reads a packed fixed hash64 field, which consists of a length header and a
  1084. * list of fixed hash64s.
  1085. * @return {!Array<string>}
  1086. */
  1087. jspb.BinaryReader.prototype.readPackedFixedHash64 = function() {
  1088. return this.readPackedField_(this.decoder_.readFixedHash64);
  1089. };