]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_key_md5.cc
Maintenance: bump astyle to 2.04 and quieten report
[thirdparty/squid.git] / src / store_key_md5.cc
CommitLineData
9cef6668 1/*
bbc27441 2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
9cef6668 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9cef6668 7 */
8
bbc27441
AJ
9/* DEBUG: section 20 Storage Manager MD5 Cache Keys */
10
582c2af2 11#include "squid.h"
a2ac85d9 12#include "HttpRequest.h"
582c2af2 13#include "md5.h"
fb548aaf 14#include "store_key_md5.h"
b1bd952a 15#include "URL.h"
6507d007 16
c3031d67 17static cache_key null_key[SQUID_MD5_DIGEST_LENGTH];
25535cbe 18
6507d007 19const char *
f1b70fe6 20storeKeyText(const cache_key *key)
6507d007 21{
8c3cd042
AR
22 if (!key)
23 return "[null_store_key]";
24
c3031d67 25 static char buf[SQUID_MD5_DIGEST_LENGTH * 2+1];
6507d007 26 int i;
62e76326 27
5db6bf73 28 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i)
62e76326 29 snprintf(&buf[i*2],sizeof(buf) - i*2, "%02X", *(key + i));
30
528b2c61 31 return buf;
6507d007 32}
33
b8890359 34const cache_key *
6507d007 35storeKeyScan(const char *buf)
36{
c3031d67 37 static unsigned char digest[SQUID_MD5_DIGEST_LENGTH];
6507d007 38 int i;
39 int j = 0;
7363fc17 40 char t[3];
62e76326 41
5db6bf73 42 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
62e76326 43 t[0] = *(buf + (j++));
44 t[1] = *(buf + (j++));
45 t[2] = '\0';
46 *(digest + i) = (unsigned char) strtol(t, NULL, 16);
6507d007 47 }
62e76326 48
6507d007 49 return digest;
50}
51
52int
53storeKeyHashCmp(const void *a, const void *b)
54{
e6ccf245 55 const unsigned char *A = (const unsigned char *)a;
56 const unsigned char *B = (const unsigned char *)b;
6507d007 57 int i;
62e76326 58
5db6bf73 59 for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; ++i) {
62e76326 60 if (A[i] < B[i])
61 return -1;
62
63 if (A[i] > B[i])
64 return 1;
6507d007 65 }
62e76326 66
6507d007 67 return 0;
68}
69
70unsigned int
71storeKeyHashHash(const void *key, unsigned int n)
72{
73 /* note, n must be a power of 2! */
e6ccf245 74 const unsigned char *digest = (const unsigned char *)key;
6507d007 75 unsigned int i = digest[0]
62e76326 76 | digest[1] << 8
77 | digest[2] << 16
78 | digest[3] << 24;
6507d007 79 return (i & (--n));
80}
81
82const cache_key *
60745f24 83storeKeyPrivate(const char *url, const HttpRequestMethod& method, int id)
6507d007 84{
c3031d67 85 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
86 SquidMD5_CTX M;
007b8be4 87 assert(id > 0);
a8a0b1c2 88 debugs(20, 3, "storeKeyPrivate: " << method << " " << url);
c3031d67 89 SquidMD5Init(&M);
90 SquidMD5Update(&M, (unsigned char *) &id, sizeof(id));
91 SquidMD5Update(&M, (unsigned char *) &method, sizeof(method));
92 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
93 SquidMD5Final(digest, &M);
6507d007 94 return digest;
95}
96
b8890359 97const cache_key *
60745f24 98storeKeyPublic(const char *url, const HttpRequestMethod& method)
b8890359 99{
c3031d67 100 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
914b89a2 101 unsigned char m = (unsigned char) method.id();
c3031d67 102 SquidMD5_CTX M;
103 SquidMD5Init(&M);
104 SquidMD5Update(&M, &m, sizeof(m));
105 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
106 SquidMD5Final(digest, &M);
b8890359 107 return digest;
108}
109
f66a9ef4 110const cache_key *
190154cf 111storeKeyPublicByRequest(HttpRequest * request)
f66a9ef4 112{
113 return storeKeyPublicByRequestMethod(request, request->method);
114}
115
116const cache_key *
60745f24 117storeKeyPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method)
f66a9ef4 118{
c3031d67 119 static cache_key digest[SQUID_MD5_DIGEST_LENGTH];
914b89a2 120 unsigned char m = (unsigned char) method.id();
a8a0b1c2 121 const char *url = request->storeId(); /* storeId returns the right storeID\canonical URL for the md5 calc */
c3031d67 122 SquidMD5_CTX M;
123 SquidMD5Init(&M);
124 SquidMD5Update(&M, &m, sizeof(m));
125 SquidMD5Update(&M, (unsigned char *) url, strlen(url));
62e76326 126
a8a0b1c2 127 if (request->vary_headers) {
c3031d67 128 SquidMD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
a8a0b1c2
EC
129 debugs(20, 3, "updating public key by vary headers: " << request->vary_headers << " for: " << url);
130 }
3690da07 131
c3031d67 132 SquidMD5Final(digest, &M);
62e76326 133
f66a9ef4 134 return digest;
135}
136
186477c1 137cache_key *
6507d007 138storeKeyDup(const cache_key * key)
139{
e6ccf245 140 cache_key *dup = (cache_key *)memAllocate(MEM_MD5_DIGEST);
41d00cd3 141 memcpy(dup, key, SQUID_MD5_DIGEST_LENGTH);
6507d007 142 return dup;
143}
144
399cabec 145cache_key *
5942e8d4 146storeKeyCopy(cache_key * dst, const cache_key * src)
399cabec 147{
41d00cd3 148 memcpy(dst, src, SQUID_MD5_DIGEST_LENGTH);
399cabec 149 return dst;
150}
151
6507d007 152void
153storeKeyFree(const cache_key * key)
154{
db1cd23c 155 memFree((void *) key, MEM_MD5_DIGEST);
6507d007 156}
157
158int
baf144ad 159storeKeyHashBuckets(int nbuckets)
6507d007 160{
baf144ad 161 int n = 0x2000;
62e76326 162
baf144ad 163 while (n < nbuckets)
62e76326 164 n <<= 1;
165
baf144ad 166 return n;
6507d007 167}
25535cbe 168
169int
170storeKeyNull(const cache_key * key)
171{
c3031d67 172 if (memcmp(key, null_key, SQUID_MD5_DIGEST_LENGTH) == 0)
62e76326 173 return 1;
25535cbe 174 else
62e76326 175 return 0;
25535cbe 176}
177
178void
179storeKeyInit(void)
180{
c3031d67 181 memset(null_key, '\0', SQUID_MD5_DIGEST_LENGTH);
25535cbe 182}