mkstack.pl revision 160814
168651Skris#!/usr/local/bin/perl -w
268651Skris
368651Skris# This is a utility that searches out "DECLARE_STACK_OF()"
468651Skris# declarations in .h and .c files, and updates/creates/replaces
568651Skris# the corresponding macro declarations in crypto/stack/safestack.h.
668651Skris# As it's not generally possible to have macros that generate macros,
768651Skris# we need to control this from the "outside", here in this script.
868651Skris#
968651Skris# Geoff Thorpe, June, 2000 (with massive Perl-hacking
1068651Skris#                           help from Steve Robb)
1168651Skris
1268651Skrismy $safestack = "crypto/stack/safestack";
1368651Skris
1468651Skrismy $do_write;
1568651Skriswhile (@ARGV) {
1668651Skris	my $arg = $ARGV[0];
1768651Skris	if($arg eq "-write") {
1868651Skris		$do_write = 1;
1968651Skris	}
2068651Skris	shift @ARGV;
2168651Skris}
2268651Skris
2368651Skris
24109998Smarkm@source = (<crypto/*.[ch]>, <crypto/*/*.[ch]>, <ssl/*.[ch]>);
2568651Skrisforeach $file (@source) {
2668651Skris	next if -l $file;
2768651Skris
2868651Skris	# Open the .c/.h file for reading
2968651Skris	open(IN, "< $file") || die "Can't open $file for reading: $!";
3068651Skris
3168651Skris	while(<IN>) {
3268651Skris		if (/^DECLARE_STACK_OF\(([^)]+)\)/) {
3368651Skris			push @stacklst, $1;
3468651Skris		} if (/^DECLARE_ASN1_SET_OF\(([^)]+)\)/) {
3568651Skris			push @asn1setlst, $1;
3668651Skris		} if (/^DECLARE_PKCS12_STACK_OF\(([^)]+)\)/) {
3768651Skris			push @p12stklst, $1;
3868651Skris		}
3968651Skris	}
4068651Skris	close(IN);
4168651Skris}
4268651Skris
4368651Skris
4468651Skris
4568651Skrismy $old_stackfile = "";
4668651Skrismy $new_stackfile = "";
4768651Skrismy $inside_block = 0;
4868651Skrismy $type_thing;
4968651Skris
5068651Skrisopen(IN, "< $safestack.h") || die "Can't open input file: $!";
5168651Skriswhile(<IN>) {
5268651Skris	$old_stackfile .= $_;
5368651Skris
5468651Skris	if (m|^/\* This block of defines is updated by util/mkstack.pl, please do not touch! \*/|) {
5568651Skris		$inside_block = 1;
5668651Skris	}
5768651Skris	if (m|^/\* End of util/mkstack.pl block, you may now edit :-\) \*/|) {
5868651Skris		$inside_block = 0;
5968651Skris	} elsif ($inside_block == 0) {
6068651Skris		$new_stackfile .= $_;
6168651Skris	}
6268651Skris	next if($inside_block != 1);
6368651Skris	$new_stackfile .= "/* This block of defines is updated by util/mkstack.pl, please do not touch! */";
6468651Skris
6568651Skris	foreach $type_thing (sort @stacklst) {
6668651Skris		$new_stackfile .= <<EOF;
6768651Skris
6868651Skris#define sk_${type_thing}_new(st) SKM_sk_new($type_thing, (st))
6968651Skris#define sk_${type_thing}_new_null() SKM_sk_new_null($type_thing)
7068651Skris#define sk_${type_thing}_free(st) SKM_sk_free($type_thing, (st))
7168651Skris#define sk_${type_thing}_num(st) SKM_sk_num($type_thing, (st))
7268651Skris#define sk_${type_thing}_value(st, i) SKM_sk_value($type_thing, (st), (i))
7368651Skris#define sk_${type_thing}_set(st, i, val) SKM_sk_set($type_thing, (st), (i), (val))
7468651Skris#define sk_${type_thing}_zero(st) SKM_sk_zero($type_thing, (st))
7568651Skris#define sk_${type_thing}_push(st, val) SKM_sk_push($type_thing, (st), (val))
7668651Skris#define sk_${type_thing}_unshift(st, val) SKM_sk_unshift($type_thing, (st), (val))
7768651Skris#define sk_${type_thing}_find(st, val) SKM_sk_find($type_thing, (st), (val))
78160814Ssimon#define sk_${type_thing}_find_ex(st, val) SKM_sk_find_ex($type_thing, (st), (val))
7968651Skris#define sk_${type_thing}_delete(st, i) SKM_sk_delete($type_thing, (st), (i))
8068651Skris#define sk_${type_thing}_delete_ptr(st, ptr) SKM_sk_delete_ptr($type_thing, (st), (ptr))
8168651Skris#define sk_${type_thing}_insert(st, val, i) SKM_sk_insert($type_thing, (st), (val), (i))
8268651Skris#define sk_${type_thing}_set_cmp_func(st, cmp) SKM_sk_set_cmp_func($type_thing, (st), (cmp))
8368651Skris#define sk_${type_thing}_dup(st) SKM_sk_dup($type_thing, st)
8468651Skris#define sk_${type_thing}_pop_free(st, free_func) SKM_sk_pop_free($type_thing, (st), (free_func))
8568651Skris#define sk_${type_thing}_shift(st) SKM_sk_shift($type_thing, (st))
8668651Skris#define sk_${type_thing}_pop(st) SKM_sk_pop($type_thing, (st))
8768651Skris#define sk_${type_thing}_sort(st) SKM_sk_sort($type_thing, (st))
88142425Snectar#define sk_${type_thing}_is_sorted(st) SKM_sk_is_sorted($type_thing, (st))
8968651SkrisEOF
9068651Skris	}
9168651Skris	foreach $type_thing (sort @asn1setlst) {
9268651Skris		$new_stackfile .= <<EOF;
9368651Skris
9468651Skris#define d2i_ASN1_SET_OF_${type_thing}(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \\
9568651Skris	SKM_ASN1_SET_OF_d2i($type_thing, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class))
9668651Skris#define i2d_ASN1_SET_OF_${type_thing}(st, pp, i2d_func, ex_tag, ex_class, is_set) \\
9768651Skris	SKM_ASN1_SET_OF_i2d($type_thing, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set))
9868651Skris#define ASN1_seq_pack_${type_thing}(st, i2d_func, buf, len) \\
9968651Skris	SKM_ASN1_seq_pack($type_thing, (st), (i2d_func), (buf), (len))
10068651Skris#define ASN1_seq_unpack_${type_thing}(buf, len, d2i_func, free_func) \\
10168651Skris	SKM_ASN1_seq_unpack($type_thing, (buf), (len), (d2i_func), (free_func))
10268651SkrisEOF
10368651Skris	}
10468651Skris	foreach $type_thing (sort @p12stklst) {
10568651Skris		$new_stackfile .= <<EOF;
10668651Skris
10768651Skris#define PKCS12_decrypt_d2i_${type_thing}(algor, d2i_func, free_func, pass, passlen, oct, seq) \\
10868651Skris	SKM_PKCS12_decrypt_d2i($type_thing, (algor), (d2i_func), (free_func), (pass), (passlen), (oct), (seq))
10968651SkrisEOF
11068651Skris	}
11168651Skris	$new_stackfile .= "/* End of util/mkstack.pl block, you may now edit :-) */\n";
11268651Skris	$inside_block = 2;
11368651Skris}
11468651Skris
11568651Skris
11668651Skrisif ($new_stackfile eq $old_stackfile) {
11768651Skris	print "No changes to $safestack.h.\n";
11868651Skris	exit 0; # avoid unnecessary rebuild
11968651Skris}
12068651Skris
12168651Skrisif ($do_write) {
12268651Skris	print "Writing new $safestack.h.\n";
12368651Skris	open OUT, ">$safestack.h" || die "Can't open output file";
12468651Skris	print OUT $new_stackfile;
12568651Skris	close OUT;
12668651Skris}
127