]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_key_md5.cc
reinstall ICP reqnum high-byte hack for encoding request method
[thirdparty/squid.git] / src / store_key_md5.cc
1 #include "squid.h"
2
3 static cache_key null_key[MD5_DIGEST_CHARS];
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 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 %s\n",
70 RequestMethodStr[method], url);
71 n = snprintf(key_buf, MAX_URL + 100, "%d %s %s",
72 num,
73 RequestMethodStr[method],
74 url);
75 MD5Init(&M);
76 MD5Update(&M, (unsigned char *) key_buf, n);
77 MD5Final(digest, &M);
78 return digest;
79 }
80
81 const cache_key *
82 storeKeyPublic(const char *url, method_t method)
83 {
84 static cache_key digest[MD5_DIGEST_CHARS];
85 MD5_CTX M;
86 int n;
87 char key_buf[MAX_URL + 100];
88 n = snprintf(key_buf, MAX_URL + 100, "%s %s",
89 RequestMethodStr[method],
90 url);
91 MD5Init(&M);
92 MD5Update(&M, (unsigned char *) key_buf, n);
93 MD5Final(digest, &M);
94 return digest;
95 }
96
97 const cache_key *
98 storeKeyDup(const cache_key * key)
99 {
100 cache_key *dup = xmalloc(MD5_DIGEST_CHARS);
101 xmemcpy(dup, key, MD5_DIGEST_CHARS);
102 /* XXX account key */
103 return dup;
104 }
105
106 void
107 storeKeyFree(const cache_key * key)
108 {
109 xfree((void *) key);
110 /* XXX account key */
111 }
112
113 int
114 storeKeyHashBuckets(int nobj)
115 {
116 if (nobj < 0x2000)
117 return 0x2000;
118 if (nobj < 0x4000)
119 return 0x4000;
120 if (nobj < 0x8000)
121 return 0x8000;
122 return 0x10000;
123 }
124
125 int
126 storeKeyNull(const cache_key * key)
127 {
128 if (memcmp(key, null_key, MD5_DIGEST_CHARS) == 0)
129 return 1;
130 else
131 return 0;
132 }
133
134 void
135 storeKeyInit(void)
136 {
137 memset(null_key, '\0', MD5_DIGEST_CHARS);
138 }