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