6004853bc166ef1614e8cf26b7339711ccd42751
[openssl.git] / util / mkerr.pl
1 #!/usr/local/bin/perl -w
2
3 my $config = "crypto/err/openssl.ec";
4 my $hprefix = "openssl/";
5 my $debug = 0;
6 my $rebuild = 0;
7 my $static = 1;
8 my $recurse = 0;
9 my $reindex = 0;
10 my $dowrite = 0;
11 my $staticloader = "";
12
13 my $pack_errcode;
14 my $load_errcode;
15
16 my $errcount;
17
18 while (@ARGV) {
19         my $arg = $ARGV[0];
20         if($arg eq "-conf") {
21                 shift @ARGV;
22                 $config = shift @ARGV;
23         } elsif($arg eq "-hprefix") {
24                 shift @ARGV;
25                 $hprefix = shift @ARGV;
26         } elsif($arg eq "-debug") {
27                 $debug = 1;
28                 shift @ARGV;
29         } elsif($arg eq "-rebuild") {
30                 $rebuild = 1;
31                 shift @ARGV;
32         } elsif($arg eq "-recurse") {
33                 $recurse = 1;
34                 shift @ARGV;
35         } elsif($arg eq "-reindex") {
36                 $reindex = 1;
37                 shift @ARGV;
38         } elsif($arg eq "-nostatic") {
39                 $static = 0;
40                 shift @ARGV;
41         } elsif($arg eq "-staticloader") {
42                 $staticloader = "static ";
43                 shift @ARGV;
44         } elsif($arg eq "-write") {
45                 $dowrite = 1;
46                 shift @ARGV;
47         } else {
48                 last;
49         }
50 }
51
52 if($recurse) {
53         @source = (<crypto/*.c>, <crypto/*/*.c>, <ssl/*.c>);
54 } else {
55         @source = @ARGV;
56 }
57
58 # Read in the config file
59
60 open(IN, "<$config") || die "Can't open config file $config";
61
62 # Parse config file
63
64 while(<IN>)
65 {
66         if(/^L\s+(\S+)\s+(\S+)\s+(\S+)/) {
67                 $hinc{$1} = $2;
68                 $libinc{$2} = $1;
69                 $cskip{$3} = $1;
70                 if($3 ne "NONE") {
71                         $csrc{$1} = $3;
72                         $fmax{$1} = 99;
73                         $rmax{$1} = 99;
74                         $fassigned{$1} = ":";
75                         $rassigned{$1} = ":";
76                         $fnew{$1} = 0;
77                         $rnew{$1} = 0;
78                 }
79         } elsif (/^F\s+(\S+)/) {
80         # Add extra function with $1
81         } elsif (/^R\s+(\S+)\s+(\S+)/) {
82                 $rextra{$1} = $2;
83                 $rcodes{$1} = $2;
84         }
85 }
86
87 close IN;
88
89 # Scan each header file in turn and make a list of error codes
90 # and function names
91
92 while (($hdr, $lib) = each %libinc)
93 {
94         next if($hdr eq "NONE");
95         print STDERR "Scanning header file $hdr\n" if $debug; 
96         my $line = "", $def= "", $linenr = 0, $gotfile = 0;
97         if (open(IN, "<$hdr")) {
98             $gotfile = 1;
99             while(<IN>) {
100                 $linenr++;
101                 print STDERR "line: $linenr\r" if $debug;
102
103                 last if(/BEGIN\s+ERROR\s+CODES/);
104                 if ($line ne '') {
105                     $_ = $line . $_;
106                     $line = '';
107                 }
108
109                 if (/\\$/) {
110                     $line = $_;
111                     next;
112                 }
113
114                 if(/\/\*/) {
115                     if (not /\*\//) {           # multiline comment...
116                         $line = $_;             # ... just accumulate
117                         next; 
118                     } else {
119                         s/\/\*.*?\*\///gs;      # wipe it
120                     }
121                 }
122
123                 if ($cpp) {
124                     $cpp++ if /^#\s*if/;
125                     $cpp-- if /^#\s*endif/;
126                     next;
127                 }
128                 $cpp = 1 if /^#.*ifdef.*cplusplus/;  # skip "C" declaration
129
130                 next if (/^\#/);                      # skip preprocessor directives
131
132                 s/{[^{}]*}//gs;                      # ignore {} blocks
133
134                 if (/\{|\/\*/) { # Add a } so editor works...
135                     $line = $_;
136                 } else {
137                     $def .= $_;
138                 }
139             }
140         }
141
142         print STDERR "                                  \r" if $debug;
143         $defnr = 0;
144         # Delete any DECLARE_ macros
145         $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
146         foreach (split /;/, $def) {
147             $defnr++;
148             print STDERR "def: $defnr\r" if $debug;
149
150             # The goal is to collect function names from function declarations.
151
152             s/^[\n\s]*//g;
153             s/[\n\s]*$//g;
154
155             # Skip over recognized non-function declarations
156             next if(/typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/);
157
158             # Remove STACK_OF(foo)
159             s/STACK_OF\(\w+\)/void/;
160
161             # Reduce argument lists to empty ()
162             # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
163             while(/\(.*\)/s) {
164                 s/\([^\(\)]+\)/\{\}/gs;
165                 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;    #(*f{}) -> f
166             }
167             # pretend as we didn't use curly braces: {} -> ()
168             s/\{\}/\(\)/gs;
169
170             if (/(\w+)\s*\(\).*/s) {    # first token prior [first] () is
171                 my $name = $1;          # a function name!
172                 $name =~ tr/[a-z]/[A-Z]/;
173                 $ftrans{$name} = $1;
174             } elsif (/[\(\)]/ and not (/=/)) {
175                 print STDERR "Header $hdr: cannot parse: $_;\n";
176             }
177         }
178
179         print STDERR "                                  \r" if $debug;
180
181         next if $reindex;
182
183         # Scan function and reason codes and store them: keep a note of the
184         # maximum code used.
185
186         if ($gotfile) {
187           while(<IN>) {
188                 if(/^\#define\s+(\S+)\s+(\S+)/) {
189                         $name = $1;
190                         $code = $2;
191                         next if $name =~ /^${lib}err/;
192                         unless($name =~ /^${lib}_([RF])_(\w+)$/) {
193                                 print STDERR "Invalid error code $name\n";
194                                 next;
195                         }
196                         if($1 eq "R") {
197                                 $rcodes{$name} = $code;
198                                 if ($rassigned{$lib} =~ /:$code:/) {
199                                         print STDERR "!! ERROR: $lib reason code $code assigned twice\n";
200                                         ++$errcount;
201                                 }
202                                 $rassigned{$lib} .= "$code:";
203                                 if(!(exists $rextra{$name}) &&
204                                          ($code > $rmax{$lib}) ) {
205                                         $rmax{$lib} = $code;
206                                 }
207                         } else {
208                                 if ($fassigned{$lib} =~ /:$code:/) {
209                                         print STDERR "!! ERROR: $lib function code $code assigned twice\n";
210                                         ++$errcount;
211                                 }
212                                 $fassigned{$lib} .= "$code:";
213                                 if($code > $fmax{$lib}) {
214                                         $fmax{$lib} = $code;
215                                 }
216                                 $fcodes{$name} = $code;
217                         }
218                 }
219           }
220         }
221
222         if ($debug) {
223                 if (defined($fmax{$lib})) {
224                         print STDERR "Max function code fmax" . "{" . "$lib" . "} = $fmax{$lib}\n";
225                         $fassigned{$lib} =~ m/^:(.*):$/;
226                         @fassigned = sort {$a <=> $b} split(":", $1);
227                         print STDERR "  @fassigned\n";
228                 }
229                 if (defined($rmax{$lib})) {
230                         print STDERR "Max reason code rmax" . "{" . "$lib" . "} = $rmax{$lib}\n";
231                         $rassigned{$lib} =~ m/^:(.*):$/;
232                         @rassigned = sort {$a <=> $b} split(":", $1);
233                         print STDERR "  @rassigned\n";
234                 }
235         }
236
237         if ($lib eq "SSL") {
238                 if ($rmax{$lib} >= 1000) {
239                         print STDERR "!! ERROR: SSL error codes 1000+ are reserved for alerts.\n";
240                         print STDERR "!!        Any new alerts must be added to $config.\n";
241                         ++$errcount;
242                         print STDERR "\n";
243                 }
244         }
245         close IN;
246 }
247
248 # Scan each C source file and look for function and reason codes
249 # This is done by looking for strings that "look like" function or
250 # reason codes: basically anything consisting of all upper case and
251 # numerics which has _F_ or _R_ in it and which has the name of an
252 # error library at the start. This seems to work fine except for the
253 # oddly named structure BIO_F_CTX which needs to be ignored.
254 # If a code doesn't exist in list compiled from headers then mark it
255 # with the value "X" as a place holder to give it a value later.
256 # Store all function and reason codes found in %ufcodes and %urcodes
257 # so all those unreferenced can be printed out.
258
259
260 foreach $file (@source) {
261         # Don't parse the error source file.
262         next if exists $cskip{$file};
263         print STDERR "File loaded: ".$file."\r" if $debug;
264         open(IN, "<$file") || die "Can't open source file $file\n";
265         while(<IN>) {
266                 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
267                         next unless exists $csrc{$2};
268                         next if($1 eq "BIO_F_BUFFER_CTX");
269                         $ufcodes{$1} = 1;
270                         if(!exists $fcodes{$1}) {
271                                 $fcodes{$1} = "X";
272                                 $fnew{$2}++;
273                         }
274                         $notrans{$1} = 1 unless exists $ftrans{$3};
275                 }
276                 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
277                         next unless exists $csrc{$2};
278                         $urcodes{$1} = 1;
279                         if(!exists $rcodes{$1}) {
280                                 $rcodes{$1} = "X";
281                                 $rnew{$2}++;
282                         }
283                 } 
284         }
285         close IN;
286 }
287 print STDERR "                                  \n" if $debug;
288
289 # Now process each library in turn.
290
291 foreach $lib (keys %csrc)
292 {
293         my $hfile = $hinc{$lib};
294         my $cfile = $csrc{$lib};
295         if(!$fnew{$lib} && !$rnew{$lib}) {
296                 print STDERR "$lib:\t\tNo new error codes\n";
297                 next unless $rebuild;
298         } else {
299                 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
300                 print STDERR " $rnew{$lib} New Reasons.\n";
301                 next unless $dowrite;
302         }
303
304         # If we get here then we have some new error codes so we
305         # need to rebuild the header file and C file.
306
307         # Make a sorted list of error and reason codes for later use.
308
309         my @function = sort grep(/^${lib}_/,keys %fcodes);
310         my @reasons = sort grep(/^${lib}_/,keys %rcodes);
311
312         # Rewrite the header file
313
314         if (open(IN, "<$hfile")) {
315             # Copy across the old file
316             while(<IN>) {
317                 push @out, $_;
318                 last if (/BEGIN ERROR CODES/);
319             }
320             close IN;
321         } else {
322             push @out,
323 "/* ====================================================================\n",
324 " * Copyright (c) 2001-2008 The OpenSSL Project.  All rights reserved.\n",
325 " *\n",
326 " * Redistribution and use in source and binary forms, with or without\n",
327 " * modification, are permitted provided that the following conditions\n",
328 " * are met:\n",
329 " *\n",
330 " * 1. Redistributions of source code must retain the above copyright\n",
331 " *    notice, this list of conditions and the following disclaimer. \n",
332 " *\n",
333 " * 2. Redistributions in binary form must reproduce the above copyright\n",
334 " *    notice, this list of conditions and the following disclaimer in\n",
335 " *    the documentation and/or other materials provided with the\n",
336 " *    distribution.\n",
337 " *\n",
338 " * 3. All advertising materials mentioning features or use of this\n",
339 " *    software must display the following acknowledgment:\n",
340 " *    \"This product includes software developed by the OpenSSL Project\n",
341 " *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
342 " *\n",
343 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
344 " *    endorse or promote products derived from this software without\n",
345 " *    prior written permission. For written permission, please contact\n",
346 " *    openssl-core\@openssl.org.\n",
347 " *\n",
348 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
349 " *    nor may \"OpenSSL\" appear in their names without prior written\n",
350 " *    permission of the OpenSSL Project.\n",
351 " *\n",
352 " * 6. Redistributions of any form whatsoever must retain the following\n",
353 " *    acknowledgment:\n",
354 " *    \"This product includes software developed by the OpenSSL Project\n",
355 " *    for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
356 " *\n",
357 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
358 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
359 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
360 " * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
361 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
362 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
363 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
364 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
365 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
366 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
367 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
368 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
369 " * ====================================================================\n",
370 " *\n",
371 " * This product includes cryptographic software written by Eric Young\n",
372 " * (eay\@cryptsoft.com).  This product includes software written by Tim\n",
373 " * Hudson (tjh\@cryptsoft.com).\n",
374 " *\n",
375 " */\n",
376 "\n",
377 "#ifndef HEADER_${lib}_ERR_H\n",
378 "#define HEADER_${lib}_ERR_H\n",
379 "\n",
380 "#ifdef  __cplusplus\n",
381 "extern \"C\" {\n",
382 "#endif\n",
383 "\n",
384 "/* BEGIN ERROR CODES */\n";
385         }
386         open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
387
388         print OUT @out;
389         undef @out;
390         print OUT <<"EOF";
391 /* The following lines are auto generated by the script mkerr.pl. Any changes
392  * made after this point may be overwritten when the script is next run.
393  */
394 EOF
395         if($static) {
396                 print OUT <<"EOF";
397 ${staticloader}void ERR_load_${lib}_strings(void);
398
399 EOF
400         } else {
401                 print OUT <<"EOF";
402 ${staticloader}void ERR_load_${lib}_strings(void);
403 ${staticloader}void ERR_unload_${lib}_strings(void);
404 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
405 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
406
407 EOF
408         }
409         print OUT <<"EOF";
410 /* Error codes for the $lib functions. */
411
412 /* Function codes. */
413 EOF
414
415         foreach $i (@function) {
416                 $z=6-int(length($i)/8);
417                 if($fcodes{$i} eq "X") {
418                         $fassigned{$lib} =~ m/^:([^:]*):/;
419                         $findcode = $1;
420                         if (!defined($findcode)) {
421                                 $findcode = $fmax{$lib};
422                         }
423                         while ($fassigned{$lib} =~ m/:$findcode:/) {
424                                 $findcode++;
425                         }
426                         $fcodes{$i} = $findcode;
427                         $fassigned{$lib} .= "$findcode:";
428                         print STDERR "New Function code $i\n" if $debug;
429                 }
430                 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
431         }
432
433         print OUT "\n/* Reason codes. */\n";
434
435         foreach $i (@reasons) {
436                 $z=6-int(length($i)/8);
437                 if($rcodes{$i} eq "X") {
438                         $rassigned{$lib} =~ m/^:([^:]*):/;
439                         $findcode = $1;
440                         if (!defined($findcode)) {
441                                 $findcode = $rmax{$lib};
442                         }
443                         while ($rassigned{$lib} =~ m/:$findcode:/) {
444                                 $findcode++;
445                         }
446                         $rcodes{$i} = $findcode;
447                         $rassigned{$lib} .= "$findcode:";
448                         print STDERR "New Reason code   $i\n" if $debug;
449                 }
450                 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
451         }
452         print OUT <<"EOF";
453
454 #ifdef  __cplusplus
455 }
456 #endif
457 #endif
458 EOF
459         close OUT;
460
461         # Rewrite the C source file containing the error details.
462
463         # First, read any existing reason string definitions:
464         my %err_reason_strings;
465         if (open(IN,"<$cfile")) {
466                 while (<IN>) {
467                         if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
468                                 $err_reason_strings{$1} = $2;
469                         }
470                         if (/\b${lib}_F_(\w*)\b.*\"(.*)\"/) {
471                                 if (!exists $ftrans{$1} && ($1 ne $2)) {
472                                         print STDERR "WARNING: Mismatched function string $2\n";
473                                         $ftrans{$1} = $2;
474                                 }
475                         }
476                 }
477                 close(IN);
478         }
479
480
481         my $hincf;
482         if($static) {
483                 $hfile =~ /([^\/]+)$/;
484                 $hincf = "<${hprefix}$1>";
485         } else {
486                 $hincf = "\"$hfile\"";
487         }
488
489         # If static we know the error code at compile time so use it
490         # in error definitions.
491
492         if ($static)
493                 {
494                 $pack_errcode = "ERR_LIB_${lib}";
495                 $load_errcode = "0";
496                 }
497         else
498                 {
499                 $pack_errcode = "0";
500                 $load_errcode = "ERR_LIB_${lib}";
501                 }
502
503
504         open (OUT,">$cfile") || die "Can't open $cfile for writing";
505
506         print OUT <<"EOF";
507 /* $cfile */
508 /* ====================================================================
509  * Copyright (c) 1999-2007 The OpenSSL Project.  All rights reserved.
510  *
511  * Redistribution and use in source and binary forms, with or without
512  * modification, are permitted provided that the following conditions
513  * are met:
514  *
515  * 1. Redistributions of source code must retain the above copyright
516  *    notice, this list of conditions and the following disclaimer. 
517  *
518  * 2. Redistributions in binary form must reproduce the above copyright
519  *    notice, this list of conditions and the following disclaimer in
520  *    the documentation and/or other materials provided with the
521  *    distribution.
522  *
523  * 3. All advertising materials mentioning features or use of this
524  *    software must display the following acknowledgment:
525  *    "This product includes software developed by the OpenSSL Project
526  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
527  *
528  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
529  *    endorse or promote products derived from this software without
530  *    prior written permission. For written permission, please contact
531  *    openssl-core\@OpenSSL.org.
532  *
533  * 5. Products derived from this software may not be called "OpenSSL"
534  *    nor may "OpenSSL" appear in their names without prior written
535  *    permission of the OpenSSL Project.
536  *
537  * 6. Redistributions of any form whatsoever must retain the following
538  *    acknowledgment:
539  *    "This product includes software developed by the OpenSSL Project
540  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
541  *
542  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
543  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
544  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
545  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
546  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
547  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
548  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
549  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
550  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
551  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
552  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
553  * OF THE POSSIBILITY OF SUCH DAMAGE.
554  * ====================================================================
555  *
556  * This product includes cryptographic software written by Eric Young
557  * (eay\@cryptsoft.com).  This product includes software written by Tim
558  * Hudson (tjh\@cryptsoft.com).
559  *
560  */
561
562 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
563  * made to it will be overwritten when the script next updates this file,
564  * only reason strings will be preserved.
565  */
566
567 #include <stdio.h>
568 #include <openssl/err.h>
569 #include $hincf
570
571 /* BEGIN ERROR CODES */
572 #ifndef OPENSSL_NO_ERR
573
574 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
575 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
576
577 static ERR_STRING_DATA ${lib}_str_functs[]=
578         {
579 EOF
580         # Add each function code: if a function name is found then use it.
581         foreach $i (@function) {
582                 my $fn;
583                 $i =~ /^${lib}_F_(\S+)$/;
584                 $fn = $1;
585                 if(exists $ftrans{$fn}) {
586                         $fn = $ftrans{$fn};
587                 }
588 #               print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
589                 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
590         }
591         print OUT <<"EOF";
592 {0,NULL}
593         };
594
595 static ERR_STRING_DATA ${lib}_str_reasons[]=
596         {
597 EOF
598         # Add each reason code.
599         foreach $i (@reasons) {
600                 my $rn;
601                 my $rstr = "ERR_REASON($i)";
602                 my $nspc = 0;
603                 if (exists $err_reason_strings{$i}) {
604                         $rn = $err_reason_strings{$i};
605                 } else {
606                         $i =~ /^${lib}_R_(\S+)$/;
607                         $rn = $1;
608                         $rn =~ tr/_[A-Z]/ [a-z]/;
609                 }
610                 $nspc = 40 - length($rstr) unless length($rstr) > 40;
611                 $nspc = " " x $nspc;
612                 print OUT "{${rstr}${nspc},\"$rn\"},\n";
613         }
614 if($static) {
615         print OUT <<"EOF";
616 {0,NULL}
617         };
618
619 #endif
620
621 ${staticloader}void ERR_load_${lib}_strings(void)
622         {
623 #ifndef OPENSSL_NO_ERR
624
625         if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL)
626                 {
627                 ERR_load_strings($load_errcode,${lib}_str_functs);
628                 ERR_load_strings($load_errcode,${lib}_str_reasons);
629                 }
630 #endif
631         }
632 EOF
633 } else {
634         print OUT <<"EOF";
635 {0,NULL}
636         };
637
638 #endif
639
640 #ifdef ${lib}_LIB_NAME
641 static ERR_STRING_DATA ${lib}_lib_name[]=
642         {
643 {0      ,${lib}_LIB_NAME},
644 {0,NULL}
645         };
646 #endif
647
648
649 static int ${lib}_lib_error_code=0;
650 static int ${lib}_error_init=1;
651
652 ${staticloader}void ERR_load_${lib}_strings(void)
653         {
654         if (${lib}_lib_error_code == 0)
655                 ${lib}_lib_error_code=ERR_get_next_error_library();
656
657         if (${lib}_error_init)
658                 {
659                 ${lib}_error_init=0;
660 #ifndef OPENSSL_NO_ERR
661                 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
662                 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
663 #endif
664
665 #ifdef ${lib}_LIB_NAME
666                 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
667                 ERR_load_strings(0,${lib}_lib_name);
668 #endif
669                 }
670         }
671
672 ${staticloader}void ERR_unload_${lib}_strings(void)
673         {
674         if (${lib}_error_init == 0)
675                 {
676 #ifndef OPENSSL_NO_ERR
677                 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
678                 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
679 #endif
680
681 #ifdef ${lib}_LIB_NAME
682                 ERR_unload_strings(0,${lib}_lib_name);
683 #endif
684                 ${lib}_error_init=1;
685                 }
686         }
687
688 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
689         {
690         if (${lib}_lib_error_code == 0)
691                 ${lib}_lib_error_code=ERR_get_next_error_library();
692         ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
693         }
694 EOF
695
696 }
697
698         close OUT;
699         undef %err_reason_strings;
700 }
701
702 if($debug && defined(%notrans)) {
703         print STDERR "The following function codes were not translated:\n";
704         foreach(sort keys %notrans)
705         {
706                 print STDERR "$_\n";
707         }
708 }
709
710 # Make a list of unreferenced function and reason codes
711
712 foreach (keys %fcodes) {
713         push (@funref, $_) unless exists $ufcodes{$_};
714 }
715
716 foreach (keys %rcodes) {
717         push (@runref, $_) unless exists $urcodes{$_};
718 }
719
720 if($debug && defined(@funref) ) {
721         print STDERR "The following function codes were not referenced:\n";
722         foreach(sort @funref)
723         {
724                 print STDERR "$_\n";
725         }
726 }
727
728 if($debug && defined(@runref) ) {
729         print STDERR "The following reason codes were not referenced:\n";
730         foreach(sort @runref)
731         {
732                 print STDERR "$_\n";
733         }
734 }
735
736 if($errcount) {
737         print STDERR "There were errors, failing...\n\n";
738         exit $errcount;
739 }
740