The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/local/bin/perl
use strict;
use warnings;
use Getopt::LL  qw( getoptions );
use File::BSED  qw(
    binary_search_replace
    binary_file_matches
    string_to_hexstring
);
use English     qw(-no_match_vars);
use File::Basename;

my $LICENSE_INFO = q{
Copyright (C) 2007 Ask Solem <ask@0x61736b.net>
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and distribute it.
There is NO WARRANTY, to the extent permitted by law.
};

plbsed(@ARGV);

sub plbsed {

    my $program_name = basename($PROGRAM_NAME);

    my $search;
    my $replace;
    my $do_replace = 0;
    my $matches;
    my $minmatch;
    my $maxmatch;
    my $opt_silent;
    my $opt_vsilent;
    my $getopt_opts = {
        style                   => 'GNU',
        die_on_type_mismatch    => 1,
    };
    my $getopt_rules = {
        '-s|--search'       => sub {
            my ($self, $node) = @_;
            $search = $self->get_next_arg($node);
        },
        '-S|--searchstr'    => sub {
            my ($self, $node) = @_;
            $search = $self->get_next_arg($node);
            $search = string_to_hexstring($search);
        },
        '-r|--replace'      => sub {
            my ($self, $node) = @_;
            $replace = $self->get_next_arg($node);
        },
        '-R|--replacestr'   => sub {
            my ($self, $node) = @_;
            $replace = $self->get_next_arg($node);
            $replace = string_to_hexstring($replace);
        },
        '-D|--dump'         => sub {
            my ($self, $node) = @_;
            my $string = $self->get_next_arg($node);
            print string_to_hexstring($string), "\n";
            exit(0);
        },
        '-m|--min'          => sub {
            my ($self, $node) = @_;
            $minmatch = $self->get_next_arg($node);
        },
        '-M|--max'          => sub {
            my ($self, $node) = @_;
            $maxmatch = $self->get_next_arg($node);
        },
        '-x|--silent'       => sub {
            $opt_silent++;
        },
        '-X|--very-silent'  => sub {
            $opt_silent++;
            $opt_vsilent++;
        },
        '-h|--help'         => sub {
            print_help($program_name);
        },
        '-v|--version'      => sub {
            print_version();
            }
    };
    my $options = getoptions($getopt_rules, $getopt_opts, \@_);

    my ($infile, $outfile) = @ARGV;

    if (!$search) {
        usage($program_name, "Missing search string.");
    }
    if (!$infile) {
        usage($program_name, "Missing input file name.");
    }

    if (defined $replace) {

        if (!$outfile) {
            usage($program_name, "Missing output file name.");
        }

        $matches = binary_search_replace(
            {   search      => $search,
                replace     => $replace,

                infile      => $infile,
                outfile     => $outfile,

                minmatch    => $minmatch,
                maxmatch    => $maxmatch,
            }
        );
    }
    else {
        $matches = binary_file_matches($search, $infile);
    }

    if (!$opt_silent) {
        my @warnings = @{ File::BSED::warnings() };
        for my $warning (@warnings) {
            warn "Warning: $warning\n";
        }
    }

    if (!$opt_vsilent) {
        if ($matches == -1) {
            usage($program_name, File::BSED->errtostr());
        }
        elsif ($matches == 0) {
            die "File [$infile] does not match [$search]\n";
        }
        else {
            print "Matched $matches time(s)\n";
        }
    }

    return 0;

}

sub usage {
    my ($program_name, $errormsg) = @_;

    print {*STDERR} <<"EOF"

$program_name $File::BSED::VERSION

Usage: $program_name [-m <min>|-M <max>|-x|-X] [-s|-S] <search for> infile
       $program_name [-m <min>|-M <max>|-x|-X] [-s|-S] <search for> [-r|-R] <replace with> infile outfile
       $program_name [--help|--version]
       $program_name -D <text>

EOF
        ;
    if (defined $errormsg) {
        printf {*STDERR} "ERROR: %s\n", $errormsg;
        exit 1;
    }

    exit;
}

sub print_version {
    print {*STDOUT} <<"EOF"

plbsed $File::BSED::VERSION
$LICENSE_INFO
EOF
        ;
    exit;
}

