summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-x11.c
blob: eca43a1d6a16f243e1fcddf7ddd15172ecd2444e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
/* packet-x11.c
 * Routines for X11 dissection
 * Copyright 2000, Christophe Tronche <ch.tronche@computer.org>
 * Copyright 2003, Michael Shuldman
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/* TODO (in no particular order):
 *
 * - keep track of Atom creation by server to be able to display
 *   non-predefined atoms
 * - Idem for keysym <-> keycode ???
 * - Idem for fonts
 * - Subtree the request ids (that is x11.create-window.window and
 *   x11.change-window.window should be  distinct), and add hidden fields
 *   (so we still have x11.window).
 * - add hidden fields so we can have x11.circulate-window in addition to
 *   x11.opcode == 13 (but you could match on x11.opcode == "CirculateWindow"
 *   now)
 * - add hidden fields so we have x11.listOfStuff.length
 * - use a faster scheme that linear list searching for the opcode.
 * - correct display of Unicode chars.
 * - Not everything is homogeneous, in particular the handling of items in
 *   list is a total mess.
 */

/* By the way, I wrote a program to generate every request and test
 * that stuff. If you're interested, you can get it at
 * http://tronche.com/gui/x/
 */

#include "config.h"


#include <epan/packet.h>
#include <epan/exceptions.h>
#include <epan/conversation.h>
#include <epan/expert.h>
#include <epan/show_exception.h>
#include <epan/prefs.h>

#include "packet-x11-keysymdef.h"
#include "packet-x11.h"

#include <wsutil/array.h>
#include <wsutil/bits_count_ones.h>

void proto_register_x11(void);
void proto_reg_handoff_x11(void);

static dissector_handle_t x11_handle;

#define cVALS(x) (const value_string*)(x)


/*
 * Data structure associated with a conversation; keeps track of the
 * request for which we're expecting a reply, the frame number of
 * the initial connection request, and the byte order of the connection.
 *
 * An opcode of -3 means we haven't yet seen any requests yet.
 * An opcode of -2 means we're not expecting a reply (unused).
 * An opcode of -1 means we're waiting for a reply to the initial
 * connection request.
 * An opcode of 0  means the request was not seen (or unknown).
 * Other values are the opcode of the request for which we're expecting
 * a reply.
 *
 */
#define NOTHING_SEEN            -3
#define NOTHING_EXPECTED        -2
#define INITIAL_CONN            -1
#define UNKNOWN_OPCODE           0

#define MAX_OPCODES             (255 + 1) /* 255 + INITIAL_CONN */
#define LastExtensionError      255
#define LastExtensionEvent      127

#define BYTE_ORDER_UNKNOWN      0xFFFFFFFF

static const char *modifiers[] = {
      "Shift",
      "Lock",
      "Control",
      "Mod1",
      "Mod2",
      "Mod3",
      "Mod4",
      "Mod5"
};

/* Keymasks.  From <X11/X.h>. */
#define ShiftMask               (1<<0)
#define LockMask                (1<<1)
#define ControlMask             (1<<2)
#define Mod1Mask                (1<<3)
#define Mod2Mask                (1<<4)
#define Mod3Mask                (1<<5)
#define Mod4Mask                (1<<6)
#define Mod5Mask                (1<<7)

static const int modifiermask[] = { ShiftMask, LockMask, ControlMask,
Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask };

/* from <X11/X.h> */
#define NoSymbol             0L /* special KeySym */

typedef struct _x11_conv_data {
      wmem_map_t *seqtable;            /* hashtable of sequencenumber <-> opcode. */
      wmem_map_t *valtable;            /* hashtable of sequencenumber <-> &opcode_vals */
      /* major opcodes including extensions (NULL terminated) */
      value_string opcode_vals[MAX_OPCODES+1];
      /* error codes including extensions (NULL terminated) */
      value_string errorcode_vals[LastExtensionError + 2];
      /* event codes including extensions (NULL terminated) */
      value_string eventcode_vals[LastExtensionEvent + 2];
      wmem_map_t *eventcode_funcs;      /* hashtable of eventcode <-> dissect_event() */
      wmem_map_t *reply_funcs;          /* hashtable of opcode <-> dissect_reply() */

      int       sequencenumber;   /* sequencenumber of current packet.       */
      uint32_t  iconn_frame;      /* frame # of initial connection request   */
      uint32_t  iconn_reply;      /* frame # of initial connection reply     */
      unsigned     byte_order;       /* byte order of connection */
      bool      resync;           /* resynchronization of sequence number performed */

      int       *keycodemap[256]; /* keycode to keysymvalue map. */
      int       keysyms_per_keycode;
      int       first_keycode;
      int       *modifiermap[array_length(modifiers)];/* modifier to keycode.*/
      int       keycodes_per_modifier;

      union {
            struct {
                  int   first_keycode;
            } GetKeyboardMapping;
      } request;
} x11_conv_data_t;

static wmem_map_t *extension_table; /* hashtable of extension name <-> dispatch function */
static wmem_map_t *event_table;     /* hashtable of extension name <-> event info list */
static wmem_map_t *genevent_table;     /* hashtable of extension name <-> generic event info list */
static wmem_map_t *error_table;     /* hashtable of extension name <-> error list */
static wmem_map_t *reply_table;     /* hashtable of extension name <-> reply list */

/* Initialize the protocol and registered fields */
static int proto_x11;

#include "x11-declarations.h"

/* Initialize the subtree pointers */
static int ett_x11;
static int ett_x11_color_flags;
static int ett_x11_list_of_arc;
static int ett_x11_arc;
static int ett_x11_list_of_atom;
static int ett_x11_list_of_card32;
static int ett_x11_list_of_float;
static int ett_x11_list_of_double;
static int ett_x11_list_of_color_item;
static int ett_x11_color_item;
static int ett_x11_list_of_keycode;
static int ett_x11_list_of_keysyms;
static int ett_x11_keysym;
static int ett_x11_list_of_point;
static int ett_x11_point;
static int ett_x11_list_of_rectangle;
static int ett_x11_rectangle;
static int ett_x11_list_of_segment;
static int ett_x11_segment;
static int ett_x11_list_of_string8;
static int ett_x11_list_of_text_item;
static int ett_x11_text_item;
static int ett_x11_gc_value_mask;         /* XXX - unused */
static int ett_x11_event_mask;            /* XXX - unused */
static int ett_x11_do_not_propagate_mask; /* XXX - unused */
static int ett_x11_set_of_key_mask;
static int ett_x11_pointer_event_mask;    /* XXX - unused */
static int ett_x11_window_value_mask;     /* XXX - unused */
static int ett_x11_configure_window_mask; /* XXX - unused */
static int ett_x11_keyboard_value_mask;   /* XXX - unused */
static int ett_x11_same_screen_focus;
static int ett_x11_event;
static int ett_x11_list_of_pixmap_format;
static int ett_x11_pixmap_format;
static int ett_x11_list_of_screen;
static int ett_x11_screen;
static int ett_x11_list_of_depth_detail;
static int ett_x11_depth_detail;
static int ett_x11_list_of_visualtype;
static int ett_x11_visualtype;

static expert_field ei_x11_invalid_format;
static expert_field ei_x11_request_length;
static expert_field ei_x11_keycode_value_out_of_range;

/* desegmentation of X11 messages */
static bool x11_desegment = true;

#define DEFAULT_X11_PORT_RANGE "6000-6063"

/*
 * Round a length to a multiple of 4 bytes.
 */
#define ROUND_LENGTH(n) ((((n) + 3)/4) * 4)

/************************************************************************
 ***                                                                  ***
 ***         E N U M   T A B L E S   D E F I N I T I O N S            ***
 ***                                                                  ***
 ************************************************************************/

static const value_string byte_order_vals[] = {
      { 'B', "Big-endian" },
      { 'l', "Little-endian" },
      { 0,   NULL }
};

static const value_string image_byte_order_vals[] = {
      { 0, "LSBFirst" },
      { 1, "MSBFirst" },
      { 0,   NULL }
};

static const value_string access_mode_vals[] = {
      { 0, "Disable" },
      { 1, "Enable" },
      { 0, NULL }
};

static const value_string all_temporary_vals[] = {
      { 0, "AllTemporary" },
      { 0, NULL }
};

static const value_string alloc_vals[] = {
      { 0, "None" },
      { 1, "All" },
      { 0, NULL }
};

static const value_string allow_events_mode_vals[] = {
      { 0, "AsyncPointer" },
      { 1, "SyncPointer" },
      { 2, "ReplayPointer" },
      { 3, "AsyncKeyboard" },
      { 4, "SyncKeyboard" },
      { 5, "ReplayKeyboard" },
      { 6, "AsyncBoth" },
      { 7, "SyncBoth" },
      { 0, NULL }
};

static const value_string arc_mode_vals[] = {
      { 0, "Chord" },
      { 1, "PieSlice" },
      { 0, NULL }
};

static const char *atom_predefined_interpretation[] = {
      "<error>",
      "PRIMARY",
      "SECONDARY",
      "ARC",
      "ATOM",
      "BITMAP",
      "CARDINAL",
      "COLORMAP",
      "CURSOR",
      "CUT_BUFFER0",
      "CUT_BUFFER1",
      "CUT_BUFFER2",
      "CUT_BUFFER3",
      "CUT_BUFFER4",
      "CUT_BUFFER5",
      "CUT_BUFFER6",
      "CUT_BUFFER7",
      "DRAWABLE",
      "FONT",
      "INTEGER",
      "PIXMAP",
      "POINT",
      "RECTANGLE",
      "RESOURCE_MANAGER",
      "RGB_COLOR_MAP",
      "RGB_BEST_MAP",
      "RGB_BLUE_MAP",
      "RGB_DEFAULT_MAP",
      "RGB_GRAY_MAP",
      "RGB_GREEN_MAP",
      "RGB_RED_MAP",
      "STRING",
      "VISUALID",
      "WINDOW",
      "WM_COMMAND",
      "WM_HINTS",
      "WM_CLIENT_MACHINE",
      "WM_ICON_NAME",
      "WM_ICON_SIZE",
      "WM_NAME",
      "WM_NORMAL_HINTS",
      "WM_SIZE_HINTS",
      "WM_ZOOM_HINTS",
      "MIN_SPACE",
      "NORM_SPACE",
      "MAX_SPACE",
      "END_SPACE",
      "SUPERSCRIPT_X",
      "SUPERSCRIPT_Y",
      "SUBSCRIPT_X",
      "SUBSCRIPT_Y",
      "UNDERLINE_POSITION",
      "UNDERLINE_THICKNESS",
      "STRIKEOUT_ASCENT",
      "STRIKEOUT_DESCENT",
      "ITALIC_ANGLE",
      "X_HEIGHT",
      "QUAD_WIDTH",
      "WEIGHT",
      "POINT_SIZE",
      "RESOLUTION",
      "COPYRIGHT",
      "NOTICE",
      "FONT_NAME",
      "FAMILY_NAME",
      "FULL_NAME",
      "CAP_HEIGHT",
      "WM_CLASS",
      "WM_TRANSIENT_FOR",
};

static const value_string auto_repeat_mode_vals[] = {
      { 0, "Off" },
      { 1, "On" },
      { 2, "Default" },
      { 0, NULL }
};

static const value_string background_pixmap_vals[] = {
      { 0, "None" },
      { 1, "ParentRelative" },
      { 0, NULL }
};

static const value_string backing_store_vals[] = {
      { 0, "NotUseful" },
      { 1, "WhenMapped" },
      { 2, "Always" },
      { 0, NULL }
};

static const value_string border_pixmap_vals[] = {
      { 0, "CopyFromParent" },
      { 0, NULL }
};

static const value_string button_vals[] = {
      { 0x8000, "AnyButton" },
      { 0, NULL }
};

static const value_string cap_style_vals[] = {
      { 0, "NotLast" },
      { 1, "Butt" },
      { 2, "Round" },
      { 3, "Projecting" },
      { 0, NULL }
};

static const value_string class_vals[] = {
      { 0, "Cursor" },
      { 1, "Tile" },
      { 2, "Stipple" },
      { 0, NULL }
};

static const value_string close_down_mode_vals[] = {
      { 0, "Destroy" },
      { 1, "RetainPermanent" },
      { 2, "RetainTemporary" },
      { 0, NULL }
};

static const value_string colormap_state_vals[] = {
      { 0, "Uninstalled" },
      { 1, "Installed" },
      { 0, NULL }
};

static const value_string coordinate_mode_vals[] = {
      { 0, "Origin" },
      { 1, "Previous" },
      { 0, NULL }
};

static const value_string destination_vals[] = {
      { 0, "PointerWindow" },
      { 1, "InputFocus" },
      { 0, NULL }
};

static const value_string direction_vals[] = {
      { 0, "RaiseLowest" },
      { 1, "LowerHighest" },
      { 0, NULL }
};

static const value_string event_detail_vals[] = {
      { 0, "Ancestor" },
      { 1, "Virtual" },
      { 2, "Inferior" },
      { 3, "Nonlinear" },
      { 4, "NonlinearVirtual" },
      { 0, NULL }
};

#define FAMILY_INTERNET 0
#define FAMILY_DECNET   1
#define FAMILY_CHAOS    2

static const value_string family_vals[] = {
      { FAMILY_INTERNET, "Internet" },
      { FAMILY_DECNET,   "DECnet" },
      { FAMILY_CHAOS,    "Chaos" },
      { 0, NULL }
};

static const value_string fill_rule_vals[] = {
      { 0, "EvenOdd" },
      { 1, "Winding" },
      { 0, NULL }
};

static const value_string fill_style_vals[] = {
      { 0, "Solid" },
      { 1, "Tiled" },
      { 2, "Stippled" },
      { 3, "OpaqueStippled" },
      { 0, NULL }
};

static const value_string focus_detail_vals[] = {
      { 0, "Ancestor" },
      { 1, "Virtual" },
      { 2, "Inferior" },
      { 3, "Nonlinear" },
      { 4, "NonlinearVirtual" },
      { 5, "Pointer" },
      { 6, "PointerRoot" },
      { 7, "None" },
      { 0, NULL }
};

static const value_string focus_mode_vals[] = {
      {  0, "Normal" },
      {  1, "Grab" },
      {  2, "Ungrab" },
      {  3, "WhileGrabbed" },
      {  0, NULL }
};

static const value_string focus_vals[] = {
      { 0, "None" },
      { 1, "PointerRoot" },
      { 0, NULL }
};

static const value_string function_vals[] = {
      {  0, "Clear" },
      {  1, "And" },
      {  2, "AndReverse" },
      {  3, "Copy" },
      {  4, "AndInverted" },
      {  5, "NoOp" },
      {  6, "Xor" },
      {  7, "Or" },
      {  8, "Nor" },
      {  9, "Equiv" },
      { 10, "Invert" },
      { 11, "OrReverse" },
      { 12, "CopyInverted" },
      { 13, "OrInverted" },
      { 14, "Nand" },
      { 15, "Set" },
      {  0, NULL }
};

static const value_string grab_mode_vals[] = {
      {  0, "Normal" },
      {  1, "Grab" },
      {  2, "Ungrab" },
      {  0, NULL }
};

static const value_string grab_status_vals[] = {
      {  0, "Success" },
      {  1, "AlreadyGrabbed" },
      {  2, "InvalidTime" },
      {  3, "NotViewable" },
      {  4, "Frozen" },
      {  0, NULL }
};

static const value_string bit_gravity_vals[] = {
      {  0, "Forget" },
      {  1, "NorthWest" },
      {  2, "North" },
      {  3, "NorthEast" },
      {  4, "West" },
      {  5, "Center" },
      {  6, "East" },
      {  7, "SouthWest" },
      {  8, "South" },
      {  9, "SouthEast" },
      { 10, "Static" },
      {  0, NULL }
};

static const value_string win_gravity_vals[] = {
      {  0, "Unmap" },
      {  1, "NorthWest" },
      {  2, "North" },
      {  3, "NorthEast" },
      {  4, "West" },
      {  5, "Center" },
      {  6, "East" },
      {  7, "SouthWest" },
      {  8, "South" },
      {  9, "SouthEast" },
      { 10, "Static" },
      {  0, NULL }
};

static const value_string image_format_vals[] = {
      { 0, "Bitmap" },
      { 1, "XYPixmap" },
      { 2, "ZPixmap" },
      { 0, NULL }
};

static const value_string image_pixmap_format_vals[] = {
      { 1, "XYPixmap" },
      { 2, "ZPixmap" },
      { 0, NULL }
};

static const value_string join_style_vals[] = {
      { 0, "Miter" },
      { 1, "Round" },
      { 2, "Bevel" },
      { 0, NULL }
};

static const value_string key_vals[] = {
      { 0, "AnyKey" },
      { 0, NULL }
};

#include "x11-keysym.h"

static const value_string line_style_vals[] = {
      { 0, "Solid" },
      { 1, "OnOffDash" },
      { 2, "DoubleDash" },
      { 0, NULL }
};

static const value_string mode_vals[] = {
      { 0, "Replace" },
      { 1, "Prepend" },
      { 2, "Append" },
      { 0, NULL }
};

static const value_string on_off_vals[] = {
      { 0, "Off" },
      { 1, "On" },
      { 0, NULL }
};

static const value_string place_vals[] = {
      { 0, "Top" },
      { 1, "Bottom" },
      { 0, NULL }
};

static const value_string property_state_vals[] = {
      { 0, "NewValue" },
      { 1, "Deleted" },
      { 0, NULL }
};

static const value_string visibility_state_vals[] = {
      { 0, "Unobscured" },
      { 1, "PartiallyObscured" },
      { 2, "FullyObscured" },
      { 0, NULL }
};

static const value_string mapping_request_vals[] = {
      { 0, "MappingModifier" },
      { 1, "MappingKeyboard" },
      { 2, "MappingPointer" },
      { 0, NULL }
};

/* Requestcodes.  From <X11/Xproto.h>. */
#define X_CreateWindow                  1
#define X_ChangeWindowAttributes        2
#define X_GetWindowAttributes           3
#define X_DestroyWindow                 4
#define X_DestroySubwindows             5
#define X_ChangeSaveSet                 6
#define X_ReparentWindow                7
#define X_MapWindow                     8
#define X_MapSubwindows                 9
#define X_UnmapWindow                  10
#define X_UnmapSubwindows              11
#define X_ConfigureWindow              12
#define X_CirculateWindow              13
#define X_GetGeometry                  14
#define X_QueryTree                    15
#define X_InternAtom                   16
#define X_GetAtomName                  17
#define X_ChangeProperty               18
#define X_DeleteProperty               19
#define X_GetProperty                  20
#define X_ListProperties               21
#define X_SetSelectionOwner            22
#define X_GetSelectionOwner            23
#define X_ConvertSelection             24
#define X_SendEvent                    25
#define X_GrabPointer                  26
#define X_UngrabPointer                27
#define X_GrabButton                   28
#define X_UngrabButton                 29
#define X_ChangeActivePointerGrab      30
#define X_GrabKeyboard                 31
#define X_UngrabKeyboard               32
#define X_GrabKey                      33
#define X_UngrabKey                    34
#define X_AllowEvents                  35
#define X_GrabServer                   36
#define X_UngrabServer                 37
#define X_QueryPointer                 38
#define X_GetMotionEvents              39
#define X_TranslateCoords              40
#define X_WarpPointer                  41
#define X_SetInputFocus                42
#define X_GetInputFocus                43
#define X_QueryKeymap                  44
#define X_OpenFont                     45
#define X_CloseFont                    46
#define X_QueryFont                    47
#define X_QueryTextExtents             48
#define X_ListFonts                    49
#define X_ListFontsWithInfo            50
#define X_SetFontPath                  51
#define X_GetFontPath                  52
#define X_CreatePixmap                 53
#define X_FreePixmap                   54
#define X_CreateGC                     55
#define X_ChangeGC                     56
#define X_CopyGC                       57
#define X_SetDashes                    58
#define X_SetClipRectangles            59
#define X_FreeGC                       60
#define X_ClearArea                    61
#define X_CopyArea                     62
#define X_CopyPlane                    63
#define X_PolyPoint                    64
#define X_PolyLine                     65
#define X_PolySegment                  66
#define X_PolyRectangle                67
#define X_PolyArc                      68
#define X_FillPoly                     69
#define X_PolyFillRectangle            70
#define X_PolyFillArc                  71
#define X_PutImage                     72
#define X_GetImage                     73
#define X_PolyText8                    74
#define X_PolyText16                   75
#define X_ImageText8                   76
#define X_ImageText16                  77
#define X_CreateColormap               78
#define X_FreeColormap                 79
#define X_CopyColormapAndFree          80
#define X_InstallColormap              81
#define X_UninstallColormap            82
#define X_ListInstalledColormaps       83
#define X_AllocColor                   84
#define X_AllocNamedColor              85
#define X_AllocColorCells              86
#define X_AllocColorPlanes             87
#define X_FreeColors                   88
#define X_StoreColors                  89
#define X_StoreNamedColor              90
#define X_QueryColors                  91
#define X_LookupColor                  92
#define X_CreateCursor                 93
#define X_CreateGlyphCursor            94
#define X_FreeCursor                   95
#define X_RecolorCursor                96
#define X_QueryBestSize                97
#define X_QueryExtension               98
#define X_ListExtensions               99
#define X_ChangeKeyboardMapping        100
#define X_GetKeyboardMapping           101
#define X_ChangeKeyboardControl        102
#define X_GetKeyboardControl           103
#define X_Bell                         104
#define X_ChangePointerControl         105
#define X_GetPointerControl            106
#define X_SetScreenSaver               107
#define X_GetScreenSaver               108
#define X_ChangeHosts                  109
#define X_ListHosts                    110
#define X_SetAccessControl             111
#define X_SetCloseDownMode             112
#define X_KillClient                   113
#define X_RotateProperties             114
#define X_ForceScreenSaver             115
#define X_SetPointerMapping            116
#define X_GetPointerMapping            117
#define X_SetModifierMapping           118
#define X_GetModifierMapping           119
#define X_NoOperation                  127
#define X_FirstExtension               128
#define X_LastExtension                255

