-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkvpair.c
52 lines (46 loc) · 1.48 KB
/
kvpair.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
#include "httpkit/http_retcode.h"
#include "kvpair.h"
#include <string.h>
int kvpair_vector_update(struct cvector* vec, const void* base, unsigned long koff, unsigned long klen,
unsigned long voff, unsigned long vlen) {
int ret;
unsigned long vec_size = cvector_size(vec);
for (unsigned long i = 0; i < vec_size; ++i) {
struct kvpair* item = (struct kvpair*)cvector_at(vec, i);
if (item->key.len != klen) {
continue;
}
if (memcmp((const char*)base + koff, (const char*)base + item->key.off, klen) != 0) {
continue;
}
item->value.off = voff;
item->value.len = vlen;
return HRC_OK;
}
struct kvpair new_item = {
.key = {
.off = koff,
.len = klen,
},
.value = {
.off = voff,
.len = vlen,
},
};
ret = cvector_push_back(vec, struct kvpair, new_item);
if (ret != 0) {
return HRC_NOMEM;
}
return HRC_OK;
}
struct kvpair* kvpair_vector_lookup(struct cvector* vec, const void* base, const char* key,
unsigned long klen) {
unsigned long vec_size = cvector_size(vec);
for (unsigned long i = 0; i < vec_size; ++i) {
struct kvpair* item = (struct kvpair*)cvector_at(vec, i);
if (item->key.len == klen && memcmp(key, (const char*)base + item->key.off, klen) == 0) {
return item;
}
}
return NULL;
}