Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add way to reverse input pattern, update README and help output #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ curl -L 'https://github.com/tmbinc/bgrep/raw/master/bgrep.c' | gcc -O2 -x c -o $

usage:

bgrep [-B bytes] [-A bytes] [-C bytes] <hex> [<path> [...]]
bgrep [-rehv] [-B bytes] [-A bytes] [-C bytes] <hex> [<path> [...]]

-r: recurse into directories
-e: reverse hex input
-B: bytes_before
-A: bytes_after
-C: bytes_before and bytes_after
-h: print help
-v: print version
60 changes: 53 additions & 7 deletions bgrep.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ typedef struct Options {
char* pattern_path;
char* mask_path;
bool recurse;
bool reverse;
} Options;

void recurse(const char *path, const unsigned char *value, const unsigned char *mask, int len, const Options* options)
Expand Down Expand Up @@ -252,21 +253,40 @@ void die(const char* msg, ...)
exit(1);
}

void usage()
void version()
{
fprintf(stderr, "bgrep version: %s\n", BGREP_VERSION);
}

void usage(bool extended)
{
version();
fprintf(stderr, "usage:\n");
fprintf(stderr, " %s [-r] [-B bytes] [-A bytes] [-C bytes] <hex> [<path> [...]]\n", *original_argv);
fprintf(stderr, " %s [-r] -f <pattern> [-m <mask>] [<path> [...]]\n", *original_argv);
exit(1);
fprintf(stderr, "\t%s [-rehv] [-B bytes] [-A bytes] [-C bytes] <hex> [<path> [...]]\n", *original_argv);
fprintf(stderr, "\t%s [-rehv] -f <pattern> [-m <mask>] [<path> [...]]\n", *original_argv);
if (extended)
{
fprintf(stderr, "options:\n");
fprintf(stderr, "\t-r\trecurse into directories\n");
fprintf(stderr, "\t-e\treverse byte pattern\n");
fprintf(stderr, "\t-B\tprint number of bytes before result\n");
fprintf(stderr, "\t-A\tprint number of bytes after result\n");
fprintf(stderr, "\t-C\tprint number of bytes before and after result\n");
fprintf(stderr, "\t-f\tpath to pattern file\n");
fprintf(stderr, "\t-m\tpath to mask file\n");
fprintf(stderr, "\t-h\tprint this help\n");
fprintf(stderr, "\t-v\tprint version\n");
}
else
exit(1);
}

void parse_opts(int argc, char** argv, Options* options)
{
int c;
char* pattern_path, * mask_path;

while ((c = getopt(argc, argv, "A:B:C:f:m:r")) != -1)
while ((c = getopt(argc, argv, "A:B:C:f:m:rehv")) != -1)
{
switch (c)
{
Expand All @@ -288,8 +308,19 @@ void parse_opts(int argc, char** argv, Options* options)
case 'r':
options->recurse = true;
break;
case 'e':
options->reverse = true;
break;
case 'v':
version();
exit(0);
break;
case 'h':
usage(true);
exit(0);
break;
default:
usage();
usage(false);
}
}

Expand Down Expand Up @@ -383,7 +414,7 @@ int main(int argc, char **argv)
}
} else if (argc == 0) {
// a pattern is required.
usage();
usage(false);
} else
{
char *h = *argv++;
Expand Down Expand Up @@ -476,6 +507,21 @@ int main(int argc, char **argv)
}
}

if (options.reverse && len > 1)
{
for (int i = 0; i < len / 2; ++i)
{
unsigned char tmp;
tmp = value[i];
value[i] = value[len - i - 1];
value[len - i - 1] = tmp;

tmp = mask[i];
mask[i] = mask[len - i - 1];
mask[len - i - 1] = tmp;
}
}

if (argc == 0)
searchfile("stdin", 0, value, mask, len);
else
Expand Down