aboutsummaryrefslogtreecommitdiffstats
path: root/unmarshal.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-10 20:21:19 +0100
committerMattias Andrée <maandree@kth.se>2019-02-10 20:21:19 +0100
commited0296b9055713df0d910e4e7528ffe6fc539514 (patch)
tree8cbf8ecc9b6352257d6bc4946ff75cb8a4b484c0 /unmarshal.c
downloadlibsha1-ed0296b9055713df0d910e4e7528ffe6fc539514.tar.gz
libsha1-ed0296b9055713df0d910e4e7528ffe6fc539514.tar.bz2
libsha1-ed0296b9055713df0d910e4e7528ffe6fc539514.tar.xz
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'unmarshal.c')
-rw-r--r--unmarshal.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/unmarshal.c b/unmarshal.c
new file mode 100644
index 0000000..e89a8dd
--- /dev/null
+++ b/unmarshal.c
@@ -0,0 +1,59 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+/**
+ * Unmarshal a state from a buffer
+ *
+ * @param state Output parameter for the unmarshalled state
+ * @param buf The buffer from which the state shall be unmarshalled
+ * @param bufsize The maximum number of bytes that can be unmarshalled
+ * @return The number of read bytes, 0 on failure
+ */
+size_t
+libsha1_unmarshal(struct libsha1_state *restrict state, const void *restrict buf_, size_t bufsize)
+{
+ const char *restrict buf = buf_;
+ size_t off = 0;
+
+ if (bufsize < sizeof(int) + sizeof(enum libsha1_algorithm) + sizeof(size_t)) {
+ errno = EINVAL;
+ return 0;
+ }
+
+ if (*(const int *)buf) { /* version */
+ errno = EINVAL;
+ return 0;
+ }
+ off += sizeof(int);
+
+ state->algorithm = *(const enum libsha1_algorithm *)&buf[off];
+ off += sizeof(enum libsha1_algorithm);
+ state->message_size = *(const size_t *)&buf[off];
+ off += sizeof(size_t);
+
+ if (bufsize - off < sizeof(state->w) + sizeof(state->h)) {
+ errno = EINVAL;
+ return 0;
+ }
+ memcpy(state->w, &buf[off], sizeof(state->w));
+ off += sizeof(state->w);
+ memcpy(state->h, &buf[off], sizeof(state->h));
+ off += sizeof(state->h);
+
+ if (bufsize - off < sizeof(size_t)) {
+ errno = EINVAL;
+ return 0;
+ }
+ state->chunk_size = *(const size_t *)&buf[off];
+ off += sizeof(size_t);
+
+ if (bufsize - off < (state->message_size / 8) % state->chunk_size) {
+ errno = EINVAL;
+ return 0;
+ }
+ memcpy(state->chunk, &buf[off], (state->message_size / 8) % state->chunk_size);
+ off += (state->message_size / 8) % state->chunk_size;
+
+ return off;
+}