diff options
author | Helmut Grohne <helmut@subdivi.de> | 2013-03-24 14:47:04 +0100 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2013-03-24 14:47:04 +0100 |
commit | 79ee1f7327669181e37d675fc020cf7ca58744cc (patch) | |
tree | ca19c5e072771ec2e767ae16db313a74a23dac1c | |
parent | b9dcb173727fed0c821f4def96cef3b66e1db96e (diff) | |
download | ssdeep-79ee1f7327669181e37d675fc020cf7ca58744cc.tar.gz |
place ssdeep_context on the heap
When publishing an API based on ssdeep_context this enables us to change
its size without breaking API or ABI. Also splint appears to cope better
with that.
-rw-r--r-- | fuzzy.c | 32 |
1 files changed, 19 insertions, 13 deletions
@@ -122,10 +122,15 @@ struct ssdeep_context { struct roll_state roll; }; -static int ssdeep_init(/*@out@*/ struct ssdeep_context *self) { +static /*@only@*/ /*@null@*/ struct ssdeep_context *ssdeep_new(void) { + struct ssdeep_context *self; + if(NULL == (self = malloc(sizeof(struct ssdeep_context)))) + return NULL; self->blockhashes = malloc(sizeof(struct blockhash_context)); - if(NULL == self->blockhashes) - return -1; + if(NULL == self->blockhashes) { + free(self); + return NULL; + } self->start_blocksize = MIN_BLOCKSIZE; self->blockhashes->h = HASH_INIT; self->blockhashes->halfh = HASH_INIT; @@ -133,7 +138,7 @@ static int ssdeep_init(/*@out@*/ struct ssdeep_context *self) { self->blockhashes->next = NULL; self->total_size = 0; roll_init(&self->roll); - return 0; + return self; } static void ssdeep_try_reduce_blockhash(struct ssdeep_context *self) { @@ -293,7 +298,7 @@ static void ssdeep_digest(const struct ssdeep_context *self, *result = '\0'; } -static void ssdeep_clear(struct ssdeep_context *self) { +static void ssdeep_free(/*@only@*/ struct ssdeep_context *self) { struct blockhash_context *bh, *bhn; bh = self->blockhashes; while(bh) { @@ -301,27 +306,28 @@ static void ssdeep_clear(struct ssdeep_context *self) { free(bh); bh = bhn; } + free(self); } int fuzzy_hash_buf(const unsigned char *buf, uint32_t buf_len, /*@out@*/ char *result) { - struct ssdeep_context ctx[1]; - if(ssdeep_init(ctx) < 0) + struct ssdeep_context *ctx; + if(NULL == (ctx = ssdeep_new())) return -1; if(ssdeep_engine(ctx, buf, buf_len) < 0) { - ssdeep_clear(ctx); + ssdeep_free(ctx); return -1; } ssdeep_digest(ctx, result); - ssdeep_clear(ctx); + ssdeep_free(ctx); return 0; } int fuzzy_hash_stream(FILE *handle, /*@out@*/ char *result) { - struct ssdeep_context ctx[1]; + struct ssdeep_context *ctx; unsigned char buffer[4096]; size_t n; - if(ssdeep_init(ctx) < 0) + if(NULL == (ctx = ssdeep_new())) return -1; for(;;) { n = fread(buffer, 1, 4096, handle); @@ -333,10 +339,10 @@ int fuzzy_hash_stream(FILE *handle, /*@out@*/ char *result) { if(ferror(handle) != 0) goto errout; ssdeep_digest(ctx, result); - ssdeep_clear(ctx); + ssdeep_free(ctx); return 0; errout: - ssdeep_clear(ctx); + ssdeep_free(ctx); return -1; } |