-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
65 lines (56 loc) · 1.36 KB
/
utils.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
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#define cpu_to_be32(v) (((v)>>24) | (((v)>>8)&0xff00) | (((v)<<8)&0xff0000)|((v)<<24))
static void int2char(const unsigned int src, unsigned char *dst){
unsigned int temp = src;
if(NULL == dst)
return;
for(int i=0;i<4;i++){
dst[i] = temp&0xff;
temp = temp>>8;
}
}
void print_buf(unsigned char *buf, int len)
{
int i;
if (buf == NULL || len == 0)
return;
for (i = 0; i < len; i++)
{
printf("%02x ", buf[i]);
if (0 == (i + 1) % 16)
printf("\n");
}
printf("\n");
}
int Hex2Bin(const char *src, unsigned char *dst)
{
int i = 0, j, len;
unsigned int temp;
if (NULL == src)
return -1;
int srclen = strlen(src);
if (srclen == 0)
return -1;
for (i = 0; i < srclen; i += 2)
{
for (j = 0; j < 2; j++)
{
if (src[i + j] >= '0' && src[i + j] <= '9')
continue;
if (src[i + j] >= 'a' && src[i + j] <= 'f')
continue;
if (src[i + j] >= 'A' && src[i + j] <= 'F')
continue;
else
return -1;
}
if(sscanf(&(src[i]), "%02x",&temp) >0){
dst[i/2] = temp;
}else
break;
len = i / 2;
return len;
}
}