Path: blob/main/crypto/openssl/include/internal/hashtable.h
34879 views
/*1* Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#ifndef OPENSSL_HASHTABLE_H10# define OPENSSL_HASHTABLE_H11# pragma once1213#include <stddef.h>14#include <stdint.h>15#include <openssl/e_os2.h>16#include <internal/rcu.h>17#include "crypto/context.h"1819typedef struct ht_internal_st HT;2021/*22* Represents a key to a hashtable23*/24typedef struct ht_key_header_st {25size_t keysize;26uint8_t *keybuf;27} HT_KEY;2829/*30* Represents a value in the hash table31*/32typedef struct ht_value_st {33void *value;34uintptr_t *type_id;35HT_KEY key;36} HT_VALUE;3738/*39* Represents a list of values filtered from a hash table40*/41typedef struct ht_value_list_st {42size_t list_len;43HT_VALUE **list;44} HT_VALUE_LIST;4546/*47* Hashtable configuration48*/49typedef struct ht_config_st {50OSSL_LIB_CTX *ctx;51void (*ht_free_fn)(HT_VALUE *obj);52uint64_t (*ht_hash_fn)(uint8_t *key, size_t keylen);53size_t init_neighborhoods;54uint32_t collision_check;55uint32_t lockless_reads;56} HT_CONFIG;5758/*59* Hashtable key rules60* Any struct can be used to formulate a hash table key, as long as the61* following rules62* 1) The first element of the struct defining the key must be an HT_KEY63* 2) All struct elements must have a compile time defined length64* 3) Pointers can be used, but the value of the pointer, rather than65* the contents of the address it points to will be used to compute66* the hash67* The key definition macros will assist with enforcing these rules68*/6970/*71* Starts the definition of a hash table key72*/73#define HT_START_KEY_DEFN(keyname) \74typedef struct keyname##_st { \75HT_KEY key_header; \76struct {7778/*79* Ends a hash table key definitions80*/81#define HT_END_KEY_DEFN(keyname) \82} keyfields; \83} keyname;8485/*86* Defines a field in a hash table key87*/88#define HT_DEF_KEY_FIELD(name, type) type name;8990/*91* convenience macro to define a static char92* array field in a hash table key93*/94#define HT_DEF_KEY_FIELD_CHAR_ARRAY(name, size) \95HT_DEF_KEY_FIELD(name[size], char)9697/*98* Defines a uint8_t (blob) field in a hash table key99*/100#define HT_DEF_KEY_FIELD_UINT8T_ARRAY(name, size) \101HT_DEF_KEY_FIELD(name[size], uint8_t)102103/*104* Initializes a key105*/106#define HT_INIT_KEY(key) do { \107memset((key), 0, sizeof(*(key))); \108(key)->key_header.keysize = (sizeof(*(key)) - sizeof(HT_KEY)); \109(key)->key_header.keybuf = (((uint8_t *)key) + sizeof(HT_KEY)); \110} while(0)111112/*113* Resets a hash table key to a known state114*/115#define HT_KEY_RESET(key) memset((key)->key_header.keybuf, 0, (key)->key_header.keysize)116117/*118* Sets a scalar field in a hash table key119*/120#define HT_SET_KEY_FIELD(key, member, value) (key)->keyfields.member = value;121122/*123* Sets a string field in a hash table key, preserving124* null terminator125*/126#define HT_SET_KEY_STRING(key, member, value) do { \127if ((value) != NULL) \128strncpy((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \129} while(0)130131/*132* This is the same as HT_SET_KEY_STRING, except that it uses133* ossl_ht_strcase to make the value being passed case insensitive134* This is useful for instances in which we want upper and lower case135* key value to hash to the same entry136*/137#define HT_SET_KEY_STRING_CASE(key, member, value) do { \138ossl_ht_strcase((key)->keyfields.member, value, sizeof((key)->keyfields.member) -1); \139} while(0)140141/*142* Same as HT_SET_KEY_STRING but also takes length of the string.143*/144#define HT_SET_KEY_STRING_N(key, member, value, len) do { \145if ((value) != NULL) { \146if (len < sizeof((key)->keyfields.member)) \147strncpy((key)->keyfields.member, value, len); \148else \149strncpy((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \150} \151} while(0)152153/* Same as HT_SET_KEY_STRING_CASE but also takes length of the string. */154#define HT_SET_KEY_STRING_CASE_N(key, member, value, len) do { \155if (len < sizeof((key)->keyfields.member)) \156ossl_ht_strcase((key)->keyfields.member, value, len); \157else \158ossl_ht_strcase((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \159} while(0)160161/*162* Sets a uint8_t (blob) field in a hash table key163*/164#define HT_SET_KEY_BLOB(key, member, value, len) do { \165if (value != NULL) \166memcpy((key)->keyfields.member, value, len); \167} while(0)168169/*170* Converts a defined key type to an HT_KEY171*/172#define TO_HT_KEY(key) &(key)->key_header173174/*175* Converts an HT_KEY back to its defined176* type177*/178#define FROM_HT_KEY(key, type) (type)(key)179180/*181* Implements the following type safe operations for a hash table182* ossl_ht_NAME_TYPE_insert - insert a value to a hash table of type TYPE183* ossl_ht_NAME_TYPE_get - gets a value of a specific type from the hash table184* ossl_ht_NAME_TYPE_from_value - converts an HT_VALUE to its type185* ossl_ht_NAME_TYPE_to_value - converts a TYPE to an HT_VALUE186* ossl_ht_NAME_TYPE_type - boolean to detect if a value is of TYPE187*/188#define IMPLEMENT_HT_VALUE_TYPE_FNS(vtype, name, pfx) \189static uintptr_t name##_##vtype##_id = 0; \190pfx ossl_unused int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key, \191vtype *data, \192vtype **olddata) { \193HT_VALUE inval; \194HT_VALUE *oval = NULL; \195int rc; \196\197inval.value = data; \198inval.type_id = &name##_##vtype##_id; \199rc = ossl_ht_insert(h, key, &inval, olddata == NULL ? NULL : &oval); \200if (oval != NULL) \201*olddata = (vtype *)oval->value; \202return rc; \203} \204\205pfx ossl_unused vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v) \206{ \207uintptr_t *expect_type = &name##_##vtype##_id; \208if (v == NULL) \209return NULL; \210if (v->type_id != expect_type) \211return NULL; \212return (vtype *)v->value; \213} \214\215pfx ossl_unused vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h, \216HT_KEY *key, \217HT_VALUE **v)\218{ \219HT_VALUE *vv; \220vv = ossl_ht_get(h, key); \221if (vv == NULL) \222return NULL; \223*v = ossl_rcu_deref(&vv); \224return ossl_ht_##name##_##vtype##_from_value(*v); \225} \226\227pfx ossl_unused HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, \228HT_VALUE *v) \229{ \230v->type_id = &name##_##vtype##_id; \231v->value = data; \232return v; \233} \234\235pfx ossl_unused int ossl_ht_##name##_##vtype##_type(HT_VALUE *h) \236{ \237return h->type_id == &name##_##vtype##_id; \238}239240#define DECLARE_HT_VALUE_TYPE_FNS(vtype, name) \241int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key, vtype *data, \242vtype **olddata); \243vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v); \244vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h, \245HT_KEY *key, \246HT_VALUE **v); \247HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, HT_VALUE *v); \248int ossl_ht_##name##_##vtype##_type(HT_VALUE *h); \249250/*251* Helper function to construct case insensitive keys252*/253static void ossl_unused ossl_ht_strcase(char *tgt, const char *src, int len)254{255int i;256#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)257const long int case_adjust = ~0x40;258#else259const long int case_adjust = ~0x20;260#endif261262if (src == NULL)263return;264265for (i = 0; src[i] != '\0' && i < len; i++)266tgt[i] = case_adjust & src[i];267}268269/*270* Create a new hashtable271*/272HT *ossl_ht_new(const HT_CONFIG *conf);273274/*275* Frees a hash table, potentially freeing all elements276*/277void ossl_ht_free(HT *htable);278279/*280* Lock the table for reading281*/282void ossl_ht_read_lock(HT *htable);283284/*285* Lock the table for writing286*/287void ossl_ht_write_lock(HT *htable);288289/*290* Read unlock291*/292void ossl_ht_read_unlock(HT *htable);293294/*295* Write unlock296*/297void ossl_ht_write_unlock (HT *htable);298299/*300* Empties a hash table, potentially freeing all elements301*/302int ossl_ht_flush(HT *htable);303304/*305* Inserts an element to a hash table, optionally returning306* replaced data to caller307* Returns 1 if the insert was successful, 0 on error308*/309int ossl_ht_insert(HT *htable, HT_KEY *key, HT_VALUE *data,310HT_VALUE **olddata);311312/*313* Deletes a value from a hash table, based on key314* Returns 1 if the key was removed, 0 if they key was not found315*/316int ossl_ht_delete(HT *htable, HT_KEY *key);317318/*319* Returns number of elements in the hash table320*/321size_t ossl_ht_count(HT *htable);322323/*324* Iterates over each element in the table.325* aborts the loop when cb returns 0326* Contents of elements in the list may be modified during327* this traversal, assuming proper thread safety is observed while doing328* so (holding the table write lock is sufficient). However, elements of the329* table may not be inserted or removed while iterating.330*/331void ossl_ht_foreach_until(HT *htable, int (*cb)(HT_VALUE *obj, void *arg),332void *arg);333/*334* Returns a list of elements in a hash table based on335* filter function return value. Returns NULL on error,336* or an HT_VALUE_LIST object on success. Note it is possible337* That a list will be returned with 0 entries, if none were found.338* The zero length list must still be freed via ossl_ht_value_list_free339*/340HT_VALUE_LIST *ossl_ht_filter(HT *htable, size_t max_len,341int (*filter)(HT_VALUE *obj, void *arg),342void *arg);343/*344* Frees the list returned from ossl_ht_filter345*/346void ossl_ht_value_list_free(HT_VALUE_LIST *list);347348/*349* Fetches a value from the hash table, based350* on key. Returns NULL if the element was not found.351*/352HT_VALUE *ossl_ht_get(HT *htable, HT_KEY *key);353354#endif355356357