~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to libclamav/cache.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <pthread.h>
28
28
#include <assert.h>
29
29
 
30
 
#include "md5.h"
 
30
#include <openssl/ssl.h>
 
31
#include <openssl/err.h>
 
32
#include "libclamav/crypto.h"
 
33
 
31
34
#include "mpool.h"
32
35
#include "clamav.h"
33
36
#include "cache.h"
748
751
        return 1;
749
752
    }
750
753
 
 
754
    if (engine->engine_options & ENGINE_OPTIONS_DISABLE_CACHE) {
 
755
        cli_dbgmsg("cli_cache_init: Caching disabled.\n");
 
756
        return 0;
 
757
    }
 
758
 
751
759
    if(!(cache = mpool_malloc(engine->mempool, sizeof(struct CACHE) * TREES))) {
752
760
        cli_errmsg("cli_cache_init: mpool malloc fail\n");
753
761
        return 1;
780
788
    if(!engine || !(cache = engine->cache))
781
789
        return;
782
790
 
 
791
    if (engine->engine_options & ENGINE_OPTIONS_DISABLE_CACHE) {
 
792
        return;
 
793
    }
 
794
 
783
795
    for(i=0; i<TREES; i++) {
784
796
        cacheset_destroy(&cache[i].cacheset, engine->mempool);
785
797
        pthread_mutex_destroy(&cache[i].mutex);
816
828
    if(!ctx || !ctx->engine || !ctx->engine->cache)
817
829
       return;
818
830
 
 
831
    if (ctx->engine->engine_options & ENGINE_OPTIONS_DISABLE_CACHE) {
 
832
        cli_dbgmsg("cache_add: Caching disabled. Not adding sample to cache.\n");
 
833
        return;
 
834
    }
 
835
 
819
836
    level =  (*ctx->fmap && (*ctx->fmap)->dont_cache_flag) ? ctx->recursion : 0;
820
837
    if (ctx->found_possibly_unwanted && (level || !ctx->recursion))
821
838
        return;
854
871
    if(!engine || !engine->cache)
855
872
       return;
856
873
 
 
874
    if (engine->engine_options & ENGINE_OPTIONS_DISABLE_CACHE) {
 
875
        cli_dbgmsg("cache_remove: Caching disabled.\n");
 
876
        return;
 
877
    }
 
878
 
857
879
    /* cli_warnmsg("cache_remove: key is %u\n", key); */
858
880
 
859
881
    c = &engine->cache[key];
883
905
int cache_check(unsigned char *hash, cli_ctx *ctx) {
884
906
    fmap_t *map;
885
907
    size_t todo, at = 0;
886
 
    cli_md5_ctx md5;
 
908
    void *hashctx;
887
909
    int ret;
888
910
 
889
911
    if(!ctx || !ctx->engine || !ctx->engine->cache)
890
912
       return CL_VIRUS;
891
913
 
 
914
    if (ctx->engine->engine_options & ENGINE_OPTIONS_DISABLE_CACHE) {
 
915
        cli_dbgmsg("cache_check: Caching disabled. Returning CL_VIRUS.\n");
 
916
        return CL_VIRUS;
 
917
    }
 
918
 
892
919
    map = *ctx->fmap;
893
920
    todo = map->len;
894
921
 
895
 
    cli_md5_init(&md5);
 
922
    hashctx = cl_hash_init("md5");
 
923
    if (!(hashctx))
 
924
        return CL_VIRUS;
 
925
 
 
926
 
896
927
    while(todo) {
897
 
        const void *buf;
898
 
        size_t readme = todo < FILEBUFF ? todo : FILEBUFF;
899
 
        if(!(buf = fmap_need_off_once(map, at, readme)))
900
 
            return CL_EREAD;
901
 
        todo -= readme;
902
 
        at += readme;
903
 
        if (cli_md5_update(&md5, buf, readme)) {
904
 
            cli_errmsg("cache_check: error reading while generating hash!\n");
905
 
            return CL_EREAD;
906
 
        }
 
928
        const void *buf;
 
929
        size_t readme = todo < FILEBUFF ? todo : FILEBUFF;
 
930
 
 
931
        if(!(buf = fmap_need_off_once(map, at, readme))) {
 
932
            cl_hash_destroy(hashctx);
 
933
            return CL_EREAD;
 
934
        }
 
935
 
 
936
        todo -= readme;
 
937
        at += readme;
 
938
 
 
939
        if (cl_update_hash(hashctx, buf, readme)) {
 
940
            cl_hash_destroy(hashctx);
 
941
            cli_errmsg("cache_check: error reading while generating hash!\n");
 
942
            return CL_EREAD;
 
943
        }
907
944
    }
908
 
    cli_md5_final(hash, &md5);
 
945
 
 
946
    cl_finish_hash(hashctx, hash);
 
947
 
909
948
    ret = cache_lookup_hash(hash, map->len, ctx->engine->cache, ctx->recursion);
910
949
    cli_dbgmsg("cache_check: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x is %s\n", hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], hash[8], hash[9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15], (ret == CL_VIRUS) ? "negative" : "positive");
911
950
    return ret;