| 277 | } |
| 278 | |
| 279 | func (d *decoder) readVarInt() int64 { |
| 280 | n := 11 // varints are at most 11 bytes |
| 281 | |
| 282 | if n > d.remain { |
| 283 | n = d.remain |
| 284 | } |
| 285 | |
| 286 | x := uint64(0) |
| 287 | s := uint(0) |
| 288 | |
| 289 | for n > 0 { |
| 290 | b := d.readByte() |
| 291 | |
| 292 | if (b & 0x80) == 0 { |
| 293 | x |= uint64(b) << s |
| 294 | return int64(x>>1) ^ -(int64(x) & 1) |
| 295 | } |
| 296 | |
| 297 | x |= uint64(b&0x7f) << s |
| 298 | s += 7 |
| 299 | n-- |
| 300 | } |
| 301 | |
| 302 | d.setError(fmt.Errorf("cannot decode varint from input stream")) |
| 303 | return 0 |
| 304 | } |
| 305 | |
| 306 | func (d *decoder) readUnsignedVarInt() uint64 { |
| 307 | n := 11 // varints are at most 11 bytes |