summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-ber.c
blob: b6715b035394d54b9e7723c33c93c1ea44327a83 (plain)
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
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
/* packet-ber.c
 * Helpers for ASN.1/BER dissection
 * Ronnie Sahlberg (C) 2004
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * ITU-T Recommendation X.690 (07/2002),
 *   Information technology ASN.1 encoding rules:
 *     Specification of Basic Encoding Rules (BER), Canonical Encoding Rules (CER) and Distinguished Encoding Rules (DER)
 *
 */
/* TODO: change #.REGISTER signature to dissector_t and
 * update call_ber_oid_callback() accordingly.
 *
 * Since we don't pass the TAG/LENGTH from the CHOICE/SEQUENCE/SEQUENCE OF/
 * SET OF helpers through the callbacks to the next pabket-ber helper
 * when the tags are IMPLICIT, this causes a problem when we also have
 * indefinite length at the same time as the tags are implicit.
 *
 * While the proper fix is to change the signatures for packet-ber.c helpers
 * as well as the signatures for the callbacks to include the indefinite length
 * indication that would be a major job.
 *
 * Originally we used a kludge - we set a global variable in the
 * CHOICE/SEQUENCE [OF]/SET [OF] helpers to indicate to the next helper
 * whether the length is indefinite or not.
 * That had currently only been implemented for {SEQUENCE|SET} [OF] but not
 * CHOICE.
 *
 * This version attacks the problem(s) in a different way.  If we see
 * indefinite length the get_ber_length traverses the tags within the
 * compound value and then we return the true length of the compound value
 * including the EOC. Thus the tvb length is now always correct even for
 * indefinite length, then if we get implicit tags they can be handled as
 * if they were definite length.
 */

//#define DEBUG_BER 1

#include "config.h"

#include <epan/packet.h>
#include <epan/exceptions.h>
#include <epan/reassemble.h>
#include <epan/oids.h>
#include <epan/expert.h>
#include <epan/uat.h>
#include <epan/decode_as.h>
#include <epan/tfs.h>
#include <wiretap/wtap.h>
#ifdef DEBUG_BER
#include <epan/ws_printf.h>
#endif

#include "packet-ber.h"

/*
 * Set a limit on recursion so we don't blow away the stack. Another approach
 * would be to remove recursion completely but then we'd exhaust CPU+memory
 * trying to read a hellabyte of nested indefinite lengths.

 * XXX - Max nesting in the ASN.1 plugin is 32. Should they match?
 */
#define BER_MAX_NESTING 500

void proto_register_ber(void);
void proto_reg_handoff_ber(void);

static int proto_ber;
static int hf_ber_id_class;
static int hf_ber_id_pc;
static int hf_ber_id_uni_tag;
static int hf_ber_id_uni_tag_ext;
static int hf_ber_id_tag;
static int hf_ber_id_tag_ext;
static int hf_ber_length;
static int hf_ber_length_octets;
static int hf_ber_bitstring_padding;
static int hf_ber_bitstring_empty;
static int hf_ber_unknown_OID;
static int hf_ber_unknown_relative_OID;
static int hf_ber_unknown_BOOLEAN;
static int hf_ber_unknown_OCTETSTRING;
static int hf_ber_unknown_BER_OCTETSTRING;
static int hf_ber_unknown_BER_primitive;
static int hf_ber_unknown_GraphicString;
static int hf_ber_unknown_NumericString;
static int hf_ber_unknown_PrintableString;
static int hf_ber_unknown_TeletexString;
static int hf_ber_unknown_VisibleString;
static int hf_ber_unknown_GeneralString;
static int hf_ber_unknown_UniversalString;
static int hf_ber_unknown_BMPString;
static int hf_ber_unknown_IA5String;
static int hf_ber_unknown_UTCTime;
static int hf_ber_unknown_UTF8String;
static int hf_ber_unknown_GeneralizedTime;
static int hf_ber_unknown_INTEGER;
static int hf_ber_unknown_REAL;
static int hf_ber_unknown_BITSTRING;
static int hf_ber_unknown_ENUMERATED;
static int hf_ber_direct_reference;         /* OBJECT_IDENTIFIER */
static int hf_ber_indirect_reference;       /* INTEGER */
static int hf_ber_data_value_descriptor;    /* ObjectDescriptor */
static int hf_ber_encoding;                 /* T_encoding */
static int hf_ber_single_ASN1_type;         /* T_single_ASN1_type */
static int hf_ber_octet_aligned;            /* OCTET_STRING */
static int hf_ber_arbitrary;                /* BIT_STRING */
static int hf_ber_extra_data;
static int hf_ber_encoding_boiler_plate;

/* Generated from convert_proto_tree_add_text.pl */
static int hf_ber_seq_of_eoc;
static int hf_ber_64bit_uint_as_bytes;
static int hf_ber_choice_eoc;
static int hf_ber_seq_field_eoc;
static int hf_ber_seq_eoc;
static int hf_ber_set_field_eoc;
static int hf_ber_set_eoc;
static int hf_ber_null_tag;
static int hf_ber_unknown_octetstring;
static int hf_ber_unknown_data;

static int hf_ber_fragments;
static int hf_ber_fragment;
static int hf_ber_fragment_overlap;
static int hf_ber_fragment_overlap_conflicts;
static int hf_ber_fragment_multiple_tails;
static int hf_ber_fragment_too_long_fragment;
static int hf_ber_fragment_error;
static int hf_ber_fragment_count;
static int hf_ber_reassembled_in;
static int hf_ber_reassembled_length;

static int ett_ber_octet_string;
static int ett_ber_reassembled_octet_string;
static int ett_ber_primitive;
static int ett_ber_unknown;
static int ett_ber_SEQUENCE;
static int ett_ber_EXTERNAL;
static int ett_ber_T_encoding;
static int ett_ber_fragment;
static int ett_ber_fragments;

static expert_field ei_ber_size_constraint_string;
static expert_field ei_ber_size_constraint_value;
static expert_field ei_ber_size_constraint_items;
static expert_field ei_ber_sequence_field_wrong;
static expert_field ei_ber_expected_octet_string;
static expert_field ei_ber_expected_null;
static expert_field ei_ber_expected_null_zero_length;
static expert_field ei_ber_expected_sequence;
static expert_field ei_ber_expected_set;
static expert_field ei_ber_expected_string;
static expert_field ei_ber_expected_object_identifier;
static expert_field ei_ber_expected_generalized_time;
static expert_field ei_ber_expected_utc_time;
static expert_field ei_ber_expected_bitstring;
static expert_field ei_ber_error_length;
static expert_field ei_ber_wrong_tag_in_tagged_type;
static expert_field ei_ber_universal_tag_unknown;
static expert_field ei_ber_no_oid;
static expert_field ei_ber_syntax_not_implemented;
static expert_field ei_ber_oid_not_implemented;
static expert_field ei_ber_value_too_many_bytes;
static expert_field ei_ber_unknown_field_sequence;
static expert_field ei_ber_unknown_field_set;
static expert_field ei_ber_missing_field_set;
static expert_field ei_ber_empty_choice;
static expert_field ei_ber_choice_not_found;
static expert_field ei_ber_bits_unknown;
static expert_field ei_ber_bits_set_padded;
static expert_field ei_ber_illegal_padding;
static expert_field ei_ber_invalid_format_generalized_time;
static expert_field ei_ber_invalid_format_utctime;
static expert_field ei_hf_field_not_integer_type;
static expert_field ei_ber_constr_bitstr;
static expert_field ei_ber_real_not_primitive;

static dissector_handle_t ber_handle;
static dissector_handle_t ber_file_handle;

static bool show_internal_ber_fields;
static bool decode_octetstring_as_ber;
static bool decode_primitive_as_ber;
static bool decode_unexpected;
static bool decode_warning_leading_zero_bits;

static char *decode_as_syntax;

static dissector_table_t ber_oid_dissector_table;
static dissector_table_t ber_syntax_dissector_table;

static GHashTable *syntax_table;

static int8_t   last_class;
static bool last_pc;
static int32_t  last_tag;
static uint32_t last_length;
static tvbuff_t *last_length_tvb;
static int      last_length_offset;
static int      last_length_len;
static bool last_ind;

static const value_string ber_class_codes[] = {
    { BER_CLASS_UNI,    "UNIVERSAL" },
    { BER_CLASS_APP,    "APPLICATION" },
    { BER_CLASS_CON,    "CONTEXT" },
    { BER_CLASS_PRI,    "PRIVATE" },
    { 0, NULL }
};

static const true_false_string ber_pc_codes = {
    "Constructed Encoding",
    "Primitive Encoding"
};


static const value_string ber_uni_tag_codes[] = {
    { BER_UNI_TAG_EOC,              "'end-of-content'" },
    { BER_UNI_TAG_BOOLEAN,          "BOOLEAN" },
    { BER_UNI_TAG_INTEGER,          "INTEGER" },
    { BER_UNI_TAG_BITSTRING,        "BIT STRING" },
    { BER_UNI_TAG_OCTETSTRING,      "OCTET STRING" },
    { BER_UNI_TAG_NULL,             "NULL" },
    { BER_UNI_TAG_OID,              "OBJECT IDENTIFIER" },
    { BER_UNI_TAG_ObjectDescriptor, "ObjectDescriptor" },
    { BER_UNI_TAG_EXTERNAL,         "EXTERNAL" },
    { BER_UNI_TAG_REAL,             "REAL" },
    { BER_UNI_TAG_ENUMERATED,       "ENUMERATED" },
    { BER_UNI_TAG_EMBEDDED_PDV,     "EMBEDDED PDV" },
    { BER_UNI_TAG_UTF8String,       "UTF8String" },
    { BER_UNI_TAG_RELATIVE_OID,     "RELATIVE-OID" },
    /* UNIVERSAL 14-15
     * Reserved for future editions of this
     * Recommendation | International Standard
     */
    {  14,      "Reserved for future editions" },
    {  15 ,     "Reserved for future editions" },

    { BER_UNI_TAG_SEQUENCE,         "SEQUENCE" },
    { BER_UNI_TAG_SET,              "SET" },
    { BER_UNI_TAG_NumericString,    "NumericString" },
    { BER_UNI_TAG_PrintableString,  "PrintableString" },
    { BER_UNI_TAG_TeletexString,    "TeletexString, T61String" },
    { BER_UNI_TAG_VideotexString,   "VideotexString" },
    { BER_UNI_TAG_IA5String,        "IA5String" },
    { BER_UNI_TAG_UTCTime,          "UTCTime" },
    { BER_UNI_TAG_GeneralizedTime,  "GeneralizedTime" },
    { BER_UNI_TAG_GraphicString,    "GraphicString" },
    { BER_UNI_TAG_VisibleString,    "VisibleString, ISO64String" },
    { BER_UNI_TAG_GeneralString,    "GeneralString" },
    { BER_UNI_TAG_UniversalString,  "UniversalString" },
    { BER_UNI_TAG_CHARACTERSTRING,  "CHARACTER STRING" },
    { BER_UNI_TAG_BMPString,        "BMPString" },
    { 31,                           "Continued" },
    { 0, NULL }
};
static value_string_ext ber_uni_tag_codes_ext = VALUE_STRING_EXT_INIT(ber_uni_tag_codes);

#if 0
static const true_false_string ber_real_binary_vals = {
    "Binary encoding",
    "Decimal encoding"
};

static const true_false_string ber_real_decimal_vals = {
    "SpecialRealValue",
    "Decimal encoding"
};
#endif

typedef struct _da_data {
    GHFunc   func;
    void *user_data;
} da_data;

typedef struct _oid_user_t {
    char *oid;
    char *name;
    char *syntax;
} oid_user_t;

UAT_CSTRING_CB_DEF(oid_users, oid, oid_user_t)
UAT_CSTRING_CB_DEF(oid_users, name, oid_user_t)
UAT_VS_CSTRING_DEF(oid_users, syntax, oid_user_t, 0, "")

static oid_user_t *oid_users;
static unsigned num_oid_users;

#define MAX_SYNTAX_NAMES 128
/* Define non_const_value_string as a hack to prevent chackAPIs.pl from complaining */
#define non_const_value_string value_string
static non_const_value_string syntax_names[MAX_SYNTAX_NAMES+1] = {
    {0, ""},
    {0, NULL}
};

static const fragment_items octet_string_frag_items = {
    /* Fragment subtrees */
    &ett_ber_fragment,
    &ett_ber_fragments,
    /* Fragment fields */
    &hf_ber_fragments,
    &hf_ber_fragment,
    &hf_ber_fragment_overlap,
    &hf_ber_fragment_overlap_conflicts,
    &hf_ber_fragment_multiple_tails,
    &hf_ber_fragment_too_long_fragment,
    &hf_ber_fragment_error,
    &hf_ber_fragment_count,
    /* Reassembled in field */
    &hf_ber_reassembled_in,
    /* Reassembled length field */
    &hf_ber_reassembled_length,
    /* Reassembled data field */
    NULL,
    /* Tag */
    "OCTET STRING fragments"
};

void
add_ber_encoded_label(tvbuff_t* tvb, packet_info* pinfo _U_, proto_tree* tree)
{
    proto_item *ti;

    ti = proto_tree_add_item(tree, hf_ber_encoding_boiler_plate, tvb, 0, -1, ENC_NA);
    proto_item_set_generated(ti);

}

static void *
oid_copy_cb(void *dest, const void *orig, size_t len _U_)
{
    oid_user_t       *u = (oid_user_t *)dest;
    const oid_user_t *o = (const oid_user_t *)orig;

    u->oid = g_strdup(o->oid);
    u->name = g_strdup(o->name);
    u->syntax = o->syntax;

    return dest;
}

static void
oid_free_cb(void *r)
{
    oid_user_t *u = (oid_user_t *)r;

    g_free(u->oid);
    g_free(u->name);
}

static int
cmp_value_string(const void *v1, const void *v2)
{
    const value_string *vs1 = (const value_string *)v1;
    const value_string *vs2 = (const value_string *)v2;

    return strcmp(vs1->strptr, vs2->strptr);
}

static uat_field_t users_flds[] = {
    UAT_FLD_OID(oid_users, oid, "OID", "Object Identifier"),
    UAT_FLD_CSTRING(oid_users, name, "Name", "Human readable name for the OID"),
    UAT_FLD_VS(oid_users, syntax, "Syntax", syntax_names, "Syntax of values associated with the OID"),
    UAT_END_FIELDS
};


static void ber_prompt(packet_info *pinfo _U_, char* result)
{
    snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Decode ASN.1 file as");
}

static void *ber_value(packet_info *pinfo _U_)
{
    /* Not used */
    return NULL;
}

struct ber_decode_as_populate
{
    decode_as_add_to_list_func add_to_list;
    void *ui_element;
};

static void
decode_ber_add_to_list(void *key, void *value, void *user_data)
{
    struct ber_decode_as_populate* populate = (struct ber_decode_as_populate*)user_data;
    dtbl_entry_t *dtbl_entry = (dtbl_entry_t*)value;
    populate->add_to_list("ASN.1", (char *)key, dtbl_entry_get_initial_handle(dtbl_entry), populate->ui_element);
}

static void ber_populate_list(const char *table_name _U_, decode_as_add_to_list_func add_to_list, void *ui_element)
{
    struct ber_decode_as_populate populate;

    populate.add_to_list = add_to_list;
    populate.ui_element = ui_element;

    ber_decode_as_foreach(decode_ber_add_to_list, &populate);
}

static bool ber_decode_as_reset(const char *name _U_, const void *pattern _U_)
{
    g_free(decode_as_syntax);
    decode_as_syntax = NULL;
    return false;
}

static bool ber_decode_as_change(const char *name _U_, const void *pattern _U_, const void *handle _U_, const char* list_name)
{
    g_free(decode_as_syntax);
    decode_as_syntax = g_strdup(list_name);
    return false;
}

int
dissect_ber_oid_NULL_callback(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree _U_, void* data _U_)
{
    return tvb_captured_length(tvb);
}


void
register_ber_oid_dissector_handle(const char *oid, dissector_handle_t dissector, int proto _U_, const char *name)
{
    dissector_add_string("ber.oid", oid, dissector);
    oid_add_from_string(name, oid);
}

void
register_ber_oid_dissector(const char *oid, dissector_t dissector, int proto, const char *name)
{
    dissector_handle_t dissector_handle;

    dissector_handle = create_dissector_handle(dissector, proto);
    dissector_add_string("ber.oid", oid, dissector_handle);
    oid_add_from_string(name, oid);
}

void
register_ber_syntax_dissector(const char *syntax, int proto, dissector_t dissector)
{
    dissector_handle_t dissector_handle;

    dissector_handle = create_dissector_handle_with_name_and_description(dissector, proto, NULL, syntax);
    dissector_add_string("ber.syntax", syntax, dissector_handle);

}

void
register_ber_oid_syntax(const char *oid, const char *name, const char *syntax)
{

    if (syntax && *syntax)
        g_hash_table_insert(syntax_table, (void *)g_strdup(oid), (void *)g_strdup(syntax));

    if (name && *name)
        register_ber_oid_name(oid, name);
}

/* Register the oid name to get translation in proto dissection */
void
register_ber_oid_name(const char *oid, const char *name)
{
    oid_add_from_string(name, oid);
}

static void
ber_add_syntax_name(void *key, void *value _U_, void *user_data)
{
    unsigned *i = (unsigned*)user_data;

    if (*i < MAX_SYNTAX_NAMES) {
        syntax_names[*i].value = *i;
        syntax_names[*i].strptr = (const char*)key;

        (*i)++;
    }

}

static void
ber_decode_as_dt(const char *table_name _U_, ftenum_t selector_type _U_, void *key, void *value, void *user_data)
{
    da_data *decode_as_data;

    decode_as_data = (da_data *)user_data;

    decode_as_data->func(key, value, decode_as_data->user_data);
}

void
ber_decode_as_foreach(GHFunc func, void *user_data)
{
    da_data decode_as_data;

    decode_as_data.func = func;
    decode_as_data.user_data = user_data;

    dissector_table_foreach("ber.syntax",  ber_decode_as_dt, &decode_as_data);

}

/* Get oid syntax from hash table to get translation in proto dissection(packet-per.c) */
static const char *
get_ber_oid_syntax(const char *oid)
{
    return (const char *)g_hash_table_lookup(syntax_table, oid);
}

static void
ber_update_oids(void)
{
    unsigned i;

    for (i = 0; i < num_oid_users; i++)
        register_ber_oid_syntax(oid_users[i].oid, oid_users[i].name, oid_users[i].syntax);
}

static void
ber_check_length (uint32_t length, int32_t min_len, int32_t max_len, asn1_ctx_t *actx, proto_item *item, bool bit)
{
    if ((min_len != -1) && (length < (uint32_t)min_len)) {
        expert_add_info_format(
            actx->pinfo, item, &ei_ber_size_constraint_string,
            "Size constraint: %sstring too short: %d (%d .. %d)",
            bit ? "bit " : "", length, min_len, max_len);
    } else if ((max_len != -1) && (length > (uint32_t)max_len)) {
        expert_add_info_format(
            actx->pinfo, item, &ei_ber_size_constraint_string,
            "Size constraint: %sstring too long: %d (%d .. %d)",
            bit ? "bit " : "", length, min_len, max_len);
    }
}

static void
ber_check_value64 (int64_t value, int64_t min_len, int64_t max_len, asn1_ctx_t *actx, proto_item *item)
{
    if ((min_len != -1) && (value < min_len)) {
        expert_add_info_format(
            actx->pinfo, item, &ei_ber_size_constraint_value,
            "Size constraint: value too small: %" PRId64 " (%" PRId64" .. %" PRId64 ")",
            value, min_len, max_len);
    } else if ((max_len != -1) && (value > max_len)) {
        expert_add_info_format(
            actx->pinfo, item, &ei_ber_size_constraint_value,
            "Size constraint: value too big: %" PRId64 " (%" PRId64 " .. %" PRId64 ")",
            value, min_len, max_len);
    }
}

static void
ber_check_value (uint32_t value, int32_t min_len, int32_t max_len, asn1_ctx_t *actx, proto_item *item)
{
    if ((min_len != -1) && (value < (uint32_t)min_len)) {
        expert_add_info_format(
            actx->pinfo, item, &ei_ber_size_constraint_value,
            "Size constraint: value too small: %d (%d .. %d)",
            value, min_len, max_len);
    } else if ((max_len != -1) && (value > (uint32_t)max_len)) {
        expert_add_info_format(
            actx->pinfo, item, &ei_ber_size_constraint_value,
            "Size constraint: value too big: %d (%d .. %d)",
            value, min_len, max_len);
    }
}

static void
ber_check_items (int cnt, int32_t min_len, int32_t max_len, asn1_ctx_t *actx, proto_item *item)
{
    if ((min_len != -1) && (cnt < min_len)) {
        expert_add_info_format(
            actx->pinfo, item, &ei_ber_size_constraint_items,
            "Size constraint: too few items: %d (%d .. %d)",
            cnt, min_len, max_len);
    } else if ((max_len != -1) && (cnt > max_len)) {
        expert_add_info_format(
            actx->pinfo, item, &ei_ber_size_constraint_items,
            "Size constraint: too many items: %d (%d .. %d)",
            cnt, min_len, max_len);
    }
}

/*
 * XXX - if the specified length is less than the remaining length
 * of data in the tvbuff, either 1) the specified length is bad and
 * we should report that with an expert info or 2) the tvbuff is
 * unreassembled and we should make the new tvbuff also be an
 * unreassembled tvbuff.
 */
static tvbuff_t *
ber_tvb_new_subset_length(tvbuff_t *tvb, const int backing_offset, const int backing_length)
{
    int length_remaining;

    length_remaining = tvb_reported_length_remaining(tvb, backing_offset);
    return tvb_new_subset_length(tvb, backing_offset, (length_remaining > backing_length) ? backing_length : length_remaining);
}