static const value_string opcode_vals[] = {
      { INITIAL_CONN,                   "Initial connection request" },
      { X_CreateWindow,                 "CreateWindow" },
      { X_ChangeWindowAttributes,       "ChangeWindowAttributes" },
      { X_GetWindowAttributes,          "GetWindowAttributes" },
      { X_DestroyWindow,                "DestroyWindow" },
      { X_DestroySubwindows,            "DestroySubwindows" },
      { X_ChangeSaveSet,                "ChangeSaveSet" },
      { X_ReparentWindow,               "ReparentWindow" },
      { X_MapWindow,                    "MapWindow" },
      { X_MapSubwindows,                "MapSubwindows" },
      { X_UnmapWindow,                  "UnmapWindow" },
      { X_UnmapSubwindows,              "UnmapSubwindows" },
      { X_ConfigureWindow,              "ConfigureWindow" },
      { X_CirculateWindow,              "CirculateWindow" },
      { X_GetGeometry,                  "GetGeometry" },
      { X_QueryTree,                    "QueryTree" },
      { X_InternAtom,                   "InternAtom" },
      { X_GetAtomName,                  "GetAtomName" },
      { X_ChangeProperty,               "ChangeProperty" },
      { X_DeleteProperty,               "DeleteProperty" },
      { X_GetProperty,                  "GetProperty" },
      { X_ListProperties,               "ListProperties" },
      { X_SetSelectionOwner,            "SetSelectionOwner" },
      { X_GetSelectionOwner,            "GetSelectionOwner" },
      { X_ConvertSelection,             "ConvertSelection" },
      { X_SendEvent,                    "SendEvent" },
      { X_GrabPointer,                  "GrabPointer" },
      { X_UngrabPointer,                "UngrabPointer" },
      { X_GrabButton,                   "GrabButton" },
      { X_UngrabButton,                 "UngrabButton" },
      { X_ChangeActivePointerGrab,      "ChangeActivePointerGrab" },
      { X_GrabKeyboard,                 "GrabKeyboard" },
      { X_UngrabKeyboard,               "UngrabKeyboard" },
      { X_GrabKey,                      "GrabKey" },
      { X_UngrabKey,                    "UngrabKey" },
      { X_AllowEvents,                  "AllowEvents" },
      { X_GrabServer,                   "GrabServer" },
      { X_UngrabServer,                 "UngrabServer" },
      { X_QueryPointer,                 "QueryPointer" },
      { X_GetMotionEvents,              "GetMotionEvents" },
      { X_TranslateCoords,              "TranslateCoordinates" },
      { X_WarpPointer,                  "WarpPointer" },
      { X_SetInputFocus,                "SetInputFocus" },
      { X_GetInputFocus,                "GetInputFocus" },
      { X_QueryKeymap,                  "QueryKeymap" },
      { X_OpenFont,                     "OpenFont" },
      { X_CloseFont,                    "CloseFont" },
      { X_QueryFont,                    "QueryFont" },
      { X_QueryTextExtents,             "QueryTextExtents" },
      { X_ListFonts,                    "ListFonts" },
      { X_ListFontsWithInfo,            "ListFontsWithInfo" },
      { X_SetFontPath,                  "SetFontPath" },
      { X_GetFontPath,                  "GetFontPath" },
      { X_CreatePixmap,                 "CreatePixmap" },
      { X_FreePixmap,                   "FreePixmap" },
      { X_CreateGC,                     "CreateGC" },
      { X_ChangeGC,                     "ChangeGC" },
      { X_CopyGC,                       "CopyGC" },
      { X_SetDashes,                    "SetDashes" },
      { X_SetClipRectangles,            "SetClipRectangles" },
      { X_FreeGC,                       "FreeGC" },
      { X_ClearArea,                    "ClearArea" },
      { X_CopyArea,                     "CopyArea" },
      { X_CopyPlane,                    "CopyPlane" },
      { X_PolyPoint,                    "PolyPoint" },
      { X_PolyLine,                     "PolyLine" },
      { X_PolySegment,                  "PolySegment" },
      { X_PolyRectangle,                "PolyRectangle" },
      { X_PolyArc,                      "PolyArc" },
      { X_FillPoly,                     "FillPoly" },
      { X_PolyFillRectangle,            "PolyFillRectangle" },
      { X_PolyFillArc,                  "PolyFillArc" },
      { X_PutImage,                     "PutImage" },
      { X_GetImage,                     "GetImage" },
      { X_PolyText8,                    "PolyText8" },
      { X_PolyText16,                   "PolyText16" },
      { X_ImageText8,                   "ImageText8" },
      { X_ImageText16,                  "ImageText16" },
      { X_CreateColormap,               "CreateColormap" },
      { X_FreeColormap,                 "FreeColormap" },
      { X_CopyColormapAndFree,          "CopyColormapAndFree" },
      { X_InstallColormap,              "InstallColormap" },
      { X_UninstallColormap,            "UninstallColormap" },
      { X_ListInstalledColormaps,       "ListInstalledColormaps" },
      { X_AllocColor,                   "AllocColor" },
      { X_AllocNamedColor,              "AllocNamedColor" },
      { X_AllocColorCells,              "AllocColorCells" },
      { X_AllocColorPlanes,             "AllocColorPlanes" },
      { X_FreeColors,                   "FreeColors" },
      { X_StoreColors,                  "StoreColors" },
      { X_StoreNamedColor,              "StoreNamedColor" },
      { X_QueryColors,                  "QueryColors" },
      { X_LookupColor,                  "LookupColor" },
      { X_CreateCursor,                 "CreateCursor" },
      { X_CreateGlyphCursor,            "CreateGlyphCursor" },
      { X_FreeCursor,                   "FreeCursor" },
      { X_RecolorCursor,                "RecolorCursor" },
      { X_QueryBestSize,                "QueryBestSize" },
      { X_QueryExtension,               "QueryExtension" },
      { X_ListExtensions,               "ListExtensions" },
      { X_ChangeKeyboardMapping,        "ChangeKeyboardMapping" },
      { X_GetKeyboardMapping,           "GetKeyboardMapping" },
      { X_ChangeKeyboardControl,        "ChangeKeyboardControl" },
      { X_GetKeyboardControl,           "GetKeyboardControl" },
      { X_Bell,                         "Bell" },
      { X_ChangePointerControl,         "ChangePointerControl" },
      { X_GetPointerControl,            "GetPointerControl" },
      { X_SetScreenSaver,               "SetScreenSaver" },
      { X_GetScreenSaver,               "GetScreenSaver" },
      { X_ChangeHosts,                  "ChangeHosts" },
      { X_ListHosts,                    "ListHosts" },
      { X_SetAccessControl,             "SetAccessControl" },
      { X_SetCloseDownMode,             "SetCloseDownMode" },
      { X_KillClient,                   "KillClient" },
      { X_RotateProperties,             "RotateProperties" },
      { X_ForceScreenSaver,             "ForceScreenSaver" },
      { X_SetPointerMapping,            "SetPointerMapping" },
      { X_GetPointerMapping,            "GetPointerMapping" },
      { X_SetModifierMapping,           "SetModifierMapping" },
      { X_GetModifierMapping,           "GetModifierMapping" },
      { X_NoOperation,                  "NoOperation" },
      { 0,                              NULL }
};

/* Eventscodes.  From <X11/X.h>. */
#define KeyPress                2
#define KeyRelease              3
#define ButtonPress             4
#define ButtonRelease           5
#define MotionNotify            6
#define EnterNotify             7
#define LeaveNotify             8
#define FocusIn                 9
#define FocusOut                10
#define KeymapNotify            11
#define Expose                  12
#define GraphicsExpose          13
#define NoExpose                14
#define VisibilityNotify        15
#define CreateNotify            16
#define DestroyNotify           17
#define UnmapNotify             18
#define MapNotify               19
#define MapRequest              20
#define ReparentNotify          21
#define ConfigureNotify         22
#define ConfigureRequest        23
#define GravityNotify           24
#define ResizeRequest           25
#define CirculateNotify         26
#define CirculateRequest        27
#define PropertyNotify          28
#define SelectionClear          29
#define SelectionRequest        30
#define SelectionNotify         31
#define ColormapNotify          32
#define ClientMessage           33
#define MappingNotify           34
#define GenericEvent            35

static const value_string eventcode_vals[] = {
      { KeyPress,          "KeyPress" },
      { KeyRelease,        "KeyRelease" },
      { ButtonPress,       "ButtonPress" },
      { ButtonRelease,     "ButtonRelease" },
      { MotionNotify,      "MotionNotify" },
      { EnterNotify,       "EnterNotify" },
      { LeaveNotify,       "LeaveNotify" },
      { FocusIn,           "FocusIn" },
      { FocusOut,          "FocusOut" },
      { KeymapNotify,      "KeymapNotify" },
      { Expose,            "Expose" },
      { GraphicsExpose,    "GraphicsExpose" },
      { NoExpose,          "NoExpose" },
      { VisibilityNotify,  "VisibilityNotify" },
      { CreateNotify,      "CreateNotify" },
      { DestroyNotify,     "DestroyNotify" },
      { UnmapNotify,       "UnmapNotify" },
      { MapNotify,         "MapNotify" },
      { MapRequest,        "MapRequest" },
      { ReparentNotify,    "ReparentNotify" },
      { ConfigureNotify,   "ConfigureNotify" },
      { ConfigureRequest,  "ConfigureRequest" },
      { GravityNotify,     "GravityNotify" },
      { ResizeRequest,     "ResizeRequest" },
      { CirculateNotify,   "CirculateNotify" },
      { CirculateRequest,  "CirculateRequest" },
      { PropertyNotify,    "PropertyNotify" },
      { SelectionClear,    "SelectionClear" },
      { SelectionRequest,  "SelectionRequest" },
      { SelectionNotify,   "SelectionNotify" },
      { ColormapNotify,    "ColormapNotify" },
      { ClientMessage,     "ClientMessage" },
      { MappingNotify,     "MappingNotify" },
      { GenericEvent,      "GenericEvent" },
      { 0,                 NULL }
};

/* Errorcodes.  From <X11/X.h> */
#define Success                 0       /* everything's okay */
#define BadRequest              1       /* bad request code */
#define BadValue                2       /* int parameter out of range */
#define BadWindow               3       /* parameter not a Window */
#define BadPixmap               4       /* parameter not a Pixmap */
#define BadAtom                 5       /* parameter not an Atom */
#define BadCursor               6       /* parameter not a Cursor */
#define BadFont                 7       /* parameter not a Font */
#define BadMatch                8       /* parameter mismatch */
#define BadDrawable             9       /* parameter not a Pixmap or Window */
#define BadAccess               10      /* depending on context:
                                         - key/button already grabbed
                                         - attempt to free an illegal
                                           cmap entry
                                        - attempt to store into a read-only
                                           color map entry.
                                        - attempt to modify the access control
                                           list from other than the local host.
                                        */
#define BadAlloc                11      /* insufficient resources */
#define BadColormap             12      /* no such colormap */
#define BadGC                   13      /* parameter not a GC */
#define BadIDChoice             14      /* choice not in range or already used */
#define BadName                 15      /* font or color name doesn't exist */
#define BadLength               16      /* Request length incorrect */
#define BadImplementation       17      /* server is defective */

static const value_string errorcode_vals[] = {
      { Success,               "Success" },
      { BadRequest,            "BadRequest" },
      { BadValue,              "BadValue" },
      { BadWindow,             "BadWindow" },
      { BadPixmap,             "BadPixmap" },
      { BadAtom,               "BadAtom" },
      { BadCursor,             "BadCursor" },
      { BadFont,               "BadFont" },
      { BadMatch,              "BadMatch" },
      { BadDrawable,           "BadDrawable" },
      { BadAccess,             "BadAccess" },
      { BadAlloc,              "BadAlloc" },
      { BadColormap,           "BadColormap" },
      { BadGC,                 "BadGC" },
      { BadIDChoice,           "BadIDChoice" },
      { BadName,               "BadName" },
      { BadLength,             "BadLength" },
      { BadImplementation,     "BadImplementation" },
      { 0,                     NULL }
};

static const value_string ordering_vals[] = {
      { 0, "UnSorted" },
      { 1, "YSorted" },
      { 2, "YXSorted" },
      { 3, "YXBanded" },
      { 0, NULL }
};

static const value_string plane_mask_vals[] = {
      { 0xFFFFFFFF, "AllPlanes" },
      { 0, NULL }
};

static const value_string pointer_keyboard_mode_vals[] = {
      { 0, "Synchronous" },
      { 1, "Asynchronous" },
      { 0, NULL }
};

static const value_string revert_to_vals[] = {
      { 0, "None" },
      { 1, "PointerRoot" },
      { 2, "Parent" },
      { 0, NULL }
};

static const value_string insert_delete_vals[] = {
      { 0, "Insert" },
      { 1, "Delete" },
      { 0, NULL }
};

static const value_string screen_saver_mode_vals[] = {
      { 0, "Reset" },
      { 1, "Activate" },
      { 0, NULL }
};

static const value_string shape_vals[] = {
      { 0, "Complex" },
      { 1, "Nonconvex" },
      { 2, "Convex" },
      { 0, NULL }
};

static const value_string stack_mode_vals[] = {
      { 0, "Above" },
      { 1, "Below" },
      { 2, "TopIf" },
      { 3, "BottomIf" },
      { 4, "Opposite" },
      { 0, NULL }
};

static const value_string subwindow_mode_vals[] = {
      { 0, "ClipByChildren" },
      { 1, "IncludeInferiors" },
      { 0, NULL }
};

static const value_string window_class_vals[] = {
      { 0, "CopyFromParent" },
      { 1, "InputOutput" },
      { 2, "InputOnly" },
      { 0, NULL }
};

static const value_string yes_no_default_vals[] = {
      { 0, "No" },
      { 1, "Yes" },
      { 2, "Default" },
      { 0, NULL }
};

static const value_string zero_is_any_property_type_vals[] = {
      { 0, "AnyPropertyType" },
      { 0, NULL }
};

static const value_string zero_is_none_vals[] = {
      { 0, "None" },
      { 0, NULL }
};

/************************************************************************
 ***                                                                  ***
 ***           F I E L D   D E C O D I N G   M A C R O S              ***
 ***                                                                  ***
 ************************************************************************/
#define FIELD8(name)  (field8(tvb, offsetp, t, hf_x11_##name, byte_order))
#define FIELD16(name) (field16(tvb, offsetp, t, hf_x11_##name, byte_order))
#define FIELD32(name) (field32(tvb, offsetp, t, hf_x11_##name, byte_order))

#define FLAG(position, name) {\
      proto_tree_add_boolean(bitmask_tree, hf_x11_##position##_mask##_##name, tvb, bitmask_offset, bitmask_size, bitmask_value); }

#define FLAG_IF_NONZERO(position, name) do {\
      if (bitmask_value & proto_registrar_get_nth(hf_x11_##position##_mask##_##name) -> bitmask) \
            proto_tree_add_boolean(bitmask_tree, hf_x11_##position##_mask##_##name, tvb, bitmask_offset, bitmask_size, bitmask_value); } while (0)

#define ATOM(name)     { atom(tvb, offsetp, t, hf_x11_##name, byte_order); }
#define BOOL(name)     (add_boolean(tvb, offsetp, t, hf_x11_##name))
#define BUTTON(name)   FIELD8(name)
#define CARD8(name)    FIELD8(name)
#define CARD16(name)   (FIELD16(name))
#define CARD32(name)   (FIELD32(name))
#define COLOR_FLAGS(name) colorFlags(tvb, offsetp, t)
#define COLORMAP(name) FIELD32(name)
#define CURSOR(name)   FIELD32(name)
#define DRAWABLE(name) FIELD32(name)
#define ENUM8(name)    (FIELD8(name))
#define ENUM16(name)   (FIELD16(name))
#define FONT(name)     FIELD32(name)
#define FONTABLE(name) FIELD32(name)
#define GCONTEXT(name) FIELD32(name)
#define INT8(name)     FIELD8(name)
#define INT16(name)    FIELD16(name)
#define INT32(name)    FIELD32(name)
#define KEYCODE(name)  FIELD8(name)
#define KEYCODE_DECODED(name, keycode, mask)  do {                    \
      proto_tree_add_uint_format(t, hf_x11_##name, tvb, offset, 1,    \
      keycode, "keycode: %d (%s)",                                    \
      keycode,  keycode2keysymString(state->keycodemap,               \
      state->first_keycode, state->keysyms_per_keycode,               \
      state->modifiermap, state->keycodes_per_modifier,               \
      keycode, mask));                                                \
      ++offset;                                                       \
} while (0)
#define EVENT() do { \
      tvbuff_t *next_tvb;                                             \
      unsigned char eventcode;                                        \
      const char *sent;                                               \
      proto_tree *event_proto_tree;                                   \
      next_tvb = tvb_new_subset_length_caplen(tvb, offset, next_offset - offset,    \
                                next_offset - offset);                \
      eventcode = tvb_get_uint8(next_tvb, 0);                        \
      sent = (eventcode & 0x80) ? "Sent-" : "";                       \
      event_proto_tree = proto_tree_add_subtree_format(t, next_tvb,   \
                               0, -1, ett_x11_event, NULL,            \
                               "event: %d (%s)",                      \
                               eventcode,                             \
                               val_to_str(eventcode & 0x7F,           \
                                          state->eventcode_vals,      \
                                          "<Unknown eventcode %u>")); \
      decode_x11_event(next_tvb, eventcode, sent, event_proto_tree,   \
                       state, byte_order);                            \
      offset = next_offset;                                           \
} while (0)

#define LISTofARC(name) { listOfArc(tvb, offsetp, t, hf_x11_##name, (next_offset - *offsetp) / 12, byte_order); }
#define LISTofATOM(name, length) { listOfAtom(tvb, offsetp, t, hf_x11_##name, (length) / 4, byte_order); }
#define LISTofBYTE(name, length) { listOfByte(tvb, offsetp, t, hf_x11_##name, (length), byte_order); }
#define LISTofCARD8(name, length) { listOfByte(tvb, offsetp, t, hf_x11_##name, (length), byte_order); }
#define LISTofIPADDRESS(name, length) { listOfByte(tvb, offsetp, t, hf_x11_##name, (length), false); }
#define LISTofCARD16(name, length) { listOfCard16(tvb, offsetp, t, hf_x11_##name, hf_x11_##name##_item, (length) / 2, byte_order); }
#define LISTofCARD32(name, length) { listOfCard32(tvb, offsetp, t, hf_x11_##name, hf_x11_##name##_item, (length) / 4, byte_order); }
#define LISTofCOLORITEM(name, length) { listOfColorItem(tvb, offsetp, t, hf_x11_##name, (length) / 12, byte_order); }
#define LISTofKEYCODE(map, name, length) { listOfKeycode(tvb, offsetp, t, hf_x11_##name, map, (length), byte_order); }
#define LISTofKEYSYM(name, map, keycode_first, keycode_count, \
    keysyms_per_keycode) {\
      listOfKeysyms(tvb, pinfo, offsetp, t, hf_x11_##name, hf_x11_##name##_item, map, (keycode_first), (keycode_count), (keysyms_per_keycode), byte_order); }
#define LISTofPOINT(name, length) { listOfPoint(tvb, offsetp, t, hf_x11_##name, (length) / 4, byte_order); }
#define LISTofRECTANGLE(name) { listOfRectangle(tvb, offsetp, t, hf_x11_##name, (next_offset - *offsetp) / 8, byte_order); }
#define LISTofPIXMAPFORMAT(name, length) { listOfPixmapFormat(tvb, offsetp, t, hf_x11_##name, (length), byte_order); }
#define LISTofSCREEN(name, length) { listOfScreen(tvb, offsetp, t, hf_x11_##name, (length), byte_order); }
#define LISTofSEGMENT(name) { listOfSegment(tvb, offsetp, t, hf_x11_##name, (next_offset - *offsetp) / 8, byte_order); }
#define LISTofSTRING8(name, length) { listOfString8(tvb, offsetp, t, hf_x11_##name, hf_x11_##name##_string, (length), byte_order); }
#define LISTofTEXTITEM8(name) { listOfTextItem(tvb, offsetp, t, hf_x11_##name, false, next_offset, byte_order); }
#define LISTofTEXTITEM16(name) { listOfTextItem(tvb, offsetp, t, hf_x11_##name, true, next_offset, byte_order); }
#define OPCODE() {                                                \
      opcode = tvb_get_uint8(tvb, *offsetp);                             \
      proto_tree_add_uint(t, hf_x11_opcode, tvb, *offsetp,        \
            1, opcode);                                           \
      *offsetp += 1;                                              \
  }

#define PIXMAP(name)   { FIELD32(name); }
#define REQUEST_LENGTH() (requestLength(tvb, offsetp, t, byte_order))
#define SETofEVENT(name) { setOfEvent(tvb, offsetp, t, byte_order); }
#define SETofDEVICEEVENT(name) { setOfDeviceEvent(tvb, offsetp, t, byte_order);}
#define SETofKEYMASK(name) { setOfKeyButMask(tvb, offsetp, t, byte_order, 0); }
#define SETofKEYBUTMASK(name) { setOfKeyButMask(tvb, offsetp, t, byte_order, 1); }
#define SETofPOINTEREVENT(name) { setOfPointerEvent(tvb, offsetp, t, byte_order); }
#define STRING8(name, length)  { string8(tvb, offsetp, t, hf_x11_##name, length); }
#define STRING16(name, length)  { string16(tvb, offsetp, t, hf_x11_##name, hf_x11_##name##_bytes, length, byte_order); }
#define TIMESTAMP(name){ timestamp(tvb, offsetp, t, hf_x11_##name, byte_order); }
#define UNDECODED(x)   { proto_tree_add_item(t, hf_x11_undecoded, tvb, *offsetp,  x, ENC_NA); *offsetp += x; }
#define PAD()          { if (next_offset - *offsetp > 0) proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, next_offset - *offsetp, ENC_NA); *offsetp = next_offset; }
#define WINDOW(name)   { FIELD32(name); }

#define VISUALID(name) { int32_t v = tvb_get_uint32(tvb, *offsetp, byte_order); \
    proto_tree_add_uint_format(t, hf_x11_##name, tvb, *offsetp, 4, v, "Visualid: 0x%08x%s", v, \
                               v ? "" : " (CopyFromParent)"); *offsetp += 4; }
#define REPLY(name)       FIELD8(name);
#define REPLYLENGTH(name) FIELD32(name);

#define EVENTCONTENTS_COMMON() do {                          \
      TIMESTAMP(time);                                       \
      WINDOW(rootwindow);                                    \
      WINDOW(eventwindow);                                   \
      WINDOW(childwindow);                                   \
      INT16(root_x);                                         \
      INT16(root_y);                                         \
      INT16(event_x);                                        \
      INT16(event_y);                                        \
      setOfKeyButMask(tvb, offsetp, t, byte_order, 1);    \
} while (0)

#define SEQUENCENUMBER_REPLY(name) do {                                       \
      uint16_t seqno;                                                          \
                                                                              \
      seqno = tvb_get_uint16(tvb, *offsetp, byte_order);                     \
      proto_tree_add_uint_format(t, hf_x11_reply_##name, tvb,                 \
      *offsetp, 2, seqno,                                                     \
      "sequencenumber: %d (%s)",                                              \
      (int)seqno,                                                             \
      val_to_str(opcode & 0xFF, state->opcode_vals, "<Unknown opcode %d>"));  \
      *offsetp += 2;                                                          \
} while (0)

#define REPLYCONTENTS_COMMON() do {                                   \
      REPLY(reply);                                                   \
      proto_tree_add_item(t, hf_x11_undecoded, tvb, *offsetp,         \
             1, ENC_NA);                                              \
      ++(*offsetp);                                                   \
      SEQUENCENUMBER_REPLY(sequencenumber);                           \
      REPLYLENGTH(replylength);                                       \
      proto_tree_add_item(t, hf_x11_undecoded, tvb, *offsetp,         \
             tvb_reported_length_remaining(tvb, *offsetp), ENC_NA);   \
      *offsetp += tvb_reported_length_remaining(tvb, *offsetp);       \
} while (0)


#define HANDLE_REPLY(plen, length_remaining, str, func) do {          \
      if (length_remaining < plen) {                                  \
            if (x11_desegment && pinfo->can_desegment) {              \
                  pinfo->desegment_offset = offset;                   \
                  pinfo->desegment_len    = plen - length_remaining;  \
                  return;                                             \
            } else {                                                  \
                  ; /* XXX yes, what then?  Need to skip/join. */     \
            }                                                         \
      }                                                               \
      if (length_remaining > plen)                                    \
            length_remaining = plen;                                  \
      next_tvb = tvb_new_subset_length_caplen(tvb, offset, length_remaining, plen); \
                                                                      \
      if (sep == NULL) {                                              \
            col_set_str(pinfo->cinfo, COL_INFO, str);                 \
            sep = ":";                                                \
      }                                                               \
                                                                      \
      TRY {                                                           \
            func(next_tvb, pinfo, tree, sep, state, byte_order);      \
      }                                                               \
                                                                      \
      CATCH_NONFATAL_ERRORS {                                         \
            show_exception(next_tvb, pinfo, tree, EXCEPT_CODE, GET_MESSAGE); \
      }                                                               \
      ENDTRY;                                                         \
                                                                      \
      sep = ",";                                                      \
} while (0)

static void
dissect_x11_initial_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                  const char *sep, x11_conv_data_t *state,
                  unsigned byte_order);

static void
dissect_x11_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                  const char *sep, x11_conv_data_t *state,
                  unsigned byte_order);

static void
dissect_x11_error(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                  const char *sep, x11_conv_data_t *state,
                  unsigned byte_order);

static void
dissect_x11_event(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                  const char *sep, x11_conv_data_t *state,
                  unsigned byte_order);

static void
decode_x11_event(tvbuff_t *tvb, unsigned char eventcode, const char *sent,
                 proto_tree *t, x11_conv_data_t *state,
                 unsigned byte_order);

static x11_conv_data_t *
x11_stateinit(conversation_t *conversation);

static const char *
keysymString(uint32_t v);


/************************************************************************
 ***                                                                  ***
 ***                  D E C O D I N G   F I E L D S                   ***
 ***                                                                  ***
 ************************************************************************/

