File size: 1,151 Bytes
e36aeda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package trace

import (
	"errors"
)

// maxVarintLenN is the maximum length of a varint-encoded N-bit integer.
const maxVarintLen64 = 10

var (
	errOverflow = errors.New("binary: varint overflows a 64-bit integer")
	errEOB      = errors.New("binary: end of buffer")
)

// TODO deduplicate this function.
func readUvarint(b []byte) (uint64, int, error) {
	var x uint64
	var s uint
	var byt byte
	for i := 0; i < maxVarintLen64 && i < len(b); i++ {
		byt = b[i]
		if byt < 0x80 {
			if i == maxVarintLen64-1 && byt > 1 {
				return x, i, errOverflow
			}
			return x | uint64(byt)<<s, i + 1, nil
		}
		x |= uint64(byt&0x7f) << s
		s += 7
	}
	return x, len(b), errOverflow
}

// putUvarint encodes a uint64 into buf and returns the number of bytes written.
// If the buffer is too small, PutUvarint will panic.
// TODO deduplicate this function.
func putUvarint(buf []byte, x uint64) int {
	i := 0
	for x >= 0x80 {
		buf[i] = byte(x) | 0x80
		x >>= 7
		i++
	}
	buf[i] = byte(x)
	return i + 1
}