forked from wireshark/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-x11-xcb.pl
executable file
·1946 lines (1668 loc) · 67.5 KB
/
process-x11-xcb.pl
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
#!/usr/bin/perl
#
# Script to convert xcbproto and mesa protocol files for
# X11 dissector. Creates header files containing code to
# dissect X11 extensions.
#
# Instructions for using this script are in epan/dissectors/README.X11
#
# Copyright 2008, 2009, 2013, 2014 Open Text Corporation <pharris[AT]opentext.com>
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <[email protected]>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
#TODO
# - support constructs that are legal in XCB, but don't appear to be used
use 5.010;
use warnings;
use strict;
# given/when is going to be removed (and/or dramatically altered)
# in 5.20. Patches welcome.
# Patches even more welcome if they rewrite this whole thing in a
# language with a proper compatibility document, such as
# http://golang.org/doc/go1compat
no if $] >= 5.018, warnings => "experimental::smartmatch";
use IO::File;
use XML::Twig;
use File::Spec;
my $srcdir = shift;
die "'$srcdir' is not a directory" unless -d $srcdir;
my @reslist = grep {!/xproto\.xml$/} glob File::Spec->catfile($srcdir, 'xcbproto', 'src', '*.xml');
my @register;
my $script_name = File::Spec->abs2rel ($0, $srcdir);
my %basictype = (
char => { size => 1, encoding => 'ENC_ASCII|ENC_NA', type => 'FT_STRING', base => 'BASE_NONE', get => 'tvb_get_guint8', list => 'listOfByte', },
void => { size => 1, encoding => 'ENC_NA', type => 'FT_BYTES', base => 'BASE_NONE', get => 'tvb_get_guint8', list => 'listOfByte', },
BYTE => { size => 1, encoding => 'ENC_NA', type => 'FT_BYTES', base => 'BASE_NONE', get => 'tvb_get_guint8', list => 'listOfByte', },
CARD8 => { size => 1, encoding => 'byte_order', type => 'FT_UINT8', base => 'BASE_HEX_DEC', get => 'tvb_get_guint8', list => 'listOfByte', },
CARD16 => { size => 2, encoding => 'byte_order', type => 'FT_UINT16', base => 'BASE_HEX_DEC', get => 'tvb_get_guint16', list => 'listOfCard16', },
CARD32 => { size => 4, encoding => 'byte_order', type => 'FT_UINT32', base => 'BASE_HEX_DEC', get => 'tvb_get_guint32', list => 'listOfCard32', },
CARD64 => { size => 8, encoding => 'byte_order', type => 'FT_UINT64', base => 'BASE_HEX_DEC', get => 'tvb_get_guint64', list => 'listOfCard64', },
INT8 => { size => 1, encoding => 'byte_order', type => 'FT_INT8', base => 'BASE_DEC', get => 'tvb_get_guint8', list => 'listOfByte', },
INT16 => { size => 2, encoding => 'byte_order', type => 'FT_INT16', base => 'BASE_DEC', get => 'tvb_get_guint16', list => 'listOfInt16', },
INT32 => { size => 4, encoding => 'byte_order', type => 'FT_INT32', base => 'BASE_DEC', get => 'tvb_get_guint32', list => 'listOfInt32', },
INT64 => { size => 8, encoding => 'byte_order', type => 'FT_INT64', base => 'BASE_DEC', get => 'tvb_get_guint64', list => 'listOfInt64', },
float => { size => 4, encoding => 'byte_order', type => 'FT_FLOAT', base => 'BASE_NONE', get => 'tvb_get_ieee_float', list => 'listOfFloat', },
double => { size => 8, encoding => 'byte_order', type => 'FT_DOUBLE', base => 'BASE_NONE', get => 'tvb_get_ieee_double', list => 'listOfDouble', },
BOOL => { size => 1, encoding => 'byte_order', type => 'FT_BOOLEAN',base => 'BASE_NONE', get => 'tvb_get_guint8', list => 'listOfByte', },
);
my %simpletype; # Reset at the beginning of each extension
my %gltype; # No need to reset, since it's only used once
my %struct = # Not reset; contains structures already defined.
# Also contains this black-list of structures never used by any
# extension (to avoid generating useless code).
(
# structures defined by xproto, but not used by any extension
'xproto:CHAR2B' => 1,
'xproto:ARC' => 1,
'xproto:FORMAT' => 1,
'xproto:VISUALTYPE' => 1,
'xproto:DEPTH' => 1,
'xproto:SCREEN' => 1,
'xproto:SetupRequest' => 1,
'xproto:SetupFailed' => 1,
'xproto:SetupAuthenticate' => 1,
'xproto:Setup' => 1,
'xproto:TIMECOORD' => 1,
'xproto:FONTPROP' => 1,
'xproto:CHARINFO' => 1,
'xproto:SEGMENT' => 1,
'xproto:COLORITEM' => 1,
'xproto:RGB' => 1,
'xproto:HOST' => 1,
'xproto:POINT' => 1,
# structures defined by xinput, but never used (except by each other)(bug in xcb?)
'xinput:KeyInfo' => 1,
'xinput:ButtonInfo' => 1,
'xinput:ValuatorInfo' => 1,
'xinput:KbdFeedbackState' => 1,
'xinput:PtrFeedbackState' => 1,
'xinput:IntegerFeedbackState' => 1,
'xinput:StringFeedbackState' => 1,
'xinput:BellFeedbackState' => 1,
'xinput:LedFeedbackState' => 1,
'xinput:KbdFeedbackCtl' => 1,
'xinput:PtrFeedbackCtl' => 1,
'xinput:IntegerFeedbackCtl' => 1,
'xinput:StringFeedbackCtl' => 1,
'xinput:BellFeedbackCtl' => 1,
'xinput:LedFeedbackCtl' => 1,
'xinput:KeyState' => 1,
'xinput:ButtonState' => 1,
'xinput:ValuatorState' => 1,
'xinput:DeviceResolutionState' => 1,
'xinput:DeviceAbsCalibState' => 1,
'xinput:DeviceAbsAreaState' => 1,
'xinput:DeviceCoreState' => 1,
'xinput:DeviceEnableState' => 1,
'xinput:DeviceResolutionCtl' => 1,
'xinput:DeviceAbsCalibCtl' => 1,
'xinput:DeviceAbsAreaCtrl' => 1,
'xinput:DeviceCoreCtrl' => 1,
'xinput:DeviceEnableCtrl' => 1,
'xinput:DeviceName' => 1,
'xinput:AddMaster' => 1,
'xinput:RemoveMaster' => 1,
'xinput:AttachSlave' => 1,
'xinput:DetachSlave' => 1,
'xinput:ButtonClass' => 1,
'xinput:KeyClass' => 1,
'xinput:ScrollClass' => 1,
'xinput:TouchClass' => 1,
'xinput:ValuatorClass' => 1,
# structures defined by xv, but never used (bug in xcb?)
'xv:Image' => 1,
# structures defined by xkb, but never used (except by each other)(bug in xcb?)
'xkb:Key' => 1,
'xkb:Outline' => 1,
'xkb:Overlay' => 1,
'xkb:OverlayKey' => 1,
'xkb:OverlayRow' => 1,
'xkb:Row' => 1,
'xkb:Shape' => 1,
);
my %enum; # Not reset; contains enums already defined.
my %enum_name;
my %type_name;
my $header;
my $extname;
my @incname;
my %request;
my %genericevent;
my %event;
my %reply;
# Output files
my $impl;
my $reg;
my $decl;
my $error;
# glRender sub-op output files
my $enum;
# Mesa API definitions keep moving
my @mesas = ($srcdir . '/mesa/src/mapi/glapi/gen', # 2010-04-26
$srcdir . '/mesa/src/mesa/glapi/gen', # 2010-02-22
$srcdir . '/mesa/src/mesa/glapi'); # 2004-05-18
my $mesadir = (grep { -d } @mesas)[0];
sub mesa_category {
my ($t, $elt) = @_;
$t->purge;
}
#used to prevent duplication and sort enumerated values
my %mesa_enum_hash = ();
sub mesa_enum {
my ($t, $elt) = @_;
my $name = $elt->att('name');
my $value = $elt->att('value');
my $hex_value = hex($value); #convert string to hex value to catch leading zeros
#make sure value isn't already in the hash, to prevent duplication in value_string
if (!exists($mesa_enum_hash{$hex_value})) {
$mesa_enum_hash{$hex_value} = $name;
}
$t->purge;
}
sub mesa_type {
my ($t, $elt) = @_;
my $name = $elt->att('name');
my $size = $elt->att('size');
my $float = $elt->att('float');
my $unsigned = $elt->att('unsigned');
my $base;
$t->purge;
if($name eq 'enum') {
# enum does not have a direct X equivalent
$gltype{'GLenum'} = { size => 4, encoding => 'byte_order', type => 'FT_UINT32', base => 'BASE_HEX|BASE_EXT_STRING',
get => 'tvb_get_guint32', list => 'listOfCard32',
val => '&mesa_enum_ext', };
return;
}
$name = 'GL'.$name;
if (defined($float) && $float eq 'true') {
$base = 'float';
$base = 'double' if ($size == 8);
} else {
$base = 'INT';
if (defined($unsigned) && $unsigned eq 'true') {
$base = 'CARD';
}
$base .= ($size * 8);
$base = 'BOOL' if ($name eq 'bool');
$base = 'BYTE' if ($name eq 'void');
}
$gltype{$name} = $basictype{$base};
}
sub registered_name($$)
{
my $name = shift;
my $field = shift;
return "hf_x11_$header"."_$name"."_$field";
}
sub mesa_function {
my ($t, $elt) = @_;
# rop == glRender sub-op
# sop == GLX minor opcode
my $glx = $elt->first_child('glx');
unless(defined $glx) { $t->purge; return; }
my $rop = $glx->att('rop');
unless (defined $rop) { $t->purge; return; }
# Ideally, we want the main name, not the alias name.
# Practically, we'd have to scan the file twice to find
# the functions that we want to skip.
my $alias = $elt->att('alias');
if (defined $alias) { $t->purge; return; }
my $name = $elt->att('name');
$request{$rop} = $name;
my $image;
my $length = 0;
my @elements = $elt->children('param');
# Wireshark defines _U_ to mean "Unused" (compiler specific define)
if (!@elements) {
print $impl <<eot
static void mesa_$name(tvbuff_t *tvb _U_, int *offsetp _U_, proto_tree *t _U_, guint byte_order _U_, int length _U_)
{
eot
;
} else {
print $impl <<eot
static void mesa_$name(tvbuff_t *tvb, int *offsetp, proto_tree *t, guint byte_order, int length _U_)
{
eot
;
}
my %type_param;
foreach my $e (@elements) {
# Detect count && variable_param
my $count = $e->att('count');
my $variable_param = $e->att('variable_param');
if (defined $count and defined $variable_param) {
$type_param{$variable_param} = 1;
}
}
foreach my $e (@elements) {
# Register field with wireshark
my $type = $e->att('type');
$type =~ s/^const //;
my $list;
$list = 1 if ($type =~ /\*$/);
$type =~ s/ \*$//;
my $fieldname = $e->att('name');
my $regname = registered_name($name, $fieldname);
my $info = $gltype{$type};
my $ft = $info->{'type'};
my $base = $info->{'base'};
my $val = $info->{'val'} // 'NULL';
my $count = $e->att('count');
my $variable_param = $e->att('variable_param');
if ($list and $count and $variable_param) {
print $decl "static int ${regname} = -1;\n";
print $reg "{ &$regname, { \"$fieldname\", \"x11.glx.render.$name.$fieldname\", FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL }},\n";
print $decl "static int ${regname}_signed = -1;\n";
print $reg "{ &${regname}_signed, { \"$fieldname\", \"x11.glx.render.$name.$fieldname\", FT_INT8, BASE_DEC, NULL, 0, NULL, HFILL }},\n";
print $decl "static int ${regname}_unsigned = -1;\n";
print $reg "{ &${regname}_unsigned, { \"$fieldname\", \"x11.glx.render.$name.$fieldname\", FT_UINT8, BASE_DEC, NULL, 0, NULL, HFILL }},\n";
print $decl "static int ${regname}_item_card16 = -1;\n";
print $reg "{ &${regname}_item_card16, { \"$fieldname\", \"x11.glx.render.$name.$fieldname\", FT_UINT16, BASE_DEC, NULL, 0, NULL, HFILL }},\n";
print $decl "static int ${regname}_item_int16 = -1;\n";
print $reg "{ &${regname}_item_int16, { \"$fieldname\", \"x11.glx.render.$name.$fieldname\", FT_INT16, BASE_DEC, NULL, 0, NULL, HFILL }},\n";
print $decl "static int ${regname}_item_card32 = -1;\n";
print $reg "{ &${regname}_item_card32, { \"$fieldname\", \"x11.glx.render.$name.$fieldname\", FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL }},\n";
print $decl "static int ${regname}_item_int32 = -1;\n";
print $reg "{ &${regname}_item_int32, { \"$fieldname\", \"x11.glx.render.$name.$fieldname\", FT_INT32, BASE_DEC, NULL, 0, NULL, HFILL }},\n";
print $decl "static int ${regname}_item_float = -1;\n";
print $reg "{ &${regname}_item_float, { \"$fieldname\", \"x11.glx.render.$name.$fieldname\", FT_FLOAT, BASE_NONE, NULL, 0, NULL, HFILL }},\n";
} else {
print $decl "static int $regname = -1;\n";
if ($list and $info->{'size'} > 1) {
print $reg "{ &$regname, { \"$fieldname\", \"x11.glx.render.$name.$fieldname.list\", FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL }},\n";
$regname .= '_item';
print $decl "static int $regname = -1;\n";
}
print $reg "{ &$regname, { \"$fieldname\", \"x11.glx.render.$name.$fieldname\", $ft, $base, $val, 0, NULL, HFILL }},\n";
if ($e->att('counter') or $type_param{$fieldname}) {
print $impl " int $fieldname;\n";
}
}
if ($list) {
if ($e->att('img_format')) {
$image = 1;
foreach my $wholename (('swap bytes', 'lsb first')) {
# Boolean values
my $varname = $wholename;
$varname =~ s/\s//g;
my $regname = registered_name($name, $varname);
print $decl "static int $regname = -1;\n";
print $reg "{ &$regname, { \"$wholename\", \"x11.glx.render.$name.$varname\", FT_BOOLEAN, BASE_NONE, NULL, 0, NULL, HFILL }},\n";
}
foreach my $wholename (('row length', 'skip rows', 'skip pixels', 'alignment')) {
# Integer values
my $varname = $wholename;
$varname =~ s/\s//g;
my $regname = registered_name($name, $varname);
print $decl "static int $regname = -1;\n";
print $reg "{ &$regname, { \"$wholename\", \"x11.glx.render.$name.$varname\", FT_UINT32, BASE_HEX_DEC, NULL, 0, NULL, HFILL }},\n";
}
}
}
}
# The image requests have a few implicit elements first:
if ($image) {
foreach my $wholename (('swap bytes', 'lsb first')) {
# Boolean values
my $varname = $wholename;
$varname =~ s/\s//g;
my $regname = registered_name($name, $varname);
print $impl " proto_tree_add_item(t, $regname, tvb, *offsetp, 1, byte_order);\n";
print $impl " *offsetp += 1;\n";
$length += 1;
}
print $impl " proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, 2, ENC_NA);\n";
print $impl " *offsetp += 2;\n";
$length += 2;
foreach my $wholename (('row length', 'skip rows', 'skip pixels', 'alignment')) {
# Integer values
my $varname = $wholename;
$varname =~ s/\s//g;
my $regname = registered_name($name, $varname);
print $impl " proto_tree_add_item(t, $regname, tvb, *offsetp, 4, byte_order);\n";
print $impl " *offsetp += 4;\n";
$length += 4;
}
}
foreach my $e (@elements) {
my $type = $e->att('type');
$type =~ s/^const //;
my $list;
$list = 1 if ($type =~ /\*$/);
$type =~ s/ \*$//;
my $fieldname = $e->att('name');
my $regname = registered_name($name, $fieldname);
my $info = $gltype{$type};
my $ft = $info->{'type'};
my $base = $info->{'base'};
if (!$list) {
my $size = $info->{'size'};
my $encoding = $info->{'encoding'};
my $get = $info->{'get'};
if ($e->att('counter') or $type_param{$fieldname}) {
if ($get ne "tvb_get_guint8") {
print $impl " $fieldname = $get(tvb, *offsetp, $encoding);\n";
} else {
print $impl " $fieldname = $get(tvb, *offsetp);\n";
}
}
print $impl " proto_tree_add_item(t, $regname, tvb, *offsetp, $size, $encoding);\n";
print $impl " *offsetp += $size;\n";
$length += $size;
} else { # list
my $list = $info->{'list'};
my $count = $e->att('count');
my $variable_param = $e->att('variable_param');
if (defined($count) && !defined($variable_param)) {
$regname .= ", $regname".'_item' if ($info->{'size'} > 1);
print $impl " $list(tvb, offsetp, t, $regname, $count, byte_order);\n";
} else {
if (defined($count)) {
# Currently, only CallLists has both a count and a variable_param
# The XML contains a size description of all the possibilities
# for CallLists, but not a type description. Implement by hand,
# with the caveat that more types may need to be added in the
# future.
say $impl " switch($variable_param) {";
say $impl " case 0x1400: /* BYTE */";
say $impl " listOfByte(tvb, offsetp, t, ${regname}_signed, $count, byte_order);";
say $impl " proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, (length - $length - $count), ENC_NA);";
say $impl " *offsetp += (length - $length - $count);";
say $impl " break;";
say $impl " case 0x1401: /* UNSIGNED_BYTE */";
say $impl " listOfByte(tvb, offsetp, t, ${regname}_unsigned, $count, byte_order);";
say $impl " proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, (length - $length - $count), ENC_NA);";
say $impl " *offsetp += (length - $length - $count);";
say $impl " break;";
say $impl " case 0x1402: /* SHORT */";
say $impl " listOfInt16(tvb, offsetp, t, $regname, ${regname}_item_int16, $count, byte_order);";
say $impl " proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, (length - $length - 2 * $count), ENC_NA);";
say $impl " *offsetp += (length - $length - 2 * $count);";
say $impl " break;";
say $impl " case 0x1403: /* UNSIGNED_SHORT */";
say $impl " listOfCard16(tvb, offsetp, t, $regname, ${regname}_item_card16, $count, byte_order);";
say $impl " proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, (length - $length - 2 * $count), ENC_NA);";
say $impl " *offsetp += (length - $length - 2 * $count);";
say $impl " break;";
say $impl " case 0x1404: /* INT */";
say $impl " listOfInt32(tvb, offsetp, t, $regname, ${regname}_item_int32, $count, byte_order);";
say $impl " break;";
say $impl " case 0x1405: /* UNSIGNED_INT */";
say $impl " listOfCard32(tvb, offsetp, t, $regname, ${regname}_item_card32, $count, byte_order);";
say $impl " break;";
say $impl " case 0x1406: /* FLOAT */";
say $impl " listOfFloat(tvb, offsetp, t, $regname, ${regname}_item_float, $count, byte_order);";
say $impl " break;";
say $impl " case 0x1407: /* 2_BYTES */";
say $impl " listOfCard16(tvb, offsetp, t, $regname, ${regname}_item_card16, $count, ENC_BIG_ENDIAN);";
say $impl " proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, (length - $length - 2 * $count), ENC_NA);";
say $impl " *offsetp += (length - $length - 2 * $count);";
say $impl " break;";
say $impl " case 0x1408: /* 3_BYTES */";
say $impl " UNDECODED(3 * $count);";
say $impl " proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, (length - $length - 3 * $count), ENC_NA);";
say $impl " *offsetp += (length - $length - 3 * $count);";
say $impl " break;";
say $impl " case 0x1409: /* 4_BYTES */";
say $impl " listOfCard32(tvb, offsetp, t, $regname, ${regname}_item_card32, $count, ENC_BIG_ENDIAN);";
say $impl " break;";
say $impl " case 0x140B: /* HALF_FLOAT */";
say $impl " UNDECODED(2 * $count);";
say $impl " proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, (length - $length - 2 * $count), ENC_NA);";
say $impl " *offsetp += (length - $length - 2 * $count);";
say $impl " break;";
say $impl " default: /* Unknown */";
say $impl " UNDECODED(length - $length);";
say $impl " break;";
say $impl " }";
} else {
$regname .= ", $regname".'_item' if ($info->{'size'} > 1);
print $impl " $list(tvb, offsetp, t, $regname, (length - $length) / $gltype{$type}{'size'}, byte_order);\n";
}
}
}
}
print $impl "}\n\n";
$t->purge;
}
sub get_op($;$);
sub get_unop($;$);
sub get_ref($$)
{
my $elt = shift;
my $refref = shift;
my $rv;
given($elt->name()) {
when ('fieldref') {
$rv = $elt->text();
$refref->{$rv} = 1;
$rv = 'f_'.$rv;
}
when ('value') { $rv = $elt->text(); }
when ('op') { $rv = get_op($elt, $refref); }
when (['unop','popcount']) { $rv = get_unop($elt, $refref); }
default { die "Invalid op fragment: $_" }
}
return $rv;
}
sub get_op($;$) {
my $op = shift;
my $refref = shift // {};
my @elements = $op->children(qr/fieldref|value|op|unop|popcount/);
(@elements == 2) or die ("Wrong number of children for 'op'\n");
my $left;
my $right;
$left = get_ref($elements[0], $refref);
$right = get_ref($elements[1], $refref);
return "($left " . $op->att('op') . " $right)";
}
sub get_unop($;$) {
my $op = shift;
my $refref = shift // {};
my @elements = $op->children(qr/fieldref|value|op|unop|popcount/);
(@elements == 1) or die ("Wrong number of children for 'unop'\n");
my $left;
$left = get_ref($elements[0], $refref);
given ($op->name()) {
when ('unop') {
return '(' . $op->att('op') . "$left)";
}
when ('popcount') {
return "ws_count_ones($left)";
}
default { die "Invalid unop element $op->name()\n"; }
}
}
sub qualname {
my $name = shift;
$name = $incname[0].':'.$name unless $name =~ /:/;
return $name
}
sub get_simple_info {
my $name = shift;
my $info = $basictype{$name};
return $info if (defined $info);
$info = $simpletype{$name};
return $info if (defined $info);
if (defined($type_name{$name})) {
return $simpletype{$type_name{$name}};
}
return undef
}
sub get_struct_info {
my $name = shift;
my $info = $struct{$name};
return $info if (defined $info);
if (defined($type_name{$name})) {
return $struct{$type_name{$name}};
}
return undef
}
sub getinfo {
my $name = shift;
my $info = get_simple_info($name) // get_struct_info($name);
# If the script fails here search for $name in this script and remove it from the black list
die "$name is defined to be unused in process-x11-xcb.pl but is actually used!" if (defined($info) && $info == "1");
return $info;
}
sub dump_enum_values($)
{
my $e = shift;
defined($enum{$e}) or die("Enum $e not found");
my $enumname = "x11_enum_$e";
return $enumname if (defined $enum{$e}{done});
say $enum 'static const value_string '.$enumname.'[] = {';
my $value = $enum{$e}{value};
for my $val (sort { $a <=> $b } keys %$value) {
say $enum sprintf(" { %3d, \"%s\" },", $val, $$value{$val});
}
say $enum sprintf(" { %3d, NULL },", 0);
say $enum '};';
say $enum '';
$enum{$e}{done} = 1;
return $enumname;
}
# Find all references, so we can declare only the minimum necessary
sub reference_elements($$);
sub reference_elements($$)
{
my $e = shift;
my $refref = shift;
given ($e->name()) {
when ('switch') {
my $lentype = $e->first_child();
if (defined $lentype) {
given ($lentype->name()) {
when ('fieldref') { $refref->{field}{$lentype->text()} = 1; }
when ('op') { get_op($lentype, $refref->{field}); }
}
}
my @elements = $e->children(qr/(bit)?case/);
for my $case (@elements) {
my @sub_elements = $case->children(qr/list|switch/);
foreach my $sub_e (@sub_elements) {
reference_elements($sub_e, $refref);
}
}
}
when ('list') {
my $type = $e->att('type');
my $info = getinfo($type);
if (defined $info->{paramref}) {
for my $pref (keys %{$info->{paramref}}) {
$refref->{field}{$pref} = 1;
}
}
my $lentype = $e->first_child();
if (defined $lentype) {
given ($lentype->name()) {
when ('fieldref') { $refref->{field}{$lentype->text()} = 1; }
when ('op') { get_op($lentype, $refref->{field}); }
when (['unop','popcount']) { get_unop($lentype, $refref->{field}); }
when ('sumof') { $refref->{sumof}{$lentype->att('ref')} = 1; }
}
} else {
$refref->{field}{'length'} = 1;
$refref->{'length'} = 1;
}
}
}
}
sub register_element($$$$;$)
{
my $e = shift;
my $varpat = shift;
my $humanpat = shift;
my $refref = shift;
my $indent = shift // ' ' x 4;
given ($e->name()) {
when ('pad') { return; } # Pad has no variables
when ('switch') { return; } # Switch defines varaibles in a tighter scope to avoid collisions
}
# Register field with wireshark
my $fieldname = $e->att('name');
my $type = $e->att('type') or die ("Field $fieldname does not have a valid type\n");
my $regname = 'hf_x11_'.sprintf ($varpat, $fieldname);
my $humanname = 'x11.'.sprintf ($humanpat, $fieldname);
my $info = getinfo($type);
my $ft = $info->{'type'} // 'FT_NONE';
my $base = $info->{'base'} // 'BASE_NONE';
my $vals = 'NULL';
my $enum = $e->att('enum') // $e->att('altenum');
if (defined $enum) {
my $enumname = dump_enum_values($enum_name{$enum});
$vals = "VALS($enumname)";
# Wireshark does not allow FT_BYTES, FT_BOOLEAN, or BASE_NONE to have an enum
$ft =~ s/FT_BYTES/FT_UINT8/;
$ft =~ s/FT_BOOLEAN/FT_UINT8/;
$base =~ s/BASE_NONE/BASE_DEC/;
}
$enum = $e->att('mask');
if (defined $enum) {
# Create subtree items:
defined($enum{$enum_name{$enum}}) or die("Enum $enum not found");
# Wireshark does not allow FT_BYTES or BASE_NONE to have an enum
$ft =~ s/FT_BYTES/FT_UINT8/;
$base =~ s/BASE_NONE/BASE_DEC/;
my $bitsize = $info->{'size'} * 8;
my $bit = $enum{$enum_name{$enum}}{bit};
for my $val (sort { $a <=> $b } keys %$bit) {
my $itemname = $$bit{$val};
my $item = $regname . '_mask_' . $itemname;
my $itemhuman = $humanname . '.' . $itemname;
my $bitshift = "1U << $val";
say $decl "static int $item = -1;";
say $reg "{ &$item, { \"$itemname\", \"$itemhuman\", FT_BOOLEAN, $bitsize, NULL, $bitshift, NULL, HFILL }},";
}
}
print $decl "static int $regname = -1;\n";
if ($e->name() eq 'list' and defined $info->{'size'} and $info->{'size'} > 1) {
print $reg "{ &$regname, { \"$fieldname\", \"$humanname.list\", FT_NONE, BASE_NONE, NULL, 0, NULL, HFILL }},\n";
$regname .= '_item';
print $decl "static int $regname = -1;\n";
}
print $reg "{ &$regname, { \"$fieldname\", \"$humanname\", $ft, $base, $vals, 0, NULL, HFILL }},\n";
if ($refref->{sumof}{$fieldname}) {
print $impl $indent."int sumof_$fieldname = 0;\n";
}
if ($e->name() eq 'field') {
if ($refref->{field}{$fieldname} and get_simple_info($type)) {
# Pre-declare variable
if ($ft eq 'FT_FLOAT') {
print $impl $indent."gfloat f_$fieldname;\n";
} elsif ($ft eq 'FT_DOUBLE') {
print $impl $indent."gdouble f_$fieldname;\n";
} elsif ($ft eq 'FT_INT64' or $ft eq 'FT_UINT64') {
print $impl $indent."gint64 f_$fieldname;\n";
} else {
print $impl $indent."int f_$fieldname;\n";
}
}
}
}
sub dissect_element($$$$$;$$);
sub dissect_element($$$$$;$$)
{
my $e = shift;
my $varpat = shift;
my $humanpat = shift;
my $length = shift;
my $refref = shift;
my $adjustlength = shift;
my $indent = shift // ' ' x 4;
given ($e->name()) {
when ('pad') {
my $bytes = $e->att('bytes');
my $align = $e->att('align');
if (defined $bytes) {
print $impl $indent."proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, $bytes, ENC_NA);\n";
print $impl $indent."*offsetp += $bytes;\n";
$length += $bytes;
} else {
say $impl $indent.'if (*offsetp % '.$align.') {';
say $impl $indent." proto_tree_add_item(t, hf_x11_unused, tvb, *offsetp, ($align - *offsetp % $align), ENC_NA);";
say $impl $indent." *offsetp += ($align - *offsetp % $align);";
say $impl $indent."}";
if ($length % $align != 0) {
$length += $align - $length % $align;
}
if ($adjustlength) {
say $impl $indent.'length = ((length + '.($align-1).') & ~'.($align-1).');';
}
}
}
when ('field') {
my $fieldname = $e->att('name');
my $regname = 'hf_x11_'.sprintf ($varpat, $fieldname);
my $type = $e->att('type');
if (get_simple_info($type)) {
my $info = get_simple_info($type);
my $size = $info->{'size'};
my $encoding = $info->{'encoding'};
my $get = $info->{'get'};
if ($e->att('enum') // $e->att('altenum')) {
my $fieldsize = $size * 8;
print $impl $indent;
if ($refref->{field}{$fieldname}) {
print $impl "f_$fieldname = ";
}
say $impl "field$fieldsize(tvb, offsetp, t, $regname, byte_order);";
} elsif ($e->att('mask')) {
if ($refref->{field}{$fieldname}) {
if ($get ne "tvb_get_guint8") {
say $impl $indent."f_$fieldname = $get(tvb, *offsetp, byte_order);";
} else {
say $impl $indent."f_$fieldname = $get(tvb, *offsetp);";
}
}
my $bitmask_field = $fieldname . "_bits";
say $impl $indent."{";
say $impl $indent." int* const $bitmask_field [] = {";
my $bit = $enum{$enum_name{$e->att('mask')}}{bit};
for my $val (sort { $a <=> $b } keys %$bit) {
my $item = $regname . '_mask_' . $$bit{$val};
say $impl "$indent$indent&$item,";
}
say $impl "$indent$indent" . "NULL";
say $impl $indent." };";
say $impl $indent." proto_tree_add_bitmask(t, tvb, *offsetp, $regname, ett_x11_rectangle, $bitmask_field, $encoding);";
say $impl $indent."}";
say $impl $indent."*offsetp += $size;";
} else {
if ($refref->{field}{$fieldname}) {
if ($get ne "tvb_get_guint8") {
say $impl $indent."f_$fieldname = $get(tvb, *offsetp, byte_order);";
} else {
say $impl $indent."f_$fieldname = $get(tvb, *offsetp);";
}
}
print $impl $indent."proto_tree_add_item(t, $regname, tvb, *offsetp, $size, $encoding);\n";
print $impl $indent."*offsetp += $size;\n";
}
$length += $size;
} elsif (get_struct_info($type)) {
# TODO: variable-lengths (when $info->{'size'} == 0 )
my $info = get_struct_info($type);
$length += $info->{'size'};
print $impl $indent."struct_$info->{'name'}(tvb, offsetp, t, byte_order, 1);\n";
} else {
die ("Unrecognized type: $type\n");
}
}
when ('list') {
my $fieldname = $e->att('name');
my $regname = 'hf_x11_'.sprintf ($varpat, $fieldname);
my $type = $e->att('type');
my $info = getinfo($type);
my $lencalc;
my $lentype = $e->first_child();
if (defined $info->{'size'}) {
$lencalc = "(length - $length) / $info->{'size'}";
} else {
$lencalc = "(length - $length)";
}
if (defined $lentype) {
given ($lentype->name()) {
when ('value') { $lencalc = $lentype->text(); }
when ('fieldref') { $lencalc = 'f_'.$lentype->text(); }
when ('paramref') { $lencalc = 'p_'.$lentype->text(); }
when ('op') { $lencalc = get_op($lentype); }
when (['unop','popcount']) { $lencalc = get_unop($lentype); }
when ('sumof') { $lencalc = 'sumof_'.$lentype->att('ref'); }
}
}
if (get_simple_info($type)) {
my $list = $info->{'list'};
my $size = $info->{'size'};
$regname .= ", $regname".'_item' if ($size > 1);
if ($refref->{sumof}{$fieldname}) {
my $get = $info->{'get'};
say $impl $indent."{";
say $impl $indent." int i;";
say $impl $indent." for (i = 0; i < $lencalc; i++) {";
if ($get ne "tvb_get_guint8") {
say $impl $indent." sumof_$fieldname += $get(tvb, *offsetp + i * $size, byte_order);";
} else {
say $impl $indent." sumof_$fieldname += $get(tvb, *offsetp + i * $size);";
}
say $impl $indent." }";
say $impl $indent."}";
}
print $impl $indent."$list(tvb, offsetp, t, $regname, $lencalc, byte_order);\n";
} elsif (get_struct_info($type)) {
my $si = get_struct_info($type);
my $prefs = "";
foreach my $pref (sort keys %{$si->{paramref}}) {
$prefs .= ", f_$pref";
}
print $impl $indent."struct_$info->{'name'}(tvb, offsetp, t, byte_order, $lencalc$prefs);\n";
} else {
# TODO: Fix unrecognized type. Comment out for now to generate dissector
# die ("Unrecognized type: $type\n");
}
if ($adjustlength && defined($lentype)) {
# Some requests end with a list of unspecified length
# Adjust the length field here so that the next $lencalc will be accurate
if (defined $info->{'size'}) {
say $impl $indent."length -= $lencalc * $info->{'size'};";
} else {
say $impl $indent."length -= $lencalc * 1;";
}
}
}
when ('switch') {
my $switchtype = $e->first_child() or die("Switch element not defined");
my $switchon = get_ref($switchtype, {});
my @elements = $e->children(qr/(bit)?case/);
for my $case (@elements) {
my @refs = $case->children('enumref');
my @test;
my $fieldname;
foreach my $ref (@refs) {
my $enum_ref = $ref->att('ref');
my $field = $ref->text();
$fieldname //= $field; # Use first named field
if ($case->name() eq 'bitcase') {
my $bit = $enum{$enum_name{$enum_ref}}{rbit}{$field};
if (! defined($bit)) {
for my $foo (keys %{$enum{$enum_name{$enum_ref}}{rbit}}) { say "'$foo'"; }
die ("Field '$field' not found in '$enum_ref'");
}
push @test , "$switchon & (1U << $bit)";
} else {
my $val = $enum{$enum_name{$enum_ref}}{rvalue}{$field};
if (! defined($val)) {
for my $foo (keys %{$enum{$enum_name{$enum_ref}}{rvalue}}) { say "'$foo'"; }
die ("Field '$field' not found in '$enum_ref'");
}
push @test , "$switchon == $val";
}
}
if (@test > 1) {
# We have more than one conditional, add parentheses to them.
# We don't add parentheses to all the conditionals because
# clang complains about the extra parens if you do "if ((x == y))".
my @tests_with_parens;
foreach my $conditional (@test) {
push @tests_with_parens, "($conditional)";
}
@test = @tests_with_parens;
}
my $list = join ' || ', @test;
say $impl $indent."if ($list) {";
my $vp = $varpat;
my $hp = $humanpat;
$vp =~ s/%s/${fieldname}_%s/;
$hp =~ s/%s/${fieldname}.%s/;
my @sub_elements = $case->children(qr/pad|field|list|switch/);
my $subref = { field => {}, sumof => {} };
foreach my $sub_e (@sub_elements) {
reference_elements($sub_e, $subref);
}
foreach my $sub_e (@sub_elements) {
register_element($sub_e, $vp, $hp, $subref, $indent . ' ');
}
foreach my $sub_e (@sub_elements) {
$length = dissect_element($sub_e, $vp, $hp, $length, $subref, $adjustlength, $indent . ' ');
}
say $impl $indent."}";
}
}
default { die "Unknown field type: $_\n"; }
}
return $length;
}
sub struct {
my ($t, $elt) = @_;
my $name = $elt->att('name');
my $qualname = qualname($name);
$type_name{$name} = $qualname;
if (defined $struct{$qualname}) {
$t->purge;
return;
}
my @elements = $elt->children(qr/pad|field|list|switch/);
print(" - Struct $name\n");
$name = $qualname;
$name =~ s/:/_/;
my %refs;
my %paramrefs;
my $size = 0;
my $dynamic = 0;