static void atom(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                 unsigned byte_order)
{
      const char *interpretation = NULL;

      uint32_t v = tvb_get_uint32(tvb, *offsetp, byte_order);
      if (v >= 1 && v < array_length(atom_predefined_interpretation))
            interpretation = atom_predefined_interpretation[v];
      else if (v)
            interpretation = "Not a predefined atom";
      else {
            header_field_info *hfi = proto_registrar_get_nth(hf);
            if (hfi -> strings)
                  interpretation = try_val_to_str(v, cVALS(hfi -> strings));
      }
      if (!interpretation) interpretation = "error in Xlib client program ?";
      proto_tree_add_uint_format(t, hf, tvb, *offsetp, 4, v, "%s: %u (%s)",
                                 proto_registrar_get_nth(hf) -> name, v, interpretation);
      *offsetp += 4;
}

static uint32_t add_boolean(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf)
{
      uint32_t v = tvb_get_uint8(tvb, *offsetp);
      proto_tree_add_boolean(t, hf, tvb, *offsetp, 1, v);
      *offsetp += 1;
      return v;
}

static void colorFlags(tvbuff_t *tvb, int *offsetp, proto_tree *t)
{
      unsigned do_red_green_blue = tvb_get_uint8(tvb, *offsetp);
      proto_item *ti;
      proto_tree *tt;

      if (do_red_green_blue) {
            bool sep = false;
            wmem_strbuf_t *buffer = wmem_strbuf_create(wmem_packet_scope());
            wmem_strbuf_append(buffer, "flags: ");

            if (do_red_green_blue & 0x1) {
                  wmem_strbuf_append(buffer, "DoRed");
                  sep = true;
            }

            if (do_red_green_blue & 0x2) {
                  if (sep) wmem_strbuf_append(buffer, " | ");
                  wmem_strbuf_append(buffer, "DoGreen");
                  sep = true;
            }

            if (do_red_green_blue & 0x4) {
                  if (sep) wmem_strbuf_append(buffer, " | ");
                  wmem_strbuf_append(buffer, "DoBlue");
                  sep = true;
            }

            if (do_red_green_blue & 0xf8) {
                  if (sep) wmem_strbuf_append(buffer, " + trash");
            }

            ti = proto_tree_add_uint_format(t, hf_x11_coloritem_flags, tvb, *offsetp, 1, do_red_green_blue,
                                            "%s", wmem_strbuf_get_str(buffer));
            tt = proto_item_add_subtree(ti, ett_x11_color_flags);
            if (do_red_green_blue & 0x1)
                  proto_tree_add_boolean(tt, hf_x11_coloritem_flags_do_red, tvb, *offsetp, 1,
                                         do_red_green_blue & 0x1);
            if (do_red_green_blue & 0x2)
                  proto_tree_add_boolean(tt, hf_x11_coloritem_flags_do_green, tvb, *offsetp, 1,
                                         do_red_green_blue & 0x2);
            if (do_red_green_blue & 0x4)
                  proto_tree_add_boolean(tt, hf_x11_coloritem_flags_do_blue, tvb, *offsetp, 1,
                                         do_red_green_blue & 0x4);
            if (do_red_green_blue & 0xf8)
                  proto_tree_add_boolean(tt, hf_x11_coloritem_flags_unused, tvb, *offsetp, 1,
                                         do_red_green_blue & 0xf8);
      } else
            proto_tree_add_uint_format(t, hf_x11_coloritem_flags, tvb, *offsetp, 1, do_red_green_blue,
                                       "flags: none");
      *offsetp += 1;
}

static void listOfArc(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                      int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 8, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_arc);
      while(length--) {
            int16_t x = tvb_get_uint16(tvb, *offsetp, byte_order);
            int16_t y = tvb_get_uint16(tvb, *offsetp + 2, byte_order);
            uint16_t width = tvb_get_uint16(tvb, *offsetp + 4, byte_order);
            uint16_t height = tvb_get_uint16(tvb, *offsetp + 6, byte_order);
            int16_t angle1 = tvb_get_uint16(tvb, *offsetp + 8, byte_order);
            int16_t angle2 = tvb_get_uint16(tvb, *offsetp + 10, byte_order);

            proto_item *tti = proto_tree_add_none_format(tt, hf_x11_arc, tvb, *offsetp, 12,
                                                             "arc: %dx%d+%d+%d, angle %d -> %d (%f degrees -> %f degrees)",
                                                             width, height, x, y, angle1, angle2,
                                                             angle1 / 64.0, angle2 / 64.0);
            proto_tree *ttt = proto_item_add_subtree(tti, ett_x11_arc);
            proto_tree_add_int(ttt, hf_x11_arc_x, tvb, *offsetp, 2, x);
            *offsetp += 2;
            proto_tree_add_int(ttt, hf_x11_arc_y, tvb, *offsetp, 2, y);
            *offsetp += 2;
            proto_tree_add_uint(ttt, hf_x11_arc_width, tvb, *offsetp, 2, y);
            *offsetp += 2;
            proto_tree_add_uint(ttt, hf_x11_arc_height, tvb, *offsetp, 2, y);
            *offsetp += 2;
            proto_tree_add_int(ttt, hf_x11_arc_angle1, tvb, *offsetp, 2, y);
            *offsetp += 2;
            proto_tree_add_int(ttt, hf_x11_arc_angle2, tvb, *offsetp, 2, y);
            *offsetp += 2;
      }
}

static void listOfAtom(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                       int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 4, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_atom);
      while(length--)
            atom(tvb, offsetp, tt, hf_x11_properties_item, byte_order);
}

static void listOfByte(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                       int length, unsigned byte_order)
{
      if (length <= 0) length = 1;
      proto_tree_add_item(t, hf, tvb, *offsetp, length, byte_order);
      *offsetp += length;
}

static void listOfCard16(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                         int hf_item, int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 2, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_card32);
      while(length--) {
            proto_tree_add_item(tt, hf_item, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
      }
}

static void listOfInt16(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                         int hf_item, int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 2, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_card32);
      while(length--) {
            proto_tree_add_item(tt, hf_item, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
      }
}

static void listOfCard32(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                         int hf_item, int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 4, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_card32);
      while(length--) {
            proto_tree_add_item(tt, hf_item, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
      }
}

static void listOfInt32(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                         int hf_item, int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 4, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_card32);
      while(length--) {
            proto_tree_add_item(tt, hf_item, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
      }
}

static void listOfCard64(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                         int hf_item, int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 8, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_card32);
      while(length--) {
            proto_tree_add_item(tt, hf_item, tvb, *offsetp, 8, byte_order);
            *offsetp += 8;
      }
}

#if 0 /* Not yet used by any extension */
static void listOfInt64(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                         int hf_item, int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 8, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_card32);
      while(length--) {
            proto_tree_add_item(tt, hf_item, tvb, *offsetp, 8, byte_order);
            *offsetp += 8;
      }
}
#endif

static void listOfFloat(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                         int hf_item, int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 4, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_float);
      while(length--) {
            proto_tree_add_item(tt, hf_item, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
      }
}

static void listOfDouble(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                         int hf_item, int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 8, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_double);
      while(length--) {
            proto_tree_add_item(tt, hf_item, tvb, *offsetp, 8, byte_order);
            *offsetp += 8;
      }
}

static void listOfColorItem(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                            int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 8, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_color_item);
      while(length--) {
            proto_item *tti;
            proto_tree *ttt;
            unsigned do_red_green_blue;
            uint16_t red, green, blue;
            wmem_strbuf_t *buffer;
            const char *sep;

            buffer=wmem_strbuf_create(wmem_packet_scope());
            wmem_strbuf_append(buffer, "colorItem ");
            red = tvb_get_uint16(tvb, *offsetp + 4, byte_order);
            green = tvb_get_uint16(tvb, *offsetp + 6, byte_order);
            blue = tvb_get_uint16(tvb, *offsetp + 8, byte_order);
            do_red_green_blue = tvb_get_uint8(tvb, *offsetp + 10);

            sep = "";
            if (do_red_green_blue & 0x1) {
                wmem_strbuf_append_printf(buffer, "red = %d", red);
                sep = ", ";
            }
            if (do_red_green_blue & 0x2) {
                wmem_strbuf_append_printf(buffer, "%sgreen = %d", sep, green);
                sep = ", ";
            }
            if (do_red_green_blue & 0x4)
                wmem_strbuf_append_printf(buffer, "%sblue = %d", sep, blue);

            tti = proto_tree_add_none_format(tt, hf_x11_coloritem, tvb, *offsetp, 12, "%s", wmem_strbuf_get_str(buffer));
            ttt = proto_item_add_subtree(tti, ett_x11_color_item);
            proto_tree_add_item(ttt, hf_x11_coloritem_pixel, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_coloritem_red, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_coloritem_green, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_coloritem_blue, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            colorFlags(tvb, offsetp, ttt);
            proto_tree_add_item(ttt, hf_x11_coloritem_unused, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
      }
}

#if 0  /* XXX: Use of GTree no longer needed; use value_string_ext */
static int compareGuint32(const void *a, const void *b)
{
      return GPOINTER_TO_INT(b) - GPOINTER_TO_INT(a);
}
#endif

static void
XConvertCase(register int sym, int *lower, int *upper)
{
    *lower = sym;
    *upper = sym;
    switch(sym >> 8) {
    case 0: /* Latin 1 */
        if ((sym >= XK_A) && (sym <= XK_Z))
            *lower += (XK_a - XK_A);
        else if ((sym >= XK_a) && (sym <= XK_z))
            *upper -= (XK_a - XK_A);
        else if ((sym >= XK_Agrave) && (sym <= XK_Odiaeresis))
            *lower += (XK_agrave - XK_Agrave);
        else if ((sym >= XK_agrave) && (sym <= XK_odiaeresis))
            *upper -= (XK_agrave - XK_Agrave);
        else if ((sym >= XK_Ooblique) && (sym <= XK_Thorn))
            *lower += (XK_oslash - XK_Ooblique);
        else if ((sym >= XK_oslash) && (sym <= XK_thorn))
            *upper -= (XK_oslash - XK_Ooblique);
        break;
    case 1: /* Latin 2 */
        /* Assume the KeySym is a legal value (ignore discontinuities) */
        if (sym == XK_Aogonek)
            *lower = XK_aogonek;
        else if (sym >= XK_Lstroke && sym <= XK_Sacute)
            *lower += (XK_lstroke - XK_Lstroke);
        else if (sym >= XK_Scaron && sym <= XK_Zacute)
            *lower += (XK_scaron - XK_Scaron);
        else if (sym >= XK_Zcaron && sym <= XK_Zabovedot)
            *lower += (XK_zcaron - XK_Zcaron);
        else if (sym == XK_aogonek)
            *upper = XK_Aogonek;
        else if (sym >= XK_lstroke && sym <= XK_sacute)
            *upper -= (XK_lstroke - XK_Lstroke);
        else if (sym >= XK_scaron && sym <= XK_zacute)
            *upper -= (XK_scaron - XK_Scaron);
        else if (sym >= XK_zcaron && sym <= XK_zabovedot)
            *upper -= (XK_zcaron - XK_Zcaron);
        else if (sym >= XK_Racute && sym <= XK_Tcedilla)
            *lower += (XK_racute - XK_Racute);
        else if (sym >= XK_racute && sym <= XK_tcedilla)
            *upper -= (XK_racute - XK_Racute);
        break;
    case 2: /* Latin 3 */
        /* Assume the KeySym is a legal value (ignore discontinuities) */
        if (sym >= XK_Hstroke && sym <= XK_Hcircumflex)
            *lower += (XK_hstroke - XK_Hstroke);
        else if (sym >= XK_Gbreve && sym <= XK_Jcircumflex)
            *lower += (XK_gbreve - XK_Gbreve);
        else if (sym >= XK_hstroke && sym <= XK_hcircumflex)
            *upper -= (XK_hstroke - XK_Hstroke);
        else if (sym >= XK_gbreve && sym <= XK_jcircumflex)
            *upper -= (XK_gbreve - XK_Gbreve);
        else if (sym >= XK_Cabovedot && sym <= XK_Scircumflex)
            *lower += (XK_cabovedot - XK_Cabovedot);
        else if (sym >= XK_cabovedot && sym <= XK_scircumflex)
            *upper -= (XK_cabovedot - XK_Cabovedot);
        break;
    case 3: /* Latin 4 */
        /* Assume the KeySym is a legal value (ignore discontinuities) */
        if (sym >= XK_Rcedilla && sym <= XK_Tslash)
            *lower += (XK_rcedilla - XK_Rcedilla);
        else if (sym >= XK_rcedilla && sym <= XK_tslash)
            *upper -= (XK_rcedilla - XK_Rcedilla);
        else if (sym == XK_ENG)
            *lower = XK_eng;
        else if (sym == XK_eng)
            *upper = XK_ENG;
        else if (sym >= XK_Amacron && sym <= XK_Umacron)
            *lower += (XK_amacron - XK_Amacron);
        else if (sym >= XK_amacron && sym <= XK_umacron)
            *upper -= (XK_amacron - XK_Amacron);
        break;
    case 6: /* Cyrillic */
        /* Assume the KeySym is a legal value (ignore discontinuities) */
        if (sym >= XK_Serbian_DJE && sym <= XK_Serbian_DZE)
            *lower -= (XK_Serbian_DJE - XK_Serbian_dje);
        else if (sym >= XK_Serbian_dje && sym <= XK_Serbian_dze)
            *upper += (XK_Serbian_DJE - XK_Serbian_dje);
        else if (sym >= XK_Cyrillic_YU && sym <= XK_Cyrillic_HARDSIGN)
            *lower -= (XK_Cyrillic_YU - XK_Cyrillic_yu);
        else if (sym >= XK_Cyrillic_yu && sym <= XK_Cyrillic_hardsign)
            *upper += (XK_Cyrillic_YU - XK_Cyrillic_yu);
        break;
    case 7: /* Greek */
        /* Assume the KeySym is a legal value (ignore discontinuities) */
        if (sym >= XK_Greek_ALPHAaccent && sym <= XK_Greek_OMEGAaccent)
            *lower += (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
        else if (sym >= XK_Greek_alphaaccent && sym <= XK_Greek_omegaaccent &&
                 sym != XK_Greek_iotaaccentdieresis &&
                 sym != XK_Greek_upsilonaccentdieresis)
            *upper -= (XK_Greek_alphaaccent - XK_Greek_ALPHAaccent);
        else if (sym >= XK_Greek_ALPHA && sym <= XK_Greek_OMEGA)
            *lower += (XK_Greek_alpha - XK_Greek_ALPHA);
        else if (sym >= XK_Greek_alpha && sym <= XK_Greek_omega &&
                 sym != XK_Greek_finalsmallsigma)
            *upper -= (XK_Greek_alpha - XK_Greek_ALPHA);
        break;
    }
}

static const char *
keycode2keysymString(int *keycodemap[256], int first_keycode,
                     int keysyms_per_keycode,
                     int *modifiermap[array_length(modifiers)],
                     int keycodes_per_modifier,
                     uint32_t keycode, uint32_t bitmask)
{
      int *syms;
      int groupmodkc, numlockkc, numlockmod, groupmod;
      int lockmod_is_capslock = 0, lockmod_is_shiftlock = 0;
      int lockmod_is_nosymbol = 1;
      int modifier, kc, keysym;

      if ((syms = keycodemap[keycode]) == NULL)
            return "<Unknown>";

      for (kc = first_keycode, groupmodkc = numlockkc = -1; kc < 256; ++kc)
            for (keysym = 0; keysym < keysyms_per_keycode; ++keysym) {
                  if (keycodemap[kc] == NULL)
                        return "<Unknown>";
                  switch (keycodemap[kc][keysym]) {
                        case 0xff7e:
                              groupmodkc = kc;
                              break;

                        case 0xff7f:
                              numlockkc = kc;
                              break;

                        case 0xffe5:
                              lockmod_is_capslock = kc;
                              break;

                        case 0xffe6:
                              lockmod_is_shiftlock = kc;
                              break;
                  }
            }


      /*
       * If we have not seen the modifiermap we don't know what the
       * keycode translates to, but we do know it's one of the keys
       * in syms (give or take a case-conversion), so we could in
       * theory list them all.
       */
      if (modifiermap[array_length(modifiers) - 1] == NULL) /* all or none */
            return "<Unknown>";

      /* find out what the numlockmodifer and groupmodifier is. */
      for (modifier = 0, numlockmod = groupmod = -1;
           modifier < (int)array_length(modifiers) && numlockmod == -1;
           ++modifier)
            for (kc = 0; kc < keycodes_per_modifier; ++kc)
                  if (modifiermap[modifier][kc] == numlockkc)
                        numlockmod = modifier;
                  else if (modifiermap[modifier][kc] == groupmodkc)
                        groupmod = modifier;

      /*
       * ... and what the lockmodifier is interpreted as.
       * (X11v4r6 ref, keyboard and pointers section.)
       */
      for (kc = 0; kc < keycodes_per_modifier; ++kc)
            if (modifiermap[1][kc] == lockmod_is_capslock) {
                  lockmod_is_shiftlock = lockmod_is_nosymbol = 0;
                  break;
            }
            else if (modifiermap[0][kc] == lockmod_is_shiftlock) {
                  lockmod_is_capslock = lockmod_is_nosymbol = 0;
                  break;
            }

#if 0
      /*
       * This is (how I understand) the X11v4R6 protocol description given
       * in A. Nye's book.  It is quite different from the
       * code in _XTranslateKey() in the file
       * "$XConsortium: KeyBind.c /main/55 1996/02/02 14:08:55 kaleb $"
       * as shipped with XFree, and doesn't work correctly, nor do I see
       * how it could (e.g. the case of lower/uppercase-letters).
       * -- Michael Shuldman
       */

      if (numlockmod >= 0 && (bitmask & modifiermask[numlockmod])
          && ((syms[1] >= 0xff80
               && syms[1] <= 0xffbd)
              || (syms[1] >= 0x11000000
                  && syms[1] <= 0x1100ffff))) {
            if ((bitmask & ShiftMask) || lockmod_is_shiftlock)
                  return keysymString(syms[groupmod + 0]);
            else
                  if (syms[groupmod + 1] == NoSymbol)
                        return keysymString(syms[groupmod + 0]);
                  else
                        return keysymString(syms[groupmod + 1]);
      }
      else if (!(bitmask & ShiftMask) && !(bitmask & LockMask))
            return keysymString(syms[groupmod + 0]);
      else if (!(bitmask & ShiftMask)
               && ((bitmask & LockMask) && lockmod_is_capslock))
            if (islower(syms[groupmod + 0]))
/*                      return toupper(keysymString(syms[groupmod + 0])); */
                  return "Uppercase"; /* XXX */
            else
                  return keysymString(syms[groupmod + 0]);

      else if ((bitmask & ShiftMask)
               && ((bitmask & LockMask) && lockmod_is_capslock))
            if (islower(syms[groupmod + 1]))
/*                      return toupper(keysymString(syms[groupmod + 1])); */
                  return "Uppercase"; /* XXX */
            else
                  return keysymString(syms[groupmod + 1]);

      else if ((bitmask & ShiftMask)
               ||  ((bitmask & LockMask) && lockmod_is_shiftlock))
            return keysymString(syms[groupmod + 1]);
#else /* _XTranslateKey() based code. */

      while (keysyms_per_keycode > 2
             && keycodemap[keysyms_per_keycode - 1] == NoSymbol)
            --keysyms_per_keycode;
      if (keysyms_per_keycode > 2
          && (groupmod >= 0 && (modifiermask[groupmod] & bitmask))) {
            syms += 2;
            keysyms_per_keycode -= 2;
      }

      if (numlockmod >= 0 && (bitmask & modifiermask[numlockmod])
          && keysyms_per_keycode > 1
          && ((syms[1] >= 0xff80 && syms[1] <= 0xffbd)
              || (syms[1] >= 0x11000000 && syms[1] <= 0x1100ffff))) {
            if ((bitmask & ShiftMask)
                || (bitmask & LockMask && lockmod_is_shiftlock))
                  keysym = syms[0];
            else
                  keysym = syms[1];
      }
      else if (!(bitmask & ShiftMask)
               && (!(bitmask & LockMask) || lockmod_is_nosymbol)) {
            if (keysyms_per_keycode == 1
                || (keysyms_per_keycode > 1 && syms[1] == NoSymbol)) {
                  int usym;

                  XConvertCase(syms[0], &keysym, &usym);
            }
            else
                  keysym = syms[0];
      }
      else if (!(bitmask & LockMask) || !lockmod_is_capslock) {
            int lsym, usym = 0;

            if (keysyms_per_keycode == 1
                || (keysyms_per_keycode > 1 && (usym = syms[1]) == NoSymbol))
                  XConvertCase(syms[0], &lsym, &usym);
            keysym = usym;
      }
      else {
            int lsym, usym = 0;

            if (keysyms_per_keycode == 1
                || (keysyms_per_keycode > 1 && syms[1] == NoSymbol))
                  keysym = syms[0];

            XConvertCase(keysym, &lsym, &usym);

            if (!(bitmask & ShiftMask) && keysym != syms[0]
                && ((keysym != usym) || (lsym == usym)))
                  XConvertCase(syms[0], &lsym, &usym);
            keysym = usym;
      }

      if (keysym == XK_VoidSymbol)
            keysym = NoSymbol;

      return wmem_strdup_printf(wmem_packet_scope(), "%d, \"%s\"", keysym, keysymString(keysym));
#endif
}

static const char *keysymString(uint32_t v)
{
#if 0  /* XXX: Use of GTree no longer needed; use value_string_ext */
      static GTree *keysymTable = NULL;

      void *res;
      if (!keysymTable) {

            /* This table is so big that we built it only if necessary */
            const value_string *p = x11_keysym_vals_source;
            keysymTable = g_tree_new(compareGuint32);
            for(; p -> strptr; p++)
                  g_tree_insert(keysymTable, GINT_TO_POINTER(p -> value), (void *) (p -> strptr) );
      }
      res = g_tree_lookup(keysymTable, GINT_TO_POINTER(v));
      return res ? res : "<Unknown>";
#endif

      return val_to_str_ext_const(v, &x11_keysym_vals_source_ext, "<Unknown>");

}

static void listOfKeycode(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                          int *modifiermap[], int keycodes_per_modifier,
                          unsigned byte_order _U_)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp,
        (int)array_length(modifiers) * keycodes_per_modifier, ENC_NA);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_keycode);
      size_t m;

      for (m = 0; m < array_length(modifiers);
        ++m, *offsetp += keycodes_per_modifier) {
            proto_item *tikc;
            int i;

            tikc = proto_tree_add_bytes_format(tt, hf_x11_keycodes_item, tvb,
                *offsetp, keycodes_per_modifier, NULL, "item: ");
            modifiermap[m] = (int *)
                wmem_alloc(wmem_file_scope(), sizeof(*modifiermap[m]) * keycodes_per_modifier);
            for(i = 0; i < keycodes_per_modifier; ++i) {
                unsigned char c = tvb_get_uint8(tvb, (*offsetp) + i);

                if (c)
                    proto_item_append_text(tikc, " %s=%d", modifiers[m], c);

                modifiermap[m][i] = c;
            }
      }
}

