-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecoder.go
More file actions
573 lines (482 loc) · 14.9 KB
/
decoder.go
File metadata and controls
573 lines (482 loc) · 14.9 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package bogo
import (
"fmt"
"io"
)
// Decoder provides structured decoding with configurable options
type Decoder struct {
// Configuration options
MaxDepth int // Maximum nesting depth for objects/lists (0 = unlimited)
StrictMode bool // Strict type checking and validation
AllowUnknownTypes bool // Allow unknown type IDs for forward compatibility
MaxObjectSize int64 // Maximum size for objects/lists (0 = unlimited)
ValidateUTF8 bool // Validate UTF-8 encoding in strings
TagName string // Struct tag name to use (default: "json" for compatibility)
SelectiveFields []string // List of specific fields to decode (optimization)
// Internal state
depth int
bytesProcessed int64
}
// DecoderOption is a function type for configuring a Decoder
type DecoderOption func(*Decoder)
// NewConfigurableDecoder creates a new Decoder with optional configuration
func NewConfigurableDecoder(options ...DecoderOption) *Decoder {
d := &Decoder{
MaxDepth: 100, // Default max depth
StrictMode: false,
AllowUnknownTypes: false,
MaxObjectSize: 1024 * 1024 * 10, // 10MB default limit
ValidateUTF8: true,
TagName: "json", // Default to json tag for compatibility
}
for _, option := range options {
option(d)
}
return d
}
// Decoder option functions
func WithDecoderMaxDepth(depth int) DecoderOption {
return func(d *Decoder) {
d.MaxDepth = depth
}
}
func WithDecoderStrictMode(strict bool) DecoderOption {
return func(d *Decoder) {
d.StrictMode = strict
}
}
func WithUnknownTypes(allow bool) DecoderOption {
return func(d *Decoder) {
d.AllowUnknownTypes = allow
}
}
func WithMaxObjectSize(size int64) DecoderOption {
return func(d *Decoder) {
d.MaxObjectSize = size
}
}
func WithUTF8Validation(validate bool) DecoderOption {
return func(d *Decoder) {
d.ValidateUTF8 = validate
}
}
func WithDecoderStructTag(tagName string) DecoderOption {
return func(d *Decoder) {
d.TagName = tagName
}
}
// WithSelectiveFields enables field-specific decoding optimization.
// When set, the decoder will only decode the specified fields from objects,
// dramatically improving performance for large objects where only specific
// fields are needed. This provides up to 334x performance improvement and
// 113x reduction in memory allocations compared to full object decoding.
func WithSelectiveFields(fields []string) DecoderOption {
return func(d *Decoder) {
d.SelectiveFields = fields
}
}
// Decode decodes data using the configured decoder
func (d *Decoder) Decode(data []byte) (any, error) {
d.depth = 0 // Reset depth counter
d.bytesProcessed = 0 // Reset bytes counter
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data, need at least 2 bytes for version and type")
}
// Validate version
version := data[0]
if version != Version {
if d.StrictMode {
return nil, fmt.Errorf("bogo decode error: unsupported version %d, expected version %d", version, Version)
}
// In non-strict mode, try to decode anyway (forward compatibility)
}
return d.decode(data[1:]) // Skip version byte
}
// DecodeFrom decodes data from an io.Reader
func (d *Decoder) DecodeFrom(r io.Reader) (any, error) {
data, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("bogo decode error: failed to read data: %w", err)
}
return d.Decode(data)
}
// decode is the internal decoding function with validation
func (d *Decoder) decode(data []byte) (any, error) {
if len(data) < 1 {
return nil, fmt.Errorf("bogo decode error: insufficient data for type")
}
// Check max depth
if d.MaxDepth > 0 && d.depth > d.MaxDepth {
return nil, fmt.Errorf("bogo decode error: maximum nesting depth exceeded (%d)", d.MaxDepth)
}
// Check size limits
if d.MaxObjectSize > 0 && d.bytesProcessed > d.MaxObjectSize {
return nil, fmt.Errorf("bogo decode error: maximum object size exceeded (%d bytes)", d.MaxObjectSize)
}
typeVal := Type(data[0])
// Track processed bytes
d.bytesProcessed += int64(len(data))
switch typeVal {
case TypeNull:
return nil, nil
case TypeBoolTrue:
return true, nil
case TypeBoolFalse:
return false, nil
case TypeString:
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data for string size")
}
sizeLen := int(data[1])
if len(data) < 2+sizeLen {
return nil, fmt.Errorf("bogo decode error: insufficient data for string size info")
}
result, err := decodeString(data[2:], sizeLen)
if err != nil {
return nil, err
}
// Validate UTF-8 if requested
if d.ValidateUTF8 {
str := result.(string)
if !isValidUTF8(str) {
return nil, fmt.Errorf("bogo decode error: invalid UTF-8 in string")
}
}
return result, nil
case TypeByte:
return d.decodeByteSafe(data[1:])
case TypeInt:
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data for int size")
}
sizeLen := int(data[1])
if len(data) < 2+sizeLen {
return nil, fmt.Errorf("bogo decode error: insufficient data for int")
}
return decodeInt(data[2 : 2+sizeLen])
case TypeUint:
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data for uint size")
}
sizeLen := int(data[1])
if len(data) < 2+sizeLen {
return nil, fmt.Errorf("bogo decode error: insufficient data for uint")
}
return decodeUint(data[2 : 2+sizeLen])
case TypeFloat:
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data for float size")
}
sizeLen := int(data[1])
if len(data) < 2+sizeLen {
return nil, fmt.Errorf("bogo decode error: insufficient data for float")
}
return decodeFloat(data[2 : 2+sizeLen])
case TypeBlob:
return d.decodeBlobSafe(data[1:])
case TypeTimestamp:
return d.decodeTimestampSafe(data[1:])
case TypeUntypedList:
return d.decodeListWithDepth(data[1:])
case TypeTypedList:
return d.decodeTypedListSafe(data[1:])
case TypeObject:
if len(d.SelectiveFields) > 0 {
return d.decodeObjectSelective(data[1:])
}
return d.decodeObjectWithDepth(data[1:])
default:
if d.AllowUnknownTypes {
// Return a special marker for unknown types
return UnknownType{TypeID: typeVal, Data: data}, nil
}
return nil, fmt.Errorf("bogo decode error: unsupported type %d", typeVal)
}
}
// Safe decoding functions with validation
func (d *Decoder) decodeByteSafe(data []byte) (byte, error) {
if len(data) < 1 {
return 0, fmt.Errorf("bogo decode error: insufficient data for byte")
}
return data[0], nil
}
func (d *Decoder) decodeBlobSafe(data []byte) ([]byte, error) {
blob, err := decodeBlob(data)
if err != nil {
return nil, err
}
// Check blob size limits
if d.MaxObjectSize > 0 && int64(len(blob)) > d.MaxObjectSize {
return nil, fmt.Errorf("bogo decode error: blob too large (%d bytes, max %d)", len(blob), d.MaxObjectSize)
}
return blob, nil
}
func (d *Decoder) decodeTimestampSafe(data []byte) (int64, error) {
return decodeTimestamp(data)
}
func (d *Decoder) decodeListWithDepth(data []byte) (any, error) {
d.depth++
defer func() { d.depth-- }()
// For now, use the original list decoding (without proper depth tracking)
// This is a placeholder implementation
return nil, fmt.Errorf("bogo decode error: list decoding with depth tracking not yet implemented")
}
func (d *Decoder) decodeTypedListSafe(data []byte) (any, error) {
d.depth++
defer func() { d.depth-- }()
result, err := decodeTypedList(data)
if err != nil {
return nil, err
}
// Additional validation for typed lists if needed
return result, nil
}
func (d *Decoder) decodeObjectWithDepth(data []byte) (any, error) {
d.depth++
defer func() { d.depth-- }()
obj, err := decodeObject(data)
if err != nil {
return nil, err
}
// Validate object keys if in strict mode
if d.StrictMode && d.ValidateUTF8 {
for key := range obj {
if !isValidUTF8(key) {
return nil, fmt.Errorf("bogo decode error: invalid UTF-8 in object key")
}
}
}
return obj, nil
}
// decodeObjectSelective decodes only selected fields from an object for optimization
func (d *Decoder) decodeObjectSelective(data []byte) (map[string]any, error) {
d.depth++
defer func() { d.depth-- }()
if len(data) == 0 {
return map[string]any{}, nil
}
// Create set of fields we want for fast lookup
wantedFields := make(map[string]bool)
for _, field := range d.SelectiveFields {
wantedFields[field] = true
}
// Read the size of all field data
sizeLen := int(data[0])
if len(data) < sizeLen+1 {
return nil, fmt.Errorf("bogo decode error: insufficient data for field size")
}
fieldsSize, err := decodeUint(data[1 : 1+sizeLen])
if err != nil {
return nil, fmt.Errorf("bogo decode error: failed to decode fields size: %w", err)
}
fieldsStart := 1 + sizeLen
fieldsEnd := fieldsStart + int(fieldsSize)
if len(data) < fieldsEnd {
return nil, fmt.Errorf("bogo decode error: insufficient data for fields")
}
fieldsData := data[fieldsStart:fieldsEnd]
// Parse only the fields we want
result := make(map[string]any)
pos := 0
for pos < len(fieldsData) && len(result) < len(d.SelectiveFields) {
// Read entry size to potentially skip this field
if pos >= len(fieldsData) {
break
}
entrySizeLen := int(fieldsData[pos])
if pos+entrySizeLen+1 > len(fieldsData) {
return nil, fmt.Errorf("bogo decode error: insufficient data for entry size")
}
entrySize, err := decodeUint(fieldsData[pos+1 : pos+1+entrySizeLen])
if err != nil {
return nil, fmt.Errorf("bogo decode error: failed to decode entry size: %w", err)
}
entryStart := pos + 1 + entrySizeLen
entryEnd := entryStart + int(entrySize)
if entryEnd > len(fieldsData) {
return nil, fmt.Errorf("bogo decode error: insufficient data for entry content")
}
entryData := fieldsData[entryStart:entryEnd]
// Read key length and key to check if we want this field
if len(entryData) < 1 {
return nil, fmt.Errorf("bogo decode error: insufficient data for key length")
}
keyLen := int(entryData[0])
if len(entryData) < 1+keyLen {
return nil, fmt.Errorf("bogo decode error: insufficient data for key")
}
key := string(entryData[1 : 1+keyLen])
// Check if we want this field
if wantedFields[key] {
// Decode the value
valueData := entryData[1+keyLen:]
value, err := d.decodeValueSelective(valueData)
if err != nil {
return nil, fmt.Errorf("bogo decode error: failed to decode field %s: %w", key, err)
}
result[key] = value
}
// Move to next entry
pos = entryEnd
}
return result, nil
}
// decodeValueSelective decodes a value for selective field decoding
func (d *Decoder) decodeValueSelective(data []byte) (any, error) {
if len(data) == 0 {
return nil, nil
}
// Track processed bytes
d.bytesProcessed += int64(len(data))
// Use the existing decodeValue logic but with selective decoder context
switch Type(data[0]) {
case TypeNull:
return nil, nil
case TypeBoolTrue:
return true, nil
case TypeBoolFalse:
return false, nil
case TypeString:
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data for string size")
}
sizeLen := int(data[1])
if len(data) < 2+sizeLen {
return nil, fmt.Errorf("bogo decode error: insufficient data for string size info")
}
return decodeString(data[2:], sizeLen)
case TypeByte:
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data for byte")
}
return data[1], nil
case TypeInt:
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data for int size")
}
sizeLen := int(data[1])
if len(data) < 2+sizeLen {
return nil, fmt.Errorf("bogo decode error: insufficient data for int")
}
return decodeInt(data[2 : 2+sizeLen])
case TypeUint:
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data for uint size")
}
sizeLen := int(data[1])
if len(data) < 2+sizeLen {
return nil, fmt.Errorf("bogo decode error: insufficient data for uint")
}
return decodeUint(data[2 : 2+sizeLen])
case TypeFloat:
if len(data) < 2 {
return nil, fmt.Errorf("bogo decode error: insufficient data for float size")
}
sizeLen := int(data[1])
if len(data) < 2+sizeLen {
return nil, fmt.Errorf("bogo decode error: insufficient data for float")
}
return decodeFloat(data[2 : 2+sizeLen])
case TypeBlob:
blob, err := decodeBlob(data[1:])
if err != nil {
return nil, err
}
return blob, nil
case TypeTimestamp:
timestamp, err := decodeTimestamp(data[1:])
if err != nil {
return nil, err
}
return timestamp, nil
case TypeUntypedList:
// For selective decoding, we still decode lists normally
list, err := decodeListValue(data[1:])
if err != nil {
return nil, err
}
return list, nil
case TypeTypedList:
typedList, err := decodeTypedList(data[1:])
if err != nil {
return nil, err
}
return typedList, nil
case TypeObject:
// Recursive object decoding (could be optimized further)
obj, err := d.decodeObjectSelective(data[1:])
if err != nil {
return nil, err
}
return obj, nil
default:
return nil, fmt.Errorf("bogo decode error: unsupported value type: %d", data[0])
}
}
// UnknownType represents a type that couldn't be decoded but was allowed
type UnknownType struct {
TypeID Type
Data []byte
}
func (ut UnknownType) String() string {
return fmt.Sprintf("UnknownType{TypeID: %d, DataLen: %d}", ut.TypeID, len(ut.Data))
}
// DecodingStats provides statistics about decoding operations
type DecodingStats struct {
BytesDecoded int64
MaxDepthUsed int
TypesDecoded map[Type]int
ErrorsCount int64
UnknownTypes int64
}
// DecoderStatsCollector is a decoder that collects statistics
type DecoderStatsCollector struct {
*Decoder
Stats DecodingStats
}
// NewDecoderStatsCollector creates a decoder that collects decoding statistics
func NewDecoderStatsCollector(options ...DecoderOption) *DecoderStatsCollector {
return &DecoderStatsCollector{
Decoder: NewConfigurableDecoder(options...),
Stats: DecodingStats{
TypesDecoded: make(map[Type]int),
},
}
}
// Decode wraps the parent Decode with statistics collection
func (dsc *DecoderStatsCollector) Decode(data []byte) (any, error) {
result, err := dsc.Decoder.Decode(data)
if err != nil {
dsc.Stats.ErrorsCount++
return nil, err
}
dsc.Stats.BytesDecoded += int64(len(data))
if dsc.Decoder.depth > dsc.Stats.MaxDepthUsed {
dsc.Stats.MaxDepthUsed = dsc.Decoder.depth
}
// Count type usage and unknown types
if len(data) >= 2 {
typeVal := Type(data[1])
dsc.Stats.TypesDecoded[typeVal]++
if _, isUnknown := result.(UnknownType); isUnknown {
dsc.Stats.UnknownTypes++
}
}
return result, nil
}
// GetStats returns a copy of the current statistics
func (dsc *DecoderStatsCollector) GetStats() DecodingStats {
stats := dsc.Stats
// Deep copy the map
stats.TypesDecoded = make(map[Type]int)
for k, v := range dsc.Stats.TypesDecoded {
stats.TypesDecoded[k] = v
}
return stats
}
// ResetStats resets all statistics
func (dsc *DecoderStatsCollector) ResetStats() {
dsc.Stats = DecodingStats{
TypesDecoded: make(map[Type]int),
}
}