print_c.pl 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright The Mbed TLS Contributors
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  7. # not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. use strict;
  18. use warnings;
  19. if (!@ARGV || $ARGV[0] == '--help') {
  20. print <<EOF;
  21. Usage: $0 mbedtls_test_foo <file.pem
  22. $0 TEST_FOO mbedtls_test_foo <file.pem
  23. Print out a PEM file as C code defining a string constant.
  24. Used to include some of the test data in /library/certs.c for
  25. self-tests and sample programs.
  26. EOF
  27. exit;
  28. }
  29. my $pp_name = @ARGV > 1 ? shift @ARGV : undef;
  30. my $name = shift @ARGV;
  31. my @lines = map {chomp; s/([\\"])/\\$1/g; "\"$_\\r\\n\""} <STDIN>;
  32. if (defined $pp_name) {
  33. foreach ("#define $pp_name", @lines[0..@lines-2]) {
  34. printf "%-72s\\\n", $_;
  35. }
  36. print "$lines[@lines-1]\n";
  37. print "const char $name\[\] = $pp_name;\n";
  38. } else {
  39. print "const char $name\[\] =";
  40. foreach (@lines) {
  41. print "\n$_";
  42. }
  43. print ";\n";
  44. }