1 | #!/usr/bin/perl |
---|
2 | ## ----------------------------------------------------------------------- |
---|
3 | ## |
---|
4 | ## Copyright 1998-2008 H. Peter Anvin - All Rights Reserved |
---|
5 | ## |
---|
6 | ## This program is free software; you can redistribute it and/or modify |
---|
7 | ## it under the terms of the GNU General Public License as published by |
---|
8 | ## the Free Software Foundation, Inc., 53 Temple Place Ste 330, |
---|
9 | ## Boston MA 02111-1307, USA; either version 2 of the License, or |
---|
10 | ## (at your option) any later version; incorporated herein by reference. |
---|
11 | ## |
---|
12 | ## ----------------------------------------------------------------------- |
---|
13 | |
---|
14 | # |
---|
15 | # bin2c.pl: binary file to C source converter |
---|
16 | # |
---|
17 | |
---|
18 | eval { use bytes; }; |
---|
19 | eval { binmode STDIN; }; |
---|
20 | |
---|
21 | ($table_name, $pad) = @ARGV; |
---|
22 | |
---|
23 | if ( !defined($table_name) ) { |
---|
24 | print STDERR "Usage: $0 table_name [pad] < input_file > output_file\n"; |
---|
25 | exit 1; |
---|
26 | } |
---|
27 | |
---|
28 | $pad = 1 if ($pad < 1); |
---|
29 | |
---|
30 | printf "unsigned char %s[] = {\n", $table_name; |
---|
31 | |
---|
32 | $pos = 0; |
---|
33 | $linelen = 8; |
---|
34 | |
---|
35 | $total_len = 0; |
---|
36 | |
---|
37 | while ( ($n = read(STDIN, $data, 4096)) > 0 ) { |
---|
38 | $total_len += $n; |
---|
39 | for ( $i = 0 ; $i < $n ; $i++ ) { |
---|
40 | $byte = substr($data, $i, 1); |
---|
41 | if ( $pos >= $linelen ) { |
---|
42 | print ",\n\t"; |
---|
43 | $pos = 0; |
---|
44 | } elsif ( $pos > 0 ) { |
---|
45 | print ", "; |
---|
46 | } else { |
---|
47 | print "\t"; |
---|
48 | } |
---|
49 | printf("0x%02x", unpack("C", $byte)); |
---|
50 | $pos++; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | $align = $total_len % $pad; |
---|
55 | if ($align != 0) { |
---|
56 | $n = $pad - $align; |
---|
57 | $total_len += $n; |
---|
58 | for ( $i = 0 ; $i < $n ; $i++ ) { |
---|
59 | if ( $pos >= $linelen ) { |
---|
60 | print ",\n\t"; |
---|
61 | $pos = 0; |
---|
62 | } elsif ( $pos > 0 ) { |
---|
63 | print ", "; |
---|
64 | } else { |
---|
65 | print "\t"; |
---|
66 | } |
---|
67 | print '0x00'; |
---|
68 | $pos++; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | printf "\n};\n\nconst unsigned int %s_len = %u;\n", $table_name, $total_len; |
---|
73 | |
---|
74 | @st = stat STDIN; |
---|
75 | |
---|
76 | printf "\nconst int %s_mtime = %d;\n", $table_name, $st[9]; |
---|
77 | |
---|
78 | exit 0; |
---|