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 package profile
35
36 import (
37 "errors"
38 "fmt"
39 )
40
41 type buffer struct {
42 field int
43 typ int
44 u64 uint64
45 data []byte
46 tmp [16]byte
47 }
48
49 type decoder func(*buffer, message) error
50
51 type message interface {
52 decoder() []decoder
53 encode(*buffer)
54 }
55
56 func marshal(m message) []byte {
57 var b buffer
58 m.encode(&b)
59 return b.data
60 }
61
62 func encodeVarint(b *buffer, x uint64) {
63 for x >= 128 {
64 b.data = append(b.data, byte(x)|0x80)
65 x >>= 7
66 }
67 b.data = append(b.data, byte(x))
68 }
69
70 func encodeLength(b *buffer, tag int, len int) {
71 encodeVarint(b, uint64(tag)<<3|2)
72 encodeVarint(b, uint64(len))
73 }
74
75 func encodeUint64(b *buffer, tag int, x uint64) {
76
77 encodeVarint(b, uint64(tag)<<3)
78 encodeVarint(b, x)
79 }
80
81 func encodeUint64s(b *buffer, tag int, x []uint64) {
82 if len(x) > 2 {
83
84 n1 := len(b.data)
85 for _, u := range x {
86 encodeVarint(b, u)
87 }
88 n2 := len(b.data)
89 encodeLength(b, tag, n2-n1)
90 n3 := len(b.data)
91 copy(b.tmp[:], b.data[n2:n3])
92 copy(b.data[n1+(n3-n2):], b.data[n1:n2])
93 copy(b.data[n1:], b.tmp[:n3-n2])
94 return
95 }
96 for _, u := range x {
97 encodeUint64(b, tag, u)
98 }
99 }
100
101 func encodeUint64Opt(b *buffer, tag int, x uint64) {
102 if x == 0 {
103 return
104 }
105 encodeUint64(b, tag, x)
106 }
107
108 func encodeInt64(b *buffer, tag int, x int64) {
109 u := uint64(x)
110 encodeUint64(b, tag, u)
111 }
112
113 func encodeInt64s(b *buffer, tag int, x []int64) {
114 if len(x) > 2 {
115
116 n1 := len(b.data)
117 for _, u := range x {
118 encodeVarint(b, uint64(u))
119 }
120 n2 := len(b.data)
121 encodeLength(b, tag, n2-n1)
122 n3 := len(b.data)
123 copy(b.tmp[:], b.data[n2:n3])
124 copy(b.data[n1+(n3-n2):], b.data[n1:n2])
125 copy(b.data[n1:], b.tmp[:n3-n2])
126 return
127 }
128 for _, u := range x {
129 encodeInt64(b, tag, u)
130 }
131 }
132
133 func encodeInt64Opt(b *buffer, tag int, x int64) {
134 if x == 0 {
135 return
136 }
137 encodeInt64(b, tag, x)
138 }
139
140 func encodeString(b *buffer, tag int, x string) {
141 encodeLength(b, tag, len(x))
142 b.data = append(b.data, x...)
143 }
144
145 func encodeStrings(b *buffer, tag int, x []string) {
146 for _, s := range x {
147 encodeString(b, tag, s)
148 }
149 }
150
151 func encodeBool(b *buffer, tag int, x bool) {
152 if x {
153 encodeUint64(b, tag, 1)
154 } else {
155 encodeUint64(b, tag, 0)
156 }
157 }
158
159 func encodeBoolOpt(b *buffer, tag int, x bool) {
160 if x {
161 encodeBool(b, tag, x)
162 }
163 }
164
165 func encodeMessage(b *buffer, tag int, m message) {
166 n1 := len(b.data)
167 m.encode(b)
168 n2 := len(b.data)
169 encodeLength(b, tag, n2-n1)
170 n3 := len(b.data)
171 copy(b.tmp[:], b.data[n2:n3])
172 copy(b.data[n1+(n3-n2):], b.data[n1:n2])
173 copy(b.data[n1:], b.tmp[:n3-n2])
174 }
175
176 func unmarshal(data []byte, m message) (err error) {
177 b := buffer{data: data, typ: 2}
178 return decodeMessage(&b, m)
179 }
180
181 func le64(p []byte) uint64 {
182 return uint64(p[0]) | uint64(p[1])<<8 | uint64(p[2])<<16 | uint64(p[3])<<24 | uint64(p[4])<<32 | uint64(p[5])<<40 | uint64(p[6])<<48 | uint64(p[7])<<56
183 }
184
185 func le32(p []byte) uint32 {
186 return uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24
187 }
188
189 func decodeVarint(data []byte) (uint64, []byte, error) {
190 var u uint64
191 for i := 0; ; i++ {
192 if i >= 10 || i >= len(data) {
193 return 0, nil, errors.New("bad varint")
194 }
195 u |= uint64(data[i]&0x7F) << uint(7*i)
196 if data[i]&0x80 == 0 {
197 return u, data[i+1:], nil
198 }
199 }
200 }
201
202 func decodeField(b *buffer, data []byte) ([]byte, error) {
203 x, data, err := decodeVarint(data)
204 if err != nil {
205 return nil, err
206 }
207 b.field = int(x >> 3)
208 b.typ = int(x & 7)
209 b.data = nil
210 b.u64 = 0
211 switch b.typ {
212 case 0:
213 b.u64, data, err = decodeVarint(data)
214 if err != nil {
215 return nil, err
216 }
217 case 1:
218 if len(data) < 8 {
219 return nil, errors.New("not enough data")
220 }
221 b.u64 = le64(data[:8])
222 data = data[8:]
223 case 2:
224 var n uint64
225 n, data, err = decodeVarint(data)
226 if err != nil {
227 return nil, err
228 }
229 if n > uint64(len(data)) {
230 return nil, errors.New("too much data")
231 }
232 b.data = data[:n]
233 data = data[n:]
234 case 5:
235 if len(data) < 4 {
236 return nil, errors.New("not enough data")
237 }
238 b.u64 = uint64(le32(data[:4]))
239 data = data[4:]
240 default:
241 return nil, fmt.Errorf("unknown wire type: %d", b.typ)
242 }
243
244 return data, nil
245 }
246
247 func checkType(b *buffer, typ int) error {
248 if b.typ != typ {
249 return errors.New("type mismatch")
250 }
251 return nil
252 }
253
254 func decodeMessage(b *buffer, m message) error {
255 if err := checkType(b, 2); err != nil {
256 return err
257 }
258 dec := m.decoder()
259 data := b.data
260 for len(data) > 0 {
261
262 var err error
263 data, err = decodeField(b, data)
264 if err != nil {
265 return err
266 }
267 if b.field >= len(dec) || dec[b.field] == nil {
268 continue
269 }
270 if err := dec[b.field](b, m); err != nil {
271 return err
272 }
273 }
274 return nil
275 }
276
277 func decodeInt64(b *buffer, x *int64) error {
278 if err := checkType(b, 0); err != nil {
279 return err
280 }
281 *x = int64(b.u64)
282 return nil
283 }
284
285 func decodeInt64s(b *buffer, x *[]int64) error {
286 if b.typ == 2 {
287
288 data := b.data
289 tmp := make([]int64, 0, len(data))
290 for len(data) > 0 {
291 var u uint64
292 var err error
293
294 if u, data, err = decodeVarint(data); err != nil {
295 return err
296 }
297 tmp = append(tmp, int64(u))
298 }
299 *x = append(*x, tmp...)
300 return nil
301 }
302 var i int64
303 if err := decodeInt64(b, &i); err != nil {
304 return err
305 }
306 *x = append(*x, i)
307 return nil
308 }
309
310 func decodeUint64(b *buffer, x *uint64) error {
311 if err := checkType(b, 0); err != nil {
312 return err
313 }
314 *x = b.u64
315 return nil
316 }
317
318 func decodeUint64s(b *buffer, x *[]uint64) error {
319 if b.typ == 2 {
320 data := b.data
321
322 tmp := make([]uint64, 0, len(data))
323 for len(data) > 0 {
324 var u uint64
325 var err error
326
327 if u, data, err = decodeVarint(data); err != nil {
328 return err
329 }
330 tmp = append(tmp, u)
331 }
332 *x = append(*x, tmp...)
333 return nil
334 }
335 var u uint64
336 if err := decodeUint64(b, &u); err != nil {
337 return err
338 }
339 *x = append(*x, u)
340 return nil
341 }
342
343 func decodeString(b *buffer, x *string) error {
344 if err := checkType(b, 2); err != nil {
345 return err
346 }
347 *x = string(b.data)
348 return nil
349 }
350
351 func decodeStrings(b *buffer, x *[]string) error {
352 var s string
353 if err := decodeString(b, &s); err != nil {
354 return err
355 }
356 *x = append(*x, s)
357 return nil
358 }
359
360 func decodeBool(b *buffer, x *bool) error {
361 if err := checkType(b, 0); err != nil {
362 return err
363 }
364 if int64(b.u64) == 0 {
365 *x = false
366 } else {
367 *x = true
368 }
369 return nil
370 }
371
View as plain text