bin2c.c revision e64031e20c39650a7bc902a3e1aba613b9415dee
/* $Id$ */
/** @file
* bin2c - Binary 2 C Structure Converter.
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**
* File size.
*
* @returns file size in bytes.
* @returns 0 on failure.
* @param pFile File to size.
*/
{
long cbFile;
if ( Pos >= 0
{
if ( cbFile >= 0
return cbFile;
}
return 0;
}
{
"Syntax: %s [options] <arrayname> <binaryfile> <outname>\n"
" -min <n> check if <binaryfile> is not smaller than <n>KB\n"
" -max <n> check if <binaryfile> is not bigger than <n>KB\n"
" -mask <n> check if size of binaryfile is <n>-aligned\n"
" -width <n> number of bytes per line (default: 16)\n"
" -break <n> break every <n> lines (default: -1)\n"
" -ascii show ASCII representation of binary as comment\n",
argv0);
return 1;
}
{
int iArg;
int fAscii = 0;
int fExport = 0;
long iBreakEvery = -1;
unsigned char abLine[32];
if (argc < 2)
{
{
}
{
}
{
}
{
fAscii = 1;
}
{
fExport = 1;
}
{
{
return 1;
}
}
{
{
return 1;
}
}
break;
else
{
}
}
if (!pFileIn)
{
return 1;
}
if (!pFileOut)
{
return 1;
}
"/*\n"
" * This file was automatically generated\n"
" * from %s\n"
" * by %s.\n"
" */\n"
"\n"
"\n"
"%sconst unsigned char%s g_ab%s[] =\n"
"{\n",
/* check size restrictions */
else
{
/* the binary data */
off = 0;
{
size_t j;
if ( iBreakEvery > 0
&& off
for (j = 0; j < cbRead; j++)
for (; j < cbLine; j++)
if (fAscii)
{
for (j = 0; j < cbRead; j++)
for (; j < cbLine; j++)
}
}
/* check for errors */
else
{
/* no errors, finish the structure. */
"};\n"
"\n"
"%sconst unsigned%s g_cb%s = sizeof(g_ab%s);\n"
"/* end of file */\n",
/* flush output and check for error. */
else
rc = 0; /* success! */
}
}
/* cleanup, delete the output file on failure. */
if (rc)
return rc;
}