ssize_t bm_search_simple(const char *str, const char *substr) { bm_t binfo; int ires; size_t len; /* * Basic sanity checks */ if (!str || !substr || !(len = strlen(str))) { errno = EINVAL; return((ssize_t)-1); } /* * Build the skip table */ if ((ires = bm_skip_init(&binfo, substr))) return((ssize_t)ires); /* * And perform the search */ return(bm_search((const unsigned char *)str, len, &binfo)); }
static int get_tz_version(char *ver_str, size_t len) { int ret = 0; int fd; char *tz_data = NULL; char *offset = NULL; fd = open(TZ_PART_PATH, O_RDONLY); if (fd < 0) { ret = errno; goto err_ret; } tz_data = (char *) mmap(NULL, TZ_SZ, PROT_READ, MAP_PRIVATE, fd, 0); if (tz_data == (char *)-1) { ret = errno; goto err_fd_close; } /* Do Boyer-Moore search across TZ data */ offset = bm_search(tz_data, TZ_SZ, TZ_VER_STR, TZ_VER_STR_LEN); if (offset != NULL) { strncpy(ver_str, offset + TZ_VER_STR_LEN, len); } else { ret = -ENOENT; } munmap(tz_data, TZ_SZ); err_fd_close: close(fd); err_ret: return ret; }
void check_one ( const Container &haystack, const Container &needle, int where ) { int expected; switch ( where ) { case -2: { auto it = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ()); assert ( it != haystack.end ()); expected = std::distance ( haystack.begin (), it); } break; case -3: expected = haystack.size () - needle.size (); break; default: expected = where; break; } std::cout << "Needle is " << needle.size () << " entries long\n"; duration stds = std_search ( haystack, needle, expected ); std::cout << "Standard search took : " << stds.count () << "\t(" << dur_pct ( stds, stds ) << ")" << std::endl; duration def = default_search ( haystack, needle, expected ); std::cout << "Default search took : " << def.count () << "\t(" << dur_pct ( stds, def ) << ")" << std::endl; duration def_p = default_search ( haystack, needle, expected ); std::cout << "Default search w/pred took: " << def_p.count () << "\t(" << dur_pct ( stds, def_p ) << ")" << std::endl; duration bm = bm_search ( haystack, needle, expected ); std::cout << "Boyer-Moore search took: " << bm.count () << "\t(" << dur_pct ( stds, bm ) << ")" << std::endl; duration bm_map = bm_search_map ( haystack, needle, expected ); std::cout << "Boyer-Moore (map) search took: " << bm_map.count () << "\t(" << dur_pct ( stds, bm_map ) << ")" << std::endl; duration bmh = bmh_search ( haystack, needle, expected ); std::cout << "Boyer-Moore-Horspool search took: " << bmh.count () << "\t(" << dur_pct ( stds, bmh ) << ")" << std::endl; duration bmh_map = bmh_search_map ( haystack, needle, expected ); std::cout << "Boyer-Moore-Horspool (map) search took: " << bmh_map.count ()<< "\t(" << dur_pct ( stds, bmh_map ) << ")" << std::endl; }
void callTextSearch(uchar text[], uchar pattern[]) { int position , patn_len ; patn_len = strlen((char*)pattern); /* パターンの長さをセット */ #if BF printf("--- B F ---\n"); printf("%s\n",text); position = brute_force_search(text, pattern); printf("position=%d\n",position); #endif #if KMP printf("--- KMP ---\n"); init_next(pattern); printNext(pattern, patn_len); printf("%s\n",text); position = kmp_search(text, pattern); printf("position=%d\n",position); #endif #if BM printf("--- B M ---\n"); init_skip(pattern); printSkip(pattern, patn_len); printf("%s\n",text); position = bm_search(text, pattern); printf("position=%d\n",position); #endif }
int main(void) { unsigned char *result; result = bm_search(original_text, original_pattern); if(result == NULL){ printf("見つかりませんでした\n"); }else{ printf("見つかりました\n"); } return 0; }
int main(int argc, char *argv[]) { int pos; bm_setup(argv[1], strlen(argv[1])); pos = bm_search(argv[2], strlen(argv[2])); printf("Match at pos %d\n", pos); exit(0); }
int scan_file(char *pathname) { int fd; size_t len; unsigned char *buf; struct stat sb; fd = open(pathname, O_RDONLY); if (fd < 0) { if (verbose) { pthread_mutex_lock(&print_lock); fprintf(stderr, "%s : ERR : open() failed: %s\n", pathname, strerror(errno)); pthread_mutex_unlock(&print_lock); } return -1; } if (fstat(fd, &sb) < 0) { if (verbose) { pthread_mutex_lock(&print_lock); fprintf(stderr, "%s : ERR : fstat() failed: %s\n", pathname, strerror(errno)); pthread_mutex_unlock(&print_lock); } close(fd); return -1; } len = sb.st_size; if (debug > 1) fprintf(stderr, "*** Scanning file %s (%u Mbytes)\n", pathname, (unsigned int) (len)); buf = (unsigned char *) mmap(NULL, len, PROT_READ, MAP_PRIVATE|MAP_NORESERVE, fd, 0); if (buf == MAP_FAILED) { if (verbose) { pthread_mutex_lock(&print_lock); fprintf(stderr, "%s : ERR : mmap() failed: %s\n", pathname, strerror(errno)); pthread_mutex_unlock(&print_lock); } close(fd); return -1; } if (rstr) { int code; code = bm_search(&bmb, buf, len, matchfun, pathname); } else { pthread_mutex_lock(&print_lock); printf("%s : 0 : ", pathname); print_output(buf, len); putchar('\n'); pthread_mutex_unlock(&print_lock); } munmap((char *) buf, len); close(fd); return 1; }
int main(int argc, char *const argv[]) { size_t before = 16; size_t after = 16; int color = 0; bool hexlify = true; int ch; long ia, ib; while ((ch = getopt(argc, argv, "a:b:chH")) != -1) { switch(ch) { case 'a': ia = strtol(optarg, NULL, 10); if ((ia > 0) && (ia < 1024)) { after = (size_t)ia; } break; case 'b': ib = strtol(optarg, NULL, 10); if ((ib > 0) && (ib < 1024)) { before = (size_t)ib; } break; case 'c': color++; break; case 'H': hexlify = !hexlify; break; case 'h': default: usage(); break; } } if (!isatty(STDOUT_FILENO) && color) { color--; } argc -= optind; argv += optind; if (argc < 2) { usage(); } size_t pattern_size = 0; uint8_t *pattern; if (hexlify) { pattern = hex_decode(argv[0], &pattern_size); } else { pattern_size = strlen(argv[0]); pattern = (uint8_t*)strndup(argv[0], pattern_size); } if (!pattern) { fprintf(stderr, "Invalid pattern\n"); } // initialize boyer-moore patterns int *delta1 = (int*)malloc(ALPHABET_LEN * sizeof(int)); int *delta2 = (int*)malloc(pattern_size * sizeof(int)); make_delta1(delta1, pattern, pattern_size); make_delta2(delta2, pattern, pattern_size); size_t count = 0; size_t errors = 0; for (int f=1; f<argc; f++) { int first = 1; const char *file_name = argv[f]; int fd = open(file_name, O_RDONLY); if (fd < 0) { fprintf(stderr, "Open error "); perror(file_name); errors++; continue; } // Get the file size, to make mmap search the whole thing struct stat file_stat; if (fstat(fd, &file_stat) != 0) { fprintf(stderr, "Stat error "); perror(file_name); errors++; close(fd); // ignore error continue; } size_t file_size = (size_t)file_stat.st_size; if ((file_size == 0) || !S_ISREG(file_stat.st_mode)) { close(fd); // ignore error continue; } const uint8_t *file = mmap(0, file_size, PROT_READ, MAP_SHARED, fd, 0); if (file == MAP_FAILED) { fprintf(stderr, "Mmap error "); perror(file_name); errors++; close(fd); // ignore error continue; } // Find all matches, don't worry about overlaps size_t last = 0; size_t next = 0; while ((next = bm_search(file + last, file_size - last, pattern, pattern_size, delta1, delta2)) != NOT_FOUND) { if (first) { first = 0; printf("---- %s ----\n", file_name); } print_match(file, last + next, pattern_size, before, after, color); last += next + pattern_size; count++; } if (munmap((void*)file, file_size) != 0) { fprintf(stderr, "Unmap error "); perror(file_name); errors++; } if (close(fd) != 0) { fprintf(stderr, "Close error "); perror(file_name); errors++; } } free(delta2); free(delta1); free(pattern); if (count > 0) { return 0; } if (errors > 0) { return 2; } return 1; }
/******************************************************************************** *Function: search_chunk *Description: Analyze the given chunk by running each defined search spec on it *Return: TRUE/FALSE **********************************************************************************/ int search_chunk(f_state *s, unsigned char *buf, f_info *i, u_int64_t chunk_size, u_int64_t f_offset) { u_int64_t c_offset = 0; //u_int64_t foundat_off = 0; //u_int64_t buf_off = 0; unsigned char *foundat = buf; unsigned char *current_pos = NULL; unsigned char *header_pos = NULL; unsigned char *newbuf = NULL; unsigned char *ind_ptr = NULL; u_int64_t current_buflen = chunk_size; int tryBS[3] = { 4096, 1024, 512 }; unsigned char *extractbuf = NULL; u_int64_t file_size = 0; s_spec *needle = NULL; int j = 0; int bs = 0; int rem = 0; int x = 0; int found_ind = FALSE; off_t saveme; //char comment[32]; for (j = 0; j < s->num_builtin; j++) { needle = &search_spec[j]; foundat = buf; /*reset the buffer for the next search spec*/ #ifdef DEBUG printf(" SEARCHING FOR %s's\n", needle->suffix); #endif bs = 0; current_buflen = chunk_size; while (foundat) { needle->written = FALSE; found_ind = FALSE; memset(needle->comment, 0, COMMENT_LENGTH - 1); if (chunk_size <= (foundat - buf)) { #ifdef DEBUG printf("avoided seg fault in search_chunk()\n"); #endif foundat = NULL; break; } current_buflen = chunk_size - (foundat - buf); //if((foundat-buf)< 1 ) break; #ifdef DEBUG //foundat_off=foundat; //buf_off=buf; //printf("current buf:=%llu (foundat-buf)=%llu \n", current_buflen, (u_int64_t) (foundat_off - buf_off)); #endif if (signal_caught == SIGTERM || signal_caught == SIGINT) { user_interrupt(s, i); printf("Cleaning up.\n"); signal_caught = 0; } if (get_mode(s, mode_quick)) /*RUN QUICK SEARCH*/ { #ifdef DEBUG //printf("quick mode is on\n"); #endif /*Check if we are not on a block head, adjust if so*/ rem = (foundat - buf) % s->block_size; if (rem != 0) { foundat += (s->block_size - rem); } if (memwildcardcmp(needle->header, foundat, needle->header_len, needle->case_sen ) != 0) { /*No match, jump to the next block*/ if (current_buflen > s->block_size) { foundat += s->block_size; continue; } else /*We are out of buffer lets go to the next search spec*/ { foundat = NULL; break; } } header_pos = foundat; } else /**********RUN STANDARD SEARCH********************/ { foundat = bm_search(needle->header, needle->header_len, foundat, current_buflen, //How much to search through needle->header_bm_table, needle->case_sen, //casesensative SEARCHTYPE_FORWARD); header_pos = foundat; } if (foundat != NULL && foundat >= 0) /*We got something, run the appropriate heuristic to find the EOF*/ { current_buflen = chunk_size - (foundat - buf); if (get_mode(s, mode_ind_blk)) { #ifdef DEBUG printf("ind blk detection on\n"); #endif //dumpInd(foundat+12*1024,1024); for (x = 0; x < 3; x++) { bs = tryBS[x]; if (ind_block(foundat, current_buflen, bs)) { if (get_mode(s, mode_verbose)) { sprintf(needle->comment, " (IND BLK bs:=%d)", bs); } //dumpInd(foundat+12*bs,bs); #ifdef DEBUG printf("performing mem move\n"); #endif if(current_buflen > 13 * bs)//Make sure we have enough buffer { if (!memmove(foundat + 12 * bs, foundat + 13 * bs, current_buflen - 13 * bs)) break; found_ind = TRUE; #ifdef DEBUG printf("performing mem move complete\n"); #endif ind_ptr = foundat + 12 * bs; current_buflen -= bs; chunk_size -= bs; break; } } } } c_offset = (foundat - buf); current_pos = foundat; /*Now lets analyze the file and see if we can determine its size*/ // printf("c_offset=%llu %x %x %llx\n", c_offset,foundat,buf,c_offset); foundat = extract_file(s, c_offset, foundat, current_buflen, needle, f_offset); #ifdef DEBUG if (foundat == NULL) { printf("Foundat == NULL!!!\n"); } #endif if (get_mode(s, mode_write_all)) { if (needle->written == FALSE) { /*write every header we find*/ if (current_buflen >= needle->max_len) { file_size = needle->max_len; } else { file_size = current_buflen; } sprintf(needle->comment, " (Header dump)"); extractbuf = (unsigned char *)malloc(file_size * sizeof(char)); memcpy(extractbuf, header_pos, file_size); write_to_disk(s, needle, file_size, extractbuf, c_offset + f_offset); free(extractbuf); } } else if (!foundat) /*Should we search further?*/ { /*We couldn't determine where the file ends, now lets check to see * if we should try again */ if (current_buflen < needle->max_len) /*We need to bridge the gap*/ { #ifdef DEBUG printf(" Bridge the gap\n"); #endif saveme = ftello(i->handle); /*grow the buffer and try to extract again*/ newbuf = read_from_disk(c_offset + f_offset, i, needle->max_len); if (newbuf == NULL) break; current_pos = extract_file(s, c_offset, newbuf, needle->max_len, needle, f_offset); /*Lets put the fp back*/ fseeko(i->handle, saveme, SEEK_SET); free(newbuf); } else { foundat = header_pos; /*reset the foundat pointer to the location of the last header*/ foundat += needle->header_len + 1; /*jump past the header*/ } } } if (found_ind) { /*Put the ind blk back in, re-arrange the buffer so that the future blks names come out correct*/ #ifdef DEBUG printf("Replacing the ind block\n"); #endif /*This is slow, should we do this??????*/ if (!memmove(ind_ptr + 1 * bs, ind_ptr, current_buflen - 13 * bs)) break; memset(ind_ptr, 0, bs - 1); chunk_size += bs; memset(needle->comment, 0, COMMENT_LENGTH - 1); } } //end while } return TRUE; }