sub print_help {
    my ($program_name) = @_;

    print {*STDOUT} <<"EOF"

$program_name $File::BSED::VERSION

DESCRIPTION

plbsed lets you search and replace binary strings and text strings in binary files.
If no replace string is given, it will only print out the number of matches found
in the file. Standard in/out will be used if the input/output file name argument is '-'.

OPTIONS

    -s, --search            A string of hex values to search for.
    -r, --replace           A string of hex values to replace with.
    -S, --searchstr         Search for string instead of hex.
    -R, --replacestr        Replace with a string.
    -m, --min               Need atleast these many matches.
    -M, --max               Stop at a number of matches.

    -x, --silent            Suppress warnings.
    -X, --very-silent       Suppress all output.
    -D, --dump              Dump the hex version of a text string.
    -v, --version           Print version information and exit.
    -h, --help              This help screen.

WILDCARDS

Wildcard bytes ('??') are supported in both the search and replace strings
Both search and replace strings must be multiples of whole bytes,
Only the characters 0-9, a-f, A-F and ? are acceptible

EXAMPLES

    plbsed --search 0xffc3 /bin/ls
    plbsed --search 0xffc3 --replace 0x00ea /bin/ls ./ls.patched
    plbsed --search --min 3 --max 12000 0xff /bin/ls
    plbsed --searchstr HELLO /bin/ls
    plbsed --dump 'hello world'"

SEE ALSO

    perldoc File::BSED
    man 1 gbsed
    man 3 libgbsed
$LICENSE_INFO
Report bug reports to <bug-file-bsed\@rt.cpan.org>

EOF
;

    exit 0;
}

__END__

=pod

=head1 NAME

plbsed - Search/replace in binary files.

=head1 VERSION

This document describes plbsed version 1.0.

=head1 SYNOPSIS

    plbsed [-m <min>|-M <max>|-x|-X] [-s|-S] <search for> infile
    plbsed [-m <min>|-M <max>|-x|-X] [-s|-S] <search for> [-r|-R] <replace with> infile outfile
    plbsed -D <text>
    plbsed [--help|--version

=head1 DESCRIPTION

plbsed lets you search and replace binary strings
and text strings in binary files.\nIf no replace
string is given, it will only print out the number
of matches found in the file.

you can search by using hex values in text strings, you can also use
wildcard matches (C<??>), which will match any wide byte.

Wildcards can not be used with the text search.

Standard in/out will be used if the input/output file
name argument is C<->.

These are all valid search strings:

    --search "0xffc300193ab2f63a"
    --search "0xff??00??3ab2f??a"
    --search "FF??00??3AB2F??A"

while these are not:

    --search "the quick brown fox" # must use --searchstr option for text
    --search "0xff?c33ab3?accc"    # no nybbles only wide bytes. (??  not ?).

=head1 OPTIONS

=head2 C<--search, -s>

A string of hex values to search for.

=head2 C<-r, --replace>

A string of hex values to replace with.

=head2 C<-S, --searchstr>

Search for string instead of hex.

=head2 C<-R, --replacestr>

Replace with a string.

=head2 C<-m, --min>

Need atleast these many matches.

=head2 C<-M, --max>

Stop at a number of matches.

=head2 C<-D, --dump>

Dump hex representation of a text string (shows what is done to a -S or -R argument).

=head2 C<-x, --silent>

Suppress warnings.

=head2 C<-X, --very-silent>

Suppress all output.

=head2 C<-v, --version>

Print version information and exit.

=head2 C<-h, --help>

This help screen.

=head1 EXAMPLES

        plbsed --search 0xffc3 /bin/ls
        plbsed --search 0xffc3 --replace 0x00ea /bin/ls ./ls.patched
        plbsed --search --min 3 --max 12000 0xff /bin/ls
        plbsed --searchstr HELLO /bin/ls
        cat /bin/ls | plbsed -x -S connected -R corrupted - - | od -cx
        plbsed -D "hello world"

=head1 CONFIGURATION AND ENVIRONMENT

C<libplbsed> requires no configuration file or environment variables.

=head1 INCOMPATIBILITIES

None known.

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to
C<bug-file-bsed@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.

=head1 SEE ALSO

=over 4

=item * L<libplbsed>

=back

=head1 AUTHOR

Ask Solem,   C<< ask@0x61736b.net >>.

=head1 ACKNOWLEDGEMENTS

Dave Dykstra C<< dwdbsed@drdykstra.us >>.
for C<bsed> the original program,

I<0xfeedface>
for the wildcards patch.

=head1 LICENSE AND COPYRIGHT

Copyright (C) 2007 Ask Solem <ask@0x61736b.net>

plbsed is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

plbsed is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE
SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE
SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE,
YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE
SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO
LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER
SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.


=cut