1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <errno.h>
4 #include <time.h>
5
diff_time(struct timespec * start,struct timespec * end)6 long long diff_time(struct timespec *start, struct timespec *end)
7 {
8 long long diff;
9 diff = (end->tv_sec - start->tv_sec) * 1000 * 1000;
10 diff += (end->tv_nsec - start->tv_nsec) / 1000;
11 return diff;
12 }
13
Sleep(int ms)14 int Sleep(int ms)
15 {
16 struct timespec ts;
17 struct timespec rem;
18
19 ts.tv_sec = ms / 1000;
20 ts.tv_nsec = (ms % 1000) * 1000 * 1000;
21 for (;;) {
22 if (nanosleep(&ts, &rem) == 0) {
23 break;
24 } else {
25 if (errno == EINTR) {
26 ts = rem;
27 continue;
28 }
29 return -1;
30 }
31 }
32 return 0;
33 }
34
print_buffer(const unsigned char * buf,unsigned int len)35 void print_buffer(const unsigned char *buf, unsigned int len)
36 {
37 for (unsigned int i = 0; i < len; ++i) {
38 fprintf(stdout, "0x%02X ", buf[i]);
39 if (i % 8 == 7)
40 fprintf(stdout, "\n");
41 }
42 fprintf(stdout, "\n");
43 }
44
45
StripPath(const char * path,ssize_t size)46 const char * StripPath(const char * path, ssize_t size)
47 {
48 int i;
49 const char * str;
50
51 for (i = size - 1, str = &path[size - 1]; i > 0; --i, --str)
52 if (path[i - 1] == '/')
53 break;
54
55 return str;
56 }
57
extract_long(const unsigned char * data)58 unsigned long extract_long(const unsigned char *data)
59 {
60 return (unsigned long)data [0]
61 + (unsigned long)data [1] * 0x100
62 + (unsigned long)data [2] * 0x10000
63 + (unsigned long)data [3] * 0x1000000;
64 }
65
extract_short(const unsigned char * data)66 unsigned short extract_short(const unsigned char *data)
67 {
68 return (unsigned long)data [0]
69 + (unsigned long)data [1] * 0x100;
70 }