Debian Codegraph @

exp/internal/github.com/planetscale/vtprotobuf/protohelpers/protohelpers.go

raw
1 // Copyright (c) 2022 PlanetScale Inc. All rights reserved.
2
3 // Package protohelpers provides helper functions for encoding and decoding protobuf messages.
4 // The spec can be found at https://protobuf.dev/programming-guides/encoding/.
5 package protohelpers
6
7 import (
8 "errors"
9 "fmt"
10 "io"
11 "math/bits"
12 )
13
14 var (
15 // ErrInvalidLength is returned when decoding a negative length.
16 ErrInvalidLength = errors.New("proto: negative length found during unmarshaling")
17 // ErrIntOverflow is returned when decoding a varint representation of an integer that overflows 64 bits.
18 ErrIntOverflow = errors.New("proto: integer overflow")
19 // ErrUnexpectedEndOfGroup is returned when decoding a group end without a corresponding group start.
20 ErrUnexpectedEndOfGroup = errors.New("proto: unexpected end of group")
21 )
22
23 // EncodeVarint encodes a uint64 into a varint-encoded byte slice and returns the offset of the encoded value.
24 // The provided offset is the offset after the last byte of the encoded value.
25 func EncodeVarint(dAtA []byte, offset int, v uint64) int {
26 offset -= SizeOfVarint(v)
27 base := offset
28 for v >= 1<<7 {
29 dAtA[offset] = uint8(v&0x7f | 0x80)
30 v >>= 7
31 offset++
32 }
33 dAtA[offset] = uint8(v)
34 return base
35 }
36
37 // SizeOfVarint returns the size of the varint-encoded value.
38 func SizeOfVarint(x uint64) (n int) {
39 return (bits.Len64(x|1) + 6) / 7
40 }
41
42 // SizeOfZigzag returns the size of the zigzag-encoded value.
43 func SizeOfZigzag(x uint64) (n int) {
44 return SizeOfVarint((x << 1) ^ uint64(int64(x)>>63))
45 }
46
47 // Skip the first record of the byte slice and return the offset of the next record.
48 func Skip(dAtA []byte) (n int, err error) {
49 l := len(dAtA)
50 iNdEx := 0
51 depth := 0
52 for iNdEx < l {
53 var wire uint64
54 for shift := uint(0); ; shift += 7 {
55 if shift >= 64 {
56 return 0, ErrIntOverflow
57 }
58 if iNdEx >= l {
59 return 0, io.ErrUnexpectedEOF
60 }
61 b := dAtA[iNdEx]
62 iNdEx++
63 wire |= (uint64(b) & 0x7F) << shift
64 if b < 0x80 {
65 break
66 }
67 }
68 wireType := int(wire & 0x7)
69 switch wireType {
70 case 0:
71 for shift := uint(0); ; shift += 7 {
72 if shift >= 64 {
73 return 0, ErrIntOverflow
74 }
75 if iNdEx >= l {
76 return 0, io.ErrUnexpectedEOF
77 }
78 iNdEx++
79 if dAtA[iNdEx-1] < 0x80 {
80 break
81 }
82 }
83 case 1:
84 iNdEx += 8
85 case 2:
86 var length int
87 for shift := uint(0); ; shift += 7 {
88 if shift >= 64 {
89 return 0, ErrIntOverflow
90 }
91 if iNdEx >= l {
92 return 0, io.ErrUnexpectedEOF
93 }
94 b := dAtA[iNdEx]
95 iNdEx++
96 length |= (int(b) & 0x7F) << shift
97 if b < 0x80 {
98 break
99 }
100 }
101 if length < 0 {
102 return 0, ErrInvalidLength
103 }
104 iNdEx += length
105 case 3:
106 depth++
107 case 4:
108 if depth == 0 {
109 return 0, ErrUnexpectedEndOfGroup
110 }
111 depth--
112 case 5:
113 iNdEx += 4
114 default:
115 return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
116 }
117 if iNdEx < 0 {
118 return 0, ErrInvalidLength
119 }
120 if depth == 0 {
121 return iNdEx, nil
122 }
123 }
124 return 0, io.ErrUnexpectedEOF
125 }