1#!/bin/sh
2
3include_dir=../../include/openssl
4
5cat > "${include_dir}/stack_macros.h" << EOF
6/* Copyright (c) 2014, Google Inc.
7 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
15 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
17 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
19
20#if !defined(IN_STACK_H)
21#error "Don't include this file directly. Include stack.h."
22#endif
23
24EOF
25
26output_stack () {
27  type=$1
28  ptrtype=$2
29
30  cat >> "${include_dir}/stack_macros.h" << EOF
31/* ${type} */
32#define sk_${type}_new(comp)\\
33  ((STACK_OF(${type})*) sk_new(CHECKED_CAST(stack_cmp_func, int (*) (const ${ptrtype} *a, const ${ptrtype} *b), comp)))
34
35#define sk_${type}_new_null()\\
36  ((STACK_OF(${type})*) sk_new_null())
37
38#define sk_${type}_num(sk)\\
39  sk_num(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk))
40
41#define sk_${type}_zero(sk)\\
42  sk_zero(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk));
43
44#define sk_${type}_value(sk, i)\\
45  ((${ptrtype}) sk_value(CHECKED_CAST(_STACK*, const STACK_OF(${type})*, sk), (i)))
46
47#define sk_${type}_set(sk, i, p)\\
48  ((${ptrtype}) sk_set(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), (i), CHECKED_CAST(void*, ${ptrtype}, p)))
49
50#define sk_${type}_free(sk)\\
51  sk_free(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk))
52
53#define sk_${type}_pop_free(sk, free_func)\\
54  sk_pop_free(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(void (*) (void*), void (*) (${ptrtype}), free_func))
55
56#define sk_${type}_insert(sk, p, where)\\
57  sk_insert(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(void*, ${ptrtype}, p), (where))
58
59#define sk_${type}_delete(sk, where)\\
60  ((${ptrtype}) sk_delete(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), (where)))
61
62#define sk_${type}_delete_ptr(sk, p)\\
63  ((${ptrtype}) sk_delete_ptr(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(void*, ${ptrtype}, p)))
64
65#define sk_${type}_find(sk, out_index, p)\\
66  sk_find(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), (out_index), CHECKED_CAST(void*, ${ptrtype}, p))
67
68#define sk_${type}_shift(sk)\\
69  ((${ptrtype}) sk_shift(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk)))
70
71#define sk_${type}_push(sk, p)\\
72  sk_push(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(void*, ${ptrtype}, p))
73
74#define sk_${type}_pop(sk)\\
75  ((${ptrtype}) sk_pop(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk)))
76
77#define sk_${type}_dup(sk)\\
78  ((STACK_OF(${type})*) sk_dup(CHECKED_CAST(_STACK*, const STACK_OF(${type})*, sk)))
79
80#define sk_${type}_sort(sk)\\
81  sk_sort(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk))
82
83#define sk_${type}_is_sorted(sk)\\
84  sk_is_sorted(CHECKED_CAST(_STACK*, const STACK_OF(${type})*, sk))
85
86#define sk_${type}_set_cmp_func(sk, comp)\\
87  ((int (*) (const ${type} **a, const ${type} **b)) sk_set_cmp_func(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(stack_cmp_func, int (*) (const ${type} **a, const ${type} **b), comp)))
88
89#define sk_${type}_deep_copy(sk, copy_func, free_func)\\
90((STACK_OF(${type})*) sk_deep_copy(CHECKED_CAST(const _STACK*, const STACK_OF(${type})*, sk), CHECKED_CAST(void* (*) (void*), ${ptrtype} (*) (${ptrtype}), copy_func), CHECKED_CAST(void (*) (void*), void (*) (${ptrtype}), free_func)))
91
92
93EOF
94}
95
96stack_types=$(cat "${include_dir}/stack.h" | grep '^ \* STACK_OF:' | sed -e 's/.*STACK_OF://' -e 's/ .*//')
97const_stack_types=$(cat "${include_dir}/stack.h" | grep '^ \* CONST_STACK_OF:' | sed -e 's/.*CONST_STACK_OF://' -e 's/ .*//')
98special_stack_types=$(cat "${include_dir}/stack.h" | grep '^ \* SPECIAL_STACK_OF:' | sed -e 's/.*SPECIAL_STACK_OF://' -e 's/ .*//')
99
100for type in $stack_types; do
101  echo Stack of ${type}
102  output_stack "${type}" "${type} *"
103done
104
105for type in $const_stack_types; do
106  echo Stack of ${type}
107  output_stack "${type}" "const ${type} *"
108done
109
110for type in $special_stack_types; do
111  echo Stack of ${type}
112  output_stack "${type}" "${type}"
113done
114
115clang-format -i "${include_dir}/stack_macros.h"
116