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# 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: 11# 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# 24# ********** 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# 45# 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# 55 56use warnings; 57use strict; 58 59use utf8; 60use Path::Class; 61use open qw(:std utf8); 62 63my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n"; 64 65(my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/; 66my $do_strings = 0; 67 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 } 79} 80 81my %subst; 82open 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 } 90 $subst{$old} = $new; 91} 92close $nfh or die; 93 94my $string = qr/"(?:\\.|[^\\"])*"/; 95my $space = qr/\s+/; 96my $idnum = qr/[a-zA-Z0-9_]+/; 97my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/; 98 99my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls'); 100my $lib_source_dir = dir($0)->parent->parent->subdir('library'); 101 102# 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 106my %warnings; 107 108# If no files were passed, exit... 109if ( not defined($ARGV[0]) ){ die $usage; } 110 111while( my $filename = shift ) 112{ 113 print STDERR "$filename... "; 114 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 121 if( -d $filename ) { print STDERR "skip (directory)\n"; next } 122 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) { 129 if( $line =~ /#include/ ) { 130 $line =~ s/polarssl/mbedtls/; 131 $line =~ s/POLARSSL/MBEDTLS/; 132 push( @out, $line ); 133 next; 134 } 135 136 my @words = ($line =~ /$token/g); 137 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 ) { 155 print "\nWarning: lines skipped due to unexpected characters:\n"; 156 for my $filename (sort keys %warnings) { 157 print "in $filename:\n"; 158 print for @{ $warnings{$filename} }; 159 } 160} 161