Blame src/include/k5-hashtab.h

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