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