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
|
#!/usr/bin/env perl
##
## bin2c -- convert an binary file into a statically initialised
## C array of characters
##
## Copyright (c) Ralf S. Engelschall, All Rights Reserved.
##
use strict;
use warnings;
use autodie;
my ( $in_fn, $out_fn, $name ) = @ARGV;
if ( @ARGV != 3 )
{
die "Usage: $0 binary-file c-file buffer-name\n";
}
my $size = -s $in_fn;
open( my $in_fh, "<", $in_fn );
open( my $out_c, ">", "$out_fn.c" );
print {$out_c} "/* $in_fn.c -- automatically generated by bin2c */\n";
print {$out_c} "\n";
print {$out_c} "int ${name}_size = " . $size . ";\n";
print {$out_c} "unsigned char ${name}_data[] = {\n";
my $i = 1;
my $c;
while ( read( $in_fh, $c, 1 ) )
{
$out_c->printf( "0x%02x", ord($c) );
$out_c->print(",") if ( $i < $size );
$out_c->print("\n") if ( $i % 15 == 0 and $i < $size );
++$i;
}
print {$out_c} " };\n\n/*EOF*/\n";
close($in_fh);
close($out_c);
|