-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.c
54 lines (41 loc) · 1.2 KB
/
common.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
#include "common.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
int is_jbrand_value(uint64_t value)
{
#if IGNORE_JBRAND_VERIFY
return 1;
#endif
uint8_t check = value>>8 ^ value >> 16 ^ value>>24 ^ value>>32 ^ value>>40 ^ value>>48 ^ value>>56;
return check == (uint8_t)value;
}
int is_jbroot_name(const char* name)
{
if(strlen(name) != (sizeof(JB_ROOT_PREFIX)-1+JB_RAND_LENGTH))
return 0;
if(strncmp(name, JB_ROOT_PREFIX, sizeof(JB_ROOT_PREFIX)-1) != 0)
return 0;
char* endp=NULL;
uint64_t value = strtoull(name+sizeof(JB_ROOT_PREFIX)-1, &endp, 16);
if(!endp || *endp!='\0')
return 0;
if(!is_jbrand_value(value))
return 0;
return 1;
}
uint64_t resolve_jbrand_value(const char* name)
{
if(strlen(name) != (sizeof(JB_ROOT_PREFIX)-1+JB_RAND_LENGTH))
return 0;
if(strncmp(name, JB_ROOT_PREFIX, sizeof(JB_ROOT_PREFIX)-1) != 0)
return 0;
char* endp=NULL;
uint64_t value = strtoull(name+sizeof(JB_ROOT_PREFIX)-1, &endp, 16);
if(!endp || *endp!='\0')
return 0;
if(!is_jbrand_value(value))
return 0;
return value;
}