Update CHANGES. Keep ordinals consistent.
[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-2007 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 "/* BEGIN ERROR CODES */\n";
381         }
382         open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
383
384         print OUT @out;
385         undef @out;
386         print OUT <<"EOF";
387 /* The following lines are auto generated by the script mkerr.pl. Any changes
388  * made after this point may be overwritten when the script is next run.
389  */
390 EOF
391         if($static) {
392                 print OUT <<"EOF";
393 ${staticloader}void ERR_load_${lib}_strings(void);
394
395 EOF
396         } else {
397                 print OUT <<"EOF";
398 ${staticloader}void ERR_load_${lib}_strings(void);
399 ${staticloader}void ERR_unload_${lib}_strings(void);
400 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
401 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
402
403 EOF
404         }
405         print OUT <<"EOF";
406 /* Error codes for the $lib functions. */
407
408 /* Function codes. */
409 EOF
410
411         foreach $i (@function) {
412                 $z=6-int(length($i)/8);
413                 if($fcodes{$i} eq "X") {
414                         $fassigned{$lib} =~ m/^:([^:]*):/;
415                         $findcode = $1;
416                         if (!defined($findcode)) {
417                                 $findcode = $fmax{$lib};
418                         }
419                         while ($fassigned{$lib} =~ m/:$findcode:/) {
420                                 $findcode++;
421                         }
422                         $fcodes{$i} = $findcode;
423                         $fassigned{$lib} .= "$findcode:";
424                         print STDERR "New Function code $i\n" if $debug;
425                 }
426                 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
427         }
428
429         print OUT "\n/* Reason codes. */\n";
430
431         foreach $i (@reasons) {
432                 $z=6-int(length($i)/8);
433                 if($rcodes{$i} eq "X") {
434                         $rassigned{$lib} =~ m/^:([^:]*):/;
435                         $findcode = $1;
436                         if (!defined($findcode)) {
437                                 $findcode = $rmax{$lib};
438                         }
439                         while ($rassigned{$lib} =~ m/:$findcode:/) {
440                                 $findcode++;
441                         }
442                         $rcodes{$i} = $findcode;
443                         $rassigned{$lib} .= "$findcode:";
444                         print STDERR "New Reason code   $i\n" if $debug;
445                 }
446                 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
447         }
448         print OUT <<"EOF";
449
450 #ifdef  __cplusplus
451 }
452 #endif
453 #endif
454 EOF
455         close OUT;
456
457         # Rewrite the C source file containing the error details.
458
459         # First, read any existing reason string definitions:
460         my %err_reason_strings;
461         if (open(IN,"<$cfile")) {
462                 while (<IN>) {
463                         if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
464                                 $err_reason_strings{$1} = $2;
465                         }
466                         if (/\b${lib}_F_(\w*)\b.*\"(.*)\"/) {
467                                 if (!exists $ftrans{$1} && ($1 ne $2)) {
468                                         print STDERR "WARNING: Mismatched function string $2\n";
469                                         $ftrans{$1} = $2;
470                                 }
471                         }
472                 }
473                 close(IN);
474         }
475
476
477         my $hincf;
478         if($static) {
479                 $hfile =~ /([^\/]+)$/;
480                 $hincf = "<${hprefix}$1>";
481         } else {
482                 $hincf = "\"$hfile\"";
483         }
484
485         # If static we know the error code at compile time so use it
486         # in error definitions.
487
488         if ($static)
489                 {
490                 $pack_errcode = "ERR_LIB_${lib}";
491                 $load_errcode = "0";
492                 }
493         else
494                 {
495                 $pack_errcode = "0";
496                 $load_errcode = "ERR_LIB_${lib}";
497                 }
498
499
500         open (OUT,">$cfile") || die "Can't open $cfile for writing";
501
502         print OUT <<"EOF";
503 /* $cfile */
504 /* ====================================================================
505  * Copyright (c) 1999-2007 The OpenSSL Project.  All rights reserved.
506  *
507  * Redistribution and use in source and binary forms, with or without
508  * modification, are permitted provided that the following conditions
509  * are met:
510  *
511  * 1. Redistributions of source code must retain the above copyright
512  *    notice, this list of conditions and the following disclaimer. 
513  *
514  * 2. Redistributions in binary form must reproduce the above copyright
515  *    notice, this list of conditions and the following disclaimer in
516  *    the documentation and/or other materials provided with the
517  *    distribution.
518  *
519  * 3. All advertising materials mentioning features or use of this
520  *    software must display the following acknowledgment:
521  *    "This product includes software developed by the OpenSSL Project
522  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
523  *
524  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
525  *    endorse or promote products derived from this software without
526  *    prior written permission. For written permission, please contact
527  *    openssl-core\@OpenSSL.org.
528  *
529  * 5. Products derived from this software may not be called "OpenSSL"
530  *    nor may "OpenSSL" appear in their names without prior written
531  *    permission of the OpenSSL Project.
532  *
533  * 6. Redistributions of any form whatsoever must retain the following
534  *    acknowledgment:
535  *    "This product includes software developed by the OpenSSL Project
536  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
537  *
538  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
539  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
540  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
541  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
542  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
543  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
544  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
545  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
546  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
547  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
548  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
549  * OF THE POSSIBILITY OF SUCH DAMAGE.
550  * ====================================================================
551  *
552  * This product includes cryptographic software written by Eric Young
553  * (eay\@cryptsoft.com).  This product includes software written by Tim
554  * Hudson (tjh\@cryptsoft.com).
555  *
556  */
557
558 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
559  * made to it will be overwritten when the script next updates this file,
560  * only reason strings will be preserved.
561  */
562
563 #include <stdio.h>
564 #include <openssl/err.h>
565 #include $hincf
566
567 /* BEGIN ERROR CODES */
568 #ifndef OPENSSL_NO_ERR
569
570 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
571 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
572
573 static ERR_STRING_DATA ${lib}_str_functs[]=
574         {
575 EOF
576         # Add each function code: if a function name is found then use it.
577         foreach $i (@function) {
578                 my $fn;
579                 $i =~ /^${lib}_F_(\S+)$/;
580                 $fn = $1;
581                 if(exists $ftrans{$fn}) {
582                         $fn = $ftrans{$fn};
583                 }
584 #               print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
585                 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
586         }
587         print OUT <<"EOF";
588 {0,NULL}
589         };
590
591 static ERR_STRING_DATA ${lib}_str_reasons[]=
592         {
593 EOF
594         # Add each reason code.
595         foreach $i (@reasons) {
596                 my $rn;
597                 my $rstr = "ERR_REASON($i)";
598                 my $nspc = 0;
599                 if (exists $err_reason_strings{$i}) {
600                         $rn = $err_reason_strings{$i};
601                 } else {
602                         $i =~ /^${lib}_R_(\S+)$/;
603                         $rn = $1;
604                         $rn =~ tr/_[A-Z]/ [a-z]/;
605                 }
606                 $nspc = 40 - length($rstr) unless length($rstr) > 40;
607                 $nspc = " " x $nspc;
608                 print OUT "{${rstr}${nspc},\"$rn\"},\n";
609         }
610 if($static) {
611         print OUT <<"EOF";
612 {0,NULL}
613         };
614
615 #endif
616
617 ${staticloader}void ERR_load_${lib}_strings(void)
618         {
619 #ifndef OPENSSL_NO_ERR
620
621         if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL)
622                 {
623                 ERR_load_strings($load_errcode,${lib}_str_functs);
624                 ERR_load_strings($load_errcode,${lib}_str_reasons);
625                 }
626 #endif
627         }
628 EOF
629 } else {
630         print OUT <<"EOF";
631 {0,NULL}
632         };
633
634 #endif
635
636 #ifdef ${lib}_LIB_NAME
637 static ERR_STRING_DATA ${lib}_lib_name[]=
638         {
639 {0      ,${lib}_LIB_NAME},
640 {0,NULL}
641         };
642 #endif
643
644
645 static int ${lib}_lib_error_code=0;
646 static int ${lib}_error_init=1;
647
648 ${staticloader}void ERR_load_${lib}_strings(void)
649         {
650         if (${lib}_lib_error_code == 0)
651                 ${lib}_lib_error_code=ERR_get_next_error_library();
652
653         if (${lib}_error_init)
654                 {
655                 ${lib}_error_init=0;
656 #ifndef OPENSSL_NO_ERR
657                 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
658                 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
659 #endif
660
661 #ifdef ${lib}_LIB_NAME
662                 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
663                 ERR_load_strings(0,${lib}_lib_name);
664 #endif
665                 }
666         }
667
668 ${staticloader}void ERR_unload_${lib}_strings(void)
669         {
670         if (${lib}_error_init == 0)
671                 {
672 #ifndef OPENSSL_NO_ERR
673                 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
674                 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
675 #endif
676
677 #ifdef ${lib}_LIB_NAME
678                 ERR_unload_strings(0,${lib}_lib_name);
679 #endif
680                 ${lib}_error_init=1;
681                 }
682         }
683
684 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
685         {
686         if (${lib}_lib_error_code == 0)
687                 ${lib}_lib_error_code=ERR_get_next_error_library();
688         ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
689         }
690 EOF
691
692 }
693
694         close OUT;
695         undef %err_reason_strings;
696 }
697
698 if($debug && defined(%notrans)) {
699         print STDERR "The following function codes were not translated:\n";
700         foreach(sort keys %notrans)
701         {
702                 print STDERR "$_\n";
703         }
704 }
705
706 # Make a list of unreferenced function and reason codes
707
708 foreach (keys %fcodes) {
709         push (@funref, $_) unless exists $ufcodes{$_};
710 }
711
712 foreach (keys %rcodes) {
713         push (@runref, $_) unless exists $urcodes{$_};
714 }
715
716 if($debug && defined(@funref) ) {
717         print STDERR "The following function codes were not referenced:\n";
718         foreach(sort @funref)
719         {
720                 print STDERR "$_\n";
721         }
722 }
723
724 if($debug && defined(@runref) ) {
725         print STDERR "The following reason codes were not referenced:\n";
726         foreach(sort @runref)
727         {
728                 print STDERR "$_\n";
729         }
730 }
731
732 if($errcount) {
733         print STDERR "There were errors, failing...\n\n";
734         exit $errcount;
735 }
736