aboutsummaryrefslogtreecommitdiffstats
path: root/etisnoop.cpp
blob: 8e4fe851ffd35fd722e03da45f94734b3d0cfcaa (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
/*
    Copyright (C) 2014 CSP Innovazione nelle ICT s.c.a r.l. (http://www.csp.it/)
    Copyright (C) 2014 Matthias P. Braendli (http://www.opendigitalradio.org)
    Copyright (C) 2015 Data Path

    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 3 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, see <http://www.gnu.org/licenses/>.

    etisnoop.cpp
          Parse ETI NI G.703 file

    Authors:
         Sergio Sagliocco <sergio.sagliocco@csp.it>
         Matthias P. Braendli <matthias@mpb.li>
                   / |  |-  ')|)  |-|_ _ (|,_   .|  _  ,_ \
         Data Path \(|(||_(|/_| (||_||(a)_||||(|||.(_()|||/

*/



#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <time.h>
#include "lib_crc.h"

#include "dabplussnoop.h"
#include "etiinput.h"

struct FIG
{
    int type;
    int ext;
    int len;
};

class WatermarkDecoder
{
    public:
        void push_confind_bit(bool confind)
        {
            // The ConfInd of FIG 0/10 contains the CRC-DABMUX and ODR-DabMux watermark
            m_confind_bits.push_back(confind);
        }

        std::string calculate_watermark()
        {
            // First try to find the 0x55 0x55 sync in the waternark data
            size_t bit_ix;
            int alternance_count = 0;
            bool last_bit = 1;
            for (bit_ix = 0; bit_ix < m_confind_bits.size(); bit_ix++) {
                if (alternance_count == 16) {
                    break;
                }
                else {
                    if (last_bit != m_confind_bits[bit_ix]) {
                        last_bit = m_confind_bits[bit_ix];
                        alternance_count++;
                    }
                    else {
                        alternance_count = 0;
                        last_bit = 1;
                    }
                }

            }

            printf("Found SYNC at offset %zu out of %zu\n", bit_ix - alternance_count, m_confind_bits.size());

            std::stringstream watermark_ss;

            uint8_t b = 0;
            size_t i = 0;
            while (bit_ix < m_confind_bits.size()) {

                b |= m_confind_bits[bit_ix] << (7 - i);

                if (i == 7) {
                    watermark_ss << (char)b;

                    b = 0;
                    i = 0;
                }
                else {
                    i++;
                }

                bit_ix += 2;
            }

            return watermark_ss.str();
        }

    private:
        std::vector<bool> m_confind_bits;
};

class FIGalyser
{
    public:
        FIGalyser()
        {
            clear();
        }

        void set_fib(int fib)
        {
            m_fib = fib;
        }

        void push_back(int type, int ext, int len)
        {
            struct FIG fig = {
                .type = type,
                .ext  = ext,
                .len  = len };

            m_figs[m_fib].push_back(fig);
        }

        void analyse()
        {
            printf("FIC ");

            for (size_t fib = 0; fib < m_figs.size(); fib++) {
                int consumed = 7;
                int fic_size = 0;
                printf("[%1zu ", fib);

                for (size_t i = 0; i < m_figs[fib].size(); i++) {
                    FIG &f = m_figs[fib][i];
                    printf("%01d/%02d (%2d) ", f.type, f.ext, f.len);

                    consumed += 10;

                    fic_size += f.len;
                }

                printf(" ");

                int align = 60 - consumed;
                if (align > 0) {
                    while (align--) {
                        printf(" ");
                    }
                }

                printf("|");

                for (int i = 0; i < 15; i++) {
                    if (2*i < fic_size) {
                        printf("#");
                    }
                    else {
                        printf("-");
                    }
                }

                printf("| ]   ");

            }

            printf("\n");
        }

        void clear()
        {
            m_figs.clear();
            m_figs.resize(3);
        }

    private:
        int m_fib;
        std::vector<std::vector<FIG> > m_figs;
};


struct FIG0_13_shortAppInfo
{
    uint16_t SId;
    uint8_t No:4;
    uint8_t SCIdS:4;
} PACKED;

class FIG0_5 {
    public:
        // Constructor
        FIG0_5() {
            // Initialization of Language_code_to_char_map:
            // ETSI TS 101 756 V1.6.1 Table 9: European languages
            Language_code_to_char_map[0x00] = "Unknown/not applicable";
            Language_code_to_char_map[0x01] = "Albanian";
            Language_code_to_char_map[0x02] = "Breton";
            Language_code_to_char_map[0x03] = "Catalan";
            Language_code_to_char_map[0x04] = "Croatian";
            Language_code_to_char_map[0x05] = "Welsh";
            Language_code_to_char_map[0x06] = "Czech";
            Language_code_to_char_map[0x07] = "Danish";
            Language_code_to_char_map[0x08] = "German";
            Language_code_to_char_map[0x09] = "English";
            Language_code_to_char_map[0x0A] = "Spanish";
            Language_code_to_char_map[0x0B] = "Esperanto";
            Language_code_to_char_map[0x0C] = "Estonian";
            Language_code_to_char_map[0x0D] = "Basque";
            Language_code_to_char_map[0x0E] = "Faroese";
            Language_code_to_char_map[0x0F] = "French";
            Language_code_to_char_map[0x10] = "Frisian";
            Language_code_to_char_map[0x11] = "Irish";
            Language_code_to_char_map[0x12] = "Gaelic";
            Language_code_to_char_map[0x13] = "Galician";
            Language_code_to_char_map[0x14] = "Icelandic";
            Language_code_to_char_map[0x15] = "Italian";
            Language_code_to_char_map[0x16] = "Lappish";
            Language_code_to_char_map[0x17] = "Latin";
            Language_code_to_char_map[0x18] = "Latvian";
            Language_code_to_char_map[0x19] = "Luxembourgian";
            Language_code_to_char_map[0x1A] = "Lithuanian";
            Language_code_to_char_map[0x1B] = "Hungarian";
            Language_code_to_char_map[0x1C] = "Maltese";
            Language_code_to_char_map[0x1D] = "Dutch";
            Language_code_to_char_map[0x1E] = "Norwegian";
            Language_code_to_char_map[0x1F] = "Occitan";
            Language_code_to_char_map[0x20] = "Polish";
            Language_code_to_char_map[0x21] = "Portuguese";
            Language_code_to_char_map[0x22] = "Romanian";
            Language_code_to_char_map[0x23] = "Romansh";
            Language_code_to_char_map[0x24] = "Serbian";
            Language_code_to_char_map[0x25] = "Slovak";
            Language_code_to_char_map[0x26] = "Slovene";
            Language_code_to_char_map[0x27] = "Finnish";
            Language_code_to_char_map[0x28] = "Swedish";
            Language_code_to_char_map[0x29] = "Turkish";
            Language_code_to_char_map[0x2A] = "Flemish";
            Language_code_to_char_map[0x2B] = "Walloon";
            Language_code_to_char_map[0x2C] = "rfu";
            Language_code_to_char_map[0x2D] = "rfu";
            Language_code_to_char_map[0x2E] = "rfu";
            Language_code_to_char_map[0x2F] = "rfu";
            Language_code_to_char_map[0x30] = "Reserved for national assignment";
            Language_code_to_char_map[0x31] = "Reserved for national assignment";
            Language_code_to_char_map[0x32] = "Reserved for national assignment";
            Language_code_to_char_map[0x33] = "Reserved for national assignment";
            Language_code_to_char_map[0x34] = "Reserved for national assignment";
            Language_code_to_char_map[0x35] = "Reserved for national assignment";
            Language_code_to_char_map[0x36] = "Reserved for national assignment";
            Language_code_to_char_map[0x37] = "Reserved for national assignment";
            Language_code_to_char_map[0x38] = "Reserved for national assignment";
            Language_code_to_char_map[0x39] = "Reserved for national assignment";
            Language_code_to_char_map[0x3A] = "Reserved for national assignment";
            Language_code_to_char_map[0x3B] = "Reserved for national assignment";
            Language_code_to_char_map[0x3C] = "Reserved for national assignment";
            Language_code_to_char_map[0x3D] = "Reserved for national assignment";
            Language_code_to_char_map[0x3E] = "Reserved for national assignment";
            Language_code_to_char_map[0x3F] = "Reserved for national assignment";

            // ETSI TS 101 756 V1.6.1 Table 10: Other languages
            Language_code_to_char_map[0x40] = "Background sound/clean feed";
            Language_code_to_char_map[0x41] = "rfu";
            Language_code_to_char_map[0x42] = "rfu";
            Language_code_to_char_map[0x43] = "rfu";
            Language_code_to_char_map[0x44] = "rfu";
            Language_code_to_char_map[0x45] = "Zulu";
            Language_code_to_char_map[0x46] = "Vietnamese";
            Language_code_to_char_map[0x47] = "Uzbek";
            Language_code_to_char_map[0x48] = "Urdu";
            Language_code_to_char_map[0x49] = "Ukranian";
            Language_code_to_char_map[0x4A] = "Thai";
            Language_code_to_char_map[0x4B] = "Telugu";
            Language_code_to_char_map[0x4C] = "Tatar";
            Language_code_to_char_map[0x4D] = "Tamil";
            Language_code_to_char_map[0x4E] = "Tadzhik";
            Language_code_to_char_map[0x4F] = "Swahili";
            Language_code_to_char_map[0x50] = "Sranan Tongo";
            Language_code_to_char_map[0x51] = "Somali";
            Language_code_to_char_map[0x52] = "Sinhalese";
            Language_code_to_char_map[0x53] = "Shona";
            Language_code_to_char_map[0x54] = "Serbo-Croat";
            Language_code_to_char_map[0x55] = "Rusyn";
            Language_code_to_char_map[0x56] = "Russian";
            Language_code_to_char_map[0x57] = "Quechua";
            Language_code_to_char_map[0x58] = "Pushtu";
            Language_code_to_char_map[0x59] = "Punjabi";
            Language_code_to_char_map[0x5A] = "Persian";
            Language_code_to_char_map[0x5B] = "Papiamento";
            Language_code_to_char_map[0x5C] = "Oriya";
            Language_code_to_char_map[0x5D] = "Nepali";
            Language_code_to_char_map[0x5E] = "Ndebele";
            Language_code_to_char_map[0x5F] = "Marathi";
            Language_code_to_char_map[0x60] = "Moldavian";
            Language_code_to_char_map[0x61] = "Malaysian";
            Language_code_to_char_map[0x62] = "Malagasay";
            Language_code_to_char_map[0x63] = "Macedonian";
            Language_code_to_char_map[0x64] = "Laotian";
            Language_code_to_char_map[0x65] = "Korean";
            Language_code_to_char_map[0x66] = "Khmer";
            Language_code_to_char_map[0x67] = "Kazakh";
            Language_code_to_char_map[0x68] = "Kannada";
            Language_code_to_char_map[0x69] = "Japanese";
            Language_code_to_char_map[0x6A] = "Indonesian";
            Language_code_to_char_map[0x6B] = "Hindi";
            Language_code_to_char_map[0x6C] = "Hebrew";
            Language_code_to_char_map[0x6D] = "Hausa";
            Language_code_to_char_map[0x6E] = "Gurani";
            Language_code_to_char_map[0x6F] = "Gujurati";
            Language_code_to_char_map[0x70] = "Greek";
            Language_code_to_char_map[0x71] = "Georgian";
            Language_code_to_char_map[0x72] = "Fulani";
            Language_code_to_char_map[0x73] = "Dari";
            Language_code_to_char_map[0x74] = "Chuvash";
            Language_code_to_char_map[0x75] = "Chinese";
            Language_code_to_char_map[0x76] = "Burmese";
            Language_code_to_char_map[0x77] = "Bulgarian";
            Language_code_to_char_map[0x78] = "Bengali";
            Language_code_to_char_map[0x79] = "Belorussian";
            Language_code_to_char_map[0x7A] = "Bambora";
            Language_code_to_char_map[0x7B] = "Azerbaijani";
            Language_code_to_char_map[0x7C] = "Assamese";
            Language_code_to_char_map[0x7D] = "Armenian";
            Language_code_to_char_map[0x7E] = "Arabic";
            Language_code_to_char_map[0x7F] = "Amharic";
        }

        // Destructor
        ~FIG0_5() {
            // Remove elements from Language_code_to_char_map map container
            Language_code_to_char_map.clear();
        }

        // Language_to_char decode fig 0/5 language code in string
        // Input : Language code number
        // Return: Language char *
        const char * Language_to_char(unsigned char language) {
            if (Language_code_to_char_map.count(language) > 0) {
                return Language_code_to_char_map[language];
            }
            else {
                return "";
            }
        }

    private:
        // Map between fig 0/5 Language code and Language char
        std::map<unsigned char, const char *> Language_code_to_char_map;
};


#define ETINIPACKETSIZE 6144

using namespace std;

struct eti_analyse_config_t {
    FILE* etifd;
    bool ignore_error;
    std::map<int, DabPlusSnoop> streams_to_decode;
    bool analyse_fic_carousel;
    bool decode_watermark;
};


// Globals
static int verbosity;
static unsigned char Mode_Identity = 0;

// fig 0/2 fig 0/3 DSCTy types string:
const char *DSCTy_types_str[64] =  {
    // ETSI TS 101 756 V1.6.1 (2014-05) table 2
    "Unspecified data",                             "Traffic Message Channel (TMC)",
    "Emergency Warning System (EWS)",               "Interactive Text Transmission System (ITTS)",
    "Paging",                                       "Transparent Data Channel (TDC)",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "MPEG-2 Transport Stream, see ETSI TS 102 427", "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Rfu",
    "Rfu",                                          "Embedded IP packets",
    "Multimedia Object Transfer (MOT)",             "Proprietary service: no DSCTy signalled",
    "Not used",                                     "Not used"
};

// map between fig 0/6 database key and LA to detect activation and deactivation of links
std::map<unsigned short, bool> fig0_6_key_la;

// fig 0/9 global variables
unsigned char Ensemble_ECC=0, International_Table_Id=0;
signed char Ensemble_LTO=0;
bool LTO_uniq;

// fig 0/14 FEC Scheme: this 2-bit field shall indicate the Forward Error Correction scheme in use, as follows:
const char *FEC_schemes_str[4] =  {
    "no FEC scheme applied",
    "FEC scheme applied according to ETSI EN 300 401 clause 5.3.5",
    "reserved for future definition",
    "reserved for future definition"
};

// fig 0/17 Programme type codes
#define INTERNATIONAL_TABLE_SIZE     2
#define PROGRAMME_TYPE_CODES_SIZE  32
const char *Programme_type_codes_str[INTERNATIONAL_TABLE_SIZE][PROGRAMME_TYPE_CODES_SIZE] = {
    {   // ETSI TS 101 756 V1.6.1 (2014-05) table 12
        "No programme type",        "News",
        "Current Affairs",          "Information",
        "Sport",                    "Education",
        "Drama",                    "Culture",
        "Science",                  "Varied",
        "Pop Music",                "Rock Music",
        "Easy Listening Music",     "Light Classical",
        "Serious Classical",        "Other Music",
        "Weather/meteorology",      "Finance/Business",
        "Children's programmes",    "Social Affairs",
        "Religion",                 "Phone In",
        "Travel",                   "Leisure",
        "Jazz Music",               "Country Music",
        "National Music",           "Oldies Music",
        "Folk Music",               "Documentary",
        "Not used",                 "Not used"
    },
    {   // ETSI TS 101 756 V1.6.1 (2014-05) table 13
        "No program type",  "News",
        "Information",      "Sports",
        "Talk",             "Rock",
        "Classic Rock",     "Adult Hits",
        "Soft Rock",        "Top 40",
        "Country",          "Oldies",
        "Soft",             "Nostalgia",
        "Jazz",             "Classical",
        "Rhythm and Blues", "Soft Rhythm and Blues",
        "Foreign Language", "Religious Music",
        "Religious Talk",   "Personality",
        "Public",           "College",
        "rfu",              "rfu",
        "rfu",              "rfu",
        "rfu",              "Weather",
        "Not used",         "Not used"
    }
};

// fig 0/18 0/19 announcement types (ETSI TS 101 756 V1.6.1 (2014-05) table 14 & 15)
const char *announcement_types_str[16] = {
    "Alarm",
    "Road Traffic flash",
    "Transport flash",
    "Warning/Service",
    "News flash",
    "Area weather flash",
    "Event announcement",
    "Special event",
    "Programme Information",
    "Sport report",
    "Financial report",
    "Reserved for future definition",
    "Reserved for future definition",
    "Reserved for future definition",
    "Reserved for future definition",
    "Reserved for future definition"
};

// fig 0/22 struct
typedef struct lat_lng {
    double latitude, longitude;
}Lat_Lng;
// map for fig 0/22 database
std::map<unsigned short, Lat_Lng> fig0_22_key_Lat_Lng;

// ETSI TS 102 367 V1.2.1 (2006-01) 5.4.1 Conditional Access Mode (CAMode)
const char *CAMode_str[8] = {
    "Sub-channel CA",   "Data Group CA",
    "MOT CA",           "proprietary CA",
    "reserved",         "reserved",
    "reserved",         "reserved"
};


// Function prototypes
void printinfo(string header,
        int indent_level,
        int min_verb=0);

void printbuf(string header,
        int indent_level,
        unsigned char* buffer,
        size_t size,
        string desc="");

void decodeFIG(FIGalyser &figs,
               WatermarkDecoder &wm_decoder,
               unsigned char* figdata,
               unsigned char figlen,
               unsigned short int figtype,
               unsigned short int indent);

int eti_analyse(eti_analyse_config_t& config);

const char *get_programme_type_str(unsigned int int_table_Id, unsigned int pty);
char *strcatPNum(char *dest_str, unsigned short Programme_Number);
int sprintfMJD(char *dst, int mjd);

std::string get_fig_0_13_userapp(int user_app_type)
{
    switch (user_app_type) {
        case 0x000: return "Reserved for future definition";
        case 0x001: return "Not used";
        case 0x002: return "MOT Slideshow";
        case 0x003: return "MOT Broadacst Web Site";
        case 0x004: return "TPEG";
        case 0x005: return "DGPS";
        case 0x006: return "TMC";
        case 0x007: return "EPG";
        case 0x008: return "DAB Java";
        case 0x44a: return "Journaline";
        default: return "Reserved for future applications";
    }
}

#define no_argument 0
#define required_argument 1
#define optional_argument 2
const struct option longopts[] = {
    {"help",               no_argument,        0, 'h'},
    {"verbose",            no_argument,        0, 'v'},
    {"ignore-error",       no_argument,        0, 'e'},
    {"decode-stream",      required_argument,  0, 'd'},
    {"input",              required_argument,  0, 'i'}
};

void usage(void)
{
    fprintf(stderr,
            "ETISnoop analyser\n\n"
            "The ETSnoop analyser decodes and prints out a RAW ETI file in a\n"
            "form that makes analysis easier.\n"
            "Usage: etisnoop [-v] [-f] [-w] [-i filename] [-d stream_index]\n"
            "\n"
            "   -v      increase verbosity (can be given more than once)\n"
            "   -d N    decode subchannel N into .dabp and .wav files\n"
            "   -f      analyse FIC carousel\n"
            "   -w      decode CRC-DABMUX and ODR-DabMux watermark.\n");
}

int main(int argc, char *argv[])
{
    int index;
    int ch = 0;
    string file_name("-");
    map<int, DabPlusSnoop> streams_to_decode;

    verbosity = 0;
    bool ignore_error = false;
    bool analyse_fic_carousel = false;
    bool decode_watermark = false;

    while(ch != -1) {
        ch = getopt_long(argc, argv, "d:efhvwi:", longopts, &index);
        switch (ch) {
            case 'd':
                {
                    int subchix = atoi(optarg);
                DabPlusSnoop dps;
                streams_to_decode[subchix] = dps;
                }
                break;
            case 'e':
                ignore_error = true;
                break;
            case 'i':
                file_name = optarg;
                break;
            case 'f':
                analyse_fic_carousel = true;
                break;
            case 'v':
                verbosity++;
                break;
            case 'w':
                decode_watermark = true;
                break;
            case 'h':
                usage();
                return 1;
                break;
        }
    }

    FILE* etifd;

    if (file_name == "-") {
        printf("Analysing stdin\n");
        etifd = stdin;
    }
    else {
        etifd = fopen(file_name.c_str(), "r");
        if (etifd == NULL) {
            perror("File open failed");
            return 1;
        }
    }

    eti_analyse_config_t config = {
        .etifd = etifd,
        .ignore_error = ignore_error,
        .streams_to_decode = streams_to_decode,
        .analyse_fic_carousel = analyse_fic_carousel,
        .decode_watermark = decode_watermark
    };
    eti_analyse(config);
    fclose(etifd);
}

int eti_analyse(eti_analyse_config_t& config)
{
    unsigned char p[ETINIPACKETSIZE];
    string desc;
    char prevsync[3]={0x00,0x00,0x00};
    unsigned char ficf,nst,fp,mid,ficl;
    unsigned short int fl,crch;
    unsigned short int crc;
    unsigned char scid,tpl,l1;
    unsigned short int sad[64],stl[64];
    char sdesc[256];
    unsigned int frame_nb = 0, frame_sec = 0, frame_ms = 0, frame_h, frame_m, frame_s;

    bool running = true;

    int stream_type = ETI_STREAM_TYPE_NONE;
    if (identify_eti_format(config.etifd, &stream_type) == -1) {
        printf("Could not identify stream type\n");

        running = false;
    }
    else {
        printf("Identified ETI type ");
        if (stream_type == ETI_STREAM_TYPE_RAW)
            printf("RAW\n");
        else if (stream_type == ETI_STREAM_TYPE_STREAMED)
            printf("STREAMED\n");
        else if (stream_type == ETI_STREAM_TYPE_FRAMED)
            printf("FRAMED\n");
        else
            printf("?\n");
    }

    WatermarkDecoder wm_decoder;

    while (running) {

        int ret = get_eti_frame(config.etifd, stream_type, p);
        if (ret == -1) {
            fprintf(stderr, "ETI file read error\n");
            break;
        }
        else if (ret == 0) {
            fprintf(stderr, "End of ETI\n");
            break;
        }

        // Timestamp and Frame Number
        frame_h = (frame_sec / 3600);
        frame_m = (frame_sec - (frame_h * 3600)) / 60;
        frame_s = (frame_sec - (frame_h * 3600) - (frame_m * 60));
        sprintf(sdesc, "%02d:%02d:%02d.%03d frame %d", frame_h, frame_m, frame_s, frame_ms, frame_nb);
        printbuf(sdesc, 0, NULL, 0);
        frame_ms += 24; // + 24 ms
        if (frame_ms >= 1000) {
            frame_ms -= 1000;
            frame_sec++;
        }
        frame_nb++;

        // SYNC
        printbuf("SYNC", 0, p, 4);

        // SYNC - ERR
        if (p[0] == 0xFF) {
            desc = "No error";
            printbuf("ERR", 1, p, 1, desc);
        }
        else {
            desc = "Error";
            printbuf("ERR", 1, p, 1, desc);
            if (!config.ignore_error) {
                printf("Aborting because of SYNC error\n");
                break;
            }
        }

        // SYNC - FSYNC

        if (memcmp(prevsync, "\x00\x00\x00", 3) == 0) {
            if ( (memcmp(p + 1, "\x07\x3a\xb6", 3) == 0) ||
                 (memcmp(p + 1, "\xf8\xc5\x49", 3) == 0) ) {
                desc = "OK";
                memcpy(prevsync, p+1, 3);
            }
            else {
                desc ="Wrong FSYNC";
                memcpy(prevsync, "\x00\x00\x00", 3);
            }
        } else if (memcmp(prevsync, "\x07\x3a\xb6", 3) == 0) {
            if (memcmp(p + 1, "\xf8\xc5\x49", 3) != 0) {
                desc = "Wrong FSYNC";
                memcpy(prevsync, "\x00\x00\x00", 3);
            } else {
                desc = "OK";
                memcpy(prevsync, p + 1, 3);
            }
        } else if (memcmp(prevsync, "\xf8\xc5\x49", 3) == 0) {
            if (memcmp(p + 1, "\x07\x3a\xb6", 3) != 0) {
                desc = "Wrong FSYNC";
                memcpy(prevsync, "\x00\x00\x00", 3);
            } else {
                desc = "OK";
                memcpy(prevsync, p + 1, 3);
            }
        }
        printbuf("Sync FSYNC", 1, p + 1, 3, desc);

        // LIDATA
        printbuf("LDATA", 0, NULL, 0);
        // LIDATA - FC
        printbuf("FC - Frame Characterization field", 1, p+4, 4);
        // LIDATA - FC - FCT
        char fct[25];
        sprintf(fct, "%d", p[4]);
        printbuf("FCT  - Frame Count", 2, p+4, 1, fct);
        // LIDATA - FC - FICF
        ficf = (p[5] & 0x80) >> 7;

        {
            stringstream ss;
            ss << (int)ficf;
            if (ficf == 1) {
                ss << "- FIC Information are present";
            }
            else {
                ss << "- FIC Information are not present";
            }

            printbuf("FICF - Fast Information Channel Flag", 2, NULL, 0, ss.str());
        }

        // LIDATA - FC - NST
        nst = p[5] & 0x7F;
        {
            stringstream ss;
            ss << (int)nst;
            printbuf("NST  - Number of streams", 2, NULL, 0, ss.str());
        }

        // LIDATA - FC - FP
        fp = (p[6] & 0xE0) >> 5;
        {
            stringstream ss;
            ss << (int)fp;
            printbuf("FP   - Frame Phase", 2, &fp, 1, ss.str());
        }

        // LIDATA - FC - MID
        mid = (p[6] & 0x18) >> 3;
        {
            stringstream ss;
            ss << "Mode ";
            if (mid != 0) {
                ss << (int)mid;
            }
            else {
                ss << "4";
            }
            printbuf("MID  - Mode Identity", 2, &mid, 1, ss.str());
            Mode_Identity = mid;
        }

        // LIDATA - FC - FL
        fl = (p[6] & 0x07) * 256 + p[7];
        {
            stringstream ss;
            ss << fl << " words";
            printbuf("FL   - Frame Length", 2, NULL, 0, ss.str());
        }

        if (ficf == 0) {
            ficl = 0;
        }
        else if (mid == 3) {
            ficl = 32;
        }
        else {
            ficl = 24;
        }

        // STC
        printbuf("STC - Stream Characterisation", 1, NULL, 0);

        for (int i=0; i < nst; i++) {
            sprintf(sdesc, "Stream number %d", i);
            printbuf("STC  - Stream Characterisation", 2, p + 8 + 4*i, 4, sdesc);
            scid = (p[8 + 4*i] & 0xFC) >> 2;
            sprintf(sdesc, "%d", scid);
            printbuf("SCID - Sub-channel Identifier", 3, NULL, 0, sdesc);
            sad[i] = (p[8+4*i] & 0x03) * 256 + p[9+4*i];
            sprintf(sdesc, "%d", sad[i]);
            printbuf("SAD  - Sub-channel Start Address", 3, NULL, 0, sdesc);
            tpl = (p[10+4*i] & 0xFC) >> 2;

            if ((tpl & 0x20) >> 5 == 1) {
                unsigned char opt, plevel;
                string plevelstr;
                opt = (tpl & 0x1c) >> 2;
                plevel = (tpl & 0x03);
                if (opt == 0x00) {
                    if (plevel == 0)
                        plevelstr = "1-A, 1/4, 16 CUs";
                    else if (plevel == 1)
                        plevelstr = "2-A, 3/8, 8 CUs";
                    else if (plevel == 2)
                        plevelstr = "3-A, 1/2, 6 CUs";
                    else if (plevel == 3)
                        plevelstr = "4-A, 3/4, 4 CUs";
                }
                else if (opt == 0x01) {
                    if (plevel == 0)
                        plevelstr = "1-B, 4/9, 27 CUs";
                    else if (plevel == 1)
                        plevelstr = "2-B, 4/7, 21 CUs";
                    else if (plevel == 2)
                        plevelstr = "3-B, 4/6, 18 CUs";
                    else if (plevel == 3)
                        plevelstr = "4-B, 4/5, 15 CUs";
                }
                else {
                    stringstream ss;
                    ss << "Unknown option " << opt;
                    plevelstr = ss.str();
                }
                sprintf(sdesc, "0x%02x - Equal Error Protection. %s", tpl, plevelstr.c_str());
            }
            else {
                unsigned char tsw, uepidx;
                tsw = (tpl & 0x08);
                uepidx = tpl & 0x07;
                sprintf(sdesc, "0x%02x - Unequal Error Protection. Table switch %d,  UEP index %d", tpl, tsw, uepidx);
            }
            printbuf("TPL  - Sub-channel Type and Protection Level", 3, NULL, 0, sdesc);
            stl[i] = (p[10+4*i] & 0x03) * 256 + \
                      p[11+4*i];
            sprintf(sdesc, "%d => %d kbit/s", stl[i], stl[i]*8/3);
            printbuf("STL  - Sub-channel Stream Length", 3, NULL, 0, sdesc);

            if (config.streams_to_decode.count(i) > 0) {
                config.streams_to_decode[i].set_subchannel_index(stl[i]/3);
                config.streams_to_decode[i].set_index(i);
            }
        }

        // EOH
        printbuf("EOH - End Of Header", 1, p + 8 + 4*nst, 4);
        unsigned short int mnsc = p[8 + 4*nst] * 256 + \
                                  p[8 + 4*nst + 1];
        {
            stringstream ss;
            ss << mnsc;
            printbuf("MNSC - Multiplex Network Signalling Channel", 2, p+8+4*nst, 2, ss.str());
        }

        crch = p[8 + 4*nst + 2]*256 + \
               p[8 + 4*nst + 3];
        crc  = 0xffff;

        for (int i=4; i < 8 + 4*nst + 2; i++)
            crc = update_crc_ccitt(crc, p[i]);
        crc =~ crc;

        if (crc == crch) {
            sprintf(sdesc,"CRC OK");
        }
        else {
            sprintf(sdesc,"CRC Mismatch: %02x",crc);
        }

        printbuf("Header CRC", 2, p + 8 + 4*nst + 2, 2, sdesc);

        // MST - FIC
        if (ficf == 1) {
            int endmarker = 0;
            int figcount = 0;
            unsigned char *fib, *fig;
            unsigned short int figcrc;

            FIGalyser figs;

            unsigned char ficdata[32*4];
            memcpy(ficdata, p + 12 + 4*nst, ficl*4);
            sprintf(sdesc, "FIC Data (%d bytes)", ficl*4);
            //printbuf(sdesc, 1, ficdata, ficl*4);
            printbuf(sdesc, 1, NULL, 0);
            fib = p + 12 + 4*nst;
            for(int i = 0; i < ficl*4/32; i++) {
                sprintf(sdesc, "FIB %d", i);
                printbuf(sdesc, 1, NULL, 0);
                fig=fib;
                figs.set_fib(i);
                endmarker=0;
                figcount=0;
                while (!endmarker) {
                    unsigned char figtype, figlen;
                    figtype = (fig[0] & 0xE0) >> 5;
                    if (figtype != 7) {
                        figlen = fig[0] & 0x1F;
                        sprintf(sdesc, "FIG %d [%d bytes]", figtype, figlen);
                        printbuf(sdesc, 3, fig+1, figlen);
                        decodeFIG(figs, wm_decoder, fig+1, figlen, figtype, 4);
                        fig += figlen + 1;
                        figcount += figlen + 1;
                        if (figcount >= 29)
                            endmarker = 1;
                    }
                    else {
                        endmarker = 1;
                    }
                }
                figcrc = fib[30]*256 + fib[31];
                crc = 0xffff;
                for (int j = 0; j < 30; j++) {
                    crc = update_crc_ccitt(crc, fib[j]);
                }
                crc =~ crc;
                if (crc == figcrc)
                    sprintf(sdesc, "FIB %d CRC OK", i);
                else
                    sprintf(sdesc, "FIB %d CRC Mismatch: %02x", i, crc);

                printbuf(sdesc,3,fib+30,2);
                fib += 32;
            }

            if (config.analyse_fic_carousel) {
                figs.analyse();
            }
        }

        int offset = 0;
        for (int i=0; i < nst; i++) {
            unsigned char streamdata[684*8];
            memcpy(streamdata, p + 12 + 4*nst + ficf*ficl*4 + offset, stl[i]*8);
            offset += stl[i] * 8;
            if (config.streams_to_decode.count(i) > 0) {
                sprintf(sdesc, "id %d, len %d, selected for decoding", i, stl[i]*8);
            }
            else {
                sprintf(sdesc, "id %d, len %d, not selected for decoding", i, stl[i]*8);
            }
            if (verbosity > 1) {
                printbuf("Stream Data", 1, streamdata, stl[i]*8, sdesc);
            }
            else {
                printbuf("Stream Data", 1, streamdata, 0, sdesc);
            }

            if (config.streams_to_decode.count(i) > 0) {
                config.streams_to_decode[i].push(streamdata, stl[i]*8);
            }

        }

        // EOF
        crch = p[12 + 4*nst + ficf*ficl*4 + offset] * 256 + \
               p[12 + 4*nst + ficf*ficl*4 + offset + 1];

        crc = 0xffff;

        for (int i = 12 + 4*nst; i < 12 + 4*nst + ficf*ficl*4 + offset; i++)
            crc = update_crc_ccitt(crc, p[i]);
        crc =~ crc;
        if (crc == crch)
            sprintf(sdesc, "CRC OK");
        else
            sprintf(sdesc, "CRC Mismatch: %02x", crc);

        printbuf("EOF", 1, p + 12 + 4*nst + ficf*ficl*4 + offset, 4);
        printbuf("CRC", 2, p + 12 + 4*nst + ficf*ficl*4 + offset, 2, sdesc);

        //RFU
        printbuf("RFU", 2, p + 12 + 4*nst + ficf*ficl*4 + offset + 2, 2);

        //TIST
        l1 = (p[12 + 4*nst + ficf*ficl*4 + offset + 5] & 0xfe) >> 1;
        sprintf(sdesc, "%d ms", l1*8);
        printbuf("TIST - Time Stamp", 1, p+12+4*nst+ficf*ficl*4+offset+4, 4, sdesc);


        if (verbosity) {
            printf("-------------------------------------------------------------------------------------------------------------\n");
        }
    }


    std::map<int, DabPlusSnoop>::iterator it;
    for (it = config.streams_to_decode.begin();
            it != config.streams_to_decode.end();
            ++it) {
        it->second.close();
    }

    if (config.decode_watermark) {
        std::string watermark(wm_decoder.calculate_watermark());
        printf("Watermark:\n  %s\n", watermark.c_str());
    }

    // remove elements from fig0_6_key_la and fig0_22_key_Lat_Lng map containers
    fig0_6_key_la.clear();
    fig0_22_key_Lat_Lng.clear();

    return 0;
}

void decodeFIG(FIGalyser &figs,
               WatermarkDecoder &wm_decoder,
               unsigned char* f,
               unsigned char figlen,
               unsigned short int figtype,
               unsigned short int indent)
{
    char desc[512];
    static FIG0_5 fig0_5;

    switch (figtype) {
        case 0:
            {
                unsigned short int ext,cn,oe,pd;

                cn = (f[0] & 0x80) >> 7;
                oe = (f[0] & 0x40) >> 6;
                pd = (f[0] & 0x20) >> 5;
                ext = f[0] & 0x1F;
                sprintf(desc, "FIG %d/%d: C/N=%d OE=%d P/D=%d",
                        figtype, ext, cn, oe, pd);
                printbuf(desc, indent, f+1, figlen-1);

                figs.push_back(figtype, ext, figlen);

                switch (ext) {

                    case 0: // FIG 0/0 Ensemble information
                        {   // ETSI EN 300 401 6.4
                            unsigned char cid, al, ch, hic, lowc, occ;
                            unsigned short int eid, eref;

                            eid  =  f[1]*256+f[2];
                            cid  = (f[1] & 0xF0) >> 4;
                            eref = (f[1] & 0x0F)*256 + \
                                    f[2];
                            ch   = (f[3] & 0xC0) >> 6;
                            al   = (f[3] & 0x20) >> 5;
                            hic  =  f[3] & 0x1F;
                            lowc =  f[4];
                            if (ch != 0) {
                                occ = f[5];
                                sprintf(desc,
                                        "Ensemble ID=0x%02x (Country id=%d, Ensemble reference=%d), Change flag=%d, Alarm flag=%d, CIF Count=%d/%d, Occurance change=%d",
                                        eid, cid, eref, ch, al, hic, lowc, occ);
                            }
                            else {
                                sprintf(desc,
                                        "Ensemble ID=0x%02x (Country id=%d, Ensemble reference=%d), Change flag=%d, Alarm flag=%d, CIF Count=%d/%d",
                                        eid, cid, eref, ch, al, hic, lowc);
                            }
                            printbuf(desc, indent+1, NULL, 0);

                        }
                        break;
                    case 1: // FIG 0/1 Basic sub-channel organization
                        {   // ETSI EN 300 401 6.2.1
                            int i = 1;

                            while (i < figlen-3) {
                                // iterate over subchannels
                                int subch_id = f[i] >> 2;
                                int start_addr = ((f[i] & 0x03) << 8) |
                                                 (f[i+1]);
                                int long_flag  = (f[i+2] >> 7);

                                if (long_flag) {
                                    int option = (f[i+2] >> 4) & 0x07;
                                    int protection_level = (f[i+2] >> 2) & 0x03;
                                    int subchannel_size  = ((f[i+2] & 0x03) << 8 ) |
                                                           f[i+3];

                                    i += 4;

                                    if (option == 0x00) {
                                        sprintf(desc,
                                                "Subch 0x%x, start_addr %d, long, EEP %d-A, subch size %d",
                                                subch_id, start_addr, protection_level, subchannel_size);
                                    }
                                    else if (option == 0x01) {
                                        sprintf(desc,
                                                "Subch 0x%x, start_addr %d, long, EEP %d-B, subch size %d",
                                                subch_id, start_addr, protection_level, subchannel_size);
                                    }
                                    else {
                                        sprintf(desc,
                                                "Subch 0x%x, start_addr %d, long, invalid option %d, protection %d, subch size %d",
                                                subch_id, start_addr, option, protection_level, subchannel_size);
                                    }
                                }
                                else {
                                    int table_switch = (f[i+2] >> 6) & 0x01;
                                    unsigned int table_index  = (f[i+2] & 0x3F);


                                    if (table_switch == 0) {
                                        sprintf(desc,
                                                "Subch 0x%x, start_addr %d, short, table index %d",
                                                subch_id, start_addr, table_index);
                                    }
                                    else {
                                        sprintf(desc,
                                                "Subch 0x%x, start_addr %d, short, invalid table_switch(=1), table index %d",
                                                subch_id, start_addr, table_index);
                                    }

                                    i += 3;
                                }
                                printbuf(desc, indent+1, NULL, 0);
                            }

                        }
                        break;
                    case 2: // FIG 0/2 Basic service and service component definition
                        {   // ETSI EN 300 401 6.3.1
                            unsigned short int sref, sid;
                            unsigned char cid, ecc, local, caid, ncomp, timd, ps, ca, subchid, scty;
                            int k=1;
                            string psdesc;
                            char sctydesc[32];

                            while (k<figlen) {
                                if (pd == 0) {
                                    sid  =  f[k] * 256 + f[k+1];
                                    cid  = (f[k] & 0xF0) >> 4;
                                    sref = (f[k] & 0x0F) * 256 + f[k+1];
                                    k += 2;
                                }
                                else {
                                    sid  =  f[k] * 256 * 256 * 256 + \
                                            f[k+1] * 256 * 256 + \
                                            f[k+2] * 256 + \
                                            f[k+3];

                                    ecc  =  f[k];
                                    cid  = (f[k+1] & 0xF0) >> 4;
                                    sref = (f[k+1] & 0x0F) * 256 * 256 + \
                                           f[k+2] * 256 + \
                                           f[k+3];

                                    k += 4;
                                }

                                local = (f[k] & 0x80) >> 7;
                                caid  = (f[k] & 0x70) >> 4;
                                ncomp =  f[k] & 0x0F;

                                if (pd == 0)
                                    sprintf(desc,
                                            "Service ID=0x%X (Country id=%d, Service reference=%d), Number of components=%d, Local flag=%d, CAID=%d",
                                            sid, cid, sref, ncomp, local, caid);
                                else
                                    sprintf(desc,
                                            "Service ID=0x%X (ECC=%d, Country id=%d, Service reference=%d), Number of components=%d, Local flag=%d, CAID=%d",
                                            sid, ecc, cid, sref, ncomp, local, caid);
                                printbuf(desc, indent+1, NULL, 0);

                                k++;
                                for (int i=0; i<ncomp; i++) {
                                    unsigned char scomp[2];

                                    memcpy(scomp, f+k, 2);
                                    sprintf(desc, "Component[%d]", i);
                                    printbuf(desc, indent+2, scomp, 2, "");
                                    timd    = (scomp[0] & 0xC0) >> 6;
                                    ps      = (scomp[1] & 0x02) >> 1;
                                    ca      =  scomp[1] & 0x01;
                                    scty    =  scomp[0] & 0x3F;
                                    subchid = (scomp[1] & 0xFC) >> 2;

                                    /* useless, kept as reference
                                    if (timd == 3) {
                                        unsigned short int scid;
                                        scid = scty*64 + subchid;
                                    }
                                    */

                                    if (ps == 0) {
                                        psdesc = "Secondary service";
                                    }
                                    else {
                                        psdesc = "Primary service";
                                    }


                                    if (timd == 0) {
                                        //MSC stream audio
                                        if (scty == 0)
                                            sprintf(sctydesc, "MPEG Foreground sound (%d)", scty);
                                        else if (scty == 1)
                                            sprintf(sctydesc, "MPEG Background sound (%d)", scty);
                                        else if (scty == 2)
                                            sprintf(sctydesc, "Multi Channel sound (%d)", scty);
                                        else if (scty == 63)
                                            sprintf(sctydesc, "AAC sound (%d)", scty);
                                        else
                                            sprintf(sctydesc, "Unknown ASCTy (%d)", scty);

                                        sprintf(desc, "Stream audio mode, %s, %s, SubChannel ID=%02X, CA=%d", psdesc.c_str(), sctydesc, subchid, ca);
                                        printbuf(desc, indent+3, NULL, 0);
                                    }
                                    else if (timd == 1) {
                                        // MSC stream data
                                        sprintf(sctydesc, "DSCTy=%d %s", scty, DSCTy_types_str[scty]);
                                        sprintf(desc, "Stream data mode, %s, %s, SubChannel ID=%02X, CA=%d", psdesc.c_str(), sctydesc, subchid, ca);
                                        printbuf(desc, indent+3, NULL, 0);
                                    }
                                    else if (timd == 2) {
                                        // FIDC
                                        sprintf(sctydesc, "DSCTy=%d %s", scty, DSCTy_types_str[scty]);
                                        sprintf(desc, "FIDC mode, %s, %s, Fast Information Data Channel ID=%02X, CA=%d", psdesc.c_str(), sctydesc, subchid, ca);
                                        printbuf(desc, indent+3, NULL, 0);
                                    }
                                    else if (timd == 3) {
                                        // MSC Packet mode
                                        sprintf(desc, "MSC Packet Mode, %s, Service Component ID=%02X, CA=%d", psdesc.c_str(), subchid, ca);
                                        printbuf(desc, indent+3, NULL, 0);
                                    }
                                    k += 2;
                                }
                            }
                        }
                        break;
                    case 3: // FIG 0/3 Service component in packet mode with or without Conditional Access
                        {   // ETSI EN 300 401 6.3.2
                            unsigned short SCId, Packet_address, CAOrg;
                            unsigned char i = 1, Rfa, DSCTy, SubChId, CAMode, SharedFlag;
                            char tmpbuf[256];
                            bool CAOrg_flag, DG_flag, Rfu;

                            while (i < (figlen - 4)) {
                                // iterate over service component in packet mode
                                SCId = ((unsigned short)f[i] << 4) | ((unsigned short)(f[i+1] >> 4) & 0x0F);
                                Rfa = (f[i+1] >> 1) & 0x07;
                                CAOrg_flag = f[i+1] & 0x01;
                                DG_flag = (f[i+2] >> 7) & 0x01;
                                Rfu = (f[i+2] >> 6) & 0x01;
                                DSCTy = f[i+2] & 0x3F;
                                SubChId = (f[i+3] >> 2);
                                Packet_address = ((unsigned short)(f[i+3] & 0x03) << 8) | ((unsigned short)f[i+4]);
                                sprintf(desc, "SCId=0x%X, CAOrg flag=%d CAOrg field %s, DG flag=%d data groups are %sused to transport the service component, DSCTy=%d %s, SubChId=0x%X, Packet address=0x%X",
                                        SCId, CAOrg_flag, CAOrg_flag?"present":"absent", DG_flag, DG_flag?"not ":"", DSCTy, DSCTy_types_str[DSCTy], SubChId, Packet_address);
                                if (Rfa != 0) {
                                    sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                    strcat(desc, tmpbuf);
                                }
                                if (Rfu != 0) {
                                    sprintf(tmpbuf, ", Rfu=%d invalid value", Rfu);
                                    strcat(desc, tmpbuf);
                                }
                                i += 5;
                                if (CAOrg_flag) {
                                    if (i < (figlen - 1)) {
                                        CAOrg = ((unsigned short)f[i] << 8) | ((unsigned short)f[i+1]);
                                        CAMode = (f[i] >> 5);
                                        SharedFlag = f[i+1];
                                        sprintf(tmpbuf, ", CAOrg=0x%X CAMode=%d \"%s\" SharedFlag=0x%X%s",
                                                CAOrg, CAMode, CAMode_str[CAMode], SharedFlag, (SharedFlag == 0)?" invalid":"");
                                        strcat(desc, tmpbuf);
                                    }
                                    else {
                                        sprintf(tmpbuf, ", invalid figlen");
                                        strcat(desc, tmpbuf);
                                    }
                                    i += 2;
                                }
                                printbuf(desc, indent+1, NULL, 0);
                            }
                        }
                        break;
                    case 5: // FIG 0/5 Service component language
                        {   // ETSI EN 300 401 8.1.2
                            unsigned short SCId;
                            unsigned char i = 1, SubChId, FIDCId, Language, Rfa;
                            char tmpbuf[256];
                            bool LS_flag, MSC_FIC_flag;

                            while (i < (figlen - 1)) {
                                // iterate over service component language
                                LS_flag = f[i] >> 7;
                                if (LS_flag == 0) {
                                    // Short form (L/S = 0)
                                    MSC_FIC_flag = (f[i] >> 6) & 0x01;
                                    Language = f[i+1];
                                    if (MSC_FIC_flag == 0) {
                                        // 0: MSC in Stream mode and SubChId identifies the sub-channel
                                        SubChId = f[i] & 0x3F;
                                        sprintf(desc, "L/S flag=%d short form, MSC/FIC flag=%d MSC, SubChId=0x%X, Language=0x%X %s",
                                                LS_flag, MSC_FIC_flag, SubChId, Language, fig0_5.Language_to_char(Language));
                                    }
                                    else {
                                        // 1: FIC and FIDCId identifies the component
                                        FIDCId = f[i] & 0x3F;
                                        sprintf(desc, "L/S flag=%d short form, MSC/FIC flag=%d FIC, FIDCId=0x%X, Language=0x%X %s",
                                                LS_flag, MSC_FIC_flag, FIDCId, Language, fig0_5.Language_to_char(Language));
                                    }
                                    printbuf(desc, indent+1, NULL, 0);
                                    i += 2;
                                }
                                else {
                                    // Long form (L/S = 1)
                                    if (i < (figlen - 2)) {
                                        Rfa = (f[i] >> 4) & 0x07;
                                        SCId = (((unsigned short)f[i] & 0x0F) << 8) | (unsigned short)f[i+1];
                                        Language = f[i+2];
                                        sprintf(desc, "L/S flag=%d long form", LS_flag);
                                        if (Rfa != 0) {
                                            sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                            strcat(desc, tmpbuf);
                                        }
                                        sprintf(tmpbuf, ", SCId=0x%X, Language=0x%X %s",
                                                SCId, Language, fig0_5.Language_to_char(Language));
                                        strcat(desc, tmpbuf);
                                        printbuf(desc, indent+1, NULL, 0);
                                    }
                                    i += 3;
                                    }
                            }
                        }
                        break;
                    case 6: // FIG 0/6 Service linking information
                        {   // ETSI EN 300 401 8.1.15
                            unsigned int j;
                            unsigned short LSN, key;
                            unsigned char i = 1, Number_of_Ids, IdLQ;
                            char signal_link[256];
                            bool Id_list_flag, LA, SH, ILS, Shd;

                            while (i < (figlen - 1)) {
                                // iterate over service linking
                                Id_list_flag  =  (f[i] >> 7) & 0x01;
                                LA  = (f[i] >> 6) & 0x01;
                                SH  = (f[i] >> 5) & 0x01;
                                ILS = (f[i] >> 4) & 0x01;
                                LSN = ((f[i] & 0x0F) << 8) | f[i+1];
                                key = (oe << 15) | (pd << 14) | (SH << 13) | (ILS << 12) | LSN;
                                strcpy(signal_link, "");
                                // check activation / deactivation
                                if ((fig0_6_key_la.count(key) > 0) && (fig0_6_key_la[key] != LA)) {
                                    if (LA == 0) {
                                        strcat(signal_link, " deactivated");
                                    }
                                    else {
                                        strcat(signal_link, " activated");
                                    }
                                }
                                fig0_6_key_la[key] = LA;
                                i += 2;
                                if (Id_list_flag == 0) {
                                    if (cn == 0) {  // Id_list_flag=0 && cn=0: CEI Change Event Indication
                                        strcat(signal_link, " CEI");
                                    }
                                    sprintf(desc, "Id list flag=%d, LA=%d %s, S/H=%d %s, ILS=%d %s, LSN=%d, database key=0x%04x%s",
                                            Id_list_flag, LA, (LA)?"active":"inactive", SH, (SH)?"Hard":"Soft", ILS, (ILS)?"international":"national", LSN, key, signal_link);
                                    printbuf(desc, indent+1, NULL, 0);
                                }
                                else {  // Id_list_flag == 1
                                    if (i < figlen) {
                                        Number_of_Ids = (f[i] & 0x0F);
                                        if (pd == 0) {
                                            IdLQ = (f[i] >> 5) & 0x03;
                                            Shd   = (f[i] >> 4) & 0x01;
                                            sprintf(desc, "Id list flag=%d, LA=%d %s, S/H=%d %s, ILS=%d %s, LSN=%d, database key=0x%04x, IdLQ=%d, Shd=%d %s, Number of Ids=%d%s",
                                                    Id_list_flag, LA, (LA)?"active":"inactive", SH, (SH)?"Hard":"Soft", ILS, (ILS)?"international":"national", LSN, key, IdLQ, Shd, (Shd)?"b11-8 in 4-F are different services":"single service", Number_of_Ids, signal_link);
                                            printbuf(desc, indent+1, NULL, 0);
                                            if (ILS == 0) {
                                                // read Id list
                                                for(j = 0; ((j < Number_of_Ids) && ((i+2+(j*2)) < figlen)); j++) {
                                                    // ETSI EN 300 401 8.1.15:
                                                    // The IdLQ shall not apply to the first entry in the Id list when OE = "0" and 
                                                    // when the version number of the type 0 field is set to "0" (using the C/N flag, see clause 5.2.2.1)
                                                    // ... , the first entry in the Id list of each Service linking field shall be 
                                                    // the SId that applies to the service in the ensemble.
                                                    if (((j == 0) && (oe == 0) && (cn == 0)) ||
                                                        (IdLQ == 0)) {
                                                        sprintf(desc, "DAB SId       0x%X", ((f[i+1+(j*2)] << 8) | f[i+2+(j*2)]));
                                                    }
                                                    else if (IdLQ == 1) {
                                                        sprintf(desc, "RDS PI        0x%X", ((f[i+1+(j*2)] << 8) | f[i+2+(j*2)]));
                                                    }
                                                    else if (IdLQ == 2) {
                                                        sprintf(desc, "AM-FM service 0x%X", ((f[i+1+(j*2)] << 8) | f[i+2+(j*2)]));
                                                    }
                                                    else {  // IdLQ == 3
                                                        sprintf(desc, "invalid ILS IdLQ configuration");
                                                    }
                                                    printbuf(desc, indent+2, NULL, 0);
                                                }
                                                // check deadlink
                                                if ((Number_of_Ids == 0) && (IdLQ == 1)) {
                                                    sprintf(desc, "deadlink");
                                                    printbuf(desc, indent+2, NULL, 0);
                                                }
                                                i += (Number_of_Ids * 2) + 1;
                                            }
                                            else {  // pd == 0 && ILS == 1
                                                // read Id list
                                                for(j = 0; ((j < Number_of_Ids) && ((i+3+(j*3)) < figlen)); j++) {
                                                    // ETSI EN 300 401 8.1.15:
                                                    // The IdLQ shall not apply to the first entry in the Id list when OE = "0" and 
                                                    // when the version number of the type 0 field is set to "0" (using the C/N flag, see clause 5.2.2.1)
                                                    // ... , the first entry in the Id list of each Service linking field shall be 
                                                    // the SId that applies to the service in the ensemble.
                                                    if (((j == 0) && (oe == 0) && (cn == 0)) ||
                                                        (IdLQ == 0)) {
                                                        sprintf(desc, "DAB SId          ecc 0x%02X Id 0x%04X", f[i+1+(j*3)], ((f[i+2+(j*3)] << 8) | f[i+3+(j*3)]));
                                                    }
                                                    else if (IdLQ == 1) {
                                                        sprintf(desc, "RDS PI           ecc 0x%02X Id 0x%04X", f[i+1+(j*3)], ((f[i+2+(j*3)] << 8) | f[i+3+(j*3)]));
                                                    }
                                                    else if (IdLQ == 2) {
                                                        sprintf(desc, "AM-FM service    ecc 0x%02X Id 0x%04X", f[i+1+(j*3)], ((f[i+2+(j*3)] << 8) | f[i+3+(j*3)]));
                                                    }
                                                    else {  // IdLQ == 3
                                                        sprintf(desc, "DRM/AMSS service ecc 0x%02X Id 0x%04X", f[i+1+(j*3)], ((f[i+2+(j*3)] << 8) | f[i+3+(j*3)]));
                                                    }
                                                    printbuf(desc, indent+2, NULL, 0);
                                                }
                                                // check deadlink
                                                if ((Number_of_Ids == 0) && (IdLQ == 1)) {
                                                    sprintf(desc, "deadlink");
                                                    printbuf(desc, indent+2, NULL, 0);
                                                }
                                                i += (Number_of_Ids * 3) + 1;
                                            }
                                        }
                                        else {  // pd == 1
                                            sprintf(desc, "Id list flag=%d, LA=%d %s, S/H=%d %s, ILS=%d %s, LSN=%d, database key=0x%04x, Number of Ids=%d%s",
                                                    Id_list_flag, LA, (LA)?"active":"inactive", SH, (SH)?"Hard":"Soft", ILS, (ILS)?"international":"national", LSN, key, Number_of_Ids, signal_link);
                                            printbuf(desc, indent+1, NULL, 0);
                                            if (Number_of_Ids > 0) {
                                                // read Id list
                                                for(j = 0; ((j < Number_of_Ids) && ((i+4+(j*4)) < figlen)); j++) {
                                                    sprintf(desc, "SId 0x%X",
                                                            ((f[i+1+(j*4)] << 24) | (f[i+2+(j*4)] << 16) | (f[i+3+(j*4)] << 8) | f[i+4+(j*4)]));
                                                    printbuf(desc, indent+2, NULL, 0);
                                                }
                                            }
                                            i += (Number_of_Ids * 4) + 1;
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    case 8: // FIG 0/8 Service component global definition
                        {   // ETSI EN 300 401 6.3.5
                            unsigned int SId;
                            unsigned short SCId;
                            unsigned char i = 1, Rfa, SCIdS, SubChId, FIDCId;
                            char tmpbuf[256];
                            bool Ext_flag, LS_flag, MSC_FIC_flag;

                            while (i < (figlen - (2 + (2 * pd)))) {
                                // iterate over service component global definition
                                if (pd == 0) {
                                    // Programme services, 16 bit SId
                                    SId = (f[i] << 8) | f[i+1];
                                    i += 2;
                                }
                                else {
                                    // Data services, 32 bit SId
                                    SId = ((unsigned int)f[i] << 24) | ((unsigned int)f[i+1] << 16) |
                                            ((unsigned int)f[i+2] << 8) | (unsigned int)f[i+3];
                                    i += 4;
                                }
                                Ext_flag = f[i] >> 7;
                                Rfa = (f[i] >> 4) & 0x7;
                                SCIdS = f[i] & 0x0F;
                                sprintf(desc, "SId=0x%X, Ext flag=%d 8-bit Rfa %s", SId, Ext_flag, (Ext_flag)?"present":"absent");
                                if (Rfa != 0) {
                                    sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                    strcat(desc, tmpbuf);
                                }
                                sprintf(tmpbuf, ", SCIdS=0x%X", SCIdS);
                                strcat(desc, tmpbuf);
                                i++;
                                if (i < figlen) {
                                    LS_flag = f[i] >> 7;
                                    sprintf(tmpbuf, ", L/S flag=%d %s", LS_flag, (LS_flag)?"Long form":"Short form");
                                    strcat(desc, tmpbuf);
                                    if (LS_flag == 0) {
                                        // Short form
                                        if (i < (figlen - Ext_flag)) {
                                            MSC_FIC_flag = (f[i] >> 6) & 0x01;
                                            if (MSC_FIC_flag == 0) {
                                                // MSC in stream mode and SubChId identifies the sub-channel
                                                SubChId = f[i] & 0x3F;
                                                sprintf(tmpbuf, ", MSC/FIC flag=%d MSC, SubChId=0x%X", MSC_FIC_flag, SubChId);
                                                strcat(desc, tmpbuf);
                                            }
                                            else {
                                                // FIC and FIDCId identifies the component
                                                FIDCId = f[i] & 0x3F;
                                                sprintf(tmpbuf, ", MSC/FIC flag=%d FIC, FIDCId=0x%X", MSC_FIC_flag, FIDCId);
                                                strcat(desc, tmpbuf);
                                            }
                                            if (Ext_flag == 1) {
                                                // Rfa field present
                                                Rfa = f[i+1];
                                                if (Rfa != 0) {
                                                    sprintf(tmpbuf, ", Rfa=0x%X invalid value", Rfa);
                                                    strcat(desc, tmpbuf);
                                                }
                                            }
                                        }
                                        i += (1 + Ext_flag);
                                    }
                                    else {
                                        // Long form
                                        if (i < (figlen - 1)) {
                                            Rfa = (f[i] >> 4) & 0x07;
                                            SCId = (((unsigned short)f[i] & 0x0F) << 8) | (unsigned short)f[i+1];
                                            if (Rfa != 0) {
                                                sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                                strcat(desc, tmpbuf);
                                            }
                                            sprintf(tmpbuf, ", SCId=0x%X", SCId);
                                            strcat(desc, tmpbuf);
                                        }
                                        i += 2;
                                    }
                                }
                                printbuf(desc, indent+1, NULL, 0);
                            }
                        }
                        break;
                    case 9: // FIG 0/9 Country, LTO and International table
                        {   // ETSI EN 300 401 8.1.3.2
                            unsigned int SId;
                            unsigned char i = 1, j, key, Number_of_services, ECC;
                            signed char LTO;
                            char tmpbuf[256];
                            bool Ext_flag;

                            if (i < (figlen - 2)) {
                                // get Ensemble LTO, ECC and International Table Id
                                key = ((unsigned char)oe << 1) | (unsigned char)pd;
                                Ext_flag = f[i] >> 7;
                                LTO_uniq = (f[i]>> 6) & 0x01;
                                Ensemble_LTO = f[i] & 0x3F;
                                if (Ensemble_LTO & 0x20) {
                                    // negative Ensemble LTO
                                    Ensemble_LTO |= 0xC0;
                                }
                                sprintf(desc, "Ext flag=%d extended field %s, LTO uniq=%d %s, Ensemble LTO=0x%X %s%d:%02d",
                                        Ext_flag, Ext_flag?"present":"absent", LTO_uniq,
                                        LTO_uniq?"several time zones":"one time zone (time specified by Ensemble LTO)",
                                        (Ensemble_LTO & 0x3F), (Ensemble_LTO >= 0)?"":"-" , abs(Ensemble_LTO) >> 1, (Ensemble_LTO & 0x01) * 30);
                                if (abs(Ensemble_LTO) > 24) {
                                    sprintf(tmpbuf, " out of range -12 hours to +12 hours");
                                    strcat(desc, tmpbuf);
                                }
                                Ensemble_ECC = f[i+1];
                                International_Table_Id = f[i+2];
                                sprintf(tmpbuf, ", Ensemble ECC=0x%X, International Table Id=0x%X, database key=0x%x",
                                        Ensemble_ECC, International_Table_Id, key);
                                strcat(desc, tmpbuf);
                                printbuf(desc, indent+1, NULL, 0);
                                i += 3;
                                if (Ext_flag == 1) {
                                    // extended field present
                                    while (i < figlen) {
                                        // iterate over extended sub-field
                                        Number_of_services = f[i] >> 6;
                                        LTO = f[i] & 0x3F;
                                        if (LTO & 0x20) {
                                            // negative LTO
                                            LTO |= 0xC0;
                                        }
                                        sprintf(desc, "Number of services=%d, LTO=0x%X %s%d:%02d",
                                                Number_of_services, (LTO & 0x3F), (LTO >= 0)?"":"-" , abs(LTO) >> 1,  (LTO & 0x01) * 30);
                                        if (abs(LTO) > 24) {
                                            sprintf(tmpbuf, " out of range -12 hours to +12 hours");
                                            strcat(desc, tmpbuf);
                                        }
                                        // CEI Change Event Indication
                                        if ((Number_of_services == 0) && (LTO == 0) /* && (Ext_flag == 1) */) {
                                            sprintf(tmpbuf, ", CEI");
                                            strcat(desc, tmpbuf);
                                        }
                                        i++;
                                        if (pd == 0) {
                                            // Programme services, 16 bit SId
                                            if (i < figlen) {
                                                ECC = f[i];
                                                sprintf(tmpbuf, ", ECC=0x%X", ECC);
                                                strcat(desc, tmpbuf);
                                                printbuf(desc, indent+2, NULL, 0);
                                                i++;
                                                for(j = i; ((j < (i + (Number_of_services * 2))) && (j < figlen)); j += 2) {
                                                    // iterate over SId
                                                    SId = ((unsigned int)f[j] << 8) | (unsigned int)f[j+1];
                                                    sprintf(desc, "SId 0x%X", SId);
                                                    printbuf(desc, indent+3, NULL, 0);
                                                }
                                                i += (Number_of_services * 2);
                                            }
                                        }
                                        else {
                                            // Data services, 32 bit SId
                                            printbuf(desc, indent+2, NULL, 0);
                                            for(j = i; ((j < (i + (Number_of_services * 4))) && (j < figlen)); j += 4) {
                                                // iterate over SId
                                                SId = ((unsigned int)f[j] << 24) | ((unsigned int)f[j+1] << 16) |
                                                        ((unsigned int)f[j+2] << 8) | (unsigned int)f[j+3];
                                                sprintf(desc, "SId 0x%X", SId);
                                                printbuf(desc, indent+3, NULL, 0);
                                            }
                                            i += (Number_of_services * 4);
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    case 10: // FIG 0/10 Date and time
                        {    // ETSI EN 300 401 8.1.3.1
                            char dateStr[256];
                            dateStr[0] = 0;

                            //bool RFU = f[1] >> 7;

                            uint32_t MJD = (((uint32_t)f[1] & 0x7F) << 10)    |
                                           ((uint32_t)(f[2]) << 2) |
                                           (f[3] >> 6);
                            sprintfMJD(dateStr, MJD);

                            bool LSI = f[3] & 0x20;
                            bool ConfInd = f[3] & 0x10;
                            wm_decoder.push_confind_bit(ConfInd);
                            bool UTC = f[3] & 0x8;

                            uint8_t hours = ((f[3] & 0x7) << 2) |
                                            ( f[4] >> 6);

                            uint8_t minutes = f[4] & 0x3f;

                            if (UTC) {
                                uint8_t seconds = f[5] >> 2;
                                uint16_t milliseconds = ((uint16_t)(f[5] & 0x2) << 8) | f[6];

                                sprintf(desc, "FIG %d/%d(long): MJD=0x%X %s, LSI %u, ConfInd %u, UTC Time: %02d:%02d:%02d.%d",
                                        figtype, ext, MJD, dateStr, LSI, ConfInd, hours, minutes, seconds, milliseconds);
                                printbuf(desc, indent+1, NULL, 0);
                            }
                            else {
                                sprintf(desc, "FIG %d/%d(short): MJD=0x%X %s, LSI %u, ConfInd %u, UTC Time: %02d:%02d",
                                        figtype, ext, MJD, dateStr, LSI, ConfInd, hours, minutes);
                                printbuf(desc, indent+1, NULL, 0);
                            }
                        }
                        break;
                    case 11: // FIG 0/11 Region definition
                        {    // ETSI EN 300 401 8.1.16.1
                            Lat_Lng gps_pos = {0, 0};
                            signed short Latitude_coarse, Longitude_coarse;
                            unsigned short Region_Id, Extent_Latitude, Extent_Longitude, key;
                            unsigned char i = 1, j, k, GATy, Rfu, Length_TII_list, Rfa, MainId, Length_SubId_list, SubId;
                            signed char bit_pos;
                            char tmpbuf[256];
                            bool GE_flag;

                            while (i < (figlen - 1)) {
                                // iterate over Region definition
                                GATy = f[i] >> 4;
                                GE_flag = (f[i] >> 3) & 0x01;
                                Region_Id = ((unsigned short)(f[i] & 0x07) << 8) | ((unsigned short)f[i+1]);
                                key = ((unsigned short)oe << 12) | ((unsigned short)pd << 11) | Region_Id;
                                i += 2;
                                if (GATy == 0) {
                                    // TII list
                                    sprintf(desc, "GATy=%d Geographical area defined by a TII list, G/E flag=%d %s coverage area, RegionId=0x%X, database key=0x%X",
                                            GATy, GE_flag, GE_flag?"Global":"Ensemble", Region_Id, key);
                                    if (i < figlen) {
                                        Rfu = f[i] >> 5;
                                        if (Rfu != 0) {
                                            sprintf(tmpbuf, ", Rfu=%d invalid value", Rfu);
                                            strcat(desc, tmpbuf);
                                        }
                                        Length_TII_list = f[i] & 0x1F;
                                        sprintf(tmpbuf, ", Length of TII list=%d", Length_TII_list);
                                        strcat(desc, tmpbuf);
                                        if (Length_TII_list == 0) {
                                            strcat(desc, ", CEI");
                                        }
                                        printbuf(desc, indent+1, NULL, 0);
                                        i++;

                                        for(j = 0;(i < (figlen - 1)) && (j < Length_TII_list); j++) {
                                            // iterate over Transmitter group
                                            Rfa = f[i] >> 7;
                                            MainId = f[i] & 0x7F;
                                            if (Rfa != 0) {
                                                sprintf(desc, "Rfa=%d invalid value, MainId=0x%X",
                                                        Rfa, MainId);
                                            }
                                            else {
                                                sprintf(desc, "MainId=0x%X", MainId);
                                            }
                                            // check MainId value
                                            if ((Mode_Identity == 1) || (Mode_Identity == 2) || (Mode_Identity == 4)) {
                                                if (MainId > 69) {
                                                    // The coding range shall be 0 to 69 for transmission modes I, II and IV
                                                    sprintf(tmpbuf, " invalid value for transmission mode %d", Mode_Identity);
                                                    strcat(desc, tmpbuf);
                                                }
                                            }
                                            else if (Mode_Identity == 3) {
                                                if (MainId > 5) {
                                                    // The coding range shall be 0 to 5 for transmission modes I, II and IV
                                                    sprintf(tmpbuf, " invalid value for transmission mode %d", Mode_Identity);
                                                    strcat(desc, tmpbuf);
                                                }
                                            }
                                            Rfa = f[i+1] >> 5;
                                            if (Rfa != 0) {
                                                sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                                strcat(desc, tmpbuf);
                                            }
                                            Length_SubId_list = f[i+1] & 0x1F;
                                            sprintf(tmpbuf, ", Length of SubId=%d", Length_SubId_list);
                                            strcat(desc, tmpbuf);
                                            printbuf(desc, indent+2, NULL, 0);
                                            i += 2;

                                            bit_pos = 3;
                                            SubId = 0;
                                            for(k = 0;(i < figlen) && (k < Length_SubId_list); k++) {
                                                // iterate SubId
                                                if (bit_pos >= 0) {
                                                    SubId |= (f[i] >> bit_pos) & 0x1F;
                                                    sprintf(desc, "SubId=0x%X", SubId);
                                                    // check SubId value
                                                    if ((SubId == 0) || (SubId > 23)) {
                                                        strcat(desc, " invalid value");
                                                    }
                                                    printbuf(desc, indent+3, NULL, 0);
                                                    bit_pos -= 5;
                                                    SubId = 0;
                                                }
                                                if (bit_pos < 0) {
                                                    SubId = (f[i] << abs(bit_pos)) & 0x1F;
                                                    bit_pos += 8;
                                                    i++;
                                                }
                                            }
                                            if (bit_pos > 3) {
                                                // jump padding
                                                i++;
                                            }
                                            if (k < Length_SubId_list) {
                                                sprintf(desc, "%d SubId missing, fig length too short !", (Length_SubId_list - k));
                                                printbuf(desc, indent+3, NULL, 0);
                                                fprintf(stderr, "WARNING: FIG %d/%d length %d too short !\n", figtype, ext, figlen);
                                            }
                                        }
                                        if (j < Length_TII_list) {
                                            sprintf(desc, "%d Transmitter group missing, fig length too short !", (Length_TII_list - j));
                                            printbuf(desc, indent+2, NULL, 0);
                                            fprintf(stderr, "WARNING: FIG %d/%d length %d too short !\n", figtype, ext, figlen);
                                        }
                                    }
                                }
                                else if (GATy == 1) {
                                    // Coordinates
                                    sprintf(desc, "GATy=%d Geographical area defined as a spherical rectangle by the geographical co-ordinates of one corner and its latitude and longitude extents, G/E flag=%d %s coverage area, RegionId=0x%X, database key=0x%X",
                                            GATy, GE_flag, GE_flag?"Global":"Ensemble", Region_Id, key);
                                    if (i < (figlen - 6)) {
                                        Latitude_coarse = ((signed short)f[i] << 8) | ((unsigned short)f[i+1]);
                                        Longitude_coarse = ((signed short)f[i+2] << 8) | ((unsigned short)f[i+3]);
                                        gps_pos.latitude = ((double)Latitude_coarse) * 90 / 32768;
                                        gps_pos.longitude = ((double)Latitude_coarse) * 180 / 32768;
                                        sprintf(tmpbuf, ", Lat Lng coarse=0x%X 0x%X => %f, %f",
                                                Latitude_coarse, Longitude_coarse, gps_pos.latitude, gps_pos.longitude);
                                        strcat(desc, tmpbuf);
                                        Extent_Latitude = ((unsigned short)f[i+4] << 4) | ((unsigned short)(f[i+5] >> 4));
                                        Extent_Longitude = ((unsigned short)(f[i+5] & 0x0F) << 8) | ((unsigned short)f[i+6]);
                                        gps_pos.latitude += ((double)Extent_Latitude) * 90 / 32768;
                                        gps_pos.longitude += ((double)Extent_Longitude) * 180 / 32768;
                                        sprintf(tmpbuf, ", Extent Lat Lng=0x%X 0x%X => %f, %f",
                                                Extent_Latitude, Extent_Longitude, gps_pos.latitude, gps_pos.longitude);
                                        strcat(desc, tmpbuf);
                                    }
                                    else {
                                        sprintf(tmpbuf, ", Coordinates missing, fig length too short !");
                                        strcat(desc, tmpbuf);
                                        fprintf(stderr, "WARNING: FIG %d/%d length %d too short !\n", figtype, ext, figlen);
                                    }
                                    printbuf(desc, indent+1, NULL, 0);
                                    i += 7;
                                }
                                else {
                                    // Rfu
                                    sprintf(desc, "GATy=%d reserved for future use of the geographical, G/E flag=%d %s coverage area, RegionId=0x%X, database key=0x%X, stop Region definition iteration %d/%d",
                                            GATy, GE_flag, GE_flag?"Global":"Ensemble", Region_Id, key, i, figlen);
                                    printbuf(desc, indent+1, NULL, 0);
                                    // stop Region definition iteration
                                    i = figlen;
                                }
                            }
                        }
                        break;
                    case 13: // FIG 0/13 User application information
                        {    // ETSI EN 300 401 8.1.20
                            uint32_t SId;
                            uint8_t  SCIdS;
                            uint8_t  No;

                            int k = 1;

                            if (pd == 0) { // Programme services, 16 bit SId
                                SId   = (f[k] << 8) |
                                         f[k+1];
                                k+=2;

                                SCIdS = f[k] >> 4;
                                No    = f[k] & 0x0F;
                                k++;
                            }
                            else { // Data services, 32 bit SId
                                SId   = (f[k]   << 24) |
                                        (f[k+1] << 16) |
                                        (f[k+2] << 8) |
                                         f[k+3];
                                k+=4;

                                SCIdS = f[k] >> 4;
                                No    = f[k] & 0x0F;
                                k++;

                            }

                            sprintf(desc, "FIG %d/%d: SId=0x%X SCIdS=%u No=%u",
                                    figtype, ext, SId, SCIdS, No);
                            printbuf(desc, indent+1, NULL, 0);

                            for (int numapp = 0; numapp < No; numapp++) {
                                uint16_t user_app_type = ((f[k] << 8) |
                                                         (f[k+1] & 0xE0)) >> 5;
                                uint8_t  user_app_len  = f[k+1] & 0x1F;
                                k+=2;

                                sprintf(desc, "User Application %d '%s'; length %u",
                                        user_app_type,
                                        get_fig_0_13_userapp(user_app_type).c_str(),
                                        user_app_len);
                                printbuf(desc, indent+2, NULL, 0);
                            }
                        }
                        break;
                    case 14: // FIG 0/14 FEC sub-channel organization
                        {    // ETSI EN 300 401 6.2.2
                            unsigned char i = 1, SubChId, FEC_scheme;

                            while (i < figlen) {
                                // iterate over Sub-channel
                                SubChId = f[i] >> 2;
                                FEC_scheme = f[i] & 0x3;
                                sprintf(desc, "SubChId=0x%X, FEC scheme=%d %s",
                                        SubChId, FEC_scheme, FEC_schemes_str[FEC_scheme]);
                                printbuf(desc, indent+1, NULL, 0);
                                i++;
                            }
                        }
                        break;
                    case 16: // FIG 0/16 Programme Number & OE Programme Number
                        {    // ETSI EN 300 401 8.1.4 & 8.1.10.3
                            unsigned short SId, PNum, New_SId, New_PNum;
                            unsigned char i = 1, Rfa, Rfu;
                            char tmpbuf[256];
                            bool Continuation_flag, Update_flag;

                            while (i < (figlen - 4)) {
                                // iterate over Programme Number
                                SId = ((unsigned short)f[i] << 8) | ((unsigned short)f[i+1]);
                                PNum = ((unsigned short)f[i+2] << 8) | ((unsigned short)f[i+3]);
                                Rfa = f[i+4] >> 6;
                                Rfu = (f[i+4] >> 2) & 0x0F;
                                Continuation_flag = (f[i+4] >> 1) & 0x01;
                                Update_flag = f[i+4] & 0x01;

                                sprintf(desc, "SId=0x%X, PNum=0x%X ", SId, PNum);
                                // Append PNum decoded string
                                strcatPNum(desc, PNum);

                                if (Rfa != 0) {
                                    sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                    strcat(desc, tmpbuf);
                                }

                                if (Rfu != 0) {
                                    sprintf(tmpbuf, ", Rfu=0x%X invalid value", Rfu);
                                    strcat(desc, tmpbuf);
                                }

                                sprintf(tmpbuf, ", Continuation flag=%d the programme will %s, Update flag=%d %sre-direction",
                                        Continuation_flag, Continuation_flag?"be interrupted but continued later":"not be subject to a planned interruption",
                                        Update_flag, Update_flag?"":"no ");
                                strcat(desc, tmpbuf);
                                i += 5;

                                if (Update_flag != 0) {
                                    // In the case of a re-direction, the New SId and New PNum shall be appended
                                    if (i < (figlen - 1)) {
                                        New_SId = ((unsigned short)f[i] << 8) | ((unsigned short)f[i+1]);
                                        sprintf(tmpbuf, ", New SId=0x%X", New_SId);
                                        strcat(desc, tmpbuf);
                                        if (i < (figlen - 3)) {
                                            New_PNum = ((unsigned short)f[i+2] << 8) | ((unsigned short)f[i+3]);
                                            sprintf(tmpbuf, ", New PNum=0x%X ", New_PNum);
                                            strcat(desc, tmpbuf);
                                            // Append New_PNum decoded string
                                            strcatPNum(desc, New_PNum);
                                        }
                                        else {
                                            sprintf(tmpbuf, ", missing New PNum !");
                                            strcat(desc, tmpbuf);
                                        }
                                    }
                                    else {
                                        sprintf(tmpbuf, ", missing New SId and New PNum !");
                                        strcat(desc, tmpbuf);
                                    }
                                    i += 4;
                                }

                                printbuf(desc, indent+1, NULL, 0);
                            }
                        }
                        break;
                    case 17: // FIG 0/17 Programme Type
                        {    // ETSI EN 300 401 8.1.5
                            unsigned short SId;
                            unsigned char i = 1, Rfa, Language, Int_code, Comp_code;
                            char tmpbuf[256];
                            bool SD_flag, PS_flag, L_flag, CC_flag, Rfu;
                            while (i < (figlen - 3)) {
                                // iterate over announcement support
                                SId = (f[i] << 8) | f[i+1];
                                SD_flag = (f[i+2] >> 7);
                                PS_flag = ((f[i+2] >> 6) & 0x01);
                                L_flag = ((f[i+2] >> 5) & 0x01);
                                CC_flag = ((f[i+2] >> 4) & 0x01);
                                Rfa = (f[i+2] & 0x0F);
                                sprintf(desc, "SId=0x%X, S/D=%d Programme Type codes and language (when present), %srepresent the current programme contents, P/S=%d %s service component, L flag=%d language field %s, CC flag=%d complementary code and preceding Rfa and Rfu fields %s",
                                        SId, SD_flag, SD_flag?"":"may not ", PS_flag, PS_flag?"secondary":"primary",
                                        L_flag, L_flag?"present":"absent", CC_flag, CC_flag?"present":"absent");
                                if (Rfa != 0) {
                                    sprintf(tmpbuf, ", Rfa=0x%X invalid value", Rfa);
                                    strcat(desc, tmpbuf);
                                }
                                i += 3;
                                if (L_flag != 0) {
                                    if (i < figlen) {
                                        Language = f[i];
                                        sprintf(tmpbuf, ", Language=0x%X %s", Language,
                                                fig0_5.Language_to_char(Language));
                                        strcat(desc, tmpbuf);
                                    }
                                    else {
                                        sprintf(tmpbuf, ", Language= invalid FIG length");
                                        strcat(desc, tmpbuf);
                                    }
                                    i++;
                                }
                                if (i < figlen) {
                                    Rfa = f[i] >> 6;
                                    Rfu = (f[i] >> 5) & 0x01;
                                    if (Rfa != 0) {
                                        sprintf(tmpbuf, ", Rfa=0x%X invalid value", Rfa);
                                        strcat(desc, tmpbuf);
                                    }
                                    if (Rfu != 0) {
                                        sprintf(tmpbuf, ", Rfu=%d invalid value", Rfu);
                                        strcat(desc, tmpbuf);
                                    }
                                    Int_code = f[i] & 0x1F;
                                    sprintf(tmpbuf, ", Int code=0x%X %s", Int_code, get_programme_type_str(International_Table_Id , Int_code));
                                    strcat(desc, tmpbuf);
                                    i++;
                                }
                                else {
                                    sprintf(tmpbuf, ", Int code= invalid FIG length");
                                    strcat(desc, tmpbuf);
                                }
                                if (CC_flag != 0) {
                                    if (i < figlen) {
                                        Rfa = f[i] >> 6;
                                        Rfu = (f[i] >> 5) & 0x01;
                                        if (Rfa != 0) {
                                            sprintf(tmpbuf, ", Rfa=0x%X invalid value", Rfa);
                                            strcat(desc, tmpbuf);
                                        }
                                        if (Rfu != 0) {
                                            sprintf(tmpbuf, ", Rfu=%d invalid value", Rfu);
                                            strcat(desc, tmpbuf);
                                        }
                                        Comp_code = f[i] & 0x1F;
                                        sprintf(tmpbuf, ", Comp code=0x%X %s", Comp_code, get_programme_type_str(International_Table_Id , Comp_code));
                                        strcat(desc, tmpbuf);
                                        i++;
                                    }
                                    else {
                                        sprintf(tmpbuf, ", Comp code= invalid FIG length");
                                        strcat(desc, tmpbuf);
                                    }
                                }
                                printbuf(desc, indent+1, NULL, 0);
                            }
                        }
                        break;
                    case 18: // FIG 0/18 Announcement support
                        {    // ETSI EN 300 401 8.1.6.1
                            unsigned int key;
                            unsigned short SId, Asu_flags;
                            unsigned char i = 1, j, Rfa, Number_clusters;
                            char tmpbuf[256];

                            while (i < (figlen - 4)) {
                                // iterate over announcement support
                                // SId, Asu flags, Rfa, Number of clusters
                                SId = ((unsigned short)f[i] << 8) | (unsigned short)f[i+1];
                                Asu_flags = ((unsigned short)f[i+2] << 8) | (unsigned short)f[i+3];
                                Rfa = (f[i+4] >> 5);
                                Number_clusters = (f[i+4] & 0x1F);
                                sprintf(desc, "SId=0x%X, Asu flags=0x%04x", SId, Asu_flags);
                                if (Rfa != 0) {
                                    sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                    strcat(desc, tmpbuf);
                                }
                                sprintf(tmpbuf, ", Number of clusters=%d", Number_clusters);
                                strcat(desc, tmpbuf);
                                key = ((unsigned int)oe << 17) | ((unsigned int)pd << 16) | (unsigned int)SId;
                                sprintf(tmpbuf, ", database key=0x%05x", key);
                                strcat(desc, tmpbuf);
                                // CEI Change Event Indication
                                if ((Number_clusters == 0) && (Asu_flags == 0)) {
                                    sprintf(tmpbuf, ", CEI");
                                    strcat(desc, tmpbuf);
                                }
                                printbuf(desc, indent+1, NULL, 0);
                                i += 5;

                                for(j = 0; (j < Number_clusters) && (i < figlen); j++) {
                                    // iterate over Cluster Id
                                    sprintf(desc, "Cluster Id=0x%X", f[j]);
                                    printbuf(desc, indent+2, NULL, 0);
                                    i++;
                                }
                                if (j < Number_clusters) {
                                    sprintf(desc, "missing Cluster Id, fig length too short !");
                                    printbuf(desc, indent+1, NULL, 0);
                                    fprintf(stderr, "WARNING: FIG %d/%d length %d too short !\n", figtype, ext, figlen);
                                }

                                // decode announcement support types
                                for(j = 0; j < 16; j++) {
                                    if (Asu_flags & (1 << j)) {
                                        sprintf(desc, "Announcement support=%s", announcement_types_str[j]);
                                        printbuf(desc, indent+2, NULL, 0);
                                    }
                                }
                            }
                        }
                        break;
                    case 19: // FIG 0/19 Announcement switching
                        {    // ETSI EN 300 401 8.1.6.2
                            unsigned short Asw_flags;
                            unsigned char i = 1, j, Cluster_Id, SubChId, Rfa, RegionId_LP;
                            char tmpbuf[256];
                            bool New_flag, Region_flag;

                            while (i < (figlen - 3)) {
                                // iterate over announcement switching
                                // Cluster Id, Asw flags, New flag, Region flag,
                                // SubChId, Rfa, Region Id Lower Part
                                Cluster_Id = f[i];
                                Asw_flags = ((unsigned short)f[i+1] << 8) | (unsigned short)f[i+2];
                                New_flag = (f[i+3] >> 7);
                                Region_flag = (f[i+3] >> 6) & 0x1;
                                SubChId = (f[i+3] & 0x3F);
                                sprintf(desc, "Cluster Id=0x%02x, Asw flags=0x%04x, New flag=%d %s, Region flag=%d last byte %s, SubChId=%d",
                                        Cluster_Id, Asw_flags, New_flag, (New_flag)?"new":"repeat", Region_flag, (Region_flag)?"present":"absent", SubChId);
                                if (Region_flag) {
                                    if (i < (figlen - 4)) {
                                        // read region lower part
                                        Rfa = (f[i+4] >> 6);
                                        RegionId_LP = (f[i+4] & 0x3F);
                                        if (Rfa != 0) {
                                            sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                            strcat(desc, tmpbuf);
                                        }
                                        sprintf(tmpbuf, ", Region Lower Part=0x%02x", RegionId_LP);
                                        strcat(desc, tmpbuf);
                                    }
                                    else {
                                        sprintf(tmpbuf, "missing Region Lower Part, fig length too short !");
                                        strcat(desc, tmpbuf);
                                        fprintf(stderr, "WARNING: FIG %d/%d length %d too short !\n", figtype, ext, figlen);
                                    }
                                }
                                printbuf(desc, indent+1, NULL, 0);
                                // decode announcement switching types
                                for(j = 0; j < 16; j++) {
                                    if (Asw_flags & (1 << j)) {
                                        sprintf(desc, "Announcement switching=%s", announcement_types_str[j]);
                                        printbuf(desc, indent+2, NULL, 0);
                                    }
                                }
                                i += (4 + Region_flag);
                            }
                        }
                        break;
                    case 21: // FIG 0/21 Frequency Information
                        {    // ETSI EN 300 401 8.1.8
                            float freq;
                            unsigned int ifreq;
                            unsigned long long key;
                            unsigned short RegionId, Id_field;
                            unsigned char i = 1, j, k, Length_FI_list, RandM, Length_Freq_list, Control_field;
                            unsigned char Control_field_trans_mode, Id_field2;
                            char tmpbuf[256];
                            bool Continuity_flag;

                            while (i < (figlen - 1)) {
                                // iterate over frequency information
                                // decode RegionId, Length of FI list
                                RegionId  =  (f[i] << 3) | (f[i+1] >> 5);
                                Length_FI_list  = (f[i+1] & 0x1F);
                                sprintf(desc, "RegionId=0x%03x", RegionId);
                                printbuf(desc, indent+1, NULL, 0);
                                i += 2;
                                if ((i + Length_FI_list) <= figlen) {
                                    j = i;
                                    while ((j + 2) < (i + Length_FI_list)) {
                                        // iterate over FI list x
                                        // decode Id field, R&M, Continuity flag, Length of Freq list
                                        Id_field = (f[j] << 8) | f[j+1];
                                        RandM = f[j+2] >> 4;
                                        Continuity_flag = (f[j+2] >> 3) & 0x01;
                                        Length_Freq_list = f[j+2] & 0x07;
                                        sprintf(desc, "Id_field=");
                                        switch (RandM) {
                                            case 0x0:
                                            case 0x1:
                                                strcat(desc, "EId");
                                                break;
                                            case 0x6:
                                                strcat(desc, "DRM Service Id");
                                                break;
                                            case 0x8:
                                                strcat(desc, "RDS PI");
                                                break;
                                            case 0x9:
                                            case 0xa:
                                            case 0xc:
                                                strcat(desc, "Dummy");
                                                break;
                                            case 0xe:
                                                strcat(desc, "AMSS Service Id");
                                                break;
                                            default:
                                                strcat(desc, "invalid");
                                                break;
                                        }
                                        sprintf(tmpbuf, "=0x%X, R&M=0x%1x", Id_field, RandM);
                                        strcat(desc, tmpbuf);
                                        switch (RandM) {
                                            case 0x0:
                                                strcat(desc, "=DAB ensemble, no local windows");
                                                break;
                                            case 0x6:
                                                strcat(desc, "=DRM");
                                                break;
                                            case 0x8:
                                                strcat(desc, "=FM with RDS");
                                                break;
                                            case 0x9:
                                                strcat(desc, "=FM without RDS");
                                                break;
                                            case 0xa:
                                                strcat(desc, "=AM (MW in 9 kHz steps & LW)");
                                                break;
                                            case 0xc:
                                                strcat(desc, "=AM (MW in 5 kHz steps & SW)");
                                                break;
                                            case 0xe:
                                                strcat(desc, "=AMSS");
                                                break;
                                            default:
                                                strcat(desc, "=Rfu");
                                                break;
                                        }
                                        sprintf(tmpbuf, ", Continuity flag=%d", Continuity_flag);
                                        strcat(desc, tmpbuf);
                                        if ((oe == 0) || ((oe == 1) && (RandM != 0x6) && \
                                            ((RandM < 0x8) || (RandM > 0xa)) && (RandM != 0xc) && (RandM != 0xe))) {
                                            if (Continuity_flag == 0) {
                                                switch (RandM) {
                                                    case 0x0:
                                                    case 0x1:
                                                        strcat(desc, "=continuous output not expected");
                                                        break;
                                                    case 0x6:
                                                        strcat(desc, "=no compensating time delay on DRM audio signal");
                                                        break;
                                                    case 0x8:
                                                    case 0x9:
                                                        strcat(desc, "=no compensating time delay on FM audio signal");
                                                        break;
                                                    case 0xa:
                                                    case 0xc:
                                                    case 0xe:
                                                        strcat(desc, "=no compensating time delay on AM audio signal");
                                                        break;
                                                    default:
                                                        strcat(desc, "=Rfu");
                                                        break;
                                                }
                                            }
                                            else {  // Continuity_flag == 1
                                                switch (RandM) {
                                                    case 0x0:
                                                    case 0x1:
                                                        strcat(desc, "=continuous output possible");
                                                        break;
                                                    case 0x6:
                                                        strcat(desc, "=compensating time delay on DRM audio signal");
                                                        break;
                                                    case 0x8:
                                                    case 0x9:
                                                        strcat(desc, "=compensating time delay on FM audio signal");
                                                        break;
                                                    case 0xa:
                                                    case 0xc:
                                                    case 0xe:
                                                        strcat(desc, "=compensating time delay on AM audio signal");
                                                        break;
                                                    default:
                                                        strcat(desc, "=Rfu");
                                                        break;
                                                }
                                            }
                                        }
                                        else {  // oe == 1
                                            strcat(desc, "=reserved for future addition");
                                        }
                                        key = ((unsigned long long)oe << 32) | ((unsigned long long)pd << 31) | \
                                                ((unsigned long long)RegionId << 20) | ((unsigned long long)Id_field << 4) | \
                                                (unsigned long long)RandM;
                                        sprintf(tmpbuf, ", database key=0x%09llx", key);
                                        // CEI Change Event Indication
                                        if (Length_Freq_list == 0) {
                                            strcat(tmpbuf, ", CEI");
                                        }
                                        strcat(desc, tmpbuf);
                                        printbuf(desc, indent+2, NULL, 0);
                                        j += 3; // add header

                                        k = j;
                                        switch (RandM) {
                                            case 0x0:
                                            case 0x1:
                                                while((k + 2) < (j + Length_Freq_list)) {
                                                    // iteration over Freq list
                                                    ifreq = (((unsigned int)(f[k] & 0x07) << 16) | ((unsigned int)f[k+1] << 8) | (unsigned int)f[k+2]) * 16;
                                                    if (ifreq != 0) {
                                                        Control_field = (f[k] >> 3);
                                                        Control_field_trans_mode = (Control_field >> 1) & 0x07;
                                                        if ((Control_field & 0x10) == 0) {
                                                            sprintf(desc, "%d KHz, ", ifreq);
                                                            if ((Control_field & 0x01) == 0) {
                                                                strcat(desc, "geographically adjacent area, ");
                                                            }
                                                            else {  // (Control_field & 0x01) == 1
                                                                strcat(desc, "no geographically adjacent area, ");
                                                            }
                                                            if (Control_field_trans_mode == 0) {
                                                                strcat(desc, "no transmission mode signalled");
                                                            }
                                                            else if (Control_field_trans_mode <= 4) {
                                                                sprintf(tmpbuf, "transmission mode %d", Control_field_trans_mode);
                                                                strcat(desc, tmpbuf);
                                                            }
                                                            else {  // Control_field_trans_mode > 4
                                                                sprintf(tmpbuf, "invalid transmission mode 0x%x", Control_field_trans_mode);
                                                                strcat(desc, tmpbuf);
                                                            }
                                                        }
                                                        else {  // (Control_field & 0x10) == 0x10
                                                            sprintf(desc, "%d KHz, invalid Control field b23 0x%x", ifreq, Control_field);
                                                        }
                                                    }
                                                    else {
                                                        sprintf(desc, "Frequency not to be used (0)");
                                                    }
                                                    printbuf(desc, indent+3, NULL, 0);
                                                    k += 3;
                                                }
                                                break;
                                            case 0x8:
                                            case 0x9:
                                            case 0xa:
                                                while(k < (j + Length_Freq_list)) {
                                                    // iteration over Freq list
                                                    if (f[k] != 0) {    // freq != 0
                                                        if (RandM == 0xa) {
                                                            if (f[k] < 16) {
                                                                ifreq = (144 + ((unsigned int)f[k] * 9));
                                                            }
                                                            else {  // f[k] >= 16
                                                                ifreq = (387 + ((unsigned int)f[k] * 9));
                                                            }
                                                            sprintf(desc, "%d KHz", ifreq);
                                                        }
                                                        else {  // RandM == 8 or 9
                                                            freq = (87.5 + ((float)f[k] * 0.1));
                                                            sprintf(desc, "%.1f MHz", freq);
                                                        }
                                                    }
                                                    else {
                                                        sprintf(desc, "Frequency not to be used (0)");
                                                    }
                                                    printbuf(desc, indent+3, NULL, 0);
                                                    k++;
                                                }
                                                break;
                                            case 0xc:
                                                while((k + 1) < (j + Length_Freq_list)) {
                                                    // iteration over Freq list
                                                    ifreq = (((unsigned int)f[k] << 8) | (unsigned int)f[k+1]) * 5;
                                                    if (ifreq != 0) {
                                                        sprintf(desc, "%d KHz", ifreq);
                                                    }
                                                    else {
                                                        sprintf(desc, "Frequency not to be used (0)");
                                                    }
                                                    printbuf(desc, indent+3, NULL, 0);
                                                    k += 2;
                                                }
                                                break;
                                            case 0x6:
                                            case 0xe:
                                                while((k + 2) < (j + Length_Freq_list)) {
                                                    // iteration over Freq list
                                                    Id_field2 = f[k];
                                                    ifreq = ((((unsigned int)f[k+1] & 0x7f) << 8) | (unsigned int)f[k+2]);
                                                    if (ifreq != 0) {
                                                        sprintf(desc, "%d KHz", ifreq);
                                                    }
                                                    else {
                                                        sprintf(desc, "Frequency not to be used (0)");
                                                    }
                                                    if (RandM == 0x6) {
                                                        sprintf(tmpbuf, ", DRM Service Id 0x%X", Id_field2);
                                                        strcat(desc, tmpbuf);
                                                    }
                                                    else if (RandM == 0xe) {
                                                        sprintf(tmpbuf, ", AMSS Service Id 0x%X", Id_field2);
                                                        strcat(desc, tmpbuf);
                                                    }
                                                    if ((f[k+1] & 0x80) == 0x80) {
                                                        sprintf(tmpbuf, ", invalid Rfu b15 set to 1 instead of 0");
                                                        strcat(desc, tmpbuf);
                                                    }
                                                    printbuf(desc, indent+3, NULL, 0);
                                                    k += 3;
                                                }
                                                break;
                                            default:
                                                break;
                                        }
                                        j += Length_Freq_list;
                                    }
                                    i += Length_FI_list;
                                }
                            }
                        }
                        break;
                    case 22: // FIG 0/22 Transmitter Identification Information (TII) database
                        {    // ETSI EN 300 401 8.1.9
                            Lat_Lng gps_pos = {0, 0};
                            double latitude_sub, longitude_sub;
                            signed short Latitude_coarse, Longitude_coarse;
                            unsigned short key, TD;
                            signed short Latitude_offset, Longitude_offset;
                            unsigned char i = 1, j, MainId = 0, Rfu, Nb_SubId_fields, SubId;
                            unsigned char Latitude_fine, Longitude_fine;
                            char tmpbuf[256];
                            bool MS;

                            while (i < figlen) {
                                // iterate over Transmitter Identification Information (TII) fields
                                MS = f[i] >> 7;
                                MainId = f[i] & 0x7F;
                                key = (oe << 8) | (pd << 7) | MainId;
                                sprintf(desc, "M/S=%d %sidentifier, MainId=0x%X",
                                        MS, MS?"Sub-":"Main ", MainId);
                                // check MainId value
                                if ((Mode_Identity == 1) || (Mode_Identity == 2) || (Mode_Identity == 4)) {
                                    if (MainId > 69) {
                                        // The coding range shall be 0 to 69 for transmission modes I, II and IV
                                        sprintf(tmpbuf, " invalid value for transmission mode %d", Mode_Identity);
                                        strcat(desc, tmpbuf);
                                    }
                                }
                                else if (Mode_Identity == 3) {
                                    if (MainId > 5) {
                                        // The coding range shall be 0 to 5 for transmission modes I, II and IV
                                        sprintf(tmpbuf, " invalid value for transmission mode %d", Mode_Identity);
                                        strcat(desc, tmpbuf);
                                    }
                                }
                                // print database key
                                sprintf(tmpbuf, ", database key=0x%X", key);
                                strcat(desc, tmpbuf);
                                i++;
                                if (MS == 0) {
                                    // Main identifier

                                    if (i < (figlen - 4)) {
                                        Latitude_coarse = (f[i] << 8) | f[i+1];
                                        Longitude_coarse = (f[i+2] << 8) | f[i+3];
                                        Latitude_fine = f[i+4] >> 4;
                                        Longitude_fine = f[i+4] & 0x0F;
                                        gps_pos.latitude = (double)((signed int)((((signed int)Latitude_coarse) << 4) | (unsigned int)Latitude_fine)) * 90 / 524288;
                                        gps_pos.longitude = (double)((signed int)((((signed int)Longitude_coarse) << 4) | (unsigned int)Longitude_fine)) * 180 / 524288;
                                        fig0_22_key_Lat_Lng[key] = gps_pos;
                                        sprintf(tmpbuf, ", Lat Lng coarse=0x%X 0x%X, Lat Lng fine=0x%X 0x%X => Lat Lng=%f, %f",
                                                Latitude_coarse, Longitude_coarse, Latitude_fine, Longitude_fine,
                                                gps_pos.latitude, gps_pos.longitude);
                                        strcat(desc, tmpbuf);
                                        i += 5;
                                    }
                                    else {
                                        strcat(desc, ", invalid length of Latitude Longitude coarse fine");
                                    }
                                    printbuf(desc, indent+1, NULL, 0);
                                }
                                else {  // MS == 1
                                    // Sub-identifier

                                    if (i < figlen) {
                                        Rfu = f[i] >> 3;
                                        Nb_SubId_fields = f[i] & 0x07;
                                        if (Rfu != 0) {
                                            sprintf(tmpbuf, ", Rfu=%d invalid value", Rfu);
                                            strcat(desc, tmpbuf);
                                        }
                                        sprintf(tmpbuf, ", Number of SubId fields=%d%s",
                                                Nb_SubId_fields, (Nb_SubId_fields == 0)?", CEI":"");
                                        strcat(desc, tmpbuf);
                                        printbuf(desc, indent+1, NULL, 0);
                                        i++;

                                        for(j = i; ((j < (i + (Nb_SubId_fields * 6))) && (j < (figlen - 5))); j += 6) {
                                            // iterate over SubId fields
                                            SubId = f[j] >> 3;
                                            sprintf(desc, "SubId=0x%X", SubId);
                                            // check SubId value
                                            if ((SubId == 0) || (SubId > 23)) {
                                                strcat(desc, " invalid value");
                                            }

                                            TD = ((f[j] & 0x03) << 8) | f[j+1];
                                            Latitude_offset = (f[j+2] << 8) | f[j+3];
                                            Longitude_offset = (f[j+4] << 8) | f[j+5];
                                            sprintf(tmpbuf, ", TD=%d us, Lat Lng offset=0x%X 0x%X",
                                                    TD, Latitude_offset, Longitude_offset);
                                            strcat(desc, tmpbuf);

                                            if (fig0_22_key_Lat_Lng.count(key) > 0) {
                                                // latitude longitude available in database for Main Identifier
                                                latitude_sub = (90 * (double)Latitude_offset / 524288) + fig0_22_key_Lat_Lng[key].latitude;
                                                longitude_sub = (180 * (double)Longitude_offset / 524288) + fig0_22_key_Lat_Lng[key].longitude;
                                                sprintf(tmpbuf, " => Lat Lng=%f, %f", latitude_sub, longitude_sub);
                                                strcat(desc, tmpbuf);
                                            }
                                            else {
                                                // latitude longitude not available in database for Main Identifier
                                                latitude_sub = 90 * (double)Latitude_offset / 524288;
                                                longitude_sub = 180 * (double)Longitude_offset / 524288;
                                                sprintf(tmpbuf, " => Lat Lng=%f, %f wrong value because Main identifier latitude/longitude not available in database", latitude_sub, longitude_sub);
                                                strcat(desc, tmpbuf);
                                            }
                                            printbuf(desc, indent+2, NULL, 0);
                                        }
                                        i += (Nb_SubId_fields * 6);
                                    }
                                    else {
                                        strcat(desc, ", invalid fig length or Number of SubId fields length");
                                        printbuf(desc, indent+1, NULL, 0);
                                    }
                                }
                            }
                        }
                        break;
                    case 24: // FIG 0/24 OE Services
                        {    // ETSI EN 300 401 8.1.10.2
                            unsigned long long key;
                            unsigned int SId;
                            unsigned short EId;
                            unsigned char i = 1, j, Number_of_EIds, CAId;
                            char tmpbuf[256];
                            bool Rfa;

                            while (i < (figlen - (((unsigned char)pd + 1) * 2))) {
                                // iterate over other ensembles services
                                if (pd == 0) {
                                    SId = ((unsigned int)f[i] << 8) | (unsigned int)f[i+1];
                                    i += 2;
                                }
                                else {  // pd == 1
                                    SId = ((unsigned int)f[i] << 24) | ((unsigned int)f[i+1] << 16) |
                                            ((unsigned int)f[i+2] << 8) | (unsigned int)f[i+3];
                                    i += 4;
                                }
                                Rfa  =  (f[i] >> 7);
                                CAId  = (f[i] >> 4);
                                Number_of_EIds  = (f[i] & 0x0f);
                                key = ((unsigned long long)oe << 33) | ((unsigned long long)pd << 32) | \
                                        (unsigned long long)SId;
                                if (pd == 0) {
                                    sprintf(desc, "SId=0x%X, CAId=%d, Number of EId=%d, database key=%09llx", SId, CAId, Number_of_EIds, key);
                                }
                                else {  // pd == 1
                                    sprintf(desc, "SId=0x%X, CAId=%d, Number of EId=%d, database key=%09llx", SId, CAId, Number_of_EIds, key);
                                }
                                if (Rfa != 0) {
                                    sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                    strcat(desc, tmpbuf);
                                }
                                // CEI Change Event Indication
                                if (Number_of_EIds == 0) {
                                    sprintf(tmpbuf, ", CEI");
                                    strcat(desc, tmpbuf);
                                }
                                printbuf(desc, indent+1, NULL, 0);
                                i++;

                                for(j = i; ((j < (i + (Number_of_EIds * 2))) && (j < figlen)); j += 2) {
                                    // iterate over EIds
                                    EId = ((unsigned short)f[j] <<8) | (unsigned short)f[j+1];
                                    sprintf(desc, "EId 0x%04x", EId);
                                    printbuf(desc, indent+2, NULL, 0);
                                }
                                i += (Number_of_EIds * 2);
                            }
                        }
                        break;
                    case 25: // FIG 0/25 OE Announcement support
                        {    // ETSI EN 300 401 8.1.10.5.1
                            unsigned int key;
                            unsigned short SId, Asu_flags, EId;
                            unsigned char i = 1, j, Rfu, Number_EIds;
                            char tmpbuf[256];

                            while (i < (figlen - 4)) {
                                // iterate over other ensembles announcement support
                                // SId, Asu flags, Rfu, Number of EIds
                                SId = ((unsigned short)f[i] << 8) | (unsigned short)f[i+1];
                                Asu_flags = ((unsigned short)f[i+2] << 8) | (unsigned short)f[i+3];
                                Rfu = (f[i+4] >> 4);
                                Number_EIds = (f[i+4] & 0x0F);
                                sprintf(desc, "SId=0x%X, Asu flags=0x%X", SId, Asu_flags);
                                if (Rfu != 0) {
                                    sprintf(tmpbuf, ", Rfu=%d invalid value", Rfu);
                                    strcat(desc, tmpbuf);
                                }
                                sprintf(tmpbuf, ", Number of EIds=%d", Number_EIds);
                                strcat(desc, tmpbuf);
                                key = ((unsigned int)oe << 17) | ((unsigned int)pd << 16) | (unsigned int)SId;
                                sprintf(tmpbuf, ", database key=0x%05x", key);
                                strcat(desc, tmpbuf);
                                // CEI Change Event Indication
                                if (Number_EIds == 0) {
                                    sprintf(tmpbuf, ", CEI");
                                    strcat(desc, tmpbuf);
                                }
                                printbuf(desc, indent+1, NULL, 0);
                                i += 5;

                                for(j = 0; (j < Number_EIds) && (i < (figlen - 1)); j++) {
                                    // iterate over EIds
                                    EId = ((unsigned short)f[i] << 8) | (unsigned short)f[i+1];
                                    sprintf(desc, "EId=0x%X", EId);
                                    printbuf(desc, indent+2, NULL, 0);
                                    i += 2;
                                }
                                if (j < Number_EIds) {
                                    sprintf(desc, "missing EId, fig length too short !");
                                    printbuf(desc, indent+1, NULL, 0);
                                    fprintf(stderr, "WARNING: FIG %d/%d length %d too short !\n", figtype, ext, figlen);
                                }

                                // decode OE announcement support types
                                for(j = 0; j < 16; j++) {
                                    if (Asu_flags & (1 << j)) {
                                        sprintf(desc, "OE Announcement support=%s", announcement_types_str[j]);
                                        printbuf(desc, indent+2, NULL, 0);
                                    }
                                }
                            }
                        }
                        break;
                    case 26: // FIG 0/26 OE Announcement switching
                        {    // ETSI EN 300 401 8.1.10.5.2
                            unsigned short Asw_flags, EId_Other_Ensemble;
                            unsigned char i = 1, j, Rfa, Cluster_Id_Current_Ensemble, Region_Id_Current_Ensemble;
                            unsigned char Cluster_Id_Other_Ensemble, Region_Id_Other_Ensemble;
                            bool New_flag, Region_flag;
                            char tmpbuf[256];

                            while (i < (figlen - 6)) {
                                // iterate over other ensembles announcement switching
                                Cluster_Id_Current_Ensemble = f[i];
                                Asw_flags = ((unsigned short)f[i+1] << 8) | (unsigned short)f[i+2];
                                New_flag = f[i+3] >> 7;
                                Region_flag = (f[i+3] >> 6) & 0x01;
                                Region_Id_Current_Ensemble = f[i+3] & 0x3F;
                                EId_Other_Ensemble = ((unsigned short)f[i+4] << 8) | (unsigned short)f[i+5];
                                Cluster_Id_Other_Ensemble = f[i+6];
                                sprintf(desc, "Cluster Id Current Ensemble=0x%X, Asw flags=0x%X, New flag=%d %s announcement, Region flag=%d last byte %s, Region Id Current Ensemble=0x%X, EId Other Ensemble=0x%X, Cluster Id Other Ensemble=0x%X",
                                        Cluster_Id_Current_Ensemble, Asw_flags, New_flag, New_flag?"newly introduced":"repeated",
                                        Region_flag, Region_flag?"present":"absent. The announcement concerns the whole service area",
                                        Region_Id_Current_Ensemble, EId_Other_Ensemble, Cluster_Id_Other_Ensemble);
                                i += 7;
                                if (Region_flag != 0) {
                                    if (i < figlen) {
                                        // get Region Id Other Ensemble
                                        Rfa = (f[i] >> 6);
                                        Region_Id_Other_Ensemble = f[i] & 0x3F;
                                        if (Rfa != 0) {
                                            sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                            strcat(desc, tmpbuf);
                                        }
                                        sprintf(tmpbuf, ", Region Id Other Ensemble=0x%X", Region_Id_Other_Ensemble);
                                        strcat(desc, tmpbuf);
                                    }
                                    else {
                                        sprintf(tmpbuf, "missing Region Id Other Ensemble, fig length too short !");
                                        strcat(desc, tmpbuf);
                                        fprintf(stderr, "WARNING: FIG %d/%d length %d too short !\n", figtype, ext, figlen);
                                    }
                                    i++;
                                }
                                printbuf(desc, indent+1, NULL, 0);
                                // decode announcement switching types
                                for(j = 0; j < 16; j++) {
                                    if (Asw_flags & (1 << j)) {
                                        sprintf(desc, "Announcement switching=%s", announcement_types_str[j]);
                                        printbuf(desc, indent+2, NULL, 0);
                                    }
                                }
                            }
                        }
                        break;
                    case 27: // FIG 0/27 FM Announcement support
                        {    // ETSI EN 300 401 8.1.11.2.1
                            unsigned short SId, PI;
                            unsigned char i = 1, j, Rfu, Number_PI_codes, key;
                            char tmpbuf[256];

                            while (i < (figlen - 2)) {
                                // iterate over FM announcement support
                                SId = ((unsigned short)f[i] << 8) | (unsigned short)f[i+1];
                                Rfu = f[i+2] >> 4;
                                Number_PI_codes = f[i+2] & 0x0F;
                                key = (oe << 5) | (pd << 4) | Number_PI_codes;
                                sprintf(desc, "SId=0x%X", SId);
                                if (Rfu != 0) {
                                    sprintf(tmpbuf, ", Rfu=%d invalid value", Rfu);
                                    strcat(desc, tmpbuf);
                                }
                                sprintf(tmpbuf, ", Number of PI codes=%d", Number_PI_codes);
                                strcat(desc, tmpbuf);
                                if (Number_PI_codes > 12) {
                                    strcat(desc, " above maximum value of 12");
                                    fprintf(stderr, "WARNING: FIG %d/%d Number of PI codes=%d > 12 (maximum value)\n", figtype, ext, Number_PI_codes);
                                }
                                sprintf(tmpbuf, ", database key=0x%02X", key);
                                strcat(desc, tmpbuf);
                                // CEI Change Event Indication
                                if (Number_PI_codes == 0) {
                                    // The Change Event Indication (CEI) is signalled by the Number of PI codes field = 0
                                    strcat(desc, ", CEI");
                                }
                                printbuf(desc, indent+1, NULL, 0);
                                i += 3;
                                for(j = 0; (j < Number_PI_codes) && (i < (figlen - 1)); j++) {
                                    // iterate over PI
                                    PI = ((unsigned short)f[i] << 8) | (unsigned short)f[i+1];
                                    sprintf(desc, "PI=0x%X", PI);
                                    printbuf(desc, indent+2, NULL, 0);
                                    i += 2;
                                }
                                if (j != Number_PI_codes) {
                                    sprintf(desc, "fig length too short !");
                                    printbuf(desc, indent+2, NULL, 0);
                                    fprintf(stderr, "WARNING: FIG %d/%d length %d too short !\n", figtype, ext, figlen);
                                }
                            }
                        }
                        break;
                    case 28: // FIG 0/28 FM Announcement switching
                        {    // ETSI EN 300 401 8.1.11.2.2
                            unsigned short PI;
                            unsigned char i = 1, Cluster_Id_Current_Ensemble, Region_Id_Current_Ensemble;
                            bool New_flag, Rfa;
                            char tmpbuf[256];

                            while (i < (figlen - 3)) {
                                // iterate over FM announcement switching
                                Cluster_Id_Current_Ensemble = f[i];
                                New_flag = f[i+1] >> 7;
                                Rfa = (f[i+1] >> 6) & 0x01;
                                Region_Id_Current_Ensemble = f[i+1] & 0x3F;
                                PI = ((unsigned short)f[i+2] << 8) | (unsigned short)f[i+3];
                                sprintf(desc, "Cluster Id Current Ensemble=0x%X", Cluster_Id_Current_Ensemble);
                                if (Cluster_Id_Current_Ensemble == 0) {
                                    strcat(desc, " invalid value");
                                    fprintf(stderr, "WARNING: FIG %d/%d Cluster Id Current Ensemble invalid value 0\n", figtype, ext);
                                }
                                sprintf(tmpbuf, ", New flag=%d %s announcement",
                                        New_flag, New_flag?"newly introduced":"repeated");
                                strcat(desc, tmpbuf);
                                if (Rfa != 0) {
                                    sprintf(tmpbuf, ", Rfa=%d invalid value", Rfa);
                                    strcat(desc, tmpbuf);
                                }
                                sprintf(tmpbuf, ", Region Id Current Ensemble=0x%X, PI=0x%X", Region_Id_Current_Ensemble, PI);
                                strcat(desc, tmpbuf);
                                printbuf(desc, indent+1, NULL, 0);
                                i += 4;
                            }
                        }
                        break;
                    case 31: // FIG 0/31 FIC re-direction
                        {    // ETSI EN 300 401 8.1.12
                            unsigned int FIG_type0_flag_field = 0, flag_field;
                            unsigned char i = 1, j, FIG_type1_flag_field = 0, FIG_type2_flag_field = 0;

                            if (i < (figlen - 5)) {
                                // Read FIC re-direction
                                FIG_type0_flag_field = ((unsigned int)f[i] << 24) | ((unsigned int)f[i+1] << 16) |
                                                       ((unsigned int)f[i+2] << 8) | (unsigned int)f[i+3];
                                FIG_type1_flag_field = f[i+4];
                                FIG_type2_flag_field = f[i+5];

                                sprintf(desc, "FIG type 0 flag field=0x%X, FIG type 1 flag field=0x%X, FIG type 2 flag field=0x%X",
                                        FIG_type0_flag_field, FIG_type1_flag_field, FIG_type2_flag_field);
                                printbuf(desc, indent+1, NULL, 0);

                                for(j = 0; j < 32; j++) {
                                    // iterate over FIG type 0 re-direction
                                    flag_field = FIG_type0_flag_field & ((unsigned int)1 << j);
                                    if ((flag_field != 0) && (((j >= 0) && (j <= 5)) || (j == 8) ||
                                        (j == 10) || (j == 13) || (j == 14) ||
                                        (j == 19) || (j == 26) || (j == 28))) {
                                        sprintf(desc, "oe=%d FIG 0/%d carried in AIC, invalid configuration, shall always be carried entirely in the FIC",
                                                oe, j);
                                        printbuf(desc, indent+2, NULL, 0);
                                        fprintf(stderr, "WARNING: FIG %d/%d FIG re-direction of oe=%d FIG0/%d not allowed\n", figtype, ext, oe, j);
                                    }
                                    else if ((flag_field != 0) && ((j == 21) || (j == 24))) {
                                        sprintf(desc, "oe=%d FIG 0/%d carried in AIC, same shall be carried in FIC", oe, j);
                                        printbuf(desc, indent+2, NULL, 0);
                                    }
                                    else if (flag_field != 0) {
                                        if (oe == 0) {
                                            sprintf(desc, "oe=%d FIG 0/%d carried in AIC, same shall be carried in FIC", oe, j);
                                        }
                                        else {  // oe == 1
                                            sprintf(desc, "oe=%d FIG 0/%d carried in AIC, may be carried entirely in AIC", oe, j);
                                        }
                                        printbuf(desc, indent+2, NULL, 0);
                                    }
                                }

                                for(j = 0; j < 8; j++) {
                                    // iterate over FIG type 1 re-direction
                                    flag_field = FIG_type1_flag_field & ((unsigned int)1 << j);
                                    if (flag_field != 0) {
                                        if (oe == 0) {
                                            sprintf(desc, "oe=%d FIG 1/%d carried in AIC, same shall be carried in FIC", oe, j);
                                        }
                                        else {  // oe == 1
                                            sprintf(desc, "oe=%d FIG 1/%d carried in AIC, may be carried entirely in AIC", oe, j);
                                        }
                                        printbuf(desc, indent+2, NULL, 0);
                                    }
                                }

                                for(j = 0; j < 8; j++) {
                                    // iterate over FIG type 2 re-direction
                                    flag_field = FIG_type2_flag_field & ((unsigned int)1 << j);
                                    if (flag_field != 0) {
                                        if (oe == 0) {
                                            sprintf(desc, "oe=%d FIG 2/%d carried in AIC, same shall be carried in FIC", oe, j);
                                        }
                                        else {  // oe == 1
                                            sprintf(desc, "oe=%d FIG 2/%d carried in AIC, may be carried entirely in AIC", oe, j);
                                        }
                                        printbuf(desc, indent+2, NULL, 0);
                                    }
                                }
                            }
                            if (figlen != 7) {
                                fprintf(stderr, "WARNING: FIG %d/%d invalid length %d, expecting 7\n", figtype, ext, figlen);
                            }
                        }
                        break;
                }
            }
            break;

        case 1:
            {// SHORT LABELS
                unsigned short int ext,oe,charset;
                unsigned short int flag;
                char label[17];

                charset = (f[0] & 0xF0) >> 4;
                oe = (f[0] & 0x08) >> 3;
                ext = f[0] & 0x07;
                sprintf(desc,
                        "FIG %d/%d: OE=%d, Charset=%d",
                        figtype, ext, oe, charset);

                printbuf(desc, indent, f+1, figlen-1);
                memcpy(label, f+figlen-18, 16);
                label[16] = 0x00;
                flag = f[figlen-2] * 256 + \
                       f[figlen-1];

                figs.push_back(figtype, ext, figlen);

                switch (ext) {
                    case 0: // FIG 1/0 Ensemble label
                        {   // ETSI EN 300 401 8.1.13
                            unsigned short int eid;
                            eid = f[1] * 256 + f[2];
                            sprintf(desc, "Ensemble ID 0x%04X label: \"%s\", Short label mask: 0x%04X", eid, label, flag);
                            printinfo(desc, indent+1);
                        }
                        break;

                    case 1: // FIG 1/1 Programme service label
                        {   // ETSI EN 300 401 8.1.14.1
                            unsigned short int sid;
                            sid = f[1] * 256 + f[2];
                            sprintf(desc, "Service ID 0x%X label: \"%s\", Short label mask: 0x%04X", sid, label, flag);
                            printinfo(desc, indent+1);
                        }
                        break;

                    case 4: // FIG 1/4 Service component label
                        {   // ETSI EN 300 401 8.1.14.3
                            unsigned int sid;
                            unsigned char pd, SCIdS;
                            pd    = (f[1] & 0x80) >> 7;
                            SCIdS =  f[1] & 0x0F;
                            if (pd == 0) {
                                sid = f[2] * 256 + \
                                      f[3];
                            }
                            else {
                                sid = f[2] * 256 * 256 * 256 + \
                                      f[3] * 256 * 256 + \
                                      f[4] * 256 + \
                                      f[5];
                            }
                            sprintf(desc,
                                    "Service ID  0x%X , Service Component ID 0x%04X Short, label: \"%s\", label mask: 0x%04X",
                                    sid, SCIdS, label, flag);
                            printinfo(desc, indent+1);
                        }
                        break;

                    case 5: // FIG 1/5 Data service label
                        {   // ETSI EN 300 401 8.1.14.2
                            unsigned int sid;
                            sid = f[1] * 256 * 256 * 256 + \
                                  f[2] * 256 * 256 + \
                                  f[3] * 256 + \
                                  f[4];

                            sprintf(desc,
                                    "Service ID 0x%X label: \"%s\", Short label mask: 0x%04X",
                                    sid, label, flag);
                            printinfo(desc, indent+1);
                        }
                        break;


                    case 6: // FIG 1/6 X-PAD user application label
                        {   // ETSI EN 300 401 8.1.14.4
                            unsigned int sid;
                            unsigned char pd, SCIdS, xpadapp;
                            string xpadappdesc;

                            pd    = (f[1] & 0x80) >> 7;
                            SCIdS =  f[1] & 0x0F;
                            if (pd == 0) {
                                sid = f[2] * 256 + \
                                      f[3];
                                xpadapp = f[4] & 0x1F;
                            }
                            else {
                                sid = f[2] * 256 * 256 * 256 + \
                                      f[3] * 256 * 256 + \
                                      f[4] * 256 + \
                                      f[5];
                                xpadapp = f[6] & 0x1F;
                            }

                            if (xpadapp == 2) {
                                xpadappdesc = "DLS";
                            }
                            else if (xpadapp == 12) {
                                xpadappdesc = "MOT";
                            }
                            else {
                                xpadappdesc = "?";
                            }


                            sprintf(desc,"Service ID  0x%X , Service Component ID 0x%04X Short, X-PAD App %02X (%s), label: \"%s\", label mask: 0x%04X",
                                    sid, SCIdS, xpadapp, xpadappdesc.c_str(), label, flag);
                            printbuf(desc,indent+1,NULL,0,"");
                        }
                        break;
                }
            }
            break;
        case 2:
            {// LONG LABELS
                unsigned short int ext,oe;

                uint8_t toggle_flag = (f[0] & 0x80) >> 7;
                uint8_t segment_index = (f[0] & 0x70) >> 4;
                oe = (f[0] & 0x08) >> 3;
                ext = f[0] & 0x07;
                sprintf(desc,
                        "FIG %d/%d: Toggle flag=%d, Segment_index=%d, OE=%d",
                        figtype, ext, toggle_flag, segment_index, oe);

                printbuf(desc, indent, f+1, figlen-1);

                figs.push_back(figtype, ext, figlen);
            }
            break;
        case 5:
            {// FIDC
                unsigned short int ext;

                uint8_t d1 = (f[0] & 0x80) >> 7;
                uint8_t d2 = (f[0] & 0x40) >> 6;
                uint8_t tcid = (f[0] & 0x38) >> 5;
                ext = f[0] & 0x07;
                sprintf(desc,
                        "FIG %d/%d: D1=%d, D2=%d, TCId=%d",
                        figtype, ext, d1, d2, tcid);

                printbuf(desc, indent, f+1, figlen-1);

                figs.push_back(figtype, ext, figlen);
            }
            break;
        case 6:
            {// Conditional access
                fprintf(stderr, "ERROR: ETI contains unsupported FIG 6\n");
            }
            break;
    }
}

// get_programme_type_str return the programme type string from international table Id and programme type
const char *get_programme_type_str(unsigned int int_table_Id, unsigned int pty) {
    if ((int_table_Id - 1) < INTERNATIONAL_TABLE_SIZE) {
       if (pty < PROGRAMME_TYPE_CODES_SIZE) {
           return Programme_type_codes_str[int_table_Id - 1][pty];
       }
       else {
           return "invalid programme type";
       }
    }
    else {
        return "unknown international table Id";
    }
}

// strcatPNum decode Programme_Number into string and append it to dest_str
// Programme_Number: this 16-bit field shall define the date and time at which
// a programme begins or will be continued. This field is coded in the same way
// as the RDS "Programme Item Number (PIN)" feature (EN 62106).
char *strcatPNum(char *dest_str, unsigned short Programme_Number) {
    unsigned char day, hour, minute;
    char tempbuf[256];

    minute = (unsigned char)(Programme_Number & 0x003F);
    hour = (unsigned char)((Programme_Number >> 6) & 0x001F);
    day = (unsigned char)((Programme_Number >> 11) & 0x001F);
    if (day != 0) {
        sprintf(tempbuf, "day of month=%d time=%02d:%02d", day, hour, minute);
    }
    else {  // day == 0
        // Special codes are allowed when the date part of the PNum field
        // signals date = "0". In this case, the hours and minutes part of
        // the field shall contain a special code, as follows
        if ((hour == 0) && (minute == 0)) {
            sprintf(tempbuf, "Status code: no meaningful PNum is currently provided");
        }
        else if ((hour == 0) && (minute == 1)) {
            sprintf(tempbuf, "Blank code: the current programme is not worth recording");
        }
        else if ((hour == 0) && (minute == 2)) {
            sprintf(tempbuf, "Interrupt code: the interrupt is unplanned (for example a traffic announcement)");
        }
        else {
            sprintf(tempbuf, "invalid value");
        }
    }
    return strcat(dest_str, tempbuf);
}

// sprintfMJD: convert MJD (Modified Julian Date) into date string
int sprintfMJD(char *dst, int mjd) {
    // EN 62106 Annex G
    // These formulas are applicable between the inclusive dates: 1st March 1900 to 28th February 2100
    int y, m, k;
    struct tm timeDate;

    memset(&timeDate, 0, sizeof(struct tm));

    // find Y, M, D from MJD
    y = (int)(((double)mjd - 15078.2) / 365.25);
    m = (int)(((double)mjd - 14956.1 - (int)((double)y * 365.25)) / 30.6001);
    timeDate.tm_mday = mjd - 14956 - (int)((double)y * 365.25) - (int)((double)m * 30.6001);
    if ((m == 14) || (m == 15)) {
        k = 1;
    }
    else {
        k = 0;
    }
    timeDate.tm_year = y + k;
    timeDate.tm_mon = (m - 1 - (k * 12)) - 1;

    // find WD from MJD
    timeDate.tm_wday = (((mjd + 2) % 7) + 1) % 7;

    //timeDate.tm_yday = 0; // Number of days since the first day of January not calculated
    timeDate.tm_isdst = -1; // No time print then information not available

    // print date string
    if ((timeDate.tm_mday < 0) || (timeDate.tm_mon < 0) || (timeDate.tm_year < 0)) {
        return sprintf(dst, "invalid MJD mday=%d mon=%d year=%d", timeDate.tm_mday, timeDate.tm_mon, timeDate.tm_year);
    }
    return strftime(dst, 256, "%a %b %d %Y", &timeDate);
}

void printinfo(string header,
        int indent_level,
        int min_verb)
{
    if (verbosity >= min_verb) {
        for (int i = 0; i < indent_level; i++) {
            printf("\t");
        }
        printf("%s\n", header.c_str());
    }
}

void printbuf(string header,
        int indent_level,
        unsigned char* buffer,
        size_t size,
        string desc)
{
    if (verbosity > 0) {
        for (int i = 0; i < indent_level; i++) {
            printf("\t");
        }

        printf("%s", header.c_str());

        if (verbosity > 1) {
            if (size != 0) {
                printf(": ");
            }

            for (size_t i = 0; i < size; i++) {
                printf("%02x ", buffer[i]);
            }
        }

        if (desc != "") {
            printf(" [%s] ", desc.c_str());
        }

        printf("\n");
    }
}