From: Pete Ashdown on
I've been working on a project where we've reverse engineered some Windows
software for controlling a display over ethernet. There are several control
packets that are sent that we've extract the contents from, but we really
don't care to reverse engineer the meaning of the contents out. They just
need to be sent before the display data is sent.

Somewhere in my addled memory, I remember seeing a way to include large
amounts of data in a C program for bitmaps and such. I'll be damned if I can
find any reference as to how to do that now. The second question is whether
there is a simple bin -> .h Linux program that will do said conversion.

Thanks in advance.

--


Pete Ashdown pashdown(a)xmission.com http://pashdown.org Salt Lake City, Utah
XMission Internet Access - http://xmission.com - Voice: 801 539 0852
From: Richard Heathfield on
In <h8lvu7$5s8$1(a)news.xmission.com>, Pete Ashdown wrote:

> I've been working on a project where we've reverse engineered some
> Windows
> software for controlling a display over ethernet. There are several
> control packets that are sent that we've extract the contents from,
> but we really
> don't care to reverse engineer the meaning of the contents out.
> They just need to be sent before the display data is sent.
>
> Somewhere in my addled memory, I remember seeing a way to include
> large
> amounts of data in a C program for bitmaps and such. I'll be damned
> if I can
> find any reference as to how to do that now. The second question is
> whether there is a simple bin -> .h Linux program that will do said
> conversion.

The easiest way is not to bother - just stick 'em in a file and read
the file at runtime. But if you really want them as data, it's easy
enough. The following code has inadequate error reporting. Add it
yourself if you want it. Also, it's not amazingly robust. If you try
to trick it, you will probably succeed. And because it's a silly time
in the morning, I haven't worried too much about the final comma.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFLEN 256
#define BYTES_PER_LINE 8
#define MAX_FILENAME 256

int CreateHeaderFromBinary(const char *binary,
const char *header,
const char *array)
{
char headername[MAX_FILENAME] = {0};
char headertail[] = ".h";
int rc = 0;
size_t hlen = strlen(header);

FILE *fpi = fopen(binary, "rb");
if(hlen + sizeof headertail > sizeof headername)
{
hlen = sizeof headername - sizeof headertail;
}
sprintf(headername, "%.*s%s", (int)hlen, header, headertail);

if(fpi != NULL)
{
FILE *fpo = fopen(headername, "w");
if(fpo != NULL)
{
unsigned char buf[BUFLEN] = {0};
size_t bytes = 0;
unsigned long bytecount = 0;
size_t linelen = 0;
fprintf(fpo, "#ifndef H_%s\n", header);
fprintf(fpo, "#define H_%s 1\n\n", header);
fprintf(fpo, "static const unsigned char %s[] =\n", array);
fprintf(fpo, "{\n ");

while((bytes = fread(buf, 1, sizeof buf, fpi)) > 0)
{
size_t i = 0;

while(i++ < bytes)
{
++bytecount;
fprintf(fpo, " 0x%02X,", buf[i]);
if(++linelen == BYTES_PER_LINE)
{
fprintf(fpo, "\n ");
linelen = 0;
}
}
}
fprintf(fpo, "\n};\n");
fprintf(fpo, "#endif\n");

if(fclose(fpo) != 0)
{
rc = 3;
}
else
{
fprintf(stderr, "%lu byte%s processed.\n",
bytecount, (bytecount == 1) ? "" : "s");
}
}
else
{
rc = 2;
}
fclose(fpi);
}
else
{
rc = 1;
}
return rc;
}

int main(int argc, char **argv)
{
int rc = EXIT_SUCCESS;

if(argc > 3)
{
int result = CreateHeaderFromBinary(argv[1], argv[2], argv[3]);
if(0 != result)
{
fprintf(stderr, "Error %d\n", result);
rc = EXIT_FAILURE;
}
}
else
{
fprintf(stderr, "Insufficient arguments. You should argue
more.\n");
fprintf(stderr, "arg1 = binary's filename\n");
fprintf(stderr, "arg2 = header's filename (no h! I'll add
that.)\n");
fprintf(stderr, "arg3 = array name\n");
rc = EXIT_FAILURE;
}

return rc;
}

Sample output:

#ifndef H_demo
#define H_demo 1

static const unsigned char demo[] =
{
0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00,
0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00,
0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x20, 0x08,
0x03, 0x00, 0x00, 0x00, 0x21, 0x40, 0xED, 0xD5,
0x00, 0x00, 0x00, 0x60, 0x50, 0x4C, 0x54, 0x45,
0xCE, 0xCE, 0xCE, 0xFF, 0xFF, 0xFF, 0xA5, 0xA5,
0xA5, 0x84, 0x84, 0x84, 0x73, 0x73, 0x73, 0x6B,

/* lots of lines snipped */

0x5D, 0xD0, 0x9F, 0x40, 0xF0, 0xD2, 0x4B, 0x2F,
0xFD, 0x65, 0xFA, 0x0F, 0x7B, 0x35, 0xB6, 0x8C,
0xAC, 0x93, 0x7E, 0x40, 0x00, 0x00, 0x00, 0x00,
0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
0x4A,
};
#endif

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within