aboutsummaryrefslogtreecommitdiffstats
path: root/marshal.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 /marshal.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 'marshal.c')
-rw-r--r--marshal.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/marshal.c b/marshal.c
new file mode 100644
index 0000000..c2d1c11
--- /dev/null
+++ b/marshal.c
@@ -0,0 +1,43 @@
+/* See LICENSE file for copyright and license details. */
+#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_)
+{
+ char *restrict buf = buf_;
+ size_t off = 0;
+
+ if (buf)
+ *(int *)buf = 0; /* version */
+ off += sizeof(int);
+ if (buf)
+ *(enum libsha1_algorithm *)&buf[off] = state->algorithm;
+ off += sizeof(enum libsha1_algorithm);
+ if (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);
+
+ if (buf)
+ *(size_t *)&buf[off] = state->chunk_size;
+ off += sizeof(size_t);
+ if (buf)
+ memcpy(&buf[off], state->chunk, (state->message_size / 8) % state->chunk_size);
+ off += (state->message_size / 8) % state->chunk_size;
+
+ return off;
+}