-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
215 lines (181 loc) · 4.41 KB
/
Copy pathmain.c
File metadata and controls
215 lines (181 loc) · 4.41 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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#define PROGRAMNAME "CexDumper"
#define VERSION "v0.4"
void printUsage(char *fileName)
{
printf("%s %s - C Hex Dumper\n\n", PROGRAMNAME, VERSION);
printf("Usage: %s [options] <filename>\n\n", fileName);
printf("Options:\n -h Show this help message\n -v Show version\n -o <file> Write output to file");
exit(EXIT_SUCCESS);
}
int saveToFile(char *inputFilePath, char *outputFilePath)
{
FILE *fp;
size_t length;
char *buffer;
unsigned int position = 0;
fp = fopen(inputFilePath, "rb");
if (!fp)
{
perror("Could not open file");
return EXIT_FAILURE;
}
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
if (length == -1L)
{
perror("Could not get length of the file");
fclose(fp);
return EXIT_FAILURE;
}
fseek(fp, 0L, SEEK_SET);
buffer = (char *)calloc(length, sizeof(char));
if (buffer == NULL)
{
fclose(fp);
perror("Could not allocate buffer using calloc");
return EXIT_FAILURE;
}
if (fread(buffer, sizeof(char), length, fp) != length)
{
perror("Could not read whole file");
fclose(fp);
free(buffer);
return EXIT_FAILURE;
}
fclose(fp);
fp = fopen(outputFilePath, "wb");
if (!fp)
{
perror("Could not open file");
return EXIT_FAILURE;
}
for (size_t i = 0; i < length; i = i + 2)
{
if (i % 16 == 0)
{
if (i != 0)
{
fprintf(fp, "\n");
}
fprintf(fp, "%08x", position);
}
if (length % 2 == 1 && position == length - 1)
{
fprintf(fp, " %x", buffer[i]);
position = position + 1;
}
else
{
fprintf(fp, " %x%x", buffer[i], buffer[i + 1]);
position = position + 2;
}
}
fprintf(fp, "\n");
fprintf(fp, "%08lx", length);
fclose(fp);
free(buffer);
return 0;
}
int main(int argc, char *argv[])
{
FILE *fp;
size_t length;
char *buffer;
int opt;
unsigned int position = 0;
char *inputFilePath = NULL;
char *outputFilePath = NULL;
if (argc < 2)
{
printUsage(argv[0]);
return EXIT_FAILURE;
}
inputFilePath = argv[argc - 1];
while ((opt = getopt(argc, argv, ":o:hv")) != -1)
{
switch (opt)
{
case 'o':
outputFilePath = optarg;
int success = saveToFile(inputFilePath, outputFilePath);
if (success != 0)
{
fprintf(stderr, "Could not save to file\n");
return success;
}
break;
case 'v':
printf("%s %s\n\n", PROGRAMNAME, VERSION);
exit(EXIT_SUCCESS);
break;
case 'h':
printUsage(argv[0]);
break;
default:
fprintf(stderr, "Please specify valid arguments");
printUsage(argv[0]);
break;
}
}
if (outputFilePath != NULL)
{
return EXIT_SUCCESS;
}
fp = fopen(inputFilePath, "rb");
if (!fp)
{
perror("Could not open file");
return EXIT_FAILURE;
}
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if (length == -1L)
{
perror("Could not get length of the file");
fclose(fp);
return EXIT_FAILURE;
}
buffer = (char *)calloc(length, sizeof(char));
if (buffer == NULL)
{
perror("Could not allocate buffer using calloc");
return EXIT_FAILURE;
}
if (fread(buffer, sizeof(char), length, fp) != length)
{
perror("Could not read whole file");
fclose(fp);
return EXIT_FAILURE;
}
fclose(fp);
for (size_t i = 0; i < length; i = i + 2)
{
if (i % 16 == 0)
{
if (i != 0)
{
printf("\n");
}
printf("%08x", position);
}
if (length % 2 == 1 && position == length - 1)
{
printf(" %x", buffer[i]);
position = position + 1;
}
else
{
printf(" %x%x", buffer[i], buffer[i + 1]);
position = position + 2;
}
}
printf("\n");
printf("%08lx", length);
free(buffer);
return 0;
}