2
0

dertoc.pl 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/perl
  2. # dertoc.pl
  3. # version 1.0
  4. # Updated 07/31/2018
  5. #
  6. # Copyright (C) 2006-2018 wolfSSL Inc.
  7. #
  8. use strict;
  9. use warnings;
  10. my $num_args = $#ARGV + 1;
  11. if ($num_args != 3 ) {
  12. print "usage: ./scripts/dertoc.pl ./certs/server-cert.der server_cert_der_2048 dertoc.c\n";
  13. exit;
  14. }
  15. my $inFile = $ARGV[0];
  16. my $outName = $ARGV[1];
  17. my $outputFile = $ARGV[2];
  18. # open our output file, "+>" creates and/or truncates
  19. open OUT_FILE, "+>", $outputFile or die $!;
  20. print OUT_FILE "/* $outputFile */\n\n";
  21. print OUT_FILE "static const unsigned char $outName\[] =\n";
  22. print OUT_FILE "{\n";
  23. file_to_hex($inFile);
  24. print OUT_FILE "};\n";
  25. print OUT_FILE "static const int sizeof_$outName = sizeof($outName);\n\n";
  26. # close file
  27. close OUT_FILE or die $!;
  28. # print file as hex, comma-separated, as needed by C buffer
  29. sub file_to_hex {
  30. my $fileName = $_[0];
  31. open my $fp, "<", $fileName or die $!;
  32. binmode($fp);
  33. my $fileLen = -s $fileName;
  34. my $byte;
  35. for (my $i = 0, my $j = 1; $i < $fileLen; $i++, $j++)
  36. {
  37. if ($j == 1) {
  38. print OUT_FILE "\t";
  39. }
  40. read($fp, $byte, 1) or die "Error reading $fileName";
  41. my $output = sprintf("0x%02X", ord($byte));
  42. print OUT_FILE $output;
  43. if ($i != ($fileLen - 1)) {
  44. print OUT_FILE ", ";
  45. }
  46. if ($j == 10) {
  47. $j = 0;
  48. print OUT_FILE "\n";
  49. }
  50. }
  51. print OUT_FILE "\n";
  52. close($fp);
  53. }