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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
|
/* packet-csn1.c
* Routines for CSN1 dissection in wireshark.
* By Vincent Helfre, based on original code by Jari Sassi
* with the gracious authorization of STE
* Copyright (c) 2011 ST-Ericsson
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <epan/packet.h>
#include <epan/to_str.h>
#include "packet-csn1.h"
void proto_register_csn1(void);
static int hf_null_data = -1;
static expert_field ei_csn1_more_bits_to_unpack = EI_INIT;
static expert_field ei_csn1_general = EI_INIT;
static expert_field ei_csn1_not_implemented = EI_INIT;
static expert_field ei_csn1_union_index = EI_INIT;
static expert_field ei_csn1_script_error = EI_INIT;
static expert_field ei_csn1_more32bits = EI_INIT;
static expert_field ei_csn1_fixed_not_matched = EI_INIT;
static expert_field ei_csn1_stream_not_supported = EI_INIT;
#define pvDATA(_pv, _offset) ((void*) ((unsigned char*)_pv + _offset))
#define pui8DATA(_pv, _offset) ((guint8*) pvDATA(_pv, _offset))
#define pui16DATA(_pv, _offset) ((guint16*) pvDATA(_pv, _offset))
#define pui32DATA(_pv, _offset) ((guint32*) pvDATA(_pv, _offset))
/* used to tag existence of next element in variable length lists */
#define STANDARD_TAG 1
#define REVERSED_TAG 0
static const unsigned char ixBitsTab[] = {0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5};
static gint proto_csn1 = -1;
/* Returns no_of_bits (up to 8) masked with 0x2B */
static guint8
tvb_get_masked_bits8(tvbuff_t *tvb, gint bit_offset, const gint no_of_bits)
{
static const guint8 maskBits[] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF};
gint byte_offset = bit_offset >> 3; /* divide by 8 */
gint relative_bit_offset = bit_offset & 0x07; /* modulo 8 */
guint8 result;
gint bit_shift = 8 - relative_bit_offset - (gint) no_of_bits;
if (bit_shift >= 0)
{
result = (0x2B ^ tvb_get_guint8(tvb, byte_offset)) >> bit_shift;
result &= maskBits[no_of_bits];
}
else
{
guint8 hight_part = (0x2B ^ tvb_get_guint8(tvb, byte_offset)) & maskBits[8 - relative_bit_offset];
hight_part = (guint8) (hight_part << (-bit_shift));
result = (0x2B ^ tvb_get_guint8(tvb, byte_offset+1)) >> (8 + bit_shift);
result |= hight_part;
}
return result;
}
/**
* ================================================================================================
* set initial/start values in help data structure used for packing/unpacking operation
* ================================================================================================
*/
void
csnStreamInit(csnStream_t* ar, gint bit_offset, gint remaining_bits_len, packet_info* pinfo)
{
ar->remaining_bits_len = remaining_bits_len;
ar->bit_offset = bit_offset;
ar->pinfo = pinfo;
}
static gint16
ProcessError(proto_tree *tree, packet_info* pinfo, tvbuff_t *tvb, gint bit_offset, gint16 err, expert_field* err_field, const CSN_DESCR* pDescr)
{
if (err_field != NULL)
proto_tree_add_expert_format(tree, pinfo, err_field, tvb, bit_offset>>3, 1, "%s (%s)", expert_get_summary(err_field), pDescr?pDescr->sz:"-");
return err;
}
#if 0
static const char* CSN_DESCR_type[]=
{
"CSN_END",
"CSN_BIT",
"CSN_UINT",
"CSN_TYPE",
"CSN_CHOICE",
"CSN_UNION",
"CSN_UNION_LH",
"CSN_UINT_ARRAY",
"CSN_TYPE_ARRAY",
"CSN_BITMAP",
"CSN_VARIABLE_BITMAP",
"CSN_VARIABLE_BITMAP_1",
"CSN_LEFT_ALIGNED_VAR_BMP",
"CSN_LEFT_ALIGNED_VAR_BMP_1",
"CSN_VARIABLE_ARRAY",
"CSN_VARIABLE_TARRAY",
"CSN_VARIABLE_TARRAY_OFFSET",
"CSN_RECURSIVE_ARRAY",
"CSN_RECURSIVE_TARRAY",
"CSN_RECURSIVE_TARRAY_1",
"CSN_RECURSIVE_TARRAY_2",
"CSN_EXIST",
"CSN_EXIST_LH",
"CSN_NEXT_EXIST",
"CSN_NEXT_EXIST_LH",
"CSN_NULL",
"CSN_FIXED",
"CSN_CALLBACK",
"CSN_UINT_OFFSET",
"CSN_UINT_LH",
"CSN_SERIALIZE",
"CSN_TRAP_ERROR"
"CSN_???"
};
#endif
/**
* ================================================================================================
* Return TRUE if tag in bit stream indicates existence of next list element,
* otherwise return FALSE.
* Will work for tag values equal to both 0 and 1.
* ================================================================================================
*/
static gboolean
existNextElement(tvbuff_t *tvb, gint bit_offset, guint8 Tag)
{
guint8 res = tvb_get_bits8(tvb, bit_offset, 1);
if (Tag == STANDARD_TAG)
{
return (res > 0);
}
return (res == 0);
}
gint16
// NOLINTNEXTLINE(misc-no-recursion)
csnStreamDissector(proto_tree *tree, csnStream_t* ar, const CSN_DESCR* pDescr, tvbuff_t *tvb, void* data, int ett_csn1)
{
gint remaining_bits_len = ar->remaining_bits_len;
gint bit_offset = ar->bit_offset;
guint8* pui8 = NULL;
guint16* pui16;
guint32* pui32;
guint8 Tag = STANDARD_TAG;
/* Negative number definitely indicates an error */
if (remaining_bits_len < 0)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
do
{
switch (pDescr->type)
{
case CSN_BIT:
{
if (remaining_bits_len > 0)
{
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = tvb_get_bits8(tvb, bit_offset, 1);
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, 1, ENC_BIG_ENDIAN);
/* end add the bit value to protocol tree */
}
else if(pDescr->may_be_null)
{
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = 0;
proto_tree_add_none_format(tree, hf_null_data, tvb, 0, 0, "[NULL data]: %s Not Present", proto_registrar_get_name(*(pDescr->hf_ptr)));
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
pDescr++;
remaining_bits_len--;
bit_offset++;
break;
}
case CSN_NULL:
{ /* Empty member! */
bit_offset += pDescr->i;
pDescr++;
break;
}
case CSN_UINT:
{
guint8 no_of_bits = (guint8) pDescr->i;
if (remaining_bits_len >= no_of_bits)
{
if (no_of_bits <= 8)
{
guint8 ui8 = tvb_get_bits8(tvb, bit_offset, no_of_bits);
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = ui8;
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else if (no_of_bits <= 16)
{
guint16 ui16 = tvb_get_bits16(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
pui16 = pui16DATA(data, pDescr->offset);
memcpy(pui16, &ui16, 2);
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else if (no_of_bits <= 32)
{
guint32 ui32 = tvb_get_bits32(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
pui32 = pui32DATA(data, pDescr->offset);
memcpy(pui32, &ui32, 4);
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
remaining_bits_len -= no_of_bits;
bit_offset += no_of_bits;
}
else if(pDescr->may_be_null)
{
if (no_of_bits <= 8)
{
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = 0;
}
else if (no_of_bits <= 16)
{
pui16 = pui16DATA(data, pDescr->offset);
memset(pui16, 0, 2);
}
else if (no_of_bits <= 32)
{
pui32 = pui32DATA(data, pDescr->offset);
memset(pui32, 0, 4);
}
proto_tree_add_none_format(tree, hf_null_data, tvb, 0, 0, "[NULL data]: %s Not Present", proto_registrar_get_name(*(pDescr->hf_ptr)));
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
pDescr++;
break;
}
case CSN_UINT_OFFSET:
{
guint8 no_of_bits = (guint8) pDescr->i;
if (remaining_bits_len >= no_of_bits)
{
if (no_of_bits <= 8)
{
guint8 ui8 = tvb_get_bits8(tvb, bit_offset, no_of_bits);
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = ui8 + (guint8)pDescr->descr.value;
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits,
*pui8, ENC_BIG_ENDIAN, "%u (Raw %u + Offset %u)", *pui8, ui8,
(guint8) pDescr->descr.value);
}
else if (no_of_bits <= 16)
{
guint16 ui16 = tvb_get_bits16(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN) + (guint16)pDescr->descr.value;
pui16 = pui16DATA(data, pDescr->offset);
memcpy(pui16, &ui16, 2);
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits,
*pui16, ENC_BIG_ENDIAN, "%u (Raw %u + Offset %u)", *pui16, ui16,
(guint16) pDescr->descr.value);
}
else if (no_of_bits <= 32)
{
guint32 ui32 = tvb_get_bits32(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN) + (guint16)pDescr->descr.value;
pui32 = pui32DATA(data, pDescr->offset);
memcpy(pui32, &ui32, 4);
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits,
*pui32, ENC_BIG_ENDIAN, "%u (Raw %u + Offset %u)", *pui32, ui32,
(guint16) pDescr->descr.value);
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
remaining_bits_len -= no_of_bits;
bit_offset += no_of_bits;
pDescr++;
break;
}
case CSN_UINT_LH:
{
guint8 no_of_bits = (guint8) pDescr->i;
if (remaining_bits_len >= no_of_bits)
{
if (no_of_bits <= 8)
{
guint8 ui8 = tvb_get_masked_bits8(tvb, bit_offset, no_of_bits);
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = ui8;
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else
{/* Maybe we should support more than 8 bits ? */
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
remaining_bits_len -= no_of_bits;
bit_offset += no_of_bits;
pDescr++;
break;
}
case CSN_SPLIT_BITS:
{
guint8 no_of_value_bits = (guint8) pDescr->i;
guint64 value;
proto_tree_add_split_bits_item_ret_val(tree, *pDescr->hf_ptr, tvb, bit_offset, pDescr->descr.crumb_spec, &value);
if (no_of_value_bits <= 8)
{
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = (guint8)value;
}
else if (no_of_value_bits <= 16)
{
guint16 ui16 = (guint16) value;
pui16 = pui16DATA(data, pDescr->offset);
memcpy(pui16, &ui16, 2);
}
else if (no_of_value_bits <= 32)
{
guint32 ui32 = (guint32) value;
pui32 = pui32DATA(data, pDescr->offset);
memcpy(pui32, &ui32, 4);
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
pDescr++;
break;
}
case CSN_SPLIT_BITS_CRUMB:
{
if (remaining_bits_len >= pDescr->descr.crumb_spec[pDescr->i].crumb_bit_length)
{
proto_tree_add_split_bits_crumb(tree, *pDescr->hf_ptr, tvb, bit_offset,
pDescr->descr.crumb_spec, pDescr->i);
remaining_bits_len -= pDescr->descr.crumb_spec[pDescr->i].crumb_bit_length;
bit_offset += pDescr->descr.crumb_spec[pDescr->i].crumb_bit_length;
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
pDescr++;
break;
}
case CSN_UINT_ARRAY:
{
guint8 no_of_bits = (guint8) pDescr->i;
guint16 nCount = (guint16)pDescr->descr.value; /* nCount supplied by value i.e. M_UINT_ARRAY(...) */
int i = 0;
if (pDescr->value != 0)
{ /* nCount specified by a reference to field holding value i.e. M_VAR_UINT_ARRAY(...) */
memcpy(&nCount, pui16DATA(data, nCount), 2);
}
if (remaining_bits_len >= (no_of_bits * nCount))
{
remaining_bits_len -= (no_of_bits * nCount);
if (no_of_bits <= 8)
{
pui8 = pui8DATA(data, pDescr->offset);
do
{
*pui8++ = tvb_get_bits8(tvb, bit_offset, no_of_bits);
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, *pui8, ENC_BIG_ENDIAN, " (Count %d)", i++);
bit_offset += no_of_bits;
} while (--nCount > 0);
}
else if (no_of_bits <= 16)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, 999, &ei_csn1_not_implemented, pDescr);
}
else if (no_of_bits <= 32)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, 999, &ei_csn1_not_implemented, pDescr);
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
pDescr++;
break;
}
case CSN_VARIABLE_TARRAY_OFFSET:
case CSN_VARIABLE_TARRAY:
case CSN_TYPE_ARRAY:
{
gint16 Status;
csnStream_t arT = *ar;
gint16 nCount = pDescr->i;
guint16 nSize = (guint16)(gint32)pDescr->value;
int i =0;
pui8 = pui8DATA(data, pDescr->offset);
if (pDescr->type == CSN_VARIABLE_TARRAY)
{ /* Count specified in field */
nCount = *pui8DATA(data, pDescr->i);
}
else if (pDescr->type == CSN_VARIABLE_TARRAY_OFFSET)
{ /* Count specified in field */
nCount = *pui8DATA(data, pDescr->i);
/* nCount--; the 1 offset is already taken into account in CSN_UINT_OFFSET */
}
while (nCount > 0)
{ /* resulting array of length 0 is possible
* but no bits shall be read from bitstream
*/
proto_item *ti;
proto_tree *test_tree;
test_tree = proto_tree_add_subtree_format(tree, tvb, bit_offset>>3, 1, ett_csn1, &ti, "%s[%d]",pDescr->sz, i++);
csnStreamInit(&arT, bit_offset, remaining_bits_len, ar->pinfo);
increment_dissection_depth(ar->pinfo);
Status = csnStreamDissector(test_tree, &arT, (const CSN_DESCR*)pDescr->descr.ptr, tvb, pui8, ett_csn1);
decrement_dissection_depth(ar->pinfo);
if (Status >= 0)
{
pui8 += nSize;
proto_item_set_len(ti,((arT.bit_offset-1)>>3) - (bit_offset>>3)+1);
remaining_bits_len = arT.remaining_bits_len;
bit_offset = arT.bit_offset;
}
else
{
return Status;
}
nCount--;
}
pDescr++;
break;
}
case CSN_BITMAP:
{ /* bitmap with given length. The result is left aligned! */
guint8 no_of_bits = (guint8) pDescr->i; /* length of bitmap */
if (no_of_bits > 0)
{
if (no_of_bits > remaining_bits_len)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
if (no_of_bits <= 32)
{
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else if (no_of_bits <= 64)
{
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, 999, &ei_csn1_not_implemented, pDescr);
}
remaining_bits_len -= no_of_bits;
bit_offset += no_of_bits;
}
/* bitmap was successfully extracted or it was empty */
pDescr++;
break;
}
case CSN_TYPE:
{
gint16 Status;
csnStream_t arT = *ar;
proto_item *ti;
proto_tree *test_tree;
if (pDescr->may_be_null && remaining_bits_len == 0)
{
proto_tree_add_none_format(tree, hf_null_data, tvb, 0, 0, "[NULL data]: %s Not Present", pDescr->sz);
} else {
test_tree = proto_tree_add_subtree_format(tree, tvb, bit_offset>>3, 1, ett_csn1, &ti, "%s", pDescr->sz);
csnStreamInit(&arT, bit_offset, remaining_bits_len, ar->pinfo);
increment_dissection_depth(ar->pinfo);
Status = csnStreamDissector(test_tree, &arT, (const CSN_DESCR*)pDescr->descr.ptr, tvb, pvDATA(data, pDescr->offset), ett_csn1);
decrement_dissection_depth(ar->pinfo);
if (Status >= 0)
{
proto_item_set_len(ti,((arT.bit_offset-1)>>3) - (bit_offset>>3)+1);
remaining_bits_len = arT.remaining_bits_len;
bit_offset = arT.bit_offset;
}
else
{
/* Has already been processed: ProcessError("csnStreamDissector", Status, pDescr); */
return Status;
}
}
pDescr++;
break;
}
case CSN_CHOICE:
{
gint16 count = pDescr->i;
guint8 i = 0;
const CSN_ChoiceElement_t* pChoice = (const CSN_ChoiceElement_t*) pDescr->descr.ptr;
/* Make sure that the list of choice items is not empty */
if (!count)
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_IN_SCRIPT, &ei_csn1_script_error, pDescr);
while (count > 0)
{
guint8 no_of_bits = pChoice->bits;
guint8 value = tvb_get_bits8(tvb, bit_offset, no_of_bits);
if (value == pChoice->value)
{
CSN_DESCR descr[2];
gint16 Status;
csnStream_t arT = *ar;
proto_item *ti = NULL;
proto_tree *test_tree;
descr[0] = pChoice->descr;
memset(&descr[1], 0x00, sizeof(CSN_DESCR));
descr[1].type = CSN_END;
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = i;
if (pDescr->sz)
{
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
if (!pChoice->keep_bits)
{
bit_offset += no_of_bits;
remaining_bits_len -= no_of_bits;
}
if (pDescr->sz)
{
test_tree = proto_tree_add_subtree(tree, tvb, bit_offset>>3, 1, ett_csn1, &ti, pDescr->sz);
} else {
test_tree = tree;
}
csnStreamInit(&arT, bit_offset, remaining_bits_len, ar->pinfo);
increment_dissection_depth(ar->pinfo);
Status = csnStreamDissector(test_tree, &arT, (const CSN_DESCR*)descr, tvb, data, ett_csn1);
decrement_dissection_depth(ar->pinfo);
if (Status >= 0)
{
if (ti)
proto_item_set_len(ti,((arT.bit_offset-1)>>3) - (bit_offset>>3)+1);
remaining_bits_len = arT.remaining_bits_len;
bit_offset = arT.bit_offset;
}
else
{
return Status;
}
break;
}
count--;
pChoice++;
i++;
}
/* Neither of the choice items matched => unknown value */
if (!count) {
return ProcessError(tree, ar->pinfo, tvb, bit_offset,
CSN_ERROR_STREAM_NOT_SUPPORTED,
&ei_csn1_stream_not_supported, pDescr);
}
pDescr++;
break;
}
case CSN_SERIALIZE:
{
StreamSerializeFcn_t serialize = (StreamSerializeFcn_t)pDescr->aux_fn;
csnStream_t arT = *ar;
guint length_len = pDescr->i;
gint16 Status = -1;
proto_item *ti;
proto_tree *test_tree;
guint8 length = 0;
if (length_len)
{
length = tvb_get_bits8(tvb, bit_offset, length_len);
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, length_len, ENC_BIG_ENDIAN);
bit_offset += length_len;
remaining_bits_len -= length_len;
test_tree = proto_tree_add_subtree(tree, tvb, bit_offset>>3, ((bit_offset+length-1)>>3)-(bit_offset>>3) + 1, ett_csn1, &ti, pDescr->sz);
} else {
test_tree = proto_tree_add_subtree(tree, tvb, bit_offset>>3, 1, ett_csn1, &ti, pDescr->sz);
}
csnStreamInit(&arT, bit_offset, length > 0 ? length : remaining_bits_len, ar->pinfo);
Status = serialize(test_tree, &arT, tvb, pvDATA(data, pDescr->offset), ett_csn1);
if (Status >= 0)
{
if (length > 0) {
remaining_bits_len -= length;
bit_offset += length;
} else {
proto_item_set_len(ti,((arT.bit_offset - bit_offset)>>3)+1);
remaining_bits_len = arT.remaining_bits_len;
bit_offset = arT.bit_offset;
}
pDescr++;
}
else
{
/* Has already been processed: */
return Status;
}
break;
}
case CSN_UNION_LH:
case CSN_UNION:
{
gint16 Bits;
guint8 t_index;
gint16 count = pDescr->i;
const CSN_DESCR* pDescrNext = pDescr;
pDescrNext += count + 1; /* now this is next after the union */
if ((count <= 0) || (count > 16))
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_INVALID_UNION_INDEX, &ei_csn1_union_index, pDescr);
}
/* Now get the bits to extract the index */
Bits = ixBitsTab[count];
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, Bits, tvb_get_bits8(tvb, bit_offset, Bits), ENC_BIG_ENDIAN, " (Union)");
t_index = 0;
while (Bits > 0)
{
t_index <<= 1;
if (CSN_UNION_LH == pDescr->type)
{
t_index |= tvb_get_masked_bits8(tvb, bit_offset, 1);
}
else
{
t_index |= tvb_get_bits8(tvb, bit_offset, 1);
}
remaining_bits_len--;
bit_offset++;
Bits--;
}
/* Assign UnionType */
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = t_index;
/* script index to continue on, limited in case we do not have a power of 2 */
pDescr += (MIN(t_index + 1, count));
switch (pDescr->type)
{ /* get the right element of the union based on computed index */
case CSN_BIT:
{
pui8 = pui8DATA(data, pDescr->offset);
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, 1, ENC_BIG_ENDIAN);
*pui8 = 0x00;
if (tvb_get_bits8(tvb, bit_offset, 1) > 0)
{
*pui8 = 0x01;
}
remaining_bits_len --;
bit_offset++;
pDescr++;
break;
}
case CSN_NULL:
{ /* Empty member! */
bit_offset += pDescr->i;
pDescr++;
break;
}
case CSN_UINT:
{
guint8 no_of_bits = (guint8) pDescr->i;
if (remaining_bits_len >= no_of_bits)
{
if (no_of_bits <= 8)
{
guint8 ui8 = tvb_get_bits8(tvb, bit_offset, no_of_bits);
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = ui8;
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else if (no_of_bits <= 16)
{
guint16 ui16 = tvb_get_bits16(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
pui16 = pui16DATA(data, pDescr->offset);
memcpy(pui16, &ui16, 2);
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else if (no_of_bits <= 32)
{
guint32 ui32 = tvb_get_bits32(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
pui32 = pui32DATA(data, pDescr->offset);
memcpy(pui32, &ui32, 4);
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
remaining_bits_len -= no_of_bits;
bit_offset += no_of_bits;
pDescr++;
break;
}
case CSN_UINT_OFFSET:
{
guint8 no_of_bits = (guint8) pDescr->i;
if (remaining_bits_len >= no_of_bits)
{
if (no_of_bits <= 8)
{
guint8 ui8 = tvb_get_bits8(tvb, bit_offset, no_of_bits);
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = ui8 + (guint8)pDescr->descr.value;
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ui8, ENC_BIG_ENDIAN, "%d", ui8);
}
else if (no_of_bits <= 16)
{
guint16 ui16 = tvb_get_bits16(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN) + (guint16)pDescr->descr.value;
pui16 = pui16DATA(data, pDescr->offset);
memcpy(pui16, &ui16, 2);
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ui16, ENC_BIG_ENDIAN, "%d", ui16);
}
else if (no_of_bits <= 32)
{
guint32 ui32 = tvb_get_bits32(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN) + (guint16)pDescr->descr.value;
pui32 = pui32DATA(data, pDescr->offset);
memcpy(pui32, &ui32, 4);
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ui32, ENC_BIG_ENDIAN, "%d", ui32);
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
remaining_bits_len -= no_of_bits;
bit_offset += no_of_bits;
pDescr++;
break;
}
case CSN_UINT_LH:
{
guint8 no_of_bits = (guint8) pDescr->i;
if (remaining_bits_len >= no_of_bits)
{
if (no_of_bits <= 8)
{
guint8 ui8 = tvb_get_masked_bits8(tvb, bit_offset, no_of_bits);
pui8 = pui8DATA(data, pDescr->offset);
*pui8 = ui8;
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else
{ /* Maybe we should support more than 8 bits ? */
ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
remaining_bits_len -= no_of_bits;
bit_offset += no_of_bits;
pDescr++;
break;
}
case CSN_UINT_ARRAY:
{
guint8 no_of_bits = (guint8) pDescr->i;
guint16 nCount = (guint16)pDescr->descr.value; /* nCount supplied by value i.e. M_UINT_ARRAY(...) */
gint i = 0;
if (pDescr->value != 0)
{ /* nCount specified by a reference to field holding value i.e. M_VAR_UINT_ARRAY(...) */
memcpy(&nCount, pui16DATA(data, nCount), 2);
}
if (remaining_bits_len >= (no_of_bits * nCount))
{
remaining_bits_len -= (no_of_bits * nCount);
if (no_of_bits <= 8)
{
pui8 = pui8DATA(data, pDescr->offset);
while (nCount > 0)
{
*pui8 = tvb_get_bits8(tvb, bit_offset, no_of_bits);
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, *pui8, ENC_BIG_ENDIAN, " (Count %d)", i++);
pui8++;
bit_offset += no_of_bits;
nCount--;
}
}
else if (no_of_bits <= 16)
{
pui16 = pui16DATA(data, pDescr->offset);
while (nCount > 0)
{
guint16 ui16;
ui16 = tvb_get_bits16(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ui16, ENC_BIG_ENDIAN, " (Count %d)", i++);
memcpy(pui16++, &ui16, sizeof(ui16));
bit_offset += no_of_bits;
nCount--;
}
}
else if (no_of_bits <= 32)
{ /* not supported */
return ProcessError(tree, ar->pinfo, tvb, bit_offset, 999, &ei_csn1_not_implemented, pDescr);
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_GENERAL, &ei_csn1_general, pDescr);
}
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
pDescr++;
break;
}
case CSN_VARIABLE_TARRAY_OFFSET:
case CSN_VARIABLE_TARRAY:
case CSN_TYPE_ARRAY:
{
gint16 Status;
csnStream_t arT = *ar;
guint16 nCount = (guint16) pDescr->i;
guint16 nSize = (guint16)(guint32)pDescr->value;
gint i = 0;
pui8 = pui8DATA(data, pDescr->offset);
if (CSN_VARIABLE_TARRAY == pDescr->type)
{ /* Count specified in field */
nCount = *pui8DATA(data, pDescr->i);
}
else if (CSN_VARIABLE_TARRAY_OFFSET == pDescr->type)
{ /* Count specified in field */
nCount = *pui8DATA(data, pDescr->i);
nCount--; /* Offset 1 */
}
while (nCount--) /* Changed to handle length = 0. */
{
proto_item *ti;
proto_tree *test_tree;
test_tree = proto_tree_add_subtree_format(tree, tvb, bit_offset>>3, 1, ett_csn1, &ti, "%s[%d]",pDescr->sz, i++);
csnStreamInit(&arT, bit_offset, remaining_bits_len, ar->pinfo);
increment_dissection_depth(ar->pinfo);
Status = csnStreamDissector(test_tree, &arT, (const CSN_DESCR *)pDescr->descr.ptr, tvb, pui8, ett_csn1);
decrement_dissection_depth(ar->pinfo);
if (Status >= 0)
{
pui8 += nSize;
proto_item_set_len(ti,((arT.bit_offset-1)>>3) - (bit_offset>>3)+1);
remaining_bits_len = arT.remaining_bits_len;
bit_offset = arT.bit_offset;
}
else
{
return Status;
}
}
pDescr++;
break;
}
case CSN_BITMAP:
{ /* bitmap with given length. The result is left aligned! */
guint8 no_of_bits = (guint8) pDescr->i; /* length of bitmap */
if (no_of_bits > 0)
{ /* a non empty bitmap */
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
remaining_bits_len -= no_of_bits;
bit_offset += no_of_bits;
if (remaining_bits_len < 0)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
}
/* bitmap was successfully extracted or it was empty */
pDescr++;
break;
}
case CSN_TYPE:
{
gint16 Status;
csnStream_t arT = *ar;
proto_item *ti;
proto_tree *test_tree;
if (pDescr->may_be_null && remaining_bits_len == 0)
{
proto_tree_add_none_format(tree, hf_null_data, tvb, 0, 0, "[NULL data]: %s Not Present", pDescr->sz);
} else {
test_tree = proto_tree_add_subtree(tree, tvb, bit_offset>>3, 1, ett_csn1, &ti, pDescr->sz);
csnStreamInit(&arT, bit_offset, remaining_bits_len, ar->pinfo);
increment_dissection_depth(ar->pinfo);
Status = csnStreamDissector(test_tree, &arT, (const CSN_DESCR *)pDescr->descr.ptr, tvb, pvDATA(data, pDescr->offset), ett_csn1);
decrement_dissection_depth(ar->pinfo);
if (Status >= 0)
{
proto_item_set_len(ti,((arT.bit_offset-1)>>3) - (bit_offset>>3)+1);
remaining_bits_len = arT.remaining_bits_len;
bit_offset = arT.bit_offset;
}
else
{ /* return error code Has already been processed: */
return Status;
}
}
pDescr++;
break;
}
default:
{ /* descriptions of union elements other than above are illegal */
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_IN_SCRIPT, &ei_csn1_script_error, pDescr);
}
}
pDescr = pDescrNext;
break;
}
case CSN_EXIST:
case CSN_EXIST_LH:
{
guint8 fExist;
pui8 = pui8DATA(data, pDescr->offset);
if (CSN_EXIST_LH == pDescr->type)
{
fExist = tvb_get_masked_bits8(tvb, bit_offset, 1);
proto_tree_add_uint(tree, *(pDescr->hf_ptr), tvb, bit_offset>>3, 1, fExist);
}
else
{
fExist = tvb_get_bits8(tvb, bit_offset, 1);
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, 1, ENC_BIG_ENDIAN);
}
*pui8 = fExist;
remaining_bits_len --;
bit_offset++;
pDescr++;
if (!fExist)
{
ar->remaining_bits_len = remaining_bits_len;
ar->bit_offset = bit_offset;
return remaining_bits_len;
}
break;
}
case CSN_NEXT_EXIST:
{
guint8 isnull;
pui8 = pui8DATA(data, pDescr->offset);
/* this if-statement represents the M_NEXT_EXIST_OR_NULL description element */
if ((pDescr->may_be_null) && (remaining_bits_len == 0))
{ /* no more bits to decode is fine here - end of message detected and allowed */
/* Skip i entries + this entry */
pDescr += pDescr->i + 1;
/* Set the data member to "not exist" */
*pui8 = 0;
break;
}
/* the "regular" M_NEXT_EXIST description element */
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, 1, ENC_BIG_ENDIAN);
isnull = 1;
if (tvb_get_bits8(tvb, bit_offset, 1))
{
if (remaining_bits_len == 1)
{
/* If { 1 < end > } and all next items may be null, store it as { 0 } */
const CSN_DESCR* pDescrNext = pDescr + 1;
guint8 i;
for (i = 0; i < pDescr->i; i++, pDescrNext++)
{
if (!pDescrNext->may_be_null)
isnull = 0;
}
} else {
isnull = 0;
}
}
*pui8 = isnull ? 0 : 1;
remaining_bits_len --;
bit_offset++;
if (isnull)
{ /* Skip 'i' entries */
pDescr += pDescr->i;
}
pDescr++;
break;
}
case CSN_NEXT_EXIST_LH:
{
guint8 isnull;
pui8 = pui8DATA(data, pDescr->offset);
/* this if-statement represents the M_NEXT_EXIST_OR_NULL_LH description element */
if ((pDescr->descr.ptr != NULL) && (remaining_bits_len == 0))
{ /* no more bits to decode is fine here - end of message detected and allowed */
/* skip 'i' entries + this entry */
pDescr += pDescr->i + 1;
/* set the data member to "not exist" */
*pui8 = 0;
break;
}
/* the "regular" M_NEXT_EXIST_LH description element */
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, 1, ENC_BIG_ENDIAN);
isnull = 1;
if (tvb_get_masked_bits8(tvb, bit_offset, 1))
{
if (remaining_bits_len == 1) {
/* If { 1 < end > } and all next items may be null, store it as { 0 } */
const CSN_DESCR* pDescrNext = pDescr + 1;
guint8 i;
for (i = 0; i < pDescr->i; i++, pDescrNext++)
{
if (!pDescrNext->may_be_null)
isnull = 0;
}
} else {
isnull = 0;
}
}
*pui8++ = isnull ? 0 : 1;
remaining_bits_len --;
bit_offset++;
if (isnull)
{ /* Skip 'i' entries */
pDescr += pDescr->i;
}
pDescr++;
break;
}
case CSN_VARIABLE_BITMAP_1:
{ /* Bitmap from here and to the end of message */
*pui8DATA(data, (gint16)pDescr->descr.value) = (guint8) remaining_bits_len; /* length of bitmap == remaining bits */
/*no break -
* with a length set we have a regular variable length bitmap so we continue */
}
/* FALL THROUGH */
case CSN_VARIABLE_BITMAP:
{ /* {CSN_VARIABLE_BITMAP, 0, offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER}
* <N: bit (5)> <bitmap: bit(N + offset)>
* Bit array with length (in bits) specified in parameter (pDescr->descr)
* The result is right aligned!
*/
gint16 no_of_bits = *pui8DATA(data, (gint16)pDescr->descr.value);
no_of_bits += pDescr->i; /* adjusted by offset */
while (no_of_bits > 0)
{
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, 1, ENC_BIG_ENDIAN);
bit_offset++;
no_of_bits--;
remaining_bits_len--;
if (remaining_bits_len < 0)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
}
pDescr++;
break;
}
case CSN_LEFT_ALIGNED_VAR_BMP_1:
{ /* Bitmap from here and to the end of message */
*pui8DATA(data, (gint16)pDescr->descr.value) = (guint8) remaining_bits_len; /* length of bitmap == remaining bits */
/* no break -
* with a length set we have a regular left aligned variable length bitmap so we continue
*/
}
/* FALL THROUGH */
case CSN_LEFT_ALIGNED_VAR_BMP:
{ /* {CSN_LEFT_ALIGNED_VAR_BMP, _OFFSET, (void*)offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER}
* <N: bit (5)> <bitmap: bit(N + offset)>
* bit array with length (in bits) specified in parameter (pDescr->descr)
*/
gint16 no_of_bits = *pui8DATA(data, (gint16)pDescr->descr.value);/* Size of bitmap */
no_of_bits += pDescr->i;/* size adjusted by offset */
if (no_of_bits > 0)
{
if (no_of_bits <= 32)
{
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else if (no_of_bits <= 64)
{
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else
{
proto_tree_add_uint64_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset>>3, ((bit_offset+no_of_bits-1)>>3)-(bit_offset>>3)+1, no_of_bits, "%u bits",
no_of_bits);
}
bit_offset += no_of_bits;
remaining_bits_len -= no_of_bits;
if (remaining_bits_len < 0)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
}
/* bitmap was successfully extracted or it was empty */
pDescr++;
break;
}
case CSN_PADDING_BITS:
{ /* Padding from here and to the end of message */
if (remaining_bits_len > 0)
{
proto_tree *padding_tree = proto_tree_add_subtree(tree, tvb, bit_offset>>3, -1, ett_csn1, NULL, "Padding Bits");
while (remaining_bits_len > 0)
{
gint bits_to_handle = remaining_bits_len + (bit_offset%8);
if (bits_to_handle > 32)
{
bits_to_handle = 32 - (bit_offset%8);
}
else
{
bits_to_handle -= (bit_offset%8);
}
proto_tree_add_bits_item(padding_tree, *(pDescr->hf_ptr), tvb, bit_offset, bits_to_handle, ENC_BIG_ENDIAN);
bit_offset += bits_to_handle;
remaining_bits_len -= bits_to_handle;
}
}
if (remaining_bits_len < 0)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
/* Padding was successfully extracted or it was empty */
pDescr++;
break;
}
case CSN_VARIABLE_ARRAY:
{ /* {int type; int i; void* descr; int offset; const char* sz; } CSN_DESCR;
* {CSN_VARIABLE_ARRAY, _OFFSET, (void*)offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER}
* Array with length specified in parameter:
* <count: bit (x)>
* <list: octet(count + offset)>
*/
gint16 count = *pui8DATA(data, (gint16)pDescr->descr.value);
count += pDescr->i; /* Adjusted by offset */
if (count > 0)
{
pui8 = pui8DATA(data, pDescr->offset);
while (count > 0)
{
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, 8, ENC_BIG_ENDIAN);
*pui8++ = tvb_get_bits8(tvb, bit_offset, 8);
bit_offset += 8;
count--;
remaining_bits_len -= 8;
if (remaining_bits_len < 0)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
}
}
pDescr++;
break;
}
case CSN_RECURSIVE_ARRAY:
{ /* Recursive way to specify an array: <list> ::= {1 <number: bit (4)> <list> | 0}
* or more generally: <list> ::= { <tag> <element> <list> | <EndTag> }
* where <element> ::= bit(value)
* <tag> ::= 0 | 1
* <EndTag> ::= reversed tag i.e. tag == 1 -> EndTag == 0 and vice versa
* {CSN_RECURSIVE_ARRAY, _BITS, (void*)offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER}
* REMARK: recursive way to specify an array but an iterative implementation!
*/
gint16 no_of_bits = pDescr->i;
guint8 ElementCount = 0;
pui8 = pui8DATA(data, pDescr->offset);
while (existNextElement(tvb, bit_offset, Tag))
{ /* tag control shows existence of next list elements */
proto_tree_add_bits_item(tree, *(pDescr->hf_exist_ptr), tvb, bit_offset, 1, ENC_BIG_ENDIAN);
bit_offset++;
remaining_bits_len--;
/* extract and store no_of_bits long element from bitstream */
*pui8++ = tvb_get_bits8(tvb, bit_offset, no_of_bits);
ElementCount++;
if (remaining_bits_len < 0)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
bit_offset += no_of_bits;
remaining_bits_len -= no_of_bits;
}
proto_tree_add_bits_item(tree, *(pDescr->hf_exist_ptr), tvb, bit_offset, 1, ENC_BIG_ENDIAN);
/* existNextElement() returned FALSE, 1 bit consumed */
bit_offset++;
remaining_bits_len--;
/* Store the counted number of elements of the array */
*pui8DATA(data, (gint16)pDescr->descr.value) = ElementCount;
pDescr++;
break;
}
case CSN_RECURSIVE_TARRAY:
{ /* Recursive way to specify an array of type: <lists> ::= { 1 <type> } ** 0 ;
* M_REC_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)
* {t, offsetof(_STRUCT, _ElementCountField), (void*)CSNDESCR_##_MEMBER_TYPE, offsetof(_STRUCT, _MEMBER), #_MEMBER, (StreamSerializeFcn_t)sizeof(_MEMBER_TYPE), NULL, NULL, (void_fn_t)ElementsOf(((_STRUCT*)0)->_MEMBER)}
*/
gint16 nSizeElement = (gint16)(gint32)pDescr->value;
guint32 nSizeArray = (guint32)((guintptr)pDescr->aux_fn);
guint8 ElementCount = 0;
while (existNextElement(tvb, bit_offset, Tag))
{ /* tag control shows existence of next list elements */
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, 1, ENC_BIG_ENDIAN);
/* existNextElement() returned TRUE, 1 bit consumed */
bit_offset++;
remaining_bits_len--;
ElementCount++;
if (ElementCount > nSizeArray)
{
/* error: too many elements in recursive array. Increase its size! */
return ProcessError(tree , ar->pinfo, tvb, bit_offset, CSN_ERROR_STREAM_NOT_SUPPORTED, &ei_csn1_stream_not_supported, pDescr);
}
{ /* unpack the following data structure */
csnStream_t arT = *ar;
gint16 Status;
proto_item *ti;
proto_tree *test_tree;
test_tree = proto_tree_add_subtree(tree, tvb, bit_offset>>3, 1, ett_csn1, &ti, pDescr->sz);
csnStreamInit(&arT, bit_offset, remaining_bits_len, ar->pinfo);
increment_dissection_depth(ar->pinfo);
Status = csnStreamDissector(test_tree, &arT, (const CSN_DESCR *)pDescr->descr.ptr, tvb, pvDATA(data, pDescr->offset), ett_csn1);
decrement_dissection_depth(ar->pinfo);
if (Status >= 0)
{ /* successful completion */
pui8 += nSizeElement; /* -> to next data element */
proto_item_set_len(ti,((arT.bit_offset-1)>>3) - (bit_offset>>3)+1);
remaining_bits_len = arT.remaining_bits_len;
bit_offset = arT.bit_offset;
}
else
{ /* something went awry */
return Status;
}
}
if (remaining_bits_len < 0)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
}
/* existNextElement() returned FALSE, 1 bit consumed */
bit_offset++;
remaining_bits_len --;
/* Store the counted number of elements of the array */
*pui8DATA(data, (gint16)(gint32)pDescr->i) = ElementCount;
pDescr++;
break;
}
case CSN_RECURSIVE_TARRAY_2:
{ /* Recursive way to specify an array of type: <list> ::= <type> { 0 <type> } ** 1 ; */
Tag = REVERSED_TAG;
/* NO break -
* handling is exactly the same as for CSN_RECURSIVE_TARRAY_1 so we continue
*/
}
/* FALL THROUGH */
case CSN_RECURSIVE_TARRAY_1:
{ /* Recursive way to specify an array of type: <lists> ::= <type> { 1 <type> } ** 0 ;
* M_REC_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)
* {t, offsetof(_STRUCT, _ElementCountField), (void*)CSNDESCR_##_MEMBER_TYPE, offsetof(_STRUCT, _MEMBER), #_MEMBER, (StreamSerializeFcn_t)sizeof(_MEMBER_TYPE), NULL, NULL, (void_fn_t)ElementsOf(((_STRUCT*)0)->_MEMBER)}
*/
gint16 nSizeElement = (gint16)(gint32)pDescr->value;
guint32 nSizeArray = (guint32)((guintptr)pDescr->aux_fn);
guint8 ElementCount = 0;
csnStream_t arT = *ar;
gboolean EndOfList = FALSE;
gint16 Status;
proto_item *ti;
proto_tree *test_tree;
do
{ /* get data element */
ElementCount++;
if (ElementCount >= nSizeArray)
{
/* error: too many elements in recursive array. Increase its size! */
return ProcessError(tree , ar->pinfo, tvb, bit_offset, CSN_ERROR_STREAM_NOT_SUPPORTED, &ei_csn1_stream_not_supported, pDescr);
}
test_tree = proto_tree_add_subtree_format(tree, tvb, bit_offset>>3, 1, ett_csn1, &ti, "%s[%d]", pDescr->sz, ElementCount-1);
csnStreamInit(&arT, bit_offset, remaining_bits_len, ar->pinfo);
increment_dissection_depth(ar->pinfo);
Status = csnStreamDissector(test_tree, &arT, (const CSN_DESCR *)pDescr->descr.ptr, tvb, pvDATA(data, pDescr->offset), ett_csn1);
decrement_dissection_depth(ar->pinfo);
if (Status >= 0)
{ /* successful completion */
pui8 += nSizeElement; /* -> to next */
proto_item_set_len(ti,((arT.bit_offset-1)>>3) - (bit_offset>>3)+1);
remaining_bits_len = arT.remaining_bits_len;
bit_offset = arT.bit_offset;
}
else
{ /* something went awry */
return Status;
}
if (remaining_bits_len < 0)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
/* control of next element's tag */
proto_tree_add_uint_bits_format_value(tree, *(pDescr->hf_ptr), tvb, bit_offset, 1, tvb_get_bits8(tvb, bit_offset, 1), ENC_BIG_ENDIAN, "%s[%d]",
proto_registrar_get_name(*(pDescr->hf_ptr)), ElementCount);
EndOfList = !(existNextElement(tvb, bit_offset, Tag));
bit_offset++;
remaining_bits_len--; /* 1 bit consumed (tag) */
} while (!EndOfList);
/* Store the count of the array */
*pui8DATA(data, pDescr->i) = ElementCount;
Tag = STANDARD_TAG; /* in case it was set to "reversed" */
pDescr++;
break;
}
case CSN_FIXED:
{ /* Verify the fixed bits */
guint8 no_of_bits = (guint8) pDescr->i;
guint32 ui32;
if (no_of_bits <= 8)
{
ui32 = tvb_get_bits8(tvb, bit_offset, no_of_bits);
}
else if (no_of_bits <= 16)
{
ui32 = tvb_get_bits16(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else if (no_of_bits <= 32)
{
ui32 = tvb_get_bits32(tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
}
else
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, -1, &ei_csn1_more32bits, pDescr);
}
if (ui32 != (unsigned)(gint32)pDescr->offset)
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, -1, &ei_csn1_fixed_not_matched, pDescr);
}
proto_tree_add_bits_item(tree, *(pDescr->hf_ptr), tvb, bit_offset, no_of_bits, ENC_BIG_ENDIAN);
remaining_bits_len -= no_of_bits;
bit_offset += no_of_bits;
pDescr++;
break;
}
case CSN_CALLBACK:
{
guint16 no_of_bits;
DissectorCallbackFcn_t callback = (DissectorCallbackFcn_t)pDescr->aux_fn;
no_of_bits = callback(tree, tvb, pvDATA(data, pDescr->i), pvDATA(data, pDescr->offset), bit_offset, ett_csn1, ar->pinfo);
bit_offset += no_of_bits;
remaining_bits_len -= no_of_bits;
pDescr++;
break;
}
case CSN_TRAP_ERROR:
{
return ProcessError(tree, ar->pinfo, tvb, bit_offset, -1, pDescr->error, pDescr);
}
case CSN_END:
{
ar->remaining_bits_len = remaining_bits_len;
ar->bit_offset = bit_offset;
return remaining_bits_len;
}
default:
{
DISSECTOR_ASSERT(0);
}
}
} while (remaining_bits_len >= 0);
return ProcessError(tree, ar->pinfo, tvb, bit_offset, CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, &ei_csn1_more_bits_to_unpack, pDescr);
}
void
proto_register_csn1(void)
{
static hf_register_info hf[] = {
{ &hf_null_data,
{ "NULL data", "csn1.null_data",
FT_NONE, BASE_NONE, NULL, 0x00,
NULL, HFILL }
},
};
static ei_register_info ei[] = {
{ &ei_csn1_more_bits_to_unpack, { "csn1.more_bits_to_unpack", PI_MALFORMED, PI_ERROR, "NEED_MORE BITS TO UNPACK", EXPFILL }},
{ &ei_csn1_general, { "csn1.general_error", PI_PROTOCOL, PI_WARN, "General -1", EXPFILL }},
{ &ei_csn1_not_implemented, { "csn1.not_implemented", PI_UNDECODED, PI_WARN, "NOT IMPLEMENTED", EXPFILL }},
{ &ei_csn1_union_index, { "csn1.union_index_invalid", PI_PROTOCOL, PI_WARN, "INVALID UNION INDEX", EXPFILL }},
{ &ei_csn1_script_error, { "csn1.script_error", PI_MALFORMED, PI_ERROR, "ERROR IN SCRIPT", EXPFILL }},
{ &ei_csn1_more32bits, { "csn1.more32bits", PI_PROTOCOL, PI_WARN, "no_of_bits > 32", EXPFILL }},
{ &ei_csn1_fixed_not_matched, { "csn1.fixed_not_matched", PI_PROTOCOL, PI_WARN, "FIXED value does not match", EXPFILL }},
{ &ei_csn1_stream_not_supported, { "csn1.stream_not_supported", PI_PROTOCOL, PI_WARN, "STREAM NOT SUPPORTED", EXPFILL }},
};
expert_module_t* expert_csn1;
proto_csn1 = proto_register_protocol("CSN.1", "CSN1", "csn1");
proto_register_field_array(proto_csn1, hf, array_length(hf));
expert_csn1 = expert_register_protocol(proto_csn1);
expert_register_field_array(expert_csn1, ei, array_length(ei));
proto_set_cant_toggle(proto_csn1);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 2
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=8:noTabs=true:
*/
|