| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use File::Basename; | ||
| 6 | use Getopt::Long; | ||
| 7 | use Parse::Win32Registry 0.51 qw( make_multiple_subtree_iterator | ||
| 8 | compare_multiple_keys | ||
| 9 | compare_multiple_values | ||
| 10 | hexdump ); | ||
| 11 | |||
| 12 | binmode(STDOUT, ':utf8'); | ||
| 13 | |||
| 14 | Getopt::Long::Configure('bundling'); | ||
| 15 | |||
| 16 | GetOptions('values|v' => \my $show_values, | ||
| 17 | 'hexdump|x' => \my $show_hexdump, | ||
| 18 | 'long|l' => \my $show_long, | ||
| 19 | 'all|a' => \my $show_all); | ||
| 20 | |||
| 21 | my $show_keys = 1; | ||
| 22 | |||
| 23 | my @filenames = (); | ||
| 24 | my @root_keys = (); | ||
| 25 | my @start_keys = (); | ||
| 26 | my $initial_key_path; | ||
| 27 | |||
| 28 | if (@ARGV) { | ||
| 29 | while (my $filename = shift) { | ||
| 30 | if (-r $filename) { | ||
| 31 | my $registry = Parse::Win32Registry->new($filename); | ||
| 32 | if (defined $registry) { | ||
| 33 | my $root_key = $registry->get_root_key; | ||
| 34 | if (defined $root_key) { | ||
| 35 | push @root_keys, $root_key; | ||
| 36 | push @filenames, $filename; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | # If $filename is not a readable file, assume it is a key path: | ||
| 42 | $initial_key_path = $filename; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | die usage(); | ||
| 48 | } | ||
| 49 | |||
| 50 | if (@root_keys < 2) { | ||
| 51 | die "Specify two or more filenames\n"; | ||
| 52 | } | ||
| 53 | |||
| 54 | @start_keys = @root_keys; | ||
| 55 | if ($initial_key_path) { | ||
| 56 | @start_keys = map { $_->get_subkey($initial_key_path) || undef } @root_keys; | ||
| 57 | } | ||
| 58 | |||
| 59 | my $num_start_keys = grep { defined } @start_keys; | ||
| 60 | if ($num_start_keys < 1) { | ||
| 61 | die "Could not locate the key '$initial_key_path' in any file\n"; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $subtree_iter = make_multiple_subtree_iterator(@start_keys); | ||
| 65 | my $batch_size = @start_keys; | ||
| 66 | |||
| 67 | if ($show_long) { | ||
| 68 | for (my $num = 0; $num < $batch_size; $num++) { | ||
| 69 | print "[$num]:\tFILE\t'$filenames[$num]'\n"; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | my $last_key_shown; | ||
| 74 | |||
| 75 | while (my ($keys_ref, $values_ref) = $subtree_iter->get_next) { | ||
| 76 | my @keys = @$keys_ref; | ||
| 77 | my $any_key = (grep { defined } @keys)[0]; | ||
| 78 | die "Unexpected error: no keys!" if !defined $any_key; | ||
| 79 | |||
| 80 | if (defined $values_ref) { | ||
| 81 | my @values = @$values_ref; | ||
| 82 | my $any_value = (grep { defined } @values)[0]; | ||
| 83 | die "Unexpected error: no values!" if !defined $any_value; | ||
| 84 | |||
| 85 | my @changes = compare_multiple_values(@values); | ||
| 86 | my $num_changes = grep { $_ } @changes; | ||
| 87 | if ($num_changes > 0 && $show_values) { | ||
| 88 | if (!defined $last_key_shown | ||
| 89 | || $last_key_shown ne $any_key->get_path) | ||
| 90 | { | ||
| 91 | print "-" x $batch_size, "\t", $any_key->get_path, "\n"; | ||
| 92 | $last_key_shown = $any_key->get_path; | ||
| 93 | } | ||
| 94 | if (!$show_long) { | ||
| 95 | for (my $num = 0; $num < $batch_size; $num++) { | ||
| 96 | my $diff = substr($changes[$num], 0, 1) || | ||
| 97 | (defined $values[$num] ? '.' : ' '); | ||
| 98 | print $diff; | ||
| 99 | } | ||
| 100 | print "\t", $any_value->get_name, "\n"; | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | for (my $num = 0; $num < $batch_size; $num++) { | ||
| 104 | my $next_change = $changes[$num + 1]; | ||
| 105 | if ($changes[$num] || $show_all | ||
| 106 | || defined $next_change | ||
| 107 | && $next_change eq 'DELETED') | ||
| 108 | { | ||
| 109 | print "[$num]:\t$changes[$num]\t"; | ||
| 110 | if (defined $values[$num]) { | ||
| 111 | if (!$show_hexdump) { | ||
| 112 | print $values[$num]->as_string, "\n"; | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | my $value_name = $values[$num]->get_name; | ||
| 116 | $value_name = "(Default)" if $value_name eq ""; | ||
| 117 | my $value_type | ||
| 118 | = $values[$num]->get_type_as_string; | ||
| 119 | print "$value_name ($value_type):\n"; | ||
| 120 | print hexdump($values[$num]->get_raw_data); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | print "\n"; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | my @changes = compare_multiple_keys(@keys); | ||
| 133 | my $num_changes = grep { $_ } @changes; | ||
| 134 | if ($num_changes > 0 && $show_keys) { | ||
| 135 | if (!$show_long) { | ||
| 136 | for (my $num = 0; $num < $batch_size; $num++) { | ||
| 137 | my $diff = substr($changes[$num], 0, 1) || | ||
| 138 | (defined $keys[$num] ? '.' : ' '); | ||
| 139 | print $diff; | ||
| 140 | } | ||
| 141 | print "\t", $any_key->get_path, "\n"; | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | for (my $num = 0; $num < $batch_size; $num++) { | ||
| 145 | my $next_change = $changes[$num+1]; | ||
| 146 | if ($changes[$num] || $show_all | ||
| 147 | || defined $next_change | ||
| 148 | && $next_change eq 'DELETED') | ||
| 149 | { | ||
| 150 | print "[$num]:\t$changes[$num]\t"; | ||
| 151 | if (defined $keys[$num]) { | ||
| 152 | print $keys[$num]->as_string; | ||
| 153 | $last_key_shown = $keys[$num]->get_path; | ||
| 154 | } | ||
| 155 | elsif ($changes[$num] eq 'DELETED') { | ||
| 156 | print $keys[$num-1]->as_string; | ||
| 157 | $last_key_shown = $keys[$num-1]->get_path; | ||
| 158 | } | ||
| 159 | print "\n"; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | $last_key_shown = $any_key->get_path; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | sub usage { | ||
| 169 | my $script_name = basename $0; | ||
| 170 | return <<USAGE; | ||
| 171 | $script_name for Parse::Win32Registry $Parse::Win32Registry::VERSION | ||
| 172 | |||
| 173 | Compares two or more registry files. | ||
| 174 | Defaults to displaying a summary of the changes where each letter | ||
| 175 | represents a change (N=NEWER, O=OLDER, A=ADDED, D=DELETED, C=CHANGED). | ||
| 176 | The long output will display full details of each change. | ||
| 177 | |||
| 178 | $script_name <file1> <file2> <file3> ... [<subkey>] [-v] [-x] [-l] [-a] | ||
| 179 | -v or --values display values | ||
| 180 | -x or --hexdump display value data as a hex dump | ||
| 181 | -l or --long show each changed key or value instead of a summary | ||
| 182 | -a or --all show all keys and values before and after a change | ||
| 183 | USAGE | ||
| 184 | } |