version 1.1.1.1, 2005/02/21 07:07:13
|
version 1.4, 2005/02/25 22:55:23
|
|
|
#!/usr/bin/perl | #!/usr/bin/perl |
# hexify.pl |
# chexify.pl |
# Takes a list of filenames on stdin and create a C data array for each one. | # Takes a list of filenames on stdin and create a C data array for each one. |
# Copyright Dan Kegel 2005 |
# Copyright Dan Kegel, Rizwan Kassim 2005 |
# Licensed under LGPL | # Licensed under LGPL |
| |
# no input record separator |
|
undef $/; |
use English; |
|
use strict; |
|
|
|
|
|
my $file; #each of the files we are working on |
|
my $varname; #file name converted to variable name |
|
my $buffer; #buffer to store the hexified contents of a file till output |
|
|
|
# no input record separator to just slurp everything |
|
undef $INPUT_RECORD_SEPARATOR; |
| |
foreach $file (@ARGV) { | foreach $file (@ARGV) { |
open FILE, $file || die; |
open(FILE, $file) or die("Can't open $file : $OS_ERROR"); |
binmode FILE; | binmode FILE; |
| |
# munge filename into C variable name | # munge filename into C variable name |
$varname = $file; | $varname = $file; |
$varname =~ s/\./_/; | $varname =~ s/\./_/; |
| |
|
#name of the file |
print "const static char name_$varname"."[] = \"$file\";\n"; | print "const static char name_$varname"."[] = \"$file\";\n"; |
|
#hex contents of the file |
|
#BUG:have to get rid of the last comma |
print "const static char file_$varname"."[] = {\n"; | print "const static char file_$varname"."[] = {\n"; |
while (read(FILE, $buffer, 16)) { | while (read(FILE, $buffer, 16)) { |
$buffer =~ s/(.|\n)/'0x' . unpack('H2', $1).', '/ge; | $buffer =~ s/(.|\n)/'0x' . unpack('H2', $1).', '/ge; |
print "\t$buffer\n"; | print "\t$buffer\n"; |
} | } |
|
print "};\n"; |
close FILE; | close FILE; |
|
#size of the file |
print "};\n\n"; |
print "const static int size_$varname"." = sizeof(file_$varname);\n\n"; |
} | } |