1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- use strict;
- use warnings;
- my $num_args = $#ARGV + 1;
- if ($num_args != 3 ) {
- print "usage: ./scripts/dertoc.pl ./certs/server-cert.der server_cert_der_2048 dertoc.c\n";
- exit;
- }
- my $inFile = $ARGV[0];
- my $outName = $ARGV[1];
- my $outputFile = $ARGV[2];
- open OUT_FILE, "+>", $outputFile or die $!;
- print OUT_FILE "/* $outputFile */\n\n";
- print OUT_FILE "static const unsigned char $outName\[] =\n";
- print OUT_FILE "{\n";
- file_to_hex($inFile);
- print OUT_FILE "};\n";
- print OUT_FILE "static const int sizeof_$outName = sizeof($outName);\n\n";
- close OUT_FILE or die $!;
- sub file_to_hex {
- my $fileName = $_[0];
- open my $fp, "<", $fileName or die $!;
- binmode($fp);
- my $fileLen = -s $fileName;
- my $byte;
- for (my $i = 0, my $j = 1; $i < $fileLen; $i++, $j++)
- {
- if ($j == 1) {
- print OUT_FILE "\t";
- }
- read($fp, $byte, 1) or die "Error reading $fileName";
- my $output = sprintf("0x%02X", ord($byte));
- print OUT_FILE $output;
- if ($i != ($fileLen - 1)) {
- print OUT_FILE ", ";
- }
- if ($j == 10) {
- $j = 0;
- print OUT_FILE "\n";
- }
- }
- print OUT_FILE "\n";
- close($fp);
- }
|