2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/crypto.h>
15 static long saved_argl;
16 static void *saved_argp;
19 static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
20 int idx, long argl, void *argp)
22 OPENSSL_assert(idx == saved_idx);
23 OPENSSL_assert(argl == saved_argl);
24 OPENSSL_assert(argp == saved_argp);
25 OPENSSL_assert(ptr == NULL);
28 static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from,
29 void *from_d, int idx, long argl, void *argp)
31 OPENSSL_assert(idx == saved_idx);
32 OPENSSL_assert(argl == saved_argl);
33 OPENSSL_assert(argp == saved_argp);
34 OPENSSL_assert(from_d != NULL);
38 static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
39 int idx, long argl, void *argp)
41 OPENSSL_assert(idx == saved_idx);
42 OPENSSL_assert(argl == saved_argl);
43 OPENSSL_assert(argp == saved_argp);
46 typedef struct myobj_st {
47 CRYPTO_EX_DATA ex_data;
52 static MYOBJ *MYOBJ_new()
55 MYOBJ *obj = OPENSSL_malloc(sizeof(*obj));
58 obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
59 OPENSSL_assert(obj->st != 0);
63 static void MYOBJ_sethello(MYOBJ *obj, char *cp)
65 obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp);
66 OPENSSL_assert(obj->st != 0);
69 static char *MYOBJ_gethello(MYOBJ *obj)
71 return CRYPTO_get_ex_data(&obj->ex_data, saved_idx);
74 static void MYOBJ_free(MYOBJ *obj)
76 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data);
80 static MYOBJ *MYOBJ_dup(MYOBJ *in)
82 MYOBJ *obj = MYOBJ_new();
84 obj->st = CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_APP, &obj->ex_data,
86 OPENSSL_assert(obj->st != 0);
96 p = strdup("hello world");
98 saved_argp = malloc(1);
99 saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
100 saved_argl, saved_argp,
101 exnew, exdup, exfree);
104 MYOBJ_sethello(t1, p);
105 cp = MYOBJ_gethello(t1);
106 OPENSSL_assert(cp == p);
109 cp = MYOBJ_gethello(t2);
110 OPENSSL_assert(cp == NULL);
114 cp = MYOBJ_gethello(t3);
115 OPENSSL_assert(cp == p);
118 cp = MYOBJ_gethello(t2);