1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
:
eval 'exec perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
##
## bin2c -- convert an binary file into a statically initialised
## C array of characters
##
## Copyright (c) Ralf S. Engelschall, All Rights Reserved.
##
$filein = $ARGV[0];
$fileout = $ARGV[1];
$name = $ARGV[2];
if ($#ARGV ne 2) {
printf(STDERR "Usage: $0 binary-file c-file buffer-name\n");
exit(1);
}
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($filein);
open(IN, "<$filein");
#open(OUTH, ">$fileout.h");
open(OUTC, ">$fileout.c");
print OUTC "/* $filein.c -- automatically generated by bin2c */\n";
print OUTC "\n";
print OUTC "int ${name}_size = " . $size . ";\n";
print OUTC "unsigned char ${name}_data[] = {\n";
$i = 1;
while (read(IN, $c, 1)) {
printf(OUTC "0x%02x", ord($c));
printf(OUTC ",") if ($i < $size);
printf(OUTC "\n") if ($i % 15 == 0 && $i < $size);
$i++;
}
print OUTC " };\n";
print OUTC "\n";
print OUTC "/*EOF*/\n";
$filename = "$fileout.h";
$filename =~ tr|a-z.|A-Z_|;
#print OUTH "/* $fileout.h -- automatically generated by bin2c */\n";
#print OUTH "#ifndef __$filename\n";
#print OUTH "#define __$filename\n";
#print OUTH "\n";
#print OUTH "extern unsigned char *$name;\n";
#print OUTH "\n";
#print OUTH "#endif /* __$filename */\n";
close(IN);
#close(OUTH);
close(OUTC);
##EOF##
|