1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // This file implements varint encoding analogous to the one in encoding/binary.
8 // We need a string version of this function, so we add that here and then add
9 // the rest for consistency.
14 errIllegalVarint = errors.New("catmsg: illegal varint")
15 errVarintTooLarge = errors.New("catmsg: varint too large for uint64")
18 const maxVarintBytes = 10 // maximum length of a varint
20 // encodeUint encodes x as a variable-sized integer into buf and returns the
21 // number of bytes written. buf must be at least maxVarintBytes long
22 func encodeUint(buf []byte, x uint64) (n int) {
24 buf[n] = 0x80 | uint8(x&0x7F)
32 func decodeUintString(s string) (x uint64, size int, err error) {
34 for shift := uint(0); shift < 64; shift += 7 {
36 return 0, i, errIllegalVarint
40 x |= (b & 0x7F) << shift
45 return 0, i, errVarintTooLarge
48 func decodeUint(b []byte) (x uint64, size int, err error) {
50 for shift := uint(0); shift < 64; shift += 7 {
52 return 0, i, errIllegalVarint
56 x |= (c & 0x7F) << shift
61 return 0, i, errVarintTooLarge