forked from 51Degrees/common-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverrides.c
449 lines (402 loc) · 12.6 KB
/
overrides.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
*
* 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 "overrides.h"
#include "fiftyone.h"
MAP_TYPE(Collection)
typedef struct override_profile_ids_t {
fiftyoneDegreesOverrideProfileIdMethod override; /* Method to call when
a profile ids is found */
void *state; /* State to pass to the override method */
} overrideProfileIds;
typedef struct add_state_t {
OverridePropertyArray *properties; /* Properties available to be
overridden */
OverrideValueArray *values; /* Values from evidence to use when fetching
overridden properties */
} addState;
#ifdef _MSC_VER
#pragma warning (disable:4100)
#endif
static void collectionRelease(Item *item) {
// Do Nothing.
}
#ifdef _MSC_VER
#pragma warning (default:4100)
#endif
/* Prefix to use when comparing property names. */
#define OVERRIDE_PREFIX "51D_"
static const Collection dummyCollection = {
NULL,
collectionRelease,
NULL,
NULL,
NULL,
0,
0,
0 };
/**
* Checks if the string starts with the prefix string.
* @return true if the prefix is present, otherwise false.
*/
static bool startsWith(String *string, String *prefix) {
int16_t i = 0;
// Check that the string is longer than the prefix. Also prevents the
// prefix and the string being the same string.
if (string->size > prefix->size) {
// Loop checking characters until one doesn't match or all the prefix
// characters are matched.
while (i < prefix->size - 1) {
if ((&string->value)[i] != (&prefix->value)[i]) {
return false;
}
i++;
}
return true;
}
return false;
}
/**
* If there is an expected prefix to be ignored then check for it in the
* property name and skip those characters if present.
* @param prefix true if there is a prefix to be skipped
* @param field the name of the field provided
* @return a pointer to the start of the field name for checking.
*/
static const char* skipPrefix(bool prefixed, const char *field) {
char *result = (char*)field;
if (prefixed == true && strlen(result) > sizeof(OVERRIDE_PREFIX) &&
StringCompareLength(
field,
OVERRIDE_PREFIX,
sizeof(OVERRIDE_PREFIX) - 1) == 0) {
result += sizeof(OVERRIDE_PREFIX) - 1;
}
return result;
}
static int getRequiredPropertyIndexFromName(
OverridePropertyArray *properties,
const char *name) {
uint32_t i;
const char *current;
OverrideProperty *property;
// Skip the field name prefix.
name = (const char*)skipPrefix(properties->prefix, name);
// Search for the property name in the array of properties that can support
// being overridden.
for (i = 0; i < properties->count; i++) {
property = &properties->items[i];
current = STRING(property->available->name.data.ptr);
if (StringCompare((char*)current, (char*)name) == 0) {
return property->requiredPropertyIndex;
}
}
return -1;
}
bool fiftyoneDegreesOverridesAdd(
fiftyoneDegreesOverrideValueArray *values,
int requiredPropertyIndex,
const char *value) {
uint32_t currentOverrideIndex = 0;
size_t length;
String *copy;
OverrideValue *override;
if (requiredPropertyIndex >= 0 && values->count < values->capacity) {
// Set the override either as a new item, or override an existing
// one if there is already one with the same required property
// index.
while (currentOverrideIndex < values->count &&
(int)values->items[currentOverrideIndex].requiredPropertyIndex !=
requiredPropertyIndex) {
currentOverrideIndex++;
}
override = &values->items[currentOverrideIndex];
if (currentOverrideIndex == values->count) {
// Increment the override count and set the required property
// index.
values->count++;
override->requiredPropertyIndex = requiredPropertyIndex;
}
// Ensure there is sufficient memory for the string being copied.
length = strlen(value);
copy = (String*)fiftyoneDegreesDataMalloc(
&override->string,
sizeof(String) + length);
// Copy the string from the evidence pair to the override data
// item.
memcpy(©->value, value, length + 1);
copy->size = (int16_t)(length + 1);
}
return values->count < values->capacity;
}
static bool addOverrideToResults(void *state, EvidenceKeyValuePair *pair) {
addState *add = (addState*)state;
// Find the required property index, if any for the field.
int requiredPropertyIndex = getRequiredPropertyIndexFromName(
add->properties,
pair->field);
return fiftyoneDegreesOverridesAdd(
add->values,
requiredPropertyIndex,
(const char*)pair->parsedValue);
}
static uint32_t countOverridableProperties(
PropertiesAvailable *available,
void *state,
fiftyoneDegreesOverridesFilterMethod filter) {
uint32_t i, count = 0;
for (i = 0; i < available->count; i++) {
if (filter(state, i) == true) {
count++;
}
}
return count;
}
static uint32_t addOverridableProperties(
PropertiesAvailable *available,
OverridePropertyArray *properties,
void *state,
fiftyoneDegreesOverridesFilterMethod filter) {
uint32_t i, count = 0;
OverrideProperty *property;
for (i = 0; i < available->count; i++) {
if (filter(state, i) == true) {
property = &properties->items[properties->count++];
property->available = &available->items[i];
property->requiredPropertyIndex = i;
}
}
return count;
}
fiftyoneDegreesOverrideValueArray* fiftyoneDegreesOverrideValuesCreate(
uint32_t capacity) {
uint32_t i;
OverrideValue *item;
fiftyoneDegreesOverrideValueArray* overrides;
FIFTYONE_DEGREES_ARRAY_CREATE(OverrideValue, overrides, capacity);
if (overrides != NULL) {
for (i = 0; i < capacity; i++) {
item = &overrides->items[i];
item->requiredPropertyIndex = 0;
DataReset(&item->string);
}
}
return overrides;
}
fiftyoneDegreesOverridePropertyArray* fiftyoneDegreesOverridePropertiesCreate(
fiftyoneDegreesPropertiesAvailable *available,
bool prefix,
void *state,
fiftyoneDegreesOverridesFilterMethod filter) {
OverridePropertyArray *properties = NULL;
uint32_t count = countOverridableProperties(available, state, filter);
if (count > 0) {
FIFTYONE_DEGREES_ARRAY_CREATE(OverrideProperty, properties, count);
if (properties != NULL) {
properties->prefix = prefix;
addOverridableProperties(available, properties, state, filter);
}
}
return properties;
}
void fiftyoneDegreesOverridePropertiesFree(
fiftyoneDegreesOverridePropertyArray *properties) {
Free(properties);
}
uint32_t fiftyoneDegreesOverridesExtractFromEvidence(
fiftyoneDegreesOverridePropertyArray *properties,
fiftyoneDegreesOverrideValueArray *values,
fiftyoneDegreesEvidenceKeyValuePairArray *evidence) {
addState state;
uint32_t count = 0;
if (properties != NULL && values != NULL) {
state.values = values;
state.properties = properties;
count = EvidenceIterate(
evidence,
FIFTYONE_DEGREES_EVIDENCE_COOKIE |
FIFTYONE_DEGREES_EVIDENCE_QUERY,
&state,
addOverrideToResults);
}
return count;
}
fiftyoneDegreesString* fiftyoneDegreesOverrideValuesGetFirst(
fiftyoneDegreesOverrideValueArray *values,
uint32_t requiredPropertyIndex,
fiftyoneDegreesCollectionItem *item) {
uint32_t i;
if (values != NULL) {
for (i = 0; i < values->count; i++) {
if (values->items[i].requiredPropertyIndex ==
requiredPropertyIndex) {
item->collection = (Collection*)&dummyCollection;
// Copy the pointer to the string in the values array. Set
// allocated and used to false as the memory must not be
// freed until the results the overrides are part of are
// freed.
item->data.ptr = values->items[i].string.ptr;
item->data.allocated = 0;
item->data.used = 0;
return (String*)item->data.ptr;
}
}
}
return NULL;
}
bool fiftyoneDegreesOverrideHasValueForRequiredPropertyIndex(
fiftyoneDegreesOverrideValueArray *values,
uint32_t requiredPropertyIndex) {
uint32_t i;
OverrideValue *current;
if (values != NULL) {
// Loop through the values available from the overrides checking they
// match the required property index.
for (i = 0; i < values->count; i++) {
current = &values->items[i];
if (current->requiredPropertyIndex == requiredPropertyIndex) {
return true;
}
}
}
return false;
}
uint32_t fiftyoneDegreesOverrideValuesAdd(
fiftyoneDegreesOverrideValueArray *values,
uint32_t requiredPropertyIndex,
fiftyoneDegreesList *list) {
uint32_t i, count = 0;
OverrideValue *current;
Item valueItem;
if (values != NULL) {
// Use a dummy collection so that the call to release will work
// if the client respects the collection pattern.
valueItem.collection = (Collection*)&dummyCollection;
valueItem.handle = NULL;
// Loop through the values available from the overrides checking they
// match the required property index.
for (i = 0; i < values->count && list->count < list->capacity; i++) {
current = &values->items[i];
if (current->requiredPropertyIndex == requiredPropertyIndex) {
// Copy the pointer to the string in the values array. Set
// allocated and used to false as the memory must not be
// freed until the results the overrides are part of are
// freed.
valueItem.data.ptr = current->string.ptr;
valueItem.data.allocated = 0;
valueItem.data.used = 0;
// Add the collection item to the list of values.
ListAdd(list, &valueItem);
count++;
}
}
}
return count;
}
int fiftyoneDegreesOverridesGetOverridingRequiredPropertyIndex(
fiftyoneDegreesPropertiesAvailable *available,
uint32_t requiredPropertyIndex) {
String *compare, *propertyName;
uint32_t i;
propertyName = (String*)available->items[
requiredPropertyIndex].name.data.ptr;
for (i = 0; i < available->count; i++) {
if (i != requiredPropertyIndex) {
compare = (String*)available->items[i].name.data.ptr;
if (startsWith(compare, propertyName)) {
return i;
}
}
}
return -1;
}
void fiftyoneDegreesOverrideValuesFree(
fiftyoneDegreesOverrideValueArray *overrides) {
uint32_t i;
OverrideValue *item;
if (overrides != NULL) {
for (i = 0; i < overrides->capacity; i++) {
item = &overrides->items[i];
if (item->string.ptr != NULL && item->string.allocated > 0) {
Free(item->string.ptr);
}
}
Free(overrides);
overrides = NULL;
}
}
void fiftyoneDegreesOverrideValuesReset(
fiftyoneDegreesOverrideValueArray *overrides) {
uint32_t i;
OverrideValue *item;
if (overrides != NULL) {
for (i = 0; i < overrides->capacity; i++) {
item = &overrides->items[i];
if (item->string.ptr != NULL && item->string.allocated > 0) {
memset(item->string.ptr, 0, item->string.allocated);
item->string.used = 0;
item->requiredPropertyIndex = 0;
}
}
overrides->count = 0;
}
}
static void extractProfileId(char *value, overrideProfileIds *state) {
if (*value >= 0 && isdigit(*value) != 0) {
int profileId = atoi(value);
if (profileId >= 0) {
state->override(state->state, profileId);
}
}
}
static void extractProfileIds(overrideProfileIds *state, char *value) {
char *current = value, *previous = value;
while (*current != '\0') {
if (*current < 0 || isdigit(*current) == 0) {
extractProfileId(previous, state);
previous = current + 1;
}
current++;
}
extractProfileId(previous, state);
}
static bool iteratorProfileId(void *state, EvidenceKeyValuePair *pair) {
if (StringCompare(
skipPrefix(true, (char*)pair->field),
"ProfileIds") == 0) {
extractProfileIds((overrideProfileIds*)state, (char*)pair->parsedValue);
}
return true;
}
void fiftyoneDegreesOverrideProfileIds(
fiftyoneDegreesEvidenceKeyValuePairArray *evidence,
void *state,
fiftyoneDegreesOverrideProfileIdMethod override) {
overrideProfileIds callback;
callback.state = state;
callback.override = override;
EvidenceIterate(
evidence,
FIFTYONE_DEGREES_EVIDENCE_COOKIE |
FIFTYONE_DEGREES_EVIDENCE_QUERY,
&callback,
iteratorProfileId);
}