int
dissect_ber_tagged_type(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, int8_t tag_cls, int32_t tag_tag, bool tag_impl, ber_type_fn type)
{
    int8_t      tmp_cls;
    int32_t     tmp_tag;
    int         identifier_offset;
    int         identifier_len;
    uint32_t    tmp_len;
    tvbuff_t   *next_tvb = tvb;

#ifdef DEBUG_BER
{
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(tvb, offset) > 3) {
proto_tree_add_debug_text(tree, "dissect_ber_tagged_type(%s) entered implicit_tag:%d offset:%d len:%d %02x:%02x:%02x\n", name, implicit_tag, offset, tvb_reported_length_remaining(tvb, offset), tvb_get_uint8(tvb, offset), tvb_get_uint8(tvb, offset+1), tvb_get_uint8(tvb, offset+2));
} else {
proto_tree_add_debug_text(tree, "dissect_ber_tagged_type(%s) entered\n", name);
}
}
#endif

    if (implicit_tag) {
        offset = type(tag_impl, tvb, offset, actx, tree, hf_id);
        return offset;
    }

    identifier_offset = offset;
    offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &tmp_cls, NULL, &tmp_tag);
    identifier_len = offset - identifier_offset;
    offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &tmp_len, NULL);

    if ((tmp_cls != tag_cls) || (tmp_tag != tag_tag)) {
        proto_tree_add_expert_format(
            tree, actx->pinfo, &ei_ber_wrong_tag_in_tagged_type,
            tvb, identifier_offset, identifier_len,
            "BER Error: Wrong tag in tagged type - expected class:%s(%d) tag:%d (%s) but found class:%s(%d) tag:%d",
            val_to_str_const(tag_cls, ber_class_codes, "Unknown"),
            tag_cls,
            tag_tag,
            val_to_str_ext_const(tag_tag, &ber_uni_tag_codes_ext, "Unknown"),
            val_to_str_const(tmp_cls, ber_class_codes, "Unknown"),
            tmp_cls,
            tmp_tag);
    }

    if (tag_impl) {
        next_tvb = ber_tvb_new_subset_length(tvb, offset, tmp_len);
        type(tag_impl, next_tvb, 0, actx, tree, hf_id);
        offset += tmp_len;
    } else {
        offset = type(tag_impl, tvb, offset, actx, tree, hf_id);
    }

    return offset;
}

/*
 * Add a "length bogus" error.
 */
static proto_item *
ber_add_bad_length_error(packet_info *pinfo, proto_tree *tree,
                         const char *name, tvbuff_t *tvb, const int start,
                         int length)
{
    proto_item *ti;

    ti = proto_tree_add_expert_format(
        tree, pinfo, &ei_ber_error_length, tvb, start, length,
        "BER Error: %s: length of item (%d) is not valid",
        name, length);
    return ti;
}

/*
 * Add an "exceeds tvb length" error.
 */
static void
ber_add_large_length_error(packet_info *pinfo, proto_tree *tree, tvbuff_t *tvb,
                         int offset, int length, tvbuff_t *len_tvb,
                         const int len_offset, const uint32_t len_length)
{
    proto_tree_add_expert_format(
        tree, pinfo, &ei_ber_error_length, len_tvb, len_offset, len_length,
        "BER Error: length %u longer than tvb_reported_length_remaining: %d",
        length,
        tvb_reported_length_remaining(tvb, offset));
}

/*
 * Like proto_tree_add_item(), but checks whether the length of the item
 * being added is appropriate for the type of the item being added, so
 * if it's not, we report an error rather than a dissector bug.
 *
 * This is for use when a field that's nominally an OCTET STRING but
 * where we want the string further interpreted, e.g. as a number or
 * a network address or a UN*X-style time stamp.
 *
 * XXX - this duplicates the length checking in proto_tree_add_item()
 * and the routines it calls; that should really be done in one
 * place.  We *do* want to report a dissector bug in proto_tree_add_item()
 * if the dissector explicitly says, for example, "this IPv4 address is
 * 7 bytes long", but we don't want to report a dissector bug if the
 * *packet* says "this IPv4 address is 7 bytes long", we want to report
 * a malformed packet.
 */
static proto_item *
ber_proto_tree_add_item(packet_info *pinfo, proto_tree *tree,
                        const int hfindex, tvbuff_t *tvb, const int start,
                        int length, const unsigned encoding)
{
    header_field_info *hfinfo;
    proto_item* ti;

    hfinfo = proto_registrar_get_nth((unsigned)hfindex);
    if (hfinfo != NULL) {
        switch (hfinfo->type) {

        case FT_BOOLEAN:
        case FT_UINT8:
        case FT_UINT16:
        case FT_UINT24:
        case FT_UINT32:
        case FT_INT8:
        case FT_INT16:
        case FT_INT24:
        case FT_INT32:
            if ((length != 1) && (length != 2) && (length != 3) &&
                (length != 4))
                return ber_add_bad_length_error(pinfo, tree,
                    hfinfo->name, tvb, start, length);
            break;

        case FT_IPv4:
            if (length != FT_IPv4_LEN)
                return ber_add_bad_length_error(pinfo, tree,
                    hfinfo->name, tvb, start, length);
            break;

        case FT_IPXNET:
            if (length != FT_IPXNET_LEN)
                return ber_add_bad_length_error(pinfo, tree,
                    hfinfo->name, tvb, start, length);
            break;

        case FT_IPv6:
            if ((length < 0) || (length > FT_IPv6_LEN))
                return ber_add_bad_length_error(pinfo, tree,
                    hfinfo->name, tvb, start, length);
            break;

        case FT_ETHER:
            if (length != FT_ETHER_LEN)
                return ber_add_bad_length_error(pinfo, tree,
                    hfinfo->name, tvb, start, length);
            break;

        case FT_GUID:
            if (length != FT_GUID_LEN)
                return ber_add_bad_length_error(pinfo, tree,
                    hfinfo->name, tvb, start, length);
            break;

        case FT_FLOAT:
            if (length != 4)
                return ber_add_bad_length_error(pinfo, tree,
                    hfinfo->name, tvb, start, length);
            break;

        case FT_DOUBLE:
            if (length != 8)
                return ber_add_bad_length_error(pinfo, tree,
                    hfinfo->name, tvb, start, length);
            break;

        case FT_ABSOLUTE_TIME:
        case FT_RELATIVE_TIME:
            if ((length != 4) && (length != 8))
                return ber_add_bad_length_error(pinfo, tree,
                    hfinfo->name, tvb, start, length);
            break;
        case FT_STRING:
            if (length == 0) {
                ti = proto_tree_add_item(tree, hfindex, tvb, start, length, encoding);
                proto_item_append_text(ti, "<MISSING>");
                return ti;
            }
            break;
        default:
            break;
        }
    }
    return proto_tree_add_item(tree, hfindex, tvb, start, length, encoding);
}

static int
// NOLINTNEXTLINE(misc-no-recursion)
try_dissect_unknown_ber(packet_info *pinfo, tvbuff_t *tvb, volatile int offset, proto_tree *tree, int nest_level)
{
    int                start_offset;
    int8_t             ber_class;
    bool               pc, ind;
    int32_t            tag;
    int                identifier_offset;
    int                identifier_len;
    uint32_t           len;
    int                len_offset;
    int                len_len;
    int                hdr_len;
    proto_item        *item      = NULL;
    proto_tree        *next_tree = NULL;
    uint8_t            c;
    uint32_t           i;
    bool           is_printable;
    volatile bool  is_decoded_as;
    proto_item        *pi;
    asn1_ctx_t         asn1_ctx;

    if (nest_level > BER_MAX_NESTING) {
        /* Assume that we have a malformed packet. */
        THROW(ReportedBoundsError);
    }

    start_offset = offset;
    asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, true, pinfo);

    offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
    len_offset = offset;
    offset = get_ber_length(tvb, offset, &len, &ind);
    len_len = offset - len_offset;

    if (len > (uint32_t)tvb_reported_length_remaining(tvb, offset)) {
        /* hmm   maybe something bad happened or the frame is short;
           since these are not vital outputs just return instead of
           throwing an exception.
         */

        if (show_internal_ber_fields) {
            offset = dissect_ber_identifier(pinfo, tree, tvb, start_offset, &ber_class, &pc, &tag);
            offset = dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
        }
        ber_add_large_length_error(pinfo, tree, tvb, offset, len, tvb, len_offset, len_len);
        return tvb_reported_length(tvb);
    }
/* we don't care about the class only on the constructor flag */
    if (pc != true) {

        /* this is not constructed */

        switch (ber_class) { /* we do care about the class */
        case BER_CLASS_UNI: /* it a Universal tag - we can decode it */
            switch (tag) {
            case BER_UNI_TAG_EOC:
                /* XXX: shouldn't really get here */
                break;
            case BER_UNI_TAG_INTEGER:
                offset = dissect_ber_integer(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_INTEGER, NULL);
                break;
            case BER_UNI_TAG_REAL:
                offset = dissect_ber_real(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_REAL, NULL);
                break;
            case BER_UNI_TAG_BITSTRING:
                offset = dissect_ber_bitstring(false, &asn1_ctx, tree, tvb, start_offset, NULL, 0, hf_ber_unknown_BITSTRING, -1, NULL);
                break;
            case BER_UNI_TAG_ENUMERATED:
                offset = dissect_ber_integer(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_ENUMERATED, NULL);
                break;
            case BER_UNI_TAG_GraphicString:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_GraphicString, NULL);
                break;
            case BER_UNI_TAG_OCTETSTRING:
                is_decoded_as = false;
                if (decode_octetstring_as_ber && (len >= 2)) {
                    volatile int ber_offset = 0;
                    uint32_t ber_len = 0;
                    TRY{
                        ber_offset = get_ber_identifier(tvb, offset, NULL, &pc, NULL);
                        ber_offset = get_ber_length(tvb, ber_offset, &ber_len, NULL);
                    } CATCH_ALL {
                    }
                    ENDTRY;
                    if (pc && (ber_len > 0) && (ber_len + (ber_offset - offset) == len)) {
                        /* Decoded a constructed ASN.1 tag with a length indicating this
                         * could be BER encoded data.  Try dissecting as unknown BER.
                         */
                        is_decoded_as = true;
                        if (show_internal_ber_fields) {
                            offset = dissect_ber_identifier(pinfo, tree, tvb, start_offset, NULL, NULL, NULL);
                            offset = dissect_ber_length(pinfo, tree, tvb, offset, NULL, NULL);
                        }
                        item = ber_proto_tree_add_item(pinfo, tree, hf_ber_unknown_BER_OCTETSTRING, tvb, offset, len, ENC_NA);
                        next_tree = proto_item_add_subtree(item, ett_ber_octet_string);
                        offset = try_dissect_unknown_ber(pinfo, tvb, offset, next_tree, nest_level + 1);
                    }
                }
                if (!is_decoded_as) {
                    offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_OCTETSTRING, NULL);
                }
                break;
            case BER_UNI_TAG_OID:
                offset = dissect_ber_object_identifier_str(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_OID, NULL);
                break;
            case BER_UNI_TAG_RELATIVE_OID:
                offset = dissect_ber_relative_oid_str(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_relative_OID, NULL);
                break;
            case BER_UNI_TAG_NumericString:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_NumericString, NULL);
                break;
            case BER_UNI_TAG_PrintableString:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_PrintableString, NULL);
                break;
            case BER_UNI_TAG_TeletexString:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_TeletexString, NULL);
                break;
            case BER_UNI_TAG_VisibleString:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_VisibleString, NULL);
                break;
            case BER_UNI_TAG_GeneralString:
                offset = dissect_ber_GeneralString(&asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_GeneralString, NULL, 0);
                break;
            case BER_UNI_TAG_BMPString:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_BMPString, NULL);
                break;
            case BER_UNI_TAG_UniversalString:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_UniversalString, NULL);
                break;
            case BER_UNI_TAG_IA5String:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_IA5String, NULL);
                break;
            case BER_UNI_TAG_UTCTime:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_UTCTime, NULL);
                break;
            case BER_UNI_TAG_NULL:
                proto_tree_add_item(tree, hf_ber_null_tag, tvb, offset, len, ENC_NA);
                break;
            case BER_UNI_TAG_UTF8String:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_UTF8String, NULL);
                break;
            case BER_UNI_TAG_GeneralizedTime:
                offset = dissect_ber_octet_string(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_GeneralizedTime, NULL);
                break;
            case BER_UNI_TAG_BOOLEAN:
                offset = dissect_ber_boolean(false, &asn1_ctx, tree, tvb, start_offset, hf_ber_unknown_BOOLEAN, NULL);
                break;
            default:
                identifier_offset = start_offset;
                offset = dissect_ber_identifier(pinfo, tree, tvb, start_offset, &ber_class, &pc, &tag);
                identifier_len = offset - identifier_offset;
                offset = dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
                proto_tree_add_expert_format(
                    tree, pinfo, &ei_ber_universal_tag_unknown,
                    tvb, identifier_offset, identifier_len,
                    "BER Error: can not handle universal tag:%d",
                    tag);
                offset += len;
            }
            break;
        case BER_CLASS_APP:
        case BER_CLASS_CON:
        case BER_CLASS_PRI:
        default:
            /* we dissect again if show_internal_ber_fields is set */
            if (show_internal_ber_fields) {
                offset = dissect_ber_identifier(pinfo, tree, tvb, start_offset, &ber_class, &pc, &tag);
                offset = dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
            }

            /* we can't dissect this directly as it is specific */
            pi = proto_tree_add_none_format(tree, hf_ber_unknown_BER_primitive, tvb, offset, len,
                "[%s %d] ", val_to_str_const(ber_class, ber_class_codes, "Unknown"), tag);

            is_decoded_as = false;
            if (decode_primitive_as_ber && (len >= 2)) {
                volatile int ber_offset = 0;
                uint32_t ber_len = 0;
                TRY{
                    ber_offset = get_ber_identifier(tvb, offset, NULL, &pc, NULL);
                    ber_offset = get_ber_length(tvb, ber_offset, &ber_len, NULL);
                } CATCH_ALL {
                }
                ENDTRY;
                if (pc && (ber_len > 0) && (ber_len + (ber_offset - offset) == len)) {
                    /* Decoded a constructed ASN.1 tag with a length indicating this
                     * could be BER encoded data.  Try dissecting as unknown BER.
                     */
                    is_decoded_as = true;
                    proto_item_append_text(pi, "[BER encoded]");
                    next_tree = proto_item_add_subtree(pi, ett_ber_primitive);
                    offset = try_dissect_unknown_ber(pinfo, tvb, offset, next_tree, nest_level + 1);
                }
            }

            if (!is_decoded_as && len) {
                /* we may want to do better and show the bytes */
                is_printable = true;
                for (i = 0; i < len; i++) {
                    c = tvb_get_uint8(tvb, offset + i);

                    if (is_printable && !g_ascii_isprint(c))
                        is_printable = false;

                    proto_item_append_text(pi, "%02x", c);
                }

                if (is_printable) { /* give a nicer representation if it looks like a string */
                    proto_item_append_text(pi, " (");
                    for (i = 0; i < len; i++) {
                        proto_item_append_text(pi, "%c", tvb_get_uint8(tvb, offset + i));
                    }
                    proto_item_append_text(pi, ")");
                }
                offset += len;
            }

            break;
        }
    } else {
        /* this is constructed */

        /* we dissect again if show_internal_ber_fields is set */
        if (show_internal_ber_fields) {
            offset = dissect_ber_identifier(pinfo, tree, tvb, start_offset, &ber_class, &pc, &tag);
            offset = dissect_ber_length(pinfo, tree, tvb, offset, &len, NULL);
        }

        hdr_len = offset-start_offset;

        switch (ber_class) {
        case BER_CLASS_UNI:
            next_tree = proto_tree_add_subtree(tree, tvb, offset, len, ett_ber_SEQUENCE, NULL,
                                               val_to_str_ext_const(tag, &ber_uni_tag_codes_ext, "Unknown"));
            while (offset < (int)(start_offset + len + hdr_len))
                offset = try_dissect_unknown_ber(pinfo, tvb, offset, next_tree, nest_level+1);
            break;
        case BER_CLASS_APP:
        case BER_CLASS_CON:
        case BER_CLASS_PRI:
        default:
            next_tree = proto_tree_add_subtree_format(tree, tvb, offset, len, ett_ber_SEQUENCE, NULL,
                            "[%s %d]", val_to_str_const(ber_class, ber_class_codes, "Unknown"), tag);
            while (offset < (int)(start_offset + len + hdr_len))
                offset = try_dissect_unknown_ber(pinfo, tvb, offset, next_tree, nest_level+1);
            break;

        }
    }

    return offset;
}

int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_unknown_ber(packet_info *pinfo, tvbuff_t *tvb, int offset, proto_tree *tree)
{
    return try_dissect_unknown_ber(pinfo, tvb, offset, tree, 1);
}

int
call_ber_oid_callback(const char *oid, tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, void* data)
{
    tvbuff_t   *next_tvb;
    const char *syntax = NULL;
    int         len = 0;

    if (!tvb) {
        return offset;
    }

    next_tvb = tvb_new_subset_remaining(tvb, offset);
    if (oid == NULL ||
        ((((syntax = get_ber_oid_syntax(oid)) == NULL) ||
          /* First see if a syntax has been registered for this oid (user defined) */
          (len = dissector_try_string(ber_syntax_dissector_table, syntax, next_tvb, pinfo, tree, data)) == 0) &&
         /* Then try registered oid's */
         (len = dissector_try_string(ber_oid_dissector_table, oid, next_tvb, pinfo, tree, data)) == 0))
    {
        proto_item *item      = NULL;
        proto_tree *next_tree = NULL;
        int         length_remaining;

        /* XXX we should probably use get_ber_length() here */
        length_remaining = tvb_reported_length_remaining(tvb, offset);

        if (oid == NULL) {
            item = proto_tree_add_expert(tree, pinfo, &ei_ber_no_oid, next_tvb, 0, length_remaining);
        } else if (tvb_get_ntohs (tvb, offset) != 0x0500) { /* Not NULL tag */
            if (syntax) {
                item = proto_tree_add_expert_format(
                    tree, pinfo, &ei_ber_syntax_not_implemented, next_tvb, 0, length_remaining,
                    "BER: Dissector for syntax:%s not implemented."
                    " Contact Wireshark developers if you want this supported",
                    syntax);
            } else {
                item = proto_tree_add_expert(tree, pinfo, &ei_ber_oid_not_implemented, next_tvb, 0, length_remaining);
            }
        } else {
            next_tree = tree;
        }
        if (decode_unexpected) {
            int ber_offset;
            int32_t ber_len;

            if (item) {
                next_tree = proto_item_add_subtree(item, ett_ber_unknown);
            }
            ber_offset = get_ber_identifier(next_tvb, 0, NULL, NULL, NULL);
            ber_offset = get_ber_length(next_tvb, ber_offset, &ber_len, NULL);
            if ((ber_len + ber_offset) == length_remaining) {
                /* Decoded an ASN.1 tag with a length indicating this
                 * could be BER encoded data.  Try dissecting as unknown BER.
                 */
                dissect_unknown_ber(pinfo, next_tvb, 0, next_tree);
            } else {
                proto_tree_add_item(next_tree, hf_ber_unknown_data, next_tvb, 0, length_remaining, ENC_NA);
            }
        }
        len = length_remaining;
    }

    offset += len;

    return offset;
}

static int
call_ber_syntax_callback(const char *syntax, tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
    tvbuff_t *next_tvb;
    int       len = 0;

    next_tvb = tvb_new_subset_remaining(tvb, offset);
    if (syntax == NULL ||
        (len = dissector_try_string(ber_syntax_dissector_table, syntax, next_tvb, pinfo, tree, NULL)) == 0)
    {
        proto_item *item = NULL;

        if (syntax == NULL) {
            item = proto_tree_add_expert_format(
                    tree, pinfo, &ei_ber_no_oid, next_tvb, 0, tvb_reported_length_remaining(tvb, offset),
                    "BER Error: No syntax supplied to call_ber_syntax_callback");
        } else {
            item = proto_tree_add_expert_format(
                    tree, pinfo, &ei_ber_syntax_not_implemented, next_tvb, 0, tvb_reported_length_remaining(tvb, offset),
                    "BER: Dissector for syntax:%s not implemented."
                    " Contact Wireshark developers if you want this supported",
                    syntax);
        }
        if (decode_unexpected) {
            proto_tree *unknown_tree = proto_item_add_subtree(item, ett_ber_unknown);
            dissect_unknown_ber(pinfo, next_tvb, 0, unknown_tree);
        }
        len = tvb_reported_length_remaining(tvb, offset);
    }

    offset += len;

    return offset;
}


/* 8.1 General rules for encoding */

/*  8.1.2 Identifier octets */
int
get_ber_identifier(tvbuff_t *tvb, int offset, int8_t *ber_class, bool *pc, int32_t *tag) {
    uint8_t  id, t;
    int8_t   tmp_class;
    bool tmp_pc;
    int32_t  tmp_tag;

    id = tvb_get_uint8(tvb, offset);
    offset += 1;
#ifdef DEBUG_BER
ws_debug_printf("BER ID=%02x", id);
#endif
    /* 8.1.2.2 */
    tmp_class = (id >> 6) & 0x03;
    tmp_pc = (id >> 5) & 0x01;
    tmp_tag = id & 0x1F;
    /* 8.1.2.4 */
    if (tmp_tag == 0x1F) {
        tmp_tag = 0;
        while (tvb_reported_length_remaining(tvb, offset) > 0) {
            t = tvb_get_uint8(tvb, offset);
#ifdef DEBUG_BER
ws_debug_printf(" %02x", t);
#endif
            offset += 1;
            tmp_tag <<= 7;
            tmp_tag |= t & 0x7F;
            if (!(t & 0x80))
                break;
        }
    }

#ifdef DEBUG_BER
ws_debug_printf("\n");
#endif
    if (ber_class)
        *ber_class = tmp_class;
    if (pc)
        *pc  = tmp_pc;
    if (tag)
        *tag = tmp_tag;

    last_class = tmp_class;
    last_pc  = tmp_pc;
    last_tag = tmp_tag;

    return offset;
}

