reader.js 36 KB

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