1 |
/* |
2 |
* Copyright (C) 2003-2004 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* |
13 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
14 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
16 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
17 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
18 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
19 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
20 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
21 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
22 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
23 |
* SUCH DAMAGE. |
24 |
* |
25 |
* |
26 |
* gxemul: $Id: Xconv_raw_to_c.c,v 1.2 2004/08/03 00:31:59 debug Exp $ |
27 |
* $ycx2: Xconv_raw_to_c.c,v 1.2 2003/11/25 01:20:24 debug Exp $ |
28 |
* |
29 |
* Convert a raw binary file into an unsigned char array, usable from C. |
30 |
* Example: |
31 |
* |
32 |
* ./Xconv_raw_to_c somefile.raw hello |
33 |
* |
34 |
* gives the following output: |
35 |
* |
36 |
* unsigned char hello[] = { |
37 |
* 35,10,35,32,32,77,97,107,101,102, |
38 |
* 105,108,101,32,105,115,32,103,101,110, |
39 |
* 101,114,97,116,101,100,32,102,114,111, |
40 |
* 109,32,77,97,107,101,102,105,108,101, |
41 |
* 46,115,107,101,108,46,32,68,111,110, |
42 |
* ... |
43 |
* } |
44 |
* |
45 |
* This is useful to include things such as trampoline code, which needs |
46 |
* to be copied to some specific address at runtime, into a kernel image. |
47 |
*/ |
48 |
|
49 |
#include <stdio.h> |
50 |
#include <stdlib.h> |
51 |
|
52 |
|
53 |
int main(int argc, char *argv[]) |
54 |
{ |
55 |
FILE *f; |
56 |
unsigned char buf[50000]; |
57 |
int len, i; |
58 |
|
59 |
if (argc < 3) { |
60 |
fprintf(stderr, "syntax: %s input_binary symbol_name\n", |
61 |
argv[0]); |
62 |
fprintf(stderr, "Output is written to stdout.\n"); |
63 |
exit(1); |
64 |
} |
65 |
|
66 |
f = fopen(argv[1], "r"); |
67 |
printf("unsigned char %s[] = {", argv[2]); |
68 |
len = fread(&buf, 1, sizeof(buf), f); |
69 |
|
70 |
for (i=0; i<len; i++) { |
71 |
if ((i%10)==0) |
72 |
printf("\n\t"); |
73 |
printf("%u", buf[i]); |
74 |
if (i < len-1) |
75 |
printf(","); |
76 |
} |
77 |
printf("\n};\n"); |
78 |
|
79 |
return 0; |
80 |
} |
81 |
|