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