]> git.ipfire.org Git - thirdparty/squid.git/blob - src/StoreMeta.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / StoreMeta.cc
1 /*
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 /* DEBUG: section 20 Storage Manager Swapfile Metadata */
10
11 #include "squid.h"
12 #include "base/Range.h"
13 #include "MemObject.h"
14 #include "Store.h"
15 #include "StoreMeta.h"
16 #include "StoreMetaMD5.h"
17 #include "StoreMetaObjSize.h"
18 #include "StoreMetaSTD.h"
19 #include "StoreMetaSTDLFS.h"
20 #include "StoreMetaURL.h"
21 #include "StoreMetaVary.h"
22
23 bool
24 StoreMeta::validType(char type)
25 {
26 /* VOID is reserved, and new types have to be added as classes */
27 if (type <= STORE_META_VOID || type >= STORE_META_END + 10) {
28 debugs(20, DBG_CRITICAL, "storeSwapMetaUnpack: bad type (" << type << ")!");
29 return false;
30 }
31
32 /* Not yet implemented */
33 if (type >= STORE_META_END ||
34 type == STORE_META_STOREURL ||
35 type == STORE_META_VARY_ID) {
36 debugs(20, 3, "storeSwapMetaUnpack: Not yet implemented (" << type << ") in disk metadata");
37 return false;
38 }
39
40 /* Unused in any current squid code */
41 if (type == STORE_META_KEY_URL ||
42 type == STORE_META_KEY_SHA ||
43 type == STORE_META_HITMETERING ||
44 type == STORE_META_VALID) {
45 debugs(20, DBG_CRITICAL, "Obsolete and unused type (" << type << ") in disk metadata");
46 return false;
47 }
48
49 return true;
50 }
51
52 const int StoreMeta::MinimumTLVLength = 0;
53 const int StoreMeta::MaximumTLVLength = 1 << 16;
54
55 bool
56 StoreMeta::validLength(int aLength) const
57 {
58 static const Range<int> TlvValidLengths = Range<int>(StoreMeta::MinimumTLVLength, StoreMeta::MaximumTLVLength);
59 if (!TlvValidLengths.contains(aLength)) {
60 debugs(20, DBG_CRITICAL, MYNAME << ": insane length (" << aLength << ")!");
61 return false;
62 }
63
64 return true;
65 }
66
67 StoreMeta *
68 StoreMeta::Factory (char type, size_t len, void const *value)
69 {
70 if (!validType(type))
71 return NULL;
72
73 StoreMeta *result;
74
75 switch (type) {
76
77 case STORE_META_KEY:
78 result = new StoreMetaMD5;
79 break;
80
81 case STORE_META_URL:
82 result = new StoreMetaURL;
83 break;
84
85 case STORE_META_STD:
86 result = new StoreMetaSTD;
87 break;
88
89 case STORE_META_STD_LFS:
90 result = new StoreMetaSTDLFS;
91 break;
92
93 case STORE_META_OBJSIZE:
94 result = new StoreMetaObjSize;
95 break;
96
97 case STORE_META_VARY_HEADERS:
98 result = new StoreMetaVary;
99 break;
100
101 default:
102 debugs(20, DBG_CRITICAL, "Attempt to create unknown concrete StoreMeta");
103 return NULL;
104 }
105
106 if (!result->validLength(len)) {
107 delete result;
108 return NULL;
109 }
110
111 result->length = len;
112 result->value = xmalloc(len);
113 memcpy(result->value, value, len);
114 return result;
115 }
116
117 void
118 StoreMeta::FreeList(StoreMeta **head)
119 {
120 StoreMeta *node;
121
122 while ((node = *head) != NULL) {
123 *head = node->next;
124 xfree(node->value);
125 delete node;
126 }
127 }
128
129 StoreMeta **
130 StoreMeta::Add(StoreMeta **tail, StoreMeta *aNode)
131 {
132 assert (*tail == NULL);
133 *tail = aNode;
134 return &aNode->next; /* return new tail pointer */
135 }
136
137 bool
138 StoreMeta::checkConsistency(StoreEntry *) const
139 {
140 switch (getType()) {
141
142 case STORE_META_KEY:
143
144 case STORE_META_URL:
145
146 case STORE_META_VARY_HEADERS:
147 assert(0);
148 break;
149
150 case STORE_META_STD:
151 break;
152
153 case STORE_META_STD_LFS:
154 break;
155
156 case STORE_META_OBJSIZE:
157 break;
158
159 default:
160 debugs(20, DBG_IMPORTANT, "WARNING: got unused STORE_META type " << getType());
161 break;
162 }
163
164 return true;
165 }
166
167 StoreMeta::StoreMeta(const StoreMeta &s) :
168 length(s.length),
169 value(s.value),
170 next(s.next)
171 {}
172
173 StoreMeta& StoreMeta::operator=(const StoreMeta &s)
174 {
175 length=s.length;
176 value=s.value;
177 next=s.next;
178 return *this;
179 }
180