static void listOfKeysyms(tvbuff_t *tvb, packet_info *pinfo, int *offsetp, proto_tree *t, int hf,
                          int hf_item, int *keycodemap[256],
                          int keycode_first, int keycode_count,
                          int keysyms_per_keycode, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, keycode_count * keysyms_per_keycode * 4, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_keysyms);
      proto_item *tti;
      proto_tree *ttt;
      int i, keycode;

      DISSECTOR_ASSERT(keycode_first >= 0);
      DISSECTOR_ASSERT(keycode_count >= 0);

      for (keycode = keycode_first; keycode_count > 0;
           ++keycode, --keycode_count) {
            tti = proto_tree_add_none_format(tt, hf_item, tvb, *offsetp,
                                             4 * keysyms_per_keycode, "keysyms (keycode %d):", keycode);
            if (keycode >= 256) {
                  expert_add_info_format(pinfo, tti, &ei_x11_keycode_value_out_of_range,
                                      "keycode value %d is out of range", keycode);
                  *offsetp += 4 * keysyms_per_keycode;
                  continue;
            }

            ttt = proto_item_add_subtree(tti, ett_x11_keysym);

            tvb_ensure_bytes_exist(tvb, *offsetp, 4 * keysyms_per_keycode);
            keycodemap[keycode]
                  =  (int *)wmem_alloc(wmem_file_scope(), sizeof(*keycodemap[keycode]) * keysyms_per_keycode);

            for(i = 0; i < keysyms_per_keycode; ++i) {
                  /* keysymvalue = byte3 * 256 + byte4. */
                  uint32_t v = tvb_get_uint32(tvb, *offsetp, byte_order);

                  proto_item_append_text(tti, " %s", keysymString(v));
                  proto_tree_add_uint_format(ttt, hf_x11_keysyms_item_keysym,
                                             tvb, *offsetp, 4, v,
                                             "keysym (keycode %d): 0x%08x (%s)",
                                             keycode, v, keysymString(v));

                  keycodemap[keycode][i] = v;
                  *offsetp += 4;
            }

            for (i = 1; i < keysyms_per_keycode; ++i)
                  if (keycodemap[keycode][i] != NoSymbol)
                        break;

            if (i == keysyms_per_keycode) {
                  /* all but (possibly) first were NoSymbol. */
                  if (keysyms_per_keycode == 4) {
                        keycodemap[keycode][1] = NoSymbol;
                        keycodemap[keycode][2] = keycodemap[keycode][0];
                        keycodemap[keycode][3] = NoSymbol;
                  }

                  continue;
            }

            for (i = 2; i < keysyms_per_keycode; ++i)
                  if (keycodemap[keycode][i] != NoSymbol)
                        break;
            if (i == keysyms_per_keycode) {
                  /* all but (possibly) first two were NoSymbol. */
                  if (keysyms_per_keycode == 4) {
                        keycodemap[keycode][2] = keycodemap[keycode][0];
                        keycodemap[keycode][3] =  keycodemap[keycode][1];
                  }

                  continue;
            }
      }
}

static void listOfPoint(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                        int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 4, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_point);
      while(length--) {
            int16_t x, y;
            proto_item *tti;
            proto_tree *ttt;

            x = tvb_get_uint16(tvb, *offsetp, byte_order);
            y = tvb_get_uint16(tvb, *offsetp + 2, byte_order);

            tti = proto_tree_add_none_format(tt, hf_x11_point, tvb, *offsetp, 4, "point: (%d,%d)", x, y);
            ttt = proto_item_add_subtree(tti, ett_x11_point);
            proto_tree_add_int(ttt, hf_x11_point_x, tvb, *offsetp, 2, x);
            *offsetp += 2;
            proto_tree_add_int(ttt, hf_x11_point_y, tvb, *offsetp, 2, y);
            *offsetp += 2;
      }
}

static void listOfRectangle(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                            int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 8, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_rectangle);
      while(length--) {
            int16_t x, y;
            unsigned width, height;
            proto_item *tti;
            proto_tree *ttt;

            x = tvb_get_uint16(tvb, *offsetp, byte_order);
            y = tvb_get_uint16(tvb, *offsetp + 2, byte_order);
            width = tvb_get_uint16(tvb, *offsetp + 4, byte_order);
            height = tvb_get_uint16(tvb, *offsetp + 6, byte_order);

            tti = proto_tree_add_none_format(tt, hf_x11_rectangle, tvb, *offsetp, 8,
                                                 "rectangle: %dx%d+%d+%d", width, height, x, y);
            ttt = proto_item_add_subtree(tti, ett_x11_rectangle);
            proto_tree_add_int(ttt, hf_x11_rectangle_x, tvb, *offsetp, 2, x);
            *offsetp += 2;
            proto_tree_add_int(ttt, hf_x11_rectangle_y, tvb, *offsetp, 2, y);
            *offsetp += 2;
            proto_tree_add_uint(ttt, hf_x11_rectangle_width, tvb, *offsetp, 2, width);
            *offsetp += 2;
            proto_tree_add_uint(ttt, hf_x11_rectangle_height, tvb, *offsetp, 2, height);
            *offsetp += 2;
      }
}

static void listOfSegment(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                          int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 8, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_segment);
      while(length--) {
            int16_t x1, y1, x2, y2;
            proto_item *tti;
            proto_tree *ttt;

            x1 = tvb_get_uint16(tvb, *offsetp, byte_order);
            y1 = tvb_get_uint16(tvb, *offsetp + 2, byte_order);
            x2 = tvb_get_uint16(tvb, *offsetp + 4, byte_order);
            y2 = tvb_get_uint16(tvb, *offsetp + 6, byte_order);

            tti = proto_tree_add_none_format(tt, hf_x11_segment, tvb, *offsetp, 8,
                                                 "segment: (%d,%d)-(%d,%d)", x1, y1, x2, y2);
            ttt = proto_item_add_subtree(tti, ett_x11_segment);
            proto_tree_add_item(ttt, hf_x11_segment_x1, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_segment_y1, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_segment_x2, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_segment_y2, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
      }
}

