aboutsummaryrefslogtreecommitdiffstats
path: root/marshal.c
blob: 21f0f793c229d5b9d86af97e16f13fab569c3c42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* 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
libsha2_marshal(const struct libsha2_state *restrict state, char *restrict buf)
{
	size_t off = 0;

	if (buf)
		*(int *)buf = 0; /* version */
	off += sizeof(int);
	if (buf)
		*(enum libsha2_algorithm *)&buf[off] = state->algorithm;
	off += sizeof(enum libsha2_algorithm);
	if (buf)
		*(size_t *)&buf[off] = state->message_size;
	off += sizeof(size_t);

	switch (state->algorithm) {
	case LIBSHA2_224:
	case LIBSHA2_256:
		if (buf)
			memcpy(&buf[off], state->k.b32, sizeof(state->k.b32));
		off += sizeof(state->k.b32);
		if (buf)
			memcpy(&buf[off], state->w.b32, sizeof(state->w.b32));
		off += sizeof(state->w.b32);
		if (buf)
			memcpy(&buf[off], state->h.b32, sizeof(state->h.b32));
		off += sizeof(state->h.b32);
		break;

	default:
		if (buf)
			memcpy(&buf[off], state->k.b64, sizeof(state->k.b64));
		off += sizeof(state->k.b64);
		if (buf)
			memcpy(&buf[off], state->w.b64, sizeof(state->w.b64));
		off += sizeof(state->w.b64);
		if (buf)
			memcpy(&buf[off], state->h.b64, sizeof(state->h.b64));
		off += sizeof(state->h.b64);
		break;
	}

	if (buf)
		memcpy(&buf[off], state->chunk, sizeof(state->chunk));
	off += sizeof(state->chunk);
	if (buf)
		*(size_t *)&buf[off] = state->chunk_size;
	off += sizeof(size_t);

	return off;
}