/contrib/bind9/lib/dns/spnego_asn1.pl

https://bitbucket.org/freebsd/freebsd-head/ · Perl · 200 lines · 90 code · 51 blank · 59 comment · 15 complexity · 4cff024ca7da3664debde2cbd016a826 MD5 · raw file

  1. #!/bin/bin/perl -w
  2. #
  3. # Copyright (C) 2006, 2007 Internet Systems Consortium, Inc. ("ISC")
  4. #
  5. # Permission to use, copy, modify, and/or distribute this software for any
  6. # purpose with or without fee is hereby granted, provided that the above
  7. # copyright notice and this permission notice appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. # PERFORMANCE OF THIS SOFTWARE.
  16. # $Id: spnego_asn1.pl,v 1.4 2007/06/19 23:47:16 tbox Exp $
  17. # Our SPNEGO implementation uses some functions generated by the
  18. # Heimdal ASN.1 compiler, which this script then whacks a bit to make
  19. # them work properly in this stripped down implementation. We don't
  20. # want to require our users to have a copy of the compiler, so we ship
  21. # the output of this script, but we need to keep the script around in
  22. # any case to cope with future changes to the SPNEGO ASN.1 code, so we
  23. # might as well supply the script for users who want it.
  24. # Overall plan: run the ASN.1 compiler, run each of its output files
  25. # through indent, fix up symbols and whack everything to be static.
  26. # We use indent for two reasons: (1) to whack the Heimdal compiler's
  27. # output into something closer to ISC's coding standard, and (2) to
  28. # make it easier for this script to parse the result.
  29. # Output from this script is C code which we expect to be #included
  30. # into another C file, which is why everything generated by this
  31. # script is marked "static". The intent is to minimize the number of
  32. # extern symbols exported by the SPNEGO implementation, to avoid
  33. # potential conflicts with the GSSAPI libraries.
  34. ###
  35. # Filename of the ASN.1 specification. Hardcoded for the moment
  36. # since this script is intended for compiling exactly one module.
  37. my $asn1_source = $ENV{ASN1_SOURCE} || "spnego.asn1";
  38. # Heimdal ASN.1 compiler. This script was written using the version
  39. # from Heimdal 0.7.1. To build this, download a copy of
  40. # heimdal-0.7.1.tar.gz, configure and build with the default options,
  41. # then look for the compiler in heimdal-0.7.1/lib/asn1/asn1_compile.
  42. my $asn1_compile = $ENV{ASN1_COMPILE} || "asn1_compile";
  43. # BSD indent program. This script was written using the version of
  44. # indent that comes with FreeBSD 4.11-STABLE. The GNU project, as
  45. # usual, couldn't resist the temptation to monkey with indent's
  46. # command line syntax, so this probably won't work with GNU indent.
  47. my $indent = $ENV{INDENT} || "indent";
  48. ###
  49. # Step 1: run the compiler. Input is the ASN.1 file. Outputs are a
  50. # header file (name specified on command line without the .h suffix),
  51. # a file called "asn1_files" listing the names of the other output
  52. # files, and a set of files containing C code generated by the
  53. # compiler for each data type that the compiler found.
  54. if (! -r $asn1_source || system($asn1_compile, $asn1_source, "asn1")) {
  55. die("Couldn't compile ASN.1 source file $asn1_source\n");
  56. }
  57. my @files = ("asn1.h");
  58. open(F, "asn1_files")
  59. or die("Couldn't open asn1_files: $!\n");
  60. push(@files, split)
  61. while (<F>);
  62. close(F);
  63. unlink("asn1_files");
  64. ###
  65. # Step 2: generate header block.
  66. print(q~/*
  67. * Copyright (C) 2006 Internet Systems Consortium, Inc. ("ISC")
  68. *
  69. * Permission to use, copy, modify, and distribute this software for any
  70. * purpose with or without fee is hereby granted, provided that the above
  71. * copyright notice and this permission notice appear in all copies.
  72. *
  73. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  74. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  75. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  76. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  77. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  78. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  79. * PERFORMANCE OF THIS SOFTWARE.
  80. */
  81. /* $Id: spnego_asn1.pl,v 1.4 2007/06/19 23:47:16 tbox Exp $ */
  82. /*! \file
  83. * \brief Method routines generated from SPNEGO ASN.1 module.
  84. * See spnego_asn1.pl for details. Do not edit.
  85. */
  86. ~);
  87. ###
  88. # Step 3: read and process each generated file, then delete it.
  89. my $output;
  90. for my $file (@files) {
  91. my $is_static = 0;
  92. system($indent, "-di1", "-ldi1", $file) == 0
  93. or die("Couldn't indent $file");
  94. unlink("$file.BAK");
  95. open(F, $file)
  96. or die("Couldn't open $file: $!");
  97. while (<F>) {
  98. # Symbol name fixups
  99. s/heim_general_string/general_string/g;
  100. s/heim_octet_string/octet_string/g;
  101. s/heim_oid/oid/g;
  102. s/heim_utf8_string/utf8_string/g;
  103. # Convert all externs to statics
  104. if (/^static/) {
  105. $is_static = 1;
  106. }
  107. if (!/^typedef/ &&
  108. !$is_static &&
  109. /^[A-Za-z_][0-9A-Za-z_]*[ \t]*($|[^:0-9A-Za-z_])/) {
  110. $_ = "static " . $_;
  111. $is_static = 1;
  112. }
  113. if (/[{};]/) {
  114. $is_static = 0;
  115. }
  116. # Suppress file inclusion, pass anything else through
  117. if (!/#include/) {
  118. $output .= $_;
  119. }
  120. }
  121. close(F);
  122. unlink($file);
  123. }
  124. # Step 4: Delete unused stuff to avoid code bloat and compiler warnings.
  125. my @unused_functions = qw(ContextFlags2int
  126. int2ContextFlags
  127. asn1_ContextFlags_units
  128. length_NegTokenInit
  129. copy_NegTokenInit
  130. length_NegTokenResp
  131. copy_NegTokenResp
  132. length_MechTypeList
  133. length_MechType
  134. copy_MechTypeList
  135. length_ContextFlags
  136. copy_ContextFlags
  137. copy_MechType);
  138. $output =~ s<^static [^\n]+\n$_\(.+?^}></* unused function: $_ */\n>ms
  139. foreach (@unused_functions);
  140. $output =~ s<^static .+$_\(.*\);$></* unused declaration: $_ */>m
  141. foreach (@unused_functions);
  142. $output =~ s<^static struct units ContextFlags_units\[\].+?^};>
  143. </* unused variable: ContextFlags_units */>ms;
  144. $output =~ s<^static int asn1_NegotiationToken_dummy_holder = 1;>
  145. </* unused variable: asn1_NegotiationToken_dummy_holder */>ms;
  146. $output =~ s<^static void\nfree_ContextFlags\(ContextFlags \* data\)\n{\n>
  147. <$&\t(void)data;\n>ms;
  148. # Step 5: Write the result.
  149. print($output);