1 #!/usr/local/bin/perl -w
3 my $safestack = "include/openssl/safestack.h";
7 $do_write = 1 if $_ eq "-write";
13 my @source = (<include/openssl/*.h>, <crypto/include/internal/*.h>, <crypto/*.[ch]>, <crypto/*/*.[ch]>, <ssl/*.[ch]>, <apps/*.[ch]>);
14 foreach $file (@source) {
17 # Open the .c/.h file for reading
18 open(IN, "< $file") || die "Can't open $file for reading, $!";
21 next unless /^DECLARE_/;
22 if (/^DECLARE_LHASH_OF\(([^)]+)\)/) {
29 my $new_stackfile = <<'EOF';
30 /* automatically generated by util/mkstack.pl */
31 /* ====================================================================
32 * Copyright (c) 1999-2015 The OpenSSL Project. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
60 * 6. Redistributions of any form whatsoever must retain the following
62 * "This product includes software developed by the OpenSSL Project
63 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
85 #ifndef HEADER_SAFESTACK_H
86 # define HEADER_SAFESTACK_H
88 # include <openssl/stack.h>
89 # include <openssl/e_os2.h>
95 # ifndef CHECKED_PTR_OF
96 # define CHECKED_PTR_OF(type, p) ((void*) (1 ? p : (type*)0))
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
105 # define CHECKED_STACK_OF(type, p) \
106 ((_STACK*) (1 ? p : (STACK_OF(type)*)0))
108 # define CHECKED_SK_COPY_FUNC(type, p) \
109 ((void *(*)(void *)) ((1 ? p : (type *(*)(const type *))0)))
111 # define CHECKED_SK_FREE_FUNC(type, p) \
112 ((void (*)(void *)) ((1 ? p : (void (*)(type *))0)))
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)))
118 # define STACK_OF(type) struct stack_st_##type
120 # define SKM_DEFINE_STACK_OF(t1, t2, t3) \
122 static ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
124 return sk_num((_STACK *)sk); \
126 static ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \
128 return (t2 *)sk_value((_STACK *)sk, idx); \
130 static ossl_inline STACK_OF(t1) *sk_##t1##_new(int (*cmpf)(const t3 * const *a, const t3 * const *b)) \
132 return (STACK_OF(t1) *)sk_new((int (*)(const void *a, const void *b))cmpf); \
134 static ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
136 return (STACK_OF(t1) *)sk_new_null(); \
138 static ossl_inline void sk_##t1##_free(const STACK_OF(t1) *sk) \
140 sk_free((_STACK *)sk); \
142 static ossl_inline void sk_##t1##_zero(const STACK_OF(t1) *sk) \
144 sk_zero((_STACK *)sk); \
146 static ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \
148 return (t2 *)sk_delete((_STACK *)sk, i); \
150 static ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \
152 return (t2 *)sk_delete_ptr((_STACK *)sk, (void *)ptr); \
154 static ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \
156 return sk_push((_STACK *)sk, (void *)ptr); \
158 static ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \
160 return sk_unshift((_STACK *)sk, (void *)ptr); \
162 static ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \
164 return (t2 *)sk_pop((_STACK *)sk); \
166 static ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \
168 return (t2 *)sk_shift((_STACK *)sk); \
170 static ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, void (*func)(t3 *a)) \
172 sk_pop_free((_STACK *)sk, (void (*)(void *))func); \
174 static ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \
176 return sk_insert((_STACK *)sk, (void *)ptr, idx); \
178 static ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \
180 return (t2 *)sk_set((_STACK *)sk, idx, (void *)ptr); \
182 static ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \
184 return sk_find((_STACK *)sk, (void *)ptr); \
186 static ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \
188 return sk_find_ex((_STACK *)sk, (void *)ptr); \
190 static ossl_inline void sk_##t1##_sort(const STACK_OF(t1) *sk) \
192 sk_sort((_STACK *)sk); \
194 static ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \
196 return sk_is_sorted((_STACK *)sk); \
198 static ossl_inline STACK_OF(t1) * sk_##t1##_dup(STACK_OF(t1) *sk) \
200 return (STACK_OF(t1) *)sk_dup((_STACK *)sk); \
202 static ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(STACK_OF(t1) *sk, \
203 t3 *(*copyfn)(const t3 *), \
204 void (*freefn)(t3 *)) \
206 return (STACK_OF(t1) *)sk_deep_copy((_STACK *)sk, \
207 (void * (*)(void *a))copyfn, \
208 (void (*)(void *a))freefn); \
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 *) \
212 return (int (*)(const t3 * const *,const t3 * const *))sk_set_cmp_func((_STACK *)sk, (int (*)(const void *a, const void *b))cmpf); \
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)
220 * Strings are special: normally an lhash entry will point to a single
221 * (somewhat) mutable object. In the case of strings:
223 * a) Instead of a single char, there is an array of chars, NUL-terminated.
224 * b) The string may have be immutable.
226 * So, they need their own declarations. Especially important for
227 * type-checking tools, such as Deputy.
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
233 typedef char *OPENSSL_STRING;
234 typedef const char *OPENSSL_CSTRING;
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.
243 DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char)
246 * Similarly, we sometimes use a block of characters, NOT nul-terminated.
247 * These should also be distinguished from "normal" stacks.
249 typedef void *OPENSSL_BLOCK;
250 DEFINE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void)
253 * This file is automatically generated by util/mkstack.pl
258 * SKM_sk_... stack macros are internal to safestack.h: never use them
259 * directly, use sk_<type>_... instead
267 open(IN, "$safestack") || die "Can't open $safestack, $!";
268 $old_stackfile = <IN>;
274 foreach $type_thing (sort @lhashlst) {
275 my $lc_tt = lc $type_thing;
276 $new_stackfile .= <<EOF;
278 # define lh_${type_thing}_new() LHM_lh_new(${type_thing},${lc_tt})
279 # define lh_${type_thing}_insert(lh,inst) LHM_lh_insert(${type_thing},lh,inst)
280 # define lh_${type_thing}_retrieve(lh,inst) LHM_lh_retrieve(${type_thing},lh,inst)
281 # define lh_${type_thing}_delete(lh,inst) LHM_lh_delete(${type_thing},lh,inst)
282 # define lh_${type_thing}_doall(lh,fn) LHM_lh_doall(${type_thing},lh,fn)
283 # define lh_${type_thing}_doall_arg(lh,fn,arg_type,arg) \\
284 LHM_lh_doall_arg(${type_thing},lh,fn,arg_type,arg)
285 # define lh_${type_thing}_error(lh) LHM_lh_error(${type_thing},lh)
286 # define lh_${type_thing}_num_items(lh) LHM_lh_num_items(${type_thing},lh)
287 # define lh_${type_thing}_down_load(lh) LHM_lh_down_load(${type_thing},lh)
288 # define lh_${type_thing}_node_stats_bio(lh,out) \\
289 LHM_lh_node_stats_bio(${type_thing},lh,out)
290 # define lh_${type_thing}_node_usage_stats_bio(lh,out) \\
291 LHM_lh_node_usage_stats_bio(${type_thing},lh,out)
292 # define lh_${type_thing}_stats_bio(lh,out) \\
293 LHM_lh_stats_bio(${type_thing},lh,out)
294 # define lh_${type_thing}_free(lh) LHM_lh_free(${type_thing},lh)
298 $new_stackfile .= <<'EOF';
306 if ($new_stackfile eq $old_stackfile) {
307 print "No changes to $safestack.\n";
310 print "Writing new $safestack.\n";
311 open OUT, ">$safestack" || die "Can't open $safestack for writing, $!";
312 print OUT $new_stackfile;