forked from angrytongan/c2api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
112 lines (90 loc) · 2.38 KB
/
utils.cpp
File metadata and controls
112 lines (90 loc) · 2.38 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#define HEXDUMP_WIDTH 16
/*
* Dump ascii part of a hexdump line.
*/
static void hexdump_readable(unsigned char *b, unsigned char *s) {
int i = 3 * (HEXDUMP_WIDTH - (b - s));
if (s == b) {
return;
}
printf(" ");
while (i--) {
printf(" ");
}
for (; s != b; s++) {
printf("%c", (*s >= 0x20 && *s <= 0x7e) ? *s : '.');
}
printf("\n");
}
/*
* Dump a buffer in hexdump / ascii format.
*/
void hexdump(unsigned char *buff, unsigned int len) {
int i;
unsigned char *b;
for (b = buff, i = 0; b != &buff[len]; ) {
printf("%02x ", *b++);
if (++i == HEXDUMP_WIDTH) {
hexdump_readable(b, b - HEXDUMP_WIDTH);
i = 0;
}
}
hexdump_readable(b, b - (len % HEXDUMP_WIDTH));
}
/*
* Convert seconds to hours, minutes, seconds.
*/
void dhms(unsigned int seconds, unsigned int *d, unsigned int *h, unsigned int *m,
unsigned int *s) {
*d = seconds / (60 * 60 * 24);
seconds %= (60 * 60 * 24);
*h = seconds / (60 * 60);
seconds %= (60 * 60);
*m = seconds / 60;
*s = seconds % 60;
}
/* pack a value v into LSB -> MSB buffer of length l */
void val2bytes(unsigned int v, unsigned int l, unsigned char *b) {
unsigned int i;
for (i = 0; i < l; i++) {
b[i] = (v >> (8 * i)) & 0xff;
}
}
/* unpack LSB -> MSB buffer of length l into value v */
void bytes2val(unsigned char *b, unsigned int l, unsigned int *v) {
unsigned int i;
*v = 0;
for (i = 0; i < l; i++) {
*v += b[i] << (8 * i);
}
}
#ifdef UNITTEST_UTILS
void print_time(unsigned int seconds, unsigned int d, unsigned int h, unsigned int m,
unsigned int s) {
printf("%02d days, %02d:%02d:%02d : %d\n", d, h, m, s, seconds);
}
int main(void) {
int i;
unsigned char buff[128];
unsigned int d, h, m, s;
unsigned int times[] = {
1, 59, 60, 61, 119, 120, 121, 1000,
86399, 86400, 86401,
172799, 172800, 172801
};
for (i = 0; i < sizeof(buff); i++) {
buff[i] = i & 0xff;
}
hexdump(buff, sizeof(buff));
hexdump(buff, 1);
hexdump(buff, 5);
hexdump(buff, 27);
hexdump(buff, 64);
for (i = 0; i < sizeof(times) / sizeof(unsigned int); i++) {
dhms(times[i], &d, &h, &m, &s);
print_time(times[i], d, h, m, s);
}
return 0;
}
#endif /* UNITTEST */