]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_key_md5.cc
fix some of the header detection issues on FreeBSD
[thirdparty/squid.git] / src / store_key_md5.cc
CommitLineData
9cef6668 1
2/*
f66a9ef4 3 * $Id: store_key_md5.cc,v 1.26 2001/04/14 00:25:19 hno Exp $
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.
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
6507d007 36#include "squid.h"
37
25535cbe 38static cache_key null_key[MD5_DIGEST_CHARS];
39
6507d007 40const char *
41storeKeyText(const unsigned char *key)
42{
137ee196 43 static MemBuf mb = MemBufNULL;
6507d007 44 int i;
137ee196 45 memBufReset(&mb);
46 for (i = 0; i < MD5_DIGEST_CHARS; i++)
47 memBufPrintf(&mb, "%02X", *(key + i));
48 return mb.buf;
6507d007 49}
50
b8890359 51const cache_key *
6507d007 52storeKeyScan(const char *buf)
53{
54 static unsigned char digest[MD5_DIGEST_CHARS];
55 int i;
56 int j = 0;
7363fc17 57 char t[3];
164f7660 58 for (i = 0; i < MD5_DIGEST_CHARS; i++) {
59 t[0] = *(buf + (j++));
60 t[1] = *(buf + (j++));
6507d007 61 t[2] = '\0';
164f7660 62 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
6507d007 63 }
64 return digest;
65}
66
67int
68storeKeyHashCmp(const void *a, const void *b)
69{
70 const unsigned char *A = a;
71 const unsigned char *B = b;
72 int i;
73 for (i = 0; i < MD5_DIGEST_CHARS; i++) {
74 if (A[i] < B[i])
75 return -1;
76 if (A[i] > B[i])
77 return 1;
78 }
79 return 0;
80}
81
82unsigned int
83storeKeyHashHash(const void *key, unsigned int n)
84{
85 /* note, n must be a power of 2! */
86 const unsigned char *digest = key;
87 unsigned int i = digest[0]
164f7660 88 | digest[1] << 8
89 | digest[2] << 16
90 | digest[3] << 24;
6507d007 91 return (i & (--n));
92}
93
94const cache_key *
007b8be4 95storeKeyPrivate(const char *url, method_t method, int id)
6507d007 96{
97 static cache_key digest[MD5_DIGEST_CHARS];
98 MD5_CTX M;
007b8be4 99 assert(id > 0);
5ad33356 100 debug(20, 3) ("storeKeyPrivate: %s %s\n",
101 RequestMethodStr[method], url);
6507d007 102 MD5Init(&M);
007b8be4 103 MD5Update(&M, (unsigned char *) &id, sizeof(id));
104 MD5Update(&M, (unsigned char *) &method, sizeof(method));
105 MD5Update(&M, (unsigned char *) url, strlen(url));
6507d007 106 MD5Final(digest, &M);
107 return digest;
108}
109
b8890359 110const cache_key *
111storeKeyPublic(const char *url, const method_t method)
112{
113 static cache_key digest[MD5_DIGEST_CHARS];
114 unsigned char m = (unsigned char) method;
115 MD5_CTX M;
116 MD5Init(&M);
117 MD5Update(&M, &m, sizeof(m));
118 MD5Update(&M, (unsigned char *) url, strlen(url));
119 MD5Final(digest, &M);
120 return digest;
121}
122
f66a9ef4 123const cache_key *
124storeKeyPublicByRequest(request_t * request)
125{
126 return storeKeyPublicByRequestMethod(request, request->method);
127}
128
129const cache_key *
130storeKeyPublicByRequestMethod(request_t * request, const method_t method)
131{
132 static cache_key digest[MD5_DIGEST_CHARS];
133 unsigned char m = (unsigned char) method;
134 const char *url = urlCanonical(request);
135 MD5_CTX M;
136 MD5Init(&M);
137 MD5Update(&M, &m, sizeof(m));
138 MD5Update(&M, (unsigned char *) url, strlen(url));
139 if (request->vary_headers)
140 MD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
141 MD5Final(digest, &M);
142 return digest;
143}
144
186477c1 145cache_key *
6507d007 146storeKeyDup(const cache_key * key)
147{
e55650e3 148 cache_key *dup = memAllocate(MEM_MD5_DIGEST);
6507d007 149 xmemcpy(dup, key, MD5_DIGEST_CHARS);
6507d007 150 return dup;
151}
152
399cabec 153cache_key *
5942e8d4 154storeKeyCopy(cache_key * dst, const cache_key * src)
399cabec 155{
156 xmemcpy(dst, src, MD5_DIGEST_CHARS);
157 return dst;
158}
159
6507d007 160void
161storeKeyFree(const cache_key * key)
162{
db1cd23c 163 memFree((void *) key, MEM_MD5_DIGEST);
6507d007 164}
165
166int
baf144ad 167storeKeyHashBuckets(int nbuckets)
6507d007 168{
baf144ad 169 int n = 0x2000;
170 while (n < nbuckets)
171 n <<= 1;
172 return n;
6507d007 173}
25535cbe 174
175int
176storeKeyNull(const cache_key * key)
177{
178 if (memcmp(key, null_key, MD5_DIGEST_CHARS) == 0)
179 return 1;
180 else
181 return 0;
182}
183
184void
185storeKeyInit(void)
186{
187 memset(null_key, '\0', MD5_DIGEST_CHARS);
188}