Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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 | /** |
| 32 | * @fileoverview This file contains utilities for converting binary, |
| 33 | * wire-format protocol buffers into Javascript data structures. |
| 34 | * |
| 35 | * jspb's BinaryReader class wraps the BinaryDecoder class to add methods |
| 36 | * that understand the protocol buffer syntax and can do the type checking and |
| 37 | * bookkeeping necessary to parse trees of nested messages. |
| 38 | * |
| 39 | * Major caveat - Users of this library _must_ keep their Javascript proto |
| 40 | * parsing code in sync with the original .proto file - presumably you'll be |
| 41 | * using the typed jspb code generator, but if you bypass that you'll need |
| 42 | * to keep things in sync by hand. |
| 43 | * |
| 44 | * @author aappleby@google.com (Austin Appleby) |
| 45 | */ |
| 46 | |
| 47 | goog.provide('jspb.BinaryReader'); |
| 48 | |
| 49 | goog.require('goog.asserts'); |
| 50 | goog.require('jspb.BinaryConstants'); |
| 51 | goog.require('jspb.BinaryDecoder'); |
| 52 | |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * BinaryReader implements the decoders for all the wire types specified in |
| 57 | * https://developers.google.com/protocol-buffers/docs/encoding. |
| 58 | * |
| 59 | * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from. |
| 60 | * @param {number=} opt_start The optional offset to start reading at. |
| 61 | * @param {number=} opt_length The optional length of the block to read - |
| 62 | * we'll throw an assertion if we go off the end of the block. |
| 63 | * @constructor |
| 64 | * @struct |
| 65 | */ |
| 66 | jspb.BinaryReader = function(opt_bytes, opt_start, opt_length) { |
| 67 | /** |
| 68 | * Wire-format decoder. |
| 69 | * @private {!jspb.BinaryDecoder} |
| 70 | */ |
| 71 | this.decoder_ = jspb.BinaryDecoder.alloc(opt_bytes, opt_start, opt_length); |
| 72 | |
| 73 | /** |
| 74 | * Cursor immediately before the field tag. |
| 75 | * @private {number} |
| 76 | */ |
| 77 | this.fieldCursor_ = this.decoder_.getCursor(); |
| 78 | |
| 79 | /** |
| 80 | * Field number of the next field in the buffer, filled in by nextField(). |
| 81 | * @private {number} |
| 82 | */ |
| 83 | this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER; |
| 84 | |
| 85 | /** |
| 86 | * Wire type of the next proto field in the buffer, filled in by |
| 87 | * nextField(). |
| 88 | * @private {jspb.BinaryConstants.WireType} |
| 89 | */ |
| 90 | this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID; |
| 91 | |
| 92 | /** |
| 93 | * Set to true if this reader encountered an error due to corrupt data. |
| 94 | * @private {boolean} |
| 95 | */ |
| 96 | this.error_ = false; |
| 97 | |
| 98 | /** |
| 99 | * User-defined reader callbacks. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 100 | * @private {Object<string, function(!jspb.BinaryReader):*>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 101 | */ |
| 102 | this.readCallbacks_ = null; |
| 103 | }; |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Global pool of BinaryReader instances. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 108 | * @private {!Array<!jspb.BinaryReader>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 109 | */ |
| 110 | jspb.BinaryReader.instanceCache_ = []; |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Pops an instance off the instance cache, or creates one if the cache is |
| 115 | * empty. |
| 116 | * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from. |
| 117 | * @param {number=} opt_start The optional offset to start reading at. |
| 118 | * @param {number=} opt_length The optional length of the block to read - |
| 119 | * we'll throw an assertion if we go off the end of the block. |
| 120 | * @return {!jspb.BinaryReader} |
| 121 | */ |
| 122 | jspb.BinaryReader.alloc = |
| 123 | function(opt_bytes, opt_start, opt_length) { |
| 124 | if (jspb.BinaryReader.instanceCache_.length) { |
| 125 | var newReader = jspb.BinaryReader.instanceCache_.pop(); |
| 126 | if (opt_bytes) { |
| 127 | newReader.decoder_.setBlock(opt_bytes, opt_start, opt_length); |
| 128 | } |
| 129 | return newReader; |
| 130 | } else { |
| 131 | return new jspb.BinaryReader(opt_bytes, opt_start, opt_length); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Alias for the above method. |
| 138 | * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from. |
| 139 | * @param {number=} opt_start The optional offset to start reading at. |
| 140 | * @param {number=} opt_length The optional length of the block to read - |
| 141 | * we'll throw an assertion if we go off the end of the block. |
| 142 | * @return {!jspb.BinaryReader} |
| 143 | */ |
| 144 | jspb.BinaryReader.prototype.alloc = jspb.BinaryReader.alloc; |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Puts this instance back in the instance cache. |
| 149 | */ |
| 150 | jspb.BinaryReader.prototype.free = function() { |
| 151 | this.decoder_.clear(); |
| 152 | this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER; |
| 153 | this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID; |
| 154 | this.error_ = false; |
| 155 | this.readCallbacks_ = null; |
| 156 | |
| 157 | if (jspb.BinaryReader.instanceCache_.length < 100) { |
| 158 | jspb.BinaryReader.instanceCache_.push(this); |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * Returns the cursor immediately before the current field's tag. |
| 165 | * @return {number} The internal read cursor. |
| 166 | */ |
| 167 | jspb.BinaryReader.prototype.getFieldCursor = function() { |
| 168 | return this.fieldCursor_; |
| 169 | }; |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Returns the internal read cursor. |
| 174 | * @return {number} The internal read cursor. |
| 175 | */ |
| 176 | jspb.BinaryReader.prototype.getCursor = function() { |
| 177 | return this.decoder_.getCursor(); |
| 178 | }; |
| 179 | |
| 180 | |
| 181 | /** |
| 182 | * Returns the raw buffer. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 183 | * @return {?Uint8Array} The raw buffer. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 184 | */ |
| 185 | jspb.BinaryReader.prototype.getBuffer = function() { |
| 186 | return this.decoder_.getBuffer(); |
| 187 | }; |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * @return {number} The field number of the next field in the buffer, or |
| 192 | * INVALID_FIELD_NUMBER if there is no next field. |
| 193 | */ |
| 194 | jspb.BinaryReader.prototype.getFieldNumber = function() { |
| 195 | return this.nextField_; |
| 196 | }; |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * @return {jspb.BinaryConstants.WireType} The wire type of the next field |
| 201 | * in the stream, or WireType.INVALID if there is no next field. |
| 202 | */ |
| 203 | jspb.BinaryReader.prototype.getWireType = function() { |
| 204 | return this.nextWireType_; |
| 205 | }; |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * @return {boolean} Whether the current wire type is an end-group tag. Used as |
| 210 | * an exit condition in decoder loops in generated code. |
| 211 | */ |
| 212 | jspb.BinaryReader.prototype.isEndGroup = function() { |
| 213 | return this.nextWireType_ == jspb.BinaryConstants.WireType.END_GROUP; |
| 214 | }; |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * Returns true if this reader hit an error due to corrupt data. |
| 219 | * @return {boolean} |
| 220 | */ |
| 221 | jspb.BinaryReader.prototype.getError = function() { |
| 222 | return this.error_ || this.decoder_.getError(); |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Points this reader at a new block of bytes. |
| 228 | * @param {!Uint8Array} bytes The block of bytes we're reading from. |
| 229 | * @param {number} start The offset to start reading at. |
| 230 | * @param {number} length The length of the block to read. |
| 231 | */ |
| 232 | jspb.BinaryReader.prototype.setBlock = function(bytes, start, length) { |
| 233 | this.decoder_.setBlock(bytes, start, length); |
| 234 | this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER; |
| 235 | this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID; |
| 236 | }; |
| 237 | |
| 238 | |
| 239 | /** |
| 240 | * Rewinds the stream cursor to the beginning of the buffer and resets all |
| 241 | * internal state. |
| 242 | */ |
| 243 | jspb.BinaryReader.prototype.reset = function() { |
| 244 | this.decoder_.reset(); |
| 245 | this.nextField_ = jspb.BinaryConstants.INVALID_FIELD_NUMBER; |
| 246 | this.nextWireType_ = jspb.BinaryConstants.WireType.INVALID; |
| 247 | }; |
| 248 | |
| 249 | |
| 250 | /** |
| 251 | * Advances the stream cursor by the given number of bytes. |
| 252 | * @param {number} count The number of bytes to advance by. |
| 253 | */ |
| 254 | jspb.BinaryReader.prototype.advance = function(count) { |
| 255 | this.decoder_.advance(count); |
| 256 | }; |
| 257 | |
| 258 | |
| 259 | /** |
| 260 | * Reads the next field header in the stream if there is one, returns true if |
| 261 | * we saw a valid field header or false if we've read the whole stream. |
| 262 | * Throws an error if we encountered a deprecated START_GROUP/END_GROUP field. |
| 263 | * @return {boolean} True if the stream contains more fields. |
| 264 | */ |
| 265 | jspb.BinaryReader.prototype.nextField = function() { |
| 266 | // If we're at the end of the block, there are no more fields. |
| 267 | if (this.decoder_.atEnd()) { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | // If we hit an error decoding the previous field, stop now before we |
| 272 | // try to decode anything else |
| 273 | if (this.getError()) { |
| 274 | goog.asserts.fail('Decoder hit an error'); |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | // Otherwise just read the header of the next field. |
| 279 | this.fieldCursor_ = this.decoder_.getCursor(); |
| 280 | var header = this.decoder_.readUnsignedVarint32(); |
| 281 | |
| 282 | var nextField = header >>> 3; |
| 283 | var nextWireType = /** @type {jspb.BinaryConstants.WireType} */ |
| 284 | (header & 0x7); |
| 285 | |
| 286 | // If the wire type isn't one of the valid ones, something's broken. |
| 287 | if (nextWireType != jspb.BinaryConstants.WireType.VARINT && |
| 288 | nextWireType != jspb.BinaryConstants.WireType.FIXED32 && |
| 289 | nextWireType != jspb.BinaryConstants.WireType.FIXED64 && |
| 290 | nextWireType != jspb.BinaryConstants.WireType.DELIMITED && |
| 291 | nextWireType != jspb.BinaryConstants.WireType.START_GROUP && |
| 292 | nextWireType != jspb.BinaryConstants.WireType.END_GROUP) { |
| 293 | goog.asserts.fail('Invalid wire type'); |
| 294 | this.error_ = true; |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | this.nextField_ = nextField; |
| 299 | this.nextWireType_ = nextWireType; |
| 300 | |
| 301 | return true; |
| 302 | }; |
| 303 | |
| 304 | |
| 305 | /** |
| 306 | * Winds the reader back to just before this field's header. |
| 307 | */ |
| 308 | jspb.BinaryReader.prototype.unskipHeader = function() { |
| 309 | this.decoder_.unskipVarint((this.nextField_ << 3) | this.nextWireType_); |
| 310 | }; |
| 311 | |
| 312 | |
| 313 | /** |
| 314 | * Skips all contiguous fields whose header matches the one we just read. |
| 315 | */ |
| 316 | jspb.BinaryReader.prototype.skipMatchingFields = function() { |
| 317 | var field = this.nextField_; |
| 318 | this.unskipHeader(); |
| 319 | |
| 320 | while (this.nextField() && (this.getFieldNumber() == field)) { |
| 321 | this.skipField(); |
| 322 | } |
| 323 | |
| 324 | if (!this.decoder_.atEnd()) { |
| 325 | this.unskipHeader(); |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | |
| 330 | /** |
| 331 | * Skips over the next varint field in the binary stream. |
| 332 | */ |
| 333 | jspb.BinaryReader.prototype.skipVarintField = function() { |
| 334 | if (this.nextWireType_ != jspb.BinaryConstants.WireType.VARINT) { |
| 335 | goog.asserts.fail('Invalid wire type for skipVarintField'); |
| 336 | this.skipField(); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | this.decoder_.skipVarint(); |
| 341 | }; |
| 342 | |
| 343 | |
| 344 | /** |
| 345 | * Skips over the next delimited field in the binary stream. |
| 346 | */ |
| 347 | jspb.BinaryReader.prototype.skipDelimitedField = function() { |
| 348 | if (this.nextWireType_ != jspb.BinaryConstants.WireType.DELIMITED) { |
| 349 | goog.asserts.fail('Invalid wire type for skipDelimitedField'); |
| 350 | this.skipField(); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | var length = this.decoder_.readUnsignedVarint32(); |
| 355 | this.decoder_.advance(length); |
| 356 | }; |
| 357 | |
| 358 | |
| 359 | /** |
| 360 | * Skips over the next fixed32 field in the binary stream. |
| 361 | */ |
| 362 | jspb.BinaryReader.prototype.skipFixed32Field = function() { |
| 363 | if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED32) { |
| 364 | goog.asserts.fail('Invalid wire type for skipFixed32Field'); |
| 365 | this.skipField(); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | this.decoder_.advance(4); |
| 370 | }; |
| 371 | |
| 372 | |
| 373 | /** |
| 374 | * Skips over the next fixed64 field in the binary stream. |
| 375 | */ |
| 376 | jspb.BinaryReader.prototype.skipFixed64Field = function() { |
| 377 | if (this.nextWireType_ != jspb.BinaryConstants.WireType.FIXED64) { |
| 378 | goog.asserts.fail('Invalid wire type for skipFixed64Field'); |
| 379 | this.skipField(); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | this.decoder_.advance(8); |
| 384 | }; |
| 385 | |
| 386 | |
| 387 | /** |
| 388 | * Skips over the next group field in the binary stream. |
| 389 | */ |
| 390 | jspb.BinaryReader.prototype.skipGroup = function() { |
| 391 | // Keep a stack of start-group tags that must be matched by end-group tags. |
| 392 | var nestedGroups = [this.nextField_]; |
| 393 | do { |
| 394 | if (!this.nextField()) { |
| 395 | goog.asserts.fail('Unmatched start-group tag: stream EOF'); |
| 396 | this.error_ = true; |
| 397 | return; |
| 398 | } |
| 399 | if (this.nextWireType_ == |
| 400 | jspb.BinaryConstants.WireType.START_GROUP) { |
| 401 | // Nested group start. |
| 402 | nestedGroups.push(this.nextField_); |
| 403 | } else if (this.nextWireType_ == |
| 404 | jspb.BinaryConstants.WireType.END_GROUP) { |
| 405 | // Group end: check that it matches top-of-stack. |
| 406 | if (this.nextField_ != nestedGroups.pop()) { |
| 407 | goog.asserts.fail('Unmatched end-group tag'); |
| 408 | this.error_ = true; |
| 409 | return; |
| 410 | } |
| 411 | } |
| 412 | } while (nestedGroups.length > 0); |
| 413 | }; |
| 414 | |
| 415 | |
| 416 | /** |
| 417 | * Skips over the next field in the binary stream - this is useful if we're |
| 418 | * decoding a message that contain unknown fields. |
| 419 | */ |
| 420 | jspb.BinaryReader.prototype.skipField = function() { |
| 421 | switch (this.nextWireType_) { |
| 422 | case jspb.BinaryConstants.WireType.VARINT: |
| 423 | this.skipVarintField(); |
| 424 | break; |
| 425 | case jspb.BinaryConstants.WireType.FIXED64: |
| 426 | this.skipFixed64Field(); |
| 427 | break; |
| 428 | case jspb.BinaryConstants.WireType.DELIMITED: |
| 429 | this.skipDelimitedField(); |
| 430 | break; |
| 431 | case jspb.BinaryConstants.WireType.FIXED32: |
| 432 | this.skipFixed32Field(); |
| 433 | break; |
| 434 | case jspb.BinaryConstants.WireType.START_GROUP: |
| 435 | this.skipGroup(); |
| 436 | break; |
| 437 | default: |
| 438 | goog.asserts.fail('Invalid wire encoding for field.'); |
| 439 | } |
| 440 | }; |
| 441 | |
| 442 | |
| 443 | /** |
| 444 | * Registers a user-defined read callback. |
| 445 | * @param {string} callbackName |
| 446 | * @param {function(!jspb.BinaryReader):*} callback |
| 447 | */ |
| 448 | jspb.BinaryReader.prototype.registerReadCallback = |
| 449 | function(callbackName, callback) { |
| 450 | if (goog.isNull(this.readCallbacks_)) { |
| 451 | this.readCallbacks_ = {}; |
| 452 | } |
| 453 | goog.asserts.assert(!this.readCallbacks_[callbackName]); |
| 454 | this.readCallbacks_[callbackName] = callback; |
| 455 | }; |
| 456 | |
| 457 | |
| 458 | /** |
| 459 | * Runs a registered read callback. |
| 460 | * @param {string} callbackName The name the callback is registered under. |
| 461 | * @return {*} The value returned by the callback. |
| 462 | */ |
| 463 | jspb.BinaryReader.prototype.runReadCallback = function(callbackName) { |
| 464 | goog.asserts.assert(!goog.isNull(this.readCallbacks_)); |
| 465 | var callback = this.readCallbacks_[callbackName]; |
| 466 | goog.asserts.assert(callback); |
| 467 | return callback(this); |
| 468 | }; |
| 469 | |
| 470 | |
| 471 | /** |
| 472 | * Reads a field of any valid non-message type from the binary stream. |
| 473 | * @param {jspb.BinaryConstants.FieldType} fieldType |
| 474 | * @return {jspb.AnyFieldType} |
| 475 | */ |
| 476 | jspb.BinaryReader.prototype.readAny = function(fieldType) { |
| 477 | this.nextWireType_ = jspb.BinaryConstants.FieldTypeToWireType(fieldType); |
| 478 | var fieldTypes = jspb.BinaryConstants.FieldType; |
| 479 | switch (fieldType) { |
| 480 | case fieldTypes.DOUBLE: |
| 481 | return this.readDouble(); |
| 482 | case fieldTypes.FLOAT: |
| 483 | return this.readFloat(); |
| 484 | case fieldTypes.INT64: |
| 485 | return this.readInt64(); |
| 486 | case fieldTypes.UINT64: |
| 487 | return this.readUint64(); |
| 488 | case fieldTypes.INT32: |
| 489 | return this.readInt32(); |
| 490 | case fieldTypes.FIXED64: |
| 491 | return this.readFixed64(); |
| 492 | case fieldTypes.FIXED32: |
| 493 | return this.readFixed32(); |
| 494 | case fieldTypes.BOOL: |
| 495 | return this.readBool(); |
| 496 | case fieldTypes.STRING: |
| 497 | return this.readString(); |
| 498 | case fieldTypes.GROUP: |
| 499 | goog.asserts.fail('Group field type not supported in readAny()'); |
| 500 | case fieldTypes.MESSAGE: |
| 501 | goog.asserts.fail('Message field type not supported in readAny()'); |
| 502 | case fieldTypes.BYTES: |
| 503 | return this.readBytes(); |
| 504 | case fieldTypes.UINT32: |
| 505 | return this.readUint32(); |
| 506 | case fieldTypes.ENUM: |
| 507 | return this.readEnum(); |
| 508 | case fieldTypes.SFIXED32: |
| 509 | return this.readSfixed32(); |
| 510 | case fieldTypes.SFIXED64: |
| 511 | return this.readSfixed64(); |
| 512 | case fieldTypes.SINT32: |
| 513 | return this.readSint32(); |
| 514 | case fieldTypes.SINT64: |
| 515 | return this.readSint64(); |
| 516 | case fieldTypes.FHASH64: |
| 517 | return this.readFixedHash64(); |
| 518 | case fieldTypes.VHASH64: |
| 519 | return this.readVarintHash64(); |
| 520 | default: |
| 521 | goog.asserts.fail('Invalid field type in readAny()'); |
| 522 | } |
| 523 | return 0; |
| 524 | }; |
| 525 | |
| 526 | |
| 527 | /** |
| 528 | * Deserialize a proto into the provided message object using the provided |
| 529 | * reader function. This function is templated as we currently have one client |
| 530 | * who is using manual deserialization instead of the code-generated versions. |
| 531 | * @template T |
| 532 | * @param {T} message |
| 533 | * @param {function(T, !jspb.BinaryReader)} reader |
| 534 | */ |
| 535 | jspb.BinaryReader.prototype.readMessage = function(message, reader) { |
| 536 | goog.asserts.assert( |
| 537 | this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED); |
| 538 | |
| 539 | // Save the current endpoint of the decoder and move it to the end of the |
| 540 | // embedded message. |
| 541 | var oldEnd = this.decoder_.getEnd(); |
| 542 | var length = this.decoder_.readUnsignedVarint32(); |
| 543 | var newEnd = this.decoder_.getCursor() + length; |
| 544 | this.decoder_.setEnd(newEnd); |
| 545 | |
| 546 | // Deserialize the embedded message. |
| 547 | reader(message, this); |
| 548 | |
| 549 | // Advance the decoder past the embedded message and restore the endpoint. |
| 550 | this.decoder_.setCursor(newEnd); |
| 551 | this.decoder_.setEnd(oldEnd); |
| 552 | }; |
| 553 | |
| 554 | |
| 555 | /** |
| 556 | * Deserialize a proto into the provided message object using the provided |
| 557 | * reader function, assuming that the message is serialized as a group |
| 558 | * with the given tag. |
| 559 | * @template T |
| 560 | * @param {number} field |
| 561 | * @param {T} message |
| 562 | * @param {function(T, !jspb.BinaryReader)} reader |
| 563 | */ |
| 564 | jspb.BinaryReader.prototype.readGroup = |
| 565 | function(field, message, reader) { |
| 566 | // Ensure that the wire type is correct. |
| 567 | goog.asserts.assert( |
| 568 | this.nextWireType_ == jspb.BinaryConstants.WireType.START_GROUP); |
| 569 | // Ensure that the field number is correct. |
| 570 | goog.asserts.assert(this.nextField_ == field); |
| 571 | |
| 572 | // Deserialize the message. The deserialization will stop at an END_GROUP tag. |
| 573 | reader(message, this); |
| 574 | |
| 575 | if (!this.error_ && |
| 576 | this.nextWireType_ != jspb.BinaryConstants.WireType.END_GROUP) { |
| 577 | goog.asserts.fail('Group submessage did not end with an END_GROUP tag'); |
| 578 | this.error_ = true; |
| 579 | } |
| 580 | }; |
| 581 | |
| 582 | |
| 583 | /** |
| 584 | * Return a decoder that wraps the current delimited field. |
| 585 | * @return {!jspb.BinaryDecoder} |
| 586 | */ |
| 587 | jspb.BinaryReader.prototype.getFieldDecoder = function() { |
| 588 | goog.asserts.assert( |
| 589 | this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED); |
| 590 | |
| 591 | var length = this.decoder_.readUnsignedVarint32(); |
| 592 | var start = this.decoder_.getCursor(); |
| 593 | var end = start + length; |
| 594 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 595 | var innerDecoder = |
| 596 | jspb.BinaryDecoder.alloc(this.decoder_.getBuffer(), start, length); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 597 | this.decoder_.setCursor(end); |
| 598 | return innerDecoder; |
| 599 | }; |
| 600 | |
| 601 | |
| 602 | /** |
| 603 | * Reads a signed 32-bit integer field from the binary stream, or throws an |
| 604 | * error if the next field in the stream is not of the correct wire type. |
| 605 | * |
| 606 | * @return {number} The value of the signed 32-bit integer field. |
| 607 | */ |
| 608 | jspb.BinaryReader.prototype.readInt32 = function() { |
| 609 | goog.asserts.assert( |
| 610 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 611 | return this.decoder_.readSignedVarint32(); |
| 612 | }; |
| 613 | |
| 614 | |
| 615 | /** |
| 616 | * Reads a signed 32-bit integer field from the binary stream, or throws an |
| 617 | * error if the next field in the stream is not of the correct wire type. |
| 618 | * |
| 619 | * Returns the value as a string. |
| 620 | * |
| 621 | * @return {string} The value of the signed 32-bit integer field as a decimal |
| 622 | * string. |
| 623 | */ |
| 624 | jspb.BinaryReader.prototype.readInt32String = function() { |
| 625 | goog.asserts.assert( |
| 626 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 627 | return this.decoder_.readSignedVarint32String(); |
| 628 | }; |
| 629 | |
| 630 | |
| 631 | /** |
| 632 | * Reads a signed 64-bit integer field from the binary stream, or throws an |
| 633 | * error if the next field in the stream is not of the correct wire type. |
| 634 | * |
| 635 | * @return {number} The value of the signed 64-bit integer field. |
| 636 | */ |
| 637 | jspb.BinaryReader.prototype.readInt64 = function() { |
| 638 | goog.asserts.assert( |
| 639 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 640 | return this.decoder_.readSignedVarint64(); |
| 641 | }; |
| 642 | |
| 643 | |
| 644 | /** |
| 645 | * Reads a signed 64-bit integer field from the binary stream, or throws an |
| 646 | * error if the next field in the stream is not of the correct wire type. |
| 647 | * |
| 648 | * Returns the value as a string. |
| 649 | * |
| 650 | * @return {string} The value of the signed 64-bit integer field as a decimal |
| 651 | * string. |
| 652 | */ |
| 653 | jspb.BinaryReader.prototype.readInt64String = function() { |
| 654 | goog.asserts.assert( |
| 655 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 656 | return this.decoder_.readSignedVarint64String(); |
| 657 | }; |
| 658 | |
| 659 | |
| 660 | /** |
| 661 | * Reads an unsigned 32-bit integer field from the binary stream, or throws an |
| 662 | * error if the next field in the stream is not of the correct wire type. |
| 663 | * |
| 664 | * @return {number} The value of the unsigned 32-bit integer field. |
| 665 | */ |
| 666 | jspb.BinaryReader.prototype.readUint32 = function() { |
| 667 | goog.asserts.assert( |
| 668 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 669 | return this.decoder_.readUnsignedVarint32(); |
| 670 | }; |
| 671 | |
| 672 | |
| 673 | /** |
| 674 | * Reads an unsigned 32-bit integer field from the binary stream, or throws an |
| 675 | * error if the next field in the stream is not of the correct wire type. |
| 676 | * |
| 677 | * Returns the value as a string. |
| 678 | * |
| 679 | * @return {string} The value of the unsigned 32-bit integer field as a decimal |
| 680 | * string. |
| 681 | */ |
| 682 | jspb.BinaryReader.prototype.readUint32String = function() { |
| 683 | goog.asserts.assert( |
| 684 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 685 | return this.decoder_.readUnsignedVarint32String(); |
| 686 | }; |
| 687 | |
| 688 | |
| 689 | /** |
| 690 | * Reads an unsigned 64-bit integer field from the binary stream, or throws an |
| 691 | * error if the next field in the stream is not of the correct wire type. |
| 692 | * |
| 693 | * @return {number} The value of the unsigned 64-bit integer field. |
| 694 | */ |
| 695 | jspb.BinaryReader.prototype.readUint64 = function() { |
| 696 | goog.asserts.assert( |
| 697 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 698 | return this.decoder_.readUnsignedVarint64(); |
| 699 | }; |
| 700 | |
| 701 | |
| 702 | /** |
| 703 | * Reads an unsigned 64-bit integer field from the binary stream, or throws an |
| 704 | * error if the next field in the stream is not of the correct wire type. |
| 705 | * |
| 706 | * Returns the value as a string. |
| 707 | * |
| 708 | * @return {string} The value of the unsigned 64-bit integer field as a decimal |
| 709 | * string. |
| 710 | */ |
| 711 | jspb.BinaryReader.prototype.readUint64String = function() { |
| 712 | goog.asserts.assert( |
| 713 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 714 | return this.decoder_.readUnsignedVarint64String(); |
| 715 | }; |
| 716 | |
| 717 | |
| 718 | /** |
| 719 | * Reads a signed zigzag-encoded 32-bit integer field from the binary stream, |
| 720 | * or throws an error if the next field in the stream is not of the correct |
| 721 | * wire type. |
| 722 | * |
| 723 | * @return {number} The value of the signed 32-bit integer field. |
| 724 | */ |
| 725 | jspb.BinaryReader.prototype.readSint32 = function() { |
| 726 | goog.asserts.assert( |
| 727 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 728 | return this.decoder_.readZigzagVarint32(); |
| 729 | }; |
| 730 | |
| 731 | |
| 732 | /** |
| 733 | * Reads a signed zigzag-encoded 64-bit integer field from the binary stream, |
| 734 | * or throws an error if the next field in the stream is not of the correct |
| 735 | * wire type. |
| 736 | * |
| 737 | * @return {number} The value of the signed 64-bit integer field. |
| 738 | */ |
| 739 | jspb.BinaryReader.prototype.readSint64 = function() { |
| 740 | goog.asserts.assert( |
| 741 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 742 | return this.decoder_.readZigzagVarint64(); |
| 743 | }; |
| 744 | |
| 745 | |
| 746 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 747 | * Reads a signed zigzag-encoded 64-bit integer field from the binary stream, |
| 748 | * or throws an error if the next field in the stream is not of the correct |
| 749 | * wire type. |
| 750 | * |
| 751 | * @return {string} The value of the signed 64-bit integer field as a decimal string. |
| 752 | */ |
| 753 | jspb.BinaryReader.prototype.readSint64String = function() { |
| 754 | goog.asserts.assert( |
| 755 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 756 | return this.decoder_.readZigzagVarint64String(); |
| 757 | }; |
| 758 | |
| 759 | |
| 760 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 761 | * Reads an unsigned 32-bit fixed-length integer fiield from the binary stream, |
| 762 | * or throws an error if the next field in the stream is not of the correct |
| 763 | * wire type. |
| 764 | * |
| 765 | * @return {number} The value of the double field. |
| 766 | */ |
| 767 | jspb.BinaryReader.prototype.readFixed32 = function() { |
| 768 | goog.asserts.assert( |
| 769 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32); |
| 770 | return this.decoder_.readUint32(); |
| 771 | }; |
| 772 | |
| 773 | |
| 774 | /** |
| 775 | * Reads an unsigned 64-bit fixed-length integer fiield from the binary stream, |
| 776 | * or throws an error if the next field in the stream is not of the correct |
| 777 | * wire type. |
| 778 | * |
| 779 | * @return {number} The value of the float field. |
| 780 | */ |
| 781 | jspb.BinaryReader.prototype.readFixed64 = function() { |
| 782 | goog.asserts.assert( |
| 783 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64); |
| 784 | return this.decoder_.readUint64(); |
| 785 | }; |
| 786 | |
| 787 | |
| 788 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 789 | * Reads a signed 64-bit integer field from the binary stream as a string, or |
| 790 | * throws an error if the next field in the stream is not of the correct wire |
| 791 | * type. |
| 792 | * |
| 793 | * Returns the value as a string. |
| 794 | * |
| 795 | * @return {string} The value of the unsigned 64-bit integer field as a decimal |
| 796 | * string. |
| 797 | */ |
| 798 | jspb.BinaryReader.prototype.readFixed64String = function() { |
| 799 | goog.asserts.assert( |
| 800 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64); |
| 801 | return this.decoder_.readUint64String(); |
| 802 | }; |
| 803 | |
| 804 | |
| 805 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 806 | * Reads a signed 32-bit fixed-length integer fiield from the binary stream, or |
| 807 | * throws an error if the next field in the stream is not of the correct wire |
| 808 | * type. |
| 809 | * |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 810 | * @return {number} The value of the signed 32-bit integer field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 811 | */ |
| 812 | jspb.BinaryReader.prototype.readSfixed32 = function() { |
| 813 | goog.asserts.assert( |
| 814 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32); |
| 815 | return this.decoder_.readInt32(); |
| 816 | }; |
| 817 | |
| 818 | |
| 819 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 820 | * Reads a signed 32-bit fixed-length integer fiield from the binary stream, or |
| 821 | * throws an error if the next field in the stream is not of the correct wire |
| 822 | * type. |
| 823 | * |
| 824 | * @return {string} The value of the signed 32-bit integer field as a decimal |
| 825 | * string. |
| 826 | */ |
| 827 | jspb.BinaryReader.prototype.readSfixed32String = function() { |
| 828 | goog.asserts.assert( |
| 829 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32); |
| 830 | return this.decoder_.readInt32().toString(); |
| 831 | }; |
| 832 | |
| 833 | |
| 834 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 835 | * Reads a signed 64-bit fixed-length integer fiield from the binary stream, or |
| 836 | * throws an error if the next field in the stream is not of the correct wire |
| 837 | * type. |
| 838 | * |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 839 | * @return {number} The value of the sfixed64 field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 840 | */ |
| 841 | jspb.BinaryReader.prototype.readSfixed64 = function() { |
| 842 | goog.asserts.assert( |
| 843 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64); |
| 844 | return this.decoder_.readInt64(); |
| 845 | }; |
| 846 | |
| 847 | |
| 848 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 849 | * Reads a signed 64-bit fixed-length integer fiield from the binary stream, or |
| 850 | * throws an error if the next field in the stream is not of the correct wire |
| 851 | * type. |
| 852 | * |
| 853 | * Returns the value as a string. |
| 854 | * |
| 855 | * @return {string} The value of the sfixed64 field as a decimal string. |
| 856 | */ |
| 857 | jspb.BinaryReader.prototype.readSfixed64String = function() { |
| 858 | goog.asserts.assert( |
| 859 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64); |
| 860 | return this.decoder_.readInt64String(); |
| 861 | }; |
| 862 | |
| 863 | |
| 864 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 865 | * Reads a 32-bit floating-point field from the binary stream, or throws an |
| 866 | * error if the next field in the stream is not of the correct wire type. |
| 867 | * |
| 868 | * @return {number} The value of the float field. |
| 869 | */ |
| 870 | jspb.BinaryReader.prototype.readFloat = function() { |
| 871 | goog.asserts.assert( |
| 872 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED32); |
| 873 | return this.decoder_.readFloat(); |
| 874 | }; |
| 875 | |
| 876 | |
| 877 | /** |
| 878 | * Reads a 64-bit floating-point field from the binary stream, or throws an |
| 879 | * error if the next field in the stream is not of the correct wire type. |
| 880 | * |
| 881 | * @return {number} The value of the double field. |
| 882 | */ |
| 883 | jspb.BinaryReader.prototype.readDouble = function() { |
| 884 | goog.asserts.assert( |
| 885 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64); |
| 886 | return this.decoder_.readDouble(); |
| 887 | }; |
| 888 | |
| 889 | |
| 890 | /** |
| 891 | * Reads a boolean field from the binary stream, or throws an error if the next |
| 892 | * field in the stream is not of the correct wire type. |
| 893 | * |
| 894 | * @return {boolean} The value of the boolean field. |
| 895 | */ |
| 896 | jspb.BinaryReader.prototype.readBool = function() { |
| 897 | goog.asserts.assert( |
| 898 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 899 | return !!this.decoder_.readUnsignedVarint32(); |
| 900 | }; |
| 901 | |
| 902 | |
| 903 | /** |
| 904 | * Reads an enum field from the binary stream, or throws an error if the next |
| 905 | * field in the stream is not of the correct wire type. |
| 906 | * |
| 907 | * @return {number} The value of the enum field. |
| 908 | */ |
| 909 | jspb.BinaryReader.prototype.readEnum = function() { |
| 910 | goog.asserts.assert( |
| 911 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 912 | return this.decoder_.readSignedVarint64(); |
| 913 | }; |
| 914 | |
| 915 | |
| 916 | /** |
| 917 | * Reads a string field from the binary stream, or throws an error if the next |
| 918 | * field in the stream is not of the correct wire type. |
| 919 | * |
| 920 | * @return {string} The value of the string field. |
| 921 | */ |
| 922 | jspb.BinaryReader.prototype.readString = function() { |
| 923 | goog.asserts.assert( |
| 924 | this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED); |
| 925 | var length = this.decoder_.readUnsignedVarint32(); |
| 926 | return this.decoder_.readString(length); |
| 927 | }; |
| 928 | |
| 929 | |
| 930 | /** |
| 931 | * Reads a length-prefixed block of bytes from the binary stream, or returns |
| 932 | * null if the next field in the stream has an invalid length value. |
| 933 | * |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 934 | * @return {!Uint8Array} The block of bytes. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 935 | */ |
| 936 | jspb.BinaryReader.prototype.readBytes = function() { |
| 937 | goog.asserts.assert( |
| 938 | this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED); |
| 939 | var length = this.decoder_.readUnsignedVarint32(); |
| 940 | return this.decoder_.readBytes(length); |
| 941 | }; |
| 942 | |
| 943 | |
| 944 | /** |
| 945 | * Reads a 64-bit varint or fixed64 field from the stream and returns it as a |
| 946 | * 8-character Unicode string for use as a hash table key, or throws an error |
| 947 | * if the next field in the stream is not of the correct wire type. |
| 948 | * |
| 949 | * @return {string} The hash value. |
| 950 | */ |
| 951 | jspb.BinaryReader.prototype.readVarintHash64 = function() { |
| 952 | goog.asserts.assert( |
| 953 | this.nextWireType_ == jspb.BinaryConstants.WireType.VARINT); |
| 954 | return this.decoder_.readVarintHash64(); |
| 955 | }; |
| 956 | |
| 957 | |
| 958 | /** |
| 959 | * Reads a 64-bit varint or fixed64 field from the stream and returns it as a |
| 960 | * 8-character Unicode string for use as a hash table key, or throws an error |
| 961 | * if the next field in the stream is not of the correct wire type. |
| 962 | * |
| 963 | * @return {string} The hash value. |
| 964 | */ |
| 965 | jspb.BinaryReader.prototype.readFixedHash64 = function() { |
| 966 | goog.asserts.assert( |
| 967 | this.nextWireType_ == jspb.BinaryConstants.WireType.FIXED64); |
| 968 | return this.decoder_.readFixedHash64(); |
| 969 | }; |
| 970 | |
| 971 | |
| 972 | /** |
| 973 | * Reads a packed scalar field using the supplied raw reader function. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 974 | * @param {function(this:jspb.BinaryDecoder)} decodeMethod |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 975 | * @return {!Array} |
| 976 | * @private |
| 977 | */ |
| 978 | jspb.BinaryReader.prototype.readPackedField_ = function(decodeMethod) { |
| 979 | goog.asserts.assert( |
| 980 | this.nextWireType_ == jspb.BinaryConstants.WireType.DELIMITED); |
| 981 | var length = this.decoder_.readUnsignedVarint32(); |
| 982 | var end = this.decoder_.getCursor() + length; |
| 983 | var result = []; |
| 984 | while (this.decoder_.getCursor() < end) { |
| 985 | // TODO(aappleby): .call is slow |
| 986 | result.push(decodeMethod.call(this.decoder_)); |
| 987 | } |
| 988 | return result; |
| 989 | }; |
| 990 | |
| 991 | |
| 992 | /** |
| 993 | * Reads a packed int32 field, which consists of a length header and a list of |
| 994 | * signed varints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 995 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 996 | */ |
| 997 | jspb.BinaryReader.prototype.readPackedInt32 = function() { |
| 998 | return this.readPackedField_(this.decoder_.readSignedVarint32); |
| 999 | }; |
| 1000 | |
| 1001 | |
| 1002 | /** |
| 1003 | * Reads a packed int32 field, which consists of a length header and a list of |
| 1004 | * signed varints. Returns a list of strings. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1005 | * @return {!Array<string>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1006 | */ |
| 1007 | jspb.BinaryReader.prototype.readPackedInt32String = function() { |
| 1008 | return this.readPackedField_(this.decoder_.readSignedVarint32String); |
| 1009 | }; |
| 1010 | |
| 1011 | |
| 1012 | /** |
| 1013 | * Reads a packed int64 field, which consists of a length header and a list of |
| 1014 | * signed varints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1015 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1016 | */ |
| 1017 | jspb.BinaryReader.prototype.readPackedInt64 = function() { |
| 1018 | return this.readPackedField_(this.decoder_.readSignedVarint64); |
| 1019 | }; |
| 1020 | |
| 1021 | |
| 1022 | /** |
| 1023 | * Reads a packed int64 field, which consists of a length header and a list of |
| 1024 | * signed varints. Returns a list of strings. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1025 | * @return {!Array<string>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1026 | */ |
| 1027 | jspb.BinaryReader.prototype.readPackedInt64String = function() { |
| 1028 | return this.readPackedField_(this.decoder_.readSignedVarint64String); |
| 1029 | }; |
| 1030 | |
| 1031 | |
| 1032 | /** |
| 1033 | * Reads a packed uint32 field, which consists of a length header and a list of |
| 1034 | * unsigned varints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1035 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1036 | */ |
| 1037 | jspb.BinaryReader.prototype.readPackedUint32 = function() { |
| 1038 | return this.readPackedField_(this.decoder_.readUnsignedVarint32); |
| 1039 | }; |
| 1040 | |
| 1041 | |
| 1042 | /** |
| 1043 | * Reads a packed uint32 field, which consists of a length header and a list of |
| 1044 | * unsigned varints. Returns a list of strings. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1045 | * @return {!Array<string>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1046 | */ |
| 1047 | jspb.BinaryReader.prototype.readPackedUint32String = function() { |
| 1048 | return this.readPackedField_(this.decoder_.readUnsignedVarint32String); |
| 1049 | }; |
| 1050 | |
| 1051 | |
| 1052 | /** |
| 1053 | * Reads a packed uint64 field, which consists of a length header and a list of |
| 1054 | * unsigned varints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1055 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1056 | */ |
| 1057 | jspb.BinaryReader.prototype.readPackedUint64 = function() { |
| 1058 | return this.readPackedField_(this.decoder_.readUnsignedVarint64); |
| 1059 | }; |
| 1060 | |
| 1061 | |
| 1062 | /** |
| 1063 | * Reads a packed uint64 field, which consists of a length header and a list of |
| 1064 | * unsigned varints. Returns a list of strings. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1065 | * @return {!Array<string>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1066 | */ |
| 1067 | jspb.BinaryReader.prototype.readPackedUint64String = function() { |
| 1068 | return this.readPackedField_(this.decoder_.readUnsignedVarint64String); |
| 1069 | }; |
| 1070 | |
| 1071 | |
| 1072 | /** |
| 1073 | * Reads a packed sint32 field, which consists of a length header and a list of |
| 1074 | * zigzag varints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1075 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1076 | */ |
| 1077 | jspb.BinaryReader.prototype.readPackedSint32 = function() { |
| 1078 | return this.readPackedField_(this.decoder_.readZigzagVarint32); |
| 1079 | }; |
| 1080 | |
| 1081 | |
| 1082 | /** |
| 1083 | * Reads a packed sint64 field, which consists of a length header and a list of |
| 1084 | * zigzag varints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1085 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1086 | */ |
| 1087 | jspb.BinaryReader.prototype.readPackedSint64 = function() { |
| 1088 | return this.readPackedField_(this.decoder_.readZigzagVarint64); |
| 1089 | }; |
| 1090 | |
| 1091 | |
| 1092 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1093 | * Reads a packed sint64 field, which consists of a length header and a list of |
| 1094 | * zigzag varints. Returns a list of strings. |
| 1095 | * @return {!Array<string>} |
| 1096 | */ |
| 1097 | jspb.BinaryReader.prototype.readPackedSint64String = function() { |
| 1098 | return this.readPackedField_(this.decoder_.readZigzagVarint64String); |
| 1099 | }; |
| 1100 | |
| 1101 | |
| 1102 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1103 | * Reads a packed fixed32 field, which consists of a length header and a list |
| 1104 | * of unsigned 32-bit ints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1105 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1106 | */ |
| 1107 | jspb.BinaryReader.prototype.readPackedFixed32 = function() { |
| 1108 | return this.readPackedField_(this.decoder_.readUint32); |
| 1109 | }; |
| 1110 | |
| 1111 | |
| 1112 | /** |
| 1113 | * Reads a packed fixed64 field, which consists of a length header and a list |
| 1114 | * of unsigned 64-bit ints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1115 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1116 | */ |
| 1117 | jspb.BinaryReader.prototype.readPackedFixed64 = function() { |
| 1118 | return this.readPackedField_(this.decoder_.readUint64); |
| 1119 | }; |
| 1120 | |
| 1121 | |
| 1122 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1123 | * Reads a packed fixed64 field, which consists of a length header and a list |
| 1124 | * of unsigned 64-bit ints. Returns a list of strings. |
| 1125 | * @return {!Array<number>} |
| 1126 | */ |
| 1127 | jspb.BinaryReader.prototype.readPackedFixed64String = function() { |
| 1128 | return this.readPackedField_(this.decoder_.readUint64String); |
| 1129 | }; |
| 1130 | |
| 1131 | |
| 1132 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1133 | * Reads a packed sfixed32 field, which consists of a length header and a list |
| 1134 | * of 32-bit ints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1135 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1136 | */ |
| 1137 | jspb.BinaryReader.prototype.readPackedSfixed32 = function() { |
| 1138 | return this.readPackedField_(this.decoder_.readInt32); |
| 1139 | }; |
| 1140 | |
| 1141 | |
| 1142 | /** |
| 1143 | * Reads a packed sfixed64 field, which consists of a length header and a list |
| 1144 | * of 64-bit ints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1145 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1146 | */ |
| 1147 | jspb.BinaryReader.prototype.readPackedSfixed64 = function() { |
| 1148 | return this.readPackedField_(this.decoder_.readInt64); |
| 1149 | }; |
| 1150 | |
| 1151 | |
| 1152 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1153 | * Reads a packed sfixed64 field, which consists of a length header and a list |
| 1154 | * of 64-bit ints. Returns a list of strings. |
| 1155 | * @return {!Array<string>} |
| 1156 | */ |
| 1157 | jspb.BinaryReader.prototype.readPackedSfixed64String = function() { |
| 1158 | return this.readPackedField_(this.decoder_.readInt64String); |
| 1159 | }; |
| 1160 | |
| 1161 | |
| 1162 | /** |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1163 | * Reads a packed float field, which consists of a length header and a list of |
| 1164 | * floats. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1165 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1166 | */ |
| 1167 | jspb.BinaryReader.prototype.readPackedFloat = function() { |
| 1168 | return this.readPackedField_(this.decoder_.readFloat); |
| 1169 | }; |
| 1170 | |
| 1171 | |
| 1172 | /** |
| 1173 | * Reads a packed double field, which consists of a length header and a list of |
| 1174 | * doubles. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1175 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1176 | */ |
| 1177 | jspb.BinaryReader.prototype.readPackedDouble = function() { |
| 1178 | return this.readPackedField_(this.decoder_.readDouble); |
| 1179 | }; |
| 1180 | |
| 1181 | |
| 1182 | /** |
| 1183 | * Reads a packed bool field, which consists of a length header and a list of |
| 1184 | * unsigned varints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1185 | * @return {!Array<boolean>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1186 | */ |
| 1187 | jspb.BinaryReader.prototype.readPackedBool = function() { |
| 1188 | return this.readPackedField_(this.decoder_.readBool); |
| 1189 | }; |
| 1190 | |
| 1191 | |
| 1192 | /** |
| 1193 | * Reads a packed enum field, which consists of a length header and a list of |
| 1194 | * unsigned varints. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1195 | * @return {!Array<number>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1196 | */ |
| 1197 | jspb.BinaryReader.prototype.readPackedEnum = function() { |
| 1198 | return this.readPackedField_(this.decoder_.readEnum); |
| 1199 | }; |
| 1200 | |
| 1201 | |
| 1202 | /** |
| 1203 | * Reads a packed varint hash64 field, which consists of a length header and a |
| 1204 | * list of varint hash64s. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1205 | * @return {!Array<string>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1206 | */ |
| 1207 | jspb.BinaryReader.prototype.readPackedVarintHash64 = function() { |
| 1208 | return this.readPackedField_(this.decoder_.readVarintHash64); |
| 1209 | }; |
| 1210 | |
| 1211 | |
| 1212 | /** |
| 1213 | * Reads a packed fixed hash64 field, which consists of a length header and a |
| 1214 | * list of fixed hash64s. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1215 | * @return {!Array<string>} |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1216 | */ |
| 1217 | jspb.BinaryReader.prototype.readPackedFixedHash64 = function() { |
| 1218 | return this.readPackedField_(this.decoder_.readFixedHash64); |
| 1219 | }; |