nexmon – Blame information for rev 1

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