summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/dcerpc/idl2wrs.c
blob: 3d95a4d62826ac5be993c2d30bd8e56a0722cbe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
/* idl2wrs.c
 * DCE RPC IDL to Wireshark dissector compiler
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
TODO
   check that every cnf defined type,hffield,rename,... has been referenced
   at least once and if not,   abort with an error

   need to distinguish between NTTIME (absolute time) and relative time

   prune_xxx should only act inside of '[' ']'

   add support for bool8,16,32,64 with tfs strings

   add the remaining array type  (uvarray)

   add code to verify that union tag length is correct
*/
/* List of built in types :
   WERROR	A 32 bit integer holding a DCE/NT status code.

   uint8	A 8 bit integer
   int8

   uint16	A 16 bit integer
   int16

   uint32	A 32 bit integer
   int32

   uint64	A 64 bit integer

   udlong	A 64 bit integer aligned on 4 byte boundary
   dlong

   time_t	A 32 bit integer holding a unix style time_t

   NTTIME_hyper A 64 bit integer representing a NTTIME
   NTTIME_1sec

   unistr		A conformant and varying unicode string

   ascstr		A conformant and varying ascii string


   SID			A SID structure.

   uuid_t	A 16 byte FT_GUID blob.
   GUID


   policy_handle
   bool8
   uuid_t
   policy_handle
   NTTIME
*/

/* All field dissectors that call a normal type
   (i.e. not a pointer, not an array)
   has a local variable  guint param declared which is passed on to the
   type dissector.
   The default value is 0 but the PARAM_VALUE conformance tag can be used to
   change it.
   This is only meaningful if the called type dissector actually does anything
   with this parameter.
*/

#define _GNU_SOURCE

#include <glib.h>
#include <glib/gstdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <wsutil/strtoi.h>

#undef IDL2WRS_DEBUG

#define DISSECTORNAME_MAXLEN 256

#define BASE_BUFFER_SIZE 256

static FILE *tfh, *eth_code, *eth_hdr, *eth_hf, *eth_hfarr, *eth_ett, *eth_ettarr, *eth_ft, *eth_handoff;
static char *uuid=NULL;
static char *version=NULL;
static const char *pointer_default=NULL;
static char *ifname=NULL;
static char hf_status[BASE_BUFFER_SIZE];
static int lineno,linepos;
static char line[4 * BASE_BUFFER_SIZE];

static void FPRINTF(FILE *fh, const char *format, ...)
{
	va_list args;

#ifdef IDL2WRS_DEBUG
	va_start(args, format);
	vfprintf (stderr, format, args);
	va_end(args);
#endif
	if (fh) {
		va_start(args, format);
		vfprintf (fh, format, args);
		va_end(args);
	}
}

typedef struct _pointer_item_t {
	struct _pointer_item_t *next;
	const char *type;
} pointer_item_t;

#define BI_CASE			0x00000001
#define BI_CASE_DEFAULT		0x00000002
#define BI_IN			0x00000004
#define BI_OUT			0x00000008
#define BI_SIZE_IS		0x00000010
#define BI_LENGTH_IS		0x00000020
#define BI_POINTER		0x00000040
#define BI_BITMAP8		0x00000100
#define BI_BITMAP32		0x00000200
#define BI_SWITCH_TYPE		0x00000400
typedef struct _bracket_item_t {
	unsigned int flags;
	const char *case_name;
	pointer_item_t *pointer_list;
	int union_tag_size;
} bracket_item_t;

typedef struct _no_emit_item_t {
	struct _no_emit_item_t *next;
	char *name;
} no_emit_item_t;
static no_emit_item_t *no_emit_list=NULL;

typedef struct _hf_rename_item_t {
	struct _hf_rename_item_t *next;
	int refcount;	/* number of times this rename has been referenced */
	char *old_name;
	char *new_name;
} hf_rename_item_t;
static hf_rename_item_t *hf_rename_list=NULL;

typedef struct _enum_list_t {
	struct _enum_list_t *next;
	char *name;
	int val;
} enum_list_t;

typedef struct _token_item_t {
	struct _token_item_t *next;
	char *str;
} token_item_t;
static token_item_t *token_list=NULL;
static token_item_t *last_token_item=NULL;

typedef struct _type_item_t {
	struct _type_item_t *next;
	char *name;
	char *dissector;
	char *ft_type;
	char *base_type;
	char *mask;
	char *vals;
	int alignment;
} type_item_t;
static type_item_t *type_list=NULL;

typedef struct _union_tag_size_item_t {
	struct _union_tag_size_item_t *next;
	char *name;
	int size;
} union_tag_size_item_t;
static union_tag_size_item_t *union_tag_size_list=NULL;

typedef struct _hf_field_item_t {
	struct _hf_field_item_t *next;
	char *name;
	char *ft_type;
} hf_field_item_t;
static hf_field_item_t *hf_field_list=NULL;

typedef struct _dissector_param_value_t {
	struct _dissector_param_value_t *next;
	char *name;
	char *value;
} dissector_param_value_t;
static dissector_param_value_t *dissector_param_list=NULL;

static type_item_t *find_type(char *name);
static int Exit(int code);

static void
register_dissector_param_value(const char *name, const char *value)
{
	dissector_param_value_t *dpv;
	dpv=g_new0(dissector_param_value_t, 1);
	if (!dpv) {
		FPRINTF(stderr, "Can't allocate memory. Exit.\n");
		exit(10);
	}
	dpv->next=dissector_param_list;
	dissector_param_list=dpv;
	dpv->name=g_strdup(name);
	dpv->value=g_strdup(value);
}

static const char *
find_dissector_param_value(char *name)
{
	dissector_param_value_t *dpv;
	for(dpv=dissector_param_list;dpv;dpv=dpv->next){
		if(!g_strcmp0(name,dpv->name)){
			return dpv->value;
		}
	}
	return "0";
}

static pointer_item_t *
prepend_pointer_list(pointer_item_t *ptrs, int num_pointers)
{
	pointer_item_t *pi;

	pi=ptrs;
	while(pi){
		if(num_pointers)num_pointers--;
		pi=pi->next;
	}
	if(!pi)pi=ptrs;
	while(num_pointers--){
		pi=g_new0(pointer_item_t, 1);
		if (!pi) {
			FPRINTF(stderr, "Can't allocate memory. Exit.\n");
			exit(10);
		}
		pi->next=ptrs;
		pi->type=pointer_default;
		ptrs=pi;
	}
	ptrs=pi;

	return ptrs;
}

static const char *
ptr_to_define(const char *pointer_type)
{
	if(!g_strcmp0(pointer_type, "unique")){
		return "NDR_POINTER_UNIQUE";
	} else if(!g_strcmp0(pointer_type, "ref")){
		return "NDR_POINTER_REF";
	} else if(!g_strcmp0(pointer_type, "ptr")){
		return "NDR_POINTER_PTR";
	}

	FPRINTF(stderr, "prt_to_define, 	weirdo pointer :%s\n", pointer_type);
	exit(10);
}

static int
get_union_tag_size(char *name)
{
	union_tag_size_item_t *utsi;
	for(utsi=union_tag_size_list;utsi;utsi=utsi->next){
		if(!g_strcmp0(name, utsi->name)){
			return utsi->size;
		}
	}
	FPRINTF(stderr, "ERROR: size of tag for union:%s is not known\n", name);
	FPRINTF(stderr, "  use the UNION_TAG_SIZE directive to specify it in teh conformance file\n");
	exit(10);
}


/* this function will add an entry to the hf_rename list */
static void
register_hf_rename(const char *old_name, const char *new_name)
{
	hf_rename_item_t *new_item;
	new_item=g_new0(hf_rename_item_t, 1);
	if (!new_item) {
		FPRINTF(stderr, "Can't allocate memory. Exit.\n");
		exit(10);
	}
	new_item->next=hf_rename_list;
	hf_rename_list=new_item;
	new_item->refcount=0;
	new_item->old_name=g_strdup(old_name);
	new_item->new_name=g_strdup(new_name);
}

/* this function checks that all hf_rename fields have actually been referenced
   if not	out conformance file is stale
*/
static void
check_hf_rename_refcount(void)
{
	hf_rename_item_t *hri;

	/* don't generate code for renamed hf fields  just return the new name*/
	for(hri=hf_rename_list;hri;hri=hri->next){
		if(!hri->refcount){
			FPRINTF(stderr, "ERROR: the hf_rename field:%s was never referenced. it is likely the conformance file is stale\n", hri->old_name);
			exit(10);
		}
	}
}

static hf_field_item_t *
find_hf_field(char *name)
{
	hf_field_item_t *hfi;

	for(hfi=hf_field_list;hfi;hfi=hfi->next){
		if(!g_strcmp0(hfi->name, name)){
			break;
		}
	}
	if (!hfi) {
		FPRINTF(stderr, "find_hf_field:  unknown hf_field:%s\n",name);
		Exit(10);
	}

	return hfi;
}


/* this function will create the code required for a hf field.
   it MIGHT rename the field so a user MUST use the name returned
   from this function.
   for fields that are to be renamed  no code is generated
*/
static const char *
register_hf_field(const char *hf_name, const char *title, const char *filter_name, const char *ft_type, const char *base_type, const char *valsstring, const char *mask, const char *blurb)
{
	hf_field_item_t *hfi;
	hf_rename_item_t *hri;

	/* don't generate code for renamed hf fields  just return the new name*/
	for(hri=hf_rename_list;hri;hri=hri->next){
		if(!strncmp(hf_name, hri->old_name, strlen(hf_name))){
			hfi=find_hf_field(hri->new_name);
			if(g_strcmp0(ft_type, hfi->ft_type)){
				FPRINTF(stderr, "register_hf_field:  hf_fields %s and %s have different types %s %s\n",hf_name,hfi->name,ft_type,hfi->ft_type);
				Exit(10);
			}
			hri->refcount++;
			return hri->new_name;
		}
	}

	hfi=g_new0(hf_field_item_t, 1);
	if (!hfi) {
		FPRINTF(stderr, "Can't allocate memory. Exit.\n");
		exit(10);
	}
	hfi->next=hf_field_list;
	hf_field_list=hfi;
	hfi->name=g_strdup(hf_name);
	hfi->ft_type=g_strdup(ft_type);

	FPRINTF(eth_hf, "static int %s = -1;\n", hf_name);
	FPRINTF(eth_hfarr, "		{ &%s,\n", hf_name);
	FPRINTF(eth_hfarr, "		  { \"%s\", \"%s\", %s, %s,\n", title, filter_name, ft_type, base_type);
	FPRINTF(eth_hfarr, "		  %s, %s,\n", valsstring, mask);
	if (strlen(blurb) > 0)
		FPRINTF(eth_hfarr, "		 \"%s\", HFILL }},\n", blurb);
	else
		FPRINTF(eth_hfarr, "		 NULL, HFILL }},\n");
	FPRINTF(eth_hfarr, "\n");

	return hf_name;
}

/* this function will parse the no emit list and decide whether code should
   be generated for this dissector or if we should only register the type.
*/
static int
check_if_to_emit(char *name)
{
	no_emit_item_t *nel;

	for(nel=no_emit_list;nel;nel=nel->next){
		if(!g_strcmp0(name, nel->name)){
			FPRINTF(NULL, "SKIPPED emitting of %s\n",name);
			return 0;
		}
	}
	return 1;
}

#if 0
static void
prune_keywords(char *name)
{
	token_item_t *ti;

	for(ti=token_list;ti;ti=ti->next){
		if(!ti->next){
			break;
		}
		if(!g_strcmp0(ti->next->str, name)){
			if(!g_strcmp0(ti->next->next->str, ",")){
				ti->next=ti->next->next->next;
			} else {
				ti->next=ti->next->next;
			}
		}
	}
}
#endif

static void
rename_tokens(const char *old_name, const char *new_name)
{
	token_item_t *ti;

	for(ti=token_list;ti;ti=ti->next){
		if(!g_strcmp0(ti->str, old_name)){
			ti->str=g_strdup(new_name);
		}
	}
}

static void
prune_keyword_parameters(const char *name)
{
	token_item_t *ti, *tmpti;

	for(ti=token_list;ti;ti=ti->next){
		if(!g_strcmp0(ti->str, name)){
			if(!g_strcmp0(ti->next->str, "(")){
				tmpti=ti;
				while(1){
					if(!g_strcmp0(tmpti->str, ")")){
						ti->next=tmpti->next;
						break;
					}
					tmpti=tmpti->next;
				}
			}
		}
	}
}

