-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoder.go
391 lines (319 loc) · 10.1 KB
/
encoder.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright (c) 2022-2024 Xelaj Software
//
// This file is a part of tl package.
// See https://github.com/xelaj/tl/blob/master/LICENSE_README.md for details.
package tl
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
)
func Marshal(v any) ([]byte, error) {
buf := bytes.NewBuffer(nil)
encoder := NewEncoder(buf)
if err := encoder.Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// MarshalState provides set of different methods to marshal binary message into
// specific struct. It's working absolutely like fmt.State, you just need to
// write data to this abstraction.
type MarshalState interface {
// if object will write bytes the size of which is not divided by the length
// of the word, it will throw specific error
io.Writer
PutBool(bool) error
PutInt(int32) error
PutLong(int64) error
PutCRC(crc32) error
PutMessage([]byte) error
}
type RealEncoder interface {
Encode(v any) error
}
// encoder is a type, which allows you to decode serialized message.
type encoder struct {
w io.Writer
endianess binary.ByteOrder
}
func NewEncoder(w io.Writer) RealEncoder {
return &encoder{w: w, endianess: binary.LittleEndian}
}
func (e *encoder) Encode(value any) error {
if value == nil {
return ErrUnexpectedNil
}
v := reflect.ValueOf(value)
if v.Kind() != reflect.Ptr {
return fmt.Errorf("value is not pointer as expected. got %v", v.Type())
}
return e.encodeValue(v)
}
// writeErr works like write (also throwing panic), but without count of
// written bytes.
func (e *encoder) writeErr(b []byte) error {
if n, err := e.write(b); err != nil {
return err
} else if n != len(b) {
return &ErrPartialWrite{Has: n, Want: len(b)}
}
return nil
}
func (e *encoder) Write(b []byte) (int, error) {
if len(b)%WordLen != 0 {
return 0, errors.New("value can't be divided by word length")
}
return e.write(b)
}
// write is private, cause this function might panic.
func (e *encoder) write(b []byte) (int, error) {
if len(b)%WordLen != 0 { //revive:disable-line:add-constant // makes no sense
// it's panic, because it's internal method, and we must not write in
// any case data, which is not divided by word length
panic("raw bytes does not divide by word size of protocol")
}
if len(b) == 0 {
return 0, nil
}
return e.w.Write(b) //nolint:wrapcheck // write() is a wrapper
}
//nolint:cyclop // it contains only assertion and switch statement
//revive:disable:function-length // same: can't make better
func (e *encoder) encodeValue(value reflect.Value) error {
if maybeNil(value) {
return ErrUnexpectedNil
}
if marshaler, ok := value.Interface().(Marshaler); ok {
//nolint:wrapcheck // object implements Marshaler must throw unwrapped
// error
return marshaler.MarshalTL(e)
}
switch k := value.Type().Kind(); k { //nolint:exhaustive // has default case
case reflect.Uint32:
return e.putUint(uint32(value.Uint()))
case reflect.Int32:
return e.putUint(uint32(value.Int()))
case reflect.Uint64:
return e.putULong(value.Uint())
case reflect.Int64:
return e.PutLong(value.Int())
case reflect.Float64:
return e.putDouble(value.Float())
case reflect.Bool:
return e.PutBool(value.Bool())
case reflect.String:
return e.putString(value.String())
case reflect.Struct:
return e.encodeStruct(value, false)
case reflect.Ptr, reflect.Interface:
return e.encodeValue(value.Elem())
// case reflect.Map:
// return e.encodeMap(value)
case reflect.Slice:
return e.encodeVector(value)
case reflect.Array:
if value.Type().Elem() == byteTyp { // [N]byte
return e.encodeRaw(value)
}
return e.encodeVector(value)
default:
return ErrUnsupportedType{Type: value.Type()}
}
}
//revive:enable
// v must be pointer to struct.
func (e *encoder) encodeStruct(v reflect.Value, ignoreCRC bool) error {
o, ok := v.Interface().(Object)
if !ok {
// Trying to look implementation by pointer
//
// Since just struct might be non addressable, we are creating new
// instance, and setting it to provided value.
vcopy := reflect.New(v.Type()).Elem()
vcopy.Set(v)
var ok bool
if o, ok = vcopy.Addr().Interface().(Object); !ok {
return errors.New(v.Type().String() + " doesn't implement tl.Object interface")
}
}
typ := v.Type()
properties, bitflags, err := parseStructTags(typ)
if err != nil {
return fmt.Errorf("parsing struct flags: %w", err)
}
optFlags := make(map[int]crc32)
for i, target := range bitflags {
// even if we don't have any non null optional values, we still need to
// initialize bitflags
if _, ok := optFlags[target.FieldIndex]; !ok {
optFlags[target.FieldIndex] = 0
}
if isFieldContainsData(v.Field(i)) {
optFlags[target.FieldIndex] |= 1 << target.BitIndex
}
}
// what we checked and what we know about value:
// 1) it's not Marshaler (marshaler method if exist used already in c.encodeValue())
// 2) implements tl.Object
// 3) it's addressable
// 4) definitely struct (we don't call encodeStruct() but in c.encodeValue())
// 5) not nil (structs can't be nil, only pointers and interfaces)
if !ignoreCRC {
if err := e.PutCRC(o.CRC()); err != nil {
return err
}
}
for i := 0; i < v.NumField(); i++ {
// putting bitflags, if this field is bitflag
if flags, ok := optFlags[i]; ok {
if err := e.putUint(flags); err != nil {
return err
}
continue
}
tag := properties[i]
_, fieldOptional := bitflags[i]
// if ignore or field is unexported, then go on
if tag.Ignore() ||
!v.Field(i).CanSet() ||
(fieldOptional && (!isFieldContainsData(v.Field(i)) || tag.isImplicit())) {
continue
}
err := e.encodeValue(v.Field(i))
if err != nil {
return fmt.Errorf("encoding %v: %w", v.Type().Field(i).Name, err)
}
}
return nil
}
/*
func (e *encoder) encodeMap(m reflect.Value) error {
if m.Type().Key().Kind() != reflect.String {
return errors.New("map keys are not string")
}
crc, err := getCRCFromMap(m)
if err != nil {
return err
}
definition, ok := e.registry.Tags(crc)
if !ok {
//nolint:goerr113 // it's an internal error
return fmt.Errorf("crc code 0x%08x is not found in registry", crc)
}
// TODO: need to cache encoded non empty objects in slice, then write
// everything after we will be sure that cached bitflag will not be
// changed (means that we checked already most right optional field
// for this bitflag)
bitflags, err := definition.collectBitflags(m)
if err != nil {
return err
}
if err := e.putCRC(crc); err != nil {
return err
}
for i, field := range definition.fields {
// putting bitflags, if this field is bitflag
if flags, ok := bitflags[uint8(i)]; ok {
if err := e.putUint(flags); err != nil {
return err
}
continue
}
val := m.MapIndex(reflect.ValueOf(field.name))
if !val.IsValid() || field.noEncode {
continue
}
if err := e.encodeValue(val); err != nil {
return errors.Wrapf(err, "encoding %q", field.name)
}
}
return nil
}
*/
func (e *encoder) encodeRaw(v reflect.Value) error {
if v.Kind() != reflect.Array {
panic("raw must be array")
} else if v.Type().Elem() != byteTyp {
panic("raw must be array of bytes")
} else if n := v.Len(); n%WordLen != 0 {
// special case: this means that we want to take exact N of bytes and pop it from reader
// n%WordLen == 0, cause we can't read less or more than word
return fmt.Errorf("array of bytes must be divided by %v, got %v", WordLen, n)
}
_, err := e.Write(v.Slice(0, v.Len()).Interface().([]byte))
return err
}
func (e *encoder) encodeVector(slice reflect.Value) error {
if b, ok := slice.Interface().([]byte); ok {
return e.PutMessage(b)
}
if err := e.PutCRC(crcVector); err != nil {
return err
}
if err := e.putUint(uint32(slice.Len())); err != nil {
return err
}
for i := 0; i < slice.Len(); i++ {
item := slice.Index(i)
err := e.encodeValue(item)
if err != nil {
return fmt.Errorf("[%v]: %w", i, err)
}
}
return nil
}
func (e *encoder) putUint(v uint32) error { return e.writeErr(u32b(e.endianess, v)) }
func (e *encoder) putULong(v uint64) error { return e.writeErr(u64b(e.endianess, v)) }
func (e *encoder) PutLong(v int64) error { return e.writeErr(u64b(e.endianess, uint64(v))) }
func (e *encoder) putDouble(v float64) error { return e.writeErr(f64b(e.endianess, v)) }
func (e *encoder) PutCRC(v uint32) error { return e.putUint(v) } // for selfdoc code
func (e *encoder) PutInt(v int32) error { return e.putUint(uint32(v)) }
func (e *encoder) PutBool(v bool) error { return e.putUint(boolToCRC(v)) }
func (e *encoder) putString(v string) error { return e.PutMessage([]byte(v)) }
func (e *encoder) PutMessage(msg []byte) error {
// 3 left bytes of word, which is 4 bytes
const maxLen = 1 << ((WordLen - 1) * bitsInByte)
if len(msg) > maxLen {
//nolint:goerr113 // it's an internal error
return fmt.Errorf("message entity too large: expect less than %v, got %v", maxLen, len(msg))
}
var lenBytes []byte
// how does it works:
// any object can be putted to byte set ONLY with length, without modula
// after dividing to word length. e.g. bytes 'Hi!' can be written as:
// | 0x03 0x48 0x6A 0x21 |
// Divides by 32 bits? Yes, so it's good.
//
// BUT! bytes 'Hello!' MUST be written as
// | 0x06 0x48 0x65 0x6C | 0x6C 0x6F 0x21 0x00 |
// See? We added extra empty byte to pad message to length of word. That is
// most important part of putting bytes to buffer.
//
// So we must to create a buffer with length mod to 32 == 0. To not add
// extra bytes manually. They could be random, but who needs that, right?
if len(msg) < fuckingMagicNumber {
lenBytes = []byte{byte(len(msg))}
} else {
lenBytes = append([]byte{fuckingMagicNumber}, littleUint24Bytes(len(msg))...)
}
return e.writeErr(appendMany(
lenBytes,
msg,
make([]byte, pad(len(lenBytes)+len(msg), WordLen)),
))
}
// m only map.
func getCRCFromMap(m reflect.Value) (uint32, error) {
crcVal := m.MapIndex(reflect.ValueOf(MapCrcKey))
if !crcVal.IsValid() {
return 0, errors.New("key " + MapCrcKey + " not exist in map")
}
if !crcVal.Type().ConvertibleTo(uint32Typ) {
return 0, errors.New(MapCrcKey + " is not convertible to uint32")
}
return uint32(crcVal.Convert(uint32Typ).Uint()), nil
}