Skip to content

Commit d24f12f

Browse files
committed
cf_beecrypt: use single allocation for digest data, joining variable and constant data in one struct
1 parent 7782c02 commit d24f12f

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

cf_beecrypt/src/beecrypt_digest.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
struct _beecrypt_digest {
1212
struct cf_digest digest;
1313
hashFunction *hash;
14-
hashFunctionParam *param;
14+
char param[1];
1515
};
1616

17+
/* Minus 1 to account for space taken by param */
18+
#define BEECRYPT_DIGEST_STRUCT_SIZE(paramSize) (sizeof(struct _beecrypt_digest) - 1 + (paramSize))
19+
1720
static cf_digest_t create_beecrypt_container(hashFunction *hash);
1821

1922
static cf_rv_t _digest_update(cf_digest_t digest, void *data, size_t data_len) {
2023
struct _beecrypt_digest *impl = (struct _beecrypt_digest*)digest;
2124
/* TODO: check return value */
22-
impl->hash->update(impl->param, data, data_len);
25+
impl->hash->update((void*)impl->param, data, data_len);
2326
return CF_S_OK;
2427
}
2528

@@ -31,8 +34,6 @@ static cf_rv_t _digest_finish(cf_digest_t digest, void *output, size_t *output_l
3134
return CF_S_OK;
3235
}
3336
if(!output && !output_len) {
34-
free(impl->param);
35-
impl->param = NULL;
3637
free(impl);
3738
return CF_S_OK;
3839
}
@@ -42,9 +43,7 @@ static cf_rv_t _digest_finish(cf_digest_t digest, void *output, size_t *output_l
4243
}
4344
*output_len = real_len;
4445
/* TODO: check return value */
45-
impl->hash->digest(impl->param, output);
46-
free(impl->param);
47-
impl->param = NULL;
46+
impl->hash->digest((void*)impl->param, output);
4847
free(impl);
4948
return CF_S_OK;
5049
}
@@ -68,18 +67,13 @@ static struct cf_digest_instance_ops beecrypt_digest_instance_ops = {
6867
};
6968

7069
static cf_digest_t create_beecrypt_container(hashFunction *hash) {
71-
struct _beecrypt_digest *impl = calloc(1, sizeof(*impl));
70+
/* Allocate size for structure + additional param data at end - extra padding in */
71+
struct _beecrypt_digest *impl = calloc(1, BEECRYPT_DIGEST_STRUCT_SIZE(hash->paramsize));
7272
if(!impl) {
7373
return NULL;
7474
}
7575
impl->digest.ops = &beecrypt_digest_instance_ops;
7676
impl->hash = hash;
77-
impl->param = (hashFunctionParam*)malloc(hash->paramsize);
78-
if (!impl->param) {
79-
free(impl);
80-
impl = NULL;
81-
return NULL;
82-
}
8377
/* TODO: check return value */
8478
hash->reset(impl->param);
8579
return (cf_digest_t)impl;

0 commit comments

Comments
 (0)