1#!/usr/local/bin/perl
2
3# fixes bug in floating point emulation on sparc64 when
4# this script produces off-by-one output on sparc64
5eval 'use integer;';
6
7print STDERR "Warning: perl module integer not found.\n" if ($@);
8
9sub obj_cmp
10	{
11	local(@a,@b,$_,$r);
12
13	$A=$obj_len{$obj{$nid{$a}}};
14	$B=$obj_len{$obj{$nid{$b}}};
15
16	$r=($A-$B);
17	return($r) if $r != 0;
18
19	$A=$obj_der{$obj{$nid{$a}}};
20	$B=$obj_der{$obj{$nid{$b}}};
21
22	return($A cmp $B);
23	}
24
25sub expand_obj
26	{
27	local(*v)=@_;
28	local($k,$d);
29	local($i);
30
31	do	{
32		$i=0;
33		foreach $k (keys %v)
34			{
35			if (($v{$k} =~ s/(OBJ_[^,]+),/$v{$1},/))
36				{ $i++; }
37			}
38		} while($i);
39	foreach $k (keys %v)
40		{
41		@a=split(/,/,$v{$k});
42		$objn{$k}=$#a+1;
43		}
44	return(%objn);
45	}
46
47open (IN,"$ARGV[0]") || die "Can't open input file $ARGV[0]";
48open (OUT,">$ARGV[1]") || die "Can't open output file $ARGV[1]";
49
50while (<IN>)
51	{
52	next unless /^\#define\s+(\S+)\s+(.*)$/;
53	$v=$1;
54	$d=$2;
55	$d =~ s/^\"//;
56	$d =~ s/\"$//;
57	if ($v =~ /^SN_(.*)$/)
58		{
59		if(defined $snames{$d})
60			{
61			print "WARNING: Duplicate short name \"$d\"\n";
62			}
63		else
64			{ $snames{$d} = "X"; }
65		$sn{$1}=$d;
66		}
67	elsif ($v =~ /^LN_(.*)$/)
68		{
69		if(defined $lnames{$d})
70			{
71			print "WARNING: Duplicate long name \"$d\"\n";
72			}
73		else
74			{ $lnames{$d} = "X"; }
75		$ln{$1}=$d;
76		}
77	elsif ($v =~ /^NID_(.*)$/)
78		{ $nid{$d}=$1; }
79	elsif ($v =~ /^OBJ_(.*)$/)
80		{
81		$obj{$1}=$v;
82		$objd{$v}=$d;
83		}
84	}
85close IN;
86
87%ob=&expand_obj(*objd);
88
89@a=sort { $a <=> $b } keys %nid;
90$n=$a[$#a]+1;
91
92@lvalues=();
93$lvalues=0;
94
95for ($i=0; $i<$n; $i++)
96	{
97	if (!defined($nid{$i}))
98		{
99		push(@out,"{NULL,NULL,NID_undef,0,NULL,0},\n");
100		}
101	else
102		{
103		$sn=defined($sn{$nid{$i}})?"$sn{$nid{$i}}":"NULL";
104		$ln=defined($ln{$nid{$i}})?"$ln{$nid{$i}}":"NULL";
105
106		if ($sn eq "NULL") {
107			$sn=$ln;
108			$sn{$nid{$i}} = $ln;
109		}
110
111		if ($ln eq "NULL") {
112			$ln=$sn;
113			$ln{$nid{$i}} = $sn;
114		}
115
116		$out ="{";
117		$out.="\"$sn\"";
118		$out.=","."\"$ln\"";
119		$out.=",NID_$nid{$i},";
120		if (defined($obj{$nid{$i}}))
121			{
122			$v=$objd{$obj{$nid{$i}}};
123			$v =~ s/L//g;
124			$v =~ s/,/ /g;
125			$r=&der_it($v);
126			$z="";
127			$length=0;
128			foreach (unpack("C*",$r))
129				{
130				$z.=sprintf("0x%02X,",$_);
131				$length++;
132				}
133			$obj_der{$obj{$nid{$i}}}=$z;
134			$obj_len{$obj{$nid{$i}}}=$length;
135
136			push(@lvalues,sprintf("%-45s/* [%3d] %s */\n",
137				$z,$lvalues,$obj{$nid{$i}}));
138			$out.="$length,&(lvalues[$lvalues]),0";
139			$lvalues+=$length;
140			}
141		else
142			{
143			$out.="0,NULL,0";
144			}
145		$out.="},\n";
146		push(@out,$out);
147		}
148	}
149
150@a=grep(defined($sn{$nid{$_}}),0 .. $n);
151foreach (sort { $sn{$nid{$a}} cmp $sn{$nid{$b}} } @a)
152	{
153	push(@sn,sprintf("&(nid_objs[%2d]),/* \"$sn{$nid{$_}}\" */\n",$_));
154	}
155
156@a=grep(defined($ln{$nid{$_}}),0 .. $n);
157foreach (sort { $ln{$nid{$a}} cmp $ln{$nid{$b}} } @a)
158	{
159	push(@ln,sprintf("&(nid_objs[%2d]),/* \"$ln{$nid{$_}}\" */\n",$_));
160	}
161
162@a=grep(defined($obj{$nid{$_}}),0 .. $n);
163foreach (sort obj_cmp @a)
164	{
165	$m=$obj{$nid{$_}};
166	$v=$objd{$m};
167	$v =~ s/L//g;
168	$v =~ s/,/ /g;
169	push(@ob,sprintf("&(nid_objs[%2d]),/* %-32s %s */\n",$_,$m,$v));
170	}
171
172print OUT <<'EOF';
173/* crypto/objects/obj_dat.h */
174
175/* THIS FILE IS GENERATED FROM objects.h by obj_dat.pl via the
176 * following command:
177 * perl obj_dat.pl obj_mac.h obj_dat.h
178 */
179
180/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
181 * All rights reserved.
182 *
183 * This package is an SSL implementation written
184 * by Eric Young (eay@cryptsoft.com).
185 * The implementation was written so as to conform with Netscapes SSL.
186 *
187 * This library is free for commercial and non-commercial use as long as
188 * the following conditions are aheared to.  The following conditions
189 * apply to all code found in this distribution, be it the RC4, RSA,
190 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
191 * included with this distribution is covered by the same copyright terms
192 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
193 *
194 * Copyright remains Eric Young's, and as such any Copyright notices in
195 * the code are not to be removed.
196 * If this package is used in a product, Eric Young should be given attribution
197 * as the author of the parts of the library used.
198 * This can be in the form of a textual message at program startup or
199 * in documentation (online or textual) provided with the package.
200 *
201 * Redistribution and use in source and binary forms, with or without
202 * modification, are permitted provided that the following conditions
203 * are met:
204 * 1. Redistributions of source code must retain the copyright
205 *    notice, this list of conditions and the following disclaimer.
206 * 2. Redistributions in binary form must reproduce the above copyright
207 *    notice, this list of conditions and the following disclaimer in the
208 *    documentation and/or other materials provided with the distribution.
209 * 3. All advertising materials mentioning features or use of this software
210 *    must display the following acknowledgement:
211 *    "This product includes cryptographic software written by
212 *     Eric Young (eay@cryptsoft.com)"
213 *    The word 'cryptographic' can be left out if the rouines from the library
214 *    being used are not cryptographic related :-).
215 * 4. If you include any Windows specific code (or a derivative thereof) from
216 *    the apps directory (application code) you must include an acknowledgement:
217 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
218 *
219 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
220 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
223 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
225 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
226 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
228 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
229 * SUCH DAMAGE.
230 *
231 * The licence and distribution terms for any publically available version or
232 * derivative of this code cannot be changed.  i.e. this code cannot simply be
233 * copied and put under another distribution licence
234 * [including the GNU Public Licence.]
235 */
236
237EOF
238
239printf OUT "#define NUM_NID %d\n",$n;
240printf OUT "#define NUM_SN %d\n",$#sn+1;
241printf OUT "#define NUM_LN %d\n",$#ln+1;
242printf OUT "#define NUM_OBJ %d\n\n",$#ob+1;
243
244printf OUT "static unsigned char lvalues[%d]={\n",$lvalues+1;
245print OUT @lvalues;
246print OUT "};\n\n";
247
248printf OUT "static ASN1_OBJECT nid_objs[NUM_NID]={\n";
249foreach (@out)
250	{
251	if (length($_) > 75)
252		{
253		$out="";
254		foreach (split(/,/))
255			{
256			$t=$out.$_.",";
257			if (length($t) > 70)
258				{
259				print OUT "$out\n";
260				$t="\t$_,";
261				}
262			$out=$t;
263			}
264		chop $out;
265		print OUT "$out";
266		}
267	else
268		{ print OUT $_; }
269	}
270print  OUT "};\n\n";
271
272printf OUT "static ASN1_OBJECT *sn_objs[NUM_SN]={\n";
273print  OUT @sn;
274print  OUT "};\n\n";
275
276printf OUT "static ASN1_OBJECT *ln_objs[NUM_LN]={\n";
277print  OUT @ln;
278print  OUT "};\n\n";
279
280printf OUT "static ASN1_OBJECT *obj_objs[NUM_OBJ]={\n";
281print  OUT @ob;
282print  OUT "};\n\n";
283
284close OUT;
285
286sub der_it
287	{
288	local($v)=@_;
289	local(@a,$i,$ret,@r);
290
291	@a=split(/\s+/,$v);
292	$ret.=pack("C*",$a[0]*40+$a[1]);
293	shift @a;
294	shift @a;
295	foreach (@a)
296		{
297		@r=();
298		$t=0;
299		while ($_ >= 128)
300			{
301			$x=$_%128;
302			$_/=128;
303			push(@r,((($t++)?0x80:0)|$x));
304			}
305		push(@r,((($t++)?0x80:0)|$_));
306		$ret.=pack("C*",reverse(@r));
307		}
308	return($ret);
309	}
310