doc/pxechn.txt: add -S, examples, clarify
[syslinux.git] / libinstaller / bin2c.pl
blob07c11ddbdfc68b702a9418229737566b9c47f149
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.
12 ## -----------------------------------------------------------------------
15 # bin2c.pl: binary file to C source converter
18 eval { use bytes; };
19 eval { binmode STDIN; };
21 ($table_name, $pad) = @ARGV;
23 if ( !defined($table_name) ) {
24 print STDERR "Usage: $0 table_name [pad] < input_file > output_file\n";
25 exit 1;
28 $pad = 1 if ($pad < 1);
30 printf "unsigned char %s[] = {\n", $table_name;
32 $pos = 0;
33 $linelen = 8;
35 $total_len = 0;
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";
49 printf("0x%02x", unpack("C", $byte));
50 $pos++;
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";
67 print '0x00';
68 $pos++;
72 printf "\n};\n\nconst unsigned int %s_len = %u;\n", $table_name, $total_len;
74 @st = stat STDIN;
76 printf "\nconst int %s_mtime = %d;\n", $table_name, $st[9];
78 exit 0;