static void
get_last_ber_identifier(int8_t *ber_class, bool *pc, int32_t *tag)
{
    if (ber_class)
        *ber_class = last_class;
    if (pc)
        *pc  = last_pc;
    if (tag)
        *tag = last_tag;

}

int
dissect_ber_identifier(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, int8_t *ber_class, bool *pc, int32_t *tag)
{
    int      old_offset = offset;
    int8_t   tmp_class;
    bool tmp_pc;
    int32_t  tmp_tag;

    offset = get_ber_identifier(tvb, offset, &tmp_class, &tmp_pc, &tmp_tag);

    if (show_internal_ber_fields) {
        proto_tree_add_uint(tree, hf_ber_id_class, tvb, old_offset, 1, tmp_class << 6);
        proto_tree_add_boolean(tree, hf_ber_id_pc, tvb, old_offset, 1, (tmp_pc) ? 0x20 : 0x00);
        if (tmp_tag > 0x1F) {
            if (tmp_class == BER_CLASS_UNI) {
                proto_tree_add_uint(tree, hf_ber_id_uni_tag_ext, tvb, old_offset + 1, offset - (old_offset + 1), tmp_tag);
            } else {
                proto_tree_add_uint(tree, hf_ber_id_tag_ext, tvb, old_offset + 1, offset - (old_offset + 1), tmp_tag);
            }
        } else {
            if (tmp_class == BER_CLASS_UNI) {
                proto_tree_add_uint(tree, hf_ber_id_uni_tag, tvb, old_offset, 1, tmp_tag);
            } else {
                proto_tree_add_uint(tree, hf_ber_id_tag, tvb, old_offset, 1, tmp_tag);
            }
        }
    }

    if (ber_class)
        *ber_class = tmp_class;
    if (pc)
        *pc = tmp_pc;
    if (tag)
        *tag = tmp_tag;

    return offset;
}

/** Try to get the length octets of the BER TLV.
 * Only (TAGs and) LENGTHs that fit inside 32 bit integers are supported.
 *
 * @return true if we have the entire length, false if we're in the middle of
 * an indefinite length and haven't reached EOC.
 */
/* 8.1.3 Length octets */

static int
// NOLINTNEXTLINE(misc-no-recursion)
try_get_ber_length(tvbuff_t *tvb, int offset, uint32_t *length, bool *ind, int nest_level) {
    uint8_t  oct, len;
    uint32_t indef_len;
    uint32_t tmp_length;
    bool     tmp_ind;
    int      tmp_offset, s_offset;
    int8_t   tclass;
    bool tpc;
    int32_t  ttag;

    tmp_length = 0;
    tmp_ind    = false;

    if (nest_level > BER_MAX_NESTING) {
        /* Assume that we have a malformed packet. */
        THROW(ReportedBoundsError);
    }

    oct = tvb_get_uint8(tvb, offset);
    offset += 1;

    if (!(oct & 0x80)) {
        /* 8.1.3.4 */
        tmp_length = oct;
    } else {
        len = oct & 0x7F;
        if (len) {
            /* 8.1.3.5 */
            while (len--) {
                oct = tvb_get_uint8(tvb, offset);
                offset++;
                tmp_length = (tmp_length<<8) + oct;
            }
        } else {
            /* 8.1.3.6 */

            tmp_offset = offset;
            /* ok in here we can traverse the BER to find the length, this will fix most indefinite length issues */
            /* Assumption here is that indefinite length is always used on constructed types*/
            /* check for EOC */
            while (tvb_get_uint8(tvb, offset) || tvb_get_uint8(tvb, offset+1)) {
                /* not an EOC at offset */
                s_offset = offset;
                offset= get_ber_identifier(tvb, offset, &tclass, &tpc, &ttag);
                offset= try_get_ber_length(tvb, offset, &indef_len, NULL, nest_level+1);
                tmp_length += indef_len+(offset-s_offset); /* length + tag and length */
                offset += indef_len;
                                /* Make sure we've moved forward in the packet */
                if (offset <= s_offset)
                    THROW(ReportedBoundsError);
            }
            tmp_length += 2;
            tmp_ind = true;
            offset = tmp_offset;
        }
    }

    /* Several users treat the length as signed value, clamp the value to avoid
     * an overflow to negative values. */
    if (tmp_length > (uint32_t)INT32_MAX)
        tmp_length = (uint32_t)INT32_MAX;

    if (length)
        *length = tmp_length;
    if (ind)
        *ind = tmp_ind;

#ifdef DEBUG_BER
ws_debug_printf("get BER length %d, offset %d (remaining %d)\n", tmp_length, offset, tvb_reported_length_remaining(tvb, offset));
#endif

    return offset;
}

int
get_ber_length(tvbuff_t *tvb, int offset, uint32_t *length, bool *ind)
{
    return try_get_ber_length(tvb, offset, length, ind, 1);
}

static void
get_last_ber_length(uint32_t *length, bool *ind, tvbuff_t **len_tvb, int *len_offset, int *len_len)
{
    if (length)
        *length = last_length;
    if (ind)
        *ind = last_ind;
    if (len_tvb)
        *len_tvb = last_length_tvb;
    if (len_offset)
        *len_offset = last_length_offset;
    if (len_len)
        *len_len = last_length_len;
}

/* this function dissects the length octets of the BER TLV.
 * We only handle (TAGs and) LENGTHs that fit inside 32 bit integers.
 */
int
dissect_ber_length(packet_info *pinfo _U_, proto_tree *tree, tvbuff_t *tvb, int offset, uint32_t *length, bool *ind)
{
    int      old_offset = offset;
    uint32_t tmp_length;
    bool tmp_ind;

    offset = get_ber_length(tvb, offset, &tmp_length, &tmp_ind);

    if (show_internal_ber_fields) {
        if (tmp_ind) {
            proto_tree_add_uint_format_value(tree, hf_ber_length, tvb, old_offset, 1, tmp_length, "Indefinite length %d", tmp_length);
        } else {
            if ((offset - old_offset) > 1) {
                proto_tree_add_uint(tree, hf_ber_length_octets, tvb, old_offset, 1, (tvb_get_uint8(tvb, old_offset) & 0x7f));
                proto_tree_add_uint(tree, hf_ber_length, tvb, old_offset+1, offset - (old_offset+1), tmp_length);
            } else {
                proto_tree_add_uint(tree, hf_ber_length, tvb, old_offset, offset - old_offset, tmp_length);
            }
        }
    }
    if (length)
        *length = tmp_length;
    if (ind)
        *ind = tmp_ind;

#ifdef DEBUG_BER
proto_tree_add_debug_text(tree, "dissect BER length %d, offset %d (remaining %d)\n", tmp_length, offset, tvb_reported_length_remaining(tvb, offset));
#endif

    last_length = tmp_length;
    last_ind = tmp_ind;
    last_length_tvb = tvb;
    last_length_offset = old_offset;
    last_length_len = offset - old_offset;

    return offset;
}

static reassembly_table octet_segment_reassembly_table;

static int
dissect_ber_constrained_octet_string_impl(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int32_t min_len, int32_t max_len, int hf_id, tvbuff_t **out_tvb, unsigned nest_level, unsigned encoding);

static int
// NOLINTNEXTLINE(misc-no-recursion)
reassemble_octet_string(asn1_ctx_t *actx, proto_tree *tree, int hf_id, tvbuff_t *tvb, int offset, uint32_t con_len, bool ind, tvbuff_t **out_tvb, unsigned nest_level)
{
    fragment_head *fd_head         = NULL;
    tvbuff_t      *next_tvb        = NULL;
    tvbuff_t      *reassembled_tvb = NULL;
    uint16_t       dst_ref         = 0;
    int            start_offset    = offset;
    bool       fragment        = true;
    bool       firstFragment   = true;

    if (nest_level > BER_MAX_NESTING) {
        /* Assume that we have a malformed packet. */
        THROW(ReportedBoundsError);
    }

    /* so we need to consume octet strings for the given length */

    if (out_tvb)
        *out_tvb = NULL;

    if (con_len == 0) /* Zero encodings (8.7.3) */
        return offset;

    /* not sure we need this */
    actx->pinfo->fragmented = true;

    while(!fd_head) {

        offset = dissect_ber_constrained_octet_string_impl(false, actx, NULL,
                tvb, offset, NO_BOUND, NO_BOUND, hf_id, &next_tvb, nest_level + 1, 0);

        if (next_tvb == NULL) {
            /* Assume that we have a malformed packet. */
            THROW(ReportedBoundsError);
        }

        if (ind) {
            /* this was indefinite length - so check for EOC */

            if ((tvb_get_uint8(tvb, offset) == 0) && (tvb_get_uint8(tvb, offset+1) == 0)) {
                fragment = false;
                /* skip past EOC */
                offset +=2;
            }
        } else {

            if ((uint32_t)(offset - start_offset) >= con_len)
                fragment = false;
        }

        if (!fragment && firstFragment) {
            /* there is only one fragment (I'm sure there's a reason it was constructed) */
            /* anyway, we can get out of here */
            bool pc;
            get_ber_identifier(tvb, start_offset, NULL, &pc, NULL);
            if (!pc && tree) {
                /* Only display here if not constructed */
                dissect_ber_octet_string(false, actx, tree, tvb, start_offset, hf_id, NULL);
            }
            reassembled_tvb = next_tvb;
            break;
        }


        if (tvb_reported_length(next_tvb) < 1) {
            /* Don't cause an assertion in the reassembly code. */
            THROW(ReportedBoundsError);
        }
        fd_head = fragment_add_seq_next(&octet_segment_reassembly_table,
                                        next_tvb, 0, actx->pinfo,
                                        (dst_ref | nest_level << 16), NULL,
                                        tvb_reported_length(next_tvb),
                                        fragment);

        firstFragment = false;
    }

    if (fd_head) {
        if (fd_head->next) {
            /* not sure I really want to do this here - should be nearer the application where we can give it a better name*/
            proto_tree *next_tree;
            proto_item *frag_tree_item;

            reassembled_tvb = tvb_new_chain(next_tvb, fd_head->tvb_data);

            actx->created_item = proto_tree_add_item(tree, hf_id, reassembled_tvb, 0, -1, ENC_BIG_ENDIAN);
            next_tree = proto_item_add_subtree (actx->created_item, ett_ber_reassembled_octet_string);

            add_new_data_source(actx->pinfo, reassembled_tvb, "Reassembled OCTET STRING");
            show_fragment_seq_tree(fd_head, &octet_string_frag_items, next_tree, actx->pinfo, reassembled_tvb, &frag_tree_item);
        }
    }

    if (out_tvb)
        *out_tvb = reassembled_tvb;

    /* again - not sure we need this */
    actx->pinfo->fragmented = false;

    return offset;

}

/* 8.7 Encoding of an octetstring value */
int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_constrained_octet_string(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int32_t min_len, int32_t max_len, int hf_id, tvbuff_t **out_tvb) {
  return dissect_ber_constrained_octet_string_impl(implicit_tag, actx, tree, tvb, offset, min_len, max_len, hf_id, out_tvb, 0, 0);
}

static int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_constrained_octet_string_impl(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int32_t min_len, int32_t max_len, int hf_id, tvbuff_t **out_tvb, unsigned nest_level, unsigned encoding) {
    int8_t      ber_class;
    bool        pc, ind;
    int32_t     tag;
    int         identifier_offset;
    int         identifier_len;
    uint32_t    len;
    tvbuff_t   *len_tvb;
    int         len_offset;
    int         len_len;
    int         hoffset;
    int         end_offset;
    proto_item *it, *cause;
    uint32_t    len_remain;

#ifdef DEBUG_BER
{
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(tvb, offset) > 3) {
proto_tree_add_debug_text(tree, "OCTET STRING dissect_ber_octet string(%s) entered implicit_tag:%d offset:%d len:%d %02x:%02x:%02x\n", name, implicit_tag, offset, tvb_reported_length_remaining(tvb, offset), tvb_get_uint8(tvb, offset), tvb_get_uint8(tvb, offset+1), tvb_get_uint8(tvb, offset+2));
} else {
proto_tree_add_debug_text(tree, "OCTET STRING dissect_ber_octet_string(%s) entered\n", name);
}
}
#endif

    if (out_tvb)
        *out_tvb = NULL;

    if (!implicit_tag) {
        hoffset = offset;
        /* read header and len for the octet string */
        identifier_offset = offset;
        offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
        identifier_len = offset - identifier_offset;
        offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, &ind);
        end_offset = offset+len;

        /* sanity check: we only handle Constructed Universal Sequences */
        if ((ber_class != BER_CLASS_APP) && (ber_class != BER_CLASS_PRI)) {
            if ( (ber_class != BER_CLASS_UNI)
              || ((tag < BER_UNI_TAG_NumericString) && (tag != BER_UNI_TAG_OCTETSTRING) && (tag != BER_UNI_TAG_UTF8String)) ) {
                tvb_ensure_bytes_exist(tvb, hoffset, 2);
                cause = proto_tree_add_expert_format(
                    tree, actx->pinfo, &ei_ber_expected_octet_string,
                    tvb, identifier_offset, identifier_len,
                    "BER Error: OctetString expected but class:%s(%d) %s tag:%d was unexpected",
                    val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                    ber_class,
                    tfs_get_string(pc, &tfs_constructed_primitive),
                    tag);
                if (decode_unexpected) {
                    proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                    dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
                }
                return end_offset;
            }
        }
    } else {
        /* implicit tag so get from last tag/length */

        get_last_ber_identifier(&ber_class, &pc, &tag);
        get_last_ber_length(&len, &ind, &len_tvb, &len_offset, &len_len);

        end_offset = offset+len;

        /* caller may have created new buffer for indefinite length data Verify via length */
        len_remain = (uint32_t)tvb_reported_length_remaining(tvb, offset);
        if (ind && (len_remain == (len - 2))) {
            /* new buffer received so adjust length and indefinite flag */
            len -= 2;
            end_offset -= 2;
            ind = false;
        } else if (len_remain < len) {
            /*
             * error - short frame, or this item runs past the
             * end of the item containing it
             */
            ber_add_large_length_error(actx->pinfo, tree, tvb, offset, len, len_tvb, len_offset, len_len);
            return end_offset;
        }

    }

    actx->created_item = NULL;

    if (pc) {
        /* constructed */
        end_offset = reassemble_octet_string(actx, tree, hf_id, tvb, offset, len, ind, out_tvb, nest_level);
    } else {
        /* primitive */
        int length_remaining;

        length_remaining = tvb_reported_length_remaining(tvb, offset);
#if 0
        if (length_remaining < 1) {
            return end_offset;
        }
#endif

        if (len <= (uint32_t)length_remaining) {
            length_remaining = len;
        }
        if (hf_id > 0) {
            /*
             * Strings are special.  See X.680 section 41 "Definition of
             * restricted character string types" and X.690 section 8.20
             * "Encoding for values of the restricted character string
             * types".
             *
             * Some restricted character string types are defined in X.680
             * "by reference to a registration number in the ISO International
             * Register of Character Sets" - or, in Table 8, by multiple
             * registration numbers, or one or more registration numbers
             * plus some characters such as SPACE and/or DELETE, or a
             * reference to "all G sets" or "all G sets and all C sets".
             * Presumably this indicates which characters are allowed
             * in strings of those character types, with "all {G,C} sets"
             * meaning the only restriction is to character sets registered
             * in said registry.
             *
             * The encodings of those types are specified in X.690 as
             * containing "the octets specified in ISO/IEC 2022 for
             * encodings in an 8-bit environment, using the escape sequence
             * and character codings registered in accordance with ISO/IEC
             * 2375".
             *
             * ISO/IEC 2022 is also ECMA-35:
             *
             *    http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-035.pdf
             *
             * ISO/IEC 2375 is the procedure for registering character
             * codings in the ISO International Register of Character Sets.
             * See
             *
             *     http://kikaku.itscj.ipsj.or.jp/ISO-IR/
             *
             * and
             *
             *     http://kikaku.itscj.ipsj.or.jp/ISO-IR/overview.htm
             *
             * for that registry.
             *
             * If we've been provided with a non-zero encoding, use
             * that; otherwise, calculate it based on the tag.  (A
             * zero encoding is ENC_ASCII|ENC_NA/ENC_BIG_ENDIAN, which
             * is the default, so it's OK to use here; this is for
             * protcols such as LDAP that use OCTET STRING for UTF-8
             * strings.)
             */
            if (encoding == 0) {
                switch (tag) {

                case BER_UNI_TAG_UTF8String:
                    /*
                     * UTF-8, obviously.
                     */
                    encoding = ENC_UTF_8|ENC_NA;
                    break;

                case BER_UNI_TAG_NumericString:
                case BER_UNI_TAG_PrintableString:
                case BER_UNI_TAG_VisibleString:
                case BER_UNI_TAG_IA5String:
                    /*
                     * (Subsets of) Boring Old ASCII, with no(?) ISO 2022
                     * escape sequences.
                     */
                    encoding = ENC_ASCII|ENC_NA;
                    break;

                case BER_UNI_TAG_TeletexString:
                    encoding = ENC_T61|ENC_NA;
                    break;

                case BER_UNI_TAG_VideotexString:
                    encoding = ENC_T61|ENC_NA;
                    break;

                case BER_UNI_TAG_GraphicString:
                case BER_UNI_TAG_GeneralString:
                    /*
                     * One of the types defined in terms of character sets
                     * in the ISO International Register of Character Sets,
                     * with the BER encoding being ISO 2022-based.
                     *
                     * XXX - treat as ASCII for now.
                     */
                    encoding = ENC_ASCII|ENC_NA;
                    break;

                case BER_UNI_TAG_UniversalString:
                    /*
                     * UCS-4.
                     */
                    encoding = ENC_UCS_4|ENC_BIG_ENDIAN;
                    break;

                case BER_UNI_TAG_CHARACTERSTRING:
                    /*
                     * XXX - what's the transfer syntax?
                     * Treat as ASCII for now.
                     */
                    encoding = ENC_ASCII|ENC_NA;
                    break;

                case BER_UNI_TAG_BMPString:
                    /*
                     * UCS-2, not UTF-16; as it says, BMP, as in Basic
                     * Multilingual Plane.
                     */
                    encoding = ENC_UCS_2|ENC_BIG_ENDIAN;
                    break;

                default:
                     encoding = ENC_BIG_ENDIAN;
                     break;
                }
            }
            it = ber_proto_tree_add_item(actx->pinfo, tree, hf_id, tvb, offset, length_remaining, encoding);
            actx->created_item = it;
            ber_check_length(length_remaining, min_len, max_len, actx, it, false);
        } else {

            proto_tree_add_item(tree, hf_ber_unknown_octetstring, tvb, offset, len, ENC_NA);
        }

        if (out_tvb) {
            *out_tvb = ber_tvb_new_subset_length(tvb, offset, len);
        }
    }
    return end_offset;
}

int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_octet_string(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, tvbuff_t **out_tvb) {
  return dissect_ber_constrained_octet_string_impl(implicit_tag, actx, tree, tvb, offset, NO_BOUND, NO_BOUND, hf_id, out_tvb, 0, 0);
}

int
dissect_ber_octet_string_with_encoding(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, tvbuff_t **out_tvb, unsigned encoding) {
  return dissect_ber_constrained_octet_string_impl(implicit_tag, actx, tree, tvb, offset, NO_BOUND, NO_BOUND, hf_id, out_tvb, 0, encoding);
}

int
dissect_ber_octet_string_wcb(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, ber_callback func)
{
    tvbuff_t *out_tvb = NULL;

    offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_id, (func) ? &out_tvb : NULL);
    if (func && out_tvb && (tvb_reported_length(out_tvb) > 0)) {
        if (hf_id > 0)
            tree = proto_item_add_subtree(actx->created_item, ett_ber_octet_string);
        /* TODO Should hf_id2 be passed as last parameter???*/
        func(false, out_tvb, 0, actx, tree, -1);
    }
    return offset;
}

/* 8.8 Encoding of a null value */
int
dissect_ber_null(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id) {
    int8_t      ber_class;
    bool    pc;
    int32_t     tag;
    int         identifier_offset;
    int         identifier_len;
    uint32_t    len;
    int         len_offset;
    int         len_len;

    if (!implicit_tag) {
        identifier_offset = offset;
        offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
        identifier_len = offset - identifier_offset;
        if (pc ||
            ((ber_class != BER_CLASS_UNI) || (tag != BER_UNI_TAG_NULL))) {
            proto_tree_add_expert_format(
                tree, actx->pinfo, &ei_ber_expected_null,
                tvb, identifier_offset, identifier_len,
                "BER Error: NULL expected but class:%s(%d) %s tag:%d was unexpected",
                val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                ber_class,
                tfs_get_string(pc, &tfs_constructed_primitive),
                tag);
        }

        len_offset = offset;
        offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, NULL);
        len_len = offset - len_offset;
        if (len) {
            proto_tree_add_expert_format(
                tree, actx->pinfo, &ei_ber_expected_null_zero_length,
                tvb, len_offset, len_len,
                "BER Error: NULL type expects zero length data but Length=%d",
                len);
            proto_tree_add_item(tree, hf_ber_extra_data, tvb, offset, len, ENC_NA);
            offset += len;
        }
    }
    if (hf_id > 0)
        proto_tree_add_item(tree, hf_id, tvb, offset, 0, ENC_BIG_ENDIAN);
    return offset;
}