/* this function will parse a bracket item
	   [ ... ]
   it will return the token of the next item following the ']'
*/
static token_item_t *
parsebrackets(token_item_t *ti, bracket_item_t **bracket){
	bracket_item_t *br;
	type_item_t *type_item;

	if(g_strcmp0(ti->str, "[")){
		FPRINTF(stderr, "ERROR: parsebrackets first token is not '['\n");
		Exit(10);
	}
	ti=ti->next;

	br=g_new0(bracket_item_t, 1);
	if (!br) {
		FPRINTF(stderr, "Can't allocate memory. Exit.\n");
		exit(10);
	}
	*bracket=br;
	br->flags=0;
	br->case_name=NULL;
	br->pointer_list=NULL;

	while(ti){
		if( !g_strcmp0(ti->str, "{")
		  ||!g_strcmp0(ti->str, "}")){
			FPRINTF(stderr, "ERROR: parsebrackets '{' '}' inside bracket item\n");
			Exit(10);
		}

		if(!g_strcmp0(ti->str, "[")){
			FPRINTF(stderr, "ERROR: parsebrackets '[' inside bracket item\n");
			Exit(10);
		}

		/* finished */
		if(!g_strcmp0(ti->str, "]")){
			/* check for [ ... ] [ ...] */
			ti=ti->next;

			if(!g_strcmp0(ti->str, "[")){
				ti=ti->next;
				continue;
			}
			return ti;
		}

		/* just ignore all ',' */
		if(!g_strcmp0(ti->str, ",")){
			ti=ti->next;
			continue;
		}

		/* case '(' tag ')' */
		if(!g_strcmp0(ti->str, "case")){
			br->flags|=BI_CASE;
			ti=ti->next;

			if(g_strcmp0(ti->str, "(")){
				FPRINTF(stderr, "ERROR: parsebrackets case not followed by '('\n");
				Exit(10);
			}
			ti=ti->next;

			/* name */
			br->case_name=ti->str;
			ti=ti->next;

			if(g_strcmp0(ti->str, ")")){
				FPRINTF(stderr, "ERROR: parsebrackets case does not end with ')'\n");
				Exit(10);
			}
			ti=ti->next;
			continue;
		}

		/* default */
		if(!g_strcmp0(ti->str, "default")){
			br->flags|=BI_CASE;
			br->flags|=BI_CASE_DEFAULT;
			br->case_name="default";
			ti=ti->next;
			continue;
		}


		/* in */
		if(!g_strcmp0(ti->str, "in")){
			br->flags|=BI_IN;
			ti=ti->next;
			continue;
		}

		/* out */
		if(!g_strcmp0(ti->str, "out")){
			br->flags|=BI_OUT;
			ti=ti->next;
			continue;
		}

		/* public : we don't care about this one */
		if(!g_strcmp0(ti->str, "public")){
			ti=ti->next;
			continue;
		}

		/* gensize : we don't care about this one */
		if(!g_strcmp0(ti->str, "gensize")){
			ti=ti->next;
			continue;
		}

		/* switch_is */
		if(!g_strcmp0(ti->str, "switch_is")){
			FPRINTF(stderr, "WARNING: parsebrackets can not handle switch_is properly yet  so we can not verify the tag size\n");
			while(ti){
				if(!g_strcmp0(ti->str, ")")){
					ti=ti->next;
					break;
				}
				ti=ti->next;
			}
			continue;
		}

		/* switch_is */
		if(!g_strcmp0(ti->str, "subcontext")){
			while(ti){
				if(!g_strcmp0(ti->str, ")")){
					ti=ti->next;
					break;
				}
				ti=ti->next;
			}
			continue;
		}

		/* value   we don't care about this one so just skip it */
		if(!g_strcmp0(ti->str, "value")){
			int level;
			ti=ti->next;
			if( g_strcmp0(ti->str, "(") ){
				FPRINTF(stderr, "WARNING: parsebrackets value was not followed by '('\n");
				Exit(10);
			}
			level=0;
			while(ti){
				if(!g_strcmp0(ti->str, "(")){
					ti=ti->next;
					level++;
					continue;
				}
				if(!g_strcmp0(ti->str, ")")){
					ti=ti->next;
					level--;
					if(level){
						continue;
					}
					break;
				}
				ti=ti->next;
			}
			continue;
		}

		/* range   we don't care about this one so just skip it */
		if(!g_strcmp0(ti->str, "range")){
			int level;
			ti=ti->next;
			if( g_strcmp0(ti->str, "(") ){
				FPRINTF(stderr, "WARNING: parsebrackets range was not followed by '('\n");
				Exit(10);
			}
			level=0;
			while(ti){
				if(!g_strcmp0(ti->str, "(")){
					ti=ti->next;
					level++;
					continue;
				}
				if(!g_strcmp0(ti->str, ")")){
					ti=ti->next;
					level--;
					if(level){
						continue;
					}
					break;
				}
				ti=ti->next;
			}
			continue;
		}

		/* flag   we don't care about this one so just skip it */
		if(!g_strcmp0(ti->str, "flag")){
			int level;
			ti=ti->next;
			if( g_strcmp0(ti->str, "(") ){
				FPRINTF(stderr, "WARNING: parsebrackets flag was not followed by '('\n");
				Exit(10);
			}
			level=0;
			while(ti){
				if(!g_strcmp0(ti->str, "(")){
					ti=ti->next;
					level++;
					continue;
				}
				if(!g_strcmp0(ti->str, ")")){
					ti=ti->next;
					level--;
					if(level){
						continue;
					}
					break;
				}
				ti=ti->next;
			}
			continue;
		}

		/* switch_type */
		if(!g_strcmp0(ti->str, "switch_type")){
			br->flags|=BI_SWITCH_TYPE;
			ti=ti->next;

			if(g_strcmp0(ti->str, "(")){
				FPRINTF(stderr, "WARNING: parsebrackets switch_type was not followed by '('\n");
				Exit(10);
			}
			ti=ti->next;

			type_item=find_type(ti->str);
			if(!type_item){
				FPRINTF(stderr, "ERROR : parsebrackets switch_type unknown type %s\n",ti->str);
				Exit(10);
			}
			br->union_tag_size=type_item->alignment;
			ti=ti->next;

			if(g_strcmp0(ti->str, ")")){
				FPRINTF(stderr, "WARNING: parsebrackets switch_type did not end with ')'\n");
				Exit(10);
			}
			ti=ti->next;

			continue;
		}

		/* size_is */
		if(!g_strcmp0(ti->str, "size_is")){
			br->flags|=BI_SIZE_IS;
			ti=ti->next;
			continue;
		}

		/* length_is */
		if(!g_strcmp0(ti->str, "length_is")){
			br->flags|=BI_LENGTH_IS;
			ti=ti->next;
			continue;
		}

		/* bitmap8bit */
		if(!g_strcmp0(ti->str, "bitmap8bit")){
			br->flags|=BI_BITMAP8;
			ti=ti->next;
			continue;
		}

		/* bitmap32bit */
		if(!g_strcmp0(ti->str, "bitmap32bit")){
			br->flags|=BI_BITMAP32;
			ti=ti->next;
			continue;
		}

		/* ref, unique or ptr */
		if(!g_strcmp0(ti->str, "ref")
		|| !g_strcmp0(ti->str, "unique")
		|| !g_strcmp0(ti->str, "ptr")){
			pointer_item_t *newpi;

			br->flags|=BI_POINTER;
			newpi=g_new0(pointer_item_t, 1);
			if (!newpi) {
				FPRINTF(stderr, "Can't allocate memory. Exit.\n");
				exit(10);
			}
			newpi->next=NULL;
			newpi->type=ti->str;
			newpi->next=br->pointer_list;
			br->pointer_list=newpi;
			ti=ti->next;
			continue;
		}

		FPRINTF(stderr, "ERROR: parsebrackets should not be reached  unknown tag:%s\n", ti->str);
		Exit(10);
	}

	return NULL;
}

/* this function will register a new type learnt from the IDL file
*/
static type_item_t *
register_new_type(const char *name, const char *dissectorname, const char *ft_type, const char *base_type, const char *mask, const char *valsstring, int alignment){
	type_item_t *new_type;

	FPRINTF(NULL,"XXX new type:%s dissector:%s Type:%s Base:%s Mask:%s Vals:%s alignment:%d\n", name, dissectorname, ft_type, base_type, mask, valsstring, alignment);

	new_type=g_new0(type_item_t, 1);
	if (!new_type) {
		FPRINTF(stderr, "Can't allocate memory. Exit.\n");
		exit(10);
	}
	new_type->next=type_list;
	new_type->name=g_strdup(name);
	new_type->dissector=g_strdup(dissectorname);
	new_type->ft_type=g_strdup(ft_type);
	new_type->base_type=g_strdup(base_type);
	new_type->mask=g_strdup(mask);
	new_type->vals=g_strdup(valsstring);
	new_type->alignment=alignment;
	type_list=new_type;

	return new_type;
}


/* this function will print the remaining content of the token list
*/
static void printtokenlist(int count)
{
	token_item_t *ti;
	FPRINTF(stderr, "TOKENLIST:\n");
	for(ti=token_list;ti&&count;count--,ti=ti->next){
		FPRINTF(stderr, "Token \"%s\"\n",ti->str);
	}
	if(!count){
		FPRINTF(stderr, "	 ...\n");
	}
}


/* this function will parse the header and pick up the fields
 * we are interested in.
 * the header is supposed to start at the very first token and look like
 * [ <fields> ] inteface <ifname> {
 *
 * we are interested in the fields:
 *	   uuid
 *	   version
 *	   pointer_default
 *
 * this function will also g_remove the header from the token list
 */
static void parseheader(void)
{
	char filter_name[BASE_BUFFER_SIZE];
	token_item_t *ti;
	int level=0;
	int major, minor;

	ti=token_list;
	if(!ti){
		FPRINTF(stderr, "ERROR: no tokens\n");
		Exit(10);
	}

	/* first token must be '[' */
	if( g_strcmp0(ti->str, "[") ){
		FPRINTF(stderr, "ERROR: first token is not '['\n");
		Exit(10);
	}

	for(ti=token_list;ti;ti=ti->next){
		if( !g_strcmp0(ti->str, "[")){
			level++;
			continue;
		}
		if( !g_strcmp0(ti->str, "]")){
			level--;
			if(!level){
				token_list=ti->next;
				break;
			}
		}
		if(level==1){
			if( !g_strcmp0(ti->str, "uuid")){
				uuid=ti->next->next->str;
				FPRINTF(NULL,"UUID:%s\n",uuid);
			}
			if( !g_strcmp0(ti->str, "version")){
				version=ti->next->next->str;
				FPRINTF(NULL,"VERSION:%s\n",version);
			}
			if( !g_strcmp0(ti->str, "pointer_default")){
				if(!g_strcmp0(ti->next->next->str, "unique")){
					pointer_default="unique";
				} else if(!g_strcmp0(ti->next->next->str, "ptr")){
					pointer_default="ptr";
				} else {
					FPRINTF(stderr, "ERROR: unknown pointer type\n");
					Exit(10);
				}
				FPRINTF(NULL,"POINTER_DEFAULT:%s\n",pointer_default);
			}
		}
	}
	if(!token_list){
		FPRINTF(stderr, "ERROR: ran out of tokens inside header\n");
		Exit(10);
	}
	/* interface */
	if(g_strcmp0(token_list->str, "interface")){
		FPRINTF(stderr, "ERROR: interface not found\n");
		Exit(10);
	}
	token_list=token_list->next;
	if (!token_list){
		FPRINTF(stderr, "ERROR: ran out of tokens\n");
		Exit(10);
	}
	/* interface name */
	ifname=token_list->str;
	token_list=token_list->next;
	FPRINTF(NULL,"Interface:%s\n",ifname);

	/* opnum */
	snprintf(hf_status, BASE_BUFFER_SIZE, "hf_%s_opnum", ifname);
	snprintf(filter_name, BASE_BUFFER_SIZE, "%s.opnum", ifname);
	register_hf_field(hf_status, "Operation", filter_name, "FT_UINT16", "BASE_DEC", "NULL", "0", "");

	/* status */
	snprintf(hf_status, BASE_BUFFER_SIZE, "hf_%s_rc", ifname);
	snprintf(filter_name, BASE_BUFFER_SIZE, "%s.rc", ifname);
	register_hf_field(hf_status, "Return code", filter_name, "FT_UINT32", "BASE_HEX", "VALS(NT_errors)", "0", "");

	FPRINTF(eth_ett, "static gint ett_%s = -1;\n", ifname);
	FPRINTF(eth_ettarr, "		 &ett_%s,\n", ifname);

	/* the body must start with { */
	if(g_strcmp0(token_list->str, "{")){
		FPRINTF(stderr, "ERROR: body does not start with '{'\n");
		Exit(10);
	}

	/* skip the initial '{' */
	token_list=token_list->next;

	if(!uuid){
		FPRINTF(stderr, "ERROR: no uuid found\n");
		Exit(10);
	}
	FPRINTF(eth_code,"static e_guid_t uuid_dcerpc_%s = {\n", ifname);
	FPRINTF(eth_code,"	  0x%c%c%c%c%c%c%c%c, 0x%c%c%c%c, 0x%c%c%c%c,\n",uuid[1],uuid[2],uuid[3],uuid[4],uuid[5],uuid[6],uuid[7],uuid[8],uuid[10],uuid[11],uuid[12],uuid[13],uuid[15],uuid[16],uuid[17],uuid[18]);
	FPRINTF(eth_code,"	  { 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c, 0x%c%c}\n",uuid[20],uuid[21],uuid[22],uuid[23],uuid[25],uuid[26],uuid[27],uuid[28],uuid[29],uuid[30],uuid[31],uuid[32],uuid[33],uuid[34],uuid[35],uuid[36]);
	FPRINTF(eth_code,"};\n");
	FPRINTF(eth_code,"\n");

	sscanf(version, "%d.%d", &major, &minor);
	FPRINTF(eth_code,"static guint16 ver_%s = %d;\n", ifname, major);
	FPRINTF(eth_code,"\n");

	FPRINTF(eth_handoff, "	  dcerpc_init_uuid(proto_%s, ett_%s,\n", ifname, ifname);
	FPRINTF(eth_handoff, "		  &uuid_dcerpc_%s, ver_%s,\n", ifname, ifname);
	FPRINTF(eth_handoff, "		  function_dissectors, hf_%s_opnum);\n", ifname);
}



/* this helper function is called by the tokenizer and will just append the
   current token to the linked list
*/
static void pushtoken(char *token)
{
	token_item_t *new_token_item;
	new_token_item=g_new0(token_item_t, 1);
	if (!new_token_item) {
		FPRINTF(stderr, "Can't allocate memory. Exit.\n");
		exit(10);
	}
	new_token_item->next=NULL;
	new_token_item->str=token;
	if(!token_list){
		token_list=new_token_item;
	} else {
		last_token_item->next=new_token_item;
	}
	last_token_item=new_token_item;
}

