aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-dvbci.c
blob: 9389e433b8e43b734f64ec8e02900f8e0e939640 (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
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
/* packet-dvbci.c
 * Routines for DVB-CI (Common Interface) dissection
 * Copyright 2011-2015, Martin Kaiser <martin@kaiser.cx>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* This dissector supports DVB-CI as defined in EN50221 and
 * CI+ (www.ci-plus.com).
 * For more details, see https://wiki.wireshark.org/DVB-CI.
 *
 * The pcap input format for this dissector is documented at
 * http://www.kaiser.cx/pcap-dvbci.html.
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/addr_resolv.h>
#include <epan/circuit.h>
#include <epan/dvb_chartbl.h>
#include <epan/exported_pdu.h>
#include <epan/reassemble.h>
#include <epan/prefs.h>
#include <epan/tap.h>
#include <epan/expert.h>
#include <epan/asn1.h>
#include "packet-dvbci.h"
#include "packet-mpeg-descriptor.h"
#include "packet-mpeg-sect.h"
#include "packet-mpeg-pmt.h"
#include "packet-x509af.h"
#include "packet-x509ce.h"
#include "packet-ber.h"
#include <wsutil/wsgcrypt.h>

void proto_register_dvbci(void);
void proto_reg_handoff_dvbci(void);

#define AES_BLOCK_LEN 16
#define AES_KEY_LEN 16

#define EXPORTED_SAC_MSG_PROTO "CI+ SAC message"

#define IS_DATA_TRANSFER(e) (e==DVBCI_EVT_DATA_CAM_TO_HOST || e==DVBCI_EVT_DATA_HOST_TO_CAM)

/* direction of data transfer in [as]pdu_info_t and elsewhere */
#define DATA_CAM_TO_HOST  DVBCI_EVT_DATA_CAM_TO_HOST
#define DATA_HOST_TO_CAM  DVBCI_EVT_DATA_HOST_TO_CAM
#define DIRECTION_ANY     0x0

/* source/destination address field */
#define ADDR_HOST "Host"
#define ADDR_CAM  "CAM"

/* hardware event */
#define CAM_IN    0x01
#define CAM_OUT   0x02
#define POWER_ON  0x03
#define POWER_OFF 0x04
#define TS_ROUTE  0x05
#define TS_BYPASS 0x06
#define RESET_H   0x07
#define RESET_L   0x08
#define READY_H   0x09
#define READY_L   0x0A

/* Card Information Structure (CIS) */

/* tuples */
#define CISTPL_NO_LINK       0x14
#define CISTPL_VERS_1        0x15
#define CISTPL_CONFIG        0x1A
#define CISTPL_CFTABLE_ENTRY 0x1B
#define CISTPL_DEVICE_OC     0x1C
#define CISTPL_DEVICE_OA     0x1D
#define CISTPL_MANFID        0x20
#define CISTPL_END           0xFF
/* subtuple */
#define CCSTPL_CIF           0xC0
/* interface types */
#define TPCE_IF_TYPE_MEM     0
#define TPCE_IF_TYPE_IO_MEM  1
#define TPCE_IF_TYPE_CUST0   4
#define TPCE_IF_TYPE_CUST1   5
#define TPCE_IF_TYPE_CUST2   6
#define TPCE_IF_TYPE_CUST3   7
/* "voltage used" field in the device tuples */
#define CIS_DEV_VCC50 0
#define CIS_DEV_VCC33 1

/* link layer */
#define ML_MORE 0x80
#define ML_LAST 0x00

/* base values of sequence ids for reassembly of fragmented tpdus
   if there's two open transport connections, their tpdu fragments may be
    interleaved, we must add the tcid to the base value in order to
    distinguish between different transport connections
   TPDU_SEQ_ID_BASE and SPDU_SEQ_ID_BASE can be arbitrary 32bit values, they
    must be more than 256 apart since we add the 8bit tcid */
#define TPDU_SEQ_ID_BASE       123
/* same as above, the spdu fragments are also demultiplexed based on the
    t_c_id field */
#define SPDU_SEQ_ID_BASE      2417

/* transport layer */
#define NO_TAG        0x00
#define T_SB          0x80
#define T_RCV         0x81
#define T_CREATE_T_C  0x82
#define T_C_T_C_REPLY 0x83
#define T_DELETE_T_C  0x84
#define T_D_T_C_REPLY 0x85
#define T_REQUEST_T_C 0x86
#define T_NEW_T_C     0x87
#define T_T_C_ERROR   0x88
#define T_DATA_LAST   0xA0
#define T_DATA_MORE   0xA1

#define SB_VAL_MSG_AVAILABLE    0x80
#define SB_VAL_NO_MSG_AVAILABLE 0x00

/* session layer */
#define T_SESSION_NUMBER          0x90
#define T_OPEN_SESSION_REQUEST    0x91
#define T_OPEN_SESSION_RESPONSE   0x92
#define T_CREATE_SESSION          0x93
#define T_CREATE_SESSION_RESPONSE 0x94
#define T_CLOSE_SESSION_REQUEST   0x95
#define T_CLOSE_SESSION_RESPONSE  0x96

/* status for open/create session */
#define SESS_OPENED                   0x00
#define SESS_NOT_OPENED_RES_NON_EXIST 0xF0
#define SESS_NOT_OPENED_RES_UNAVAIL   0xF1
#define SESS_NOT_OPENED_RES_VER_LOWER 0xF2
#define SESS_NOT_OPENED_RES_BUSY      0xF3

/* status for close session */
#define SESS_CLOSED       0x00
#define SESS_NB_NOT_ALLOC 0xF0

/* circuit id from session number (16bit) and transport connection id * (8bit) */
#define CT_ID(s,t) ((guint32)(s<<8|t))

/* resource id */
#define RES_ID_TYPE_MASK 0xC0000000
#define RES_CLASS_MASK   0x3FFF0000
#define RES_TYPE_MASK    0x0000FFC0
#define RES_VER_MASK     0x0000003F

/* resource class */
#define RES_CLASS_RM  0x01
#define RES_CLASS_AP  0x02
#define RES_CLASS_CA  0x03
#define RES_CLASS_AUT 0x10
#define RES_CLASS_HC  0x20
#define RES_CLASS_DT  0x24
#define RES_CLASS_MMI 0x40
#define RES_CLASS_AMI 0x41
#define RES_CLASS_LSC 0x60
#define RES_CLASS_CC  0x8C
#define RES_CLASS_HLC 0x8D
#define RES_CLASS_CUP 0x8E
#define RES_CLASS_OPP 0x8F
#define RES_CLASS_SAS 0x96

#define RES_ID_LEN 4 /* bytes */
#define RES_CLASS(_res_id) (_res_id & RES_CLASS_MASK) >> 16
#define RES_VER(_res_id)   (_res_id & RES_VER_MASK)

/* appinfo resource */
#define APP_TYPE_CA  0x1
#define APP_TYPE_EPG 0x2

#define DATA_RATE_72 0x0
#define DATA_RATE_96 0x1

/* ca resource */
#define LIST_MGMT_MORE   0x0
#define LIST_MGMT_FIRST  0x1
#define LIST_MGMT_LAST   0x2
#define LIST_MGMT_ONLY   0x3
#define LIST_MGMT_ADD    0x4
#define LIST_MGMT_UPDATE 0x5

#define CMD_ID_OK_DESCR     0x1
#define CMD_ID_OK_MMI       0x2
#define CMD_ID_QUERY        0x3
#define CMD_ID_NOT_SELECTED 0x4

#define CA_DESC_TAG 0x9

#define CA_ENAB_DESC_OK             0x01
#define CA_ENAB_DESC_OK_PURCHASE    0x02
#define CA_ENAB_DESC_OK_TECH        0x03
#define CA_ENAB_DESC_NG_ENTITLEMENT 0x71
#define CA_ENAB_DESC_NG_TECH        0x73

/* host control resource */
#define HC_STAT_OK            0x0
#define HC_STAT_ERR_DLVRY     0x1
#define HC_STAT_ERR_LOCK      0x2
#define HC_STAT_ERR_BUSY      0x3
#define HC_STAT_ERR_PARAM     0x4
#define HC_STAT_ERR_NOT_FOUND 0x5
#define HC_STAT_ERR_UNKNOWN   0x6

#define HC_RELEASE_OK      0x0
#define HC_RELEASE_REFUSED 0x1

/* mmi resource */
#define CLOSE_MMI_CMD_ID_IMMEDIATE 0x0
#define CLOSE_MMI_CMD_ID_DELAY     0x1

/* only commands and parameters for high-level mmi are supported */
#define DISP_CMD_SET_MMI_MODE 1
#define DISP_CMD_GET_DISP_TBL 2
#define DISP_CMD_GET_INP_TBL  3

#define MMI_MODE_HIGH 1

#define DISP_REP_ID_MMI_MODE_ACK     0x01
#define DISP_REP_ID_DISP_CHAR_TBL    0x02
#define DISP_REP_ID_INP_CHAR_TBL     0x03
#define DISP_REP_ID_UNKNOWN_CMD      0xF0
#define DISP_REP_ID_UNKNOWN_MMI_MODE 0xF1
#define DISP_REP_ID_UNKNOWN_CHAR_TBL 0xF2

#define VISIBLE_ANS 0
#define BLIND_ANS   1

#define ANSW_ID_CANCEL 0x00
#define ANSW_ID_ANSWER 0x01

/* used for answer_text_length, choice_nb and item_nb */
#define NB_UNKNOWN 0xFF

/* cam upgrade resource */
#define CUP_DELAYED   0x0
#define CUP_IMMEDIATE 0x1

#define CUP_ANS_NO  0x0
#define CUP_ANS_YES 0x1
#define CUP_ANS_ASK 0x2

#define CUP_RESET_PCMCIA 0x0
#define CUP_RESET_CMDIF  0x1
#define CUP_RESET_NONE   0x2

/* content control resource */
#define CC_ID_HOST_ID            0x05
#define CC_ID_CICAM_ID           0x06
#define CC_ID_HOST_BRAND_CERT    0x07
#define CC_ID_CICAM_BRAND_CERT   0x08
#define CC_ID_KP                 0x0C
#define CC_ID_DHPH               0x0D
#define CC_ID_DHPM               0x0E
#define CC_ID_HOST_DEV_CERT      0x0F
#define CC_ID_CICAM_DEV_CERT     0x10
#define CC_ID_SIG_A              0x11
#define CC_ID_SIG_B              0x12
#define CC_ID_AUTH_NONCE         0x13
#define CC_ID_NS_HOST            0x14
#define CC_ID_NS_MODULE          0x15
#define CC_ID_AKH                0x16
#define CC_ID_URI                0x19
#define CC_ID_PROG_NUM           0x1A
#define CC_ID_URI_CNF            0x1B
#define CC_ID_KEY_REGISTER       0x1C
#define CC_ID_URI_VERSIONS       0x1D
#define CC_ID_STATUS_FIELD       0x1E
#define CC_ID_SRM_DATA           0x1F
#define CC_ID_SRM_CONFIRM        0x20
#define CC_ID_CICAM_LICENSE      0x21
#define CC_ID_LICENSE_STATUS     0x22
#define CC_ID_LICENSE_RCV_STATUS 0x23
#define CC_ID_HOST_LICENSE       0x24
#define CC_ID_PLAY_COUNT         0x25
#define CC_ID_OPERATING_MODE     0x26
#define CC_ID_PINCODE_DATA       0x27
#define CC_ID_REC_START_STATUS   0x28
#define CC_ID_MODE_CHG_STATUS    0x29
#define CC_ID_REC_STOP_STATUS    0x2A

#define CC_EMI_FREE    0x00
#define CC_EMI_NO_MORE 0x01
#define CC_EMI_ONCE    0x02
#define CC_EMI_NEVER   0x03

#define CC_KEY_EVEN 0x0
#define CC_KEY_ODD  0x1

#define CC_STATUS_OK                    0x0
#define CC_STATUS_NO_CC_SUPPORT         0x1
#define CC_STATUS_HOST_BUSY             0x2
#define CC_STATUS_AUTH_FAILED_OR_NO_SRM 0x3
#define CC_STATUS_CICAM_BUSY            0x4
#define CC_STATUS_REC_MODE_ERR          0x5

#define SAC_MSG_HDR_LEN 8

#define CC_SAC_AUTH_AES128_XCBC_MAC 0x0
#define CC_SAC_ENC_AES128_CBC       0x0

#define CC_CAP_NONE               0x0
#define CC_CAP_CAS_PIN            0x1
#define CC_CAP_CAS_FTA_PIN        0x2
#define CC_CAP_CAS_PIN_CACHED     0x3
#define CC_CAP_CAS_FTA_PIN_CACHED 0x4

/* length of DVB-SI utc time field in bytes */
#define UTC_TIME_LEN 5

#define CC_PIN_BAD         0x0
#define CC_PIN_CAM_BUSY    0x1
#define CC_PIN_OK          0x2
#define CC_PIN_UNCONFIRMED 0x3
#define CC_PIN_VB_NOT_REQ  0x4
#define CC_PIN_CSA         0x5

#define CC_OP_MODE_WATCH_BUFFER 0x0
#define CC_OP_MODE_TIMESHIFT    0x1
#define CC_OP_MODE_UNATTENDED   0x2

/* application mmi resource */
#define ACK_CODE_OK        0x1
#define ACK_CODE_WRONG_API 0x2
#define ACK_CODE_API_BUSY  0x3

#define REQ_TYPE_FILE      0x0
#define REQ_TYPE_DATA      0x1
#define REQ_TYPE_FILE_HASH 0x2
#define REQ_TYPE_REQ       0x3

/* lsc resource */
#define COMMS_CMD_ID_CONNECT_ON_CHANNEL    1
#define COMMS_CMD_ID_DISCONNECT_ON_CHANNEL 2
#define COMMS_CMD_ID_SET_PARAMS            3
#define COMMS_CMD_ID_ENQUIRE_STATUS        4
#define COMMS_CMD_ID_GET_NEXT_BUFFER       5

#define CONN_DESC_TEL      1
#define CONN_DESC_CABLE    2
#define CONN_DESC_IP       3
#define CONN_DESC_HOSTNAME 4

#define LSC_DESC_IP       0xCF
#define LSC_DESC_HOSTNAME 0xCD

#define LSC_IPV4 1
#define LSC_IPV6 2

#define LSC_TCP 1
#define LSC_UDP 2

#define COMMS_REP_ID_CONNECT_ACK         1
#define COMMS_REP_ID_DISCONNECT_ACK      2
#define COMMS_REP_ID_SET_PARAMS_ACK      3
#define COMMS_REP_ID_STATUS_REPLY        4
#define COMMS_REP_ID_GET_NEXT_BUFFER_ACK 5
#define COMMS_REP_ID_SEND_ACK            6

#define LSC_RET_OK 0
#define LSC_RET_DISCONNECTED 0
#define LSC_RET_CONNECTED    1
#define LSC_RET_TOO_BIG 0xFE

/* operator profile resource */
#define TABLE_ID_CICAM_NIT 0x40  /* CICAM NIT must be a NIT actual */

#define OPP_REF_REG_FLG_NONE  0
#define OPP_REF_REG_FLG_ADV   1
#define OPP_REF_REG_FLG_URG   2
#define OPP_REF_REG_FLG_SCHED 3

#define OPP_ERR_FLG_OK          0
#define OPP_ERR_FLG_PROF        1
#define OPP_ERR_FLG_UNSUPPORTED 2
#define OPP_ERR_FLG_CANCELLED   3

/* EIT p/f, EIT schedule usage */
#define OPP_EIT_ABSENT 0
#define OPP_EIT_NOT_X  1
#define OPP_EIT_FULL_X 2
#define OPP_EIT_BARKER 3
#define OPP_EPG_APP    4

#define OPP_EXT_EVT_DIFF 0
#define OPP_EXT_EVT_ADD  1

/* these values match the delivery system descriptor tags */
#define OPP_DLV_CAP_S  0x43
#define OPP_DLV_CAP_C  0x44
#define OPP_DLV_CAP_T  0x5A
#define OPP_DLV_CAP_S2 0x79

#define OPP_TUNE_OK          0
#define OPP_TUNE_UNSUPPORTED 1
#define OPP_TUNE_INVALID     2
#define OPP_TUNE_ERR         3

#define OPP_NO_MORE_DESC 0xFF

/* sas resource */
#define SAS_SESS_STATE_CONNECTED 0
#define SAS_SESS_STATE_NOT_FOUND 1
#define SAS_SESS_STATE_DENIED    2


/* application layer */

#define APDU_TAG_SIZE 3

/* "don't care" value for min_len_field and len_field (this can't be 0) */
#define LEN_FIELD_ANY G_MAXUINT32

static GHashTable *apdu_table = NULL;

typedef struct _apdu_info_t {
    guint32 tag;
    /* the minimum length required for this apdu */
    guint32 min_len_field;
    /* if the apdu has a well-known length, we enforce it here
     * (otherwise, we set this to LEN_FIELD_ANY) */
    guint32 len_field;
    guint8 direction;
    guint16 res_class;
    guint8  res_min_ver;
    void (*dissect_payload)(guint32, gint,
            tvbuff_t *, gint, circuit_t *, packet_info *, proto_tree *);
} apdu_info_t;


static void
dissect_dvbci_payload_rm(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_ap(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_ca(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_aut(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo _U_, proto_tree *tree);
static void
dissect_dvbci_payload_hc(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_dt(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_mmi(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_hlc(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_cup(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_cc(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_ami(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_lsc(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_opp(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree);
static void
dissect_dvbci_payload_sas(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit,
        packet_info *pinfo, proto_tree *tree);


/* apdu defines */
#define T_PROFILE_ENQ                   0x9F8010
#define T_PROFILE                       0x9F8011
#define T_PROFILE_CHANGE                0x9F8012
#define T_APP_INFO_ENQ                  0x9F8020
#define T_APP_INFO                      0x9F8021
#define T_ENTER_MENU                    0x9F8022
#define T_REQUEST_CICAM_RESET           0x9F8023
#define T_DATARATE_INFO                 0x9F8024
#define T_CA_INFO_ENQ                   0x9F8030
#define T_CA_INFO                       0x9F8031
#define T_CA_PMT                        0x9F8032
#define T_CA_PMT_REPLY                  0x9F8033
#define T_AUTH_REQ                      0x9F8200
#define T_AUTH_RESP                     0x9F8201
#define T_TUNE                          0x9F8400
#define T_REPLACE                       0x9F8401
#define T_CLEAR_REPLACE                 0x9F8402
#define T_ASK_RELEASE                   0x9F8403
#define T_TUNE_BROADCAST_REQ            0x9F8404
#define T_TUNE_REPLY                    0x9F8405
#define T_ASK_RELEASE_REPLY             0x9F8406
#define T_DATE_TIME_ENQ                 0x9F8440
#define T_DATE_TIME                     0x9F8441
#define T_CLOSE_MMI                     0x9F8800
#define T_DISPLAY_CONTROL               0x9F8801
#define T_DISPLAY_REPLY                 0x9F8802
#define T_ENQ                           0x9F8807
#define T_ANSW                          0x9F8808
#define T_MENU_LAST                     0x9F8809
#define T_MENU_MORE                     0x9F880A
#define T_MENU_ANSW                     0x9F880B
#define T_LIST_LAST                     0x9F880C
#define T_LIST_MORE                     0x9F880D
#define T_HOST_COUNTRY_ENQ              0x9F8100
#define T_HOST_COUNTRY                  0x9F8101
#define T_HOST_LANGUAGE_ENQ             0x9F8110
#define T_HOST_LANGUAGE                 0x9F8111
#define T_CAM_FIRMWARE_UPGRADE          0x9F9D01
#define T_CAM_FIRMWARE_UPGRADE_REPLY    0x9F9D02
#define T_CAM_FIRMWARE_UPGRADE_PROGRESS 0x9F9D03
#define T_CAM_FIRMWARE_UPGRADE_COMPLETE 0x9F9D04
#define T_CC_OPEN_REQ                   0x9F9001
#define T_CC_OPEN_CNF                   0x9F9002
#define T_CC_DATA_REQ                   0x9F9003
#define T_CC_DATA_CNF                   0x9F9004
#define T_CC_SYNC_REQ                   0x9F9005
#define T_CC_SYNC_CNF                   0x9F9006
#define T_CC_SAC_DATA_REQ               0x9F9007
#define T_CC_SAC_DATA_CNF               0x9F9008
#define T_CC_SAC_SYNC_REQ               0x9F9009
#define T_CC_SAC_SYNC_CNF               0x9F9010
#define T_CC_PIN_CAPABILITIES_REQ       0x9F9011
#define T_CC_PIN_CAPABILITIES_REPLY     0x9F9012
#define T_CC_PIN_CMD                    0x9F9013
#define T_CC_PIN_REPLY                  0x9F9014
#define T_CC_PIN_EVENT                  0x9F9015
#define T_CC_PIN_PLAYBACK               0x9F9016
#define T_CC_PIN_MMI_REQ                0x9F9017
#define T_REQUEST_START                 0x9F8000
#define T_REQUEST_START_ACK             0x9F8001
#define T_FILE_REQUEST                  0x9F8002
#define T_FILE_ACKNOWLEDGE              0x9F8003
#define T_APP_ABORT_REQUEST             0x9F8004
#define T_APP_ABORT_ACK                 0x9F8005
#define T_COMMS_CMD                     0x9F8C00
#define T_COMMS_REPLY                   0x9F8C02
#define T_COMMS_SEND_LAST               0x9F8C03
#define T_COMMS_SEND_MORE               0x9F8C04
#define T_COMMS_RCV_LAST                0x9F8C05
#define T_COMMS_RCV_MORE                0x9F8C06
#define T_OPERATOR_STATUS_REQ           0x9F9C00
#define T_OPERATOR_STATUS               0x9F9C01
#define T_OPERATOR_NIT_REQ              0x9F9C02
#define T_OPERATOR_NIT                  0x9F9C03
#define T_OPERATOR_INFO_REQ             0x9F9C04
#define T_OPERATOR_INFO                 0x9F9C05
#define T_OPERATOR_SEARCH_START         0x9F9C06
#define T_OPERATOR_SEARCH_STATUS        0x9F9C07
#define T_OPERATOR_EXIT                 0x9F9C08
#define T_OPERATOR_TUNE                 0x9F9C09
#define T_OPERATOR_TUNE_STATUS          0x9F9C0A
#define T_OPERATOR_ENTITLEMENT_ACK      0x9F9C0B
#define T_OPERATOR_SEARCH_CANCEL        0x9F9C0C
#define T_SAS_CONNECT_RQST              0x9F9A00
#define T_SAS_CONNECT_CNF               0x9F9A01
#define T_SAS_ASYNC_MSG                 0x9F9A07

/* these are no real apdus, they just use the same format */
#define T_TEXT_LAST             0x9F8803
#define T_TEXT_MORE             0x9F8804
#define T_CONNECTION_DESCRIPTOR 0x9F8C01

#define IS_MENU_APDU(t) (t==T_MENU_MORE || t==T_MENU_LAST)


static const apdu_info_t apdu_info[] = {
    {T_PROFILE_ENQ,         0, 0,             DIRECTION_ANY,    RES_CLASS_RM, 1, NULL},
    {T_PROFILE,             0, LEN_FIELD_ANY, DIRECTION_ANY,    RES_CLASS_RM, 1, dissect_dvbci_payload_rm},
    {T_PROFILE_CHANGE,      0, 0,             DIRECTION_ANY,    RES_CLASS_RM, 1, NULL},

    {T_APP_INFO_ENQ,        0, 0,             DATA_HOST_TO_CAM, RES_CLASS_AP, 1, NULL},
    {T_APP_INFO,            6, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_AP, 1, dissect_dvbci_payload_ap},
    {T_ENTER_MENU,          0, 0,             DATA_HOST_TO_CAM, RES_CLASS_AP, 1, NULL},
    {T_REQUEST_CICAM_RESET, 0, 0,             DATA_CAM_TO_HOST, RES_CLASS_AP, 3, NULL},
    {T_DATARATE_INFO,       0, 1,             DATA_HOST_TO_CAM, RES_CLASS_AP, 3, dissect_dvbci_payload_ap},

    {T_CA_INFO_ENQ,         0, 0,             DATA_HOST_TO_CAM, RES_CLASS_CA, 1, NULL},
    {T_CA_INFO,             0, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_CA, 1, dissect_dvbci_payload_ca},
    {T_CA_PMT,              6, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_CA, 1, dissect_dvbci_payload_ca},
    {T_CA_PMT_REPLY,        8, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_CA, 1, dissect_dvbci_payload_ca},

    {T_AUTH_REQ,            2, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_AUT, 1, dissect_dvbci_payload_aut},
    {T_AUTH_RESP,           2, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_AUT, 1, dissect_dvbci_payload_aut},

    {T_TUNE,                0, 8,             DATA_CAM_TO_HOST, RES_CLASS_HC, 1, dissect_dvbci_payload_hc},
    {T_REPLACE,             0, 5,             DATA_CAM_TO_HOST, RES_CLASS_HC, 1, dissect_dvbci_payload_hc},
    {T_CLEAR_REPLACE,       0, 1,             DATA_CAM_TO_HOST, RES_CLASS_HC, 1, dissect_dvbci_payload_hc},
    {T_ASK_RELEASE,         0, 0,             DATA_HOST_TO_CAM, RES_CLASS_HC, 1, NULL},
    {T_TUNE_BROADCAST_REQ,  5, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_HC, 2, dissect_dvbci_payload_hc},
    {T_TUNE_REPLY,          1, 1,             DATA_HOST_TO_CAM, RES_CLASS_HC, 2, dissect_dvbci_payload_hc},
    {T_ASK_RELEASE_REPLY,   1, 1,             DATA_CAM_TO_HOST, RES_CLASS_HC, 2, dissect_dvbci_payload_hc},

    {T_DATE_TIME_ENQ,       0, 1,             DATA_CAM_TO_HOST, RES_CLASS_DT, 1, dissect_dvbci_payload_dt},
    {T_DATE_TIME,           5, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_DT, 1, dissect_dvbci_payload_dt},

    {T_CLOSE_MMI,           1, LEN_FIELD_ANY, DIRECTION_ANY,    RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},
    {T_DISPLAY_CONTROL,     1, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},
    {T_DISPLAY_REPLY,       1, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},
    {T_ENQ,                 2, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},
    {T_ANSW,                1, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},
    {T_MENU_LAST,          13, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},
    {T_MENU_MORE,          13, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},
    {T_MENU_ANSW,           0, 1,             DATA_HOST_TO_CAM, RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},
    {T_LIST_LAST,          13, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},
    {T_LIST_MORE,          13, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_MMI, 1, dissect_dvbci_payload_mmi},

    {T_HOST_COUNTRY_ENQ,    0, 0,             DATA_CAM_TO_HOST, RES_CLASS_HLC, 1, NULL},
    {T_HOST_COUNTRY,        0, 3,             DATA_HOST_TO_CAM, RES_CLASS_HLC, 1, dissect_dvbci_payload_hlc},
    {T_HOST_LANGUAGE_ENQ,   0, 0,             DATA_CAM_TO_HOST, RES_CLASS_HLC, 1, NULL},
    {T_HOST_LANGUAGE,       0, 3,             DATA_HOST_TO_CAM, RES_CLASS_HLC, 1, dissect_dvbci_payload_hlc},

    {T_CAM_FIRMWARE_UPGRADE,          0, 3, DATA_CAM_TO_HOST, RES_CLASS_CUP, 1, dissect_dvbci_payload_cup},
    {T_CAM_FIRMWARE_UPGRADE_REPLY,    0, 1, DATA_HOST_TO_CAM, RES_CLASS_CUP, 1, dissect_dvbci_payload_cup},
    {T_CAM_FIRMWARE_UPGRADE_PROGRESS, 0, 1, DATA_CAM_TO_HOST, RES_CLASS_CUP, 1, dissect_dvbci_payload_cup},
    {T_CAM_FIRMWARE_UPGRADE_COMPLETE, 0, 1, DATA_CAM_TO_HOST, RES_CLASS_CUP, 1, dissect_dvbci_payload_cup},

    {T_CC_OPEN_REQ,                0,  0,             DATA_CAM_TO_HOST, RES_CLASS_CC, 1, NULL},
    {T_CC_OPEN_CNF,                0,  1,             DATA_HOST_TO_CAM, RES_CLASS_CC, 1, dissect_dvbci_payload_cc},
    {T_CC_DATA_REQ,                3,  LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_CC, 1, dissect_dvbci_payload_cc},
    {T_CC_DATA_CNF,                2,  LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_CC, 1, dissect_dvbci_payload_cc},
    {T_CC_SYNC_REQ,                0,  0,             DATA_CAM_TO_HOST, RES_CLASS_CC, 1, NULL},
    {T_CC_SYNC_CNF,                0,  1,             DATA_HOST_TO_CAM, RES_CLASS_CC, 1, dissect_dvbci_payload_cc},
    {T_CC_SAC_DATA_REQ,            8,  LEN_FIELD_ANY, DIRECTION_ANY,    RES_CLASS_CC, 1, dissect_dvbci_payload_cc},
    {T_CC_SAC_DATA_CNF,            8,  LEN_FIELD_ANY, DIRECTION_ANY,    RES_CLASS_CC, 1, dissect_dvbci_payload_cc},
    {T_CC_SAC_SYNC_REQ,            8,  LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_CC, 1, dissect_dvbci_payload_cc},
    {T_CC_SAC_SYNC_CNF,            8,  LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_CC, 1, dissect_dvbci_payload_cc},
    {T_CC_PIN_CAPABILITIES_REQ,    0,  0,             DATA_HOST_TO_CAM, RES_CLASS_CC, 2, NULL},
    {T_CC_PIN_CAPABILITIES_REPLY,  7,  7,             DATA_CAM_TO_HOST, RES_CLASS_CC, 2, dissect_dvbci_payload_cc},
    {T_CC_PIN_CMD,                 1,  LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_CC, 2, dissect_dvbci_payload_cc},
    {T_CC_PIN_REPLY,               1,  1,             DATA_CAM_TO_HOST, RES_CLASS_CC, 2, dissect_dvbci_payload_cc},
    {T_CC_PIN_EVENT,              25, 25,             DATA_CAM_TO_HOST, RES_CLASS_CC, 2, dissect_dvbci_payload_cc},
    {T_CC_PIN_PLAYBACK,           16, 16,             DATA_HOST_TO_CAM, RES_CLASS_CC, 2, dissect_dvbci_payload_cc},
    {T_CC_PIN_MMI_REQ,             1,  LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_CC, 2, dissect_dvbci_payload_cc},

    {T_REQUEST_START,       2, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_AMI, 1, dissect_dvbci_payload_ami},
    {T_REQUEST_START_ACK,   0, 1,             DATA_HOST_TO_CAM, RES_CLASS_AMI, 1, dissect_dvbci_payload_ami},
    {T_FILE_REQUEST,        1, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_AMI, 1, dissect_dvbci_payload_ami},
    {T_FILE_ACKNOWLEDGE,    2, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_AMI, 1, dissect_dvbci_payload_ami},
    {T_APP_ABORT_REQUEST,   0, LEN_FIELD_ANY, DIRECTION_ANY,    RES_CLASS_AMI, 1, dissect_dvbci_payload_ami},
    {T_APP_ABORT_ACK,       0, LEN_FIELD_ANY, DIRECTION_ANY,    RES_CLASS_AMI, 1, dissect_dvbci_payload_ami},

    {T_COMMS_CMD,           1, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_LSC, 1, dissect_dvbci_payload_lsc},
    {T_COMMS_REPLY,         0, 2,             DATA_HOST_TO_CAM, RES_CLASS_LSC, 1, dissect_dvbci_payload_lsc},
    {T_COMMS_SEND_LAST,     2, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_LSC, 1, dissect_dvbci_payload_lsc},
    {T_COMMS_SEND_MORE,     2, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_LSC, 1, dissect_dvbci_payload_lsc},
    {T_COMMS_RCV_LAST,      2, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_LSC, 1, dissect_dvbci_payload_lsc},
    {T_COMMS_RCV_MORE,      2, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_LSC, 1, dissect_dvbci_payload_lsc},

    {T_OPERATOR_STATUS_REQ,       0, 0,             DATA_HOST_TO_CAM, RES_CLASS_OPP, 1, NULL},
    {T_OPERATOR_STATUS,           0, 6,             DATA_CAM_TO_HOST, RES_CLASS_OPP, 1, dissect_dvbci_payload_opp},
    {T_OPERATOR_NIT_REQ,          0, 0,             DATA_HOST_TO_CAM, RES_CLASS_OPP, 1, NULL},
    {T_OPERATOR_NIT,              2, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_OPP, 1, dissect_dvbci_payload_opp},
    {T_OPERATOR_INFO_REQ,         0, 0,             DATA_HOST_TO_CAM, RES_CLASS_OPP, 1, NULL},
    {T_OPERATOR_INFO,             1, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_OPP, 1, dissect_dvbci_payload_opp},
    {T_OPERATOR_SEARCH_START,     3, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_OPP, 1, dissect_dvbci_payload_opp},
    {T_OPERATOR_SEARCH_STATUS,    0, 6,             DATA_CAM_TO_HOST, RES_CLASS_OPP, 1, dissect_dvbci_payload_opp},
    {T_OPERATOR_EXIT,             0, 0,             DATA_HOST_TO_CAM, RES_CLASS_OPP, 1, NULL},
    {T_OPERATOR_TUNE,             2, LEN_FIELD_ANY, DATA_CAM_TO_HOST, RES_CLASS_OPP, 1, dissect_dvbci_payload_opp},
    {T_OPERATOR_TUNE_STATUS,      5, LEN_FIELD_ANY, DATA_HOST_TO_CAM, RES_CLASS_OPP, 1, dissect_dvbci_payload_opp},
    {T_OPERATOR_ENTITLEMENT_ACK,  0, 0,             DATA_HOST_TO_CAM, RES_CLASS_OPP, 1, NULL},
    {T_OPERATOR_SEARCH_CANCEL,    0, 0,             DATA_HOST_TO_CAM, RES_CLASS_OPP, 1, NULL},

    {T_SAS_CONNECT_RQST,    0, 8,             DATA_HOST_TO_CAM, RES_CLASS_SAS, 1, dissect_dvbci_payload_sas},
    {T_SAS_CONNECT_CNF,     0, 9,             DATA_CAM_TO_HOST, RES_CLASS_SAS, 1, dissect_dvbci_payload_sas},
    {T_SAS_ASYNC_MSG,       3, LEN_FIELD_ANY, DIRECTION_ANY,    RES_CLASS_SAS, 1, dissect_dvbci_payload_sas}
};

static const value_string dvbci_apdu_tag[] = {
    { T_PROFILE_ENQ,                   "Profile enquiry" },
    { T_PROFILE,                       "Profile information" },
    { T_PROFILE_CHANGE,                "Profile change notification" },
    { T_APP_INFO_ENQ,                  "Application info enquiry" },
    { T_APP_INFO,                      "Application info" },
    { T_ENTER_MENU,                    "Enter menu" },
    { T_REQUEST_CICAM_RESET,           "Request CICAM reset" },
    { T_DATARATE_INFO,                 "Datarate info" },
    { T_CA_INFO_ENQ,                   "CA info enquiry" },
    { T_CA_INFO,                       "CA info" },
    { T_CA_PMT,                        "CA PMT" },
    { T_CA_PMT_REPLY,                  "CA PMT reply" },
    { T_AUTH_REQ,                      "Authentication request" },
    { T_AUTH_RESP,                     "Authentication response" },
    { T_TUNE,                          "Tune" },
    { T_REPLACE,                       "Replace" },
    { T_CLEAR_REPLACE,                 "Clear replace" },
    { T_ASK_RELEASE,                   "Ask release" },
    { T_TUNE_BROADCAST_REQ,            "Tune broadcast request" },
    { T_TUNE_REPLY,                    "Tune reply" },
    { T_ASK_RELEASE_REPLY,             "Ask release reply" },
    { T_DATE_TIME_ENQ,                 "Date-Time enquiry" },
    { T_DATE_TIME,                     "Date-Time" },
    { T_CLOSE_MMI,                     "Close MMI" },
    { T_DISPLAY_CONTROL,               "Display control" },
    { T_DISPLAY_REPLY,                 "Display reply" },
    { T_TEXT_LAST,                     "Text last" },
    { T_TEXT_MORE,                     "Text more" },
    { T_ENQ,                           "Enquiry" },
    { T_ANSW,                          "Answer" },
    { T_MENU_LAST,                     "Menu last" },
    { T_MENU_MORE,                     "Menu more" },
    { T_MENU_ANSW,                     "Menu answer" },
    { T_LIST_LAST,                     "List last" },
    { T_LIST_MORE,                     "List more" },
    { T_HOST_COUNTRY_ENQ,              "Host country enquiry" },
    { T_HOST_COUNTRY,                  "Host country" },
    { T_HOST_LANGUAGE_ENQ,             "Host language enquiry" },
    { T_HOST_LANGUAGE,                 "Host language" },
    { T_CAM_FIRMWARE_UPGRADE,          "CAM firmware upgrade" },
    { T_CAM_FIRMWARE_UPGRADE_REPLY,    "CAM firmware upgrade reply" },
    { T_CAM_FIRMWARE_UPGRADE_PROGRESS, "CAM firmware upgrade progress" },
    { T_CAM_FIRMWARE_UPGRADE_COMPLETE, "CAM firmware upgrade complete" },
    { T_CC_OPEN_REQ,                   "CC open request" },
    { T_CC_OPEN_CNF,                   "CC open confirm" },
    { T_CC_DATA_REQ,                   "CC data request" },
    { T_CC_DATA_CNF,                   "CC data confirm" },
    { T_CC_SYNC_REQ,                   "CC sync request" },
    { T_CC_SYNC_CNF,                   "CC sync confirm" },
    { T_CC_SAC_DATA_REQ,               "CC SAC data request" },
    { T_CC_SAC_DATA_CNF,               "CC SAC data confirm" },
    { T_CC_SAC_SYNC_REQ,               "CC SAC sync request" },
    { T_CC_SAC_SYNC_CNF,               "CC SAC sync confirm" },
    { T_CC_PIN_CAPABILITIES_REQ,       "CC PIN capabilities request" },
    { T_CC_PIN_CAPABILITIES_REPLY,     "CC PIN capabilities reply" },
    { T_CC_PIN_CMD,                    "CC PIN command" },
    { T_CC_PIN_REPLY,                  "CC PIN reply" },
    { T_CC_PIN_EVENT,                  "CC PIN event" },
    { T_CC_PIN_PLAYBACK,               "CC PIN playback" },
    { T_CC_PIN_MMI_REQ,                "CC PIN MMI request" },
    { T_REQUEST_START,                 "Request start" },
    { T_REQUEST_START_ACK,             "Request start ack" },
    { T_FILE_REQUEST,                  "File request" },
    { T_FILE_ACKNOWLEDGE,              "File acknowledge" },
    { T_APP_ABORT_REQUEST,             "App abort request" },
    { T_APP_ABORT_ACK,                 "App abort ack" },
    { T_COMMS_CMD,                     "Comms command" },
    { T_COMMS_REPLY,                   "Comms reply" },
    { T_CONNECTION_DESCRIPTOR,         "Connection descriptor" },
    { T_COMMS_SEND_LAST,               "Comms send last" },
    { T_COMMS_SEND_MORE,               "Comms send more" },
    { T_COMMS_RCV_LAST,                "Comms receive last" },
    { T_COMMS_RCV_MORE,                "Comms receive more" },
    { T_OPERATOR_STATUS_REQ,           "Operator status request" },
    { T_OPERATOR_STATUS,               "Operator status" },
    { T_OPERATOR_NIT_REQ,              "Operator NIT request" },
    { T_OPERATOR_NIT,                  "Operator NIT" },
    { T_OPERATOR_INFO_REQ,             "Operator info request" },
    { T_OPERATOR_INFO,                 "Operator info" },
    { T_OPERATOR_SEARCH_START,         "Operator search start" },
    { T_OPERATOR_SEARCH_STATUS,        "Operator search status" },
    { T_OPERATOR_EXIT,                 "Operator exit" },
    { T_OPERATOR_TUNE,                 "Operator tune" },
    { T_OPERATOR_TUNE_STATUS,          "Operator tune status" },
    { T_OPERATOR_ENTITLEMENT_ACK,      "Operator entitlement acknowledge" },
    { T_OPERATOR_SEARCH_CANCEL,        "Operator search cancel" },
    { T_SAS_CONNECT_RQST,              "SAS connect request" },
    { T_SAS_CONNECT_CNF,               "SAS connect confirm" },
    { T_SAS_ASYNC_MSG,                 "SAS async message" },
    { 0, NULL }
};

WS_DLL_PUBLIC_DEF const value_string dvbci_event[] = {
    { DVBCI_EVT_DATA_HOST_TO_CAM, "data transfer Host -> CAM" },
    { DVBCI_EVT_DATA_CAM_TO_HOST, "data transfer CAM -> Host" },
    { DVBCI_EVT_CIS_READ,         "read the Card Information Structure (CIS)" },
    { DVBCI_EVT_COR_WRITE,        "write into the Configuration Option Register (COR)" },
    { DVBCI_EVT_HW_EVT,           "hardware event" },
    { 0, NULL }
};

static int proto_dvbci = -1;

static const gchar *dvbci_sek = NULL;
static const gchar *dvbci_siv = NULL;
static gboolean dvbci_dissect_lsc_msg = FALSE;

/* the output of pref_key_string_to_bin() applied to dvbci_sek and _siv */
static unsigned char *dvbci_sek_bin = NULL;
static unsigned char *dvbci_siv_bin = NULL;

static dissector_handle_t data_handle;
static dissector_handle_t mpeg_pmt_handle;
static dissector_handle_t dvb_nit_handle;
static dissector_handle_t png_handle;
static dissector_table_t tcp_dissector_table;
static dissector_table_t udp_dissector_table;

static gint exported_pdu_tap = -1;

static gint ett_dvbci = -1;
static gint ett_dvbci_hdr = -1;
static gint ett_dvbci_cis = -1;
static gint ett_dvbci_cis_tpl = -1;
static gint ett_dvbci_cis_subtpl = -1;
static gint ett_dvbci_link = -1;
static gint ett_dvbci_link_frag = -1;
static gint ett_dvbci_link_frags = -1;
static gint ett_dvbci_transport = -1;
static gint ett_dvbci_transport_frag = -1;
static gint ett_dvbci_transport_frags = -1;
static gint ett_dvbci_session = -1;
static gint ett_dvbci_res = -1;
static gint ett_dvbci_application = -1;
static gint ett_dvbci_es = -1;
static gint ett_dvbci_ca_desc = -1;
static gint ett_dvbci_text = -1;
static gint ett_dvbci_cc_item = -1;
static gint ett_dvbci_sac_msg_body = -1;
static gint ett_dvbci_ami_req_types = -1;
static gint ett_dvbci_lsc_conn_desc = -1;
static gint ett_dvbci_opp_cap_loop = -1;
static gint ett_dvbci_dlv_sys_hint = -1;


static int hf_dvbci_hdr_ver = -1;
static int hf_dvbci_event = -1;
static int hf_dvbci_len = -1;
static int hf_dvbci_hw_event = -1;
static int hf_dvbci_cor_addr = -1;
static int hf_dvbci_cor_val = -1;
static int hf_dvbci_cis_tpl_code = -1;
static int hf_dvbci_cis_tpl_len = -1;
static int hf_dvbci_cis_tpl_data = -1;
static int hf_dvbci_cis_tpll_v1_major = -1;
static int hf_dvbci_cis_tpll_v1_minor = -1;
static int hf_dvbci_cis_tpll_v1_info_manuf = -1;
static int hf_dvbci_cis_tpll_v1_info_name = -1;
static int hf_dvbci_cis_tpll_v1_info_additional = -1;
static int hf_dvbci_cis_tpll_v1_end = -1;
static int hf_dvbci_cis_tpcc_rfsz = -1;
static int hf_dvbci_cis_tpcc_rmsz = -1;
static int hf_dvbci_cis_tpcc_rasz = -1;
static int hf_dvbci_cis_tpcc_last = -1;
static int hf_dvbci_cis_tpcc_radr = -1;
static int hf_dvbci_cis_tpcc_rmsk = -1;
static int hf_dvbci_cis_st_code = -1;
static int hf_dvbci_cis_st_len = -1;
static int hf_dvbci_cis_stci_ifn_size = -1;
static int hf_dvbci_cis_stci_ifn = -1;
static int hf_dvbci_cis_stci_str = -1;
static int hf_dvbci_cis_tpce_indx_intface = -1;
static int hf_dvbci_cis_tpce_indx_default = -1;
static int hf_dvbci_cis_tpce_indx_cnf_entry = -1;
static int hf_dvbci_cis_tpce_if_type = -1;
static int hf_dvbci_cis_tpce_fs_mem_space = -1;
static int hf_dvbci_cis_tpce_fs_irq = -1;
static int hf_dvbci_cis_tpce_fs_io = -1;
static int hf_dvbci_cis_dev_vcc_used = -1;
static int hf_dvbci_cis_dev_mwait = -1;
static int hf_dvbci_cis_dev_oth_cond_info = -1;
static int hf_dvbci_cis_tplmid_manf = -1;
static int hf_dvbci_cis_tplmid_card = -1;
static int hf_dvbci_buf_size = -1;
static int hf_dvbci_tcid = -1;
static int hf_dvbci_ml = -1;
static int hf_dvbci_l_frags = -1;
static int hf_dvbci_l_frag = -1;
static int hf_dvbci_l_frag_overlap = -1;
static int hf_dvbci_l_frag_overlap_conflicts = -1;
static int hf_dvbci_l_frag_multiple_tails = -1;
static int hf_dvbci_l_frag_too_long_frag = -1;
static int hf_dvbci_l_frag_err = -1;
static int hf_dvbci_l_frag_cnt = -1;
static int hf_dvbci_l_reass_in = -1;
static int hf_dvbci_l_reass_len = -1;
static int hf_dvbci_c_tpdu_tag = -1;
static int hf_dvbci_r_tpdu_tag = -1;
static int hf_dvbci_t_c_id = -1;
static int hf_dvbci_sb_tag = -1;
static int hf_dvbci_sb_value = -1;
static int hf_dvbci_t_frags = -1;
static int hf_dvbci_t_frag = -1;
static int hf_dvbci_t_frag_overlap = -1;
static int hf_dvbci_t_frag_overlap_conflicts = -1;
static int hf_dvbci_t_frag_multiple_tails = -1;
static int hf_dvbci_t_frag_too_long_frag = -1;
static int hf_dvbci_t_frag_err = -1;
static int hf_dvbci_t_frag_cnt = -1;
static int hf_dvbci_t_reass_in = -1;
static int hf_dvbci_t_reass_len = -1;
static int hf_dvbci_spdu_tag = -1;
static int hf_dvbci_sess_status = -1;
static int hf_dvbci_sess_nb = -1;
static int hf_dvbci_close_sess_status = -1;
static int hf_dvbci_res_id = -1;
static int hf_dvbci_res_id_type = -1;
static int hf_dvbci_res_class = -1;
static int hf_dvbci_res_type = -1;
static int hf_dvbci_res_ver = -1;
static int hf_dvbci_apdu_tag = -1;
static int hf_dvbci_app_type = -1;
static int hf_dvbci_app_manf = -1;
static int hf_dvbci_manf_code = -1;
static int hf_dvbci_menu_str_len = -1;
static int hf_dvbci_ap_char_tbl = -1;
static int hf_dvbci_menu_str = -1;
static int hf_dvbci_data_rate = -1;
static int hf_dvbci_ca_sys_id = -1;
static int hf_dvbci_ca_pmt_list_mgmt = -1;
static int hf_dvbci_prog_num = -1;
static int hf_dvbci_ca_ver = -1;
static int hf_dvbci_curr_next = -1;
static int hf_dvbci_prog_info_len = -1;
static int hf_dvbci_stream_type = -1;
static int hf_dvbci_es_pid = -1;
static int hf_dvbci_es_info_len = -1;
static int hf_dvbci_ca_pmt_cmd_id = -1;
static int hf_dvbci_descr_len = -1;
static int hf_dvbci_ca_pid = -1;
static int hf_dvbci_ca_priv_data = -1;
static int hf_dvbci_ca_enable_flag = -1;
static int hf_dvbci_ca_enable = -1;
static int hf_dvbci_auth_proto_id = -1;
static int hf_dvbci_auth_req_bytes = -1;
static int hf_dvbci_auth_resp_bytes = -1;
static int hf_dvbci_network_id = -1;
static int hf_dvbci_original_network_id = -1;
static int hf_dvbci_transport_stream_id = -1;
static int hf_dvbci_service_id = -1;
static int hf_dvbci_replacement_ref = -1;
static int hf_dvbci_replaced_pid = -1;
static int hf_dvbci_replacement_pid = -1;
static int hf_dvbci_pmt_flag = -1;
static int hf_dvbci_hc_desc_loop_len = -1;
static int hf_dvbci_hc_status = -1;
static int hf_dvbci_hc_release_reply = -1;
static int hf_dvbci_resp_intv = -1;
static int hf_dvbci_utc_time = -1;
static int hf_dvbci_local_offset = -1;
static int hf_dvbci_close_mmi_cmd_id = -1;
static int hf_dvbci_close_mmi_delay = -1;
static int hf_dvbci_disp_ctl_cmd = -1;
static int hf_dvbci_mmi_mode = -1;
static int hf_dvbci_disp_rep_id = -1;
static int hf_dvbci_mmi_char_tbl = -1;
static int hf_dvbci_blind_ans = -1;
static int hf_dvbci_ans_txt_len = -1;
static int hf_dvbci_enq = -1;
static int hf_dvbci_ans_id = -1;
static int hf_dvbci_ans = -1;
static int hf_dvbci_choice_nb = -1;
static int hf_dvbci_choice_ref = -1;
static int hf_dvbci_item_nb = -1;
static int hf_dvbci_title = -1;
static int hf_dvbci_subtitle = -1;
static int hf_dvbci_bottom = -1;
static int hf_dvbci_item = -1;
static int hf_dvbci_host_country = -1;
static int hf_dvbci_host_language = -1;
static int hf_dvbci_cup_type = -1;
static int hf_dvbci_cup_download_time = -1;
static int hf_dvbci_cup_answer = -1;
static int hf_dvbci_cup_progress = -1;
static int hf_dvbci_cup_reset = -1;
static int hf_dvbci_cc_sys_id_bitmask = -1;
static int hf_dvbci_cc_snd_dat_nbr = -1;
static int hf_dvbci_cc_req_dat_nbr = -1;
static int hf_dvbci_cc_dat_id = -1;
static int hf_dvbci_cc_dat_len = -1;
static int hf_dvbci_brand_cert = -1;
static int hf_dvbci_dev_cert = -1;
static int hf_dvbci_uri_ver = -1;
static int hf_dvbci_uri_aps = -1;
static int hf_dvbci_uri_emi = -1;
static int hf_dvbci_uri_ict = -1;
static int hf_dvbci_uri_rct = -1;
static int hf_dvbci_uri_dot = -1;
static int hf_dvbci_uri_rl = -1;
static int hf_dvbci_cc_key_register = -1;
static int hf_dvbci_cc_status_field = -1;
static int hf_dvbci_cc_op_mode = -1;
static int hf_dvbci_cc_data = -1;
static int hf_dvbci_sac_msg_ctr = -1;
static int hf_dvbci_sac_proto_ver = -1;
static int hf_dvbci_sac_auth_cip = -1;
static int hf_dvbci_sac_payload_enc = -1;
static int hf_dvbci_sac_enc_cip = -1;
static int hf_dvbci_sac_payload_len = -1;
static int hf_dvbci_sac_enc_body = -1;
static int hf_dvbci_sac_padding = -1;
static int hf_dvbci_sac_signature = -1;
static int hf_dvbci_rating = -1;
static int hf_dvbci_capability_field = -1;
static int hf_dvbci_pin_chg_time = -1;
static int hf_dvbci_pincode_status = -1;
static int hf_dvbci_cc_prog_num = -1;
static int hf_dvbci_pin_evt_time = -1;
static int hf_dvbci_pin_evt_cent = -1;
static int hf_dvbci_cc_priv_data = -1;
static int hf_dvbci_pincode = -1;
static int hf_dvbci_app_dom_id_len = -1;
static int hf_dvbci_init_obj_len = -1;
static int hf_dvbci_app_dom_id = -1;
static int hf_dvbci_init_obj = -1;
static int hf_dvbci_ack_code = -1;
static int hf_dvbci_req_type = -1;
static int hf_dvbci_file_hash = -1;
static int hf_dvbci_file_name_len = -1;
static int hf_dvbci_file_name = -1;
static int hf_dvbci_file_data_len = -1;
static int hf_dvbci_ami_priv_data = -1;
static int hf_dvbci_req_ok = -1;
static int hf_dvbci_file_ok = -1;
static int hf_dvbci_file_data = -1;
static int hf_dvbci_abort_req_code = -1;
static int hf_dvbci_abort_ack_code = -1;
static int hf_dvbci_phase_id = -1;
static int hf_dvbci_comms_rep_id = -1;
static int hf_dvbci_lsc_buf_size = -1;
static int hf_dvbci_lsc_ret_val = -1;
static int hf_dvbci_comms_cmd_id = -1;
static int hf_dvbci_conn_desc_type = -1;
static int hf_dvbci_lsc_media_tag = -1;
static int hf_dvbci_lsc_media_len = -1;
static int hf_dvbci_lsc_media_data = -1;
static int hf_dvbci_lsc_ip_ver = -1;
static int hf_dvbci_lsc_ipv4_addr = -1;
static int hf_dvbci_lsc_ipv6_addr = -1;
static int hf_dvbci_lsc_dst_port = -1;
static int hf_dvbci_lsc_proto = -1;
static int hf_dvbci_lsc_hostname = -1;
static int hf_dvbci_lsc_retry_count = -1;
static int hf_dvbci_lsc_timeout = -1;
static int hf_dvbci_info_ver_op_status = -1;
static int hf_dvbci_nit_ver = -1;
static int hf_dvbci_pro_typ = -1;
static int hf_dvbci_init_flag = -1;
static int hf_dvbci_ent_chg_flag = -1;
static int hf_dvbci_ent_val_flag = -1;
static int hf_dvbci_ref_req_flag = -1;
static int hf_dvbci_err_flag = -1;
static int hf_dvbci_dlv_sys_hint = -1;
static int hf_dvbci_dlv_sys_hint_t = -1;
static int hf_dvbci_dlv_sys_hint_s = -1;
static int hf_dvbci_dlv_sys_hint_c = -1;
static int hf_dvbci_refr_req_date = -1;
static int hf_dvbci_refr_req_time = -1;
static int hf_dvbci_nit_loop_len = -1;
static int hf_dvbci_info_valid = -1;
static int hf_dvbci_info_ver_op_info = -1;
static int hf_dvbci_cicam_onid = -1;
static int hf_dvbci_cicam_id = -1;
static int hf_dvbci_opp_char_tbl = -1;
static int hf_dvbci_sdt_rst_trusted = -1;
static int hf_dvbci_eit_rst_trusted = -1;
static int hf_dvbci_eit_pf_usage = -1;
static int hf_dvbci_eit_sch_usage = -1;
static int hf_dvbci_ext_evt_usage = -1;
static int hf_dvbci_sdt_oth_trusted = -1;
static int hf_dvbci_eit_evt_trigger = -1;
static int hf_dvbci_opp_lang_code = -1;
static int hf_dvbci_prof_name = -1;
static int hf_dvbci_unattended = -1;
static int hf_dvbci_opp_svc_type_loop_len = -1;
static int hf_dvbci_opp_svc_type = -1;
static int hf_dvbci_dlv_cap_loop_len = -1;
static int hf_dvbci_dlv_cap_byte = -1;
static int hf_dvbci_app_cap_loop_len = -1;
static int hf_dvbci_app_cap_bytes = -1;
static int hf_dvbci_desc_num = -1;
static int hf_dvbci_sig_strength = -1;
static int hf_dvbci_sig_qual = -1;
static int hf_dvbci_opp_tune_status = -1;
static int hf_dvbci_opp_desc_loop_len = -1;
static int hf_dvbci_sas_app_id = -1;
static int hf_dvbci_sas_sess_state = -1;
static int hf_dvbci_sas_msg_nb = -1;
static int hf_dvbci_sas_msg_len = -1;

static const int *dvb_ci_res_id_fields[] = {
  &hf_dvbci_res_id_type,
  &hf_dvbci_res_class,
  &hf_dvbci_res_type,
  &hf_dvbci_res_ver,
  NULL
};

static const int *dvbci_opp_dlv_sys_hint_fields[] = {
    &hf_dvbci_dlv_sys_hint_t,
    &hf_dvbci_dlv_sys_hint_s,
    &hf_dvbci_dlv_sys_hint_c,
    NULL
};


static expert_field ei_dvbci_cor_addr = EI_INIT;
static expert_field ei_dvbci_buf_size = EI_INIT;
static expert_field ei_dvbci_ml = EI_INIT;
static expert_field ei_dvbci_c_tpdu_tag = EI_INIT;
static expert_field ei_dvbci_r_tpdu_status_mandatory = EI_INIT;
static expert_field ei_dvbci_r_tpdu_tag = EI_INIT;
static expert_field ei_dvbci_sb_value = EI_INIT;
static expert_field ei_dvbci_t_c_id = EI_INIT;
static expert_field ei_dvbci_tpdu_status_tag = EI_INIT;
static expert_field ei_dvbci_spdu_tag = EI_INIT;
static expert_field ei_dvbci_spdu_cam_to_host = EI_INIT;
static expert_field ei_dvbci_spdu_host_to_cam = EI_INIT;
static expert_field ei_dvbci_apdu_tag = EI_INIT;
static expert_field ei_dvbci_apu_cam_to_host = EI_INIT;
static expert_field ei_dvbci_apu_host_to_cam = EI_INIT;
static expert_field ei_dvbci_apdu_not_supported = EI_INIT;
static expert_field ei_dvbci_res_ver = EI_INIT;
static expert_field ei_dvbci_res_class = EI_INIT;
static expert_field ei_dvbci_bad_length = EI_INIT;
static expert_field ei_dvbci_invalid_char_tbl = EI_INIT;
static expert_field ei_dvbci_no_ca_desc_es = EI_INIT;
static expert_field ei_dvbci_no_ca_desc_prog = EI_INIT;
static expert_field ei_dvbci_ca_pmt_cmd_id = EI_INIT;
static expert_field ei_dvbci_time_offs_unknown = EI_INIT;
static expert_field ei_dvbci_not_text_more_or_text_last = EI_INIT;
static expert_field ei_dvbci_network_id = EI_INIT;
static expert_field ei_dvbci_cc_pin_nvr_chg = EI_INIT;
static expert_field ei_dvbci_pin_evt_cent = EI_INIT;
static expert_field ei_dvbci_sac_payload_enc = EI_INIT;
static expert_field ei_dvbci_sig_qual = EI_INIT;
static expert_field ei_dvbci_cicam_nit_table_id = EI_INIT;
static expert_field ei_dvbci_cup_progress = EI_INIT;

static dissector_table_t sas_msg_dissector_table;

static reassembly_table tpdu_reassembly_table;
static reassembly_table spdu_reassembly_table;

static const fragment_items tpdu_frag_items = {
    &ett_dvbci_link_frag,
    &ett_dvbci_link_frags,

    &hf_dvbci_l_frags,
    &hf_dvbci_l_frag,
    &hf_dvbci_l_frag_overlap,
    &hf_dvbci_l_frag_overlap_conflicts,
    &hf_dvbci_l_frag_multiple_tails,
    &hf_dvbci_l_frag_too_long_frag,
    &hf_dvbci_l_frag_err,
    &hf_dvbci_l_frag_cnt,

    &hf_dvbci_l_reass_in,
    &hf_dvbci_l_reass_len,
    /* Reassembled data field */
    NULL,
    "Tpdu fragments"
};
static const fragment_items spdu_frag_items = {
    &ett_dvbci_transport_frag,
    &ett_dvbci_transport_frags,

    &hf_dvbci_t_frags,
    &hf_dvbci_t_frag,
    &hf_dvbci_t_frag_overlap,
    &hf_dvbci_t_frag_overlap_conflicts,
    &hf_dvbci_t_frag_multiple_tails,
    &hf_dvbci_t_frag_too_long_frag,
    &hf_dvbci_t_frag_err,
    &hf_dvbci_t_frag_cnt,

    &hf_dvbci_t_reass_in,
    &hf_dvbci_t_reass_len,
    /* Reassembled data field */
    NULL,
    "Spdu fragments"
};



typedef struct _spdu_info_t {
    guint8 tag;
    guint8 direction;
    guint8 len_field;
} spdu_info_t;

static const value_string dvbci_hw_event[] = {
    { CAM_IN,    "CI Module is inserted" },
    { CAM_OUT,   "CI Module is removed" },
    { POWER_ON,  "CI slot power on" },
    { POWER_OFF, "CI slot power off" },
    { TS_ROUTE,  "Transport stream routed through the CI Module" },
    { TS_BYPASS, "Transport stream bypasses the CI Module" },
    { RESET_H,   "Reset pin is high" },
    { RESET_L,   "Reset pin is low" },
    { READY_H,   "Ready pin is high" },
    { READY_L,   "Ready pin is low" },
    { 0, NULL }
};
static const value_string dvbci_cis_tpl_code[] = {
    { CISTPL_NO_LINK, "No-link tuple" },
    { CISTPL_VERS_1, "Level 1 version/product information" },
    { CISTPL_CONFIG, "Configuration for a 16bit PC-Card" },
    { CISTPL_CFTABLE_ENTRY, "Configuration-table entry" },
    { CISTPL_DEVICE_OC, "Device information for Common Memory" },
    { CISTPL_DEVICE_OA, "Device information for Attribute Memory" },
    { CISTPL_MANFID, "Manufacturer indentification string" },
    { CISTPL_END, "End of chain" },
    { 0, NULL }
};
static const value_string dvbci_cis_subtpl_code[] = {
    { CCSTPL_CIF, "Custom interface subtuple" },
    { 0, NULL }
};
static const value_string dvbci_cis_tpce_if_type[] = {
    { TPCE_IF_TYPE_MEM,    "Memory" },
    { TPCE_IF_TYPE_IO_MEM, "I/O and Memory" },
    { TPCE_IF_TYPE_CUST0,  "Custom Interface 0" },
    { TPCE_IF_TYPE_CUST1,  "Custom Interface 1" },
    { TPCE_IF_TYPE_CUST2,  "Custom Interface 2" },
    { TPCE_IF_TYPE_CUST3,  "Custom Interface 3" },
    { 0, NULL }
};
static const value_string dvbci_cis_dev_vcc_used[] = {
    { CIS_DEV_VCC50, "5.0V" },
    { CIS_DEV_VCC33, "3.3V" },
    { 0, NULL }
};
static const value_string dvbci_ml[] = {
    { ML_MORE, "more TPDU fragments pending" },
    { ML_LAST, "last TPDU fragment" },
    { 0, NULL }
};
static const value_string dvbci_c_tpdu[] = {
    { T_RCV, "T_RCV" },
    { T_CREATE_T_C,  "T_create_t_c" },
    { T_DELETE_T_C,  "T_delete_t_c" },
    { T_D_T_C_REPLY, "T_d_t_c_reply" },
    { T_NEW_T_C,     "T_new_t_c" },
    { T_T_C_ERROR,   "T_t_c_error" },
    { T_DATA_LAST,   "T_data_last" },
    { T_DATA_MORE,   "T_data_more" },
    { 0, NULL }
};
static const value_string dvbci_r_tpdu[] = {
    { T_C_T_C_REPLY, "T_c_tc_reply" },
    { T_DELETE_T_C,  "T_delete_t_c" },
    { T_D_T_C_REPLY, "T_d_t_c_reply" },
    { T_REQUEST_T_C, "T_request_t_c" },
    { T_DATA_LAST,   "T_data_last" },
    { T_DATA_MORE,   "T_data_more" },
    { 0, NULL }
};
static const value_string dvbci_sb_value[] = {
    { SB_VAL_MSG_AVAILABLE,    "message available" },
    { SB_VAL_NO_MSG_AVAILABLE, "no message available" },
    { 0, NULL }
};
static const value_string dvbci_spdu_tag[] = {
    { T_SESSION_NUMBER,          "Session Number (payload data)" },
    { T_OPEN_SESSION_REQUEST,    "Open Session Request" },
    { T_OPEN_SESSION_RESPONSE,   "Open Session Response" },
    { T_CREATE_SESSION,          "Create Session" },
    { T_CREATE_SESSION_RESPONSE, "Create Session Response" },
    { T_CLOSE_SESSION_REQUEST,   "Close Session Request" },
    { T_CLOSE_SESSION_RESPONSE,  "Close Session Response" },
    { 0, NULL }
};
static GHashTable *spdu_table = NULL;
static const spdu_info_t spdu_info[] = {
    { T_SESSION_NUMBER,          DIRECTION_ANY, 2 },
    { T_OPEN_SESSION_REQUEST,    DATA_CAM_TO_HOST, 4 },
    { T_OPEN_SESSION_RESPONSE,   DATA_HOST_TO_CAM, 7 },
    { T_CREATE_SESSION,          DATA_HOST_TO_CAM, 6 },
    { T_CREATE_SESSION_RESPONSE, DATA_CAM_TO_HOST, 7 },
    { T_CLOSE_SESSION_REQUEST,   DIRECTION_ANY, 2 },
    { T_CLOSE_SESSION_RESPONSE,  DIRECTION_ANY, 3 }
};
static const value_string dvbci_sess_status[] = {
    { SESS_OPENED,
      "Session opened" },
    { SESS_NOT_OPENED_RES_NON_EXIST,
      "Resource does not exist" },
    { SESS_NOT_OPENED_RES_UNAVAIL,
      "Resource exists but it's unavailable" },
    { SESS_NOT_OPENED_RES_VER_LOWER,
      "Existing resource's version is lower than requested version" },
    { SESS_NOT_OPENED_RES_BUSY,
      "Resource is busy" },
    { 0, NULL }
};
static const value_string dvbci_close_sess_status[] = {
    { SESS_CLOSED,       "Session closed" },
    { SESS_NB_NOT_ALLOC, "Session number not allocated" },
    { 0, NULL }
};
static const value_string dvbci_res_class[] = {
    { RES_CLASS_RM,  "Resource Manager" },
    { RES_CLASS_AP,  "Application Info" },
    { RES_CLASS_CA,  "Conditional Access" },
    { RES_CLASS_AUT, "Authentication" },
    { RES_CLASS_HC,  "Host Control" },
    { RES_CLASS_DT,  "Date-Time" },
    { RES_CLASS_MMI, "Man-machine interface (MMI)" },
    { RES_CLASS_AMI, "Application MMI" },
    { RES_CLASS_LSC, "Low-Speed Communication" },
    { RES_CLASS_CC,  "Content Control" },
    { RES_CLASS_HLC, "Host Language & Country" },
    { RES_CLASS_CUP, "CAM Upgrade" },
    { RES_CLASS_OPP, "Operator Profile" },
    { RES_CLASS_SAS, "Specific Application Support" },
    { 0, NULL }
};
static const value_string dvbci_app_type[] = {
    { APP_TYPE_CA,  "Conditional Access" },
    { APP_TYPE_EPG, "Electronic Progam Guide" },
    { 0, NULL }
};
static const value_string dvbci_data_rate[] = {
    { DATA_RATE_72, "72 Mbit/s" },
    { DATA_RATE_96, "96 Mbit/s" },
    { 0, NULL }
};
static const value_string dvbci_ca_pmt_list_mgmt[] = {
    { LIST_MGMT_MORE,   "more" },
    { LIST_MGMT_FIRST,  "first" },
    { LIST_MGMT_LAST,   "last" },
    { LIST_MGMT_ONLY,   "only" },
    { LIST_MGMT_ADD,    "add" },
    { LIST_MGMT_UPDATE, "update" },
    { 0, NULL }
};
static const value_string dvbci_ca_pmt_cmd_id[] = {
    { CMD_ID_OK_DESCR,     "ok descrambling" },
    { CMD_ID_OK_MMI,       "ok mmi" },
    { CMD_ID_QUERY,        "query" },
    { CMD_ID_NOT_SELECTED, "not selected" },
    { 0, NULL }
};
static const value_string dvbci_ca_enable[] = {
    { CA_ENAB_DESC_OK, "descrambling possible" },
    { CA_ENAB_DESC_OK_PURCHASE,
        "descrambling possible under conditions (purchase dialogue)" },
    { CA_ENAB_DESC_OK_TECH,
        "descrambling possible under conditions (technical dialogue)" },
    { CA_ENAB_DESC_NG_ENTITLEMENT,
        "descrambling not possible (because no entitlement)" },
    { CA_ENAB_DESC_NG_TECH,
        "descrambling not possible (for technical reasons)" },
    { 0, NULL }
};
static const value_string dvbci_hc_status[] = {
    { HC_STAT_OK, "ok" },
    { HC_STAT_ERR_DLVRY, "unsupported delivery system descriptor" },
    { HC_STAT_ERR_LOCK, "tuner not locking" },
    { HC_STAT_ERR_BUSY, "tuner busy" },
    { HC_STAT_ERR_PARAM, "bad or missing parameters" },
    { HC_STAT_ERR_NOT_FOUND, "service not found" },
    { HC_STAT_ERR_UNKNOWN, "unknown error" },
    { 0, NULL }
};
static const value_string dvbci_hc_release_reply[] = {
    { HC_RELEASE_OK, "Host regains control of the tuner" },
    { HC_RELEASE_REFUSED, "CICAM retains control of the tuner" },
    { 0, NULL }
};
static const value_string dvbci_close_mmi_cmd_id[] = {
    { CLOSE_MMI_CMD_ID_IMMEDIATE, "immediate close" },
    { CLOSE_MMI_CMD_ID_DELAY, "delayed close" },
    { 0, NULL }
};
static const value_string dvbci_disp_ctl_cmd[] = {
    { DISP_CMD_SET_MMI_MODE, "set MMI mode" },
    { DISP_CMD_GET_DISP_TBL, "get display character tables" },
    { DISP_CMD_GET_INP_TBL,  "get input character tables" },
    { 0, NULL }
};
static const value_string dvbci_mmi_mode[] = {
    { MMI_MODE_HIGH, "High-level MMI" },
    { 0, NULL }
};
static const value_string dvbci_disp_rep_id[] = {
    { DISP_REP_ID_MMI_MODE_ACK,     "MMI mode acknowledge" },
    { DISP_REP_ID_DISP_CHAR_TBL,    "list display character tables" },
    { DISP_REP_ID_INP_CHAR_TBL,     "list input character tables" },
    { DISP_REP_ID_UNKNOWN_CMD,      "unknown display control command" },
    { DISP_REP_ID_UNKNOWN_MMI_MODE, "unknown MMI mode" },
    { DISP_REP_ID_UNKNOWN_CHAR_TBL, "unknown character table" },
    { 0, NULL }
};
static const value_string dvbci_blind_ans[] = {
    { VISIBLE_ANS, "visible" },
    { BLIND_ANS,   "blind" },
    { 0, NULL }
};
static const value_string dvbci_ans_id[] = {
    { ANSW_ID_CANCEL, "cancel" },
    { ANSW_ID_ANSWER, "answer" },
    { 0, NULL }
};
static const value_string dvbci_cup_type[] = {
    { CUP_DELAYED, "delayed" },
    { CUP_IMMEDIATE, "immediate" },
    { 0, NULL }
};
static const value_string dvbci_cup_answer[] = {
    { CUP_ANS_NO,  "upgrade denied" },
    { CUP_ANS_YES, "upgrade allowed" },
    { CUP_ANS_ASK, "ask the user for permission" },
    { 0, NULL }
};
static const value_string dvbci_cup_reset[] = {
    { CUP_RESET_PCMCIA, "PCMCIA reset" },
    { CUP_RESET_CMDIF,  "CI command interface reset" },
    { CUP_RESET_NONE,   "no reset" },
    { 0, NULL }
};
static const value_string dvbci_cc_dat_id[] = {
    { CC_ID_HOST_ID,            "Host ID" },
    { CC_ID_CICAM_ID,           "Cicam ID" },
    { CC_ID_HOST_BRAND_CERT,    "Host brand certificate" },
    { CC_ID_CICAM_BRAND_CERT,   "Cicam brand certificate" },
    { CC_ID_KP,                 "Key precursor for CCK" },
    { CC_ID_DHPH,               "Host Diffie-Hellman public key" },
    { CC_ID_DHPM,               "Cicam Diffie-Hellman public key" },
    { CC_ID_HOST_DEV_CERT,      "Host device certificate" },
    { CC_ID_CICAM_DEV_CERT,     "Cicam device certificate" },
    { CC_ID_SIG_A,              "Signature of host Diffie-Hellman public key" },
    { CC_ID_SIG_B,              "Signature of cicam Diffie-Hellman public key" },
    { CC_ID_NS_HOST,            "Host nonce" },
    { CC_ID_AUTH_NONCE,         "Nonce for authentication" },
    { CC_ID_NS_MODULE,          "Cicam nonce" },
    { CC_ID_AKH,                "Host authentication key" },
    { CC_ID_URI,                "URI" },
    { CC_ID_PROG_NUM,           "Program number" },
    { CC_ID_URI_CNF,            "URI confirmation" },
    { CC_ID_KEY_REGISTER,       "Key register" },
    { CC_ID_URI_VERSIONS,       "Supported URI versions" },
    { CC_ID_STATUS_FIELD,       "Status field" },
    { CC_ID_SRM_DATA,           "SRM for HDCP" },
    { CC_ID_SRM_CONFIRM,        "SRM confirmation hash" },
    { CC_ID_CICAM_LICENSE,      "License received from the cicam" },
    { CC_ID_LICENSE_STATUS,     "Current status of the license" },
    { CC_ID_LICENSE_RCV_STATUS, "Status of the license exchange" },
    { CC_ID_HOST_LICENSE,
        "License for which the host requests the current status" },
    { CC_ID_PLAY_COUNT,         "Play count" },
    { CC_ID_OPERATING_MODE,     "Operating mode" },
    { CC_ID_PINCODE_DATA,       "Pincode data" },
    { CC_ID_REC_START_STATUS,   "Record start status" },
    { CC_ID_MODE_CHG_STATUS,    "Change operating mode status" },
    { CC_ID_REC_STOP_STATUS,    "Record stop status" },
    { 0, NULL }
};
static const value_string dvbci_cc_uri_emi[] = {
    { CC_EMI_FREE,    "Copy free" },
    { CC_EMI_NO_MORE, "Copy no more" },
    { CC_EMI_ONCE,    "Copy once" },
    { CC_EMI_NEVER,   "Copy never" },
    { 0, NULL }
};
static const value_string dvbci_cc_key_register[] = {
    { CC_KEY_EVEN,  "Even" },
    { CC_KEY_ODD,   "Odd" },
    { 0, NULL }
};
static const value_string dvbci_cc_status[] = {
    { CC_STATUS_OK,                    "Ok" },
    { CC_STATUS_NO_CC_SUPPORT,         "No CC support" },
    { CC_STATUS_HOST_BUSY,             "Host busy" },
    { CC_STATUS_AUTH_FAILED_OR_NO_SRM, "Authentication failed / SRM not required" },
    { CC_STATUS_CICAM_BUSY,            "CICAM busy" },
    { CC_STATUS_REC_MODE_ERR,          "Recording mode error" },
    { 0, NULL }
};
static const value_string dvbci_cc_sac_auth[] = {
    { CC_SAC_AUTH_AES128_XCBC_MAC, "AES 128 XCBC MAC" },
    { 0, NULL }
};
static const value_string dvbci_cc_sac_enc[] = {
    { CC_SAC_ENC_AES128_CBC, "AES 128 CBC" },
    { 0, NULL }
};
static const value_string dvbci_cc_cap[] = {
    { CC_CAP_NONE,
        "No PIN handling capability" },
    { CC_CAP_CAS_PIN,
        "CAM can do PIN handling on CAS services" },
    { CC_CAP_CAS_FTA_PIN,
        "CAM can do PIN handling on CAS and free services" },
    { CC_CAP_CAS_PIN_CACHED,
        "CAM can do PIN handling on CAS services and supports PIN caching" },
    { CC_CAP_CAS_FTA_PIN_CACHED,
        "CAM can do PIN handling on CAS and free services, supports PIN caching" },
    { 0, NULL }
};
static const value_string dvbci_pincode_status[] = {
    { CC_PIN_BAD,         "Bad pin code" },
    { CC_PIN_CAM_BUSY,    "CAM busy" },
    { CC_PIN_OK,          "Pin code correct" },
    { CC_PIN_UNCONFIRMED, "Pin code unconfirmed" },
    { CC_PIN_VB_NOT_REQ,  "Video blanking not required" },
    { CC_PIN_CSA,         "Content still CSA scrambled" },
    { 0, NULL }
};
static const value_string dvbci_cc_op_mode[] = {
    { CC_OP_MODE_WATCH_BUFFER, "Watch and buffer" },
    { CC_OP_MODE_TIMESHIFT,    "Timeshift" },
    { CC_OP_MODE_UNATTENDED,   "Unattended recording" },
    { 0, NULL }
};
static const value_string dvbci_ack_code[] = {
    { ACK_CODE_OK, "Ok" },
    { ACK_CODE_WRONG_API,  "Application Domain unsupported" },
    { ACK_CODE_API_BUSY,   "Application Domain currently unavailable" },
    { 0, NULL }
};
static const value_string dvbci_req_type[] = {
    { REQ_TYPE_FILE, "File" },
    { REQ_TYPE_DATA, "Data" },
    { REQ_TYPE_FILE_HASH, "FileHash" },
    { REQ_TYPE_REQ, "List supported request types" },
    { 0, NULL }
};
static const value_string dvbci_comms_cmd_id[] = {
    { COMMS_CMD_ID_CONNECT_ON_CHANNEL, "connect on channel" },
    { COMMS_CMD_ID_DISCONNECT_ON_CHANNEL, "disconnect on channel" },
    { COMMS_CMD_ID_SET_PARAMS, "set parameters" },
    { COMMS_CMD_ID_ENQUIRE_STATUS, "status enquiry" },
    { COMMS_CMD_ID_GET_NEXT_BUFFER, "get next buffer" },
    { 0, NULL }
};
static const value_string dvbci_conn_desc_type[] = {
    { CONN_DESC_TEL, "DVB-SI telephone descriptor" },
    { CONN_DESC_CABLE, "cable return channel" },
    { CONN_DESC_IP, "IP descriptor" },
    { CONN_DESC_HOSTNAME, "hostname descriptor" },
    { 0, NULL }
};
static const value_string dvbci_lsc_desc_tag[] = {
    { LSC_DESC_IP, "IP descriptor" },
    { LSC_DESC_HOSTNAME, "hostname descriptor" },
    { 0, NULL }
};
static const value_string dvbci_lsc_ip_ver[] = {
    { LSC_IPV4, "IPv4" },
    { LSC_IPV6, "IPv6" },
    { 0, NULL }
};
static const value_string dvbci_lsc_proto[] = {
    { LSC_TCP, "TCP" },
    { LSC_UDP, "UDP" },
    { 0, NULL }
};
static const value_string dvbci_comms_rep_id[] = {
    { COMMS_REP_ID_CONNECT_ACK, "connect ack" },
    { COMMS_REP_ID_DISCONNECT_ACK, "disconnect ack" },
    { COMMS_REP_ID_SET_PARAMS_ACK, "set parameters ack" },
    { COMMS_REP_ID_STATUS_REPLY, "status reply" },
    { COMMS_REP_ID_GET_NEXT_BUFFER_ACK, "get next buffer ack" },
    { COMMS_REP_ID_SEND_ACK, "send ack" },
    { 0, NULL }
};
static const value_string dvbci_lsc_ret_val[] = {
    { LSC_RET_OK, "ok" },
    { 0, NULL }
};
static const value_string dvbci_lsc_ret_val_connect[] = {
    { LSC_RET_DISCONNECTED, "disconnected" },
    { LSC_RET_CONNECTED, "connected" },
    { 0, NULL }
};
static const value_string dvbci_lsc_ret_val_params[] = {
    { LSC_RET_OK, "ok" },
    { LSC_RET_TOO_BIG, "buffer size too big" },
    { 0, NULL }
};
static const value_string dvbci_opp_ref_req_flag[] = {
    { OPP_REF_REG_FLG_NONE,  "none" },
    { OPP_REF_REG_FLG_ADV,   "advance warning" },
    { OPP_REF_REG_FLG_URG,   "urgent" },
    { OPP_REF_REG_FLG_SCHED, "scheduled" },
    { 0, NULL }
};
static const value_string dvbci_opp_err_flag[] = {
    { OPP_ERR_FLG_OK,          "no error" },
    { OPP_ERR_FLG_PROF,        "CICAM can't acquire the profile" },
    { OPP_ERR_FLG_UNSUPPORTED, "unsupported delivery system" },
    { OPP_ERR_FLG_CANCELLED,   "operator search was cancelled" },
    { 0, NULL }
};
static const value_string dvbci_opp_eit_pf_usage[] = {
    { OPP_EIT_ABSENT, "no EIT present" },
    { OPP_EIT_NOT_X,  "EIT is not fully ross-carried" },
    { OPP_EIT_FULL_X, "EIT is fully cross-carried" },
    { 0, NULL }
};
static const value_string dvbci_opp_eit_sch_usage[] = {
    { OPP_EIT_ABSENT, "no EIT present" },
    { OPP_EIT_NOT_X,  "EIT is not fully ross-carried" },
    { OPP_EIT_FULL_X, "EIT is fully cross-carried" },
    { OPP_EIT_BARKER, "EIT is available from a barker channel" },
    { OPP_EPG_APP,    "EPG is delivered using an application" },
    { 0, NULL }
};
static const value_string dvbci_opp_ext_evt[] = {
    { OPP_EXT_EVT_DIFF, "extended event info is different from short event" },
    { OPP_EXT_EVT_ADD,  "extended event info includes short event" },
    { 0, NULL }
};
static const value_string dvbci_opp_dlv_cap[] = {
    { OPP_DLV_CAP_S,  "DVB-S" },
    { OPP_DLV_CAP_C,  "DVB-C" },
    { OPP_DLV_CAP_T,  "DVB-T" },
    { OPP_DLV_CAP_S2, "DVB-S2" },
    { 0, NULL }
};
static const value_string dvbci_opp_tune_stat[] = {
    { OPP_TUNE_OK,          "success" },
    { OPP_TUNE_UNSUPPORTED, "unsupported delivery system descriptor" },
    { OPP_TUNE_INVALID,     "invalid delivery system descriptor" },
    { OPP_TUNE_ERR,         "failed" },
    { 0, NULL }
};
static const value_string dvbci_sas_sess_state[] = {
    { SAS_SESS_STATE_CONNECTED, "connected" },
    { SAS_SESS_STATE_NOT_FOUND, "application not found" },
    { SAS_SESS_STATE_DENIED, "denied, no more connections available" },
    { 0, NULL }
};


static guint16 buf_size_cam;    /* buffer size proposal by the CAM */
/* buffer size proposal by the host == negotiated buffer size */
static guint16 buf_size_host;


gint
dvbci_set_addrs(guint8 event, packet_info *pinfo)
{
    if (!IS_DATA_TRANSFER(event))
        return -1;

    if (event == DVBCI_EVT_DATA_HOST_TO_CAM) {
        set_address(&pinfo->src, AT_STRINGZ,
                (int)strlen(ADDR_HOST)+1, ADDR_HOST);
        set_address(&pinfo->dst, AT_STRINGZ,
                (int)strlen(ADDR_CAM)+1 , ADDR_CAM);
    }
    else {
        set_address(&pinfo->src, AT_STRINGZ,
                (int)strlen(ADDR_CAM)+1 , ADDR_CAM);
        set_address(&pinfo->dst, AT_STRINGZ,
                (int)strlen(ADDR_HOST)+1, ADDR_HOST);
    }

    return 1;
}

static guint8
dvbci_get_evt_from_addrs(packet_info *pinfo)
{
    /* this should be working from C89 on */
    static const address a_cam  = ADDRESS_INIT(AT_STRINGZ, sizeof(ADDR_CAM), ADDR_CAM);
    static const address a_host = ADDRESS_INIT(AT_STRINGZ, sizeof(ADDR_HOST), ADDR_HOST);

    if ( addresses_equal(&(pinfo->src), &a_cam) &&
         addresses_equal(&(pinfo->dst), &a_host) ) {
        return DVBCI_EVT_DATA_CAM_TO_HOST;
    }
    else if ( addresses_equal(&(pinfo->src), &a_host) &&
              addresses_equal(&(pinfo->dst), &a_cam) ) {
        return DVBCI_EVT_DATA_HOST_TO_CAM;
    }
    else
        return DVBCI_EVT_INVALID_EVT;
}


/* this must be a function, not a macro,
   so that we can enforce the return type */
static inline gint16 two_comp_to_int16(guint16 x)
{
   return (x&0x8000) ? -~(x-1) : x;
}


/* initialize/reset per capture state data */
static void
dvbci_init(void)
{
    buf_size_cam  = 0;
    buf_size_host = 0;
}


/* dissect a delivery system descriptor loop
   and the preceding length field
   (used for host control and operator profile)
   return the number of bytes dissected */
static gint
dissect_desc_loop(int len_hf,
        tvbuff_t *tvb, gint offset, packet_info *pinfo _U_, proto_tree *tree)
{
    gint offset_start;
    guint16 desc_loop_len;
    guint desc_len;

    offset_start = offset;

    desc_loop_len = tvb_get_ntohs(tvb, offset) & 0x0FFF;
    proto_tree_add_item(tree, len_hf, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;
    while (offset-offset_start < 2+desc_loop_len) {
        desc_len = proto_mpeg_descriptor_dissect(tvb, offset, tree);
        if (desc_len==0)
            break;
        offset += desc_len;
    }

    return offset-offset_start;
}


/* dissect operator profile's status body, return its length */
static gint
dissect_opp_status_body(tvbuff_t *tvb, gint offset,
        packet_info *pinfo _U_, proto_tree *tree)
{
    gint offset_start;

    offset_start = offset;
    proto_tree_add_item(tree, hf_dvbci_info_ver_op_status,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_nit_ver,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(tree, hf_dvbci_pro_typ,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_init_flag,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_ent_chg_flag,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_ent_val_flag,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_ref_req_flag,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(tree, hf_dvbci_err_flag,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_bitmask(tree, tvb, offset,
            hf_dvbci_dlv_sys_hint, ett_dvbci_dlv_sys_hint,
            dvbci_opp_dlv_sys_hint_fields, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(tree, hf_dvbci_refr_req_date,
            tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;
    proto_tree_add_item(tree, hf_dvbci_refr_req_time,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;

    return offset-offset_start;
}


/* dissect a capability loop in an operator_search_start apdu */
static gint
dissect_opp_cap_loop(guint8 cap_loop_len, const gchar *title,
        int item_hf, guint item_len,
        tvbuff_t *tvb, gint offset,
        packet_info *pinfo _U_, proto_tree *tree)
{
    proto_tree *loop_tree = NULL;
    guint       i;

    if (!title)
        return -1;
    if (item_len==0)
        return -1;

    if (tree && cap_loop_len>0) {
        loop_tree = proto_tree_add_subtree(tree, tvb, offset, cap_loop_len, ett_dvbci_opp_cap_loop, NULL, title);
    }
    for (i=0; i<item_len*cap_loop_len; i+=item_len) {
        proto_tree_add_item(loop_tree, item_hf,
                tvb, offset+i, item_len, ENC_BIG_ENDIAN);
    }

    return cap_loop_len;
}


/* dissect age rating byte encoded as defined in
   DVB-SI parental rating descriptor
   returns rating's length in bytes or -1 for error */
static gint
dissect_rating(tvbuff_t *tvb, gint offset,
        packet_info *pinfo _U_, proto_tree *tree)
{
    guint8 rating;

    rating = tvb_get_guint8(tvb, offset);
    if (1<=rating && rating<=0x0F) {
        proto_tree_add_uint_format(tree, hf_dvbci_rating,
                tvb, offset, 1, rating,
                "Rating is %d years (%d+3)", rating+3, rating);
    } else {
        proto_tree_add_uint_format(tree, hf_dvbci_rating,
                tvb, offset, 1, rating,
                "Rating is unknown/undefined (%d)", rating);
    }

    return 1;
}


/* if there's a dissector for the protocol and target port of our
    lsc connection, store it in the lsc session's circuit */
static void
store_lsc_msg_dissector(circuit_t *circuit, guint8 ip_proto, guint16 port)
{
    dissector_handle_t msg_handle = NULL;

    if (!circuit)
        return;

    if (ip_proto==LSC_TCP)
        msg_handle = dissector_get_uint_handle(tcp_dissector_table, port);
    else if (ip_proto==LSC_UDP)
        msg_handle = dissector_get_uint_handle(udp_dissector_table, port);

    circuit_set_dissector(circuit, msg_handle);
}


/* dissect a connection_descriptor for the lsc resource
   returns its length or -1 for error */
static gint
dissect_conn_desc(tvbuff_t *tvb, gint offset,  circuit_t *circuit,
        packet_info *pinfo, proto_tree *tree)
{
    proto_item *ti;
    proto_tree *conn_desc_tree = NULL;
    guint32     tag;
    gint        offset_start, offset_body;
    gint        len_field;
    guint8      conn_desc_type;
    guint8      ip_ver, ip_proto;
    guint16     port;
    proto_item *port_item      = NULL;
    gint        hostname_len;

    offset_start = offset;

    tag = tvb_get_ntoh24(tvb, offset);
    if (tag!= T_CONNECTION_DESCRIPTOR)
        return 0;

    conn_desc_tree = proto_tree_add_subtree(tree, tvb,
            offset_start, -1, ett_dvbci_lsc_conn_desc, &ti, "Connection descriptor");

    proto_tree_add_item(conn_desc_tree, hf_dvbci_apdu_tag,
            tvb, offset, APDU_TAG_SIZE, ENC_BIG_ENDIAN);
    offset += APDU_TAG_SIZE;
    offset = dissect_ber_length(pinfo, conn_desc_tree,
                    tvb, offset, &len_field, NULL);
    offset_body = offset;

    conn_desc_type = tvb_get_guint8(tvb, offset);
    proto_tree_add_item(conn_desc_tree, hf_dvbci_conn_desc_type,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;

    if (conn_desc_type == CONN_DESC_IP) {
        proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_media_tag,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_media_len,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        ip_ver = tvb_get_guint8(tvb, offset);
        proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_ip_ver,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;

        if (ip_ver == LSC_IPV4) {
            offset += FT_IPv6_LEN-FT_IPv4_LEN;
            proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_ipv4_addr,
                    tvb, offset, FT_IPv4_LEN, ENC_BIG_ENDIAN);
            offset += FT_IPv4_LEN;
        }
        else if (ip_ver == LSC_IPV6) {
            proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_ipv6_addr,
                    tvb, offset, FT_IPv6_LEN, ENC_NA);
            offset += FT_IPv6_LEN;
        }
        else
            offset += FT_IPv6_LEN;

        port = tvb_get_ntohs(tvb, offset);
        port_item = proto_tree_add_item(conn_desc_tree,
                hf_dvbci_lsc_dst_port, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset +=2;
        ip_proto = tvb_get_guint8(tvb, offset);
        proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_proto,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
        offset ++;
        if (port_item) {
            if (ip_proto==LSC_TCP && tcp_port_to_display(wmem_packet_scope(), port)) {
                proto_item_append_text(port_item, " (%s)",
                        tcp_port_to_display(wmem_packet_scope(), port));
            }
            else if (ip_proto==LSC_UDP && udp_port_to_display(wmem_packet_scope(), port)) {
                proto_item_append_text(port_item, " (%s)",
                        udp_port_to_display(wmem_packet_scope(), port));
            }
        }
        store_lsc_msg_dissector(circuit, ip_proto, port);

    } else if (conn_desc_type == CONN_DESC_HOSTNAME) {
        proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_media_tag,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_media_len,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        ip_proto = tvb_get_guint8(tvb, offset);
        proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_proto,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
        offset ++;
        port = tvb_get_ntohs(tvb, offset);
        port_item = proto_tree_add_item(conn_desc_tree,
                hf_dvbci_lsc_dst_port, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset +=2;
        if (port_item) {
            if (ip_proto==LSC_TCP && tcp_port_to_display(wmem_packet_scope(), port)) {
                proto_item_append_text(port_item, " (%s)",
                        tcp_port_to_display(wmem_packet_scope(), port));
            }
            else if (ip_proto==LSC_UDP && udp_port_to_display(wmem_packet_scope(), port)) {
                proto_item_append_text(port_item, " (%s)",
                        udp_port_to_display(wmem_packet_scope(), port));
            }
        }
        store_lsc_msg_dissector(circuit, ip_proto, port);

        /* everything from here to the descriptor's end is a hostname */
        hostname_len = (offset_body+len_field)-offset;
        proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_hostname,
                tvb, offset, hostname_len, ENC_ASCII|ENC_NA);
        offset += hostname_len;
    } else {
        proto_tree_add_item(conn_desc_tree, hf_dvbci_lsc_media_data,
                tvb, offset, len_field-1, ENC_NA);
        offset += len_field-1;
    }

    proto_item_set_len(ti, offset-offset_start);
    return offset-offset_start;
}


/* check if the given CC resource item may appear in the clear
   as part of an exported PDU */
static inline gboolean
is_cc_item_exportable(guint8 dat_id)
{
    /* the CCK precursor value does not contain sensitive information as such
       nevertheless, it is safer to prevent people from exporting this value
       accidentially */
    if (dat_id == CC_ID_KP)
        return FALSE;
    /* we could add some more items here which do not appear in SAC messages
       normally: CC_ID_DHPH, CC_ID_DHPM */

    return TRUE;
}


/* dissect the URI, return the number of bytes processed or -1 for error */
static gint
dissect_uri(tvbuff_t *tvb, gint offset, packet_info *pinfo, proto_tree *tree)
{
    gint        offset_start;
    guint8      uri_ver, emi, rl;
    proto_item *rl_item;

    offset_start = offset;

    col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "URI");

    uri_ver = tvb_get_guint8(tvb, offset);
    proto_tree_add_item(tree, hf_dvbci_uri_ver,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;

    proto_tree_add_item(tree, hf_dvbci_uri_aps,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    emi = (tvb_get_guint8(tvb, offset) & 0x30) >> 4;
    proto_tree_add_item(tree, hf_dvbci_uri_emi,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
            val_to_str_const(emi, dvbci_cc_uri_emi, "unknown"));
    proto_tree_add_item(tree, hf_dvbci_uri_ict,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    if (emi==CC_EMI_FREE) {
        proto_tree_add_item(tree, hf_dvbci_uri_rct,
                tvb, offset, 1, ENC_BIG_ENDIAN);
    }
    if (uri_ver>=2 && emi==CC_EMI_NEVER) {
        proto_tree_add_item(tree, hf_dvbci_uri_dot,
                tvb, offset, 1, ENC_BIG_ENDIAN);
    }
    offset++;

    if (emi==CC_EMI_NEVER) {
        if (uri_ver==1)
            rl = tvb_get_guint8(tvb, offset) & 0x3F;
        else
            rl = tvb_get_guint8(tvb, offset);

        rl_item = proto_tree_add_uint(tree, hf_dvbci_uri_rl,
                tvb, offset, 1, rl);

        if (rl==0x00)
            proto_item_append_text(rl_item, " (90 minutes)");
        else if (rl==0x01)
            proto_item_append_text(rl_item, " (6 hours)");
        else if (rl==0x02)
            proto_item_append_text(rl_item, " (12 hours)");
        else if (uri_ver>=2 && rl==0xFF)
            proto_item_append_text(rl_item, " (unlimited)");
        else {
            /* no need for a range check, rl 0x3F mask was applied above */
            proto_item_append_text(rl_item, " (%d days)", rl-2);
        }
    }

    return offset-offset_start;
}


/* dissect an item from cc_(sac_)data_req/cc_(sac_)data_cnf,
   returns its length or -1 for error
   if dat_id_ptr is not NULL, fill in the datatype id */
static gint
dissect_cc_item(tvbuff_t *tvb, gint offset,
        packet_info *pinfo, proto_tree *tree, guint8 *dat_id_ptr)
{
    proto_item *ti;
    proto_tree *cc_item_tree = NULL;
    gint        offset_start;
    guint16     dat_len;
    guint8      dat_id;
    asn1_ctx_t  asn1_ctx;
    int         hf_cert_index;
    guint16     prog_num;
    guint8      status;


    offset_start = offset;
    dat_id = tvb_get_guint8(tvb, offset);
    if (dat_id_ptr)
        *dat_id_ptr = dat_id;

    cc_item_tree = proto_tree_add_subtree_format(tree, tvb, offset_start, -1, ett_dvbci_cc_item, &ti,
        "CC data item: %s", val_to_str_const(dat_id, dvbci_cc_dat_id, "unknown"));

    proto_tree_add_item(cc_item_tree, hf_dvbci_cc_dat_id,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    dat_len = tvb_get_ntohs(tvb, offset);
    proto_tree_add_item(cc_item_tree, hf_dvbci_cc_dat_len,
            tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;
    switch (dat_id) {
        case CC_ID_HOST_BRAND_CERT:
        case CC_ID_CICAM_BRAND_CERT:
        case CC_ID_HOST_DEV_CERT:
        case CC_ID_CICAM_DEV_CERT:
            asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
            hf_cert_index = (dat_id==CC_ID_HOST_BRAND_CERT ||
                             dat_id==CC_ID_CICAM_BRAND_CERT) ?
                hf_dvbci_brand_cert : hf_dvbci_dev_cert;

            /* enable dissection of CI+ specific X.509 extensions
               only for our certificates */
            x509ce_enable_ciplus();
            dissect_x509af_Certificate(FALSE, tvb, offset,
                    &asn1_ctx, cc_item_tree, hf_cert_index);
            x509ce_disable_ciplus();
            break;
        case CC_ID_URI:
            dissect_uri(tvb, offset, pinfo, cc_item_tree);
            break;
        case CC_ID_PROG_NUM:
            prog_num = tvb_get_ntohs(tvb, offset);
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL,
                    "Program number 0x%x", prog_num);
            proto_tree_add_item(cc_item_tree, hf_dvbci_cc_prog_num,
                    tvb, offset, 2, ENC_BIG_ENDIAN);
            break;
        case CC_ID_KEY_REGISTER:
            proto_tree_add_item(cc_item_tree, hf_dvbci_cc_key_register,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            break;
        case CC_ID_STATUS_FIELD:
        case CC_ID_REC_START_STATUS:
        case CC_ID_MODE_CHG_STATUS:
        case CC_ID_REC_STOP_STATUS:
            status = tvb_get_guint8(tvb, offset);
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "Status: %s",
                    val_to_str_const(status, dvbci_cc_status, "unknown"));
            proto_tree_add_item(cc_item_tree, hf_dvbci_cc_status_field,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            break;
        case CC_ID_OPERATING_MODE:
            proto_tree_add_item(cc_item_tree, hf_dvbci_cc_op_mode,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            break;
        default:
            proto_tree_add_item(cc_item_tree, hf_dvbci_cc_data,
                    tvb, offset, dat_len, ENC_NA);
            break;
    }
    offset += dat_len;

    proto_item_set_len(ti, offset-offset_start);
    return offset-offset_start;
}


/* add the CC protocol name and step to the info column */
static void
add_cc_protocol_name_step(packet_info *pinfo,
    guint64 snd_dat_ids, guint64 req_dat_ids)
{
    gboolean chk_snd_ids = FALSE;

    switch (req_dat_ids) {
        case CC_ID_DHPH<<24|CC_ID_SIG_A<<16|CC_ID_HOST_DEV_CERT<<8|CC_ID_HOST_BRAND_CERT:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Authentication Step 1)");
            break;
        case CC_ID_STATUS_FIELD:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Authentication Step 3)");
            break;
        case CC_ID_AKH:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(AuthKey Step 1)");
            break;
        case CC_ID_HOST_ID<<8|CC_ID_STATUS_FIELD:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(CC key calculation Step 1)");
            break;
        case CC_ID_HOST_ID<<8|CC_ID_NS_HOST:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(SAC key calculation Step 1)");
            break;
        case CC_ID_URI_CNF:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(URI transmission Step 1)");
            break;
        case CC_ID_URI_VERSIONS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(URI version negotiation Step 1)");
            break;
        case CC_ID_LICENSE_RCV_STATUS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(CICAM to Host License Exchange Step 1)");
            break;
        case CC_ID_PROG_NUM<<24|CC_ID_LICENSE_STATUS<<16|CC_ID_URI<<8|CC_ID_CICAM_LICENSE:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Playback License Exchange Step 1)");
            break;
        case CC_ID_LICENSE_STATUS<<8|CC_ID_PLAY_COUNT:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(License Check Exchange Step 1)");
            break;
        case CC_ID_REC_START_STATUS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Record Start Step 1)");
            break;
        case CC_ID_MODE_CHG_STATUS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Change Operating Mode Step 1)");
            break;
        case CC_ID_REC_STOP_STATUS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Record Stop Step 1)");
            break;
        case CC_ID_STATUS_FIELD<<8|CC_ID_SRM_CONFIRM:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(SRM Transmission Step 1)");
            break;
        default:
            /* unable to determine the protocol from the requested ids
               check the sent ids as well */
            chk_snd_ids = TRUE;
            break;
    }
    if (!chk_snd_ids)
        return;

    switch (snd_dat_ids) {
        case CC_ID_DHPH<<24|CC_ID_SIG_A<<16|CC_ID_HOST_DEV_CERT<<8|CC_ID_HOST_BRAND_CERT:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Authentication Step 2)");
            break;
        case CC_ID_STATUS_FIELD:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Authentication Step 4)");
            break;
        case CC_ID_AKH:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(AuthKey Step 2)");
            break;
        case CC_ID_HOST_ID<<8|CC_ID_STATUS_FIELD:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(CC key calculation Step 2)");
            break;
        case CC_ID_HOST_ID<<8|CC_ID_NS_HOST:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(SAC key calculation Step 2)");
            break;
        case CC_ID_URI_CNF:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(URI transmission Step 2)");
            break;
        case CC_ID_URI_VERSIONS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(URI version negotiation Step 2)");
            break;
        case CC_ID_LICENSE_RCV_STATUS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(CICAM to Host License Exchange Step 2)");
            break;
        case CC_ID_PROG_NUM<<24|CC_ID_LICENSE_STATUS<<16|CC_ID_URI<<8|CC_ID_CICAM_LICENSE:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Playback License Exchange Step 2)");
            break;
        case CC_ID_LICENSE_STATUS<<8|CC_ID_PLAY_COUNT:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(License Check Exchange Step 2)");
            break;
        case CC_ID_REC_START_STATUS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Record Start Step 2)");
            break;
        case CC_ID_MODE_CHG_STATUS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Change Operating Mode Step 2)");
            break;
        case CC_ID_REC_STOP_STATUS:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(Record Stop Step 2)");
            break;
        case CC_ID_STATUS_FIELD<<8|CC_ID_SRM_CONFIRM:
            col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                    "(SRM Transmission Step 2)");
            break;
    }
}


/* dissect the payload of a cc message that contains data items
   if not NULL, set exportable_flag to TRUE if the message contains no
    sensitive data and can be passed to the export PDU mechanism */
static gint
dissect_cc_data_payload(guint32 tag, tvbuff_t *tvb, gint offset,
        packet_info *pinfo, proto_tree *tree, gboolean *exportable_flag)
{
    gint        offset_start;
    guint8      i, snd_dat_nbr, req_dat_nbr;
    guint8      dat_id;
    gint        item_len;
    /* the last 8 sent/requested datatype ids */
    guint64     snd_dat_ids=0, req_dat_ids=0;

    /* we only export cc_sac_data_req and cc_sac_data_cnf
       the only meta info in the exported PDU is the data transfer
        direction, if we only ever export  cc_sac_data_req and
        cc_sac_data_cnf, this info is enough to recover the apdu tag from the
        direction
       cc_sac_sync req and cc_sac_sync_cnf contain no interesting data */
    if (exportable_flag)
        *exportable_flag = (tag==T_CC_SAC_DATA_REQ || tag==T_CC_SAC_DATA_CNF);

    offset_start = offset;

    proto_tree_add_item(
            tree, hf_dvbci_cc_sys_id_bitmask, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    snd_dat_nbr = tvb_get_guint8(tvb, offset);
    proto_tree_add_item(
            tree, hf_dvbci_cc_snd_dat_nbr, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    for(i=0; i<snd_dat_nbr &&
            tvb_reported_length_remaining(tvb, offset)>0; i++) {
        item_len = dissect_cc_item(tvb, offset, pinfo, tree, &dat_id);
        if (item_len < 0)
            return -1;
        offset += item_len;
        /* for more than 8 sent datatype ids, some ids might get lost by
         * the shift, that's ok, we're only using the last 8 ids for
         * protocol detection */
        snd_dat_ids = snd_dat_ids<<8|dat_id;
        if (!exportable_flag || *exportable_flag==FALSE)
            continue;
        if (!is_cc_item_exportable(dat_id))
            *exportable_flag = FALSE;
    }

    if (tag==T_CC_DATA_REQ || tag==T_CC_SAC_DATA_REQ) {
        req_dat_nbr = tvb_get_guint8(tvb, offset);
        proto_tree_add_item(
                tree, hf_dvbci_cc_req_dat_nbr, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        for(i=0; i<req_dat_nbr &&
                tvb_reported_length_remaining(tvb, offset)>0; i++) {
            dat_id = tvb_get_guint8(tvb, offset);
            req_dat_ids = req_dat_ids<<8|dat_id;
            proto_tree_add_item(
                    tree, hf_dvbci_cc_dat_id, tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
        }
    }

    add_cc_protocol_name_step(pinfo, snd_dat_ids, req_dat_ids);

    return offset-offset_start;
}


/* convert a 0-terminated preference key_string that contains a hex number
 *  into its binary representation
 * e.g. key_string "abcd" will be converted into two bytes 0xab, 0xcd
 * return the number of binary bytes or -1 for error */
static gint
pref_key_string_to_bin(const gchar *key_string, unsigned char **key_bin)
{
    int  key_string_len;
    int  i, j;
    char input[3];

    if (!key_string || !key_bin)
        return -1;
    key_string_len = (int)strlen(key_string);
    if (key_string_len != 2*AES_KEY_LEN)
        return -1;
    *key_bin = (unsigned char*)g_malloc(key_string_len/2);
    input[2] = '\0';

    j=0;
    for (i=0; i<key_string_len-1; i+=2) {
        input[0] = key_string[0+i];
        input[1] = key_string[1+i];
        /* attention, brackets are required */
        (*key_bin)[j++] = (unsigned char)strtoul((const char*)&input, NULL, 16);
    }

    return key_string_len/2;
}


static tvbuff_t *
decrypt_sac_msg_body(packet_info *pinfo,
        guint8 enc_cip, tvbuff_t *encrypted_tvb, gint offset, gint len)
{
    gboolean         opened = FALSE;
    gcry_cipher_hd_t cipher;
    gcry_error_t     err;
    gint             clear_len;
    unsigned char    *clear_data = NULL;
    tvbuff_t         *clear_tvb = NULL;

    if (enc_cip != CC_SAC_ENC_AES128_CBC)
        goto end;
    if (len%AES_BLOCK_LEN != 0)
        goto end;

    if (!dvbci_sek_bin || !dvbci_siv_bin)
        goto end;

    err = gcry_cipher_open(&cipher, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC, 0);
    if (gcry_err_code (err))
        goto end;
    opened = TRUE;
    err = gcry_cipher_setkey (cipher, dvbci_sek_bin, AES_KEY_LEN);
    if (gcry_err_code (err))
        goto end;
    err = gcry_cipher_setiv (cipher, dvbci_siv_bin, AES_BLOCK_LEN);
    if (gcry_err_code (err))
        goto end;

    clear_len = len;
    clear_data = (unsigned char *)wmem_alloc(pinfo->pool, clear_len);

    err = gcry_cipher_decrypt (cipher, clear_data, clear_len,
            tvb_memdup(wmem_packet_scope(), encrypted_tvb, offset, len), len);
    if (gcry_err_code (err))
        goto end;

    clear_tvb = tvb_new_child_real_data(encrypted_tvb,
                        (const guint8 *)clear_data, clear_len, clear_len);

end:
    if (opened)
        gcry_cipher_close (cipher);
    return clear_tvb;
}


/* dissect a text string that is encoded according to DVB-SI (EN 300 468) */
static void
dissect_si_string(tvbuff_t *tvb, gint offset, gint str_len,
        packet_info *pinfo, proto_tree *tree, int hf, const gchar *title,
        gboolean show_col_info)
{
    guint           enc_len;
    dvb_encoding_e  encoding;
    guint8         *si_str = NULL;

    if (!title)  /* we always have a title for our strings */
        return;
    /* str_len==-1 is not supported, we need an actual length */
    if (str_len<=0)
        return;

    enc_len = dvb_analyze_string_charset(tvb, offset, str_len, &encoding);
    dvb_add_chartbl(tree, hf_dvbci_mmi_char_tbl, tvb, offset, enc_len, encoding);

    offset += enc_len;
    str_len -= enc_len;

    si_str = tvb_get_string_enc(wmem_packet_scope(),
            tvb, offset, str_len, dvb_enc_to_item_enc(encoding));
    if (!si_str)
        return;

    proto_tree_add_string_format(tree, hf,
            tvb, offset, str_len, si_str, "%s: %s", title, si_str);

    if (show_col_info)
        col_append_sep_str(pinfo->cinfo, COL_INFO, " ", si_str);
}


/* dissect ca_enable_flag and ca_enable fields in the ca_pmt_reply
 * return true if descrambling is possible, false otherwise */
static gboolean
dissect_ca_enable(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
        proto_tree *tree)
{
    gboolean desc_ok = FALSE;
    guint8   byte, ca_enab;

    byte = tvb_get_guint8(tvb,offset);
    proto_tree_add_item(tree, hf_dvbci_ca_enable_flag, tvb, offset, 1, ENC_BIG_ENDIAN);
    if (byte&0x80) {
        ca_enab = byte & ~0x80;
        proto_tree_add_item(tree, hf_dvbci_ca_enable, tvb, offset, 1, ENC_BIG_ENDIAN);
        if (ca_enab==CA_ENAB_DESC_OK ||
            ca_enab==CA_ENAB_DESC_OK_PURCHASE ||
            ca_enab==CA_ENAB_DESC_OK_TECH) {
            desc_ok = TRUE;
        }
    }

    return desc_ok;
}


/* dissect a ca descriptor in the ca_pmt */
static gint
dissect_ca_desc(tvbuff_t *tvb, gint offset, packet_info *pinfo,
        proto_tree *tree)
{
    gint        offset_start;
    guint8      tag, len_byte;
    proto_item *ti;
    proto_tree *ca_desc_tree = NULL;

    offset_start = offset;

    tag = tvb_get_guint8(tvb,offset);
    if (tag != CA_DESC_TAG) {
        /* we could skip unknown descriptors and make this a warning */
        proto_tree_add_expert(tree, pinfo, &ei_dvbci_ca_pmt_cmd_id, tvb, offset, 1);
        return 0;
    }

    ca_desc_tree = proto_tree_add_subtree(
            tree, tvb, offset_start, -1, ett_dvbci_ca_desc, &ti, "Conditional Access descriptor");
    offset++;

    len_byte = tvb_get_guint8(tvb,offset);
    proto_tree_add_item(
            ca_desc_tree, hf_dvbci_descr_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;

    proto_tree_add_item(
            ca_desc_tree, hf_dvbci_ca_sys_id, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    proto_tree_add_item(
            ca_desc_tree, hf_dvbci_ca_pid, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    if ((len_byte-4) != 0) {
        proto_tree_add_item(ca_desc_tree, hf_dvbci_ca_priv_data,
                tvb, offset, len_byte-4, ENC_NA);
        offset += (len_byte-4);
    }

    proto_item_set_len(ti, offset-offset_start);
    return offset-offset_start;
}


/* dissect an elementary stream entry in the ca_pmt */
static gint
dissect_es(tvbuff_t *tvb, gint offset,
        packet_info *pinfo, proto_tree *tree, gboolean *scrambled)
{
    proto_item *ti;
    proto_tree *es_tree = NULL;
    gint        offset_start, ca_desc_len;
    gint        es_info_len, all_len;

    offset_start = offset;

    if (scrambled) {
        /* default to free service until we found a ca descriptor
           (we could have es info len > 0 and no ca descriptors) */
        *scrambled = FALSE;
    }

    es_tree = proto_tree_add_subtree(tree, tvb, offset_start, -1, ett_dvbci_application, &ti, "Elementary Stream");

    proto_tree_add_item(
            es_tree, hf_dvbci_stream_type, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(
            es_tree, hf_dvbci_es_pid, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;
    es_info_len = tvb_get_ntohs(tvb, offset) & 0x0FFF;
    /* the definition of hf_dvbci_es_info_len also applies the mask */
    proto_tree_add_item(
            es_tree, hf_dvbci_es_info_len, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;
    if (es_info_len>0) {
        all_len = offset + es_info_len;

        proto_tree_add_item(
                es_tree, hf_dvbci_ca_pmt_cmd_id, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        while (offset < all_len) {
            ca_desc_len = dissect_ca_desc(tvb, offset, pinfo, es_tree);
            if (ca_desc_len <= 0)
                return -1;
            offset += ca_desc_len;
            if (scrambled)
                *scrambled = TRUE;
        }
    }
    else {
        proto_tree_add_expert(
                es_tree, pinfo, &ei_dvbci_no_ca_desc_es, tvb, 0, 0);
    }

    proto_item_set_len(ti, offset-offset_start);
    return offset-offset_start;
}

/* dissect a text pseudo-apdu */
static gint
dissect_dvbci_text(const gchar *title, tvbuff_t *tvb, gint offset,
                   packet_info *pinfo, proto_tree *tree, int hf)
{
    proto_item *ti;
    proto_tree *text_tree;
    guint32     tag;
    gint        offset_start;
    gint        len_field;

    offset_start = offset;

    if (!title)
        return 0;

    /* check the tag before setting up the tree */
    tag = tvb_get_ntoh24(tvb, offset);
    if (tag!=T_TEXT_LAST && tag!=T_TEXT_MORE)
        return 0;

    text_tree = proto_tree_add_subtree(tree, tvb, offset_start, -1, ett_dvbci_text, &ti, title);

    proto_tree_add_item(text_tree, hf_dvbci_apdu_tag,
            tvb, offset, APDU_TAG_SIZE, ENC_BIG_ENDIAN);
    offset += APDU_TAG_SIZE;
    offset = dissect_ber_length(pinfo, text_tree, tvb, offset, &len_field, NULL);
    dissect_si_string(tvb, offset, len_field, pinfo, text_tree,
            hf, "Text", FALSE);
    offset += len_field;

    proto_item_set_len(ti, offset-offset_start);
    return (offset-offset_start);
}


static proto_item *
dissect_res_id(tvbuff_t *tvb, gint offset, packet_info *pinfo,
        proto_tree *tree, guint32 res_id, gboolean show_col_info)
{
    /* there's two possible inputs for this function
        the resource id is either in a tvbuff_t (tvb!=NULL, res_id==0)
        or in a guint32 (tvb==NULL, res_id!=0) */

    if (tvb) {
        /* resource id comes in via tvbuff */
        if (res_id!=0)
            return NULL;
        res_id = tvb_get_ntohl(tvb, offset);
    }
    else {
        /* resource id comes in via guint32 */
        if (res_id==0)
            return NULL;
        /* we'll call proto_tree_add_...( tvb==NULL, offset==0, length==0 )
           this creates a filterable item without any reference to a tvb */
        offset = 0;
    }

    if (show_col_info) {
        col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL, "%s Version %d",
                val_to_str_const(RES_CLASS(res_id), dvbci_res_class,
                    "Invalid Resource class"),
                RES_VER(res_id));
    }

    return proto_tree_add_bitmask_value_with_flags(tree, tvb, offset,
            hf_dvbci_res_id, ett_dvbci_res, dvb_ci_res_id_fields, res_id,
            BMT_NO_APPEND);
}

/* dissect the body of a resource manager apdu */
static void
dissect_dvbci_payload_rm(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
    const gchar *tag_str;

    if (tag==T_PROFILE) {
        if (len_field % RES_ID_LEN) {
            tag_str = val_to_str(tag, dvbci_apdu_tag, "Unknown: %d");
            proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb, 0, APDU_TAG_SIZE,
                   "Invalid APDU length field, %s must be a multiple of 4 bytes",
                   tag_str);
            return;
        }

        while (tvb_reported_length_remaining(tvb, offset) > 0) {
            dissect_res_id(tvb, offset, pinfo, tree, 0, FALSE);
            offset += RES_ID_LEN;
        }
    }
}

static void
dissect_dvbci_payload_ap(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
    guint8          menu_str_len;
    guint           enc_len;
    dvb_encoding_e  encoding;
    const guint8   *menu_string;
    guint8          data_rate;

    if (tag==T_APP_INFO) {
        proto_tree_add_item(tree, hf_dvbci_app_type, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        proto_tree_add_item(
                tree, hf_dvbci_app_manf, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset+=2;
        proto_tree_add_item(
                tree, hf_dvbci_manf_code, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset+=2;
        menu_str_len = tvb_get_guint8(tvb,offset);
        proto_tree_add_item(
                tree, hf_dvbci_menu_str_len, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        if (menu_str_len > 0) {
            enc_len = dvb_analyze_string_charset(
                    tvb, offset, menu_str_len, &encoding);
            dvb_add_chartbl(
                    tree, hf_dvbci_ap_char_tbl, tvb, offset, enc_len, encoding);
            offset += enc_len;
            menu_str_len -= enc_len;
            proto_tree_add_item_ret_string(tree, hf_dvbci_menu_str,
                    tvb, offset, menu_str_len, dvb_enc_to_item_enc(encoding),
                    wmem_packet_scope(), &menu_string);
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL,
                    "Module name %s", menu_string);
        }
    }
    else if (tag== T_DATARATE_INFO) {
        data_rate = tvb_get_guint8(tvb, offset);
        col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                    val_to_str(data_rate, dvbci_data_rate, "unknown (0x%x)"));
        proto_tree_add_item(tree, hf_dvbci_data_rate, tvb, offset, 1, ENC_BIG_ENDIAN);
    }
}

static void
dissect_dvbci_payload_ca(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
    const gchar *tag_str;
    guint16      prog_num;
    guint        prog_info_len;
    gint         es_info_len, all_len;
    gint         ca_desc_len;
    gboolean     scrambled = FALSE;
    gboolean     es_scrambled = FALSE;
    proto_tree  *es_tree = NULL;
    gboolean     desc_ok = FALSE;


    if (tag==T_CA_INFO) {
        if (len_field % 2) {
            tag_str = val_to_str(tag, dvbci_apdu_tag, "Unknown: %d");
            proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb, 0, APDU_TAG_SIZE,
                    "Invalid APDU length field, %s must be a multiple of 2 bytes",
                    tag_str);
            return;
        }

        while (tvb_reported_length_remaining(tvb, offset) > 0) {
            proto_tree_add_item(
                    tree, hf_dvbci_ca_sys_id, tvb, offset, 2, ENC_BIG_ENDIAN);
            offset += 2;
        }
    }
    else if (tag==T_CA_PMT) {
        proto_tree_add_item(
                tree, hf_dvbci_ca_pmt_list_mgmt, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        prog_num = tvb_get_ntohs(tvb, offset);
        col_append_sep_fstr(
                pinfo->cinfo, COL_INFO, NULL, "Program number %x", prog_num);
        proto_tree_add_item(
                tree, hf_dvbci_prog_num, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset += 2;
        proto_tree_add_item(
                tree, hf_dvbci_ca_ver, tvb, offset, 1, ENC_BIG_ENDIAN);
        proto_tree_add_item(
                tree, hf_dvbci_curr_next, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        prog_info_len = tvb_get_ntohs(tvb, offset) & 0x0FFF;
        /* the definition of hf_dvbci_prog_info_len also applies the mask */
        proto_tree_add_item(
                tree, hf_dvbci_prog_info_len, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset += 2;
        if (prog_info_len != 0) {
            all_len = offset + prog_info_len;

            proto_tree_add_item(
                    tree, hf_dvbci_ca_pmt_cmd_id, tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            while (offset < all_len) {
                ca_desc_len = dissect_ca_desc(tvb, offset, pinfo, tree);
                if (ca_desc_len <= 0)
                    return;
                offset += ca_desc_len;
                /* set this only if we've seen at least one valid ca descriptor */
                scrambled = TRUE;
            }
        }
        else {
            proto_tree_add_expert(
                    tree, pinfo, &ei_dvbci_no_ca_desc_prog, tvb, 0, 0);
        }

        while (tvb_reported_length_remaining(tvb, offset) > 0) {
            es_info_len = dissect_es(tvb, offset, pinfo, tree, &es_scrambled);
            if (es_info_len <= 0)
                return;
            offset += es_info_len;
            if (es_scrambled)
                scrambled = TRUE;
        }

        col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
                scrambled ? "scrambled service" : "free service");
    }
    else if (tag==T_CA_PMT_REPLY) {
        prog_num = tvb_get_ntohs(tvb, offset);
        col_append_sep_fstr(
                pinfo->cinfo, COL_INFO, NULL, "Program number %x", prog_num);
        proto_tree_add_item(
                tree, hf_dvbci_prog_num, tvb, offset, 2, ENC_BIG_ENDIAN);
        offset += 2;
        proto_tree_add_item(
                tree, hf_dvbci_ca_ver, tvb, offset, 1, ENC_BIG_ENDIAN);
        proto_tree_add_item(
                tree, hf_dvbci_curr_next, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        desc_ok |= dissect_ca_enable(tvb, offset, pinfo, tree);
        offset++;
        while (tvb_reported_length_remaining(tvb, offset) > 0) {
            /* there's no need to check for tree==NULL */
            es_tree = proto_tree_add_subtree(tree, tvb, offset, 3, ett_dvbci_application, NULL, "Elementary Stream");

            proto_tree_add_item(es_tree, hf_dvbci_es_pid,
                    tvb, offset, 2, ENC_BIG_ENDIAN);
            offset += 2;
            desc_ok |= dissect_ca_enable(tvb, offset, pinfo, es_tree);
            offset++;
        }
        if (desc_ok) {
            col_append_sep_str(
                pinfo->cinfo, COL_INFO, NULL, "descrambling possible");
        }
     }
}


static void
dissect_dvbci_payload_aut(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo _U_, proto_tree *tree)
{
    gint bytes_len;

    proto_tree_add_item(tree, hf_dvbci_auth_proto_id,
            tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    bytes_len = tvb_reported_length_remaining(tvb, offset);
    if (bytes_len <= 0)
        return;

    if (tag==T_AUTH_REQ) {
        proto_tree_add_item(tree, hf_dvbci_auth_req_bytes,
            tvb, offset, bytes_len, ENC_NA);
    }
    else if (tag==T_AUTH_RESP) {
        proto_tree_add_item(tree, hf_dvbci_auth_resp_bytes,
            tvb, offset, bytes_len, ENC_NA);
    }
}


static void
dissect_dvbci_payload_hc(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
    proto_item *pi;
    guint16     nid, onid, tsid, svcid;
    guint8      ref;
    guint16     old_pid, new_pid;
    gboolean    pmt_flag;
    gint        desc_loop_len;
    tvbuff_t   *pmt_tvb = NULL;
    guint8      status;


    switch (tag) {
        case T_TUNE:
            nid = tvb_get_ntohs(tvb, offset);
            pi = proto_tree_add_item(
                    tree, hf_dvbci_network_id, tvb, offset, 2, ENC_BIG_ENDIAN);
            if (nid) {
                expert_add_info(pinfo, pi, &ei_dvbci_network_id);
            }
            offset += 2;
            onid = tvb_get_ntohs(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_original_network_id,
                    tvb, offset, 2, ENC_BIG_ENDIAN);
            offset += 2;
            tsid = tvb_get_ntohs(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_transport_stream_id,
                    tvb, offset, 2, ENC_BIG_ENDIAN);
            offset += 2;
            svcid = tvb_get_ntohs(tvb, offset);
            proto_tree_add_item(
                    tree, hf_dvbci_service_id, tvb, offset, 2, ENC_BIG_ENDIAN);
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ",
                    "nid 0x%x, onid 0x%x, tsid 0x%x, svcid 0x%x",
                    nid, onid, tsid, svcid);
            break;
        case T_REPLACE:
            ref = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_replacement_ref,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            old_pid = tvb_get_ntohs(tvb, offset) & 0x1FFF;
            proto_tree_add_item(tree, hf_dvbci_replaced_pid,
                    tvb, offset, 2, ENC_BIG_ENDIAN);
            offset += 2;
            new_pid = tvb_get_ntohs(tvb, offset) & 0x1FFF;
            proto_tree_add_item( tree, hf_dvbci_replacement_pid,
                    tvb, offset, 2, ENC_BIG_ENDIAN);
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ",
                    "ref 0x%x, 0x%x -> 0x%x", ref, old_pid, new_pid);
            break;
        case T_CLEAR_REPLACE:
            ref = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_replacement_ref,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ", "ref 0x%x", ref);
            break;
        case T_TUNE_BROADCAST_REQ:
            pmt_flag = ((tvb_get_guint8(tvb, offset) & 0x01) == 0x01);
            proto_tree_add_item(tree, hf_dvbci_pmt_flag,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            proto_tree_add_item(
                    tree, hf_dvbci_service_id, tvb, offset, 2, ENC_BIG_ENDIAN);
            offset += 2;
            desc_loop_len = dissect_desc_loop(hf_dvbci_hc_desc_loop_len,
                                tvb, offset, pinfo, tree);
            if (desc_loop_len<0)
                break;
            offset += desc_loop_len;
            if (pmt_flag) {
                pmt_tvb = tvb_new_subset_remaining(tvb, offset);
                if (mpeg_pmt_handle) {
                    col_append_str(pinfo->cinfo, COL_INFO, ", ");
                    /* prevent mpeg_pmt dissector from clearing col_info */
                    col_set_fence(pinfo->cinfo, COL_INFO);
                    call_dissector(mpeg_pmt_handle, pmt_tvb, pinfo, tree);
                }
                else
                    call_dissector(data_handle, pmt_tvb, pinfo, tree);
            }
            break;
        case T_TUNE_REPLY:
            status = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_hc_status,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                        (status == HC_STAT_OK ?  "ok" : "error"));
            break;
        case T_ASK_RELEASE_REPLY:
            proto_tree_add_item(tree, hf_dvbci_hc_release_reply,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            break;
        default:
            break;
    }
}


static void
dissect_dvbci_payload_dt(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
    nstime_t     resp_intv;
    proto_item  *pi = NULL;
    const gchar *tag_str;
    gint         time_field_len;
    nstime_t     utc_time;
    gint16       local_offset;  /* field in the apdu */


    if (tag==T_DATE_TIME_ENQ) {
        nstime_set_zero(&resp_intv);
        resp_intv.secs = tvb_get_guint8(tvb, offset);
        pi = proto_tree_add_time(tree, hf_dvbci_resp_intv,
                tvb, offset, 1, &resp_intv);
        if (resp_intv.secs==0) {
            col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "individual query");
            proto_item_append_text(pi, " (individual query)");
        }
        else {
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL,
                    "update every %s", rel_time_to_str(wmem_packet_scope(), &resp_intv));
        }
    }
    else if (tag==T_DATE_TIME) {
        if (len_field!=5 && len_field!=7) {
            tag_str = val_to_str_const(tag, dvbci_apdu_tag, "unknown");
            proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb, APDU_TAG_SIZE, offset-APDU_TAG_SIZE,
                    "Invalid APDU length field, %s must be 5 or 7 bytes", tag_str);
            return;
        }

        time_field_len = packet_mpeg_sect_mjd_to_utc_time(tvb, offset, &utc_time);
        if (time_field_len<0) {
            proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb, offset, 5,
                "Invalid UTC time field, 2 bytes MJD, 3 bytes BCD time hhmmss");
            return;
        }
        proto_tree_add_time(tree, hf_dvbci_utc_time,
                tvb, offset, time_field_len, &utc_time);
        col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ", "%s UTC",
                abs_time_to_str(wmem_packet_scope(), &utc_time, ABSOLUTE_TIME_UTC, FALSE));
        offset += time_field_len;

        if (len_field==7) {
            local_offset = two_comp_to_int16(tvb_get_ntohs(tvb, offset));
            proto_tree_add_int_format(tree, hf_dvbci_local_offset,
                    tvb, offset, 2, local_offset,
                    "offset between UTC and local time is %d minutes",
                    local_offset);
        }
        else {
            proto_tree_add_expert(tree, pinfo, &ei_dvbci_time_offs_unknown,
                    tvb, 0, 0);
        }
    }
}


static void
dissect_dvbci_payload_mmi(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
    gint            offset_start;
    proto_item     *pi;
    guint8          close_mmi_cmd_id;
    guint8          disp_ctl_cmd, disp_rep_id;
    const gchar    *disp_ctl_cmd_str = NULL, *disp_rep_id_str = NULL;
    gint            msg_len;
    guint           enc_len;
    dvb_encoding_e  encoding;
    guint8          ans_txt_len;
    guint8          ans_id;
    guint8          choice_or_item_nb;
    gint            text_len;
    guint8          choice_ref;


    offset_start = offset;

    switch(tag) {
        case T_CLOSE_MMI:
            close_mmi_cmd_id = tvb_get_guint8(tvb,offset);
            proto_tree_add_item(tree, hf_dvbci_close_mmi_cmd_id,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            /* apdu layer len field checks are sufficient for "immediate" */
            if (close_mmi_cmd_id == CLOSE_MMI_CMD_ID_DELAY) {
                if (len_field != 2) {
                    proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb,
                            APDU_TAG_SIZE, offset_start-APDU_TAG_SIZE,
                            "Length field must be 2");
                    return;
                }
                proto_tree_add_item(tree, hf_dvbci_close_mmi_delay, tvb,
                        offset, 1, ENC_BIG_ENDIAN);
            }
            break;
        case T_DISPLAY_CONTROL:
            disp_ctl_cmd = tvb_get_guint8(tvb,offset);
            disp_ctl_cmd_str = val_to_str_const(disp_ctl_cmd,
                                                dvbci_disp_ctl_cmd, "unknown command");
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                    disp_ctl_cmd_str);
            proto_tree_add_item(tree, hf_dvbci_disp_ctl_cmd, tvb,
                    offset, 1, ENC_BIG_ENDIAN);
            offset++;
            if (disp_ctl_cmd == DISP_CMD_SET_MMI_MODE)
            {
                proto_tree_add_item(tree, hf_dvbci_mmi_mode, tvb,
                        offset, 1, ENC_BIG_ENDIAN);
                if (len_field != 2) {
                    proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb,
                            APDU_TAG_SIZE, offset_start-APDU_TAG_SIZE,
                            "Length field must be 2");
                    return;
                }
            }
            break;
        case T_DISPLAY_REPLY:
            disp_rep_id = tvb_get_guint8(tvb,offset);
            disp_rep_id_str = val_to_str_const(disp_rep_id,
                    dvbci_disp_rep_id, "unknown command");
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                    disp_rep_id_str);
            proto_tree_add_item(tree, hf_dvbci_disp_rep_id,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            if (disp_rep_id == DISP_REP_ID_MMI_MODE_ACK) {
                proto_tree_add_item(tree, hf_dvbci_mmi_mode,
                        tvb, offset, 1, ENC_BIG_ENDIAN);
            }
            else if (disp_rep_id == DISP_REP_ID_DISP_CHAR_TBL ||
                     disp_rep_id == DISP_REP_ID_INP_CHAR_TBL) {
                while ((msg_len=tvb_reported_length_remaining(tvb, offset)) > 0) {
                    enc_len = dvb_analyze_string_charset(
                            tvb, offset, msg_len, &encoding);
                    if (enc_len==0) {
                        proto_tree_add_expert(tree, pinfo,
                            &ei_dvbci_invalid_char_tbl, tvb, offset, msg_len);
                        break;
                    }
                    dvb_add_chartbl(tree, hf_dvbci_mmi_char_tbl,
                            tvb, offset, enc_len, encoding);
                    offset += enc_len;
                 }
            }
            break;
        case T_ENQ:
            proto_tree_add_item(tree, hf_dvbci_blind_ans,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            ans_txt_len = tvb_get_guint8(tvb,offset);
            pi = proto_tree_add_item(tree, hf_dvbci_ans_txt_len,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            if (ans_txt_len == NB_UNKNOWN) {
                proto_item_append_text(pi,
                        " (Length of expected answer is unknown)");
            }
            offset++;
            dissect_si_string(tvb, offset,
                    tvb_reported_length_remaining(tvb, offset),
                    pinfo, tree, hf_dvbci_enq, "Enquiry string", FALSE);
            break;
        case T_ANSW:
            ans_id = tvb_get_guint8(tvb,offset);
            proto_tree_add_item(tree, hf_dvbci_ans_id, tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            if (ans_id == ANSW_ID_ANSWER) {
                dissect_si_string(tvb, offset,
                    tvb_reported_length_remaining(tvb, offset),
                    pinfo, tree, hf_dvbci_ans, "Answer", TRUE);
            }
            break;
        case T_MENU_LAST:
        case T_MENU_MORE:
        case T_LIST_LAST:
        case T_LIST_MORE:
            choice_or_item_nb = tvb_get_guint8(tvb,offset);
            if (IS_MENU_APDU(tag)) {
                pi = proto_tree_add_item(
                        tree, hf_dvbci_choice_nb, tvb, offset, 1, ENC_BIG_ENDIAN);
                if (choice_or_item_nb == NB_UNKNOWN) {
                    proto_item_append_text(pi,
                            " (Number of choices is unknown)");
                }
            }
            else {
                pi = proto_tree_add_item(
                        tree, hf_dvbci_item_nb, tvb, offset, 1, ENC_BIG_ENDIAN);
                if (choice_or_item_nb == NB_UNKNOWN) {
                    proto_item_append_text(pi,
                            "(Number of items is unknown)");
                }
            }
            offset++;
            text_len = dissect_dvbci_text("Title", tvb, offset,
                    pinfo, tree, hf_dvbci_title);
            offset += text_len;
            text_len = dissect_dvbci_text("Sub-title", tvb, offset,
                    pinfo, tree, hf_dvbci_subtitle);
            offset += text_len;
            text_len = dissect_dvbci_text("Bottom line", tvb, offset,
                    pinfo, tree, hf_dvbci_bottom);
            offset += text_len;
            while (tvb_reported_length_remaining(tvb, offset) > 0) {
                text_len = dissect_dvbci_text("Item", tvb, offset, pinfo, tree, hf_dvbci_item);
                /* minimum is apdu tag + 1 byte len field */
                if (text_len<APDU_TAG_SIZE+1) {
                    proto_tree_add_expert(tree, pinfo, &ei_dvbci_not_text_more_or_text_last, tvb, offset, -1);
                    return;
                }
                offset += text_len;
            }
            break;
        case T_MENU_ANSW:
            choice_ref = tvb_get_guint8(tvb,offset);
            pi = proto_tree_add_item(
                    tree, hf_dvbci_choice_ref, tvb, offset, 1, ENC_BIG_ENDIAN);
            if (choice_ref == 0x0) {
                proto_item_append_text(pi, " (Selection was cancelled)");
                col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                        "cancelled");
            }
            else {
                col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ",
                        "Item %d", choice_ref);
            }
            break;
        default:
            break;
    }
}


static void
dissect_dvbci_payload_hlc(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
  guint8 *str;

  if (tag==T_HOST_COUNTRY) {
      proto_tree_add_item(tree, hf_dvbci_host_country,
              tvb, offset, tvb_reported_length_remaining(tvb, offset),
              ENC_ISO_8859_1|ENC_NA);
  }
  else if (tag==T_HOST_LANGUAGE) {
      proto_tree_add_item(tree, hf_dvbci_host_language,
              tvb, offset, tvb_reported_length_remaining(tvb, offset),
              ENC_ISO_8859_1|ENC_NA);
  }

  /* both apdus' body is only a country code, this can be shared */
  str = tvb_get_string_enc(wmem_packet_scope(), tvb, offset,
              tvb_reported_length_remaining(tvb, offset),
              ENC_ISO_8859_1|ENC_NA);
  if (str)
      col_append_sep_str(pinfo->cinfo, COL_INFO, ": ", str);
}


static void
dissect_dvbci_payload_cup(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
  guint8      upgrade_type;
  guint16     download_time;
  guint8      answer, progress;

  switch(tag) {
    case T_CAM_FIRMWARE_UPGRADE:
      upgrade_type = tvb_get_guint8(tvb, offset);
      proto_tree_add_item(tree, hf_dvbci_cup_type, tvb, offset, 1, ENC_BIG_ENDIAN);
      col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "(%s)",
                    val_to_str_const(upgrade_type, dvbci_cup_type, "unknown"));
      offset++;
      download_time = tvb_get_ntohs(tvb, offset);
      if (download_time == 0) {
          proto_tree_add_uint_format(tree, hf_dvbci_cup_download_time,
                  tvb, offset, 2, download_time,
                  "estimated download time is unknown");
      }
      else {
          proto_tree_add_uint_format(tree, hf_dvbci_cup_download_time,
                  tvb, offset, 2, download_time,
                  "estimated download time is %d seconds",
                  download_time);
      }
      break;
    case T_CAM_FIRMWARE_UPGRADE_REPLY:
      answer = tvb_get_guint8(tvb, offset);
      proto_tree_add_item(tree, hf_dvbci_cup_answer, tvb, offset, 1, ENC_BIG_ENDIAN);
      col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                    val_to_str_const(answer, dvbci_cup_answer, "unknown"));
      break;
    case T_CAM_FIRMWARE_UPGRADE_PROGRESS:
      progress = tvb_get_guint8(tvb, offset);
      if (progress > 100) {
        proto_tree_add_expert(tree, pinfo, &ei_dvbci_cup_progress, tvb, offset, 1);
      }
      else {
          col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ", "%d%%", progress);
          proto_tree_add_uint_format(tree, hf_dvbci_cup_progress,
                  tvb, offset, 1, progress,
                  "download progress %d%%", progress);
      }
      break;
    case T_CAM_FIRMWARE_UPGRADE_COMPLETE:
      proto_tree_add_item(tree, hf_dvbci_cup_reset, tvb, offset, 1, ENC_BIG_ENDIAN);
      break;
    default:
      break;
  }
}

static int exp_pdu_data_dvbci_size(packet_info *pinfo _U_, void* data _U_)
{
  return EXP_PDU_TAG_DVBCI_EVT_LEN + 4;
}

static int exp_pdu_data_dvbci_populate_data(packet_info *pinfo, void* data, guint8 *tlv_buffer, guint32 buffer_size _U_)
{
  tlv_buffer[0] = 0;
  tlv_buffer[1] = EXP_PDU_TAG_DVBCI_EVT;
  tlv_buffer[2] = 0;
  tlv_buffer[3] = EXP_PDU_TAG_DVBCI_EVT_LEN;
  tlv_buffer[4] = dvbci_get_evt_from_addrs(pinfo);

  return exp_pdu_data_dvbci_size(pinfo, data);
}

static exp_pdu_data_item_t exp_pdu_dvbci = {exp_pdu_data_dvbci_size, exp_pdu_data_dvbci_populate_data, NULL};

static void
dissect_sac_msg(guint32 tag, tvbuff_t *tvb, gint offset,
        packet_info *pinfo, proto_tree *tree, gboolean exported)
{
    gint        offset_start;
    guint32     msg_ctr;
    guint8      enc_flag, enc_cip;
    proto_item *enc_flag_pi;
    guint16     sac_payload_len;          /* payload data and padding */
    gint        sac_payload_data_len = 0; /* just payload data */
    tvbuff_t   *clear_sac_body_tvb;
    proto_tree *sac_tree             = NULL;
    gboolean    is_exportable        = FALSE;

    offset_start = offset;

    /* it's not useful to move sac header dissection to a separate
       function, we need enc/auth cipher etc here to handle the body */
    msg_ctr = tvb_get_ntohl(tvb, offset);
    proto_tree_add_item(tree, hf_dvbci_sac_msg_ctr,
            tvb, offset, 4, ENC_BIG_ENDIAN);
    col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL,
            "message #%d", msg_ctr);
    offset += 4;
    proto_tree_add_item(tree, hf_dvbci_sac_proto_ver,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_sac_auth_cip,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    enc_flag = tvb_get_guint8(tvb, offset) & 0x1;
    enc_flag_pi = proto_tree_add_item(tree, hf_dvbci_sac_payload_enc,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    if (exported && enc_flag) {
        expert_add_info(pinfo, enc_flag_pi, &ei_dvbci_sac_payload_enc);
        enc_flag = 0;
    }
    offset++;
    enc_cip = (tvb_get_guint8(tvb, offset)&0xE0) >> 5;
    proto_tree_add_item(tree, hf_dvbci_sac_enc_cip,
            tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    sac_payload_len = tvb_get_ntohs(tvb, offset);
    proto_tree_add_item(tree, hf_dvbci_sac_payload_len,
            tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;
    if (tvb_reported_length_remaining(tvb, offset) < 0)
        return;
    if (enc_flag) {
        clear_sac_body_tvb = decrypt_sac_msg_body(pinfo, enc_cip,
                tvb, offset, tvb_reported_length_remaining(tvb, offset));
    }
    else {
        clear_sac_body_tvb = tvb_new_subset_remaining(tvb, offset);
    }
    if (!clear_sac_body_tvb) {
        /* we could not decrypt (or access) the clear sac message body */
        proto_tree_add_item(tree, hf_dvbci_sac_enc_body, tvb, offset,
                tvb_reported_length_remaining(tvb, offset), ENC_NA);
        return;
    }
    if (enc_flag)
        add_new_data_source(pinfo, clear_sac_body_tvb, "Clear SAC message body");
    if (sac_payload_len>0) {
        sac_tree = proto_tree_add_subtree(tree, clear_sac_body_tvb, 0, sac_payload_len,
                ett_dvbci_sac_msg_body, NULL, "SAC message payload");
        if (tag==T_CC_SAC_DATA_REQ || tag==T_CC_SAC_DATA_CNF) {
            sac_payload_data_len = dissect_cc_data_payload(tag,
                    clear_sac_body_tvb, 0, pinfo, sac_tree, &is_exportable);
        }
        else if (tag==T_CC_SAC_SYNC_REQ) {
            sac_payload_data_len = 0;
        }
        else if (tag==T_CC_SAC_SYNC_CNF) {
            proto_tree_add_item(sac_tree, hf_dvbci_cc_status_field,
                    clear_sac_body_tvb, 0, 1, ENC_BIG_ENDIAN);
            sac_payload_data_len = 1;
        }

        if (sac_payload_data_len < 0)
            return;
        if (sac_payload_len > sac_payload_data_len) {
            proto_tree_add_item(sac_tree, hf_dvbci_sac_padding,
                    clear_sac_body_tvb, sac_payload_data_len,
                    sac_payload_len-sac_payload_data_len, ENC_NA);
        }
    }
    proto_tree_add_item(tree, hf_dvbci_sac_signature,
            clear_sac_body_tvb, sac_payload_len,
            tvb_reported_length_remaining(clear_sac_body_tvb,
                sac_payload_len), ENC_NA);

    /* we call this function also to dissect exported SAC messages,
        don't try to export them a second time */
    if (!exported && is_exportable && have_tap_listener(exported_pdu_tap)) {
        static const exp_pdu_data_item_t *dvbci_exp_pdu_items[] = {
            &exp_pdu_dvbci,
            NULL
        };

        tvbuff_t       *clear_sac_msg_tvb;
        exp_pdu_data_t *exp_pdu_data;

        clear_sac_msg_tvb = tvb_new_composite();
        tvb_composite_append(clear_sac_msg_tvb,
                tvb_clone_offset_len(tvb, offset_start, SAC_MSG_HDR_LEN));
        tvb_composite_append(clear_sac_msg_tvb, clear_sac_body_tvb);
        tvb_composite_finalize(clear_sac_msg_tvb);

        exp_pdu_data = export_pdu_create_tags(pinfo, EXPORTED_SAC_MSG_PROTO, EXP_PDU_TAG_PROTO_NAME, dvbci_exp_pdu_items);

        exp_pdu_data->tvb_captured_length = tvb_captured_length(clear_sac_msg_tvb);
        exp_pdu_data->tvb_reported_length = tvb_reported_length(clear_sac_msg_tvb);
        exp_pdu_data->pdu_tvb = clear_sac_msg_tvb;
        tap_queue_packet(exported_pdu_tap, pinfo, exp_pdu_data);
    }
}


static int
dissect_dvbci_exported_sac_msg(
        tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    guint8 evt;
    guint32 tag;

    evt = dvbci_get_evt_from_addrs(pinfo);
    if (!IS_DATA_TRANSFER(evt))
        return 0;

    col_append_sep_str(pinfo->cinfo, COL_PROTOCOL, NULL, EXPORTED_SAC_MSG_PROTO);
    col_clear(pinfo->cinfo, COL_INFO);

    /* we only export cc_sac_data_req and _cnf, therefore, the tag can be
       derived from the direction of the transfer */
    if (evt== DVBCI_EVT_DATA_CAM_TO_HOST)
       tag = T_CC_SAC_DATA_REQ;
    else
       tag = T_CC_SAC_DATA_CNF;

    col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
            val_to_str_const(tag, dvbci_apdu_tag, "unknown"));

    dissect_sac_msg(tag, tvb, 0, pinfo, tree, TRUE);
    return tvb_reported_length(tvb);
}


static void
dissect_dvbci_payload_cc(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
    guint8      status;
    proto_item *pi;
    nstime_t    utc_time;
    guint8      pin_stat;
    guint8      evt_cent;

    switch(tag) {
        case T_CC_OPEN_CNF:
            proto_tree_add_item(tree, hf_dvbci_cc_sys_id_bitmask,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            break;
        case T_CC_DATA_REQ:
        case T_CC_DATA_CNF:
            dissect_cc_data_payload(tag, tvb, offset, pinfo, tree, NULL);
            break;
        case T_CC_SYNC_CNF:
            status = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(
                    tree, hf_dvbci_cc_status_field, tvb, offset, 1, ENC_BIG_ENDIAN);
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                    val_to_str_const(status, dvbci_cc_status, "unknown"));
            break;
        case T_CC_SAC_DATA_REQ:
        case T_CC_SAC_DATA_CNF:
        case T_CC_SAC_SYNC_REQ:
        case T_CC_SAC_SYNC_CNF:
            dissect_sac_msg(tag, tvb, offset, pinfo, tree, FALSE);
            break;
        case T_CC_PIN_CAPABILITIES_REPLY:
            proto_tree_add_item(tree, hf_dvbci_capability_field,
                    tvb, offset, 1 , ENC_BIG_ENDIAN);
            offset++;
            /* we can't packet_mpeg_sect_mjd_to_utc_time()
               and check with nstime_is_zero() */
            if (tvb_get_ntoh40(tvb, offset) == 0) {
                proto_tree_add_expert(tree, pinfo, &ei_dvbci_cc_pin_nvr_chg,
                        tvb, offset, UTC_TIME_LEN);
            }
            else {
                if (packet_mpeg_sect_mjd_to_utc_time(tvb, offset, &utc_time) < 0) {
                    proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb, offset, UTC_TIME_LEN,
                            "Invalid UTC time field, 2 bytes MJD, 3 bytes BCD time hhmmss");
                    break;
                }
                else {
                    /* abs_time_to_ep_str() never returns NULL */
                    proto_tree_add_time(tree, hf_dvbci_pin_chg_time,
                            tvb, offset, UTC_TIME_LEN, &utc_time);
                }
            }
            offset += UTC_TIME_LEN;
            dissect_rating(tvb, offset, pinfo, tree);
            break;
        case T_CC_PIN_REPLY:
            pin_stat = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_pincode_status,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                    val_to_str_const(pin_stat, dvbci_pincode_status, "unknown"));
            break;
        case T_CC_PIN_EVENT:
            proto_tree_add_item(tree, hf_dvbci_cc_prog_num,
                    tvb, offset, 2, ENC_BIG_ENDIAN);
            offset += 2;
            proto_tree_add_item(tree, hf_dvbci_pincode_status,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            dissect_rating(tvb, offset, pinfo, tree);
            offset++;
            if (packet_mpeg_sect_mjd_to_utc_time(tvb, offset, &utc_time) < 0) {
                proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb, offset, UTC_TIME_LEN,
                        "Invalid UTC time field, 2 bytes MJD, 3 bytes BCD time hhmmss");
                break;
            }
            else {
                proto_tree_add_time(tree, hf_dvbci_pin_evt_time,
                        tvb, offset, UTC_TIME_LEN, &utc_time);
            }
            offset += UTC_TIME_LEN;
            evt_cent = tvb_get_guint8(tvb, offset);
            pi = proto_tree_add_item(tree, hf_dvbci_pin_evt_cent,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            if (evt_cent > 100) {
                expert_add_info(pinfo, pi, &ei_dvbci_pin_evt_cent);
            }
            offset++;
            /* length field was already checked by the caller */
            proto_tree_add_item(tree, hf_dvbci_cc_priv_data, tvb, offset,
                    tvb_reported_length_remaining(tvb, offset), ENC_NA);
            break;
        case T_CC_PIN_PLAYBACK:
            dissect_rating(tvb, offset, pinfo, tree);
            offset++;
            /* length field was already checked by the caller */
            proto_tree_add_item(tree, hf_dvbci_cc_priv_data, tvb, offset,
                    tvb_reported_length_remaining(tvb, offset), ENC_NA);
            break;
        case T_CC_PIN_CMD:
        case T_CC_PIN_MMI_REQ:
            proto_tree_add_item(tree, hf_dvbci_pincode, tvb, offset,
                    tvb_reported_length_remaining(tvb, offset),
                    ENC_ASCII|ENC_NA);
            break;
        default:
            break;
    }
}


static void
dissect_dvbci_ami_file_ack(tvbuff_t *tvb, gint offset,
        packet_info *pinfo, proto_tree *tree)
{
    guint8      req_type;
    gboolean    req_ok = FALSE, file_ok;
    guint8      file_name_len;
    guint8     *file_name_str;
    guint32     file_data_len;
    tvbuff_t   *png_file_tvb = NULL;
    proto_tree *req_tree;

    req_type = tvb_get_guint8(tvb, offset+1);
    if (req_type==REQ_TYPE_FILE_HASH) {
        req_ok = ((tvb_get_guint8(tvb, offset) & 0x02) == 0x02);
        proto_tree_add_item(tree, hf_dvbci_req_ok,
                tvb, offset, 1, ENC_BIG_ENDIAN);
    }
    file_ok = ((tvb_get_guint8(tvb, offset) & 0x01) == 0x01);
    proto_tree_add_item(tree, hf_dvbci_file_ok, tvb, offset, 1, ENC_BIG_ENDIAN);
    offset++;
    proto_tree_add_item(tree, hf_dvbci_req_type, tvb, offset, 1, ENC_BIG_ENDIAN);
    col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
            val_to_str_const(req_type, dvbci_req_type, "unknown"));
    offset++;
    if (req_type==REQ_TYPE_FILE || req_type==REQ_TYPE_FILE_HASH) {
        file_name_len = tvb_get_guint8(tvb, offset);
        proto_tree_add_item(tree, hf_dvbci_file_name_len,
                tvb, offset, 1, ENC_BIG_ENDIAN);
        offset++;
        file_name_str = tvb_get_string_enc(wmem_packet_scope(),
                tvb, offset, file_name_len, ENC_ASCII);
        if (!file_name_str)
            return;
        col_append_sep_str(pinfo->cinfo, COL_INFO, " ",
                file_name_str);
        proto_tree_add_string_format_value(tree, hf_dvbci_file_name,
                tvb, offset, file_name_len, file_name_str,
                "%s", file_name_str);
        offset += file_name_len;
        file_data_len = tvb_get_ntohl(tvb, offset);
        proto_tree_add_item(tree, hf_dvbci_file_data_len,
                tvb, offset, 4, ENC_BIG_ENDIAN);
        offset += 4;
        if (file_data_len > 0) {
            if (file_name_len>4) {
                gchar *suffix_lo;
                suffix_lo = wmem_ascii_strdown(wmem_packet_scope(),
                        &file_name_str[file_name_len-4], -1);
                if (g_strcmp0(suffix_lo, ".png")==0) {
                    png_file_tvb = tvb_new_subset_length(
                            tvb, offset, file_data_len);
                }
            }

            if (png_handle && png_file_tvb) {
                col_set_fence(pinfo->cinfo, COL_PROTOCOL);
                col_set_fence(pinfo->cinfo, COL_INFO);
                call_dissector(png_handle, png_file_tvb, pinfo, tree);
            }
            else {
                proto_tree_add_item(tree, hf_dvbci_file_data,
                        tvb, offset, file_data_len, ENC_NA);
            }
        }
    }
    else if (req_type==REQ_TYPE_DATA) {
        if (tvb_reported_length_remaining(tvb, offset) <= 0)
            return;
        proto_tree_add_item(tree, hf_dvbci_ami_priv_data, tvb, offset,
                tvb_reported_length_remaining(tvb, offset), ENC_NA);
    }
    else if (req_type==REQ_TYPE_REQ) {
        req_tree = proto_tree_add_subtree(tree, tvb,
                offset, tvb_reported_length_remaining(tvb, offset),
                ett_dvbci_ami_req_types, NULL, "Supported request types");
        while (tvb_reported_length_remaining(tvb, offset) > 0) {
            proto_tree_add_item(req_tree, hf_dvbci_req_type,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
        }
    }

    if (req_type==REQ_TYPE_FILE_HASH && req_ok && !file_ok) {
        col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
                "cached copy is valid");
    }
}


static void
dissect_dvbci_payload_ami(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
    guint8  app_dom_id_len, init_obj_len;
    const guint8 *app_dom_id;
    guint8  ack_code;
    guint8  req_type;
    const guint8 *req_str;

    switch(tag) {
        case T_REQUEST_START:
            /* no filter for length items */
            app_dom_id_len = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_app_dom_id_len,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            init_obj_len = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_init_obj_len,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            proto_tree_add_item_ret_string(tree, hf_dvbci_app_dom_id,
                    tvb, offset, app_dom_id_len, ENC_ASCII|ENC_NA, wmem_packet_scope(), &app_dom_id);
            if (app_dom_id) {
                col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ",
                        "for %s", app_dom_id);
            }
            offset += app_dom_id_len;
            proto_tree_add_item(tree, hf_dvbci_init_obj,
                    tvb, offset, init_obj_len, ENC_ASCII|ENC_NA);
            break;
        case T_REQUEST_START_ACK:
            ack_code = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(
                    tree, hf_dvbci_ack_code, tvb, offset, 1, ENC_BIG_ENDIAN);
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                    val_to_str_const(ack_code, dvbci_ack_code, "unknown"));
            break;
        case T_FILE_REQUEST:
            req_type = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_req_type, tvb, offset, 1, ENC_BIG_ENDIAN);
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                    val_to_str_const(req_type, dvbci_req_type, "unknown"));
            offset++;
            if (req_type==REQ_TYPE_FILE_HASH) {
                proto_tree_add_item(tree, hf_dvbci_file_hash,
                        tvb, offset, 16, ENC_NA);
                offset += 16;
            }
            if (tvb_reported_length_remaining(tvb, offset) <= 0)
              break;
            if (req_type==REQ_TYPE_FILE || req_type==REQ_TYPE_FILE_HASH) {
                proto_tree_add_item_ret_string(tree, hf_dvbci_file_name,
                        tvb, offset, tvb_reported_length_remaining(tvb, offset),
                        ENC_ASCII, wmem_packet_scope(), &req_str);
                col_append_sep_str(pinfo->cinfo, COL_INFO, " ", req_str);
            }
            else if (req_type==REQ_TYPE_DATA) {
                proto_tree_add_item(tree, hf_dvbci_ami_priv_data, tvb, offset,
                        tvb_reported_length_remaining(tvb, offset), ENC_NA);
            }
            break;
        case T_FILE_ACKNOWLEDGE:
            dissect_dvbci_ami_file_ack(tvb, offset, pinfo, tree);
            break;
        case T_APP_ABORT_REQUEST:
            if (tvb_reported_length_remaining(tvb, offset) > 0) {
                proto_tree_add_item(tree, hf_dvbci_abort_req_code, tvb, offset,
                        tvb_reported_length_remaining(tvb, offset), ENC_NA);
            }
            break;
        case T_APP_ABORT_ACK:
            if (tvb_reported_length_remaining(tvb, offset) > 0) {
                proto_tree_add_item(tree, hf_dvbci_abort_ack_code, tvb, offset,
                        tvb_reported_length_remaining(tvb, offset), ENC_NA);
            }
            break;
        default:
            break;
    }
}


static void
dissect_dvbci_payload_lsc(guint32 tag, gint len_field,
        tvbuff_t *tvb, gint offset, circuit_t *circuit,
        packet_info *pinfo, proto_tree *tree)
{
    gint                offset_start;
    guint8              id, timeout, ret_val, phase_id;
    gint                conn_desc_len, param_len;
    guint16             buf_size;
    proto_item         *pi          = NULL;
    const gchar        *ret_val_str = NULL;
    gint                msg_len;
    tvbuff_t           *msg_tvb;
    dissector_handle_t  msg_handle;

    offset_start = offset;

    switch(tag) {
        case T_COMMS_CMD:
            proto_tree_add_item(tree, hf_dvbci_comms_cmd_id,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            id = tvb_get_guint8(tvb, offset);
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                    val_to_str(id, dvbci_comms_cmd_id, "Unknown: %d"));
            offset++;
            switch(id) {
                case COMMS_CMD_ID_CONNECT_ON_CHANNEL:
                    conn_desc_len = dissect_conn_desc(tvb, offset,
                            circuit, pinfo, tree);
                    if (conn_desc_len < 0)
                        break;
                    offset += conn_desc_len;
                    proto_tree_add_item(tree, hf_dvbci_lsc_retry_count,
                            tvb, offset, 1, ENC_BIG_ENDIAN);
                    offset++;
                    timeout = tvb_get_guint8(tvb, offset);
                    if (timeout==0) {
                        proto_tree_add_uint_format(tree, hf_dvbci_lsc_timeout,
                                tvb, offset, 1, timeout, "Infinite timeout");
                    } else {
                        proto_tree_add_uint_format(tree, hf_dvbci_lsc_timeout,
                                tvb, offset, 1, timeout,
                                "Timeout is %d seconds", timeout);
                    }
                    break;
                case COMMS_CMD_ID_DISCONNECT_ON_CHANNEL:
                case COMMS_CMD_ID_ENQUIRE_STATUS:
                    /* len_field == 1 -> only id, no further parameters */
                    if (len_field != 1) {
                        proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb,
                            APDU_TAG_SIZE, offset_start-APDU_TAG_SIZE,
                            "Length field must be 1");
                    }
                    break;
                case COMMS_CMD_ID_SET_PARAMS:
                    param_len = tvb_reported_length_remaining(tvb, offset);
                    if (param_len == 2)
                        buf_size = (guint16)tvb_get_guint8(tvb, offset);
                    else if (param_len == 3)
                        buf_size = tvb_get_ntohs(tvb, offset);
                    else {
                        /* length field == 1 byte id + param_len */
                        proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb,
                            APDU_TAG_SIZE, offset_start-APDU_TAG_SIZE,
                            "Length field must be 3 or 4");
                        break;
                    }
                    proto_tree_add_uint_format(tree, hf_dvbci_lsc_buf_size,
                            tvb, offset, param_len-1, buf_size,
                            "buffer size %d bytes", buf_size);
                    offset += param_len-1;
                    timeout = tvb_get_guint8(tvb, offset);
                    proto_tree_add_uint_format(tree, hf_dvbci_lsc_timeout,
                            tvb, offset, 1, timeout,
                            "timeout is %d milliseconds", timeout*10);
                    break;
                case COMMS_CMD_ID_GET_NEXT_BUFFER:
                    phase_id = tvb_get_guint8(tvb, offset);
                    proto_tree_add_item(tree, hf_dvbci_phase_id,
                            tvb, offset, 1, ENC_BIG_ENDIAN);
                    col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL,
                            "received #%d", phase_id);
                    break;
                default:
                    break;
            }
            break;
        case T_COMMS_REPLY:
            proto_tree_add_item(tree, hf_dvbci_comms_rep_id,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            id = tvb_get_guint8(tvb,offset);
            col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
                    val_to_str(id, dvbci_comms_rep_id, "Unknown: %d"));
            offset++;
            ret_val = tvb_get_guint8(tvb,offset);
            pi = proto_tree_add_item(tree, hf_dvbci_lsc_ret_val,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            switch (id) {
                case COMMS_REP_ID_SEND_ACK:
                    col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ",
                        "sent #%d", ret_val);
                    proto_item_append_text(pi, " (sent #%d)", ret_val);
                    break;
                case COMMS_REP_ID_SET_PARAMS_ACK:
                    ret_val_str = val_to_str_const(ret_val,
                            dvbci_lsc_ret_val_params, "unknown/error");
                    break;
                case COMMS_REP_ID_STATUS_REPLY:
                    ret_val_str = val_to_str_const(ret_val,
                            dvbci_lsc_ret_val_connect, "unknown/error");
                    break;
                default:
                    ret_val_str = val_to_str_const(ret_val,
                            dvbci_lsc_ret_val, "unknown/error");
                    break;
            }
            if (ret_val_str) {
                col_append_sep_str(pinfo->cinfo, COL_INFO, ": ",
                            ret_val_str);
                proto_item_append_text(pi, " (%s)", ret_val_str);
            }
            break;
        case T_COMMS_SEND_LAST:
        case T_COMMS_SEND_MORE:
        case T_COMMS_RCV_LAST:
        case T_COMMS_RCV_MORE:
            phase_id = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_phase_id,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ",
                    "Phase ID %d", phase_id);
            offset++;
            msg_len = tvb_reported_length_remaining(tvb, offset);
            if (msg_len<=0)
                break;
            msg_tvb = tvb_new_subset_remaining(tvb, offset);
            if (!msg_tvb)
                break;
            if (dvbci_dissect_lsc_msg && circuit_get_dissector(circuit)) {
                msg_handle = circuit_get_dissector(circuit);
                col_append_str(pinfo->cinfo, COL_INFO, ", ");
                col_set_fence(pinfo->cinfo, COL_INFO);
                col_append_str(pinfo->cinfo, COL_PROTOCOL, ", ");
                col_set_fence(pinfo->cinfo, COL_PROTOCOL);
            }
            else {
                msg_handle = data_handle;
            }
            if (msg_handle)
                call_dissector(msg_handle, msg_tvb, pinfo, tree);
            break;
        default:
            break;
    }
}


static void
dissect_dvbci_payload_opp(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit _U_,
        packet_info *pinfo, proto_tree *tree)
{
    guint16         nit_loop_len, nit_loop_offset;
    tvbuff_t       *nit_loop_tvb, *nit_loop_partial_tvb;
    guint           dvb_nit_bytes;
    guint8          table_id;
    guint8          cap_loop_len;
    gboolean        info_valid;
    guint           enc_len;
    dvb_encoding_e  encoding;
    guint8          desc_num;
    guint8          sig_strength, sig_qual;
    proto_item     *pi;

    switch(tag) {
        case T_OPERATOR_STATUS:
        case T_OPERATOR_SEARCH_STATUS:
            dissect_opp_status_body(tvb, offset, pinfo, tree);
          break;
        case T_OPERATOR_NIT:
          nit_loop_len = tvb_get_ntohs(tvb, offset);
          proto_tree_add_item(tree, hf_dvbci_nit_loop_len,
                  tvb, offset, 2, ENC_BIG_ENDIAN);
          if (nit_loop_len==0)
              break;
          offset += 2;
          nit_loop_tvb = tvb_new_subset_length(
                  tvb, offset, nit_loop_len);
          nit_loop_offset = 0;
          if (!dvb_nit_handle) {
              call_dissector(data_handle, nit_loop_tvb, pinfo, tree);
              break;
          }
          /* prevent dvb_nit dissector from clearing the dvb-ci infos */
          col_append_str(pinfo->cinfo, COL_INFO, ", ");
          col_set_fence(pinfo->cinfo, COL_INFO);
          do {
              table_id = tvb_get_guint8(nit_loop_tvb, nit_loop_offset);
              if (table_id != TABLE_ID_CICAM_NIT) {
                  proto_tree_add_expert(tree, pinfo, &ei_dvbci_cicam_nit_table_id,
                          nit_loop_tvb, nit_loop_offset, 1);
              }
              nit_loop_partial_tvb =
                  tvb_new_subset_remaining(nit_loop_tvb, nit_loop_offset);
              dvb_nit_bytes = call_dissector(
                      dvb_nit_handle, nit_loop_partial_tvb, pinfo, tree);
              nit_loop_offset += dvb_nit_bytes;
              /* offsets go from 0 to nit_loop_len-1 */
          } while (dvb_nit_bytes>0 && nit_loop_offset<nit_loop_len-1);
          break;
        case T_OPERATOR_INFO:
          info_valid = ((tvb_get_guint8(tvb, offset) & 0x08) == 0x08);
          proto_tree_add_item(tree, hf_dvbci_info_valid,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item(tree, hf_dvbci_info_ver_op_info,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          if (!info_valid)
              break;
          offset++;
          proto_tree_add_item(tree, hf_dvbci_cicam_onid,
                  tvb, offset, 2, ENC_BIG_ENDIAN);
          offset += 2;
          proto_tree_add_item(tree, hf_dvbci_cicam_id,
                  tvb, offset, 4, ENC_BIG_ENDIAN);
          offset += 4;

          enc_len = dvb_analyze_string_charset(tvb, offset,
                  tvb_reported_length_remaining(tvb, offset), &encoding);
          if (enc_len==0) {
              proto_tree_add_expert(tree, pinfo,
                      &ei_dvbci_invalid_char_tbl, tvb, offset,
                      tvb_reported_length_remaining(tvb, offset));
              break;
          }
          dvb_add_chartbl(tree, hf_dvbci_opp_char_tbl,
                  tvb, offset, enc_len, encoding);
          offset += enc_len;

          proto_tree_add_item(tree, hf_dvbci_sdt_rst_trusted,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item(tree, hf_dvbci_eit_rst_trusted,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item(tree, hf_dvbci_eit_pf_usage,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item(tree, hf_dvbci_eit_sch_usage,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item(tree, hf_dvbci_ext_evt_usage,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          offset++;
          proto_tree_add_item(tree, hf_dvbci_sdt_oth_trusted,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item(tree, hf_dvbci_eit_evt_trigger,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          offset++;
          proto_tree_add_item(tree, hf_dvbci_opp_lang_code,
                  tvb, offset, 3, ENC_ASCII|ENC_NA);
          offset += 3;
          /* hf_dvbci_prof_name is an FT_UINT_STRING, one leading len byte */
          proto_tree_add_item(tree, hf_dvbci_prof_name,
              tvb, offset, 1, ENC_ASCII|ENC_BIG_ENDIAN);
          break;
        case T_OPERATOR_SEARCH_START:
          proto_tree_add_item(tree, hf_dvbci_unattended,
                  tvb, offset, 1, ENC_BIG_ENDIAN);

          cap_loop_len = tvb_get_guint8(tvb, offset) & 0x7F;
          proto_tree_add_item(tree, hf_dvbci_opp_svc_type_loop_len,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          offset++;
          /* no need for error checking, we continue anyway */
          dissect_opp_cap_loop(cap_loop_len, "Service type loop",
                  hf_dvbci_opp_svc_type, 1, tvb, offset, pinfo, tree);
          offset += cap_loop_len;

          cap_loop_len = tvb_get_guint8(tvb, offset);
          proto_tree_add_item(tree, hf_dvbci_dlv_cap_loop_len,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          offset++;
          /* XXX - handle multi-byte delivery capabilities */
          dissect_opp_cap_loop(cap_loop_len,
                  "Delivery system capabilities loop",
                  hf_dvbci_dlv_cap_byte, 1,
                  tvb, offset, pinfo, tree);
          offset += cap_loop_len;

          cap_loop_len = tvb_get_guint8(tvb, offset);
          proto_tree_add_item(tree, hf_dvbci_app_cap_loop_len,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          offset++;
          dissect_opp_cap_loop(cap_loop_len,
                  "Application capabilities loop",
                  hf_dvbci_app_cap_bytes, 2,
                  tvb, offset, pinfo, tree);
          break;
        case T_OPERATOR_TUNE_STATUS:
          desc_num = tvb_get_guint8(tvb, offset);
          pi = proto_tree_add_item(tree, hf_dvbci_desc_num,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          if (desc_num==OPP_NO_MORE_DESC)
              proto_item_append_text(pi, " (all descriptors were processed)");
          offset++;
          sig_strength = tvb_get_guint8(tvb, offset);
          proto_tree_add_item(tree, hf_dvbci_sig_strength,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          offset++;
          sig_qual = tvb_get_guint8(tvb, offset);
          pi = proto_tree_add_item(tree, hf_dvbci_sig_qual,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          if (sig_strength>100 || sig_qual>100) {
              expert_add_info(pinfo, pi, &ei_dvbci_sig_qual);
          }
          offset++;
          proto_tree_add_item(tree, hf_dvbci_opp_tune_status,
                  tvb, offset, 1, ENC_BIG_ENDIAN);
          dissect_desc_loop(hf_dvbci_opp_desc_loop_len,
                  tvb, offset, pinfo, tree);
          break;
        case T_OPERATOR_TUNE:
          dissect_desc_loop(hf_dvbci_opp_desc_loop_len,
                  tvb, offset, pinfo, tree);
          break;
        default:
          break;
    }
}


static void
dissect_dvbci_payload_sas(guint32 tag, gint len_field _U_,
        tvbuff_t *tvb, gint offset, circuit_t *circuit,
        packet_info *pinfo, proto_tree *tree)
{
    gchar   app_id_str[2+16+1]; /* "0x", string of 16 hex digits, trailing 0 */
    guint8  sas_status;
    dissector_handle_t msg_handle;
    guint8  msg_nb;
    guint16 msg_len;
    tvbuff_t *msg_tvb;

    switch(tag) {
        case T_SAS_CONNECT_RQST:
        case T_SAS_CONNECT_CNF:
            g_snprintf(app_id_str, sizeof(app_id_str),
                    "0x%016" G_GINT64_MODIFIER "x", tvb_get_ntoh64(tvb, offset));
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ", app_id_str);
            proto_tree_add_item(tree, hf_dvbci_sas_app_id,
                    tvb, offset, 8, ENC_BIG_ENDIAN);
            offset += 8;
            if (tag == T_SAS_CONNECT_CNF) {
                sas_status = tvb_get_guint8(tvb, offset);
                col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
                        (sas_status == SAS_SESS_STATE_CONNECTED ?
                         "Ok" : "Error"));
                proto_tree_add_item(tree, hf_dvbci_sas_sess_state,
                        tvb, offset, 1, ENC_BIG_ENDIAN);
                if (!circuit)
                    break;
                if (sas_status == SAS_SESS_STATE_CONNECTED) {
                    msg_handle = dissector_get_string_handle(
                            sas_msg_dissector_table, app_id_str);
                    /* this clears the dissector for msg_handle==NULL */
                    circuit_set_dissector(circuit, msg_handle);
                }
                else
                    circuit_set_dissector(circuit, NULL);
            }
            break;
        case T_SAS_ASYNC_MSG:
            msg_nb = tvb_get_guint8(tvb, offset);
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ",
                    "Message #%d ", msg_nb);
            proto_tree_add_item(tree, hf_dvbci_sas_msg_nb,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            offset++;
            msg_len = tvb_get_ntohs(tvb, offset);
            proto_tree_add_item(tree, hf_dvbci_sas_msg_len,
                    tvb, offset, 2, ENC_BIG_ENDIAN);
            offset += 2;
            msg_tvb = tvb_new_subset_length(tvb, offset, msg_len);
            msg_handle = circuit_get_dissector(circuit);
            if (msg_handle == NULL)
                msg_handle = data_handle;
            call_dissector(msg_handle, msg_tvb, pinfo, tree);
            break;
        default:
          break;
    }
}


static void
dissect_dvbci_apdu(tvbuff_t *tvb, circuit_t *circuit,
        packet_info *pinfo, proto_tree *tree, guint8 direction)
{
    proto_tree  *app_tree;
    guint32      apdu_len, tag, len_field;
    const gchar *tag_str;
    gint         offset;
    proto_item  *pi;
    apdu_info_t *ai;
    guint32      apdu_res_id;
    const gchar *ai_res_class_str;


    apdu_len = tvb_reported_length(tvb);

    app_tree = proto_tree_add_subtree(tree, tvb, 0, apdu_len, ett_dvbci_application, NULL, "Application Layer");

    tag = tvb_get_ntoh24(tvb, 0);
    tag_str = try_val_to_str(tag, dvbci_apdu_tag);
    offset = APDU_TAG_SIZE;

    col_set_str(pinfo->cinfo, COL_INFO,
            val_to_str_const(tag, dvbci_apdu_tag, "Unknown/invalid APDU"));
    pi = proto_tree_add_item(
                app_tree, hf_dvbci_apdu_tag, tvb, 0, APDU_TAG_SIZE, ENC_BIG_ENDIAN);
    if (tag_str == NULL) {
        expert_add_info(pinfo, pi, &ei_dvbci_apdu_tag);
        return;
    }

    offset = dissect_ber_length(pinfo, app_tree, tvb, offset, &len_field, NULL);
    if ((offset+len_field) != apdu_len) {
        proto_tree_add_expert_format(app_tree, pinfo, &ei_dvbci_bad_length, tvb,
                APDU_TAG_SIZE, offset-APDU_TAG_SIZE,
                "Length field is different from the number of apdu payload bytes");
        /* we need len_field bytes of apdu payload to call
           ai->dissect_payload() and continue dissecting */
        if (apdu_len < offset+len_field)
            return;
    }

    ai = (apdu_info_t *)g_hash_table_lookup(apdu_table,
                                            GUINT_TO_POINTER((guint)tag));
    if (!ai) {
        proto_tree_add_expert(app_tree, pinfo, &ei_dvbci_apdu_not_supported, tvb, 0, APDU_TAG_SIZE);
        return;
    }
    if (ai->direction!=DIRECTION_ANY && ai->direction!=direction) {
        if (ai->direction==DATA_HOST_TO_CAM) {
            proto_tree_add_expert(app_tree, pinfo, &ei_dvbci_apu_host_to_cam, tvb, 0, APDU_TAG_SIZE);
        }
        else {
            proto_tree_add_expert(app_tree, pinfo, &ei_dvbci_apu_cam_to_host, tvb, 0, APDU_TAG_SIZE);
        }
        /* don't return, we can continue dissecting the APDU */
    }
    if (ai->min_len_field!=LEN_FIELD_ANY && len_field<ai->min_len_field) {
        proto_tree_add_expert_format(app_tree, pinfo, &ei_dvbci_bad_length, tvb, 0, APDU_TAG_SIZE,
                "Invalid APDU length field, minimum length field for %s is %d", tag_str, ai->min_len_field);
        return;
    }
    if (ai->len_field!=LEN_FIELD_ANY && len_field!=ai->len_field) {
        proto_tree_add_expert_format(app_tree, pinfo, &ei_dvbci_bad_length, tvb, 0, APDU_TAG_SIZE,
                "Invalid APDU length field, length field for %s must be %d", tag_str, ai->len_field);
        return;
    }
    if (circuit) {
        apdu_res_id = GPOINTER_TO_UINT(
                (gpointer)circuit_get_proto_data(circuit, proto_dvbci));

        ai_res_class_str = val_to_str_const(ai->res_class, dvbci_res_class, "Unknown");

        if(RES_CLASS(apdu_res_id) != ai->res_class) {
            proto_tree_add_expert_format(app_tree, pinfo, &ei_dvbci_res_class, tvb, 0, APDU_TAG_SIZE,
                    "Invalid resource class for this apdu, %s can only be sent on a %s session",
                    tag_str, ai_res_class_str);
        }
        if(RES_VER(apdu_res_id) < ai->res_min_ver) {
            proto_tree_add_expert_format(app_tree, pinfo, &ei_dvbci_res_ver, tvb, 0, APDU_TAG_SIZE,
                    "Invalid resource version for this apdu, %s apdu requires at least %s version %d",
                    tag_str, ai_res_class_str, ai->res_min_ver);
        }
        /* don't return, we can continue dissecting the APDU */
    }
    if (ai->len_field!=0) {
        if (!ai->dissect_payload) {
            /* don't display an error, getting here means we have illegal
             * data in apdu_info[] */
            return;
        }
        ai->dissect_payload(
                tag, len_field, tvb, offset, circuit, pinfo, app_tree);
    }
}

static void
dissect_dvbci_spdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        guint8 direction, guint8 tcid)
{
    guint32            spdu_len;
    proto_item        *ti;
    proto_tree        *sess_tree;
    guint8             tag;
    const gchar       *tag_str;
    circuit_t         *circuit     = NULL;
    proto_item        *pi;
    gint               offset;
    guint32            len_field;
    const spdu_info_t *si;
    proto_item        *res_id_it   = NULL;
    guint32            res_id;
    guint16 ssnb                   = 0;  /* session numbers start with 1, 0 is invalid */
    guint8             sess_stat;
    tvbuff_t          *payload_tvb = NULL;


    spdu_len = tvb_reported_length(tvb);

    sess_tree = proto_tree_add_subtree(tree, tvb, 0, -1, ett_dvbci_session, &ti, "Session Layer");

    tag = tvb_get_guint8(tvb,0);
    tag_str = try_val_to_str(tag, dvbci_spdu_tag);
    col_set_str(pinfo->cinfo, COL_INFO,
            val_to_str_const(tag, dvbci_spdu_tag, "Invalid SPDU"));
    pi = proto_tree_add_item(sess_tree, hf_dvbci_spdu_tag, tvb, 0, 1, ENC_BIG_ENDIAN);
    if (tag_str == NULL) {
        expert_add_info(pinfo, pi, &ei_dvbci_spdu_tag);
        return;
    }

    offset = dissect_ber_length(pinfo, sess_tree, tvb, 1, &len_field, NULL);

    si = (spdu_info_t *)g_hash_table_lookup(spdu_table,
                                            GUINT_TO_POINTER((guint)tag));
    if (!si)
        return;
    if (si->direction!=0 && si->direction!=direction) {
        if (si->direction==DATA_HOST_TO_CAM) {
            proto_tree_add_expert(sess_tree, pinfo, &ei_dvbci_spdu_host_to_cam, tvb, 0, 1);
        }
        else {
            proto_tree_add_expert(sess_tree, pinfo, &ei_dvbci_spdu_cam_to_host, tvb, 0, 1);
        }
    }
    if (si->len_field != len_field) {
        /* offset points to 1st byte after the length field */
        proto_tree_add_expert_format(sess_tree, pinfo, &ei_dvbci_bad_length, tvb, 1, offset-1,
                "Invalid SPDU length field, correct length field for %s is %d", tag_str, si->len_field);
        return;
    }

    switch(tag)
    {
        case T_OPEN_SESSION_REQUEST:
            res_id_it = dissect_res_id(tvb, offset, pinfo, sess_tree, 0, TRUE);
            break;
        case T_CREATE_SESSION:
            res_id_it = dissect_res_id(tvb, offset, pinfo, sess_tree, 0, TRUE);
            /* DVB-CI uses network byte order == big endian */
            ssnb = tvb_get_ntohs(tvb, offset+RES_ID_LEN);
            proto_tree_add_item(sess_tree, hf_dvbci_sess_nb,
                    tvb, offset+RES_ID_LEN, 2, ENC_BIG_ENDIAN);
            break;
        case T_OPEN_SESSION_RESPONSE:
        case T_CREATE_SESSION_RESPONSE:
            sess_stat = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(sess_tree, hf_dvbci_sess_status,
                    tvb, offset, 1, ENC_BIG_ENDIAN);
            res_id = tvb_get_ntohl(tvb, offset+1);
            res_id_it = dissect_res_id(tvb, offset+1, pinfo, sess_tree, 0, TRUE);
            ssnb = tvb_get_ntohs(tvb, offset+1+RES_ID_LEN);
            proto_tree_add_item(sess_tree, hf_dvbci_sess_nb, tvb,
                    offset+1+RES_ID_LEN, 2, ENC_BIG_ENDIAN);
            if (sess_stat != SESS_OPENED) {
                col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "Error");
                break;
            }
            col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "Session opened");
            circuit = circuit_new(CT_DVBCI, CT_ID(ssnb, tcid), pinfo->num);
            if (circuit) {
                /* we always add the resource id immediately after the circuit
                   was created */
                circuit_add_proto_data(
                        circuit, proto_dvbci, GUINT_TO_POINTER(res_id));
            }
            break;
        case T_CLOSE_SESSION_REQUEST:
            ssnb = tvb_get_ntohs(tvb, offset);
            proto_tree_add_item(
                    sess_tree, hf_dvbci_sess_nb, tvb,
                    offset, 2, ENC_BIG_ENDIAN);
            break;
        case T_CLOSE_SESSION_RESPONSE:
            sess_stat = tvb_get_guint8(tvb, offset);
            proto_tree_add_item(
                    sess_tree, hf_dvbci_close_sess_status, tvb,
                    offset, 1, ENC_BIG_ENDIAN);
            col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
                    sess_stat==SESS_CLOSED ? "Session closed" : "Error");
            ssnb = tvb_get_ntohs(tvb, offset+1);
            proto_tree_add_item(sess_tree, hf_dvbci_sess_nb,
                    tvb, offset+1, 2, ENC_BIG_ENDIAN);
            circuit = find_circuit(CT_DVBCI, CT_ID(ssnb, tcid), pinfo->num);
            if (circuit)
                close_circuit(circuit, pinfo->num);
            break;
        case T_SESSION_NUMBER:
            ssnb = tvb_get_ntohs(tvb, offset);
            proto_tree_add_item(
                    sess_tree, hf_dvbci_sess_nb, tvb,
                    offset, 2, ENC_BIG_ENDIAN);
            payload_tvb =
                tvb_new_subset_remaining(tvb, offset+2);
            break;
        default:
            break;
    }

    if (ssnb && !circuit)
        circuit = find_circuit(CT_DVBCI, CT_ID(ssnb, tcid), pinfo->num);

    /* if the packet contains no resource id, we add the cached id from
       the circuit so that each packet has a resource id that can be
       used for filtering */
    if (circuit && !res_id_it) {
        /* when a circuit is found, it always contains a valid resource id */
        res_id = GPOINTER_TO_UINT(
                (gpointer)circuit_get_proto_data(circuit, proto_dvbci));
        res_id_it = dissect_res_id(NULL, 0, pinfo, sess_tree, res_id, TRUE);
        PROTO_ITEM_SET_GENERATED(res_id_it);
    }

    if (payload_tvb) {
        proto_item_set_len(ti, spdu_len-tvb_reported_length(payload_tvb));
        dissect_dvbci_apdu(payload_tvb, circuit, pinfo, tree, direction);
    }
    else {
        proto_item_set_len(ti, spdu_len);
    }
}

/* dissect the status of an r_tpdu, return its length or -1 for error */
static gint
dissect_dvbci_tpdu_status(tvbuff_t *tvb, gint offset,
        packet_info *pinfo, proto_tree *tree,
        guint8 lpdu_tcid, guint8 r_tpdu_tag)
{
    gint         offset_new, len_start_offset;
    guint8       tag;
    guint32      len_field;
    guint8       t_c_id, sb_value;
    const gchar *sb_str;
    proto_item  *pi;

    offset_new = offset;

    tag = tvb_get_guint8(tvb, offset_new);
    if (tag!=T_SB) {
        proto_tree_add_expert(tree, pinfo, &ei_dvbci_tpdu_status_tag, tvb, offset_new, 1);
        return -1;
    }
    proto_tree_add_item(tree, hf_dvbci_sb_tag, tvb,
            offset_new, 1, ENC_BIG_ENDIAN);
    col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, "T_SB");
    offset_new++;

    len_start_offset = offset_new;
    offset_new = dissect_ber_length(
            pinfo, tree, tvb, offset_new, &len_field, NULL);
    if (len_field != 2) {
        proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb, len_start_offset, offset_new-len_start_offset,
                "Invalid status length field, this must always be 2");

        return -1;
    }

    t_c_id = tvb_get_guint8(tvb, offset_new);
    pi = proto_tree_add_item(tree, hf_dvbci_t_c_id, tvb, offset_new, 1, ENC_BIG_ENDIAN);
    /* tcid in transport header and link layer must only match for data
     * transmission commands */
    if (t_c_id!=lpdu_tcid) {
        if (r_tpdu_tag==NO_TAG ||
                r_tpdu_tag==T_DATA_MORE || r_tpdu_tag==T_DATA_LAST) {
            expert_add_info_format(pinfo, pi, &ei_dvbci_t_c_id,
                    "Transport Connection ID mismatch, tcid is %d in the transport layer and %d in the link layer",
                    t_c_id, lpdu_tcid);
            return -1;
        }
    }
    offset_new++;

    sb_value = tvb_get_guint8(tvb, offset_new);
    sb_str = try_val_to_str(sb_value, dvbci_sb_value);
    pi = proto_tree_add_item(tree, hf_dvbci_sb_value, tvb,
            offset_new, 1, ENC_BIG_ENDIAN);
    if (sb_str) {
        col_append_sep_str(pinfo->cinfo, COL_INFO, ": ", sb_str);
    }
    else {
        expert_add_info(pinfo, pi, &ei_dvbci_sb_value);
    }
    offset_new++;

    return offset_new-offset;
}


/* dissect the header of a c_tpdu or r_tpdu
   return the length of the header (tag, len_field, t_c_id) or -1 for error */
static gint
dissect_dvbci_tpdu_hdr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        guint8 direction, guint8 lpdu_tcid, guint32 tpdu_len,
        guint8 *hdr_tag, guint32 *body_len)
{
    guint8       c_tpdu_tag, r_tpdu_tag, *tag = NULL;
    const gchar *c_tpdu_str, *r_tpdu_str;
    proto_item  *pi;
    gint         offset;
    guint32      len_field;
    guint8       t_c_id;

    if (direction==DATA_HOST_TO_CAM) {
        c_tpdu_tag = tvb_get_guint8(tvb, 0);
        tag = &c_tpdu_tag;
        c_tpdu_str = try_val_to_str(c_tpdu_tag, dvbci_c_tpdu);
        pi = proto_tree_add_item(tree, hf_dvbci_c_tpdu_tag, tvb, 0, 1, ENC_BIG_ENDIAN);
        if (c_tpdu_str) {
            col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, c_tpdu_str);
        }
        else {
            col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
                    "Invalid Command-TPDU tag");
            expert_add_info(pinfo, pi, &ei_dvbci_c_tpdu_tag);
            return -1;
        }
    }
    else {
        r_tpdu_tag = tvb_get_guint8(tvb, 0);
        if (r_tpdu_tag == T_SB) {
            /* we have an r_tpdu without header and body,
               it contains only the status part */
            if (hdr_tag)
                *hdr_tag = NO_TAG;
            if (body_len)
                *body_len = 0;
            return 0;
        }

        tag = &r_tpdu_tag;
        r_tpdu_str = try_val_to_str(r_tpdu_tag, dvbci_r_tpdu);
        pi = proto_tree_add_item(tree, hf_dvbci_r_tpdu_tag, tvb, 0, 1, ENC_BIG_ENDIAN);
        if (r_tpdu_str) {
            col_append_sep_str(pinfo->cinfo, COL_INFO, NULL, r_tpdu_str);
        }
        else {
            col_append_sep_str(pinfo->cinfo, COL_INFO, NULL,
                    "Invalid Response-TPDU tag");
            expert_add_info(pinfo, pi, &ei_dvbci_r_tpdu_tag);
            return -1;
        }
    }

    offset = dissect_ber_length(pinfo, tree, tvb, 1, &len_field, NULL);
    /* len_field must be at least 1 for the following t_c_id
       c_tpdu's len_field must match tvbuff exactly
       r_tpdu's len_field does not include the status part after the body */
    if (len_field==0 ||
        ((direction==DATA_HOST_TO_CAM) && ((offset+len_field)!=tpdu_len)) ||
        ((direction==DATA_CAM_TO_HOST) && ((offset+len_field)>tpdu_len))) {
        /* offset points to 1st byte after the length field */
        proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_bad_length, tvb, 1, offset-1,
            len_field==0 ? "Invalid length field, length field must be at least 1" :
                           "Invalid length field, length field mismatch");
        return -1;
    }

    t_c_id = tvb_get_guint8(tvb, offset);
    pi = proto_tree_add_item(tree, hf_dvbci_t_c_id, tvb, offset, 1, ENC_BIG_ENDIAN);
    /* tcid in transport header and link layer must only match for
     * data transmission commands */
    if (t_c_id!=lpdu_tcid) {
        if (tag && (*tag==T_RCV || *tag==T_DATA_MORE || *tag==T_DATA_LAST)) {
            expert_add_info_format(pinfo, pi, &ei_dvbci_t_c_id, "Transport Connection ID mismatch, tcid is %d in the transport layer and %d in the link layer",
                    t_c_id, lpdu_tcid);
        }
    }
    else {
        col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ", "tcid %d", t_c_id);
    }
    offset++;

    if (hdr_tag && tag)
        *hdr_tag = *tag;
    if (body_len)
        *body_len = len_field-1;  /* -1 for t_c_id */
    return offset;
}

static void
dissect_dvbci_tpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        guint8 direction, guint8 lpdu_tcid)
{
    guint32        tpdu_len, body_len;
    proto_item    *ti;
    proto_tree    *trans_tree;
    gint           offset, status_len;
    guint8         hdr_tag                = NO_TAG;
    tvbuff_t      *body_tvb, *payload_tvb = NULL;
    fragment_head *frag_msg               = NULL;


    tpdu_len = tvb_reported_length(tvb);

    col_clear(pinfo->cinfo, COL_INFO);

    trans_tree = proto_tree_add_subtree(tree, tvb, 0, -1, ett_dvbci_transport, &ti, "Transport Layer");

    offset = dissect_dvbci_tpdu_hdr(tvb, pinfo, trans_tree, direction,
            lpdu_tcid, tpdu_len, &hdr_tag, &body_len);
    if (offset==-1)
        return;
    proto_item_set_len(ti, offset);
    if ((offset>0) && (body_len!=0)) {
        /* for unfragmented data, the reassembly api behaviour is unclear
            if we put the body part of the tvb into fragment_add_seq_next(),
            process_reassembled_data() returns the remainder of the tvb
            which is body|status part
           if there's more than one fragment, payload_tvb contains only
            the reassembled bodies as expected
           to work around this issue, we use a dedicated body_tvb as
            input to reassembly routines */
        body_tvb = tvb_new_subset_length(tvb, offset, body_len);
        /* dissect_dvbci_tpdu_hdr() checked that lpdu_tcid==t_c_id */
        frag_msg = fragment_add_seq_next(&spdu_reassembly_table,
                body_tvb, 0, pinfo, SPDU_SEQ_ID_BASE+lpdu_tcid, NULL,
                body_len,
                hdr_tag == T_DATA_MORE ? 1 : 0);
        payload_tvb = process_reassembled_data(body_tvb, 0, pinfo,
                "Reassembled SPDU", frag_msg, &spdu_frag_items,
                NULL, trans_tree);
        if (!payload_tvb) {
            if (hdr_tag == T_DATA_MORE) {
                pinfo->fragmented = TRUE;
                col_append_str(pinfo->cinfo, COL_INFO, " (Message fragment)");
            } else {
                payload_tvb = body_tvb;
            }
        }
        offset += body_len;
    }

    if (direction==DATA_CAM_TO_HOST) {
        /* minimum length of an rtpdu status is 4 bytes */
        if (tpdu_len-offset < 4) {
            proto_tree_add_expert(trans_tree, pinfo, &ei_dvbci_r_tpdu_status_mandatory, tvb, 0, 0);
            return;
        }
        status_len = dissect_dvbci_tpdu_status(
                tvb, offset, pinfo, trans_tree, lpdu_tcid, hdr_tag);
        if (status_len<0)
            return;
        proto_tree_set_appendix(trans_tree, tvb, offset, status_len);
    }

    if (payload_tvb)
        dissect_dvbci_spdu(payload_tvb, pinfo, tree, direction, lpdu_tcid);
}


static void
dissect_dvbci_lpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        guint8 direction)
{
    proto_tree    *link_tree;
    guint32        payload_len;
    guint8         tcid, more_last;
    proto_item    *pi;
    tvbuff_t      *payload_tvb;
    fragment_head *frag_msg;


    payload_len = tvb_reported_length(tvb);

    col_set_str(pinfo->cinfo, COL_INFO, "LPDU");

    link_tree = proto_tree_add_subtree(tree, tvb, 0, 2, ett_dvbci_link, NULL, "Link Layer");

    tcid = tvb_get_guint8(tvb, 0);
    col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ", "tcid %d", tcid);
    proto_tree_add_item(link_tree, hf_dvbci_tcid, tvb, 0, 1, ENC_BIG_ENDIAN);

    more_last = tvb_get_guint8(tvb, 1);
    pi = proto_tree_add_item(link_tree, hf_dvbci_ml, tvb, 1, 1, ENC_BIG_ENDIAN);
    if (try_val_to_str(more_last, dvbci_ml) == NULL) {
        expert_add_info(pinfo, pi, &ei_dvbci_ml);
    }

    /* buf_size_host==0 -> we did not capture the buffer size negotiation */
    if (buf_size_host!=0 && payload_len>buf_size_host) {
        proto_tree_add_expert_format(link_tree, pinfo, &ei_dvbci_bad_length, tvb, 2, payload_len,
                "Payload too large, maximum payload length is the negotiated buffer size (%d bytes)", buf_size_host);
    }

    frag_msg = fragment_add_seq_next(&tpdu_reassembly_table,
            tvb, 2, pinfo, TPDU_SEQ_ID_BASE+tcid, NULL,
            tvb_reported_length_remaining(tvb, 2),
            more_last == ML_MORE ? 1 : 0);

    payload_tvb = process_reassembled_data(tvb, 2, pinfo,
            "Reassembled TPDU", frag_msg, &tpdu_frag_items,
            NULL, link_tree);
    if (!payload_tvb) {
        if (more_last == ML_MORE) {
            pinfo->fragmented = TRUE;
            col_append_str(pinfo->cinfo, COL_INFO, " (Message fragment)");
       } else
            payload_tvb = tvb_new_subset_remaining(tvb, 2);
    }
    if (payload_tvb)
        dissect_dvbci_tpdu(payload_tvb, pinfo, tree, direction, tcid);
}

/* dissect DVB-CI buffer size negotiation */
static void
dissect_dvbci_buf_neg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        guint8 direction)
{
    guint16     buf_size;
    proto_item *pi;

    buf_size = tvb_get_ntohs(tvb, 0);

    col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %u bytes",
                 direction == DATA_HOST_TO_CAM ?
                 "negotiated buffer size" : "buffer size proposal",
                 buf_size);

    if (direction == DATA_HOST_TO_CAM) {
        buf_size_host = buf_size;
        pi = proto_tree_add_uint_format(tree, hf_dvbci_buf_size, tvb,
                0, 2, buf_size,
                "Negotiated buffer size: %u bytes", buf_size);
        if (buf_size_host > buf_size_cam) {
            /* ATTENTION:
               wireshark may run through each packet multiple times
               if we didn't check the direction, we'd get the error when
               wireshark runs through the initial CAM packet for the 2nd time
             */
            expert_add_info_format(pinfo, pi, &ei_dvbci_buf_size, "Illegal buffer size command. Host shall not request a buffer size larger than the CAM proposal");
        }
    }
    else if (direction == DATA_CAM_TO_HOST) {
        buf_size_cam = buf_size;
        proto_tree_add_uint_format(tree, hf_dvbci_buf_size, tvb,
                0, 2, buf_size,
                "Buffer size proposal by the CAM: %u bytes", buf_size);
    }

    if (buf_size < 16) {
        proto_tree_add_expert_format(tree, pinfo, &ei_dvbci_buf_size, tvb, 0, 2,
               "Illegal buffer size, minimum buffer size is 16 bytes");
    }
}

/* dissect Level 1 version/product information tuple's payload
   data_tvb is a separate tvb for the tuple payload (without tag and len)
   return the number of dissected bytes or -1 for error */
static gint
dissect_dvbci_cis_payload_tpll_v1(tvbuff_t *data_tvb,
        packet_info *pinfo _U_, proto_tree *tree)
{
    gint offset=0, offset_str_end;

    /* the CIS is defined by PCMCIA, all multi-byte values are little endian
       (the rest of DVB-CI is a big-endian protocol) */
    proto_tree_add_item(tree, hf_dvbci_cis_tpll_v1_major,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;
    proto_tree_add_item(tree, hf_dvbci_cis_tpll_v1_minor,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;

    /* manufacturer, name and additional infos are 0-terminated strings */
    offset_str_end = tvb_find_guint8(data_tvb, offset, -1, 0x0);
    if (offset_str_end<offset) /* offset_str_end==offset is ok */
        return offset;
    proto_tree_add_item(tree, hf_dvbci_cis_tpll_v1_info_manuf,
            data_tvb, offset, offset_str_end-offset, ENC_ASCII|ENC_NA);
    offset = offset_str_end+1; /* +1 for 0 termination */

    offset_str_end = tvb_find_guint8(data_tvb, offset, -1, 0x0);
    if (offset_str_end<offset)
        return offset;
    proto_tree_add_item(tree, hf_dvbci_cis_tpll_v1_info_name,
            data_tvb, offset, offset_str_end-offset, ENC_ASCII|ENC_NA);
    offset = offset_str_end+1;

    /* the pc-card spec mentions two additional info strings,
        it's unclear if both are mandatory
       >1 because the last byte is the tuple end marker */
    while (tvb_reported_length_remaining(data_tvb, offset)>1) {
        offset_str_end = tvb_find_guint8(data_tvb, offset, -1, 0x0);
        if (offset_str_end<offset)
            break;
        proto_tree_add_item(tree, hf_dvbci_cis_tpll_v1_info_additional,
                data_tvb, offset, offset_str_end-offset, ENC_ASCII|ENC_NA);
        offset = offset_str_end+1;
    }

    proto_tree_add_item(tree, hf_dvbci_cis_tpll_v1_end,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;

    return offset;
}

static gint
dissect_dvbci_cis_payload_config(tvbuff_t *data_tvb,
        packet_info *pinfo _U_, proto_tree *tree)
{
    gint         offset = 0;
    /* these are the actual sizes, the CIS stores rmsz-1 and rasz-1 */
    guint8       rfsz, rmsz, rasz;
    guint8       st_code, st_len;
    const gchar *st_code_str;
    proto_item  *st_item = NULL;
    proto_tree  *st_tree = NULL;
    guint8       stci_ifn_size;   /* actual size, see comment above */

    rfsz = (tvb_get_guint8(data_tvb, offset)&0xC0) >> 6;
    proto_tree_add_item(tree, hf_dvbci_cis_tpcc_rfsz,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    rmsz = ((tvb_get_guint8(data_tvb, offset)&0x3C) >> 2) + 1;
    proto_tree_add_item(tree, hf_dvbci_cis_tpcc_rmsz,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    rasz = (tvb_get_guint8(data_tvb, offset)&0x03) + 1;
    proto_tree_add_item(tree, hf_dvbci_cis_tpcc_rasz,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;

    proto_tree_add_item(tree, hf_dvbci_cis_tpcc_last,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;

    proto_tree_add_item(tree, hf_dvbci_cis_tpcc_radr,
            data_tvb, offset, rasz, ENC_LITTLE_ENDIAN);
    offset += rasz;
    proto_tree_add_item(tree, hf_dvbci_cis_tpcc_rmsk,
            data_tvb, offset, rmsz, ENC_NA);
    offset += rmsz;
    offset += rfsz; /* skip reserved bytes */

    while (tvb_reported_length_remaining(data_tvb, offset) > 0) {
        st_code = tvb_get_guint8(data_tvb, offset);
        st_code_str = val_to_str_const(st_code, dvbci_cis_subtpl_code, "unknown");
        st_tree = proto_tree_add_subtree_format(tree, data_tvb, offset, -1,
                ett_dvbci_cis_subtpl, &st_item, "Subtuple: %s (0x%x)", st_code_str, st_code);
        proto_tree_add_item(st_tree, hf_dvbci_cis_st_code,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
        offset++;
        st_len = tvb_get_guint8(data_tvb, offset);
        proto_item_set_len(st_item, 2+st_len); /* tag, len byte, body */
        proto_tree_add_item(st_tree, hf_dvbci_cis_st_len,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
        offset++;
        if (st_code == CCSTPL_CIF) {
            stci_ifn_size = ((tvb_get_guint8(data_tvb, offset) & 0xC0)>>6)+1;
            proto_tree_add_item(st_tree, hf_dvbci_cis_stci_ifn_size,
                    data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
            /* don't increment offset,
               size and actual value's LSB share the same byte */
            proto_tree_add_item(st_tree, hf_dvbci_cis_stci_ifn,
                    data_tvb, offset, stci_ifn_size, ENC_LITTLE_ENDIAN);
            offset += stci_ifn_size;
            /* the stci_str field could consist of multiple strings,
               this case is not supported for now */
            proto_tree_add_item(st_tree, hf_dvbci_cis_stci_str,
                    data_tvb, offset, st_len-stci_ifn_size, ENC_ASCII|ENC_NA);
            offset += st_len-stci_ifn_size;
        }
        else {
            /* skip unknown subtuple's content */
            offset += st_len;
        }
    }

    return offset;
}


static gint
dissect_dvbci_cis_payload_cftable_entry(tvbuff_t *data_tvb,
        packet_info *pinfo _U_, proto_tree *tree)
{
    gint offset = 0;
    gboolean intface_flag;

    intface_flag = ((tvb_get_guint8(data_tvb, offset) & 0x80) == 0x80);
    /* tpce_indx byte */
    proto_tree_add_item(tree, hf_dvbci_cis_tpce_indx_intface,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_cis_tpce_indx_default,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_cis_tpce_indx_cnf_entry,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;

    if (intface_flag) {
        /* tpce_if byte */
        proto_tree_add_item(tree, hf_dvbci_cis_tpce_if_type,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
        /* XXX parse other components of tpce_if */
        offset++;
    }

    /* tpce_fs byte: this is present in any case */
    proto_tree_add_item(tree, hf_dvbci_cis_tpce_fs_mem_space,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_cis_tpce_fs_irq,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_cis_tpce_fs_io,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    /* XXX parse other components of tpce_fs */
    offset++;

    return offset;
}

/* dissect the payload of a device_oc or device_oa tuple */
static gint
dissect_dvbci_cis_payload_device(tvbuff_t *data_tvb,
        packet_info *pinfo _U_, proto_tree *tree)
{
    gint      offset = 0;
    gboolean  ext;

    ext = ((tvb_get_guint8(data_tvb, offset) & 0x80) == 0x80);

    proto_tree_add_item(tree, hf_dvbci_cis_dev_vcc_used,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    proto_tree_add_item(tree, hf_dvbci_cis_dev_mwait,
            data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
    offset++;

    while (ext) {
        ext = ((tvb_get_guint8(data_tvb, offset) & 0x80) == 0x80);

        proto_tree_add_item(tree, hf_dvbci_cis_dev_oth_cond_info,
                data_tvb, offset, 1, ENC_LITTLE_ENDIAN);
        offset++;
    }

    return offset;
}

static void
dissect_dvbci_cis(tvbuff_t *tvb, gint offset,
        packet_info *pinfo, proto_tree *tree)
{
    gint         offset_start;
    proto_tree  *cis_tree = NULL, *tpl_tree = NULL;
    proto_item  *ti_main, *ti_tpl;
    guint8       tpl_code;
    const gchar *tpl_code_str = NULL;
    guint8       len_field;
    tvbuff_t    *tpl_data_tvb;

    offset_start = offset;

    cis_tree = proto_tree_add_subtree(tree, tvb, offset, -1,
            ett_dvbci_cis, &ti_main, "Card Information Structure (CIS)");

    do {
        tpl_code = tvb_get_guint8(tvb, offset);
        tpl_code_str = val_to_str_const(tpl_code, dvbci_cis_tpl_code, "unknown");

        tpl_tree = proto_tree_add_subtree_format(cis_tree,
                tvb, offset, -1, ett_dvbci_cis_tpl, &ti_tpl, "CIS tuple: %s", tpl_code_str);

        proto_tree_add_uint_format(tpl_tree, hf_dvbci_cis_tpl_code,
                tvb, offset, 1, tpl_code, "Tuple code: %s (0x%x)",
                tpl_code_str, tpl_code);
        offset++;

        if (tpl_code == CISTPL_END) {
            proto_item_set_len(ti_tpl, 1); /* only tag (no len and content) */
            break;
        }

        len_field = tvb_get_guint8(tvb, offset);
        proto_tree_add_item(tpl_tree, hf_dvbci_cis_tpl_len,
                tvb, offset, 1, ENC_LITTLE_ENDIAN);
        offset++;

        tpl_data_tvb = tvb_new_subset_length(tvb, offset, len_field);
        switch (tpl_code) {
            case CISTPL_VERS_1:
                dissect_dvbci_cis_payload_tpll_v1(
                        tpl_data_tvb, pinfo, tpl_tree);
                offset += len_field;
                break;
            case CISTPL_CONFIG:
                dissect_dvbci_cis_payload_config(tpl_data_tvb, pinfo, tpl_tree);
                offset += len_field;
                break;
            case CISTPL_CFTABLE_ENTRY:
                dissect_dvbci_cis_payload_cftable_entry(
                        tpl_data_tvb, pinfo, tpl_tree);
                offset += len_field;
                break;
            case CISTPL_DEVICE_OC:
                /* fall through: those two tuples' data is identical */
            case CISTPL_DEVICE_OA:
                dissect_dvbci_cis_payload_device(
                        tpl_data_tvb, pinfo, tpl_tree);
                offset += len_field;
                break;
            case CISTPL_MANFID:
                proto_tree_add_item(tpl_tree, hf_dvbci_cis_tplmid_manf,
                        tvb, offset, 2, ENC_LITTLE_ENDIAN);
                offset+=2;
                proto_tree_add_item(tpl_tree, hf_dvbci_cis_tplmid_card,
                        tvb, offset, 2, ENC_LITTLE_ENDIAN);
                offset+=2;
                break;
            default:
                if (len_field>0) {
                    proto_tree_add_item(tpl_tree, hf_dvbci_cis_tpl_data,
                            tvb, offset, len_field, ENC_NA);
                }
                offset += len_field;
                break;
        }

        proto_item_set_len(ti_tpl, 2+len_field); /* tag, len byte, content */

    } while (tvb_reported_length_remaining(tvb, offset) > 0);

    proto_item_set_len(ti_main, offset-offset_start);
}


static int
dissect_dvbci(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    gint         packet_len, offset = 0, offset_ver, offset_evt, offset_len_field;
    guint8       version, event;
    const gchar *event_str;
    guint16      len_field;
    proto_item  *ti;
    proto_tree  *dvbci_tree, *hdr_tree;
    tvbuff_t    *payload_tvb;
    guint16      cor_addr;
    guint8       cor_value;
    proto_item  *pi;
    guint8       hw_event;

    if (tvb_captured_length(tvb) < 4)
        return 0;

    offset_ver = offset;
    version = tvb_get_guint8(tvb, offset++);
    if (version != 0)
        return 0;

    offset_evt = offset;
    event = tvb_get_guint8(tvb, offset++);
    event_str = try_val_to_str(event, dvbci_event);
    if (!event_str)
        return 0;

    packet_len = tvb_reported_length(tvb);
    offset_len_field = offset;
    len_field = tvb_get_ntohs(tvb, offset);
    if (len_field != (packet_len-4))
        return 0;
    offset += 2;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "DVB-CI");
    col_set_str(pinfo->cinfo, COL_INFO, event_str);

    ti = proto_tree_add_protocol_format(tree, proto_dvbci,
            tvb, 0, packet_len, "DVB Common Interface: %s", event_str);
    dvbci_tree = proto_item_add_subtree(ti, ett_dvbci);
    hdr_tree = proto_tree_add_subtree(dvbci_tree,
            tvb, 0, offset, ett_dvbci_hdr, NULL, "Pseudo header");
    proto_tree_add_item(hdr_tree, hf_dvbci_hdr_ver,
            tvb, offset_ver, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(hdr_tree, hf_dvbci_event,
            tvb, offset_evt, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(hdr_tree, hf_dvbci_len,
            tvb, offset_len_field, 2, ENC_BIG_ENDIAN);

    if (IS_DATA_TRANSFER(event)) {
        dvbci_set_addrs(event, pinfo);

        payload_tvb = tvb_new_subset_remaining(tvb, offset);
        if (len_field == 2) {
            dissect_dvbci_buf_neg(payload_tvb, pinfo, dvbci_tree, event);
        }
        else {
            dissect_dvbci_lpdu(payload_tvb, pinfo, dvbci_tree, event);
        }
    }
    else if (event==DVBCI_EVT_COR_WRITE) {
        /* PCAP format for DVB-CI defines COR address as big endian */
        pi = proto_tree_add_item(dvbci_tree, hf_dvbci_cor_addr,
                tvb, offset, 2, ENC_BIG_ENDIAN);
        cor_addr = tvb_get_ntohs(tvb, offset);
        if (cor_addr == 0xFFFF) {
            proto_item_append_text(pi, " (COR address is unknown)");
            col_append_sep_str(pinfo->cinfo, COL_INFO, ": ", "unknown address");
        }
        else if (cor_addr > 0xFFE) {
            expert_add_info(pinfo, pi, &ei_dvbci_cor_addr);
        }
        else {
            col_append_sep_fstr(pinfo->cinfo, COL_INFO, ": ",
                "address 0x%x", cor_addr);
        }
        offset += 2;
        cor_value = tvb_get_guint8(tvb, offset);
        proto_tree_add_item(dvbci_tree, hf_dvbci_cor_val,
                tvb, offset, 1, ENC_BIG_ENDIAN);
        col_append_sep_fstr(pinfo->cinfo, COL_INFO, NULL,
            "value 0x%x", cor_value);
    }
    else if (event==DVBCI_EVT_CIS_READ) {
        dissect_dvbci_cis(tvb, offset, pinfo, dvbci_tree);
    }
    else if (event==DVBCI_EVT_HW_EVT) {
        hw_event = tvb_get_guint8(tvb, offset);
        col_set_str(pinfo->cinfo, COL_INFO,
                val_to_str_const(hw_event, dvbci_hw_event, "Invalid hardware event"));
        proto_tree_add_item(dvbci_tree, hf_dvbci_hw_event,
                tvb, offset, 1, ENC_BIG_ENDIAN);
    }

    return packet_len;
}

static void
dvbci_shutdown(void)
{
    if (spdu_table)
        g_hash_table_destroy(spdu_table);
    if (apdu_table)
        g_hash_table_destroy(apdu_table);
}

void
proto_register_dvbci(void)
{
    guint     i;
    module_t *dvbci_module;
    expert_module_t* expert_dvbci;

    static gint *ett[] = {
        &ett_dvbci,
        &ett_dvbci_hdr,
        &ett_dvbci_cis,
        &ett_dvbci_cis_tpl,
        &ett_dvbci_cis_subtpl,
        &ett_dvbci_link,
        &ett_dvbci_link_frag,
        &ett_dvbci_link_frags,
        &ett_dvbci_transport,
        &ett_dvbci_transport_frag,
        &ett_dvbci_transport_frags,
        &ett_dvbci_session,
        &ett_dvbci_res,
        &ett_dvbci_application,
        &ett_dvbci_es,
        &ett_dvbci_ca_desc,
        &ett_dvbci_text,
        &ett_dvbci_cc_item,
        &ett_dvbci_sac_msg_body,
        &ett_dvbci_ami_req_types,
        &ett_dvbci_lsc_conn_desc,
        &ett_dvbci_opp_cap_loop,
        &ett_dvbci_dlv_sys_hint
    };

    static hf_register_info hf[] = {
        { &hf_dvbci_hdr_ver,
          { "Version", "dvb-ci.hdr_version",
            FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_event,
          { "Event", "dvb-ci.event",
            FT_UINT8, BASE_HEX, VALS(dvbci_event), 0, NULL, HFILL }
        },
        { &hf_dvbci_len,
          { "Length field", "dvb-ci.length_field",
            FT_UINT16, BASE_DEC, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_hw_event,
          { "Hardware event", "dvb-ci.hw_event",
            FT_UINT8, BASE_HEX, VALS(dvbci_hw_event), 0, NULL, HFILL }
        },
        { &hf_dvbci_cor_addr,
          { "COR address", "dvb-ci.cor_address",
            FT_UINT16, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cor_val,
          { "COR value", "dvb-ci.cor_value",
            FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpl_code,
          { "CIS tuple code", "dvb-ci.cis.tpl_code",
            FT_UINT8, BASE_HEX, VALS(dvbci_cis_tpl_code), 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpl_len,
          { "Length field", "dvb-ci.cis.tpl_len",
            FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpl_data,
          { "Tuple data", "dvb-ci.cis.tpl_data",
            FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpll_v1_major,
          { "Major version number", "dvb-ci.cis.tpll_v1_major",
            FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpll_v1_minor,
          { "Minor version number", "dvb-ci.cis.tpll_v1_minor",
            FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpll_v1_info_manuf,
          { "Manufacturer", "dvb-ci.cis.tpll_v1_info.manufacturer",
            FT_STRING, STR_ASCII, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpll_v1_info_name,
          { "Name", "dvb-ci.cis.tpll_v1_info.name",
            FT_STRING, STR_ASCII, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpll_v1_info_additional,
          { "Additional info", "dvb-ci.cis.tpll_v1_info.additional",
            FT_STRING, STR_ASCII, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpll_v1_end,
          { "End of chain", "dvb-ci.cis.tpll_v1_end",
            FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpcc_rfsz,
          { "Size of reserved area", "dvb-ci.cis.tpcc_rfsz",
            FT_UINT8, BASE_HEX, NULL, 0xC0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpcc_rmsz,
          { "Size of TPCC_RMSK field - 1", "dvb-ci.cis.tpcc_rmsz",
            FT_UINT8, BASE_HEX, NULL, 0x3C, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpcc_rasz,
          { "Size of TPCC_RADR - 1", "dvb-ci.cis.tpcc_rasz",
            FT_UINT8, BASE_HEX, NULL, 0x03, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpcc_last,
          { "Index of the last cftable entry", "dvb-ci.cis.tpcc_last",
            FT_UINT8, BASE_HEX, NULL, 0x3F, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpcc_radr,
          { "COR base address", "dvb-ci.cis.tpcc_radr",
            FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpcc_rmsk,
          { "Configuration register presence mask", "dvb-ci.cis.tpcc_rmsk",
            FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_st_code,
          { "Subtuple tag", "dvb-ci.cis.st_code",
            FT_UINT8, BASE_HEX, VALS(dvbci_cis_subtpl_code), 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_st_len,
          { "Subtuple length", "dvb-ci.cis.st_len",
            FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_stci_ifn_size,
          { "Size of interface ID number - 1", "dvb-ci.cis.stci_ifn_size",
            FT_UINT8, BASE_HEX, NULL, 0xC0, NULL, HFILL }
        },
        { &hf_dvbci_cis_stci_ifn,
          { "Interface ID number", "dvb-ci.cis.stci_ifn",
            FT_UINT32, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_stci_str,
          { "Interface description strings", "dvb-ci.cis.stci_str",
            FT_STRING, STR_ASCII, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpce_indx_intface,
          { "Intface", "dvb-ci.cis.tpce_indx.intface",
            FT_UINT8, BASE_HEX, NULL, 0x80, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpce_indx_default,
          { "Default", "dvb-ci.cis.tpce_indx.default",
            FT_UINT8, BASE_HEX, NULL, 0x40, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpce_indx_cnf_entry,
          { "Configuration entry number", "dvb-ci.cis.tpce_indx.cnf_entry",
            FT_UINT8, BASE_HEX, NULL, 0x3F, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpce_if_type,
          { "Interface type", "dvb-ci.cis.tpce_if.type", FT_UINT8, BASE_HEX,
              VALS(dvbci_cis_tpce_if_type), 0x0F, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpce_fs_mem_space,
          { "Mem space", "dvb-ci.cis.tpce_fs.mem_space",
            FT_UINT8, BASE_HEX, NULL, 0x60, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpce_fs_irq,
          { "IRQ", "dvb-ci.cis.tpce_fs.irq",
            FT_UINT8, BASE_HEX, NULL, 0x10, NULL, HFILL }
        },
        { &hf_dvbci_cis_tpce_fs_io,
          { "IO Space", "dvb-ci.cis.tpce_fs.io",
            FT_UINT8, BASE_HEX, NULL, 0x08, NULL, HFILL }
        },
        { &hf_dvbci_cis_dev_vcc_used,
          { "Vcc used", "dvb-ci.cis.device.vcc_used",
              FT_UINT8, BASE_HEX, VALS(dvbci_cis_dev_vcc_used), 0x06, NULL, HFILL }
        },
        { &hf_dvbci_cis_dev_mwait,
          { "MWait", "dvb-ci.cis.device.mwait",
            FT_UINT8, BASE_HEX, NULL, 0x01, NULL, HFILL }
        },
        { &hf_dvbci_cis_dev_oth_cond_info,
          { "Other conditions info", "dvb-ci.cis.device.other_cond",
            FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tplmid_manf,
          { "PC Card manufacturer code", "dvb-ci.cis.tplmid_manf",
            FT_UINT16, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_cis_tplmid_card,
          { "Manufacturer info", "dvb-ci.cis.tplmid_card",
            FT_UINT16, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_buf_size,
          { "Buffer Size", "dvb-ci.buf_size",
            FT_UINT16, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_tcid,
          { "Transport Connection ID", "dvb-ci.tcid",
            FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }
        },
        { &hf_dvbci_ml,
          { "More/Last indicator", "dvb-ci.more_last",
            FT_UINT8, BASE_HEX, VALS(dvbci_ml), 0, NULL, HFILL }
        },
        /* on the link layer, tpdus are reassembled */
        { &hf_dvbci_l_frags,
          { "Tpdu fragments", "dvb-ci.tpdu_fragments",
           FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_dvbci_l_frag,
          { "Tpdu fragment", "dvb-ci.tpdu_fragment",
           FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_dvbci_l_frag_overlap,
          { "Tpdu fragment overlap", "dvb-ci.tpdu_fragment.overlap",
           FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_dvbci_l_frag_overlap_conflicts,
          { "Tpdu fragment overlapping with conflicting data",
           "dvb-ci.tpdu_fragment.overlap.conflicts",
           FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_dvbci_l_frag_multiple_tails,
          { "Tpdu has multiple tail fragments",
           "dvb-ci.tpdu_fragment.multiple_tails",
          FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_dvbci_l_frag_too_long_frag,
          { "Tpdu fragment too long", "dvb-ci.tpdu_fragment.too_long_fragment",
           FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_dvbci_l_frag_err,
          { "Tpdu defragmentation error", "dvb-ci.tpdu_fragment.error",
           FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL }
        },
        { &hf_dvbci_l_frag_cnt,
          { "Tpdu fragment count", "dvb-ci.tpdu_fragment.count",
           FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL }
        },
        { &hf_dvbci_l_reass_in,
          { "Tpdu reassembled in", "dvb-ci.tpdu_reassembled.in",
           FT_FRAMENUM, BASE_NONE, NULL, 0x00,