diff options
Diffstat (limited to 'src/lib/libcrypto/stack/stack.c')
-rw-r--r-- | src/lib/libcrypto/stack/stack.c | 163 |
1 files changed, 95 insertions, 68 deletions
diff --git a/src/lib/libcrypto/stack/stack.c b/src/lib/libcrypto/stack/stack.c index 610ccbb756..2496f28a8c 100644 --- a/src/lib/libcrypto/stack/stack.c +++ b/src/lib/libcrypto/stack/stack.c | |||
@@ -59,7 +59,7 @@ | |||
59 | /* Code for stacks | 59 | /* Code for stacks |
60 | * Author - Eric Young v 1.0 | 60 | * Author - Eric Young v 1.0 |
61 | * 1.2 eay 12-Mar-97 - Modified sk_find so that it _DOES_ return the | 61 | * 1.2 eay 12-Mar-97 - Modified sk_find so that it _DOES_ return the |
62 | * lowest index for the seached item. | 62 | * lowest index for the searched item. |
63 | * | 63 | * |
64 | * 1.1 eay - Take from netdb and added to SSLeay | 64 | * 1.1 eay - Take from netdb and added to SSLeay |
65 | * | 65 | * |
@@ -67,38 +67,34 @@ | |||
67 | */ | 67 | */ |
68 | #include <stdio.h> | 68 | #include <stdio.h> |
69 | #include "cryptlib.h" | 69 | #include "cryptlib.h" |
70 | #include "stack.h" | 70 | #include <openssl/stack.h> |
71 | 71 | ||
72 | #undef MIN_NODES | 72 | #undef MIN_NODES |
73 | #define MIN_NODES 4 | 73 | #define MIN_NODES 4 |
74 | 74 | ||
75 | char *STACK_version="STACK part of SSLeay 0.9.0b 29-Jun-1998"; | 75 | const char *STACK_version="Stack" OPENSSL_VERSION_PTEXT; |
76 | |||
77 | #ifndef NOPROTO | ||
78 | #define FP_ICC (int (*)(const void *,const void *)) | ||
79 | #else | ||
80 | #define FP_ICC | ||
81 | #endif | ||
82 | 76 | ||
83 | #include <errno.h> | 77 | #include <errno.h> |
84 | 78 | ||
85 | void sk_set_cmp_func(sk,c) | 79 | int (*sk_set_cmp_func(STACK *sk, int (*c)(const char * const *,const char * const *))) |
86 | STACK *sk; | 80 | (const char * const *, const char * const *) |
87 | int (*c)(); | ||
88 | { | 81 | { |
82 | int (*old)(const char * const *,const char * const *)=sk->comp; | ||
83 | |||
89 | if (sk->comp != c) | 84 | if (sk->comp != c) |
90 | sk->sorted=0; | 85 | sk->sorted=0; |
91 | sk->comp=c; | 86 | sk->comp=c; |
87 | |||
88 | return old; | ||
92 | } | 89 | } |
93 | 90 | ||
94 | STACK *sk_dup(sk) | 91 | STACK *sk_dup(STACK *sk) |
95 | STACK *sk; | ||
96 | { | 92 | { |
97 | STACK *ret; | 93 | STACK *ret; |
98 | char **s; | 94 | char **s; |
99 | 95 | ||
100 | if ((ret=sk_new(sk->comp)) == NULL) goto err; | 96 | if ((ret=sk_new(sk->comp)) == NULL) goto err; |
101 | s=(char **)Realloc((char *)ret->data, | 97 | s=(char **)OPENSSL_realloc((char *)ret->data, |
102 | (unsigned int)sizeof(char *)*sk->num_alloc); | 98 | (unsigned int)sizeof(char *)*sk->num_alloc); |
103 | if (s == NULL) goto err; | 99 | if (s == NULL) goto err; |
104 | ret->data=s; | 100 | ret->data=s; |
@@ -110,19 +106,25 @@ STACK *sk; | |||
110 | ret->comp=sk->comp; | 106 | ret->comp=sk->comp; |
111 | return(ret); | 107 | return(ret); |
112 | err: | 108 | err: |
109 | if(ret) | ||
110 | sk_free(ret); | ||
113 | return(NULL); | 111 | return(NULL); |
114 | } | 112 | } |
115 | 113 | ||
116 | STACK *sk_new(c) | 114 | STACK *sk_new_null(void) |
117 | int (*c)(); | 115 | { |
116 | return sk_new((int (*)(const char * const *, const char * const *))0); | ||
117 | } | ||
118 | |||
119 | STACK *sk_new(int (*c)(const char * const *, const char * const *)) | ||
118 | { | 120 | { |
119 | STACK *ret; | 121 | STACK *ret; |
120 | int i; | 122 | int i; |
121 | 123 | ||
122 | if ((ret=(STACK *)Malloc(sizeof(STACK))) == NULL) | 124 | if ((ret=(STACK *)OPENSSL_malloc(sizeof(STACK))) == NULL) |
123 | goto err0; | 125 | goto err; |
124 | if ((ret->data=(char **)Malloc(sizeof(char *)*MIN_NODES)) == NULL) | 126 | if ((ret->data=(char **)OPENSSL_malloc(sizeof(char *)*MIN_NODES)) == NULL) |
125 | goto err1; | 127 | goto err; |
126 | for (i=0; i<MIN_NODES; i++) | 128 | for (i=0; i<MIN_NODES; i++) |
127 | ret->data[i]=NULL; | 129 | ret->data[i]=NULL; |
128 | ret->comp=c; | 130 | ret->comp=c; |
@@ -130,22 +132,20 @@ int (*c)(); | |||
130 | ret->num=0; | 132 | ret->num=0; |
131 | ret->sorted=0; | 133 | ret->sorted=0; |
132 | return(ret); | 134 | return(ret); |
133 | err1: | 135 | err: |
134 | Free((char *)ret); | 136 | if(ret) |
135 | err0: | 137 | OPENSSL_free(ret); |
136 | return(NULL); | 138 | return(NULL); |
137 | } | 139 | } |
138 | 140 | ||
139 | int sk_insert(st,data,loc) | 141 | int sk_insert(STACK *st, char *data, int loc) |
140 | STACK *st; | ||
141 | char *data; | ||
142 | int loc; | ||
143 | { | 142 | { |
144 | char **s; | 143 | char **s; |
145 | 144 | ||
145 | if(st == NULL) return 0; | ||
146 | if (st->num_alloc <= st->num+1) | 146 | if (st->num_alloc <= st->num+1) |
147 | { | 147 | { |
148 | s=(char **)Realloc((char *)st->data, | 148 | s=(char **)OPENSSL_realloc((char *)st->data, |
149 | (unsigned int)sizeof(char *)*st->num_alloc*2); | 149 | (unsigned int)sizeof(char *)*st->num_alloc*2); |
150 | if (s == NULL) | 150 | if (s == NULL) |
151 | return(0); | 151 | return(0); |
@@ -161,7 +161,7 @@ int loc; | |||
161 | 161 | ||
162 | f=(char **)st->data; | 162 | f=(char **)st->data; |
163 | t=(char **)&(st->data[1]); | 163 | t=(char **)&(st->data[1]); |
164 | for (i=st->num; i>loc; i--) | 164 | for (i=st->num; i>=loc; i--) |
165 | t[i]=f[i]; | 165 | t[i]=f[i]; |
166 | 166 | ||
167 | #ifdef undef /* no memmove on sunos :-( */ | 167 | #ifdef undef /* no memmove on sunos :-( */ |
@@ -176,9 +176,7 @@ int loc; | |||
176 | return(st->num); | 176 | return(st->num); |
177 | } | 177 | } |
178 | 178 | ||
179 | char *sk_delete_ptr(st,p) | 179 | char *sk_delete_ptr(STACK *st, char *p) |
180 | STACK *st; | ||
181 | char *p; | ||
182 | { | 180 | { |
183 | int i; | 181 | int i; |
184 | 182 | ||
@@ -188,14 +186,13 @@ char *p; | |||
188 | return(NULL); | 186 | return(NULL); |
189 | } | 187 | } |
190 | 188 | ||
191 | char *sk_delete(st,loc) | 189 | char *sk_delete(STACK *st, int loc) |
192 | STACK *st; | ||
193 | int loc; | ||
194 | { | 190 | { |
195 | char *ret; | 191 | char *ret; |
196 | int i,j; | 192 | int i,j; |
197 | 193 | ||
198 | if ((st->num == 0) || (loc < 0) || (loc >= st->num)) return(NULL); | 194 | if ((st == NULL) || (st->num == 0) || (loc < 0) |
195 | || (loc >= st->num)) return(NULL); | ||
199 | 196 | ||
200 | ret=st->data[loc]; | 197 | ret=st->data[loc]; |
201 | if (loc != st->num-1) | 198 | if (loc != st->num-1) |
@@ -213,13 +210,12 @@ int loc; | |||
213 | return(ret); | 210 | return(ret); |
214 | } | 211 | } |
215 | 212 | ||
216 | int sk_find(st,data) | 213 | int sk_find(STACK *st, char *data) |
217 | STACK *st; | ||
218 | char *data; | ||
219 | { | 214 | { |
220 | char **r; | 215 | char **r; |
221 | int i; | 216 | int i; |
222 | int (*comp_func)(); | 217 | int (*comp_func)(const void *,const void *); |
218 | if(st == NULL) return -1; | ||
223 | 219 | ||
224 | if (st->comp == NULL) | 220 | if (st->comp == NULL) |
225 | { | 221 | { |
@@ -228,55 +224,55 @@ char *data; | |||
228 | return(i); | 224 | return(i); |
229 | return(-1); | 225 | return(-1); |
230 | } | 226 | } |
231 | comp_func=(int (*)())st->comp; | 227 | sk_sort(st); |
232 | if (!st->sorted) | ||
233 | { | ||
234 | qsort((char *)st->data,st->num,sizeof(char *),FP_ICC comp_func); | ||
235 | st->sorted=1; | ||
236 | } | ||
237 | if (data == NULL) return(-1); | 228 | if (data == NULL) return(-1); |
229 | /* This (and the "qsort" below) are the two places in OpenSSL | ||
230 | * where we need to convert from our standard (type **,type **) | ||
231 | * compare callback type to the (void *,void *) type required by | ||
232 | * bsearch. However, the "data" it is being called(back) with are | ||
233 | * not (type *) pointers, but the *pointers* to (type *) pointers, | ||
234 | * so we get our extra level of pointer dereferencing that way. */ | ||
235 | comp_func=(int (*)(const void *,const void *))(st->comp); | ||
238 | r=(char **)bsearch(&data,(char *)st->data, | 236 | r=(char **)bsearch(&data,(char *)st->data, |
239 | st->num,sizeof(char *),FP_ICC comp_func); | 237 | st->num,sizeof(char *), comp_func); |
240 | if (r == NULL) return(-1); | 238 | if (r == NULL) return(-1); |
241 | i=(int)(r-st->data); | 239 | i=(int)(r-st->data); |
242 | for ( ; i>0; i--) | 240 | for ( ; i>0; i--) |
243 | if ((*st->comp)(&(st->data[i-1]),&data) < 0) | 241 | /* This needs a cast because the type being pointed to from |
242 | * the "&" expressions are (char *) rather than (const char *). | ||
243 | * For an explanation, read: | ||
244 | * http://www.eskimo.com/~scs/C-faq/q11.10.html :-) */ | ||
245 | if ((*st->comp)((const char * const *)&(st->data[i-1]), | ||
246 | (const char * const *)&data) < 0) | ||
244 | break; | 247 | break; |
245 | return(i); | 248 | return(i); |
246 | } | 249 | } |
247 | 250 | ||
248 | int sk_push(st,data) | 251 | int sk_push(STACK *st, char *data) |
249 | STACK *st; | ||
250 | char *data; | ||
251 | { | 252 | { |
252 | return(sk_insert(st,data,st->num)); | 253 | return(sk_insert(st,data,st->num)); |
253 | } | 254 | } |
254 | 255 | ||
255 | int sk_unshift(st,data) | 256 | int sk_unshift(STACK *st, char *data) |
256 | STACK *st; | ||
257 | char *data; | ||
258 | { | 257 | { |
259 | return(sk_insert(st,data,0)); | 258 | return(sk_insert(st,data,0)); |
260 | } | 259 | } |
261 | 260 | ||
262 | char *sk_shift(st) | 261 | char *sk_shift(STACK *st) |
263 | STACK *st; | ||
264 | { | 262 | { |
265 | if (st == NULL) return(NULL); | 263 | if (st == NULL) return(NULL); |
266 | if (st->num <= 0) return(NULL); | 264 | if (st->num <= 0) return(NULL); |
267 | return(sk_delete(st,0)); | 265 | return(sk_delete(st,0)); |
268 | } | 266 | } |
269 | 267 | ||
270 | char *sk_pop(st) | 268 | char *sk_pop(STACK *st) |
271 | STACK *st; | ||
272 | { | 269 | { |
273 | if (st == NULL) return(NULL); | 270 | if (st == NULL) return(NULL); |
274 | if (st->num <= 0) return(NULL); | 271 | if (st->num <= 0) return(NULL); |
275 | return(sk_delete(st,st->num-1)); | 272 | return(sk_delete(st,st->num-1)); |
276 | } | 273 | } |
277 | 274 | ||
278 | void sk_zero(st) | 275 | void sk_zero(STACK *st) |
279 | STACK *st; | ||
280 | { | 276 | { |
281 | if (st == NULL) return; | 277 | if (st == NULL) return; |
282 | if (st->num <= 0) return; | 278 | if (st->num <= 0) return; |
@@ -284,9 +280,7 @@ STACK *st; | |||
284 | st->num=0; | 280 | st->num=0; |
285 | } | 281 | } |
286 | 282 | ||
287 | void sk_pop_free(st,func) | 283 | void sk_pop_free(STACK *st, void (*func)(void *)) |
288 | STACK *st; | ||
289 | void (*func)(); | ||
290 | { | 284 | { |
291 | int i; | 285 | int i; |
292 | 286 | ||
@@ -297,11 +291,44 @@ void (*func)(); | |||
297 | sk_free(st); | 291 | sk_free(st); |
298 | } | 292 | } |
299 | 293 | ||
300 | void sk_free(st) | 294 | void sk_free(STACK *st) |
301 | STACK *st; | ||
302 | { | 295 | { |
303 | if (st == NULL) return; | 296 | if (st == NULL) return; |
304 | if (st->data != NULL) Free((char *)st->data); | 297 | if (st->data != NULL) OPENSSL_free(st->data); |
305 | Free((char *)st); | 298 | OPENSSL_free(st); |
306 | } | 299 | } |
307 | 300 | ||
301 | int sk_num(const STACK *st) | ||
302 | { | ||
303 | if(st == NULL) return -1; | ||
304 | return st->num; | ||
305 | } | ||
306 | |||
307 | char *sk_value(const STACK *st, int i) | ||
308 | { | ||
309 | if(st == NULL) return NULL; | ||
310 | return st->data[i]; | ||
311 | } | ||
312 | |||
313 | char *sk_set(STACK *st, int i, char *value) | ||
314 | { | ||
315 | if(st == NULL) return NULL; | ||
316 | return (st->data[i] = value); | ||
317 | } | ||
318 | |||
319 | void sk_sort(STACK *st) | ||
320 | { | ||
321 | if (st && !st->sorted) | ||
322 | { | ||
323 | int (*comp_func)(const void *,const void *); | ||
324 | |||
325 | /* same comment as in sk_find ... previously st->comp was declared | ||
326 | * as a (void*,void*) callback type, but this made the population | ||
327 | * of the callback pointer illogical - our callbacks compare | ||
328 | * type** with type**, so we leave the casting until absolutely | ||
329 | * necessary (ie. "now"). */ | ||
330 | comp_func=(int (*)(const void *,const void *))(st->comp); | ||
331 | qsort(st->data,st->num,sizeof(char *), comp_func); | ||
332 | st->sorted=1; | ||
333 | } | ||
334 | } | ||