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