• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# Purpose
7#
8# This script migrates application source code from the Mbed TLS 1.3 API to the
9# Mbed TLS 2.0 API.
10#
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#
16
17use warnings;
18use strict;
19
20use utf8;
21use Path::Class;
22use open qw(:std utf8);
23
24my $usage = "Usage: $0 [-f datafile] [-s] [--] [filenames...]\n";
25
26(my $datafile = $0) =~ s/rename.pl$/data_files\/rename-1.3-2.0.txt/;
27my $do_strings = 0;
28
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    }
40}
41
42my %subst;
43open 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    }
51    $subst{$old} = $new;
52}
53close $nfh or die;
54
55my $string = qr/"(?:\\.|[^\\"])*"/;
56my $space = qr/\s+/;
57my $idnum = qr/[a-zA-Z0-9_]+/;
58my $symbols = qr/[-!#\$%&'()*+,.\/:;<=>?@[\\\]^_`{|}~]+|"/;
59
60my $lib_include_dir = dir($0)->parent->parent->subdir('include', 'mbedtls');
61my $lib_source_dir = dir($0)->parent->parent->subdir('library');
62
63# 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
67my %warnings;
68
69# If no files were passed, exit...
70if ( not defined($ARGV[0]) ){ die $usage; }
71
72while( my $filename = shift )
73{
74    print STDERR "$filename... ";
75
76    if( dir($filename)->parent eq $lib_include_dir ||
77         dir($filename)->parent eq $lib_source_dir )
78    {
79        die "Script cannot be executed on the Mbed TLS library itself.";
80    }
81
82    if( -d $filename ) { print STDERR "skip (directory)\n"; next }
83
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) {
90        if( $line =~ /#include/ ) {
91            $line =~ s/polarssl/mbedtls/;
92            $line =~ s/POLARSSL/MBEDTLS/;
93            push( @out, $line );
94            next;
95        }
96
97        my @words = ($line =~ /$token/g);
98        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 ) {
116    print "\nWarning: lines skipped due to unexpected characters:\n";
117    for my $filename (sort keys %warnings) {
118        print "in $filename:\n";
119        print for @{ $warnings{$filename} };
120    }
121}
122