/
ReadBufferJsonBased.go
491 lines (460 loc) · 15.1 KB
/
ReadBufferJsonBased.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package utils
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"math/big"
"strings"
)
// NewJsonReadBuffer return as ReadBuffer which doesn't validate attributes and lists
func NewJsonReadBuffer(reader io.Reader) ReadBuffer {
decoder := json.NewDecoder(reader)
var rootElement map[string]any
err := decoder.Decode(&rootElement)
return &jsonReadBuffer{
rootElement: rootElement,
pos: 1,
doValidateAttr: true,
err: err,
}
}
// NewStrictJsonReadBuffer return as ReadBuffer which does validate attributes on the setting
func NewStrictJsonReadBuffer(reader io.Reader, validateAttr bool) ReadBuffer {
decoder := json.NewDecoder(reader)
var rootElement map[string]any
err := decoder.Decode(&rootElement)
return &jsonReadBuffer{
rootElement: rootElement,
pos: 1,
doValidateAttr: validateAttr,
err: err,
}
}
///////////////////////////////////////
///////////////////////////////////////
//
// Internal section
//
type jsonReadBuffer struct {
BufferCommons
Stack
rootElement map[string]any
pos uint
doValidateAttr bool
err error
}
//
// Internal section
//
///////////////////////////////////////
///////////////////////////////////////
func (j *jsonReadBuffer) GetPos() uint16 {
return uint16(j.pos / 8)
}
func (j *jsonReadBuffer) Reset(pos uint16) {
j.pos = uint(pos * 8)
}
func (j *jsonReadBuffer) HasMore(bitLength uint8) bool {
// TODO: work with x.InputOffset() and check if we are at EOF
return true
}
func (j *jsonReadBuffer) PullContext(logicalName string, readerArgs ...WithReaderArgs) error {
if j.err != nil {
return j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
if j.Empty() {
if context, ok := j.rootElement[logicalName]; ok {
j.Push(context)
return nil
} else {
return errors.Errorf("Required context %s not found in %v", logicalName, j.rootElement)
}
}
peek := j.Peek()
switch peek.(type) {
case []any:
pop := j.Pop()
contextList := pop.([]any)
context := contextList[0].(map[string]any)
if len(contextList) < 2 {
j.Push(make([]any, 0))
} else {
j.Push(contextList[1 : len(contextList)-1])
}
if subContext, ok := context[logicalName]; ok {
j.Push(subContext)
return nil
} else {
return errors.Errorf("Required context %s not found in %v", logicalName, peek)
}
}
if context, ok := peek.(map[string]any)[logicalName]; ok {
j.Push(context)
return nil
} else {
return errors.Errorf("Required context %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadBit(logicalName string, readerArgs ...WithReaderArgs) (bool, error) {
if j.err != nil {
return false, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(1)
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "bit", 1, readerArgs...); err != nil {
return false, err
}
if value, ok := element[logicalName]; ok {
return value.(bool), nil
} else {
return false, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadByte(logicalName string, readerArgs ...WithReaderArgs) (byte, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(8)
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "byte", 8, readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
hexString := value.(string)
if !strings.HasPrefix(hexString, "0x") {
return 0, errors.Errorf("Hex string should start with 0x. Actual value %s", hexString)
}
hexString = strings.Replace(hexString, "0x", "", 1)
decoded, err := hex.DecodeString(hexString)
if err != nil {
return 0, err
}
return decoded[0], nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadByteArray(logicalName string, numberOfBytes int, readerArgs ...WithReaderArgs) ([]byte, error) {
if j.err != nil {
return nil, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(numberOfBytes / 8))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "byte", uint(numberOfBytes*8), readerArgs...); err != nil {
return nil, err
}
if value, ok := element[logicalName]; ok {
hexString := value.(string)
if !strings.HasPrefix(hexString, "0x") {
return nil, errors.Errorf("Hex string should start with 0x. Actual value %s", hexString)
}
hexString = strings.Replace(hexString, "0x", "", 1)
decoded, err := hex.DecodeString(hexString)
if err != nil {
return nil, err
}
return decoded, nil
} else {
return nil, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadUint8(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (uint8, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "uint", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return uint8(value.(float64)), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadUint16(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (uint16, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "uint", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return uint16(value.(float64)), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadUint32(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (uint32, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "uint", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return uint32(value.(float64)), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadUint64(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (uint64, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "uint", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return uint64(value.(float64)), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadInt8(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (int8, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "int", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return int8(value.(float64)), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadInt16(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (int16, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "int", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return int16(value.(float64)), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadInt32(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (int32, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "int", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return int32(value.(float64)), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadInt64(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (int64, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "int", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return int64(value.(float64)), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadBigInt(logicalName string, bitLength uint64, readerArgs ...WithReaderArgs) (*big.Int, error) {
if j.err != nil {
return nil, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
logicalName = j.SanitizeLogicalName(logicalName)
peek, element := j.getElement(logicalName)
// TODO: not enough bits
if err := j.validateAttr(logicalName, element, "int", uint(bitLength), readerArgs...); err != nil {
return nil, err
}
newInt := big.NewInt(0)
if value, ok := element[logicalName]; ok {
newInt.SetString(value.(string), 10)
return newInt, nil
} else {
return newInt, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadFloat32(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (float32, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "float", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return float32(value.(float64)), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadFloat64(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (float64, error) {
if j.err != nil {
return 0, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "float", uint(bitLength), readerArgs...); err != nil {
return 0, err
}
if value, ok := element[logicalName]; ok {
return value.(float64), nil
} else {
return 0, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadBigFloat(logicalName string, bitLength uint8, readerArgs ...WithReaderArgs) (*big.Float, error) {
if j.err != nil {
return nil, j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "float", uint(bitLength), readerArgs...); err != nil {
return nil, err
}
newFloat := big.NewFloat(0)
if value, ok := element[logicalName]; ok {
newFloat.SetString(value.(string))
return newFloat, nil
} else {
return newFloat, errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) ReadString(logicalName string, bitLength uint32, encoding string, readerArgs ...WithReaderArgs) (string, error) {
if j.err != nil {
return "", j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
j.move(uint(bitLength))
peek, element := j.getElement(logicalName)
if err := j.validateAttr(logicalName, element, "string", uint(bitLength), readerArgs...); err != nil {
return "", err
}
if value, ok := element[logicalName]; ok {
return value.(string), nil
} else {
return "", errors.Errorf("Required element %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) CloseContext(logicalName string, readerArgs ...WithReaderArgs) error {
if j.err != nil {
return j.err
}
logicalName = j.SanitizeLogicalName(logicalName)
if j.Empty() {
return errors.Errorf("Required context close %s not found in %v", logicalName, j.rootElement)
}
// Delete us from Stack
_ = j.Pop()
if j.Empty() {
return nil
}
peek := j.Peek()
switch peek.(type) {
case []any:
return nil
}
if _, ok := peek.(map[string]any)[logicalName]; ok {
delete(peek.(map[string]any), logicalName)
return nil
} else {
return errors.Errorf("Required context %s not found in %v", logicalName, peek)
}
}
func (j *jsonReadBuffer) getElement(logicalName string) (any, map[string]any) {
logicalName = j.SanitizeLogicalName(logicalName)
peek := j.Peek()
var element map[string]any
switch peek.(type) {
case []any:
pop := j.Pop()
elementList := pop.([]any)
element = elementList[0].(map[string]any)
if len(elementList) < 2 {
j.Push(make([]any, 0))
} else {
j.Push(elementList[1 : len(elementList)-1])
}
case map[string]any:
element = peek.(map[string]any)
default:
panic(fmt.Sprintf("Invalid state at %s with %v", logicalName, element))
}
return peek, element
}
func (j *jsonReadBuffer) move(bits uint) {
j.pos += bits
}
func (j *jsonReadBuffer) validateAttr(logicalName string, element map[string]any, dataType string, bitLength uint, readerArgs ...WithReaderArgs) error {
if !j.doValidateAttr {
return nil
}
renderedKeyDataLengthKey := fmt.Sprintf("%s__plc4x_%s", logicalName, rwDataTypeKey)
if value, ok := element[renderedKeyDataLengthKey]; !ok || value != dataType {
return errors.Errorf("Unexpected %s :%s. Want %s", renderedKeyDataLengthKey, value, dataType)
}
renderedBitLengthKey := fmt.Sprintf("%s__plc4x_%s", logicalName, rwBitLengthKey)
if value, ok := element[renderedBitLengthKey]; !ok || uint(value.(float64)) != bitLength {
return errors.Errorf("Unexpected %s :%d. Want %d", renderedBitLengthKey, value, bitLength)
}
return nil
}