1#!/usr/bin/env perl 2# 3# Copyright The Mbed TLS Contributors 4# 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. 17# 18# Purpose 19# 20# This script migrates application source code from the mbed TLS 1.3 API to the 21# mbed TLS 2.0 API. 22# 23# The script processes the given source code and renames identifiers - functions 24# types, enums etc, as 25# 26# Usage: rename.pl [-f datafile] [-s] [--] [filenames...] 27# 28 29use warnings; 30use strict; 31 32use utf8; 33use Path::Class; 34use open qw(:std utf8); 35 36my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n"; 37 38(my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/; 39my $do_strings = 0; 40 41while( @ARGV && $ARGV[0] =~ /^-/ ) { 42 my $opt = shift; 43 if( $opt eq '--' ) { 44 last; 45 } elsif( $opt eq '-f' ) { 46 $datafile = shift; 47 } elsif( $opt eq '-s' ) { 48 $do_strings = 1; shift; 49 } else { 50 die $usage; 51 } 52} 53 54my %subst; 55open my $nfh, '<', $datafile or die "Could not read $datafile\n"; 56my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/; 57while( my $line = <$nfh> ) { 58 chomp $line; 59 my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ ); 60 if( ! $old || ! $new ) { 61 die "$0: $datafile:$.: bad input '$line'\n"; 62 } 63 $subst{$old} = $new; 64} 65close $nfh or die; 66 67my $string = qr/"(?:\\.|[^\\"])*"/; 68my $space = qr/\s+/; 69my $idnum = qr/[a-zA-Z0-9_]+/; 70my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/; 71 72my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls'); 73my $lib_source_dir = dir($0)->parent->parent->subdir('library'); 74 75# if we replace inside strings, we don't consider them a token 76my $token = $do_strings ? qr/$space|$idnum|$symbols/ 77 : qr/$string|$space|$idnum|$symbols/; 78 79my %warnings; 80 81# If no files were passed, exit... 82if ( not defined($ARGV[0]) ){ die $usage; } 83 84while( my $filename = shift ) 85{ 86 print STDERR "$filename... "; 87 88 if( dir($filename)->parent eq $lib_include_dir || 89 dir($filename)->parent eq $lib_source_dir ) 90 { 91 die "Script cannot be executed on the mbed TLS library itself."; 92 } 93 94 if( -d $filename ) { print STDERR "skip (directory)\n"; next } 95 96 open my $rfh, '<', $filename or die; 97 my @lines = <$rfh>; 98 close $rfh or die; 99 100 my @out; 101 for my $line (@lines) { 102 if( $line =~ /#include/ ) { 103 $line =~ s/polarssl/mbedtls/; 104 $line =~ s/POLARSSL/MBEDTLS/; 105 push( @out, $line ); 106 next; 107 } 108 109 my @words = ($line =~ /$token/g); 110 my $checkline = join '', @words; 111 if( $checkline eq $line ) { 112 my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words; 113 push( @out, join '', @new ); 114 } else { 115 $warnings{$filename} = [] unless $warnings{$filename}; 116 push @{ $warnings{$filename} }, $line; 117 push( @out, $line ); 118 } 119 } 120 121 open my $wfh, '>', $filename or die; 122 print $wfh $_ for @out; 123 close $wfh or die; 124 print STDERR "done\n"; 125} 126 127if( %warnings ) { 128 print "\nWarning: lines skipped due to unexpected characters:\n"; 129 for my $filename (sort keys %warnings) { 130 print "in $filename:\n"; 131 print for @{ $warnings{$filename} }; 132 } 133} 134