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: 43 53 81.1 %
Date: 2021-05-02 03:18:47 Functions: 7 7 100.0 %

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

Generated by: LCOV version 1.13