Skip to content

Commit

Permalink
skip archive binaries; buffer size problem remains
Browse files Browse the repository at this point in the history
  • Loading branch information
styxyang committed Sep 1, 2013
1 parent 06d5710 commit ca1e32a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define M_VAX_COFF_EX 0x0178
#define M_TAR_ZIP_LZW 0x1F9D
#define M_TAR_ZIP_LZH 0x1FA0
#define M_ARCH_BIN 0x213C617263683E0A
#define M_PDF 0x25504446
#define M_PSD 0x38425053
#define M_BMP 0x424D
Expand Down Expand Up @@ -45,6 +46,7 @@ static uint8_t magic_index[][MENTRY_LEN + 1] = {
{0x01, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2},
{0x1F, 0x9D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2},
{0x1F, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2},
{0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, 0x0A, 8},
{0x25, 0x50, 0x44, 0x46, 0x00, 0x00, 0x00, 0x00, 4},
{0x38, 0x42, 0x50, 0x53, 0x00, 0x00, 0x00, 0x00, 4},
{0x42, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2},
Expand Down
1 change: 1 addition & 0 deletions magic_defs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ CAFEBABE MACH_O_FAT_CLASS
0000002066747970 3GPP2_FTYP
0000001866747970 MP4_FTYP
0178 VAX_COFF_EX
213C617263683E0A ARCH_BIN
13 changes: 7 additions & 6 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ void parse_options(int argc, char *argv[]) {
struct option longopts[] = {
{ "kmp", no_argument, &opt.str_matching_algo, 0 },
{ "file", required_argument, NULL, 0 },
{ "dir", required_argument, NULL, 0 }
};

/* since the arguments are not so many
Expand All @@ -44,8 +43,6 @@ void parse_options(int argc, char *argv[]) {
dprintf(INFO, "%d, %d\n", optopt, optind);
if (strcmp(longopts[opt_idx].name, "file") == 0) {
opt.input_file = optarg;
} else if (strcmp(longopts[opt_idx].name, "dir") == 0) {
opt.input_dir = optarg;
} else if (opt.str_matching_algo == SM_ALGO_KMP) {
printf("using KMP algorithm\n");
}
Expand All @@ -61,14 +58,18 @@ void parse_options(int argc, char *argv[]) {
/* argv += optind; */

if (optind >= argc) {
dprintf(ERROR, "parse_options: missing pattern string");
print_usage();
}
opt.search_pattern = argv[optind];
opt.search_pattern = argv[optind++];
opt.search_patlen = strlen(opt.search_pattern);

if (optind < argc) {
dprintf(ERROR, "%d %d", optind, argc);
opt.input_dirs = argv + optind;
}
printf("search for \"%s\"-%zd\n", opt.search_pattern, opt.search_patlen);

if (!opt.input_file && !opt.input_dir) {
if (!opt.input_file && !opt.input_dirs) {
dprintf(ERROR, "parse_options: missing search scope");
printf("default in current directory\n");
if (!opt.search_pattern) {
Expand Down
2 changes: 1 addition & 1 deletion options.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ typedef struct {
char *search_pattern;
int search_patlen;
char *input_file;
char *input_dir;
char **input_dirs;
} cli_options;

extern cli_options opt;
Expand Down
5 changes: 4 additions & 1 deletion thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static uint32_t begin_of_line(char *fb, uint32_t mid)
return mid;
}

/* write matched data to buffer with line number `linum' and `lastlinum' */
static void worker_writebuffer(char *fb, uint32_t pos,
uint32_t linum, uint32_t lastlinum)
{
Expand Down Expand Up @@ -117,7 +118,8 @@ void *worker_thread(void *arg)
break;
}

dprintf(INFO, "T%d <%s> match at %d", tid, fi.filename, matchpos + startpos);
dprintf(INFO, "T%d <%s> match at %d", tid, fi.filename,
matchpos + startpos);

memset(buf, 0, sizeof(buf));
worker_writebuffer(fb, matchpos + startpos, linum, lastlinum);
Expand All @@ -128,6 +130,7 @@ void *worker_thread(void *arg)
startpos += matchpos + opt.search_patlen;
}

/* if offset is not zero, buffer contains something to write */
if (off) {
/* FIXME abstract as `write_filename' maybe */
pthread_mutex_lock(&outmtx);
Expand Down
1 change: 1 addition & 0 deletions trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct trie *trie_lookup(struct trie *root, uint8_t *target, int size)
return trie_walk(root, target, size, false, false);
}

/* will return the node regardless of whether it is a leaf */
struct trie *trie_scan(struct trie *root, uint8_t *target, int size)
{
assert(root);
Expand Down

0 comments on commit ca1e32a

Please sign in to comment.