mkstack.pl revision 68651
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 2468651Skris@source = (<crypto/*.[ch]>, <crypto/*/*.[ch]>, <rsaref/*.[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)) 7868651Skris#define sk_${type_thing}_delete(st, i) SKM_sk_delete($type_thing, (st), (i)) 7968651Skris#define sk_${type_thing}_delete_ptr(st, ptr) SKM_sk_delete_ptr($type_thing, (st), (ptr)) 8068651Skris#define sk_${type_thing}_insert(st, val, i) SKM_sk_insert($type_thing, (st), (val), (i)) 8168651Skris#define sk_${type_thing}_set_cmp_func(st, cmp) SKM_sk_set_cmp_func($type_thing, (st), (cmp)) 8268651Skris#define sk_${type_thing}_dup(st) SKM_sk_dup($type_thing, st) 8368651Skris#define sk_${type_thing}_pop_free(st, free_func) SKM_sk_pop_free($type_thing, (st), (free_func)) 8468651Skris#define sk_${type_thing}_shift(st) SKM_sk_shift($type_thing, (st)) 8568651Skris#define sk_${type_thing}_pop(st) SKM_sk_pop($type_thing, (st)) 8668651Skris#define sk_${type_thing}_sort(st) SKM_sk_sort($type_thing, (st)) 8768651SkrisEOF 8868651Skris } 8968651Skris foreach $type_thing (sort @asn1setlst) { 9068651Skris $new_stackfile .= <<EOF; 9168651Skris 9268651Skris#define d2i_ASN1_SET_OF_${type_thing}(st, pp, length, d2i_func, free_func, ex_tag, ex_class) \\ 9368651Skris SKM_ASN1_SET_OF_d2i($type_thing, (st), (pp), (length), (d2i_func), (free_func), (ex_tag), (ex_class)) 9468651Skris#define i2d_ASN1_SET_OF_${type_thing}(st, pp, i2d_func, ex_tag, ex_class, is_set) \\ 9568651Skris SKM_ASN1_SET_OF_i2d($type_thing, (st), (pp), (i2d_func), (ex_tag), (ex_class), (is_set)) 9668651Skris#define ASN1_seq_pack_${type_thing}(st, i2d_func, buf, len) \\ 9768651Skris SKM_ASN1_seq_pack($type_thing, (st), (i2d_func), (buf), (len)) 9868651Skris#define ASN1_seq_unpack_${type_thing}(buf, len, d2i_func, free_func) \\ 9968651Skris SKM_ASN1_seq_unpack($type_thing, (buf), (len), (d2i_func), (free_func)) 10068651SkrisEOF 10168651Skris } 10268651Skris foreach $type_thing (sort @p12stklst) { 10368651Skris $new_stackfile .= <<EOF; 10468651Skris 10568651Skris#define PKCS12_decrypt_d2i_${type_thing}(algor, d2i_func, free_func, pass, passlen, oct, seq) \\ 10668651Skris SKM_PKCS12_decrypt_d2i($type_thing, (algor), (d2i_func), (free_func), (pass), (passlen), (oct), (seq)) 10768651SkrisEOF 10868651Skris } 10968651Skris $new_stackfile .= "/* End of util/mkstack.pl block, you may now edit :-) */\n"; 11068651Skris $inside_block = 2; 11168651Skris} 11268651Skris 11368651Skris 11468651Skrisif ($new_stackfile eq $old_stackfile) { 11568651Skris print "No changes to $safestack.h.\n"; 11668651Skris exit 0; # avoid unnecessary rebuild 11768651Skris} 11868651Skris 11968651Skrisif ($do_write) { 12068651Skris print "Writing new $safestack.h.\n"; 12168651Skris open OUT, ">$safestack.h" || die "Can't open output file"; 12268651Skris print OUT $new_stackfile; 12368651Skris close OUT; 12468651Skris} 125