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 251930 : void string_hash_func(const char *p, struct siphash *state) {
9 251930 : siphash24_compress(p, strlen(p) + 1, state);
10 251930 : }
11 :
12 : DEFINE_HASH_OPS(string_hash_ops, char, string_hash_func, string_compare_func);
13 1650 : DEFINE_HASH_OPS_FULL(string_hash_ops_free_free,
14 : char, string_hash_func, string_compare_func, free,
15 : char, free);
16 :
17 73338 : void path_hash_func(const char *q, struct siphash *state) {
18 : size_t n;
19 :
20 73338 : assert(q);
21 73338 : 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 73338 : n = strspn(q, "/");
29 73338 : if (n > 0) { /* Eat up initial slashes, and add one "/" to the hash for all of them */
30 72092 : siphash24_compress(q, 1, state);
31 72092 : q += n;
32 : }
33 :
34 : for (;;) {
35 : /* Determine length of next component */
36 732032 : n = strcspn(q, "/");
37 402685 : if (n == 0) /* Reached the end? */
38 630 : break;
39 :
40 : /* Add this component to the hash and skip over it */
41 402055 : siphash24_compress(q, n, state);
42 402055 : q += n;
43 :
44 : /* How many slashes follow this component? */
45 402055 : n = strspn(q, "/");
46 402055 : 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 72708 : break;
48 :
49 : /* We are not add the end yet. Hash exactly one slash for all of the ones we just encountered. */
50 329347 : siphash24_compress(q, 1, state);
51 329347 : q += n;
52 : }
53 73338 : }
54 :
55 36744 : int path_compare_func(const char *a, const char *b) {
56 36744 : return path_compare(a, b);
57 : }
58 :
59 : DEFINE_HASH_OPS(path_hash_ops, char, path_hash_func, path_compare_func);
60 :
61 134282 : void trivial_hash_func(const void *p, struct siphash *state) {
62 134282 : siphash24_compress(&p, sizeof(p), state);
63 134282 : }
64 :
65 174057 : int trivial_compare_func(const void *a, const void *b) {
66 174057 : 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 22117 : void uint64_hash_func(const uint64_t *p, struct siphash *state) {
75 22117 : siphash24_compress(p, sizeof(uint64_t), state);
76 22117 : }
77 :
78 21982 : int uint64_compare_func(const uint64_t *a, const uint64_t *b) {
79 21982 : 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
|