blob: 3f47bf2f02b44901e3985756660596566a281d3c [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Simon Butcher6dc7c9c2016-06-19 22:49:58 +01002#
Bence Szépkútia2947ac2020-08-19 16:37:36 +02003# Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02004# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5#
6# This file is provided under the Apache License 2.0, or the
7# GNU General Public License v2.0 or later.
8#
9# **********
10# Apache License 2.0:
Bence Szépkúti51b41d52020-05-26 01:54:15 +020011#
12# Licensed under the Apache License, Version 2.0 (the "License"); you may
13# not use this file except in compliance with the License.
14# You may obtain a copy of the License at
15#
16# http://www.apache.org/licenses/LICENSE-2.0
17#
18# Unless required by applicable law or agreed to in writing, software
19# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21# See the License for the specific language governing permissions and
22# limitations under the License.
23#
Bence Szépkútif744bd72020-06-05 13:02:18 +020024# **********
25#
26# **********
27# GNU General Public License v2.0 or later:
28#
29# This program is free software; you can redistribute it and/or modify
30# it under the terms of the GNU General Public License as published by
31# the Free Software Foundation; either version 2 of the License, or
32# (at your option) any later version.
33#
34# This program is distributed in the hope that it will be useful,
35# but WITHOUT ANY WARRANTY; without even the implied warranty of
36# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37# GNU General Public License for more details.
38#
39# You should have received a copy of the GNU General Public License along
40# with this program; if not, write to the Free Software Foundation, Inc.,
41# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
42#
43# **********
44#
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010045# Purpose
46#
47# This script migrates application source code from the mbed TLS 1.3 API to the
48# mbed TLS 2.0 API.
49#
50# The script processes the given source code and renames identifiers - functions
51# types, enums etc, as
52#
53# Usage: rename.pl [-f datafile] [-s] [--] [filenames...]
54#
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020055
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010056use warnings;
57use strict;
58
59use utf8;
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010060use Path::Class;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010061use open qw(:std utf8);
62
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020063my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010064
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020065(my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/;
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020066my $do_strings = 0;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020067
68while( @ARGV && $ARGV[0] =~ /^-/ ) {
69 my $opt = shift;
70 if( $opt eq '--' ) {
71 last;
72 } elsif( $opt eq '-f' ) {
73 $datafile = shift;
74 } elsif( $opt eq '-s' ) {
75 $do_strings = 1; shift;
76 } else {
77 die $usage;
78 }
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020079}
80
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010081my %subst;
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020082open my $nfh, '<', $datafile or die "Could not read $datafile\n";
83my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/;
84while( my $line = <$nfh> ) {
85 chomp $line;
86 my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ );
87 if( ! $old || ! $new ) {
88 die "$0: $datafile:$.: bad input '$line'\n";
89 }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010090 $subst{$old} = $new;
91}
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020092close $nfh or die;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010093
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +020094my $string = qr/"(?:\\.|[^\\"])*"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010095my $space = qr/\s+/;
96my $idnum = qr/[a-zA-Z0-9_]+/;
Manuel Pégourié-Gonnard5f29a732015-04-20 12:27:12 +010097my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010098
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010099my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls');
100my $lib_source_dir = dir($0)->parent->parent->subdir('library');
101
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +0200102# if we replace inside strings, we don't consider them a token
103my $token = $do_strings ? qr/$space|$idnum|$symbols/
104 : qr/$string|$space|$idnum|$symbols/;
105
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100106my %warnings;
107
Simon Butcher6dc7c9c2016-06-19 22:49:58 +0100108# If no files were passed, exit...
109if ( not defined($ARGV[0]) ){ die $usage; }
110
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100111while( my $filename = shift )
112{
113 print STDERR "$filename... ";
Simon Butcher6dc7c9c2016-06-19 22:49:58 +0100114
115 if( dir($filename)->parent eq $lib_include_dir ||
116 dir($filename)->parent eq $lib_source_dir )
117 {
118 die "Script cannot be executed on the mbed TLS library itself.";
119 }
120
Manuel Pégourié-Gonnardf7d945f2015-04-03 15:21:50 +0200121 if( -d $filename ) { print STDERR "skip (directory)\n"; next }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100122
123 open my $rfh, '<', $filename or die;
124 my @lines = <$rfh>;
125 close $rfh or die;
126
127 my @out;
128 for my $line (@lines) {
Manuel Pégourié-Gonnardfb9f2a02015-04-03 18:37:07 +0200129 if( $line =~ /#include/ ) {
130 $line =~ s/polarssl/mbedtls/;
131 $line =~ s/POLARSSL/MBEDTLS/;
132 push( @out, $line );
133 next;
134 }
135
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +0200136 my @words = ($line =~ /$token/g);
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100137 my $checkline = join '', @words;
138 if( $checkline eq $line ) {
139 my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words;
140 push( @out, join '', @new );
141 } else {
142 $warnings{$filename} = [] unless $warnings{$filename};
143 push @{ $warnings{$filename} }, $line;
144 push( @out, $line );
145 }
146 }
147
148 open my $wfh, '>', $filename or die;
149 print $wfh $_ for @out;
150 close $wfh or die;
151 print STDERR "done\n";
152}
153
154if( %warnings ) {
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +0200155 print "\nWarning: lines skipped due to unexpected characters:\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100156 for my $filename (sort keys %warnings) {
157 print "in $filename:\n";
158 print for @{ $warnings{$filename} };
159 }
160}