/* this function reads the idl file and translates it into tokens.
   the tokens are stored in a linked list  token_list of type token_item_t
*/
static void tokenize(FILE *fh)
{
	int ch;
	int fullinecomment=0;
	int normalcomment=0;
	int insidequote=0;
	char qs[4 * BASE_BUFFER_SIZE];
	int qspos=0;
	int insidetoken=0;
	char token[4 * BASE_BUFFER_SIZE];
	int tokenpos=0;

	while(!feof(fh)){
		ch=fgetc(fh);

		/* full line comment */
		if(fullinecomment){
			if( (ch=='\n')||(ch=='\r') ){
				fullinecomment=0;
				linepos=0;
			}
			continue;
		}
		if( (ch=='#')&&(linepos==0) ){
			fullinecomment=1;
			continue;
		}

		/* normal comment */
		if(normalcomment==0){
			if(ch=='/'){
				int nextch;
				nextch=fgetc(fh);
				if(nextch=='*'){
					normalcomment=1;
					continue;
				}
				ungetc(nextch, fh);
			}
		} else {
			if(ch=='*'){
				int nextch;
				nextch=fgetc(fh);
				if(nextch=='/'){
					normalcomment=0;
					continue;
				}
				ungetc(nextch, fh);
			}
			continue;
		}

		/* quoted string */
		if(insidequote){
			if(ch=='"'){
				insidequote=0;
				qs[qspos++]='"';
				qs[qspos]=0;
				pushtoken(g_strdup(qs));
				continue;
			} else {
				qs[qspos++]=(char)ch;
				continue;
			}
		} else {
			if(ch=='"'){
				insidequote=1;
				qs[0]='"';
				qspos=1;
				continue;
			}
		}


		switch(ch){
		case '\n':
		case '\r':
			if(insidetoken){
				insidetoken=0;
				token[tokenpos]=0;
				pushtoken(g_strdup(token));
			}
			line[linepos]=0;

			linepos=0;
			lineno++;
			break;
		case '\t':
		case ' ':
			if(insidetoken){
				insidetoken=0;
				token[tokenpos]=0;
				pushtoken(g_strdup(token));
			}
			break;
		case '[':
		case ']':
		case '(':
		case ')':
		case ',':
		case ';':
		case '*':
		case '=':
			if(insidetoken){
				insidetoken=0;
				token[tokenpos]=0;
				pushtoken(g_strdup(token));
			}
			token[0]=(char)ch;
			token[1]=0;
			pushtoken(g_strdup(token));
			break;
		default:
			if(!insidetoken){
				tokenpos=0;
			}
			insidetoken=1;
			token[tokenpos++]=(char)ch;
			line[linepos++]=(char)ch;
			break;
		}

	}
}


