Esempio n. 1
0
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 search_file (const char *path, const char *term)
{
	int i;
	char *text;
	int text_len;
	size_t term_len = strlen(term);
	
	int rk_found, kp_found, am_found;
	size_t *rk_pos_array, *kp_pos_array, *am_pos_array;
	unsigned long t1, t2;
	
	if ((text_len = read_file(path, &text)) <= 0)
		perror("file read");
		
	printf("read %d bytes, searching for \"%s\" in \"%s\"\n", text_len, term, path);
	
	rdtscl(t1);
	if ((rk_found = rk_search(text, term, text_len, term_len, &rk_pos_array)) <= 0)
		printf("error searching\n");
	rdtscl(t2);
	printf("rk: found %d in %ld cycles\n", rk_found, t2 - t1);
	
	rdtscl(t1);	
	if ((kp_found = kmp_search(text, term, text_len, term_len, &kp_pos_array)) <= 0)
		printf("error searching\n");	
	rdtscl(t2);
	printf("kmp: found %d in %ld cycles\n", kp_found, t2 - t1);
		
	rdtscl(t1);		
	if ((am_found = am_search(text, term, text_len, term_len, &am_pos_array)) <= 0)
		printf("error searching\n");				
	rdtscl(t2);
	printf("am: found %d in %ld cycles\n", am_found, t2 - t1);
		
	if (kp_found != rk_found || kp_found != am_found) {
		printf("error!\n");
	}
	
	for (i = 0; i < kp_found; i++) {
		if (kp_pos_array[i] != rk_pos_array[i] || kp_pos_array[i] != am_pos_array[i])
			printf("search term %d error\n", i);
	//	printf("%ld/%ld/%ld: %.10s\n", kp_pos_array[i], rk_pos_array[i], am_pos_array[i], text + rk_pos_array[i]);
	}

	free(kp_pos_array);
	free(rk_pos_array);
	free(am_pos_array);
	free(text);
	
	return 0;
}
Esempio n. 3
0
File: kmp.c Progetto: hy0kl/study
int main(int argc, char *argv[])
{
    int haystack_len = 0;
    int pattern_len  = 0;
    int i;
    int pos;

    if (argc > 1 && argv[1][0])
    {
        snprintf(haystack, STR_BUF_LEN, "%s", argv[1]);
    }

    if (argc > 2 && argv[2][0])
    {
        snprintf(pattern, STR_BUF_LEN, "%s", argv[2]);
    }
    printf("haystack: %s\n", haystack);
    printf("pattern: %s\n", pattern);

    haystack_len = strlen(haystack);
    pattern_len  = strlen(pattern);

    get_nextval(pattern, pattern_len, next_map);
    for (i = 0; i < pattern_len; i++)
    {
       printf("next_map[%d] = %d\n", i, next_map[i]);
    }

    if (-1 != 
        (pos = kmp_search(haystack, haystack_len, pattern, pattern_len, next_map, 0)))
    {
        printf("OK, the pattern:[%s] is substr of haystack:[%s], position is:%d.\n",
            pattern, haystack, pos);
    }
    else
    {
        printf("Sorry, the pattern:[%s] is NOT substr of haystack:[%s].\n",
            pattern, haystack);
    }

    return 0;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
  string sd(D), sc(C);
  int la = strlen(A), lb = strlen(B), lc = strlen(C), ld = strlen(D);
  int ed = edist(A,la,B,lb);
  printf("Levenshtein distance (%s, %s): %d (should be 3)\n", A, B, ed);
  int ls = lcs(A,la,B,lb);
  printf("Longest common subsequence (%s, %s): %d (should be 4)\n", A, B, ls);
  

  strcpy(pattern, C);
  preffix_suffix(pattern, array);
  strcpy(text, D);
  printf("%s\n",pattern);
  show(array, strlen(pattern));
  printf("dopasowanie na m = %d\n",kmp_search(text, pattern));

 
 printf("rabi_karp, dopasowanie na m = %d\n", rabin_karp(sc, sd));

  return 0;
}
static void
ngx_http_prefetch_filter_url(ngx_http_request_t *r, u_char *buf_start, u_char *buf_end, ngx_log_t *log)
{
	ngx_int_t 				type = 0;	
	ngx_str_t				http_str = ngx_string("http:");
	ngx_int_t 				index = -1;
	ngx_int_t 				start = 0;	
	ngx_int_t 				end = 0;
	ngx_int_t 				i = 0;
	ngx_int_t 				j = 0;
	u_char					*buf_str;
	u_char					*temp_str;
	u_char					temp[1000];

	ngx_memzero(temp, 1000);
	buf_str = buf_start;
	start = 0;
	end  = 0;
	index = -1;
	ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "ngx_http_prefetch_body_filter need to prefetch ");
	while (1) {
		buf_str = buf_str + start;
		if (buf_str > buf_end) break;

		index = kmp_search(buf_str, buf_end - buf_str, http_str.data, http_str.len, log);

		if (index != -1) {
			if (index - 1 >= 0) {
				ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "ngx_http_prefetch_body_filter flag before http:%c", buf_str[index - 1]);
			}


			if (buf_str + index > buf_end) break;
			temp_str = buf_str;
			buf_str = buf_str + index;
			if (buf_str > buf_end) break;
			if (buf_str[0] == '"') {
				buf_str += 1;
			} 

			start = 0;
			end = -1;
			if (index - 1 >= 0) {
				if (temp_str[index - 1] == '\'') {
					end = kmp_search(buf_str, buf_end - buf_str, (u_char *)"\'", 1, log); 
				} else if (temp_str[index - 1] == '\"') {
					end = kmp_search(buf_str, buf_end - buf_str, (u_char *)"\"", 1, log); 
				} else if (temp_str[index - 1] == '(') {
					end = kmp_search(buf_str, buf_end - buf_str, (u_char *)")", 1, log); 
				} 
			} else {
				end = kmp_search(buf_str, buf_end - buf_str, (u_char *)"\"", 1, log); 
			}

			if (end != -1) {

				if (buf_str + end > buf_end || end > 1000) break;
				for (i = start, j = 0; i < end; i++) {
					if (buf_str[i] != '\\') {
						temp[j] = buf_str[i];
						j++;
					}
				}
				temp[j] = 0;			
				ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "ngx_http_prefetch_body_filter filter result:%s", temp);
				if ((type = ngx_http_valid_url(temp, r->connection->log)) > NGX_OK) {
					ngx_http_prefetch_handle_url(type, temp, end, r);
				}
				start = end;
			} else {
				break;
			}

		} else {
			break;
		}
	}			
}
static ngx_int_t 
ngx_http_valid_url(u_char *url, ngx_log_t *log) 
{
	ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "ngx_http_valid_url");
	
	size_t 		len;
	size_t		i = 0;
	ngx_int_t 	ret = -1;
	ngx_str_t	http_str = ngx_string("http:");
	ngx_str_t	gif_str = ngx_string(".gif");
	ngx_str_t	png_str = ngx_string(".png");
	ngx_str_t	jpg_str = ngx_string(".jpg");
	ngx_str_t	css_str = ngx_string(".css");
	ngx_str_t	js_str = ngx_string(".js");
	ngx_str_t	flv_str = ngx_string(".flv");
	ngx_str_t	ico_str = ngx_string(".ico");
	ngx_str_t	swf_str = ngx_string(".swf");
	ngx_str_t	dot_str = ngx_string("?");
	
	ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "ngx_http_valid_url");

	len = strlen((char *)url);
	if (len < 8) {
		goto error;
	} 
	for (i = 0; i < http_str.len; i++) {
		if (http_str.data[i] != url[i]) {
			goto error;
		}
	}
	i = len - 1;
	while (url[i] == ' ') {
		url[i] = 0;
	}	
	len = strlen((char *)url);
	
	// check if there is a ? 
	ret = kmp_search(url, len, dot_str.data, dot_str.len, log);
	if (ret == -1) {
		if (len > 5) {
			url = url + len - 5;
			len = 5;
		}
	} else {
		url += ret - 5;
		len = len - (ret - 5);
	}



	// check if this is a gif
	if (kmp_search(url, len, gif_str.data, gif_str.len, log) != -1) {
		return NGX_GIF_RETURN;
	}
	// check if this is a png
	if (kmp_search(url, len, png_str.data, png_str.len, log) != -1) {
		return NGX_PNG_RETURN;
	}
	// check if this is a jpg
	if (kmp_search(url, len, jpg_str.data, jpg_str.len, log) != -1) {
		return NGX_JPG_RETURN;
	}
	// check if this is a css
	if (kmp_search(url, len, css_str.data, css_str.len, log) != -1) {
		return NGX_CSS_RETURN;
	}
	// check if this is a js
	if (kmp_search(url, len, js_str.data, js_str.len, log) != -1) {
		return NGX_JS_RETURN;
	}
	// check if this is a flv
	if (kmp_search(url, len, flv_str.data, flv_str.len, log) != -1) {
		return NGX_FLV_RETURN;
	}
	// check if this is a ico
	if (kmp_search(url, len, ico_str.data, ico_str.len, log) != -1) {
		return NGX_ICO_RETURN;
	}
	// check if this is a swf
	if (kmp_search(url, len, swf_str.data, swf_str.len, log) != -1) {
		return NGX_SWF_RETURN;
	}

	
	return NGX_OK;
