]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_key_md5.cc
Merged from trunk
[thirdparty/squid.git] / src / store_key_md5.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 20 Storage Manager MD5 Cache Keys
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37 #include "HttpRequest.h"
38 #include "md5.h"
39 #include "Mem.h"
40 #include "store_key_md5.h"
41 #include "URL.h"
42
43 static cache_key null_key[SQUID_MD5_DIGEST_LENGTH];
44
45 const char *
46 storeKeyText(const cache_key *key)
47 {
48 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
49 int i;
50
51 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
52 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
53
54 return buf;
55 }
56
57 const cache_key *
58 storeKeyScan(const char *buf)
59 {
60 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
61 int i;
62 int j = 0;
63 char t[3];
64
65 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
66 t[0] = *(buf + (j++));
67 t[1] = *(buf + (j++));
68 t[2] = '\0';
69 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
70 }
71
72 return digest;
73 }
74
75 int
76 storeKeyHashCmp(const void *a, const void *b)
77 {
78 const unsigned char *A = (const unsigned char *)a;
79 const unsigned char *B = (const unsigned char *)b;
80 int i;
81
82 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
83 if (A[i] < B[i])
84 return -1;
85
86 if (A[i] > B[i])
87 return 1;
88 }
89
90 return 0;
91 }
92
93 unsigned int
94 storeKeyHashHash(const void *key, unsigned int n)
95 {
96 /* note, n must be a power of 2! */
97 const unsigned char *digest = (const unsigned char *)key;
98 unsigned int i = digest[0]
99 | digest[1] << 8
100 | digest[2] << 16
101 | digest[3] << 24;
102 return (i & (--n));
103 }
104
105 const cache_key *
106 storeKeyPrivate(const char *url, const HttpRequestMethod& method, int id)
107 {
108 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
109 SquidMD5_CTX M;
110 assert(id > 0);
111 debugs(20, 3, "storeKeyPrivate: " << RequestMethodStr(method) << " " << url);
112 SquidMD5Init(&M);
113 SquidMD5Update(&M, (unsigned char *) &id, sizeof(id));
114 SquidMD5Update(&M, (unsigned char *) &method, sizeof(method));
115 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
116 SquidMD5Final(digest, &M);
117 return digest;
118 }
119
120 const cache_key *
121 storeKeyPublic(const char *url, const HttpRequestMethod& method)
122 {
123 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
124 unsigned char m = (unsigned char) method.id();
125 SquidMD5_CTX M;
126 SquidMD5Init(&M);
127 SquidMD5Update(&M, &m, sizeof(m));
128 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
129 SquidMD5Final(digest, &M);
130 return digest;
131 }
132
133 const cache_key *
134 storeKeyPublicByRequest(HttpRequest * request)
135 {
136 return storeKeyPublicByRequestMethod(request, request->method);
137 }
138
139 const cache_key *
140 storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method)
141 {
142 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
143 unsigned char m = (unsigned char) method.id();
144 const char *url = urlCanonical(request);
145 SquidMD5_CTX M;
146 SquidMD5Init(&M);
147 SquidMD5Update(&M, &m, sizeof(m));
148 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
149
150 if (request->vary_headers)
151 SquidMD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
152
153 SquidMD5Final(digest, &M);
154
155 return digest;
156 }
157
158 cache_key *
159 storeKeyDup(const cache_key * key)
160 {
161 cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
162 memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
163 return dup;
164 }
165
166 cache_key *
167 storeKeyCopy(cache_key * dst, const cache_key * src)
168 {
169 memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
170 return dst;
171 }
172
173 void
174 storeKeyFree(const cache_key * key)
175 {
176 memFree((void *) key, MEM_MD5_DIGEST);
177 }
178
179 int
180 storeKeyHashBuckets(int nbuckets)
181 {
182 int n = 0x2000;
183
184 while (n < nbuckets)
185 n <<= 1;
186
187 return n;
188 }
189
190 int
191 storeKeyNull(const cache_key * key)
192 {
193 if (memcmp(key, null_key, SQUID_MD5_DIGEST_LENGTH) == 0)
194 return 1;
195 else
196 return 0;
197 }
198
199 void
200 storeKeyInit(void)
201 {
202 memset(null_key, '\0', SQUID_MD5_DIGEST_LENGTH);
203 }