VirtualBox

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

Last change on this file since 10390 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

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