reader.js 36 KB

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