tests: add regression tests for Follow TCP Stream
[metze/wireshark/wip.git] / tools / convert_proto_tree_add_text.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright 2013 Michael Mann (see AUTHORS file)
4 #
5 # A program to help convert proto_tree_add_text calls into filterable "items" that
6 # use proto_tree_add_item.  The program requires 2 passes.  "Pass 1" (generate) collects 
7 # the eligible proto_tree_add_text calls and outputs the necessary data into a delimited
8 # file.  "Pass 2" (fix-all) takes the data from the delimited file and replaces the
9 # proto_tree_add_text calls with proto_tree_add_item or "expert info" calls as well as 
10 # generating separate files for the hf and/or ei variable declarations and hf and/or ei array data.
11 # The hf "files" can be copy/pasted into the dissector where appropriate (until such time as 
12 # its done automatically)
13 #
14 # Note that the output from "Pass 1" won't always be a perfect conversion for "Pass 2", so
15 # "human interaction" is needed as an intermediary to verify and update the delimited file
16 # before "Pass 2" is done.
17 # It is also recommended to run checkhf.pl and checkAPIs.pl after "Pass 2" is completed.
18 #
19 # Delimited file field format:
20 # <convert proto_tree_add_text_call[0|1|10-13]><add hf or ei variable[0|1|2]><proto_tree var><hf var><tvb var><offset><length><encoding|[EXPERT_GROUPS]>
21 # <[FIELDNAME]><[FIELDTYPE]|[EXPERT_SEVERITY]><[FIELDABBREV]><[FIELDDISPLAY]><[FIELDCONVERT]><[BITMASK]>
22 #
23 # convert proto_tree_add_text_call enumerations:
24 # 0  - no conversions
25 # 1  - proto_tree_add_item
26 # 10 - expert_add_info
27 # 11 - expert_add_info_format
28 # 12 - proto_tree_add_expert
29 # 13 - proto_tree_add_expert_format
30 #
31 # Usage: convert_proto_tree_add_text.pl action=<generate|fix-all> <file or files>
32 #
33 # Lots of code shamelessly borrowed from fix-encoding-args.pl (Thanks Bill!)
34 #
35 # Wireshark - Network traffic analyzer
36 # By Gerald Combs <gerald@wireshark.org>
37 # Copyright 1998 Gerald Combs
38 #
39 # SPDX-License-Identifier: GPL-2.0-or-later
40 #
41
42 use strict;
43 use warnings;
44
45 use Getopt::Long;
46
47 my %DISPLAY_BASE = ('BASE_NONE' => "BASE_NONE",
48                                            'BASE_DEC' => "BASE_DEC",
49                                            'BASE_HEX' => "BASE_HEX",
50                                            'BASE_OCT' => "BASE_OCT",
51                                            'BASE_DEC_HEX' => "BASE_DEC_HEX",
52                                            'BASE_HEX_DEC' => "BASE_HEX_DEC",
53                                            'BASE_EXT_STRING' => "BASE_EXT_STRING",
54                                            'BASE_RANGE_STRING' => "BASE_RANGE_STRING",
55                                            'ABSOLUTE_TIME_LOCAL' => "ABSOLUTE_TIME_LOCAL",
56                                            'ABSOLUTE_TIME_UTC' => "ABSOLUTE_TIME_UTC",
57                                            'ABSOLUTE_TIME_DOY_UTC' => "ABSOLUTE_TIME_DOY_UTC",
58                                            'BASE_CUSTOM' => "BASE_CUSTOM");
59
60 my %ENCODINGS = ('ENC_BIG_ENDIAN' => "ENC_BIG_ENDIAN",
61                                            'ENC_LITTLE_ENDIAN' => "ENC_LITTLE_ENDIAN",
62                                            'ENC_TIME_SECS_NSECS' => "ENC_TIME_SECS_NSECS",
63                                            'ENC_TIME_NTP' => "ENC_TIME_NTP",
64                                            'ENC_ASCII' => "ENC_ASCII",
65                                            'ENC_UTF_8' => "ENC_UTF_8",
66                                            'ENC_UTF_16' => "ENC_UTF_16",
67                                            'ENC_UCS_2' => "ENC_UCS_2",
68                                            'ENC_EBCDIC' => "ENC_EBCDIC",
69                                            'ENC_NA' => "ENC_NA");
70
71 my %FIELD_TYPE = ('FT_NONE' => "FT_NONE", 'FT_PROTOCOL' => "FT_PROTOCOL", 'FT_BOOLEAN' => "FT_BOOLEAN",
72                                    'FT_UINT8' => "FT_UINT8", 'FT_UINT16' => "FT_UINT16", 'FT_UINT24' => "FT_UINT24", 'FT_UINT32' => "FT_UINT32", 'FT_UINT64' => "FT_UINT64",
73                                    'FT_INT8' => "FT_INT8", 'FT_INT16' => "FT_INT16", 'FT_INT24' => "FT_INT24", 'FT_INT32' => "FT_INT32", 'FT_INT64' => "FT_INT64",
74                                    'FT_FLOAT' => "FT_FLOAT", 'FT_DOUBLE' => "FT_DOUBLE",
75                                    'FT_ABSOLUTE_TIME' => "FT_ABSOLUTE_TIME", 'FT_RELATIVE_TIME' => "FT_RELATIVE_TIME",
76                                    'FT_STRING' => "FT_STRING", 'FT_STRINGZ' => "FT_STRINGZ", 'FT_UINT_STRING' => "FT_UINT_STRING",
77                                    'FT_ETHER' => "FT_ETHER", 'FT_BYTES' => "FT_BYTES", 'FT_UINT_BYTES' => "FT_UINT_BYTES",
78                                    'FT_IPv4' => "FT_IPv4", 'FT_IPv6' => "FT_IPv6", 'FT_IPXNET' => "FT_IPXNET", 'FT_AX25' => "FT_AX25", 'FT_VINES' => "FT_VINES",
79                                    'FT_FRAMENUM' => "FT_FRAMENUM", 'FT_PCRE' => "FT_PCRE", 'FT_GUID' => "FT_GUID", 'FT_OID' => "FT_OID", 'FT_REL_OID' => "FT_REL_OID", 'FT_EUI64' => "FT_EUI64");
80
81 my %EXPERT_SEVERITY = ('PI_COMMENT' => "PI_COMMENT",
82                                            'PI_CHAT' => "PI_CHAT",
83                                            'PI_NOTE' => "PI_NOTE",
84                                            'PI_WARN' => "PI_WARN",
85                                            'PI_ERROR' => "PI_ERROR");
86
87 my %EXPERT_GROUPS = ('PI_CHECKSUM' => "PI_CHECKSUM",
88                                            'PI_SEQUENCE' => "PI_SEQUENCE",
89                                            'PI_RESPONSE_CODE' => "PI_RESPONSE_CODE",
90                                            'PI_REQUEST_CODE' => "PI_REQUEST_CODE",
91                                            'PI_UNDECODED' => "PI_UNDECODED",
92                                            'PI_REASSEMBLE' => "PI_REASSEMBLE",
93                                            'PI_MALFORMED' => "PI_MALFORMED",
94                                            'PI_DEBUG' => "PI_DEBUG",
95                                            'PI_PROTOCOL' => "PI_PROTOCOL",
96                                            'PI_SECURITY' => "PI_SECURITY",
97                                            'PI_COMMENTS_GROUP' => "PI_COMMENTS_GROUP");
98
99 my @proto_tree_list;
100 my @expert_list;
101 my $protabbrev = "";
102
103 # Perl trim function to remove whitespace from the start and end of the string
104 sub trim($)
105 {
106         my $string = shift;
107         $string =~ s/^\s+//;
108         $string =~ s/\s+$//;
109         return $string;
110 }
111
112 # ---------------------------------------------------------------------
113 #
114 # MAIN
115 #
116 my $helpFlag  = '';
117 my $action    = 'generate';
118 my $encoding  = '';
119 my $expert  = '';
120
121 my $result = GetOptions(
122                                                 'action=s' => \$action,
123                                                 'encoding=s' => \$encoding,
124                                                 'expert'   => \$expert,
125                                                 'help|?'   => \$helpFlag
126                                                 );
127
128 if (!$result || $helpFlag || !$ARGV[0]) {
129         usage();
130 }
131
132 sub usage {
133         print "\nUsage: $0 [--action=generate|fix-all|find-all] [--encoding=ENC_BIG_ENDIAN|ENC_LITTLE_ENDIAN] FILENAME [...]\n\n";
134                 print "  --action = generate (default)\n";
135                 print "    generate - create a delimited file (FILENAME.proto_tree_input) with\n";
136                 print "               proto_tree_add_text fields in FILENAME(s)\n";
137                 print "    fix-all  - Use delimited file (FILENAME.proto_tree_input) to convert\n";
138                 print "               proto_tree_add_text to proto_tree_add_item\n";
139                 print "               Also generates FILENAME.hf and FILENAME.hf_array to be\n";
140                 print "               copy/pasted into the dissector where appropriate\n";
141                 print "    find-all - Output the number of eligible proto_tree_add_text calls\n";
142                 print "               for conversion\n\n";
143                 print "  --expert     (Optional) Includes proto_tree_add_text calls with no printf arguments in\n";
144                 print "               the .proto_tree_input file as they could be converted to expert info\n";
145                 print "               (otherwise they are ignored)\n";
146                 print "               Must be called for 'fix-all' if called on 'generate'\n";
147                 print "  --encoding   (Optional) Default encoding if one can't be determined\n";
148                 print "               (effective only for generate)\n";
149                 print "               If not specified, an encoding will not be auto-populated\n";
150                 print "               if undetermined\n\n";
151
152         exit(1);
153 }
154
155 #
156 # XXX Outline general algorithm here
157 #
158 my $found_total = 0;
159 my $protabbrev_index;
160 my $line_number = 0;
161
162 while (my $fileName = $ARGV[0]) {
163         shift;
164         my $fileContents = '';
165
166         die "No such file: \"$fileName\"\n" if (! -e $fileName);
167
168         # delete leading './'
169         $fileName =~ s{ ^ \. / } {}xo;
170
171         #determine PROTABBREV for dissector based on file name format of (dirs)/packet-PROTABBREV.c
172         $protabbrev_index = rindex($fileName, "packet-");
173         if ($protabbrev_index == -1) {
174                 print "$fileName doesn't fit format of packet-PROTABBREV.c\n";
175                 next;
176         }
177
178         $protabbrev = substr($fileName, $protabbrev_index+length("packet-"));
179         $protabbrev_index = rindex($protabbrev, ".");
180         if ($protabbrev_index == -1) {
181                 print "$fileName doesn't fit format of packet-PROTABBREV.c\n";
182                 next;
183         }
184         $protabbrev = lc(substr($protabbrev, 0, $protabbrev_index));
185
186         # Read in the file (ouch, but it's easier that way)
187         open(FCI, "<", $fileName) || die("Couldn't open $fileName");
188         while (<FCI>) {
189                 $fileContents .= $_;
190         }
191         close(FCI);
192
193         if ($action eq "generate") {
194                 generate_hfs(\$fileContents, $fileName);
195         }
196
197         if ($action eq "fix-all") {
198                 # Read in the hf "input" file
199                 $line_number = 0;
200                 my $errors = 0;
201                 open(FCI, "<", $fileName . ".proto_tree_input") || die("Couldn't open $fileName.proto_tree_input");
202                 while(my $line=<FCI>){
203                         my @proto_tree_item = split(/;|\n/, $line);
204
205                         $line_number++;
206                         $errors += verify_line(@proto_tree_item);
207
208                         push(@proto_tree_list, \@proto_tree_item);
209                         if ($proto_tree_item[1] eq "2") {
210                                 push(@expert_list, \@proto_tree_item);
211                         }
212                 }
213                 close(FCI);
214
215                 if ($errors > 0) {
216                         print "Aborting conversion.\n";
217                         exit(-1);
218                 }
219
220                 fix_proto_tree_add_text(\$fileContents, $fileName);
221
222                 # Write out the hf data
223                 output_hf_array($fileName);
224                 output_hf($fileName);
225
226                 # Write out the changed version to a file
227                 open(FCO, ">", $fileName . ".proto_tree_add_text");
228                 print FCO "$fileContents";
229                 close(FCO);
230         }
231
232         if ($action eq "find-all") {
233                 # Find all proto_tree_add_text() statements eligible for conversion
234                 $found_total += find_all(\$fileContents, $fileName);
235         }
236
237 } # while
238
239 exit $found_total;
240
241 # ---------------------------------------------------------------------
242 # Sanity check the data in the .proto_tree_input file
243 sub verify_line {
244         my( @proto_tree_item) = @_;
245         my $errors = 0;
246
247         #do some basic error checking of the file
248         if ($proto_tree_item[0] eq "1") {
249                 if (!($proto_tree_item[3] =~ /^hf_/)) {
250                         print "$line_number: Poorly formed hf_ variable ($proto_tree_item[3])!\n";
251                         $errors++;
252                 }
253
254                 foreach (split(/\|/, $proto_tree_item[7])) {
255                         if (!exists($ENCODINGS{$_})) {
256                                 print "$line_number: Encoding value '$_' unknown!\n";
257                                 $errors++;
258                         }
259                 }
260         } elsif (($proto_tree_item[0] eq "10") ||
261                          ($proto_tree_item[0] eq "11") ||
262                          ($proto_tree_item[0] eq "12") ||
263                          ($proto_tree_item[0] eq "13")) {
264                 #expert info conversions
265                 if (!($proto_tree_item[3] =~ /^ei_/)) {
266                         print "$line_number: Poorly formed ei_ variable ($proto_tree_item[3])!\n";
267                         $errors++;
268                 }
269         } elsif ($proto_tree_item[0] ne "0") {
270                 print "Bad conversion value!  Aborting conversion.\n";
271                 $errors++;
272         }
273
274         if ($proto_tree_item[1] eq "1") {
275                 if (!($proto_tree_item[3] =~ /^hf_/)) {
276                         print "$line_number: Poorly formed hf_ variable ($proto_tree_item[3])!\n";
277                         $errors++;
278                 }
279                 if (!exists($FIELD_TYPE{$proto_tree_item[9]})) {
280                         print "$line_number: Field type '$proto_tree_item[9]' unknown!\n";
281                         $errors++;
282                 }
283                 foreach (split(/\|/, $proto_tree_item[11])) {
284                         if ((!exists($DISPLAY_BASE{$_})) &&
285                                 (!($proto_tree_item[11] =~ /\d+/))) {
286                                 print "$line_number: Display base '$proto_tree_item[11]' unknown!\n";
287                                 $errors++;
288                         }
289                 }
290                 if (($proto_tree_item[9] eq "FT_UINT8") ||
291                         ($proto_tree_item[9] eq "FT_UINT16") ||
292                         ($proto_tree_item[9] eq "FT_UINT24") ||
293                         ($proto_tree_item[9] eq "FT_UINT32") ||
294                         ($proto_tree_item[9] eq "FT_UINT64") ||
295                         ($proto_tree_item[9] eq "FT_INT8") ||
296                         ($proto_tree_item[9] eq "FT_INT16") ||
297                         ($proto_tree_item[9] eq "FT_INT24") ||
298                         ($proto_tree_item[9] eq "FT_INT32") ||
299                         ($proto_tree_item[9] eq "FT_INT64")) {
300                         if ($proto_tree_item[11] eq "BASE_NONE") {
301                                 print "$line_number: Interger type should not be BASE_NONE!\n";
302                                 $errors++;
303                         }
304                 }
305
306         } elsif ($proto_tree_item[1] eq "2") {
307                 if (!($proto_tree_item[3] =~ /^ei_/)) {
308                         print "$line_number: Poorly formed ei_ variable ($proto_tree_item[3])!\n";
309                         $errors++;
310                 }
311                 if (!exists($EXPERT_SEVERITY{$proto_tree_item[9]})) {
312                         print "$line_number: Expert severity value '$proto_tree_item[9]' unknown!\n";
313                         $errors++;
314                 }
315                 if (!exists($EXPERT_GROUPS{$proto_tree_item[7]})) {
316                         print "$line_number: Expert group value '$proto_tree_item[7]' unknown!\n";
317                         $errors++;
318                 }
319
320         } elsif ($proto_tree_item[1] ne "0") {
321                         print "$line_number: Bad hf/ei variable generation value!\n";
322                         $errors++;
323         }
324
325         return $errors;
326 }
327
328 sub generate_hfs {
329         my( $fileContentsRef, $fileName) = @_;
330         my @args;
331         my $num_items = 0;
332         my @temp;
333         my $str_temp;
334         my $pat;
335
336         if ($expert ne "") {
337                 $pat = qr /
338                                         (
339                                                  (?:proto_tree_add_text)\s* \(
340                                                  (([^[\,;])*\,){4,}
341                                                  [^;]*
342                                                  \s* \) \s* ;
343                                         )
344                                 /xs;
345         } else {
346                 $pat = qr /
347                                         (
348                                                  (?:proto_tree_add_text)\s* \(
349                                                  (([^[\,;])*\,){5,}
350                                                  [^;]*
351                                                  \s* \) \s* ;
352                                         )
353                                 /xs;
354         }
355
356         while ($$fileContentsRef =~ / $pat /xgso) {
357                 my @proto_tree_item = (1, 1, "tree", "hf_name", "tvb", "offset", "length", "encoding",
358                                                            "fieldfullname", "fieldtype", "fieldabbrevname", "BASE_NONE", "NULL", "0x0");
359                 my $str = "${1}\n";
360                 $str =~ tr/\t\n\r/ /d;
361                 $str =~ s/ \s+ / /xg;
362                 #print "$fileName: $str\n";
363
364                 @args = split(/,/, $str);
365                 #printf "ARGS(%d): %s\n", scalar @args, join("# ", @args);
366                 $args[0] =~ s/proto_tree_add_text\s*\(\s*//;
367                 $proto_tree_item[2] = $args[0];                 #tree
368                 $proto_tree_item[4] = trim($args[1]);   #tvb
369                 $proto_tree_item[5] = trim($args[2]);   #offset
370                 $proto_tree_item[6] = trim($args[3]);   #length
371                 if (scalar @args == 5) {
372                         #remove the "); at the end
373                         $args[4] =~ s/\"\s*\)\s*;$//;
374                 }
375
376                 #encoding
377                 if (scalar @args > 5) {
378                         if (($proto_tree_item[6] eq "1") ||
379                                 ($args[5] =~ /tvb_get_guint8/) ||
380                                 ($args[5] =~ /tvb_bytes_to_str/) ||
381                                 ($args[5] =~ /tvb_ether_to_str/))  {
382                                 $proto_tree_item[7] = "ENC_NA";
383                         } elsif ($args[5] =~ /tvb_get_ntoh/) {
384                                 $proto_tree_item[7] = "ENC_BIG_ENDIAN";
385                         } elsif ($args[5] =~ /tvb_get_letoh/) {
386                                 $proto_tree_item[7] = "ENC_LITTLE_ENDIAN";
387                         } elsif (($args[5] =~ /tvb_get_ephemeral_string/) || 
388                                          ($args[5] =~ /tvb_format_text/)){
389                                 $proto_tree_item[7] = "ENC_NA|ENC_ASCII";
390                         } elsif ($encoding ne "") {
391                                 $proto_tree_item[7] = $encoding;
392                         }
393                 }
394
395                 #field full name
396                 if (($expert ne "") || (scalar @args > 5)) {
397                         my @arg_temp = split(/=|:/, $args[4]);
398                         $proto_tree_item[8] = $arg_temp[0];
399                 } else {
400                         $proto_tree_item[8] = $args[4];
401                 }
402                 $proto_tree_item[8] =~ s/\"//;
403                 $proto_tree_item[8] = trim($proto_tree_item[8]);
404
405                 if ($proto_tree_item[8] eq "%s\"") {
406                         #assume proto_tree_add_text will not be converted
407                         $proto_tree_item[0] = 0;
408                         $proto_tree_item[1] = 0;
409                         $proto_tree_item[3] = sprintf("hf_%s_", $protabbrev);
410                         $proto_tree_item[10] = sprintf("%s.", $protabbrev);
411                 } else {
412                         #hf variable name
413                         $proto_tree_item[3] = sprintf("hf_%s_%s", $protabbrev, lc($proto_tree_item[8]));
414                         $proto_tree_item[3] =~ s/\s+|-|:/_/g;
415
416                         #field abbreviated name
417                         $proto_tree_item[10] = sprintf("%s.%s", $protabbrev, lc($proto_tree_item[8]));
418                         $proto_tree_item[10] =~ s/\s+|-|:/_/g;
419                 }
420
421                 #VALS
422                 if ($str =~ /val_to_str(_const)?\(\s*tvb_get_[^\(]*\([^\,]*,[^\)]*\)\s*\,\s*([^\,]*)\s*\,\s*([^\)]*)\)/) {
423                         $proto_tree_item[12] = sprintf("VALS(%s)", trim($2));
424                 } elsif ($str =~ /val_to_str(_const)?\([^\,]*\,([^\,]*)\,/) {
425                         $proto_tree_item[12] = sprintf("VALS(%s)", trim($2));
426                 } elsif ($str =~ /val_to_str_ext(_const)?\(\s*tvb_get_[^\(]*\([^\,]*,[^\)]*\)\s*\,\s*([^\,]*)\s*\,\s*([^\)]*)\)/) {
427                         $proto_tree_item[12] = trim($2);
428                 } elsif ($str =~ /val_to_str_ext(_const)?\([^\,]*\,([^\,]*)\,/) {
429                         $proto_tree_item[12] = trim($2);
430                 }
431
432                 #field type
433                 if (scalar @args > 5) {
434                         if ($args[5] =~ /tvb_get_guint8/) {
435                                 if ($args[4] =~ /%[0-9]*[i]/) {
436                                         $proto_tree_item[9] = "FT_INT8";
437                                 } else {
438                                         $proto_tree_item[9] = "FT_UINT8";
439                                 }
440                         } elsif ($args[5] =~ /tvb_get_(n|"le")tohs/) {
441                                 if ($args[4] =~ /%[0-9]*[i]/) {
442                                         $proto_tree_item[9] = "FT_INT16";
443                                 } else {
444                                         $proto_tree_item[9] = "FT_UINT16";
445                                 }
446                         } elsif ($args[5] =~ /tvb_get_(n|"le")toh24/) {
447                                 if ($args[4] =~ /%[0-9]*[i]/) {
448                                         $proto_tree_item[9] = "FT_INT24";
449                                 } else {
450                                         $proto_tree_item[9] = "FT_UINT24";
451                                 }
452                         } elsif ($args[5] =~ /tvb_get_(n|"le")tohl/) {
453                                 if ($args[4] =~ /%[0-9]*[i]/) {
454                                         $proto_tree_item[9] = "FT_INT32";
455                                 } else {
456                                         $proto_tree_item[9] = "FT_UINT32";
457                                 }
458                         } elsif ($args[5] =~ /tvb_get_(n|"le")toh("40"|"48"|"56"|"64")/) {
459                                 if ($args[4] =~ /%[0-9]*[i]/) {
460                                         $proto_tree_item[9] = "FT_INT64";
461                                 } else {
462                                         $proto_tree_item[9] = "FT_UINT64";
463                                 }
464                         } elsif (($args[5] =~ /tvb_get_(n|"le")tohieee_float/) ||
465                                          ($args[4] =~ /%[0-9\.]*[fFeEgG]/)) {
466                                 $proto_tree_item[9] = "FT_FLOAT";
467                         } elsif ($args[5] =~ /tvb_get_(n|"le")tohieee_double/) {
468                                 $proto_tree_item[9] = "FT_DOUBLE";
469                         } elsif (($args[5] =~ /tvb_get_ipv4/) ||
470                                          ($args[5] =~ /tvb_ip_to_str/)) {
471                                 $proto_tree_item[9] = "FT_IPv4";
472                         } elsif (($args[5] =~ /tvb_get_ipv6/) ||
473                                          ($args[5] =~ /tvb_ip6_to_str/)) {
474                                 $proto_tree_item[9] = "FT_IPv6";
475                         } elsif ($args[5] =~ /tvb_get_(n|"le")tohguid/) {
476                                 $proto_tree_item[9] = "FT_GUID";
477                         } elsif ($args[5] =~ /tvb_get_ephemeral_stringz/) {
478                                 $proto_tree_item[9] = "FT_STRINGZ";
479                         } elsif (($args[5] =~ /tvb_get_ephemeral_string/) || 
480                                          ($args[5] =~ /tvb_format_text/)){
481                                 $proto_tree_item[9] = "FT_STRING";
482                         } elsif (($args[5] =~ /tvb_bytes_to_str/)) {
483                                 $proto_tree_item[9] = "FT_BYTES";
484                         } elsif ($args[5] =~ /tvb_ether_to_str/) {
485                                 $proto_tree_item[9] = "FT_ETHER";
486                         }
487
488                         #if we still can't determine type, assume a constant length
489                         #value means we have an unsigned value
490                         if ($proto_tree_item[9] eq "fieldtype") {
491                                 my $len_str = trim($args[3]);
492                                 if ($len_str eq "1") {
493                                         $proto_tree_item[9] = "FT_UINT8";
494                                 } elsif ($len_str eq "2") {
495                                         $proto_tree_item[9] = "FT_UINT16";
496                                 } elsif ($len_str eq "3") {
497                                         $proto_tree_item[9] = "FT_UINT24";
498                                 } elsif ($len_str eq "4") {
499                                         $proto_tree_item[9] = "FT_UINT32";
500                                 } elsif ($len_str eq "8") {
501                                         $proto_tree_item[9] = "FT_UINT64";
502                                 }
503                         }
504                 }
505
506                 #display base
507                 if ($args[4] =~ /%[0-9]*[xX]/) {
508                         $proto_tree_item[11] = "BASE_HEX";
509                 } elsif ($args[4] =~ /%[0-9]*[uld]/) {
510                         $proto_tree_item[11] = "BASE_DEC";
511                 } elsif ($args[4] =~ /%[0-9]*o/) {
512                         $proto_tree_item[11] = "BASE_OCT";
513                 }
514                 if ($str =~ /val_to_str_ext(_const)?\([^\,]*\,([^\,]*)\,/) {
515                         $proto_tree_item[11] .= "|BASE_EXT_STRING";
516                 }
517
518                 if (($proto_tree_item[7] eq "encoding") && ($proto_tree_item[9] eq "FT_BYTES")) {
519                         $proto_tree_item[7] = "ENC_NA";
520                 }
521
522                 push(@proto_tree_list, \@proto_tree_item);
523
524                 $num_items += 1;
525         }
526
527         if ($num_items > 0) {
528                 open(FCO, ">", $fileName . ".proto_tree_input");
529                 for my $item (@proto_tree_list) {
530                         print FCO join(";", @{$item}), "\n";
531                 }
532                 close(FCO);
533         }
534 }
535
536 # ---------------------------------------------------------------------
537 # Find all proto_tree_add_text calls and replace them with the data
538 # found in proto_tree_list
539 sub fix_proto_tree_add_text {
540         my( $fileContentsRef, $fileName) = @_;
541         my $found = 0;
542         my $pat;
543
544         if ($expert ne "") {
545                 $pat = qr /
546                                         (
547                                                  (?:proto_tree_add_text)\s* \(
548                                                  (([^[\,;])*\,){4,}
549                                                  [^;]*
550                                                  \s* \) \s* ;
551                                         )
552                                 /xs;
553         } else {
554                 $pat = qr /
555                                         (
556                                                  (?:proto_tree_add_text)\s* \(
557                                                  (([^[\,;])*\,){5,}
558                                                  [^;]*
559                                                  \s* \) \s* ;
560                                         )
561                                 /xs;
562         }
563
564         $$fileContentsRef =~ s/ $pat /patsub($found, $1)/xges;
565 }
566
567 # ---------------------------------------------------------------------
568 # Format proto_tree_add_item or expert info functions with proto_tree_list data
569 sub patsub {
570         my $item_str;
571         if ($proto_tree_list[$_[0]][0] eq "1") {
572                 $item_str = sprintf("proto_tree_add_item(%s, %s, %s, %s, %s, %s);",
573                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3],
574                                                  $proto_tree_list[$_[0]][4], $proto_tree_list[$_[0]][5],
575                                                  $proto_tree_list[$_[0]][6], $proto_tree_list[$_[0]][7]);
576         } elsif ($proto_tree_list[$_[0]][0] eq "10") {
577                 $item_str = sprintf("expert_add_info(pinfo, %s, &%s);",
578                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3]);
579         } elsif ($proto_tree_list[$_[0]][0] eq "11") {
580                 $item_str = sprintf("expert_add_info_format(pinfo, %s, &%s, \"%s\"",
581                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3],
582                                                  $proto_tree_list[$_[0]][8]);
583                 if ($proto_tree_list[$_[0]][11] ne "") {
584                         $item_str .= ", $proto_tree_list[$_[0]][11]";
585                 }
586                 $item_str .= ");";
587         } elsif ($proto_tree_list[$_[0]][0] eq "12") {
588                 $item_str = sprintf("proto_tree_add_expert(%s, pinfo, &%s, %s, %s, %s);",
589                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3],
590                                                  $proto_tree_list[$_[0]][4], $proto_tree_list[$_[0]][5],
591                                                  $proto_tree_list[$_[0]][6]);
592         } elsif ($proto_tree_list[$_[0]][0] eq "13") {
593                 $item_str = sprintf("proto_tree_add_expert_format(%s, pinfo, &%s, %s, %s, %s, \"%s\"",
594                                                  $proto_tree_list[$_[0]][2], $proto_tree_list[$_[0]][3],
595                                                  $proto_tree_list[$_[0]][4], $proto_tree_list[$_[0]][5],
596                                                  $proto_tree_list[$_[0]][6], $proto_tree_list[$_[0]][8]);
597                 if ($proto_tree_list[$_[0]][11] ne "") {
598                         $item_str .= ", $proto_tree_list[$_[0]][11]";
599                 }
600                 $item_str .= ");";
601         } else {
602                 $item_str = $1;
603         }
604
605         $_[0] += 1;
606
607         return $item_str;
608 }
609
610 # ---------------------------------------------------------------------
611 # Output the hf variable declarations.  For now, write them to a file.
612 # XXX - Eventually find the right place to add it to the modified dissector file
613 sub output_hf {
614         my( $fileName) = @_;
615         my %hfs = ();
616         my %eis = ();
617         my $index;
618         my $key;
619
620         open(FCO, ">", $fileName . ".hf");
621
622         print FCO "/* Generated from convert_proto_tree_add_text.pl */\n";
623
624         #add hfs to hash table to prevent against (accidental) duplicates
625         for ($index=0;$index<@proto_tree_list;$index++) {
626                 if ($proto_tree_list[$index][1] eq "1") {
627                         $hfs{$proto_tree_list[$index][3]} = $proto_tree_list[$index][3];
628                         print FCO "static int $proto_tree_list[$index][3] = -1;\n";
629                 } elsif ($proto_tree_list[$index][1] eq "2") {
630                         $eis{$proto_tree_list[$index][3]} = $proto_tree_list[$index][3];
631                 }
632         }
633
634         if (scalar keys %hfs > 0) {
635                 print FCO "\n\n";
636         }
637
638         print FCO "/* Generated from convert_proto_tree_add_text.pl */\n";
639
640         foreach $key (keys %eis) {
641                 print FCO "static expert_field $key = EI_INIT;\n";
642         }
643         close(FCO);
644
645 }
646
647 # ---------------------------------------------------------------------
648 # Output the hf array items.  For now, write them to a file.
649 # XXX - Eventually find the right place to add it to the modified dissector file
650 # (bonus points if formatting of hf array in dissector file is kept)
651 sub output_hf_array {
652         my( $fileName) = @_;
653         my $index;
654         my %hfs = ();
655         my %eis = ();
656
657         open(FCO, ">", $fileName . ".hf_array");
658
659         print FCO "      /* Generated from convert_proto_tree_add_text.pl */\n";
660
661         for ($index=0;$index<@proto_tree_list;$index++) {
662                 if ($proto_tree_list[$index][1] eq "1") {
663                         if (exists($hfs{$proto_tree_list[$index][3]})) {
664                                 print "duplicate hf entry '$proto_tree_list[$index][3]' found!  Aborting conversion.\n";
665                                 exit(-1);
666                         }
667                         $hfs{$proto_tree_list[$index][3]} = $proto_tree_list[$index][3];
668                         print FCO "      { &$proto_tree_list[$index][3], { \"$proto_tree_list[$index][8]\", \"$proto_tree_list[$index][10]\", ";
669                         print FCO "$proto_tree_list[$index][9], $proto_tree_list[$index][11], $proto_tree_list[$index][12], $proto_tree_list[$index][13], NULL, HFILL }},\r\n";
670                 }
671         }
672
673         if ($index > 0) {
674                 print FCO "\n\n";
675         }
676
677         print FCO "      /* Generated from convert_proto_tree_add_text.pl */\n";
678         for ($index=0;$index<@expert_list;$index++) {
679                 if (exists($eis{$expert_list[$index][3]})) {
680                         print "duplicate ei entry '$expert_list[$index][3]' found!  Aborting conversion.\n";
681                         exit(-1);
682                 }
683                 $eis{$expert_list[$index][3]} = $expert_list[$index][3];
684
685                 print FCO "      { &$expert_list[$index][3], { \"$expert_list[$index][10]\", $expert_list[$index][7], ";
686                 print FCO "$expert_list[$index][9], \"$expert_list[$index][8]\", EXPFILL }},\r\n";
687         }
688
689         close(FCO);
690 }
691
692 # ---------------------------------------------------------------------
693 # Find all proto_tree_add_text calls that have parameters passed in them
694 # and output number found
695
696 sub find_all {
697         my( $fileContentsRef, $fileName) = @_;
698
699         my $found = 0;
700         my $tvb_found = 0;
701         my $pat;
702         my $tvb_percent;
703
704         if ($expert ne "") {
705                 $pat = qr /
706                                         (
707                                                  (?:proto_tree_add_text)\s* \(
708                                                  (([^[\,;])*\,){4,}
709                                                  [^;]*
710                                                  \s* \) \s* ;
711                                         )
712                                 /xs;
713         } else {
714                 $pat = qr /
715                                         (
716                                                  (?:proto_tree_add_text)\s* \(
717                                                  (([^[\,;])*\,){5,}
718                                                  [^;]*
719                                                  \s* \) \s* ;
720                                         )
721                                 /xs;
722         }
723
724         while ($$fileContentsRef =~ / $pat /xgso) {
725                 my $str = "${1}\n";
726                 my @args = split(/,/, ${1});
727
728                 #cleanup whitespace to show proto_tree_add_text in single line (easier for seeing grep results)
729                 $str =~ tr/\t\n\r/ /d;
730                 $str =~ s/ \s+ / /xg;
731                 #print "$fileName: $str\n";
732
733                 #find all instances where proto_tree_add_text has a tvb_get (or similar) call, because
734                 #convert_proto_tree_add_text.pl has an easier time determining hf_ field values with it
735                 if (scalar @args > 5) {
736                         my $tvb = trim($args[5]);
737                         if ($tvb =~ /^tvb_/) {
738                                 $tvb_found += 1;
739                         }
740                 }
741
742                 $found += 1;
743         }
744
745         if ($found > 0) {
746                 if ($tvb_found > 0) {
747                         $tvb_percent = 100*$tvb_found/$found;
748
749                         printf "%s: Found %d proto_tree_add_text calls eligible for conversion, %d contain a \"tvb get\" call (%.2f%%).\n",
750                                 $fileName, $found, $tvb_found, $tvb_percent;
751                 } else {
752                         print "$fileName: Found $found proto_tree_add_text calls eligible for conversion, 0 \"tvb get\" calls.\n";
753                 }
754         }
755         return $found;
756 }