static void listOfVisualTypes(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                              int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 24, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_visualtype);
      while(length--) {
            proto_item *tti;
            proto_tree *ttt;

            tti = proto_tree_add_none_format(tt, hf_x11_visualtype, tvb, *offsetp, 24,
                                             "visualtype");
            ttt = proto_item_add_subtree(tti, ett_x11_visualtype);
            proto_tree_add_item(ttt, hf_x11_visualtype_visualid, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_visualtype_class, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            proto_tree_add_item(ttt, hf_x11_visualtype_bits_per_rgb_value, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            proto_tree_add_item(ttt, hf_x11_visualtype_colormap_entries, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_visualtype_red_mask, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_visualtype_green_mask, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_visualtype_blue_mask, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_unused, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
      }
}
static void listOfDepth(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                        int length, unsigned byte_order)
{
      uint16_t number_of_visualtypes;
      proto_item *ti;
      proto_tree *tt;

      ti = proto_tree_add_item(t, hf, tvb, *offsetp, -1, byte_order);
      tt = proto_item_add_subtree(ti, ett_x11_list_of_depth_detail);
      while(length--) {
            proto_item *tti;
            proto_tree *ttt;
            number_of_visualtypes = tvb_get_uint16(tvb, *offsetp + 2, byte_order);

            tti = proto_tree_add_none_format(tt, hf_x11_depth_detail, tvb, *offsetp, 8 + 24 * number_of_visualtypes,
                                             "depth-detail");
            ttt = proto_item_add_subtree(tti, ett_x11_screen);
            proto_tree_add_item(ttt, hf_x11_depth_detail_depth, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            proto_tree_add_item(ttt, hf_x11_unused, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            proto_tree_add_item(ttt, hf_x11_depth_detail_visualtypes_numbers, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_unused, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            if (number_of_visualtypes > 0)
                    listOfVisualTypes(tvb, offsetp, ttt, hf_x11_visualtype, number_of_visualtypes, byte_order);
      }
}

static void listOfScreen(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                         int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, -1, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_screen);
      while(length--) {
            uint8_t number_of_depths, root_depth;
            uint16_t width_in_pixels, height_in_pixels;
            uint32_t screen_root;
            proto_item *tti;
            proto_tree *ttt;

            screen_root = tvb_get_uint32(tvb, *offsetp, byte_order);
            width_in_pixels = tvb_get_uint16(tvb, *offsetp + 20, byte_order);
            height_in_pixels = tvb_get_uint16(tvb, *offsetp + 22, byte_order);
            root_depth = tvb_get_uint8(tvb, *offsetp + 38);
            tti = proto_tree_add_none_format(tt, hf_x11_screen, tvb, *offsetp, 0,
                                                 "screen (%08x: %d x %d x %d)", screen_root,
                                                 width_in_pixels, height_in_pixels, root_depth);
            ttt = proto_item_add_subtree(tti, ett_x11_screen);
            proto_tree_add_item(ttt, hf_x11_screen_root, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_screen_default_colormap, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_screen_white_pixel, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_screen_black_pixel, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_screen_current_input_masks, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_screen_width_in_pixels, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_screen_height_in_pixels, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_screen_width_in_millimeters, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_screen_height_in_millimeters, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_screen_min_installed_maps, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_screen_max_installed_maps, tvb, *offsetp, 2, byte_order);
            *offsetp += 2;
            proto_tree_add_item(ttt, hf_x11_screen_root_visual, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            proto_tree_add_item(ttt, hf_x11_screen_backing_stores, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            proto_tree_add_item(ttt, hf_x11_screen_save_unders, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            proto_tree_add_item(ttt, hf_x11_screen_root_depth, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            number_of_depths = tvb_get_uint8(tvb, *offsetp);
            proto_tree_add_item(ttt, hf_x11_screen_allowed_depths_len, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            listOfDepth(tvb, offsetp, ttt, hf_x11_depth_detail, number_of_depths, byte_order);
      }
}
static void listOfPixmapFormat(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                               int length, unsigned byte_order)
{
      proto_item *ti = proto_tree_add_item(t, hf, tvb, *offsetp, length * 8, byte_order);
      proto_tree *tt = proto_item_add_subtree(ti, ett_x11_list_of_pixmap_format);
      while(length--) {
            proto_item *tti;
            proto_tree *ttt;

            tti = proto_tree_add_none_format(tt, hf_x11_pixmap_format, tvb, *offsetp, 8,
                                                 "pixmap-format");
            ttt = proto_item_add_subtree(tti, ett_x11_pixmap_format);
            proto_tree_add_item(ttt, hf_x11_pixmap_format_depth, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            proto_tree_add_item(ttt, hf_x11_pixmap_format_bits_per_pixel, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            proto_tree_add_item(ttt, hf_x11_pixmap_format_scanline_pad, tvb, *offsetp, 1, byte_order);
            *offsetp += 1;
            proto_tree_add_item(ttt, hf_x11_unused, tvb, *offsetp, 5, byte_order);
            *offsetp += 5;
      }
}

static void listOfString8(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                          int hf_item, int length, unsigned byte_order)
{
      char *s = NULL;
      proto_item *ti;
      proto_tree *tt;
      int i;

      /* Compute total length */

      int scanning_offset = *offsetp; /* Scanning pointer */
      for(i = length; i; i--) {
            int l;
            l = tvb_get_uint8(tvb, scanning_offset);
            scanning_offset += 1 + l;
      }

      ti = proto_tree_add_item(t, hf, tvb, *offsetp, scanning_offset - *offsetp, byte_order);
      tt = proto_item_add_subtree(ti, ett_x11_list_of_string8);

      while(length--) {
            unsigned l = tvb_get_uint8(tvb, *offsetp);
            s = tvb_get_string_enc(wmem_packet_scope(), tvb, *offsetp + 1, l, ENC_ASCII);
            proto_tree_add_string_format(tt, hf_item, tvb, *offsetp, l + 1, s, "\"%s\"", s);
            *offsetp += l + 1;
      }
}

static int stringIsActuallyAn8BitString(tvbuff_t *tvb, int offset, unsigned length)
{
      for(; length > 0; offset += 2, length--) {
            if (tvb_get_uint8(tvb, offset))
                  return false;
      }
      return true;
}

/* XXX - assumes that the string encoding is ASCII; even if 0x00 through
   0x7F are ASCII, 0x80 through 0xFF might not be, and even 0x00 through
   0x7F aren't necessarily ASCII. */
static char *tvb_get_ascii_string16(tvbuff_t *tvb, int offset, unsigned length)
{
      wmem_strbuf_t *str;
      uint8_t ch;

      str = wmem_strbuf_new_sized(wmem_packet_scope(), length + 1);

      while(length--) {
            offset++;
            ch = tvb_get_uint8(tvb, offset);
            if (ch < 0x80)
                  wmem_strbuf_append_c(str, ch);
            else
                  wmem_strbuf_append_unichar_repl(str);
            offset++;
      }
      return wmem_strbuf_finalize(str);
}

static void listOfTextItem(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
    int sizeIs16, int next_offset, unsigned byte_order)
{
      char *s = NULL;
      proto_item *ti;
      proto_tree *tt;
      uint32_t fid;

      /* Compute total length */

      int scanning_offset = *offsetp; /* Scanning pointer */
      int n = 0;                        /* Number of items */

      while(scanning_offset < next_offset) {
            int l;                            /* Length of an individual item */
            l = tvb_get_uint8(tvb, scanning_offset);
            scanning_offset++;
            if (!l) break;
            n++;
            scanning_offset += l == 255 ? 4 : l + (sizeIs16 ? l : 0) + 1;
      }

      ti = proto_tree_add_item(t, hf, tvb, *offsetp, scanning_offset - *offsetp, byte_order);
      tt = proto_item_add_subtree(ti, ett_x11_list_of_text_item);

      while(n--) {
            unsigned l = tvb_get_uint8(tvb, *offsetp);
            if (l == 255) { /* Item is a font */
                  fid = tvb_get_ntohl(tvb, *offsetp + 1);
                  proto_tree_add_uint(tt, hf_x11_textitem_font, tvb, *offsetp, 5, fid);
                  *offsetp += 5;
            } else { /* Item is a string */
                  proto_item *tti;
                  proto_tree *ttt;
                  int8_t delta = tvb_get_uint8(tvb, *offsetp + 1);
                  if (sizeIs16) {
                        if (stringIsActuallyAn8BitString(tvb, *offsetp + 2, l)) {
                              s = tvb_get_ascii_string16(tvb, *offsetp + 2, l);
                              tti = proto_tree_add_none_format(tt, hf_x11_textitem_string, tvb, *offsetp, l*2 + 2,
                                                               "textitem (string): delta = %d, \"%s\"",
                                                               delta, s);
                              ttt = proto_item_add_subtree(tti, ett_x11_text_item);
                              proto_tree_add_item(ttt, hf_x11_textitem_string_delta, tvb, *offsetp + 1, 1, byte_order);
                              proto_tree_add_string_format_value(ttt, hf_x11_textitem_string_string16, tvb,
                                                                 *offsetp + 2, l, s, "\"%s\"", s);
                        } else {
                              tti = proto_tree_add_none_format(tt, hf_x11_textitem_string, tvb, *offsetp, l*2 + 2,
                                                               "textitem (string): delta = %d, %s",
                                                               delta,
                                                               tvb_bytes_to_str(wmem_packet_scope(), tvb, *offsetp + 2, l*2));
                              ttt = proto_item_add_subtree(tti, ett_x11_text_item);
                              proto_tree_add_item(ttt, hf_x11_textitem_string_delta, tvb, *offsetp + 1, 1, byte_order);
                              proto_tree_add_item(ttt, hf_x11_textitem_string_string16_bytes, tvb, *offsetp + 2, l*2, byte_order);
                        }
                        *offsetp += l*2 + 2;
                  } else {
                        s = tvb_get_string_enc(wmem_packet_scope(), tvb, *offsetp + 2, l, ENC_ASCII);
                        tti = proto_tree_add_none_format(tt, hf_x11_textitem_string, tvb, *offsetp, l + 2,
                                                         "textitem (string): delta = %d, \"%s\"",
                                                         delta, s);
                        ttt = proto_item_add_subtree(tti, ett_x11_text_item);
                        proto_tree_add_item(ttt, hf_x11_textitem_string_delta, tvb, *offsetp + 1, 1, byte_order);
                        proto_tree_add_string_format(ttt, hf_x11_textitem_string_string8, tvb,
                                                     *offsetp + 2, l, s, "\"%s\"", s);
                        *offsetp += l + 2;
                  }
            }
      }
}

static uint32_t field8(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                      unsigned byte_order)
{
      uint32_t v = tvb_get_uint8(tvb, *offsetp);
      header_field_info *hfi = proto_registrar_get_nth(hf);
      const char *enumValue = NULL;

      if (hfi -> strings)
            enumValue = try_val_to_str(v, cVALS(hfi -> strings));
      if (enumValue)
            proto_tree_add_uint_format(t, hf, tvb, *offsetp, 1, v,
            hfi -> display == BASE_DEC ? "%s: %u (%s)" : "%s: 0x%02x (%s)",
            hfi -> name, v, enumValue);
      else
            proto_tree_add_item(t, hf, tvb, *offsetp, 1, byte_order);
      *offsetp += 1;
      return v;
}

static uint32_t field16(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                       unsigned byte_order)
{
      uint32_t v = tvb_get_uint16(tvb, *offsetp, byte_order);
      header_field_info *hfi = proto_registrar_get_nth(hf);
      const char *enumValue = NULL;

      if (hfi -> strings)
            enumValue = try_val_to_str(v, cVALS(hfi -> strings));
      if (enumValue)
            proto_tree_add_uint_format(t, hf, tvb, *offsetp, 2, v,
            hfi -> display == BASE_DEC ? "%s: %u (%s)" : "%s: 0x%02x (%s)",
            hfi -> name, v, enumValue);
      else
            proto_tree_add_item(t, hf, tvb, *offsetp, 2, byte_order);
      *offsetp += 2;
      return v;
}

static uint32_t field32(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                       unsigned byte_order)
{
      uint32_t v = tvb_get_uint32(tvb, *offsetp, byte_order);
      header_field_info *hfi = proto_registrar_get_nth(hf);
      const char *enumValue = NULL;
      const char *nameAsChar = hfi -> name;

      if (hfi -> strings)
            enumValue = try_val_to_str(v, cVALS(hfi -> strings));
      if (enumValue)
            proto_tree_add_uint_format(t, hf, tvb, *offsetp, 4, v,
                                       hfi -> display == BASE_DEC ? "%s: %u (%s)" : "%s: 0x%08x (%s)",
                                       nameAsChar, v, enumValue);
      else
            proto_tree_add_uint_format(t, hf, tvb, *offsetp, 4, v,
                                       hfi -> display == BASE_DEC ? "%s: %u" : "%s: 0x%08x",
                                       nameAsChar, v);
      *offsetp += 4;
      return v;
}

static int * const gc_mask_attributes[] = {
    &hf_x11_gc_value_mask_function,
    &hf_x11_gc_value_mask_plane_mask,
    &hf_x11_gc_value_mask_foreground,
    &hf_x11_gc_value_mask_background,
    &hf_x11_gc_value_mask_line_width,
    &hf_x11_gc_value_mask_line_style,
    &hf_x11_gc_value_mask_cap_style,
    &hf_x11_gc_value_mask_join_style,
    &hf_x11_gc_value_mask_fill_style,
    &hf_x11_gc_value_mask_fill_rule,
    &hf_x11_gc_value_mask_tile,
    &hf_x11_gc_value_mask_stipple,
    &hf_x11_gc_value_mask_tile_stipple_x_origin,
    &hf_x11_gc_value_mask_tile_stipple_y_origin,
    &hf_x11_gc_value_mask_font,
    &hf_x11_gc_value_mask_subwindow_mode,
    &hf_x11_gc_value_mask_graphics_exposures,
    &hf_x11_gc_value_mask_clip_x_origin,
    &hf_x11_gc_value_mask_clip_y_origin,
    &hf_x11_gc_value_mask_clip_mask,
    &hf_x11_gc_value_mask_dash_offset,
    &hf_x11_gc_value_mask_gc_dashes,
    &hf_x11_gc_value_mask_arc_mode,
    NULL
};

static void gcAttributes(tvbuff_t *tvb, int *offsetp, proto_tree *t,
                         unsigned byte_order)
{
      uint32_t bitmask;
      bitmask = tvb_get_uint32(tvb, *offsetp, byte_order);
      proto_tree_add_bitmask(t, tvb, *offsetp, hf_x11_gc_value_mask, ett_x11_gc_value_mask, gc_mask_attributes, byte_order);
      *offsetp += 4;

      if (bitmask & 0x00000001) {
          proto_tree_add_item(t, hf_x11_function, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000002) {
          proto_tree_add_item(t, hf_x11_plane_mask, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000004) {
          proto_tree_add_item(t, hf_x11_foreground, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000008) {
          proto_tree_add_item(t, hf_x11_background, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000010) {
          proto_tree_add_item(t, hf_x11_line_width, tvb, *offsetp, 2, byte_order);
          *offsetp += 2;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
          *offsetp += 2;
      }
      if (bitmask & 0x00000020) {
          proto_tree_add_item(t, hf_x11_line_style, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000040) {
          proto_tree_add_item(t, hf_x11_cap_style, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000080) {
          proto_tree_add_item(t, hf_x11_join_style, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000100) {
          proto_tree_add_item(t, hf_x11_fill_style, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000200) {
          proto_tree_add_item(t, hf_x11_fill_rule, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000400) {
          proto_tree_add_item(t, hf_x11_tile, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000800) {
          proto_tree_add_item(t, hf_x11_stipple, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00001000) {
          proto_tree_add_item(t, hf_x11_tile_stipple_x_origin, tvb, *offsetp, 2, byte_order);
          *offsetp += 2;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
          *offsetp += 2;
      }
      if (bitmask & 0x00002000) {
          proto_tree_add_item(t, hf_x11_tile_stipple_y_origin, tvb, *offsetp, 2, byte_order);
          *offsetp += 2;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
          *offsetp += 2;
      }
      if (bitmask & 0x00004000) {
          proto_tree_add_item(t, hf_x11_font, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00008000) {
          proto_tree_add_item(t, hf_x11_subwindow_mode, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00010000) {
          proto_tree_add_item(t, hf_x11_graphics_exposures, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00020000) {
          proto_tree_add_item(t, hf_x11_clip_x_origin, tvb, *offsetp, 2, byte_order);
          *offsetp += 2;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
          *offsetp += 2;
      }
      if (bitmask & 0x00040000) {
          proto_tree_add_item(t, hf_x11_clip_y_origin, tvb, *offsetp, 2, byte_order);
          *offsetp += 2;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
          *offsetp += 2;
      }
      if (bitmask & 0x00080000) {
          proto_tree_add_item(t, hf_x11_clip_mask, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00100000) {
          proto_tree_add_item(t, hf_x11_dash_offset, tvb, *offsetp, 2, byte_order);
          *offsetp += 2;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
          *offsetp += 2;
      }
      if (bitmask & 0x00200000) {
          proto_tree_add_item(t, hf_x11_gc_dashes, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00400000) {
          proto_tree_add_item(t, hf_x11_arc_mode, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
}

static void gcMask(tvbuff_t *tvb, int *offsetp, proto_tree *t,
                   unsigned byte_order)
{
      proto_tree_add_bitmask(t, tvb, *offsetp, hf_x11_gc_value_mask, ett_x11_gc_value_mask, gc_mask_attributes, byte_order);
      *offsetp += 4;
}

static uint32_t requestLength(tvbuff_t *tvb, int *offsetp, proto_tree *t,
                             unsigned byte_order)
{
      uint32_t res;
      proto_item *ti = proto_tree_add_item_ret_uint(t, hf_x11_request_length, tvb, *offsetp, 2, byte_order, &res);
      *offsetp += 2;
      if (res == 0) {
            proto_item_append_text(ti, " (extended length flag)");
            proto_tree_add_item_ret_uint(t, hf_x11_request_length, tvb, *offsetp, 4, byte_order, &res);
            *offsetp += 4;
      }
      return res * 4;
}

static void setOfEvent(tvbuff_t *tvb, int *offsetp, proto_tree *t,
                       unsigned byte_order)
{
      static int * const events[] = {
            &hf_x11_event_mask_KeyPress,
            &hf_x11_event_mask_KeyRelease,
            &hf_x11_event_mask_ButtonPress,
            &hf_x11_event_mask_ButtonRelease,
            &hf_x11_event_mask_EnterWindow,
            &hf_x11_event_mask_LeaveWindow,
            &hf_x11_event_mask_PointerMotion,
            &hf_x11_event_mask_PointerMotionHint,
            &hf_x11_event_mask_Button1Motion,
            &hf_x11_event_mask_Button2Motion,
            &hf_x11_event_mask_Button3Motion,
            &hf_x11_event_mask_Button4Motion,
            &hf_x11_event_mask_Button5Motion,
            &hf_x11_event_mask_ButtonMotion,
            &hf_x11_event_mask_KeymapState,
            &hf_x11_event_mask_Exposure,
            &hf_x11_event_mask_VisibilityChange,
            &hf_x11_event_mask_StructureNotify,
            &hf_x11_event_mask_ResizeRedirect,
            &hf_x11_event_mask_SubstructureNotify,
            &hf_x11_event_mask_SubstructureRedirect,
            &hf_x11_event_mask_FocusChange,
            &hf_x11_event_mask_PropertyChange,
            &hf_x11_event_mask_ColormapChange,
            &hf_x11_event_mask_OwnerGrabButton,
            NULL
      };

      proto_tree_add_bitmask(t, tvb, *offsetp, hf_x11_event_mask, ett_x11_event_mask, events, byte_order);
      *offsetp += 4;
}

static void setOfDeviceEvent(tvbuff_t *tvb, int *offsetp, proto_tree *t,
                             unsigned byte_order)
{
      static int * const do_not_propagate_events[] = {
            &hf_x11_do_not_propagate_mask_KeyPress,
            &hf_x11_do_not_propagate_mask_KeyRelease,
            &hf_x11_do_not_propagate_mask_ButtonPress,
            &hf_x11_do_not_propagate_mask_ButtonRelease,
            &hf_x11_do_not_propagate_mask_PointerMotion,
            &hf_x11_do_not_propagate_mask_Button1Motion,
            &hf_x11_do_not_propagate_mask_Button2Motion,
            &hf_x11_do_not_propagate_mask_Button3Motion,
            &hf_x11_do_not_propagate_mask_Button4Motion,
            &hf_x11_do_not_propagate_mask_Button5Motion,
            &hf_x11_do_not_propagate_mask_ButtonMotion,
            NULL
      };

      proto_tree_add_bitmask(t, tvb, *offsetp, hf_x11_do_not_propagate_mask, ett_x11_do_not_propagate_mask, do_not_propagate_events, byte_order);
      *offsetp += 4;
}


static void setOfKeyButMask(tvbuff_t *tvb, int *offsetp, proto_tree *t,
                            unsigned byte_order, bool butmask)
{
      proto_item *ti;
      uint32_t bitmask_value;
      int bitmask_offset;
      int bitmask_size;
      proto_tree *bitmask_tree;

      bitmask_value = tvb_get_uint16(tvb, *offsetp, byte_order);
      bitmask_offset = *offsetp;
      bitmask_size = 2;

      if (!butmask && bitmask_value == 0x8000)
            proto_tree_add_uint_format(t, hf_x11_modifiers_mask_AnyModifier, tvb, *offsetp, 2, 0x8000,
                                       "modifiers-masks: 0x8000 (AnyModifier)");
      else {
            ti = proto_tree_add_uint(t, hf_x11_modifiers_mask, tvb, *offsetp, 2,
                                                 bitmask_value);
            bitmask_tree = proto_item_add_subtree(ti, ett_x11_set_of_key_mask);
            FLAG(modifiers, Shift);
            FLAG(modifiers, Lock);
            FLAG(modifiers, Control);
            FLAG(modifiers, Mod1);
            FLAG(modifiers, Mod2);
            FLAG(modifiers, Mod3);
            FLAG(modifiers, Mod4);
            FLAG(modifiers, Mod5);

            if (butmask) {
                  FLAG(modifiers, Button1);
                  FLAG(modifiers, Button2);
                  FLAG(modifiers, Button3);
                  FLAG(modifiers, Button4);
                  FLAG(modifiers, Button5);
            }

            if (butmask)
                  FLAG_IF_NONZERO(keybut, erroneous_bits);
            else
                  FLAG_IF_NONZERO(modifiers, erroneous_bits);
      }
      *offsetp += 2;
}

static void setOfPointerEvent(tvbuff_t *tvb, int *offsetp, proto_tree *t,
                              unsigned byte_order)
{
      static int * const pointer_events[] = {
            &hf_x11_pointer_event_mask_ButtonRelease,
            &hf_x11_pointer_event_mask_EnterWindow,
            &hf_x11_pointer_event_mask_LeaveWindow,
            &hf_x11_pointer_event_mask_PointerMotion,
            &hf_x11_pointer_event_mask_PointerMotionHint,
            &hf_x11_pointer_event_mask_Button1Motion,
            &hf_x11_pointer_event_mask_Button2Motion,
            &hf_x11_pointer_event_mask_Button3Motion,
            &hf_x11_pointer_event_mask_Button4Motion,
            &hf_x11_pointer_event_mask_Button5Motion,
            &hf_x11_pointer_event_mask_ButtonMotion,
            &hf_x11_pointer_event_mask_KeymapState,
            NULL
      };

      proto_tree_add_bitmask(t, tvb, *offsetp, hf_x11_pointer_event_mask, ett_x11_pointer_event_mask, pointer_events, byte_order);
      *offsetp += 2;
}

static void string8(tvbuff_t *tvb, int *offsetp, proto_tree *t,
    int hf, unsigned length)
{
      proto_tree_add_item(t, hf, tvb, *offsetp, length, ENC_NA|ENC_ASCII);
      *offsetp += length;
}

/* The length supplied is the length of the string in CHAR2Bs (half the number of bytes) */

static void string16(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
    int hf_bytes, unsigned length, unsigned byte_order)
{
      unsigned l = length*2; /* byte count */
      char *s;

      if (stringIsActuallyAn8BitString(tvb, *offsetp, length)) {
            s = tvb_get_ascii_string16(tvb, *offsetp, length);
            proto_tree_add_string_format_value(t, hf, tvb, *offsetp, l, s, "\"%s\"", s);
      } else
            proto_tree_add_item(t, hf_bytes, tvb, *offsetp, l, byte_order);

      *offsetp += l;
}

static void timestamp(tvbuff_t *tvb, int *offsetp, proto_tree *t, int hf,
                      unsigned byte_order)
{
      uint32_t v = tvb_get_uint32(tvb, *offsetp, byte_order);

      if (!v)
            proto_tree_add_uint_format(t, hf, tvb, *offsetp, 4, 0, "%s: 0 (CurrentTime)",
                                       proto_registrar_get_nth(hf) -> name);
      else
            proto_tree_add_uint(t, hf, tvb, *offsetp, 4, v);
      *offsetp += 4;
}

static void windowAttributes(tvbuff_t *tvb, int *offsetp, proto_tree *t,
                             unsigned byte_order)
{
      uint32_t bitmask;
      static int * const window_attributes_flags[] = {
            &hf_x11_window_value_mask_background_pixmap,
            &hf_x11_window_value_mask_background_pixel,
            &hf_x11_window_value_mask_border_pixmap,
            &hf_x11_window_value_mask_border_pixel,
            &hf_x11_window_value_mask_bit_gravity,
            &hf_x11_window_value_mask_win_gravity,
            &hf_x11_window_value_mask_backing_store,
            &hf_x11_window_value_mask_backing_planes,
            &hf_x11_window_value_mask_backing_pixel,
            &hf_x11_window_value_mask_override_redirect,
            &hf_x11_window_value_mask_save_under,
            &hf_x11_window_value_mask_event_mask,
            &hf_x11_window_value_mask_do_not_propagate_mask,
            &hf_x11_window_value_mask_colormap,
            &hf_x11_window_value_mask_cursor,
            NULL
      };

      bitmask = tvb_get_uint32(tvb, *offsetp, byte_order);
      proto_tree_add_bitmask(t, tvb, *offsetp, hf_x11_window_value_mask, ett_x11_window_value_mask, window_attributes_flags, byte_order);
      *offsetp += 4;

      if (bitmask & 0x00000001) {
          proto_tree_add_item(t, hf_x11_background_pixmap, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000002) {
          proto_tree_add_item(t, hf_x11_background_pixel, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000004) {
          proto_tree_add_item(t, hf_x11_border_pixmap, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000008) {
          proto_tree_add_item(t, hf_x11_border_pixel, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000010) {
          proto_tree_add_item(t, hf_x11_bit_gravity, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000020) {
          proto_tree_add_item(t, hf_x11_win_gravity, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000040) {
          proto_tree_add_item(t, hf_x11_backing_store, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000080) {
          proto_tree_add_item(t, hf_x11_backing_planes, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000100) {
          proto_tree_add_item(t, hf_x11_backing_pixel, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00000200) {
          proto_tree_add_item(t, hf_x11_override_redirect, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000400) {
          proto_tree_add_item(t, hf_x11_save_under, tvb, *offsetp, 1, byte_order);
          *offsetp += 1;
          proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
          *offsetp += 3;
      }
      if (bitmask & 0x00000800) {
          setOfEvent(tvb, offsetp, t, byte_order);
      }
      if (bitmask & 0x00001000) {
          setOfDeviceEvent(tvb, offsetp, t, byte_order);
      }
      if (bitmask & 0x00002000) {
          proto_tree_add_item(t, hf_x11_colormap, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
      if (bitmask & 0x00004000) {
          proto_tree_add_item(t, hf_x11_cursor, tvb, *offsetp, 4, byte_order);
          *offsetp += 4;
      }
}

/************************************************************************
 ***                                                                  ***
 ***         G U E S S I N G   T H E   B Y T E   O R D E R I N G      ***
 ***                                                                  ***
 ************************************************************************/

/* If we can't guess, we return ENC_LITTLE_ENDIAN, cause
   I'm developing on a Linux box :-). The (non-)guess isn't cached
   however, so we may have more luck next time. I'm quite conservative
   in my assertions, cause once it's cached, it stays in cache, and
   we may be fooled up by a packet starting with the end of a request
   started in a previous packet...
*/

static int numberOfBitSetTable[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };

static int numberOfBitSet(tvbuff_t *tvb, int offset, int maskLength)
{
      int res = 0;
      while(maskLength--) {
            int c = tvb_get_uint8(tvb, offset);
            offset++;
            res += numberOfBitSetTable[c & 0xf] + numberOfBitSetTable[c >> 4];
      }
      return res;
}

static int listOfStringLengthConsistent(tvbuff_t *tvb, int offset, int length, int listLength)
{
      if (listLength > length) return false;
      while(listLength--) {
            int l;
            if (!tvb_bytes_exist(tvb, offset, 1)) return true;
            l = tvb_get_uint8(tvb, offset);
            if (!l) break;
            l++;
            if (l > length) return false;
            if (!tvb_bytes_exist(tvb, offset, l)) return true;
            offset += l;
            length -= l;
      }
      if (length > 3) return false;
      return true;
}

static int rounded4(int n)
{
      int remainder = n % 4;
      int res = n / 4;
      if (remainder) res++;
      return res;
}

/* We assume the order to be consistent, until proven wrong. */

static bool consistentWithOrder(int length, tvbuff_t *tvb, int offset, int encoding)
{
      switch(tvb_get_uint8(tvb, offset)) {
            case X_CreateWindow:
                  return !tvb_bytes_exist(tvb, offset, 32) || length == 8 + numberOfBitSet(tvb, offset + 7 * 4, 4);

            case X_ChangeWindowAttributes:
            case X_ChangeGC:
                  return !tvb_bytes_exist(tvb, offset, 12) || length == 3 + numberOfBitSet(tvb, offset + 8, 4);

            case X_GetWindowAttributes:
            case X_DestroyWindow:
            case X_DestroySubwindows:
            case X_ChangeSaveSet:
            case X_MapWindow:
            case X_MapSubwindows:
            case X_UnmapWindow:
            case X_UnmapSubwindows:
            case X_CirculateWindow:
            case X_GetGeometry:
            case X_QueryTree:
            case X_GetAtomName:
            case X_ListProperties:
            case X_GetSelectionOwner:
            case X_UngrabPointer:
            case X_UngrabKeyboard:
            case X_AllowEvents:
            case X_QueryPointer:
            case X_CloseFont:
            case X_QueryFont:
            case X_FreePixmap:
            case X_FreeGC:
            case X_FreeColormap:
            case X_InstallColormap:
            case X_UninstallColormap:
            case X_ListInstalledColormaps:
            case X_FreeCursor:
            case X_GetKeyboardMapping:
            case X_KillClient:
                  return length == 2;

            case X_ReparentWindow:
            case X_SetSelectionOwner:
            case X_ChangeActivePointerGrab:
            case X_GrabKeyboard:
            case X_GrabKey:
            case X_GetMotionEvents:
            case X_TranslateCoords:
            case X_CreatePixmap:
            case X_CopyGC:
            case X_ClearArea:
            case X_CreateColormap:
            case X_AllocColor:
            case X_AllocColorPlanes:
                  return length == 4;

            case X_ConfigureWindow:
                  return !tvb_bytes_exist(tvb, offset, 10) || length == 3 + numberOfBitSet(tvb, offset + 8, 2);

            case X_InternAtom:
            case X_QueryExtension:
                  return !tvb_bytes_exist(tvb, offset, 6) || length == 2 + rounded4(tvb_get_uint16(tvb, offset + 4, encoding));

            case X_ChangeProperty:
            {
                  int multiplier, type;
                  if (!tvb_bytes_exist(tvb, offset, 17)) return true;
                  type = tvb_get_uint8(tvb, 16);
                  if (type != 8 && type != 16 && type != 32) return false;
                  multiplier = type == 8 ? 1 : type == 16 ? 2 : 4;
                  if (!tvb_bytes_exist(tvb, offset, 24)) return true;
                  return length == 6 + rounded4(tvb_get_uint32(tvb, offset + 20, encoding) * multiplier);
            }

            case X_DeleteProperty:
            case X_UngrabButton:
            case X_UngrabKey:
            case X_SetInputFocus:
            case X_CopyColormapAndFree:
            case X_AllocColorCells:
            case X_QueryBestSize:
            case X_ChangePointerControl:
            case X_SetScreenSaver:
                  return length == 3;

            case X_GetProperty:
            case X_ConvertSelection:
            case X_GrabPointer:
            case X_GrabButton:
            case X_WarpPointer:
                  return length == 6;

            case X_SendEvent:
                  return length == 11;

            case X_GrabServer:
            case X_UngrabServer:
            case X_GetInputFocus:
            case X_QueryKeymap:
            case X_GetFontPath:
            case X_ListExtensions:
            case X_GetKeyboardControl:
            case X_Bell:
            case X_GetPointerControl:
            case X_GetScreenSaver:
            case X_ListHosts:
            case X_SetAccessControl:
            case X_SetCloseDownMode:
            case X_ForceScreenSaver:
            case X_GetPointerMapping:
            case X_GetModifierMapping:
                  return length == 1;

            case X_OpenFont:
            case X_AllocNamedColor:
            case X_LookupColor:
                  return !tvb_bytes_exist(tvb, offset, 10) || length == 3 + rounded4(tvb_get_uint16(tvb, offset + 8, encoding));

            case X_QueryTextExtents:
                  return length >= 2;

            case X_ListFonts:
            case X_ListFontsWithInfo:
            case X_ChangeHosts:
                  return !tvb_bytes_exist(tvb, offset, 8) || length == 2 + rounded4(tvb_get_uint16(tvb, offset + 6, encoding));

            case X_SetFontPath:
                  if (length < 2) return false;
                  if (!tvb_bytes_exist(tvb, offset, 8)) return true;
                  return listOfStringLengthConsistent(tvb, offset + 8, (length - 2) * 4, tvb_get_uint16(tvb, offset + 4, encoding));

            case X_CreateGC:
                  return !tvb_bytes_exist(tvb, offset, 16) || length == 4 + numberOfBitSet(tvb, offset + 12, 4);

            case X_SetDashes:
                  return !tvb_bytes_exist(tvb, offset, 12) || length == 3 + rounded4(tvb_get_uint16(tvb, offset + 10, encoding));

            case X_SetClipRectangles:
            case X_PolySegment:
            case X_PolyRectangle:
            case X_PolyFillRectangle:
                  return length >= 3 && (length - 3) % 2 == 0;

            case X_CopyArea:
                  return length == 7;

            case X_CopyPlane:
            case X_CreateCursor:
            case X_CreateGlyphCursor:
                  return length == 8;

            case X_PolyPoint:
            case X_PolyLine:
            case X_FreeColors:
                  return length >= 3;

            case X_PolyArc:
            case X_PolyFillArc:
                  return length >= 3 && (length - 3) % 3 == 0;

            case X_FillPoly:
            case X_ImageText8:
                  return length >= 4;

            case X_PutImage:
                  return length >= 6;

            case X_GetImage:
            case X_RecolorCursor:
                  return length == 5;

            case X_PolyText8:
                  if (length < 4) return false;
                  return true; /* We don't perform many controls on this one */

            case X_PolyText16:
                  if (length < 4) return false;
                  return true; /* We don't perform many controls on this one */

            case X_ImageText16:
                  return length >= 4;

            case X_StoreColors:
                  return length > 2 && (length - 2) % 3 == 0;

            case X_StoreNamedColor:
                  return !tvb_bytes_exist(tvb, offset, 14) || length == 4 + rounded4(tvb_get_uint16(tvb, offset + 12, encoding));

            case X_QueryColors:
                  return length >= 2;

            case X_ChangeKeyboardMapping:
                  return !tvb_bytes_exist(tvb, offset, 6) || length == 2 + tvb_get_uint8(tvb, 1) * tvb_get_uint8(tvb, 5);

            case X_ChangeKeyboardControl:
                  return !tvb_bytes_exist(tvb, offset, 6) || length == 2 + numberOfBitSet(tvb, offset + 4, 2);

            case X_RotateProperties:
                  return !tvb_bytes_exist(tvb, offset, 10) || length == 3 + tvb_get_uint16(tvb, offset + 8, encoding);

            case X_SetPointerMapping:
                  return length == 1 + rounded4(tvb_get_uint8(tvb, 1));

            case X_SetModifierMapping:
                  return length == 1 + tvb_get_uint8(tvb, 1) * 2;

            case X_NoOperation:
                  return length >= 1;

            default:
                  return true;
      }
}

/* -1 means doesn't match, +1 means match, 0 means don't know */

static int x_endian_match(tvbuff_t *tvb, int encoding)
{
      int offset, nextoffset;
      int atLeastOne = 0;

      for(offset = 0; tvb_bytes_exist(tvb, offset, 4); offset = nextoffset) {
            int length;
            length = tvb_get_uint16(tvb, offset + 2, encoding);
            if (!length)
                return -1;
            nextoffset = offset + length * 4;
            if (!consistentWithOrder(length, tvb, offset, encoding))
                return -1;
            atLeastOne = 1;
      }
      return atLeastOne;
}

static unsigned
guess_byte_ordering(tvbuff_t *tvb, packet_info *pinfo,
                    x11_conv_data_t *state)
{
      /* With X the client gives the byte ordering for the protocol,
         and the port on the server tells us we're speaking X. */

      int le, be;
      unsigned decision;

      if (state->byte_order == ENC_BIG_ENDIAN)
            return ENC_BIG_ENDIAN;    /* known to be big-endian */
      else if (state->byte_order == ENC_LITTLE_ENDIAN)
            return ENC_LITTLE_ENDIAN; /* known to be little-endian */

      if (pinfo->srcport == pinfo->match_uint) {
            /*
             * This is a reply or event; we don't try to guess the
             * byte order on it for now.
             */
            return ENC_LITTLE_ENDIAN;
      }

      le = x_endian_match(tvb, ENC_LITTLE_ENDIAN);
      be = x_endian_match(tvb, ENC_BIG_ENDIAN);

      if (le == be) {
            /* We have no reason to believe it's little- rather than
               big-endian, so we guess the shortest length is the
               right one.
            */
            if (!tvb_bytes_exist(tvb, 0, 4))
                /* Not even a way to get the length. We're biased
                   toward little endianness here (essentially the
                   x86 world right now). Decoding won't go very far
                   anyway.
                */
                decision = ENC_LITTLE_ENDIAN;
            else {
                if (tvb_get_letohs(tvb, 2) <= tvb_get_ntohs(tvb, 2))
                      decision = ENC_LITTLE_ENDIAN;
                else
                      decision = ENC_BIG_ENDIAN;
            }
      } else {
            if (le >= be)
                  decision = ENC_LITTLE_ENDIAN;
            else
                  decision = ENC_BIG_ENDIAN;
      }

      if ((le < 0 && be > 0) || (le > 0 && be < 0)) {
            /*
             * Remember the decision.
             */
            state->byte_order = decision;
      }

      return decision;
}

/************************************************************************
 ***                                                                  ***
 ***              D E C O D I N G   O N E   P A C K E T               ***
 ***                                                                  ***
 ************************************************************************/

/*
 * Decode an initial connection request.
 */
static void dissect_x11_initial_conn(tvbuff_t *tvb, packet_info *pinfo,
    proto_tree *tree, x11_conv_data_t *state, unsigned byte_order)
{
      int offset = 0;
      int *offsetp = &offset;
      proto_item *ti;
      proto_tree *t;
      uint16_t auth_proto_name_length, auth_proto_data_length;
      int left;

      ti = proto_tree_add_item(tree, proto_x11, tvb, 0, -1, ENC_NA);
      proto_item_append_text(ti, ", Request, Initial connection request");
      t = proto_item_add_subtree(ti, ett_x11);

      CARD8(byte_order);
      proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
      *offsetp += 1;
      CARD16(protocol_major_version);
      CARD16(protocol_minor_version);
      auth_proto_name_length = CARD16(authorization_protocol_name_length);
      auth_proto_data_length = CARD16(authorization_protocol_data_length);
      proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
      *offsetp += 2;

      if (auth_proto_name_length != 0) {
            STRING8(authorization_protocol_name, auth_proto_name_length);
            offset = ROUND_LENGTH(offset);
      }

      if (auth_proto_data_length != 0) {
            STRING8(authorization_protocol_data, auth_proto_data_length);
            offset = ROUND_LENGTH(offset);
      }

      if ((left = tvb_reported_length_remaining(tvb, offset)) > 0)
            proto_tree_add_item(t, hf_x11_undecoded, tvb, offset, left,
                                ENC_NA);

      /*
       * This is the initial connection request...
       */
      state->iconn_frame = pinfo->num;

      /*
       * ...and we're expecting a reply to it.
       */
      state->sequencenumber = 0;
      wmem_map_insert(state->seqtable, GINT_TO_POINTER(state->sequencenumber),
                          (int *)INITIAL_CONN);
}

static void dissect_x11_initial_reply(tvbuff_t *tvb, packet_info *pinfo,
    proto_tree *tree, const char _U_ *sep, x11_conv_data_t *state,
    unsigned byte_order)
{
      int offset = 0, *offsetp = &offset, left;
      unsigned char success;
      int length_of_vendor;
      int length_of_reason;
      int number_of_formats_in_pixmap_formats;
      int number_of_screens_in_roots;
      int unused;
      proto_item *ti;
      proto_tree *t;

      ti = proto_tree_add_item(tree, proto_x11, tvb, 0, -1, ENC_NA);
      proto_item_append_text(ti, ", Reply, Initial connection reply");
      t = proto_item_add_subtree(ti, ett_x11);

      state->iconn_reply = pinfo->num;
      success = INT8(success);
      if (success) {
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            length_of_reason = 0;
      }
      else {
            length_of_reason = INT8(length_of_reason);
      }

      INT16(protocol_major_version);
      INT16(protocol_minor_version);
      INT16(replylength);
      if (success) {
            INT32(release_number);
            INT32(resource_id_base);
            INT32(resource_id_mask);
            INT32(motion_buffer_size);
            length_of_vendor = INT16(length_of_vendor);
            INT16(maximum_request_length);
            number_of_screens_in_roots = INT8(number_of_screens_in_roots);
            number_of_formats_in_pixmap_formats = INT8(number_of_formats_in_pixmap_formats);
            INT8(image_byte_order);
            INT8(bitmap_format_bit_order);
            INT8(bitmap_format_scanline_unit);
            INT8(bitmap_format_scanline_pad);
            INT8(min_keycode);
            INT8(max_keycode);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 4, ENC_NA);
            *offsetp += 4;
            STRING8(vendor, length_of_vendor);
            unused = (4 - (length_of_vendor % 4)) % 4;
            if (unused > 0) {
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, unused, ENC_NA);
                *offsetp += unused;
            }
            LISTofPIXMAPFORMAT(pixmap_format, number_of_formats_in_pixmap_formats);
            LISTofSCREEN(screen, number_of_screens_in_roots);
      } else {
            STRING8(reason, length_of_reason);
      }

      if ((left = tvb_reported_length_remaining(tvb, offset)) > 0)
            UNDECODED(left);

}

typedef struct x11_reply_info {
      const uint8_t minor;
      void (*dissect)(tvbuff_t *tvb, packet_info *pinfo, int *offsetp, proto_tree *t, unsigned byte_order);
} x11_reply_info;

typedef struct event_info {
      const char *name;
      void (*dissect)(tvbuff_t *tvb, int *offsetp, proto_tree *t, unsigned byte_order);
} x11_event_info;

typedef struct x11_generic_event_info {
      const uint16_t minor;
      void (*dissect)(tvbuff_t *tvb, int length, int *offsetp, proto_tree *t, unsigned byte_order);
} x11_generic_event_info;

static void set_handler(const char *name, void (*func)(tvbuff_t *tvb, packet_info *pinfo, int *offsetp, proto_tree *t, unsigned byte_order),
                        const char * const *errors,
                        const x11_event_info *event_info,
                        const x11_generic_event_info *genevent_info,
                        const x11_reply_info *reply_info)
{
      wmem_map_insert(extension_table, (void *)name, (void *)func);
      wmem_map_insert(error_table, (void *)name, (void *)errors);
      wmem_map_insert(event_table, (void *)name, (void *)event_info);
      if (genevent_info)
          wmem_map_insert(genevent_table, (void *)name, (void *)genevent_info);
      wmem_map_insert(reply_table, (void *)name, (void *)reply_info);
}

#include "x11-extension-errors.h"
#include "x11-extension-implementation.h"

static void tryExtension(int opcode, tvbuff_t *tvb, packet_info *pinfo, int *offsetp, proto_tree *t,
                         x11_conv_data_t *state, unsigned byte_order)
{
      const char *extension;
      void (*func)(tvbuff_t *tvb, packet_info *pinfo, int *offsetp, proto_tree *t, unsigned byte_order);

      extension = try_val_to_str(opcode, state->opcode_vals);
      if (!extension)
            return;

      func = (void (*)(tvbuff_t *, packet_info *, int *, proto_tree *, unsigned))wmem_map_lookup(extension_table, extension);
      if (func)
            func(tvb, pinfo, offsetp, t, byte_order);
}

static void tryExtensionReply(int opcode, tvbuff_t *tvb, packet_info *pinfo, int *offsetp, proto_tree *t,
                              x11_conv_data_t *state, unsigned byte_order)
{
      void (*func)(tvbuff_t *tvb, packet_info *pinfo, int *offsetp, proto_tree *t, unsigned byte_order);

      func = (void (*)(tvbuff_t *, packet_info *, int *, proto_tree *, unsigned))wmem_map_lookup(state->reply_funcs, GINT_TO_POINTER(opcode));
      if (func)
            func(tvb, pinfo, offsetp, t, byte_order);
      else
            REPLYCONTENTS_COMMON();
}

static void tryExtensionEvent(int event, tvbuff_t *tvb, int *offsetp, proto_tree *t,
                              x11_conv_data_t *state, unsigned byte_order)
{
      void (*func)(tvbuff_t *tvb, int *offsetp, proto_tree *t, unsigned byte_order);

      func = (void (*)(tvbuff_t *, int *, proto_tree *, unsigned))wmem_map_lookup(state->eventcode_funcs, GINT_TO_POINTER(event));
      if (func)
            func(tvb, offsetp, t, byte_order);
}

static void tryGenericExtensionEvent(tvbuff_t *tvb, int *offsetp, proto_tree *t,
                                     x11_conv_data_t *state, unsigned byte_order)
{
      const char *extname;
      int extension, length;

      extension = tvb_get_uint8(tvb, *offsetp);
      (*offsetp)++;
      extname = try_val_to_str(extension, state->opcode_vals);

      if (extname) {
          proto_tree_add_uint_format(t, hf_x11_extension, tvb, *offsetp, 1, extension, "extension: %d (%s)", extension, extname);
      } else {
          proto_tree_add_uint(t, hf_x11_extension, tvb, *offsetp, 1, extension);
      }

      CARD16(event_sequencenumber);

      length = REPLYLENGTH(eventlength);
      length = length * 4 + 32;
      *offsetp += 4;

      if (extname) {
          x11_generic_event_info *info;
          info = (x11_generic_event_info *)wmem_map_lookup(genevent_table, extname);

          if (info) {
              int i;

              int opcode = tvb_get_uint16(tvb, *offsetp, byte_order);

              for (i = 0; info[i].dissect != NULL; i++) {
                  if (info[i].minor == opcode) {
                      *offsetp += 2;
                      info[i].dissect(tvb, length, offsetp, t, byte_order);
                      return;
                  }
              }
          }
      }
      CARD16(minor_opcode);
}

static void register_extension(x11_conv_data_t *state, value_string *vals_p,
    int major_opcode, unsigned int first_event, unsigned int first_error)
{
      const char * const *error_string;
      x11_event_info *event_info;
      x11_reply_info *reply_info;
      int i;

      vals_p->value = major_opcode;

      error_string = (const char * const *)wmem_map_lookup(error_table, vals_p->strptr);
      while (error_string && *error_string && first_error <= LastExtensionError) {
            /* store string of extension error */
            for (i = 0; i <= LastExtensionError; i++) {
                  if (state->errorcode_vals[i].strptr == NULL) {
                        state->errorcode_vals[i].value = first_error;
                        state->errorcode_vals[i].strptr = *error_string;
                        break;
                  } else if (state->errorcode_vals[i].value == first_error) {
                        /* TODO: Warn about extensions stepping on each other */
                        state->errorcode_vals[i].strptr = *error_string;
                        break;
                  }
            }
            first_error++;
            error_string++;
      }

      event_info = (x11_event_info *)wmem_map_lookup(event_table, vals_p->strptr);
      while (event_info && event_info->name && first_event <= LastExtensionEvent) {
            /* store string of extension event */
            for (i = 0; i <= LastExtensionEvent; i++) {
                  if (state->eventcode_vals[i].strptr == NULL) {
                        state->eventcode_vals[i].value = first_event;
                        state->eventcode_vals[i].strptr = event_info->name;
                        break;
                  } else if (state->eventcode_vals[i].value == first_event) {
                        /* TODO: Warn about extensions stepping on each other */
                        state->eventcode_vals[i].strptr = event_info->name;
                        break;
                  }
            }

            /* store event decode function */
            wmem_map_insert(state->eventcode_funcs, GINT_TO_POINTER(first_event), (void *)event_info->dissect);

            first_event++;
            event_info++;
      }

      reply_info = (x11_reply_info *)wmem_map_lookup(reply_table, vals_p->strptr);
      if (reply_info)
            for (i = 0; reply_info[i].dissect; i++)
                  wmem_map_insert(state->reply_funcs,
                                      GINT_TO_POINTER(major_opcode | (reply_info[i].minor << 8)),
                                      (void *)reply_info[i].dissect);
}


static void dissect_x11_request(tvbuff_t *tvb, packet_info *pinfo,
    proto_tree *tree, const char *sep, x11_conv_data_t *state,
    unsigned byte_order)
{
      int offset = 0;
      int *offsetp = &offset;
      int query_ext_offset;
      int next_offset;
      proto_item *ti;
      proto_tree *t;
      int length, opcode, i;
      uint8_t v8, v8_2, v8_3;
      uint16_t v16;
      uint32_t v32;
      int left;
      char *name;

      query_ext_offset = 2; /* "opcode" and "unused" */

      length = tvb_get_uint16(tvb, query_ext_offset, byte_order) * 4;
      query_ext_offset += 2;

      if (length == 0) {
            /* BIG-REQUESTS extension */
            length = tvb_get_uint32(tvb, query_ext_offset, byte_order);
            if ((int64_t)length * 4 > INT32_MAX)
                return;
            length *= 4;
            query_ext_offset += 4;
      }

      if (length < 4) {
            /* Bogus message length? */
            return;
      }

      next_offset = offset + length;

      ti = proto_tree_add_item(tree, proto_x11, tvb, 0, -1, ENC_NA);
      t = proto_item_add_subtree(ti, ett_x11);

      if (!pinfo->fd->visited)
            ++state->sequencenumber;

      OPCODE();

      col_append_fstr(pinfo->cinfo, COL_INFO, "%s %s", sep,
                    val_to_str(opcode, state->opcode_vals,
                                "<Unknown opcode %d>"));

      proto_item_append_text(ti, ", Request, opcode: %d (%s)",
                             opcode, val_to_str(opcode, state->opcode_vals,
                                                "<Unknown opcode %d>"));

      /*
       * Does this request expect a reply?
       */
      switch(opcode) {

            case X_QueryExtension:

                  /* necessary processing even if tree == NULL */

                  v16 = tvb_get_uint16(tvb, query_ext_offset, byte_order);
                  query_ext_offset += 2;
                  /* Some unused bytes */
                  query_ext_offset += 2;
                  name = tvb_get_string_enc(wmem_file_scope(), tvb, query_ext_offset, v16, ENC_ASCII);

                  /* store string of extension, opcode will be set at reply */
                  i = 0;
                  while(i < MAX_OPCODES) {
                        if (state->opcode_vals[i].strptr == NULL) {
                              state->opcode_vals[i].strptr = name;
                              state->opcode_vals[i].value = -1;
                              wmem_map_insert(state->valtable,
                                                  GINT_TO_POINTER(state->sequencenumber),
                                                  (int *)&state->opcode_vals[i]);
                              break;
                        } else if (strcmp(state->opcode_vals[i].strptr,
                                          name) == 0) {
                              wmem_map_insert(state->valtable,
                                                  GINT_TO_POINTER(state->sequencenumber),
                                                  (int *)&state->opcode_vals[i]);
                              break;
                        }
                        i++;
                  }

                  /* QueryExtension expects a reply, fall through */
      /* FALLTHROUGH */
      case X_AllocColor:
      case X_AllocColorCells:
      case X_AllocColorPlanes:
      case X_AllocNamedColor:
      case X_GetAtomName:
      case X_GetFontPath:
      case X_GetGeometry:
      case X_GetImage:
      case X_GetInputFocus:
      case X_GetKeyboardControl:
      case X_GetKeyboardMapping:
      case X_GetModifierMapping:
      case X_GetMotionEvents:
      case X_GetPointerControl:
      case X_GetPointerMapping:
      case X_GetProperty:
      case X_GetScreenSaver:
      case X_GetSelectionOwner:
      case X_GetWindowAttributes:
      case X_GrabKeyboard:
      case X_GrabPointer:
      case X_InternAtom:
      case X_ListExtensions:
      case X_ListFonts:
      case X_ListFontsWithInfo:
      case X_ListHosts:
      case X_ListInstalledColormaps:
      case X_ListProperties:
      case X_LookupColor:
      case X_QueryBestSize:
      case X_QueryColors:
      case X_QueryFont:
      case X_QueryKeymap:
      case X_QueryPointer:
      case X_QueryTextExtents:
      case X_QueryTree:
      case X_SetModifierMapping:
      case X_SetPointerMapping:
      case X_TranslateCoords:
            /*
             * Those requests expect a reply.
             */
            wmem_map_insert(state->seqtable,
                                GINT_TO_POINTER(state->sequencenumber),
                                GINT_TO_POINTER(opcode));

            break;

      default:
            /*
             * With Extension, we don't know, so assume there could be one
             */
            if (opcode >= X_FirstExtension && opcode <= X_LastExtension) {
                  uint32_t minor;
                  minor = tvb_get_uint8(tvb, 1);

                  wmem_map_insert(state->seqtable,
                                      GINT_TO_POINTER(state->sequencenumber),
                                      GINT_TO_POINTER(opcode | (minor << 8)));
            }

            /*
             * No reply is expected from any other request.
             */
            break;
      }

      if (tree == NULL)
            return;

      switch(opcode) {

      case X_CreateWindow:
            CARD8(depth);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(wid);
            WINDOW(parent);
            INT16(x);
            INT16(y);
            CARD16(width);
            CARD16(height);
            CARD16(border_width);
            ENUM16(window_class);
            VISUALID(visual);
            windowAttributes(tvb, offsetp, t, byte_order);
            break;

      case X_ChangeWindowAttributes:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            windowAttributes(tvb, offsetp, t, byte_order);
            break;

      case X_GetWindowAttributes:
      case X_DestroyWindow:
      case X_DestroySubwindows:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            break;

      case X_ChangeSaveSet:
            ENUM8(save_set_mode);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            break;

      case X_ReparentWindow:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            WINDOW(parent);
            INT16(x);
            INT16(y);
            break;

      case X_MapWindow:
      case X_MapSubwindows:
      case X_UnmapWindow:
      case X_UnmapSubwindows:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            break;

      case X_ConfigureWindow:
            {
            uint16_t bitmask16;
            static int * const configure_window_mask_flags[] = {
                  &hf_x11_configure_window_mask_x,
                  &hf_x11_configure_window_mask_y,
                  &hf_x11_configure_window_mask_width,
                  &hf_x11_configure_window_mask_height,
                  &hf_x11_configure_window_mask_border_width,
                  &hf_x11_configure_window_mask_sibling,
                  &hf_x11_configure_window_mask_stack_mode,
                  NULL
            };

            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            proto_tree_add_item(t, hf_x11_window, tvb, *offsetp, 4, byte_order);
            *offsetp += 4;
            bitmask16 = tvb_get_uint16(tvb, *offsetp, byte_order);
            proto_tree_add_bitmask(t, tvb, *offsetp, hf_x11_configure_window_mask, ett_x11_configure_window_mask, configure_window_mask_flags, byte_order);
            *offsetp += 2;
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            if (bitmask16 & 0x0001) {
                proto_tree_add_item(t, hf_x11_x, tvb, *offsetp, 2, byte_order);
                *offsetp += 2;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
                *offsetp += 2;
            }
            if (bitmask16 & 0x0002) {
                proto_tree_add_item(t, hf_x11_y, tvb, *offsetp, 2, byte_order);
                *offsetp += 2;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
                *offsetp += 2;
            }
            if (bitmask16 & 0x0004) {
                proto_tree_add_item(t, hf_x11_width, tvb, *offsetp, 2, byte_order);
                *offsetp += 2;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
                *offsetp += 2;
            }
            if (bitmask16 & 0x0008) {
                proto_tree_add_item(t, hf_x11_height, tvb, *offsetp, 2, byte_order);
                *offsetp += 2;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
                *offsetp += 2;
            }
            if (bitmask16 & 0x0010) {
                proto_tree_add_item(t, hf_x11_border_width, tvb, *offsetp, 2, byte_order);
                *offsetp += 2;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
                *offsetp += 2;
            }
            if (bitmask16 & 0x0020) {
                proto_tree_add_item(t, hf_x11_sibling, tvb, *offsetp, 4, byte_order);
                *offsetp += 4;
            }
            if (bitmask16 & 0x0040) {
                proto_tree_add_item(t, hf_x11_stack_mode, tvb, *offsetp, 1, byte_order);
                *offsetp += 1;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
                *offsetp += 3;
            }
            if (next_offset - *offsetp > 0) {
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, next_offset - *offsetp, ENC_NA);
                *offsetp = next_offset;
            }
            }
            break;

      case X_CirculateWindow:
            ENUM8(direction);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            break;

      case X_GetGeometry:
      case X_QueryTree:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            break;

      case X_InternAtom:
            BOOL(only_if_exists);
            requestLength(tvb, offsetp, t, byte_order);
            v16 = FIELD16(name_length);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            STRING8(name, v16);
            PAD();
            break;

      case X_GetAtomName:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            ATOM(atom);
            break;

      case X_ChangeProperty:
            ENUM8(mode);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            ATOM(property);
            ATOM(type);
            v8 = CARD8(format);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
            *offsetp += 3;
            v32 = CARD32(data_length);
            switch (v8) {
            case 8:
                if (v32)
                    LISTofBYTE(data, v32);
                break;
            case 16:
                if (v32)
                    LISTofCARD16(data16, v32 * 2);
                break;
            case 32:
                if (v32)
                    LISTofCARD32(data32, v32 * 4);
                break;
            default:
                expert_add_info(pinfo, ti, &ei_x11_invalid_format);
                break;
            }
            PAD();
            break;

      case X_DeleteProperty:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            ATOM(property);
            break;

      case X_GetProperty:
            BOOL(delete);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            ATOM(property);
            ATOM(get_property_type);
            CARD32(long_offset);
            CARD32(long_length);
            break;

      case X_ListProperties:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            break;

      case X_SetSelectionOwner:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(owner);
            ATOM(selection);
            TIMESTAMP(time);
            break;

      case X_GetSelectionOwner:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            ATOM(selection);
            break;

      case X_ConvertSelection:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(requestor);
            ATOM(selection);
            ATOM(target);
            ATOM(property);
            TIMESTAMP(time);
            break;

      case X_SendEvent:
            BOOL(propagate);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(destination);
            SETofEVENT(event_mask);
            EVENT();
            break;

      case X_GrabPointer:
            BOOL(owner_events);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(grab_window);
            SETofPOINTEREVENT(pointer_event_mask);
            ENUM8(pointer_mode);
            ENUM8(keyboard_mode);
            WINDOW(confine_to);
            CURSOR(cursor);
            TIMESTAMP(time);
            break;

      case X_UngrabPointer:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            TIMESTAMP(time);
            break;

      case X_GrabButton:
            BOOL(owner_events);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(grab_window);
            SETofPOINTEREVENT(event_mask);
            ENUM8(pointer_mode);
            ENUM8(keyboard_mode);
            WINDOW(confine_to);
            CURSOR(cursor);
            BUTTON(button);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            SETofKEYMASK(modifiers);
            break;

      case X_UngrabButton:
            BUTTON(button);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(grab_window);
            SETofKEYMASK(modifiers);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            break;

      case X_ChangeActivePointerGrab:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            CURSOR(cursor);
            TIMESTAMP(time);
            SETofPOINTEREVENT(event_mask);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            break;

      case X_GrabKeyboard:
            BOOL(owner_events);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(grab_window);
            TIMESTAMP(time);
            ENUM8(pointer_mode);
            ENUM8(keyboard_mode);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            break;

      case X_UngrabKeyboard:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            TIMESTAMP(time);
            break;

      case X_GrabKey:
            BOOL(owner_events);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(grab_window);
            SETofKEYMASK(modifiers);
            KEYCODE(key);
            ENUM8(pointer_mode);
            ENUM8(keyboard_mode);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
            *offsetp += 3;
            break;

      case X_UngrabKey:
            KEYCODE(key);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(grab_window);
            SETofKEYMASK(modifiers);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            break;

      case X_AllowEvents:
            ENUM8(allow_events_mode);
            requestLength(tvb, offsetp, t, byte_order);
            TIMESTAMP(time);
            break;

      case X_GrabServer:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_UngrabServer:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_QueryPointer:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            break;

      case X_GetMotionEvents:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            TIMESTAMP(start);
            TIMESTAMP(stop);
            break;

      case X_TranslateCoords:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(src_window);
            WINDOW(dst_window);
            INT16(src_x);
            INT16(src_y);
            break;

      case X_WarpPointer:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(warp_pointer_src_window);
            WINDOW(warp_pointer_dst_window);
            INT16(src_x);
            INT16(src_y);
            CARD16(src_width);
            CARD16(src_height);
            INT16(dst_x);
            INT16(dst_y);
            break;

      case X_SetInputFocus:
            ENUM8(revert_to);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(focus);
            TIMESTAMP(time);
            break;

      case X_GetInputFocus:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_QueryKeymap:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_OpenFont:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            FONT(fid);
            v16 = FIELD16(name_length);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            STRING8(name, v16);
            PAD();
            break;

      case X_CloseFont:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            FONT(font);
            break;

      case X_QueryFont:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            FONTABLE(font);
            break;

      case X_QueryTextExtents:
            v8 = BOOL(odd_length);
            requestLength(tvb, offsetp, t, byte_order);
            FONTABLE(font);
            STRING16(string16, (next_offset - offset - (v8 ? 2 : 0)) / 2);
            PAD();
            break;

      case X_ListFonts:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            CARD16(max_names);
            v16 = FIELD16(pattern_length);
            STRING8(pattern, v16);
            PAD();
            break;

      case X_ListFontsWithInfo:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            CARD16(max_names);
            v16 = FIELD16(pattern_length);
            STRING8(pattern, v16);
            PAD();
            break;

      case X_SetFontPath:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            v16 = CARD16(str_number_in_path);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            LISTofSTRING8(path, v16);
            PAD();
            break;

      case X_GetFontPath:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_CreatePixmap:
            CARD8(depth);
            requestLength(tvb, offsetp, t, byte_order);
            PIXMAP(pid);
            DRAWABLE(drawable);
            CARD16(width);
            CARD16(height);
            break;

      case X_FreePixmap:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            PIXMAP(pixmap);
            break;

      case X_CreateGC:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            GCONTEXT(cid);
            DRAWABLE(drawable);
            gcAttributes(tvb, offsetp, t, byte_order);
            break;

      case X_ChangeGC:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            GCONTEXT(gc);
            gcAttributes(tvb, offsetp, t, byte_order);
            break;

      case X_CopyGC:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            GCONTEXT(src_gc);
            GCONTEXT(dst_gc);
            gcMask(tvb, offsetp, t, byte_order);
            break;

      case X_SetDashes:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            GCONTEXT(gc);
            CARD16(dash_offset);
            v16 = FIELD16(dashes_length);
            LISTofCARD8(dashes, v16);
            PAD();
            break;

      case X_SetClipRectangles:
            ENUM8(ordering);
            requestLength(tvb, offsetp, t, byte_order);
            GCONTEXT(gc);
            INT16(clip_x_origin);
            INT16(clip_y_origin);
            LISTofRECTANGLE(rectangles);
            break;

      case X_FreeGC:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            GCONTEXT(gc);
            break;

      case X_ClearArea:
            BOOL(exposures);
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            INT16(x);
            INT16(y);
            CARD16(width);
            CARD16(height);
            break;

      case X_CopyArea:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(src_drawable);
            DRAWABLE(dst_drawable);
            GCONTEXT(gc);
            INT16(src_x);
            INT16(src_y);
            INT16(dst_x);
            INT16(dst_y);
            CARD16(width);
            CARD16(height);
            break;

      case X_CopyPlane:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(src_drawable);
            DRAWABLE(dst_drawable);
            GCONTEXT(gc);
            INT16(src_x);
            INT16(src_y);
            INT16(dst_x);
            INT16(dst_y);
            CARD16(width);
            CARD16(height);
            CARD32(bit_plane);
            break;

      case X_PolyPoint:
            ENUM8(coordinate_mode);
            v16 = requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            LISTofPOINT(points, v16 - 12);
            break;

      case X_PolyLine:
            ENUM8(coordinate_mode);
            v16 = requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            LISTofPOINT(points, v16 - 12);
            break;

      case X_PolySegment:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            LISTofSEGMENT(segments);
            break;

      case X_PolyRectangle:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            LISTofRECTANGLE(rectangles);
            break;

      case X_PolyArc:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            LISTofARC(arcs);
            break;

      case X_FillPoly:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            v16 = requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            ENUM8(shape);
            ENUM8(coordinate_mode);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            LISTofPOINT(points, v16 - 16);
            break;

      case X_PolyFillRectangle:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            LISTofRECTANGLE(rectangles);
            break;

      case X_PolyFillArc:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            LISTofARC(arcs);
            break;

      case X_PutImage:
            ENUM8(image_format);
            v16 = requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            CARD16(width);
            CARD16(height);
            INT16(dst_x);
            INT16(dst_y);
            CARD8(left_pad);
            CARD8(depth);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            LISTofBYTE(data, v16 - 24);
            PAD();
            break;

      case X_GetImage:
            ENUM8(image_pixmap_format);
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            INT16(x);
            INT16(y);
            CARD16(width);
            CARD16(height);
            CARD32(plane_mask);
            break;

      case X_PolyText8:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            INT16(x);
            INT16(y);
            LISTofTEXTITEM8(items);
            PAD();
            break;

      case X_PolyText16:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            INT16(x);
            INT16(y);
            LISTofTEXTITEM16(items);
            PAD();
            break;

      case X_ImageText8:
            v8 = FIELD8(string_length);
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            INT16(x);
            INT16(y);
            STRING8(string, v8);
            PAD();
            break;

      case X_ImageText16:
            v8 = FIELD8(string_length);
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            GCONTEXT(gc);
            INT16(x);
            INT16(y);
            STRING16(string16, v8);
            PAD();
            break;

      case X_CreateColormap:
            ENUM8(alloc);
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(mid);
            WINDOW(window);
            VISUALID(visual);
            break;

      case X_FreeColormap:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            break;

      case X_CopyColormapAndFree:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(mid);
            COLORMAP(src_cmap);
            break;

      case X_InstallColormap:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            break;

      case X_UninstallColormap:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            break;

      case X_ListInstalledColormaps:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            break;

      case X_AllocColor:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            CARD16(red);
            CARD16(green);
            CARD16(blue);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            break;

      case X_AllocNamedColor:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            v16 = FIELD16(name_length);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            STRING8(name, v16);
            PAD();
            break;

      case X_AllocColorCells:
            BOOL(contiguous);
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            CARD16(colors);
            CARD16(planes);
            break;

      case X_AllocColorPlanes:
            BOOL(contiguous);
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            CARD16(colors);
            CARD16(reds);
            CARD16(greens);
            CARD16(blues);
            break;

      case X_FreeColors:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            v16 = requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            CARD32(plane_mask);
            LISTofCARD32(pixels, v16 - 12);
            break;

      case X_StoreColors:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            v16 = requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            LISTofCOLORITEM(color_items, v16 - 8);
            break;

      case X_StoreNamedColor:
            COLOR_FLAGS(color);
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            CARD32(pixel);
            v16 = FIELD16(name_length);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            STRING8(name, v16);
            PAD();
            break;

      case X_QueryColors:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            v16 = requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            LISTofCARD32(pixels, v16 - 8);
            break;

      case X_LookupColor:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            COLORMAP(cmap);
            v16 = FIELD16(name_length);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            STRING8(name, v16);
            PAD();
            break;

      case X_CreateCursor:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            CURSOR(cid);
            PIXMAP(source_pixmap);
            PIXMAP(mask);
            CARD16(fore_red);
            CARD16(fore_green);
            CARD16(fore_blue);
            CARD16(back_red);
            CARD16(back_green);
            CARD16(back_blue);
            CARD16(x);
            CARD16(y);
            break;

      case X_CreateGlyphCursor:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            CURSOR(cid);
            FONT(source_font);
            FONT(mask_font);
            CARD16(source_char);
            CARD16(mask_char);
            CARD16(fore_red);
            CARD16(fore_green);
            CARD16(fore_blue);
            CARD16(back_red);
            CARD16(back_green);
            CARD16(back_blue);
            break;

      case X_FreeCursor:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            CURSOR(cursor);
            break;

      case X_RecolorCursor:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            CURSOR(cursor);
            CARD16(fore_red);
            CARD16(fore_green);
            CARD16(fore_blue);
            CARD16(back_red);
            CARD16(back_green);
            CARD16(back_blue);
            break;

      case X_QueryBestSize:
            ENUM8(class);
            requestLength(tvb, offsetp, t, byte_order);
            DRAWABLE(drawable);
            CARD16(width);
            CARD16(height);
            break;

      case X_QueryExtension:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            v16 = FIELD16(name_length);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            STRING8(name, v16);
            PAD();
            break;

      case X_ListExtensions:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_ChangeKeyboardMapping:
            v8 = FIELD8(keycode_count);
            requestLength(tvb, offsetp, t, byte_order);
            v8_2 = KEYCODE(first_keycode);
            v8_3 = FIELD8(keysyms_per_keycode);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            LISTofKEYSYM(keysyms, state->keycodemap, v8_2, v8, v8_3);
            break;

      case X_GetKeyboardMapping:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            state->request.GetKeyboardMapping.first_keycode
            = KEYCODE(first_keycode);
            FIELD8(count);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            break;

      case X_ChangeKeyboardControl:
            {
            uint32_t bitmask32;
            static int * const keyboard_value_flags[] = {
                  &hf_x11_keyboard_value_mask_key_click_percent,
                  &hf_x11_keyboard_value_mask_bell_percent,
                  &hf_x11_keyboard_value_mask_bell_pitch,
                  &hf_x11_keyboard_value_mask_bell_duration,
                  &hf_x11_keyboard_value_mask_led,
                  &hf_x11_keyboard_value_mask_led_mode,
                  &hf_x11_keyboard_value_mask_keyboard_key,
                  &hf_x11_keyboard_value_mask_auto_repeat_mode,
                  NULL
            };

            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            bitmask32 = tvb_get_uint32(tvb, *offsetp, byte_order);
            proto_tree_add_bitmask(t, tvb, *offsetp, hf_x11_keyboard_value_mask, ett_x11_keyboard_value_mask, keyboard_value_flags, byte_order);
            *offsetp += 4;
            if (bitmask32 & 0x00000001) {
                proto_tree_add_item(t, hf_x11_key_click_percent, tvb, *offsetp, 1, byte_order);
                *offsetp += 1;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
                *offsetp += 3;
            }
            if (bitmask32 & 0x00000002) {
                proto_tree_add_item(t, hf_x11_bell_percent, tvb, *offsetp, 1, byte_order);
                *offsetp += 1;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
                *offsetp += 3;
            }
            if (bitmask32 & 0x00000004) {
                proto_tree_add_item(t, hf_x11_bell_pitch, tvb, *offsetp, 2, byte_order);
                *offsetp += 2;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
                *offsetp += 2;
            }
            if (bitmask32 & 0x00000008) {
                proto_tree_add_item(t, hf_x11_bell_duration, tvb, *offsetp, 2, byte_order);
                *offsetp += 2;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
                *offsetp += 2;
            }
            if (bitmask32 & 0x00000010) {
                proto_tree_add_item(t, hf_x11_led, tvb, *offsetp, 2, byte_order);
                *offsetp += 2;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
                *offsetp += 2;
            }
            if (bitmask32 & 0x00000020) {
                proto_tree_add_item(t, hf_x11_led_mode, tvb, *offsetp, 1, byte_order);
                *offsetp += 1;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
                *offsetp += 3;
            }
            if (bitmask32 & 0x00000040) {
                proto_tree_add_item(t, hf_x11_keyboard_key, tvb, *offsetp, 1, byte_order);
                *offsetp += 1;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
                *offsetp += 3;
            }
            if (bitmask32 & 0x00000080) {
                proto_tree_add_item(t, hf_x11_auto_repeat_mode, tvb, *offsetp, 1, byte_order);
                *offsetp += 1;
                proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 3, ENC_NA);
                *offsetp += 3;
            }
            }
            break;

      case X_GetKeyboardControl:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_Bell:
            INT8(percent);
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_ChangePointerControl:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            INT16(acceleration_numerator);
            INT16(acceleration_denominator);
            INT16(threshold);
            BOOL(do_acceleration);
            BOOL(do_threshold);
            break;

      case X_GetPointerControl:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_SetScreenSaver:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            INT16(timeout);
            INT16(interval);
            ENUM8(prefer_blanking);
            ENUM8(allow_exposures);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
            *offsetp += 2;
            break;

      case X_GetScreenSaver:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_ChangeHosts:
            ENUM8(change_host_mode);
            requestLength(tvb, offsetp, t, byte_order);
            v8 = ENUM8(family);
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            v16 = CARD16(address_length);
            if (v8 == FAMILY_INTERNET && v16 == 4) {
                  /*
                   * IPv4 addresses.
                   * XXX - what about IPv6?  Is that a family of
                   * FAMILY_INTERNET (0) with a length of 16?
                   */
                  LISTofIPADDRESS(ip_address, v16);
            } else
                  LISTofCARD8(address, v16);
            break;

      case X_ListHosts:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_SetAccessControl:
            ENUM8(access_mode);
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_SetCloseDownMode:
            ENUM8(close_down_mode);
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_KillClient:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            CARD32(resource);
            break;

      case X_RotateProperties:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            v16 = requestLength(tvb, offsetp, t, byte_order);
            WINDOW(window);
            CARD16(property_number);
            INT16(delta);
            LISTofATOM(properties, (v16 - 12));
            break;

      case X_ForceScreenSaver:
            ENUM8(screen_saver_mode);
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_SetPointerMapping:
            v8 = FIELD8(map_length);
            requestLength(tvb, offsetp, t, byte_order);
            LISTofCARD8(map, v8);
            PAD();
            break;

      case X_GetPointerMapping:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_SetModifierMapping:
            v8 = FIELD8(keycodes_per_modifier);
            requestLength(tvb, offsetp, t, byte_order);
            LISTofKEYCODE(state->modifiermap, keycodes, v8);
            break;

      case X_GetModifierMapping:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;

      case X_NoOperation:
            proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
            *offsetp += 1;
            requestLength(tvb, offsetp, t, byte_order);
            break;
      default:
            tryExtension(opcode, tvb, pinfo, offsetp, t, state, byte_order);
            break;
      }

      if ((left = tvb_reported_length_remaining(tvb, offset)) > 0)
            UNDECODED(left);
}

static void dissect_x11_requests(tvbuff_t *tvb, packet_info *pinfo,
    proto_tree *tree)
{
      volatile int offset = 0;
      int length_remaining;
      volatile unsigned byte_order;
      uint8_t opcode;
      volatile int plen;
      proto_item *ti;
      volatile bool is_initial_creq;
      uint16_t auth_proto_len, auth_data_len;
      const char *volatile sep = NULL;
      conversation_t *conversation;
      x11_conv_data_t *volatile state;
      int length;
      tvbuff_t *volatile next_tvb;

      while ((length_remaining = tvb_reported_length_remaining(tvb, offset)) > 0) {

            /*
             * Can we do reassembly?
             */
            if (x11_desegment && pinfo->can_desegment) {
                  /*
                   * Yes - is the X11 request header split across
                   * segment boundaries?
                   */
                  if (length_remaining < 4) {
                        /*
                         * Yes.  Tell the TCP dissector where the data
                         * for this message starts in the data it handed
                         * us and that we need "some more data."  Don't tell
                         * it exactly how many bytes we need because if/when
                         * we ask for even more (after the header) that will
                         * break reassembly.
                         */
                        pinfo->desegment_offset = offset;
                        pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
                        return;
                  }
            }

            /*
             * Get the state for this conversation; create the conversation
             * if we don't have one, and create the state if we don't have
             * any.
             */
            conversation = find_or_create_conversation(pinfo);

            /*
             * Is there state attached to this conversation?
             */
            if ((state = (x11_conv_data_t *)conversation_get_proto_data(conversation, proto_x11))
            == NULL)
                state = x11_stateinit(conversation);

            /*
             * Guess the byte order if we don't already know it.
             */
            byte_order = guess_byte_ordering(tvb, pinfo, state);

            /*
             * Get the opcode and length of the putative X11 request.
             */
            opcode = tvb_get_uint8(tvb, 0);
            plen = tvb_get_uint16(tvb, offset + 2, byte_order);

            if (plen == 0) {
                  /*
                   * A length field of 0 indicates that the BIG-REQUESTS
                   * extension is used: The next four bytes are the real length.
                   */
                  plen = tvb_get_uint32(tvb, offset + 4, byte_order);
            }

            if (plen <= 0) {
                  /*
                   * This can't be less then 0, as it includes the header length.
                   * A different choice of byte order wouldn't have
                   * helped.
                   * Give up.
                   */
                  ti = proto_tree_add_item(tree, proto_x11, tvb, offset, -1, ENC_NA);
                  expert_add_info_format(pinfo, ti, &ei_x11_request_length, "Bogus request length (%d)", plen);
                  return;
            }

            if (state->iconn_frame == pinfo->num ||
                (wmem_map_lookup(state->seqtable,
                GINT_TO_POINTER(state->sequencenumber)) == (int *)NOTHING_SEEN &&
                 (opcode == 'B' || opcode == 'l') &&
                 (plen == 11 || plen == 2816))) {
                  /*
                   * Either
                   *
                   *    we saw this on the first pass and this is
                   *    it again
                   *
                   * or
                   *    we haven't already seen any requests, the first
                   *    byte of the message is 'B' or 'l', and the 16-bit
                   *    integer 2 bytes into the data stream is either 11
                   *    or a byte-swapped 11.
                   *
                   * This means it's probably an initial connection
                   * request, not a message.
                   *
                   * 'B' is decimal 66, which is the opcode for a
                   * PolySegment request; unfortunately, 11 is a valid
                   * length for a PolySegment request request, so we
                   * might mis-identify that request.  (Are there any
                   * other checks we can do?)
                   *
                   * 'l' is decimal 108, which is the opcode for a
                   * GetScreenSaver request; the only valid length
                   * for that request is 1.
                   */
                  is_initial_creq = true;

                  /*
                   * We now know the byte order.  Override the guess.
                   */
                  if (state->byte_order == BYTE_ORDER_UNKNOWN) {
                        if (opcode == 'B') {
                              /*
                               * Big-endian.
                               */
                              byte_order = state->byte_order = ENC_BIG_ENDIAN;
                        } else {
                              /*
                               * Little-endian.
                               */
                              byte_order = state->byte_order = ENC_LITTLE_ENDIAN;
                        }
                  }

                  /*
                   * Can we do reassembly?
                   */
                  if (x11_desegment && pinfo->can_desegment) {
                        /*
                         * Yes - is the fixed-length portion of the
                         * initial connection header split across
                         * segment boundaries?
                         */
                        if (length_remaining < 10) {
                              /*
                               * Yes.  Tell the TCP dissector where the
                               * data for this message starts in the data
                               * it handed us and that we need "some more
                               * data."  Don't tell it exactly how many bytes
                               * we need because if/when we ask for even more
                               * (after the header) that will break reassembly.
                               */
                              pinfo->desegment_offset = offset;
                              pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
                              return;
                        }
                  }

                  /*
                   * Get the lengths of the authorization protocol and
                   * the authorization data.
                   */
                  auth_proto_len = tvb_get_uint16(tvb, offset + 6, byte_order);
                  auth_data_len = tvb_get_uint16(tvb, offset + 8, byte_order);
                  plen = 12 + ROUND_LENGTH(auth_proto_len) +
                        ROUND_LENGTH(auth_data_len);
            } else {
                  volatile int64_t tmp = (int64_t)plen * 4;
                  /*
                   * This is probably an ordinary request.
                   */
                  is_initial_creq = false;

                  /*
                   * The length of a request is in 4-byte words.
                   */
                  if (tmp > INT32_MAX) {
                        ti = proto_tree_add_item(tree, proto_x11, tvb, offset, -1, ENC_NA);
                        expert_add_info_format(pinfo, ti, &ei_x11_request_length, "Bogus request length (%"PRId64")", tmp);
                        return;
                  }
                  plen = (int)tmp;
            }

            /*
             * Can we do reassembly?
             */
            if (x11_desegment && pinfo->can_desegment) {
                  /*
                   * Yes - is the X11 request split across segment
                   * boundaries?
                   */
                  if (length_remaining < plen) {
                        /*
                         * Yes.  Tell the TCP dissector where the data
                         * for this message starts in the data it handed
                         * us, and how many more bytes we need, and return.
                         */
                        pinfo->desegment_offset = offset;
                        pinfo->desegment_len = plen - length_remaining;
                        return;
                  }
            }

            /*
             * Construct a tvbuff containing the amount of the payload
             * we have available.  Make its reported length the
             * amount of data in the X11 request.
             *
             * XXX - if reassembly isn't enabled. the subdissector
             * will throw a BoundsError exception, rather than a
             * ReportedBoundsError exception.  We really want a tvbuff
             * where the length is "length", the reported length is "plen",
             * and the "if the snapshot length were infinite" length is the
             * minimum of the reported length of the tvbuff handed to us
             * and "plen", with a new type of exception thrown if the offset
             * is within the reported length but beyond that third length,
             * with that exception getting the "Unreassembled Packet" error.
             */
            length = length_remaining;
            if (length > plen)
                  length = plen;
            next_tvb = tvb_new_subset_length_caplen(tvb, offset, length, plen);

            /*
             * Set the column appropriately.
             */
            if (is_initial_creq) {
                  col_set_str(pinfo->cinfo, COL_INFO, "Initial connection request");
            } else {
                  if (sep == NULL) {
                        /*
                         * We haven't set the column yet; set it.
                         */
                        col_set_str(pinfo->cinfo, COL_INFO, "Requests");

                        /*
                         * Initialize the separator.
                         */
                        sep = ":";
                  }
            }

            /*
             * Dissect the X11 request.
             *
             * Catch the ReportedBoundsError exception; if this
             * particular message happens to get a ReportedBoundsError
             * exception, that doesn't mean that we should stop
             * dissecting X11 requests within this frame or chunk of
             * reassembled data.
             *
             * If it gets a BoundsError, we can stop, as there's nothing
             * more to see, so we just re-throw it.
             */
            TRY {
                  if (is_initial_creq) {
                        dissect_x11_initial_conn(next_tvb, pinfo, tree,
                            state, byte_order);
                  } else {
                        dissect_x11_request(next_tvb, pinfo, tree, sep,
                            state, byte_order);
                  }
            }
            CATCH_NONFATAL_ERRORS {
                  show_exception(tvb, pinfo, tree, EXCEPT_CODE, GET_MESSAGE);
            }
            ENDTRY;

            /*
             * Skip the X11 message.
             */
            offset += plen;

            sep = ",";
      }
}

static x11_conv_data_t *
x11_stateinit(conversation_t *conversation)
{
      x11_conv_data_t *state;
      static x11_conv_data_t stateinit;
      int i;

      state = wmem_new(wmem_file_scope(), x11_conv_data_t);
      *state = stateinit;

      /* initialise opcodes */
      for (i = 0; opcode_vals[i].strptr != NULL; i++) {
            state->opcode_vals[i].value = opcode_vals[i].value;
            state->opcode_vals[i].strptr = opcode_vals[i].strptr;
      }
      for (; i <= MAX_OPCODES; i++) {
            state->opcode_vals[i].value = 0;
            state->opcode_vals[i].strptr = NULL;
      }

      /* initialise errorcodes */
      for (i = 0; errorcode_vals[i].strptr != NULL; i++) {
            state->errorcode_vals[i].value = errorcode_vals[i].value;
            state->errorcode_vals[i].strptr = errorcode_vals[i].strptr;
      }
      for (; i <= LastExtensionError + 1; i++) {
            state->errorcode_vals[i].value = 0;
            state->errorcode_vals[i].strptr = NULL;
      }

      /* initialise eventcodes */
      for (i = 0; eventcode_vals[i].strptr != NULL; i++) {
            state->eventcode_vals[i].value = eventcode_vals[i].value;
            state->eventcode_vals[i].strptr = eventcode_vals[i].strptr;
      }
      for (; i <= LastExtensionEvent + 1; i++) {
            state->eventcode_vals[i].value = 0;
            state->eventcode_vals[i].strptr = NULL;
      }
      state->eventcode_funcs = wmem_map_new(wmem_file_scope(), g_direct_hash, g_direct_equal);
      state->reply_funcs = wmem_map_new(wmem_file_scope(), g_direct_hash, g_direct_equal);

      state->seqtable = wmem_map_new(wmem_file_scope(), g_direct_hash, g_direct_equal);
      state->valtable = wmem_map_new(wmem_file_scope(), g_direct_hash, g_direct_equal);
      wmem_map_insert(state->seqtable, (int *)0, (int *)NOTHING_SEEN);
      state->byte_order = BYTE_ORDER_UNKNOWN; /* don't know yet*/
      conversation_add_proto_data(conversation, proto_x11, state);
      return state;
}


static void
dissect_x11_replies(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
/* Set up structures we will need to add the protocol subtree and manage it */
      volatile int offset, plen;
      tvbuff_t *volatile next_tvb;
      conversation_t *conversation;
      x11_conv_data_t *volatile state;
      volatile unsigned byte_order;
      int length_remaining;
      const char *volatile sep = NULL;


      /*
       * Get the state for this conversation; create the conversation
       * if we don't have one, and create the state if we don't have
       * any.
       */
      conversation = find_or_create_conversation(pinfo);

      /*
       * Is there state attached to this conversation?
       */
      if ((state = (x11_conv_data_t *)conversation_get_proto_data(conversation, proto_x11))
          == NULL) {
            /*
             * No - create a state structure and attach it.
             */
            state = x11_stateinit(conversation);
      }

      /*
       * Guess the byte order if we don't already know it.
       */
      byte_order = guess_byte_ordering(tvb, pinfo, state);

      offset = 0;
      while ((length_remaining = tvb_reported_length_remaining(tvb, offset)) > 0) {
            /*
             * Can we do reassembly?
             */
            if (x11_desegment && pinfo->can_desegment) {
                  /*
                   * Yes - is the X11 Reply or GenericEvent header split across
                   * segment boundaries?
                   */
                  if (length_remaining < 8) {
                        /*
                         * Yes.  Tell the TCP dissector where the data
                         * for this message starts in the data it handed
                         * us and that we need "some more data."  Don't tell
                         * it exactly how many bytes we need because if/when
                         * we ask for even more (after the header) that will
                         * break reassembly.
                         */
                        pinfo->desegment_offset = offset;
                        pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
                        return;
                  }
            }

            /*
             * Find out what kind of a reply it is.
             * There are four possible:
             *  - reply to initial connection
             *  - errorreply (a request generated an error)
             *  - requestreply (reply to a request)
             *  - event (some event occurred)
             */
            if (wmem_map_lookup(state->seqtable,
                                    GINT_TO_POINTER(state->sequencenumber)) == (int *)INITIAL_CONN
                || (state->iconn_reply == pinfo->num)) {
                  /*
                   * Either the connection is in the "initial
                   * connection" state, or this frame is known
                   * to have the initial connection reply.
                   * That means this is the initial connection
                   * reply.
                   */
                  plen = 8 + tvb_get_uint16(tvb, offset + 6, byte_order) * 4;

                  HANDLE_REPLY(plen, length_remaining,
                               "Initial connection reply",
                               dissect_x11_initial_reply);
            } else {
                  /*
                   * This isn't an initial connection reply
                   * (XXX - unless we missed the initial
                   * connection request).  Look at the first
                   * byte to determine what it is; errors
                   * start with a byte of 0, replies start
                   * with a byte of 1, events start with
                   * a byte with of 2 or greater.
                   */
                  switch (tvb_get_uint8(tvb, offset)) {

                        case 0:
                              plen = 32;
                              HANDLE_REPLY(plen, length_remaining,
                                           "Error", dissect_x11_error);
                              break;

                        case 1:
                        {
                              /* To avoid an "assert w/side-effect" warning,
                               * use a non-volatile temp variable instead. */
                              int tmp_plen;

                              /* replylength is in units of four. */
                              tmp_plen = plen = 32 + tvb_get_uint32(tvb, offset + 4, byte_order) * 4;
                              /* If tmp_plen < 32, we got an overflow;
                               * the reply length is too long. */
                              THROW_ON(tmp_plen < 32, ReportedBoundsError);
                              HANDLE_REPLY(plen, length_remaining,
                                           "Reply", dissect_x11_reply);
                              break;
                        }

                        case GenericEvent:
                        {
                              /* An Event, but with a length field like a Reply. */

                              /* To avoid an "assert w/side-effect" warning,
                               * use a non-volatile temp variable instead. */
                              int tmp_plen;

                              /* GenericEvent's length is also in units of four. */
                              tmp_plen = plen = 32 + tvb_get_uint32(tvb, offset + 4, byte_order) * 4;
                              /* If tmp_plen < 32, we got an overflow;
                               * the event length is too long. */
                              THROW_ON(tmp_plen < 32, ReportedBoundsError);
                              HANDLE_REPLY(plen, length_remaining,
                                           "Event", dissect_x11_event);
                              break;
                        }

                        default:
                              /* Event */
                              plen = 32;
                              HANDLE_REPLY(plen, length_remaining,
                                           "Event", dissect_x11_event);
                              break;
                  }
            }

            offset += plen;
      }

      return;
}

static void
dissect_x11_reply(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                  const char *sep, x11_conv_data_t *state,
                  unsigned byte_order)
{
      int offset = 0, *offsetp = &offset, length, left, opcode;
      int major_opcode, sequence_number, first_error, first_event;
      value_string *vals_p;
      proto_item *ti;
      proto_tree *t;

      ti = proto_tree_add_item(tree, proto_x11, tvb, 0, -1, ENC_NA);
      t = proto_item_add_subtree(ti, ett_x11);


      /*
       * XXX - this doesn't work correctly if either
       *
       *        1) the request sequence number wraps in the lower 16
       *           bits;
       *
       *        2) we don't see the initial connection request and the
       *         resynchronization of sequence number fails and thus
       *           don't have the right sequence numbers
       *
       *        3) we don't have all the packets in the capture and
       *           get out of sequence.
       *
       * We might, instead, want to assume that a reply is a reply to
       * the most recent not-already-replied-to request in the same
       * connection.  That also might mismatch replies to requests if
       * packets are lost, but there's nothing you can do to fix that.
       */

      sequence_number = tvb_get_uint16(tvb, offset + 2, byte_order);
      opcode = GPOINTER_TO_INT(wmem_map_lookup(state->seqtable,
                                                   GINT_TO_POINTER(sequence_number)));

      if (state->iconn_frame == 0 &&  state->resync == false) {

            /*
             * We don't see the initial connection request and no
             * resynchronization has been performed yet (first reply),
             * set the current sequence number to the one of the
             * current reply (this is only performed once).
             */
            state->sequencenumber = sequence_number;
            state->resync = true;
      }

      if (opcode == UNKNOWN_OPCODE) {
            col_append_fstr(pinfo->cinfo, COL_INFO,
                            "%s to unknown request", sep);
            proto_item_append_text(ti, ", Reply to unknown request");
      } else {
            col_append_fstr(pinfo->cinfo, COL_INFO, "%s %s",
                            sep,
                            val_to_str(opcode & 0xFF, state->opcode_vals,
                                        "<Unknown opcode %d>"));

            if (opcode > 0xFF)
                  proto_item_append_text(ti, ", Reply, opcode: %d.%d (%s)",
                                         opcode & 0xFF, opcode >> 8, val_to_str(opcode & 0xFF,
                                                                                state->opcode_vals,
                                                                                "<Unknown opcode %d>"));
            else
                  proto_item_append_text(ti, ", Reply, opcode: %d (%s)",
                                         opcode, val_to_str(opcode,
                                                            state->opcode_vals,
                                                            "<Unknown opcode %d>"));
      }

      switch (opcode) {

            /*
             * Replies that need special processing outside tree
             */

            case X_QueryExtension:

                  /*
                   * if extension is present and request is known:
                   * store opcode of extension in value_string of
                   * opcodes
                   */
                  if (!tvb_get_uint8(tvb, offset + 8)) {
                        /* not present */
                        break;
                  }

                  vals_p = (value_string *)wmem_map_lookup(state->valtable,
                                               GINT_TO_POINTER(sequence_number));
                  if (vals_p != NULL) {
                        major_opcode = tvb_get_uint8(tvb, offset + 9);
                        first_event = tvb_get_uint8(tvb, offset + 10);
                        first_error = tvb_get_uint8(tvb, offset + 11);

                        register_extension(state, vals_p, major_opcode, first_event, first_error);
                        wmem_map_remove(state->valtable,
                                            GINT_TO_POINTER(sequence_number));
                  }
                  break;

            default:
                  break;
      }

      if (tree == NULL)
            return;

      switch (opcode) {
            /*
             * Requests that expect a reply.
             */

            case X_GetWindowAttributes:
                  REPLYCONTENTS_COMMON();
                  break;

            case X_GetGeometry:
                  REPLY(reply);
                  CARD8(depth);
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  WINDOW(rootwindow);
                  INT16(x);
                  INT16(y);
                  CARD16(width);
                  CARD16(height);
                  CARD16(border_width);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 10, ENC_NA);
                  *offsetp += 10;
                  break;

            case X_QueryTree:
                  REPLYCONTENTS_COMMON();
                  break;

            case X_InternAtom:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  ATOM(atom);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 20, ENC_NA);
                  *offsetp += 20;
                  break;

            case X_GetAtomName:
                  REPLYCONTENTS_COMMON();
                  break;

            case X_GetProperty:
                  REPLY(reply);
                  CARD8(format);
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  ATOM(get_property_type);
                  CARD32(bytes_after);
                  CARD32(valuelength);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 12, ENC_NA);
                  *offsetp += 12;
                  break;

            case X_ListProperties:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  length = CARD16(property_number);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 22, ENC_NA);
                  *offsetp += 22;
                  LISTofATOM(properties, length*4);
                  break;

            case X_GetSelectionOwner:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  WINDOW(owner);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 20, ENC_NA);
                  *offsetp += 20;
                  break;

            case X_GrabPointer:
            case X_GrabKeyboard:
                  REPLY(reply);
                  ENUM8(grab_status);
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 24, ENC_NA);
                  *offsetp += 24;
                  break;

            case X_QueryPointer:
                  REPLY(reply);
                  BOOL(same_screen);
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  WINDOW(rootwindow);
                  WINDOW(childwindow);
                  INT16(root_x);
                  INT16(root_y);
                  INT16(win_x);
                  INT16(win_y);
                  SETofKEYBUTMASK(mask);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 6, ENC_NA);
                  *offsetp += 6;
                  break;

            case X_GetMotionEvents:
                  REPLYCONTENTS_COMMON();
                  break;

            case X_TranslateCoords:
                  REPLY(reply);
                  BOOL(same_screen);
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  WINDOW(childwindow);
                  INT16(dst_x);
                  INT16(dst_y);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 16, ENC_NA);
                  *offsetp += 16;
                  break;

            case X_GetInputFocus:
                  REPLY(reply);
                  ENUM8(revert_to);
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  WINDOW(focus);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 20, ENC_NA);
                  *offsetp += 20;
                  break;

            case X_QueryKeymap:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  LISTofCARD8(keys, 32);
                  break;

            case X_QueryFont:
            case X_QueryTextExtents:
            case X_ListFonts:
            case X_GetImage:
            case X_ListInstalledColormaps:
                  REPLYCONTENTS_COMMON();
                  break;

            case X_AllocColor:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  CARD16(red);
                  CARD16(green);
                  CARD16(blue);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);
                  *offsetp += 2;
                  CARD32(pixel);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 12, ENC_NA);
                  *offsetp += 12;
                  break;

            case X_QueryColors:
                  REPLYCONTENTS_COMMON();
                  break;

            case X_LookupColor:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  CARD16(exact_red);
                  CARD16(exact_green);
                  CARD16(exact_blue);
                  CARD16(visual_red);
                  CARD16(visual_green);
                  CARD16(visual_blue);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 12, ENC_NA);
                  *offsetp += 12;
                  break;

            case X_QueryBestSize:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  CARD16(width);
                  CARD16(height);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 20, ENC_NA);
                  *offsetp += 20;
                  break;

            case X_QueryExtension:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  BOOL(present);
                  CARD8(major_opcode);
                  CARD8(first_event);
                  CARD8(first_error);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 20, ENC_NA);
                  *offsetp += 20;
                  break;

            case X_ListExtensions:
                  REPLYCONTENTS_COMMON();
                  break;

            case X_GetKeyboardMapping:
                  state->first_keycode =
                        state->request.GetKeyboardMapping.first_keycode;
                  REPLY(reply);
                  state->keysyms_per_keycode =
                        FIELD8(keysyms_per_keycode);
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  length = REPLYLENGTH(replylength);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 24, ENC_NA);
                  *offsetp += 24;
                  LISTofKEYSYM(keysyms, state->keycodemap,
                               state->request.GetKeyboardMapping.first_keycode,
                               /* XXX - length / state->keysyms_per_keycode can raise a division by zero,
                                * don't know if this is the *right* way to fix it ... */
                               state->keysyms_per_keycode ? length / state->keysyms_per_keycode : 0,
                               state->keysyms_per_keycode);
                  break;

            case X_GetKeyboardControl:
                  REPLYCONTENTS_COMMON();
                  break;

            case X_GetPointerControl:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  CARD16(acceleration_numerator);
                  CARD16(acceleration_denominator);
                  CARD16(threshold);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 18, ENC_NA);
                  *offsetp += 18;
                  break;

            case X_GetScreenSaver:
                  REPLY(reply);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  CARD16(timeout);
                  CARD16(interval);
                  ENUM8(prefer_blanking);
                  ENUM8(allow_exposures);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 18, ENC_NA);
                  *offsetp += 18;
                  break;

            case X_ListHosts:
            case X_SetPointerMapping:
            case X_GetPointerMapping:
            case X_SetModifierMapping:
                  REPLYCONTENTS_COMMON();
                  break;

            case X_GetModifierMapping:
                  REPLY(reply);
                  state->keycodes_per_modifier =
                        FIELD8(keycodes_per_modifier);
                  SEQUENCENUMBER_REPLY(sequencenumber);
                  REPLYLENGTH(replylength);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 24, ENC_NA);
                  *offsetp += 24;
                  LISTofKEYCODE(state->modifiermap, keycodes,
                                state->keycodes_per_modifier);
                  break;

            case UNKNOWN_OPCODE:
                  REPLYCONTENTS_COMMON();
                  break;

            default:
                  tryExtensionReply(opcode, tvb, pinfo, offsetp, t, state, byte_order);
      }

      if ((left = tvb_reported_length_remaining(tvb, offset)) > 0)
            UNDECODED(left);
}

static void
same_screen_focus(tvbuff_t *tvb, int *offsetp, proto_tree *t)
{
      proto_item *ti;
      uint32_t bitmask_value;
      int bitmask_offset;
      int bitmask_size;
      proto_tree *bitmask_tree;

      bitmask_value = tvb_get_uint8(tvb, *offsetp);
      bitmask_offset = *offsetp;
      bitmask_size = 1;

      ti = proto_tree_add_uint(t, hf_x11_same_screen_focus_mask, tvb, *offsetp, 1,
                               bitmask_value);
      bitmask_tree = proto_item_add_subtree(ti, ett_x11_same_screen_focus);
      FLAG(same_screen_focus, focus);
      FLAG(same_screen_focus, same_screen);

      *offsetp += 1;
}

static void
dissect_x11_event(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                  const char *sep, x11_conv_data_t *state,
                  unsigned byte_order)
{
      unsigned char eventcode;
      const char *sent;
      proto_item *ti;
      proto_tree *t;

      ti = proto_tree_add_item(tree, proto_x11, tvb, 0, -1, ENC_NA);
      t = proto_item_add_subtree(ti, ett_x11);

      eventcode = tvb_get_uint8(tvb, 0);
      sent = (eventcode & 0x80) ? "Sent-" : "";

      col_append_fstr(pinfo->cinfo, COL_INFO, "%s %s%s",
                    sep, sent,
                    val_to_str(eventcode & 0x7F, state->eventcode_vals,
                                "<Unknown eventcode %u>"));

      proto_item_append_text(ti, ", Event, eventcode: %d (%s%s)",
                             eventcode, sent,
                             val_to_str(eventcode & 0x7F, state->eventcode_vals,
                                        "<Unknown eventcode %u>"));

      if (tree == NULL)
            return;

      decode_x11_event(tvb, eventcode, sent, t, state, byte_order);

      return;
}

static void
decode_x11_event(tvbuff_t *tvb, unsigned char eventcode, const char *sent,
                 proto_tree *t, x11_conv_data_t *state,
                 unsigned byte_order)
{
      int offset = 0, *offsetp = &offset, left;

      proto_tree_add_uint_format(t, hf_x11_eventcode, tvb, offset, 1,
                                 eventcode,
                                 "eventcode: %d (%s%s)",
                                 eventcode, sent,
                                 val_to_str(eventcode & 0x7F, state->eventcode_vals,
                                            "<Unknown eventcode %u>"));
      ++offset;

      switch (eventcode & 0x7F) {
            case KeyPress:
            case KeyRelease: {
                  int code, mask;

                  /* need to do some prefetching here ... */
                  code = tvb_get_uint8(tvb, offset);
                  mask = tvb_get_uint16(tvb, 28, byte_order);

                  KEYCODE_DECODED(keycode, code, mask);
                  CARD16(event_sequencenumber);
                  EVENTCONTENTS_COMMON();
                  BOOL(same_screen);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  break;
            }

            case ButtonPress:
            case ButtonRelease:
                  BUTTON(eventbutton);
                  CARD16(event_sequencenumber);
                  EVENTCONTENTS_COMMON();
                  BOOL(same_screen);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  break;

            case MotionNotify:
                  CARD8(detail);
                  CARD16(event_sequencenumber);
                  EVENTCONTENTS_COMMON();
                  BOOL(same_screen);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  break;

            case EnterNotify:
            case LeaveNotify:
                  ENUM8(event_detail);
                  CARD16(event_sequencenumber);
                  EVENTCONTENTS_COMMON();
                  ENUM8(grab_mode);
                  same_screen_focus(tvb, offsetp, t);
                  break;

            case FocusIn:
            case FocusOut:
                  ENUM8(focus_detail);
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  ENUM8(focus_mode);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 23, ENC_NA);
                  *offsetp += 23;
                  break;

            case KeymapNotify:
                  break;

            case Expose:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  INT16(x);
                  INT16(y);
                  CARD16(width);
                  CARD16(height);
                  CARD16(count);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 14, ENC_NA);
                  *offsetp += 14;
                  break;

            case GraphicsExpose:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  DRAWABLE(drawable);
                  CARD16(x);
                  CARD16(y);
                  CARD16(width);
                  CARD16(height);
                  CARD16(minor_opcode);
                  CARD16(count);
                  CARD8(major_opcode);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 11, ENC_NA);
                  *offsetp += 11;
                  break;

            case NoExpose:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  DRAWABLE(drawable);
                  CARD16(minor_opcode);
                  CARD8(major_opcode);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 21, ENC_NA);
                  *offsetp += 21;
                  break;

            case VisibilityNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  ENUM8(visibility_state);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 23, ENC_NA);
                  *offsetp += 23;
                  break;

            case CreateNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(parent);
                  WINDOW(eventwindow);
                  INT16(x);
                  INT16(y);
                  CARD16(width);
                  CARD16(height);
                  CARD16(border_width);
                  BOOL(override_redirect);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 9, ENC_NA);
                  *offsetp += 9;
                  break;

            case DestroyNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  WINDOW(window);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 20, ENC_NA);
                  *offsetp += 20;
                  break;

            case UnmapNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  WINDOW(window);
                  BOOL(from_configure);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 19, ENC_NA);
                  *offsetp += 19;
                  break;

            case MapNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  WINDOW(window);
                  BOOL(override_redirect);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 19, ENC_NA);
                  *offsetp += 19;
                  break;

            case MapRequest:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(parent);
                  WINDOW(eventwindow);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 20, ENC_NA);
                  *offsetp += 20;
                  break;

            case ReparentNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  WINDOW(window);
                  WINDOW(parent);
                  INT16(x);
                  INT16(y);
                  BOOL(override_redirect);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 11, ENC_NA);
                  *offsetp += 11;
                  break;

            case ConfigureNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  WINDOW(window);
                  WINDOW(above_sibling);
                  INT16(x);
                  INT16(y);
                  CARD16(width);
                  CARD16(height);
                  CARD16(border_width);
                  BOOL(override_redirect);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 5, ENC_NA);
                  *offsetp += 5;
                  break;

            case ConfigureRequest:
                  break;

            case GravityNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  WINDOW(window);
                  INT16(x);
                  INT16(y);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 16, ENC_NA);
                  *offsetp += 16;
                  break;

            case ResizeRequest:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  CARD16(width);
                  CARD16(height);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 20, ENC_NA);
                  *offsetp += 20;
                  break;

            case CirculateNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  WINDOW(window);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 4, ENC_NA);
                  *offsetp += 4;
                  ENUM8(place);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 15, ENC_NA);
                  *offsetp += 15;
                  break;

            case CirculateRequest:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(parent);
                  WINDOW(eventwindow);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 4, ENC_NA);
                  *offsetp += 4;
                  ENUM8(place);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 15, ENC_NA);
                  *offsetp += 15;
                  break;

            case PropertyNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  ATOM(atom);
                  TIMESTAMP(time);
                  ENUM8(property_state);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 15, ENC_NA);
                  *offsetp += 15;
                  break;

            case SelectionClear:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  TIMESTAMP(time);
                  WINDOW(owner);
                  ATOM(selection);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 16, ENC_NA);
                  *offsetp += 16;
                  break;

            case SelectionRequest:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  TIMESTAMP(time);
                  WINDOW(owner);
                  WINDOW(requestor);
                  ATOM(selection);
                  ATOM(target);
                  ATOM(property);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 4, ENC_NA);
                  *offsetp += 4;
                  break;

            case SelectionNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  TIMESTAMP(time);
                  WINDOW(requestor);
                  ATOM(selection);
                  ATOM(target);
                  ATOM(property);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 8, ENC_NA);
                  *offsetp += 8;
                  break;

            case ColormapNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  COLORMAP(cmap);
                  BOOL(new);
                  ENUM8(colormap_state);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 18, ENC_NA);
                  *offsetp += 18;
                  break;

            case ClientMessage:
                  CARD8(format);
                  CARD16(event_sequencenumber);
                  WINDOW(eventwindow);
                  ATOM(type);
                  LISTofBYTE(data, 20);
                  break;

            case MappingNotify:
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 1, ENC_NA);
                  *offsetp += 1;
                  CARD16(event_sequencenumber);
                  ENUM8(mapping_request);
                  CARD8(first_keycode);
                  CARD8(count);
                  proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 25, ENC_NA);
                  *offsetp += 25;
                  break;

            case GenericEvent:
                  tryGenericExtensionEvent(tvb, offsetp, t, state, byte_order);
                  break;

            default:
                  tryExtensionEvent(eventcode & 0x7F, tvb, offsetp, t, state, byte_order);
                  break;
      }

      if ((left = tvb_reported_length_remaining(tvb, offset)) > 0)
            UNDECODED(left);
}

