-
Notifications
You must be signed in to change notification settings - Fork 47
/
utils.c
308 lines (267 loc) · 8.33 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "./header/utils.h"
#include <sys/time.h>
#include <time.h>
#include "./header/fileLib.h"
#include "./header/memory.h"
#include "./header/vmrp.h"
// 只支持240*320大小
void printScreen(char *filename, uint16_t *buf) {
// clang-format off
unsigned char bmpHeader[70] =
{
0x42, 0x4D, 0x48, 0x58, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x38, 0x00,
0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0x01, 0x00, 0x10, 0x00, 0x03, 0x00,
0x00, 0x00, 0x02, 0x58, 0x02, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0xE0, 0x07, 0x00, 0x00, 0x1F, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
} ;
// clang-format on
int fh = my_open(filename, MR_FILE_CREATE | MR_FILE_RDWR);
my_write(fh, bmpHeader, sizeof(bmpHeader));
my_write(fh, buf, 240 * 320 * 2);
uint16_t end = 0;
my_write(fh, &end, sizeof(end));
my_close(fh);
}
char *memTypeStr(uc_mem_type type) {
// clang-format off
switch (type)
{
case UC_MEM_READ:return "UC_MEM_READ";
case UC_MEM_WRITE:return "UC_MEM_WRITE";
case UC_MEM_FETCH:return "UC_MEM_FETCH";
case UC_MEM_READ_UNMAPPED:return "UC_MEM_READ_UNMAPPED";
case UC_MEM_WRITE_UNMAPPED:return "UC_MEM_WRITE_UNMAPPED";
case UC_MEM_FETCH_UNMAPPED:return "UC_MEM_FETCH_UNMAPPED";
case UC_MEM_WRITE_PROT:return "UC_MEM_WRITE_PROT";
case UC_MEM_READ_PROT:return "UC_MEM_READ_PROT";
case UC_MEM_FETCH_PROT:return "UC_MEM_FETCH_PROT";
case UC_MEM_READ_AFTER:return "UC_MEM_READ_AFTER";
}
// clang-format on
return "<error type>";
}
void cpsrToStr(uint32_t v, char *out) {
out[0] = (v & (1 << 31)) ? 'N' : 'n';
out[1] = (v & (1 << 30)) ? 'Z' : 'z';
out[2] = (v & (1 << 29)) ? 'C' : 'c';
out[3] = (v & (1 << 28)) ? 'V' : 'v';
out[4] = '\0';
}
void dumpREG(uc_engine *uc) {
uint32_t v, cpsr;
uc_reg_read(uc, UC_ARM_REG_CPSR, &cpsr);
// clang-format off
printf("==========================REG=================================\n");
uc_reg_read(uc, UC_ARM_REG_R0, &v); printf(" R0=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_R1, &v); printf("R1=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_R2, &v); printf(" R2=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_R3, &v); printf(" R3=0x%08X\tN:%d\n", v, (cpsr&(1<<31))>>31);
uc_reg_read(uc, UC_ARM_REG_R4, &v); printf(" R4=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_R5, &v); printf("R5=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_R6, &v); printf(" R6=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_R7, &v); printf(" R7=0x%08X\tZ:%d\n", v, (cpsr&(1<<30))>>30);
uc_reg_read(uc, UC_ARM_REG_R8, &v); printf(" R8=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_R9, &v); printf("R9=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_R10, &v); printf("R10=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_R11, &v); printf("R11=0x%08X\tC:%d\n", v, (cpsr&(1<<29))>>29);
uc_reg_read(uc, UC_ARM_REG_R12, &v); printf("R12=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_SP, &v); printf("SP=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_LR, &v); printf(" LR=0x%08X\t", v);
uc_reg_read(uc, UC_ARM_REG_PC, &v); printf(" PC=0x%08X\tV:%d\n", v, (cpsr&(1<<28))>>28);
printf("==============================================================\n");
// clang-format on
}
void dumpMemStr(void *ptr, size_t len) {
char *p = ptr;
for (int i = 0; i < len; i++) {
if (isgraph(*p)) {
putchar(*p);
} else {
putchar('.');
}
p++;
}
}
/*
将字符串看成是split分隔的数组,多个连续的split会看成一个,返回的内存需要free
"dump,,,a.bin,0x2b3e16,,0xff"
*/
char *getSplitStr(char *str, char split, int n) {
char *start = str;
char *end, *ret, *mem;
int count = 0;
if (!start) return NULL;
if (n == 0) goto retResult;
while (*start) {
if (*start != split) {
start++;
continue;
}
count++;
while (*start && (*start == split)) start++;
if (count == n) {
goto retResult;
}
}
return NULL;
retResult:
end = start;
while (*end && (*end != split)) end++;
ret = malloc(end - start + 1);
mem = ret;
while (start < end) {
*mem = *start;
mem++;
start++;
}
*mem = '\0';
return ret;
}
int wstrlen(char *txt) {
int i = 0;
if (txt) {
while (txt[i] || txt[i + 1]) i += 2;
}
return i;
}
uint32_t copyWstrToMrp(char *str) {
if (!str) return 0;
int len = wstrlen(str) + 2;
void *p = my_mallocExt(len);
memcpy(p, str, len);
return toMrpMemAddr(p);
}
uint32_t copyStrToMrp(char *str) {
if (!str) return 0;
uint32_t len = strlen(str) + 1;
void *p = my_mallocExt(len);
memcpy(p, str, len);
return toMrpMemAddr(p);
}
int64_t get_uptime_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
}
int64_t get_time_ms(void) {
struct timeval tv;
if (-1 == gettimeofday(&tv, NULL)) {
return -1;
}
return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
}
int32_t getDatetime(mr_datetime *datetime) {
if (!datetime)
return MR_FAILED;
time_t now;
struct tm *t;
time(&now);
t = localtime(&now);
datetime->year = t->tm_year + 1900;
datetime->month = t->tm_mon + 1;
datetime->day = t->tm_mday;
datetime->hour = t->tm_hour;
datetime->minute = t->tm_min;
datetime->second = t->tm_sec;
return MR_SUCCESS;
}
//////////////////////////////////////////////////////////////////////////////
uIntMap *uIntMap_search(struct rb_root *root, uint32_t key) {
struct rb_node *n = root->rb_node;
uIntMap *obj;
int cmp;
while (n) {
obj = rb_entry(n, uIntMap, node);
cmp = key - obj->key;
if (cmp < 0) {
n = n->rb_left;
} else if (cmp > 0) {
n = n->rb_right;
} else {
return obj;
}
}
return NULL;
}
int uIntMap_insert(struct rb_root *root, uIntMap *obj) {
struct rb_node **p = &(root->rb_node);
struct rb_node *parent = NULL;
uIntMap *cur;
int cmp;
while (*p) {
parent = *p;
cur = rb_entry(parent, uIntMap, node);
cmp = obj->key - cur->key;
if (cmp < 0) {
p = &(*p)->rb_left;
} else if (cmp > 0) {
p = &(*p)->rb_right;
} else {
return -1;
}
}
rb_link_node(&obj->node, parent, p);
rb_insert_color(&obj->node, root);
return 0;
}
uIntMap *uIntMap_delete(struct rb_root *root, uint32_t key) {
uIntMap *obj = uIntMap_search(root, key);
if (!obj) {
return NULL;
}
rb_erase(&obj->node, root);
return obj;
}
static void testInsert(struct rb_root *root, uint32_t key, void *data) {
uIntMap *obj;
int ret;
obj = malloc(sizeof(uIntMap));
obj->key = key;
obj->data = data;
ret = uIntMap_insert(root, obj);
if (ret == -1) {
printf("insert failed %d exists.\n", key);
return;
}
printf("insert %d success.\n", key);
}
static void testSearch(struct rb_root *root, uint32_t key) {
uIntMap *obj = uIntMap_search(root, key);
if (obj == NULL) {
printf("search: not found %d\n", key);
} else {
printf("search: %d=%s\n", key, (char *)obj->data);
}
}
static void printAll(struct rb_root *root) {
for (struct rb_node *p = rb_first(root); p; p = rb_next(p)) {
uIntMap *obj = rb_entry(p, uIntMap, node);
printf("iterator: %d \t %s\n", obj->key, (char *)obj->data);
}
}
static void testDelete(struct rb_root *root, uint32_t key) {
uIntMap *obj = uIntMap_delete(root, key);
if (obj != NULL) {
printf("delete %d %s\n", obj->key, (char *)obj->data);
free(obj);
} else {
printf("delete %d not found\n", key);
}
}
void uIntMap_testMain() {
struct rb_root root = RB_ROOT;
testInsert(&root, 100, "hell");
testInsert(&root, 0, "world");
testInsert(&root, 0, "world2");
testInsert(&root, 990, "test");
printAll(&root);
testSearch(&root, 990);
testSearch(&root, 22);
testDelete(&root, 990);
testDelete(&root, 990);
testSearch(&root, 990);
printAll(&root);
testInsert(&root, 990, "test2");
printAll(&root);
}