]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_key_md5.cc
Cleanup: zap CVS Id tags
[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
39 static cache_key null_key[SQUID_MD5_DIGEST_LENGTH];
40
41 const char *
42 storeKeyText(const cache_key *key)
43 {
44 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
45 int i;
46
47 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; i++)
48 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
49
50 return buf;
51 }
52
53 const cache_key *
54 storeKeyScan(const char *buf)
55 {
56 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
57 int i;
58 int j = 0;
59 char t[3];
60
61 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; i++) {
62 t[0] = *(buf + (j++));
63 t[1] = *(buf + (j++));
64 t[2] = '\0';
65 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
66 }
67
68 return digest;
69 }
70
71 int
72 storeKeyHashCmp(const void *a, const void *b)
73 {
74 const unsigned char *A = (const unsigned char *)a;
75 const unsigned char *B = (const unsigned char *)b;
76 int i;
77
78 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; i++) {
79 if (A[i] < B[i])
80 return -1;
81
82 if (A[i] > B[i])
83 return 1;
84 }
85
86 return 0;
87 }
88
89 unsigned int
90 storeKeyHashHash(const void *key, unsigned int n)
91 {
92 /* note, n must be a power of 2! */
93 const unsigned char *digest = (const unsigned char *)key;
94 unsigned int i = digest[0]
95 | digest[1] << 8
96 | digest[2] << 16
97 | digest[3] << 24;
98 return (i & (--n));
99 }
100
101 const cache_key *
102 storeKeyPrivate(const char *url, const HttpRequestMethod& method, int id)
103 {
104 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
105 SquidMD5_CTX M;
106 assert(id > 0);
107 debugs(20, 3, "storeKeyPrivate: " << RequestMethodStr(method) << " " << url);
108 SquidMD5Init(&M);
109 SquidMD5Update(&M, (unsigned char *) &id, sizeof(id));
110 SquidMD5Update(&M, (unsigned char *) &method, sizeof(method));
111 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
112 SquidMD5Final(digest, &M);
113 return digest;
114 }
115
116 const cache_key *
117 storeKeyPublic(const char *url, const HttpRequestMethod& method)
118 {
119 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
120 unsigned char m = (unsigned char) method.id();
121 SquidMD5_CTX M;
122 SquidMD5Init(&M);
123 SquidMD5Update(&M, &m, sizeof(m));
124 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
125 SquidMD5Final(digest, &M);
126 return digest;
127 }
128
129 const cache_key *
130 storeKeyPublicByRequest(HttpRequest * request)
131 {
132 return storeKeyPublicByRequestMethod(request, request->method);
133 }
134
135 const cache_key *
136 storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method)
137 {
138 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
139 unsigned char m = (unsigned char) method.id();
140 const char *url = urlCanonical(request);
141 SquidMD5_CTX M;
142 SquidMD5Init(&M);
143 SquidMD5Update(&M, &m, sizeof(m));
144 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
145
146 if (request->vary_headers)
147 SquidMD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
148
149 SquidMD5Final(digest, &M);
150
151 return digest;
152 }
153
154 cache_key *
155 storeKeyDup(const cache_key * key)
156 {
157 cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
158 xmemcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
159 return dup;
160 }
161
162 cache_key *
163 storeKeyCopy(cache_key * dst, const cache_key * src)
164 {
165 xmemcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
166 return dst;
167 }
168
169 void
170 storeKeyFree(const cache_key * key)
171 {
172 memFree((void *) key, MEM_MD5_DIGEST);
173 }
174
175 int
176 storeKeyHashBuckets(int nbuckets)
177 {
178 int n = 0x2000;
179
180 while (n < nbuckets)
181 n <<= 1;
182
183 return n;
184 }
185
186 int
187 storeKeyNull(const cache_key * key)
188 {
189 if (memcmp(key, null_key, SQUID_MD5_DIGEST_LENGTH) == 0)
190 return 1;
191 else
192 return 0;
193 }
194
195 void
196 storeKeyInit(void)
197 {
198 memset(null_key, '\0', SQUID_MD5_DIGEST_LENGTH);
199 }