static void
dissect_x11_error(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
                  const char *sep, x11_conv_data_t *state _U_,
                  unsigned byte_order)
{
      int offset = 0, *offsetp = &offset, left;
      unsigned char errorcode;
      proto_item *ti;
      proto_tree *t;

      ti = proto_tree_add_item(tree, proto_x11, tvb, 0, -1, ENC_NA);
      t = proto_item_add_subtree(ti, ett_x11);

      CARD8(error);

      errorcode = tvb_get_uint8(tvb, offset);
      col_append_fstr(pinfo->cinfo, COL_INFO, "%s %s",
                    sep, val_to_str(errorcode, state->errorcode_vals, "<Unknown errorcode %u>"));

      proto_tree_add_uint_format(t, hf_x11_errorcode, tvb, offset, 1,
                                 errorcode,
                                 "errorcode: %d (%s)",
                                 errorcode,
                                 val_to_str(errorcode, state->errorcode_vals,
                                            "<Unknown errorcode %u>"));
      ++offset;

      proto_item_append_text(ti, ", Error, errorcode: %d (%s)",
                             errorcode, val_to_str(errorcode, state->errorcode_vals,
                                                   "<Unknown errorcode %u>"));

      if (tree == NULL)
            return;

      CARD16(error_sequencenumber);

      switch (errorcode) {
            case BadValue:
                  CARD32(error_badvalue);
                  break;
            case BadWindow:
            case BadPixmap:
            case BadCursor:
            case BadFont:
            case BadDrawable:
            case BadColormap:
            case BadGC:
            case BadIDChoice:
                  CARD32(error_badresourceid);
                  break;

            default:
                  UNDECODED(4);
      }

      CARD16(minor_opcode);
      CARD8(major_opcode);

      if ((left = tvb_reported_length_remaining(tvb, offset)) > 0)
            UNDECODED(left);
}



