VirtualBox

source: vbox/trunk/src/bldprogs/bin2c.c@ 3904

Last change on this file since 3904 was 3407, checked in by vboxsync, 17 years ago

build fix if the BIOS code contains /* or */ :)

  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1/* $Id: bin2c.c 3407 2007-07-04 08:00:45Z 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 */
40static 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
56int 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
110syntax_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 /* be careful with '/' prefixed/followed by a '*'! */
179 fprintf(pFileOut, "%c",
180 isprint(abLine[i]) && abLine[i] != '/' ? abLine[i] : '.');
181 for (; i < sizeof(abLine); i++)
182 fprintf(pFileOut, " ");
183 fprintf(pFileOut, " */");
184 }
185 fprintf(pFileOut, "\n");
186
187 off += cbRead;
188 }
189
190 /* check for errors */
191 if (ferror(pFileIn) && !feof(pFileIn))
192 {
193 fprintf(stderr, "%s: read error\n", argv[0]);
194 goto error;
195 }
196 if (off != cbBin)
197 {
198 fprintf(stderr, "%s: read error off=%ld cbBin=%ld\n", argv[0], (long)off, (long)cbBin);
199 goto error;
200 }
201
202 /* finish the structure. */
203 fprintf(pFileOut,
204 "};\n"
205 "\n"
206 "%sconst unsigned%s g_cb%s = sizeof(g_ab%s);\n"
207 "/* end of file */\n",
208 fExport ? "DECLEXPORT(" : "", fExport ? ")" : "", argv[i], argv[i]);
209 fclose(pFileIn);
210
211 /* flush output and check for error. */
212 fflush(pFileOut);
213 if (ferror(pFileOut))
214 {
215 fprintf(stderr, "%s: write error\n", argv[0]);
216 goto error;
217 }
218 fclose(pFileOut);
219
220 return 0;
221
222error:
223 fclose(pFileOut);
224 remove(argv[i+2]);
225 return 1;
226}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette