]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/hash-funcs.c
basic/set: let set_put_strdup() create the set with string hash ops
[thirdparty/systemd.git] / src / basic / hash-funcs.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <string.h>
4
5 #include "hash-funcs.h"
6 #include "path-util.h"
7
8 void string_hash_func(const char *p, struct siphash *state) {
9 siphash24_compress(p, strlen(p) + 1, state);
10 }
11
12 DEFINE_HASH_OPS(string_hash_ops, char, string_hash_func, string_compare_func);
13 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(string_hash_ops_free,
14 char, string_hash_func, string_compare_func, free);
15 DEFINE_HASH_OPS_FULL(string_hash_ops_free_free,
16 char, string_hash_func, string_compare_func, free,
17 char, free);
18
19 void path_hash_func(const char *q, struct siphash *state) {
20 size_t n;
21
22 assert(q);
23 assert(state);
24
25 /* Calculates a hash for a path in a way this duplicate inner slashes don't make a differences, and also
26 * whether there's a trailing slash or not. This fits well with the semantics of path_compare(), which does
27 * similar checks and also doesn't care for trailing slashes. Note that relative and absolute paths (i.e. those
28 * which begin in a slash or not) will hash differently though. */
29
30 n = strspn(q, "/");
31 if (n > 0) { /* Eat up initial slashes, and add one "/" to the hash for all of them */
32 siphash24_compress(q, 1, state);
33 q += n;
34 }
35
36 for (;;) {
37 /* Determine length of next component */
38 n = strcspn(q, "/");
39 if (n == 0) /* Reached the end? */
40 break;
41
42 /* Add this component to the hash and skip over it */
43 siphash24_compress(q, n, state);
44 q += n;
45
46 /* How many slashes follow this component? */
47 n = strspn(q, "/");
48 if (q[n] == 0) /* Is this a trailing slash? If so, we are at the end, and don't care about the slashes anymore */
49 break;
50
51 /* We are not add the end yet. Hash exactly one slash for all of the ones we just encountered. */
52 siphash24_compress(q, 1, state);
53 q += n;
54 }
55 }
56
57 DEFINE_HASH_OPS(path_hash_ops, char, path_hash_func, path_compare);
58
59 void trivial_hash_func(const void *p, struct siphash *state) {
60 siphash24_compress(&p, sizeof(p), state);
61 }
62
63 int trivial_compare_func(const void *a, const void *b) {
64 return CMP(a, b);
65 }
66
67 const struct hash_ops trivial_hash_ops = {
68 .hash = trivial_hash_func,
69 .compare = trivial_compare_func,
70 };
71
72 void uint64_hash_func(const uint64_t *p, struct siphash *state) {
73 siphash24_compress(p, sizeof(uint64_t), state);
74 }
75
76 int uint64_compare_func(const uint64_t *a, const uint64_t *b) {
77 return CMP(*a, *b);
78 }
79
80 DEFINE_HASH_OPS(uint64_hash_ops, uint64_t, uint64_hash_func, uint64_compare_func);
81
82 #if SIZEOF_DEV_T != 8
83 void devt_hash_func(const dev_t *p, struct siphash *state) {
84 siphash24_compress(p, sizeof(dev_t), state);
85 }
86
87 int devt_compare_func(const dev_t *a, const dev_t *b) {
88 return CMP(*a, *b);
89 }
90
91 DEFINE_HASH_OPS(devt_hash_ops, dev_t, devt_hash_func, devt_compare_func);
92 #endif