Debian Codegraph @

server/proto.go

raw
1 // Copyright 2024 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Inspired by https://github.com/protocolbuffers/protobuf-go/blob/master/encoding/protowire/wire.go
15
16 package server
17
18 import (
19 "errors"
20 "fmt"
21 "math"
22 )
23
24 var errProtoInsufficient = errors.New("insufficient data to read a value")
25 var errProtoOverflow = errors.New("too much data for a value")
26 var errProtoInvalidFieldNumber = errors.New("invalid field number")
27
28 func protoScanField(b []byte) (num, typ, size int, err error) {
29 num, typ, sizeTag, err := protoScanTag(b)
30 if err != nil {
31 return 0, 0, 0, err
32 }
33 b = b[sizeTag:]
34
35 sizeValue, err := protoScanFieldValue(typ, b)
36 if err != nil {
37 return 0, 0, 0, err
38 }
39 return num, typ, sizeTag + sizeValue, nil
40 }
41
42 func protoScanTag(b []byte) (num, typ, size int, err error) {
43 tagint, size, err := protoScanVarint(b)
44 if err != nil {
45 return 0, 0, 0, err
46 }
47
48 // NOTE: MessageSet allows for larger field numbers than normal.
49 if (tagint >> 3) > uint64(math.MaxInt32) {
50 return 0, 0, 0, errProtoInvalidFieldNumber
51 }
52 num = int(tagint >> 3)
53 if num < 1 {
54 return 0, 0, 0, errProtoInvalidFieldNumber
55 }
56 typ = int(tagint & 7)
57
58 return num, typ, size, nil
59 }
60
61 func protoScanFieldValue(typ int, b []byte) (size int, err error) {
62 switch typ {
63 case 0:
64 _, size, err = protoScanVarint(b)
65 case 5: // fixed32
66 if len(b) < 4 {
67 return 0, errProtoInsufficient
68 }
69 size = 4
70 case 1: // fixed64
71 if len(b) < 8 {
72 return 0, errProtoInsufficient
73 }
74 size = 8
75 case 2: // length-delimited
76 size, err = protoScanBytes(b)
77 default:
78 return 0, fmt.Errorf("unsupported type: %d", typ)
79 }
80 return size, err
81 }
82
83 func protoScanVarint(b []byte) (v uint64, size int, err error) {
84 var y uint64
85 if len(b) <= 0 {
86 return 0, 0, errProtoInsufficient
87 }
88 v = uint64(b[0])
89 if v < 0x80 {
90 return v, 1, nil
91 }
92 v -= 0x80
93
94 if len(b) <= 1 {
95 return 0, 0, errProtoInsufficient
96 }
97 y = uint64(b[1])
98 v += y << 7
99 if y < 0x80 {
100 return v, 2, nil
101 }
102 v -= 0x80 << 7
103
104 if len(b) <= 2 {
105 return 0, 0, errProtoInsufficient
106 }
107 y = uint64(b[2])
108 v += y << 14
109 if y < 0x80 {
110 return v, 3, nil
111 }
112 v -= 0x80 << 14
113
114 if len(b) <= 3 {
115 return 0, 0, errProtoInsufficient
116 }
117 y = uint64(b[3])
118 v += y << 21
119 if y < 0x80 {
120 return v, 4, nil
121 }
122 v -= 0x80 << 21
123
124 if len(b) <= 4 {
125 return 0, 0, errProtoInsufficient
126 }
127 y = uint64(b[4])
128 v += y << 28
129 if y < 0x80 {
130 return v, 5, nil
131 }
132 v -= 0x80 << 28
133
134 if len(b) <= 5 {
135 return 0, 0, errProtoInsufficient
136 }
137 y = uint64(b[5])
138 v += y << 35
139 if y < 0x80 {
140 return v, 6, nil
141 }
142 v -= 0x80 << 35
143
144 if len(b) <= 6 {
145 return 0, 0, errProtoInsufficient
146 }
147 y = uint64(b[6])
148 v += y << 42
149 if y < 0x80 {
150 return v, 7, nil
151 }
152 v -= 0x80 << 42
153
154 if len(b) <= 7 {
155 return 0, 0, errProtoInsufficient
156 }
157 y = uint64(b[7])
158 v += y << 49
159 if y < 0x80 {
160 return v, 8, nil
161 }
162 v -= 0x80 << 49
163
164 if len(b) <= 8 {
165 return 0, 0, errProtoInsufficient
166 }
167 y = uint64(b[8])
168 v += y << 56
169 if y < 0x80 {
170 return v, 9, nil
171 }
172 v -= 0x80 << 56
173
174 if len(b) <= 9 {
175 return 0, 0, errProtoInsufficient
176 }
177 y = uint64(b[9])
178 v += y << 63
179 if y < 2 {
180 return v, 10, nil
181 }
182 return 0, 0, errProtoOverflow
183 }
184
185 func protoScanBytes(b []byte) (size int, err error) {
186 l, lenSize, err := protoScanVarint(b)
187 if err != nil {
188 return 0, err
189 }
190 if l > uint64(len(b[lenSize:])) {
191 return 0, errProtoInsufficient
192 }
193 return lenSize + int(l), nil
194 }
195
196 func protoEncodeVarint(v uint64) []byte {
197 b := make([]byte, 0, 10)
198 switch {
199 case v < 1<<7:
200 b = append(b, byte(v))
201 case v < 1<<14:
202 b = append(b,
203 byte((v>>0)&0x7f|0x80),
204 byte(v>>7))
205 case v < 1<<21:
206 b = append(b,
207 byte((v>>0)&0x7f|0x80),
208 byte((v>>7)&0x7f|0x80),
209 byte(v>>14))
210 case v < 1<<28:
211 b = append(b,
212 byte((v>>0)&0x7f|0x80),
213 byte((v>>7)&0x7f|0x80),
214 byte((v>>14)&0x7f|0x80),
215 byte(v>>21))
216 case v < 1<<35:
217 b = append(b,
218 byte((v>>0)&0x7f|0x80),
219 byte((v>>7)&0x7f|0x80),
220 byte((v>>14)&0x7f|0x80),
221 byte((v>>21)&0x7f|0x80),
222 byte(v>>28))
223 case v < 1<<42:
224 b = append(b,
225 byte((v>>0)&0x7f|0x80),
226 byte((v>>7)&0x7f|0x80),
227 byte((v>>14)&0x7f|0x80),
228 byte((v>>21)&0x7f|0x80),
229 byte((v>>28)&0x7f|0x80),
230 byte(v>>35))
231 case v < 1<<49:
232 b = append(b,
233 byte((v>>0)&0x7f|0x80),
234 byte((v>>7)&0x7f|0x80),
235 byte((v>>14)&0x7f|0x80),
236 byte((v>>21)&0x7f|0x80),
237 byte((v>>28)&0x7f|0x80),
238 byte((v>>35)&0x7f|0x80),
239 byte(v>>42))
240 case v < 1<<56:
241 b = append(b,
242 byte((v>>0)&0x7f|0x80),
243 byte((v>>7)&0x7f|0x80),
244 byte((v>>14)&0x7f|0x80),
245 byte((v>>21)&0x7f|0x80),
246 byte((v>>28)&0x7f|0x80),
247 byte((v>>35)&0x7f|0x80),
248 byte((v>>42)&0x7f|0x80),
249 byte(v>>49))
250 case v < 1<<63:
251 b = append(b,
252 byte((v>>0)&0x7f|0x80),
253 byte((v>>7)&0x7f|0x80),
254 byte((v>>14)&0x7f|0x80),
255 byte((v>>21)&0x7f|0x80),
256 byte((v>>28)&0x7f|0x80),
257 byte((v>>35)&0x7f|0x80),
258 byte((v>>42)&0x7f|0x80),
259 byte((v>>49)&0x7f|0x80),
260 byte(v>>56))
261 default:
262 b = append(b,
263 byte((v>>0)&0x7f|0x80),
264 byte((v>>7)&0x7f|0x80),
265 byte((v>>14)&0x7f|0x80),
266 byte((v>>21)&0x7f|0x80),
267 byte((v>>28)&0x7f|0x80),
268 byte((v>>35)&0x7f|0x80),
269 byte((v>>42)&0x7f|0x80),
270 byte((v>>49)&0x7f|0x80),
271 byte((v>>56)&0x7f|0x80),
272 1)
273 }
274 return b
275 }