int
dissect_ber_integer64(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, int64_t *value)
{
    int8_t   ber_class;
    bool pc;
    int32_t  tag;
    uint32_t len;
    int64_t  val;
    uint32_t i;
    bool used_too_many_bytes = false;
    uint8_t first = 0;
#ifdef DEBUG_BER
{
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(tvb, offset) > 3) {
proto_tree_add_debug_text(tree, "INTEGERnew dissect_ber_integer(%s) entered implicit_tag:%d offset:%d len:%d %02x:%02x:%02x\n", name, implicit_tag, offset, tvb_reported_length_remaining(tvb, offset), tvb_get_uint8(tvb, offset), tvb_get_uint8(tvb, offset+1), tvb_get_uint8(tvb, offset+2));
} else {
proto_tree_add_debug_text(tree, "INTEGERnew dissect_ber_integer(%s) entered implicit_tag:%d \n", name, implicit_tag);
}
}
#endif


    if (value) {
        *value = 0;
    }

    if (!implicit_tag) {
      offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
      offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, NULL);
    } else {
      int32_t remaining = tvb_reported_length_remaining(tvb, offset);
      len = remaining>0 ? remaining : 0;
    }

    val = 0;
    if (len > 0) {

        first = tvb_get_uint8(tvb, offset);
        /* we can't handle integers > 64 bits */
        /* take into account the use case of a 64bits unsigned integer: you will have a 9th byte set to 0 */
        if ((len > 9) || ((len == 9) && (first != 0))) {
            if (hf_id > 0) {
                header_field_info *hfinfo = proto_registrar_get_nth(hf_id);

                /* use the original field only if it is suitable for bytes */
                if (hfinfo->type != FT_BYTES)
                    hf_id = hf_ber_64bit_uint_as_bytes;

                proto_tree_add_bytes_format(tree, hf_id, tvb, offset, len, NULL,
                                            "%s: 0x%s", hfinfo->name, tvb_bytes_to_str(actx->pinfo->pool, tvb, offset, len));
            }

            offset += len;
            return offset;
        }
        /* extend sign bit for signed fields */
        enum ftenum type  = FT_INT32; /* Default to signed, is this correct? */
        if (hf_id > 0) {
            type = proto_registrar_get_ftype(hf_id);
        }
        if (first & 0x80 && FT_IS_INT(type)) {
            val = -1;
        }
        if ((len > 1) && decode_warning_leading_zero_bits) {
            uint8_t second = tvb_get_uint8(tvb, offset+1);
            if (((first == 0x00) && ((second & 0x80) == 0)) ||
                ((first == 0xff) && ((second & 0x80) != 0))) {
                used_too_many_bytes = true;
            }
        }
        for (i=0; i<len; i++) {
            val = ((uint64_t)val<<8) | tvb_get_uint8(tvb, offset);
            offset++;
        }
    }

    actx->created_item = NULL;

    if (hf_id > 0) {
        /*  */
        header_field_info* hfi;

        hfi = proto_registrar_get_nth(hf_id);
        if ((len < 1) || (len > 9) || ((len == 9) && (first != 0))) {
          proto_tree_add_expert_format(
              tree, actx->pinfo, &ei_ber_error_length, tvb, offset-len, len,
              "BER Error: %s: length of item (%u) is not valid", hfi->name, len);
        } else {
            switch (hfi->type) {
            case FT_UINT8:
            case FT_UINT16:
            case FT_UINT24:
            case FT_UINT32:
                actx->created_item = proto_tree_add_uint(tree, hf_id, tvb, offset-len, len, (uint32_t)val);
                break;
            case FT_INT8:
            case FT_INT16:
            case FT_INT24:
            case FT_INT32:
                actx->created_item = proto_tree_add_int(tree, hf_id, tvb, offset-len, len, (int32_t)val);
                break;
            case FT_INT64:
                actx->created_item = proto_tree_add_int64(tree, hf_id, tvb, offset-len, len, val);
                break;
            case FT_UINT64:
                actx->created_item = proto_tree_add_uint64(tree, hf_id, tvb, offset-len, len, (uint64_t)val);
                break;
            case FT_BYTES:
                /*
                 * Some protocols have INTEGER fields that can store values
                 * larger than 64 bits and therefore have to use FT_BYTES.
                 * Values larger than 64 bits are handled above while smaller
                 * values are handled here.
                 */
                actx->created_item = proto_tree_add_bytes_format(tree, hf_id, tvb, offset-len, len, NULL,
                        "%s: 0x%s", hfi->name, tvb_bytes_to_str(actx->pinfo->pool, tvb, offset-len, len));
                break;
            default:
                DISSECTOR_ASSERT_NOT_REACHED();
            }

            if (used_too_many_bytes) {
                expert_add_info_format(
                    actx->pinfo, actx->created_item, &ei_ber_value_too_many_bytes,
                    "Value is encoded with too many bytes(9 leading zero or one bits), hf_abbr: %s",
                    hfi->abbrev);
            }
        }
    }

    if (value) {
        *value = val;
    }

    return offset;
}

int
dissect_ber_constrained_integer64(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int64_t min_len, int64_t max_len, int hf_id, int64_t *value)
{
    int64_t val;

    offset = dissect_ber_integer64(implicit_tag, actx, tree, tvb, offset, hf_id, &val);
    if (value) {
        *value = val;
    }

    ber_check_value64 (val, min_len, max_len, actx, actx->created_item);

    return offset;
}

int
dissect_ber_integer(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, uint32_t *value)
{
    int64_t val;

    offset = dissect_ber_integer64(implicit_tag, actx, tree, tvb, offset, hf_id, &val);
    if (value) {
        *value = (uint32_t)val;
    }

    return offset;
}

int
dissect_ber_constrained_integer(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int32_t min_len, int32_t max_len, int hf_id, uint32_t *value)
{
    int64_t val;

    offset = dissect_ber_integer64(implicit_tag, actx, tree, tvb, offset, hf_id, &val);
    if (value) {
        *value = (uint32_t)val;
    }

    ber_check_value ((uint32_t)val, min_len, max_len, actx, actx->created_item);

    return offset;
}

int
dissect_ber_boolean(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, bool *value)
{
    int8_t   ber_class;
    bool pc;
    int32_t  tag;
    uint32_t len;
    uint8_t  val;
    header_field_info *hfi;

    if (!implicit_tag) {
        offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
        offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, NULL);
        /*if (ber_class != BER_CLASS_UNI)*/
    } else {
        int32_t remaining = tvb_reported_length_remaining(tvb, offset);
        len = remaining > 0 ? remaining : 0;
    }

    if (len == 1)
    {
        val = tvb_get_uint8(tvb, offset);
        offset += 1;

        actx->created_item = NULL;

        if (hf_id > 0) {
            hfi = proto_registrar_get_nth(hf_id);
            if (hfi->type == FT_BOOLEAN)
                actx->created_item = proto_tree_add_boolean(tree, hf_id, tvb, offset-1, 1, val);
            else
                actx->created_item = proto_tree_add_uint(tree, hf_id, tvb, offset-1, 1, val ? 1 : 0);
        }
    } else {
        val = 0;
        actx->created_item = NULL;

        if (hf_id > 0) {
            hfi = proto_registrar_get_nth(hf_id);
            proto_tree_add_expert_format(
                    tree, actx->pinfo, &ei_ber_error_length, tvb, offset, len,
                    "BER Error: %s: length of item (%u) is not valid", hfi->name, len);
        }
    }

    if (value) {
        *value = (val ? true : false);
    }

    return offset;
}


/* 8.5  Encoding of a real value */
/* Somewhat tested */
int
dissect_ber_real(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, double *value)
{
    int8_t   ber_class;
    bool pc;
    int32_t  tag;
    uint32_t val_length = 0, len_remain, end_offset;
    tvbuff_t *len_tvb;
    int      len_offset;
    int      len_len;
    double   val        = 0;

    if (!implicit_tag) {
        offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
        offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &val_length, NULL);

        end_offset = offset + val_length;
    } else {
        /* implicit tag so get from last tag/length */
        get_last_ber_identifier(&ber_class, &pc, &tag);
        get_last_ber_length(&val_length, NULL, &len_tvb, &len_offset, &len_len);

        end_offset = offset + val_length;

        /* Check is buffer has (at least) the expected length */
        len_remain = (uint32_t)tvb_reported_length_remaining(tvb, offset);
        if (len_remain < val_length) {
            /* error - this item runs past the end of the item containing it */
            ber_add_large_length_error(actx->pinfo, tree, tvb, offset, val_length, len_tvb, len_offset, len_len);
            return end_offset;
        }
    }
    /* 8.5.1    The encoding of a real value shall be primitive. */
    if(pc) {
      /*  Constructed (not primitive) */
      proto_tree_add_expert(
          tree, actx->pinfo, &ei_ber_real_not_primitive, tvb, offset - 2, 1);
    }

    val = asn1_get_real(tvb_get_ptr(tvb, offset, val_length), val_length);
    actx->created_item = proto_tree_add_double(tree, hf_id, tvb, end_offset - val_length, val_length, val);

    if (value)
        *value = val;

    return end_offset;

}
/* this function dissects a BER sequence
 */
int
dissect_ber_sequence(bool implicit_tag, asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, const ber_sequence_t *seq, int hf_id, int ett_id) {
    int8_t      classx;
    bool    pcx, ind   = 0, ind_field, imp_tag = false;
    int32_t     tagx;
    int         identifier_offset;
    int         identifier_len;
    uint32_t    lenx;
    proto_tree *tree       = parent_tree;
    proto_item *item       = NULL;
    proto_item *cause;
    int         end_offset = 0;
    int         hoffset;
    tvbuff_t   *next_tvb;

#ifdef DEBUG_BER
{
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(tvb, offset) > 3) {
proto_tree_add_debug_text(tree, "SEQUENCE dissect_ber_sequence(%s) entered offset:%d len:%d %02x:%02x:%02x\n", name, offset, tvb_reported_length_remaining(tvb, offset), tvb_get_uint8(tvb, offset), tvb_get_uint8(tvb, offset+1), tvb_get_uint8(tvb, offset+2));
} else {
proto_tree_add_debug_text(tree, "SEQUENCE dissect_ber_sequence(%s) entered\n", name);
}
}
#endif
    hoffset = offset;
    if (!implicit_tag) {
        offset = get_ber_identifier(tvb, offset, NULL, NULL, NULL);
        offset = get_ber_length(tvb, offset, &lenx, NULL);
    } else {
        /* was implicit tag so just use the length of the tvb */
        lenx = tvb_reported_length_remaining(tvb, offset);
        end_offset = offset+lenx;
    }
    /* create subtree */
    if (hf_id > 0) {
        if (parent_tree) {
            item = proto_tree_add_item(parent_tree, hf_id, tvb, hoffset, lenx + offset - hoffset, ENC_BIG_ENDIAN);
            tree = proto_item_add_subtree(item, ett_id);
        }
    }
    offset = hoffset;

    if (!implicit_tag) {
        /* first we must read the sequence header */
        identifier_offset = offset;
        offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &classx, &pcx, &tagx);
        identifier_len = offset - identifier_offset;
        offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &lenx, &ind);
        if (ind) {
        /*  Fixed the length is correctly returned from dissect ber_length
          end_offset = tvb_reported_length(tvb);*/
          end_offset = offset + lenx -2;
        } else {
          end_offset = offset + lenx;
        }

        /* sanity check: we only handle Constructed Universal Sequences */
        if ((classx != BER_CLASS_APP) && (classx != BER_CLASS_PRI)) {
            if (!pcx
             || ((classx != BER_CLASS_UNI) || (tagx != BER_UNI_TAG_SEQUENCE))) {
                tvb_ensure_bytes_exist(tvb, hoffset, 2);
                cause = proto_tree_add_expert_format(
                    tree, actx->pinfo, &ei_ber_expected_sequence,
                    tvb, identifier_offset, identifier_len,
                    "BER Error: Sequence expected but class:%s(%d) %s tag:%d was unexpected",
                    val_to_str_const(classx, ber_class_codes, "Unknown"),
                    classx,
                    tfs_get_string(pcx, &tfs_constructed_primitive),
                    tagx);
                if (decode_unexpected) {
                    proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                    dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
                }
                return end_offset;
            }
        }
    }
    if(offset == end_offset){
        proto_item_append_text(item, " [0 length]");
    }
    /* loop over all entries until we reach the end of the sequence */
    while (offset < end_offset) {
        int8_t   ber_class;
        bool pc;
        int32_t  tag;
        uint32_t len;
        int      eoffset, count;

        /*if (ind) {  this sequence was of indefinite length, if this is implicit indefinite impossible maybe
                    but ber dissector uses this to eat the tag length then pass into here... EOC still on there...*/
            if ((tvb_get_uint8(tvb, offset) == 0) && (tvb_get_uint8(tvb, offset+1) == 0)) {
                /* If the first bytes is 00 00 of a indefenert length field it's a zero length field*/
                offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
                dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, &ind);
                proto_item_append_text(item, " 0 items");
                return end_offset;
                /*
                if (show_internal_ber_fields) {
                    proto_tree_add_expert(tree, pinfo, &ei_ber_error_seq_eoc, tvb, s_offset, offset+2, "ERROR WRONG SEQ EOC");
                }
                return end_offset;
                */
            }
        /* } */
        hoffset = offset;
        /* read header and len for next field */
        offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
        offset = get_ber_length(tvb, offset, &len, &ind_field);
        eoffset = offset + len;
                /* Make sure we move forward */
        if (eoffset <= hoffset)
            THROW(ReportedBoundsError);

        /*if (ind_field && (len == 2)) {
                / disgusting indefinite length zero length field, what are these people doing /
            offset = eoffset;
            continue;
        }
        */

ber_sequence_try_again:
        /* have we run out of known entries in the sequence ?*/
        if (!seq->func) {
            /* it was not,  move to the next one and try again */
            offset = dissect_ber_identifier(actx->pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
            offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, NULL);
            cause = proto_tree_add_expert_format(
                tree, actx->pinfo, &ei_ber_unknown_field_sequence,
                tvb, hoffset, ((offset - hoffset) + len),
                "BER Error: This field lies beyond the end of the known sequence definition.");
            if (decode_unexpected) {
                proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
            }
            offset = eoffset;
            continue;
        }

        /* Verify that this one is the one we want.
         * Skip check completely if ber_class == ANY
         * of if NOCHKTAG is set
         */
/* XXX Bug in asn2wrs,
 * for   scope            [7]  Scope OPTIONAL,
 * it generates
 *   { BER_CLASS_CON, 7, BER_FLAGS_OPTIONAL|BER_FLAGS_NOTCHKTAG, dissect_scope },
 * and there should not be a NOTCHKTAG here
 */
        if ( ((seq->ber_class == BER_CLASS_CON) || (seq->ber_class == BER_CLASS_APP) || (seq->ber_class == BER_CLASS_PRI))
          && (!(seq->flags & BER_FLAGS_NOOWNTAG)) ) {
            if ( (seq->ber_class != BER_CLASS_ANY)
             &&  (seq->tag != -1)
             &&  ( (seq->ber_class != ber_class)
                || (seq->tag != tag) ) ) {
                /* it was not,  move to the next one and try again */
                if (seq->flags & BER_FLAGS_OPTIONAL) {
                    /* well this one was optional so just skip to the next one and try again. */
                    seq++;
                    goto ber_sequence_try_again;
                }
                identifier_offset = hoffset;
                offset = dissect_ber_identifier(actx->pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
                identifier_len = offset - identifier_offset;
                dissect_ber_length(actx->pinfo, tree, tvb, offset, NULL, NULL);
                if (seq->ber_class == BER_CLASS_UNI) {
                    cause = proto_tree_add_expert_format(
                        tree, actx->pinfo, &ei_ber_sequence_field_wrong,
                        tvb, identifier_offset, identifier_len,
                        "BER Error: Wrong field in SEQUENCE: expected class:%s(%d) tag:%d (%s) but found class:%s(%d) tag:%d",
                        val_to_str_const(seq->ber_class, ber_class_codes, "Unknown"),
                        seq->ber_class,
                        seq->tag,
                        val_to_str_ext_const(seq->tag, &ber_uni_tag_codes_ext, "Unknown"),
                        val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                        ber_class,
                        tag);
                } else {
                    cause = proto_tree_add_expert_format(
                        tree, actx->pinfo, &ei_ber_sequence_field_wrong,
                        tvb, identifier_offset, identifier_len,
                        "BER Error: Wrong field in SEQUENCE: expected class:%s(%d) tag:%d but found class:%s(%d) tag:%d",
                        val_to_str_const(seq->ber_class, ber_class_codes, "Unknown"),
                        seq->ber_class,
                        seq->tag,
                        val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                        ber_class,
                        tag);
                }
                if (decode_unexpected) {
                    proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                    dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
                }
                seq++;
                offset = eoffset;
                continue;
            }
        } else if (!(seq->flags & BER_FLAGS_NOTCHKTAG)) {
            if ( (seq->ber_class != BER_CLASS_ANY)
              && (seq->tag != -1)
              && ( (seq->ber_class != ber_class)
                || (seq->tag != tag) ) ) {
                /* it was not,  move to the next one and try again */
                if (seq->flags & BER_FLAGS_OPTIONAL) {
                    /* well this one was optional so just skip to the next one and try again. */
                    seq++;
                    goto ber_sequence_try_again;
                }

                identifier_offset = hoffset;
                offset = dissect_ber_identifier(actx->pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
                identifier_len = offset - identifier_offset;
                dissect_ber_length(actx->pinfo, tree, tvb, offset, NULL, NULL);
                if ( seq->ber_class == BER_CLASS_UNI) {
                    cause = proto_tree_add_expert_format(
                        tree, actx->pinfo, &ei_ber_sequence_field_wrong,
                        tvb, identifier_offset, identifier_len,
                        "BER Error: Wrong field in SEQUENCE: expected class:%s(%d) tag:%d(%s) but found class:%s(%d) tag:%d",
                        val_to_str_const(seq->ber_class, ber_class_codes, "Unknown"),
                        seq->ber_class,
                        seq->tag,
                        val_to_str_ext_const(seq->tag, &ber_uni_tag_codes_ext, "Unknown"),
                        val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                        ber_class, tag);
                } else {
                    cause = proto_tree_add_expert_format(
                        tree, actx->pinfo, &ei_ber_sequence_field_wrong,
                        tvb, identifier_offset, identifier_len,
                        "BER Error: Wrong field in SEQUENCE: expected class:%s(%d) tag:%d but found class:%s(%d) tag:%d",
                        val_to_str_const(seq->ber_class, ber_class_codes, "Unknown"),
                        seq->ber_class,
                        seq->tag,
                        val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                        ber_class,
                        tag);
                }
                if (decode_unexpected) {
                    proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                    dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
                }
                seq++;
                offset = eoffset;
                continue;
            }
        }

        if (!(seq->flags & BER_FLAGS_NOOWNTAG) ) {
            /* dissect header and len for field */
            if (ind_field && (len == 2)) {
                /* This is a Zero length field */
                next_tvb = ber_tvb_new_subset_length(tvb, offset, len);
                hoffset = eoffset;
            } else {
                hoffset = dissect_ber_identifier(actx->pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
                hoffset = dissect_ber_length(actx->pinfo, tree, tvb, hoffset, NULL, NULL);
                next_tvb = ber_tvb_new_subset_length(tvb, hoffset, eoffset - hoffset - (ind_field ? 2 : 0));
            }
        } else {
            next_tvb = ber_tvb_new_subset_length(tvb, hoffset, eoffset - hoffset);
        }

#if 0
        /* call the dissector for this field */
        if ((eoffset-hoffset) > length_remaining) {
        /* If the field is indefinite (i.e. we don't know the
         * length) of if the tvb is short, then just
         * give it all of the tvb and hope for the best.
         */
            next_tvb = tvb_new_subset_remaining(tvb, hoffset);
        } else {

        }
#endif

#ifdef DEBUG_BER
{
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(next_tvb, 0) > 3) {
proto_tree_add_debug_text(tree, "SEQUENCE dissect_ber_sequence(%s) calling subdissector offset:%d len:%d %02x:%02x:%02x\n", name, offset, tvb_reported_length_remaining(next_tvb, 0), tvb_get_uint8(next_tvb, 0), tvb_get_uint8(next_tvb, 1), tvb_get_uint8(next_tvb, 2));
} else {
proto_tree_add_debug_text(tree, "SEQUENCE dissect_ber_sequence(%s) calling subdissector\n", name);
}
}
#endif
        if (next_tvb == NULL) {
            /* Assume that we have a malformed packet. */
            THROW(ReportedBoundsError);
        }
        imp_tag = false;
        if (seq->flags & BER_FLAGS_IMPLTAG) {
            imp_tag = true;
        }

        count = seq->func(imp_tag, next_tvb, 0, actx, tree, *seq->p_id);

#ifdef DEBUG_BER
{
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
proto_tree_add_debug_text(tree, "SEQUENCE dissect_ber_sequence(%s) subdissector ate %d bytes\n", name, count);
}
#endif
        /* if it was optional and no bytes were eaten and it was */
        /* supposed to (len <> 0), just try again. */
        if ((len != 0) && (count == 0) && (seq->flags & BER_FLAGS_OPTIONAL)) {
            seq++;
            goto ber_sequence_try_again;
        /* move the offset to the beginning of the next sequenced item */
        }
        offset = eoffset;
        if (!(seq->flags & BER_FLAGS_NOOWNTAG) ) {
            /* if we stripped the tag and length we should also strip the EOC is ind_len
             * Unless it's a zero length field (len = 2)
             */
            if ((ind_field == 1) && (len > 2))
            {
                /* skip over EOC */
                if (show_internal_ber_fields) {
                    proto_tree_add_item(tree, hf_ber_seq_field_eoc, tvb, offset-2, 2, ENC_NA);
                }
            }
        }
        seq++;
    }

    /* if we didn't end up at exactly offset, then we ate too many bytes */
    if (offset != end_offset) {
        tvb_ensure_bytes_exist(tvb, offset-2, 2);
        proto_tree_add_expert_format(
            tree, actx->pinfo, &ei_ber_error_length, tvb, offset-2, 2,
            "BER Error: SEQUENCE is %d too many bytes long",
            offset - end_offset);
    }
    if (ind) {
        /*  need to eat this EOC
        end_offset = tvb_reported_length(tvb);*/
        end_offset += 2;
        if (show_internal_ber_fields) {
            proto_tree_add_item(tree, hf_ber_seq_eoc, tvb, end_offset-2, 2, ENC_NA);
        }
    }
    return end_offset;
}

