-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdmap-helpers.c
92 lines (74 loc) · 1.57 KB
/
dmap-helpers.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
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
#include "dmap-helpers.h"
unsigned long dmap_hash_pointer(void *ptr)
{
unsigned long val = (unsigned long)ptr;
unsigned long hash, i, c;
hash = 5381;
val = val >> 3;
for (i = 0; i < sizeof(val); i++) {
c = (unsigned char)val & 0xFF;
hash = ((hash << 5) + hash) + c;
val = val >> 8;
}
return hash;
}
const char *dmap_truncate_file_name(const char *file_name)
{
char *base;
base = strrchr(file_name, '/');
if (base)
return ++base;
else
return file_name;
}
char dmap_byte_to_hex(unsigned char c)
{
if (WARN_ON(c > 15))
return '0';
if (c < 10)
return '0' + c;
else
return 'a' + c - 10;
}
void dmap_bytes_to_hex(unsigned char *src, int slen, char *dst, int dlen)
{
int i;
if (WARN_ON(dlen < (2 * slen + 1)))
return;
for (i = 0; i < slen; i++) {
dst[2 * i] = dmap_byte_to_hex((src[i] >> 4) & 0xF);
dst[2 * i + 1] = dmap_byte_to_hex(src[i] & 0xF);
}
dst[2 * slen] = '\0';
}
unsigned char dmap_hex_to_byte(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
else
return -1;
}
int dmap_hex_to_bytes(char *hex, int hex_len, unsigned char *dst,
int dst_len)
{
int i, pos;
int low, high;
if (hex_len <= 0 || dst_len <= 0)
return -EINVAL;
if (hex_len & 1)
return -EINVAL;
if (dst_len != hex_len/2)
return -EINVAL;
for (i = 0, pos = 0; i < hex_len; i += 2, pos += 1) {
high = dmap_hex_to_byte(hex[i]);
low = dmap_hex_to_byte(hex[i + 1]);
if (high == -1 || low == -1)
return -EINVAL;
dst[pos] = (high << 4) + low;
}
return 0;
}