blob: 6c2c23f624efd2a71b612af3f0aee63f342881e0 [file] [log] [blame]
Harald Welte5708dbf2008-07-09 22:30:30 +08001/* bin2header.c - program to convert binary file into a C structure
2 * definition to be included in a header file.
3 *
4 * (C) Copyright 2008 by Harald Welte <laforge@openmoko.org>
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Harald Welte5708dbf2008-07-09 22:30:30 +08007 */
8
9#include <stdlib.h>
10#include <stdio.h>
Tom Rini286856c2016-03-15 12:49:12 -040011#include <unistd.h>
Harald Welte5708dbf2008-07-09 22:30:30 +080012
13int main(int argc, char **argv)
14{
15 if (argc < 2) {
16 fprintf(stderr, "%s needs one argument: the structure name\n",
17 argv[0]);
18 exit(1);
19 }
20
21 printf("/* bin2header output - automatically generated */\n");
22 printf("unsigned char %s[] = {\n", argv[1]);
23
24 while (1) {
25 int i, nread;
26 unsigned char buf[10];
27 nread = read(0, buf, sizeof(buf));
28 if (nread <= 0)
29 break;
30
31 printf("\t");
32 for (i = 0; i < nread - 1; i++)
33 printf("0x%02x, ", buf[i]);
34
35 printf("0x%02x,\n", buf[nread-1]);
36 }
37
38 printf("};\n");
39
40 exit(0);
41}