]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_key_md5.cc
Cleanup: zap CVS Id tags
[thirdparty/squid.git] / src / store_key_md5.cc
CommitLineData
9cef6668 1
2/*
262a0e14 3 * $Id$
9cef6668 4 *
5 * DEBUG: section 20 Storage Manager MD5 Cache Keys
6 * AUTHOR: Duane Wessels
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9cef6668 9 * ----------------------------------------------------------
10 *
2b6662ba 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.
9cef6668 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.
26ac0430 24 *
9cef6668 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.
26ac0430 29 *
9cef6668 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
6507d007 36#include "squid.h"
a2ac85d9 37#include "HttpRequest.h"
6507d007 38
c3031d67 39static cache_key null_key[SQUID_MD5_DIGEST_LENGTH];
25535cbe 40
6507d007 41const char *
f1b70fe6 42storeKeyText(const cache_key *key)
6507d007 43{
c3031d67 44 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
6507d007 45 int i;
62e76326 46
c3031d67 47 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; i++)
62e76326 48 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
49
528b2c61 50 return buf;
6507d007 51}
52
b8890359 53const cache_key *
6507d007 54storeKeyScan(const char *buf)
55{
c3031d67 56 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
6507d007 57 int i;
58 int j = 0;
7363fc17 59 char t[3];
62e76326 60
c3031d67 61 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; i++) {
62e76326 62 t[0] = *(buf + (j++));
63 t[1] = *(buf + (j++));
64 t[2] = '\0';
65 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
6507d007 66 }
62e76326 67
6507d007 68 return digest;
69}
70
71int
72storeKeyHashCmp(const void *a, const void *b)
73{
e6ccf245 74 const unsigned char *A = (const unsigned char *)a;
75 const unsigned char *B = (const unsigned char *)b;
6507d007 76 int i;
62e76326 77
c3031d67 78 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; i++) {
62e76326 79 if (A[i] < B[i])
80 return -1;
81
82 if (A[i] > B[i])
83 return 1;
6507d007 84 }
62e76326 85
6507d007 86 return 0;
87}
88
89unsigned int
90storeKeyHashHash(const void *key, unsigned int n)
91{
92 /* note, n must be a power of 2! */
e6ccf245 93 const unsigned char *digest = (const unsigned char *)key;
6507d007 94 unsigned int i = digest[0]
62e76326 95 | digest[1] << 8
96 | digest[2] << 16
97 | digest[3] << 24;
6507d007 98 return (i & (--n));
99}
100
101const cache_key *
60745f24 102storeKeyPrivate(const char *url, const HttpRequestMethod& method, int id)
6507d007 103{
c3031d67 104 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
105 SquidMD5_CTX M;
007b8be4 106 assert(id > 0);
60745f24 107 debugs(20, 3, "storeKeyPrivate: " << RequestMethodStr(method) << " " << url);
c3031d67 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);
6507d007 113 return digest;
114}
115
b8890359 116const cache_key *
60745f24 117storeKeyPublic(const char *url, const HttpRequestMethod& method)
b8890359 118{
c3031d67 119 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
914b89a2 120 unsigned char m = (unsigned char) method.id();
c3031d67 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);
b8890359 126 return digest;
127}
128
f66a9ef4 129const cache_key *
190154cf 130storeKeyPublicByRequest(HttpRequest * request)
f66a9ef4 131{
132 return storeKeyPublicByRequestMethod(request, request->method);
133}
134
135const cache_key *
60745f24 136storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method)
f66a9ef4 137{
c3031d67 138 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
914b89a2 139 unsigned char m = (unsigned char) method.id();
f66a9ef4 140 const char *url = urlCanonical(request);
c3031d67 141 SquidMD5_CTX M;
142 SquidMD5Init(&M);
143 SquidMD5Update(&M, &m, sizeof(m));
144 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
62e76326 145
f66a9ef4 146 if (request->vary_headers)
c3031d67 147 SquidMD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
62e76326 148
c3031d67 149 SquidMD5Final(digest, &M);
62e76326 150
f66a9ef4 151 return digest;
152}
153
186477c1 154cache_key *
6507d007 155storeKeyDup(const cache_key * key)
156{
e6ccf245 157 cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
c3031d67 158 xmemcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
6507d007 159 return dup;
160}
161
399cabec 162cache_key *
5942e8d4 163storeKeyCopy(cache_key * dst, const cache_key * src)
399cabec 164{
c3031d67 165 xmemcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
399cabec 166 return dst;
167}
168
6507d007 169void
170storeKeyFree(const cache_key * key)
171{
db1cd23c 172 memFree((void *) key, MEM_MD5_DIGEST);
6507d007 173}
174
175int
baf144ad 176storeKeyHashBuckets(int nbuckets)
6507d007 177{
baf144ad 178 int n = 0x2000;
62e76326 179
baf144ad 180 while (n < nbuckets)
62e76326 181 n <<= 1;
182
baf144ad 183 return n;
6507d007 184}
25535cbe 185
186int
187storeKeyNull(const cache_key * key)
188{
c3031d67 189 if (memcmp(key, null_key, SQUID_MD5_DIGEST_LENGTH) == 0)
62e76326 190 return 1;
25535cbe 191 else
62e76326 192 return 0;
25535cbe 193}
194
195void
196storeKeyInit(void)
197{
c3031d67 198 memset(null_key, '\0', SQUID_MD5_DIGEST_LENGTH);
25535cbe 199}