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

mpse.c

Go to the documentation of this file.
00001 /*
00002 *  $Id$
00003 *
00004 *   mpse.c
00005 *    
00006 *   An abstracted interface to the Multi-Pattern Matching routines,
00007 *   thats why we're passing 'void *' objects around.
00008 *
00009 *   Copyright (C) 2002 SourceFire, Inc
00010 *   Marc A Norton <mnorton@sourcefire.com>
00011 *
00012 **  
00013 ** This program is free software; you can redistribute it and/or modify
00014 ** it under the terms of the GNU General Public License as published by
00015 ** the Free Software Foundation; either version 2 of the License, or
00016 ** (at your option) any later version.
00017 **
00018 ** This program is distributed in the hope that it will be useful,
00019 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 ** GNU General Public License for more details.
00022 **
00023 ** You should have received a copy of the GNU General Public License
00024 ** along with this program; if not, write to the Free Software
00025 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00026 **
00027 */
00028 #ifdef HAVE_CONFIG_H
00029 #include "config.h"
00030 #endif
00031 
00032 #include "bitop.h"
00033 #include "mwm.h"
00034 #include "acsmx.h"
00035 #include "acsmx2.h"
00036 #include "sfksearch.h"
00037 #include "mpse.h"  
00038 
00039 static UINT64 s_bcnt=0;
00040 
00041 typedef struct _mpse_struct {
00042 
00043   int    method;
00044   void * obj;
00045 
00046 }MPSE;
00047 
00048 void * mpseNew( int method )
00049 {
00050    MPSE * p;
00051 
00052    p = (MPSE*)malloc( sizeof(MPSE) );
00053    if( !p ) return NULL;
00054 
00055    p->method=method;
00056    p->obj   =NULL;
00057    s_bcnt  =0;
00058 
00059    switch( method )
00060    {
00061      case MPSE_AUTO:
00062      case MPSE_MWM:
00063         p->obj = mwmNew();
00064         return (void*)p;
00065      break;
00066      case MPSE_AC:
00067        p->obj = acsmNew();
00068        return (void*)p;
00069      break;
00070      case MPSE_ACF:
00071        p->obj = acsmNew2();
00072        if(p->obj)acsmSelectFormat2((ACSM_STRUCT2*)p->obj,ACF_FULL  );
00073        return (void*)p;
00074      break;
00075      case MPSE_ACS:
00076        p->obj = acsmNew2();
00077        if(p->obj)acsmSelectFormat2((ACSM_STRUCT2*)p->obj,ACF_SPARSE  );
00078        return (void*)p;
00079      break;
00080      case MPSE_ACB:
00081        p->obj = acsmNew2();
00082        if(p->obj)acsmSelectFormat2((ACSM_STRUCT2*)p->obj,ACF_BANDED  );
00083        return (void*)p;
00084      break;
00085      case MPSE_ACSB:
00086        p->obj = acsmNew2();
00087        if(p->obj)acsmSelectFormat2((ACSM_STRUCT2*)p->obj,ACF_SPARSEBANDS  );
00088        return (void*)p;
00089      break;
00090      case MPSE_KTBM:
00091      case MPSE_LOWMEM:
00092         p->obj = KTrieNew();
00093        return (void*)p;
00094      break; 
00095      
00096      default:
00097        return 0;
00098      break; 
00099    }
00100 }
00101 
00102 
00103 void   mpseFree( void * pv )
00104 {
00105   MPSE * p = (MPSE*)pv;
00106  
00107   switch( p->method )
00108    {
00109      case MPSE_AC:
00110        if(p->obj)acsmFree(p->obj);
00111        free(p);
00112        return ;
00113      break;
00114      case MPSE_ACF:
00115      case MPSE_ACS:
00116      case MPSE_ACB:
00117      case MPSE_ACSB:
00118        if(p->obj)acsmFree2(p->obj);
00119        free(p);
00120        return ;
00121      break;
00122      case MPSE_AUTO:
00123      case MPSE_MWM:
00124        if(p->obj)mwmFree( p->obj );
00125        free( p );
00126      break;
00127      case MPSE_KTBM:
00128      case MPSE_LOWMEM:
00129        return ;
00130      break;
00131      default:
00132        return ;
00133      break; 
00134    }
00135 }
00136 
00137 int  mpseAddPattern ( void * pv, void * P, int m, 
00138              unsigned noCase,unsigned offset, unsigned depth,  void* ID, int IID )
00139 {
00140   MPSE * p = (MPSE*)pv;
00141 
00142   switch( p->method )
00143    {
00144      case MPSE_AC:
00145        return acsmAddPattern( (ACSM_STRUCT*)p->obj, (unsigned char *)P, m,
00146               noCase, offset, depth, ID, IID );
00147      break;
00148      case MPSE_ACF:
00149      case MPSE_ACS:
00150      case MPSE_ACB:
00151      case MPSE_ACSB:
00152        return acsmAddPattern2( (ACSM_STRUCT2*)p->obj, (unsigned char *)P, m,
00153               noCase, offset, depth, ID, IID );
00154      break;
00155      case MPSE_MWM:
00156        return mwmAddPatternEx( p->obj, (unsigned char *)P, m, 
00157               noCase, offset, depth, (void*)ID, IID );
00158      break;
00159      case MPSE_KTBM:
00160      case MPSE_LOWMEM:
00161        return KTrieAddPattern( (KTRIE_STRUCT *)p->obj, (unsigned char *)P, m, 
00162               noCase, ID );
00163      break; 
00164      default:
00165        return -1;
00166      break; 
00167    }
00168 }
00169 
00170 void mpseLargeShifts   ( void * pv, int flag )
00171 {
00172   MPSE * p = (MPSE*)pv;
00173  
00174   switch( p->method )
00175    {
00176      case MPSE_AUTO:
00177      case MPSE_MWM:
00178        mwmLargeShifts( p->obj, flag );
00179      break; 
00180      
00181      default:
00182        return ;
00183      break; 
00184    }
00185 }
00186 
00187 int  mpsePrepPatterns  ( void * pv )
00188 {
00189   MPSE * p = (MPSE*)pv;
00190 
00191   switch( p->method )
00192    {
00193      case MPSE_AC:
00194        return acsmCompile( (ACSM_STRUCT*) p->obj);
00195      break;
00196      case MPSE_ACF:
00197      case MPSE_ACS:
00198      case MPSE_ACB:
00199      case MPSE_ACSB:
00200        return acsmCompile2( (ACSM_STRUCT2*) p->obj);
00201      break;
00202      case MPSE_AUTO:
00203      case MPSE_MWM:
00204        return mwmPrepPatterns( p->obj );
00205      break;
00206      case MPSE_KTBM:
00207      case MPSE_LOWMEM:
00208        return KTrieCompile( (KTRIE_STRUCT *)p->obj);
00209      break; 
00210      
00211      default:
00212        return 1;
00213      break; 
00214    }
00215 }
00216 
00217 void mpseSetRuleMask ( void *pv, BITOP * rm )
00218 {
00219   MPSE * p = (MPSE*)pv;
00220 
00221   switch( p->method )
00222    {
00223      case MPSE_AUTO:
00224      case MPSE_MWM:
00225        mwmSetRuleMask( p->obj, rm );
00226      break;
00227      
00228      default:
00229        return ;
00230      break; 
00231    }
00232 
00233 
00234 }
00235 int mpsePrintDetail( void *pv )
00236 {
00237   MPSE * p = (MPSE*)pv;
00238 
00239   switch( p->method )
00240    {
00241      case MPSE_AC:
00242       return acsmPrintDetailInfo( (ACSM_STRUCT*) p->obj );
00243      break;
00244      case MPSE_ACF:
00245      case MPSE_ACS:
00246      case MPSE_ACB:
00247      case MPSE_ACSB:
00248       return acsmPrintDetailInfo2( (ACSM_STRUCT2*) p->obj );
00249       break;
00250      case MPSE_AUTO:
00251      case MPSE_MWM:
00252       return 0;
00253      break;
00254      case MPSE_LOWMEM:
00255        return 0;;
00256      break; 
00257      
00258      default:
00259        return 1;
00260      break; 
00261    }
00262 
00263  return 0;
00264 }       
00265 
00266 
00267 int mpsePrintSummary( )
00268 {
00269    acsmPrintSummaryInfo();
00270    acsmPrintSummaryInfo2();
00271    return 0;
00272 }       
00273 
00274 int mpseSearch( void *pv, unsigned char * T, int n, 
00275     int ( *action )(void*id, int index, void *data), 
00276     void * data ) 
00277 {
00278   MPSE * p = (MPSE*)pv;
00279 
00280   s_bcnt += n;
00281   
00282   switch( p->method )
00283    {
00284      case MPSE_AC:
00285       return acsmSearch( (ACSM_STRUCT*) p->obj, T, n, action, data );
00286      break;
00287      case MPSE_ACF:
00288      case MPSE_ACS:
00289      case MPSE_ACB:
00290      case MPSE_ACSB:
00291       return acsmSearch2( (ACSM_STRUCT2*) p->obj, T, n, action, data );
00292       break;
00293      case MPSE_AUTO:
00294      case MPSE_MWM:
00295       return mwmSearch( p->obj, T, n, action, data );
00296      break;
00297      case MPSE_LOWMEM:
00298        return  KTrieSearch( (KTRIE_STRUCT *)p->obj, T, n, action, data );
00299      break; 
00300      default:
00301        return 1;
00302      break; 
00303    }
00304 
00305 }
00306 
00307 
00308 UINT64 mpseGetPatByteCount( )
00309 {
00310   return s_bcnt; 
00311 }
00312 
00313 void mpseResetByteCount( )
00314 {
00315     s_bcnt = 0;
00316 }
00317 
00318  

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