summaryrefslogtreecommitdiff
path: root/src/util.c
blob: 96cbe118fdf8424cfd2bf2b346bffcce33a393c9 (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
63
64
65
66
67
#include "aplos.h"

uint16_t
read_uint16_le(const uint8_t *p)
{
	return p[0] | (p[1] << 8);
}

uint16_t
read_uint16_be(const uint8_t *p)
{
	return p[1] | (p[0] << 8);
}

uint32_t
read_uint32_le(const uint8_t *p)
{
	return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}

void write_uint16_le(uint8_t *p, uint16_t v)
{
	p[0] = v & 0xFF;
	p[1] = (v >> 8) & 0xFF;
}

void write_uint32_le(uint8_t *p, uint32_t v)
{
	p[0] = v & 0xFF;
	p[1] = (v >> 8) & 0xFF;
	p[2] = (v >> 16) & 0xFF;
	p[3] = (v >> 24) & 0xFF;
}

void write_uint64_le(uint8_t *p, uint64_t v)
{
	p[0] = v & 0xFF;
	p[1] = (v >> 8) & 0xFF;
	p[2] = (v >> 16) & 0xFF;
	p[3] = (v >> 24) & 0xFF;
	p[4] = (v >> 32) & 0xFF;
	p[5] = (v >> 40) & 0xFF;
	p[6] = (v >> 48) & 0xFF;
	p[7] = (v >> 56) & 0xFF;
}

int
memcmp(const void *p1, const void *p2, size_t n)
{
	const uint8_t *a = p1;
	const uint8_t *b = p2;
	for(size_t i = 0; i < n; i++){
		if(a[i] < b[i])
			return -1;
		else if(a[i] > b[i])
			return 1;
	}
	return 0;
}

void
memset(void *v, uint8_t b, size_t n)
{
	uint8_t *p = v;
	for(size_t i = 0; i < n; i++)
		p[i] = b;
}