/* This function dissects a BER set
 */
int
dissect_ber_set(bool implicit_tag, asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, const ber_sequence_t *set, int hf_id, int ett_id) {
    int8_t      classx;
    bool    pcx, ind = 0, ind_field, imp_tag;
    int32_t     tagx;
    int         identifier_offset;
    int         identifier_len;
    uint32_t    lenx;
    int         len_offset;
    int         len_len;
    proto_tree *tree     = parent_tree;
    proto_item *item     = NULL;
    proto_item *cause;
    int         end_offset, s_offset;
    int         hoffset;
    tvbuff_t   *next_tvb;
    uint32_t    mandatory_fields = 0;
    uint8_t     set_idx;
    bool        first_pass;
    const ber_sequence_t *cset = NULL;

#define MAX_SET_ELEMENTS 32

    s_offset = offset;

#ifdef DEBUG_BER
    {
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(tvb, offset) > 3) {
proto_tree_add_debug_text(tree, "SET dissect_ber_set(%s) entered offset:%d len:%d %02x:%02x:%02x\n", name, offset, tvb_reported_length_remaining(tvb, offset), tvb_get_uint8(tvb, offset), tvb_get_uint8(tvb, offset+1), tvb_get_uint8(tvb, offset+2));
} else {
proto_tree_add_debug_text(tree, "SET dissect_ber_set(%s) entered\n", name);
}
}
#endif

    if (!implicit_tag) {
        hoffset = offset;
        /* first we must read the sequence header */
        identifier_offset = offset;
        offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &classx, &pcx, &tagx);
        identifier_len = offset - identifier_offset;
        offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &lenx, &ind);
        if (ind) {
        /*  Fixed the length is correctly returned from dissect ber_length
          end_offset = tvb_reported_length(tvb);*/
          end_offset = offset + lenx -2;
        } else {
          end_offset = offset + lenx;
        }

        /* sanity check: we only handle Constructed Universal Sets */
        if ((classx != BER_CLASS_APP) && (classx != BER_CLASS_PRI)) {
            if (!pcx
             || ((classx != BER_CLASS_UNI) || (tagx != BER_UNI_TAG_SET))) {
                tvb_ensure_bytes_exist(tvb, hoffset, 2);
                cause = proto_tree_add_expert_format(
                    tree, actx->pinfo, &ei_ber_expected_set,
                    tvb, identifier_offset, identifier_len,
                    "BER Error: SET expected but class:%s(%d) %s tag:%d was found",
                    val_to_str_const(classx, ber_class_codes, "Unknown"),
                    classx,
                    tfs_get_string(pcx, &tfs_constructed_primitive),
                    tagx);
                if (decode_unexpected) {
                    proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                    dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
                }
                return end_offset;
            }
        }
    } else {
        /* was implicit tag so just use the length of the tvb */
        lenx = tvb_reported_length_remaining(tvb, offset);
        end_offset = offset+lenx;
        identifier_offset = 0;
        identifier_len = 0;
    }

    /* create subtree */
    if (hf_id > 0) {
        if (parent_tree) {
            item = proto_tree_add_item(parent_tree, hf_id, tvb, offset, lenx, ENC_BIG_ENDIAN);
            tree = proto_item_add_subtree(item, ett_id);
        }
    }

    /* record the mandatory elements of the set so we can check we found everything at the end
       we can only record 32 elements for now ... */
    for (set_idx = 0; (set_idx < MAX_SET_ELEMENTS) && (cset = &set[set_idx])->func; set_idx++) {

        if (!(cset->flags & BER_FLAGS_OPTIONAL))
            mandatory_fields |= 1 << set_idx;

    }

    /* loop over all entries until we reach the end of the set */
    while (offset < end_offset) {
        int8_t   ber_class;
        bool pc;
        int32_t  tag;
        uint32_t len;
        int      eoffset, count;

        /*if (ind) {  this sequence was of indefinite length, if this is implicit indefinite impossible maybe
          but ber dissector uses this to eat the tag length then pass into here... EOC still on there...*/

            if ((tvb_get_uint8(tvb, offset) == 0) && (tvb_get_uint8(tvb, offset+1) == 0)) {
                if (show_internal_ber_fields) {
                    proto_tree_add_item(tree, hf_ber_seq_eoc, tvb, s_offset, offset+2, ENC_NA);
                }
                return end_offset;
            }
        /* } */
        hoffset = offset;
        /* read header and len for next field */
        identifier_offset = offset;
        offset  = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
        identifier_len = offset - identifier_offset;
        len_offset = offset;
        offset  = get_ber_length(tvb, offset, &len, &ind_field);
        len_len = offset - len_offset;
        eoffset = offset + len;

        if (len > (uint32_t)(end_offset - offset) || len > (uint32_t) tvb_reported_length_remaining(tvb, offset)) {
            ber_add_large_length_error(actx->pinfo, tree, tvb, offset, len, tvb, len_offset, len_len);
            return end_offset;
        }

        /* Look through the Set to see if this class/id exists and
         * hasn't been seen before
         * Skip check completely if ber_class == ANY
         * of if NOCHKTAG is set
         */

        for (first_pass = true, cset = set, set_idx = 0; cset->func || first_pass; cset++, set_idx++) {

            /* we reset for a second pass when we will look for choices */
            if (!cset->func) {
                first_pass = false;

                cset = set; /* reset to the beginning */
                set_idx = 0;
                /* If the set has no values, there is no point in trying again. */
                if (!cset->func) {
                    break;
                }
            }

            if ((first_pass && ((cset->ber_class == ber_class) && (cset->tag == tag))) ||
                (!first_pass && ((cset->ber_class == BER_CLASS_ANY) && (cset->tag == -1))) ) /* choices */
            {
                if (!(cset->flags & BER_FLAGS_NOOWNTAG) ) {
                    /* dissect header and len for field */
                    hoffset = dissect_ber_identifier(actx->pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
                    hoffset = dissect_ber_length(actx->pinfo, tree, tvb, hoffset, NULL, NULL);
                    next_tvb = ber_tvb_new_subset_length(tvb, hoffset, eoffset - hoffset - (ind_field ? 2 : 0));
                } else {
                    next_tvb = ber_tvb_new_subset_length(tvb, hoffset, eoffset - hoffset);
                }


#if 0
                /* call the dissector for this field */
                if    ((eoffset-hoffset)>length_remaining) {
                    /* If the field is indefinite (i.e. we don't know the
                     * length) of if the tvb is short, then just
                     * give it all of the tvb and hope for the best.
                     */
                    next_tvb = tvb_new_subset_remaining(tvb, hoffset);
                } else {

                }
#endif

#ifdef DEBUG_BER
{
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(next_tvb, 0) > 3) {
proto_tree_add_debug_text(tree, "SET dissect_ber_set(%s) calling subdissector offset:%d len:%d %02x:%02x:%02x\n", name, offset, tvb_reported_length_remaining(next_tvb, 0), tvb_get_uint8(next_tvb, 0), tvb_get_uint8(next_tvb, 1), tvb_get_uint8(next_tvb, 2));
} else {
proto_tree_add_debug_text(tree, "SET dissect_ber_set(%s) calling subdissector\n", name);
}
}
#endif
                if (next_tvb == NULL) {
                    /* Assume that we have a malformed packet. */
                    THROW(ReportedBoundsError);
                }
                imp_tag = false;
                if ((cset->flags & BER_FLAGS_IMPLTAG))
                    imp_tag = true;
                count = cset->func(imp_tag, next_tvb, 0, actx, tree, *cset->p_id);

                /* if we consumed some bytes,
                   or we knew the length was zero (during the first pass only) */
                if (count || (first_pass && ((len == 0) || ((ind_field == 1) && (len == 2))))) {
                    /* we found it! */
                    if (set_idx < MAX_SET_ELEMENTS)
                        mandatory_fields &= ~(1 << set_idx);

                    offset = eoffset;

                    if (!(cset->flags & BER_FLAGS_NOOWNTAG) ) {
                        /* if we stripped the tag and length we should also strip the EOC is ind_len */
                        if (ind_field == 1) {
                            /* skip over EOC */
                            if (show_internal_ber_fields) {
                                proto_tree_add_item(tree, hf_ber_set_field_eoc, tvb, offset, count, ENC_NA);
                            }
                        }
                    }
                    break;
                }
            }
        }

        if (!cset->func) {
            /* we didn't find a match */
            cause = proto_tree_add_expert_format(
                tree, actx->pinfo, &ei_ber_unknown_field_set,
                tvb, identifier_offset, identifier_len,
                "BER Error: Unknown field in SET class:%s(%d) tag:%d",
                val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                ber_class,
                tag);
            if (decode_unexpected) {
                proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
            }
            offset = eoffset;
        }
    }

    if (mandatory_fields) {

        /* OK - we didn't find some of the elements we expected */

        for (set_idx = 0; (set_idx < MAX_SET_ELEMENTS) && (cset = &set[set_idx])->func; set_idx++) {
            if (mandatory_fields & (1U << set_idx)) {
                /* here is something we should have seen - but didn't! */
                proto_tree_add_expert_format(
                    tree, actx->pinfo, &ei_ber_missing_field_set,
                    tvb, identifier_offset, identifier_len,
                    "BER Error: Missing field in SET class:%s(%d) tag:%d expected",
                    val_to_str_const(cset->ber_class, ber_class_codes, "Unknown"),
                    cset->ber_class,
                    cset->tag);
            }

        }
    }

    /* if we didn't end up at exactly offset, then we ate too many bytes */
    if (offset != end_offset) {
        tvb_ensure_bytes_exist(tvb, offset-2, 2);
        proto_tree_add_expert_format(
            tree, actx->pinfo, &ei_ber_error_length, tvb, offset-2, 2,
            "BER Error: SET is %d too many bytes long",
            offset - end_offset);
    }

    if (ind) {
        /*  need to eat this EOC
          end_offset = tvb_reported_length(tvb);*/
        end_offset += 2;
        if (show_internal_ber_fields) {
            proto_tree_add_item(tree, hf_ber_set_eoc, tvb, end_offset-2, 2, ENC_NA);
        }
    }

    return end_offset;

}

#ifdef DEBUG_BER
#define DEBUG_BER_CHOICE
#endif

int
dissect_ber_choice(asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, const ber_choice_t *choice, int hf_id, int ett_id, int *branch_taken)
{
    int8_t      ber_class;
    bool        pc, ind, imp_tag = false;
    int32_t     tag;
    int         identifier_offset;
    int         identifier_len;
    uint32_t    len;
    proto_tree *tree = parent_tree;
    proto_item *item = NULL;
    int         end_offset, start_offset, count;
    int         hoffset = offset;
    int         length;
    tvbuff_t   *next_tvb;
    bool        first_pass;
    header_field_info  *hfinfo;
    const ber_choice_t *ch;

#ifdef DEBUG_BER_CHOICE
{
const char *name;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(tvb, offset) > 3) {
proto_tree_add_debug_text(tree, "CHOICE dissect_ber_choice(%s) entered offset:%d len:%d %02x:%02x:%02x\n", name, offset, tvb_reported_length_remaining(tvb, offset), tvb_get_uint8(tvb, offset), tvb_get_uint8(tvb, offset+1), tvb_get_uint8(tvb, offset+2));
} else {
proto_tree_add_debug_text(tree, "CHOICE dissect_ber_choice(%s) entered len:%d\n", name, tvb_reported_length_remaining(tvb, offset));
}
}
#endif

    start_offset = offset;

    if (branch_taken) {
        *branch_taken = -1;
    }

    if (tvb_reported_length_remaining(tvb, offset) == 0) {
        proto_tree_add_expert(
            parent_tree, actx->pinfo, &ei_ber_empty_choice, tvb, offset, 0);
        return offset;
    }

    /* read header and len for choice field */
    identifier_offset = offset;
    offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
    identifier_len = offset - identifier_offset;
    offset = get_ber_length(tvb, offset, &len, &ind);
    end_offset = offset + len ;

    /* Some sanity checks.
     * The hf field passed to us MUST be an integer type
     */
    if (hf_id > 0) {
        hfinfo = proto_registrar_get_nth(hf_id);
        switch (hfinfo->type) {
        case FT_UINT8:
        case FT_UINT16:
        case FT_UINT24:
        case FT_UINT32:
            break;
        default:
            proto_tree_add_expert_format(
                tree, actx->pinfo, &ei_hf_field_not_integer_type,
                tvb, identifier_offset, identifier_len,
                "BER Error: dissect_ber_choice(): frame:%u offset:%d Was passed an HF field that was not integer type : %s",
                actx->pinfo->num, offset, hfinfo->abbrev);
            return end_offset;
        }
    }

    /* loop over all entries until we find the right choice or
       run out of entries */
    ch = choice;
    first_pass = true;
    while (ch->func || first_pass) {
        if (branch_taken) {
            (*branch_taken)++;
        }
        /* we reset for a second pass when we will look for choices */
        if (!ch->func) {
            first_pass = false;
            ch = choice; /* reset to the beginning */
            if (branch_taken) {
                *branch_taken = -1;
            }
            continue;
        }

#ifdef DEBUG_BER_CHOICE
proto_tree_add_debug_text(tree, "CHOICE testing potential subdissector class[%p]:%d:(expected)%d  tag:%d:(expected)%d flags:%d\n", ch, ber_class, ch->ber_class, tag, ch->tag, ch->flags);
#endif
        if ( (first_pass
           && (((ch->ber_class == ber_class) && (ch->tag == tag))
            || ((ch->ber_class == ber_class) && (ch->tag == -1) && (ch->flags & BER_FLAGS_NOOWNTAG))))
          || (!first_pass && (((ch->ber_class == BER_CLASS_ANY) && (ch->tag == -1)))) /* we failed on the first pass so now try any choices */
        ) {
            if (!(ch->flags & BER_FLAGS_NOOWNTAG)) {
                /* dissect header and len for field */
                hoffset = dissect_ber_identifier(actx->pinfo, tree, tvb, start_offset, NULL, NULL, NULL);
                hoffset = dissect_ber_length(actx->pinfo, tree, tvb, hoffset, NULL, NULL);
                start_offset = hoffset;
                if (ind) {
                    length = len - 2;
                } else {
                    length = len;
                }
            } else {
                length = end_offset- hoffset;
            }
            /* create subtree */
            if (hf_id > 0) {
                if (parent_tree) {
                    item = proto_tree_add_uint(parent_tree, hf_id, tvb, hoffset, end_offset - hoffset, ch->value);
                    tree = proto_item_add_subtree(item, ett_id);
                }
            }

#ifdef REMOVED
            /* This is bogus and makes the OID_1.0.9506.1.1.cap file
             * in Steven J Schaeffer's email of 2005-09-12 fail to dissect
             * properly.  Maybe we should get rid of 'first_pass'
             * completely.
             * It was added as a qad workaround for some problem CMIP
             * traces anyway.
             * God, this file is a mess and it is my fault. /ronnie
             */
            if (first_pass)
                next_tvb = ber_tvb_new_subset_length(tvb, hoffset, length);
            else
                next_tvb = tvb; /* we didn't make selection on this class/tag so pass it on */
#endif
            next_tvb = ber_tvb_new_subset_length(tvb, hoffset, length);


#ifdef DEBUG_BER_CHOICE
{
const char *name;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(next_tvb, 0) > 3) {
proto_tree_add_debug_text(tree, "CHOICE dissect_ber_choice(%s) calling subdissector start_offset:%d offset:%d len:%d %02x:%02x:%02x\n", name, start_offset, offset, tvb_reported_length_remaining(next_tvb, 0), tvb_get_uint8(next_tvb, 0), tvb_get_uint8(next_tvb, 1), tvb_get_uint8(next_tvb, 2));
} else {
proto_tree_add_debug_text(tree, "CHOICE dissect_ber_choice(%s) calling subdissector len:%d\n", name, tvb_reported_length(next_tvb));
}
}
#endif
            if (next_tvb == NULL) {
                /* Assume that we have a malformed packet. */
                THROW(ReportedBoundsError);
            }
            imp_tag = false;
            if ((ch->flags & BER_FLAGS_IMPLTAG))
                imp_tag = true;
            count = ch->func(imp_tag, next_tvb, 0, actx, tree, *ch->p_id);
#ifdef DEBUG_BER_CHOICE
{
const char *name;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
proto_tree_add_debug_text(tree, "CHOICE dissect_ber_choice(%s) subdissector ate %d bytes\n", name, count);
}
#endif
            if ((count == 0) && (((ch->ber_class == ber_class) && (ch->tag == -1) && (ch->flags & BER_FLAGS_NOOWNTAG)) || !first_pass)) {
                /* wrong one, break and try again */
                ch++;
#ifdef DEBUG_BER_CHOICE
{
const char *name;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
proto_tree_add_debug_text(tree, "CHOICE dissect_ber_choice(%s) trying again\n", name);
}
#endif
                continue;
            }
            if (!(ch->flags & BER_FLAGS_NOOWNTAG)) {
                if (ind) {
                /* we are traversing a indfinite length choice where we did not pass the tag length */
                /* we need to eat the EOC */
                    if (show_internal_ber_fields) {
                        proto_tree_add_item(tree, hf_ber_choice_eoc, tvb, end_offset-2, 2, ENC_NA);
                    }
                }
            }
            return end_offset;
        }
        ch++;
    }
    if (branch_taken) {
        /* none of the branches were taken so set the param
           back to -1 */
        *branch_taken = -1;
    }

#ifdef REMOVED
    /*XXX here we should have another flag to the CHOICE to distinguish
     * between the case when we know it is a mandatory   or if the CHOICE is optional == no arm matched */

    /* oops no more entries and we still haven't found
     * our guy :-(
     */
    proto_tree_add_expert(
        tree, actx->pinfo, &ei_ber_choice_not_found, tvb, offset, len);
    return end_offset;
#endif

    return start_offset;
}

#if 0
/* this function dissects a BER GeneralString
 */
int
dissect_ber_GeneralString(asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, char *name_string, int name_len)
{
    int8_t      ber_class;
    bool    pc;
    int32_t     tag;
    int         identifier_offset;
    int         identifier_len;
    uint32_t    len;
    int         end_offset;
    int         hoffset;
    char        str_arr[256];
    uint32_t    max_len;
    char       *str;
    proto_item *cause;

    str = str_arr;
    max_len = 255;
    if (name_string) {
        str = name_string;
        max_len = name_len;
    }

    hoffset = offset;
    /* first we must read the GeneralString header */
    identifier_offset = offset;
    offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
    identifier_len = offset - identifier_offset;
    offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, NULL);
    end_offset = offset + len;

    /* sanity check: we only handle Universal GeneralString*/
    if ( (ber_class != BER_CLASS_UNI)
      || (tag != BER_UNI_TAG_GENSTR) ) {
        tvb_ensure_bytes_exist(tvb, hoffset, 2);
        cause = proto_tree_add_expert_format(
            tree, actx->pinfo, &ei_ber_expected_general_string,
            tvb, identifier_offset, identifier_len,
            "BER Error: GeneralString expected but class:%s(%d) %s tag:%d was unexpected",
            val_to_str_const(ber_class, ber_class_codes, "Unknown"),
            ber_class, tfs_get_string(pc, &tfs_constructed_primitive),
            tag);
        if (decode_unexpected) {
            proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
            dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
        }
        return end_offset;
    }

    if (len >= (max_len - 1)) {
        len = max_len - 1;
    }

    tvb_memcpy(tvb, str, offset, len);
    str[len]=0;

    if (hf_id > 0) {
        proto_tree_add_string(tree, hf_id, tvb, offset, len, str);
    }

    return end_offset;
}
#endif

int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_constrained_restricted_string(bool implicit_tag, int32_t type,  asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int32_t min_len, int32_t max_len, int hf_id, tvbuff_t **out_tvb) {
    int8_t      ber_class;
    bool    pc;
    int32_t     tag;
    int         identifier_offset;
    int         identifier_len;
    uint32_t    len;
    int         eoffset;
    int         hoffset = offset;
    proto_item *cause;

#ifdef DEBUG_BER
{
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(tvb, offset) > 3) {
proto_tree_add_debug_text(tree, "RESTRICTED STRING dissect_ber_octet string(%s) entered implicit_tag:%d offset:%d len:%d %02x:%02x:%02x\n", name, implicit_tag, offset, tvb_reported_length_remaining(tvb, offset), tvb_get_uint8(tvb, offset), tvb_get_uint8(tvb, offset+1), tvb_get_uint8(tvb, offset+2));
} else {
proto_tree_add_debug_text(tree, "RESTRICTED STRING dissect_ber_octet_string(%s) entered\n", name);
}
}
#endif

    if (!implicit_tag) {
        identifier_offset = offset;
        offset  = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
        identifier_len = offset - identifier_offset;
        offset  = get_ber_length(tvb, offset, &len, NULL);
        eoffset = offset + len;

        /* sanity check */
        if ( (ber_class != BER_CLASS_UNI)
          || (tag != type) ) {
            tvb_ensure_bytes_exist(tvb, hoffset, 2);
            cause = proto_tree_add_expert_format(
                tree, actx->pinfo, &ei_ber_expected_string,
                tvb, identifier_offset, identifier_len,
                "BER Error: String with tag=%d expected but class:%s(%d) %s tag:%d was unexpected",
                type,
                val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                ber_class, tfs_get_string(pc, &tfs_constructed_primitive),
                tag);
            if (decode_unexpected) {
                proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
            }
            return eoffset;
        }
    }

    /* 8.21.3 */
    return dissect_ber_constrained_octet_string(implicit_tag, actx, tree, tvb, hoffset, min_len, max_len, hf_id, out_tvb);
}

