1#!/usr/bin/env perl
2#
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5
6use strict;
7use warnings;
8
9if (!@ARGV || $ARGV[0] == '--help') {
10    print <<EOF;
11Usage: $0 mbedtls_test_foo <file.pem
12       $0 TEST_FOO mbedtls_test_foo <file.pem
13Print out a PEM file as C code defining a string constant.
14
15Used to include some of the test data in /library/certs.c for
16self-tests and sample programs.
17EOF
18    exit;
19}
20
21my $pp_name = @ARGV > 1 ? shift @ARGV : undef;
22my $name = shift @ARGV;
23
24my @lines = map {chomp; s/([\\"])/\\$1/g; "\"$_\\r\\n\""} <STDIN>;
25
26if (defined $pp_name) {
27    foreach ("#define $pp_name", @lines[0..@lines-2]) {
28        printf "%-72s\\\n", $_;
29    }
30    print "$lines[@lines-1]\n";
31    print "const char $name\[\] = $pp_name;\n";
32} else {
33    print "const char $name\[\] =";
34    foreach (@lines) {
35        print "\n$_";
36    }
37    print ";\n";
38}
39