1193141Sdougb#!/bin/bin/perl -w
2193141Sdougb#
3245163Serwin# Copyright (C) 2006, 2007, 2012  Internet Systems Consortium, Inc. ("ISC")
4193141Sdougb#
5193141Sdougb# Permission to use, copy, modify, and/or distribute this software for any
6193141Sdougb# purpose with or without fee is hereby granted, provided that the above
7193141Sdougb# copyright notice and this permission notice appear in all copies.
8193141Sdougb#
9193141Sdougb# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10193141Sdougb# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11193141Sdougb# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12193141Sdougb# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13193141Sdougb# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14193141Sdougb# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15193141Sdougb# PERFORMANCE OF THIS SOFTWARE.
16193141Sdougb
17234010Sdougb# $Id: spnego_asn1.pl,v 1.4 2007/06/19 23:47:16 tbox Exp $
18193141Sdougb
19193141Sdougb# Our SPNEGO implementation uses some functions generated by the
20193141Sdougb# Heimdal ASN.1 compiler, which this script then whacks a bit to make
21193141Sdougb# them work properly in this stripped down implementation.  We don't
22193141Sdougb# want to require our users to have a copy of the compiler, so we ship
23193141Sdougb# the output of this script, but we need to keep the script around in
24193141Sdougb# any case to cope with future changes to the SPNEGO ASN.1 code, so we
25193141Sdougb# might as well supply the script for users who want it.
26193141Sdougb
27193141Sdougb# Overall plan: run the ASN.1 compiler, run each of its output files
28193141Sdougb# through indent, fix up symbols and whack everything to be static.
29193141Sdougb# We use indent for two reasons: (1) to whack the Heimdal compiler's
30193141Sdougb# output into something closer to ISC's coding standard, and (2) to
31193141Sdougb# make it easier for this script to parse the result.
32193141Sdougb
33193141Sdougb# Output from this script is C code which we expect to be #included
34193141Sdougb# into another C file, which is why everything generated by this
35193141Sdougb# script is marked "static".  The intent is to minimize the number of
36193141Sdougb# extern symbols exported by the SPNEGO implementation, to avoid
37193141Sdougb# potential conflicts with the GSSAPI libraries.
38193141Sdougb
39193141Sdougb###
40193141Sdougb
41193141Sdougb# Filename of the ASN.1 specification.  Hardcoded for the moment
42193141Sdougb# since this script is intended for compiling exactly one module.
43193141Sdougb
44193141Sdougbmy $asn1_source = $ENV{ASN1_SOURCE} || "spnego.asn1";
45193141Sdougb
46193141Sdougb# Heimdal ASN.1 compiler.  This script was written using the version
47193141Sdougb# from Heimdal 0.7.1.  To build this, download a copy of
48193141Sdougb# heimdal-0.7.1.tar.gz, configure and build with the default options,
49193141Sdougb# then look for the compiler in heimdal-0.7.1/lib/asn1/asn1_compile.
50193141Sdougb
51193141Sdougbmy $asn1_compile = $ENV{ASN1_COMPILE} || "asn1_compile";
52193141Sdougb
53193141Sdougb# BSD indent program.  This script was written using the version of
54193141Sdougb# indent that comes with FreeBSD 4.11-STABLE.  The GNU project, as
55193141Sdougb# usual, couldn't resist the temptation to monkey with indent's
56193141Sdougb# command line syntax, so this probably won't work with GNU indent.
57193141Sdougb
58193141Sdougbmy $indent = $ENV{INDENT} || "indent";
59193141Sdougb
60193141Sdougb###
61193141Sdougb
62193141Sdougb# Step 1: run the compiler.  Input is the ASN.1 file.  Outputs are a
63193141Sdougb# header file (name specified on command line without the .h suffix),
64193141Sdougb# a file called "asn1_files" listing the names of the other output
65193141Sdougb# files, and a set of files containing C code generated by the
66193141Sdougb# compiler for each data type that the compiler found.
67193141Sdougb
68193141Sdougbif (! -r $asn1_source || system($asn1_compile, $asn1_source, "asn1")) {
69193141Sdougb    die("Couldn't compile ASN.1 source file $asn1_source\n");
70193141Sdougb}
71193141Sdougb
72193141Sdougbmy @files = ("asn1.h");
73193141Sdougb
74193141Sdougbopen(F, "asn1_files")
75193141Sdougb    or die("Couldn't open asn1_files: $!\n");
76193141Sdougbpush(@files, split)
77193141Sdougb    while (<F>);
78193141Sdougbclose(F);
79193141Sdougb
80193141Sdougbunlink("asn1_files");
81193141Sdougb
82193141Sdougb###
83193141Sdougb
84193141Sdougb# Step 2: generate header block.
85193141Sdougb
86193141Sdougbprint(q~/*
87193141Sdougb * Copyright (C) 2006  Internet Systems Consortium, Inc. ("ISC")
88193141Sdougb *
89193141Sdougb * Permission to use, copy, modify, and distribute this software for any
90193141Sdougb * purpose with or without fee is hereby granted, provided that the above
91193141Sdougb * copyright notice and this permission notice appear in all copies.
92193141Sdougb *
93193141Sdougb * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
94193141Sdougb * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
95193141Sdougb * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
96193141Sdougb * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
97193141Sdougb * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
98193141Sdougb * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
99193141Sdougb * PERFORMANCE OF THIS SOFTWARE.
100193141Sdougb */
101193141Sdougb
102234010Sdougb/* $Id: spnego_asn1.pl,v 1.4 2007/06/19 23:47:16 tbox Exp $ */
103193141Sdougb
104193141Sdougb/*! \file
105193141Sdougb * \brief Method routines generated from SPNEGO ASN.1 module.
106193141Sdougb * See spnego_asn1.pl for details.  Do not edit.
107193141Sdougb */
108193141Sdougb
109193141Sdougb~);
110193141Sdougb
111193141Sdougb###
112193141Sdougb
113193141Sdougb# Step 3: read and process each generated file, then delete it.
114193141Sdougb
115193141Sdougbmy $output;
116193141Sdougb
117193141Sdougbfor my $file (@files) {
118193141Sdougb
119193141Sdougb    my $is_static = 0;
120193141Sdougb
121193141Sdougb    system($indent, "-di1", "-ldi1", $file) == 0
122193141Sdougb	or die("Couldn't indent $file");
123193141Sdougb
124193141Sdougb    unlink("$file.BAK");
125193141Sdougb
126193141Sdougb    open(F, $file)
127193141Sdougb	or die("Couldn't open $file: $!");
128193141Sdougb
129193141Sdougb    while (<F>) {
130193141Sdougb
131193141Sdougb	# Symbol name fixups
132193141Sdougb
133193141Sdougb	s/heim_general_string/general_string/g;
134193141Sdougb	s/heim_octet_string/octet_string/g;
135193141Sdougb	s/heim_oid/oid/g;
136193141Sdougb	s/heim_utf8_string/utf8_string/g;
137193141Sdougb
138193141Sdougb	# Convert all externs to statics
139193141Sdougb
140193141Sdougb	if (/^static/) {
141193141Sdougb	    $is_static = 1;
142193141Sdougb	}
143193141Sdougb
144193141Sdougb	if (!/^typedef/ &&
145193141Sdougb	    !$is_static &&
146193141Sdougb	    /^[A-Za-z_][0-9A-Za-z_]*[ \t]*($|[^:0-9A-Za-z_])/) {
147193141Sdougb	    $_ = "static " . $_;
148193141Sdougb	    $is_static = 1;
149193141Sdougb	}
150193141Sdougb
151193141Sdougb	if (/[{};]/) {
152193141Sdougb	    $is_static = 0;
153193141Sdougb	}
154193141Sdougb
155193141Sdougb	# Suppress file inclusion, pass anything else through
156193141Sdougb
157193141Sdougb	if (!/#include/) {
158193141Sdougb	    $output .= $_;
159193141Sdougb	}
160193141Sdougb    }
161193141Sdougb
162193141Sdougb    close(F);
163193141Sdougb    unlink($file);
164193141Sdougb}
165193141Sdougb
166193141Sdougb# Step 4: Delete unused stuff to avoid code bloat and compiler warnings.
167193141Sdougb
168193141Sdougbmy @unused_functions = qw(ContextFlags2int
169193141Sdougb			  int2ContextFlags
170193141Sdougb			  asn1_ContextFlags_units
171193141Sdougb			  length_NegTokenInit
172193141Sdougb			  copy_NegTokenInit
173193141Sdougb			  length_NegTokenResp
174193141Sdougb			  copy_NegTokenResp
175193141Sdougb			  length_MechTypeList
176193141Sdougb			  length_MechType
177193141Sdougb			  copy_MechTypeList
178193141Sdougb			  length_ContextFlags
179193141Sdougb			  copy_ContextFlags
180193141Sdougb			  copy_MechType);
181193141Sdougb
182193141Sdougb$output =~ s<^static [^\n]+\n$_\(.+?^}></* unused function: $_ */\n>ms
183193141Sdougb    foreach (@unused_functions);
184193141Sdougb
185193141Sdougb$output =~ s<^static .+$_\(.*\);$></* unused declaration: $_ */>m
186193141Sdougb    foreach (@unused_functions);
187193141Sdougb
188193141Sdougb$output =~ s<^static struct units ContextFlags_units\[\].+?^};>
189193141Sdougb            </* unused variable: ContextFlags_units */>ms;
190193141Sdougb
191193141Sdougb$output =~ s<^static int asn1_NegotiationToken_dummy_holder = 1;>
192193141Sdougb            </* unused variable: asn1_NegotiationToken_dummy_holder */>ms;
193193141Sdougb
194193141Sdougb$output =~ s<^static void\nfree_ContextFlags\(ContextFlags \* data\)\n{\n>
195193141Sdougb            <$&\t(void)data;\n>ms;
196193141Sdougb
197193141Sdougb# Step 5: Write the result.
198193141Sdougb
199193141Sdougbprint($output);
200193141Sdougb
201