VirtualBox

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

Last change on this file since 37043 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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