1 | /* $Id: bin2c.c 2981 2007-06-01 16:01:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PC-BIOS - Binary 2 C Structure Converter.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <ctype.h>
|
---|
27 | #include <stdio.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <stdlib.h>
|
---|
30 | #include <sys/types.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * File size.
|
---|
35 | *
|
---|
36 | * @returns file size in bytes.
|
---|
37 | * @returns 0 on failure.
|
---|
38 | * @param pFile File to size.
|
---|
39 | */
|
---|
40 | static size_t fsize(FILE *pFile)
|
---|
41 | {
|
---|
42 | long cbFile;
|
---|
43 | off_t Pos = ftell(pFile);
|
---|
44 | if ( Pos >= 0
|
---|
45 | && !fseek(pFile, 0, SEEK_END))
|
---|
46 | {
|
---|
47 | cbFile = ftell(pFile);
|
---|
48 | if ( cbFile >= 0
|
---|
49 | && !fseek(pFile, 0, SEEK_SET))
|
---|
50 | return cbFile;
|
---|
51 | }
|
---|
52 | return 0;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | int main(int argc, char *argv[])
|
---|
57 | {
|
---|
58 | FILE *pFileIn;
|
---|
59 | FILE *pFileOut;
|
---|
60 | int i;
|
---|
61 | size_t cbMin = 0;
|
---|
62 | size_t cbMax = ~0U;
|
---|
63 | size_t uMask = 0;
|
---|
64 | int fAscii = 0;
|
---|
65 | int fExport = 0;
|
---|
66 | unsigned char abLine[16];
|
---|
67 | size_t off;
|
---|
68 | size_t cbRead;
|
---|
69 | size_t cbBin;
|
---|
70 |
|
---|
71 | if (argc < 2)
|
---|
72 | goto syntax_error;
|
---|
73 |
|
---|
74 | for (i=1; i<argc; i++)
|
---|
75 | {
|
---|
76 | if (!strcmp(argv[i], "-min"))
|
---|
77 | {
|
---|
78 | if (++i>=argc)
|
---|
79 | goto syntax_error;
|
---|
80 | cbMin = 1024 * strtoul(argv[i], NULL, 0);
|
---|
81 | continue;
|
---|
82 | }
|
---|
83 | else if (!strcmp(argv[i], "-max"))
|
---|
84 | {
|
---|
85 | if (++i>=argc)
|
---|
86 | goto syntax_error;
|
---|
87 | cbMax = 1024 * strtoul(argv[i], NULL, 0);
|
---|
88 | continue;
|
---|
89 | }
|
---|
90 | else if (!strcmp(argv[i], "-mask"))
|
---|
91 | {
|
---|
92 | if (++i>=argc)
|
---|
93 | goto syntax_error;
|
---|
94 | uMask = strtoul(argv[i], NULL, 0);
|
---|
95 | continue;
|
---|
96 | }
|
---|
97 | else if (!strcmp(argv[i], "-ascii"))
|
---|
98 | {
|
---|
99 | fAscii = 1;
|
---|
100 | continue;
|
---|
101 | }
|
---|
102 | else if (!strcmp(argv[i], "-export"))
|
---|
103 | {
|
---|
104 | fExport = 1;
|
---|
105 | continue;
|
---|
106 | }
|
---|
107 | else if (i==argc-3)
|
---|
108 | break;
|
---|
109 |
|
---|
110 | syntax_error:
|
---|
111 | fprintf(stderr,
|
---|
112 | "Syntax: %s [options] <arrayname> <binaryfile> <outname>\n"
|
---|
113 | " -min <n> check if <binaryfile> is not smaller than <n>KB\n"
|
---|
114 | " -max <n> check if <binaryfile> is not bigger than <n>KB\n"
|
---|
115 | " -mask <n> check if size of binaryfile is <n>-aligned\n"
|
---|
116 | " -ascii show ASCII representation of binary as comment\n",
|
---|
117 | argv[0]);
|
---|
118 | return 1;
|
---|
119 | }
|
---|
120 |
|
---|
121 | pFileIn = fopen(argv[i+1], "rb");
|
---|
122 | if (!pFileIn)
|
---|
123 | {
|
---|
124 | fprintf(stderr, "Error: failed to open input file '%s'!\n", argv[i+1]);
|
---|
125 | return 1;
|
---|
126 | }
|
---|
127 |
|
---|
128 | pFileOut = fopen(argv[i+2], "wb");
|
---|
129 | if (!pFileOut)
|
---|
130 | {
|
---|
131 | fprintf(stderr, "Error: failed to open output file '%s'!\n", argv[i+2]);
|
---|
132 | fclose(pFileIn);
|
---|
133 | return 1;
|
---|
134 | }
|
---|
135 |
|
---|
136 | cbBin = fsize(pFileIn);
|
---|
137 |
|
---|
138 | fprintf(pFileOut,
|
---|
139 | "/*\n"
|
---|
140 | " * This file was automatically generated\n"
|
---|
141 | " * from %s by\n"
|
---|
142 | " * by %s.\n"
|
---|
143 | " */\n"
|
---|
144 | "\n"
|
---|
145 | "#include <iprt/cdefs.h>\n"
|
---|
146 | "\n"
|
---|
147 | "%sconst unsigned char%s g_ab%s[] =\n"
|
---|
148 | "{\n",
|
---|
149 | argv[i+1], argv[0], fExport ? "DECLEXPORT(" : "", fExport ? ")" : "", argv[i]);
|
---|
150 |
|
---|
151 | /* check size restrictions */
|
---|
152 | if (uMask && (cbBin & uMask))
|
---|
153 | {
|
---|
154 | fprintf(stderr, "%s: size=%ld - Not aligned!\n", argv[0], (long)cbBin);
|
---|
155 | return 1;
|
---|
156 | }
|
---|
157 | if (cbBin < cbMin || cbBin > cbMax)
|
---|
158 | {
|
---|
159 | fprintf(stderr, "%s: size=%ld - Not %ld-%ldb in size!\n",
|
---|
160 | argv[0], (long)cbBin, (long)cbMin, (long)cbMax);
|
---|
161 | return 1;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* the binary data */
|
---|
165 | off = 0;
|
---|
166 | while ((cbRead = fread(&abLine[0], 1, sizeof(abLine), pFileIn)) > 0)
|
---|
167 | {
|
---|
168 | size_t i;
|
---|
169 | fprintf(pFileOut, " ");
|
---|
170 | for (i = 0; i < cbRead; i++)
|
---|
171 | fprintf(pFileOut, " 0x%02x,", abLine[i]);
|
---|
172 | for (; i < sizeof(abLine); i++)
|
---|
173 | fprintf(pFileOut, " ");
|
---|
174 | if (fAscii)
|
---|
175 | {
|
---|
176 | fprintf(pFileOut, " /* 0x%08lx: ", (long)off);
|
---|
177 | for (i = 0; i < cbRead; i++)
|
---|
178 | fprintf(pFileOut, "%c", isprint(abLine[i]) ? abLine[i] : '.');
|
---|
179 | for (; i < sizeof(abLine); i++)
|
---|
180 | fprintf(pFileOut, " ");
|
---|
181 | fprintf(pFileOut, " */");
|
---|
182 | }
|
---|
183 | fprintf(pFileOut, "\n");
|
---|
184 |
|
---|
185 | off += cbRead;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /* check for errors */
|
---|
189 | if (ferror(pFileIn) && !feof(pFileIn))
|
---|
190 | {
|
---|
191 | fprintf(stderr, "%s: read error\n", argv[0]);
|
---|
192 | goto error;
|
---|
193 | }
|
---|
194 | if (off != cbBin)
|
---|
195 | {
|
---|
196 | fprintf(stderr, "%s: read error off=%ld cbBin=%ld\n", argv[0], (long)off, (long)cbBin);
|
---|
197 | goto error;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /* finish the structure. */
|
---|
201 | fprintf(pFileOut,
|
---|
202 | "};\n"
|
---|
203 | "\n"
|
---|
204 | "%sconst unsigned%s g_cb%s = sizeof(g_ab%s);\n"
|
---|
205 | "/* end of file */\n",
|
---|
206 | fExport ? "DECLEXPORT(" : "", fExport ? ")" : "", argv[i], argv[i]);
|
---|
207 | fclose(pFileIn);
|
---|
208 |
|
---|
209 | /* flush output and check for error. */
|
---|
210 | fflush(pFileOut);
|
---|
211 | if (ferror(pFileOut))
|
---|
212 | {
|
---|
213 | fprintf(stderr, "%s: write error\n", argv[0]);
|
---|
214 | goto error;
|
---|
215 | }
|
---|
216 | fclose(pFileOut);
|
---|
217 |
|
---|
218 | return 0;
|
---|
219 |
|
---|
220 | error:
|
---|
221 | fclose(pFileOut);
|
---|
222 | remove(argv[i+2]);
|
---|
223 | return 1;
|
---|
224 | }
|
---|