]>
Commit | Line | Data |
---|---|---|
440e5d80 | 1 | /* |
629192c1 | 2 | * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved. |
e6390aca | 3 | * |
440e5d80 RS |
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 | |
e6390aca | 8 | */ |
440e5d80 | 9 | |
e6390aca | 10 | #include <stdio.h> |
e6390aca RS |
11 | #include <string.h> |
12 | #include <stdlib.h> | |
629192c1 | 13 | #include <assert.h> |
e6390aca RS |
14 | #include <openssl/crypto.h> |
15 | ||
629192c1 RS |
16 | static long saved_argl; |
17 | static void *saved_argp; | |
18 | static int saved_idx; | |
e6390aca RS |
19 | |
20 | static void exnew(void *parent, void *ptr, CRYPTO_EX_DATA *ad, | |
21 | int idx, long argl, void *argp) | |
22 | { | |
629192c1 RS |
23 | assert(idx == saved_idx); |
24 | assert(argl == saved_argl); | |
25 | assert(argp == saved_argp); | |
e6390aca RS |
26 | } |
27 | ||
629192c1 | 28 | static int exdup(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, |
e6390aca RS |
29 | void *from_d, int idx, long argl, void *argp) |
30 | { | |
629192c1 RS |
31 | assert(idx == saved_idx); |
32 | assert(argl == saved_argl); | |
33 | assert(argp == saved_argp); | |
e6390aca RS |
34 | return 0; |
35 | } | |
36 | ||
37 | static void exfree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, | |
38 | int idx, long argl, void *argp) | |
39 | { | |
629192c1 RS |
40 | assert(idx == saved_idx); |
41 | assert(argl == saved_argl); | |
42 | assert(argp == saved_argp); | |
e6390aca RS |
43 | } |
44 | ||
45 | typedef struct myobj_st { | |
46 | CRYPTO_EX_DATA ex_data; | |
47 | int id; | |
629192c1 | 48 | int st; |
e6390aca RS |
49 | } MYOBJ; |
50 | ||
51 | static MYOBJ *MYOBJ_new() | |
52 | { | |
53 | static int count = 0; | |
54 | MYOBJ *obj = OPENSSL_malloc(sizeof(*obj)); | |
e6390aca RS |
55 | |
56 | obj->id = ++count; | |
629192c1 RS |
57 | obj->st = CRYPTO_new_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data); |
58 | assert(obj->st != 0); | |
e6390aca RS |
59 | return obj; |
60 | } | |
61 | ||
62 | static void MYOBJ_sethello(MYOBJ *obj, char *cp) | |
63 | { | |
629192c1 RS |
64 | obj->st = CRYPTO_set_ex_data(&obj->ex_data, saved_idx, cp); |
65 | assert(obj->st != 0); | |
e6390aca RS |
66 | } |
67 | ||
68 | static char *MYOBJ_gethello(MYOBJ *obj) | |
69 | { | |
629192c1 | 70 | return CRYPTO_get_ex_data(&obj->ex_data, saved_idx); |
e6390aca RS |
71 | } |
72 | ||
73 | static void MYOBJ_free(MYOBJ *obj) | |
74 | { | |
75 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_APP, obj, &obj->ex_data); | |
76 | OPENSSL_free(obj); | |
77 | } | |
78 | ||
79 | int main() | |
80 | { | |
81 | MYOBJ *t1, *t2; | |
82 | const char *cp; | |
83 | char *p; | |
84 | ||
85 | p = strdup("hello world"); | |
629192c1 RS |
86 | saved_argl = 21; |
87 | saved_argp = malloc(1); | |
88 | saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP, | |
89 | saved_argl, saved_argp, | |
90 | exnew, exdup, exfree); | |
e6390aca RS |
91 | t1 = MYOBJ_new(); |
92 | t2 = MYOBJ_new(); | |
93 | MYOBJ_sethello(t1, p); | |
94 | cp = MYOBJ_gethello(t1); | |
95 | assert(cp == p); | |
629192c1 RS |
96 | if (cp != p) |
97 | return 1; | |
e6390aca RS |
98 | cp = MYOBJ_gethello(t2); |
99 | assert(cp == NULL); | |
629192c1 RS |
100 | if (cp != NULL) |
101 | return 1; | |
e6390aca RS |
102 | MYOBJ_free(t1); |
103 | MYOBJ_free(t2); | |
629192c1 | 104 | free(saved_argp); |
e6390aca RS |
105 | free(p); |
106 | return 0; | |
107 | } |