summaryrefslogtreecommitdiffstats
path: root/src/lib-mail/message-binary-part.c
blob: 20416ff76ce0c87fe4167eb0935d6de572a65912 (plain)
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
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "numpack.h"
#include "message-binary-part.h"

void message_binary_part_serialize(const struct message_binary_part *parts,
				   buffer_t *dest)
{
	const struct message_binary_part *part;

	for (part = parts; part != NULL; part = part->next) {
		numpack_encode(dest, part->physical_pos);
		numpack_encode(dest, part->binary_hdr_size);
		numpack_encode(dest, part->binary_body_size);
		numpack_encode(dest, part->binary_body_lines_count);
	}
}

int message_binary_part_deserialize(pool_t pool, const void *data, size_t size,
				    struct message_binary_part **parts_r)
{
	const uint8_t *p = data, *end = p + size;
	uint64_t n1, n2, n3, n4;
	struct message_binary_part *part = NULL, *prev_part = NULL;

	while (p != end) {
		part = p_new(pool, struct message_binary_part, 1);
		part->next = prev_part;
		prev_part = part;
		if (numpack_decode(&p, end, &n1) < 0 ||
		    numpack_decode(&p, end, &n2) < 0 ||
		    numpack_decode(&p, end, &n3) < 0 ||
		    numpack_decode(&p, end, &n4) < 0 ||
		    n4 > UINT_MAX)
			return -1;
		part->physical_pos = n1;
		part->binary_hdr_size = n2;
		part->binary_body_size = n3;
		part->binary_body_lines_count = n4;
	}
	*parts_r = part;
	return 0;
}