-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry.go
528 lines (467 loc) · 11.7 KB
/
entry.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
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
package ntgo
import (
"errors"
"github.com/imdario/mergo"
"io"
)
const (
EntryTypeBoolean EntryType = 0x00
EntryTypeDouble = 0x01
EntryTypeString = 0x02
EntryTypeRawData = 0x03
EntryTypeBooleanArr = 0x10
EntryTypeDoubleArr = 0x11
EntryTypeStringArr = 0x12
EntryTypeRPCDef = 0x20
EntryTypeUndef = 0xFF
EntryFlagTemporary EntryFlag = 0x00
EntryFlagPersistent = 0x01
EntryFlagReserved = 0xFE
EntryFlagUndef = 0xFF
)
const (
BoolFalse byte = iota
BoolTrue
)
var (
ErrEntryTypeCastInvalid = errors.New("entry: could not cast entrytype to type")
ErrEntryCastInvalid = errors.New("entry: could not cast entryvalue to type")
ErrEntryNoSuchType = errors.New("entry: no such type")
ErrEntryDataInvalid = errors.New("entry: data invalid")
ErrArrayIndexOutOfBounds = errors.New("entryarray: index out of bounds")
ErrArrayOutOfSpace = errors.New("entryarray: no more space")
ErrEntryFlagNoSuchType = errors.New("entryflag: no such flag")
)
type EntryType byte
type EntryFlag byte
type Entry struct {
Name *ValueString
Type EntryType
ID [2]byte
Sequence [2]byte
Flags EntryFlag
Value EntryValue
}
type EntryValue interface {
GetRaw() []byte
}
type EntryValueArray interface {
Get(uint8) (EntryValue, error)
Update(uint8, EntryValue) error
Add(EntryValue) error
EntryValue
}
func DecodeEntryFlag(r io.Reader) (EntryFlag, error) {
flagRaw := make([]byte, 1)
_, flagErr := r.Read(flagRaw)
if flagErr != nil {
return EntryFlagUndef, flagErr
}
flag := EntryFlag(flagRaw[0])
switch flag {
case EntryFlagPersistent, EntryFlagTemporary, EntryFlagReserved:
return flag, nil
default:
return EntryFlagUndef, ErrEntryFlagNoSuchType
}
}
func DecodeEntryType(r io.Reader) (EntryType, error) {
rawType := make([]byte, 1)
_, readErr := r.Read(rawType)
if readErr != nil {
return EntryTypeUndef, readErr
}
return EntryType(rawType[0]), nil
}
func DecodeEntryValueAndType(r io.Reader) (value EntryValue, entryType EntryType, err error) {
entryTypeRaw := make([]byte, 1)
_, readErr := r.Read(entryTypeRaw)
if readErr != nil {
return nil, EntryTypeUndef, readErr
}
entryType = EntryType(entryTypeRaw[0])
value, err = DecodeEntryValue(r, entryType)
return
}
func DecodeEntryValue(r io.Reader, entryType EntryType) (EntryValue, error) {
switch entryType {
case EntryTypeBoolean:
return DecodeBoolean(r)
case EntryTypeDouble:
return DecodeDouble(r)
case EntryTypeString:
return DecodeString(r)
case EntryTypeRawData:
return DecodeRaw(r)
case EntryTypeBooleanArr:
return DecodeBooleanArray(r)
case EntryTypeDoubleArr:
return DecodeDoubleArray(r)
case EntryTypeStringArr:
return DecodeStringArray(r)
default:
return nil, ErrEntryNoSuchType
}
}
type ValueBoolean struct {
Value bool
RawValue []byte
}
func DecodeBoolean(r io.Reader) (*ValueBoolean, error) {
val := make([]byte, 1)
_, readErr := io.ReadFull(r, val)
if readErr != nil {
return nil, readErr
}
entry := &ValueBoolean{RawValue: val}
if entry.RawValue[0] == BoolFalse {
entry.Value = false
return entry, nil
} else if entry.RawValue[0] == BoolTrue {
entry.Value = true
return entry, nil
} else {
return nil, ErrEntryDataInvalid
}
}
func BuildBoolean(value bool) *ValueBoolean {
var rawValue []byte
if value {
rawValue = []byte{BoolTrue}
} else {
rawValue = []byte{BoolFalse}
}
return &ValueBoolean{
Value: value,
RawValue: rawValue,
}
}
func (entry *ValueBoolean) UpdateRaw(r io.Reader) error {
newEntry, entryErr := DecodeBoolean(r)
if entryErr != nil {
return entryErr
}
return mergo.MergeWithOverwrite(entry, *newEntry)
}
func (entry *ValueBoolean) GetRaw() []byte {
return entry.RawValue
}
func (entry *ValueBoolean) UpdateValue(value bool) error {
return mergo.MergeWithOverwrite(entry, *BuildBoolean(value))
}
type ValueString struct {
Value string
RawValue []byte
}
func DecodeString(r io.Reader) (*ValueString, error) {
uleb, ulebData, ulebErr := DecodeAndSaveULEB128(r)
if ulebErr != nil {
return nil, ulebErr
}
data := make([]byte, uleb)
_, readErr := io.ReadFull(r, data)
if readErr != nil {
return nil, readErr
}
return &ValueString{
Value: string(data),
RawValue: append(ulebData, data...),
}, nil
}
func BuildString(value string) *ValueString {
stringBytes := []byte(value)
rawValue := append(EncodeULEB128(uint32(len(stringBytes))), stringBytes...)
return &ValueString{
Value: value,
RawValue: rawValue,
}
}
func (entry *ValueString) UpdateRaw(r io.Reader) error {
newEntry, newErr := DecodeString(r)
if newErr != nil {
return newErr
}
return mergo.MergeWithOverwrite(entry, *newEntry)
}
func (entry *ValueString) GetRaw() []byte {
return entry.RawValue
}
func (entry *ValueString) UpdateValue(value string) error {
return mergo.MergeWithOverwrite(entry, *BuildString(value))
}
type ValueDouble struct {
Value float64
RawValue []byte
}
func DecodeDouble(r io.Reader) (*ValueDouble, error) {
data := make([]byte, 8)
_, readErr := io.ReadFull(r, data)
if readErr != nil {
return nil, readErr
}
return &ValueDouble{
Value: BytesToFloat64(data),
RawValue: data,
}, nil
}
func BuildDouble(value float64) *ValueDouble {
return &ValueDouble{
Value: value,
RawValue: Float64ToBytes(value),
}
}
func (entry *ValueDouble) UpdateRaw(r io.Reader) error {
newEntry, newErr := DecodeDouble(r)
if newErr != nil {
return newErr
}
return mergo.MergeWithOverwrite(entry, *newEntry)
}
func (entry *ValueDouble) GetRaw() []byte {
return entry.RawValue
}
func (entry *ValueDouble) UpdateValue(value float64) error {
return mergo.MergeWithOverwrite(entry, *BuildDouble(value))
}
type ValueRaw struct {
Value []byte
RawValue []byte
}
func DecodeRaw(r io.Reader) (*ValueRaw, error) {
uleb, ulebData, ulebErr := DecodeAndSaveULEB128(r)
if ulebErr != nil {
return nil, ulebErr
}
data := make([]byte, uleb)
_, readErr := io.ReadFull(r, data)
if readErr != nil {
return nil, readErr
}
return &ValueRaw{
Value: data,
RawValue: append(ulebData, data...),
}, nil
}
func BuildRaw(value []byte) *ValueRaw {
return &ValueRaw{
Value: value,
RawValue: append(EncodeULEB128(uint32(len(value))), value...),
}
}
func (entry *ValueRaw) UpdateRaw(r io.Reader) error {
newEntry, newErr := DecodeRaw(r)
if newErr != nil {
return newErr
}
return mergo.MergeWithOverwrite(entry, *newEntry)
}
func (entry *ValueRaw) GetRaw() []byte {
return entry.RawValue
}
func (entry *ValueRaw) UpdateValue(value []byte) error {
return mergo.MergeWithOverwrite(entry, *BuildRaw(value))
}
type ValueBooleanArray struct {
elements []*ValueBoolean
}
func DecodeBooleanArray(r io.Reader) (*ValueBooleanArray, error) {
indexData := make([]byte, 1)
_, readErr := io.ReadFull(r, indexData)
if readErr != nil {
return nil, readErr
}
index := uint8(indexData[0])
elements := make([]*ValueBoolean, index)
for i := uint8(0); i < index; i++ {
boolean, decodeErr := DecodeBoolean(r)
if decodeErr != nil {
return nil, decodeErr
}
elements[i] = boolean
}
return &ValueBooleanArray{
elements: elements,
}, nil
}
func BuildBooleanArray(values []*ValueBoolean) *ValueBooleanArray {
var index uint8
if len(values) > 255 {
index = 255
} else {
index = uint8(len(values))
}
return &ValueBooleanArray{
elements: values[:index],
}
}
func (array *ValueBooleanArray) Get(index uint8) (EntryValue, error) {
if index >= uint8(len(array.elements)) {
return nil, ErrArrayIndexOutOfBounds
}
return array.elements[index], nil
}
func (array *ValueBooleanArray) Update(index uint8, entry EntryValue) error {
boolean, ok := entry.(*ValueBoolean)
if !ok {
return ErrEntryCastInvalid
}
if index >= uint8(len(array.elements)) {
return ErrArrayIndexOutOfBounds
}
return mergo.MergeWithOverwrite(array.elements[index], *boolean)
}
func (array *ValueBooleanArray) Add(entry EntryValue) error {
boolean, ok := entry.(*ValueBoolean)
if !ok {
return ErrEntryCastInvalid
}
if len(array.elements) > 255 {
return ErrArrayOutOfSpace
}
array.elements = append(array.elements, boolean)
return nil
}
func (array *ValueBooleanArray) GetRaw() []byte {
data := []byte{byte(uint8(len(array.elements)))}
var i uint8 = 0
for ; i < uint8(len(array.elements)); i++ {
data = append(data, array.elements[i].RawValue...)
}
return data
}
type ValueDoubleArray struct {
elements []*ValueDouble
}
func DecodeDoubleArray(r io.Reader) (*ValueDoubleArray, error) {
indexData := make([]byte, 1)
_, readErr := io.ReadFull(r, indexData)
if readErr != nil {
return nil, readErr
}
index := uint8(indexData[0])
elements := make([]*ValueDouble, index)
for i := uint8(0); i < index; i++ {
double, decodeErr := DecodeDouble(r)
if decodeErr != nil {
return nil, decodeErr
}
elements[i] = double
}
return &ValueDoubleArray{
elements: elements,
}, nil
}
func BuildDoubleArray(values []*ValueDouble) *ValueDoubleArray {
var index uint8
if len(values) > 255 {
index = 255
} else {
index = uint8(len(values))
}
return &ValueDoubleArray{
elements: values[:index],
}
}
func (array *ValueDoubleArray) Get(index uint8) (EntryValue, error) {
if index >= uint8(len(array.elements)) {
return nil, ErrArrayIndexOutOfBounds
}
return array.elements[index], nil
}
func (array *ValueDoubleArray) Update(index uint8, entry EntryValue) error {
double, ok := entry.(*ValueDouble)
if !ok {
return ErrEntryCastInvalid
}
if index >= uint8(len(array.elements)) {
return ErrArrayIndexOutOfBounds
}
return mergo.MergeWithOverwrite(&array.elements[index], double)
}
func (array *ValueDoubleArray) Add(entry EntryValue) error {
double, ok := entry.(*ValueDouble)
if !ok {
return ErrEntryCastInvalid
}
if len(array.elements) > 255 {
return ErrArrayOutOfSpace
}
array.elements = append(array.elements, double)
return nil
}
func (array *ValueDoubleArray) GetRaw() []byte {
data := []byte{byte(uint8(len(array.elements)))}
var i uint8 = 0
for ; i < uint8(len(array.elements)); i++ {
data = append(data, array.elements[i].RawValue...)
}
return data
}
type ValueStringArray struct {
elements []*ValueString
}
func DecodeStringArray(r io.Reader) (*ValueStringArray, error) {
indexData := make([]byte, 1)
_, readErr := io.ReadFull(r, indexData)
if readErr != nil {
return nil, readErr
}
index := uint8(indexData[0])
elements := make([]*ValueString, index)
for i := uint8(0); i < index; i++ {
string, decodeErr := DecodeString(r)
if decodeErr != nil {
return nil, decodeErr
}
elements[i] = string
}
return &ValueStringArray{
elements: elements,
}, nil
}
func BuildStringArray(values []*ValueString) *ValueStringArray {
var index uint8
if len(values) > 255 {
index = 255
} else {
index = uint8(len(values))
}
return &ValueStringArray{
elements: values[:index],
}
}
func (array *ValueStringArray) Get(index uint8) (EntryValue, error) {
if index >= uint8(len(array.elements)) {
return nil, ErrArrayIndexOutOfBounds
}
return array.elements[index], nil
}
func (array *ValueStringArray) Update(index uint8, entry EntryValue) error {
string, ok := entry.(*ValueString)
if !ok {
return ErrEntryCastInvalid
}
if index >= uint8(len(array.elements)) {
return ErrArrayIndexOutOfBounds
}
return mergo.MergeWithOverwrite(array.elements[index], *string)
}
func (array *ValueStringArray) Add(entry EntryValue) error {
string, ok := entry.(*ValueString)
if !ok {
return ErrEntryCastInvalid
}
if len(array.elements) > 255 {
return ErrArrayOutOfSpace
}
array.elements = append(array.elements, string)
return nil
}
func (array *ValueStringArray) GetRaw() []byte {
data := []byte{byte(uint8(len(array.elements)))}
var i uint8 = 0
for ; i < uint8(len(array.elements)); i++ {
data = append(data, array.elements[i].RawValue...)
}
return data
}