|
|
@@ -61,7 +61,6 @@ var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for
|
|
|
// int32, int64, uint32, uint64, bool, and enum
|
|
|
// protocol buffer types.
|
|
|
func DecodeVarint(buf []byte) (x uint64, n int) {
|
|
|
- // x, n already 0
|
|
|
for shift := uint(0); shift < 64; shift += 7 {
|
|
|
if n >= len(buf) {
|
|
|
return 0, 0
|
|
|
@@ -78,13 +77,7 @@ func DecodeVarint(buf []byte) (x uint64, n int) {
|
|
|
return 0, 0
|
|
|
}
|
|
|
|
|
|
-// DecodeVarint reads a varint-encoded integer from the Buffer.
|
|
|
-// This is the format for the
|
|
|
-// int32, int64, uint32, uint64, bool, and enum
|
|
|
-// protocol buffer types.
|
|
|
-func (p *Buffer) DecodeVarint() (x uint64, err error) {
|
|
|
- // x, err already 0
|
|
|
-
|
|
|
+func (p *Buffer) decodeVarintSlow() (x uint64, err error) {
|
|
|
i := p.index
|
|
|
l := len(p.buf)
|
|
|
|
|
|
@@ -107,6 +100,107 @@ func (p *Buffer) DecodeVarint() (x uint64, err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// DecodeVarint reads a varint-encoded integer from the Buffer.
|
|
|
+// This is the format for the
|
|
|
+// int32, int64, uint32, uint64, bool, and enum
|
|
|
+// protocol buffer types.
|
|
|
+func (p *Buffer) DecodeVarint() (x uint64, err error) {
|
|
|
+ i := p.index
|
|
|
+ buf := p.buf
|
|
|
+
|
|
|
+ if i >= len(buf) {
|
|
|
+ return 0, io.ErrUnexpectedEOF
|
|
|
+ } else if buf[i] < 0x80 {
|
|
|
+ p.index++
|
|
|
+ return uint64(buf[i]), nil
|
|
|
+ } else if len(buf)-i < 10 {
|
|
|
+ return p.decodeVarintSlow()
|
|
|
+ }
|
|
|
+
|
|
|
+ var b uint64
|
|
|
+ // we already checked the first byte
|
|
|
+ x = uint64(buf[i]) - 0x80
|
|
|
+ i++
|
|
|
+
|
|
|
+ b = uint64(buf[i])
|
|
|
+ i++
|
|
|
+ x += b << 7
|
|
|
+ if b&0x80 == 0 {
|
|
|
+ goto done
|
|
|
+ }
|
|
|
+ x -= 0x80 << 7
|
|
|
+
|
|
|
+ b = uint64(buf[i])
|
|
|
+ i++
|
|
|
+ x += b << 14
|
|
|
+ if b&0x80 == 0 {
|
|
|
+ goto done
|
|
|
+ }
|
|
|
+ x -= 0x80 << 14
|
|
|
+
|
|
|
+ b = uint64(buf[i])
|
|
|
+ i++
|
|
|
+ x += b << 21
|
|
|
+ if b&0x80 == 0 {
|
|
|
+ goto done
|
|
|
+ }
|
|
|
+ x -= 0x80 << 21
|
|
|
+
|
|
|
+ b = uint64(buf[i])
|
|
|
+ i++
|
|
|
+ x += b << 28
|
|
|
+ if b&0x80 == 0 {
|
|
|
+ goto done
|
|
|
+ }
|
|
|
+ x -= 0x80 << 28
|
|
|
+
|
|
|
+ b = uint64(buf[i])
|
|
|
+ i++
|
|
|
+ x += b << 35
|
|
|
+ if b&0x80 == 0 {
|
|
|
+ goto done
|
|
|
+ }
|
|
|
+ x -= 0x80 << 35
|
|
|
+
|
|
|
+ b = uint64(buf[i])
|
|
|
+ i++
|
|
|
+ x += b << 42
|
|
|
+ if b&0x80 == 0 {
|
|
|
+ goto done
|
|
|
+ }
|
|
|
+ x -= 0x80 << 42
|
|
|
+
|
|
|
+ b = uint64(buf[i])
|
|
|
+ i++
|
|
|
+ x += b << 49
|
|
|
+ if b&0x80 == 0 {
|
|
|
+ goto done
|
|
|
+ }
|
|
|
+ x -= 0x80 << 49
|
|
|
+
|
|
|
+ b = uint64(buf[i])
|
|
|
+ i++
|
|
|
+ x += b << 56
|
|
|
+ if b&0x80 == 0 {
|
|
|
+ goto done
|
|
|
+ }
|
|
|
+ x -= 0x80 << 56
|
|
|
+
|
|
|
+ b = uint64(buf[i])
|
|
|
+ i++
|
|
|
+ x += b << 63
|
|
|
+ if b&0x80 == 0 {
|
|
|
+ goto done
|
|
|
+ }
|
|
|
+ // x -= 0x80 << 63 // Always zero.
|
|
|
+
|
|
|
+ return 0, errOverflow
|
|
|
+
|
|
|
+done:
|
|
|
+ p.index = i
|
|
|
+ return x, nil
|
|
|
+}
|
|
|
+
|
|
|
// DecodeFixed64 reads a 64-bit integer from the Buffer.
|
|
|
// This is the format for the
|
|
|
// fixed64, sfixed64, and double protocol buffer types.
|