-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvdisk-helpers.c
66 lines (54 loc) · 1.15 KB
/
vdisk-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
#include "vdisk-helpers.h"
unsigned long vdisk_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 *vdisk_truncate_file_name(const char *file_name)
{
char *base;
base = strrchr(file_name, '/');
if (base)
return ++base;
else
return file_name;
}
int vdisk_hex_to_byte(unsigned 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 vdisk_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 = vdisk_hex_to_byte(hex[i]);
low = vdisk_hex_to_byte(hex[i + 1]);
if (high == -1 || low == -1)
return -EINVAL;
dst[pos] = (high << 4) + low;
}
return 0;
}