blob: c92cb91403c628636acc3000ee1224213e98ed50 [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úti1e148272020-08-07 13:07:28 +02003# Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00004# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02005#
Simon Butcher6dc7c9c2016-06-19 22:49:58 +01006# Purpose
7#
Gilles Peskinef08ca832023-09-12 19:21:54 +02008# This script migrates application source code from the Mbed TLS 1.3 API to the
9# Mbed TLS 2.0 API.
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010010#
11# The script processes the given source code and renames identifiers - functions
12# types, enums etc, as
13#
14# Usage: rename.pl [-f datafile] [-s] [--] [filenames...]
15#
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020016
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010017use warnings;
18use strict;
19
20use utf8;
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010021use Path::Class;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010022use open qw(:std utf8);
23
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020024my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010025
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020026(my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/;
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020027my $do_strings = 0;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020028
29while( @ARGV && $ARGV[0] =~ /^-/ ) {
30 my $opt = shift;
31 if( $opt eq '--' ) {
32 last;
33 } elsif( $opt eq '-f' ) {
34 $datafile = shift;
35 } elsif( $opt eq '-s' ) {
36 $do_strings = 1; shift;
37 } else {
38 die $usage;
39 }
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020040}
41
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010042my %subst;
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020043open my $nfh, '<', $datafile or die "Could not read $datafile\n";
44my $ident = qr/[_A-Za-z][_A-Za-z0-9]*/;
45while( my $line = <$nfh> ) {
46 chomp $line;
47 my ( $old, $new ) = ( $line =~ /^($ident)\s+($ident)$/ );
48 if( ! $old || ! $new ) {
49 die "$0: $datafile:$.: bad input '$line'\n";
50 }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010051 $subst{$old} = $new;
52}
Manuel Pégourié-Gonnard2aa81cc2015-04-10 11:19:10 +020053close $nfh or die;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010054
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +020055my $string = qr/"(?:\\.|[^\\"])*"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010056my $space = qr/\s+/;
57my $idnum = qr/[a-zA-Z0-9_]+/;
Manuel Pégourié-Gonnard5f29a732015-04-20 12:27:12 +010058my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/;
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010059
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010060my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls');
61my $lib_source_dir = dir($0)->parent->parent->subdir('library');
62
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020063# if we replace inside strings, we don't consider them a token
64my $token = $do_strings ? qr/$space|$idnum|$symbols/
65 : qr/$string|$space|$idnum|$symbols/;
66
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010067my %warnings;
68
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010069# If no files were passed, exit...
70if ( not defined($ARGV[0]) ){ die $usage; }
71
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010072while( my $filename = shift )
73{
74 print STDERR "$filename... ";
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010075
76 if( dir($filename)->parent eq $lib_include_dir ||
77 dir($filename)->parent eq $lib_source_dir )
78 {
Gilles Peskinef08ca832023-09-12 19:21:54 +020079 die "Script cannot be executed on the Mbed TLS library itself.";
Simon Butcher6dc7c9c2016-06-19 22:49:58 +010080 }
81
Manuel Pégourié-Gonnardf7d945f2015-04-03 15:21:50 +020082 if( -d $filename ) { print STDERR "skip (directory)\n"; next }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010083
84 open my $rfh, '<', $filename or die;
85 my @lines = <$rfh>;
86 close $rfh or die;
87
88 my @out;
89 for my $line (@lines) {
Manuel Pégourié-Gonnardfb9f2a02015-04-03 18:37:07 +020090 if( $line =~ /#include/ ) {
91 $line =~ s/polarssl/mbedtls/;
92 $line =~ s/POLARSSL/MBEDTLS/;
93 push( @out, $line );
94 next;
95 }
96
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020097 my @words = ($line =~ /$token/g);
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010098 my $checkline = join '', @words;
99 if( $checkline eq $line ) {
100 my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words;
101 push( @out, join '', @new );
102 } else {
103 $warnings{$filename} = [] unless $warnings{$filename};
104 push @{ $warnings{$filename} }, $line;
105 push( @out, $line );
106 }
107 }
108
109 open my $wfh, '>', $filename or die;
110 print $wfh $_ for @out;
111 close $wfh or die;
112 print STDERR "done\n";
113}
114
115if( %warnings ) {
Manuel Pégourié-Gonnardc559f042015-04-08 20:10:16 +0200116 print "\nWarning: lines skipped due to unexpected characters:\n";
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +0100117 for my $filename (sort keys %warnings) {
118 print "in $filename:\n";
119 print for @{ $warnings{$filename} };
120 }
121}