]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_key_md5.cc
MD5 support for Squid's cache keys!
[thirdparty/squid.git] / src / store_key_md5.cc
1 #include "squid.h"
2
3 #if STORE_KEY_MD5
4
5 const char *
6 storeKeyText(const unsigned char *key)
7 {
8 LOCAL_ARRAY(char, buf, 33);
9 int i;
10 int o;
11 for (i = 0; i < MD5_DIGEST_CHARS; i++) {
12 o = i << 1;
13 snprintf(buf + o, 33 - o, "%02X", *(key + i));
14 }
15 return buf;
16 }
17
18 const unsigned char *
19 storeKeyScan(const char *buf)
20 {
21 static unsigned char digest[MD5_DIGEST_CHARS];
22 int i;
23 int j = 0;
24 unsigned char t[3];
25 for (i=0; i<MD5_DIGEST_CHARS; i++) {
26 t[0] = *(buf+(j++));
27 t[1] = *(buf+(j++));
28 t[2] = '\0';
29 *(digest+i) = (unsigned char) strtol(t, NULL, 16);
30 }
31 return digest;
32 }
33
34 int
35 storeKeyHashCmp(const void *a, const void *b)
36 {
37 const unsigned char *A = a;
38 const unsigned char *B = b;
39 int i;
40 for (i = 0; i < MD5_DIGEST_CHARS; i++) {
41 if (A[i] < B[i])
42 return -1;
43 if (A[i] > B[i])
44 return 1;
45 }
46 return 0;
47 }
48
49 unsigned int
50 storeKeyHashHash(const void *key, unsigned int n)
51 {
52 /* note, n must be a power of 2! */
53 const unsigned char *digest = key;
54 unsigned int i = digest[0]
55 | digest[1] << 8
56 | digest[2] <<16
57 | digest[3] <<24;
58 return (i & (--n));
59 }
60
61 const cache_key *
62 storeKeyPrivate(const char *url, method_t method, int num)
63 {
64 static cache_key digest[MD5_DIGEST_CHARS];
65 MD5_CTX M;
66 int n;
67 char key_buf[MAX_URL + 100];
68 assert(num > 0);
69 debug(20, 3) ("storeKeyPrivate: '%s'\n", url);
70 n = snprintf(key_buf, MAX_URL + 100, "%d %s %s",
71 num,
72 RequestMethodStr[method],
73 url);
74 MD5Init(&M);
75 MD5Update(&M, key_buf, n);
76 MD5Final(digest, &M);
77 return digest;
78 }
79
80 const cache_key *
81 storeKeyPublic(const char *url, method_t method)
82 {
83 static cache_key digest[MD5_DIGEST_CHARS];
84 MD5_CTX M;
85 int n;
86 char key_buf[MAX_URL + 100];
87 n = snprintf(key_buf, MAX_URL + 100, "%s %s",
88 RequestMethodStr[method],
89 url);
90 MD5Init(&M);
91 MD5Update(&M, key_buf, n);
92 MD5Final(digest, &M);
93 return digest;
94 }
95
96 const cache_key *
97 storeKeyDup(const cache_key * key)
98 {
99 cache_key *dup = xmalloc(MD5_DIGEST_CHARS);
100 xmemcpy(dup, key, MD5_DIGEST_CHARS);
101 meta_data.store_keys += MD5_DIGEST_CHARS;
102 return dup;
103 }
104
105 void
106 storeKeyFree(const cache_key * key)
107 {
108 xfree((void *) key);
109 meta_data.store_keys -= MD5_DIGEST_CHARS;
110 }
111
112 int
113 storeKeyHashBuckets(int nobj)
114 {
115 if (nobj < 0x2000)
116 return 0x2000;
117 if (nobj < 0x4000)
118 return 0x4000;
119 if (nobj < 0x8000)
120 return 0x8000;
121 return 0x10000;
122 }
123
124 #endif /* STORE_KEY_MD5 */