forked from JohnDMcMaster/usbrply
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
544 lines (485 loc) · 10.8 KB
/
util.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include <string>
#include <vector>
#include <map>
#include "util.h"
#include "uv_err.h"
#include <stdarg.h>
static unsigned int uv_get_num_tokens_core(const char *text, char delim, int ret_blanks);
std::string UVDSprintf(const char *format, ...);
//ypedef int uv_err_t;
#define uv_assert_ret(x)
std::vector<std::string> split(const std::string &s, char delim, bool ret_blanks);
std::vector<std::string> UVDSplit(const std::string &s, char delim, bool ret_blanks);
char **uv_split_core(const char *str, char delim, unsigned int *n_ret, int ret_blanks);
uv_err_t parseNumericRangeString(const std::string &s, uint32_t *first, uint32_t *second)
{
char delim = 0;
std::vector<std::string> parts;
uv_assert_ret(first);
uv_assert_ret(second);
if( s.find('-') != std::string::npos )
{
delim = '-';
}
else if( s.find(',') != std::string::npos )
{
delim = ',';
}
else if( s.find(':') != std::string::npos )
{
delim = ':';
}
//Self to self then
else
{
*first = strtol(s.c_str(), NULL, 0);
*second = *first;
return UV_ERR_OK;
}
parts = split(s, delim, true);
uv_assert_ret(parts.size() == 2);
*first = strtol(parts[0].c_str(), NULL, 0);
std::string second_str = parts[1];
if (second_str == "") {
*second = UINT_MAX;
} else {
*second = strtol(second_str.c_str(), NULL, 0);
}
return UV_ERR_OK;
}
bool g_util_halt_error = false;
std::string transfer_type_str( unsigned int tt ) {
switch (tt) {
case URB_ISOCHRONOUS:
return "URB_ISOCHRONOUS";
case URB_INTERRUPT:
return "URB_INTERRUPT";
case URB_CONTROL:
return "URB_CONTROL";
case URB_BULK:
return "URB_BULK";
default:
printf("WTF? %d\n", __LINE__);
if (g_util_halt_error) {
exit(1);
}
return "";
}
}
std::string get_request_str(unsigned int bRequestType, unsigned int bRequest) {
switch (bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (bRequest) {
case USB_REQ_GET_STATUS:
return "USB_REQ_GET_STATUS";
case USB_REQ_CLEAR_FEATURE:
return "USB_REQ_CLEAR_FEATURE";
case USB_REQ_SET_FEATURE:
return "USB_REQ_SET_FEATURE";
case USB_REQ_SET_ADDRESS:
return "USB_REQ_SET_ADDRESS";
case USB_REQ_GET_DESCRIPTOR:
return "USB_REQ_GET_DESCRIPTOR";
case USB_REQ_SET_DESCRIPTOR:
return "USB_REQ_SET_DESCRIPTOR";
case USB_REQ_GET_CONFIGURATION:
return "USB_REQ_GET_CONFIGURATION";
case USB_REQ_SET_CONFIGURATION:
return "USB_REQ_SET_CONFIGURATION";
case USB_REQ_GET_INTERFACE:
return "USB_REQ_GET_INTERFACE";
case USB_REQ_SET_INTERFACE:
return "USB_REQ_SET_INTERFACE";
case USB_REQ_SYNCH_FRAME:
return "USB_REQ_SYNCH_FRAME";
default:
printf("WTF? %d\n", __LINE__);
if (g_util_halt_error) {
exit(1);
}
return "";
};
//TODO: consider decoding class, although really its not as of much interest for replaying
//since it should be well defined
case USB_TYPE_CLASS:
case USB_TYPE_VENDOR:
case USB_TYPE_RESERVED:
return UVDSprintf("0x%02X", bRequest);
default:
printf("WTF? %d\n", __LINE__);
if (g_util_halt_error) {
exit(1);
}
return "";
}
}
std::string get_request_type_str(unsigned int bRequestType) {
std::string ret = "";
if ((bRequestType & USB_DIR_IN) == USB_DIR_IN) {
ret += "USB_DIR_IN";
} else {
ret += "USB_DIR_OUT";
}
switch (bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
ret += " | USB_TYPE_STANDARD";
break;
case USB_TYPE_CLASS:
ret += " | USB_TYPE_CLASS";
break;
case USB_TYPE_VENDOR:
ret += " | USB_TYPE_VENDOR";
break;
case USB_TYPE_RESERVED:
ret += " | USB_TYPE_RESERVED";
break;
default:
printf("WTF? %d\n", __LINE__);
if (g_util_halt_error) {
exit(1);
}
return "";
}
switch (bRequestType & USB_RECIP_MASK) {
case USB_RECIP_DEVICE:
ret += " | USB_RECIP_DEVICE";
break;
case USB_RECIP_INTERFACE:
ret += " | USB_RECIP_INTERFACE";
break;
case USB_RECIP_ENDPOINT:
ret += " | USB_RECIP_ENDPOINT";
break;
case USB_RECIP_OTHER:
ret += " | USB_RECIP_OTHER";
break;
case USB_RECIP_PORT:
ret += " | USB_RECIP_PORT";
break;
case USB_RECIP_RPIPE:
ret += " | USB_RECIP_RPIPE";
break;
default:
printf("WTF? %d\n", __LINE__);
if (g_util_halt_error) {
exit(1);
}
return "";
}
return ret;
}
std::string urb_type_str(unsigned int t) {
switch (t) {
case URB_SUBMIT:
return "URB_SUBMIT";
case URB_COMPLETE:
return "URB_COMPLETE";
case URB_ERROR:
return "URB_ERROR";
default:
printf("WTF? %d\n", __LINE__);
if (g_util_halt_error) {
exit(1);
}
return "";
}
}
void print_urb(usb_urb_t *urb) {
printf("\tid: 0x%016lX\n", urb->id);
printf("\ttype: %s (%c / 0x%02X)\n", urb_type_str(urb->type).c_str(), urb->type, urb->type);
printf("\ttransfer_type: %s (0x%02X)\n",
transfer_type_str(urb->transfer_type).c_str(), urb->transfer_type );
printf("\tendpoint: 0x%02X\n", urb->endpoint);
printf("\tdevice: 0x%02X\n", urb->device);
printf("\tbus_id: 0x%04X\n", urb->bus_id);
printf("\tsetup_request: 0x%02X\n", urb->setup_request);
printf("\tdata: 0x%02X\n", urb->data);
//printf("\tsec: 0x%016llX\n", urb->sec);
printf("\tusec: 0x%08X\n", urb->usec);
printf("\tstatus: 0x%08X\n", urb->status);
printf("\tlength: 0x%08X\n", urb->length);
printf("\tdata_length: 0x%08X\n", urb->data_length);
}
uint32_t g_bytesPerRow = 16;
uint32_t g_bytesPerHalfRow = 8;
static unsigned int hexdumpHalfRow(const uint8_t *data, size_t size, uint32_t start)
{
uint32_t col = 0;
for( ; col < g_bytesPerHalfRow && start + col < size; ++col )
{
uint32_t index = start + col;
uint8_t c = data[index];
printf("%.2X ", (unsigned int)c);
fflush(stdout);
}
//pad remaining
while( col < g_bytesPerHalfRow )
{
printf(" ");
fflush(stdout);
++col;
}
//End pad
printf(" ");
fflush(stdout);
return start + g_bytesPerHalfRow;
}
void UVDHexdumpCore(const uint8_t *data, size_t size, const std::string &prefix)
{
/*
[mcmaster@gespenst icd2prog-0.3.0]$ hexdump -C /bin/ls |head
00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 02 00 03 00 01 00 00 00 f0 99 04 08 34 00 00 00 |............4...|
00017380 00 00 00 00 01 00 00 00 00 00 00 00 |............|
*/
size_t pos = 0;
while( pos < size )
{
uint32_t row_start = pos;
uint32_t i = 0;
printf("%s", prefix.c_str());
fflush(stdout);
pos = hexdumpHalfRow(data, size, pos);
pos = hexdumpHalfRow(data, size, pos);
printf("|");
fflush(stdout);
//Char view
for( i = row_start; i < row_start + g_bytesPerRow && i < size; ++i )
{
char c = data[i];
if( isprint(c) )
{
printf("%c", c);
fflush(stdout);
}
else
{
printf("%c", '.');
fflush(stdout);
}
}
for( ; i < row_start + g_bytesPerRow; ++i )
{
printf(" ");
fflush(stdout);
}
printf("|\n");
fflush(stdout);
}
fflush(stdout);
}
void UVDHexdump(const uint8_t *data, size_t size)
{
UVDHexdumpCore(data, size, "");
}
#define uv_err_str_case(x) case x: return #x;
const char *uv_err_str(uv_err_t err)
{
switch(err)
{
uv_err_str_case(UV_ERR_GENERAL);
uv_err_str_case(UV_ERR_ACCESS);
uv_err_str_case(UV_ERR_OUTMEM);
uv_err_str_case(UV_ERR_NOTFOUND);
uv_err_str_case(UV_ERR_ABORTED);
uv_err_str_case(UV_ERR_ARGS);
uv_err_str_case(UV_ERR_NOTSUPPORTED);
uv_err_str_case(UV_ERR_BUFFERSIZE);
uv_err_str_case(UV_ERR_ARBITRARYLIMIT);
uv_err_str_case(UV_ERR_COMPATIBILITY);
uv_err_str_case(UV_ERR_NOTIMPLEMENTED);
uv_err_str_case(UV_ERR_DISASM_COMBO);
uv_err_str_case(UV_ERR_DISASM_NODAT);
uv_err_str_case(UV_ERR_DISASM_PREFIX);
default:
return "UNKNOWN";
}
}
uv_err_t uv_err_ret_handler(uv_err_t rc, const char *file, uint32_t line, const char *func)
{
if( UV_FAILED(rc) )
{
printf("ERROR: %s (%s:%d): rc=%s\n", func, file, line, uv_err_str(rc));
}
else if( UV_WARNING(rc) )
{
printf("WARNING: %s (%s:%d): rc=%s\n", func, file, line, uv_err_str(rc));
}
return rc;
}
std::vector<std::string> split(const std::string &s, char delim, bool ret_blanks)
{
return UVDSplit(s, delim, ret_blanks);
}
std::vector<std::string> UVDSplit(const std::string &s, char delim, bool ret_blanks)
{
char **coreRet = NULL;
char **cur = NULL;
std::vector<std::string> ret;
coreRet = uv_split_core(s.c_str(), delim, NULL, ret_blanks);
if( !coreRet )
{
return ret;
}
for( cur = coreRet; *cur; ++cur )
{
ret.push_back(*cur);
free(*cur);
}
free(coreRet);
return ret;
}
/*
XXX: this is really old code that seems to work, but really should be phased out
str: string to split
delim: character to delimit by
If null, will return the array with a single string
n_ret: if set, will return number of items in the output, otherwise, output is a null terminated array
ret_blanks: return empty strings?
*/
char **uv_split_core(const char *str, char delim, unsigned int *n_ret, int ret_blanks)
{
unsigned int n = 0;
char *buff = NULL;
static char **ret = NULL;
unsigned int str_index = 0;
unsigned int i = 0;
n = uv_get_num_tokens_core(str, delim, ret_blanks);
if( n_ret )
{
*n_ret = n;
}
else
{
++n;
}
ret = (char **)malloc(sizeof(char *) * (n));
if( !ret || (n_ret && n == 0) )
{
return NULL;
}
/* The extra bit of n is only needed shortly during allocation */
if( !n_ret )
{
--n;
ret[n] = NULL;
}
buff = strdup(str);
if( !buff )
{
return NULL;
}
for( i = 0; i < n; ++i )
{
unsigned int j = 0;
for( j = 0; str[str_index] != delim && str[str_index] != 0; ++j, ++str_index )
{
buff[j] = str[str_index];
}
buff[j] = 0;
/* skip over the null */
++str_index;
ret[i] = strdup(buff);
if( ret[i] == NULL )
{
goto error;
}
}
free(buff);
return ret;
error:
for( ;; )
{
free(ret[i]);
if( i == 0 )
{
break;
}
--i;
}
free(ret);
free(buff);
return NULL;
}
static unsigned int uv_get_num_tokens_core(const char *text, char delim, int ret_blanks)
{
int ret = 1;
unsigned int i = 0;
for( i = 0; text[i] != 0; ++i )
{
if( text[i] == delim && (ret_blanks || (i != 0 && text[i - 1] != delim)) )
{
++ret;
}
}
return ret;
}
char *uv_get_line(const char *text, unsigned int lineNumber)
{
char *line = NULL;
unsigned int index = 0;
char cur_char='a';
unsigned int line_size = 0;
unsigned int i = 0;
for(i = 0; i < lineNumber; ++i)
{
while( text[index] != '\n' )
{
++index;
}
}
//Find line size
for( line_size = 0; true; ++line_size )
{
cur_char = text[index + line_size];
if( cur_char == '\n' || cur_char == '\r' || cur_char == 0 )
{
break;
}
}
line = (char *)malloc(sizeof(char) * (line_size + 1));
if( !line )
{
return NULL;
}
//Then copy
for( i = 0; true; ++i )
{
cur_char = text[index + i];
if( cur_char == '\n' || cur_char == '\r' || cur_char == 0 )
{
break;
}
line[i] = cur_char;
}
line[i] = 0;
return line;
}
char *cap_fixup(char *str)
{
char *ptr = str;
//UV_ENTER();
if( !str )
{
return str;
}
/*
if( g_config->m_caps )
{
while( *ptr )
{
*ptr = toupper(*ptr);
++ptr;
}
}
else
*/
{
while( *ptr )
{
*ptr = tolower(*ptr);
++ptr;
}
}
return str;
}