| 1 |
// Copyright 2025 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. |
| 4 |
|
| 5 |
package trace |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
) |
| 10 |
|
| 11 |
// maxVarintLenN is the maximum length of a varint-encoded N-bit integer. |
| 12 |
const maxVarintLen64 = 10 |
| 13 |
|
| 14 |
var ( |
| 15 |
errOverflow = errors.New("binary: varint overflows a 64-bit integer") |
| 16 |
errEOB = errors.New("binary: end of buffer") |
| 17 |
) |
| 18 |
|
| 19 |
// TODO deduplicate this function. |
| 20 |
func readUvarint(b []byte) (uint64, int, error) { |
| 21 |
var x uint64 |
| 22 |
var s uint |
| 23 |
var byt byte |
| 24 |
for i := 0; i < maxVarintLen64 && i < len(b); i++ { |
| 25 |
byt = b[i] |
| 26 |
if byt < 0x80 { |
| 27 |
if i == maxVarintLen64-1 && byt > 1 { |
| 28 |
return x, i, errOverflow |
| 29 |
} |
| 30 |
return x | uint64(byt)<<s, i + 1, nil |
| 31 |
} |
| 32 |
x |= uint64(byt&0x7f) << s |
| 33 |
s += 7 |
| 34 |
} |
| 35 |
return x, len(b), errOverflow |
| 36 |
} |
| 37 |
|
| 38 |
// putUvarint encodes a uint64 into buf and returns the number of bytes written. |
| 39 |
// If the buffer is too small, PutUvarint will panic. |
| 40 |
// TODO deduplicate this function. |
| 41 |
func putUvarint(buf []byte, x uint64) int { |
| 42 |
i := 0 |
| 43 |
for x >= 0x80 { |
| 44 |
buf[i] = byte(x) | 0x80 |
| 45 |
x >>= 7 |
| 46 |
i++ |
| 47 |
} |
| 48 |
buf[i] = byte(x) |
| 49 |
return i + 1 |
| 50 |
} |