]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_key_md5.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / store_key_md5.cc
1 /*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 20 Storage Manager MD5 Cache Keys */
10
11 #include "squid.h"
12 #include "HttpRequest.h"
13 #include "md5.h"
14 #include "store_key_md5.h"
15
16 const char *
17 storeKeyText(const cache_key *key)
18 {
19 if (!key)
20 return "[null_store_key]";
21
22 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
23 int i;
24
25 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
26 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
27
28 return buf;
29 }
30
31 const cache_key *
32 storeKeyScan(const char *buf)
33 {
34 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
35 int i;
36 int j = 0;
37 char t[3];
38
39 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
40 t[0] = *(buf + (j++));
41 t[1] = *(buf + (j++));
42 t[2] = '\0';
43 *(digest + i) = (unsigned char) strtol(t, nullptr, 16);
44 }
45
46 return digest;
47 }
48
49 int
50 storeKeyHashCmp(const void *a, const void *b)
51 {
52 const unsigned char *A = (const unsigned char *)a;
53 const unsigned char *B = (const unsigned char *)b;
54 int i;
55
56 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
57 if (A[i] < B[i])
58 return -1;
59
60 if (A[i] > B[i])
61 return 1;
62 }
63
64 return 0;
65 }
66
67 unsigned int
68 storeKeyHashHash(const void *key, unsigned int n)
69 {
70 /* note, n must be a power of 2! */
71 const unsigned char *digest = (const unsigned char *)key;
72 unsigned int i = digest[0]
73 | digest[1] << 8
74 | digest[2] << 16
75 | digest[3] << 24;
76 return (i & (--n));
77 }
78
79 const cache_key *
80 storeKeyPrivate()
81 {
82 // only the count field is required
83 // others just simplify searching for keys in a multi-process cache.log
84 static struct {
85 uint64_t count;
86 pid_t pid;
87 int32_t kid;
88 } key = { 0, getpid(), KidIdentifier };
89 assert(sizeof(key) == SQUID_MD5_DIGEST_LENGTH);
90 ++key.count;
91 return reinterpret_cast<cache_key*>(&key);
92 }
93
94 const cache_key *
95 storeKeyPublic(const char *url, const HttpRequestMethod& method, const KeyScope keyScope)
96 {
97 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
98 unsigned char m = (unsigned char) method.id();
99 SquidMD5_CTX M;
100 SquidMD5Init(&M);
101 SquidMD5Update(&M, &m, sizeof(m));
102 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
103 if (keyScope)
104 SquidMD5Update(&M, &keyScope, sizeof(keyScope));
105 SquidMD5Final(digest, &M);
106 return digest;
107 }
108
109 const cache_key *
110 storeKeyPublicByRequest(HttpRequest * request, const KeyScope keyScope)
111 {
112 return storeKeyPublicByRequestMethod(request, request->method, keyScope);
113 }
114
115 const cache_key *
116 storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method, const KeyScope keyScope)
117 {
118 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
119 unsigned char m = (unsigned char) method.id();
120 const SBuf url = request->storeId(); /* returns the right storeID\URL for the MD5 calc */
121 SquidMD5_CTX M;
122 SquidMD5Init(&M);
123 SquidMD5Update(&M, &m, sizeof(m));
124 SquidMD5Update(&M, (unsigned char *) url.rawContent(), url.length());
125 if (keyScope)
126 SquidMD5Update(&M, &keyScope, sizeof(keyScope));
127
128 if (!request->vary_headers.isEmpty()) {
129 SquidMD5Update(&M, request->vary_headers.rawContent(), request->vary_headers.length());
130 debugs(20, 3, "updating public key by vary headers: " << request->vary_headers << " for: " << url);
131 }
132
133 SquidMD5Final(digest, &M);
134
135 return digest;
136 }
137
138 cache_key *
139 storeKeyDup(const cache_key * key)
140 {
141 cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
142 memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
143 return dup;
144 }
145
146 cache_key *
147 storeKeyCopy(cache_key * dst, const cache_key * src)
148 {
149 memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
150 return dst;
151 }
152
153 void
154 storeKeyFree(const cache_key * key)
155 {
156 memFree((void *) key, MEM_MD5_DIGEST);
157 }
158
159 int
160 storeKeyHashBuckets(int nbuckets)
161 {
162 int n = 0x2000;
163
164 while (n < nbuckets)
165 n <<= 1;
166
167 return n;
168 }
169