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