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