Make auto DH work with DHEPSK
[openssl.git] / util / mkstack.pl
1 #!/usr/local/bin/perl -w
2
3 # Search out "DECLARE_STACK_OF()" # declarations in .h and .c files,
4 # and create corresponding macro declarations for crypto/stack/safestack.h.
5
6 my $safestack = "include/openssl/safestack.h";
7 my $do_write = 0;
8
9 foreach ( @ARGV ) {
10     $do_write = 1 if $_ eq "-write";
11 }
12
13 my @stacklst;
14 my @sstacklst;
15 my @asn1setlst;
16 my @p12stklst;
17 my @lhashlst;
18 my @source = (<include/openssl/*.h>, <crypto/*.[ch]>, <crypto/*/*.[ch]>, <ssl/*.[ch]>, <apps/*.[ch]>);
19 foreach $file (@source) {
20     next if -l $file;
21
22     # Open the .c/.h file for reading
23     open(IN, "< $file") || die "Can't open $file for reading, $!";
24
25     while(<IN>) {
26         next unless /^DECLARE_/;
27         if (/^DECLARE_STACK_OF\(([^)]+)\)/) {
28             push @stacklst, $1;
29         }
30         elsif (/^DECLARE_SPECIAL_STACK_OF\(([^,\s]+)\s*,\s*([^>\s]+)\)/) {
31             push @sstacklst, [$1, $2];
32         }
33         elsif (/^DECLARE_LHASH_OF\(([^)]+)\)/) {
34             push @lhashlst, $1;
35         }
36     }
37     close(IN);
38 }
39
40 my $new_stackfile = <<'EOF';
41 /* automatically generated by util/mkstack.pl */
42 /* ====================================================================
43  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in
54  *    the documentation and/or other materials provided with the
55  *    distribution.
56  *
57  * 3. All advertising materials mentioning features or use of this
58  *    software must display the following acknowledgment:
59  *    "This product includes software developed by the OpenSSL Project
60  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
61  *
62  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
63  *    endorse or promote products derived from this software without
64  *    prior written permission. For written permission, please contact
65  *    openssl-core@openssl.org.
66  *
67  * 5. Products derived from this software may not be called "OpenSSL"
68  *    nor may "OpenSSL" appear in their names without prior written
69  *    permission of the OpenSSL Project.
70  *
71  * 6. Redistributions of any form whatsoever must retain the following
72  *    acknowledgment:
73  *    "This product includes software developed by the OpenSSL Project
74  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
75  *
76  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
77  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
79  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
80  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
81  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
82  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
83  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
85  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
86  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
87  * OF THE POSSIBILITY OF SUCH DAMAGE.
88  * ====================================================================
89  *
90  * This product includes cryptographic software written by Eric Young
91  * (eay@cryptsoft.com).  This product includes software written by Tim
92  * Hudson (tjh@cryptsoft.com).
93  *
94  */
95
96 #ifndef HEADER_SAFESTACK_H
97 # define HEADER_SAFESTACK_H
98
99 # include <openssl/stack.h>
100
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
104
105 # ifndef CHECKED_PTR_OF
106 #  define CHECKED_PTR_OF(type, p) ((void*) (1 ? p : (type*)0))
107 # endif
108
109 /*
110  * In C++ we get problems because an explicit cast is needed from (void *) we
111  * use CHECKED_STACK_OF to ensure the correct type is passed in the macros
112  * below.
113  */
114
115 # define CHECKED_STACK_OF(type, p) \
116     ((_STACK*) (1 ? p : (STACK_OF(type)*)0))
117
118 # define CHECKED_SK_COPY_FUNC(type, p) \
119     ((void *(*)(void *)) ((1 ? p : (type *(*)(const type *))0)))
120
121 # define CHECKED_SK_FREE_FUNC(type, p) \
122     ((void (*)(void *)) ((1 ? p : (void (*)(type *))0)))
123
124 # define CHECKED_SK_CMP_FUNC(type, p) \
125     ((int (*)(const void *, const void *)) \
126         ((1 ? p : (int (*)(const type * const *, const type * const *))0)))
127
128 # define STACK_OF(type) struct stack_st_##type
129 # define PREDECLARE_STACK_OF(type) STACK_OF(type);
130
131 # define DECLARE_STACK_OF(type) STACK_OF(type);
132 # define DECLARE_SPECIAL_STACK_OF(type, type2) STACK_OF(type);
133
134 /*-
135  * Strings are special: normally an lhash entry will point to a single
136  * (somewhat) mutable object. In the case of strings:
137  *
138  * a) Instead of a single char, there is an array of chars, NUL-terminated.
139  * b) The string may have be immutable.
140  *
141  * So, they need their own declarations. Especially important for
142  * type-checking tools, such as Deputy.
143  *
144  * In practice, however, it appears to be hard to have a const
145  * string. For now, I'm settling for dealing with the fact it is a
146  * string at all.
147  */
148 typedef char *OPENSSL_STRING;
149 typedef const char *OPENSSL_CSTRING;
150
151 /*-
152  * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but
153  * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned
154  * above, instead of a single char each entry is a NUL-terminated array of
155  * chars. So, we have to implement STRING specially for STACK_OF. This is
156  * dealt with in the autogenerated macros below.
157  */
158 DECLARE_SPECIAL_STACK_OF(OPENSSL_STRING, char)
159
160 /*
161  * Similarly, we sometimes use a block of characters, NOT nul-terminated.
162  * These should also be distinguished from "normal" stacks.
163  */
164 typedef void *OPENSSL_BLOCK;
165 DECLARE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void)
166
167 /*
168  * This file is automatically generated by util/mkstack.pl
169  * Do not edit!
170  */
171
172 /*
173  * SKM_sk_... stack macros are internal to safestack.h: never use them
174  * directly, use sk_<type>_... instead
175  */
176 # define SKM_sk_new(type, cmp) \
177         ((STACK_OF(type) *)sk_new(CHECKED_SK_CMP_FUNC(type, cmp)))
178 # define SKM_sk_new_null(type) \
179         ((STACK_OF(type) *)sk_new_null())
180 # define SKM_sk_free(type, st) \
181         sk_free(CHECKED_STACK_OF(type, st))
182 # define SKM_sk_num(type, st) \
183         sk_num(CHECKED_STACK_OF(type, st))
184 # define SKM_sk_value(type, st,i) \
185         ((type *)sk_value(CHECKED_STACK_OF(type, st), i))
186 # define SKM_sk_set(type, st,i,val) \
187         sk_set(CHECKED_STACK_OF(type, st), i, CHECKED_PTR_OF(type, val))
188 # define SKM_sk_zero(type, st) \
189         sk_zero(CHECKED_STACK_OF(type, st))
190 # define SKM_sk_push(type, st, val) \
191         sk_push(CHECKED_STACK_OF(type, st), CHECKED_PTR_OF(type, val))
192 # define SKM_sk_unshift(type, st, val) \
193         sk_unshift(CHECKED_STACK_OF(type, st), CHECKED_PTR_OF(type, val))
194 # define SKM_sk_find(type, st, val) \
195         sk_find(CHECKED_STACK_OF(type, st), CHECKED_PTR_OF(type, val))
196 # define SKM_sk_find_ex(type, st, val) \
197         sk_find_ex(CHECKED_STACK_OF(type, st), \
198                    CHECKED_PTR_OF(type, val))
199 # define SKM_sk_delete(type, st, i) \
200         (type *)sk_delete(CHECKED_STACK_OF(type, st), i)
201 # define SKM_sk_delete_ptr(type, st, ptr) \
202         (type *)sk_delete_ptr(CHECKED_STACK_OF(type, st), CHECKED_PTR_OF(type, ptr))
203 # define SKM_sk_insert(type, st,val, i) \
204         sk_insert(CHECKED_STACK_OF(type, st), CHECKED_PTR_OF(type, val), i)
205 # define SKM_sk_set_cmp_func(type, st, cmp) \
206         ((int (*)(const type * const *,const type * const *)) \
207         sk_set_cmp_func(CHECKED_STACK_OF(type, st), CHECKED_SK_CMP_FUNC(type, cmp)))
208 # define SKM_sk_dup(type, st) \
209         (STACK_OF(type) *)sk_dup(CHECKED_STACK_OF(type, st))
210 # define SKM_sk_pop_free(type, st, free_func) \
211         sk_pop_free(CHECKED_STACK_OF(type, st), CHECKED_SK_FREE_FUNC(type, free_func))
212 # define SKM_sk_deep_copy(type, st, copy_func, free_func) \
213         (STACK_OF(type) *)sk_deep_copy(CHECKED_STACK_OF(type, st), CHECKED_SK_COPY_FUNC(type, copy_func), CHECKED_SK_FREE_FUNC(type, free_func))
214 # define SKM_sk_shift(type, st) \
215         (type *)sk_shift(CHECKED_STACK_OF(type, st))
216 # define SKM_sk_pop(type, st) \
217         (type *)sk_pop(CHECKED_STACK_OF(type, st))
218 # define SKM_sk_sort(type, st) \
219         sk_sort(CHECKED_STACK_OF(type, st))
220 # define SKM_sk_is_sorted(type, st) \
221         sk_is_sorted(CHECKED_STACK_OF(type, st))
222
223 # define SKM_ASN1_SET_OF_d2i(type, st, pp, length, d2i_func, free_func, ex_tag, ex_class) \
224   (STACK_OF(type) *)d2i_ASN1_SET( \
225                                 (STACK_OF(OPENSSL_BLOCK) **)CHECKED_PTR_OF(STACK_OF(type)*, st), \
226                                 pp, length, \
227                                 CHECKED_D2I_OF(type, d2i_func), \
228                                 CHECKED_SK_FREE_FUNC(type, free_func), \
229                                 ex_tag, ex_class)
230 # define SKM_ASN1_SET_OF_i2d(type, st, pp, i2d_func, ex_tag, ex_class, is_set) \
231         i2d_ASN1_SET(CHECKED_STACK_OF(type, st), pp, \
232                                 CHECKED_I2D_OF(type, i2d_func), \
233                                 ex_tag, ex_class, is_set)
234
235 # define SKM_ASN1_seq_pack(type, st, i2d_func, buf, len) \
236         ASN1_seq_pack(CHECKED_PTR_OF(STACK_OF(type), st), \
237                         CHECKED_I2D_OF(type, i2d_func), buf, len)
238 # define SKM_ASN1_seq_unpack(type, buf, len, d2i_func, free_func) \
239         (STACK_OF(type) *)ASN1_seq_unpack(buf, \
240                               len, CHECKED_D2I_OF(type, d2i_func), \
241                               CHECKED_SK_FREE_FUNC(type, free_func))
242 # define SKM_PKCS12_decrypt_d2i(type, algor, d2i_func, free_func, pass, passlen, oct, seq) \
243         (STACK_OF(type) *)PKCS12_decrypt_d2i(algor, \
244                                 CHECKED_D2I_OF(type, d2i_func), \
245                                 CHECKED_SK_FREE_FUNC(type, free_func), \
246                                 pass, passlen, oct, seq)
247 EOF
248
249 my $old_stackfile;
250 {
251     local $/ = undef;
252     open(IN, "$safestack") || die "Can't open $safestack, $!";
253     $old_stackfile = <IN>;
254     close(IN);
255 }
256
257 my $type_thing;
258 foreach $type_thing (sort @stacklst) {
259     $new_stackfile .= <<EOF;
260
261 # define sk_${type_thing}_new(cmp) SKM_sk_new($type_thing, (cmp))
262 # define sk_${type_thing}_new_null() SKM_sk_new_null($type_thing)
263 # define sk_${type_thing}_free(st) SKM_sk_free($type_thing, (st))
264 # define sk_${type_thing}_num(st) SKM_sk_num($type_thing, (st))
265 # define sk_${type_thing}_value(st, i) SKM_sk_value($type_thing, (st), (i))
266 # define sk_${type_thing}_set(st, i, val) SKM_sk_set($type_thing, (st), (i), (val))
267 # define sk_${type_thing}_zero(st) SKM_sk_zero($type_thing, (st))
268 # define sk_${type_thing}_push(st, val) SKM_sk_push($type_thing, (st), (val))
269 # define sk_${type_thing}_unshift(st, val) SKM_sk_unshift($type_thing, (st), (val))
270 # define sk_${type_thing}_find(st, val) SKM_sk_find($type_thing, (st), (val))
271 # define sk_${type_thing}_find_ex(st, val) SKM_sk_find_ex($type_thing, (st), (val))
272 # define sk_${type_thing}_delete(st, i) SKM_sk_delete($type_thing, (st), (i))
273 # define sk_${type_thing}_delete_ptr(st, ptr) SKM_sk_delete_ptr($type_thing, (st), (ptr))
274 # define sk_${type_thing}_insert(st, val, i) SKM_sk_insert($type_thing, (st), (val), (i))
275 # define sk_${type_thing}_set_cmp_func(st, cmp) SKM_sk_set_cmp_func($type_thing, (st), (cmp))
276 # define sk_${type_thing}_dup(st) SKM_sk_dup($type_thing, st)
277 # define sk_${type_thing}_pop_free(st, free_func) SKM_sk_pop_free($type_thing, (st), (free_func))
278 # define sk_${type_thing}_deep_copy(st, copy_func, free_func) SKM_sk_deep_copy($type_thing, (st), (copy_func), (free_func))
279 # define sk_${type_thing}_shift(st) SKM_sk_shift($type_thing, (st))
280 # define sk_${type_thing}_pop(st) SKM_sk_pop($type_thing, (st))
281 # define sk_${type_thing}_sort(st) SKM_sk_sort($type_thing, (st))
282 # define sk_${type_thing}_is_sorted(st) SKM_sk_is_sorted($type_thing, (st))
283 EOF
284 }
285
286 foreach $type_thing (sort { $a->[0] cmp $b->[0]} @sstacklst) {
287     my $t1 = $type_thing->[0];
288     my $t2 = $type_thing->[1];
289     $new_stackfile .= <<EOF;
290
291 # define sk_${t1}_new(cmp) ((STACK_OF($t1) *)sk_new(CHECKED_SK_CMP_FUNC($t2, cmp)))
292 # define sk_${t1}_new_null() ((STACK_OF($t1) *)sk_new_null())
293 # define sk_${t1}_push(st, val) sk_push(CHECKED_STACK_OF($t1, st), CHECKED_PTR_OF($t2, val))
294 # define sk_${t1}_find(st, val) sk_find(CHECKED_STACK_OF($t1, st), CHECKED_PTR_OF($t2, val))
295 # define sk_${t1}_value(st, i) (($t1)sk_value(CHECKED_STACK_OF($t1, st), i))
296 # define sk_${t1}_num(st) SKM_sk_num($t1, st)
297 # define sk_${t1}_pop_free(st, free_func) sk_pop_free(CHECKED_STACK_OF($t1, st), CHECKED_SK_FREE_FUNC($t2, free_func))
298 # define sk_${t1}_deep_copy(st, copy_func, free_func) ((STACK_OF($t1) *)sk_deep_copy(CHECKED_STACK_OF($t1, st), CHECKED_SK_COPY_FUNC($t2, copy_func), CHECKED_SK_FREE_FUNC($t2, free_func)))
299 # define sk_${t1}_insert(st, val, i) sk_insert(CHECKED_STACK_OF($t1, st), CHECKED_PTR_OF($t2, val), i)
300 # define sk_${t1}_free(st) SKM_sk_free(${t1}, st)
301 # define sk_${t1}_set(st, i, val) sk_set(CHECKED_STACK_OF($t1, st), i, CHECKED_PTR_OF($t2, val))
302 # define sk_${t1}_zero(st) SKM_sk_zero($t1, (st))
303 # define sk_${t1}_unshift(st, val) sk_unshift(CHECKED_STACK_OF($t1, st), CHECKED_PTR_OF($t2, val))
304 # define sk_${t1}_find_ex(st, val) sk_find_ex((_STACK *)CHECKED_CONST_PTR_OF(STACK_OF($t1), st), CHECKED_CONST_PTR_OF($t2, val))
305 # define sk_${t1}_delete(st, i) SKM_sk_delete($t1, (st), (i))
306 # define sk_${t1}_delete_ptr(st, ptr) ($t1 *)sk_delete_ptr(CHECKED_STACK_OF($t1, st), CHECKED_PTR_OF($t2, ptr))
307 # define sk_${t1}_set_cmp_func(st, cmp)  \\
308         ((int (*)(const $t2 * const *,const $t2 * const *)) \\
309         sk_set_cmp_func(CHECKED_STACK_OF($t1, st), CHECKED_SK_CMP_FUNC($t2, cmp)))
310 # define sk_${t1}_dup(st) SKM_sk_dup($t1, st)
311 # define sk_${t1}_shift(st) SKM_sk_shift($t1, (st))
312 # define sk_${t1}_pop(st) ($t2 *)sk_pop(CHECKED_STACK_OF($t1, st))
313 # define sk_${t1}_sort(st) SKM_sk_sort($t1, (st))
314 # define sk_${t1}_is_sorted(st) SKM_sk_is_sorted($t1, (st))
315 EOF
316 }
317
318 foreach $type_thing (sort @lhashlst) {
319     my $lc_tt = lc $type_thing;
320     $new_stackfile .= <<EOF;
321
322 # define lh_${type_thing}_new() LHM_lh_new(${type_thing},${lc_tt})
323 # define lh_${type_thing}_insert(lh,inst) LHM_lh_insert(${type_thing},lh,inst)
324 # define lh_${type_thing}_retrieve(lh,inst) LHM_lh_retrieve(${type_thing},lh,inst)
325 # define lh_${type_thing}_delete(lh,inst) LHM_lh_delete(${type_thing},lh,inst)
326 # define lh_${type_thing}_doall(lh,fn) LHM_lh_doall(${type_thing},lh,fn)
327 # define lh_${type_thing}_doall_arg(lh,fn,arg_type,arg) \\
328   LHM_lh_doall_arg(${type_thing},lh,fn,arg_type,arg)
329 # define lh_${type_thing}_error(lh) LHM_lh_error(${type_thing},lh)
330 # define lh_${type_thing}_num_items(lh) LHM_lh_num_items(${type_thing},lh)
331 # define lh_${type_thing}_down_load(lh) LHM_lh_down_load(${type_thing},lh)
332 # define lh_${type_thing}_node_stats_bio(lh,out) \\
333   LHM_lh_node_stats_bio(${type_thing},lh,out)
334 # define lh_${type_thing}_node_usage_stats_bio(lh,out) \\
335   LHM_lh_node_usage_stats_bio(${type_thing},lh,out)
336 # define lh_${type_thing}_stats_bio(lh,out) \\
337   LHM_lh_stats_bio(${type_thing},lh,out)
338 # define lh_${type_thing}_free(lh) LHM_lh_free(${type_thing},lh)
339 EOF
340 }
341
342 $new_stackfile .= <<'EOF';
343
344 # ifdef  __cplusplus
345 }
346 # endif
347 #endif
348 EOF
349
350 if ($new_stackfile eq $old_stackfile) {
351     print "No changes to $safestack.\n";
352 }
353 elsif ($do_write) {
354     print "Writing new $safestack.\n";
355     open OUT, ">$safestack" || die "Can't open $safestack for writing, $!";
356     print OUT $new_stackfile;
357     close OUT;
358 }
359
360 exit 0;