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