/************************************************************************
 ***                                                                  ***
 ***         I N I T I A L I Z A T I O N   A N D   M A I N            ***
 ***                                                                  ***
 ************************************************************************/

static int
dissect_x11(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
      col_set_str(pinfo->cinfo, COL_PROTOCOL, "X11");

      if (pinfo->match_uint == pinfo->srcport)
            dissect_x11_replies(tvb, pinfo, tree);
      else
            dissect_x11_requests(tvb, pinfo, tree);

      return tvb_captured_length(tvb);
}

/* Register the protocol with Wireshark */
void proto_register_x11(void)
{

/* Setup list of header fields */
      static hf_register_info hf[] = {
#include "x11-register-info.h"
      };

/* Setup protocol subtree array */
      static int *ett[] = {
            &ett_x11,
            &ett_x11_color_flags,
            &ett_x11_list_of_arc,
            &ett_x11_arc,
            &ett_x11_list_of_atom,
            &ett_x11_list_of_card32,
            &ett_x11_list_of_float,
            &ett_x11_list_of_double,
            &ett_x11_list_of_color_item,
            &ett_x11_color_item,
            &ett_x11_list_of_keycode,
            &ett_x11_list_of_keysyms,
            &ett_x11_keysym,
            &ett_x11_list_of_point,
            &ett_x11_point,
            &ett_x11_list_of_rectangle,
            &ett_x11_rectangle,
            &ett_x11_list_of_segment,
            &ett_x11_segment,
            &ett_x11_list_of_string8,
            &ett_x11_list_of_text_item,
            &ett_x11_text_item,
            &ett_x11_gc_value_mask,
            &ett_x11_event_mask,
            &ett_x11_do_not_propagate_mask,
            &ett_x11_set_of_key_mask,
            &ett_x11_pointer_event_mask,
            &ett_x11_window_value_mask,
            &ett_x11_configure_window_mask,
            &ett_x11_keyboard_value_mask,
            &ett_x11_same_screen_focus,
            &ett_x11_event,
            &ett_x11_list_of_pixmap_format,
            &ett_x11_pixmap_format,
            &ett_x11_list_of_screen,
            &ett_x11_screen,
            &ett_x11_list_of_depth_detail,
            &ett_x11_depth_detail,
            &ett_x11_list_of_visualtype,
            &ett_x11_visualtype,
      };

      static ei_register_info ei[] = {
            { &ei_x11_invalid_format, { "x11.invalid_format", PI_PROTOCOL, PI_WARN, "Invalid Format", EXPFILL }},
            { &ei_x11_request_length, { "x11.request-length.invalid", PI_PROTOCOL, PI_WARN, "Invalid Length", EXPFILL }},
            { &ei_x11_keycode_value_out_of_range, { "x11.keycode_value_out_of_range", PI_PROTOCOL, PI_WARN, "keycode value is out of range", EXPFILL }},
      };

      module_t *x11_module;
      expert_module_t* expert_x11;

/* Register the protocol name and description */
      proto_x11 = proto_register_protocol("X11", "X11", "x11");
      x11_handle = register_dissector("x11", dissect_x11, proto_x11);

/* Required function calls to register the header fields and subtrees used */
      proto_register_field_array(proto_x11, hf, array_length(hf));
      proto_register_subtree_array(ett, array_length(ett));
      expert_x11 = expert_register_protocol(proto_x11);
      expert_register_field_array(expert_x11, ei, array_length(ei));

      extension_table = wmem_map_new(wmem_epan_scope(), wmem_str_hash, g_str_equal);
      error_table = wmem_map_new(wmem_epan_scope(), wmem_str_hash, g_str_equal);
      event_table = wmem_map_new(wmem_epan_scope(), wmem_str_hash, g_str_equal);
      genevent_table = wmem_map_new(wmem_epan_scope(), wmem_str_hash, g_str_equal);
      reply_table = wmem_map_new(wmem_epan_scope(), wmem_str_hash, g_str_equal);
      register_x11_extensions();

      x11_module = prefs_register_protocol(proto_x11, NULL);
      prefs_register_bool_preference(x11_module, "desegment",
            "Reassemble X11 messages spanning multiple TCP segments",
            "Whether the X11 dissector should reassemble messages spanning multiple TCP segments. "
            "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
            &x11_desegment);
}

void
proto_reg_handoff_x11(void)
{
      dissector_add_uint_range_with_preference("tcp.port",  DEFAULT_X11_PORT_RANGE, x11_handle);
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 6
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=6 tabstop=8 expandtab:
 * :indentSize=6:tabSize=8:noTabs=true:
 */