error:
	return NGX_ERROR;

}
static ngx_int_t
ngx_http_prefetch_header_filter(ngx_http_request_t *r)
{
	ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_prefetch_header_filter");
	ngx_http_upstream_t				*u;
	ngx_http_prefetch_ctx_t			*ctx;
	ngx_pool_t 						*temp_pool;
	ngx_http_prefetch_filter_t 		*filter;

	ngx_str_t 						 gzip_type = ngx_string("gzip");

	u = r->upstream;

	
	ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_prefetch_header_filter going check"); 

	ctx = ngx_http_get_module_ctx(r, ngx_http_prefetch_filter_module);

	if (ctx == NULL) {
		ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_prefetch_ctx_t));
		if (ctx == NULL) {
			return ngx_http_next_header_filter(r);
			//return NGX_ERROR;
		}
		
		temp_pool = ngx_create_pool(1024, ngx_cycle->log);
		if (temp_pool == NULL) {
			return ngx_http_next_header_filter(r);
		}
		filter = ngx_pcalloc(temp_pool, sizeof(ngx_http_prefetch_filter_t));
		if (filter == NULL) {
			ngx_destroy_pool(temp_pool);
			return ngx_http_next_header_filter(r);
		}

		filter->pool = temp_pool;
		ctx->filter = filter;

		/*now we need to check if we should analysis the response*/ 
		if (u != NULL &&
			u->headers_in.content_type != NULL &&
		   	u->headers_in.content_type->value.data != NULL &&
			ngx_strncmp((const char *)u->headers_in.content_type->value.data, 
					PREFETCH_CONTENT_TYPE, strlen(PREFETCH_CONTENT_TYPE)) == 0) {

			ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_prefetch_header_filter content_type");
			ctx->flag = PREFETCH_FLAG;
			ctx->in_buf = ngx_create_temp_buf(r->pool, IN_BUF_SIZE);
			if (ctx->in_buf == NULL) {
				ctx->flag = PREFETCH_NOT_FLAG;
				ctx->gzip_flag = GZIP_NOT_FLAG;
			}

			ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_prefetch_header_filter gzip check");

#if (NGX_HTTP_GZIP)
			if (ctx->flag != PREFETCH_NOT_FLAG &&
				u->headers_in.content_encoding != NULL &&
				u->headers_in.content_encoding->value.data != NULL &&
				kmp_search(u->headers_in.content_encoding->value.data, u->headers_in.content_encoding->value.len, gzip_type.data, gzip_type.len, r->connection->log) != -1) {
				ctx->gzip_flag = GZIP_FLAG;
				ctx->out_buf = ngx_create_temp_buf(r->pool, OUT_BUF_SIZE);
				if (ctx->out_buf == NULL) {
					ctx->flag = PREFETCH_NOT_FLAG;
					ctx->gzip_flag = GZIP_NOT_FLAG;
				}

			}
#endif
		}	
		ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_prefetch_header_filter type check over");


		ngx_http_set_ctx(r, ctx, ngx_http_prefetch_filter_module);
	}

	ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_prefetch_header_filter end");
	

	return ngx_http_next_header_filter(r);
}
Esempio n. 8
0
int main(void){
   char *text = "ABC ABCDAB ABCDABCDABDE";
   char *pattern = "ABCDABD";
   printf("[pattern found] %d\n", kmp_search(text,pattern));
   return EXIT_SUCCESS;
}