1/* Bug report and test case contributed by Guoye Zhang -- thanks! */
2
3#include "lsxpack_header.h"
4#include "lsqpack.h"
5
6static void _decoderUnblocked(void *context) {
7
8}
9
10static struct lsxpack_header *_decoderPrepareDecode(void *context, struct lsxpack_header *header, size_t space) {
11
12    return NULL;
13
14}
15
16static int _decoderProcessHeader(void *context, struct lsxpack_header *header) {
17
18    return 0;
19
20}
21
22int main(int argc, const char * argv[]) {
23
24    struct lsqpack_enc qpackEncoder;
25
26    struct lsqpack_dec qpackDecoder;
27
28    static struct lsqpack_dec_hset_if callbacks = {
29
30        .dhi_unblocked = _decoderUnblocked,
31
32        .dhi_prepare_decode = _decoderPrepareDecode,
33
34        .dhi_process_header = _decoderProcessHeader,
35
36    };
37
38    uint8_t stdc[LSQPACK_LONGEST_SDTC];
39
40    size_t stdcSize = sizeof(stdc);
41
42    lsqpack_enc_init(&qpackEncoder, NULL, 4096, 4096, 100, LSQPACK_ENC_OPT_SERVER, stdc, &stdcSize);
43
44    lsqpack_dec_init(&qpackDecoder, stdout, 4096, 100, &callbacks, (enum lsqpack_dec_opts)0);
45
46    lsqpack_dec_enc_in(&qpackDecoder, stdc, stdcSize);
47
48    uint8_t iciBuffer[LSQPACK_LONGEST_ICI];
49
50    ssize_t iciSize = 0;
51
52    for (unsigned i = 0; i < 6; i++) {
53
54        uint8_t encoderBuffer[1024];
55
56        size_t usedEncoderSize = 0, encoderSize = sizeof(encoderBuffer);
57
58        uint8_t headerBuffer[1024];
59
60        size_t usedHeaderSize = 0, headerSize = sizeof(headerBuffer);
61
62        struct lsxpack_header header;
63        memset(&header, 0, sizeof(header));
64
65        const char *name, *value;
66
67        lsqpack_enc_start_header(&qpackEncoder, 0, i);
68
69        name = ":authority" "localhost";
70
71        lsxpack_header_set_offset2(&header, name, 0, strlen(":authority"), strlen(":authority"), strlen("localhost"));
72
73        enum lsqpack_enc_status status = lsqpack_enc_encode(&qpackEncoder, &encoderBuffer[usedEncoderSize], &encoderSize, &headerBuffer[usedHeaderSize], &headerSize, &header, (enum lsqpack_enc_flags)0);
74
75        assert(status == LQES_OK);
76
77        usedEncoderSize += encoderSize; usedHeaderSize += headerSize;
78
79        encoderSize = sizeof(encoderBuffer) - usedEncoderSize; headerSize = sizeof(headerBuffer) - usedHeaderSize;
80
81        uint8_t prefixBuffer[22];
82
83        lsqpack_enc_end_header(&qpackEncoder, prefixBuffer, sizeof(prefixBuffer), NULL);
84
85        if (usedEncoderSize > 0) {
86
87            lsqpack_dec_enc_in(&qpackDecoder, encoderBuffer, usedEncoderSize);
88
89            if (lsqpack_dec_ici_pending(&qpackDecoder)) {
90
91                iciSize = lsqpack_dec_write_ici(&qpackDecoder, iciBuffer, sizeof(iciBuffer));
92
93            }
94
95        }
96
97    }
98
99    lsqpack_enc_decoder_in(&qpackEncoder, iciBuffer, iciSize);
100
101    return 0;
102
103}
104
105