// NOLINTNEXTLINE(misc-no-recursion)
int dissect_ber_restricted_string(bool implicit_tag, int32_t type, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, tvbuff_t **out_tvb)
{
    return dissect_ber_constrained_restricted_string(implicit_tag, type, actx, tree, tvb, offset, NO_BOUND, NO_BOUND, hf_id, out_tvb);
}

int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_GeneralString(asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, char *name_string, unsigned name_len)
{
    tvbuff_t *out_tvb = NULL;
    int       tvb_len;

    offset = dissect_ber_restricted_string(false, BER_UNI_TAG_GeneralString, actx, tree, tvb, offset, hf_id, (name_string) ? &out_tvb : NULL);

    if (name_string) {
        /*
         * XXX - do we want to just get what's left in the tvbuff
         * if the full length isn't available in the tvbuff, or
         * do we want to throw an exception?
         */
        if (out_tvb) {
            tvb_len = tvb_reported_length(out_tvb);
            if ((unsigned)tvb_len >= name_len) {
                tvb_memcpy(out_tvb, (uint8_t*)name_string, 0, name_len-1);
                name_string[name_len-1] = '\0';
            } else {
                tvb_memcpy(out_tvb, (uint8_t*)name_string, 0, tvb_len);
                name_string[tvb_len] = '\0';
            }
        }
    }

    return offset;
}

/* 8.19 Encoding of a relative or absolute object identifier value.
 */
static int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_any_oid(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, tvbuff_t **value_tvb, bool is_absolute)
{
    int8_t       ber_class;
    bool     pc;
    int32_t      tag;
    int          identifier_offset;
    int          identifier_len;
    uint32_t     len;
    int          eoffset;
    int          hoffset;
    const char  *str;
    proto_item  *cause;
    const char *name;
    header_field_info *hfi;

#ifdef DEBUG_BER
{
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(tvb, offset) > 3) {
proto_tree_add_debug_text(tree, "OBJECT IDENTIFIER dissect_ber_any_oid(%s) entered implicit_tag:%d offset:%d len:%d %02x:%02x:%02x\n", name, implicit_tag, offset, tvb_reported_length_remaining(tvb, offset), tvb_get_uint8(tvb, offset), tvb_get_uint8(tvb, offset+1), tvb_get_uint8(tvb, offset+2));
} else {
proto_tree_add_debug_text(tree, "OBJECT IDENTIFIER dissect_ber_any_oid(%s) entered\n", name);
}
}
#endif

    if (!implicit_tag) {
        hoffset = offset;
        /* sanity check */
        identifier_offset = offset;
        offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
        identifier_len = offset - identifier_offset;
        offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, NULL);
        eoffset = offset + len;
        if ( (ber_class != BER_CLASS_UNI)
          || (is_absolute && tag != BER_UNI_TAG_OID)
          || (!is_absolute && tag != BER_UNI_TAG_RELATIVE_OID) ) {
                tvb_ensure_bytes_exist(tvb, hoffset, 2);
            cause = proto_tree_add_expert_format(
                tree, actx->pinfo, &ei_ber_expected_object_identifier,
                tvb, identifier_offset, identifier_len,
                "BER Error: Object Identifier expected but class:%s(%d) %s tag:%d was unexpected",
                val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                ber_class,
                tfs_get_string(pc, &tfs_constructed_primitive),
                tag);
            if (decode_unexpected) {
                proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
            }
            return eoffset;
        }
    } else {
        len = tvb_reported_length_remaining(tvb, offset);
        eoffset = offset+len;
    }

    actx->created_item = NULL;
    hfi = proto_registrar_get_nth(hf_id);
    if ((is_absolute && hfi->type == FT_OID) || (!is_absolute && hfi->type == FT_REL_OID)) {
        actx->created_item = proto_tree_add_item(tree, hf_id, tvb, offset, len, ENC_BIG_ENDIAN);
    } else if (FT_IS_STRING(hfi->type)) {
        str = oid_encoded2string(actx->pinfo->pool, tvb_get_ptr(tvb, offset, len), len);
        actx->created_item = proto_tree_add_string(tree, hf_id, tvb, offset, len, str);
        if (actx->created_item) {
            /* see if we know the name of this oid */
            name = oid_resolved_from_encoded(actx->pinfo->pool, tvb_get_ptr(tvb, offset, len), len);
            if (name) {
                proto_item_append_text(actx->created_item, " (%s)", name);
            }
        }
    } else {
        DISSECTOR_ASSERT_NOT_REACHED();
    }

    if (value_tvb)
        *value_tvb = ber_tvb_new_subset_length(tvb, offset, len);

    return eoffset;
}

static int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_any_oid_str(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, const char **value_stringx, bool is_absolute)
{
    tvbuff_t *value_tvb = NULL;
    unsigned  length;

    offset = dissect_ber_any_oid(implicit_tag, actx, tree, tvb, offset, hf_id, (value_stringx) ? &value_tvb : NULL, is_absolute);

    if (value_stringx) {
        if (value_tvb && (length = tvb_reported_length(value_tvb))) {
            *value_stringx = oid_encoded2string(actx->pinfo->pool, tvb_get_ptr(value_tvb, 0, length), length);
        } else {
            *value_stringx = "";
        }
    }

    return offset;
}

/* 8.19 Encoding of a relative object identifier value.
 */
int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_relative_oid(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, tvbuff_t **value_tvb)
{
    return dissect_ber_any_oid(implicit_tag, actx, tree, tvb, offset, hf_id, value_tvb, false);
}

int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_relative_oid_str(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, const char **value_stringx)
{
    return dissect_ber_any_oid_str(implicit_tag, actx, tree, tvb, offset, hf_id, value_stringx, false);
}

/* 8.19 Encoding of an object identifier value.
 */
int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_object_identifier(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, tvbuff_t **value_tvb)
{
    return dissect_ber_any_oid(implicit_tag, actx, tree, tvb, offset, hf_id, value_tvb, true);
}

int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_object_identifier_str(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, const char **value_stringx)
{
    return dissect_ber_any_oid_str(implicit_tag, actx, tree, tvb, offset, hf_id, value_stringx, true);
}

#ifdef DEBUG_BER
#define DEBUG_BER_SQ_OF
#endif

static int
dissect_ber_sq_of(bool implicit_tag, int32_t type, asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, int32_t min_len, int32_t max_len, const ber_sequence_t *seq, int hf_id, int ett_id) {
    int8_t             classx;
    bool               pcx, ind = false, ind_field;
    int32_t            tagx;
    int                identifier_offset;
    int                identifier_len;
    uint32_t           lenx;

    proto_tree        *tree     = parent_tree;
    proto_item        *item     = NULL;
    proto_item        *causex;
    int                cnt, hoffsetx, end_offset;
    bool               have_cnt;
    header_field_info *hfi;
    tvbuff_t          *next_tvb;

#ifdef DEBUG_BER_SQ_OF
{
const char *name;
header_field_info *hfinfo;
if (hf_id > 0) {
hfinfo = proto_registrar_get_nth(hf_id);
name = hfinfo->name;
} else {
name = "unnamed";
}
if (tvb_reported_length_remaining(tvb,offset) > 3) {
proto_tree_add_debug_text(tree, "SQ OF dissect_ber_sq_of(%s) entered implicit_tag:%d offset:%d len:%d %02x:%02x:%02x\n", name, implicit_tag, offset, tvb_reported_length_remaining(tvb, offset), tvb_get_uint8(tvb, offset), tvb_get_uint8(tvb, offset+1), tvb_get_uint8(tvb, offset+2));
} else {
proto_tree_add_debug_text(tree, "SQ OF dissect_ber_sq_of(%s) entered\n", name);
}
}
#endif

    if (!implicit_tag) {
        hoffsetx = offset;
        /* first we must read the sequence header */
        identifier_offset = offset;
        offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &classx, &pcx, &tagx);
        identifier_len = offset - identifier_offset;
        offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &lenx, &ind);
        end_offset = offset + lenx;

        /* sanity check: we only handle Constructed Universal Sequences */
        if ((classx != BER_CLASS_APP) && (classx != BER_CLASS_PRI)) {
            if (!pcx
             || ((classx != BER_CLASS_UNI) || (tagx != type))) {
                tvb_ensure_bytes_exist(tvb, hoffsetx, 2);
                causex = proto_tree_add_expert_format(
                    tree, actx->pinfo,
                    (type == BER_UNI_TAG_SEQUENCE) ? &ei_ber_expected_set : &ei_ber_expected_sequence,
                    tvb, identifier_offset, identifier_len,
                    "BER Error: %s OF expected but class:%s(%d) %s tag:%d was unexpected",
                    (type == BER_UNI_TAG_SEQUENCE) ? "SET" : "SEQUENCE",
                    val_to_str_const(classx, ber_class_codes, "Unknown"),
                    classx, tfs_get_string(pcx, &tfs_constructed_primitive),
                    tagx);
                if (decode_unexpected) {
                    proto_tree *unknown_tree = proto_item_add_subtree(causex, ett_ber_unknown);
                    dissect_unknown_ber(actx->pinfo, tvb, hoffsetx, unknown_tree);
                }
                return end_offset;
            }
        }
    } else {
        /* the tvb length should be correct now nope we could be coming from an implicit choice or sequence, thus we
        read the items we match and return the length*/
        lenx = tvb_reported_length_remaining(tvb, offset);
        end_offset = offset + lenx;
    }

    /* count number of items */
    cnt = 0;
    have_cnt = false;
    hoffsetx = offset;
    /* only count the number of items IFF we have the full blob,
     * else this will just generate a [short frame] before we even start
     * dissecting a single item.
     */
    /* XXX Do we really need to count them at all ?  ronnie */
    if (tvb_captured_length_remaining(tvb, offset) == tvb_reported_length_remaining(tvb, offset)) {
        have_cnt = true;
        while (offset < end_offset) {
            uint32_t len;
            int     s_offset;

            s_offset = offset;

            /*if (ind) {  this sequence of was of indefinite length, if this is implicit indefinite impossible maybe
              but ber dissector uses this to eat the tag length then pass into here... EOC still on there...*/
                if ((tvb_get_uint8(tvb, offset) == 0) && (tvb_get_uint8(tvb, offset+1) == 0)) {
                    break;
                }
            /* } */

            /* read header and len for next field */
            offset = get_ber_identifier(tvb, offset, NULL, NULL, NULL);
            offset = get_ber_length(tvb, offset, &len, &ind);
            /* best place to get real length of implicit sequence of or set of is here... */
            /* adjust end_offset if we find somthing that doesn't match */
            offset += len;
            cnt++;
            if (offset <= s_offset) {
                /* Underflow - give up; this can happen with a very large
                 * length.
                 */
                have_cnt = false;
                cnt = 0;
                break;
            }
        }
    }
    offset = hoffsetx;

    /* create subtree */
    if (hf_id > 0) {
        hfi = proto_registrar_get_nth(hf_id);
        if (parent_tree) {
            if (hfi->type == FT_NONE) {
                item = proto_tree_add_item(parent_tree, hf_id, tvb, offset, lenx, ENC_BIG_ENDIAN);
                proto_item_append_text(item, ":");
            } else {
                if (have_cnt) {
                    item = proto_tree_add_uint(parent_tree, hf_id, tvb, offset, lenx, cnt);
                    proto_item_append_text(item, (cnt == 1) ? " item" : " items");
                } else
                    item = proto_tree_add_uint_format_value(parent_tree, hf_id, tvb, offset, lenx, cnt, "unknown number of items");
            }
            tree = proto_item_add_subtree(item, ett_id);
            ber_check_items (cnt, min_len, max_len, actx, item);
        }
    }

    /* loop over all entries until we reach the end of the sequence */
    while (offset < end_offset) {
        int8_t      ber_class;
        bool    pc;
        int32_t     tag;
        uint32_t    len;
        int         eoffset;
        int         hoffset;
        proto_item *cause;
        bool    imp_tag;

        hoffset = offset;
        /*if (ind) {  this sequence was of indefinite length, if this is implicit indefinite impossible maybe
          but ber dissector uses this to eat the tag length then pass into here... EOC still on there...*/
            if ((tvb_get_uint8(tvb, offset) == 0) && (tvb_get_uint8(tvb, offset+1) == 0)) {
                if (show_internal_ber_fields) {
                    proto_tree_add_item(tree, hf_ber_seq_of_eoc, tvb, hoffset, end_offset-hoffset, ENC_NA);
                }
                return offset+2;
            }
        /*}*/
        /* read header and len for next field */
        identifier_offset = offset;
        offset  = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
        identifier_len = offset - identifier_offset;
        offset  = get_ber_length(tvb, offset, &len, &ind_field);
        eoffset = offset + len;
                /* Make sure we move forward */
        if (eoffset <= hoffset)
            THROW(ReportedBoundsError);

        if ((ber_class == BER_CLASS_UNI) && (tag == BER_UNI_TAG_EOC)) {
            /* This is a zero length sequence of*/
            hoffset = dissect_ber_identifier(actx->pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
            dissect_ber_length(actx->pinfo, tree, tvb, hoffset, NULL, NULL);
            return eoffset;
        }
        /* verify that this one is the one we want */
        /* ahup if we are implicit then we return to the upper layer how much we have used */
        if (seq->ber_class != BER_CLASS_ANY) {
          if ((seq->ber_class != ber_class)
           || (seq->tag != tag) ) {
            if (!(seq->flags & BER_FLAGS_NOTCHKTAG)) {
                if ( seq->ber_class == BER_CLASS_UNI) {
                    cause = proto_tree_add_expert_format(
                        tree, actx->pinfo, &ei_ber_sequence_field_wrong,
                        tvb, identifier_offset, identifier_len,
                        "BER Error: Wrong field in SEQUENCE OF: expected class:%s(%d) tag:%d(%s) but found class:%s(%d) tag:%d",
                        val_to_str_const(seq->ber_class, ber_class_codes, "Unknown"),
                        seq->ber_class,
                        seq->tag,
                        val_to_str_ext_const(seq->tag, &ber_uni_tag_codes_ext, "Unknown"),
                        val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                        ber_class, tag);
                } else {
                    cause = proto_tree_add_expert_format(
                        tree, actx->pinfo, &ei_ber_sequence_field_wrong,
                        tvb, identifier_offset, identifier_len,
                        "BER Error: Wrong field in SEQUENCE OF: expected class:%s(%d) tag:%d but found class:%s(%d) tag:%d",
                        val_to_str_const(seq->ber_class, ber_class_codes, "Unknown"),
                        seq->ber_class,
                        seq->tag,
                        val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                        ber_class,
                        tag);
                }
                if (decode_unexpected) {
                    proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                    dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
                }
                offset = eoffset;
                continue;
                /* wrong.... */
            }
          }
        }

        if (!(seq->flags & BER_FLAGS_NOOWNTAG) && !(seq->flags & BER_FLAGS_IMPLTAG)) {
            /* dissect header and len for field */
            hoffset = dissect_ber_identifier(actx->pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
            hoffset = dissect_ber_length(actx->pinfo, tree, tvb, hoffset, NULL, NULL);
        }
        if ((seq->flags == BER_FLAGS_IMPLTAG) && (seq->ber_class == BER_CLASS_CON)) {
            /* Constructed sequence of with a tag */
            /* dissect header and len for field */
            hoffset = dissect_ber_identifier(actx->pinfo, tree, tvb, hoffset, NULL, NULL, NULL);
            hoffset = dissect_ber_length(actx->pinfo, tree, tvb, hoffset, NULL, NULL);
            /* Function has IMPLICIT TAG */
        }

        next_tvb = ber_tvb_new_subset_length(tvb, hoffset, eoffset-hoffset);

        imp_tag = false;
        if (seq->flags == BER_FLAGS_IMPLTAG)
            imp_tag = true;
        /* call the dissector for this field */
        seq->func(imp_tag, next_tvb, 0, actx, tree, *seq->p_id);
        /* hold on if we are implicit and the result is zero, i.e. the item in the sequence of
           doesn't match the next item, thus this implicit sequence is over, return the number of bytes
           we have eaten to allow the possible upper sequence continue... */
        cnt++; /* rubbish*/
        offset = eoffset;
    }

    /* if we didn't end up at exactly offset, then we ate too many bytes */
    if (offset != end_offset) {
        tvb_ensure_bytes_exist(tvb, offset-2, 2);
        proto_tree_add_expert_format(
            tree, actx->pinfo, &ei_ber_error_length, tvb, offset-2, 2,
            "BER Error: %s OF contained %d too many bytes",
            (type == BER_UNI_TAG_SEQUENCE) ? "SEQUENCE" : "SET",
            offset - end_offset);
    }

    return end_offset;
}

int
dissect_ber_constrained_sequence_of(bool implicit_tag, asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, int32_t min_len, int32_t max_len, const ber_sequence_t *seq, int hf_id, int ett_id) {
    return dissect_ber_sq_of(implicit_tag, BER_UNI_TAG_SEQUENCE, actx, parent_tree, tvb, offset, min_len, max_len, seq, hf_id, ett_id);
}

int
dissect_ber_sequence_of(bool implicit_tag, asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, const ber_sequence_t *seq, int hf_id, int ett_id) {
    return dissect_ber_sq_of(implicit_tag, BER_UNI_TAG_SEQUENCE, actx, parent_tree, tvb, offset, NO_BOUND, NO_BOUND, seq, hf_id, ett_id);
}

int
dissect_ber_constrained_set_of(bool implicit_tag, asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, int32_t min_len, int32_t max_len, const ber_sequence_t *seq, int hf_id, int ett_id) {
    return dissect_ber_sq_of(implicit_tag, BER_UNI_TAG_SET, actx, parent_tree, tvb, offset, min_len, max_len, seq, hf_id, ett_id);
}

int
dissect_ber_set_of(bool implicit_tag, asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, const ber_sequence_t *seq, int hf_id, int ett_id) {
    return dissect_ber_sq_of(implicit_tag, BER_UNI_TAG_SET, actx, parent_tree, tvb, offset, NO_BOUND, NO_BOUND, seq, hf_id, ett_id);
}

int
dissect_ber_GeneralizedTime(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id)
{
    nstime_t      ts;
    const uint8_t *tmpstr;
    int8_t        ber_class;
    bool      pc;
    int32_t       tag;
    int           identifier_offset;
    int           identifier_len;
    uint32_t      len;
    int           len_offset;
    int           len_len;
    int           end_offset;
    int           hoffset;
    proto_item   *cause;

    if (!implicit_tag) {
        hoffset = offset;
        identifier_offset = offset;
        offset = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
        identifier_len = offset - identifier_offset;
        len_offset = offset;
        offset = dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, NULL);
        len_len = offset - len_offset;
        end_offset = offset+len;

        /* sanity check. we only handle universal/generalized time */
        if ( (ber_class != BER_CLASS_UNI) || (tag != BER_UNI_TAG_GeneralizedTime)) {
            tvb_ensure_bytes_exist(tvb, hoffset, 2);
            cause = proto_tree_add_expert_format(
                tree, actx->pinfo, &ei_ber_expected_generalized_time,
                tvb, identifier_offset, identifier_len,
                "BER Error: GeneralizedTime expected but class:%s(%d) %s tag:%d was unexpected",
                val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                ber_class,
                tfs_get_string(pc, &tfs_constructed_primitive),
                tag);
            if (decode_unexpected) {
                proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
            }
            return end_offset;
        }
    } else {
        len = tvb_reported_length_remaining(tvb, offset);
        len_offset = 0;
        len_len = 0;
        end_offset = offset+len;
    }

    /* ASN.1 GeneralizedTime is a ISO 8601 Basic profile that omits the T
     * between date and time. BER allows accuracy of hours, minutes, seconds,
     * fractions of a second "to any degree of accuracy", and even
     * fractional minutes or hours (see ITU-T X.680 46.2 and ITU-T X.690 8.25.)
     *
     * CER/DER (and PER) require that the seconds field be present (cf.
     * ITU-T X.690 11.7 and ITU-T X.691 10.6.5), that the decimal point
     * element be ".", that fractional seconds trailing zeros MUST be omitted,
     * and that the decimal point shall also be omitted if the entire fractional
     * second is 0 (in order to have a unique representation.)
     *
     * RFC 5280 says that X.509 certificate validity dates after 2050, which
     * MUST use GeneralizedTime, MUST be expressed in Z and MUST include
     * seconds but MUST NOT include fractional seconds.
     *
     * The minimum that iso8601_to_nstime() handles currently is
     * YYYYMMDDhhmm = 12 digits
     * and the maximimum is
     * YYYYMMDDhhmmss.sssssssss+hhmm = 29 digits
     *
     * That doesn't handle everything that BER technically supports, but
     * everything seen in practice. For the protocols that are more restrictive
     * if someone really wants to validate and complain about e.g. fractional
     * seconds in a X.509 certificate, that could be added to the conformance
     * file.
     */
    if ((len < 12) || (len > 29)) {
        cause = proto_tree_add_expert_format(
            tree, actx->pinfo, &ei_ber_error_length,
            tvb, len_offset, len_len,
            "BER Error: GeneralizedTime invalid length: %u",
            len);
        if (decode_unexpected) {
            proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
            dissect_unknown_ber(actx->pinfo, tvb, offset, unknown_tree);
        }
        return end_offset;
    }

    tmpstr = tvb_get_string_enc(actx->pinfo->pool, tvb, offset, len, ENC_ASCII);
    if (!iso8601_to_nstime(&ts, tmpstr, ISO8601_DATETIME_BASIC)) {
        cause = proto_tree_add_expert_format(
            tree, actx->pinfo, &ei_ber_invalid_format_generalized_time,
            tvb, offset, len,
            "BER Error: GeneralizedTime invalid format: %s",
            tmpstr);
        if (decode_unexpected) {
            proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
            dissect_unknown_ber(actx->pinfo, tvb, offset, unknown_tree);
        }
        return end_offset;
    }

    if (hf_id > 0) {
        proto_tree_add_time(tree, hf_id, tvb, offset, len, &ts);
    }

    offset+=len;
    return offset;

}

