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 18use strict; 19use warnings; 20 21if (!@ARGV || $ARGV[0] == '--help') { 22 print <<EOF; 23Usage: $0 mbedtls_test_foo <file.pem 24 $0 TEST_FOO mbedtls_test_foo <file.pem 25Print out a PEM file as C code defining a string constant. 26 27Used to include some of the test data in /library/certs.c for 28self-tests and sample programs. 29EOF 30 exit; 31} 32 33my $pp_name = @ARGV > 1 ? shift @ARGV : undef; 34my $name = shift @ARGV; 35 36my @lines = map {chomp; s/([\\"])/\\$1/g; "\"$_\\r\\n\""} <STDIN>; 37 38if (defined $pp_name) { 39 foreach ("#define $pp_name", @lines[0..@lines-2]) { 40 printf "%-72s\\\n", $_; 41 } 42 print "$lines[@lines-1]\n"; 43 print "const char $name\[\] = $pp_name;\n"; 44} else { 45 print "const char $name\[\] ="; 46 foreach (@lines) { 47 print "\n$_"; 48 } 49 print ";\n"; 50} 51