-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
147 lines (121 loc) · 2.67 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
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
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#define PER_LINE 16
int fd = -1;
void printText(FILE* file, FILE* stream, int* flags)
{
char trans[PER_LINE+1];
trans[PER_LINE] = 0;
int c = fgetc(file), count = 0, line = 1;
if (flags[3] == 0)
{
fprintf(stream, "%#08x: ", line); // FIRST LINE NUMBER
}
do {
if (c > 32 && c < 127) {
trans[count] = c;
} else {
trans[count] = '.';
}
if (flags[3] == 0) {
fprintf(stream, "%#04x ", c);
count++;
if (count >= PER_LINE)
{
fprintf(stream, "|%s|\n", trans); // ASCII
line++;
fprintf(stream, "%#08x: ", line); // LINE #
count = 0;
}
} else {
fprintf(stream, "%02x ", c);
count++;
if (count >= PER_LINE)
{
fprintf(stream, "\n");
count = 0;
}
}
c = fgetc(file);
} while (c != EOF);
if (count != 0 && flags[3] == 0) {
for (int i = PER_LINE-1; i > count-1; i--) {
fprintf(stream, "%#04x ", 3);
trans[i] = '.';
}
fprintf(stream, "|%s|\n",trans);
}
}
int printDocs() {
FILE* docs = fopen("hexdocs.txt", "r");
int c = 0;
if (docs == NULL) {
printf("No documentation found!");
return -1;
}
while ((c = fgetc(docs)) != EOF) {
printf("%c",c);
}
fclose(docs);
}
void defineFlags(int flags[], FILE** stream, char *argv[], int argc) {
// Function assumes file is real
for (int i = 2; i < argc; i+=1) {
if (strcmp("-e", argv[i]) == 0) {
flags[0] = atoi(argv[i+1]);
i++;
continue;
} else if (strcmp("-s", argv[i]) == 0){
flags[1] = atoi(argv[i+1]);
i++;
continue;
} else if (strcmp("-p", argv[i]) == 0) {
*stream = fopen(argv[i+1], "w+");
flags[2] = 1;
i++;
continue;
} else if (strcmp("-r", argv[i]) == 0) {
flags[3] = 1;
i++;
continue;
} else {
printf("%s is not a valid argument. Do -h for a list of valid flags!\n Flags include -p for output file (not sure why I made it -p and not -o)\n", argv[i]);
exit(1);
}
}
}
/* Flag numbers as follows
* -e flag 0 (end, means nothing)
* -s flag 1 (start, means nothing)
* -p flag 2 (print to file)
* -r flag 3 (raw data)
*/
int main (int argc, char *argv[])
{
int fileParam = 1;
int flags[4] = {-1, -1, 0, 0};
FILE* stream = stdout;
if (strcmp("-h", argv[1]) == 0){
printDocs();
return 1;
}
FILE* file; // The file we are going to load
file = fopen(argv[fileParam], "rb");
if (file == NULL)
{
printf("File %s not found!", argv[fileParam]);
return -1;
}
printf("File found at %#x\n", file);
defineFlags(flags, &stream, argv, argc);
printText(file, stream, flags);
if (flags[2] == 1) {
}
printf("File binaries completed!");
fclose(stream);
fclose(file);
return 0;
}