1########################################################################
2#
3# Copyright (c) 2010, Secure Endpoints Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11#   notice, this list of conditions and the following disclaimer.
12#
13# - Redistributions in binary form must reproduce the above copyright
14#   notice, this list of conditions and the following disclaimer in
15#   the documentation and/or other materials provided with the
16#   distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32use Getopt::Long;
33use Pod::Usage;
34use feature "switch";
35
36my $def_name = '';
37my $vs_name = '';
38my $show_help = 0;
39
40my %syms;
41
42my $def_only = 0;
43my $vs_only = 0;
44
45GetOptions ("def=s" => \$def_name,
46	    "vs=s" => \$vs_name,
47	    "help|?" => \$show_help) or pod2usage( -exitval => 2,
48						   -verbose => 3 );
49pod2usage( -exitval => 1,
50	   -verbose => 3 ) if $show_help or !$def_name or !$vs_name;
51
52open (my $def, '<', $def_name) or die $!;
53open (my $vs, '<', $vs_name) or die $!;
54
55# First go through the version-script
56
57my $global = 0;
58
59while(<$vs>)
60{
61    next unless m/^([^#]+)/;
62
63    @a = split(/\s+|({|})/,$1);
64
65    for $f (@a) {
66	given ($f) {
67	    when (/global\:/) { $global = 1; }
68	    when (/{|}|.*\:/) { $global = 0; }
69	    when (/(.*)\;/ and $global == 1) {
70		$syms{$1} = 1;
71	    }
72	}
73    }
74}
75
76while(<$def>)
77{
78    next if m/^#/;
79    next unless m/^;!([^;]+)/ or m/^([^;]+);?(!?)/;
80
81    @a = split(/\s+/, $1);
82
83    for $f (@a) {
84	next if $f =~ /EXPORTS/ or $f =~ /DATA/ or not $f;
85
86	if (not exists $syms{$f} and not $2) {
87	    print "$f: Only in DEF\n";
88	    ++$def_only;
89	}
90	delete $syms{$f};
91    }
92}
93
94#while (($k,$v) = each %syms) {
95for $k (sort keys %syms) {
96    print "$k: Only in VS\n";
97    ++$vs_only;
98}
99
100close($def);
101close($vs);
102
103if ($def_only or $vs_only) {
104    print "\nMismatches found.\n";
105    exit(1);
106}
107
108__END__
109
110=head1 NAME
111
112w32-sync-exported-symbols.pl - Synchronize Windows .def with version-script
113
114=head1 SYNOPSIS
115
116w32-sync-exported-symbols.pl {options}
117
118  Options:
119    --def        Name of .def file
120    --vs         Name of version-script file
121
122=head1 DESCRIPTION
123
124Verifies that all the symbols exported by the version-script is also
125accounted for in the .def file.  Also checks that no extra symbols are
126exported by the .def file unless they are marked as safe.
127
128=cut
129
130