e188840595a3d371fc38caa93615441b0302e502
[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
120 # define SKM_DEFINE_STACK_OF(t1, t2, t3) \
121     STACK_OF(t1); \
122     static ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
123     { \
124         return sk_num((_STACK *)sk); \
125     } \
126     static ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \
127     { \
128         return (t2 *)sk_value((_STACK *)sk, idx); \
129     } \
130     static ossl_inline STACK_OF(t1) *sk_##t1##_new(int (*cmpf)(const t3 * const *a, const t3 * const *b)) \
131     { \
132         return (STACK_OF(t1) *)sk_new((int (*)(const void *a, const void *b))cmpf); \
133     } \
134     static ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
135     { \
136         return (STACK_OF(t1) *)sk_new_null(); \
137     } \
138     static ossl_inline void sk_##t1##_free(const STACK_OF(t1) *sk) \
139     { \
140         sk_free((_STACK *)sk); \
141     } \
142     static ossl_inline void sk_##t1##_zero(const STACK_OF(t1) *sk) \
143     { \
144         sk_zero((_STACK *)sk); \
145     } \
146     static ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \
147     { \
148         return (t2 *)sk_delete((_STACK *)sk, i); \
149     } \
150     static ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \
151     { \
152         return (t2 *)sk_delete_ptr((_STACK *)sk, (void *)ptr); \
153     } \
154     static ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \
155     { \
156         return sk_push((_STACK *)sk, (void *)ptr); \
157     } \
158     static ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \
159     { \
160         return sk_unshift((_STACK *)sk, (void *)ptr); \
161     } \
162     static ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \
163     { \
164         return (t2 *)sk_pop((_STACK *)sk); \
165     } \
166     static ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \
167     { \
168         return (t2 *)sk_shift((_STACK *)sk); \
169     } \
170     static ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, void (*func)(t3 *a)) \
171     { \
172         sk_pop_free((_STACK *)sk, (void (*)(void *))func); \
173     } \
174     static ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \
175     { \
176         return sk_insert((_STACK *)sk, (void *)ptr, idx); \
177     } \
178     static ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \
179     { \
180         return (t2 *)sk_set((_STACK *)sk, idx, (void *)ptr); \
181     } \
182     static ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \
183     { \
184         return sk_find((_STACK *)sk, (void *)ptr); \
185     } \
186     static ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \
187     { \
188         return sk_find_ex((_STACK *)sk, (void *)ptr); \
189     } \
190     static ossl_inline void sk_##t1##_sort(const STACK_OF(t1) *sk) \
191     { \
192         sk_sort((_STACK *)sk); \
193     } \
194     static ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \
195     { \
196         return sk_is_sorted((_STACK *)sk); \
197     } \
198     static ossl_inline STACK_OF(t1) * sk_##t1##_dup(STACK_OF(t1) *sk) \
199     { \
200         return (STACK_OF(t1) *)sk_dup((_STACK *)sk); \
201     } \
202     static ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(STACK_OF(t1) *sk, \
203                                                     t3 *(*copyfn)(const t3 *), \
204                                                     void (*freefn)(t3 *)) \
205     { \
206         return (STACK_OF(t1) *)sk_deep_copy((_STACK *)sk, \
207                                             (void * (*)(void *a))copyfn, \
208                                             (void (*)(void *a))freefn); \
209     } \
210     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 *) \
211     { \
212         return (int (*)(const t3 * const *,const t3 * const *))sk_set_cmp_func((_STACK *)sk, (int (*)(const void *a, const void *b))cmpf); \
213     }
214
215 # define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2)
216 # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
217 # define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t)
218
219 /*-
220  * Strings are special: normally an lhash entry will point to a single
221  * (somewhat) mutable object. In the case of strings:
222  *
223  * a) Instead of a single char, there is an array of chars, NUL-terminated.
224  * b) The string may have be immutable.
225  *
226  * So, they need their own declarations. Especially important for
227  * type-checking tools, such as Deputy.
228  *
229  * In practice, however, it appears to be hard to have a const
230  * string. For now, I'm settling for dealing with the fact it is a
231  * string at all.
232  */
233 typedef char *OPENSSL_STRING;
234 typedef const char *OPENSSL_CSTRING;
235
236 /*-
237  * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but
238  * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned
239  * above, instead of a single char each entry is a NUL-terminated array of
240  * chars. So, we have to implement STRING specially for STACK_OF. This is
241  * dealt with in the autogenerated macros below.
242  */
243 DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char)
244
245 /*
246  * Similarly, we sometimes use a block of characters, NOT nul-terminated.
247  * These should also be distinguished from "normal" stacks.
248  */
249 typedef void *OPENSSL_BLOCK;
250 DEFINE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void)
251
252 /*
253  * This file is automatically generated by util/mkstack.pl
254  * Do not edit!
255  */
256
257 /*
258  * SKM_sk_... stack macros are internal to safestack.h: never use them
259  * directly, use sk_<type>_... instead
260  */
261
262 EOF
263
264 my $old_stackfile;
265 {
266     local $/ = undef;
267     open(IN, "$safestack") || die "Can't open $safestack, $!";
268     $old_stackfile = <IN>;
269     close(IN);
270 }
271
272 my $type_thing;
273
274 foreach $type_thing (sort @lhashlst) {
275     my $lc_tt = lc $type_thing;
276     $new_stackfile .= <<EOF;
277
278 # define lh_${type_thing}_doall(lh,fn) LHM_lh_doall(${type_thing},lh,fn)
279 # define lh_${type_thing}_doall_arg(lh,fn,arg_type,arg) \\
280   LHM_lh_doall_arg(${type_thing},lh,fn,arg_type,arg)
281 EOF
282 }
283
284 $new_stackfile .= <<'EOF';
285
286 # ifdef  __cplusplus
287 }
288 # endif
289 #endif
290 EOF
291
292 if ($new_stackfile eq $old_stackfile) {
293     print "No changes to $safestack.\n";
294 }
295 elsif ($do_write) {
296     print "Writing new $safestack.\n";
297     open OUT, ">$safestack" || die "Can't open $safestack for writing, $!";
298     print OUT $new_stackfile;
299     close OUT;
300 }
301
302 exit 0;