138494Sobrien#!/bin/bin/perl -w
2174294Sobrien#
338494Sobrien# Copyright (C) 2006, 2007, 2012  Internet Systems Consortium, Inc. ("ISC")
438494Sobrien#
538494Sobrien# Permission to use, copy, modify, and/or distribute this software for any
638494Sobrien# purpose with or without fee is hereby granted, provided that the above
738494Sobrien# copyright notice and this permission notice appear in all copies.
838494Sobrien#
938494Sobrien# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
1038494Sobrien# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1138494Sobrien# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
1238494Sobrien# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1338494Sobrien# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
1438494Sobrien# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1538494Sobrien# PERFORMANCE OF THIS SOFTWARE.
1638494Sobrien
1738494Sobrien# $Id: spnego_asn1.pl,v 1.4 2007/06/19 23:47:16 tbox Exp $
1838494Sobrien
1938494Sobrien# Our SPNEGO implementation uses some functions generated by the
2042629Sobrien# Heimdal ASN.1 compiler, which this script then whacks a bit to make
2138494Sobrien# them work properly in this stripped down implementation.  We don't
2238494Sobrien# want to require our users to have a copy of the compiler, so we ship
2338494Sobrien# the output of this script, but we need to keep the script around in
2438494Sobrien# any case to cope with future changes to the SPNEGO ASN.1 code, so we
2538494Sobrien# might as well supply the script for users who want it.
2638494Sobrien
2738494Sobrien# Overall plan: run the ASN.1 compiler, run each of its output files
2838494Sobrien# through indent, fix up symbols and whack everything to be static.
2938494Sobrien# We use indent for two reasons: (1) to whack the Heimdal compiler's
3038494Sobrien# output into something closer to ISC's coding standard, and (2) to
3138494Sobrien# make it easier for this script to parse the result.
3238494Sobrien
3338494Sobrien# Output from this script is C code which we expect to be #included
3438494Sobrien# into another C file, which is why everything generated by this
3538494Sobrien# script is marked "static".  The intent is to minimize the number of
3638494Sobrien# extern symbols exported by the SPNEGO implementation, to avoid
3738494Sobrien# potential conflicts with the GSSAPI libraries.
3838494Sobrien
3938494Sobrien###
40174294Sobrien
4138494Sobrien# Filename of the ASN.1 specification.  Hardcoded for the moment
4238494Sobrien# since this script is intended for compiling exactly one module.
4338494Sobrien
4438494Sobrienmy $asn1_source = $ENV{ASN1_SOURCE} || "spnego.asn1";
4538494Sobrien
4638494Sobrien# Heimdal ASN.1 compiler.  This script was written using the version
4738494Sobrien# from Heimdal 0.7.1.  To build this, download a copy of
4838494Sobrien# heimdal-0.7.1.tar.gz, configure and build with the default options,
4938494Sobrien# then look for the compiler in heimdal-0.7.1/lib/asn1/asn1_compile.
5038494Sobrien
5138494Sobrienmy $asn1_compile = $ENV{ASN1_COMPILE} || "asn1_compile";
5238494Sobrien
5338494Sobrien# BSD indent program.  This script was written using the version of
54174294Sobrien# indent that comes with FreeBSD 4.11-STABLE.  The GNU project, as
5538494Sobrien# usual, couldn't resist the temptation to monkey with indent's
5638494Sobrien# command line syntax, so this probably won't work with GNU indent.
57174294Sobrien
58174294Sobrienmy $indent = $ENV{INDENT} || "indent";
59174294Sobrien
60174294Sobrien###
6138494Sobrien
6238494Sobrien# Step 1: run the compiler.  Input is the ASN.1 file.  Outputs are a
6338494Sobrien# header file (name specified on command line without the .h suffix),
6438494Sobrien# a file called "asn1_files" listing the names of the other output
6538494Sobrien# files, and a set of files containing C code generated by the
66174294Sobrien# compiler for each data type that the compiler found.
67174294Sobrien
68174294Sobrienif (! -r $asn1_source || system($asn1_compile, $asn1_source, "asn1")) {
69174294Sobrien    die("Couldn't compile ASN.1 source file $asn1_source\n");
70174294Sobrien}
71174294Sobrien
72174294Sobrienmy @files = ("asn1.h");
73174294Sobrien
74174294Sobrienopen(F, "asn1_files")
75174294Sobrien    or die("Couldn't open asn1_files: $!\n");
76174294Sobrienpush(@files, split)
77174294Sobrien    while (<F>);
78174294Sobrienclose(F);
79174294Sobrien
8038494Sobrienunlink("asn1_files");
8138494Sobrien
8238494Sobrien###
83174294Sobrien
8438494Sobrien# Step 2: generate header block.
8538494Sobrien
8638494Sobrienprint(q~/*
87174294Sobrien * Copyright (C) 2006  Internet Systems Consortium, Inc. ("ISC")
8838494Sobrien *
89174294Sobrien * Permission to use, copy, modify, and distribute this software for any
9038494Sobrien * purpose with or without fee is hereby granted, provided that the above
91174294Sobrien * copyright notice and this permission notice appear in all copies.
9238494Sobrien *
9338494Sobrien * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9438494Sobrien * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9538494Sobrien * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
9638494Sobrien * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
9738494Sobrien * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
9838494Sobrien * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
9938494Sobrien * PERFORMANCE OF THIS SOFTWARE.
10038494Sobrien */
10138494Sobrien
10238494Sobrien/* $Id: spnego_asn1.pl,v 1.4 2007/06/19 23:47:16 tbox Exp $ */
10338494Sobrien
10438494Sobrien/*! \file
10538494Sobrien * \brief Method routines generated from SPNEGO ASN.1 module.
10638494Sobrien * See spnego_asn1.pl for details.  Do not edit.
10738494Sobrien */
10838494Sobrien
10938494Sobrien~);
11038494Sobrien
11138494Sobrien###
11238494Sobrien
11338494Sobrien# Step 3: read and process each generated file, then delete it.
11438494Sobrien
11538494Sobrienmy $output;
11638494Sobrien
11738494Sobrienfor my $file (@files) {
11838494Sobrien
11938494Sobrien    my $is_static = 0;
12038494Sobrien
121174294Sobrien    system($indent, "-di1", "-ldi1", $file) == 0
122174294Sobrien	or die("Couldn't indent $file");
12338494Sobrien
12438494Sobrien    unlink("$file.BAK");
12538494Sobrien
12638494Sobrien    open(F, $file)
12738494Sobrien	or die("Couldn't open $file: $!");
12838494Sobrien
12938494Sobrien    while (<F>) {
13038494Sobrien
13138494Sobrien	# Symbol name fixups
13238494Sobrien
13338494Sobrien	s/heim_general_string/general_string/g;
13438494Sobrien	s/heim_octet_string/octet_string/g;
13538494Sobrien	s/heim_oid/oid/g;
13638494Sobrien	s/heim_utf8_string/utf8_string/g;
13738494Sobrien
13838494Sobrien	# Convert all externs to statics
13938494Sobrien
140174294Sobrien	if (/^static/) {
141174294Sobrien	    $is_static = 1;
142174294Sobrien	}
14338494Sobrien
14438494Sobrien	if (!/^typedef/ &&
14538494Sobrien	    !$is_static &&
14638494Sobrien	    /^[A-Za-z_][0-9A-Za-z_]*[ \t]*($|[^:0-9A-Za-z_])/) {
14738494Sobrien	    $_ = "static " . $_;
14838494Sobrien	    $is_static = 1;
14938494Sobrien	}
15038494Sobrien
15138494Sobrien	if (/[{};]/) {
15238494Sobrien	    $is_static = 0;
15338494Sobrien	}
15438494Sobrien
15538494Sobrien	# Suppress file inclusion, pass anything else through
15638494Sobrien
15738494Sobrien	if (!/#include/) {
15838494Sobrien	    $output .= $_;
15938494Sobrien	}
16038494Sobrien    }
16138494Sobrien
16238494Sobrien    close(F);
16338494Sobrien    unlink($file);
16438494Sobrien}
16538494Sobrien
16638494Sobrien# Step 4: Delete unused stuff to avoid code bloat and compiler warnings.
16738494Sobrien
16838494Sobrienmy @unused_functions = qw(ContextFlags2int
16982794Sobrien			  int2ContextFlags
17082794Sobrien			  asn1_ContextFlags_units
17182794Sobrien			  length_NegTokenInit
17238494Sobrien			  copy_NegTokenInit
17338494Sobrien			  length_NegTokenResp
17438494Sobrien			  copy_NegTokenResp
17538494Sobrien			  length_MechTypeList
176174294Sobrien			  length_MechType
177174294Sobrien			  copy_MechTypeList
178174294Sobrien			  length_ContextFlags
179174294Sobrien			  copy_ContextFlags
180174294Sobrien			  copy_MechType);
181174294Sobrien
182174294Sobrien$output =~ s<^static [^\n]+\n$_\(.+?^}></* unused function: $_ */\n>ms
18338494Sobrien    foreach (@unused_functions);
18438494Sobrien
18538494Sobrien$output =~ s<^static .+$_\(.*\);$></* unused declaration: $_ */>m
18638494Sobrien    foreach (@unused_functions);
18738494Sobrien
18838494Sobrien$output =~ s<^static struct units ContextFlags_units\[\].+?^};>
189174294Sobrien            </* unused variable: ContextFlags_units */>ms;
19038494Sobrien
19138494Sobrien$output =~ s<^static int asn1_NegotiationToken_dummy_holder = 1;>
19238494Sobrien            </* unused variable: asn1_NegotiationToken_dummy_holder */>ms;
19338494Sobrien
19438494Sobrien$output =~ s<^static void\nfree_ContextFlags\(ContextFlags \* data\)\n{\n>
19538494Sobrien            <$&\t(void)data;\n>ms;
19638494Sobrien
19738494Sobrien# Step 5: Write the result.
19838494Sobrien
19938494Sobrienprint($output);
20038494Sobrien
20138494Sobrien