-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.c
66 lines (52 loc) · 1.26 KB
/
util.c
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
#include "main.h"
static uint8_t hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
static void to_hex(uint8_t *a, uint8_t *p, int size)
{
uint8_t b, *end = p + size;
while(p != end) {
b = *p++;
*a++ = hex[b >> 4];
*a++ = hex[b & 0xF];
}
}
void key_to_string(uint8_t *dest, uint8_t *src)
{
to_hex(dest, src, 32);
}
void id_to_string(uint8_t *dest, uint8_t *src)
{
to_hex(dest, src, TOX_ID_SIZE);
}
_Bool string_to_id(uint8_t *w, uint8_t *a)
{
uint8_t *end = w + TOX_ID_SIZE;
while(w != end) {
uint8_t c, v;
c = *a++;
if(c >= '0' && c <= '9') {
v = (c - '0') << 4;
} else if(c >= 'A' && c <= 'F') {
v = (c - 'A' + 10) << 4;
} else {
return 0;
}
c = *a++;
if(c >= '0' && c <= '9') {
v |= (c - '0');
} else if(c >= 'A' && c <= 'F') {
v |= (c - 'A' + 10);
} else {
return 0;
}
*w++ = v;
}
return 1;
}
_Bool validate_id(uint8_t *id)
{
uint8_t checksum[2] = {0};
uint32_t i;
for (i = 0; i < 36; ++i)
checksum[i % 2] ^= id[i];
return (id[36] == checksum[0] && id[37] == checksum[1]);
}