static type_item_t *
find_type(char *name)
{
	type_item_t *tmptype;
	for(tmptype=type_list;tmptype;tmptype=tmptype->next){
		if(!g_strcmp0(tmptype->name, name)){
			break;
		}
	}
	/* autogenerate built in types */
	if(!tmptype){
		char dissectorname[DISSECTORNAME_MAXLEN];
		if(!g_strcmp0(name,"uint16")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uint16(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("uint16", dissectorname, "FT_UINT16", "BASE_DEC", "0", "NULL", 2);
		} else if(!g_strcmp0(name,"int16")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uint16(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("int16", dissectorname, "FT_INT16", "BASE_DEC", "0", "NULL", 2);
		} else if(!g_strcmp0(name,"uint32")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("uint32", dissectorname, "FT_UINT32", "BASE_DEC", "0", "NULL", 4);
		} else if( (!g_strcmp0(name,"int32"))
			|| (!g_strcmp0(name,"long")) ){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			if (!g_strcmp0(name,"int32"))
				tmptype=register_new_type("int32", dissectorname, "FT_INT32", "BASE_DEC", "0", "NULL", 4);
			else
				tmptype=register_new_type("long", dissectorname, "FT_INT32", "BASE_DEC", "0", "NULL", 4);
		} else if( (!g_strcmp0(name,"uint8")) ){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uint8(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("uint8", dissectorname, "FT_UINT8", "BASE_DEC", "0", "NULL", 1);
		} else if( (!g_strcmp0(name,"int8"))
			|| (!g_strcmp0(name, "char")) ){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uint8(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			if (!g_strcmp0(name,"int8"))
				tmptype=register_new_type("int8", dissectorname, "FT_INT8", "BASE_DEC", "0", "NULL", 1);
			else
				tmptype=register_new_type("char", dissectorname, "FT_INT8", "BASE_DEC", "0", "NULL", 1);
		} else if(!g_strcmp0(name,"bool8")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uint8(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("bool8", dissectorname, "FT_INT8", "BASE_DEC", "0", "NULL", 1);
		} else if(!g_strcmp0(name,"unistr")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_cvstring(tvb, offset, pinfo, tree, di, drep, 2, hf_index, FALSE, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("unistr", dissectorname, "FT_STRING", "BASE_NONE", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"ascstr")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_cvstring(tvb, offset, pinfo, tree, di, drep, 1, hf_index, FALSE, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("ascstr", dissectorname, "FT_STRING", "BASE_NONE", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"GUID")
			||!g_strcmp0(name,"uuid_t")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uuid_t(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type(name, dissectorname, "FT_GUID", "BASE_NONE", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"policy_handle")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static e_ctx_hnd policy_hnd;\n");
			FPRINTF(eth_code, "static proto_item *hnd_item;\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset = dissect_nt_policy_hnd(tvb, offset, pinfo, tree, di, drep,\n");
			FPRINTF(eth_code, " 				  hf_index, &policy_hnd, &hnd_item,\n");
			FPRINTF(eth_code, " 				  param&0x01, param&0x02);\n");

			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("policy_handle", dissectorname, "FT_BYTES", "BASE_NONE", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"NTTIME")){
			/* 8 bytes, aligned to 4 bytes */
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, hf_index);\n");

			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("NTTIME", dissectorname, "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"NTTIME_hyper")){
			/* 8 bytes, aligned to 8 bytes */
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    ALIGN_TO_8_BYTES;\n");
			FPRINTF(eth_code, "    offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, hf_index);\n");

			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("NTTIME_hyper", dissectorname, "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"NTTIME_1sec")){
			/* 8 bytes, aligned to 8 bytes */
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    ALIGN_TO_8_BYTES;\n");
			FPRINTF(eth_code, "    offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, hf_index);\n");

			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("NTTIME_1sec", dissectorname, "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"udlong")){
			/* 8 bytes, aligned to 4 bytes */
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("udlong", dissectorname, "FT_UINT64", "BASE_DEC", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"dlong")){
			/* 8 bytes, aligned to 4 bytes */
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("dlong", dissectorname, "FT_INT64", "BASE_DEC", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"uint64")){
			/* 8 bytes, aligned to 8 bytes */
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    \n");
			FPRINTF(eth_code, "    ALIGN_TO_8_BYTES;\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uint64(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("uint64", dissectorname, "FT_UINT64", "BASE_DEC", "0", "NULL", 8);
		} else if(!g_strcmp0(name,"time_t")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    \n");
			FPRINTF(eth_code, "    offset=dissect_ndr_time_t(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("time_t", dissectorname, "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"SID")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    offset=dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, di, drep, param, hf_index);\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("SID", dissectorname, "FT_STRING", "BASE_NONE", "0", "NULL", 4);
		} else if(!g_strcmp0(name,"WERROR")){
			snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, name);
			FPRINTF(NULL,"\nAutogenerating built-in type:%s\n------------\n",name);
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    \n");
			FPRINTF(eth_code, "    offset=dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");
			tmptype=register_new_type("WERROR", dissectorname, "FT_UINT32", "BASE_DEC", "0", "VALS(NT_errors)", 4);
		}
	}

	return tmptype;
}


/* this function will skip past an entire declare ... ; statement */
static void skipdeclare(void)
{
	token_item_t *ti;

	/* first must be the keyword const */
	ti=token_list;
	if(strncmp(ti->str, "declare", 7)){
		FPRINTF(stderr, "ERROR: skipdeclare  first token is not 'declare'\n");
		Exit(10);
	}
	while(ti->str[0] != ';'){
		ti=ti->next;
	}
	ti=ti->next;

	token_list=ti;
}

/* this function will parse a
	   const
   and generate the appropriate code
   const must be followed by a suitable keyword [uint16|uint32|...]
   the const will later be g_removed from the token list
   the function assumes that the const is the first object in the token_list
*/
static void parseconst(void)
{
	token_item_t *ti;
	char *name, *value;

	/* first must be the keyword const */
	ti=token_list;
	if(strncmp(ti->str, "const", 5)){
		FPRINTF(stderr, "ERROR: const  first token is not 'const'\n");
		Exit(10);
	}
	ti=ti->next;

	/* just skip second token */
	ti=ti->next;

	/* third is a variable and not a type */
	if(find_type(ti->str)){
		FPRINTF(stderr, "ERROR: const, not a variable name:%s\n", ti->str);
		Exit(10);
	}
	name=ti->str;
	ti=ti->next;

	/* fourth is '=' */
	if(ti->str[0] != '='){
		FPRINTF(stderr, "ERROR: const  fourth token is not '='\n");
		Exit(10);
	}
	ti=ti->next;

	/* fifth is the value */
	value=ti->str;
	ti=ti->next;

	/* sixth is ';' */
	if(ti->str[0] != ';'){
		FPRINTF(stderr, "ERROR: const  sixth token is not ';'\n");
		Exit(10);
	}
	ti=ti->next;

	FPRINTF(NULL,"\nCONST:%s\n-------\n",name);

	FPRINTF(eth_hdr, "#define %s		%s\n", name, value);

	FPRINTF(NULL,"\n----------\nEND CONST:%s\n",name);

	token_list=ti;
}

/* this function will parse a
	   typedef struct {
   construct and generate the appropriate code.
   the typedef will be g_removed from the token_list once it has been processed
   the function assumes that the typedef is the first object in the token_list
   the function will be called twice, once with pass=0 and once with pass=1
   which controls whether subdissectors are to be generated or whether the
   struct dissector itself is to be generated
*/
static void parsetypedefstruct(int pass)
{
	token_item_t *ti, *tmpti;
	char *struct_name;
	char dissectorname[DISSECTORNAME_MAXLEN];
	char tmpstr[BASE_BUFFER_SIZE], *ptmpstr;
	int level, num_pointers;
	static int alignment;
	type_item_t *type_item;
	char hf_index[BASE_BUFFER_SIZE];
	bracket_item_t *bi=NULL;
	pointer_item_t *pi;
	const char *pointer_type;
	char *field_name;
	guint32 fixed_array_size;
	int is_array_of_pointers;
	int empty_struct = 0;

	ti=token_list;
	if(g_strcmp0(ti->str, "typedef")){
		FPRINTF(stderr, "ERROR: typedefstruct  first token is not 'typedef'\n");
		Exit(10);
	}
	ti=ti->next;

	if(!g_strcmp0(ti->str, "[")){
		ti=parsebrackets(ti, &bi);
	}
	/* check that we know how to handle the bracket thing */
	if(bi){
		if(bi->flags){
			FPRINTF(stderr, "ERROR: typedefstruct unknown bracket flags encountered : 0x%08x\n",bi->flags);
			Exit(10);
		}
	}

	if(g_strcmp0(ti->str, "struct")){
		FPRINTF(stderr, "ERROR: typedefstruct  second token is not 'struct'\n");
		Exit(10);
	}
	ti=ti->next;

	if(g_strcmp0(ti->str, "{")){
		FPRINTF(stderr, "ERROR: typedefstruct  third token is not '{'\n");
		Exit(10);
	}
	ti=ti->next;

	/* Check if the struct is empty (search if there is no end bracket) */
	if(g_strcmp0(ti->str, "}") == 0){
		empty_struct = 1;
	}

	/* search forward until the '}' so we can find the name of the struct */
	for(tmpti=ti,level=0;tmpti;tmpti=tmpti->next){
		if(!g_strcmp0(tmpti->str, "{")){
			level++;
			continue;
		}
		if(!g_strcmp0(tmpti->str, "}")){
			if(!level){
				break;
			}
			level--;
			continue;
		}
	}
	if(!tmpti || !tmpti->next){
		FPRINTF(stderr, "ERROR: typedefstruct  missing matching '}'\n");
		Exit(10);
	}

	struct_name=tmpti->next->str;
	snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, struct_name);

	FPRINTF(NULL,"\nSTRUCT:%s pass:%d\n-------\n",struct_name,pass);

	if(!check_if_to_emit(dissectorname)){
		FPRINTF(NULL,"NOEMIT Skipping this struct dissector.\n");
		ti=tmpti;
		goto typedef_struct_finished;
	}

	/* this is pass 0  so reset alignment to zero and update as items are
	   processed. we need alignment when pass 1 is run.
	   set alignment initially to 1 so we don't fail for empty structs
	*/
	if(pass==0){
		alignment=1;
	}
	/* pass 1  generate header for the struct dissector */
	if(pass==1){
		FPRINTF(eth_ett, "static gint ett_%s_%s = -1;\n", ifname, struct_name);
		FPRINTF(eth_ettarr, "		 &ett_%s_%s,\n", ifname, struct_name);
		FPRINTF(eth_hdr, "int %s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param);\n", dissectorname);
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "int\n");
		FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *parent_tree, dcerpc_info *di _U_, guint8 *drep _U_, int hf_index, guint32 param _U_)\n", dissectorname);
		FPRINTF(eth_code, "{\n");
		FPRINTF(eth_code, "    proto_item *item=NULL;\n");
		if(!empty_struct){
			FPRINTF(eth_code, "    proto_tree *tree=NULL;\n");
		}
		FPRINTF(eth_code, "    int old_offset;\n");
		FPRINTF(eth_code, "\n");
		switch(alignment){
		case 1:
			break;
		case 2:
			FPRINTF(eth_code, "    ALIGN_TO_2_BYTES;\n");
			FPRINTF(eth_code, "\n");
			break;
		case 4:
			FPRINTF(eth_code, "    ALIGN_TO_4_BYTES;\n");
			FPRINTF(eth_code, "\n");
			break;
		case 8:
			FPRINTF(eth_code, "    ALIGN_TO_8_BYTES;\n");
			FPRINTF(eth_code, "\n");
			break;
		default:
			FPRINTF(stderr, "ERROR: can not handle alignment:%d\n",alignment);
			Exit(10);
		}
		FPRINTF(eth_code, "    old_offset=offset;\n");
		FPRINTF(eth_code, "    if(parent_tree){\n");
		FPRINTF(eth_code, " 	   item=proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, ENC_NA);\n");
		if(!empty_struct){
			FPRINTF(eth_code, " 	   tree=proto_item_add_subtree(item, ett_%s_%s);\n", ifname, struct_name);
		}
		FPRINTF(eth_code, "    }\n");
		FPRINTF(eth_code, "\n");
	}

	/* scan the struct and create all subdissectors */
	level=0;
	while(ti){
		if(!g_strcmp0(ti->str, "{")){
			level++;
			ti=ti->next;
			continue;
		}
		if(!g_strcmp0(ti->str, "}")){
			if(!level){
				break;
			}
			level--;
			ti=ti->next;
			continue;
		}
		if(!g_strcmp0(ti->str, "[")){
			ti=parsebrackets(ti, &bi);
			continue;
		}

		/* check that we know how to handle the bracket thing */
		if(bi){
			if(bi->flags&(~(BI_SIZE_IS|BI_LENGTH_IS|BI_POINTER))){
				FPRINTF(stderr, "ERROR: typedefstruct unknown bracket flags encountered : 0x%08x\n",bi->flags);
				Exit(10);
			}
		}

		/* handle the type, verify that we KNOW this type */
		type_item=find_type(ti->str);
		if(!type_item){
			FPRINTF(stderr, "ERROR : typedefstruct unknown type %s\n",ti->str);
			Exit(10);
		}
		ti=ti->next;
		/* count the levels of pointers */
		for(num_pointers=0;!g_strcmp0(ti->str, "*");ti=ti->next){
			num_pointers++;
			/* poitners are aligned at 4 byte boundaries */
			if(alignment<4){
				alignment=4;
			}
		}
		/* now that we know how many real pointers there were we must
		   prepend default pointers to the list so it has the right
		   length.
		*/
		pi=prepend_pointer_list(bi?bi->pointer_list:NULL, num_pointers);
		/* keep track of alignment */
		if(alignment<type_item->alignment){
			alignment=type_item->alignment;
		}

		field_name=ti->str;
		ti=ti->next;

		/* see if it is a fixed array */
		fixed_array_size=0;
		is_array_of_pointers=0;
		if(!g_strcmp0(ti->str, "[")){
			/* this might be a fixed array */
			ti=ti->next;

			if(!g_strcmp0("]", ti->str)){
				/* this is just a normal [] array */
				fixed_array_size=0;
			} else if(!g_strcmp0("*", ti->str)){
				pi=prepend_pointer_list(pi, num_pointers+1);
				fixed_array_size=0;
				is_array_of_pointers=1;
				ti=ti->next;
			} else if (!ws_strtou32(ti->str, NULL, &fixed_array_size)) {
				FPRINTF(stderr, "ERROR: invalid integer: %s\n", ti->str);
				Exit(10);
			} else {
				ti=ti->next;
			}

			if(g_strcmp0(ti->str, "]")){
				FPRINTF(stderr, "ERROR: typedefstruct  fixed array does not end with ']' it ended with %s\n",ti->str);
				Exit(10);
			}
			ti=ti->next;
		}

		snprintf(hf_index, BASE_BUFFER_SIZE, "hf_%s_%s_%s", ifname, struct_name, field_name);
		/* pass 0  generate subdissectors */
		if(pass==0){
			char filter_name[BASE_BUFFER_SIZE];
			const char *hf;

			snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_dissect_%s_%s", ifname, struct_name, field_name);
			ptmpstr=g_strdup(tmpstr);

			if(check_if_to_emit(tmpstr)){
			  snprintf(filter_name, BASE_BUFFER_SIZE, "%s.%s.%s", ifname, struct_name, field_name);
			  hf=register_hf_field(hf_index, field_name, filter_name, type_item->ft_type, type_item->base_type, type_item->vals, type_item->mask, "");
			  FPRINTF(eth_code, "static int\n");
			  FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", ptmpstr);
			  FPRINTF(eth_code, "{\n");
			  FPRINTF(eth_code, "	 guint32 param=%s;\n",find_dissector_param_value(ptmpstr));
			  FPRINTF(eth_code, "	 offset=%s(tvb, offset, pinfo, tree, di, drep, %s, param);\n", type_item->dissector, hf);
			  FPRINTF(eth_code, "	 return offset;\n");
			  FPRINTF(eth_code, "}\n");
			  FPRINTF(eth_code, "\n");
			} else {
			  FPRINTF(NULL,"NOEMIT Skipping this struct item :%s\n",tmpstr);
			}

			if(is_array_of_pointers){
				pointer_type=pi->type;
				pi=pi->next;
				snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_%s", pointer_type, ptmpstr);
				if(check_if_to_emit(tmpstr)){
				  FPRINTF(eth_code, "static int\n");
				  FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				  FPRINTF(eth_code, "{\n");
				  FPRINTF(eth_code, "	 offset=dissect_ndr_embedded_pointer(tvb, offset, pinfo, tree, di, drep, %s, %s, \"%s\", -1);\n", ptmpstr, ptr_to_define(pointer_type), field_name);
				  FPRINTF(eth_code, "	 return offset;\n");
				  FPRINTF(eth_code, "}\n");
				  FPRINTF(eth_code, "\n");
				} else {
				  FPRINTF(NULL,"NOEMIT Skipping this struct item :%s\n",tmpstr);
				}

				ptmpstr=g_strdup(tmpstr);
			} else if(fixed_array_size){
				snprintf(tmpstr, BASE_BUFFER_SIZE, "fixedarray_%s", ptmpstr);
				if(check_if_to_emit(tmpstr)){
				  FPRINTF(eth_code, "static int\n");
				  FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				  FPRINTF(eth_code, "{\n");
				  FPRINTF(eth_code, "	 int count=%d;\n",fixed_array_size);
				  FPRINTF(eth_code, "	 while(count--){\n");
				  FPRINTF(eth_code, "		 offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
				  FPRINTF(eth_code, "	 }\n");
				  FPRINTF(eth_code, "\n");
				  FPRINTF(eth_code, "	 return offset;\n");
				  FPRINTF(eth_code, "}\n");
				  FPRINTF(eth_code, "\n");
				} else {
				  FPRINTF(NULL,"NOEMIT Skipping this struct item :%s\n",tmpstr);
				}
				ptmpstr=g_strdup(tmpstr);
			}

			/* handle switch_is */
			if(bi){
			  switch(bi->flags&(BI_SIZE_IS|BI_LENGTH_IS)){
			  case 0:
				break;
			  case BI_SIZE_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "ucarray_%s", ptmpstr);
				if(check_if_to_emit(tmpstr)){
				  FPRINTF(eth_code, "static int\n");
				  FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				  FPRINTF(eth_code, "{\n");
				  FPRINTF(eth_code, "	 offset=dissect_ndr_ucarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
				  FPRINTF(eth_code, "	 return offset;\n");
				  FPRINTF(eth_code, "}\n");
				  FPRINTF(eth_code, "\n");
				} else {
				  FPRINTF(NULL,"NOEMIT Skipping this struct item :%s\n",tmpstr);
				}
				ptmpstr=g_strdup(tmpstr);
				break;
			  case BI_LENGTH_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "uvarray_%s", ptmpstr);
				if(check_if_to_emit(tmpstr)){
				  FPRINTF(eth_code, "static int\n");
				  FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				  FPRINTF(eth_code, "{\n");
				  FPRINTF(eth_code, "	 offset=dissect_ndr_uvarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
				  FPRINTF(eth_code, "	 return offset;\n");
				  FPRINTF(eth_code, "}\n");
				  FPRINTF(eth_code, "\n");
				} else {
				  FPRINTF(NULL,"NOEMIT Skipping this struct item :%s\n",tmpstr);
				}
				ptmpstr=g_strdup(tmpstr);
				break;
			  case BI_SIZE_IS|BI_LENGTH_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "ucvarray_%s", ptmpstr);
				if(check_if_to_emit(tmpstr)){
				  FPRINTF(eth_code, "static int\n");
				  FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				  FPRINTF(eth_code, "{\n");
				  FPRINTF(eth_code, "	 offset=dissect_ndr_ucvarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
				  FPRINTF(eth_code, "	 return offset;\n");
				  FPRINTF(eth_code, "}\n");
				  FPRINTF(eth_code, "\n");
				} else {
				  FPRINTF(NULL,"NOEMIT Skipping this struct item :%s\n",tmpstr);
				}
				ptmpstr=g_strdup(tmpstr);
				break;
			  default:
				FPRINTF(stderr, "ERROR: typedefstruct can not handle this combination of sizeis/lengthis\n");
				Exit(10);
			  }
			}

			/* handle pointers */
			while(num_pointers--){
				pointer_type=pi->type;
				pi=pi->next;
				snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_%s", pointer_type, ptmpstr);
				if(check_if_to_emit(tmpstr)){
				  FPRINTF(eth_code, "static int\n");
				  FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				  FPRINTF(eth_code, "{\n");
				  FPRINTF(eth_code, "	 offset=dissect_ndr_embedded_pointer(tvb, offset, pinfo, tree, di, drep, %s, %s, \"%s\", -1);\n", ptmpstr, ptr_to_define(pointer_type), field_name);
				  FPRINTF(eth_code, "	 return offset;\n");
				  FPRINTF(eth_code, "}\n");
				  FPRINTF(eth_code, "\n");
				} else {
				  FPRINTF(NULL,"NOEMIT Skipping this struct item :%s\n",tmpstr);
				}

				ptmpstr=g_strdup(tmpstr);
			}
		}

		if(pass==1){
			snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_dissect_%s_%s", ifname, struct_name, field_name);
			ptmpstr=g_strdup(tmpstr);

			/* handle fixedsizearrays */
			if(is_array_of_pointers){
				pointer_type=pi->type;
				pi=pi->next;
				snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_%s", pointer_type, ptmpstr);
				ptmpstr=g_strdup(tmpstr);
			} else if(fixed_array_size){
				snprintf(tmpstr, BASE_BUFFER_SIZE, "fixedarray_%s", ptmpstr);
				ptmpstr=g_strdup(tmpstr);
			}

			/* handle switch_is */
			if(bi){
			  switch(bi->flags&(BI_SIZE_IS|BI_LENGTH_IS)){
			  case 0:
				break;
			  case BI_SIZE_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "ucarray_%s", ptmpstr);
				ptmpstr=g_strdup(tmpstr);
				break;
			  case BI_LENGTH_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "uvarray_%s", ptmpstr);
				ptmpstr=g_strdup(tmpstr);
				break;
			  case BI_SIZE_IS|BI_LENGTH_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "ucvarray_%s", ptmpstr);
				ptmpstr=g_strdup(tmpstr);
				break;
			  default:
				FPRINTF(stderr, "ERROR: typedefstruct can not handle this combination of sizeis/lengthis\n");
				Exit(10);
			  }
			}

			/* handle pointers */
			while(num_pointers--){
				pointer_type=pi->type;
				pi=pi->next;
				snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_%s", pointer_type, ptmpstr);
				ptmpstr=g_strdup(tmpstr);
			}

			FPRINTF(eth_code, "    offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
			FPRINTF(eth_code, "\n");
		}

		if(g_strcmp0(ti->str,";")){
			FPRINTF(stderr, "ERROR: field does not en with ';'\n");
			Exit(10);
		}
		ti=ti->next;
		bi=NULL; /* clear bi before we start on the next field */
	}

	if(pass==1){
		FPRINTF(eth_code, "    proto_item_set_len(item, offset-old_offset);\n");
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "    return offset;\n");
		FPRINTF(eth_code, "}\n");
		register_new_type(struct_name, dissectorname, "FT_NONE", "BASE_NONE", "0", "NULL", alignment);
	}


typedef_struct_finished:
	FPRINTF(NULL,"\nEND STRUCT:%s pass:%d\n-------\n",struct_name,pass);

	/* only advance token_list for pass==1
	   ti now points to the '}' token
	*/
	if(pass==1){
		if(!ti || g_strcmp0(ti->str,"}")){
			FPRINTF(stderr, "ERROR: struct does not end with '}'\n");
			Exit(10);
		}
		ti=ti->next;

		/* just skip the name */
		ti=ti->next;

		if(!ti || g_strcmp0(ti->str,";")){
			FPRINTF(stderr, "ERROR: struct does not end with ';'\n");
			Exit(10);
		}
		ti=ti->next;

		token_list=ti;
	}
}

/* this function will parse a
	   typedef bitmap {
   construct and generate the appropriate code.
   the typedef will be g_removed from the token_list once it has been processed
   the function assumes that the typedef is the first object in the token_list
   the function will be called twice, once with pass=0 and once with pass=1
   which controls whether subdissectors are to be generated or whether the
   bitmap dissector itself is to be generated

   bitmaps are by default 32 bits
*/
static void parsetypedefbitmap(int pass)
{
	token_item_t *ti, *tmpti;
	char *bitmap_name;
	char dissectorname[BASE_BUFFER_SIZE], hf_bitname[BASE_BUFFER_SIZE];
	int alignment;
	unsigned int val;
	char *name, *value;
	bracket_item_t *bi=NULL;

	ti=token_list;
	if(g_strcmp0(ti->str, "typedef")){
		FPRINTF(stderr, "ERROR: typedefbitmap  first token is not 'typedef'\n");
		Exit(10);
	}
	ti=ti->next;

	alignment=4;  /* default size is 32 bits */

	if(!g_strcmp0(ti->str, "[")){
		ti=parsebrackets(ti, &bi);
	}
	/* check that we know how to handle the bracket thing */
	if(bi){
		if(bi->flags&(~(BI_BITMAP32|BI_BITMAP8))){
			FPRINTF(stderr, "ERROR: typedefbitmap unknown bracket flags encountered : 0x%08x\n",bi->flags);
			Exit(10);
		}
		if(bi->flags&BI_BITMAP32){
			alignment=4;
		}
		if(bi->flags&BI_BITMAP8){
			alignment=1;
		}
	}


	if(g_strcmp0(ti->str, "bitmap")){
		FPRINTF(stderr, "ERROR: typedefbitmap  second token is not 'bitmap'\n");
		Exit(10);
	}
	ti=ti->next;

	if(g_strcmp0(ti->str, "{")){
		FPRINTF(stderr, "ERROR: typedefbitmap  third token is not '{'\n");
		Exit(10);
	}
	ti=ti->next;

	/* search forward until the '}' so we can find the name of the bitmap */
	for(tmpti=ti;tmpti;tmpti=tmpti->next){
		if(!g_strcmp0(tmpti->str, "{")){
			FPRINTF(stderr, "ERROR: typedefbitmap '{' encountered inside bitmap\n");
			Exit(10);
		}
		if(!g_strcmp0(tmpti->str, "}")){
			break;
		}
	}
	if (!tmpti || !tmpti->next){
		FPRINTF(stderr, "ERROR: typedefbitmap missing matching '}'\n");
		Exit(10);
	}
	bitmap_name=tmpti->next->str;
	snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, bitmap_name);

	FPRINTF(NULL,"\nBITMAP:%s pass:%d\n-------\n",bitmap_name,pass);

	/* pass 1  generate header for the struct dissector */
	if(pass==1){
		FPRINTF(eth_ett, "static gint ett_%s_%s = -1;\n", ifname, bitmap_name);
		FPRINTF(eth_ettarr, "		 &ett_%s_%s,\n", ifname, bitmap_name);
		FPRINTF(eth_hdr, "int %s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param);\n", dissectorname);
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "int\n");
		FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
		FPRINTF(eth_code, "{\n");
		FPRINTF(eth_code, "    proto_item *item=NULL;\n");
		FPRINTF(eth_code, "    proto_tree *tree=NULL;\n");
		switch(alignment){
		case 1:
			FPRINTF(eth_code, "    guint8 flags;\n");
			FPRINTF(eth_code, "\n");
			break;
		case 4:
			FPRINTF(eth_code, "    guint32 flags;\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    ALIGN_TO_4_BYTES;\n");
			break;
		default:
			FPRINTF(stderr, "ERROR: typedefbitmap can not handle alignment:%d\n",alignment);
			Exit(10);
		}
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "    if(parent_tree){\n");
		FPRINTF(eth_code, " 	   item=proto_tree_add_item(parent_tree, hf_index, tvb, offset, %d, ENC_LITTLE_ENDIAN);\n", alignment);
		FPRINTF(eth_code, " 	   tree=proto_item_add_subtree(item, ett_%s_%s);\n", ifname, bitmap_name);
		FPRINTF(eth_code, "    }\n");
		FPRINTF(eth_code, "\n");
		switch(alignment){
		case 1:
			FPRINTF(eth_code, "    offset=dissect_ndr_uint8(tvb, offset, pinfo, NULL, di, drep, -1, &flags);\n");
			FPRINTF(eth_code, "\n");
			break;
		case 4:
			FPRINTF(eth_code, "    offset=dissect_ndr_uint32(tvb, offset, pinfo, NULL, di, drep, -1, &flags);\n");
			FPRINTF(eth_code, "\n");
			break;
		default:
			FPRINTF(stderr, "ERROR: typedefbitmap can not handle alignment:%d\n",alignment);
			Exit(10);
		}
		FPRINTF(eth_code, "\n");
	}

	/* scan the struct and create call for all bits */
	while(ti){
		if(!g_strcmp0(ti->str, "}")){
			break;
		}
		if(!g_strcmp0(ti->str, "[")){
			FPRINTF(stderr, "ERROR: typedefbitmap can not handle '[' yet\n");
			Exit(10);
		}

		name=ti->str;
		ti=ti->next;
		snprintf(hf_bitname, BASE_BUFFER_SIZE, "hf_%s_%s_%s", ifname, bitmap_name, name);

		if(g_strcmp0(ti->str, "=")){
			FPRINTF(stderr, "ERROR: typedefbitmap i expected a '=' here\n");
			Exit(10);
		}
		ti=ti->next;

		value=ti->str;
		ti=ti->next;
		val=0;
		if(!strncmp(value, "0x", 2)){
			sscanf(value, "0x%x", &val);
		} else {
			FPRINTF(stderr, "ERROR: typedefbitmap can only handle hexadecimal constants\n");
			Exit(10);
		}

		if( val&(val-1) ){
			FPRINTF(stderr, "ERROR: typedefbitmap can only handle single bit fields\n");
			Exit(10);
		}

		if(pass==0){
			char filter_name[BASE_BUFFER_SIZE], base_name[BASE_BUFFER_SIZE], tfs_name[BASE_BUFFER_SIZE];

			snprintf(filter_name, BASE_BUFFER_SIZE, "%s.%s.%s", ifname, bitmap_name, name);
			snprintf(base_name, BASE_BUFFER_SIZE, "%d", alignment*8);
			snprintf(tfs_name, BASE_BUFFER_SIZE, "TFS(&%s_tfs)", name);
			register_hf_field(hf_bitname, name, filter_name, "FT_BOOLEAN", base_name, tfs_name, value, "");

			FPRINTF(eth_code, "static const true_false_string %s_tfs = {\n",name);
			FPRINTF(eth_code, "    \"%s is SET\",\n", name);
			FPRINTF(eth_code, "    \"%s is NOT set\"\n", name);
			FPRINTF(eth_code, "};\n");
			FPRINTF(eth_code, "\n");
		}

		if(pass==1){
			FPRINTF(eth_code, "    proto_tree_add_boolean(tree, %s, tvb, offset-%d, %d, flags);\n", hf_bitname, alignment, alignment);
			FPRINTF(eth_code, "    if(flags&%s){\n", value);
			FPRINTF(eth_code, " 	   proto_item_append_text(item, \" %s\");\n", name);
			FPRINTF(eth_code, "    }\n");
			FPRINTF(eth_code, "    flags&=(~%s);\n", value);
			FPRINTF(eth_code, "\n");
		}

		if(!g_strcmp0(ti->str, ",")){
			ti=ti->next;
			continue;
		}
	}

	if(pass==1){
		FPRINTF(eth_code, "    if(flags){\n");
		FPRINTF(eth_code, " 	   proto_item_append_text(item, \"UNKNOWN-FLAGS\");\n");
		FPRINTF(eth_code, "    }\n");
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "    return offset;\n");
		FPRINTF(eth_code, "}\n");
		switch(alignment){
		case 1:
			register_new_type(bitmap_name, dissectorname, "FT_UINT8", "BASE_HEX", "0", "NULL", alignment);
			break;
		case 4:
			register_new_type(bitmap_name, dissectorname, "FT_UINT32", "BASE_HEX", "0", "NULL", alignment);
			break;
		default:
			FPRINTF(stderr, "ERROR: typedefbitmap can not handle alignment:%d\n",alignment);
			Exit(10);
		}
	}

	FPRINTF(NULL,"\nEND BITMAP:%s pass:%d\n-------\n",bitmap_name,pass);

	/* only advance token_list for pass==1
	   ti now points to the '}' token
	*/
	if(pass==1){
		if(!ti || g_strcmp0(ti->str,"}")){
			FPRINTF(stderr, "ERROR: bitmap does not end with '}'\n");
			Exit(10);
		}
		ti=ti->next;

		/* just skip the name */
		ti=ti->next;

		if(!ti || g_strcmp0(ti->str,";")){
			FPRINTF(stderr, "ERROR: bitmap does not end with ';'\n");
			Exit(10);
		}
		ti=ti->next;

		token_list=ti;
	}
}

