Debian Codegraph @

pdata/internal/proto/unmarshal.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package proto // import "go.opentelemetry.io/collector/pdata/internal/proto"
5
6 import (
7 "encoding/binary"
8 "errors"
9 "fmt"
10 "io"
11 )
12
13 // WireType represents the proto wire type.
14 type WireType int8
15
16 const (
17 WireTypeVarint WireType = 0
18 WireTypeI64 WireType = 1
19 WireTypeLen WireType = 2
20 WireTypeStartGroup WireType = 3
21 WireTypeEndGroup WireType = 4
22 WireTypeI32 WireType = 5
23 )
24
25 var (
26 ErrInvalidLength = errors.New("proto: negative length found during unmarshaling")
27 ErrIntOverflow = errors.New("proto: integer overflow")
28 ErrUnexpectedEndOfGroup = errors.New("proto: unexpected end of group")
29 )
30
31 // ConsumeUnknown parses buf starting at pos as a wireType field, reporting the new position.
32 func ConsumeUnknown(buf []byte, pos int, wireType WireType) (int, error) {
33 var err error
34 l := len(buf)
35 depth := 0
36 for pos < l {
37 switch wireType {
38 case WireTypeVarint:
39 _, pos, err = ConsumeVarint(buf, pos)
40 return pos, err
41 case WireTypeI64:
42 _, pos, err = ConsumeI64(buf, pos)
43 return pos, err
44 case WireTypeLen:
45 _, pos, err = ConsumeLen(buf, pos)
46 return pos, err
47 case WireTypeStartGroup:
48 depth++
49 case WireTypeEndGroup:
50 if depth == 0 {
51 return 0, ErrUnexpectedEndOfGroup
52 }
53 depth--
54 case WireTypeI32:
55 _, pos, err = ConsumeI32(buf, pos)
56 return pos, err
57 default:
58 return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
59 }
60
61 // Only when parsing a group can be here, if done return otherwise parse more tags.
62 if depth == 0 {
63 return pos, nil
64 }
65
66 // If in a group parsing, move to the next tag.
67 _, wireType, pos, err = ConsumeTag(buf, pos)
68 if err != nil {
69 return 0, err
70 }
71 }
72 return 0, io.ErrUnexpectedEOF
73 }
74
75 // ConsumeI64 parses buf starting at pos as a WireTypeI64 field, reporting the value and the new position.
76 func ConsumeI64(buf []byte, pos int) (uint64, int, error) {
77 pos += 8
78 if pos < 0 || pos > len(buf) {
79 return 0, 0, io.ErrUnexpectedEOF
80 }
81 return binary.LittleEndian.Uint64(buf[pos-8:]), pos, nil
82 }
83
84 // ConsumeLen parses buf starting at pos as a WireTypeLen field, reporting the len and the new position.
85 func ConsumeLen(buf []byte, pos int) (int, int, error) {
86 var num uint64
87 var err error
88 num, pos, err = ConsumeVarint(buf, pos)
89 if err != nil {
90 return 0, 0, err
91 }
92 length := int(num)
93 if length < 0 {
94 return 0, 0, ErrInvalidLength
95 }
96 pos += length
97 if pos < 0 || pos > len(buf) {
98 return 0, 0, io.ErrUnexpectedEOF
99 }
100 return length, pos, nil
101 }
102
103 // ConsumeI32 parses buf starting at pos as a WireTypeI32 field, reporting the value and the new position.
104 func ConsumeI32(buf []byte, pos int) (uint32, int, error) {
105 pos += 4
106 if pos < 0 || pos > len(buf) {
107 return 0, 0, io.ErrUnexpectedEOF
108 }
109 return binary.LittleEndian.Uint32(buf[pos-4:]), pos, nil
110 }
111
112 // ConsumeTag parses buf starting at pos as a varint-encoded tag, reporting the new position.
113 func ConsumeTag(buf []byte, pos int) (int32, WireType, int, error) {
114 tag, pos, err := ConsumeVarint(buf, pos)
115 if err != nil {
116 return 0, 0, 0, err
117 }
118 fieldNum := int32(tag >> 3)
119 wireType := int8(tag & 0x7)
120 if fieldNum <= 0 {
121 return 0, 0, 0, fmt.Errorf("proto: Link: illegal field=%d (tag=%d, pos=%d)", fieldNum, tag, pos)
122 }
123 return fieldNum, WireType(wireType), pos, nil
124 }
125
126 // ConsumeVarint parses buf starting at pos as a varint-encoded uint64, reporting the new position.
127 func ConsumeVarint(buf []byte, pos int) (uint64, int, error) {
128 l := len(buf)
129 var num uint64
130 for shift := uint(0); ; shift += 7 {
131 if shift >= 64 {
132 return 0, 0, ErrIntOverflow
133 }
134 if pos >= l {
135 return 0, 0, io.ErrUnexpectedEOF
136 }
137 b := buf[pos]
138 pos++
139 num |= uint64(b&0x7F) << shift
140 if b < 0x80 {
141 break
142 }
143 }
144 return num, pos, nil
145 }