-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathencode.go
220 lines (192 loc) · 4.76 KB
/
encode.go
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
package tlv
import (
"math"
"slices"
"golang.org/x/exp/constraints"
)
// EncodingBuffer is an encoding buffer.
// Zero value is an empty buffer.
type EncodingBuffer struct {
b []byte
err error
}
// Append appends a field.
// If there's an error, it is saved in the EncodingBuffer.
func (eb *EncodingBuffer) Append(f Field) {
if eb.err != nil {
return
}
eb.b, eb.err = f.Encode(eb.b)
}
// Output returns encoding output and first error.
func (eb EncodingBuffer) Output() ([]byte, error) {
return eb.b, eb.err
}
type fieldType uint8
const (
fieldTypeEmpty fieldType = iota
fieldTypeError
fieldTypeFunc
fieldTypeBytes
fieldTypeNNI
fieldTypeTLVFields
fieldTypeTLVFielders
)
// Field is an encodable field.
// Zero value encodes to nothing.
type Field struct {
typ fieldType
integer uint64
object any
}
// Encode appends to the byte slice.
// Returns modified slice and error.
func (f Field) Encode(b []byte) ([]byte, error) {
switch f.typ {
case fieldTypeEmpty:
return b, nil
case fieldTypeError:
return nil, f.object.(error)
case fieldTypeFunc:
return f.object.(func([]byte) ([]byte, error))(b)
case fieldTypeBytes:
return append(b, f.object.([]byte)...), nil
case fieldTypeNNI:
return NNI(f.integer).Encode(b), nil
case fieldTypeTLVFields:
return f.encodeTLVFields(b)
case fieldTypeTLVFielders:
return f.encodeTLVFielders(b)
default:
panic(f.typ)
}
}
func (f Field) encodeTLVFields(b []byte) (o []byte, e error) {
subs := f.object.([]Field)
parts := make([][]byte, len(subs))
for i, sub := range subs {
if parts[i], e = sub.Encode(nil); e != nil {
return nil, e
}
}
return f.encodeTLVFinish(b, parts)
}
func (f Field) encodeTLVFielders(b []byte) (o []byte, e error) {
subs := f.object.([]Fielder)
parts := make([][]byte, len(subs))
for i, sub := range subs {
if parts[i], e = sub.Field().Encode(nil); e != nil {
return nil, e
}
}
return f.encodeTLVFinish(b, parts)
}
func (f Field) encodeTLVFinish(b []byte, value [][]byte) ([]byte, error) {
length := 0
for _, part := range value {
length += len(part)
}
b = slices.Grow(b, 5+9+length)
if f.integer < math.MaxUint64 { // not EncodeValueOnly
b = VarNum(f.integer).Encode(b)
b = VarNum(length).Encode(b)
}
for _, part := range value {
b = append(b, part...)
}
return b, nil
}
// Field implements Fielder interface.
func (f Field) Field() Field {
return f
}
// Fielder is the interface implemented by an object that can encode itself to a Field.
type Fielder interface {
Field() Field
}
// FieldError creates a Field that generates an error.
func FieldError(e error) Field {
if e == nil {
e = ErrErrorField
}
return Field{
typ: fieldTypeError,
object: e,
}
}
// FieldFunc creates a Field that calls a function to append to a slice.
func FieldFunc(f func([]byte) ([]byte, error)) Field {
return Field{
typ: fieldTypeFunc,
object: f,
}
}
// Bytes creates a Field that encodes to given bytes.
func Bytes(b []byte) Field {
return Field{
typ: fieldTypeBytes,
object: b,
}
}
// TLV creates a Field that encodes to TLV element from TLV-TYPE and TLV-VALUE Fields.
func TLV(typ uint32, values ...Field) Field {
if typ < minType || typ > maxType {
return FieldError(ErrType)
}
return Field{
typ: fieldTypeTLVFields,
integer: uint64(typ),
object: values,
}
}
// TLVFrom creates a Field that encodes to TLV element from TLV-TYPE and TLV-VALUE Fielders.
func TLVFrom(typ uint32, values ...Fielder) Field {
if typ < minType || typ > maxType {
return FieldError(ErrType)
}
return Field{
typ: fieldTypeTLVFielders,
integer: uint64(typ),
object: values,
}
}
// TLVBytes creates a Field that encodes to TLV element from TLV-TYPE and TLV-VALUE byte slice.
func TLVBytes(typ uint32, value []byte) Field {
return TLV(typ, Bytes(value))
}
// TLVNNI creates a Field that encodes to TLV element from TLV-TYPE and TLV-VALUE NonNegativeInteger.
func TLVNNI[V constraints.Integer](typ uint32, v V) Field {
if v < 0 {
return FieldError(ErrRange)
}
return TLVFrom(typ, NNI(v))
}
// Encode encodes a sequence of Fields.
func Encode(fields ...Field) (wire []byte, e error) {
var eb EncodingBuffer
for _, f := range fields {
eb.Append(f)
}
return eb.Output()
}
// EncodeFrom encodes a sequence of Fielders.
func EncodeFrom(fields ...Fielder) (wire []byte, e error) {
var eb EncodingBuffer
for _, f := range fields {
eb.Append(f.Field())
}
return eb.Output()
}
// EncodeValueOnly returns TLV-VALUE of a Fielder created by TLV, TLVFrom, TLVBytes, or TLVNNI.
func EncodeValueOnly(f Fielder) ([]byte, error) {
field := f.Field()
switch field.typ {
case fieldTypeError:
return nil, field.object.(error)
case fieldTypeTLVFields, fieldTypeTLVFielders:
field.integer = math.MaxUint64
return field.Encode(nil)
default:
return nil, ErrErrorField
}
}