jpayne@69: /* jpayne@69: * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved. jpayne@69: * jpayne@69: * Licensed under the OpenSSL license (the "License"). You may not use jpayne@69: * this file except in compliance with the License. You can obtain a copy jpayne@69: * in the file LICENSE in the source distribution or at jpayne@69: * https://www.openssl.org/source/license.html jpayne@69: */ jpayne@69: jpayne@69: #ifndef HEADER_SAFESTACK_H jpayne@69: # define HEADER_SAFESTACK_H jpayne@69: jpayne@69: # include jpayne@69: # include jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: extern "C" { jpayne@69: #endif jpayne@69: jpayne@69: # define STACK_OF(type) struct stack_st_##type jpayne@69: jpayne@69: # define SKM_DEFINE_STACK_OF(t1, t2, t3) \ jpayne@69: STACK_OF(t1); \ jpayne@69: typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \ jpayne@69: typedef void (*sk_##t1##_freefunc)(t3 *a); \ jpayne@69: typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \ jpayne@69: static ossl_unused ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \ jpayne@69: { \ jpayne@69: return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \ jpayne@69: { \ jpayne@69: return (t2 *)OPENSSL_sk_value((const OPENSSL_STACK *)sk, idx); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \ jpayne@69: { \ jpayne@69: return (STACK_OF(t1) *)OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \ jpayne@69: { \ jpayne@69: return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_reserve(sk_##t1##_compfunc compare, int n) \ jpayne@69: { \ jpayne@69: return (STACK_OF(t1) *)OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \ jpayne@69: { \ jpayne@69: return OPENSSL_sk_reserve((OPENSSL_STACK *)sk, n); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline void sk_##t1##_free(STACK_OF(t1) *sk) \ jpayne@69: { \ jpayne@69: OPENSSL_sk_free((OPENSSL_STACK *)sk); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline void sk_##t1##_zero(STACK_OF(t1) *sk) \ jpayne@69: { \ jpayne@69: OPENSSL_sk_zero((OPENSSL_STACK *)sk); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \ jpayne@69: { \ jpayne@69: return (t2 *)OPENSSL_sk_delete((OPENSSL_STACK *)sk, i); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \ jpayne@69: { \ jpayne@69: return (t2 *)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)sk, \ jpayne@69: (const void *)ptr); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \ jpayne@69: { \ jpayne@69: return OPENSSL_sk_push((OPENSSL_STACK *)sk, (const void *)ptr); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \ jpayne@69: { \ jpayne@69: return OPENSSL_sk_unshift((OPENSSL_STACK *)sk, (const void *)ptr); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \ jpayne@69: { \ jpayne@69: return (t2 *)OPENSSL_sk_pop((OPENSSL_STACK *)sk); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \ jpayne@69: { \ jpayne@69: return (t2 *)OPENSSL_sk_shift((OPENSSL_STACK *)sk); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \ jpayne@69: { \ jpayne@69: OPENSSL_sk_pop_free((OPENSSL_STACK *)sk, (OPENSSL_sk_freefunc)freefunc); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \ jpayne@69: { \ jpayne@69: return OPENSSL_sk_insert((OPENSSL_STACK *)sk, (const void *)ptr, idx); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \ jpayne@69: { \ jpayne@69: return (t2 *)OPENSSL_sk_set((OPENSSL_STACK *)sk, idx, (const void *)ptr); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \ jpayne@69: { \ jpayne@69: return OPENSSL_sk_find((OPENSSL_STACK *)sk, (const void *)ptr); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \ jpayne@69: { \ jpayne@69: return OPENSSL_sk_find_ex((OPENSSL_STACK *)sk, (const void *)ptr); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline void sk_##t1##_sort(STACK_OF(t1) *sk) \ jpayne@69: { \ jpayne@69: OPENSSL_sk_sort((OPENSSL_STACK *)sk); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \ jpayne@69: { \ jpayne@69: return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)sk); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline STACK_OF(t1) * sk_##t1##_dup(const STACK_OF(t1) *sk) \ jpayne@69: { \ jpayne@69: return (STACK_OF(t1) *)OPENSSL_sk_dup((const OPENSSL_STACK *)sk); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(const STACK_OF(t1) *sk, \ jpayne@69: sk_##t1##_copyfunc copyfunc, \ jpayne@69: sk_##t1##_freefunc freefunc) \ jpayne@69: { \ jpayne@69: return (STACK_OF(t1) *)OPENSSL_sk_deep_copy((const OPENSSL_STACK *)sk, \ jpayne@69: (OPENSSL_sk_copyfunc)copyfunc, \ jpayne@69: (OPENSSL_sk_freefunc)freefunc); \ jpayne@69: } \ jpayne@69: static ossl_unused ossl_inline sk_##t1##_compfunc sk_##t1##_set_cmp_func(STACK_OF(t1) *sk, sk_##t1##_compfunc compare) \ jpayne@69: { \ jpayne@69: return (sk_##t1##_compfunc)OPENSSL_sk_set_cmp_func((OPENSSL_STACK *)sk, (OPENSSL_sk_compfunc)compare); \ jpayne@69: } jpayne@69: jpayne@69: # define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2) jpayne@69: # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t) jpayne@69: # define DEFINE_SPECIAL_STACK_OF_CONST(t1, t2) \ jpayne@69: SKM_DEFINE_STACK_OF(t1, const t2, t2) jpayne@69: # define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t) jpayne@69: jpayne@69: /*- jpayne@69: * Strings are special: normally an lhash entry will point to a single jpayne@69: * (somewhat) mutable object. In the case of strings: jpayne@69: * jpayne@69: * a) Instead of a single char, there is an array of chars, NUL-terminated. jpayne@69: * b) The string may have be immutable. jpayne@69: * jpayne@69: * So, they need their own declarations. Especially important for jpayne@69: * type-checking tools, such as Deputy. jpayne@69: * jpayne@69: * In practice, however, it appears to be hard to have a const jpayne@69: * string. For now, I'm settling for dealing with the fact it is a jpayne@69: * string at all. jpayne@69: */ jpayne@69: typedef char *OPENSSL_STRING; jpayne@69: typedef const char *OPENSSL_CSTRING; jpayne@69: jpayne@69: /*- jpayne@69: * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but jpayne@69: * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned jpayne@69: * above, instead of a single char each entry is a NUL-terminated array of jpayne@69: * chars. So, we have to implement STRING specially for STACK_OF. This is jpayne@69: * dealt with in the autogenerated macros below. jpayne@69: */ jpayne@69: DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char) jpayne@69: DEFINE_SPECIAL_STACK_OF_CONST(OPENSSL_CSTRING, char) jpayne@69: jpayne@69: /* jpayne@69: * Similarly, we sometimes use a block of characters, NOT nul-terminated. jpayne@69: * These should also be distinguished from "normal" stacks. jpayne@69: */ jpayne@69: typedef void *OPENSSL_BLOCK; jpayne@69: DEFINE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void) jpayne@69: jpayne@69: /* jpayne@69: * If called without higher optimization (min. -xO3) the Oracle Developer jpayne@69: * Studio compiler generates code for the defined (static inline) functions jpayne@69: * above. jpayne@69: * This would later lead to the linker complaining about missing symbols when jpayne@69: * this header file is included but the resulting object is not linked against jpayne@69: * the Crypto library (openssl#6912). jpayne@69: */ jpayne@69: # ifdef __SUNPRO_C jpayne@69: # pragma weak OPENSSL_sk_num jpayne@69: # pragma weak OPENSSL_sk_value jpayne@69: # pragma weak OPENSSL_sk_new jpayne@69: # pragma weak OPENSSL_sk_new_null jpayne@69: # pragma weak OPENSSL_sk_new_reserve jpayne@69: # pragma weak OPENSSL_sk_reserve jpayne@69: # pragma weak OPENSSL_sk_free jpayne@69: # pragma weak OPENSSL_sk_zero jpayne@69: # pragma weak OPENSSL_sk_delete jpayne@69: # pragma weak OPENSSL_sk_delete_ptr jpayne@69: # pragma weak OPENSSL_sk_push jpayne@69: # pragma weak OPENSSL_sk_unshift jpayne@69: # pragma weak OPENSSL_sk_pop jpayne@69: # pragma weak OPENSSL_sk_shift jpayne@69: # pragma weak OPENSSL_sk_pop_free jpayne@69: # pragma weak OPENSSL_sk_insert jpayne@69: # pragma weak OPENSSL_sk_set jpayne@69: # pragma weak OPENSSL_sk_find jpayne@69: # pragma weak OPENSSL_sk_find_ex jpayne@69: # pragma weak OPENSSL_sk_sort jpayne@69: # pragma weak OPENSSL_sk_is_sorted jpayne@69: # pragma weak OPENSSL_sk_dup jpayne@69: # pragma weak OPENSSL_sk_deep_copy jpayne@69: # pragma weak OPENSSL_sk_set_cmp_func jpayne@69: # endif /* __SUNPRO_C */ jpayne@69: jpayne@69: # ifdef __cplusplus jpayne@69: } jpayne@69: # endif jpayne@69: #endif