/* a case tag might be a negative number, i.e. contain a '-' sign which
   is not valid inside a symbol name in c.
*/
static const char *
case2str(const char *str)
{
	char *newstr;
	if(str[0]!='-'){
		return str;
	}
	newstr=g_strdup(str);
	newstr[0]='m';
	return newstr;
}

/* this function will parse a
	typedef union {
	construct and generate the appropriate code.
	the typedef will be g_removed from the token_list once it has been processed
	the function assumes that the typedef is the first object in the token_list
	the function will be called twice, once with pass=0 and once with pass=1
	which controls whether subdissectors are to be generated or whether the
	union dissector itself is to be generated
*/
static void parsetypedefunion(int pass)
{
	char *union_name;
	token_item_t *ti, *tmpti;
	char dissectorname[BASE_BUFFER_SIZE];
	bracket_item_t *bi=NULL;
	char tmpstr[BASE_BUFFER_SIZE], *ptmpstr;
	int level, num_pointers;
	static int alignment;
	type_item_t *type_item;
	char hf_index[BASE_BUFFER_SIZE];
	int tag_alignment, item_alignment;

	ti=token_list;
	if(g_strcmp0(ti->str, "typedef")){
		FPRINTF(stderr, "ERROR: typedefunion  first token is not 'typedef'\n");
		Exit(10);
	}
	ti=ti->next;

	if(!g_strcmp0(ti->str, "[")){
		ti=parsebrackets(ti, &bi);
	}
	/* check that we know how to handle the bracket thing */
	if(bi){
		if(bi->flags&(~(BI_SWITCH_TYPE))){
			FPRINTF(stderr, "ERROR: typedefunion unknown bracket flags encountered : 0x%08x\n",bi->flags);
			Exit(10);
		}
	}

	if(g_strcmp0(ti->str, "union")){
		FPRINTF(stderr, "ERROR: typedefunion  second token is not 'union'\n");
		Exit(10);
	}
	ti=ti->next;

	if(g_strcmp0(ti->str, "{")){
		FPRINTF(stderr, "ERROR: typedefunion  third token is not '{'\n");
		Exit(10);
	}
	ti=ti->next;

	/* search forward until the '}' so we can find the name of the union */
	for(tmpti=ti,level=0;tmpti;tmpti=tmpti->next){
		if(!g_strcmp0(tmpti->str, "{")){
			level++;
			continue;
		}
		if(!g_strcmp0(tmpti->str, "}")){
			if(!level){
				break;
			}
			level--;
			continue;
		}
	}

	if (!tmpti || !tmpti->next){
		FPRINTF(stderr, "ERROR: typedefunion  missing matching '}'\n");
		Exit(10);
	}
	union_name=tmpti->next->str;
	snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_union_%s", ifname, union_name);

	FPRINTF(NULL,"\nUNION:%s pass:%d\n-------\n",union_name,pass);

	if(bi && bi->flags&BI_SWITCH_TYPE){
		tag_alignment=bi->union_tag_size;
	} else {
		tag_alignment=get_union_tag_size(union_name);
	}

	/* this is pass 0  so reset alignment to the minimum possible value
	   and update as items are processed.
	   we need alignment when pass 1 is run
	*/
	if(pass==0){
		alignment=tag_alignment;
	}

	/* pass 1  generate header for the struct dissector */
	if(pass==1){
		FPRINTF(eth_ett, "static gint ett_%s_%s = -1;\n", ifname, union_name);
		FPRINTF(eth_ettarr, "		 &ett_%s_%s,\n", ifname, union_name);
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "static int\n");
		FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
		FPRINTF(eth_code, "{\n");
		FPRINTF(eth_code, "    proto_item *item=NULL;\n");
		FPRINTF(eth_code, "    proto_tree *tree=NULL;\n");
		FPRINTF(eth_code, "    int old_offset;\n");
		/* we do alignment on the tag itself here so that
		   we skip any alignment padding prior to where the tag
		   itself starts, this makes the byterange in the hexpane
		   for the union expansion start with the first byte of the tag
		*/
		switch(tag_alignment){
		case 1:
			break;
		case 2:
			FPRINTF(eth_code, "    guint16 level;\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    ALIGN_TO_2_BYTES;\n");
			FPRINTF(eth_code, "\n");
			break;
		case 4:
			FPRINTF(eth_code, "    guint32 level = 0;\n");
			FPRINTF(eth_code, "\n");
			FPRINTF(eth_code, "    ALIGN_TO_4_BYTES;\n");
			FPRINTF(eth_code, "\n");
			break;
		default:
			FPRINTF(stderr, "ERROR: typedefunion 1 can not handle alignment:%d\n",alignment);
			Exit(10);
		}
		FPRINTF(eth_code, "    old_offset=offset;\n");
		FPRINTF(eth_code, "    if(parent_tree){\n");
		FPRINTF(eth_code, " 	   tree=proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_%s_%s, &item, \"%s\");\n", ifname, union_name, union_name);
		FPRINTF(eth_code, "    }\n");
		FPRINTF(eth_code, "\n");
		switch(tag_alignment){
		case 1:
			break;
		case 2:
			FPRINTF(eth_code, "    offset=dissect_ndr_uint16(tvb, offset, pinfo, tree,\n");
			FPRINTF(eth_code, " 							 di, drep, hf_index, &level);\n");
		break;
		case 4:
			FPRINTF(eth_code, "    offset=dissect_ndr_uint32(tvb, offset, pinfo, tree,\n");
			FPRINTF(eth_code, " 							 di, drep, hf_index, &level);\n");
			break;
		default:
			FPRINTF(stderr, "ERROR: typedefunion 2 can not handle alignment:%d\n",alignment);
			Exit(10);
		}
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "    switch(level){\n");
	}

	/* scan the struct and create all subdissectors */
	level=0;
	while(ti){
		if(!g_strcmp0(ti->str, "{")){
			ti=ti->next;
			level++;
			continue;
		}
		if(!g_strcmp0(ti->str, "}")){
			if(!level){
				break;
			}
			ti=ti->next;
			level--;
			continue;
		}
		if(!g_strcmp0(ti->str, "[")){
			ti=parsebrackets(ti, &bi);
			continue;
		}

		if(!bi){
			FPRINTF(stderr, "ERROR : typedefunion no brackets found for case\n");
			Exit(10);
		}
		/* make sure we catch when we havent implemented everything
		   yet.
		   we currently only know about CASE and CASE_DEFAULT flags
		*/
		if(bi->flags&(~(BI_CASE|BI_CASE_DEFAULT|BI_POINTER))){
			FPRINTF(stderr, "ERROR: typedefunion unknown bracket flags encountered : 0x%08x\n",bi->flags);
			Exit(10);
		}
		if(!(bi->flags&BI_CASE)){
			FPRINTF(stderr, "ERROR : typedefunion no case found in brackets\n");
			Exit(10);
		}
#ifdef g_removeD
		/* only empty default cases for now */
		if(bi->flags&BI_CASE_DEFAULT){
			if(g_strcmp0(ti->str,";")){
				FPRINTF(stderr, "ERROR: default tag is not empty\n");
				Exit(10);
			}
			ti=ti->next;
			continue;
		}
#endif

		/* just skip all and any 'empty' arms */
		if(!g_strcmp0(ti->str, ";")){
			ti=ti->next;
			continue;
		}

		/* handle the type, verify that we KNOW this type */
		type_item=find_type(ti->str);
		if(!type_item){
			FPRINTF(stderr, "ERROR : typedefunion unknown type %s\n",ti->str);
			Exit(10);
		}
		ti=ti->next;
		/* count the levels of pointers */
		for(num_pointers=0;!g_strcmp0(ti->str, "*");ti=ti->next){
			num_pointers++;
		}

		/* keep track of alignment */
		if(num_pointers){
			item_alignment=4;
		} else {
			item_alignment=type_item->alignment;
		}
		if(alignment<item_alignment){
			alignment=item_alignment;
		}

		snprintf(hf_index, BASE_BUFFER_SIZE, "hf_%s_%s_%s_%s", ifname, union_name, case2str(bi->case_name), ti->str);
		/* pass 0  generate subdissectors */
		if(pass==0){
			char filter_name[BASE_BUFFER_SIZE];
			const char *hf;

			snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_dissect_union_%s_%s_%s", ifname, union_name, case2str(bi->case_name), ti->str);
			ptmpstr=g_strdup(tmpstr);

			snprintf(filter_name, BASE_BUFFER_SIZE, "%s.%s.%s", ifname, union_name, ti->str);
			hf=register_hf_field(hf_index, ti->str, filter_name, type_item->ft_type, type_item->base_type, type_item->vals, type_item->mask, "");

			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", ptmpstr);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    guint32 param=%s;\n",find_dissector_param_value(ptmpstr));
			FPRINTF(eth_code, "    offset=%s(tvb, offset, pinfo, tree, di, drep, %s, param);\n", type_item->dissector, hf);
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");

			/* handle pointers */
			while(num_pointers--){
				snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_%s", ptmpstr, "unique");
				FPRINTF(eth_code, "static int\n");
				FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				FPRINTF(eth_code, "{\n");
				FPRINTF(eth_code, "    offset=dissect_ndr_embedded_pointer(tvb, offset, pinfo, tree, di, drep, %s, NDR_POINTER_UNIQUE, \"%s\", -1);\n", ptmpstr, ti->str);
				FPRINTF(eth_code, "    return offset;\n");
				FPRINTF(eth_code, "}\n");
				FPRINTF(eth_code, "\n");

				ptmpstr=g_strdup(tmpstr);

			}
		}

		if(pass==1){
			/* handle pointers */
			snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_dissect_union_%s_%s_%s", ifname, union_name, case2str(bi->case_name), ti->str);
			ptmpstr=g_strdup(tmpstr);
			while(num_pointers--){
				snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_%s", ptmpstr, "unique");
				ptmpstr=g_strdup(tmpstr);
			}

			if(bi->flags&BI_CASE_DEFAULT){
				FPRINTF(eth_code, "    default:\n");
			} else {
				FPRINTF(eth_code, "    case %s:\n",bi->case_name);
			}
			/* each arm itself is aligned independently */
			switch(item_alignment){
			case 1:
				break;
			case 2:
				FPRINTF(eth_code, " 	   ALIGN_TO_2_BYTES;\n");
				break;
			case 4:
				FPRINTF(eth_code, " 	   ALIGN_TO_4_BYTES;\n");
				break;
			case 8:
				FPRINTF(eth_code, " 	   ALIGN_TO_8_BYTES;\n");
				break;
			default:
				FPRINTF(stderr, "ERROR: typedefunion 3 can not handle alignment:%d\n",item_alignment);
				Exit(10);
			}
			FPRINTF(eth_code, " 	   offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
			FPRINTF(eth_code, " 	   break;\n");
			FPRINTF(eth_code, "\n");
		}
		ti=ti->next;

		if(g_strcmp0(ti->str,";")){
			FPRINTF(stderr, "ERROR: field does not end with ';'\n");
			Exit(10);
		}
		ti=ti->next;
	}

	if(pass==1){
		FPRINTF(eth_code, "    }\n");
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "    proto_item_set_len(item, offset-old_offset);\n");
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "   return offset;\n");
		FPRINTF(eth_code, "}\n");
		switch(tag_alignment){
		case 2:
			register_new_type(union_name, dissectorname, "FT_UINT16", "BASE_DEC", "0", "NULL", alignment);
			break;
		case 4:
			register_new_type(union_name, dissectorname, "FT_UINT32", "BASE_DEC", "0", "NULL", alignment);
			break;
		default:
			FPRINTF(stderr, "ERROR: typedefunion 4 can not handle alignment:%d\n",alignment);
			Exit(10);
		}
	}

	FPRINTF(NULL,"\nEND UNION:%s pass:%d\n-------\n",union_name,pass);

	/* only advance token_list for pass==1
	   ti now points to the '}' token
	*/
	if(pass==1){
		if(!ti || g_strcmp0(ti->str,"}")){
			FPRINTF(stderr, "ERROR: union does not end with '}'\n");
			Exit(10);
		}
		ti=ti->next;

		/* just skip the name */
		ti=ti->next;

		if(!ti || g_strcmp0(ti->str,";")){
			FPRINTF(stderr, "ERROR: union does not end with ';'\n");
			Exit(10);
		}
		ti=ti->next;

		token_list=ti;
	}
}


