commit d0ab737bdcd447aa823452fe304135be90b731eb
parent 9ecc827d12d0f917ffbc603b82ca9ac77b29c2a8
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 7 Sep 2022 08:38:54 +0200
Update the hash computation
Use the sha256_ctx API
Diffstat:
| M | src/sars.c | | | 75 | ++++++++++++++++++++++++++++++++------------------------------------------- |
1 file changed, 32 insertions(+), 43 deletions(-)
diff --git a/src/sars.c b/src/sars.c
@@ -22,7 +22,6 @@
#include "sars_log.h"
#include <rsys/algorithm.h>
-#include <rsys/dynamic_array_char.h>
#include <rsys/hash.h>
#include <unistd.h> /* sysconf support */
@@ -215,6 +214,18 @@ cmp_band(const void* key, const void* item)
}
}
+static INLINE void
+hash_band
+ (struct sha256_ctx* ctx,
+ const struct sars_band* band,
+ const size_t nnodes)
+{
+ sha256_ctx_update(ctx, (const char*)&band->lower, sizeof(band->lower));
+ sha256_ctx_update(ctx, (const char*)&band->upper, sizeof(band->upper));
+ sha256_ctx_update(ctx, (const char*)&band->id, sizeof(band->id));
+ sha256_ctx_update(ctx, (const char*)band->k_list, sizeof(*band->k_list)*nnodes);
+}
+
static void
release_sars(ref_T* ref)
{
@@ -442,34 +453,23 @@ sars_band_compute_hash
const size_t iband,
hash256_T hash)
{
- struct darray_char bytes;
+ struct sha256_ctx ctx;
struct sars_band band;
res_T res = RES_OK;
- if(!sars || !hash) return RES_BAD_ARG;
-
- darray_char_init(sars->allocator, &bytes);
+ if(!sars || !hash) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
res = sars_get_band(sars, iband, &band);
if(res != RES_OK) goto error;
- #define WRITE(Var, Nb) { \
- size_t ibyte; \
- FOR_EACH(ibyte, 0, sizeof(*Var)*(Nb)) { \
- res = darray_char_push_back(&bytes, ((const char*)Var)+ibyte); \
- if(res != RES_OK) goto error; \
- } \
- } (void)0
- WRITE(&band.lower, 1);
- WRITE(&band.upper, 1);
- WRITE(&band.id, 1);
- WRITE(band.k_list, sars->nnodes);
- #undef WRITE
-
- hash_sha256(darray_char_cdata_get(&bytes), darray_char_size_get(&bytes), hash);
+ sha256_ctx_init(&ctx);
+ hash_band(&ctx, &band, sars->nnodes);
+ sha256_ctx_finalize(&ctx, hash);
exit:
- darray_char_release(&bytes);
return res;
error:
goto exit;
@@ -478,37 +478,26 @@ error:
res_T
sars_compute_hash(const struct sars* sars, hash256_T hash)
{
- struct darray_char bytes;
+ struct sha256_ctx ctx;
size_t i;
res_T res = RES_OK;
- if(!sars || !hash) return RES_BAD_ARG;
-
- darray_char_init(sars->allocator, &bytes);
-
- /* Write data to hash */
- #define WRITE(Var, Nb) { \
- size_t ibyte; \
- FOR_EACH(ibyte, 0, sizeof(*Var)*(Nb)) { \
- res = darray_char_push_back(&bytes, ((const char*)Var)+ibyte); \
- if(res != RES_OK) goto error; \
- } \
- } (void)0
+ if(!sars || !hash) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
- WRITE(&sars->pagesize, 1);
- WRITE(&sars->nnodes, 1);
+ sha256_ctx_init(&ctx);
+ sha256_ctx_update(&ctx, (const char*)&sars->pagesize, sizeof(sars->pagesize));
+ sha256_ctx_update(&ctx, (const char*)&sars->nnodes, sizeof(sars->nnodes));
FOR_EACH(i, 0, darray_band_size_get(&sars->bands)) {
- hash256_T hash_band;
- res = sars_band_compute_hash(sars, i, hash_band);
- if(res != RES_OK) goto error;
- WRITE(hash_band, sizeof(hash256_T));
+ struct sars_band band;
+ SARS(get_band(sars, i, &band));
+ hash_band(&ctx, &band, sars->nnodes);
}
- #undef WRITE
-
- hash_sha256(darray_char_cdata_get(&bytes), darray_char_size_get(&bytes), hash);
+ sha256_ctx_finalize(&ctx, hash);
exit:
- darray_char_release(&bytes);
return res;
error:
goto exit;