blob: ce8ed6f8e67fb045b286c58cbde57c32ef6b6a61 [file] [log] [blame]
Gilles Peskinef040a172017-05-05 18:56:12 +02001#!/usr/bin/env perl
Bence Szépkúti700ee442020-05-26 00:33:31 +02002#
Bence Szépkúti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02004# 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.
Bence Szépkúti700ee442020-05-26 00:33:31 +020017
Gilles Peskinef040a172017-05-05 18:56:12 +020018use strict;
19use warnings;
20
Gilles Peskinefd14bca2017-05-11 17:57:22 +020021if (!@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
Gilles Peskinef040a172017-05-05 18:56:12 +020033my $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}