From 8cedc519bf235a52e2c85cd06bb0ec7d392bf0ca Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 7 Jul 2022 14:13:27 +0200 Subject: Don't marshal w MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- marshal.c | 12 +----------- unmarshal.c | 9 ++++++--- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/marshal.c b/marshal.c index adca8dc..9c01211 100644 --- a/marshal.c +++ b/marshal.c @@ -2,13 +2,6 @@ #include "common.h" -/** - * Marshal a state into a buffer - * - * @param state The state to marshal - * @param buf Output buffer, `NULL` to only return the required size - * @return The number of bytes marshalled to `buf` - */ size_t libsha1_marshal(const struct libsha1_state *restrict state, void *restrict buf_) { @@ -16,7 +9,7 @@ libsha1_marshal(const struct libsha1_state *restrict state, void *restrict buf_) size_t off = 0; if (buf) - *(int *)buf = 0; /* version */ + *(int *)buf = 1; /* version */ off += sizeof(int); if (buf) *(enum libsha1_algorithm *)&buf[off] = state->algorithm; @@ -25,9 +18,6 @@ libsha1_marshal(const struct libsha1_state *restrict state, void *restrict buf_) *(size_t *)&buf[off] = state->message_size; off += sizeof(size_t); - if (buf) - memcpy(&buf[off], state->w, sizeof(state->w)); - off += sizeof(state->w); if (buf) memcpy(&buf[off], state->h, sizeof(state->h)); off += sizeof(state->h); diff --git a/unmarshal.c b/unmarshal.c index 25771b9..c6668ac 100644 --- a/unmarshal.c +++ b/unmarshal.c @@ -7,13 +7,15 @@ libsha1_unmarshal(struct libsha1_state *restrict state, const void *restrict buf { const char *restrict buf = buf_; size_t off = 0; + int version; if (bufsize < sizeof(int) + sizeof(enum libsha1_algorithm) + sizeof(size_t)) { errno = EINVAL; return 0; } - if (*(const int *)buf) { /* version */ + version = *(const int *)buf; + if (version < 0 || version > 1) { /* version */ errno = EINVAL; return 0; } @@ -28,8 +30,9 @@ libsha1_unmarshal(struct libsha1_state *restrict state, const void *restrict buf errno = EINVAL; return 0; } - memcpy(state->w, &buf[off], sizeof(state->w)); - off += sizeof(state->w); + memset(state->w, 0, sizeof(state->w)); + if (version == 0) + off += sizeof(state->w); memcpy(state->h, &buf[off], sizeof(state->h)); off += sizeof(state->h); -- cgit v1.2.3-70-g09d2