#include "hs.h"
#include "hs_common.h"
#include "hs_compile.h"
#include "hs_runtime.h"
#include <string.h>
#include <stdio.h>
typedef unsigned int UINT32;
typedef unsigned short UINT16;
typedef unsigned char UINT8;
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
/*---------------------------test---------------------------------*/
#define TEST_PATTERN_HS
#ifdef TEST_PATTERN_HS
char pat[3][64];
UINT32 pat_num= 0;
int ids[64]={5,6,7,8,9,10};
UINT32 flags_som[4] = { HS_FLAG_SOM_LEFTMOST,HS_FLAG_SOM_LEFTMOST,HS_FLAG_SOM_LEFTMOST,HS_FLAG_SOM_LEFTMOST };
//char *data_arr="152518756aAbBFfd770722426dbdb32bdb23456789111111";
//char *data_arr="123abc@sina.com";
char *data_arr="POST /js6/s?sid=ABdugdXzWAeBSHwIHwzzAULTVnTRsTdG&func=mbox:readMessage&LeftNavRecieveMailClick=1&l=read&action=read HTTP/1.1\r\nPOST /js6/s?sid=ABdugdXzWAeBSHwIHwzzAULTVnTRsTdG&func=mbox:readMessage&LeftNavRecieveMailClick=1&l=read&action=read HTTP/1.1";
typedef struct _Ctx
{
int id[10];
int index;/*返回计数*/
}Ctx;
int eventHandlerSV(unsigned int id, unsigned long long from,unsigned long long to, unsigned int flags, void *arg)
{
Ctx* ctx = (Ctx *)arg;
ctx->id[ctx->index++] = id;
printf("--matched :%d from:%d to:%d pattern:%s, id: %d \n",id,from,to,pat[id], id);
return 0;
}
int test_stream_som()
{
hs_database_t *database;
hs_compile_error_t *compile_err;
unsigned int length;
hs_scratch_t *scratch = NULL;
hs_error_t err_ret=0;
size_t scratch_size = 0;
size_t db_size=0;
int i=0;
int ret = 0;
hs_stream_t *stream = 0;
uint8_t *tmpPatten[1024] = {[0 ... 1024 -1] = NULL};
for(i = 0; i < pat_num; i++)
{
tmpPatten[i] = pat[i];
}
if (hs_compile_multi((const char * const*)tmpPatten, flags_som, ids, pat_num, HS_MODE_BLOCK, NULL, &database,&compile_err) != HS_SUCCESS)
{
fprintf(stderr, "ERROR: Unable to compile pattern : %s\n", compile_err->message);
hs_free_compile_error(compile_err);
return -1;
}
if (hs_alloc_scratch(database, &scratch) != HS_SUCCESS)
{
fprintf(stderr, "ERROR: Unable to allocate scratch space. Exiting.\n");
hs_free_database(database);
return -1;
}
hs_scratch_size( scratch,&scratch_size);
hs_database_size(database, &db_size);
printf("\n\n-------[%s] , STREAM_MODE SOM_LEFTMOST ,scratch_size:%d db_size:%d-------------\n", __func__,(int)scratch_size,(int)db_size);
{
Ctx ctx ;
ctx.index = 0;
if (hs_scan(database, (const char*)data_arr,strlen( data_arr), 0, scratch, eventHandlerSV, &ctx) != HS_SUCCESS)
{
printf("ERROR: Unable to scan input buffer. Exiting.\n");
hs_free_database(database);
return -1;
}
else
{
printf("Get Number=%d\n", ctx.id[0]);
}
}
hs_free_scratch(scratch);
hs_free_database(database);
return 0;
}
int main( int argc ,char *argv[] )
{
char *a[3] = {"/js6/s[?]sid=.*?&.*?func=mbox:readMessage&.*?action=read*", "0000", "2700"};
snprintf(pat[0], 14,"%s", a[0]);
ids[0]=7;
ids[1]=9;
snprintf(pat[1], 5, "%s", a[1]);
pat_num = 2;
flags_som[0]= HS_FLAG_SOM_LEFTMOST;
flags_som[1]= HS_FLAG_SOM_LEFTMOST;
test_stream_som();
}
#endif