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