mkerr.pl revision 296465
1290001Sglebius#!/usr/local/bin/perl -w
2290001Sglebius
3290001Sglebiusmy $config = "crypto/err/openssl.ec";
4290001Sglebiusmy $debug = 0;
5290001Sglebiusmy $rebuild = 0;
6290001Sglebiusmy $static = 1;
7290001Sglebiusmy $recurse = 0;
8290001Sglebiusmy $reindex = 0;
9290001Sglebiusmy $dowrite = 0;
10290001Sglebiusmy $staticloader = "";
11290001Sglebius
12290001Sglebiusmy $pack_errcode;
13290001Sglebiusmy $load_errcode;
14290001Sglebius
15290001Sglebiuswhile (@ARGV) {
16290001Sglebius	my $arg = $ARGV[0];
17290001Sglebius	if($arg eq "-conf") {
18290001Sglebius		shift @ARGV;
19290001Sglebius		$config = shift @ARGV;
20290001Sglebius	} elsif($arg eq "-debug") {
21290001Sglebius		$debug = 1;
22290001Sglebius		shift @ARGV;
23290001Sglebius	} elsif($arg eq "-rebuild") {
24290001Sglebius		$rebuild = 1;
25290001Sglebius		shift @ARGV;
26290001Sglebius	} elsif($arg eq "-recurse") {
27290001Sglebius		$recurse = 1;
28290001Sglebius		shift @ARGV;
29290001Sglebius	} elsif($arg eq "-reindex") {
30290001Sglebius		$reindex = 1;
31290001Sglebius		shift @ARGV;
32290001Sglebius	} elsif($arg eq "-nostatic") {
33290001Sglebius		$static = 0;
34290001Sglebius		shift @ARGV;
35290001Sglebius	} elsif($arg eq "-staticloader") {
36290001Sglebius		$staticloader = "static ";
37290001Sglebius		shift @ARGV;
38290001Sglebius	} elsif($arg eq "-write") {
39290001Sglebius		$dowrite = 1;
40290001Sglebius		shift @ARGV;
41290001Sglebius	} else {
42290001Sglebius		last;
43290001Sglebius	}
44290001Sglebius}
45290001Sglebius
46290001Sglebiusif($recurse) {
47290001Sglebius	@source = ( <crypto/*.c>, <crypto/*/*.c>, <ssl/*.c>,
48290001Sglebius			<fips/*.c>, <fips/*/*.c>);
49290001Sglebius} else {
50290001Sglebius	@source = @ARGV;
51290001Sglebius}
52290001Sglebius
53290001Sglebius# Read in the config file
54290001Sglebius
55290001Sglebiusopen(IN, "<$config") || die "Can't open config file $config";
56290001Sglebius
57290001Sglebius# Parse config file
58290001Sglebius
59290001Sglebiuswhile(<IN>)
60290001Sglebius{
61290001Sglebius	if(/^L\s+(\S+)\s+(\S+)\s+(\S+)/) {
62290001Sglebius		$hinc{$1} = $2;
63290001Sglebius		$libinc{$2} = $1;
64290001Sglebius		$cskip{$3} = $1;
65290001Sglebius		if($3 ne "NONE") {
66290001Sglebius			$csrc{$1} = $3;
67290001Sglebius			$fmax{$1} = 99;
68290001Sglebius			$rmax{$1} = 99;
69290001Sglebius			$fassigned{$1} = ":";
70290001Sglebius			$rassigned{$1} = ":";
71290001Sglebius			$fnew{$1} = 0;
72290001Sglebius			$rnew{$1} = 0;
73290001Sglebius		}
74290001Sglebius	} elsif (/^F\s+(\S+)/) {
75290001Sglebius	# Add extra function with $1
76290001Sglebius	} elsif (/^R\s+(\S+)\s+(\S+)/) {
77290001Sglebius		$rextra{$1} = $2;
78290001Sglebius		$rcodes{$1} = $2;
79290001Sglebius	}
80290001Sglebius}
81290001Sglebius
82290001Sglebiusclose IN;
83290001Sglebius
84290001Sglebius# Scan each header file in turn and make a list of error codes
85290001Sglebius# and function names
86290001Sglebius
87290001Sglebiuswhile (($hdr, $lib) = each %libinc)
88290001Sglebius{
89290001Sglebius	next if($hdr eq "NONE");
90290001Sglebius	print STDERR "Scanning header file $hdr\n" if $debug;
91290001Sglebius	my $line = "", $def= "", $linenr = 0, $gotfile = 0;
92290001Sglebius	if (open(IN, "<$hdr")) {
93290001Sglebius	    $gotfile = 1;
94290001Sglebius	    while(<IN>) {
95290001Sglebius		$linenr++;
96290001Sglebius		print STDERR "line: $linenr\r" if $debug;
97290001Sglebius
98290001Sglebius		last if(/BEGIN\s+ERROR\s+CODES/);
99290001Sglebius		if ($line ne '') {
100290001Sglebius		    $_ = $line . $_;
101290001Sglebius		    $line = '';
102290001Sglebius		}
103290001Sglebius
104290001Sglebius		if (/\\$/) {
105290001Sglebius		    $line = $_;
106290001Sglebius		    next;
107290001Sglebius		}
108290001Sglebius
109290001Sglebius		if(/\/\*/) {
110290001Sglebius		    if (not /\*\//) {		# multiline comment...
111290001Sglebius			$line = $_;		# ... just accumulate
112290001Sglebius			next;
113290001Sglebius		    } else {
114290001Sglebius			s/\/\*.*?\*\///gs;	# wipe it
115290001Sglebius		    }
116290001Sglebius		}
117290001Sglebius
118290001Sglebius		if ($cpp) {
119290001Sglebius		    $cpp++ if /^#\s*if/;
120290001Sglebius		    $cpp-- if /^#\s*endif/;
121290001Sglebius		    next;
122290001Sglebius		}
123290001Sglebius		$cpp = 1 if /^#.*ifdef.*cplusplus/;  # skip "C" declaration
124290001Sglebius
125290001Sglebius		next if (/^\#/);                      # skip preprocessor directives
126290001Sglebius
127290001Sglebius		s/{[^{}]*}//gs;                      # ignore {} blocks
128290001Sglebius
129290001Sglebius		if (/\{|\/\*/) { # Add a } so editor works...
130290001Sglebius		    $line = $_;
131290001Sglebius		} else {
132290001Sglebius		    $def .= $_;
133290001Sglebius		}
134290001Sglebius	    }
135290001Sglebius	}
136290001Sglebius
137290001Sglebius	print STDERR "                                  \r" if $debug;
138290001Sglebius        $defnr = 0;
139290001Sglebius	# Delete any DECLARE_ macros
140290001Sglebius	$def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
141290001Sglebius	foreach (split /;/, $def) {
142290001Sglebius	    $defnr++;
143290001Sglebius	    print STDERR "def: $defnr\r" if $debug;
144290001Sglebius
145290001Sglebius	    # The goal is to collect function names from function declarations.
146290001Sglebius
147290001Sglebius	    s/^[\n\s]*//g;
148290001Sglebius	    s/[\n\s]*$//g;
149290001Sglebius
150290001Sglebius	    # Skip over recognized non-function declarations
151290001Sglebius	    next if(/typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/);
152290001Sglebius
153290001Sglebius	    # Remove STACK_OF(foo)
154290001Sglebius	    s/STACK_OF\(\w+\)/void/;
155290001Sglebius
156290001Sglebius	    # Reduce argument lists to empty ()
157290001Sglebius	    # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
158290001Sglebius	    while(/\(.*\)/s) {
159290001Sglebius		s/\([^\(\)]+\)/\{\}/gs;
160290001Sglebius		s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;	#(*f{}) -> f
161290001Sglebius	    }
162290001Sglebius	    # pretend as we didn't use curly braces: {} -> ()
163290001Sglebius	    s/\{\}/\(\)/gs;
164290001Sglebius
165290001Sglebius	    if (/(\w+)\s*\(\).*/s) {	# first token prior [first] () is
166290001Sglebius		my $name = $1;		# a function name!
167290001Sglebius		$name =~ tr/[a-z]/[A-Z]/;
168290001Sglebius		$ftrans{$name} = $1;
169290001Sglebius	    } elsif (/[\(\)]/ and not (/=/)) {
170290001Sglebius		print STDERR "Header $hdr: cannot parse: $_;\n";
171290001Sglebius	    }
172290001Sglebius	}
173290001Sglebius
174290001Sglebius	print STDERR "                                  \r" if $debug;
175290001Sglebius
176290001Sglebius	next if $reindex;
177290001Sglebius
178290001Sglebius	# Scan function and reason codes and store them: keep a note of the
179290001Sglebius	# maximum code used.
180290001Sglebius
181290001Sglebius	if ($gotfile) {
182290001Sglebius	  while(<IN>) {
183290001Sglebius		if(/^\#\s*define\s+(\S+)\s+(\S+)/) {
184290001Sglebius			$name = $1;
185290001Sglebius			$code = $2;
186290001Sglebius			next if $name =~ /^${lib}err/;
187290001Sglebius			unless($name =~ /^${lib}_([RF])_(\w+)$/) {
188290001Sglebius				print STDERR "Invalid error code $name\n";
189290001Sglebius				next;
190290001Sglebius			}
191290001Sglebius			if($1 eq "R") {
192290001Sglebius				$rcodes{$name} = $code;
193290001Sglebius				if ($rassigned{$lib} =~ /:$code:/) {
194290001Sglebius					print STDERR "!! ERROR: $lib reason code $code assigned twice\n";
195290001Sglebius				}
196290001Sglebius				$rassigned{$lib} .= "$code:";
197290001Sglebius				if(!(exists $rextra{$name}) &&
198290001Sglebius					 ($code > $rmax{$lib}) ) {
199290001Sglebius					$rmax{$lib} = $code;
200290001Sglebius				}
201290001Sglebius			} else {
202				if ($fassigned{$lib} =~ /:$code:/) {
203					print STDERR "!! ERROR: $lib function code $code assigned twice\n";
204				}
205				$fassigned{$lib} .= "$code:";
206				if($code > $fmax{$lib}) {
207					$fmax{$lib} = $code;
208				}
209				$fcodes{$name} = $code;
210			}
211		}
212	  }
213	}
214
215	if ($debug) {
216		if (defined($fmax{$lib})) {
217			print STDERR "Max function code fmax" . "{" . "$lib" . "} = $fmax{$lib}\n";
218			$fassigned{$lib} =~ m/^:(.*):$/;
219			@fassigned = sort {$a <=> $b} split(":", $1);
220			print STDERR "  @fassigned\n";
221		}
222		if (defined($rmax{$lib})) {
223			print STDERR "Max reason code rmax" . "{" . "$lib" . "} = $rmax{$lib}\n";
224			$rassigned{$lib} =~ m/^:(.*):$/;
225			@rassigned = sort {$a <=> $b} split(":", $1);
226			print STDERR "  @rassigned\n";
227		}
228	}
229
230	if ($lib eq "SSL") {
231		if ($rmax{$lib} >= 1000) {
232			print STDERR "!! ERROR: SSL error codes 1000+ are reserved for alerts.\n";
233			print STDERR "!!        Any new alerts must be added to $config.\n";
234			print STDERR "\n";
235		}
236	}
237	close IN;
238}
239
240# Scan each C source file and look for function and reason codes
241# This is done by looking for strings that "look like" function or
242# reason codes: basically anything consisting of all upper case and
243# numerics which has _F_ or _R_ in it and which has the name of an
244# error library at the start. This seems to work fine except for the
245# oddly named structure BIO_F_CTX which needs to be ignored.
246# If a code doesn't exist in list compiled from headers then mark it
247# with the value "X" as a place holder to give it a value later.
248# Store all function and reason codes found in %ufcodes and %urcodes
249# so all those unreferenced can be printed out.
250
251
252foreach $file (@source) {
253	# Don't parse the error source file.
254	next if exists $cskip{$file};
255	print STDERR "File loaded: ".$file."\r" if $debug;
256	open(IN, "<$file") || die "Can't open source file $file\n";
257	while(<IN>) {
258		if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
259			next unless exists $csrc{$2};
260			next if($1 eq "BIO_F_BUFFER_CTX");
261			$ufcodes{$1} = 1;
262			if(!exists $fcodes{$1}) {
263				$fcodes{$1} = "X";
264				$fnew{$2}++;
265			}
266			$notrans{$1} = 1 unless exists $ftrans{$3};
267		}
268		if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
269			next unless exists $csrc{$2};
270			$urcodes{$1} = 1;
271			if(!exists $rcodes{$1}) {
272				$rcodes{$1} = "X";
273				$rnew{$2}++;
274			}
275		}
276	}
277	close IN;
278}
279print STDERR "                                  \n" if $debug;
280
281# Now process each library in turn.
282
283foreach $lib (keys %csrc)
284{
285	my $hfile = $hinc{$lib};
286	my $cfile = $csrc{$lib};
287	if(!$fnew{$lib} && !$rnew{$lib}) {
288		print STDERR "$lib:\t\tNo new error codes\n";
289		next unless $rebuild;
290	} else {
291		print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
292		print STDERR " $rnew{$lib} New Reasons.\n";
293		next unless $dowrite;
294	}
295
296	# If we get here then we have some new error codes so we
297	# need to rebuild the header file and C file.
298
299	# Make a sorted list of error and reason codes for later use.
300
301	my @function = sort grep(/^${lib}_/,keys %fcodes);
302	my @reasons = sort grep(/^${lib}_/,keys %rcodes);
303
304	# Rewrite the header file
305
306	if (open(IN, "<$hfile")) {
307	    # Copy across the old file
308	    while(<IN>) {
309		push @out, $_;
310		last if (/BEGIN ERROR CODES/);
311	    }
312	    close IN;
313	} else {
314	    push @out,
315"/* ====================================================================\n",
316" * Copyright (c) 2001-2011 The OpenSSL Project.  All rights reserved.\n",
317" *\n",
318" * Redistribution and use in source and binary forms, with or without\n",
319" * modification, are permitted provided that the following conditions\n",
320" * are met:\n",
321" *\n",
322" * 1. Redistributions of source code must retain the above copyright\n",
323" *    notice, this list of conditions and the following disclaimer. \n",
324" *\n",
325" * 2. Redistributions in binary form must reproduce the above copyright\n",
326" *    notice, this list of conditions and the following disclaimer in\n",
327" *    the documentation and/or other materials provided with the\n",
328" *    distribution.\n",
329" *\n",
330" * 3. All advertising materials mentioning features or use of this\n",
331" *    software must display the following acknowledgment:\n",
332" *    \"This product includes software developed by the OpenSSL Project\n",
333" *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
334" *\n",
335" * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
336" *    endorse or promote products derived from this software without\n",
337" *    prior written permission. For written permission, please contact\n",
338" *    openssl-core\@openssl.org.\n",
339" *\n",
340" * 5. Products derived from this software may not be called \"OpenSSL\"\n",
341" *    nor may \"OpenSSL\" appear in their names without prior written\n",
342" *    permission of the OpenSSL Project.\n",
343" *\n",
344" * 6. Redistributions of any form whatsoever must retain the following\n",
345" *    acknowledgment:\n",
346" *    \"This product includes software developed by the OpenSSL Project\n",
347" *    for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
348" *\n",
349" * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
350" * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
351" * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
352" * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
353" * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
354" * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
355" * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
356" * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
357" * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
358" * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
359" * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
360" * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
361" * ====================================================================\n",
362" *\n",
363" * This product includes cryptographic software written by Eric Young\n",
364" * (eay\@cryptsoft.com).  This product includes software written by Tim\n",
365" * Hudson (tjh\@cryptsoft.com).\n",
366" *\n",
367" */\n",
368"\n",
369"#ifndef HEADER_${lib}_ERR_H\n",
370"#define HEADER_${lib}_ERR_H\n",
371"\n",
372"/* BEGIN ERROR CODES */\n";
373	}
374	open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
375
376	print OUT @out;
377	undef @out;
378	print OUT <<"EOF";
379/*
380 * The following lines are auto generated by the script mkerr.pl. Any changes
381 * made after this point may be overwritten when the script is next run.
382 */
383EOF
384	if($static) {
385		print OUT <<"EOF";
386${staticloader}void ERR_load_${lib}_strings(void);
387
388EOF
389	} else {
390		print OUT <<"EOF";
391${staticloader}void ERR_load_${lib}_strings(void);
392${staticloader}void ERR_unload_${lib}_strings(void);
393${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
394# define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
395
396EOF
397	}
398	print OUT <<"EOF";
399/* Error codes for the $lib functions. */
400
401/* Function codes. */
402EOF
403
404	foreach $i (@function) {
405		$z=48 - length($i);
406		if($fcodes{$i} eq "X") {
407			$fassigned{$lib} =~ m/^:([^:]*):/;
408			$findcode = $1;
409			if (!defined($findcode)) {
410				$findcode = $fmax{$lib};
411			}
412			while ($fassigned{$lib} =~ m/:$findcode:/) {
413				$findcode++;
414			}
415			$fcodes{$i} = $findcode;
416			$fassigned{$lib} .= "$findcode:";
417			print STDERR "New Function code $i\n" if $debug;
418		}
419		printf OUT "# define $i%s $fcodes{$i}\n"," " x $z;
420	}
421
422	print OUT "\n/* Reason codes. */\n";
423
424	foreach $i (@reasons) {
425		$z=48 - length($i);
426		if($rcodes{$i} eq "X") {
427			$rassigned{$lib} =~ m/^:([^:]*):/;
428			$findcode = $1;
429			if (!defined($findcode)) {
430				$findcode = $rmax{$lib};
431			}
432			while ($rassigned{$lib} =~ m/:$findcode:/) {
433				$findcode++;
434			}
435			$rcodes{$i} = $findcode;
436			$rassigned{$lib} .= "$findcode:";
437			print STDERR "New Reason code   $i\n" if $debug;
438		}
439		printf OUT "# define $i%s $rcodes{$i}\n"," " x $z;
440	}
441	print OUT <<"EOF";
442
443#ifdef  __cplusplus
444}
445#endif
446#endif
447EOF
448	close OUT;
449
450	# Rewrite the C source file containing the error details.
451
452	# First, read any existing reason string definitions:
453	my %err_reason_strings;
454	if (open(IN,"<$cfile")) {
455		my $line = "";
456		while (<IN>) {
457			chomp;
458			$_ = $line . $_;
459			$line = "";
460			if (/{ERR_REASON\(/) {
461				if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
462					$err_reason_strings{$1} = $2;
463				} else {
464					$line = $_;
465				}
466			}
467		}
468		close(IN);
469	}
470
471	my $hincf;
472	if($static) {
473		$hfile =~ /([^\/]+)$/;
474		$hincf = "<openssl/$1>";
475	} else {
476		$hincf = "\"$hfile\"";
477	}
478
479	# If static we know the error code at compile time so use it
480	# in error definitions.
481
482	if ($static)
483		{
484		$pack_errcode = "ERR_LIB_${lib}";
485		$load_errcode = "0";
486		}
487	else
488		{
489		$pack_errcode = "0";
490		$load_errcode = "ERR_LIB_${lib}";
491		}
492
493
494	open (OUT,">$cfile") || die "Can't open $cfile for writing";
495
496	print OUT <<"EOF";
497/* $cfile */
498/* ====================================================================
499 * Copyright (c) 1999-2011 The OpenSSL Project.  All rights reserved.
500 *
501 * Redistribution and use in source and binary forms, with or without
502 * modification, are permitted provided that the following conditions
503 * are met:
504 *
505 * 1. Redistributions of source code must retain the above copyright
506 *    notice, this list of conditions and the following disclaimer.
507 *
508 * 2. Redistributions in binary form must reproduce the above copyright
509 *    notice, this list of conditions and the following disclaimer in
510 *    the documentation and/or other materials provided with the
511 *    distribution.
512 *
513 * 3. All advertising materials mentioning features or use of this
514 *    software must display the following acknowledgment:
515 *    "This product includes software developed by the OpenSSL Project
516 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
517 *
518 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
519 *    endorse or promote products derived from this software without
520 *    prior written permission. For written permission, please contact
521 *    openssl-core\@OpenSSL.org.
522 *
523 * 5. Products derived from this software may not be called "OpenSSL"
524 *    nor may "OpenSSL" appear in their names without prior written
525 *    permission of the OpenSSL Project.
526 *
527 * 6. Redistributions of any form whatsoever must retain the following
528 *    acknowledgment:
529 *    "This product includes software developed by the OpenSSL Project
530 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
531 *
532 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
533 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
534 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
535 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
536 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
537 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
538 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
539 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
540 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
541 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
542 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
543 * OF THE POSSIBILITY OF SUCH DAMAGE.
544 * ====================================================================
545 *
546 * This product includes cryptographic software written by Eric Young
547 * (eay\@cryptsoft.com).  This product includes software written by Tim
548 * Hudson (tjh\@cryptsoft.com).
549 *
550 */
551
552/*
553 * NOTE: this file was auto generated by the mkerr.pl script: any changes
554 * made to it will be overwritten when the script next updates this file,
555 * only reason strings will be preserved.
556 */
557
558#include <stdio.h>
559#include <openssl/err.h>
560#include $hincf
561
562/* BEGIN ERROR CODES */
563#ifndef OPENSSL_NO_ERR
564
565# define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
566# define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
567
568static ERR_STRING_DATA ${lib}_str_functs[] = {
569EOF
570	# Add each function code: if a function name is found then use it.
571	foreach $i (@function) {
572		my $fn;
573		$i =~ /^${lib}_F_(\S+)$/;
574		$fn = $1;
575		if(exists $ftrans{$fn}) {
576			$fn = $ftrans{$fn};
577		}
578#		print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
579		if(length($i) + length($fn) > 58) {
580			print OUT "    {ERR_FUNC($i),\n     \"$fn\"},\n";
581		} else {
582			print OUT "    {ERR_FUNC($i), \"$fn\"},\n";
583		}
584	}
585	print OUT <<"EOF";
586    {0, NULL}
587};
588
589static ERR_STRING_DATA ${lib}_str_reasons[] = {
590EOF
591	# Add each reason code.
592	foreach $i (@reasons) {
593		my $rn;
594		my $rstr = "ERR_REASON($i)";
595		if (exists $err_reason_strings{$i}) {
596			$rn = $err_reason_strings{$i};
597		} else {
598			$i =~ /^${lib}_R_(\S+)$/;
599			$rn = $1;
600			$rn =~ tr/_[A-Z]/ [a-z]/;
601		}
602		if(length($i) + length($rn) > 56) {
603			print OUT "    {${rstr},\n     \"$rn\"},\n";
604		} else {
605			print OUT "    {${rstr}, \"$rn\"},\n";
606		}
607	}
608if($static) {
609	print OUT <<"EOF";
610    {0, NULL}
611};
612
613#endif
614
615${staticloader}void ERR_load_${lib}_strings(void)
616{
617#ifndef OPENSSL_NO_ERR
618
619    if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) {
620        ERR_load_strings($load_errcode, ${lib}_str_functs);
621        ERR_load_strings($load_errcode, ${lib}_str_reasons);
622    }
623#endif
624}
625EOF
626} else {
627	print OUT <<"EOF";
628    {0, NULL}
629};
630
631#endif
632
633#ifdef ${lib}_LIB_NAME
634static ERR_STRING_DATA ${lib}_lib_name[] = {
635    {0, ${lib}_LIB_NAME},
636    {0, NULL}
637};
638#endif
639
640static int ${lib}_lib_error_code = 0;
641static int ${lib}_error_init = 1;
642
643${staticloader}void ERR_load_${lib}_strings(void)
644{
645    if (${lib}_lib_error_code == 0)
646        ${lib}_lib_error_code = ERR_get_next_error_library();
647
648    if (${lib}_error_init) {
649        ${lib}_error_init = 0;
650#ifndef OPENSSL_NO_ERR
651        ERR_load_strings(${lib}_lib_error_code, ${lib}_str_functs);
652        ERR_load_strings(${lib}_lib_error_code, ${lib}_str_reasons);
653#endif
654
655#ifdef ${lib}_LIB_NAME
656        ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code, 0, 0);
657        ERR_load_strings(0, ${lib}_lib_name);
658#endif
659    }
660}
661
662${staticloader}void ERR_unload_${lib}_strings(void)
663{
664    if (${lib}_error_init == 0) {
665#ifndef OPENSSL_NO_ERR
666        ERR_unload_strings(${lib}_lib_error_code, ${lib}_str_functs);
667        ERR_unload_strings(${lib}_lib_error_code, ${lib}_str_reasons);
668#endif
669
670#ifdef ${lib}_LIB_NAME
671        ERR_unload_strings(0, ${lib}_lib_name);
672#endif
673        ${lib}_error_init = 1;
674    }
675}
676
677${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
678{
679    if (${lib}_lib_error_code == 0)
680        ${lib}_lib_error_code = ERR_get_next_error_library();
681    ERR_PUT_error(${lib}_lib_error_code, function, reason, file, line);
682}
683EOF
684
685}
686
687	close OUT;
688	undef %err_reason_strings;
689}
690
691if($debug && %notrans) {
692	print STDERR "The following function codes were not translated:\n";
693	foreach(sort keys %notrans)
694	{
695		print STDERR "$_\n";
696	}
697}
698
699# Make a list of unreferenced function and reason codes
700
701foreach (keys %fcodes) {
702	push (@funref, $_) unless exists $ufcodes{$_};
703}
704
705foreach (keys %rcodes) {
706	push (@runref, $_) unless exists $urcodes{$_};
707}
708
709if($debug && @funref) {
710	print STDERR "The following function codes were not referenced:\n";
711	foreach(sort @funref)
712	{
713		print STDERR "$_\n";
714	}
715}
716
717if($debug && @runref) {
718	print STDERR "The following reason codes were not referenced:\n";
719	foreach(sort @runref)
720	{
721		print STDERR "$_\n";
722	}
723}
724