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