summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/store/str_mem.c
diff options
context:
space:
mode:
authorjsing <>2014-05-26 11:24:48 +0000
committerjsing <>2014-05-26 11:24:48 +0000
commit1918fecccdef550d51f6267a095575731f873b2b (patch)
treef3df1497c7d3c4533bdb1a09664b32b2c989b047 /src/lib/libcrypto/store/str_mem.c
parent8ba2fea129dbe500a291768b0aec33a3fd015669 (diff)
downloadopenbsd-1918fecccdef550d51f6267a095575731f873b2b.tar.gz
openbsd-1918fecccdef550d51f6267a095575731f873b2b.tar.bz2
openbsd-1918fecccdef550d51f6267a095575731f873b2b.zip
KNF.
Diffstat (limited to 'src/lib/libcrypto/store/str_mem.c')
-rw-r--r--src/lib/libcrypto/store/str_mem.c273
1 files changed, 139 insertions, 134 deletions
diff --git a/src/lib/libcrypto/store/str_mem.c b/src/lib/libcrypto/store/str_mem.c
index 7e2346d93d..0409fd198d 100644
--- a/src/lib/libcrypto/store/str_mem.c
+++ b/src/lib/libcrypto/store/str_mem.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -76,26 +76,23 @@
76 attribute type code). 76 attribute type code).
77*/ 77*/
78 78
79typedef struct mem_object_data_st 79typedef struct mem_object_data_st {
80 {
81 STORE_OBJECT *object; 80 STORE_OBJECT *object;
82 STORE_ATTR_INFO *attr_info; 81 STORE_ATTR_INFO *attr_info;
83 int references; 82 int references;
84 } MEM_OBJECT_DATA; 83} MEM_OBJECT_DATA;
85 84
86DECLARE_STACK_OF(MEM_OBJECT_DATA) 85DECLARE_STACK_OF(MEM_OBJECT_DATA)
87struct mem_data_st 86struct mem_data_st {
88 {
89 STACK_OF(MEM_OBJECT_DATA) *data; /* sorted with 87 STACK_OF(MEM_OBJECT_DATA) *data; /* sorted with
90 * STORE_ATTR_INFO_compare(). */ 88 * STORE_ATTR_INFO_compare(). */
91 unsigned int compute_components : 1; /* Currently unused, but can 89 unsigned int compute_components : 1; /* Currently unused, but can
92 be used to add attributes 90 be used to add attributes
93 from parts of the data. */ 91 from parts of the data. */
94 }; 92};
95 93
96DECLARE_STACK_OF(STORE_ATTR_INFO) 94DECLARE_STACK_OF(STORE_ATTR_INFO)
97struct mem_ctx_st 95struct mem_ctx_st {
98 {
99 int type; /* The type we're searching for */ 96 int type; /* The type we're searching for */
100 STACK_OF(STORE_ATTR_INFO) *search_attributes; /* Sets of 97 STACK_OF(STORE_ATTR_INFO) *search_attributes; /* Sets of
101 attributes to search for. Each 98 attributes to search for. Each
@@ -105,32 +102,31 @@ struct mem_ctx_st
105 haven't found any */ 102 haven't found any */
106 int index; /* -1 as long as we're searching for 103 int index; /* -1 as long as we're searching for
107 the first */ 104 the first */
108 }; 105};
109 106
110static int mem_init(STORE *s); 107static int mem_init(STORE *s);
111static void mem_clean(STORE *s); 108static void mem_clean(STORE *s);
112static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type, 109static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type,
113 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]); 110 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
114static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type, 111static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type,
115 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]); 112 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
116static int mem_store(STORE *s, STORE_OBJECT_TYPES type, 113static int mem_store(STORE *s, STORE_OBJECT_TYPES type, STORE_OBJECT *data,
117 STORE_OBJECT *data, OPENSSL_ITEM attributes[], 114 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
118 OPENSSL_ITEM parameters[]);
119static int mem_modify(STORE *s, STORE_OBJECT_TYPES type, 115static int mem_modify(STORE *s, STORE_OBJECT_TYPES type,
120 OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[], 116 OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[],
121 OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[], 117 OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[],
122 OPENSSL_ITEM parameters[]); 118 OPENSSL_ITEM parameters[]);
123static int mem_delete(STORE *s, STORE_OBJECT_TYPES type, 119static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
124 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]); 120 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
125static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type, 121static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type,
126 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]); 122 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]);
127static STORE_OBJECT *mem_list_next(STORE *s, void *handle); 123static STORE_OBJECT *mem_list_next(STORE *s, void *handle);
128static int mem_list_end(STORE *s, void *handle); 124static int mem_list_end(STORE *s, void *handle);
129static int mem_list_endp(STORE *s, void *handle); 125static int mem_list_endp(STORE *s, void *handle);
130static int mem_lock(STORE *s, OPENSSL_ITEM attributes[], 126static int mem_lock(STORE *s, OPENSSL_ITEM attributes[],
131 OPENSSL_ITEM parameters[]); 127 OPENSSL_ITEM parameters[]);
132static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[], 128static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[],
133 OPENSSL_ITEM parameters[]); 129 OPENSSL_ITEM parameters[]);
134static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void)); 130static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void));
135 131
136static STORE_METHOD store_memory = { 132static STORE_METHOD store_memory = {
@@ -149,64 +145,73 @@ static STORE_METHOD store_memory = {
149 .lock_store = mem_lock, 145 .lock_store = mem_lock,
150 .unlock_store = mem_unlock, 146 .unlock_store = mem_unlock,
151 .ctrl = mem_ctrl 147 .ctrl = mem_ctrl
152 }; 148};
153 149
154const STORE_METHOD *STORE_Memory(void) 150const STORE_METHOD *
155 { 151STORE_Memory(void)
152{
156 return &store_memory; 153 return &store_memory;
157 } 154}
158 155
159static int mem_init(STORE *s) 156static int
160 { 157mem_init(STORE *s)
158{
161 return 1; 159 return 1;
162 } 160}
163 161
164static void mem_clean(STORE *s) 162static void
165 { 163mem_clean(STORE *s)
164{
166 return; 165 return;
167 } 166}
168 167
169static STORE_OBJECT *mem_generate(STORE *s, STORE_OBJECT_TYPES type, 168static STORE_OBJECT *
170 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]) 169mem_generate(STORE *s, STORE_OBJECT_TYPES type, OPENSSL_ITEM attributes[],
171 { 170 OPENSSL_ITEM parameters[])
171{
172 STOREerr(STORE_F_MEM_GENERATE, STORE_R_NOT_IMPLEMENTED); 172 STOREerr(STORE_F_MEM_GENERATE, STORE_R_NOT_IMPLEMENTED);
173 return 0; 173 return 0;
174 } 174}
175static STORE_OBJECT *mem_get(STORE *s, STORE_OBJECT_TYPES type, 175
176 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]) 176static STORE_OBJECT *
177 { 177mem_get(STORE *s, STORE_OBJECT_TYPES type, OPENSSL_ITEM attributes[],
178 OPENSSL_ITEM parameters[])
179{
178 void *context = mem_list_start(s, type, attributes, parameters); 180 void *context = mem_list_start(s, type, attributes, parameters);
179 181
180 if (context) 182 if (context) {
181 {
182 STORE_OBJECT *object = mem_list_next(s, context); 183 STORE_OBJECT *object = mem_list_next(s, context);
183 184
184 if (mem_list_end(s, context)) 185 if (mem_list_end(s, context))
185 return object; 186 return object;
186 }
187 return NULL;
188 } 187 }
189static int mem_store(STORE *s, STORE_OBJECT_TYPES type, 188 return NULL;
190 STORE_OBJECT *data, OPENSSL_ITEM attributes[], 189}
191 OPENSSL_ITEM parameters[]) 190
192 { 191static int
192mem_store(STORE *s, STORE_OBJECT_TYPES type, STORE_OBJECT *data,
193 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
194{
193 STOREerr(STORE_F_MEM_STORE, STORE_R_NOT_IMPLEMENTED); 195 STOREerr(STORE_F_MEM_STORE, STORE_R_NOT_IMPLEMENTED);
194 return 0; 196 return 0;
195 } 197}
196static int mem_modify(STORE *s, STORE_OBJECT_TYPES type, 198
197 OPENSSL_ITEM search_attributes[], OPENSSL_ITEM add_attributes[], 199static int
198 OPENSSL_ITEM modify_attributes[], OPENSSL_ITEM delete_attributes[], 200mem_modify(STORE *s, STORE_OBJECT_TYPES type, OPENSSL_ITEM search_attributes[],
199 OPENSSL_ITEM parameters[]) 201 OPENSSL_ITEM add_attributes[], OPENSSL_ITEM modify_attributes[],
200 { 202 OPENSSL_ITEM delete_attributes[], OPENSSL_ITEM parameters[])
203{
201 STOREerr(STORE_F_MEM_MODIFY, STORE_R_NOT_IMPLEMENTED); 204 STOREerr(STORE_F_MEM_MODIFY, STORE_R_NOT_IMPLEMENTED);
202 return 0; 205 return 0;
203 } 206}
204static int mem_delete(STORE *s, STORE_OBJECT_TYPES type, 207
205 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]) 208static int
206 { 209mem_delete(STORE *s, STORE_OBJECT_TYPES type, OPENSSL_ITEM attributes[],
210 OPENSSL_ITEM parameters[])
211{
207 STOREerr(STORE_F_MEM_DELETE, STORE_R_NOT_IMPLEMENTED); 212 STOREerr(STORE_F_MEM_DELETE, STORE_R_NOT_IMPLEMENTED);
208 return 0; 213 return 0;
209 } 214}
210 215
211/* The list functions may be the hardest to understand. Basically, 216/* The list functions may be the hardest to understand. Basically,
212 mem_list_start compiles a stack of attribute info elements, and 217 mem_list_start compiles a stack of attribute info elements, and
@@ -215,105 +220,99 @@ static int mem_delete(STORE *s, STORE_OBJECT_TYPES type,
215 walk all the way to the end of the store (since any combination 220 walk all the way to the end of the store (since any combination
216 of attribute bits above the starting point may match the searched 221 of attribute bits above the starting point may match the searched
217 for bit pattern...). */ 222 for bit pattern...). */
218static void *mem_list_start(STORE *s, STORE_OBJECT_TYPES type, 223static void *
219 OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[]) 224mem_list_start(STORE *s, STORE_OBJECT_TYPES type, OPENSSL_ITEM attributes[],
220 { 225 OPENSSL_ITEM parameters[])
226{
221 struct mem_ctx_st *context = 227 struct mem_ctx_st *context =
222 (struct mem_ctx_st *)malloc(sizeof(struct mem_ctx_st)); 228 (struct mem_ctx_st *)malloc(sizeof(struct mem_ctx_st));
223 void *attribute_context = NULL; 229 void *attribute_context = NULL;
224 STORE_ATTR_INFO *attrs = NULL; 230 STORE_ATTR_INFO *attrs = NULL;
225 231
226 if (!context) 232 if (!context) {
227 {
228 STOREerr(STORE_F_MEM_LIST_START, ERR_R_MALLOC_FAILURE); 233 STOREerr(STORE_F_MEM_LIST_START, ERR_R_MALLOC_FAILURE);
229 return 0; 234 return 0;
230 } 235 }
231 memset(context, 0, sizeof(struct mem_ctx_st)); 236 memset(context, 0, sizeof(struct mem_ctx_st));
232 237
233 attribute_context = STORE_parse_attrs_start(attributes); 238 attribute_context = STORE_parse_attrs_start(attributes);
234 if (!attribute_context) 239 if (!attribute_context) {
235 {
236 STOREerr(STORE_F_MEM_LIST_START, ERR_R_STORE_LIB); 240 STOREerr(STORE_F_MEM_LIST_START, ERR_R_STORE_LIB);
237 goto err; 241 goto err;
238 } 242 }
239 243
240 while((attrs = STORE_parse_attrs_next(attribute_context))) 244 while ((attrs = STORE_parse_attrs_next(attribute_context))) {
241 { 245 if (context->search_attributes == NULL) {
242 if (context->search_attributes == NULL)
243 {
244 context->search_attributes = 246 context->search_attributes =
245 sk_STORE_ATTR_INFO_new(STORE_ATTR_INFO_compare); 247 sk_STORE_ATTR_INFO_new(STORE_ATTR_INFO_compare);
246 if (!context->search_attributes) 248 if (!context->search_attributes) {
247 {
248 STOREerr(STORE_F_MEM_LIST_START, 249 STOREerr(STORE_F_MEM_LIST_START,
249 ERR_R_MALLOC_FAILURE); 250 ERR_R_MALLOC_FAILURE);
250 goto err; 251 goto err;
251 }
252 } 252 }
253 sk_STORE_ATTR_INFO_push(context->search_attributes,attrs);
254 } 253 }
254 sk_STORE_ATTR_INFO_push(context->search_attributes, attrs);
255 }
255 if (!STORE_parse_attrs_endp(attribute_context)) 256 if (!STORE_parse_attrs_endp(attribute_context))
256 goto err; 257 goto err;
257 STORE_parse_attrs_end(attribute_context); 258 STORE_parse_attrs_end(attribute_context);
258 context->search_index = -1; 259 context->search_index = -1;
259 context->index = -1; 260 context->index = -1;
260 return context; 261 return context;
261 err: 262
262 if (attribute_context) STORE_parse_attrs_end(attribute_context); 263err:
264 if (attribute_context)
265 STORE_parse_attrs_end(attribute_context);
263 mem_list_end(s, context); 266 mem_list_end(s, context);
264 return NULL; 267 return NULL;
265 } 268}
266static STORE_OBJECT *mem_list_next(STORE *s, void *handle) 269
267 { 270static STORE_OBJECT *
271mem_list_next(STORE *s, void *handle)
272{
268 int i; 273 int i;
269 struct mem_ctx_st *context = (struct mem_ctx_st *)handle; 274 struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
270 struct mem_object_data_st key = { 0, 0, 1 }; 275 struct mem_object_data_st key = { 0, 0, 1 };
271 struct mem_data_st *store = 276 struct mem_data_st *store =
272 (struct mem_data_st *)STORE_get_ex_data(s, 1); 277 (struct mem_data_st *)STORE_get_ex_data(s, 1);
273 int srch; 278 int srch;
274 int cres = 0; 279 int cres = 0;
275 280
276 if (!context) 281 if (!context) {
277 {
278 STOREerr(STORE_F_MEM_LIST_NEXT, ERR_R_PASSED_NULL_PARAMETER); 282 STOREerr(STORE_F_MEM_LIST_NEXT, ERR_R_PASSED_NULL_PARAMETER);
279 return NULL; 283 return NULL;
280 } 284 }
281 if (!store) 285 if (!store) {
282 {
283 STOREerr(STORE_F_MEM_LIST_NEXT, STORE_R_NO_STORE); 286 STOREerr(STORE_F_MEM_LIST_NEXT, STORE_R_NO_STORE);
284 return NULL; 287 return NULL;
285 } 288 }
286 289
287 if (context->search_index == -1) 290 if (context->search_index == -1) {
288 {
289 for (i = 0; 291 for (i = 0;
290 i < sk_STORE_ATTR_INFO_num(context->search_attributes); 292 i < sk_STORE_ATTR_INFO_num(context->search_attributes);
291 i++) 293 i++) {
292 {
293 key.attr_info 294 key.attr_info
294 = sk_STORE_ATTR_INFO_value(context->search_attributes, 295 = sk_STORE_ATTR_INFO_value(context->search_attributes,
295 i); 296 i);
296 srch = sk_MEM_OBJECT_DATA_find_ex(store->data, &key); 297 srch = sk_MEM_OBJECT_DATA_find_ex(store->data, &key);
297 298
298 if (srch >= 0) 299 if (srch >= 0) {
299 {
300 context->search_index = srch; 300 context->search_index = srch;
301 break; 301 break;
302 }
303 } 302 }
304 } 303 }
304 }
305 if (context->search_index < 0) 305 if (context->search_index < 0)
306 return NULL; 306 return NULL;
307 307
308 key.attr_info = 308 key.attr_info = sk_STORE_ATTR_INFO_value(context->search_attributes,
309 sk_STORE_ATTR_INFO_value(context->search_attributes, 309 context->search_index);
310 context->search_index);
311 for(srch = context->search_index; 310 for(srch = context->search_index;
312 srch < sk_MEM_OBJECT_DATA_num(store->data) 311 srch < sk_MEM_OBJECT_DATA_num(store->data) &&
313 && STORE_ATTR_INFO_in_range(key.attr_info, 312 STORE_ATTR_INFO_in_range(key.attr_info,
314 sk_MEM_OBJECT_DATA_value(store->data, srch)->attr_info) 313 sk_MEM_OBJECT_DATA_value(store->data, srch)->attr_info) &&
315 && !(cres = STORE_ATTR_INFO_in_ex(key.attr_info, 314 !(cres = STORE_ATTR_INFO_in_ex(key.attr_info,
316 sk_MEM_OBJECT_DATA_value(store->data, srch)->attr_info)); 315 sk_MEM_OBJECT_DATA_value(store->data, srch)->attr_info));
317 srch++) 316 srch++)
318 ; 317 ;
319 318
@@ -321,42 +320,48 @@ static STORE_OBJECT *mem_list_next(STORE *s, void *handle)
321 if (cres) 320 if (cres)
322 return (sk_MEM_OBJECT_DATA_value(store->data, srch))->object; 321 return (sk_MEM_OBJECT_DATA_value(store->data, srch))->object;
323 return NULL; 322 return NULL;
324 } 323}
325static int mem_list_end(STORE *s, void *handle) 324
326 { 325static int
326mem_list_end(STORE *s, void *handle)
327{
327 struct mem_ctx_st *context = (struct mem_ctx_st *)handle; 328 struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
328 329
329 if (!context) 330 if (!context) {
330 {
331 STOREerr(STORE_F_MEM_LIST_END, ERR_R_PASSED_NULL_PARAMETER); 331 STOREerr(STORE_F_MEM_LIST_END, ERR_R_PASSED_NULL_PARAMETER);
332 return 0; 332 return 0;
333 } 333 }
334 if (context && context->search_attributes) 334 if (context && context->search_attributes)
335 sk_STORE_ATTR_INFO_free(context->search_attributes); 335 sk_STORE_ATTR_INFO_free(context->search_attributes);
336 free(context); 336 free(context);
337 return 1; 337 return 1;
338 } 338}
339static int mem_list_endp(STORE *s, void *handle) 339
340 { 340static int
341mem_list_endp(STORE *s, void *handle)
342{
341 struct mem_ctx_st *context = (struct mem_ctx_st *)handle; 343 struct mem_ctx_st *context = (struct mem_ctx_st *)handle;
342 344
343 if (!context 345 if (!context || context->search_index ==
344 || context->search_index 346 sk_STORE_ATTR_INFO_num(context->search_attributes))
345 == sk_STORE_ATTR_INFO_num(context->search_attributes))
346 return 1; 347 return 1;
347 return 0; 348 return 0;
348 } 349}
349static int mem_lock(STORE *s, OPENSSL_ITEM attributes[], 350
350 OPENSSL_ITEM parameters[]) 351static int
351 { 352mem_lock(STORE *s, OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
353{
352 return 1; 354 return 1;
353 } 355}
354static int mem_unlock(STORE *s, OPENSSL_ITEM attributes[], 356
355 OPENSSL_ITEM parameters[]) 357static int
356 { 358mem_unlock(STORE *s, OPENSSL_ITEM attributes[], OPENSSL_ITEM parameters[])
359{
357 return 1; 360 return 1;
358 } 361}
359static int mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void)) 362
360 { 363static int
364mem_ctrl(STORE *s, int cmd, long l, void *p, void (*f)(void))
365{
361 return 1; 366 return 1;
362 } 367}