BloomFilter

2017-09-18  本文已影响0人  希夷微
char *httphead[] = {
   "Uri=",
   "Host=",
   "Referer=",
   "User-Agent=",
};
#define MAX_BIT_COUNT 1000
#define MAX_HTTPHEAD_NUM 32
#define MAX_HASHFUNC_NUM 8

unsigned char bits[MAX_BIT_COUNT] = {0};
typedef unsigned int (* hashfunc)(char *);

hashfunc func[] = {
   SDBMHash,
   RSHash,
   JSHash,
   PJWHash,
   ELFHash,
   BKDRHash,
   DJBHash,
   APHash
};

void add_to_bloomfilter(char *str)
{
   int hashvalue = 0, i = 0;
   for(i = 0 ; i < MAX_HASHFUNC_NUM; i++)
   {
       hashvalue = func[i](str) %  (MAX_BIT_COUNT * 8);   
       bits[hashvalue/8] |= (0x01 << (hashvalue % 8));\
   }
}

unsigned char judge_bloomfilter(char *str)
{
   int hashvalue = 0, i = 0;
   for (i = 0 ; i < MAX_HASHFUNC_NUM; i++)
   {
       hashvalue = func[i](str) %  (MAX_BIT_COUNT * 8);
       if ((bits[hashvalue/8] & (0x01 << (hashvalue % 8))) == 0)
       {
           break;
       }
   }

   if (i == MAX_HASHFUNC_NUM)
   {
       return 1;
   }
   else
   {
       return 0;
   }
}

int main(int argc, char **argv)
{

   int i = 0;

   for (i = 0; i < MAX_HTTPHEAD_NUM; i++)
   {
       add_to_bloomfilter(httphead[i]);
   }

   for (i = 0; i < MAX_HTTPHEAD_NUM; i++)
   {
       if (judge_bloomfilter(httphead[i]) > 0)
       {
           printf("%s hash been find\n", httphead[i]);
       }
   }

   if (judge_bloomfilter("axjhfdsaew") > 0)
   {
       printf("%s hash been find\n", "aaaaaaa");
   }

   return 0;
}

上一篇下一篇

猜你喜欢

热点阅读