1 #!/usr/local/bin/perl -w
3 # Search out "DECLARE_STACK_OF()" # declarations in .h and .c files,
4 # and create corresponding macro declarations for crypto/stack/safestack.h.
6 my $safestack = "crypto/stack/safestack.h";
10 $do_write = 1 if $_ eq "-write";
18 my @source = (<crypto/*.[ch]>, <crypto/*/*.[ch]>, <ssl/*.[ch]>, <apps/*.[ch]>);
19 foreach $file (@source) {
22 # Open the .c/.h file for reading
23 open(IN, "< $file") || die "Can't open $file for reading, $!";
26 next unless /^DECLARE_/;
27 if (/^DECLARE_STACK_OF\(([^)]+)\)/) {
30 elsif (/^DECLARE_SPECIAL_STACK_OF\(([^,\s]+)\s*,\s*([^>\s]+)\)/) {
31 push @sstacklst, [$1, $2];
33 elsif (/^DECLARE_ASN1_SET_OF\(([^)]+)\)/) {
36 elsif (/^DECLARE_PKCS12_STACK_OF\(([^)]+)\)/) {
39 elsif (/^DECLARE_LHASH_OF\(([^)]+)\)/) {
46 my $new_stackfile = <<'EOF';
47 /* automatically generated by util/mkstack.pl */
48 /* ====================================================================
49 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
55 * 1. Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
77 * 6. Redistributions of any form whatsoever must retain the following
79 * "This product includes software developed by the OpenSSL Project
80 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
102 #ifndef HEADER_SAFESTACK_H
103 # define HEADER_SAFESTACK_H
105 # include <openssl/stack.h>
111 # ifndef CHECKED_PTR_OF
112 # define CHECKED_PTR_OF(type, p) ((void*) (1 ? p : (type*)0))
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
121 # define CHECKED_STACK_OF(type, p) \
122 ((_STACK*) (1 ? p : (STACK_OF(type)*)0))
124 # define CHECKED_SK_COPY_FUNC(type, p) \
125 ((void *(*)(void *)) ((1 ? p : (type *(*)(const type *))0)))
127 # define CHECKED_SK_FREE_FUNC(type, p) \
128 ((void (*)(void *)) ((1 ? p : (void (*)(type *))0)))
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)))
134 # define STACK_OF(type) struct stack_st_##type
135 # define PREDECLARE_STACK_OF(type) STACK_OF(type);
137 # define DECLARE_STACK_OF(type) \
142 # define DECLARE_SPECIAL_STACK_OF(type, type2) \
149 * Strings are special: normally an lhash entry will point to a single
150 * (somewhat) mutable object. In the case of strings:
152 * a) Instead of a single char, there is an array of chars, NUL-terminated.
153 * b) The string may have be immutable.
155 * So, they need their own declarations. Especially important for
156 * type-checking tools, such as Deputy.
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
162 typedef char *OPENSSL_STRING;
163 typedef const char *OPENSSL_CSTRING;
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.
172 DECLARE_SPECIAL_STACK_OF(OPENSSL_STRING, char)
175 * Similarly, we sometimes use a block of characters, NOT nul-terminated.
176 * These should also be distinguished from "normal" stacks.
178 typedef void *OPENSSL_BLOCK;
179 DECLARE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void)
182 * This file is automatically generated by util/mkstack.pl
187 * SKM_sk_... stack macros are internal to safestack.h: never use them
188 * directly, use sk_<type>_... instead
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))
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), \
241 CHECKED_D2I_OF(type, d2i_func), \
242 CHECKED_SK_FREE_FUNC(type, free_func), \
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)
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)
266 open(IN, "$safestack") || die "Can't open $safestack, $!";
267 $old_stackfile = <IN>;
272 foreach $type_thing (sort @stacklst) {
273 $new_stackfile .= <<EOF;
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))
300 foreach $type_thing (sort @sstacklst) {
301 my $t1 = $type_thing->[0];
302 my $t2 = $type_thing->[1];
303 $new_stackfile .= <<EOF;
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))
332 foreach $type_thing (sort @asn1setlst) {
333 $new_stackfile .= <<EOF;
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))
346 foreach $type_thing (sort @p12stklst) {
347 $new_stackfile .= <<EOF;
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))
354 foreach $type_thing (sort @lhashlst) {
355 my $lc_tt = lc $type_thing;
356 $new_stackfile .= <<EOF;
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)
378 $new_stackfile .= <<'EOF';
386 if ($new_stackfile eq $old_stackfile) {
387 print "No changes to $safestack.\n";
390 print "Writing new $safestack.\n";
391 open OUT, ">$safestack" || die "Can't open $safestack for writing, $!";
392 print OUT $new_stackfile;