| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # binjitsu::pattern Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Binjitsu::Pattern; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'ZadYree <zadyree[at]gmail.com>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | datadir => [ qw(datadir) ], | ||
| 20 | output => [ qw(file) ], | ||
| 21 | max_size => [ qw(size) ], | ||
| 22 | }, | ||
| 23 | attributes_default => { | ||
| 24 | max_size => 65535, | ||
| 25 | }, | ||
| 26 | commands => { | ||
| 27 | create => [ qw(size file|OPTIONAL) ], | ||
| 28 | offset => [ qw(point) ], | ||
| 29 | }, | ||
| 30 | require_modules => { | ||
| 31 | 'Metabrik::String::Hexa' => [ ], | ||
| 32 | 'Path::Tiny' => [ ], | ||
| 33 | }, | ||
| 34 | }; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub create { | ||
| 38 | my $self = shift; | ||
| 39 | my $count = int(shift) || 16; | ||
| 40 | |||
| 41 | my ($flw); | ||
| 42 | if ( $self->output ) { | ||
| 43 | $flw = Path::Tiny->new( $self->datadir )->child( $self->output ); | ||
| 44 | } | ||
| 45 | |||
| 46 | $self->log->error("create: Too large pattern :("), die | ||
| 47 | unless $count < $self->max_size; | ||
| 48 | my $set = { | ||
| 49 | ALPHA_LOWER => [ "a" .. "z" ], | ||
| 50 | ALPHA_UPPER => [ "A" .. "Z" ], | ||
| 51 | NUMS => [ 0 .. 9 ], | ||
| 52 | }; | ||
| 53 | |||
| 54 | my $patt = ""; | ||
| 55 | for my $chr0 ( @{ $set->{ALPHA_UPPER} } ) { | ||
| 56 | last unless ( length($patt) < $count ); | ||
| 57 | for my $chr1 ( @{ $set->{ALPHA_UPPER} } ) { | ||
| 58 | for my $chr2 ( @{ $set->{NUMS} } ) { | ||
| 59 | for my $chr3 ( @{ $set->{ALPHA_LOWER} } ) { | ||
| 60 | $patt .= $chr0 . $chr1 . $chr2 . $chr3; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | $patt = substr( $patt, 0, $count ); | ||
| 67 | if ($flw) { | ||
| 68 | $self->log->info( "create: Writing pattern to " . $flw->stringify ); | ||
| 69 | $flw->spew($patt); | ||
| 70 | return 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | return \$patt; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub offset { | ||
| 77 | my $self = shift; | ||
| 78 | my $point = shift; | ||
| 79 | |||
| 80 | $self->brik_help_run_undef_arg( 'offset', $point ) or return; | ||
| 81 | |||
| 82 | $self->log->verbose( | ||
| 83 | "offset: Finding offset for $point. /!\\ This is experimental (really!) /!\\"); | ||
| 84 | |||
| 85 | $point =~ s/0x//; | ||
| 86 | |||
| 87 | my ($idx); | ||
| 88 | my ($chrs); | ||
| 89 | if ( length($point) == 8 ) { | ||
| 90 | $chrs = [ split( /(..)/, substr( $point, 0, 8 ) ) ]; | ||
| 91 | $chrs = pack( 'V', hex( join( '', @$chrs ) ) ); | ||
| 92 | |||
| 93 | $idx = index( ${ $self->create( $self->max_size - 1 ) }, $chrs ); | ||
| 94 | } | ||
| 95 | elsif ( length($point) == 16 ) { | ||
| 96 | $chrs = [ split( /(..)/, substr( $point, 0, 16 ) ) ]; | ||
| 97 | $chrs = pack( 'L!', hex( join( '', @$chrs ) ) ); | ||
| 98 | |||
| 99 | $idx = index( ${ $self->create( $self->max_size - 1 ) }, $chrs ); | ||
| 100 | } | ||
| 101 | else { $self->log->error("offset: Bad address size."); return; } | ||
| 102 | |||
| 103 | if ( $idx != -1 ) { | ||
| 104 | return { | ||
| 105 | pattern => $chrs, | ||
| 106 | position => $idx, | ||
| 107 | }; | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | $self->log->error("offset: Couldn't find typed offset pattern [ $point ]"); | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | __END__ | ||