/* datestrptr: if not NULL return datetime string instead of adding to tree or NULL when packet is malformed
 * tvblen: if not NULL return consumed packet bytes
 */
int
dissect_ber_UTCTime(bool implicit_tag, asn1_ctx_t *actx, proto_tree *tree, tvbuff_t *tvb, int offset, int hf_id, char **datestrptr, uint32_t *tvblen)
{
    char         *outstr, *outstrptr;
    const uint8_t *instr;
    int8_t        ber_class;
    bool      pc;
    int32_t       tag;
    int           identifier_offset;
    int           identifier_len;
    uint32_t      len, i, n;
    int           hoffset;
    proto_item   *cause;
    proto_tree   *error_tree;
    const char   *error_str = NULL;

    outstrptr = outstr = (char *)wmem_alloc(actx->pinfo->pool, 29);

    if (datestrptr) *datestrptr = NULL; /* mark invalid */
    if (tvblen) *tvblen = 0;

    if (!implicit_tag) {
        hoffset = offset;
        identifier_offset = offset;
        offset  = dissect_ber_identifier(actx->pinfo, tree, tvb, offset, &ber_class, &pc, &tag);
        identifier_len = offset - identifier_offset;
        offset  = dissect_ber_length(actx->pinfo, tree, tvb, offset, &len, NULL);

        /* sanity check: we only handle UTCTime */
        if ( (ber_class != BER_CLASS_UNI) || (tag != BER_UNI_TAG_UTCTime) ) {
            tvb_ensure_bytes_exist(tvb, hoffset, 2);
            cause = proto_tree_add_expert_format(
                tree, actx->pinfo, &ei_ber_expected_utc_time,
                tvb, identifier_offset, identifier_len,
                "BER Error: UTCTime expected but class:%s(%d) %s tag:%d was unexpected",
                val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                ber_class,
                tfs_get_string(pc, &tfs_constructed_primitive),
                tag);
            if (decode_unexpected) {
                proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
            }
            return offset+len;
        }
    } else {
        len = tvb_reported_length_remaining(tvb, offset);
    }

    if ((len < 10) || (len > 19)) {
        error_str = wmem_strdup_printf(actx->pinfo->pool, "BER Error: UTCTime invalid length: %u", len);
        instr = tvb_get_string_enc(actx->pinfo->pool, tvb, offset, len > 19 ? 19 : len, ENC_ASCII);
        goto malformed;
    }

    instr = tvb_get_string_enc(actx->pinfo->pool, tvb, offset, len, ENC_ASCII);

    /* YYMMDDhhmm */
    for (i=0; i<10; i++) {
        if ((instr[i] < '0') || (instr[i] > '9')) {
            error_str = "BER Error: malformed UTCTime encoding, "
                        "first 10 octets have to contain YYMMDDhhmm in digits";
            goto malformed;
        }
    }
    snprintf(outstrptr, 15, "%.2s-%.2s-%.2s %.2s:%.2s", instr, instr+2, instr+4, instr+6, instr+8);
    outstrptr+= 14;

    /* (ss)? */
    if (len >= 12) {
        if ((instr[i] >= '0') && (instr[i] <= '9')) {
            i++;
            if ((instr[i] >= '0') && (instr[i] <= '9')) {
                i++;
                snprintf(outstrptr, 4, ":%.2s", instr+10);
                outstrptr+=3;
            } else {
                error_str = "BER Error: malformed UTCTime encoding, "
                        "if 11th octet is a digit for seconds, "
                        "the 12th octet has to be a digit, too";
                goto malformed;
            }
        }
    }

    /* Z|([+-]hhmm) */
    switch (instr[i]) {
    case 'Z':
        if (len != (i+1)) {
            error_str = "BER Error: malformed UTCTime encoding, "
                        "there must be no further octets after \'Z\'";
            goto malformed;
        }
        snprintf(outstrptr, 7, " (UTC)");
        i++;
        break;
    case '-':
    case '+':
        if (len != (i+5)) {
            error_str = "BER Error: malformed UTCTime encoding, "
                        "4 digits must follow on \'+\' resp. \'-\'";
            goto malformed;
        }
        for (n=i+1; n<i+5; n++) {
            if ((instr[n] < '0') || (instr[n] > '9')) {
                error_str = "BER Error: malformed UTCTime encoding, "
                            "4 digits must follow on \'+\' resp. \'-\'";
                goto malformed;
            }
        }
        snprintf(outstrptr, 12, " (UTC%c%.4s)", instr[i], instr+i+1);
        i+=5;
        break;
    default:
        error_str = wmem_strdup_printf(actx->pinfo->pool,
                                       "BER Error: malformed UTCTime encoding, "
                                       "unexpected character in %dth octet, "
                                       "must be \'Z\', \'+\' or \'-\'", i+1);
        goto malformed;
        break;
    }

    if (len != i) {
        error_str = wmem_strdup_printf(actx->pinfo->pool,
            "BER Error: malformed UTCTime encoding, %d unexpected character%s after %dth octet",
            len - i,
            (len == (i - 1) ? "s" : ""),
            i);
        goto malformed;
    }

    if (datestrptr) {
       *datestrptr = outstr; /* mark as valid */
    } else {
        if (hf_id > 0) {
            proto_tree_add_string(tree, hf_id, tvb, offset, len, outstr);
        }
    }
    if (tvblen) *tvblen = len;

    return offset+len;
malformed:
    if (hf_id > 0) {
        cause = proto_tree_add_string(tree, hf_id, tvb, offset, len, instr);
        error_tree = proto_item_add_subtree(cause, ett_ber_unknown);
    } else {
        error_tree = tree;
    }

    proto_tree_add_expert_format(
        error_tree, actx->pinfo, &ei_ber_invalid_format_utctime,
        tvb, offset, len,
        "%s",
        error_str);

    if (tvblen) *tvblen = len;

    return offset+len;
}

/* 8.6 Encoding of a bitstring value */

int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_constrained_bitstring(bool implicit_tag, asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, int32_t min_len, int32_t max_len, int * const *named_bits, int num_named_bits, int hf_id, int ett_id, tvbuff_t **out_tvb)
{
    int8_t      ber_class;
    bool    pc, ind;
    int32_t     tag;
    int         identifier_offset;
    int         identifier_len;
    int         len;
    uint8_t     pad = 0;
    int         end_offset;
    int         hoffset;
    proto_item *item = NULL;
    proto_item *cause;
    proto_tree *tree = NULL;
    /*const char *sep;*/
    /*const int **nb;*/

    if (!implicit_tag) {
        hoffset = offset;
        /* read header and len for the octet string */
        identifier_offset = offset;
        offset = dissect_ber_identifier(actx->pinfo, parent_tree, tvb, offset, &ber_class, &pc, &tag);
        identifier_len = offset - identifier_offset;
        offset = dissect_ber_length(actx->pinfo, parent_tree, tvb, offset, &len, &ind);
        end_offset = offset + len;

        /* sanity check: we only handle Universal BitStrings */

        /* for an IMPLICIT APPLICATION tag asn2wrs seems to call this
           function with implicit_tag = false. BER_FLAGS_NOOWNTAG was
           set so the APPLICATION tag was still present.
           So here we relax it for APPLICATION tags. CONTEXT tags may
           still cause a problem. */

        if (ber_class != BER_CLASS_APP) {
            if ((ber_class != BER_CLASS_UNI)
                || (tag != BER_UNI_TAG_BITSTRING)) {
                tvb_ensure_bytes_exist(tvb, hoffset, 2);
                cause = proto_tree_add_expert_format(
                    parent_tree, actx->pinfo, &ei_ber_expected_bitstring,
                    tvb, identifier_offset, identifier_len,
                    "BER Error: BitString expected but class:%s(%d) %s tag:%d was unexpected",
                    val_to_str_const(ber_class, ber_class_codes, "Unknown"),
                    ber_class, tfs_get_string(pc, &tfs_constructed_primitive),
                    tag);
                if (decode_unexpected) {
                    proto_tree *unknown_tree = proto_item_add_subtree(cause, ett_ber_unknown);
                    dissect_unknown_ber(actx->pinfo, tvb, hoffset, unknown_tree);
                }
                return end_offset;
            }
        }
    } else {
        pc = 0;
        len = tvb_reported_length_remaining(tvb, offset);
        end_offset = offset + len;
    }
    if ((int)len <= 0) {
        proto_tree_add_expert_format(
            parent_tree, actx->pinfo, &ei_ber_constr_bitstr, tvb, offset, len,
            "BER Error: dissect_ber_constrained_bitstring(): frame:%u offset:%d Was passed an illegal length of %d",
            actx->pinfo->num, offset, len);
        return offset;
    }
    actx->created_item = NULL;

    if (pc) {
        /* constructed */
        /* TO DO */
    } else {
        /* primitive */
        pad = tvb_get_uint8(tvb, offset);
        /* 8.6.2.4 If a bitstring value has no 1 bits, then an encoder (as a sender's option)
         * may encode the value with a length of 1 and with an initial octet set to 0
         * or may encode it as a bit string with one or more 0 bits following the initial octet.
         */
        if ((pad == 0) && (len == 1)) {
            /* empty */
            item = proto_tree_add_item(parent_tree, hf_id, tvb, offset, len, ENC_BIG_ENDIAN);
            actx->created_item = item;
            proto_tree_add_item(parent_tree, hf_ber_bitstring_empty, tvb, offset, 1, ENC_BIG_ENDIAN);
            if (out_tvb) {
                *out_tvb = ber_tvb_new_subset_length(tvb, offset, len);
            }
            ber_check_length(8 * len - pad, min_len, max_len, actx, item, true);
            return end_offset;
        } else {
            /* padding */
            proto_item *pad_item = proto_tree_add_item(parent_tree, hf_ber_bitstring_padding, tvb, offset, 1, ENC_BIG_ENDIAN);
            if (pad > 7) {
                expert_add_info_format(
                    actx->pinfo, pad_item, &ei_ber_illegal_padding,
                    "Illegal padding (0 .. 7): %d", pad);
            }
        }
        offset++;
        len--;
        if (hf_id > 0) {
            item = proto_tree_add_item(parent_tree, hf_id, tvb, offset, len, ENC_NA);
            actx->created_item = item;
            if (named_bits) {
                uint8_t *bitstring = (uint8_t *)tvb_memdup(actx->pinfo->pool, tvb, offset, len);
                const int named_bits_bytelen = (num_named_bits + 7) / 8;
                if (show_internal_ber_fields) {
                    if (len < named_bits_bytelen) {
                        unsigned zero_bits_omitted = num_named_bits - ((len * 8) - pad);
                        proto_item_append_text(item, " [%u zero bits not encoded, but displayed]", zero_bits_omitted);
                    }
                }
                if (ett_id > 0) {
                    tree = proto_item_add_subtree(item, ett_id);
                }
                for (int i = 0; i < named_bits_bytelen; i++) {
                    // Process 8 bits at a time instead of 64, each field masks a
                    // single byte.
                    const int bit_offset = 8 * i;
                    int* const* section_named_bits = named_bits + bit_offset;
                    int* flags[9];
                    if (num_named_bits - bit_offset > 8) {
                        memcpy(&flags[0], named_bits + bit_offset, 8 * sizeof(int*));
                        flags[8] = NULL;
                        section_named_bits = (int* const*)flags;
                    }

                    // If less data is available than the number of named bits, then
                    // the trailing (right) bits are assumed to be 0.
                    uint64_t value = 0;
                    if (i < len) {
                        value = bitstring[i];
                        if (num_named_bits - bit_offset > 7) {
                            bitstring[i] = 0;
                        } else {
                            bitstring[i] &= 0xff >> (num_named_bits - bit_offset);
                        }
                    }

                    // TODO should non-zero pad bits be masked from the value?
                    // When trailing zeroes are not present in the data, mark the
                    // last byte for the lack of a better alternative.
                    proto_tree_add_bitmask_list_value(tree, tvb, offset + MIN(i, len - 1), 1, section_named_bits, value);
                }
                // If more data is available than the number of named bits, then
                // either the spec was updated or the packet is malformed.
                for (int i = 0; i < len; i++) {
                    if (bitstring[i]) {
                        expert_add_info_format(actx->pinfo, item, &ei_ber_bits_unknown, "Unknown bit(s): 0x%s",
                             bytes_to_str(actx->pinfo->pool, bitstring, len));
                        break;
                    }
                }
            }
        }
        if (out_tvb) {
            *out_tvb = ber_tvb_new_subset_length(tvb, offset, len);
        }
    }


    if ((pad > 0) && (pad < 8) && (len > 0)) {
        uint8_t bits_in_pad = tvb_get_uint8(tvb, offset + len - 1) & (0xFF >> (8 - pad));
        if (bits_in_pad) {
            expert_add_info_format(
                actx->pinfo, item, &ei_ber_bits_set_padded,
                "Bits set in padded area: 0x%02x", bits_in_pad);
        }
    }

    ber_check_length(8 * len - pad, min_len, max_len, actx, item, true);

    return end_offset;
}


int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_ber_bitstring(bool implicit_tag, asn1_ctx_t *actx, proto_tree *parent_tree, tvbuff_t *tvb, int offset, int * const *named_bits, int num_named_bits, int hf_id, int ett_id, tvbuff_t **out_tvb)
{
    return dissect_ber_constrained_bitstring(implicit_tag, actx, parent_tree, tvb, offset, -1, -1, named_bits, num_named_bits, hf_id, ett_id, out_tvb);
}

/*
 *  8.18    Encoding of a value of the external type
 *  8.18.1  The encoding of a value of the external type shall be the BER encoding of the following
 *          sequence type, assumed to be defined in an environment of EXPLICIT TAGS,
 *          with a value as specified in the subclauses below:
 *
 *  [UNIVERSAL 8] IMPLICIT SEQUENCE {
 *      direct-reference            OBJECT IDENTIFIER OPTIONAL,
 *      indirect-reference      INTEGER OPTIONAL,
 *      data-value-descriptor       ObjectDescriptor OPTIONAL,
 *      encoding                CHOICE {
 *      single-ASN1-type                [0] ABSTRACT-SYNTAX.&Type,
 *      octet-aligned                   [1] IMPLICIT OCTET STRING,
 *      arbitrary                       [2] IMPLICIT BIT STRING } }
 *
 */

static int
dissect_ber_INTEGER(bool implicit_tag, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index) {
  offset = dissect_ber_integer(implicit_tag, actx, tree, tvb, offset, hf_index,
                                  &actx->external.indirect_reference);
  actx->external.indirect_ref_present = true;

  return offset;
}

static int
dissect_ber_T_octet_aligned(bool implicit_tag, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index)
{
    if (actx->external.u.ber.ber_callback) {
        offset = actx->external.u.ber.ber_callback(false, tvb, offset, actx, tree, hf_index);
    } else if (actx->external.direct_ref_present &&
               dissector_get_string_handle(ber_oid_dissector_table, actx->external.direct_reference)) {
        offset = call_ber_oid_callback(actx->external.direct_reference, tvb, offset, actx->pinfo, tree, NULL);
    } else {
        offset = dissect_ber_octet_string(implicit_tag, actx, tree, tvb, offset, hf_index, &actx->external.octet_aligned);
    }

    return offset;
}
static int
dissect_ber_OBJECT_IDENTIFIER(bool implicit_tag, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index)
{
    DISSECTOR_ASSERT(actx);
    offset = dissect_ber_object_identifier_str(implicit_tag, actx, tree, tvb, offset, hf_index, &actx->external.direct_reference);
    actx->external.direct_ref_present = true;

    return offset;
}

static int
dissect_ber_ObjectDescriptor(bool implicit_tag, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index)
{
    DISSECTOR_ASSERT(actx);
    offset = dissect_ber_restricted_string(implicit_tag, BER_UNI_TAG_ObjectDescriptor,
                                           actx, tree, tvb, offset, hf_index,
                                           &actx->external.data_value_descriptor);

    return offset;
}

static int
dissect_ber_T_single_ASN1_type(bool implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index)
{
    if (actx->external.u.ber.ber_callback) {
        offset = actx->external.u.ber.ber_callback(false, tvb, offset, actx, tree, hf_index);
    } else {
        offset = call_ber_oid_callback(actx->external.direct_reference, tvb, offset, actx->pinfo, tree, NULL);
    }

    return offset;
}

static int
dissect_ber_T_arbitrary(bool implicit_tag, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index)
{
    if (actx->external.u.ber.ber_callback) {
        offset = actx->external.u.ber.ber_callback(false, tvb, offset, actx, tree, hf_index);
    } else {
        offset = dissect_ber_bitstring(implicit_tag, actx, tree, tvb, offset,
                                       NULL, 0, hf_index, -1, &actx->external.arbitrary);
    }

    return offset;
}

static const value_string ber_T_encoding_vals[] = {
    {   0, "single-ASN1-type" },
    {   1, "octet-aligned" },
    {   2, "arbitrary" },
    { 0, NULL }
};

static const ber_choice_t T_encoding_choice[] = {
    {   0, &hf_ber_single_ASN1_type, BER_CLASS_CON, 0, 0, dissect_ber_T_single_ASN1_type },
    {   1, &hf_ber_octet_aligned  ,  BER_CLASS_CON, 1, BER_FLAGS_IMPLTAG, dissect_ber_T_octet_aligned },
    {   2, &hf_ber_arbitrary      ,  BER_CLASS_CON, 2, BER_FLAGS_IMPLTAG, dissect_ber_T_arbitrary },
    { 0, NULL, 0, 0, 0, NULL }
};


static int
dissect_ber_T_encoding(bool implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index) {
    // This assertion is used to remove clang's warning.
    DISSECTOR_ASSERT(actx);
    offset = dissect_ber_choice(actx, tree, tvb, offset,
                                T_encoding_choice, hf_index, ett_ber_T_encoding,
                                &actx->external.encoding);

    return offset;
}


static const ber_sequence_t external_U_sequence[] = {
    { &hf_ber_direct_reference,      BER_CLASS_UNI, BER_UNI_TAG_OID, BER_FLAGS_OPTIONAL|BER_FLAGS_NOOWNTAG, dissect_ber_OBJECT_IDENTIFIER },
    { &hf_ber_indirect_reference,    BER_CLASS_UNI, BER_UNI_TAG_INTEGER, BER_FLAGS_OPTIONAL|BER_FLAGS_NOOWNTAG, dissect_ber_INTEGER },
    { &hf_ber_data_value_descriptor, BER_CLASS_UNI, BER_UNI_TAG_ObjectDescriptor, BER_FLAGS_OPTIONAL|BER_FLAGS_NOOWNTAG, dissect_ber_ObjectDescriptor },
    { &hf_ber_encoding,              BER_CLASS_ANY/*choice*/, -1/*choice*/, BER_FLAGS_NOOWNTAG|BER_FLAGS_NOTCHKTAG, dissect_ber_T_encoding },
    { NULL, 0, 0, 0, NULL }
};
static int
dissect_ber_external_U(bool implicit_tag, tvbuff_t *tvb, int offset, asn1_ctx_t *actx , proto_tree *tree, int hf_index)
{
    offset = dissect_ber_sequence(implicit_tag, actx, tree, tvb, offset,
                                  external_U_sequence, hf_index, ett_ber_EXTERNAL);

    return offset;
}

int
dissect_ber_external_type(bool implicit_tag, proto_tree *tree, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, int hf_id, ber_callback func) {

    actx->external.u.ber.ber_callback =  func;

    offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
                     hf_id, BER_CLASS_UNI, BER_UNI_TAG_EXTERNAL, true, dissect_ber_external_U);

    asn1_ctx_clean_external(actx);

    return offset;
}
/* Experimental */
int
dissect_ber_EmbeddedPDV_Type(bool implicit_tag, proto_tree *tree, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, int hf_id, ber_callback func _U_) {


    offset = dissect_ber_tagged_type(implicit_tag, actx, tree, tvb, offset,
                                     hf_id, BER_CLASS_UNI, BER_UNI_TAG_EMBEDDED_PDV, true, dissect_ber_external_U);

    return offset;
}

static int
dissect_ber_syntax(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    return dissect_unknown_ber(pinfo, tvb, 0, tree);
}

static int
dissect_ber_common(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, const char *syntax)
{
    const char *name;
    int offset;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "BER");

    col_set_str(pinfo->cinfo, COL_DEF_SRC, "BER encoded value");

    if (!syntax) {

        /* if we got here we couldn't find anything better */
        col_set_str(pinfo->cinfo, COL_INFO, "Unknown BER");

        offset = dissect_unknown_ber(pinfo, tvb, 0, tree);

    } else {

        offset = call_ber_syntax_callback(syntax, tvb, 0, pinfo, tree);

        /* see if we have a better name */
        name = get_ber_oid_syntax(syntax);
        col_add_fstr(pinfo->cinfo, COL_INFO, "Decoded as %s", name ? name : syntax);
    }

    return offset;
}

static int
dissect_ber(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    return dissect_ber_common(tvb, pinfo, tree, decode_as_syntax);
}

static int
dissect_ber_file(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    struct ber_phdr *ber = (struct ber_phdr *)data;
    const char *ptr;
    const char *file_syntax = NULL;

    if ((ptr = strrchr(ber->pathname, '.')) != NULL)
        file_syntax = get_ber_oid_syntax(ptr);
    return dissect_ber_common(tvb, pinfo, tree, file_syntax);
}

bool
oid_has_dissector(const char *oid) {
    return (dissector_get_string_handle(ber_oid_dissector_table, oid) != NULL);
}

static void
ber_shutdown(void)
{
    g_hash_table_destroy(syntax_table);
}