/* this function will parse a
	   WERROR function (
   construct and generate the appropriate code.
   the function will be g_removed from the token_list once it has been processed
   the function assumes that the function is the first object in the token_list
   the function will be called three times with
	 pass=0   generate subdissectors and entries for the function table
	 pass=1   generate code for the REQUEST
	 pass=2   generate code for the REPLY
*/
static void parsefunction(int pass)
{
	char *function_name;
	static int funcno=0;
	token_item_t *ti;
	bracket_item_t *bi=NULL;
	pointer_item_t *pi;
	const char *pointer_type;

	char tmpstr[BASE_BUFFER_SIZE], *ptmpstr;
	int level, num_pointers;
	type_item_t *type_item;
	char hf_index[BASE_BUFFER_SIZE];

	ti=token_list;
	if(g_strcmp0(ti->str, "WERROR")){
		FPRINTF(stderr, "ERROR: function  first token is not 'WERROR'\n");
		Exit(10);
	}
	ti=ti->next;

	function_name=ti->str;
	ti=ti->next;

	if(g_strcmp0(ti->str, "(")){
		FPRINTF(stderr, "ERROR: function  third token is not '('\n");
		Exit(10);
	}
	ti=ti->next;

	FPRINTF(NULL,"\nFUNCTION:%s pass:%d\n-------\n",function_name,pass);

	if(pass==0){
		FPRINTF(eth_ft, "	 { %d, \"%s\",\n",funcno,function_name);
		FPRINTF(eth_ft, "		 %s_dissect_%s_request,\n", ifname, function_name);
		FPRINTF(eth_ft, "		 %s_dissect_%s_response },\n", ifname, function_name);
		funcno++;
	}

	/* pass 1,2  generate header for the function dissector */
	if((pass==1)||(pass==2)){
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "static int\n");
		FPRINTF(eth_code, "%s_dissect_%s_%s(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, guint8 *drep _U_)\n", ifname, function_name, (pass==1)?"request":"response");
		FPRINTF(eth_code, "{\n");
	}

	/* scan the struct and create all subdissectors */
	level=0;
	while(ti){
		if(!g_strcmp0(ti->str, "(")){
			ti=ti->next;
			level++;
			continue;
		}
		if(!g_strcmp0(ti->str, ")")){
			if(!level){
				break;
			}
			ti=ti->next;
			level--;
			continue;
		}
		if(!g_strcmp0(ti->str, "[")){
			ti=parsebrackets(ti, &bi);
			continue;
		}

		if(!bi){
			FPRINTF(stderr, "ERROR : function no brackets found for case\n");
			Exit(10);
		}

		/* make sure we catch when we havent implemented everything
		   yet.
		   we currently only know about IN and OUT flags
		*/
		if(bi->flags&(~(BI_IN|BI_OUT|BI_POINTER|BI_SIZE_IS|BI_LENGTH_IS))){
			FPRINTF(stderr, "ERROR: function unknown bracket flags encountered : 0x%08x\n",bi->flags);
			Exit(10);
		}
		if(!(bi->flags&(BI_IN|BI_OUT))){
			FPRINTF(stderr, "ERROR : function  parameter is neither in nor out\n");
			Exit(10);
		}

		/* handle the type, verify that we KNOW this type */
		type_item=find_type(ti->str);
		if(!type_item){
			FPRINTF(stderr, "ERROR : function unknown type %s\n",ti->str);
			Exit(10);
		}
		ti=ti->next;
		/* count the levels of pointers */
		for(num_pointers=0;!g_strcmp0(ti->str, "*");ti=ti->next){
			num_pointers++;
		}

		/* now that we know how many real poitner there were we must
		   prepend default pointers to the list so it has the right
		   length.
		*/
		pi=prepend_pointer_list(bi->pointer_list, num_pointers);

		snprintf(hf_index, BASE_BUFFER_SIZE, "hf_%s_%s_%s", ifname, function_name, ti->str);
		/* pass 0  generate subdissectors */
		if(pass==0){
			char filter_name[BASE_BUFFER_SIZE];
			const char *hf;

			snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_dissect_%s_%s", ifname, function_name, ti->str);
			ptmpstr=g_strdup(tmpstr);

			snprintf(filter_name, BASE_BUFFER_SIZE, "%s.%s.%s", ifname, function_name, ti->str);
			hf=register_hf_field(hf_index, ti->str, filter_name, type_item->ft_type, type_item->base_type, type_item->vals, type_item->mask, "");

			FPRINTF(eth_code, "static int\n");
			FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", ptmpstr);
			FPRINTF(eth_code, "{\n");
			FPRINTF(eth_code, "    guint32 param=%s;\n",find_dissector_param_value(ptmpstr));
			FPRINTF(eth_code, "    offset=%s(tvb, offset, pinfo, tree, di, drep, %s, param);\n", type_item->dissector, hf);
			FPRINTF(eth_code, "    return offset;\n");
			FPRINTF(eth_code, "}\n");
			FPRINTF(eth_code, "\n");


			/* handle switch_is */
			if(bi){
			  switch(bi->flags&(BI_SIZE_IS|BI_LENGTH_IS)){
			  case 0:
				break;
			  case BI_SIZE_IS|BI_LENGTH_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "ucvarray_%s", ptmpstr);
				FPRINTF(eth_code, "static int\n");
				FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				FPRINTF(eth_code, "{\n");
				FPRINTF(eth_code, "    offset=dissect_ndr_ucvarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
				FPRINTF(eth_code, "    return offset;\n");
				FPRINTF(eth_code, "}\n");
				FPRINTF(eth_code, "\n");
				ptmpstr=g_strdup(tmpstr);
				break;
			  case BI_SIZE_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "ucarray_%s", ptmpstr);
				FPRINTF(eth_code, "static int\n");
				FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				FPRINTF(eth_code, "{\n");
				FPRINTF(eth_code, "    offset=dissect_ndr_ucarray(tvb, offset, pinfo, tree, di, drep, %s);\n", ptmpstr);
				FPRINTF(eth_code, "    return offset;\n");
				FPRINTF(eth_code, "}\n");
				FPRINTF(eth_code, "\n");
				ptmpstr=g_strdup(tmpstr);
				break;
			  default:
				FPRINTF(stderr, "ERROR: typedeffunction can not handle this combination of sizeis/lengthis\n");
				Exit(10);
			  }
			}

			/* handle pointers */
			while(num_pointers--){
				pointer_type=pi->type;
				pi=pi->next;
				snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_%s", pointer_type, ptmpstr);
				FPRINTF(eth_code, "static int\n");
				FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep)\n", tmpstr);
				FPRINTF(eth_code, "{\n");
				FPRINTF(eth_code, "    offset=dissect_ndr_toplevel_pointer(tvb, offset, pinfo, tree, di, drep, %s, %s, \"%s\", -1);\n", ptmpstr, ptr_to_define(pointer_type), ti->str);
				FPRINTF(eth_code, "    return offset;\n");
				FPRINTF(eth_code, "}\n");
				FPRINTF(eth_code, "\n");

				ptmpstr=g_strdup(tmpstr);

			}
		}

		if((pass==1)||(pass==2)){
			snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_dissect_%s_%s", ifname, function_name, ti->str);
			ptmpstr=g_strdup(tmpstr);

			if(bi){
			  switch(bi->flags&(BI_SIZE_IS|BI_LENGTH_IS)){
			  case 0:
				break;
			  case BI_SIZE_IS|BI_LENGTH_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "ucvarray_%s", ptmpstr);
				ptmpstr=g_strdup(tmpstr);
				break;
			  case BI_SIZE_IS:
				snprintf(tmpstr, BASE_BUFFER_SIZE, "ucarray_%s", ptmpstr);
				ptmpstr=g_strdup(tmpstr);
				break;
			  default:
				FPRINTF(stderr, "ERROR: typedeffunction can not handle this combination of sizeis/lengthis\n");
				Exit(10);
			  }
			}

			/* handle pointers */
			while(num_pointers--){
				pointer_type=pi->type;
				pi=pi->next;
				snprintf(tmpstr, BASE_BUFFER_SIZE, "%s_%s", pointer_type, ptmpstr);
				ptmpstr=g_strdup(tmpstr);
			}

			if((pass==1)&&(bi->flags&BI_IN)){
				FPRINTF(eth_code, " 	   offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
				FPRINTF(eth_code, " 	   offset=dissect_deferred_pointers(pinfo, tvb, offset, di, drep);\n");
				FPRINTF(eth_code, "\n");
			}
			if((pass==2)&&(bi->flags&BI_OUT)){
				FPRINTF(eth_code, " 	   offset=%s(tvb, offset, pinfo, tree, di, drep);\n", ptmpstr);
				FPRINTF(eth_code, " 	   offset=dissect_deferred_pointers(pinfo, tvb, offset, di, drep);\n");
				FPRINTF(eth_code, "\n");
			}
		}
		ti=ti->next;


		if(!g_strcmp0(ti->str,",")){
			ti=ti->next;
			continue;
		}
	}

	if((pass==1)||(pass==2)){
		if(pass==2){
			FPRINTF(eth_code, "   offset=dissect_ntstatus(tvb, offset, pinfo, tree, di, drep, %s, NULL);\n", hf_status);
			FPRINTF(eth_code, "\n");
		}
		FPRINTF(eth_code, "\n");
		FPRINTF(eth_code, "   return offset;\n");
		FPRINTF(eth_code, "}\n");
	}

	FPRINTF(NULL,"\nEND FUNCTION:%s pass:%d\n-------\n",function_name,pass);

	/* only advance token_list for pass==2
	   ti now points to the ')' token
	*/
	if(pass==2){
		if(!ti || g_strcmp0(ti->str,")")){
			FPRINTF(stderr, "ERROR: function does not end with ')'\n");
			Exit(10);
		}
		ti=ti->next;

		if(!ti || g_strcmp0(ti->str,";")){
			FPRINTF(stderr, "ERROR: function does not end with ';'\n");
			Exit(10);
		}
		ti=ti->next;

		token_list=ti;
	}
}


