VirtualBox

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

Last change on this file since 6468 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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