aboutsummaryrefslogtreecommitdiff
path: root/src/cred_dict.c
blob: 10a76a05f760b8ce397af63cf602ed9e222333bc (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "cred_dict.h"
#include "util_rt.h"
#include "endian.h"
#include "strmap.h"

#include <string.h>
#include <errno.h>


void prne_init_cred_dict (prne_cred_dict_t *p) {
	prne_memzero(p, sizeof(prne_cred_dict_t));
}

void prne_free_cred_dict (prne_cred_dict_t *p) {
	if (p == NULL) {
		return;
	}

	prne_free(p->arr);
	prne_memzero(p, sizeof(prne_cred_dict_t));
}

bool prne_build_cred_dict (
	const prne_cred_dict_raw_entry_t *arr,
	const size_t cnt,
	uint8_t **out_m,
	size_t *out_l)
{
	bool ret = false;
	prne_strmap_t map;
	uint8_t *m = NULL, *p;
	size_t l = 0, strsize, sum_str;
	uint16_t idx_id, idx_pw;

	if (cnt > UINT16_MAX) {
		errno = EOVERFLOW;
		return false;
	}

	prne_init_strmap(&map);

// TRY
	for (size_t i = 0; i < cnt; i += 1) {
		if (arr[i].id == NULL || arr[i].pw == NULL) {
			errno = EINVAL;
			goto END;
		}
		prne_strmap_insert(&map, arr[i].id, 0);
		prne_strmap_insert(&map, arr[i].pw, 0);
	}

	sum_str = 0;
	for (size_t i = 0; i < map.size; i += 1) {
		map.tbl[i].val = (prne_strmap_val_t)sum_str;
		sum_str += strlen(map.tbl[i].key) + 1;
	}
	l = 2/*head*/ + 5 * cnt/*entries*/ + sum_str;
	if (sum_str > UINT16_MAX || l > UINT16_MAX) {
		errno = EOVERFLOW;
		goto END;
	}

	p = m = (uint8_t*)prne_malloc(1, l);
	if (m == NULL) {
		goto END;
	}

	p[0] = prne_getmsb16(cnt, 0);
	p[1] = prne_getmsb16(cnt, 1);
	p += 2;
	for (size_t i = 0; i < cnt; i += 1) {
		idx_id = (uint16_t)(prne_strmap_lookup(&map, arr[i].id)->val);
		idx_pw = (uint16_t)(prne_strmap_lookup(&map, arr[i].pw)->val);
		p[0] = prne_getmsb16(idx_id, 0);
		p[1] = prne_getmsb16(idx_id, 1);
		p[2] = prne_getmsb16(idx_pw, 0);
		p[3] = prne_getmsb16(idx_pw, 1);
		p[4] = arr[i].weight;
		p += 5;
	}
	for (size_t i = 0; i < map.size; i += 1) {
		strsize = strlen(map.tbl[i].key) + 1;
		memcpy(p, map.tbl[i].key, strsize);
		p += strsize;
	}

	*out_m = m;
	*out_l = l;
	m = NULL;
	ret = true;
END: // CATCH
	prne_free(m);
	prne_free_strmap(&map);
	return ret;
}

bool prne_dser_cred_dict (
	prne_cred_dict_t *dict,
	const uint8_t *buf,
	const size_t len)
{
	prne_cred_dict_entry_t *arr = NULL;
	size_t cnt, head_size, m_size;
	const uint8_t *p = buf;
	bool ret = false;

	if (len < 2) {
		errno = EINVAL;
		return false;
	}
	cnt = prne_recmb_msb16(p[0], p[1]);
	head_size = 2 + 5 * cnt;
	if (head_size > len) {
		errno = EINVAL;
		return false;
	}
	if (cnt > 0 && buf[len - 1] != 0) {
		errno = EINVAL;
		return false;
	}
	p += 2;
	m_size = len - head_size;

// TRY
	arr = prne_malloc(sizeof(prne_cred_dict_entry_t), cnt);
	if (cnt > 0 && arr == NULL) {
		goto END;
	}

	for (size_t i = 0; i < cnt; i += 1) {
		arr[i].id = prne_recmb_msb16(p[0], p[1]);
		arr[i].pw = prne_recmb_msb16(p[2], p[3]);
		arr[i].weight = p[4];
		p += 5;

		if (arr[i].id >= m_size || arr[i].pw >= m_size) {
			errno = EINVAL;
			goto END;
		}
	}

	prne_free_cred_dict(dict);
	dict->arr = arr;
	dict->cnt = cnt;
	dict->m = (const char *)(buf + head_size);
	arr = NULL;
	ret = true;
END:
	prne_free(arr);
	return ret;
}