Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_h2_tbl.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2016 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Guillaume Quintard <guillaume.quintard@gmail.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include <stdint.h>
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <string.h>
34
35
#include "vdef.h"
36
37
#include "vas.h"
38
#include "vqueue.h"
39
40
#include "hpack.h"
41
#include "vtc_h2_priv.h"
42
43
/* TODO: fix that crazy workaround */
44
#define STAT_HDRS(i, k, v) \
45
        static char key_ ## i[] = k; \
46
        static char value_ ## i[] = v;
47
#include "vtc_h2_stattbl.h"
48
#undef STAT_HDRS
49
50
/*lint -save -e778 */
51
static const struct hpk_hdr sttbl[] = {
52
        {{NULL, 0, 0}, {NULL, 0, 0}, hpk_idx, 0},
53
#define STAT_HDRS(j, k, v) \
54
{ \
55
        .key = { \
56
                .ptr = key_ ## j, \
57
                .len = sizeof(k) - 1, \
58
                .huff = 0 \
59
        }, \
60
        .value = { \
61
                .ptr = value_ ## j, \
62
                .len = sizeof(v) - 1, \
63
                .huff = 0 \
64
        }, \
65
        .t = hpk_idx, \
66
        .i = j, \
67
},
68
#include "vtc_h2_stattbl.h"
69
#undef STAT_HDRS
70
};
71
/*lint -restore */
72
73
struct hpk_ctx {
74
        const struct hpk_hdr *sttbl;
75
        struct dynamic_table      dyntbl;
76
        uint32_t maxsize;
77
        uint32_t size;
78
};
79
80
81
struct hpk_iter *
82
HPK_NewIter(struct hpk_ctx *ctx, void *buf, int size)
83 1504
{
84 1504
        struct hpk_iter *iter = malloc(sizeof(*iter));
85 1504
        assert(iter);
86 1504
        assert(ctx);
87 1504
        assert(buf);
88 1504
        assert(size);
89 1504
        iter->ctx = ctx;
90 1504
        iter->orig = buf;
91 1504
        iter->buf = buf;
92 1504
        iter->end = iter->buf + size;
93 1504
        return (iter);
94 1504
}
95
96
void
97
HPK_FreeIter(struct hpk_iter *iter)
98 1504
{
99 1504
        free(iter);
100 1504
}
101
102
static void
103
pop_header(struct hpk_ctx *ctx)
104 40
{
105 40
        assert(!VTAILQ_EMPTY(&ctx->dyntbl));
106 40
        struct dynhdr *h = VTAILQ_LAST(&ctx->dyntbl, dynamic_table);
107 40
        VTAILQ_REMOVE(&ctx->dyntbl, h, list);
108 40
        ctx->size -= h->header.key.len + h->header.value.len + 32;
109 40
        free(h->header.key.ptr);
110 40
        free(h->header.value.ptr);
111 40
        free(h);
112 40
}
113
114
void
115
push_header (struct hpk_ctx *ctx, const struct hpk_hdr *oh)
116 40
{
117 40
        const struct hpk_hdr *ih;
118 40
        struct dynhdr *h;
119 40
        uint32_t len;
120
121 40
        assert(ctx->size <= ctx->maxsize);
122 40
        AN(oh);
123
124 40
        if (!ctx->maxsize)
125 0
                return;
126 40
        len = oh->value.len + 32;
127 40
        if (oh->key.ptr)
128 25
                len += oh->key.len;
129 15
        else {
130 15
                AN(oh->i);
131 15
                ih = HPK_GetHdr(ctx, oh->i);
132 15
                AN(ih);
133 15
                len += ih->key.len;
134 15
        }
135
136 50
        while (!VTAILQ_EMPTY(&ctx->dyntbl) && ctx->maxsize - ctx->size < len)
137 10
                pop_header(ctx);
138 40
        if (ctx->maxsize - ctx->size >= len) {
139 40
                h = malloc(sizeof(*h));
140 40
                AN(h);
141 40
                h->header.t = hpk_idx;
142
143 40
                if (oh->key.ptr) {
144 25
                        h->header.key.len = oh->key.len;
145 25
                        h->header.key.ptr = malloc(oh->key.len + 1L);
146 25
                        AN(h->header.key.ptr);
147 25
                        vmemcpy(h->header.key.ptr, oh->key.ptr,
148 25
                                oh->key.len + 1L);
149 25
                } else {
150 15
                        AN(oh->i);
151 15
                        ih = HPK_GetHdr(ctx, oh->i);
152 15
                        AN(ih);
153
154 15
                        h->header.key.len = ih->key.len;
155 15
                        h->header.key.ptr = malloc(ih->key.len + 1L);
156 15
                        AN(h->header.key.ptr);
157 15
                        vmemcpy(h->header.key.ptr, ih->key.ptr,
158 15
                                ih->key.len + 1L);
159 15
                }
160
161 40
                h->header.value.len = oh->value.len;
162 40
                h->header.value.ptr = malloc(oh->value.len + 1L);
163 40
                AN(h->header.value.ptr);
164 40
                vmemcpy(h->header.value.ptr, oh->value.ptr,
165 40
                        oh->value.len + 1L);
166
167 40
                VTAILQ_INSERT_HEAD(&ctx->dyntbl, h, list);
168 40
                ctx->size += len;
169 40
        }
170
171 40
}
172
173
enum hpk_result
174
HPK_ResizeTbl(struct hpk_ctx *ctx, uint32_t num)
175 10
{
176 10
        ctx->maxsize = num;
177 15
        while (!VTAILQ_EMPTY(&ctx->dyntbl) && ctx->maxsize < ctx->size)
178 5
                pop_header(ctx);
179 10
        return (hpk_done);
180 10
}
181
182
static const struct hpk_txt *
183
tbl_get_field(const struct hpk_ctx *ctx, uint32_t idx, int key)
184 887
{
185 887
        struct dynhdr *dh;
186 887
        assert(ctx);
187 887
        if (idx > 61 + ctx->size)
188 2
                return (NULL);
189 885
        else if (idx <= 61) {
190 865
                if (key)
191 739
                        return (&ctx->sttbl[idx].key);
192 126
                else
193 126
                        return (&ctx->sttbl[idx].value);
194 865
        }
195
196 20
        idx -= 62;
197 20
        VTAILQ_FOREACH(dh, &ctx->dyntbl, list)
198 46
                if (!idx--)
199 20
                        break;
200 20
        if (idx && dh) {
201 20
                if (key)
202 10
                        return (&dh->header.key);
203 10
                else
204 10
                        return (&dh->header.value);
205 20
        } else
206 0
                return (NULL);
207 20
}
208
209
const struct hpk_txt *
210
tbl_get_key(const struct hpk_ctx *ctx, uint32_t idx)
211 751
{
212 751
        return (tbl_get_field(ctx, idx, 1));
213 751
}
214
215
const struct hpk_txt *
216
tbl_get_value(const struct hpk_ctx *ctx, uint32_t idx)
217 136
{
218 136
        return (tbl_get_field(ctx, idx, 0));
219 136
}
220
221
const struct hpk_hdr *
222
HPK_GetHdr(const struct hpk_ctx *ctx, uint32_t idx)
223 126
{
224 126
        uint32_t oi = idx;
225 126
        struct dynhdr *dh;
226 126
        assert(ctx);
227 126
        if (idx > 61 + ctx->size)
228 0
                return (NULL);
229 126
        else if (idx <= 61)
230 30
                return (&ctx->sttbl[idx]);
231
232 96
        idx -= 62;
233 96
        VTAILQ_FOREACH(dh, &ctx->dyntbl, list)
234 188
                if (!idx--)
235 96
                        break;
236 96
        if (idx && dh) {
237 96
                dh->header.i = oi;
238 96
                return (&dh->header);
239 96
        } else
240 0
                return (NULL);
241 96
}
242
243
uint32_t
244
HPK_GetTblSize(const struct hpk_ctx *ctx)
245 26
{
246 26
        return (ctx->size);
247 26
}
248
249
uint32_t
250
HPK_GetTblMaxSize(const struct hpk_ctx *ctx)
251 0
{
252 0
        return (ctx->maxsize);
253 0
}
254
255
uint32_t
256
HPK_GetTblLength(const struct hpk_ctx *ctx)
257 1
{
258 1
        struct dynhdr *dh;
259 1
        uint32_t l = 0;
260 1
        VTAILQ_FOREACH(dh, &ctx->dyntbl, list)
261 1
                l++;
262 1
        return (l);
263 1
}
264
265
#if 0
266
void
267
dump_dyn_tbl(const struct hpk_ctx *ctx)
268
{
269
        int i = 0;
270
        struct dynhdr *dh;
271
        printf("DUMPING %u/%u\n", ctx->size, ctx->maxsize);
272
        VTAILQ_FOREACH(dh, &ctx->dyntbl, list) {
273
                printf(" (%d) %s: %s\n",
274
                    i++, dh->header.key.ptr, dh->header.value.ptr);
275
        }
276
        printf("DONE\n");
277
}
278
#endif
279
280
struct hpk_ctx *
281
HPK_NewCtx(uint32_t maxsize)
282 430
{
283 430
        struct hpk_ctx *ctx = calloc(1, sizeof(*ctx));
284 430
        assert(ctx);
285 430
        ctx->sttbl = sttbl;
286 430
        ctx->maxsize = maxsize;
287 430
        ctx->size = 0;
288 430
        return (ctx);
289 430
}
290
291
void
292
HPK_FreeCtx(struct hpk_ctx *ctx)
293 430
{
294
295 455
        while (!VTAILQ_EMPTY(&ctx->dyntbl))
296 25
                pop_header(ctx);
297 430
        free(ctx);
298 430
}