fbrosson | 533407a | 2018-04-04 21:44:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 2 | # |
Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 3 | # Copyright (c) 2015-2016, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 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: |
Bence Szépkúti | 51b41d5 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 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 | # |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 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 | # |
Bence Szépkúti | 51b41d5 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 45 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 46 | # |
| 47 | # Purpose |
| 48 | # |
| 49 | # This script migrates application source code from the mbed TLS 1.3 API to the |
| 50 | # mbed TLS 2.0 API. |
| 51 | # |
| 52 | # The script processes the given source code and renames identifiers - functions |
| 53 | # types, enums etc, as |
| 54 | # |
| 55 | # Usage: rename.pl [-f datafile] [-s] [--] [filenames...] |
| 56 | # |
Manuel Pégourié-Gonnard | b20a70f | 2015-04-08 14:56:51 +0200 | [diff] [blame] | 57 | |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 58 | use warnings; |
| 59 | use strict; |
| 60 | |
| 61 | use utf8; |
Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 62 | use Path::Class; |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 63 | use open qw(:std utf8); |
| 64 | |
Manuel Pégourié-Gonnard | b20a70f | 2015-04-08 14:56:51 +0200 | [diff] [blame] | 65 | my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n"; |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 66 | |
Manuel Pégourié-Gonnard | 2aa81cc | 2015-04-10 11:19:10 +0200 | [diff] [blame] | 67 | (my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/; |
Manuel Pégourié-Gonnard | 88323c7 | 2015-04-03 14:38:02 +0200 | [diff] [blame] | 68 | my $do_strings = 0; |
Manuel Pégourié-Gonnard | b20a70f | 2015-04-08 14:56:51 +0200 | [diff] [blame] | 69 | |
| 70 | while( @ARGV && $ARGV[0] =~ /^-/ ) { |
| 71 | my $opt = shift; |
| 72 | if( $opt eq '--' ) { |
| 73 | last; |
| 74 | } elsif( $opt eq '-f' ) { |
| 75 | $datafile = shift; |
| 76 | } elsif( $opt eq '-s' ) { |
| 77 | $do_strings = 1; shift; |
| 78 | } else { |
| 79 | die $usage; |
| 80 | } |
Manuel Pégourié-Gonnard | 88323c7 | 2015-04-03 14:38:02 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 83 | my %subst; |
Manuel Pégourié-Gonnard | 2aa81cc | 2015-04-10 11:19:10 +0200 | [diff] [blame] | 84 | open my $nfh, '<', $datafile or die "Could not read $datafile\n"; |
| 85 | my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/; |
| 86 | while( my $line = <$nfh> ) { |
| 87 | chomp $line; |
| 88 | my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ ); |
| 89 | if( ! $old || ! $new ) { |
| 90 | die "$0: $datafile:$.: bad input '$line'\n"; |
| 91 | } |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 92 | $subst{$old} = $new; |
| 93 | } |
Manuel Pégourié-Gonnard | 2aa81cc | 2015-04-10 11:19:10 +0200 | [diff] [blame] | 94 | close $nfh or die; |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 95 | |
Manuel Pégourié-Gonnard | c559f04 | 2015-04-08 20:10:16 +0200 | [diff] [blame] | 96 | my $string = qr/"(?:\\.|[^\\"])*"/; |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 97 | my $space = qr/\s+/; |
| 98 | my $idnum = qr/[a-zA-Z0-9_]+/; |
Manuel Pégourié-Gonnard | 5f29a73 | 2015-04-20 12:27:12 +0100 | [diff] [blame] | 99 | my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/; |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 100 | |
Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 101 | my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls'); |
| 102 | my $lib_source_dir = dir($0)->parent->parent->subdir('library'); |
| 103 | |
Manuel Pégourié-Gonnard | 88323c7 | 2015-04-03 14:38:02 +0200 | [diff] [blame] | 104 | # if we replace inside strings, we don't consider them a token |
| 105 | my $token = $do_strings ? qr/$space|$idnum|$symbols/ |
| 106 | : qr/$string|$space|$idnum|$symbols/; |
| 107 | |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 108 | my %warnings; |
| 109 | |
Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 110 | # If no files were passed, exit... |
| 111 | if ( not defined($ARGV[0]) ){ die $usage; } |
| 112 | |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 113 | while( my $filename = shift ) |
| 114 | { |
| 115 | print STDERR "$filename... "; |
Simon Butcher | 6dc7c9c | 2016-06-19 22:49:58 +0100 | [diff] [blame] | 116 | |
| 117 | if( dir($filename)->parent eq $lib_include_dir || |
| 118 | dir($filename)->parent eq $lib_source_dir ) |
| 119 | { |
| 120 | die "Script cannot be executed on the mbed TLS library itself."; |
| 121 | } |
| 122 | |
Manuel Pégourié-Gonnard | f7d945f | 2015-04-03 15:21:50 +0200 | [diff] [blame] | 123 | if( -d $filename ) { print STDERR "skip (directory)\n"; next } |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 124 | |
| 125 | open my $rfh, '<', $filename or die; |
| 126 | my @lines = <$rfh>; |
| 127 | close $rfh or die; |
| 128 | |
| 129 | my @out; |
| 130 | for my $line (@lines) { |
Manuel Pégourié-Gonnard | fb9f2a0 | 2015-04-03 18:37:07 +0200 | [diff] [blame] | 131 | if( $line =~ /#include/ ) { |
| 132 | $line =~ s/polarssl/mbedtls/; |
| 133 | $line =~ s/POLARSSL/MBEDTLS/; |
| 134 | push( @out, $line ); |
| 135 | next; |
| 136 | } |
| 137 | |
Manuel Pégourié-Gonnard | 88323c7 | 2015-04-03 14:38:02 +0200 | [diff] [blame] | 138 | my @words = ($line =~ /$token/g); |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 139 | my $checkline = join '', @words; |
| 140 | if( $checkline eq $line ) { |
| 141 | my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words; |
| 142 | push( @out, join '', @new ); |
| 143 | } else { |
| 144 | $warnings{$filename} = [] unless $warnings{$filename}; |
| 145 | push @{ $warnings{$filename} }, $line; |
| 146 | push( @out, $line ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | open my $wfh, '>', $filename or die; |
| 151 | print $wfh $_ for @out; |
| 152 | close $wfh or die; |
| 153 | print STDERR "done\n"; |
| 154 | } |
| 155 | |
| 156 | if( %warnings ) { |
Manuel Pégourié-Gonnard | c559f04 | 2015-04-08 20:10:16 +0200 | [diff] [blame] | 157 | print "\nWarning: lines skipped due to unexpected characters:\n"; |
Manuel Pégourié-Gonnard | 3385cf4 | 2015-04-02 17:59:30 +0100 | [diff] [blame] | 158 | for my $filename (sort keys %warnings) { |
| 159 | print "in $filename:\n"; |
| 160 | print for @{ $warnings{$filename} }; |
| 161 | } |
| 162 | } |