Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

str_search.c

Go to the documentation of this file.
00001 
00002 /*
00003  * Copyright (C) 2005 Sourcefire,Inc.
00004  */
00005 
00006 #include <stdlib.h>
00007 
00008 #include "str_search.h"
00009 #include "mpse.h"
00010 
00011 typedef struct tag_search
00012 {
00013     void *mpse;
00014     unsigned int max_len;
00015 } t_search;
00016 
00017 static t_search *_mpse = NULL;
00018 static unsigned int  _num_mpse;
00019 
00020 int SearchInit(unsigned int num)
00021 {
00022     unsigned int i;
00023 
00024     _num_mpse = num;
00025 
00026     _mpse = malloc(sizeof(t_search) * num);
00027     if ( _mpse == NULL )
00028         return -1;
00029 
00030     for ( i = 0; i < num; i++ )
00031     {
00032         _mpse[i].mpse = mpseNew(MPSE_AC);
00033         _mpse[i].max_len = 0;
00034     }
00035     return 0;
00036 }
00037 
00038 void SearchFree()
00039 {
00040     unsigned int i;
00041 
00042     if ( _mpse != NULL )
00043     {
00044         for ( i = 0; i < _num_mpse; i++ )
00045         {
00046             if ( _mpse[i].mpse != NULL )
00047                 mpseFree(_mpse[i].mpse);
00048         }
00049         free(_mpse);
00050     }
00051 }
00052 
00053 
00054 /*  Do efficient search of data */
00055 int SearchFindString(unsigned int mpse_id, char *str, int str_len, int (*Match) (void *, int, void *))
00056 {
00057     int num;
00058 
00059     if ( str_len == 0 )
00060         str_len = _mpse[mpse_id].max_len;
00061     num = mpseSearch(_mpse[mpse_id].mpse, str, str_len, Match, (void *) str);
00062     
00063     return num;
00064 }
00065 
00066 
00067 void SearchAdd(unsigned int mpse_id, char *pat, int id)
00068 {
00069     unsigned int len = strlen(pat);
00070 
00071     mpseAddPattern(_mpse[mpse_id].mpse, pat, len, 1, 0, 0, (void *)(long) id, 0);
00072 
00073     if ( len > _mpse[mpse_id].max_len )
00074         _mpse[mpse_id].max_len = len;
00075 }
00076 
00077 void SearchPrepPatterns(unsigned int mpse_id)
00078 {
00079     mpsePrepPatterns(_mpse[mpse_id].mpse);
00080 }

Generated on Sun May 14 14:51:17 2006 by  doxygen 1.4.2