Repository navigation
Expand file tree
/
Copy pathencoder.go
More file actions
326 lines (290 loc) · 9.22 KB
/
Copy pathencoder.go
File metadata and controls
326 lines (290 loc) · 9.22 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package picobuf
import (
"errors"
"storj.io/picobuf/internal/protowire"
)
// Encoder implements encoding of protobuf format.
type Encoder struct {
buffer []byte
size int
depth int
sizing bool
err error
skipUTF8Validation bool
skipUTF8Depth int
}
var errEncodeRecursionDepth = errors.New("picobuf: exceeded maximum nesting depth while encoding")
// WithoutUTF8Validation runs fn without validating encoded string fields.
// It is intended for generated code implementing protobuf schema features.
//
// The exemption covers strings encoded directly within the current message,
// including map entry keys and values, but not strings inside nested
// messages, which carry their own UTF-8 validation features. fn cannot
// panic, so the state is restored with a plain call rather than a defer.
func (enc *Encoder) WithoutUTF8Validation(fn func()) {
prevSkip, prevDepth := enc.skipUTF8Validation, enc.skipUTF8Depth
enc.skipUTF8Validation, enc.skipUTF8Depth = true, enc.depth
fn()
enc.skipUTF8Validation, enc.skipUTF8Depth = prevSkip, prevDepth
}
// skipUTF8 reports whether string validation is currently disabled, per the
// scoping rules of WithoutUTF8Validation.
func (enc *Encoder) skipUTF8() bool {
return enc.skipUTF8Validation && enc.depth <= enc.skipUTF8Depth+1
}
// Err returns the error that occurred during encoding.
func (enc *Encoder) Err() error { return enc.err }
// IsSizing reports whether the encoder is counting bytes for Size rather than
// producing an encoded buffer. Custom encoders may use this to avoid work that
// only contributes to the encoded contents.
func (enc *Encoder) IsSizing() bool { return enc.sizing }
func (enc *Encoder) offset() int {
if enc.sizing {
return enc.size
}
return len(enc.buffer)
}
func (enc *Encoder) truncate(offset int) {
if enc.sizing {
enc.size = offset
} else {
enc.buffer = enc.buffer[:offset]
}
}
func (enc *Encoder) appendTag(field FieldNumber, typ protowire.Type) {
if enc.sizing {
enc.size += protowire.SizeTag(protowire.Number(field))
} else {
enc.buffer = appendTag(enc.buffer, field, typ)
}
}
func (enc *Encoder) appendVarint(v uint64) {
if enc.sizing {
enc.size += protowire.SizeVarint(v)
} else {
enc.buffer = protowire.AppendVarint(enc.buffer, v)
}
}
// Fail fails the encoding process.
func (enc *Encoder) Fail(field FieldNumber, msg string) {
enc.fail(field, msg)
}
//go:noinline
func (enc *Encoder) fail(field FieldNumber, msg string) {
if enc.err == nil {
enc.err = &encodeError{field: field, message: msg}
}
}
type encodeError struct {
field FieldNumber
message string
}
func (e encodeError) Error() string {
return "failed while encoding " + e.field.String() + ": " + e.message
}
// enterMessage reports whether another level of nesting may be encoded. A
// self-referential message can nest without bound, which would exhaust the
// stack, so refuse to descend past the limit the decoder also uses.
func (enc *Encoder) enterMessage() bool {
if enc.depth >= protowire.DefaultRecursionLimit {
if enc.err == nil {
enc.err = errEncodeRecursionDepth
}
return false
}
enc.depth++
return true
}
// NewEncoder creates a new Encoder.
func NewEncoder() *Encoder {
return NewEncoderBuffer(make([]byte, 0, 64))
}
// NewEncoderBuffer creates a new encoder using a preallocated buffer.
func NewEncoderBuffer(buffer []byte) *Encoder {
return &Encoder{buffer: buffer[:0]}
}
// Buffer returns the encoded internal buffer.
func (enc *Encoder) Buffer() []byte { return enc.buffer }
// Message decodes a message.
//
//go:noinline
func (enc *Encoder) Message(field FieldNumber, fn func(enc *Encoder) bool) {
enc.anyBytes(field, func() bool { return fn(enc) })
}
// AlwaysMessage encodes an message always.
//
//go:noinline
func (enc *Encoder) AlwaysMessage(field FieldNumber, fn func(enc *Encoder) bool) {
enc.alwaysAnyBytes(field, func() { fn(enc) })
}
// PresentMessage encodes an always present message.
//
//go:noinline
func (enc *Encoder) PresentMessage(field FieldNumber, fn func(enc *Encoder) bool) {
enc.anyBytes(field, func() bool {
lengthStart := enc.offset()
fn(enc)
return enc.offset() > lengthStart
})
}
// Group encodes a delimited message when it is present.
//
//go:noinline
func (enc *Encoder) Group(field FieldNumber, fn func(enc *Encoder) bool) {
enc.anyGroup(field, fn)
}
// AlwaysGroup always encodes a delimited message.
//
//go:noinline
func (enc *Encoder) AlwaysGroup(field FieldNumber, fn func(enc *Encoder) bool) {
enc.alwaysGroup(field, func() { fn(enc) })
}
// PresentGroup encodes a non-empty always-present delimited message.
//
//go:noinline
func (enc *Encoder) PresentGroup(field FieldNumber, fn func(enc *Encoder) bool) {
enc.anyGroup(field, func(enc *Encoder) bool {
start := enc.offset()
fn(enc)
return enc.offset() > start
})
}
func (enc *Encoder) anyGroup(field FieldNumber, fn func(enc *Encoder) bool) bool {
start := enc.offset()
enc.appendTag(field, protowire.StartGroupType)
if !enc.enterMessage() {
enc.truncate(start)
return false
}
ok := fn(enc)
enc.depth--
if !ok {
enc.truncate(start)
return false
}
enc.appendTag(field, protowire.EndGroupType)
return true
}
func (enc *Encoder) alwaysGroup(field FieldNumber, fn func()) {
enc.appendTag(field, protowire.StartGroupType)
if enc.enterMessage() {
fn()
enc.depth--
}
enc.appendTag(field, protowire.EndGroupType)
}
// RepeatedEnum encodes a repeated enumeration.
//
//go:noinline
func (enc *Encoder) RepeatedEnum(field FieldNumber, n int, fn func(index uint) int32) {
if n == 0 {
return
}
enc.alwaysAnyBytes(field, func() {
for i := range n {
enc.appendVarint(uint64(fn(uint(i))))
}
})
}
// anyBytes encodes field as Bytes and handles encoding the length.
func (enc *Encoder) anyBytes(field FieldNumber, fn func() bool) bool {
if enc.sizing {
start := enc.size
if !enc.enterMessage() {
return false
}
ok := fn()
enc.depth--
if !ok {
enc.size = start
return false
}
messageLength := enc.size - start
enc.size += protowire.SizeTag(protowire.Number(field)) + protowire.SizeVarint(uint64(messageLength))
return true
}
tagStart := len(enc.buffer)
enc.buffer = appendTag(enc.buffer, field, protowire.BytesType)
lengthStart := len(enc.buffer)
// We'll guess that we need 2 bytes for length.
// If we need less, then the copy is fast, and needing more is unlikely.
var lengthBufferPrediction [2]byte
enc.buffer = append(enc.buffer, lengthBufferPrediction[:]...)
messageStart := len(enc.buffer)
// encode the submessage
ok := enc.enterMessage()
if ok {
ok = fn()
enc.depth--
}
if !ok {
// The message was nil or too deeply nested, we can remove the tag.
enc.buffer = enc.buffer[:tagStart]
return false
}
messageLength := len(enc.buffer) - messageStart
bytesForSize := protowire.SizeVarint(uint64(messageLength))
if bytesForSize == len(lengthBufferPrediction) {
protowire.PutUvarint(enc.buffer[lengthStart:messageStart], uint64(messageLength))
return true
}
if bytesForSize > len(lengthBufferPrediction) {
enc.buffer = append(enc.buffer, make([]byte, bytesForSize-len(lengthBufferPrediction))...)
}
copy(enc.buffer[lengthStart+bytesForSize:], enc.buffer[messageStart:])
protowire.PutUvarint(enc.buffer[lengthStart:lengthStart+bytesForSize], uint64(messageLength))
enc.buffer = enc.buffer[:lengthStart+bytesForSize+messageLength]
return true
}
// AlwaysAnyBytes encodes field as Bytes and handles encoding the length.
func (enc *Encoder) AlwaysAnyBytes(field FieldNumber, fn func()) bool {
return enc.alwaysAnyBytes(field, fn)
}
// alwaysAnyBytes encodes field as Bytes and handles encoding the length.
func (enc *Encoder) alwaysAnyBytes(field FieldNumber, fn func()) bool {
if enc.sizing {
start := enc.size
if enc.enterMessage() {
fn()
enc.depth--
}
messageLength := enc.size - start
enc.size += protowire.SizeTag(protowire.Number(field)) + protowire.SizeVarint(uint64(messageLength))
return true
}
enc.buffer = appendTag(enc.buffer, field, protowire.BytesType)
lengthStart := len(enc.buffer)
// We'll guess that we need 2 bytes for length.
// If we need less, then the copy is fast, and needing more is unlikely.
var lengthBufferPrediction [2]byte
enc.buffer = append(enc.buffer, lengthBufferPrediction[:]...)
messageStart := len(enc.buffer)
// encode the submessage
if enc.enterMessage() {
fn()
enc.depth--
}
messageLength := len(enc.buffer) - messageStart
bytesForSize := protowire.SizeVarint(uint64(messageLength))
if bytesForSize == len(lengthBufferPrediction) {
protowire.PutUvarint(enc.buffer[lengthStart:messageStart], uint64(messageLength))
return true
}
if bytesForSize > len(lengthBufferPrediction) {
enc.buffer = append(enc.buffer, make([]byte, bytesForSize-len(lengthBufferPrediction))...)
}
copy(enc.buffer[lengthStart+bytesForSize:], enc.buffer[messageStart:])
protowire.PutUvarint(enc.buffer[lengthStart:lengthStart+bytesForSize], uint64(messageLength))
enc.buffer = enc.buffer[:lengthStart+bytesForSize+messageLength]
return true
}
// UnrecognizedFields encodes fields that are not in the provided set.
func (enc *Encoder) UnrecognizedFields(out []byte) {
if enc.sizing {
enc.size += len(out)
} else {
enc.buffer = append(enc.buffer, out...)
}
}