Blame shared/systemd/src/basic/hash-funcs.c

Packit Service b23acc
/* SPDX-License-Identifier: LGPL-2.1+ */
Packit Service b23acc
Packit Service b23acc
#include "nm-sd-adapt-shared.h"
Packit Service b23acc
Packit Service b23acc
#include <string.h>
Packit Service b23acc
Packit Service b23acc
#include "hash-funcs.h"
Packit Service b23acc
#include "path-util.h"
Packit Service b23acc
Packit Service b23acc
void string_hash_func(const char *p, struct siphash *state) {
Packit Service b23acc
        siphash24_compress(p, strlen(p) + 1, state);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
DEFINE_HASH_OPS(string_hash_ops, char, string_hash_func, string_compare_func);
Packit Service b23acc
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(string_hash_ops_free,
Packit Service b23acc
                                    char, string_hash_func, string_compare_func, free);
Packit Service b23acc
DEFINE_HASH_OPS_FULL(string_hash_ops_free_free,
Packit Service b23acc
                     char, string_hash_func, string_compare_func, free,
Packit Service b23acc
                     char, free);
Packit Service b23acc
Packit Service b23acc
#if 0 /* NM_IGNORED */
Packit Service b23acc
void path_hash_func(const char *q, struct siphash *state) {
Packit Service b23acc
        size_t n;
Packit Service b23acc
Packit Service b23acc
        assert(q);
Packit Service b23acc
        assert(state);
Packit Service b23acc
Packit Service b23acc
        /* Calculates a hash for a path in a way this duplicate inner slashes don't make a differences, and also
Packit Service b23acc
         * whether there's a trailing slash or not. This fits well with the semantics of path_compare(), which does
Packit Service b23acc
         * similar checks and also doesn't care for trailing slashes. Note that relative and absolute paths (i.e. those
Packit Service b23acc
         * which begin in a slash or not) will hash differently though. */
Packit Service b23acc
Packit Service b23acc
        n = strspn(q, "/");
Packit Service b23acc
        if (n > 0) { /* Eat up initial slashes, and add one "/" to the hash for all of them */
Packit Service b23acc
                siphash24_compress(q, 1, state);
Packit Service b23acc
                q += n;
Packit Service b23acc
        }
Packit Service b23acc
Packit Service b23acc
        for (;;) {
Packit Service b23acc
                /* Determine length of next component */
Packit Service b23acc
                n = strcspn(q, "/");
Packit Service b23acc
                if (n == 0) /* Reached the end? */
Packit Service b23acc
                        break;
Packit Service b23acc
Packit Service b23acc
                /* Add this component to the hash and skip over it */
Packit Service b23acc
                siphash24_compress(q, n, state);
Packit Service b23acc
                q += n;
Packit Service b23acc
Packit Service b23acc
                /* How many slashes follow this component? */
Packit Service b23acc
                n = strspn(q, "/");
Packit Service b23acc
                if (q[n] == 0) /* Is this a trailing slash? If so, we are at the end, and don't care about the slashes anymore */
Packit Service b23acc
                        break;
Packit Service b23acc
Packit Service b23acc
                /* We are not add the end yet. Hash exactly one slash for all of the ones we just encountered. */
Packit Service b23acc
                siphash24_compress(q, 1, state);
Packit Service b23acc
                q += n;
Packit Service b23acc
        }
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
DEFINE_HASH_OPS(path_hash_ops, char, path_hash_func, path_compare);
Packit Service b23acc
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(path_hash_ops_free,
Packit Service b23acc
                                    char, path_hash_func, path_compare, free);
Packit Service b23acc
#endif /* NM_IGNORED */
Packit Service b23acc
Packit Service b23acc
void trivial_hash_func(const void *p, struct siphash *state) {
Packit Service b23acc
        siphash24_compress(&p, sizeof(p), state);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
int trivial_compare_func(const void *a, const void *b) {
Packit Service b23acc
        return CMP(a, b);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
const struct hash_ops trivial_hash_ops = {
Packit Service b23acc
        .hash = trivial_hash_func,
Packit Service b23acc
        .compare = trivial_compare_func,
Packit Service b23acc
};
Packit Service b23acc
Packit Service b23acc
void uint64_hash_func(const uint64_t *p, struct siphash *state) {
Packit Service b23acc
        siphash24_compress(p, sizeof(uint64_t), state);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
int uint64_compare_func(const uint64_t *a, const uint64_t *b) {
Packit Service b23acc
        return CMP(*a, *b);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
DEFINE_HASH_OPS(uint64_hash_ops, uint64_t, uint64_hash_func, uint64_compare_func);
Packit Service b23acc
Packit Service b23acc
#if 0 /* NM_IGNORED */
Packit Service b23acc
#if SIZEOF_DEV_T != 8
Packit Service b23acc
void devt_hash_func(const dev_t *p, struct siphash *state) {
Packit Service b23acc
        siphash24_compress(p, sizeof(dev_t), state);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
int devt_compare_func(const dev_t *a, const dev_t *b) {
Packit Service b23acc
        return CMP(*a, *b);
Packit Service b23acc
}
Packit Service b23acc
Packit Service b23acc
DEFINE_HASH_OPS(devt_hash_ops, dev_t, devt_hash_func, devt_compare_func);
Packit Service b23acc
#endif
Packit Service b23acc
#endif /* NM_IGNORED */