LCOV - code coverage report
Current view: top level - src/server/protocol2/champ_chooser - hash.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 27 52 51.9 %
Date: 2016-05-30 Functions: 4 7 57.1 %

          Line data    Source code
       1             : #include "../../../burp.h"
       2             : #include "../../../alloc.h"
       3             : #include "../../../log.h"
       4             : #include "../../../prepend.h"
       5             : #include "../../../protocol2/blk.h"
       6             : #include "../../../sbuf.h"
       7             : #include "hash.h"
       8             : 
       9             : struct hash_weak *hash_table=NULL;
      10             : 
      11        4100 : struct hash_weak *hash_weak_find(uint64_t weak)
      12             : {
      13             :         struct hash_weak *hash_weak;
      14        4100 :         HASH_FIND_INT(hash_table, &weak, hash_weak);
      15        4100 :         return hash_weak;
      16             : }
      17             : 
      18           0 : struct hash_strong *hash_strong_find(struct hash_weak *hash_weak,
      19             :         uint8_t *md5sum)
      20             : {
      21             :         struct hash_strong *s;
      22           0 :         for(s=hash_weak->strong; s; s=s->next)
      23           0 :                 if(!memcmp(s->md5sum, md5sum, MD5_DIGEST_LENGTH)) return s;
      24             :         return NULL;
      25             : }
      26             : 
      27           3 : struct hash_weak *hash_weak_add(uint64_t weakint)
      28             : {
      29             :         struct hash_weak *newweak;
      30           3 :         if(!(newweak=(struct hash_weak *)
      31             :                 malloc_w(sizeof(struct hash_weak), __func__)))
      32             :                         return NULL;
      33           2 :         newweak->weak=weakint;
      34             : //logp("addweak: %016lX\n", weakint);
      35           2 :         newweak->strong=NULL;
      36           4 :         HASH_ADD_INT(hash_table, weak, newweak);
      37           2 :         return newweak;
      38             : }
      39             : 
      40           0 : static struct hash_strong *hash_strong_add(struct hash_weak *hash_weak,
      41             :         struct blk *blk)
      42             : {
      43             :         struct hash_strong *newstrong;
      44           0 :         if(!(newstrong=(struct hash_strong *)
      45             :                 malloc_w(sizeof(struct hash_strong), __func__)))
      46             :                         return NULL;
      47           0 :         newstrong->savepath=blk->savepath;
      48           0 :         memcpy(newstrong->md5sum, blk->md5sum, MD5_DIGEST_LENGTH);
      49           0 :         newstrong->next=hash_weak->strong;
      50             :         return newstrong;
      51             : }
      52             : 
      53             : static void hash_strongs_free(struct hash_strong *shead)
      54             : {
      55             :         static struct hash_strong *s;
      56           2 :         s=shead;
      57           2 :         while(shead)
      58             :         {
      59           0 :                 s=shead;
      60           0 :                 shead=shead->next;
      61           0 :                 free_v((void **)&s);
      62             :         }
      63             : }
      64             : 
      65           3 : void hash_delete_all(void)
      66             : {
      67             :         struct hash_weak *tmp;
      68             :         struct hash_weak *hash_weak;
      69             : 
      70           5 :         HASH_ITER(hh, hash_table, hash_weak, tmp)
      71             :         {
      72           2 :                 HASH_DEL(hash_table, hash_weak);
      73           2 :                 hash_strongs_free(hash_weak->strong);
      74           2 :                 free_v((void **)&hash_weak);
      75             :         }
      76           3 : }
      77             : 
      78           0 : static int process_sig(struct blk *blk)
      79             : {
      80             :         static struct hash_weak *hash_weak;
      81             : 
      82           0 :         hash_weak=hash_weak_find(blk->fingerprint);
      83             : 
      84             :         // Add to hash table.
      85           0 :         if(!hash_weak && !(hash_weak=hash_weak_add(blk->fingerprint)))
      86             :                 return -1;
      87           0 :         if(!hash_strong_find(hash_weak, blk->md5sum))
      88             :         {
      89           0 :                 if(!(hash_weak->strong=hash_strong_add(hash_weak, blk)))
      90             :                         return -1;
      91             :         }
      92             : 
      93             :         return 0;
      94             : }
      95             : 
      96           1 : enum hash_ret hash_load(const char *champ, const char *directory)
      97             : {
      98           1 :         enum hash_ret ret=HASH_RET_PERM;
      99           1 :         char *path=NULL;
     100           1 :         struct fzp *fzp=NULL;
     101           1 :         struct sbuf *sb=NULL;
     102             :         static struct blk *blk=NULL;
     103             : 
     104           1 :         if(!(path=prepend_s(directory, champ)))
     105             :                 goto end;
     106           1 :         if(!(fzp=fzp_gzopen(path, "rb")))
     107             :         {
     108             :                 ret=HASH_RET_TEMP;
     109             :                 goto end;
     110             :         }
     111             : 
     112           0 :         if((!sb && !(sb=sbuf_alloc(PROTO_2)))
     113           0 :           || (!blk && !(blk=blk_alloc())))
     114             :                 goto end;
     115             : 
     116             :         while(1)
     117             :         {
     118           0 :                 sbuf_free_content(sb);
     119           0 :                 switch(sbuf_fill_from_file(sb, fzp, blk, NULL))
     120             :                 {
     121             :                         case 1: ret=HASH_RET_OK;
     122             :                                 goto end;
     123             :                         case -1:
     124             :                                 logp("Error reading %s in %s\n", path,
     125           0 :                                         __func__);
     126           0 :                                 goto end;
     127             :                 }
     128           0 :                 if(!blk->got_save_path) continue;
     129           0 :                 if(process_sig(blk)) goto end;
     130           0 :                 blk->got_save_path=0;
     131             :         }
     132             : end:
     133           1 :         free_w(&path);
     134           1 :         fzp_close(&fzp);
     135           1 :         return ret;
     136             : }

Generated by: LCOV version 1.10