VirtualBox

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

Last change on this file since 63498 was 63498, checked in by vboxsync, 8 years ago

Better way of dealing with unused sizes in bin2c output.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 8.6 KB
Line 
1/* $Id: bin2c.c 63498 2016-08-15 20:41:37Z vboxsync $ */
2/** @file
3 * bin2c - Binary 2 C Structure Converter.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <ctype.h>
23#include <stdio.h>
24#include <string.h>
25#include <stdlib.h>
26#include <sys/types.h>
27
28
29/**
30 * File size.
31 *
32 * @returns file size in bytes.
33 * @returns 0 on failure.
34 * @param pFile File to size.
35 */
36static size_t fsize(FILE *pFile)
37{
38 long cbFile;
39 off_t Pos = ftell(pFile);
40 if ( Pos >= 0
41 && !fseek(pFile, 0, SEEK_END))
42 {
43 cbFile = ftell(pFile);
44 if ( cbFile >= 0
45 && !fseek(pFile, 0, SEEK_SET))
46 return cbFile;
47 }
48 return 0;
49}
50
51static int usage(const char *argv0)
52{
53 fprintf(stderr,
54 "Syntax: %s [options] <arrayname> <binaryfile> <outname>\n"
55 " --min <n> check if <binaryfile> is not smaller than <n>KB\n"
56 " --max <n> check if <binaryfile> is not bigger than <n>KB\n"
57 " --mask <n> check if size of binaryfile is <n>-aligned\n"
58 " --width <n> number of bytes per line (default: 16)\n"
59 " --break <n> break every <n> lines (default: -1)\n"
60 " --ascii show ASCII representation of binary as comment\n"
61 " --export emit DECLEXPORT\n"
62 " --append append to the output file (default: truncate)\n"
63 " --no-size Skip the size.\n"
64 " --static Static data scope.\n"
65 , argv0);
66
67 return 1;
68}
69
70int main(int argc, char *argv[])
71{
72 FILE *pFileIn;
73 FILE *pFileOut;
74 int iArg;
75 size_t cbMin = 0;
76 size_t cbMax = ~0U;
77 size_t uMask = 0;
78 int fAscii = 0;
79 int fAppend = 0;
80 int fExport = 0;
81 int fNoSize = 0;
82 int fStatic = 0;
83 long iBreakEvery = -1;
84 unsigned char abLine[32];
85 size_t cbLine = 16;
86 size_t off;
87 size_t cbRead;
88 size_t cbBin;
89 int rc = 1; /* assume the worst... */
90
91 if (argc < 2)
92 return usage(argv[0]);
93
94 for (iArg = 1; iArg < argc; iArg++)
95 {
96 if (!strcmp(argv[iArg], "--min") || !strcmp(argv[iArg], "-min"))
97 {
98 if (++iArg >= argc)
99 return usage(argv[0]);
100 cbMin = 1024 * strtoul(argv[iArg], NULL, 0);
101 }
102 else if (!strcmp(argv[iArg], "--max") || !strcmp(argv[iArg], "-max"))
103 {
104 if (++iArg >= argc)
105 return usage(argv[0]);
106 cbMax = 1024 * strtoul(argv[iArg], NULL, 0);
107 }
108 else if (!strcmp(argv[iArg], "--mask") || !strcmp(argv[iArg], "-mask"))
109 {
110 if (++iArg >= argc)
111 return usage(argv[0]);
112 uMask = strtoul(argv[iArg], NULL, 0);
113 }
114 else if (!strcmp(argv[iArg], "--ascii") || !strcmp(argv[iArg], "-ascii"))
115 fAscii = 1;
116 else if (!strcmp(argv[iArg], "--append"))
117 fAppend = 1;
118 else if (!strcmp(argv[iArg], "--export") || !strcmp(argv[iArg], "-export"))
119 fExport = 1;
120 else if (!strcmp(argv[iArg], "--no-size"))
121 fNoSize = 1;
122 else if (!strcmp(argv[iArg], "--static"))
123 fStatic = 1;
124 else if (!strcmp(argv[iArg], "--width") || !strcmp(argv[iArg], "-width"))
125 {
126 if (++iArg >= argc)
127 return usage(argv[0]);
128 cbLine = strtoul(argv[iArg], NULL, 0);
129 if (cbLine == 0 || cbLine > sizeof(abLine))
130 {
131 fprintf(stderr, "%s: '%s' is too wide, max %u\n",
132 argv[0], argv[iArg], (unsigned)sizeof(abLine));
133 return 1;
134 }
135 }
136 else if (!strcmp(argv[iArg], "--break") || !strcmp(argv[iArg], "-break"))
137 {
138 if (++iArg >= argc)
139 return usage(argv[0]);
140 iBreakEvery = strtol(argv[iArg], NULL, 0);
141 if (iBreakEvery <= 0 && iBreakEvery != -1)
142 {
143 fprintf(stderr, "%s: -break value '%s' is not >= 1 or -1.\n",
144 argv[0], argv[iArg]);
145 return 1;
146 }
147 }
148 else if (iArg == argc - 3)
149 break;
150 else
151 {
152 fprintf(stderr, "%s: syntax error: Unknown argument '%s'\n",
153 argv[0], argv[iArg]);
154 return usage(argv[0]);
155 }
156 }
157
158 pFileIn = fopen(argv[iArg+1], "rb");
159 if (!pFileIn)
160 {
161 fprintf(stderr, "Error: failed to open input file '%s'!\n", argv[iArg+1]);
162 return 1;
163 }
164
165 pFileOut = fopen(argv[iArg+2], fAppend ? "a" : "w"); /* no b! */
166 if (!pFileOut)
167 {
168 fprintf(stderr, "Error: failed to open output file '%s'!\n", argv[iArg+2]);
169 fclose(pFileIn);
170 return 1;
171 }
172
173 cbBin = fsize(pFileIn);
174
175 fprintf(pFileOut,
176 "/*\n"
177 " * This file was automatically generated\n"
178 " * from %s\n"
179 " * by %s.\n"
180 " */\n"
181 "\n"
182 "#include <iprt/cdefs.h>\n"
183 "\n"
184 "%sconst unsigned char%s g_ab%s[] =\n"
185 "{\n",
186 argv[iArg+1], argv[0], fStatic ? "static " : fExport ? "DECLEXPORT(" : "", !fStatic && fExport ? ")" : "", argv[iArg]);
187
188 /* check size restrictions */
189 if (uMask && (cbBin & uMask))
190 fprintf(stderr, "%s: size=%ld - Not aligned!\n", argv[0], (long)cbBin);
191 else if (cbBin < cbMin || cbBin > cbMax)
192 fprintf(stderr, "%s: size=%ld - Not %ld-%ldb in size!\n",
193 argv[0], (long)cbBin, (long)cbMin, (long)cbMax);
194 else
195 {
196 /* the binary data */
197 off = 0;
198 while ((cbRead = fread(&abLine[0], 1, cbLine, pFileIn)) > 0)
199 {
200 size_t j;
201
202 if ( iBreakEvery > 0
203 && off
204 && (off / cbLine) % iBreakEvery == 0)
205 fprintf(pFileOut, "\n");
206
207 fprintf(pFileOut, " ");
208 for (j = 0; j < cbRead; j++)
209 fprintf(pFileOut, " 0x%02x,", abLine[j]);
210 for (; j < cbLine; j++)
211 fprintf(pFileOut, " ");
212 if (fAscii)
213 {
214 fprintf(pFileOut, " /* 0x%08lx: ", (long)off);
215 for (j = 0; j < cbRead; j++)
216 /* be careful with '/' prefixed/followed by a '*'! */
217 fprintf(pFileOut, "%c",
218 isprint(abLine[j]) && abLine[j] != '/' ? abLine[j] : '.');
219 for (; j < cbLine; j++)
220 fprintf(pFileOut, " ");
221 fprintf(pFileOut, " */");
222 }
223 fprintf(pFileOut, "\n");
224
225 off += cbRead;
226 }
227
228 /* check for errors */
229 if (ferror(pFileIn) && !feof(pFileIn))
230 fprintf(stderr, "%s: read error\n", argv[0]);
231 else if (off != cbBin)
232 fprintf(stderr, "%s: read error off=%ld cbBin=%ld\n", argv[0], (long)off, (long)cbBin);
233 else
234 {
235 /* no errors, finish the structure. */
236 fprintf(pFileOut,
237 "};\n");
238
239 if (!fNoSize)
240 fprintf(pFileOut,
241 "\n"
242 "%sconst unsigned%s g_cb%s = sizeof(g_ab%s);\n",
243 fExport ? "DECLEXPORT(" : "", fExport ? ")" : "", argv[iArg], argv[iArg]);
244
245 fprintf(pFileOut, "/* end of file */\n");
246
247 /* flush output and check for error. */
248 fflush(pFileOut);
249 if (ferror(pFileOut))
250 fprintf(stderr, "%s: write error\n", argv[0]);
251 else
252 rc = 0; /* success! */
253 }
254 }
255
256 /* cleanup, delete the output file on failure. */
257 fclose(pFileOut);
258 fclose(pFileIn);
259 if (rc)
260 remove(argv[iArg+2]);
261
262 return rc;
263}
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