Harald Welte | 0a823aa | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Harald Welte | 0a823aa | 2008-07-09 22:30:30 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <stdio.h> |
| 11 | |
| 12 | int main(int argc, char **argv) |
| 13 | { |
| 14 | if (argc < 2) { |
| 15 | fprintf(stderr, "%s needs one argument: the structure name\n", |
| 16 | argv[0]); |
| 17 | exit(1); |
| 18 | } |
| 19 | |
| 20 | printf("/* bin2header output - automatically generated */\n"); |
| 21 | printf("unsigned char %s[] = {\n", argv[1]); |
| 22 | |
| 23 | while (1) { |
| 24 | int i, nread; |
| 25 | unsigned char buf[10]; |
| 26 | nread = read(0, buf, sizeof(buf)); |
| 27 | if (nread <= 0) |
| 28 | break; |
| 29 | |
| 30 | printf("\t"); |
| 31 | for (i = 0; i < nread - 1; i++) |
| 32 | printf("0x%02x, ", buf[i]); |
| 33 | |
| 34 | printf("0x%02x,\n", buf[nread-1]); |
| 35 | } |
| 36 | |
| 37 | printf("};\n"); |
| 38 | |
| 39 | exit(0); |
| 40 | } |