diff --git a/magic.c b/magic.c index 8c3684e..e4456d2 100644 --- a/magic.c +++ b/magic.c @@ -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 @@ -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}, diff --git a/magic_defs b/magic_defs index 1be5d8a..fe7ce1a 100644 --- a/magic_defs +++ b/magic_defs @@ -34,3 +34,4 @@ CAFEBABE MACH_O_FAT_CLASS 0000002066747970 3GPP2_FTYP 0000001866747970 MP4_FTYP 0178 VAX_COFF_EX +213C617263683E0A ARCH_BIN diff --git a/options.c b/options.c index 7819b8c..a0e585e 100644 --- a/options.c +++ b/options.c @@ -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 @@ -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"); } @@ -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) { diff --git a/options.h b/options.h index c6f6bf9..e6ceb75 100644 --- a/options.h +++ b/options.h @@ -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; diff --git a/thread.c b/thread.c index 1c0ec97..453e387 100644 --- a/thread.c +++ b/thread.c @@ -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) { @@ -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); @@ -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); diff --git a/trie.c b/trie.c index a555e20..1ef2a3f 100644 --- a/trie.c +++ b/trie.c @@ -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);