Harald Welte | 5708dbf | 2008-07-09 22:30:30 +0800 | [diff] [blame] | 1 | /* 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 Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Harald Welte | 5708dbf | 2008-07-09 22:30:30 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <stdio.h> |
Tom Rini | 286856c | 2016-03-15 12:49:12 -0400 | [diff] [blame^] | 11 | #include <unistd.h> |
Harald Welte | 5708dbf | 2008-07-09 22:30:30 +0800 | [diff] [blame] | 12 | |
| 13 | int 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 | } |