aboutsummaryrefslogtreecommitdiffstats
path: root/librecrypt_concat_uint_.c
blob: 2f74272f3dc50973e07a24dba9d34232b6f886e0 (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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
#include <stdio.h>

/* TODO use of sprintf(3) should be eliminated;
 *      this will make it easier to use in an unhosted environment (kernels)
 *      and make_settings will be AS-Safe,
 *      however librecrypt_add_algorithm will not be AS-Safe as hash
 *         functions may require memory allocations which is AS-Unsafe
 */


void
librecrypt_concat_uint_(struct concat_state *state, uintmax_t value)
{
	char buf[3u * sizeof(uintmax_t) + 1u];
	int len = sprintf(buf, "%ju", value);
        if (len < 1)
		abort(); /* $covered$ (impossible reliably) */
	librecrypt_concat_mem_(state, buf, (size_t)len);
}


#else


int
main(void)
{
	char buf[sizeof("0") + 3u * sizeof(uintmax_t)];
	char expected[sizeof(buf)];
	struct concat_state state = {buf, sizeof(buf), 0u};

	SET_UP_ALARM();
	INIT_RESOURCE_TEST();

	memset(buf, 'x', sizeof(buf));
	assert(sprintf(expected, "0%ju", UINTMAX_MAX) > 0);

	librecrypt_concat_uint_(&state, 0u);
	librecrypt_concat_uint_(&state, UINTMAX_MAX);
	assert(state.len == strlen(expected));
	assert(!strcmp(buf, expected));

	state = (struct concat_state){buf, 3u, 0u};
	librecrypt_concat_uint_(&state, 123u);
	assert(!strcmp(buf, "12"));
	assert(state.len == 3u);

	STOP_RESOURCE_TEST();
	return 0;
}


#endif