]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_key_md5.cc
gindent
[thirdparty/squid.git] / src / store_key_md5.cc
1
2 /*
3 * $Id: store_key_md5.cc,v 1.16 1998/09/15 19:38:02 wessels Exp $
4 *
5 * DEBUG: section 20 Storage Manager MD5 Cache Keys
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * Duane Wessels and the University of California San Diego. Please
16 * see the COPYRIGHT file for full details. Squid incorporates
17 * software developed and/or copyrighted by other sources. Please see
18 * 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
38 static cache_key null_key[MD5_DIGEST_CHARS];
39
40 const char *
41 storeKeyText(const unsigned char *key)
42 {
43 static MemBuf mb = MemBufNULL;
44 int i;
45 memBufReset(&mb);
46 for (i = 0; i < MD5_DIGEST_CHARS; i++)
47 memBufPrintf(&mb, "%02X", *(key + i));
48 return mb.buf;
49 }
50
51 const unsigned char *
52 storeKeyScan(const char *buf)
53 {
54 static unsigned char digest[MD5_DIGEST_CHARS];
55 int i;
56 int j = 0;
57 char t[3];
58 for (i = 0; i < MD5_DIGEST_CHARS; i++) {
59 t[0] = *(buf + (j++));
60 t[1] = *(buf + (j++));
61 t[2] = '\0';
62 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
63 }
64 return digest;
65 }
66
67 int
68 storeKeyHashCmp(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
82 unsigned int
83 storeKeyHashHash(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]
88 | digest[1] << 8
89 | digest[2] << 16
90 | digest[3] << 24;
91 return (i & (--n));
92 }
93
94 const cache_key *
95 storeKeyPrivate(const char *url, method_t method, int id)
96 {
97 static cache_key digest[MD5_DIGEST_CHARS];
98 MD5_CTX M;
99 assert(id > 0);
100 debug(20, 3) ("storeKeyPrivate: %s %s\n",
101 RequestMethodStr[method], url);
102 MD5Init(&M);
103 MD5Update(&M, (unsigned char *) &id, sizeof(id));
104 MD5Update(&M, (unsigned char *) &method, sizeof(method));
105 MD5Update(&M, (unsigned char *) url, strlen(url));
106 MD5Final(digest, &M);
107 return digest;
108 }
109
110 const cache_key *
111 storeKeyPublic(const char *url, method_t method)
112 {
113 static cache_key digest[MD5_DIGEST_CHARS];
114 MD5_CTX M;
115 MD5Init(&M);
116 MD5Update(&M, (unsigned char *) &method, sizeof(method));
117 MD5Update(&M, (unsigned char *) url, strlen(url));
118 MD5Final(digest, &M);
119 return digest;
120 }
121
122 const cache_key *
123 storeKeyDup(const cache_key * key)
124 {
125 cache_key *dup = memAllocate(MEM_MD5_DIGEST);
126 xmemcpy(dup, key, MD5_DIGEST_CHARS);
127 return dup;
128 }
129
130 cache_key *
131 storeKeyCopy(cache_key * dst, const cache_key * src)
132 {
133 xmemcpy(dst, src, MD5_DIGEST_CHARS);
134 return dst;
135 }
136
137 void
138 storeKeyFree(const cache_key * key)
139 {
140 memFree(MEM_MD5_DIGEST, (void *) key);
141 }
142
143 int
144 storeKeyHashBuckets(int nobj)
145 {
146 if (nobj < 0x2000)
147 return 0x2000;
148 if (nobj < 0x4000)
149 return 0x4000;
150 if (nobj < 0x8000)
151 return 0x8000;
152 return 0x10000;
153 }
154
155 int
156 storeKeyNull(const cache_key * key)
157 {
158 if (memcmp(key, null_key, MD5_DIGEST_CHARS) == 0)
159 return 1;
160 else
161 return 0;
162 }
163
164 void
165 storeKeyInit(void)
166 {
167 memset(null_key, '\0', MD5_DIGEST_CHARS);
168 }