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