1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "hash-funcs.h"
8 void string_hash_func(const char *p
, struct siphash
*state
) {
9 siphash24_compress(p
, strlen(p
) + 1, state
);
12 DEFINE_HASH_OPS(string_hash_ops
, char, string_hash_func
, string_compare_func
);
14 void path_hash_func(const char *q
, struct siphash
*state
) {
20 /* Calculates a hash for a path in a way this duplicate inner slashes don't make a differences, and also
21 * whether there's a trailing slash or not. This fits well with the semantics of path_compare(), which does
22 * similar checks and also doesn't care for trailing slashes. Note that relative and absolute paths (i.e. those
23 * which begin in a slash or not) will hash differently though. */
26 if (n
> 0) { /* Eat up initial slashes, and add one "/" to the hash for all of them */
27 siphash24_compress(q
, 1, state
);
32 /* Determine length of next component */
34 if (n
== 0) /* Reached the end? */
37 /* Add this component to the hash and skip over it */
38 siphash24_compress(q
, n
, state
);
41 /* How many slashes follow this component? */
43 if (q
[n
] == 0) /* Is this a trailing slash? If so, we are at the end, and don't care about the slashes anymore */
46 /* We are not add the end yet. Hash exactly one slash for all of the ones we just encountered. */
47 siphash24_compress(q
, 1, state
);
52 int path_compare_func(const char *a
, const char *b
) {
53 return path_compare(a
, b
);
56 DEFINE_HASH_OPS(path_hash_ops
, char, path_hash_func
, path_compare_func
);
58 void trivial_hash_func(const void *p
, struct siphash
*state
) {
59 siphash24_compress(&p
, sizeof(p
), state
);
62 int trivial_compare_func(const void *a
, const void *b
) {
66 const struct hash_ops trivial_hash_ops
= {
67 .hash
= trivial_hash_func
,
68 .compare
= trivial_compare_func
,
71 void uint64_hash_func(const uint64_t *p
, struct siphash
*state
) {
72 siphash24_compress(p
, sizeof(uint64_t), state
);
75 int uint64_compare_func(const uint64_t *a
, const uint64_t *b
) {
79 DEFINE_HASH_OPS(uint64_hash_ops
, uint64_t, uint64_hash_func
, uint64_compare_func
);
82 void devt_hash_func(const dev_t
*p
, struct siphash
*state
) {
83 siphash24_compress(p
, sizeof(dev_t
), state
);
86 int devt_compare_func(const dev_t
*a
, const dev_t
*b
) {
90 DEFINE_HASH_OPS(devt_hash_ops
, dev_t
, devt_hash_func
, devt_compare_func
);