-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataset.c
337 lines (288 loc) · 9.99 KB
/
dataset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
#include "dataset.h"
#include "fiftyone.h"
MAP_TYPE(ConfigBase)
#define CONFIG(d) ((ConfigBase*)d->config)
static StatusCode allocate(
DataSetBase **replacement,
size_t dataSetSize) {
*replacement = (DataSetBase*)Malloc(dataSetSize);
return *replacement == NULL ? INSUFFICIENT_MEMORY : SUCCESS;
}
static StatusCode replace(
ResourceManager *manager,
DataSetBase *replacement) {
// Switch the active data set for the new one.
ResourceReplace(manager, replacement, &replacement->handle);
if (replacement->handle == NULL) {
DataSetFree(replacement);
Free(replacement);
return INSUFFICIENT_MEMORY;
}
return SUCCESS;
}
static StatusCode initWithTempFile(
DataSetBase *dataSet,
long bytesToCompare) {
if (CONFIG(dataSet)->reuseTempFile == false ||
FileGetExistingTempFile(
dataSet->masterFileName,
CONFIG(dataSet)->tempDirs,
CONFIG(dataSet)->tempDirCount,
bytesToCompare,
dataSet->fileName) == false) {
return FileNewTempFile(
dataSet->masterFileName,
CONFIG(dataSet)->tempDirs,
CONFIG(dataSet)->tempDirCount,
(char *)dataSet->fileName,
sizeof(dataSet->fileName)/sizeof(dataSet->fileName[0]));
}
return SUCCESS;
}
void fiftyoneDegreesDataSetFree(fiftyoneDegreesDataSetBase *dataSet) {
// Free the memory used by the unique headers.
HeadersFree(dataSet->uniqueHeaders);
dataSet->uniqueHeaders = NULL;
// Free the override properties if any.
if (dataSet->overridable != NULL) {
fiftyoneDegreesOverridePropertiesFree(dataSet->overridable);
dataSet->overridable = NULL;
}
// Free the memory used by the available properties.
PropertiesFree(dataSet->available);
dataSet->available = NULL;
// Free the file handles and memory used by the reader.
FilePoolRelease(&dataSet->filePool);
// Free memory used to load the file into memory if still requires
// if used.
if (dataSet->memoryToFree != NULL) {
Free(dataSet->memoryToFree);
dataSet->memoryToFree = NULL;
}
// Delete the temp file if one was used.
if (CONFIG(dataSet)->useTempFile == true) {
FileDelete(dataSet->fileName);
}
}
void fiftyoneDegreesDataSetReset(fiftyoneDegreesDataSetBase *dataSet) {
FilePoolReset(&dataSet->filePool);
memset((char*)dataSet->fileName, 0, sizeof(dataSet->fileName));
memset((char*)dataSet->masterFileName, 0, sizeof(dataSet->masterFileName));
dataSet->memoryToFree = NULL;
dataSet->isInMemory = false;
dataSet->uniqueHeaders = NULL;
dataSet->available = NULL;
dataSet->overridable = NULL;
dataSet->config = NULL;
dataSet->handle = NULL;
}
fiftyoneDegreesStatusCode fiftyoneDegreesDataSetInitProperties(
fiftyoneDegreesDataSetBase *dataSet,
fiftyoneDegreesPropertiesRequired *properties,
void *state,
fiftyoneDegreesPropertiesGetMethod getPropertyMethod,
fiftyoneDegreesEvidencePropertiesGetMethod getEvidencePropertiesMethod) {
uint32_t i;
// Initialise the available properties.
dataSet->available = PropertiesCreate(
properties,
state,
getPropertyMethod,
getEvidencePropertiesMethod);
// Check the properties were initialised.
if (dataSet->available == NULL) {
return REQ_PROP_NOT_PRESENT;
}
// Check there are properties available for retrieval.
if (dataSet->available->count == 0) {
return REQ_PROP_NOT_PRESENT;
}
// Check that all property names were successfully retrieved from the
// data source.
for (i = 0; i < dataSet->available->count; i++) {
if (dataSet->available->items[i].name.data.ptr == NULL) {
return COLLECTION_FAILURE;
}
}
// Check that all the evidence properties were successfully retrived from
// the data source.
for (i = 0; i < dataSet->available->count; i++) {
if (dataSet->available->items[i].evidenceProperties == NULL) {
return INSUFFICIENT_MEMORY;
}
if (dataSet->available->items[i].evidenceProperties->capacity !=
dataSet->available->items[i].evidenceProperties->count) {
return COLLECTION_FAILURE;
}
}
return SUCCESS;
}
fiftyoneDegreesStatusCode fiftyoneDegreesDataSetInitHeaders(
fiftyoneDegreesDataSetBase *dataSet,
void *state,
fiftyoneDegreesHeadersGetMethod getHeaderMethod) {
// Initialise the unique HTTP headers.
dataSet->uniqueHeaders = HeadersCreate(
CONFIG(dataSet)->usesUpperPrefixedHeaders,
state,
getHeaderMethod);
// Check both the headers and properties were initialised.
if (dataSet->uniqueHeaders == NULL) {
return CORRUPT_DATA;
}
return SUCCESS;
}
fiftyoneDegreesStatusCode fiftyoneDegreesDataSetInitFromFile(
fiftyoneDegreesDataSetBase *dataSet,
const char *fileName,
long bytesToCompare) {
char *copiedString;
// Add 1 for the null terminator
size_t fileNameLength = strlen(fileName) + 1;
// Check there is sufficient space to store the filename provided.
if (fileNameLength > sizeof(dataSet->masterFileName) ||
fileNameLength > sizeof(dataSet->fileName)) {
return FILE_PATH_TOO_LONG;
}
#if defined(__linux__) && __GNUC__ >= 7
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-overflow"
// strncpy is called using the length computed on the string length
// adding 1 for null terminator. This is valid and should not cause
// overflow as we have properly checked the buffer size above. Thus
// suppress the warning here.
#endif
// Use the file name provided as the master data file for the data set.
copiedString = strncpy(
(char*)dataSet->masterFileName,
fileName,
sizeof(dataSet->masterFileName));
#if defined(__linux__) && __GNUC__ >= 7
#pragma GCC diagnostic pop
#endif
if (strncmp(fileName, copiedString, fileNameLength) != 0) {
return CORRUPT_DATA;
}
// If temporary files should be used to enable the master data file to
// be updated during a reload operation create or reuse a temporary
// file.
if (CONFIG(dataSet)->useTempFile == true) {
return initWithTempFile(dataSet, bytesToCompare);
}
// Temporary files are not requested so use the master file name
// as the working file name.
copiedString = strncpy(
(char*)dataSet->fileName,
dataSet->masterFileName,
fileNameLength);
if (strncmp(dataSet->masterFileName, copiedString, fileNameLength) != 0) {
return CORRUPT_DATA;
}
return SUCCESS;
}
fiftyoneDegreesStatusCode fiftyoneDegreesDataSetInitInMemory(
fiftyoneDegreesDataSetBase *dataSet,
fiftyoneDegreesMemoryReader *reader) {
// Read the file into memory checking that the operation completed.
StatusCode status = FileReadToByteArray(dataSet->fileName, reader);
if (status == SUCCESS) {
// Set the data set so that memory can be freed.
dataSet->memoryToFree = reader->current;
}
return status;
}
fiftyoneDegreesDataSetBase* fiftyoneDegreesDataSetGet(
fiftyoneDegreesResourceManager *manager) {
return (DataSetBase*)ResourceHandleIncUse(manager)->resource;
}
void fiftyoneDegreesDataSetRelease(fiftyoneDegreesDataSetBase *dataSet) {
ResourceHandleDecUse(dataSet->handle);
}
fiftyoneDegreesStatusCode fiftyoneDegreesDataSetReloadManagerFromMemory(
fiftyoneDegreesResourceManager *manager,
void *source,
long length,
size_t dataSetSize,
fiftyoneDegreesDataSetInitFromMemoryMethod initDataSet,
fiftyoneDegreesException *exception) {
DataSetBase *replacement = NULL;
const void *config;
PropertiesRequired properties = PropertiesDefault;
// Reference the properties and config from the existing data set in the
// replacement.
properties.existing = ((DataSetBase*)manager->active->resource)->available;
config = ((DataSetBase*)manager->active->resource)->config;
// Allocate memory for the replacement dataset.
StatusCode status = allocate(&replacement, dataSetSize);
if (status != SUCCESS) {
return status;
}
// Set the memory to free pointer to NULL to indicate that when this
// new data set is released the memory shouldn't be freed by 51Degrees but
// by the consumer of the service.
replacement->memoryToFree = NULL;
// Initialise the new data set with the data pointed to by source.
status = initDataSet(
replacement,
config,
&properties,
source,
length,
exception);
if (status != SUCCESS) {
Free(replacement);
return status;
}
return replace(manager, replacement);
}
fiftyoneDegreesStatusCode fiftyoneDegreesDataSetReloadManagerFromFile(
fiftyoneDegreesResourceManager* manager,
const char *fileName,
size_t dataSetSize,
fiftyoneDegreesDataSetInitFromFileMethod initDataSet,
fiftyoneDegreesException *exception) {
DataSetBase *replacement = NULL;
const void *config;
PropertiesRequired properties = PropertiesDefault;
// Reference the properties and config from the existing data set in the
// replacement.
properties.existing = ((DataSetBase*)manager->active->resource)->available;
config = ((DataSetBase*)manager->active->resource)->config;
// Allocate memory for the replacement dataset.
StatusCode status = allocate(&replacement, dataSetSize);
if (status != SUCCESS) {
return status;
}
// Initialise the new data set with the properties of the current one.
status = initDataSet(
replacement,
config,
&properties,
fileName,
exception);
if (status != SUCCESS) {
return status;
}
return replace(manager, replacement);
}