blob: 4c15be2d3ce9286a5f24259eba30e049d14ccbb3 [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#
3# Copyright (C) 2017, Arm Limited, All Rights Reserved
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#
18# This file is part of Mbed TLS (https://tls.mbed.org)
19
Gilles Peskinef040a172017-05-05 18:56:12 +020020use strict;
21use warnings;
22
Gilles Peskinefd14bca2017-05-11 17:57:22 +020023if (!@ARGV || $ARGV[0] == '--help') {
24 print <<EOF;
25Usage: $0 mbedtls_test_foo <file.pem
26 $0 TEST_FOO mbedtls_test_foo <file.pem
27Print out a PEM file as C code defining a string constant.
28
29Used to include some of the test data in /library/certs.c for
30self-tests and sample programs.
31EOF
32 exit;
33}
34
Gilles Peskinef040a172017-05-05 18:56:12 +020035my $pp_name = @ARGV > 1 ? shift @ARGV : undef;
36my $name = shift @ARGV;
37
38my @lines = map {chomp; s/([\\"])/\\$1/g; "\"$_\\r\\n\""} <STDIN>;
39
40if (defined $pp_name) {
41 foreach ("#define $pp_name", @lines[0..@lines-2]) {
42 printf "%-72s\\\n", $_;
43 }
44 print "$lines[@lines-1]\n";
45 print "const char $name\[\] = $pp_name;\n";
46} else {
47 print "const char $name\[\] =";
48 foreach (@lines) {
49 print "\n$_";
50 }
51 print ";\n";
52}