OVR: Show mirror of single eye only
[mercenary-reloaded.git] / src / mercenary / bin2hex.c
1 /* generates hex code from game binary
2  *
3  * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
4  * All Rights Reserved
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21
22 int main(int argc, char *argv[])
23 {
24         FILE *ifp, *ofp;
25         int a, b, c, d;
26         int n = 0;
27
28         if (argc <= 3) {
29                 fprintf(stderr, "Usage: %s <binary> <souce code> <array name>\n", argv[0]);
30                 return -1;
31         }
32
33         ifp = fopen(argv[1], "r");
34         if (!ifp) {
35                 fprintf(stderr, "Failed to open input file: %s\n", argv[1]);
36                 return -1;
37         }
38         ofp = fopen(argv[2], "w");
39         if (!ofp) {
40                 fprintf(stderr, "Failed to open output file: %s\n", argv[2]);
41                 return -1;
42         }
43
44         fprintf(ofp, "\n#include <stdint.h>\n");
45         fprintf(ofp, "\n/* Data is stored as 'big endian' and must be converted to host machine's endianess. */\n");
46         fprintf(ofp, "const uint32_t %s[] = {", argv[3]);
47         while ((a = fgetc(ifp)) != EOF) {
48                 b = fgetc(ifp);
49                 c = fgetc(ifp);
50                 d = fgetc(ifp);
51                 if (b == EOF || c == EOF || d == EOF) {
52                         fprintf(stderr, "Short read of input file. Input file's size must be a multiple of 4!\n");
53                         return -1;
54                 }
55                 if ((n % 32) == 0)
56                         fprintf(ofp, "\n\t");
57                 fprintf(ofp, "0x%02x%02x%02x%02x,", a, b, c, d);
58                 n += 4;
59         }
60         fprintf(ofp, "\n};\n\n");
61         fprintf(ofp, "int %s_size = %d;\n\n", argv[3], n);
62
63         fclose(ifp);
64         fclose(ofp);
65 }
66