Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_gzip.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2019 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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 "config.h"
31
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "vtc.h"
37
#include "vtc_http.h"
38
#if defined(WITH_ZLIB)
39
#include <zlib.h>
40
#else
41
#include "vgz.h"
42
#endif
43
44
#ifdef VGZ_EXTENSIONS
45
static void
46
vtc_report_gz_bits(struct vtclog *vl, const z_stream *vz)
47 219
{
48 219
        vtc_log(vl, 4, "startbit = %ju %ju/%ju",
49 219
            (uintmax_t)vz->start_bit,
50 219
            (uintmax_t)vz->start_bit >> 3, (uintmax_t)vz->start_bit & 7);
51 219
        vtc_log(vl, 4, "lastbit = %ju %ju/%ju",
52 219
            (uintmax_t)vz->last_bit,
53 219
            (uintmax_t)vz->last_bit >> 3, (uintmax_t)vz->last_bit & 7);
54 219
        vtc_log(vl, 4, "stopbit = %ju %ju/%ju",
55 219
            (uintmax_t)vz->stop_bit,
56 219
            (uintmax_t)vz->stop_bit >> 3, (uintmax_t)vz->stop_bit & 7);
57 219
}
58
#endif
59
60
static size_t
61
APOS(ssize_t sz)
62 890
{
63 890
        assert(sz >= 0);
64 890
        return (sz);
65 890
}
66
67
/**********************************************************************
68
 * GUNZIPery
69
 */
70
71
static struct vsb *
72
vtc_gunzip_vsb(struct vtclog *vl, int fatal, const struct vsb *vin)
73 45
{
74 45
        z_stream vz;
75 45
        struct vsb *vout;
76 45
        int i;
77 45
        char buf[BUFSIZ];
78
79 45
        memset(&vz, 0, sizeof vz);
80 45
        vout = VSB_new_auto();
81 45
        AN(vout);
82
83 45
        vz.next_in = (void*)VSB_data(vin);
84 45
        vz.avail_in = APOS(VSB_len(vin));
85
86 45
        assert(Z_OK == inflateInit2(&vz, 31));
87
88 99
        do {
89 99
                vz.next_out = (void*)buf;
90 99
                vz.avail_out = sizeof buf;
91 99
                i = inflate(&vz, Z_FINISH);
92 99
                if (vz.avail_out != sizeof buf)
93 99
                        VSB_bcat(vout, buf, sizeof buf - vz.avail_out);
94 99
        } while (i == Z_OK || i == Z_BUF_ERROR);
95 45
        if (i != Z_STREAM_END)
96 0
                vtc_log(vl, fatal,
97 0
                    "Gunzip error = %d (%s) in:%jd out:%jd len:%zd",
98 0
                    i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out,
99 0
                    VSB_len(vin));
100 45
        AZ(VSB_finish(vout));
101 45
#ifdef VGZ_EXTENSIONS
102 45
        vtc_report_gz_bits(vl, &vz);
103 45
#endif
104 45
        assert(Z_OK == inflateEnd(&vz));
105 45
        return (vout);
106 45
}
107
108
void
109
vtc_gunzip(struct http *hp, char *body, long *bodylen)
110 45
{
111 45
        struct vsb *vin, *vout;
112
113 45
        AN(body);
114 45
        if (body[0] != (char)0x1f || body[1] != (char)0x8b)
115 0
                vtc_log(hp->vl, hp->fatal,
116 0
                    "Gunzip error: body lacks gzip magic");
117
118 45
        vin = VSB_new_auto();
119 45
        AN(vin);
120 45
        VSB_bcat(vin, body, *bodylen);
121 45
        AZ(VSB_finish(vin));
122 45
        vout = vtc_gunzip_vsb(hp->vl, hp->fatal, vin);
123 45
        VSB_destroy(&vin);
124
125 45
        vmemcpy(body, VSB_data(vout), APOS(VSB_len(vout) + 1));
126 45
        *bodylen = APOS(VSB_len(vout));
127 45
        VSB_destroy(&vout);
128 45
        vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen);
129 45
        vtc_dump(hp->vl, 4, "body", body, *bodylen);
130 45
        bprintf(hp->bodylen, "%ld", *bodylen);
131 45
}
132
133
/**********************************************************************
134
 * GZIPery
135
 */
