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