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