136
137
static int
138
vtc_gzip_chunk(z_stream *vz, struct vsb *vout, const char *in, size_t inlen, int flush)
139 233
{
140 233
        int i;
141 233
        char buf[BUFSIZ];
142
143 233
        vz->next_in = TRUST_ME(in);
144 233
        vz->avail_in = APOS(inlen);
145 301
        do {
146 301
                vz->next_out = (void*)buf;
147 301
                vz->avail_out = sizeof buf;
148 301
                i = deflate(vz, flush);
149 301
                if (vz->avail_out != sizeof buf)
150 242
                        VSB_bcat(vout, buf, sizeof buf - vz->avail_out);
151 301
        } while (i == Z_OK || vz->avail_in > 0);
152 233
        vz->next_out = NULL;
153 233
        vz->avail_out = 0;
154 233
        vz->next_in = NULL;
155 233
        AZ(vz->avail_in);
156 233
        vz->avail_in = 0;
157 233
        return (i);
158 233
}
159
160
static void
161
vtc_gzip(struct http *hp, const char *input, char **body, long *bodylen, int fragment)
162 174
{
163 174
        struct vsb *vout;
164 174
        int i, res;
165 174
        size_t inlen = vstrlen(input);
166 174
        z_stream vz;
167
168 174
        memset(&vz, 0, sizeof vz);
169 174
        vout = VSB_new_auto();
170 174
        AN(vout);
171
172 174
        assert(Z_OK == deflateInit2(&vz,
173 174
            hp->gziplevel, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY));
174
175 233
        while (fragment && inlen > 3) {
176 59
                res = inlen / 3;
177 59
                i = vtc_gzip_chunk(&vz, vout, input, res, Z_BLOCK);
178 59
                if (i != Z_OK && i != Z_BUF_ERROR) {
179 0
                        vtc_log(hp->vl, hp->fatal,
180 0
                            "Gzip error = %d (%s) in:%jd out:%jd len:%zd",
181 0
                            i, vz.msg, (intmax_t)vz.total_in,
182 0
                            (intmax_t)vz.total_out, vstrlen(input));
183 0
                }
184 59
                input += res;
185 59
                inlen -= res;
186 59
        }
187
188 174
        i = vtc_gzip_chunk(&vz, vout, input, inlen, Z_FINISH);
189 174
        if (i != Z_STREAM_END) {
190 0
                vtc_log(hp->vl, hp->fatal,
191 0
                    "Gzip error = %d (%s) in:%jd out:%jd len:%zd",
192 0
                    i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out,
193 0
                    vstrlen(input));
194 0
        }
195 174
        AZ(VSB_finish(vout));
196 174
#ifdef VGZ_EXTENSIONS
197 174
        res = vz.stop_bit & 7;
198 174
        vtc_report_gz_bits(hp->vl, &vz);
199
#else
200
        res = 0;
201
#endif
202 174
        assert(Z_OK == deflateEnd(&vz));
203
204 174
#ifdef VGZ_EXTENSIONS
205 174
        if (hp->gzipresidual >= 0 && hp->gzipresidual != res)
206 0
                vtc_log(hp->vl, hp->fatal,
207 0
                    "Wrong gzip residual got %d wanted %d",
208 0
                    res, hp->gzipresidual);
209 174
#endif
210 174
        *body = malloc(APOS(VSB_len(vout) + 1));
211 174
        AN(*body);
212 174
        vmemcpy(*body, VSB_data(vout), APOS(VSB_len(vout) + 1));
213 174
        *bodylen = APOS(VSB_len(vout));
214 174
        VSB_destroy(&vout);
215 174
        vtc_log(hp->vl, 3, "new bodylen %ld", *bodylen);
216 174
        vtc_dump(hp->vl, 4, "body", *body, *bodylen);
217 174
        bprintf(hp->bodylen, "%ld", *bodylen);
218 174
}
219
220
int
221
vtc_gzip_cmd(struct http *hp, char * const *av, char **body, long *bodylen)
222 192
{
223 192
        char *b;
224
225 192
        AN(hp);
226 192
        AN(av);
227 192
        AN(body);
228 192
        AN(bodylen);
229
230 192
        if (!vstrcmp(*av, "-gzipresidual")) {
231 8
                hp->gzipresidual = strtoul(av[1], NULL, 0);
232 8
                return (1);
233 8
        }
234 184
        if (!vstrcmp(*av, "-gziplevel")) {
235 10
                hp->gziplevel = strtoul(av[1], NULL, 0);
236 10
                return (1);
237 10
        }
238 174
        if (!vstrcmp(*av, "-gzipbody")) {
239 169
                if (*body != NULL)
240 166
                        free(*body);
241 169
                *body = NULL;
242 169
                vtc_gzip(hp, av[1], body, bodylen, 0);
243 169
                AN(*body);
244 169
                return (2);
245 169
        }
246 5
        if (!vstrcmp(*av, "-gziplen")) {
247 5
                if (*body != NULL)
248 4
                        free(*body);
249 5
                *body = NULL;
250 5
                b = synth_body(av[1], 1);
251 5
                vtc_gzip(hp, b, body, bodylen, 1);
252 5
                AN(*body);
253 5
                free(b);
254 5
                return (2);
255 5
        }
256 0
        return (0);
257 5
}