/* this function will parse a
	   typedef enum {
   or a
	   typedef [ v1_enum ] enum {
   construct and generate the appropriate code.
   the typedef will be g_removed from the token_list once it has been processed
   the function assumes that the typedef is the first object in the token_list
*/
static void parsetypedefenum(void)
{
	token_item_t *ti;
	enum_list_t *enum_list, *el, *lastel;
	char *p;
	long val;
	int eval, enumsize;
	char dissectorname[BASE_BUFFER_SIZE], valsstring[BASE_BUFFER_SIZE], hfvalsstring[BASE_BUFFER_SIZE];

	enumsize=16;

	ti=token_list;
	if(g_strcmp0(ti->str, "typedef")){
		FPRINTF(stderr, "ERROR: typedefenum  first token is not 'typedef'\n");
		Exit(10);
	}
	ti=ti->next;

	/* this could be a [ v1_enum ] */
	if(!g_strcmp0(ti->str, "[")){
		ti=ti->next;

		if(g_strcmp0(ti->str, "v1_enum")){
			FPRINTF(stderr, "ERROR: typedefenum  not 'v1_enum' inside brackets\n");
			Exit(10);
		}
		ti=ti->next;

		if(g_strcmp0(ti->str, "]")){
			FPRINTF(stderr, "ERROR: typedefenum  'v1_enum' is not followed by ']'\n");
			Exit(10);
		}
		ti=ti->next;

		enumsize=32;
	}


	if(g_strcmp0(ti->str, "enum")){
		FPRINTF(stderr, "ERROR: typedefenum  second token is not 'enum'\n");
		Exit(10);
	}
	ti=ti->next;

	if(g_strcmp0(ti->str, "{")){
		FPRINTF(stderr, "ERROR: typedefenum  third token is not '{'\n");
		Exit(10);
	}
	ti=ti->next;

	/* now parse all values until we find the "}" */
	eval=0;
	enum_list=NULL;
	lastel=NULL;
	while(1){
		/* check for '}' */
		if(!g_strcmp0(ti->str,"}")){
			ti=ti->next;
			break;
		}

		/* handle 4 types of entries:
		 * 1, CONST = value,
		 * 2, CONST,
		 * 3, CONST = value}
		 * 4, CONST}
		 */
		el=g_new0(enum_list_t, 1);
		if (!el) {
			FPRINTF(stderr, "Can't allocate memory. Exit.\n");
			exit(10);
		}
		el->next=NULL;
		if(!enum_list){
			enum_list=el;
		} else {
			lastel->next=el;
		}
		lastel=el;

		/* grab CONST */
		el->name=ti->str;
		ti=ti->next;

		/* grab separator */
		if(!g_strcmp0(ti->str,"=")){
			ti=ti->next;
			/* grab value */
			val=strtol(ti->str,&p,0);
			if (p==ti->str||*p) {
				FPRINTF(stderr, "ERROR: typedefenum value is not a number\n");
				Exit(10);
			}
			el->val=(int)val;
			ti=ti->next;
		} else {
			el->val=eval;
		}
		eval=el->val+1;

		/* check for ',' */
		if(!g_strcmp0(ti->str,",")){
			ti=ti->next;
			continue;
		}

		/* check for '}' */
		if(!g_strcmp0(ti->str,"}")){
			ti=ti->next;
			break;
		}

		FPRINTF(stderr,"ERROR: typedefenum should not be reached\n");
		Exit(10);
	}

	/* verify that it ends with a ';' */
	if(g_strcmp0(ti->next->str,";")){
		FPRINTF(stderr,"ERROR enum terminator is not ';'\n");
		Exit(10);
	}

	snprintf(valsstring, BASE_BUFFER_SIZE, "%s_%s_vals", ifname, ti->str);
	snprintf(dissectorname, DISSECTORNAME_MAXLEN, "%s_dissect_%s", ifname, ti->str);

	FPRINTF(NULL,"\nENUM:%s\n-------\n",ti->str);

	FPRINTF(eth_hdr, "\n");
	for(el=enum_list;el;el=el->next){
		FPRINTF(eth_hdr, "#define %s		%d\n", el->name, el->val);
	}

	FPRINTF(eth_hdr, "\n");
	FPRINTF(eth_hdr, "extern const value_string %s[];\n", valsstring);
	FPRINTF(eth_hdr, "int %s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param);\n", dissectorname);

	FPRINTF(eth_code, "\n");
	FPRINTF(eth_code, "const value_string %s[] = {\n", valsstring);

	for(el=enum_list;el;el=el->next){
		FPRINTF(eth_code, "    { %d	, \"%s\" },\n", el->val, el->name);
	}
	FPRINTF(eth_code, "    { 0	, NULL }\n");
	FPRINTF(eth_code, "};\n");

	FPRINTF(eth_code, "\n");
	FPRINTF(eth_code, "int\n");
	FPRINTF(eth_code, "%s(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, dcerpc_info *di, guint8 *drep, int hf_index, guint32 param _U_)\n", dissectorname);
	FPRINTF(eth_code, "{\n");
	switch(enumsize){
	case 16:
		FPRINTF(eth_code, "    offset=dissect_ndr_uint16(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
		break;
	case 32:
		FPRINTF(eth_code, "    offset=dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf_index, NULL);\n");
		break;
	default:
		FPRINTF(stderr,"ERROR enum unknown size\n");
		Exit(10);
	}

	FPRINTF(eth_code, "    return offset;\n");
	FPRINTF(eth_code, "}\n");
	FPRINTF(eth_code, "\n");


	snprintf(hfvalsstring, BASE_BUFFER_SIZE, "VALS(%s)", valsstring);
	switch(enumsize){
	case 16:
		register_new_type(ti->str, dissectorname, "FT_INT16", "BASE_DEC", "0", hfvalsstring, 2);
		break;
	case 32:
		register_new_type(ti->str, dissectorname, "FT_INT32", "BASE_DEC", "0", hfvalsstring, 4);
		break;
	default:
		FPRINTF(stderr,"ERROR enum unknown size\n");
		Exit(10);
	}

	FPRINTF(NULL,"\n----------\nEND ENUM:%s\n",ti->str);

	/* skip past the name and the ';' */
	token_list=ti->next->next;
}

typedef struct _trimmed_prefixes_t {
	struct _trimmed_prefixes_t *next;
	char *name;
} trimmed_prefixes_t;
static trimmed_prefixes_t *prefixes_to_trim=NULL;

static void preparetrimprefix(char *prefix_name)
{
	trimmed_prefixes_t *new_prefix;
	new_prefix=g_new0(trimmed_prefixes_t, 1);
	if (!new_prefix) {
		FPRINTF(stderr, "Can't allocate memory. Exit.\n");
		exit(10);
	}
	new_prefix->next=prefixes_to_trim;
	prefixes_to_trim=new_prefix;
	new_prefix->name=g_strdup(prefix_name);
}

static void
trimprefix(void)
{
	token_item_t *ti;
	trimmed_prefixes_t *pfx;
	size_t len;

	for(pfx=prefixes_to_trim;pfx;pfx=pfx->next){
		len=strlen(pfx->name);
		for(ti=token_list;ti;ti=ti->next){
			if(!strncmp(ti->str, pfx->name, len)){
				ti->str+=len;
			}
		}
	}
}

static int Exit(int code)
{
	FPRINTF(stderr, "The tokens remaining when aborting:\n");
	printtokenlist(10);

	exit(code);
}

static void usage(void)
{
	FPRINTF(stderr, "Usage: idl2wrs <interface>\n");
}

static void
mergefile(const char *name, FILE *outfile)
{
	FILE *infile;

	FPRINTF(outfile, "\n\n/* INCLUDED FILE : %s */\n", name);
	infile=g_fopen(name, "r");
	while(!feof(infile)){
		int ch;
		ch=fgetc(infile);
		if(ch!=-1){
			fputc(ch, outfile);
		}
	}
	fclose(infile);
	FPRINTF(outfile, "/* END OF INCLUDED FILE : %s */\n\n\n", name);
}



static char *
str_read_string(char *str, char **name)
{
	char tmpstr[BASE_BUFFER_SIZE], *strptr;
	int skip_blanks;
	int quoted_string;

	strptr=tmpstr;
	skip_blanks=1;
	quoted_string=0;
	while(1){
		if(!*str){
			*strptr=0;
			*name=g_strdup(tmpstr);
			return str;
		}
		if(skip_blanks){
			if( (*str==' ') || (*str=='\t') ){
				str++;
				continue;
			}
			if( *str=='"' ){
				str++;
				quoted_string=1;
			}
			skip_blanks=0;
			continue;
		}
		if( (*str==' ') || (*str=='\t') ){
			if(quoted_string){
				*strptr++ = *str++;
				continue;
			}
			*strptr=0;
			*name=g_strdup(tmpstr);
			return str;
		}
		if( (*str=='"') || (*str=='\n') ){
			*strptr=0;
			*name=g_strdup(tmpstr);
			return ++str;
		}
		*strptr++ = *str++;
	}
	return NULL;
}

static void
readcnffile(FILE *fh)
{
	char cnfline[4 * BASE_BUFFER_SIZE];

	FPRINTF(NULL, "Reading conformance file\n=======================\n");
	while(!feof(fh)){
		cnfline[0]=0;
		if(!fgets(cnfline, 4 * BASE_BUFFER_SIZE, fh) || !cnfline[0]){
			continue;
		}
		if(cnfline[0]=='#'){
			/* ignore all comments */
		} else if(!strncmp(cnfline, "NOEMIT", 6)){
			no_emit_item_t *nei;
			char *str, *name;

			str=cnfline+6;
			str_read_string(str, &name);
			nei=g_new0(no_emit_item_t, 1);
			if (!nei) {
				FPRINTF(stderr, "Can't allocate memory. Exit.\n");
				exit(10);
			}
			nei->next=no_emit_list;
			no_emit_list=nei;
			nei->name=name;
			FPRINTF(NULL, "NOEMIT : %s\n", nei->name);
		} else if(!strncmp(cnfline, "TYPE", 4)){
			char *name, *dissectorname, *ft_type, *base_type;
			char *mask, *valsstring, *al;
			char *str;
			int alignment;

			str=cnfline+4;
			str=str_read_string(str, &name);
			str=str_read_string(str, &dissectorname);
			str=str_read_string(str, &ft_type);
			str=str_read_string(str, &base_type);
			str=str_read_string(str, &mask);
			str=str_read_string(str, &valsstring);
			str_read_string(str, &al);
			if (ws_strtoi32(al, NULL, &alignment)) {
				FPRINTF(NULL, "TYPE : X%s,%sX\n", name, dissectorname);
				register_new_type(name, dissectorname, ft_type, base_type, mask, valsstring, alignment);
			}
		} else if(!strncmp(cnfline, "PARAM_VALUE", 11)){
			char *dissectorname, *value;
			char *str;

			str=cnfline+11;
			str=str_read_string(str, &dissectorname);
			str_read_string(str, &value);

			FPRINTF(NULL, "PARAM_VALUE : %s=%s\n", dissectorname,value);
			register_dissector_param_value(dissectorname, value);
		} else if(!strncmp(cnfline, "HF_FIELD", 8)){
			char *hf_index, *title, *filter_name, *ft_type;
			char *base_type, *valsstring, *mask, *blurb;
			char *str;

			str=cnfline+8;
			str=str_read_string(str, &hf_index);
			str=str_read_string(str, &title);
			str=str_read_string(str, &filter_name);
			str=str_read_string(str, &ft_type);
			str=str_read_string(str, &base_type);
			str=str_read_string(str, &valsstring);
			str=str_read_string(str, &mask);
			str_read_string(str, &blurb);
			FPRINTF(NULL, "HF_FIELD: %s \"%s\"\n", hf_index, title);
			register_hf_field(hf_index, title, filter_name, ft_type, base_type, valsstring, mask, blurb);
		} else if(!strncmp(cnfline, "HF_RENAME", 9)){
			char *old_name, *new_name;
			char *str;

			str=cnfline+9;
			str=str_read_string(str, &old_name);
			str_read_string(str, &new_name);
			FPRINTF(NULL, "HF_RENAME: %s -> %s\n", old_name, new_name);
			register_hf_rename(old_name, new_name);
		} else if(!strncmp(cnfline, "UNION_TAG_SIZE", 14)){
			char *union_name, *union_tag;
			gint32 union_tag_size;
			union_tag_size_item_t *utsi;
			char *str;

			str=cnfline+14;
			str=str_read_string(str, &union_name);
			str_read_string(str, &union_tag);
			if (!ws_strtoi32(union_tag, NULL, &union_tag_size)) {
				FPRINTF(NULL, "UNION_TAG_SIZE: invalid string: %s\n", union_tag);
				exit(10);
			}
			FPRINTF(NULL, "UNION_TAG_SIZE: %s == %d\n", union_name, union_tag_size);
			utsi=g_new0(union_tag_size_item_t, 1);
			if (!utsi) {
				FPRINTF(stderr, "Can't allocate memory. Exit.\n");
				exit(10);
			}
			utsi->next=union_tag_size_list;
			union_tag_size_list=utsi;
			utsi->name=g_strdup(union_name);
			utsi->size=union_tag_size;
		} else if(!strncmp(cnfline, "STRIP_PREFIX", 12)){
			char *prefix_name;
			char *str;

			str=cnfline+12;
			str_read_string(str, &prefix_name);
			FPRINTF(NULL, "STRIP_PREFIX: %s\n", prefix_name);
			preparetrimprefix(prefix_name);
		} else {
			FPRINTF(stderr, "ERROR: could not parse cnf directive:%s\n",cnfline);
			exit(10);
		}
	}
}

int main(int argc, char *argv[])
{
	char idlfile[BASE_BUFFER_SIZE];
	char tmplfile[BASE_BUFFER_SIZE];
	char prefix_str[BASE_BUFFER_SIZE];
	bracket_item_t *bi;
	FILE *fh;

	if(argc!=2){
		usage();
		exit(0);
	}

	eth_code=g_fopen("ETH_CODE", "w");
	eth_hdr=g_fopen("ETH_HDR", "w");
	eth_hfarr=g_fopen("ETH_HFARR", "w");
	eth_hf=g_fopen("ETH_HF", "w");
	eth_ettarr=g_fopen("ETH_ETTARR", "w");
	eth_ett=g_fopen("ETH_ETT", "w");
	eth_ft=g_fopen("ETH_FT", "w");
	eth_handoff=g_fopen("ETH_HANDOFF", "w");

	snprintf(idlfile, BASE_BUFFER_SIZE, "%s.cnf", argv[1]);
	fh=g_fopen(idlfile,"r");
	if(fh){
		readcnffile(fh);
		fclose(fh);
	}

	snprintf(idlfile, BASE_BUFFER_SIZE, "%s.idl", argv[1]);
	fh=g_fopen(idlfile,"r");
	if(!fh){
		FPRINTF(stderr, "ERROR: could not open idl-file:%s\n", idlfile);
		Exit(0);
	}

	lineno=0;
	linepos=0;
	tokenize(fh);
	prune_keyword_parameters("size_is");
	prune_keyword_parameters("length_is");
	rename_tokens("NTSTATUS", "WERROR");
	rename_tokens("unistr_noterm", "unistr");
	rename_tokens("ascstr_noterm", "ascstr");
	rename_tokens("hyper", "uint64");
	FPRINTF(NULL,"\n\nParsing header:\n================\n");
	parseheader();

	/* some idl files prepend a lot of symbols with <ifname>_
	   search through the tokenlist and g_remove all such
	   prefixes
	*/
	snprintf(prefix_str, BASE_BUFFER_SIZE, "%s_", ifname);
	preparetrimprefix(prefix_str);
	trimprefix();

	/* this is the main loop, each iteration it tries to identify what
	   kind of construct is the first entry in the token_list and call
	   the appropriate handler
	*/
	while(1) {
		/* just skip any [ ] that starts a new construct */
		if( !g_strcmp0(token_list->str, "[") ){
			token_list=parsebrackets(token_list, &bi);
			continue;
		}

		/* typedef enum { */
		if( !g_strcmp0(token_list->str,"typedef")
		  &&!g_strcmp0(token_list->next->str,"enum") ){
			parsetypedefenum();
			continue;
		}

		/* typedef [ v1_enum ] enum { */
		if( !g_strcmp0(token_list->str,"typedef")
		  &&!g_strcmp0(token_list->next->str,"[")
		  &&!g_strcmp0(token_list->next->next->str,"v1_enum")
		  &&!g_strcmp0(token_list->next->next->next->str,"]")
		  &&!g_strcmp0(token_list->next->next->next->next->str,"enum") ){
			parsetypedefenum();
			continue;
		}

		/* const */
		if( !g_strcmp0(token_list->str,"const") ){
			parseconst();
			continue;
		}

		/* typedef struct { */
		if( !g_strcmp0(token_list->str,"typedef") ){
			token_item_t *tmpti;

			tmpti=token_list->next;
			if( !g_strcmp0(tmpti->str, "[") ){
				tmpti=parsebrackets(tmpti, &bi);
				/* do some sanity checks here of bi->flags */
			}
			if( !g_strcmp0(tmpti->str, "struct") ){
				parsetypedefstruct(0);
				parsetypedefstruct(1);
				continue;
			}
		}

		/* typedef union { */
		if( !g_strcmp0(token_list->str,"typedef") ){
			token_item_t *tmpti;

			tmpti=token_list->next;
			if( !g_strcmp0(tmpti->str, "[") ){
				tmpti=parsebrackets(tmpti, &bi);
				/* do some sanity checks here of bi->flags */
			}
			if( !g_strcmp0(tmpti->str, "union") ){
				parsetypedefunion(0);
				parsetypedefunion(1);
				continue;
			}
		}

		/* typedef bitmap { */
		if( !g_strcmp0(token_list->str,"typedef") ){
			token_item_t *tmpti;

			tmpti=token_list->next;
			if( !g_strcmp0(tmpti->str, "[") ){
				tmpti=parsebrackets(tmpti, &bi);
				/* do some sanity checks here of bi->flags */
			}
			if( !g_strcmp0(tmpti->str, "bitmap") ){
				parsetypedefbitmap(0);
				parsetypedefbitmap(1);
				continue;
			}
		}

		/* functions:  WERROR function '(' */
		if( !g_strcmp0(token_list->str,"WERROR")
		  &&!g_strcmp0(token_list->next->next->str,"(") ){
			parsefunction(0);
			parsefunction(1);
			parsefunction(2);
			continue;
		}

		/* declare ... ; */
		if( !g_strcmp0(token_list->str,"declare") ){
			skipdeclare();
			continue;
		}


		break;
	};


	fclose(eth_code);
	fclose(eth_hdr);
	fclose(eth_hf);
	fclose(eth_hfarr);
	fclose(eth_ett);
	fclose(eth_ettarr);
	fclose(eth_ft);
	fclose(eth_handoff);

	/* unless the token_list now only contains a single token : '}'
	   we have failed to compile the idl file properly
		*/
	if( g_strcmp0(token_list->str, "}") || token_list->next){
		FPRINTF(stderr, "ERROR: we did not process all tokens. Compiler is incomplete.\n===========================================\n");
		printtokenlist(10);
		exit(10);
	}

	check_hf_rename_refcount();

	/* merge code and template into dissector */
	snprintf(line, 4 * BASE_BUFFER_SIZE, "packet-dcerpc-%s.c", ifname);
	fh=g_fopen(line, "w");
	snprintf(tmplfile, BASE_BUFFER_SIZE, "packet-dcerpc-%s-template.c", argv[1]);
	tfh=g_fopen(tmplfile, "r");
	if(!tfh){
		FPRINTF(stderr, "ERROR: could not find %s\n", tmplfile);
		exit(10);
	}
	while(!feof(tfh)){
		line[0]=0;
		if(!fgets(line, 4 * BASE_BUFFER_SIZE, tfh) || !line[0]){
			continue;
		}
		if(!strncmp(line, "ETH_CODE", 8)){
			mergefile("ETH_CODE",fh);
		} else if(!strncmp(line, "ETH_HDR", 7)){
			mergefile("ETH_HDR",fh);
		} else if(!strncmp(line, "ETH_HFARR", 9)){
			mergefile("ETH_HFARR",fh);
		} else if(!strncmp(line, "ETH_HF", 6)){
			mergefile("ETH_HF",fh);
		} else if(!strncmp(line, "ETH_ETTARR", 10)){
			mergefile("ETH_ETTARR",fh);
		} else if(!strncmp(line, "ETH_ETT", 7)){
			mergefile("ETH_ETT",fh);
		} else if(!strncmp(line, "ETH_FT", 6)){
			mergefile("ETH_FT",fh);
		} else if(!strncmp(line, "ETH_HANDOFF", 11)){
			mergefile("ETH_HANDOFF",fh);
		} else {
			fputs(line, fh);
		}
	}
	fclose(fh);
	fclose(tfh);

	snprintf(line, 4 * BASE_BUFFER_SIZE, "packet-dcerpc-%s.h", ifname);
	fh=g_fopen(line, "w");
	snprintf(tmplfile, BASE_BUFFER_SIZE, "packet-dcerpc-%s-template.h", argv[1]);
	tfh=g_fopen(tmplfile, "r");
	if(!tfh){
		FPRINTF(stderr, "ERROR: could not find %s\n", tmplfile);
		exit(10);
	}
	while(!feof(tfh)){
		line[0]=0;
		if(!fgets(line, 4 * BASE_BUFFER_SIZE, tfh) || !line[0]){
			continue;
		}
		if(!strncmp(line, "ETH_CODE", 8)){
			mergefile("ETH_CODE",fh);
		} else if(!strncmp(line, "ETH_HDR", 7)){
			mergefile("ETH_HDR",fh);
		} else if(!strncmp(line, "ETH_HFARR", 9)){
			mergefile("ETH_HFARR",fh);
		} else if(!strncmp(line, "ETH_HF", 6)){
			mergefile("ETH_HF",fh);
		} else if(!strncmp(line, "ETH_ETTARR", 10)){
			mergefile("ETH_ETTARR",fh);
		} else if(!strncmp(line, "ETH_ETT", 7)){
			mergefile("ETH_ETT",fh);
		} else if(!strncmp(line, "ETH_FT", 6)){
			mergefile("ETH_FT",fh);
		} else if(!strncmp(line, "ETH_HANDOFF", 11)){
			mergefile("ETH_HANDOFF",fh);
		} else {
			fputs(line, fh);
		}
	}

	g_print("%s was successfully compiled\n", ifname);

	fclose(fh);
	fclose(tfh);

	if (g_remove("ETH_CODE") == -1) {
		FPRINTF(stderr, "Can't remove ETH_CODE");
	}
	if (g_remove("ETH_HDR") == -1) {
		FPRINTF(stderr, "Can't remove ETH_CODE");
	}
	if (g_remove("ETH_HFARR") == -1) {
		FPRINTF(stderr, "Can't remove ETH_CODE");
	}
	if (g_remove("ETH_HF") == -1) {
		FPRINTF(stderr, "Can't remove ETH_CODE");
	}
	if (g_remove("ETH_ETTARR") == -1) {
		FPRINTF(stderr, "Can't remove ETH_CODE");
	}
	if (g_remove("ETH_ETT") == -1) {
		FPRINTF(stderr, "Can't remove ETH_CODE");
	}
	if (g_remove("ETH_FT") == -1) {
		FPRINTF(stderr, "Can't remove ETH_CODE");
	}
	if (g_remove("ETH_HANDOFF") == -1) {
		FPRINTF(stderr, "Can't remove ETH_CODE");
	}


	return 0;
}

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