-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathaper.go
1273 lines (1184 loc) · 39 KB
/
aper.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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2020-present Open Networking Foundation <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
package aper
import (
"encoding/hex"
"fmt"
"reflect"
"github.com/onosproject/onos-lib-go/api/asn1/v1/asn1"
"github.com/onosproject/onos-lib-go/pkg/errors"
"github.com/onosproject/onos-lib-go/pkg/logging"
)
var log = logging.GetLogger("asn1", "aper")
type perBitData struct {
bytes []byte
byteOffset uint64
bitsOffset uint
choiceMap map[string]map[int]reflect.Type
unique int64
uniqueStructFlag bool
canonicalOrdering bool
canonicalChoiceMap map[string]map[int64]reflect.Type
choiceCanBeExtended bool
}
func perBitLog(numBits uint64, byteOffset uint64, bitsOffset uint, value interface{}) string {
if reflect.TypeOf(value).Kind() == reflect.Uint64 {
return fmt.Sprintf(" [PER got %2d bits, byteOffset(after): %d, bitsOffset(after): %d, value: 0x%0x]",
numBits, byteOffset, bitsOffset, reflect.ValueOf(value).Uint())
}
return fmt.Sprintf(" [PER got %2d bits, byteOffset(after): %d, bitsOffset(after): %d, value: 0x%0x]",
numBits, byteOffset, bitsOffset, reflect.ValueOf(value).Bytes())
}
// GetBitString is to get BitString with desire size from source byte array with bit offset
func GetBitString(srcBytes []byte, bitsOffset uint, numBits uint) (dstBytes []byte, err error) {
if numBits == 0 {
return []byte{}, nil
}
bitsLeft := uint(len(srcBytes))*8 - bitsOffset
if numBits > bitsLeft {
err = fmt.Errorf("Get bits overflow, requireBits: %d, leftBits: %d", numBits, bitsLeft)
return
}
byteLen := (bitsOffset + numBits + 7) >> 3
numBitsByteLen := (numBits + 7) >> 3
dstBytes = make([]byte, numBitsByteLen)
numBitsMask := byte(0xff)
if modEight := numBits & 0x7; modEight != 0 {
numBitsMask <<= uint8(8 - (modEight))
}
for i := 1; i < int(byteLen); i++ {
dstBytes[i-1] = srcBytes[i-1]<<bitsOffset | srcBytes[i]>>(8-bitsOffset)
}
if byteLen == numBitsByteLen {
dstBytes[byteLen-1] = srcBytes[byteLen-1] << bitsOffset
}
dstBytes[numBitsByteLen-1] &= numBitsMask
return
}
// GetFewBits is to get Value with desire few bits from source byte with bit offset
// func GetFewBits(srcByte byte, bitsOffset uint, numBits uint) (value uint64, err error) {
// if numBits == 0 {
// value = 0
// return
// }
// bitsLeft := 8 - bitsOffset
// if bitsLeft < numBits {
// err = fmt.Errorf("Get bits overflow, requireBits: %d, leftBits: %d", numBits, bitsLeft)
// return
// }
// if bitsOffset == 0 {
// value = uint64(srcByte >> (8 - numBits))
// } else {
// value = uint64((srcByte << bitsOffset) >> (8 - numBits))
// }
// return
// }
// GetBitsValue is to get Value with desire bits from source byte array with bit offset
func GetBitsValue(srcBytes []byte, bitsOffset uint, numBits uint) (value uint64, err error) {
var dstBytes []byte
dstBytes, err = GetBitString(srcBytes, bitsOffset, numBits)
if err != nil {
return
}
for i, j := 0, numBits; j >= 8; i, j = i+1, j-8 {
value <<= 8
value |= uint64(uint(dstBytes[i]))
}
if numBitsOff := numBits & 0x7; numBitsOff != 0 {
var mask uint = (1 << numBitsOff) - 1
value <<= numBitsOff
value |= uint64(uint(dstBytes[len(dstBytes)-1]>>(8-numBitsOff)) & mask)
}
return
}
func (pd *perBitData) bitCarry() {
pd.byteOffset += uint64(pd.bitsOffset >> 3)
pd.bitsOffset = pd.bitsOffset & 0x07
}
func (pd *perBitData) getBitString(numBits uint) (dstBytes []byte, err error) {
dstBytes, err = GetBitString(pd.bytes[pd.byteOffset:], pd.bitsOffset, numBits)
if err != nil {
return
}
pd.bitsOffset += numBits
pd.bitCarry()
log.Debugf("%s", perBitLog(uint64(numBits), pd.byteOffset, pd.bitsOffset, dstBytes))
return
}
func (pd *perBitData) getBitsValue(numBits uint) (value uint64, err error) {
value, err = GetBitsValue(pd.bytes[pd.byteOffset:], pd.bitsOffset, numBits)
if err != nil {
return
}
pd.bitsOffset += numBits
pd.bitCarry()
log.Debugf("%s", perBitLog(uint64(numBits), pd.byteOffset, pd.bitsOffset, value))
return
}
func (pd *perBitData) parseAlignBits() error {
if (pd.bitsOffset & 0x7) > 0 {
alignBits := 8 - ((pd.bitsOffset) & 0x7)
log.Debugf("Aligning %d bits", alignBits)
if val, err := pd.getBitsValue(alignBits); err != nil {
return err
} else if val != 0 {
return fmt.Errorf("Align Bit is not zero in (see last octet)\n%v", hex.Dump(pd.bytes[:pd.byteOffset+1]))
}
} else if pd.bitsOffset != 0 {
pd.bitCarry()
}
return nil
}
func (pd *perBitData) parseConstraintValue(valueRange int64) (value uint64, err error) {
log.Debugf("Getting Constraint Value with range %d", valueRange)
var bytes uint
if valueRange <= 255 {
if valueRange < 0 {
err = fmt.Errorf("Value range is negative")
return
}
var i uint
// 1 ~ 8 bits
for i = 1; i <= 8; i++ {
upper := 1 << i
if int64(upper) >= valueRange {
break
}
}
value, err = pd.getBitsValue(i)
return
} else if valueRange == 256 {
bytes = 1
} else if valueRange <= 65536 {
bytes = 2
} else {
err = fmt.Errorf("Constraint Value is large than 65536")
return
}
if err = pd.parseAlignBits(); err != nil {
return
}
value, err = pd.getBitsValue(bytes * 8)
return value, err
}
func (pd *perBitData) parseLength(sizeRange int64, repeat *bool) (value uint64, err error) {
*repeat = false
if sizeRange <= 65536 && sizeRange > 0 {
return pd.parseConstraintValue(sizeRange)
}
if err = pd.parseAlignBits(); err != nil {
return
}
firstByte, err := pd.getBitsValue(8)
if err != nil {
return
}
if (firstByte & 128) == 0 { // #10.9.3.6
value = firstByte & 0x7F
return
} else if (firstByte & 64) == 0 { // #10.9.3.7
var secondByte uint64
if secondByte, err = pd.getBitsValue(8); err != nil {
return
}
value = ((firstByte & 63) << 8) | secondByte
return
}
firstByte &= 63
if firstByte < 1 || firstByte > 4 {
err = fmt.Errorf("Parsed Length Out of Constraint")
return
}
*repeat = true
value = 16384 * firstByte
return value, err
}
func (pd *perBitData) parseBitString(extensed bool, lowerBoundPtr *int64, upperBoundPtr *int64) (*asn1.BitString, error) {
var lb, ub, sizeRange int64 = 0, -1, -1
if !extensed {
if lowerBoundPtr != nil {
lb = *lowerBoundPtr
}
if upperBoundPtr != nil {
ub = *upperBoundPtr
sizeRange = ub - lb + 1
}
}
if ub > 65535 {
sizeRange = -1
}
// initailization
bitString := asn1.BitString{Value: make([]byte, 0), Len: 0}
// lowerbound == upperbound
if sizeRange == 1 {
sizes := uint64(ub+7) >> 3
bitString.Len = uint32(uint64(ub))
log.Debugf("Decoding BIT STRING size %d", ub)
log.Debugf("Decoding BIT STRING size %d", bitString.Len)
if sizes > 2 {
if err := pd.parseAlignBits(); err != nil {
return nil, err
}
if (pd.byteOffset + sizes) > uint64(len(pd.bytes)) {
err := fmt.Errorf("PER data out of range")
return nil, err
}
if _, err := bitString.UpdateValue(pd.bytes[pd.byteOffset : pd.byteOffset+sizes]); err != nil {
return nil, err
}
// Truncating last trailing bits -- Length of a BitString should be already set!
if _, err := bitString.TruncateValue(); err != nil {
return nil, err
}
//bitString.Value = append(bitString.Value, pd.bytes[pd.byteOffset:pd.byteOffset+sizes]...)
pd.byteOffset += sizes
pd.bitsOffset = uint(ub & 0x7)
if pd.bitsOffset > 0 {
pd.byteOffset--
}
log.Debugf("%s", perBitLog(uint64(ub), pd.byteOffset, pd.bitsOffset, bitString.Value))
} else {
bytes, err := pd.getBitString(uint(ub))
if err != nil {
log.Errorf("PD GetBitString error: %+v", err)
return nil, err
}
if _, err = bitString.UpdateValue(bytes); err != nil {
return nil, err
}
}
log.Debugf("Decoded BIT STRING (length = %d): %0.8b", ub, bitString.Value)
return &bitString, nil
}
repeat := false
for {
var rawLength uint64
length, err := pd.parseLength(sizeRange, &repeat)
if err != nil {
return nil, err
}
rawLength = length
rawLength += uint64(lb)
log.Debugf("Decoding BIT STRING size %d", rawLength)
if rawLength == 0 {
return nil, nil
}
sizes := (rawLength + 7) >> 3
if err := pd.parseAlignBits(); err != nil {
return nil, err
}
if (pd.byteOffset + sizes) > uint64(len(pd.bytes)) {
return nil, errors.NewInvalid("PER data out of range")
}
//bitString.Value = append(bitString.Value, pd.bytes[pd.byteOffset:pd.byteOffset+sizes]...)
bitString.Len += uint32(rawLength)
// we need to get length before we want to decode with UpdateValue
if _, err := bitString.UpdateValue(pd.bytes[pd.byteOffset : pd.byteOffset+sizes]); err != nil {
return nil, err
}
// Truncating last trailing bits -- Length of a BitString should be already set!
if _, err := bitString.TruncateValue(); err != nil {
return nil, err
}
pd.byteOffset += sizes
pd.bitsOffset = uint(rawLength & 0x7)
if pd.bitsOffset != 0 {
pd.byteOffset--
}
log.Debugf("%s", perBitLog(rawLength, pd.byteOffset, pd.bitsOffset, bitString.Value))
log.Debugf("Decoded BIT STRING (length = %d): %0.8b", rawLength, bitString.Value)
if !repeat {
// if err = pd.parseAlignBits(); err != nil {
// return
// }
break
}
}
return &bitString, nil
}
func (pd *perBitData) parseOctetString(extensed bool, lowerBoundPtr *int64, upperBoundPtr *int64) (
[]byte, error) {
var lb, ub, sizeRange int64 = 0, -1, -1
if !extensed {
if lowerBoundPtr != nil {
lb = *lowerBoundPtr
}
if upperBoundPtr != nil {
ub = *upperBoundPtr
sizeRange = ub - lb + 1
}
}
if ub > 65535 {
sizeRange = -1
}
// initailization
octetString := []byte("")
// lowerbound == upperbound
if sizeRange == 1 {
log.Debugf("Decoding OCTET STRING size %d", ub)
if ub > 2 {
unsignedUB := uint64(ub)
if err := pd.parseAlignBits(); err != nil {
return octetString, err
}
if (int64(pd.byteOffset) + ub) > int64(len(pd.bytes)) {
err := fmt.Errorf("per data out of range")
return octetString, err
}
octetString = pd.bytes[pd.byteOffset : pd.byteOffset+unsignedUB]
pd.byteOffset += uint64(ub)
log.Debugf("%s", perBitLog(8*unsignedUB, pd.byteOffset, pd.bitsOffset, octetString))
} else {
octet, err := pd.getBitString(uint(ub * 8))
if err != nil {
return octetString, err
}
octetString = octet
}
log.Debugf("Decoded OCTET STRING (length = %d): 0x%0x", ub, octetString)
return octetString, nil
}
repeat := false
for {
var rawLength uint64
length, err := pd.parseLength(sizeRange, &repeat)
if err != nil {
return octetString, err
}
rawLength = length
rawLength += uint64(lb)
log.Debugf("Decoding OCTET STRING size %d", rawLength)
if rawLength == 0 {
return octetString, nil
} else if err := pd.parseAlignBits(); err != nil {
return octetString, err
}
if (rawLength + pd.byteOffset) > uint64(len(pd.bytes)) {
err := fmt.Errorf("per data out of range ")
return octetString, err
}
octetString = append(octetString, pd.bytes[pd.byteOffset:pd.byteOffset+rawLength]...)
pd.byteOffset += rawLength
log.Debugf("%s", perBitLog(8*rawLength, pd.byteOffset, pd.bitsOffset, octetString))
log.Debugf("Decoded OCTET STRING (length = %d): 0x%0x", rawLength, octetString)
if !repeat {
// if err = pd.parseAlignBits(); err != nil {
// return
// }
break
}
}
return octetString, nil
}
func (pd *perBitData) parseBool() (value bool, err error) {
log.Debugf("Decoding BOOLEAN Value")
bit, err1 := pd.getBitsValue(1)
if err1 != nil {
err = err1
return
}
if bit == 1 {
value = true
} else {
value = false
}
log.Debugf("Decoded BOOLEAN Value %v", value)
return
}
func (pd *perBitData) parseReal(lb *int64, ub *int64, valueExt bool) (float64, error) {
log.Debugf("Decoding REAL structure")
var result float64
// parsing aligning bits first
err := pd.parseAlignBits()
if err != nil {
return 0, err
}
byteLength, err := pd.getBitsValue(8)
if err != nil {
return 0, err
}
log.Debugf("Next %v bytes contain exponent and mantissa", byteLength)
// parsing header
bit, err := pd.getBitsValue(1)
if err != nil {
return 0, err
}
if bit != 1 {
return 0, errors.NewInvalid("It looks like the header for REAL contains corrupted bytes. Got %v, expected to have 1", bit)
}
// parsing sign
sign, err := pd.getBitsValue(1)
if err != nil {
return 0, err
}
negative := false
if sign == 0 {
log.Debugf("Obtained %v - REAL is a positive number", sign)
} else if sign == 1 {
log.Debugf("Obtained %v - REAL is a negative number", sign)
negative = true
}
var base int
// parsing encoding base
bb, err := pd.getBitsValue(2)
if err != nil {
return 0, err
}
switch bb {
case 0:
base = 2
log.Debugf("Obtained %v - decoding REAL with base 2 (default)", bb)
case 1:
base = 8
return 0, errors.NewInvalid("Error while parsing encoding base of REAL, obtained %v - base of 8 is not supported", bb)
case 2:
base = 16
return 0, errors.NewInvalid("Error while parsing encoding base of REAL, obtained %v - base of 16 is not supported", bb)
default:
return 0, errors.NewInvalid("Error while parsing encoding base of REAL, obtained %v", bb)
}
// parsing scaling factor
ff, err := pd.getBitsValue(2)
if err != nil {
return 0, err
}
if ff != 0 {
return 0, errors.NewInvalid("Error parsing scaling factor - decoded bits expected to be 0, obtained %v", ff)
}
// parsing exponent
ee, err := pd.getBitsValue(2)
if err != nil {
return 0, err
}
if ee != 0 {
return 0, errors.NewInvalid("Error parsing exponent - decoded bits expected to be 0, obtained %v", ee)
}
//parsing exponent value
exponent, err := pd.getBitsValue(8)
if err != nil {
return 0, err
}
twosComplement := false
if exponent >= 52 {
// getting 2's complement back
exponent = 256 - exponent
twosComplement = true
}
log.Debugf("Obtained exponent is %v", exponent)
// parsing mantissa
mantissa, err := pd.getBitsValue(8 * uint(byteLength-1-1))
if err != nil {
return 0, err
}
log.Debugf("Obtained mantissa is %v", mantissa)
if twosComplement {
result = float64(mantissa)
for i := 0; i < int(exponent); i++ {
result = result / float64(base)
}
} else {
result = float64(mantissa)
for i := 0; i < int(exponent); i++ {
result = result * float64(base)
}
}
if negative {
result = -result
}
// checking if value is within bounds
// we don't want to break decoding here, just putting warnings
if lb != nil {
lowerBound := *lb
if result < float64(lowerBound-1) {
log.Warnf("Decoding REAL - value (%v) is lower than lowerbound (%v)", result, float64(lowerBound))
}
}
if ub != nil {
upperBound := *ub
if result > float64(upperBound+1) && !valueExt {
log.Warnf("Decoding REAL - value (%v) is higher than upperbound (%v)", result, float64(upperBound))
}
}
// ToDo - find a way how to cut off distortion obtained in the division, e.g. decoding of 98765.4321
log.Debugf("Decoded REAL number is %v", result)
return result, nil
}
func (pd *perBitData) parseInteger(extensed bool, lowerBoundPtr *int64, upperBoundPtr *int64) (int64, error) {
var lb, ub, valueRange int64 = 0, -1, 0
if !extensed {
if lowerBoundPtr == nil {
log.Debugf("Decoding INTEGER with Unconstraint Value")
valueRange = -1
} else {
lb = *lowerBoundPtr
if upperBoundPtr != nil {
ub = *upperBoundPtr
valueRange = ub - lb + 1
log.Debugf("Decoding INTEGER with Value Range(%d..%d)", lb, ub)
} else {
log.Debugf("Decoding INTEGER with Semi-Constraint Range(%d..)", lb)
}
}
} else {
valueRange = -1
log.Debugf("Decoding INTEGER with Extensive Value")
}
var rawLength uint
if valueRange == 1 {
return ub, nil
} else if valueRange <= 0 {
// semi-constraint or unconstraint
if err := pd.parseAlignBits(); err != nil {
return int64(0), err
}
if pd.byteOffset >= uint64(len(pd.bytes)) {
return int64(0), fmt.Errorf("per data out of range")
}
rawLength = uint(pd.bytes[pd.byteOffset])
pd.byteOffset++
log.Debugf("%s", perBitLog(8, pd.byteOffset, pd.bitsOffset, uint64(rawLength)))
} else if valueRange <= 65536 {
rawValue, err := pd.parseConstraintValue(valueRange)
if err != nil {
return int64(0), err
}
return int64(rawValue) + lb, nil
} else {
// valueRange > 65536
var byteLen uint
unsignedValueRange := uint64(valueRange - 1)
for byteLen = 1; byteLen <= 127; byteLen++ {
unsignedValueRange >>= 8
if unsignedValueRange == 0 {
break
}
}
var i, upper uint
// 1 ~ 8 bits
for i = 1; i <= 8; i++ {
upper = 1 << i
if upper >= byteLen {
break
}
}
tempLength, err := pd.getBitsValue(i)
if err != nil {
return int64(0), err
}
rawLength = uint(tempLength)
rawLength++
if err := pd.parseAlignBits(); err != nil {
return int64(0), err
}
}
log.Debugf("Decoding INTEGER Length with %d bytes", rawLength)
if rawValue, err := pd.getBitsValue(rawLength * 8); err != nil {
return int64(0), err
} else if valueRange < 0 {
signedBitMask := uint64(1 << (rawLength*8 - 1))
valueMask := signedBitMask - 1
// negative
if rawValue&signedBitMask > 0 {
return int64((^rawValue)&valueMask+1) * -1, nil
}
return int64(rawValue) + lb, nil
} else {
return int64(rawValue) + lb, nil
}
}
// parse ENUMERATED type but do not implement extensive value and different value with index
//func (pd *perBitData) parseEnumerated(extensed bool, lowerBoundPtr *int64, upperBoundPtr *int64) (value uint64,
// err error) {
// if extensed {
// err = fmt.Errorf("Unsupport the extensive value of ENUMERATED ")
// return
// }
// if lowerBoundPtr == nil || upperBoundPtr == nil {
// err = fmt.Errorf("ENUMERATED value constraint is error ")
// return
// }
// lb, ub := *lowerBoundPtr, *upperBoundPtr
// valueRange := ub - lb + 1
// log.Debugf("Decoding ENUMERATED with Value Range(%d..%d)", lb, ub)
// if valueRange > 1 {
// value, err = pd.parseConstraintValue(valueRange)
// }
// log.Debugf("Decoded ENUMERATED Value : %d", value)
// return
//
//}
func (pd *perBitData) parseSequenceOf(sizeExtensed bool, params fieldParameters, sliceType reflect.Type) (
reflect.Value, error) {
var sliceContent reflect.Value
var lb int64
var sizeRange int64
if params.sizeLowerBound != nil && *params.sizeLowerBound < 65536 {
lb = *params.sizeLowerBound
}
if !sizeExtensed && params.sizeUpperBound != nil && *params.sizeUpperBound < 65536 {
ub := *params.sizeUpperBound
sizeRange = ub - lb + 1
log.Debugf("Decoding Length of \"SEQUENCE OF\" with Size Range(%d..%d)", lb, ub)
} else {
sizeRange = -1
log.Debugf("Decoding Length of \"SEQUENCE OF\" with Semi-Constraint Range(%d..)", lb)
}
var numElements uint64
if sizeRange > 1 {
if numElementsTmp, err := pd.parseConstraintValue(sizeRange); err != nil {
log.Errorf("Parse Constraint Value failed: %+v", err)
} else {
numElements = numElementsTmp
}
numElements += uint64(lb)
} else if sizeRange == 1 {
numElements += uint64(lb)
} else {
if err := pd.parseAlignBits(); err != nil {
return sliceContent, err
}
if pd.byteOffset >= uint64(len(pd.bytes)) {
err := fmt.Errorf("per data out of range")
return sliceContent, err
}
numElements = uint64(pd.bytes[pd.byteOffset])
pd.byteOffset++
log.Debugf("%s", perBitLog(8, pd.byteOffset, pd.bitsOffset, numElements))
}
log.Debugf("Decoding \"SEQUENCE OF\" struct %s with len(%d)", sliceType.Elem().Name(), numElements)
params.sizeExtensible = false
params.sizeUpperBound = nil
params.sizeLowerBound = nil
intNumElements := int(numElements)
sliceContent = reflect.MakeSlice(sliceType, intNumElements, intNumElements)
for i := 0; i < intNumElements; i++ {
err := parseField(sliceContent.Index(i), pd, params)
if err != nil {
return sliceContent, err
}
}
return sliceContent, nil
}
func (pd *perBitData) getChoiceIndex(numItemsNotInExtension int, choiceMapLen int) (present int, err error) {
if pd.choiceCanBeExtended {
// This flag has already served for its purpose. Setting it back to its initial value
pd.choiceCanBeExtended = false
isExtended := false
if bitsValue, err1 := pd.getBitsValue(1); err1 != nil {
err = err1
} else if bitsValue != 0 {
isExtended = true
}
if isExtended {
log.Debugf("Choice is extended. Parsing items from extension")
upperBound := choiceMapLen - numItemsNotInExtension
if upperBound == 1 {
// We have only single item inside the choice extension, returning choiceMap length as a last CHOCIE index
present = choiceMapLen
} else {
if upperBound < 1 {
err = fmt.Errorf("the upper bound of CHOICE is missing")
} else if rawChoice, err1 := pd.parseConstraintValue(int64(upperBound)); err1 != nil {
err = err1
} else {
present = int(rawChoice) + 1 + numItemsNotInExtension
}
}
} else {
log.Debugf("Choice is not extended. Parsing main items")
if numItemsNotInExtension == 1 {
present = 1
} else {
upperBound := numItemsNotInExtension
if upperBound < 1 {
err = fmt.Errorf("the upper bound of CHOICE is missing")
} else if rawChoice, err1 := pd.parseConstraintValue(int64(numItemsNotInExtension)); err1 != nil {
err = err1
} else {
present = int(rawChoice) + 1
}
}
}
} else {
upperBound := choiceMapLen
if upperBound < 1 {
err = fmt.Errorf("the upper bound of CHOICE is missing")
} else if rawChoice, err1 := pd.parseConstraintValue(int64(upperBound)); err1 != nil {
err = err1
} else {
log.Debugf("Decoded Present index of CHOICE is %d + 1", rawChoice)
present = int(rawChoice) + 1
}
}
return present, err
}
func (pd *perBitData) getCanonicalChoiceIndex() error {
return pd.parseNormallySmallNonNegativeWholeNumber()
}
func (pd *perBitData) parseNormallySmallNonNegativeWholeNumber() error {
err := pd.parseAlignBits()
if err != nil {
return err
}
log.Debugf("Parsing %v bits", len(pd.bytes[pd.byteOffset:]))
ext, err := pd.getBitsValue(1)
if err != nil {
return err
}
if ext == 0 {
log.Debugf("Extension for Normally Small Non Negative Whole Number is present")
numBytes, err := pd.getBitsValue(7)
if err != nil {
return err
}
log.Debugf("Decoding %v bytes", numBytes)
} else if ext == 1 {
log.Debugf("Extension for Normally Small Non Negative Whole Number is NOT present")
numBytes, err := pd.getBitsValue(15)
if err != nil {
return err
}
log.Debugf("Decoding %v bytes", numBytes)
}
return nil
}
//func getReferenceFieldValue(v reflect.Value) (value int64, err error) {
// fieldType := v.Type()
// switch v.Kind() {
// case reflect.Int, reflect.Int32, reflect.Int64:
// value = v.Int()
// case reflect.Struct:
// if fieldType.Field(0).Name == "Present" {
// present := int(v.Field(0).Int())
// if present == 0 {
// err = fmt.Errorf("ReferenceField Value present is 0(present's field number)")
// } else if present >= fieldType.NumField() {
// err = fmt.Errorf("'Present' is bigger than number of struct field")
// } else {
// value, err = getReferenceFieldValue(v.Field(present))
// }
// } else {
// value, err = getReferenceFieldValue(v.Field(0))
// }
// default:
// err = fmt.Errorf("OpenType reference only support INTEGER")
// }
// return
//}
// parseField is the main parsing function. Given a byte slice and an offset
// into the array, it will try to parse a suitable ASN.1 value out and store it
// in the given Value. TODO : ObjectIdenfier, handle extension Field
func parseField(v reflect.Value, pd *perBitData, params fieldParameters) error {
log.Debugf("Decoding %s", v.Type().Name())
fieldType := v.Type()
// If we have run out of data return error.
if pd.byteOffset == uint64(len(pd.bytes)) {
return fmt.Errorf("sequence truncated")
}
if v.Kind() == reflect.Ptr {
ptr := reflect.New(fieldType.Elem())
v.Set(ptr)
return parseField(v.Elem(), pd, params)
}
sizeExtensible := false
if params.sizeExtensible {
if bitsValue, err1 := pd.getBitsValue(1); err1 != nil {
return err1
} else if bitsValue != 0 {
sizeExtensible = true
}
log.Debugf("Decoded Size Extensive Bit: %t", sizeExtensible)
}
valueExtensible := false // this is to indicate that the items in the Extension are present
if params.valueExtensible && v.Kind() != reflect.Slice && !params.choiceExt {
if params.valueExtensible && v.Kind() != reflect.Slice && !params.choiceExt {
if bitsValue, err1 := pd.getBitsValue(1); err1 != nil {
return err1
} else if bitsValue != 0 {
valueExtensible = true
}
log.Debugf("Decoded Value Extensive Bit: %t", valueExtensible)
}
//No need to process bit here, it'll be done inside the case for struct
log.Debugf("Indicating Value Extensive Bit: %t", valueExtensible)
}
if params.choiceExt && v.Kind() != reflect.Slice {
// We have to make this variable global. In the decoding we're parsing parent structure first
// and then drilling down to its child. Once we've drilled down, we don't see previous (local) flag anymore.
pd.choiceCanBeExtended = true
log.Debugf("CHOICE can be extended")
}
// Setting explicitly flag for canonical ordering here due to the specificity of passing flags to choices
// (canonicalOrder flag is being set one level above than for regular choice)
if params.canonicalOrder {
pd.canonicalOrdering = true
log.Debugf("Setting canonicalOrdering flag to true. Next CHOICE is expected to be in canonical ordering")
}
// We deal with the structures defined in this package first.
switch fieldType {
case BitStringType:
bitString, err1 := pd.parseBitString(sizeExtensible, params.sizeLowerBound, params.sizeUpperBound)
if err1 != nil {
return err1
}
v.Field(3).Set(reflect.ValueOf(bitString.Value))
v.Field(4).Set(reflect.ValueOf(bitString.Len))
return nil
case reflect.TypeOf([]uint8{}):
octetString, err := pd.parseOctetString(sizeExtensible, params.sizeLowerBound, params.sizeUpperBound)
if err != nil {
return err
}
v.Set(reflect.ValueOf(octetString))
return nil
default:
log.Debugf("not a built in field type %v", fieldType)
}
switch val := v; val.Kind() {
case reflect.Bool:
parsedBool, err := pd.parseBool()
if err != nil {
return err
}
val.SetBool(parsedBool)
return nil
case reflect.Int, reflect.Int32, reflect.Int64:
parsedInt, err := pd.parseInteger(valueExtensible, params.valueLowerBound, params.valueUpperBound)
if err != nil {
return err
}
val.SetInt(parsedInt)
log.Debugf("Decoded INTEGER Value: %d", parsedInt)
if params.unique || pd.uniqueStructFlag {
pd.unique = parsedInt
log.Debugf("UNIQUE flag was found, it is %v", pd.unique)
pd.uniqueStructFlag = false // setting this flag back to false
}
return nil
case reflect.Float64:
parsedReal, err := pd.parseReal(params.valueLowerBound, params.valueUpperBound, params.valueExtensible)
if err != nil {
return err
}
val.SetFloat(parsedReal)
log.Debugf("Decoded REAL Value: %d", parsedReal)
return nil
case reflect.Struct:
structType := fieldType
var structParams []fieldParameters
var optionalCount uint
var optionalPresents uint64
extensionHeaderParsed := false
var itemsInExtensionPresents uint64
var totalNumberOfItemsInExtension int
// pass tag for optional
fieldIdx := -1
for i := 0; i < structType.NumField(); i++ {
if structType.Field(i).PkgPath != "" {
log.Debugf("struct %s ignoring unexported field : %s", structType.Name(), structType.Field(i).Name)
continue
}
fieldIdx++
tempParams := parseFieldParameters(structType.Field(i).Tag.Get("aper"))
tempParams.oneofName = structType.Field(i).Tag.Get("protobuf_oneof")
if tempParams.valueExtensible {
log.Debugf("SEQUENCE %v can be extended", structType.Field(i).Type)
}
// for optional flag
if tempParams.optional && !tempParams.fromValueExt { // OPTIONAL items from SEQUENCE extension does not count
optionalCount++
}
structParams = append(structParams, tempParams)
}
log.Debugf("optionalCount is %d", optionalCount)
if optionalCount > 0 {
optionalPresentsTmp, err := pd.getBitsValue(optionalCount)
if err != nil {
return err
}
optionalPresents = optionalPresentsTmp
log.Debugf("optionalPresents is %0b", optionalPresents)
}
fieldIdx = -1
for i := 0; i < structType.NumField(); i++ {
if structType.Field(i).PkgPath != "" {
log.Debugf("struct %s ignoring unexported field : %s", structType.Name(), structType.Field(i).Name)
continue
}
fieldIdx++
if structParams[fieldIdx].optional && optionalCount > 0 {
optionalCount--
if optionalPresents&(1<<optionalCount) == 0 {
log.Debugf("Field \"%s\" in %s is OPTIONAL and not present", structType.Field(i).Name, structType)
continue // skipping this iteration
}
log.Debugf("Field \"%s\" in %s is OPTIONAL and present", structType.Field(i).Name, structType)
}
// if no UNIQUE flag, then regular processing..
if structParams[fieldIdx].unique {
log.Debugf("UNIQUE flag was found for %v, %v", structType.Field(i).Name, structType.Field(i).Type)
// setting this flag to true to indicate that the structure carries unique flag
// assumption is that this structure contains only one int32 field..
pd.uniqueStructFlag = true
}
if structParams[fieldIdx].fromValueExt && valueExtensible { // extensible bit is present
if !extensionHeaderParsed {
log.Debugf("Decoding SEQUENCE Extension header")