void
proto_register_ber(void)
{
    static hf_register_info hf[] = {
        { &hf_ber_id_class, {
                "Class", "ber.id.class", FT_UINT8, BASE_DEC,
                VALS(ber_class_codes), 0xc0, "Class of BER TLV Identifier", HFILL }},
        { &hf_ber_bitstring_padding, {
                "Padding", "ber.bitstring.padding", FT_UINT8, BASE_DEC,
                NULL, 0x0, "Number of unused bits in the last octet of the bitstring", HFILL }},
        { &hf_ber_bitstring_empty, {
                "Empty", "ber.bitstring.empty", FT_UINT8, BASE_DEC,
                NULL, 0x0, "This is an empty bitstring", HFILL }},
        { &hf_ber_id_pc, {
                "P/C", "ber.id.pc", FT_BOOLEAN, 8,
                TFS(&ber_pc_codes), 0x20, "Primitive or Constructed BER encoding", HFILL }},
        { &hf_ber_id_uni_tag, {
                "Tag", "ber.id.uni_tag", FT_UINT8, BASE_DEC|BASE_EXT_STRING,
                &ber_uni_tag_codes_ext, 0x1f, "Universal tag type", HFILL }},
        { &hf_ber_id_uni_tag_ext, {
                "Tag", "ber.id.uni_tag", FT_UINT32, BASE_DEC,
                NULL, 0, "Universal tag type", HFILL }},
        { &hf_ber_id_tag, {
                "Tag", "ber.id.tag", FT_UINT8, BASE_DEC,
                NULL, 0x1f, "Tag value for non-Universal classes", HFILL }},
        { &hf_ber_id_tag_ext, {
                "Tag", "ber.id.tag", FT_UINT32, BASE_DEC,
                NULL, 0, "Tag value for non-Universal classes", HFILL }},
        { &hf_ber_length_octets, {
                "Length Octets", "ber.length_octets", FT_UINT8, BASE_DEC,
                NULL, 0, "Number of length octets", HFILL }},
        { &hf_ber_length, {
                "Length", "ber.length", FT_UINT32, BASE_DEC,
                NULL, 0, "Length of contents", HFILL }},
        { &hf_ber_unknown_OCTETSTRING, {
                "OCTETSTRING", "ber.unknown.OCTETSTRING", FT_BYTES, BASE_NONE,
                NULL, 0, "This is an unknown OCTETSTRING", HFILL }},
        { &hf_ber_unknown_BER_OCTETSTRING, {
                "OCTETSTRING [BER encoded]", "ber.unknown.OCTETSTRING", FT_NONE, BASE_NONE,
                NULL, 0, "This is an BER encoded OCTETSTRING", HFILL }},
        { &hf_ber_unknown_BER_primitive, {
                "Primitive [BER encoded]", "ber.unknown.primitive", FT_NONE, BASE_NONE,
                NULL, 0, "This is a BER encoded Primitive", HFILL }},
        { &hf_ber_unknown_OID, {
                "OID", "ber.unknown.OID", FT_OID, BASE_NONE,
                NULL, 0, "This is an unknown Object Identifier", HFILL }},
        { &hf_ber_unknown_relative_OID, {
                "OID", "ber.unknown.relative_OID", FT_REL_OID, BASE_NONE,
                NULL, 0, "This is an unknown relative Object Identifier", HFILL }},
        { &hf_ber_unknown_GraphicString, {
                "GRAPHICSTRING", "ber.unknown.GRAPHICSTRING", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown GRAPHICSTRING", HFILL }},
        { &hf_ber_unknown_NumericString, {
                "NumericString", "ber.unknown.NumericString", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown NumericString", HFILL }},
        { &hf_ber_unknown_PrintableString, {
                "PrintableString", "ber.unknown.PrintableString", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown PrintableString", HFILL }},
        { &hf_ber_unknown_TeletexString, {
                "TeletexString", "ber.unknown.TeletexString", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown TeletexString", HFILL }},
        { &hf_ber_unknown_VisibleString, {
                "VisibleString", "ber.unknown.VisibleString", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown VisibleString", HFILL }},
        { &hf_ber_unknown_GeneralString, {
                "GeneralString", "ber.unknown.GeneralString", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown GeneralString", HFILL }},
        { &hf_ber_unknown_UniversalString, {
                "UniversalString", "ber.unknown.UniversalString", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown UniversalString", HFILL }},
        { &hf_ber_unknown_BMPString, {
                "BMPString", "ber.unknown.BMPString", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown BMPString", HFILL }},
        { &hf_ber_unknown_IA5String, {
                "IA5String", "ber.unknown.IA5String", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown IA5String", HFILL }},
        { &hf_ber_unknown_UTCTime, {
                "UTCTime", "ber.unknown.UTCTime", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown UTCTime", HFILL }},
        { &hf_ber_unknown_UTF8String, {
                "UTF8String", "ber.unknown.UTF8String", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown UTF8String", HFILL }},
        { &hf_ber_unknown_GeneralizedTime, {
                "GeneralizedTime", "ber.unknown.GeneralizedTime", FT_STRING, BASE_NONE,
                NULL, 0, "This is an unknown GeneralizedTime", HFILL }},
        { &hf_ber_unknown_INTEGER, {
                "INTEGER", "ber.unknown.INTEGER", FT_INT64, BASE_DEC,
                NULL, 0, "This is an unknown INTEGER", HFILL }},
        { &hf_ber_unknown_REAL, {
                "REAL", "ber.unknown.REAL", FT_DOUBLE, BASE_NONE,
                NULL, 0, "This is an unknown REAL", HFILL }},
        { &hf_ber_unknown_BITSTRING, {
                "BITSTRING", "ber.unknown.BITSTRING", FT_BYTES, BASE_NONE,
                NULL, 0, "This is an unknown BITSTRING", HFILL }},
        { &hf_ber_unknown_BOOLEAN, {
                "BOOLEAN", "ber.unknown.BOOLEAN", FT_UINT8, BASE_HEX,
                NULL, 0, "This is an unknown BOOLEAN", HFILL }},
        { &hf_ber_unknown_ENUMERATED, {
                "ENUMERATED", "ber.unknown.ENUMERATED", FT_UINT32, BASE_DEC,
                NULL, 0, "This is an unknown ENUMERATED", HFILL }},
        { &hf_ber_direct_reference,
          { "direct-reference", "ber.direct_reference",
            FT_OID, BASE_NONE, NULL, 0,
            "ber.OBJECT_IDENTIFIER", HFILL }},
        { &hf_ber_indirect_reference,
          { "indirect-reference", "ber.indirect_reference",
            FT_INT32, BASE_DEC, NULL, 0,
            "ber.INTEGER", HFILL }},
        { &hf_ber_data_value_descriptor,
          { "data-value-descriptor", "ber.data_value_descriptor",
            FT_STRING, BASE_NONE, NULL, 0,
            "ber.ObjectDescriptor", HFILL }},
        { &hf_ber_encoding,
          { "encoding", "ber.encoding",
            FT_UINT32, BASE_DEC, VALS(ber_T_encoding_vals), 0,
            "ber.T_encoding", HFILL }},
        { &hf_ber_octet_aligned,
          { "octet-aligned", "ber.octet_aligned",
            FT_BYTES, BASE_NONE, NULL, 0,
            "ber.T_octet_aligned", HFILL }},
        { &hf_ber_arbitrary,
          { "arbitrary", "ber.arbitrary",
            FT_BYTES, BASE_NONE, NULL, 0,
            "ber.T_arbitrary", HFILL }},
        { &hf_ber_single_ASN1_type,
          { "single-ASN1-type", "ber.single_ASN1_type",
            FT_NONE, BASE_NONE, NULL, 0,
            "ber.T_single_ASN1_type", HFILL }},
        { &hf_ber_extra_data,
          { "Extra data", "ber.extra_data",
            FT_BYTES, BASE_NONE, NULL, 0,
            NULL, HFILL }},

        /* Fragment entries */
        { &hf_ber_fragments,
          { "OCTET STRING fragments", "ber.octet_string.fragments", FT_NONE, BASE_NONE,
            NULL, 0x00, NULL, HFILL } },
        { &hf_ber_fragment,
          { "OCTET STRING fragment", "ber.octet_string.fragment", FT_FRAMENUM, BASE_NONE,
            NULL, 0x00, NULL, HFILL } },
        { &hf_ber_fragment_overlap,
          { "OCTET STRING fragment overlap", "ber.octet_string.fragment.overlap", FT_BOOLEAN,
            BASE_NONE, NULL, 0x0, NULL, HFILL } },
        { &hf_ber_fragment_overlap_conflicts,
          { "OCTET STRING fragment overlapping with conflicting data",
            "ber.octet_string.fragment.overlap.conflicts", FT_BOOLEAN, BASE_NONE, NULL,
            0x0, NULL, HFILL } },
        { &hf_ber_fragment_multiple_tails,
          { "OCTET STRING has multiple tail fragments",
            "ber.octet_string.fragment.multiple_tails", FT_BOOLEAN, BASE_NONE,
            NULL, 0x0, NULL, HFILL } },
        { &hf_ber_fragment_too_long_fragment,
          { "OCTET STRING fragment too long", "ber.octet_string.fragment.too_long_fragment",
            FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL,
            HFILL } },
        { &hf_ber_fragment_error,
          { "OCTET STRING defragmentation error", "ber.octet_string.fragment.error", FT_FRAMENUM,
            BASE_NONE, NULL, 0x00, NULL, HFILL } },
        { &hf_ber_fragment_count,
          { "OCTET STRING fragment count", "ber.octet_string.fragment.count", FT_UINT32, BASE_DEC,
            NULL, 0x00, NULL, HFILL } },
        { &hf_ber_reassembled_in,
          { "Reassembled in", "ber.octet_string.reassembled.in", FT_FRAMENUM, BASE_NONE,
            NULL, 0x00, NULL, HFILL } },
        { &hf_ber_reassembled_length,
          { "Reassembled OCTET STRING length", "ber.octet_string.reassembled.length", FT_UINT32, BASE_DEC,
            NULL, 0x00, NULL, HFILL } },

      /* Generated from convert_proto_tree_add_text.pl */
      { &hf_ber_null_tag, { "NULL tag", "ber.null_tag", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_unknown_data, { "Unknown Data", "ber.unknown_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_unknown_octetstring, { "Unknown OctetString", "ber.unknown_octetstring", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_seq_field_eoc, { "SEQ FIELD EOC", "ber.seq_field_eoc", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_seq_eoc, { "SEQ EOC", "ber.seq_eoc", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_set_field_eoc, { "SET FIELD EOC", "ber.set_field_eoc", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_set_eoc, { "SET EOC", "ber.set_eoc", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_choice_eoc, { "CHOICE EOC", "ber.choice_eoc", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_seq_of_eoc, { "SEQ OF EOC", "ber.seq_of_eoc", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_64bit_uint_as_bytes, { "64bits unsigned integer", "ber.64bit_uint_as_bytes", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
      { &hf_ber_encoding_boiler_plate, { "BER encoded protocol, to see BER internal fields set protocol BER preferences", "ber.encoding_boiler_plate", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
    };

    static int *ett[] = {
        &ett_ber_octet_string,
        &ett_ber_reassembled_octet_string,
        &ett_ber_primitive,
        &ett_ber_unknown,
        &ett_ber_SEQUENCE,
        &ett_ber_EXTERNAL,
        &ett_ber_T_encoding,
        &ett_ber_fragment,
        &ett_ber_fragments
    };
    static ei_register_info ei[] = {
        { &ei_ber_size_constraint_string, { "ber.size_constraint.string", PI_PROTOCOL, PI_WARN, "Size constraint: string", EXPFILL }},
        { &ei_ber_size_constraint_value, { "ber.size_constraint.value", PI_PROTOCOL, PI_WARN, "Size constraint: values", EXPFILL }},
        { &ei_ber_size_constraint_items, { "ber.size_constraint.items", PI_PROTOCOL, PI_WARN, "Size constraint: items", EXPFILL }},
        { &ei_ber_sequence_field_wrong, { "ber.error.sequence.field_wrong", PI_MALFORMED, PI_WARN, "BER Error: Wrong field in SEQUENCE", EXPFILL }},
        { &ei_ber_expected_octet_string, { "ber.error.expected.octet_string", PI_MALFORMED, PI_WARN, "BER Error: OctetString expected", EXPFILL }},
        { &ei_ber_expected_null, { "ber.error.expected.null", PI_MALFORMED, PI_WARN, "BER Error: NULL expected", EXPFILL }},
        { &ei_ber_expected_null_zero_length, { "ber.error.expected.null_zero_length", PI_MALFORMED, PI_WARN, "BER Error: NULL type expects zero length data", EXPFILL }},
        { &ei_ber_expected_sequence, { "ber.error.expected.sequence", PI_MALFORMED, PI_WARN, "BER Error: Sequence expected", EXPFILL }},
        { &ei_ber_expected_set, { "ber.error.expected.set", PI_MALFORMED, PI_WARN, "BER Error: SET expected", EXPFILL }},
        { &ei_ber_expected_string, { "ber.error.expected.string", PI_MALFORMED, PI_WARN, "BER Error: String expected", EXPFILL }},
        { &ei_ber_expected_object_identifier, { "ber.error.expected.object_identifier", PI_MALFORMED, PI_WARN, "BER Error: Object Identifier expected", EXPFILL }},
        { &ei_ber_expected_generalized_time, { "ber.error.expected.generalized_time", PI_MALFORMED, PI_WARN, "BER Error: GeneralizedTime expected", EXPFILL }},
        { &ei_ber_expected_utc_time, { "ber.error.expected.utc_time", PI_MALFORMED, PI_WARN, "BER Error: UTCTime expected", EXPFILL }},
        { &ei_ber_expected_bitstring, { "ber.error.expected.bitstring", PI_MALFORMED, PI_WARN, "BER Error: BitString expected", EXPFILL }},
        { &ei_ber_error_length, { "ber.error.length", PI_MALFORMED, PI_WARN, "BER Error: length is not valid", EXPFILL }},
        { &ei_ber_wrong_tag_in_tagged_type, { "ber.error.wrong_tag_in_tagged_type", PI_MALFORMED, PI_WARN, "BER Error: Wrong tag in tagged type", EXPFILL }},
        { &ei_ber_universal_tag_unknown, { "ber.error.universal_tag_unknown", PI_MALFORMED, PI_WARN, "BER Error: can not handle universal", EXPFILL }},
        { &ei_ber_no_oid, { "ber.error.no_oid", PI_MALFORMED, PI_WARN, "BER Error: No OID supplied to call_ber_oid_callback", EXPFILL }},
        { &ei_ber_oid_not_implemented, { "ber.error.oid_not_implemented", PI_UNDECODED, PI_WARN, "BER: Dissector for OID not implemented. Contact Wireshark developers if you want this supported", EXPFILL }},
        { &ei_ber_syntax_not_implemented, { "ber.error.syntax_not_implemented", PI_UNDECODED, PI_WARN, "BER: Dissector for syntax not implemented", EXPFILL }},
        { &ei_ber_value_too_many_bytes, { "ber.error.value_too_many_bytes", PI_MALFORMED, PI_WARN, "Value is encoded with too many bytes", EXPFILL }},
        { &ei_ber_unknown_field_sequence, { "ber.error.unknown_field.sequence", PI_MALFORMED, PI_WARN, "BER Error: Unknown field in Sequence", EXPFILL }},
        { &ei_ber_unknown_field_set, { "ber.error.unknown_field.set", PI_MALFORMED, PI_WARN, "BER Error: Unknown field in SET", EXPFILL }},
        { &ei_ber_missing_field_set, { "ber.error.missing_field.set", PI_MALFORMED, PI_WARN, "BER Error: Missing field in SET", EXPFILL }},
        { &ei_ber_empty_choice, { "ber.error.empty_choice", PI_MALFORMED, PI_WARN, "BER Error: Empty choice was found", EXPFILL }},
        { &ei_ber_choice_not_found, { "ber.error.choice_not_found", PI_MALFORMED, PI_WARN, "BER Error: This choice field was not found", EXPFILL }},
        { &ei_ber_bits_unknown, { "ber.error.bits_unknown", PI_UNDECODED, PI_WARN, "BER Error: Bits unknown", EXPFILL }},
        { &ei_ber_bits_set_padded, { "ber.error.bits_set_padded", PI_UNDECODED, PI_WARN, "BER Error: Bits set in padded area", EXPFILL }},
        { &ei_ber_illegal_padding, { "ber.error.illegal_padding", PI_UNDECODED, PI_WARN, "Illegal padding", EXPFILL }},
        { &ei_ber_invalid_format_generalized_time, { "ber.error.invalid_format.generalized_time", PI_MALFORMED, PI_WARN, "BER Error: GeneralizedTime invalid format", EXPFILL }},
        { &ei_ber_invalid_format_utctime, { "ber.error.invalid_format.utctime", PI_MALFORMED, PI_WARN, "BER Error: malformed UTCTime encoding", EXPFILL }},
        { &ei_hf_field_not_integer_type, { "ber.error.hf_field_not_integer_type", PI_PROTOCOL, PI_ERROR, "Was passed a HF field that was not integer type", EXPFILL }},
        { &ei_ber_constr_bitstr,{ "ber.error.constr_bitstr.len", PI_MALFORMED, PI_WARN, "BER Error: malformed Bitstring encoding", EXPFILL } },
        { &ei_ber_real_not_primitive,{ "ber.error.not_primitive.real", PI_MALFORMED, PI_WARN, "BER Error: REAL class not encoded as primitive", EXPFILL } },
    };

    /* Decode As handling */
    static build_valid_func ber_da_build_value[1] = {ber_value};
    static decode_as_value_t ber_da_values = {ber_prompt, 1, ber_da_build_value};
    static decode_as_t ber_da = {"ber", "ber.syntax", 1, 0, &ber_da_values, NULL, NULL,
                                ber_populate_list, ber_decode_as_reset, ber_decode_as_change, NULL};

    module_t *ber_module;
    expert_module_t* expert_ber;
    uat_t* users_uat = uat_new("OID Tables",
                               sizeof(oid_user_t),
                               "oid",
                               false,
                               &oid_users,
                               &num_oid_users,
                               UAT_AFFECTS_DISSECTION, /* affects dissection of packets, but not set of named fields */
                               "ChObjectIdentifiers",
                               oid_copy_cb,
                               NULL,
                               oid_free_cb,
                               ber_update_oids,
                               NULL,
                               users_flds);

    proto_ber = proto_register_protocol("Basic Encoding Rules (ASN.1 X.690)", "BER", "ber");

    ber_handle = register_dissector("ber", dissect_ber, proto_ber);
    ber_file_handle = register_dissector("ber_file", dissect_ber_file, proto_ber);

    proto_register_field_array(proto_ber, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    expert_ber = expert_register_protocol(proto_ber);
    expert_register_field_array(expert_ber, ei, array_length(ei));

    proto_set_cant_toggle(proto_ber);

    /* Register preferences */
    ber_module = prefs_register_protocol(proto_ber, NULL);

    prefs_register_bool_preference(ber_module, "show_internals",
                                   "Show internal BER encapsulation tokens",
                                   "Whether the dissector should also display internal"
                                   " ASN.1 BER details such as Identifier and Length fields", &show_internal_ber_fields);
    prefs_register_bool_preference(ber_module, "decode_unexpected",
                                   "Decode unexpected tags as BER encoded data",
                                   "Whether the dissector should decode unexpected tags as"
                                   " ASN.1 BER encoded data", &decode_unexpected);
    prefs_register_bool_preference(ber_module, "decode_octetstring",
                                   "Decode OCTET STRING as BER encoded data",
                                   "Whether the dissector should try decoding OCTET STRINGs as"
                                   " constructed ASN.1 BER encoded data", &decode_octetstring_as_ber);

    prefs_register_bool_preference(ber_module, "decode_primitive",
                                   "Decode Primitive as BER encoded data",
                                   "Whether the dissector should try decoding unknown primitive as"
                                   " constructed ASN.1 BER encoded data", &decode_primitive_as_ber);

    prefs_register_bool_preference(ber_module, "warn_too_many_bytes",
                                   "Warn if too many leading zero bits in encoded data",
                                   "Whether the dissector should warn if excessive leading zero (0) bits",
                                   &decode_warning_leading_zero_bits);

    prefs_register_uat_preference(ber_module, "oid_table", "Object Identifiers",
                                  "A table that provides names for object identifiers"
                                  " and the syntax of any associated values",
                                  users_uat);

    ber_oid_dissector_table = register_dissector_table("ber.oid", "BER OID", proto_ber, FT_STRING, STRING_CASE_SENSITIVE);
    ber_syntax_dissector_table = register_dissector_table("ber.syntax", "BER syntax", proto_ber, FT_STRING, STRING_CASE_SENSITIVE);
    syntax_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); /* oid to syntax */

    register_ber_syntax_dissector("ASN.1", proto_ber, dissect_ber_syntax);

    reassembly_table_register(&octet_segment_reassembly_table,
                          &addresses_reassembly_table_functions);

    register_shutdown_routine(ber_shutdown);

    register_decode_as(&ber_da);
}

void
proto_reg_handoff_ber(void)
{
    unsigned i = 1;

    oid_add_from_string("asn1", "2.1");
    oid_add_from_string("basic-encoding", "2.1.1");

    ber_decode_as_foreach(ber_add_syntax_name, &i);

    if (i > 1)
        qsort(&syntax_names[1], i - 1, sizeof(value_string), cmp_value_string);
    syntax_names[i].value = 0;
    syntax_names[i].strptr = NULL;

    /* allow the dissection of BER/DER carried over a TCP/UDP transport
       by using "Decode As..." */
    dissector_add_for_decode_as_with_preference("tcp.port", ber_handle);
    dissector_add_for_decode_as_with_preference("udp.port", ber_handle);

    ber_update_oids();

    dissector_add_uint("wtap_encap", WTAP_ENCAP_BER, ber_file_handle);

    dissector_add_string("media_type.suffix", "ber", ber_handle); /* RFC 6839 */
    dissector_add_string("media_type.suffix", "der", ber_handle); /* RFC 6839 */
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */