diff options
author | Mattias Andrée <maandree@kth.se> | 2021-12-24 11:24:54 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-12-24 11:24:54 +0100 |
commit | 822a202761eb34a9588835e18f16b28e5ae03e8a (patch) | |
tree | b375de7b63eabe970d5bc557236ad32385997862 /libkeccak_state_unmarshal.c | |
parent | Suppress clang warnings (diff) | |
download | libkeccak-822a202761eb34a9588835e18f16b28e5ae03e8a.tar.gz libkeccak-822a202761eb34a9588835e18f16b28e5ae03e8a.tar.bz2 libkeccak-822a202761eb34a9588835e18f16b28e5ae03e8a.tar.xz |
Fix marshal and unmarshal
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libkeccak_state_unmarshal.c')
-rw-r--r-- | libkeccak_state_unmarshal.c | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/libkeccak_state_unmarshal.c b/libkeccak_state_unmarshal.c index 4e0df28..f221fad 100644 --- a/libkeccak_state_unmarshal.c +++ b/libkeccak_state_unmarshal.c @@ -18,11 +18,18 @@ size_t libkeccak_state_unmarshal(struct libkeccak_state *restrict state, const void *restrict data_) { -#define get(type, var) state->var = *((const type *)data), data += sizeof(type) - const unsigned char *restrict data = data_; +#define get(type, var) state->var = *(const type *)data, data += sizeof(type) + const unsigned char *restrict start = data_; + const unsigned char *restrict data = start; + size_t mptr; if (!state) { - data += (7 * sizeof(long int) + 26 * sizeof(int64_t)); - return sizeof(struct libkeccak_state) - sizeof(char *) + *(const size_t *)data * sizeof(char); + data += 7 * sizeof(long int); + data += 1 * sizeof(int64_t); + data += sizeof(state->S); + mptr = *(const size_t *)data; + data += 2 * sizeof(size_t); + data += mptr; + return (size_t)(data - start); } get(long int, r); get(long int, c); @@ -36,11 +43,15 @@ libkeccak_state_unmarshal(struct libkeccak_state *restrict state, const void *re data += sizeof(state->S); get(size_t, mptr); get(size_t, mlen); - state->M = malloc(state->mptr * sizeof(char)); - if (!state->M) - return 0; - memcpy(state->M, data, state->mptr * sizeof(char)); - data += state->mptr; - return sizeof(struct libkeccak_state) - sizeof(char *) + state->mptr * sizeof(char); + if (state->mptr) { + state->M = malloc(state->mptr * sizeof(char)); + if (!state->M) + return 0; + memcpy(state->M, data, state->mptr * sizeof(char)); + data += state->mptr; + } else { + state->M = NULL; + } + return (size_t)(data - start); #undef get } |