-
Notifications
You must be signed in to change notification settings - Fork 0
/
varint.go
155 lines (131 loc) · 3.33 KB
/
varint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package polo
import (
"encoding/binary"
"errors"
"io"
"math/bits"
)
// varint errors
var (
errVarintTerminated = errors.New("varint terminated prematurely")
errVarintOverflow = errors.New("varint overflows 64-bit integer")
)
// isBitSize returns whether x is a valid bit-size.
// Must be a power of 2 between 8 and 64. (8, 16, 32, 64)
func isBitSize(x int) bool {
return (x != 0) && ((x & (x - 1)) == 0) && (x <= 64) && (x >= 8)
}
// sizeInteger returns the min number of bytes
// required to represent an unsigned 64-bit integer.
func sizeInteger(v uint64) int {
return (bits.Len64(v) + 8 - 1) / 8
}
// sizeVarint returns the length of an encoded varint slice of bytes for a given uint64.
func sizeVarint(v uint64) int {
return int(9*uint32(bits.Len64(v))+64) / 64
}
// encodeVarint encodes a given uint64 into slice of bytes with varint encoding.
func encodeVarint(v uint64) []byte {
varint := make([]byte, sizeVarint(v))
n := binary.PutUvarint(varint, v)
return varint[:n]
}
// appendVarint appends a given uint64 into a given slice of bytes as varint-encoded dat.
func appendVarint(b []byte, v uint64) []byte {
switch {
case v < 1<<7:
b = append(b, byte(v))
case v < 1<<14:
b = append(b,
byte((v>>0)&0x7f|0x80),
byte(v>>7))
case v < 1<<21:
b = append(b,
byte((v>>0)&0x7f|0x80),
byte((v>>7)&0x7f|0x80),
byte(v>>14))
case v < 1<<28:
b = append(b,
byte((v>>0)&0x7f|0x80),
byte((v>>7)&0x7f|0x80),
byte((v>>14)&0x7f|0x80),
byte(v>>21))
case v < 1<<35:
b = append(b,
byte((v>>0)&0x7f|0x80),
byte((v>>7)&0x7f|0x80),
byte((v>>14)&0x7f|0x80),
byte((v>>21)&0x7f|0x80),
byte(v>>28))
case v < 1<<42:
b = append(b,
byte((v>>0)&0x7f|0x80),
byte((v>>7)&0x7f|0x80),
byte((v>>14)&0x7f|0x80),
byte((v>>21)&0x7f|0x80),
byte((v>>28)&0x7f|0x80),
byte(v>>35))
case v < 1<<49:
b = append(b,
byte((v>>0)&0x7f|0x80),
byte((v>>7)&0x7f|0x80),
byte((v>>14)&0x7f|0x80),
byte((v>>21)&0x7f|0x80),
byte((v>>28)&0x7f|0x80),
byte((v>>35)&0x7f|0x80),
byte(v>>42))
case v < 1<<56:
b = append(b,
byte((v>>0)&0x7f|0x80),
byte((v>>7)&0x7f|0x80),
byte((v>>14)&0x7f|0x80),
byte((v>>21)&0x7f|0x80),
byte((v>>28)&0x7f|0x80),
byte((v>>35)&0x7f|0x80),
byte((v>>42)&0x7f|0x80),
byte(v>>49))
case v < 1<<63:
b = append(b,
byte((v>>0)&0x7f|0x80),
byte((v>>7)&0x7f|0x80),
byte((v>>14)&0x7f|0x80),
byte((v>>21)&0x7f|0x80),
byte((v>>28)&0x7f|0x80),
byte((v>>35)&0x7f|0x80),
byte((v>>42)&0x7f|0x80),
byte((v>>49)&0x7f|0x80),
byte(v>>56))
default:
b = append(b,
byte((v>>0)&0x7f|0x80),
byte((v>>7)&0x7f|0x80),
byte((v>>14)&0x7f|0x80),
byte((v>>21)&0x7f|0x80),
byte((v>>28)&0x7f|0x80),
byte((v>>35)&0x7f|0x80),
byte((v>>42)&0x7f|0x80),
byte((v>>49)&0x7f|0x80),
byte((v>>56)&0x7f|0x80),
1)
}
return b
}
// consumeVarint reads an encoded unsigned integer from r and returns it as an uint64.
func consumeVarint(r io.ByteReader) (x uint64, i int, err error) {
var s uint
for i = 0; i < binary.MaxVarintLen64; i++ {
b, err := r.ReadByte()
if err != nil {
return x, i + 1, errVarintTerminated
}
if b < 0x80 {
if i == binary.MaxVarintLen64-1 && b > 1 {
return x, i + 1, errVarintOverflow
}
return x | uint64(b)<<s, i + 1, nil
}
x |= uint64(b&0x7f) << s
s += 7
}
return x, 11, errVarintOverflow
}