-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
86 lines (75 loc) · 1.99 KB
/
main.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
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
void print_usage() {
printf("cbinarytotccoverter [input_file] [bit_count]\n");
}
unsigned char* hexset = "0123456789ABCDEF";
void print_hex(unsigned char* buffer, __uint32_t bytesCount) {
printf("0x");
for (__uint32_t i = 0; i < bytesCount; i++) {
__uint32_t f = buffer[i] / 16;
__uint32_t l = buffer[i] - f*16;
__uint32_t fh = hexset[f];
__uint32_t lh = hexset[l];
putc(fh, stdout);
putc(lh, stdout);
}
printf("\n");
}
int main(int argc, char** argv) {
if (argc < 2) {
print_usage();
return 0;
}
if (argc < 3) {
printf("No bit count specified!\n");
return 0;
}
__uint32_t bytesPerInstruction = 1;
__uint32_t bitsCount = 0;
sscanf(argv[2], "%d", &bitsCount);
switch (bitsCount) {
case 8:
bytesPerInstruction = 1;
break;
case 16:
bytesPerInstruction = 2;
break;
case 32:
bytesPerInstruction = 4;
break;
case 64:
bytesPerInstruction = 8;
break;
case 128:
bytesPerInstruction = 16;
break;
default:
printf("Unsupported bit count");
return 1;
}
FILE* input = fopen(argv[1], "rb");
//printf("Starting conversion.\n");
unsigned char* buffer = (char*)malloc(bytesPerInstruction);
while (fread(buffer, 1, bytesPerInstruction, input) != NULL) {
switch (bytesPerInstruction) {
case 1:
print_hex(buffer, 1);
break;
case 2:
print_hex(buffer, 2);
break;
case 4:
print_hex(buffer, 4);
break;
case 8:
print_hex(buffer, 8);
break;
case 16:
print_hex(buffer, 16);
break;
}
}
fclose(input);
}