3 # Copyright 2011, William Meier <wmeier[AT]newsguy.com>
5 # A program to fix encoding args for certain Wireshark API function calls
6 # from TRUE/FALSE to ENC_?? as appropriate (and possible)
7 # - proto_tree_add_item
8 # - proto_tree_add_bits_item
9 # - proto_tree_add_bits_ret_val
10 # - proto_tree_add_bitmask
11 # - proto_tree_add_bitmask_text !! ToDo: encoding arg not last arg
18 # - ptvcursor_add_no_advance
19 # - ptvcursor_add_with_subtree !! ToDo: encoding arg not last arg
24 # Wireshark - Network traffic analyzer
25 # By Gerald Combs <gerald@wireshark.org>
26 # Copyright 1998 Gerald Combs
28 # This program is free software; you can redistribute it and/or
29 # modify it under the terms of the GNU General Public License
30 # as published by the Free Software Foundation; either version 2
31 # of the License, or (at your option) any later version.
33 # This program is distributed in the hope that it will be useful,
34 # but WITHOUT ANY WARRANTY; without even the implied warranty of
35 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 # GNU General Public License for more details.
38 # You should have received a copy of the GNU General Public License
39 # along with this program; if not, write to the Free Software
40 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
46 # Conversion "Requests"
48 # Standard conversions
49 my $searchReplaceFalseTrueHRef =
51 "FALSE" => "ENC_BIG_ENDIAN",
52 "0" => "ENC_BIG_ENDIAN",
53 "TRUE" => "ENC_LITTLE_ENDIAN",
54 "1" => "ENC_LITTLE_ENDIAN"
57 my $searchReplaceEncNAHRef =
63 "ENC_LITTLE_ENDIAN" => "ENC_NA",
64 "ENC_BIG_ENDIAN" => "ENC_NA"
68 # ---------------------------------------------------------------------
69 # Conversion "request" structure
71 # [ <list of field types for which this conversion request applies> ],
72 # { <hash of desired encoding arg conversions> }
77 [ qw (FT_NONE FT_BYTES FT_ETHER FT_IPv6 FT_IPXNET FT_OID)],
78 $searchReplaceEncNAHRef
83 [ qw (FT_UINT8 FT_UINT16 FT_UINT24 FT_UINT32 FT_UINT64 FT_INT8
84 FT_INT16 FT_INT24 FT_INT32 FT_INT64 FT_FLOAT FT_DOUBLE)],
85 $searchReplaceFalseTrueHRef
90 [ qw (FT_BOOLEAN FT_IPv4 FT_GUID FT_EUI64)],
91 $searchReplaceFalseTrueHRef
96 [qw (FT_STRING FT_STRINGZ)],
98 "FALSE" => "ENC_ASCII|ENC_NA",
99 "0" => "ENC_ASCII|ENC_NA",
100 "TRUE" => "ENC_ASCII|ENC_NA",
101 "1" => "ENC_ASCII|ENC_NA",
102 "ENC_LITTLE_ENDIAN" => "ENC_ASCII|ENC_NA",
103 "ENC_BIG_ENDIAN" => "ENC_ASCII|ENC_NA",
104 "ENC_NA" => "ENC_ASCII|ENC_NA",
106 "ENC_ASCII" => "ENC_ASCII|ENC_NA",
107 "ENC_ASCII|ENC_LITTLE_ENDIAN" => "ENC_ASCII|ENC_NA",
108 "ENC_ASCII|ENC_BIG_ENDIAN" => "ENC_ASCII|ENC_NA",
110 "ENC_UTF_8" => "ENC_UTF_8|ENC_NA",
111 "ENC_UTF_8|ENC_LITTLE_ENDIAN" => "ENC_UTF_8|ENC_NA",
112 "ENC_UTF_8|ENC_BIG_ENDIAN" => "ENC_UTF_8|ENC_NA",
114 "ENC_EBCDIC" => "ENC_EBCDIC|ENC_NA",
115 "ENC_EBCDIC|ENC_LITTLE_ENDIAN" => "ENC_EBCDIC|ENC_NA",
116 "ENC_EBCDIC|ENC_BIG_ENDIAN" => "ENC_EBCDIC|ENC_NA",
120 my @types_UINT_STRING =
122 [qw (FT_UINT_STRING)],
124 "FALSE" => "ENC_ASCII|ENC_BIG_ENDIAN",
125 "0" => "ENC_ASCII|ENC_BIG_ENDIAN",
126 "TRUE" => "ENC_ASCII|ENC_LITTLE_ENDIAN",
127 "1" => "ENC_ASCII|ENC_LITTLE_ENDIAN",
128 "ENC_BIG_ENDIAN" => "ENC_ASCII|ENC_BIG_ENDIAN",
129 "ENC_LITTLE_ENDIAN" => "ENC_ASCII|ENC_LITTLE_ENDIAN",
133 my @types_REG_PROTO =
136 $searchReplaceEncNAHRef
139 # ---------------------------------------------------------------------
140 # For searching (and doing no substitutions) (obsolete ?)
143 [qw (FT_ABSOLUTE_TIME FT_RELATIVE_TIME)],
182 {# valid encoding args
184 "b"=>"ENC_LITTLE_ENDIAN",
185 "c"=>"ENC_BIG_ENDIAN",
187 "d"=>"ENC_ASCII|ENC_NA",
188 "e"=>"ENC_ASCII|ENC_LITTLE_ENDIAN",
189 "f"=>"ENC_ASCII|ENC_BIG_ENDIAN",
191 "g"=>"ENC_UTF_8|ENC_NA",
192 "h"=>"ENC_UTF_8|ENC_LITTLE_ENDIAN",
193 "i"=>"ENC_UTF_8|ENC_BIG_ENDIAN",
195 "j"=>"ENC_EBCDIC|ENC_NA",
196 "k"=>"ENC_EBCDIC|ENC_LITTLE_ENDIAN",
197 "l"=>"ENC_EBCDIC|ENC_BIG_ENDIAN",
201 # ---------------------------------------------------------------------
203 my @findAllFunctionList =
204 ## proto_tree_add_bitmask_text !! ToDo: encoding arg not last arg
205 ## ptvcursor_add_with_subtree !! ToDo: encoding Arg not last arg
208 proto_tree_add_bits_item
209 proto_tree_add_bits_ret_val
210 proto_tree_add_bitmask
217 ptvcursor_add_no_advance
220 # ---------------------------------------------------------------------
226 my $action = 'fix-all';
228 my $result = GetOptions(
229 'action=s' => \$action,
230 'write' => \$writeFlag,
231 'help|?' => \$helpFlag
234 if (!$result || $helpFlag || !$ARGV[0]) {
238 if (($action ne 'fix-all') && ($action ne 'find-all')) {
243 print "\nUsage: $0 [--action=fix-all|find-all] [--write] FILENAME [...]\n\n";
244 print " --action = fix-all (default)\n";
245 print " Fix <certain-fcn-names>() encoding arg when possible in FILENAME(s)\n";
246 print " Fixes (if any) are listed on stdout)\n\n";
247 print " --write create FILENAME.encoding-arg-fixes (original file with fixes)\n";
248 print " (effective only for fix-all)\n";
250 print " --action = find-all\n";
251 print " Find all occurrences of <certain-fcn-names>() statements)\n";
252 print " highlighting the 'encoding' arg\n";
256 # Read through the files; fix up encoding parameter of proto_tree_add_item() calls
259 # . Create a hash of the hf_index_names & associated field types from the entries in hf[]
260 # . For each requested "conversion request" {
261 # . . For each hf[] entry hf_index_name with a field type in a set of specified field types {
262 # . . . For each proto_tree_add_item() statement
263 # . . . . - replace encoding arg in proto_tree_add_item(..., hf_index_name, ..., 'encoding-arg')
264 # specific values ith new values
265 # . . . . - print the statement showing the change
269 # . If requested and if replacements done: write new file "orig-filename.encoding-arg-fixes"
272 # Note: The proto_tree_add_item() encoding arg will be converted only if
273 # the hf_index_name referenced is in one of the entries in hf[] in the same file
277 while (my $fileName = $ARGV[0]) {
279 my $fileContents = '';
281 die "No such file: \"$fileName\"\n" if (! -e $fileName);
283 # delete leading './'
284 $fileName =~ s{ ^ \. / } {}xo;
286 # Read in the file (ouch, but it's easier that way)
287 open(FCI, "<", $fileName) || die("Couldn't open $fileName");
293 # Create a hash of the hf[] entries (name_index_name=>field_type)
294 my $hfArrayEntryFieldTypeHRef = find_hf_array_entries(\$fileContents, $fileName);
296 if ($action eq "fix-all") {
298 # Find and replace: <fcn_name_pattern>() encoding arg in $fileContents for:
299 # - hf[] entries with specified field types;
300 # - 'proto' as returned from proto_register_protocol()
301 my $fcn_name = "(?:proto_tree_add_item|ptvcursor_add(?:_no_advance)?)";
303 $found += fix_encoding_args_by_hf_type(1, \@types_NA, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
304 $found += fix_encoding_args_by_hf_type(1, \@types_INT, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
305 $found += fix_encoding_args_by_hf_type(1, \@types_MISC, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
306 $found += fix_encoding_args_by_hf_type(1, \@types_STRING, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
307 $found += fix_encoding_args_by_hf_type(1, \@types_UINT_STRING, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
308 $found += fix_encoding_args_by_hf_type(1, \@types_REG_PROTO, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
310 # Find and replace: alters <fcn_name>() encoding arg in $fileContents
311 $found += fix_encoding_args(1, $searchReplaceFalseTrueHRef, "proto_tree_add_bits_(?:item|ret_val)", \$fileContents, $fileName);
312 $found += fix_encoding_args(1, $searchReplaceFalseTrueHRef, "proto_tree_add_bitmask", \$fileContents, $fileName);
313 $found += fix_encoding_args(1, $searchReplaceFalseTrueHRef, "tvb_get_bits(?:16|24|32|64)?", \$fileContents, $fileName);
314 $found += fix_encoding_args(1, $searchReplaceFalseTrueHRef, "tvb_get_(?:ephemeral_)?unicode_string[z]?", \$fileContents, $fileName);
316 # If desired and if any changes, write out the changed version to a file
317 if (($writeFlag) && ($found > 0)) {
318 open(FCO, ">", $fileName . ".encoding-arg-fixes");
319 # open(FCO, ">", $fileName );
320 print FCO "$fileContents";
323 $found_total += $found;
326 if ($action eq "find-all") {
327 # Find all proto_tree_add_item() statements
328 # and output same highlighting the encoding arg
329 $found_total += find_all(\@findAllFunctionList, \$fileContents, $fileName);
332 # Optional searches: (kind of obsolete ?)
333 # search for (and output) proto_tree_add_item() statements with invalid encoding arg for specified field types
334 # $fcn_name = "proto_tree_add_item";
335 # fix_encoding_args(2, \@types_NA, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
336 # fix_encoding_args(2, \@types_INT, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
337 # fix_encoding_args(2, \@types_MISC, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
338 # fix_encoding_args(2, \@types_STRING, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
339 # fix_encoding_args(2, \@types_UINT_STRING, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
340 # fix_encoding_args(2, \@types_ALL, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
341 # search for (and output) proto_tree_add_item()$fcn_name, statements with any encoding arg for specified field types
342 # fix_encoding_args(3, \@types_TIME, $fcn_name, \$fileContents, $hfArrayEntryFieldTypeHRef, $fileName);
349 # ---------------------------------------------------------------------
350 # Create a hash containing an entry (hf_index_name => field_type) for each hf[]entry.
351 # also: create an entry in the hash for the 'protocol name' variable (proto... => FT_PROTOCOL)
352 # returns: ref to the hash
354 sub find_hf_array_entries {
355 my ($fileContentsRef, $fileName) = @_;
357 # The below Regexp is based on one from:
358 # http://aspn.activestate.com/ASPN/Cookbook/Rx/Recipe/59811
359 # It is in the public domain.
360 # A complicated regex which matches C-style comments.
361 my $CCommentRegEx = qr{ / [*] [^*]* [*]+ (?: [^/*] [^*]* [*]+ )* / }xo;
363 # hf[] entry regex (to extract an hf_index_name and associated field type)
364 my $hfArrayFieldTypeRegEx = qr {
367 &\s*([A-Z0-9_\[\]-]+) # &hf
370 .+? # (a bit dangerous)
372 (FT_[A-Z0-9_]+) # field type
379 # create a copy of $fileContents with comments removed
380 my $fileContentsWithoutComments = $$fileContentsRef;
381 $fileContentsWithoutComments =~ s {$CCommentRegEx} []xg;
383 # find all the hf[] entries (searching $fileContentsWithoutComments).
384 # Create a hash keyed by the hf_index_name with the associated value being the field_type
385 my %hfArrayEntryFieldType;
386 while ($fileContentsWithoutComments =~ m{ $hfArrayFieldTypeRegEx }xgis) {
388 if (exists $hfArrayEntryFieldType{$1}) {
389 printf "%-35.35s: ? duplicate hf[] entry: no fixes done for: $1; manual action may be req'd\n", $fileName;
390 $hfArrayEntryFieldType{$1} = "???"; # prevent any substitutions for this hf_index_name
392 $hfArrayEntryFieldType{$1} = $2;
396 # RegEx to get "proto" variable name
397 my $protoRegEx = qr /
398 ^ \s* # note m modifier below
405 proto_register_protocol
410 # Find all registered protocols
411 while ($fileContentsWithoutComments =~ m { $protoRegEx }xgioms ) {
413 if (exists $hfArrayEntryFieldType{$1}) {
414 printf "%-35.35s: ? duplicate 'proto': no fixes done for: $1; manual action may be req'd\n", $fileName;
415 $hfArrayEntryFieldType{$1} = "???"; # prevent any substitutions for this protocol
417 $hfArrayEntryFieldType{$1} = "REG_PROTO";
421 return \%hfArrayEntryFieldType;
424 # ---------------------------------------------------------------------
426 # Substitute new values for the specified <fcn_name>() encoding arg values
427 # when the encoding arg is the *last* arg of the call to fcn_name
429 # substitute_flag: 1: replace specified encoding arg values by a new value (keys/values in search hash);
430 # ref to hash containing search (keys) and replacement (values) for encoding arg
432 # ref to string containing file contents
439 my $searchReplaceHRef;
442 sub fix_encoding_args {
443 (my $subFlag, $searchReplaceHRef, my $fcn_name, my $fileContentsRef, $fileName) = @_;
448 # just match for <fcn_name>() statements which have an encoding arg matching one of the
449 # keys in the searchReplace hash.
450 # Escape any "|" characters in the keys
451 # and then create "alternatives" string containing all the values (A|B|C\|D|...)
452 $encArgPat = join "|", map { s{ ( \| ) }{\\$1}gx; $_ } keys %$searchReplaceHRef;
453 } elsif ($subFlag == 3) {
454 # match for <fcn_name>() statements for any value of the encoding parameter
455 # IOW: find all the <fcn_name> statements
456 $encArgPat = qr / [^,)]+? /x;
459 # build the complete pattern
462 (?:^|=) # don't try to handle fcn_name call when arg of another fcn call
465 [^;]+? # a bit dangerous
475 /xms; # m for ^ above
477 ##print "$patRegEx\n";
479 ## Match and substitute as specified
482 $$fileContentsRef =~ s/ $patRegEx /patsubx($1,$2,$3)/xges;
487 # Called from fix_encoding_args to determine replacement string when a regex match is encountered
489 # $_[1]: part 2: encoding arg
491 # lookup the desired replacement value for the encoding arg
492 # print match string showing and highlighting the encoding arg replacement
493 # return "replacement" string
496 my $substr = exists $$searchReplaceHRef{$_[1]} ? $$searchReplaceHRef{$_[1]} : "???";
497 my $str = sprintf("%s[[%s]-->[%s]]%s", $_[0], $_[1], $substr, $_[2]);
498 $str =~ tr/\t\n\r/ /d;
499 printf "%s: $str\n", $fileName;
500 return $_[0] . $substr . $_[2];
504 # ---------------------------------------------------------------------
505 # fix_encoding_args_by_hf_type
507 # Substitute new values for certain proto_tree_add_item() encoding arg
508 # values (for specified hf field types)
509 # Variants: search for and display for "exceptions" to allowed encoding arg values;
510 # search for and display all encoding arg values
512 # substitute_flag: 1: replace specified encoding arg values by a new value (keys/values in search hash);
513 # 2: search for "exceptions" to allowed encoding arg values (values in search hash);
514 # 3: search for all encoding arg values
515 # ref to array containing two elements:
516 # - ref to array containing hf[] types to be processed (FT_STRING, etc)
517 # - ref to hash containing search (keys) and replacement (values) for encoding arg
519 # ref to hfArrayEntries hash (key: hf name; value: field type)
520 # ref to string containing file contents
527 my $searchReplaceHRef;
531 sub fix_encoding_args_by_hf_type {
533 (my $subFlag, my $mapArg, my $fcn_name, my $fileContentsRef, my $hfArrayEntryFieldTypeHRef, $fileName) = @_;
539 $hfTypesARef = $$mapArg[0];
540 $searchReplaceHRef = $$mapArg[1];
543 @hfTypes{@$hfTypesARef}=();
545 # set up the encoding arg match pattern
547 # just match for <fcn_name>() statements which have an encoding arg matching one of the
548 # keys in the searchReplace hash.
549 # Escape any "|" characters in the keys
550 # and then create "alternatives" string containing all the values (A|B|C\|D|...)
551 $encArgPat = join "|", map { s{ ( \| ) }{\\$1}gx; $_ } keys %$searchReplaceHRef;
552 } elsif ($subFlag == 2) {
553 # Find all the <fcn_name>() statements wherein the encoding arg is a value other than
554 # one of the "replace" values.
555 # Uses zero-length negative-lookahead to find <fcn_name>() statements for which the encoding
556 # arg is something other than one of the the provided replace values.
557 # Escape any "|" characters in the values to be matched
558 # and then create "alternatives" string containing all the values (A|B|C\|D|...)
559 my $match_str = join "|", map { s{ ( \| ) }{\\$1}gx; $_ } values %$searchReplaceHRef;
561 (?! # negative zero-length look-ahead
563 (?: $match_str ) # alternatives we don't want to match
566 [^,)]+? # OK: enoding arg is other than one of the alternatives:
567 # match to end of the arg
569 } elsif ($subFlag == 3) {
570 # match for <fcn_name>() statements for any value of the encoding parameter
571 # IOW: find all the proto_tree_add_item statements with an hf entry of the desired types
572 $encArgPat = qr / [^,)]+? /x;
575 # For each hf[] entry which matches a type in %hfTypes do replacements
577 foreach my $key (keys %$hfArrayEntryFieldTypeHRef) {
578 $hf_index_name = $key;
579 $hf_index_name =~ s{ ( \[ | \] ) }{\\$1}xg; # escape any "[" or "]" characters
580 $hf_field_type = $$hfArrayEntryFieldTypeHRef{$key};
581 ##printf "--> %-35.35s: %s\n", $hf_index_name, $hf_field_type;
583 next unless exists $hfTypes{$hf_field_type}; # Do we want to process for this hf[] entry type ?
585 # build the complete pattern
605 ##print "\n$hf_index_name $hf_field_type\n";
607 ## Match and substitute as specified
608 $$fileContentsRef =~ s/ $patRegEx /patsub($1,$2,$3)/xges;
615 # Called from fix_encoding_args to determine replacement string when a regex match is encountered
617 # $_[1]: part 2: encoding arg
619 # lookup the desired replacement value for the encoding arg
620 # print match string showing and highlighting the encoding arg replacement
621 # return "replacement" string
624 my $substr = exists $$searchReplaceHRef{$_[1]} ? $$searchReplaceHRef{$_[1]} : "???";
625 my $str = sprintf("%s[[%s]-->[%s]]%s", $_[0], $_[1], $substr, $_[2]);
626 $str =~ tr/\t\n\r/ /d;
627 printf "%s: %-17.17s $str\n", $fileName, $hf_field_type . ":";
628 return $_[0] . $substr . $_[2];
632 # ---------------------------------------------------------------------
633 # Find all <fcnList> statements
634 # and output same highlighting the encoding arg
635 # Currently: encoding arg is matched as the *last* arg of the function call
638 my( $fcnListARef, $fileContentsRef, $fileName) = @_;
641 my $fcnListPat = join "|", @$fcnListARef;
644 (?:$fcnListPat) \s* \(
657 while ($$fileContentsRef =~ / $pat /xgso) {
658 my $str = "${1}[[${2}]]${3}\n";
659 $str =~ tr/\t\n\r/ /d;
660 $str =~ s/ \s+ / /xg;
661 print "$fileName: $str\n";