summaryrefslogtreecommitdiffhomepage
path: root/src/http/v3/ngx_http_v3.c
blob: e804cf6f5d59ebd7b589990ec6d29a8be989619a (plain) (blame)
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

/*
 * Copyright (C) Roman Arutyunyan
 * Copyright (C) Nginx, Inc.
 */


#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>


uintptr_t
ngx_http_v3_encode_varlen_int(u_char *p, uint64_t value)
{
    if (value <= 63) {
        if (p == NULL) {
            return 1;
        }

        *p++ = value;
        return (uintptr_t) p;
    }

    if (value <= 16383) {
        if (p == NULL) {
            return 2;
        }

        *p++ = 0x40 | (value >> 8);
        *p++ = value;
        return (uintptr_t) p;
    }

    if (value <= 1073741823) {
        if (p == NULL) {
            return 3;
        }

        *p++ = 0x80 | (value >> 16);
        *p++ = (value >> 8);
        *p++ = value;
        return (uintptr_t) p;

    }

    if (p == NULL) {
        return 4;
    }

    *p++ = 0xc0 | (value >> 24);
    *p++ = (value >> 16);
    *p++ = (value >> 8);
    *p++ = value;
    return (uintptr_t) p;
}


uintptr_t
ngx_http_v3_encode_prefix_int(u_char *p, uint64_t value, ngx_uint_t prefix)
{
    ngx_uint_t  thresh, n;

    thresh = (1 << prefix) - 1;

    if (value < thresh) {
        if (p == NULL) {
            return 1;
        }

        *p++ |= value;
        return (uintptr_t) p;
    }

    value -= thresh;

    for (n = 10; n > 1; n--) {
        if (value >> (7 * (n - 1))) {
            break;
        }
    }

    if (p == NULL) {
        return n + 1;
    }

    *p++ |= thresh;

    for ( /* void */ ; n > 1; n--) {
        *p++ = 0x80 | (value >> 7 * (n - 1));
    }

    *p++ = value & 0x7f;

    return (uintptr_t) p;
}


uint64_t
ngx_http_v3_decode_varlen_int(u_char *p)
{
    uint64_t    value;
    ngx_uint_t  len;

    len = *p >> 6;
    value = *p & 0x3f;

    while (len--) {
        value = (value << 8) + *p++;
    }

    return value;
}


int64_t
ngx_http_v3_decode_prefix_int(u_char **src, size_t len, ngx_uint_t prefix)
{
    u_char   *p;
    int64_t   value, thresh;

    if (len == 0) {
        return NGX_ERROR;
    }

    p = *src;

    thresh = (1 << prefix) - 1;
    value = *p++ & thresh;

    if (value != thresh) {
        *src = p;
        return value;
    }

    value = 0;

    /* XXX handle overflows */

    while (--len) {
        value = (value << 7) + (*p & 0x7f);
        if ((*p++ & 0x80) == 0) {
            *src = p;
            return value + thresh;
        }
    }

    return NGX_ERROR;
}


ngx_int_t
ngx_http_v3_decode_huffman(ngx_connection_t *c, ngx_str_t *s)
{
    u_char  state, *p, *data;

    state = 0;

    p = ngx_pnalloc(c->pool, s->len * 8 / 5);
    if (p == NULL) {
        return NGX_ERROR;
    }

    data = p;

    if (ngx_http_v2_huff_decode(&state, s->data, s->len, &p, 1, c->log)
        != NGX_OK)
    {
        return NGX_ERROR;
    }

    s->len = p - data;
    s->data = data;

    return NGX_OK;
}