Blame src/include/k5-hashtab.h

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* include/k5-hash.h - hash table interface definitions */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2018 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Redistribution and use in source and binary forms, with or without
Packit fd8b60
 * modification, are permitted provided that the following conditions
Packit fd8b60
 * are met:
Packit fd8b60
 *
Packit fd8b60
 * * Redistributions of source code must retain the above copyright
Packit fd8b60
 *   notice, this list of conditions and the following disclaimer.
Packit fd8b60
 *
Packit fd8b60
 * * Redistributions in binary form must reproduce the above copyright
Packit fd8b60
 *   notice, this list of conditions and the following disclaimer in
Packit fd8b60
 *   the documentation and/or other materials provided with the
Packit fd8b60
 *   distribution.
Packit fd8b60
 *
Packit fd8b60
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit fd8b60
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit fd8b60
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit fd8b60
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit fd8b60
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit fd8b60
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Packit fd8b60
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit fd8b60
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit fd8b60
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Packit fd8b60
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit fd8b60
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit fd8b60
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * This file contains declarations for a simple hash table using siphash.  Some
Packit fd8b60
 * limitations which might need to be addressed in the future:
Packit fd8b60
 *
Packit fd8b60
 * - The table does not manage caller memory.  This limitation could be
Packit fd8b60
 *   addressed by adding an optional free callback to k5_hashtab_create(), to
Packit fd8b60
 *   be called by k5_hashtab_free() and k5_hashtab_remove().
Packit fd8b60
 *
Packit fd8b60
 * - There is no way to iterate over a hash table.
Packit fd8b60
 *
Packit fd8b60
 * - k5_hashtab_add() does not check for duplicate entries.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#ifndef K5_HASH_H
Packit fd8b60
#define K5_HASH_H
Packit fd8b60
Packit fd8b60
#define K5_HASH_SEED_LEN 16
Packit fd8b60
Packit fd8b60
struct k5_hashtab;
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Create a new hash table in *ht_out.  seed must point to random bytes if keys
Packit fd8b60
 * might be under the control of an attacker; otherwise it may be NULL.
Packit fd8b60
 * initial_buckets controls the initial allocation of hash buckets; pass zero
Packit fd8b60
 * to use a default value.  The number of hash buckets will be doubled as the
Packit fd8b60
 * number of entries increases.  Return 0 on success, ENOMEM on failure.
Packit fd8b60
 */
Packit fd8b60
int k5_hashtab_create(const uint8_t seed[K5_HASH_SEED_LEN],
Packit fd8b60
                      size_t initial_buckets, struct k5_hashtab **ht_out);
Packit fd8b60
Packit fd8b60
/* Release the memory used by a hash table.  Keys and values are the caller's
Packit fd8b60
 * responsibility. */
Packit fd8b60
void k5_hashtab_free(struct k5_hashtab *ht);
Packit fd8b60
Packit fd8b60
/* Add an entry to a hash table.  key and val must remain valid until the entry
Packit fd8b60
 * is removed or the hash table is freed.  The caller must avoid duplicates. */
Packit fd8b60
int k5_hashtab_add(struct k5_hashtab *ht, const void *key, size_t klen,
Packit fd8b60
                   void *val);
Packit fd8b60
Packit fd8b60
/* Remove an entry from a hash table by key.  Does not free key or the
Packit fd8b60
 * associated value.  Return 1 if the key was found and removed, 0 if not. */
Packit fd8b60
int k5_hashtab_remove(struct k5_hashtab *ht, const void *key, size_t klen);
Packit fd8b60
Packit fd8b60
/* Retrieve a value from a hash table by key. */
Packit fd8b60
void *k5_hashtab_get(struct k5_hashtab *ht, const void *key, size_t klen);
Packit fd8b60
Packit fd8b60
uint64_t k5_siphash24(const uint8_t *data, size_t len,
Packit fd8b60
                      const uint8_t seed[K5_HASH_SEED_LEN]);
Packit fd8b60
Packit fd8b60
#endif /* K5_HASH_H */