2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include <openssl/crypto.h>
13 #include <openssl/lhash.h>
14 #include "internal/lhash.h"
15 #include "property_lcl.h"
18 * Property strings are a consolidation of all strings seen by the property
19 * subsystem. There are two name spaces to keep property names separate from
20 * property values (numeric values are not expected to be cached however).
21 * They allow a rapid conversion from a string to a unique index and any
22 * subsequent string comparison can be done via an integer compare.
24 * This implementation uses OpenSSL's standard hash table. There are more
25 * space and time efficient algorithms if this becomes a bottleneck.
30 OSSL_PROPERTY_IDX idx;
34 DEFINE_LHASH_OF(PROPERTY_STRING);
35 typedef LHASH_OF(PROPERTY_STRING) PROP_TABLE;
37 static PROP_TABLE *prop_names;
38 static PROP_TABLE *prop_values;
39 static OSSL_PROPERTY_IDX prop_name_idx = 0;
40 static OSSL_PROPERTY_IDX prop_value_idx = 0;
42 static unsigned long property_hash(const PROPERTY_STRING *a)
44 return OPENSSL_LH_strhash(a->s);
47 static int property_cmp(const PROPERTY_STRING *a, const PROPERTY_STRING *b)
49 return strcmp(a->s, b->s);
52 static void property_free(PROPERTY_STRING *ps)
57 static void property_table_free(PROP_TABLE **pt)
62 lh_PROPERTY_STRING_doall(t, &property_free);
63 lh_PROPERTY_STRING_free(t);
68 static PROPERTY_STRING *new_property_string(const char *s,
69 OSSL_PROPERTY_IDX *pidx)
71 const size_t l = strlen(s);
72 PROPERTY_STRING *ps = OPENSSL_malloc(sizeof(*ps) + l);
75 memcpy(ps->body, s, l + 1);
86 static OSSL_PROPERTY_IDX ossl_property_string(PROP_TABLE *t,
87 OSSL_PROPERTY_IDX *pidx,
90 PROPERTY_STRING p, *ps, *ps_new;
93 ps = lh_PROPERTY_STRING_retrieve(t, &p);
94 if (ps == NULL && pidx != NULL)
95 if ((ps_new = new_property_string(s, pidx)) != NULL) {
96 lh_PROPERTY_STRING_insert(t, ps_new);
97 if (lh_PROPERTY_STRING_error(t)) {
98 property_free(ps_new);
103 return ps != NULL ? ps->idx : 0;
106 OSSL_PROPERTY_IDX ossl_property_name(const char *s, int create)
108 return ossl_property_string(prop_names, create ? &prop_name_idx : NULL, s);
111 OSSL_PROPERTY_IDX ossl_property_value(const char *s, int create)
113 return ossl_property_string(prop_values, create ? &prop_value_idx : NULL, s);
116 int ossl_property_string_init(void)
118 prop_names = lh_PROPERTY_STRING_new(&property_hash, &property_cmp);
119 if (prop_names == NULL)
122 prop_values = lh_PROPERTY_STRING_new(&property_hash, &property_cmp);
123 if (prop_values == NULL)
128 ossl_property_string_cleanup();
132 void ossl_property_string_cleanup(void)
134 property_table_free(&prop_names);
135 property_table_free(&prop_values);
136 prop_name_idx = prop_value_idx = 0;