-
Notifications
You must be signed in to change notification settings - Fork 6
/
evidence.c
178 lines (165 loc) · 5.52 KB
/
evidence.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
/* *********************************************************************
* 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 "evidence.h"
#include "fiftyone.h"
typedef struct evidence_iterate_state_t {
fiftyoneDegreesEvidenceKeyValuePairArray *evidence;
EvidencePrefix prefix;
void *state;
fiftyoneDegreesEvidenceIterateMethod callback;
} evidenceIterateState;
static EvidencePrefixMap _map[] = {
{ "server.", sizeof("server.") - 1, FIFTYONE_DEGREES_EVIDENCE_SERVER },
{ "header.", sizeof("header.") - 1, FIFTYONE_DEGREES_EVIDENCE_HTTP_HEADER_STRING },
{ "query.", sizeof("query.") - 1, FIFTYONE_DEGREES_EVIDENCE_QUERY },
{ "cookie.", sizeof("cookie.") - 1, FIFTYONE_DEGREES_EVIDENCE_COOKIE }
};
static void parsePair(EvidenceKeyValuePair *pair) {
switch (pair->prefix) {
case FIFTYONE_DEGREES_EVIDENCE_HTTP_HEADER_IP_ADDRESSES:
case FIFTYONE_DEGREES_EVIDENCE_HTTP_HEADER_STRING:
case FIFTYONE_DEGREES_EVIDENCE_SERVER:
case FIFTYONE_DEGREES_EVIDENCE_QUERY:
case FIFTYONE_DEGREES_EVIDENCE_COOKIE:
default:
pair->parsedValue = pair->originalValue;
break;
}
}
fiftyoneDegreesEvidenceKeyValuePairArray*
fiftyoneDegreesEvidenceCreate(uint32_t capacity) {
EvidenceKeyValuePairArray *evidence;
uint32_t i;
FIFTYONE_DEGREES_ARRAY_CREATE(EvidenceKeyValuePair, evidence, capacity);
if (evidence != NULL) {
for (i = 0; i < evidence->capacity; i++) {
evidence->items[i].field = NULL;
evidence->items[i].originalValue = NULL;
evidence->items[i].parsedValue = NULL;
evidence->items[i].prefix = FIFTYONE_DEGREES_EVIDENCE_IGNORE;
}
evidence->pseudoEvidence = NULL;
}
return evidence;
}
void fiftyoneDegreesEvidenceFree(
fiftyoneDegreesEvidenceKeyValuePairArray *evidence) {
Free(evidence);
}
fiftyoneDegreesEvidenceKeyValuePair* fiftyoneDegreesEvidenceAddString(
fiftyoneDegreesEvidenceKeyValuePairArray *evidence,
fiftyoneDegreesEvidencePrefix prefix,
const char *field,
const char *originalValue) {
EvidenceKeyValuePair *pair = NULL;
if (evidence->count < evidence->capacity) {
pair = &evidence->items[evidence->count++];
pair->prefix = prefix;
pair->field = field;
pair->originalValue = (void*)originalValue;
pair->parsedValue = NULL;
}
return pair;
}
/*
* Iterate through an evidence collection and perform callback on the evidence
* whose prefix matches the input prefixes.
*
* @param evidence the evidence collection to process
* @param prefixes the accepted evidence prefixes
* @param state the state object to hold the current state of the process
* @param cont indicate whether the iteration should continue. This normally
* indicate if error has occured. Upon return, this value is also updated so
* that caller know whether to continue processing any other member set of
* the evidence collection.
* @param callback the method to call back when a matched evidence is found.
* @return number of evidence processed.
*/
static uint32_t evidenceIterateSub(
fiftyoneDegreesEvidenceKeyValuePairArray* evidence,
int prefixes,
void* state,
bool* cont,
fiftyoneDegreesEvidenceIterateMethod callback) {
uint32_t i = 0, count = 0;
EvidenceKeyValuePair* pair;
while (*cont == true && i < evidence->count) {
pair = &evidence->items[i++];
if ((pair->prefix & prefixes) == pair->prefix) {
if (pair->parsedValue == NULL) {
parsePair(pair);
}
*cont = callback(state, pair);
count++;
}
}
return count;
}
uint32_t fiftyoneDegreesEvidenceIterate(
fiftyoneDegreesEvidenceKeyValuePairArray *evidence,
int prefixes,
void *state,
fiftyoneDegreesEvidenceIterateMethod callback) {
bool cont = true;
uint32_t count = evidenceIterateSub(
evidence,
prefixes,
state,
&cont,
callback);
// Continue if there is a secondary evidence
if (cont == true && evidence->pseudoEvidence != NULL) {
count += evidenceIterateSub(
evidence->pseudoEvidence,
prefixes,
state,
&cont,
callback);
}
return count;
}
fiftyoneDegreesEvidencePrefixMap* fiftyoneDegreesEvidenceMapPrefix(
const char *key) {
uint32_t i;
size_t length = strlen(key);
EvidencePrefixMap *map;
for (i = 0; i < sizeof(_map) / sizeof(EvidencePrefixMap); i++) {
map = &_map[i];
if (map->prefixLength < length &&
strncmp(map->prefix, key, map->prefixLength) == 0) {
return map;
}
}
return NULL;
}
EXTERNAL const char* fiftyoneDegreesEvidencePrefixString(
fiftyoneDegreesEvidencePrefix prefix) {
uint32_t i;
EvidencePrefixMap* map;
for (i = 0; i < sizeof(_map) / sizeof(EvidencePrefixMap); i++) {
map = &_map[i];
if (map->prefixEnum == prefix) {
return map->prefix;
}
}
return NULL;
}