]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_key_md5.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / store_key_md5.cc
1 /*
2 * Copyright (C) 1996-2015 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 #include "URL.h"
16
17 static cache_key null_key[SQUID_MD5_DIGEST_LENGTH];
18
19 const char *
20 storeKeyText(const cache_key *key)
21 {
22 if (!key)
23 return "[null_store_key]";
24
25 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
26 int i;
27
28 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
29 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
30
31 return buf;
32 }
33
34 const cache_key *
35 storeKeyScan(const char *buf)
36 {
37 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
38 int i;
39 int j = 0;
40 char t[3];
41
42 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
43 t[0] = *(buf + (j++));
44 t[1] = *(buf + (j++));
45 t[2] = '\0';
46 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
47 }
48
49 return digest;
50 }
51
52 int
53 storeKeyHashCmp(const void *a, const void *b)
54 {
55 const unsigned char *A = (const unsigned char *)a;
56 const unsigned char *B = (const unsigned char *)b;
57 int i;
58
59 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
60 if (A[i] < B[i])
61 return -1;
62
63 if (A[i] > B[i])
64 return 1;
65 }
66
67 return 0;
68 }
69
70 unsigned int
71 storeKeyHashHash(const void *key, unsigned int n)
72 {
73 /* note, n must be a power of 2! */
74 const unsigned char *digest = (const unsigned char *)key;
75 unsigned int i = digest[0]
76 | digest[1] << 8
77 | digest[2] << 16
78 | digest[3] << 24;
79 return (i & (--n));
80 }
81
82 const cache_key *
83 storeKeyPrivate(const char *url, const HttpRequestMethod& method, int id)
84 {
85 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
86 SquidMD5_CTX M;
87 assert(id > 0);
88 debugs(20, 3, "storeKeyPrivate: " << method << " " << url);
89 SquidMD5Init(&M);
90 SquidMD5Update(&M, (unsigned char *) &id, sizeof(id));
91 SquidMD5Update(&M, (unsigned char *) &method, sizeof(method));
92 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
93 SquidMD5Final(digest, &M);
94 return digest;
95 }
96
97 const cache_key *
98 storeKeyPublic(const char *url, const HttpRequestMethod& method)
99 {
100 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
101 unsigned char m = (unsigned char) method.id();
102 SquidMD5_CTX M;
103 SquidMD5Init(&M);
104 SquidMD5Update(&M, &m, sizeof(m));
105 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
106 SquidMD5Final(digest, &M);
107 return digest;
108 }
109
110 const cache_key *
111 storeKeyPublicByRequest(HttpRequest * request)
112 {
113 return storeKeyPublicByRequestMethod(request, request->method);
114 }
115
116 const cache_key *
117 storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method)
118 {
119 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
120 unsigned char m = (unsigned char) method.id();
121 const char *url = request->storeId(); /* storeId returns the right storeID\canonical URL for the md5 calc */
122 SquidMD5_CTX M;
123 SquidMD5Init(&M);
124 SquidMD5Update(&M, &m, sizeof(m));
125 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
126
127 if (request->vary_headers) {
128 SquidMD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
129 debugs(20, 3, "updating public key by vary headers: " << request->vary_headers << " for: " << url);
130 }
131
132 SquidMD5Final(digest, &M);
133
134 return digest;
135 }
136
137 cache_key *
138 storeKeyDup(const cache_key * key)
139 {
140 cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
141 memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
142 return dup;
143 }
144
145 cache_key *
146 storeKeyCopy(cache_key * dst, const cache_key * src)
147 {
148 memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
149 return dst;
150 }
151
152 void
153 storeKeyFree(const cache_key * key)
154 {
155 memFree((void *) key, MEM_MD5_DIGEST);
156 }
157
158 int
159 storeKeyHashBuckets(int nbuckets)
160 {
161 int n = 0x2000;
162
163 while (n < nbuckets)
164 n <<= 1;
165
166 return n;
167 }
168
169 int
170 storeKeyNull(const cache_key * key)
171 {
172 if (memcmp(key, null_key, SQUID_MD5_DIGEST_LENGTH) == 0)
173 return 1;
174 else
175 return 0;
176 }
177
178 void
179 storeKeyInit(void)
180 {
181 memset(null_key, '\0', SQUID_MD5_DIGEST_LENGTH);
182 }
183