| 1 | #!/usr/bin/env perl | ||
| 2 | #=============================================================================== | ||
| 3 | # | ||
| 4 | # FILE: pronom2wxhexeditor.pl | ||
| 5 | # | ||
| 6 | # USAGE: ./pronom2wxhexeditor.pl | ||
| 7 | # | ||
| 8 | # DESCRIPTION: perl ./pronom2wxhexeditor.pl <DROIDSIGNATURE-FILE> <BINARYFILE> | ||
| 9 | # | ||
| 10 | # OPTIONS: --- | ||
| 11 | # REQUIREMENTS: --- | ||
| 12 | # BUGS: --- | ||
| 13 | # NOTES: --- | ||
| 14 | # AUTHOR: Andreas Romeyke, | ||
| 15 | # CREATED: 28.08.2018 14:26:43 | ||
| 16 | # REVISION: --- | ||
| 17 | #=============================================================================== | ||
| 18 | # PODNAME: pronom2wxhexeditor.pl | ||
| 19 | use strict; | ||
| 20 | use warnings 'FATAL'; | ||
| 21 | use utf8; | ||
| 22 | use feature qw(say); | ||
| 23 | use Fcntl qw(:seek); | ||
| 24 | use Digest::CRC qw( crc8 ); | ||
| 25 | use Scalar::Util; | ||
| 26 | use File::Map qw(:map :extra); | ||
| 27 | use File::FormatIdentification::Pronom; | ||
| 28 | use Getopt::Long; | ||
| 29 | use Carp; | ||
| 30 | |||
| 31 | # calc a random color | ||
| 32 | sub rndcolor { | ||
| 33 | my $rgb = int( rand( 256 * 256 * 256 ) ); | ||
| 34 | return sprintf( "#%06x", $rgb ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub puidcolor { | ||
| 38 | my $puid = shift; | ||
| 39 | my $crc = crc8($puid); | ||
| 40 | return sprintf( "#%02x%02x%02x", $crc, $crc, $crc ); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub dircolor { | ||
| 44 | my $direction = shift; | ||
| 45 | my $pos = shift; | ||
| 46 | if ( $direction > 0 ) { | ||
| 47 | return sprintf( "#ff%04x", $pos * $pos ); | ||
| 48 | } | ||
| 49 | elsif ( $direction < 0 ) { | ||
| 50 | return sprintf( "#%04xff", $pos * $pos ); | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | return sprintf( "#%02xff%02x", $pos, $pos ); | ||
| 54 | } | ||
| 55 | } ## end sub dircolor | ||
| 56 | |||
| 57 | # helper function to collect all things needed for output and adds to a given buffer | ||
| 58 | sub push_output ($$$$$$$$$$) { | ||
| 59 | my %tmp; | ||
| 60 | $tmp{puid} = shift; | ||
| 61 | $tmp{name} = shift; | ||
| 62 | $tmp{begin} = shift; | ||
| 63 | $tmp{end} = shift; | ||
| 64 | $tmp{regex} = shift; | ||
| 65 | |||
| 66 | #$tmp{hexdump} = shift; | ||
| 67 | $tmp{position} = shift; | ||
| 68 | $tmp{signature} = shift; | ||
| 69 | $tmp{internal_signature} = shift; | ||
| 70 | $tmp{bytesequence} = shift; | ||
| 71 | my $ref_buffer = shift; | ||
| 72 | push @{$ref_buffer}, \%tmp; | ||
| 73 | return; | ||
| 74 | } ## end sub push_output | ||
| 75 | |||
| 76 | #render HTML output | ||
| 77 | sub render_for_html { | ||
| 78 | my $ref_buffer = shift; | ||
| 79 | my $fh = shift; | ||
| 80 | my $binaryfile = shift; | ||
| 81 | my @tmp = sort { $a->{begin} <=> $b->{begin} } ( @{$ref_buffer} ); | ||
| 82 | say $fh <<HEAD; | ||
| 83 | <html><head /> | ||
| 84 | <body> | ||
| 85 | <h1> Result for "$binaryfile"</h1> | ||
| 86 | HEAD | ||
| 87 | foreach my $tagid ( 0 .. $#tmp ) { | ||
| 88 | my $pos = $tmp[$tagid]->{position}; | ||
| 89 | my $begin = $tmp[$tagid]->{begin}; | ||
| 90 | my $end = $tmp[$tagid]->{end}; | ||
| 91 | my $puid = $tmp[$tagid]->{puid}; | ||
| 92 | my $name = $tmp[$tagid]->{name}; | ||
| 93 | my $regex = $tmp[$tagid]->{regex}; | ||
| 94 | my $internal = $tmp[$tagid]->{internal_signature}; | ||
| 95 | my $bytesequence = $tmp[$tagid]->{bytesequence}; | ||
| 96 | my $partial = get_partial_regex( $pos, $regex ); | ||
| 97 | |||
| 98 | #my $hexdump = $tmp[$tagid]->{hexdump}; | ||
| 99 | #if ( length($hexdump) > 10 ) { | ||
| 100 | # $hexdump = substr( $hexdump, 0, 10 ) . "..."; | ||
| 101 | #} | ||
| 102 | my $fgcolor = puidcolor($puid); | ||
| 103 | |||
| 104 | #my $bgcolor = dircolor( $begin <=> $end, $pos ); | ||
| 105 | my $bgcolor = rndcolor(); | ||
| 106 | say $fh " | ||
| 107 | <h2>$puid</h2> | ||
| 108 | <p>Internal Signature: $internal</p> | ||
| 109 | <p>Byte Sequence: $bytesequence</p> | ||
| 110 | <p>Bytes $begin - $end</p> | ||
| 111 | <p>$name</p> | ||
| 112 | <p>regex=$regex</p> | ||
| 113 | <p>matching $pos-th partial regex: $partial</p> | ||
| 114 | " | ||
| 115 | |||
| 116 | #<p>pos=$pos</p> | ||
| 117 | #<p>hexdump:<br />$hexdump</p>" | ||
| 118 | } ## end foreach my $tagid ( 0 .. $#tmp) | ||
| 119 | |||
| 120 | say $fh <<FOOT; | ||
| 121 | </body> | ||
| 122 | </html> | ||
| 123 | FOOT | ||
| 124 | return; | ||
| 125 | } ## end sub render_for_html | ||
| 126 | |||
| 127 | # render output for wxhexeditor | ||
| 128 | sub render_for_wxhexeditor { | ||
| 129 | my $ref_buffer = shift; | ||
| 130 | my $fh = shift; | ||
| 131 | my $binaryfile = shift; | ||
| 132 | my @tmp = sort { | ||
| 133 | if ( $a->{begin} == $b->{begin} ) { | ||
| 134 | return ( $a->{end} <=> $b->{end} ); | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | return ( $a->{begin} <=> $b->{begin} ); | ||
| 138 | } | ||
| 139 | } ( @{$ref_buffer} ); | ||
| 140 | say $fh <<HEAD; | ||
| 141 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 142 | <wxHexEditor_XML_TAG> | ||
| 143 | <filename path="$binaryfile"> | ||
| 144 | HEAD | ||
| 145 | |||
| 146 | foreach my $tagid ( 0 .. $#tmp ) { | ||
| 147 | my $pos = $tmp[$tagid]->{position}; | ||
| 148 | my $begin = $tmp[$tagid]->{begin}; | ||
| 149 | my $end = $tmp[$tagid]->{end}; | ||
| 150 | my $puid = $tmp[$tagid]->{puid}; | ||
| 151 | my $name = $tmp[$tagid]->{name}; | ||
| 152 | my $regex = $tmp[$tagid]->{regex}; | ||
| 153 | my $internal = $tmp[$tagid]->{internal_signature}; | ||
| 154 | my $bytesequence = $tmp[$tagid]->{bytesequence}; | ||
| 155 | |||
| 156 | #my $hexdump = $tmp[$tagid]->{hexdump}; | ||
| 157 | #if ( length($hexdump) > 10 ) { | ||
| 158 | # $hexdump = substr( $hexdump, 0, 10 ) . "..."; | ||
| 159 | #} | ||
| 160 | my $fgcolor = puidcolor($puid); | ||
| 161 | |||
| 162 | #my $bgcolor = dircolor( $begin <=> $end, $pos ); | ||
| 163 | my $bgcolor = rndcolor(); | ||
| 164 | my $partial = get_partial_regex( $pos, $regex ); | ||
| 165 | say $fh " | ||
| 166 | <TAG id='$tagid'> | ||
| 167 | <start_offset>$begin</start_offset> | ||
| 168 | <end_offset>$end</end_offset> | ||
| 169 | <tag_text>$puid | ||
| 170 | $name | ||
| 171 | at Bytes($begin, $end) | ||
| 172 | $regex | ||
| 173 | matching $pos-th partial regex: $partial | ||
| 174 | Internal Signature: $internal | ||
| 175 | Byte Sequence: $bytesequence | ||
| 176 | |||
| 177 | </tag_text> | ||
| 178 | <font_colour>$fgcolor</font_colour> | ||
| 179 | <note_colour>$bgcolor</note_colour> | ||
| 180 | </TAG>"; | ||
| 181 | } ## end foreach my $tagid ( 0 .. $#tmp) | ||
| 182 | |||
| 183 | say $fh <<FOOT; | ||
| 184 | </filename> | ||
| 185 | </wxHexEditor_XML_TAG> | ||
| 186 | FOOT | ||
| 187 | return; | ||
| 188 | } ## end sub render_for_wxhexeditor | ||
| 189 | |||
| 190 | sub get_partial_regex($$) { | ||
| 191 | my $position = shift; | ||
| 192 | my $regex = shift; | ||
| 193 | if ( $regex =~ m/\({$position}(.{20})/ ) { return "'$1'..."; } | ||
| 194 | return ""; | ||
| 195 | } | ||
| 196 | |||
| 197 | ################################################################################ | ||
| 198 | # main | ||
| 199 | ################################################################################ | ||
| 200 | my $pronomfile; | ||
| 201 | my $binaryfile; | ||
| 202 | |||
| 203 | GetOptions ( | ||
| 204 | "signature=s" => \$pronomfile, | ||
| 205 | "binary=s" => \$binaryfile, | ||
| 206 | "help" => sub { | ||
| 207 | say "$0 --signature=droid_signature_filename --binary=binary_filename"; | ||
| 208 | say "$0 --help "; | ||
| 209 | say ""; | ||
| 210 | exit 1; | ||
| 211 | } | ||
| 212 | ) or croak "wrong option, try '$0 --help'"; | ||
| 213 | |||
| 214 | if ( !defined $pronomfile ) { | ||
| 215 | say "you need at least a pronom signature file"; | ||
| 216 | exit; | ||
| 217 | } | ||
| 218 | if ( !defined $binaryfile ) { | ||
| 219 | say "you need an binaryfile"; | ||
| 220 | exit; | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | # write basic main.osd | ||
| 225 | |||
| 226 | open( my $filehandle, "<", "$binaryfile" ); | ||
| 227 | binmode($filehandle); | ||
| 228 | seek( $filehandle, 0, SEEK_END ); | ||
| 229 | my $eof = tell($filehandle); | ||
| 230 | close $filehandle; | ||
| 231 | |||
| 232 | my $pronom = File::FormatIdentification::Pronom->new( | ||
| 233 | "droid_signature_filename" => $pronomfile | ||
| 234 | ); | ||
| 235 | |||
| 236 | my @output_buffer; | ||
| 237 | |||
| 238 | #my $pathobj = path($binaryfile); | ||
| 239 | #my $filestream = $pathobj->slurp_raw; | ||
| 240 | map_file my $filestream, $binaryfile, "<"; | ||
| 241 | advise( $filestream, 'random' ); | ||
| 242 | foreach my $internalid ( $pronom->get_all_internal_ids() ) { | ||
| 243 | my $sig = $pronom->get_signature_id_by_internal_id($internalid); | ||
| 244 | if ( !defined $sig ) { next; } | ||
| 245 | my $puid = $pronom->get_puid_by_signature_id($sig); | ||
| 246 | my $name = $pronom->get_name_by_signature_id($sig); | ||
| 247 | |||
| 248 | my @regexes = $pronom->get_regular_expressions_by_internal_id($internalid); | ||
| 249 | my @res; | ||
| 250 | my $timer = time; | ||
| 251 | foreach my $regex (@regexes) { | ||
| 252 | |||
| 253 | # MATCHed? | ||
| 254 | #warn "$internalid, regex='$regex'\n"; | ||
| 255 | if ( !defined $regex ) { | ||
| 256 | warn "No regex found for internalid $internalid\n"; | ||
| 257 | } | ||
| 258 | |||
| 259 | #say "REGEX='$regex'"; | ||
| 260 | if ( $filestream =~ m/$regex/saa ) { | ||
| 261 | my $tmp; | ||
| 262 | $tmp->{matched} = 1; | ||
| 263 | $tmp->{regex} = $regex; | ||
| 264 | |||
| 265 | #$tmp->{groups}; | ||
| 266 | |||
| 267 | #use Data::Printer; | ||
| 268 | #p( @+ ); | ||
| 269 | #p( @- ); | ||
| 270 | my %groups; | ||
| 271 | for ( my $match = 0 ; $match <= $#- ; $match++ ) { | ||
| 272 | if ( defined $-[$match] && defined $+[$match] ) { | ||
| 273 | my $matches; | ||
| 274 | my $begin = $-[$match]; | ||
| 275 | my $end = $+[$match]; | ||
| 276 | $matches->{begin} = $begin; | ||
| 277 | $matches->{end} = $end; | ||
| 278 | $matches->{pos} = $match; | ||
| 279 | $groups{ ( $begin, $end ) } = $matches; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | my @uniqgroups = values %groups; | ||
| 283 | |||
| 284 | #use Data::Printer; | ||
| 285 | #p( @uniqgroups ); | ||
| 286 | $tmp->{groups} = \@uniqgroups; | ||
| 287 | |||
| 288 | #p( $tmp->{groups} ); | ||
| 289 | #die "matched '$_'"; | ||
| 290 | push @res, $tmp; | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | last; # break for loop | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | if ( ( scalar @res ) == ( scalar @regexes ) ) { # all matches successfull | ||
| 298 | # my %tmp; | ||
| 299 | # $tmp{puid} = shift; | ||
| 300 | # $tmp{name} = shift; | ||
| 301 | # $tmp{begin} = shift; | ||
| 302 | # $tmp{end} = shift; | ||
| 303 | # $tmp{regex} = shift; | ||
| 304 | # #$tmp{hexdump} = shift; | ||
| 305 | # $tmp{position} = shift; | ||
| 306 | # $tmp{signature} = shift; | ||
| 307 | # my $ref_buffer = shift; | ||
| 308 | |||
| 309 | for ( my $receiptidx = 0 ; $receiptidx <= $#res ; $receiptidx++ ) { | ||
| 310 | my $receipt = $res[$receiptidx]; | ||
| 311 | foreach my $group ( @{ $receipt->{groups} } ) { | ||
| 312 | push_output( | ||
| 313 | $puid, | ||
| 314 | $name, | ||
| 315 | $group->{begin}, | ||
| 316 | $group->{end}, | ||
| 317 | $receipt->{regex}, | ||
| 318 | $group->{pos}, | ||
| 319 | $sig, | ||
| 320 | $internalid, | ||
| 321 | $receiptidx, | ||
| 322 | |||
| 323 | \@output_buffer | ||
| 324 | ); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | #say " ... time=", (time - $timer), "s"; | ||
| 330 | } ## end foreach my $internal ( keys...) | ||
| 331 | |||
| 332 | open( my $OUT, ">", "$binaryfile.tags" ); | ||
| 333 | open( my $HTML, ">", "$binaryfile.html" ); | ||
| 334 | render_for_wxhexeditor( | ||
| 335 | #filter_matches_by_signature_priority( $signatures, \@output_buffer ), | ||
| 336 | \@output_buffer, | ||
| 337 | $OUT, $binaryfile | ||
| 338 | ); | ||
| 339 | render_for_html( | ||
| 340 | #filter_matches_by_signature_priority( $signatures, \@output_buffer ), | ||
| 341 | \@output_buffer, | ||
| 342 | $HTML, $binaryfile | ||
| 343 | ); | ||
| 344 | close $HTML; | ||
| 345 | close $OUT; | ||
| 346 | |||
| 347 | 1; | ||
| 348 | |||
| 349 | __END__ | ||
| 350 | |||
| 351 | =pod | ||
| 352 | |||
| 353 | =encoding UTF-8 | ||
| 354 | |||
| 355 | =head1 NAME | ||
| 356 | |||
| 357 | pronom2wxhexeditor.pl | ||
| 358 | |||
| 359 | =head1 VERSION | ||
| 360 | |||
| 361 | version 0.06 | ||
| 362 | |||
| 363 | =head1 AUTHOR | ||
| 364 | |||
| 365 | Andreas Romeyke <pause@andreas-romeyke.de> | ||
| 366 | |||
| 367 | =head1 COPYRIGHT AND LICENSE | ||
| 368 | |||
| 369 | This software is copyright (c) 2018 by Andreas Romeyke. | ||
| 370 | |||
| 371 | This is free software; you can redistribute it and/or modify it under | ||
| 372 | the same terms as the Perl 5 programming language system itself. | ||
| 373 | |||
| 374 | =cut |