=pod =head1 NAME libgbsed - Search/Replace in binary files. =head1 SYNOPSIS #include // using file names. struct gbsed_arguments { char *search; char *replace; char *infilename; char *outfilename; int minmatch; int maxmatch; }; typedef struct gbsed_arguments GBSEDargs; int gbsed_binary_search_replace(struct gbsed_arguments *) // using FILE*s struct fgbsed_arguments { char *search; char *replace; FILE *infile; FILE *outfile; int minmatch; int maxmatch; }; typedef struct fgbsed_arguments fGBSEDargs; int gbsed_fbinary_search_replace(struct fgbsed_arguments *); // Error handling extern int gbsed_errno; const char* gbsed_errtostr(int); =head1 DESCRIPTION This is , a binary stream editor. C lets you search and replace binary data in binary files by using hex values in text strings as search patterns. You can also use wildcard matches with C, which will match any wide byte. 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"; // only hex, no text. you would have to // convert the text to hex first. search = "0xff?c33ab3?accc"; // no nybbles only wide bytes. (?? not ?). =head1 FUNCTIONS =head2 C =head3 ARGUMENTS C uses a struct for it's arguments. The members of the argument struct is as follows: =over 4 =item C What to search for. This must be a string with hex values or the wildcard character sequence C, which will match any byte. The string can start with C<0x>, but this is optional. =item C What to replace with. Must also be a string with hex values, but no wildcards allowed. It must also be of the same length as the search string (This is by intention, as binary data is always in structured form. If you add extra information to a binary executable it will be rendered useless as address offsets will be shifted and relocation tables and internal address references will point to the wrong place). =item C The file name of the file to search in. =item C The file name to save the modified binary as. =item C Need at least C matches before any work. =item C Stop after C matches. A value of C<-1> means no limit. =back =head3 EXAMPLE USAGE #include #include #include extern int gbsed_errno; int main(int argc, char **argv) { int gbsed_ret; int sysret; const char *errmessage; GBSEDargs *bargs; sysret = EXIT_SUCCESS; bargs = (GBSEDargs *)malloc(sizeof(GBSEDargs)); if (bargs == NULL) { fprintf(stderr, "Out of memory!\n"); exit(1); } bargs->search = "0xff"; bargs->replace = "0x00"; bargs->infilename = "/bin/ls"; bargs->outfilename = "bsed.out"; bargs->minmatch = 1; // atleast one match. bargs->maxmatch = GBSED_MAXMATCH_NO_LIMIT; // no limit. if (argc > 1) bargs->infilename = argv[1]; gbsed_ret = gbsed_binary_search_replace(bargs); switch (gbsed_ret) { case GBSED_ERROR: errmessage = gbsed_errtostr(gbsed_errno); fprintf(stderr, "ERROR: %s\n", errmessage); sysret = EXIT_FAILURE; break; case GBSED_NO_MATCH: fprintf(stderr, "No match for %s found in %s\n", bargs->search, bargs->infilename ); sysret = EXIT_FAILURE; break; default: printf("Search for '%s' in '%s' matched %d times.\n", bargs->search, bargs->infilename, gbsed_ret ); break; } free(bargs); return sysret; } =head2 C This function returns a string describing what happened. if an error has occurred with either C or C. Example: extern int gbsed_errno; const char *errmessage; errmessage = gbsed_errtostr(gbsed_errno); fprintf(stderr, "ERROR: %s\n", errmessage); =head1 RETURN VALUES C returns C on failure. The error code can then be found in C, error codes are defined in I. and they all start with C and is C. To get a string containing the error message you have to call C with C as argument. =head2 Error codes returned by C =head3 C No matches found. =head3 C An error has occurred and a error code has been left in C. =head2 Error codes found in C =head3 C Search string was longer than the limit. =head3 C Replace string was longer than the limit. =head3 C Missing search string. =head3 C Missing replace string. =head3 C Missing input filename. =head3 C Missing output filename. =head3 C Invalid characters in search string. Only hex values and wildcards are allowed. =head3 C Wildcard must be wild byte, not nibble. (C not C). =head1 CONFIGURATION AND ENVIRONMENT C 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, or through the web interface at L. =head1 SEE ALSO =over 4 =item * L =back =head1 AUTHOR Ask Solem, C<< ask@0x61736b.net >>. =head1 ACKNOWLEDGEMENTS Dave Dykstra C<< dwdbsed@drdykstra.us >>. for C the original program, I<0xfeedface> for the wildcards patch. =head1 LICENSE AND COPYRIGHT Copyright (C) 2007 Ask Solem gbsed 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. gbsed 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 . =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