reader.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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 field from the stream and invokes `convert` to produce
  818. * the return value, or throws an error if the next field in the stream is not
  819. * of the correct wire type.
  820. *
  821. * @param {function(number, number): T} convert Conversion function to produce
  822. * the result value, takes parameters (lowBits, highBits).
  823. * @return {T}
  824. * @template T
  825. */
  826. jspb.BinaryReader.prototype.readSplitVarint64 = function(convert) {
  827. goog.asserts.assert(
  828. this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT);
  829. return this.decoder_.readSplitVarint64(convert);
  830. };
  831. /**
  832. * Reads a 64-bit varint or fixed64 field from the stream and returns it as a
  833. * 8-character Unicode string for use as a hash table key, or throws an error
  834. * if the next field in the stream is not of the correct wire type.
  835. *
  836. * @return {string} The hash value.
  837. */
  838. jspb.BinaryReader.prototype.readFixedHash64 = function() {
  839. goog.asserts.assert(
  840. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  841. return this.decoder_.readFixedHash64();
  842. };
  843. /**
  844. * Reads a 64-bit fixed64 field from the stream and invokes `convert`
  845. * to produce the return value, or throws an error if the next field in the
  846. * stream is not of the correct wire type.
  847. *
  848. * @param {function(number, number): T} convert Conversion function to produce
  849. * the result value, takes parameters (lowBits, highBits).
  850. * @return {T}
  851. * @template T
  852. */
  853. jspb.BinaryReader.prototype.readSplitFixed64 = function(convert) {
  854. goog.asserts.assert(
  855. this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64);
  856. return this.decoder_.readSplitFixed64(convert);
  857. };
  858. /**
  859. * Reads a packed scalar field using the supplied raw reader function.
  860. * @param {function(this:jspb.BinaryDecoder)} decodeMethod
  861. * @return {!Array}
  862. * @private
  863. */
  864. jspb.BinaryReader.prototype.readPackedField_ = function(decodeMethod) {
  865. goog.asserts.assert(
  866. this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED);
  867. var length = this.decoder_.readUnsignedVarint32();
  868. var end = this.decoder_.getCursor() + length;
  869. var result = [];
  870. while (this.decoder_.getCursor() < end) {
  871. // TODO(aappleby): .call is slow
  872. result.push(decodeMethod.call(this.decoder_));
  873. }
  874. return result;
  875. };
  876. /**
  877. * Reads a packed int32 field, which consists of a length header and a list of
  878. * signed varints.
  879. * @return {!Array<number>}
  880. */
  881. jspb.BinaryReader.prototype.readPackedInt32 = function() {
  882. return this.readPackedField_(this.decoder_.readSignedVarint32);
  883. };
  884. /**
  885. * Reads a packed int32 field, which consists of a length header and a list of
  886. * signed varints. Returns a list of strings.
  887. * @return {!Array<string>}
  888. */
  889. jspb.BinaryReader.prototype.readPackedInt32String = function() {
  890. return this.readPackedField_(this.decoder_.readSignedVarint32String);
  891. };
  892. /**
  893. * Reads a packed int64 field, which consists of a length header and a list of
  894. * signed varints.
  895. * @return {!Array<number>}
  896. */
  897. jspb.BinaryReader.prototype.readPackedInt64 = function() {
  898. return this.readPackedField_(this.decoder_.readSignedVarint64);
  899. };
  900. /**
  901. * Reads a packed int64 field, which consists of a length header and a list of
  902. * signed varints. Returns a list of strings.
  903. * @return {!Array<string>}
  904. */
  905. jspb.BinaryReader.prototype.readPackedInt64String = function() {
  906. return this.readPackedField_(this.decoder_.readSignedVarint64String);
  907. };
  908. /**
  909. * Reads a packed uint32 field, which consists of a length header and a list of
  910. * unsigned varints.
  911. * @return {!Array<number>}
  912. */
  913. jspb.BinaryReader.prototype.readPackedUint32 = function() {
  914. return this.readPackedField_(this.decoder_.readUnsignedVarint32);
  915. };
  916. /**
  917. * Reads a packed uint32 field, which consists of a length header and a list of
  918. * unsigned varints. Returns a list of strings.
  919. * @return {!Array<string>}
  920. */
  921. jspb.BinaryReader.prototype.readPackedUint32String = function() {
  922. return this.readPackedField_(this.decoder_.readUnsignedVarint32String);
  923. };
  924. /**
  925. * Reads a packed uint64 field, which consists of a length header and a list of
  926. * unsigned varints.
  927. * @return {!Array<number>}
  928. */
  929. jspb.BinaryReader.prototype.readPackedUint64 = function() {
  930. return this.readPackedField_(this.decoder_.readUnsignedVarint64);
  931. };
  932. /**
  933. * Reads a packed uint64 field, which consists of a length header and a list of
  934. * unsigned varints. Returns a list of strings.
  935. * @return {!Array<string>}
  936. */
  937. jspb.BinaryReader.prototype.readPackedUint64String = function() {
  938. return this.readPackedField_(this.decoder_.readUnsignedVarint64String);
  939. };
  940. /**
  941. * Reads a packed sint32 field, which consists of a length header and a list of
  942. * zigzag varints.
  943. * @return {!Array<number>}
  944. */
  945. jspb.BinaryReader.prototype.readPackedSint32 = function() {
  946. return this.readPackedField_(this.decoder_.readZigzagVarint32);
  947. };
  948. /**
  949. * Reads a packed sint64 field, which consists of a length header and a list of
  950. * zigzag varints.
  951. * @return {!Array<number>}
  952. */
  953. jspb.BinaryReader.prototype.readPackedSint64 = function() {
  954. return this.readPackedField_(this.decoder_.readZigzagVarint64);
  955. };
  956. /**
  957. * Reads a packed sint64 field, which consists of a length header and a list of
  958. * zigzag varints. Returns a list of strings.
  959. * @return {!Array<string>}
  960. */
  961. jspb.BinaryReader.prototype.readPackedSint64String = function() {
  962. return this.readPackedField_(this.decoder_.readZigzagVarint64String);
  963. };
  964. /**
  965. * Reads a packed fixed32 field, which consists of a length header and a list
  966. * of unsigned 32-bit ints.
  967. * @return {!Array<number>}
  968. */
  969. jspb.BinaryReader.prototype.readPackedFixed32 = function() {
  970. return this.readPackedField_(this.decoder_.readUint32);
  971. };
  972. /**
  973. * Reads a packed fixed64 field, which consists of a length header and a list
  974. * of unsigned 64-bit ints.
  975. * @return {!Array<number>}
  976. */
  977. jspb.BinaryReader.prototype.readPackedFixed64 = function() {
  978. return this.readPackedField_(this.decoder_.readUint64);
  979. };
  980. /**
  981. * Reads a packed fixed64 field, which consists of a length header and a list
  982. * of unsigned 64-bit ints. Returns a list of strings.
  983. * @return {!Array<number>}
  984. */
  985. jspb.BinaryReader.prototype.readPackedFixed64String = function() {
  986. return this.readPackedField_(this.decoder_.readUint64String);
  987. };
  988. /**
  989. * Reads a packed sfixed32 field, which consists of a length header and a list
  990. * of 32-bit ints.
  991. * @return {!Array<number>}
  992. */
  993. jspb.BinaryReader.prototype.readPackedSfixed32 = function() {
  994. return this.readPackedField_(this.decoder_.readInt32);
  995. };
  996. /**
  997. * Reads a packed sfixed64 field, which consists of a length header and a list
  998. * of 64-bit ints.
  999. * @return {!Array<number>}
  1000. */
  1001. jspb.BinaryReader.prototype.readPackedSfixed64 = function() {
  1002. return this.readPackedField_(this.decoder_.readInt64);
  1003. };
  1004. /**
  1005. * Reads a packed sfixed64 field, which consists of a length header and a list
  1006. * of 64-bit ints. Returns a list of strings.
  1007. * @return {!Array<string>}
  1008. */
  1009. jspb.BinaryReader.prototype.readPackedSfixed64String = function() {
  1010. return this.readPackedField_(this.decoder_.readInt64String);
  1011. };
  1012. /**
  1013. * Reads a packed float field, which consists of a length header and a list of
  1014. * floats.
  1015. * @return {!Array<number>}
  1016. */
  1017. jspb.BinaryReader.prototype.readPackedFloat = function() {
  1018. return this.readPackedField_(this.decoder_.readFloat);
  1019. };
  1020. /**
  1021. * Reads a packed double field, which consists of a length header and a list of
  1022. * doubles.
  1023. * @return {!Array<number>}
  1024. */
  1025. jspb.BinaryReader.prototype.readPackedDouble = function() {
  1026. return this.readPackedField_(this.decoder_.readDouble);
  1027. };
  1028. /**
  1029. * Reads a packed bool field, which consists of a length header and a list of
  1030. * unsigned varints.
  1031. * @return {!Array<boolean>}
  1032. */
  1033. jspb.BinaryReader.prototype.readPackedBool = function() {
  1034. return this.readPackedField_(this.decoder_.readBool);
  1035. };
  1036. /**
  1037. * Reads a packed enum field, which consists of a length header and a list of
  1038. * unsigned varints.
  1039. * @return {!Array<number>}
  1040. */
  1041. jspb.BinaryReader.prototype.readPackedEnum = function() {
  1042. return this.readPackedField_(this.decoder_.readEnum);
  1043. };
  1044. /**
  1045. * Reads a packed varint hash64 field, which consists of a length header and a
  1046. * list of varint hash64s.
  1047. * @return {!Array<string>}
  1048. */
  1049. jspb.BinaryReader.prototype.readPackedVarintHash64 = function() {
  1050. return this.readPackedField_(this.decoder_.readVarintHash64);
  1051. };
  1052. /**
  1053. * Reads a packed fixed hash64 field, which consists of a length header and a
  1054. * list of fixed hash64s.
  1055. * @return {!Array<string>}
  1056. */
  1057. jspb.BinaryReader.prototype.readPackedFixedHash64 = function() {
  1058. return this.readPackedField_(this.decoder_.readFixedHash64);
  1059. };