vinyl-cache/bin/vinyld/cache/cache_gzip.c
0
/*-
1
 * Copyright (c) 2013-2015 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
 * Interaction with the libvgz (zlib) library.
30
 *
31
 * The zlib library pollutes namespace a LOT when you include the "vgz.h"
32
 * (aka "zlib.h") file so we contain the damage by vectoring all access
33
 * to libz through this source file.
34
 *
35
 * The API defined by this file, will also insulate the rest of the code,
36
 * should we find a better gzip library at a later date.
37
 *
38
 */
39
40
#include "config.h"
41
42
#include <stdlib.h>
43
44
#include "cache_int.h"
45
#include "cache_filter.h"
46
#include "cache_objhead.h"
47
#include "cache_vgz.h"
48
49
#include "storage/storage.h"
50
51
#include "vend.h"
52
53
#include "vgz.h"
54
55
struct vgz {
56
        unsigned                magic;
57
#define VGZ_MAGIC               0x162df0cb
58
        enum {VGZ_GZ,VGZ_UN}    dir;
59
        struct vsl_log          *vsl;
60
        const char              *id;
61
        int                     last_i;
62
        enum vgz_flag           flag;
63
64
        struct stv_buffer       *stvbuf;
65
        char                    *m_buf;
66
        ssize_t                 m_sz;
67
        ssize_t                 m_len;
68
69
        intmax_t                bits;
70
71
        z_stream                vz;
72
};
73
74
static const char *
75 24
vgz_msg(const struct vgz *vg)
76
{
77 24
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
78 24
        return (vg->vz.msg ? vg->vz.msg : "(null)");
79
}
80
81
/*--------------------------------------------------------------------
82
 * Set up a gunzip instance
83
 */
84
85
static struct vgz *
86 1024
vgz_gunzip(struct vsl_log *vsl, const char *id)
87
{
88
        struct vgz *vg;
89
90 1024
        ALLOC_OBJ(vg, VGZ_MAGIC);
91 1024
        AN(vg);
92 1024
        vg->vsl = vsl;
93 1024
        vg->id = id;
94 1024
        vg->dir = VGZ_UN;
95
96
        /*
97
         * Max memory usage according to zonf.h:
98
         *      mem_needed = "a few kb" + (1 << (windowBits))
99
         * Since we don't control windowBits, we have to assume
100
         * it is 15, so 34-35KB or so.
101
         */
102 1024
        assert(Z_OK == inflateInit2(&vg->vz, 31));
103 1024
        return (vg);
104
}
105
106
static struct vgz *
107 900
VGZ_NewGunzip(struct vsl_log *vsl, const char *id)
108
{
109 900
        VSC_C_main->n_gunzip++;
110 900
        return (vgz_gunzip(vsl, id));
111
}
112
113
static struct vgz *
114 124
VGZ_NewTestGunzip(struct vsl_log *vsl, const char *id)
115
{
116 124
        VSC_C_main->n_test_gunzip++;
117 124
        return (vgz_gunzip(vsl, id));
118
}
119
120
struct vgz *
121 368
VGZ_NewGzip(struct vsl_log *vsl, const char *id)
122
{
123
        struct vgz *vg;
124
        int i;
125
126 368
        VSC_C_main->n_gzip++;
127 368
        ALLOC_OBJ(vg, VGZ_MAGIC);
128 368
        AN(vg);
129 368
        vg->vsl = vsl;
130 368
        vg->id = id;
131 368
        vg->dir = VGZ_GZ;
132
133
        /*
134
         * From zconf.h:
135
         *
136
         *      mem_needed = "a few kb"
137
         *              + (1 << (windowBits+2))
138
         *              +  (1 << (memLevel+9))
139
         *
140
         * windowBits [8..15] (-> 1K..128K)
141
         * memLevel [1..9] (-> 1K->256K)
142
         */
143 368
        i = deflateInit2(&vg->vz,
144
            cache_param->gzip_level,            /* Level */
145
            Z_DEFLATED,                         /* Method */
146
            16 + 15,                            /* Window bits (16=gzip) */
147
            cache_param->gzip_memlevel,         /* memLevel */
148
            Z_DEFAULT_STRATEGY);
149 368
        assert(Z_OK == i);
150 368
        return (vg);
151
}
152
153
/*--------------------------------------------------------------------
154
 */
155
156
static int
157 1088
vgz_getmbuf(struct worker *wrk, struct vgz *vg)
158
{
159
        size_t sz;
160
161 1088
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
162 1088
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
163 1088
        AZ(vg->m_sz);
164 1088
        AZ(vg->m_len);
165 1088
        AZ(vg->m_buf);
166 1088
        AZ(vg->stvbuf);
167
168 1088
        vg->stvbuf = STV_AllocBuf(wrk, stv_transient, cache_param->gzip_buffer);
169 1088
        if (vg->stvbuf == NULL) {
170 0
                VSLb(vg->vsl, SLT_Gzip, "G(un)zip error:"
171
                    "failed to allocate gzip_buffer from transient stevedore");
172 0
                return (-1);
173
        }
174 1088
        vg->m_buf = STV_GetBufPtr(vg->stvbuf, &sz);
175 1088
        vg->m_sz = sz;
176 1088
        AN(vg->m_buf);
177 1088
        assert(vg->m_sz > 0);
178 1088
        return (0);
179 1088
}
180
181
/*--------------------------------------------------------------------*/
182
183
void
184 6740
VGZ_Ibuf(struct vgz *vg, const void *ptr, ssize_t len)
185
{
186
187 6740
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
188 6740
        AZ(vg->vz.avail_in);
189 6740
        assert(len >= 0);
190 6740
        if (len > 0)
191 4548
                AN(ptr);
192
193 6740
        vg->vz.next_in = TRUST_ME(ptr);
194 6740
        vg->vz.avail_in = len;
195 6740
}
196
197
int
198 15192
VGZ_IbufEmpty(const struct vgz *vg)
199
{
200
201 15192
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
202 15192
        return (vg->vz.avail_in == 0);
203
}
204
205
/*--------------------------------------------------------------------*/
206
207
void
208 7261
VGZ_Obuf(struct vgz *vg, void *ptr, ssize_t len)
209
{
210
211 7261
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
212 7261
        AN(ptr);
213 7261
        assert(len > 0);
214
215 7261
        vg->vz.next_out = ptr;
216 7261
        vg->vz.avail_out = len;
217 7261
}
218
219
int
220 4294
VGZ_ObufFull(const struct vgz *vg)
221
{
222
223 4294
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
224 4294
        return (vg->vz.avail_out == 0);
225
}
226
227
/*--------------------------------------------------------------------*/
228
229
static enum vgzret_e
230 3319
VGZ_Gunzip(struct vgz *vg, const void **pptr, ssize_t *plen)
231
{
232
        int i;
233
        ssize_t l;
234
        const uint8_t *before;
235
236 3319
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
237
238 3319
        *pptr = NULL;
239 3319
        *plen = 0;
240 3319
        AN(vg->vz.next_out);
241 3319
        AN(vg->vz.avail_out);
242 3319
        before = vg->vz.next_out;
243 3319
        i = inflate(&vg->vz, 0);
244 3319
        if (i == Z_OK || i == Z_STREAM_END) {
245 3303
                *pptr = before;
246 3303
                l = (const uint8_t *)vg->vz.next_out - before;
247 3303
                *plen = l;
248 3303
        }
249 3319
        vg->last_i = i;
250 3319
        if (i == Z_OK)
251 2287
                return (VGZ_OK);
252 1032
        if (i == Z_STREAM_END)
253 1016
                return (VGZ_END);
254 16
        if (i == Z_BUF_ERROR)
255 8
                return (VGZ_STUCK);
256 8
        VSLb(vg->vsl, SLT_Gzip, "Gunzip error: %d (%s)", i, vgz_msg(vg));
257 8
        return (VGZ_ERROR);
258 3319
}
259
260
/* set vz pointers for in and out iovecs */
261
static inline void
262 64
vgz_iovec_update(struct vgz *vg, const struct iovec *in, const struct iovec *buf)
263
{
264
        /* in: either fully consumed or the same */
265 64
        assert(vg->vz.avail_in == 0 || vg->vz.next_in == TRUST_ME(in->iov_base));
266 64
        vg->vz.next_in = TRUST_ME(in->iov_base);
267 64
        vg->vz.avail_in = in->iov_len;
268 64
        vg->vz.next_out = TRUST_ME(buf->iov_base);
269 64
        vg->vz.avail_out = buf->iov_len;
270 64
}
271
272
static enum vgzret_e
273 64
vgz_gunzip_iovec(struct vgz *vg, struct iovec *in, struct iovec *buf, struct iovec *out)
274
{
275
        int i;
276
277 64
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
278 64
        AN(in && buf && out);
279 64
        vgz_iovec_update(vg, in, buf);
280
281 64
        i = inflate(&vg->vz, 0);
282 64
        if (i == Z_OK || i == Z_STREAM_END) {
283 64
                iovec_collect(buf, out, pdiff(buf->iov_base, vg->vz.next_out));
284 64
                in->iov_base = TRUST_ME(vg->vz.next_in);
285 64
                in->iov_len = vg->vz.avail_in;
286 64
        }
287 64
        vg->last_i = i;
288 64
        if (i == Z_OK)
289 0
                return (VGZ_OK);
290 64
        if (i == Z_STREAM_END)
291 64
                return (VGZ_END);
292 0
        if (i == Z_BUF_ERROR)
293 0
                return (VGZ_STUCK);
294 0
        VSLb(vg->vsl, SLT_Gzip, "Gunzip error: %d (%s)", i, vgz_msg(vg));
295 0
        return (VGZ_ERROR);
296 64
}
297
298
/*--------------------------------------------------------------------*/
299
300
enum vgzret_e
301 4558
VGZ_Gzip(struct vgz *vg, const void **pptr, ssize_t *plen, enum vgz_flag flags)
302
{
303
        int i;
304
        int zflg;
305
        ssize_t l;
306
        const uint8_t *before;
307
308 4558
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
309
310 4558
        *pptr = NULL;
311 4558
        *plen = 0;
312 4558
        AN(vg->vz.next_out);
313 4558
        AN(vg->vz.avail_out);
314 4558
        before = vg->vz.next_out;
315 4558
        switch (flags) {
316 3006
        case VGZ_NORMAL:        zflg = Z_NO_FLUSH; break;
317 500
        case VGZ_ALIGN:         zflg = Z_SYNC_FLUSH; break;
318 504
        case VGZ_RESET:         zflg = Z_FULL_FLUSH; break;
319 548
        case VGZ_FINISH:        zflg = Z_FINISH; break;
320 0
        default:                WRONG("Invalid VGZ flag");
321 0
        }
322 4558
        i = deflate(&vg->vz, zflg);
323 4558
        if (i == Z_OK || i == Z_STREAM_END) {
324 4558
                *pptr = before;
325 4558
                l = (const uint8_t *)vg->vz.next_out - before;
326 4558
                *plen = l;
327 4558
        }
328 4558
        vg->last_i = i;
329 4558
        if (i == Z_OK)
330 4210
                return (VGZ_OK);
331 348
        if (i == Z_STREAM_END)
332 348
                return (VGZ_END);
333 0
        if (i == Z_BUF_ERROR)
334 0
                return (VGZ_STUCK);
335 0
        VSLb(vg->vsl, SLT_Gzip, "Gzip error: %d (%s)", i, vgz_msg(vg));
336 0
        return (VGZ_ERROR);
337 4558
}
338
339
/*--------------------------------------------------------------------
340
 * VDP for gunzip'ing
341
 */
342
343
// common for traditional interface and vdpio
344
static int vdp_gunzip_init_common(struct vdp_ctx *vdc);
345
346
static int v_matchproto_(vdp_init_f)
347 400
vdp_gunzip_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
348
{
349
        struct vgz *vg;
350
351 400
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
352 400
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
353 400
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
354 400
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
355 400
        AN(vdc->clen);
356 400
        AN(priv);
357
358 400
        vg = VGZ_NewGunzip(vdc->vsl, "U D -");
359 400
        AN(vg);
360 400
        if (vgz_getmbuf(vdc->wrk, vg)) {
361 0
                (void)VGZ_Destroy(vdc->wrk, &vg);
362 0
                return (-1);
363
        }
364
365 400
        VGZ_Obuf(vg, vg->m_buf, vg->m_sz);
366 400
        *priv = vg;
367 400
        return (vdp_gunzip_init_common(vdc));
368 400
}
369
370
static int
371 400
vdp_gunzip_init_common(struct vdp_ctx *vdc)
372
{
373
        struct boc *boc;
374
        enum boc_state_e bos;
375
        const char *p;
376
        ssize_t dl;
377
        uint64_t u;
378 400
        http_Unset(vdc->hp, H_Content_Encoding);
379
380 400
        *vdc->clen = -1;
381
382 400
        if (vdc->oc == NULL)
383 68
                return (0);
384
385 332
        boc = HSH_RefBoc(vdc->oc);
386 332
        if (boc != NULL) {
387 52
                CHECK_OBJ(boc, BOC_MAGIC);
388 52
                bos = boc->state;
389 52
                HSH_DerefBoc(vdc->wrk, vdc->oc);
390 52
                if (bos < BOS_FINISHED)
391 52
                        return (0); /* OA_GZIPBITS is not stable yet */
392 0
        }
393
394 280
        p = ObjGetAttr(vdc->wrk, vdc->oc, OA_GZIPBITS, &dl);
395 280
        if (p != NULL && dl == 32) {
396 280
                u = vbe64dec(p + 24);
397 280
                if (u != 0)
398 276
                        *vdc->clen = u;
399 280
        }
400 280
        return (0);
401 400
}
402
403
static int v_matchproto_(vdp_fini_f)
404 336
vdp_gunzip_fini(struct vdp_ctx *vdc, void **priv)
405
{
406
        struct vgz *vg;
407
408 336
        (void)vdc;
409 336
        TAKE_OBJ_NOTNULL(vg, priv, VGZ_MAGIC);
410 336
        AN(vg->m_buf);
411 336
        (void)VGZ_Destroy(vdc->wrk, &vg);
412 336
        return (0);
413
}
414
415
static int v_matchproto_(vdp_bytes_f)
416 1771
vdp_gunzip_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
417
    const void *ptr, ssize_t len)
418
{
419
        enum vgzret_e vr;
420
        ssize_t dl;
421
        const void *dp;
422
        struct vgz *vg;
423
424 1771
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
425 1771
        CHECK_OBJ_NOTNULL(vdc->wrk, WORKER_MAGIC);
426 1771
        (void)act;
427
428 1771
        CAST_OBJ_NOTNULL(vg, *priv, VGZ_MAGIC);
429 1771
        AN(vg->m_buf);
430
431 1771
        if (len == 0)
432 467
                return (0);
433
434 1304
        VGZ_Ibuf(vg, ptr, len);
435 1304
        do {
436 1304
                vr = VGZ_Gunzip(vg, &dp, &dl);
437 1304
                if (vr == VGZ_END && !VGZ_IbufEmpty(vg)) {
438 8
                        VSLb(vg->vsl, SLT_Gzip, "G(un)zip error: %d (%s)",
439 4
                             vr, "junk after VGZ_END");
440 4
                        return (-1);
441
                }
442 1300
                vg->m_len += dl;
443 1300
                if (vr < VGZ_OK)
444 0
                        return (-1);
445
                // END or STUCK
446 1300
                if (vg->m_len == vg->m_sz || vr != VGZ_OK) {
447 648
                        if (VDP_bytes(vdc, vr == VGZ_END ? VDP_END : VDP_FLUSH,
448 324
                            vg->m_buf, vg->m_len))
449 16
                                return (vdc->retval);
450 308
                        vg->m_len = 0;
451 308
                        VGZ_Obuf(vg, vg->m_buf, vg->m_sz);
452 308
                }
453 1284
        } while (!VGZ_IbufEmpty(vg));
454 1284
        assert(vr == VGZ_STUCK || vr == VGZ_OK || vr == VGZ_END);
455 1284
        return (0);
456 1771
}
457
458
#ifdef LATER
459
/*
460
 * XXX does it make sense to work on more than one buffer?
461
 */
462
static int v_matchproto_(vdpio_init_f)
463
vdpio_gunzip_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, int capacity)
464
{
465
        struct vgz *vg;
466
        struct vscarab *in;
467
468
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
469
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
470
        CHECK_OBJ_ORNULL(vdc->oc, OBJCORE_MAGIC);
471
        CHECK_OBJ_NOTNULL(vdc->hp, HTTP_MAGIC);
472
        AN(vdc->clen);
473
        AN(priv);
474
        assert(capacity >= 1);
475
476
        if (capacity < 4)
477
                capacity = 4;
478
479
        in = WS_Alloc(ctx->ws, VSCARAB_SIZE(capacity));
480
        if (in == NULL)
481
                return (-1);
482
        VSCARAB_INIT(in, capacity);
483
484
        vg = VGZ_NewGunzip(vdc->vsl, "U D -");
485
        if (vg == NULL)
486
                return (-1);
487
488
        AZ(vg->m_buf);
489
        vg->m_buf = (void *)in;
490
491
        *priv = vg;
492
        AZ(vdp_gunzip_init_common(vdc));
493
        return (1);
494
}
495
#endif
496
497
static int v_matchproto_(vdpio_init_f)
498 64
vdpio_gunzip_upgrade(VRT_CTX, struct vdp_ctx *vdc, void **priv, int capacity)
499
{
500
        struct vgz *vg;
501
        struct vscarab *in;
502
503 64
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
504 64
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
505 64
        AN(priv);
506 64
        assert(capacity >= 1);
507
508 64
        if (capacity < 4)
509 64
                capacity = 4;
510
511
        /* done in vdp_gunzip_init */
512 64
        CAST_OBJ_NOTNULL(vg, *priv, VGZ_MAGIC);
513
514 64
        in = WS_Alloc(ctx->ws, VSCARAB_SIZE(capacity));
515 64
        if (in == NULL)
516 0
                return (-1);
517 64
        VSCARAB_INIT(in, capacity);
518
519
        // XXX duplicate work - remove when completing transition to VAI
520 64
        AN(vg->m_buf);
521 64
        AN(vg->stvbuf);
522 64
        STV_FreeBuf(vdc->wrk, &vg->stvbuf);
523 64
        vg->stvbuf = NULL;
524 64
        vg->m_buf = (void *)in;
525
526 64
        return (1);
527 64
}
528
529
static int v_matchproto_(vdpio_lease_f)
530 64
vdpio_gunzip_lease(struct vdp_ctx *vdc, struct vdp_entry *this, struct vscarab *out)
531
{
532
        struct vscarab *in;
533
        enum vgzret_e vr;
534
        struct vgz *vg;
535
        struct viov *v, *b, *o;
536
        int r;
537
538 64
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
539 64
        CHECK_OBJ_NOTNULL(this, VDP_ENTRY_MAGIC);
540 64
        CAST_OBJ_NOTNULL(vg, this->priv, VGZ_MAGIC);
541 64
        CAST_OBJ_NOTNULL(in, (void*)vg->m_buf, VSCARAB_MAGIC);
542
543 64
        this->calls++;
544
545 64
        if (out->used == out->capacity)
546 0
                return (0);
547
548 64
        if (in->used < in->capacity && (in->flags & VSCARAB_F_END) == 0)
549 64
                r = vdpio_pull(vdc, this, in);
550
        else
551 0
                r = 0;
552
553 64
        if (in->used == 0) {
554 0
                out->flags = in->flags & VSCARAB_F_END;
555 0
                return (r);
556
        }
557
558
        // XXX more than one buffer?
559 64
        VSCARAB_LOCAL(buf, 1);
560 64
        b = VSCARAB_GET(buf);
561 64
        AN(b);
562 64
        b->iov.iov_len = cache_param->gzip_buffer;
563 64
        r = ObjVAIbuffer(vdc->wrk, vdc->vai_hdl, buf);
564 64
        if (r < 0)
565 0
                return (out->used ? 0 : r);
566
567 64
        o = VSCARAB_GET(out);
568 64
        AN(o);
569 64
        r = 0;
570
571 64
        VSCARAB_FOREACH(v, in) {
572 64
                this->bytes_in += v->iov.iov_len;
573 64
                vr = vgz_gunzip_iovec(vg, &v->iov, &b->iov, &o->iov);
574 64
                if (vr == VGZ_END && v->iov.iov_len > 0) {
575 0
                        VSLb(vg->vsl, SLT_Gzip, "G(un)zip error: %d (%s)",
576 0
                             vr, "junk after VGZ_END");
577 0
                        r = -EMSGSIZE;
578 0
                        break;
579
                }
580 64
                if (vr < VGZ_OK)
581 0
                        break;
582
583 64
                if (b->iov.iov_len == 0 || vr != VGZ_OK) {
584 64
                        r = 1;
585 64
                        break;
586
                }
587 0
        }
588
589 64
        if (r <= 0) {
590 0
                o->iov = IOV_NIL;
591 0
                vdpio_return_lease(vdc, b->lease);
592 0
                return (r);
593
        }
594
595 64
        o->lease = b->lease;
596 64
        b->lease = 0;
597
598 64
        vdpio_consolidate_vscarab(vdc, in);
599 64
        if (in->used == 0)
600 64
                out->flags = in->flags & VSCARAB_F_END;
601
602 64
        return (r);
603 64
}
604
605
static void v_matchproto_(vdpio_fini_f)
606 64
vdpio_gunzip_fini(struct vdp_ctx *vdc, void **priv)
607
{
608
        struct vscarab *in;
609
        struct vgz *vg;
610
611 64
        (void)vdc;
612 64
        TAKE_OBJ_NOTNULL(vg, priv, VGZ_MAGIC);
613 64
        CAST_OBJ_NOTNULL(in, (void *)vg->m_buf, VSCARAB_MAGIC);
614 64
        vg->m_buf = NULL;
615
616 64
        (void)VGZ_Destroy(vdc->wrk, &vg);
617 64
}
618
619
const struct vdp VDP_gunzip = {
620
        .name =         "gunzip",
621
        .init =         vdp_gunzip_init,
622
        .bytes =        vdp_gunzip_bytes,
623
        .fini =         vdp_gunzip_fini,
624
625
#ifdef LATER
626
        .io_init =      vdpio_gunzip_init,
627
#endif
628
        .io_upgrade =   vdpio_gunzip_upgrade,
629
        .io_lease =     vdpio_gunzip_lease,
630
        .io_fini =      vdpio_gunzip_fini,
631
};
632
633
/*--------------------------------------------------------------------*/
634
635
void
636 5024
VGZ_UpdateObj(const struct vfp_ctx *vc, struct vgz *vg, enum vgzret_e e)
637
{
638
        char *p;
639
        intmax_t ii;
640
641 5024
        CHECK_OBJ_NOTNULL(vg, VGZ_MAGIC);
642 5024
        if (e < VGZ_OK)
643 0
                return;
644 5024
        ii = vg->vz.start_bit + vg->vz.last_bit + vg->vz.stop_bit;
645 5024
        if (e != VGZ_END && ii == vg->bits)
646 3702
                return;
647 1322
        vg->bits = ii;
648 1322
        p = ObjSetAttr(vc->wrk, vc->oc, OA_GZIPBITS, 32, NULL);
649 1322
        AN(p);
650 1322
        vbe64enc(p, vg->vz.start_bit);
651 1322
        vbe64enc(p + 8, vg->vz.last_bit);
652 1322
        vbe64enc(p + 16, vg->vz.stop_bit);
653 1322
        if (e != VGZ_END)
654 510
                return;
655 812
        if (vg->dir == VGZ_GZ)
656 696
                vbe64enc(p + 24, vg->vz.total_in);
657 812
        if (vg->dir == VGZ_UN)
658 116
                vbe64enc(p + 24, vg->vz.total_out);
659 5024
}
660
661
/*--------------------------------------------------------------------
662
 */
663
664
enum vgzret_e
665 1392
VGZ_Destroy(struct worker *wrk, struct vgz **vgp)
666
{
667
        struct vgz *vg;
668
        enum vgzret_e vr;
669
        int i;
670
671 1392
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
672 1392
        TAKE_OBJ_NOTNULL(vg, vgp, VGZ_MAGIC);
673 1392
        AN(vg->id);
674 2784
        VSLb(vg->vsl, SLT_Gzip, "%s %jd %jd %jd %jd %jd",
675 1392
            vg->id,
676 1392
            (intmax_t)vg->vz.total_in,
677 1392
            (intmax_t)vg->vz.total_out,
678 1392
            (intmax_t)vg->vz.start_bit,
679 1392
            (intmax_t)vg->vz.last_bit,
680 1392
            (intmax_t)vg->vz.stop_bit);
681 1392
        if (vg->dir == VGZ_GZ)
682 368
                i = deflateEnd(&vg->vz);
683
        else
684 1024
                i = inflateEnd(&vg->vz);
685 1392
        if (vg->last_i == Z_STREAM_END && i == Z_OK)
686 1132
                i = Z_STREAM_END;
687 1392
        if (vg->m_buf != NULL) {
688 1024
                AN(vg->stvbuf);
689 1024
                STV_FreeBuf(wrk, &vg->stvbuf);
690 1024
        }
691 1392
        AZ(vg->stvbuf);
692 1392
        if (i == Z_OK)
693 252
                vr = VGZ_OK;
694 1140
        else if (i == Z_STREAM_END)
695 1132
                vr = VGZ_END;
696 8
        else if (i == Z_BUF_ERROR)
697 0
                vr = VGZ_STUCK;
698
        else {
699 16
                VSLb(vg->vsl, SLT_Gzip, "G(un)zip error: %d (%s)",
700 8
                    i, vgz_msg(vg));
701 8
                vr = VGZ_ERROR;
702
        }
703 1392
        FREE_OBJ(vg);
704 1392
        return (vr);
705
}
706
707
/*--------------------------------------------------------------------*/
708
709
static enum vfp_status v_matchproto_(vfp_init_f)
710 704
vfp_gzip_init(VRT_CTX, struct vfp_ctx *vc, struct vfp_entry *vfe)
711
{
712
        struct vgz *vg;
713
714 704
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
715 704
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
716 704
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
717
718
        /*
719
         * G(un)zip makes no sence on partial responses, but since
720
         * it is an pure 1:1 transform, we can just ignore it.
721
         */
722 704
        if (http_GetStatus(vc->resp) == 206)
723 16
                return (VFP_NULL);
724
725 688
        if (vfe->vfp == &VFP_gzip) {
726 64
                if (http_GetHdr(vc->resp, H_Content_Encoding, NULL))
727 0
                        return (VFP_NULL);
728 64
                vg = VGZ_NewGzip(vc->wrk->vsl, vfe->vfp->priv1);
729 64
                vc->obj_flags |= OF_GZIPED | OF_CHGCE;
730 64
        } else {
731 624
                if (!http_HdrIs(vc->resp, H_Content_Encoding, "gzip"))
732 0
                        return (VFP_NULL);
733 624
                if (vfe->vfp == &VFP_gunzip) {
734 500
                        vg = VGZ_NewGunzip(vc->wrk->vsl, vfe->vfp->priv1);
735 500
                        vc->obj_flags &= ~OF_GZIPED;
736 500
                        vc->obj_flags |= OF_CHGCE;
737 500
                } else {
738 124
                        vg = VGZ_NewTestGunzip(vc->wrk->vsl, vfe->vfp->priv1);
739 124
                        vc->obj_flags |= OF_GZIPED;
740
                }
741
        }
742 688
        AN(vg);
743 688
        vfe->priv1 = vg;
744 688
        if (vgz_getmbuf(vc->wrk, vg))
745 0
                return (VFP_ERROR);
746 688
        VGZ_Ibuf(vg, vg->m_buf, 0);
747 688
        AZ(vg->m_len);
748
749 688
        if (vfe->vfp == &VFP_gunzip || vfe->vfp == &VFP_gzip) {
750 564
                http_Unset(vc->resp, H_Content_Encoding);
751 564
                http_Unset(vc->resp, H_Content_Length);
752 564
                RFC2616_Weaken_Etag(vc->resp);
753 564
        }
754
755 688
        if (vfe->vfp == &VFP_gzip)
756 64
                http_SetHeader(vc->resp, "Content-Encoding: gzip");
757
758 688
        if (vfe->vfp == &VFP_gzip || vfe->vfp == &VFP_testgunzip)
759 188
                RFC2616_Vary_AE(vc->resp);
760
761 688
        return (VFP_OK);
762 704
}
763
764
/*--------------------------------------------------------------------
765
 * VFP_GUNZIP
766
 *
767
 * A VFP for gunzip'ing an object as we receive it from the backend
768
 */
769
770
static enum vfp_status v_matchproto_(vfp_pull_f)
771 1865
vfp_gunzip_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
772
    ssize_t *lp)
773
{
774
        ssize_t l;
775
        struct vgz *vg;
776 1865
        enum vgzret_e vr = VGZ_ERROR;
777
        const void *dp;
778
        ssize_t dl;
779 1865
        enum vfp_status vp = VFP_OK;
780
781 1865
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
782 1865
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
783 1865
        CAST_OBJ_NOTNULL(vg, vfe->priv1, VGZ_MAGIC);
784 1865
        AN(p);
785 1865
        AN(lp);
786 1865
        l = *lp;
787 1865
        *lp = 0;
788 1865
        VGZ_Obuf(vg, p, l);
789 1865
        do {
790 1873
                if (VGZ_IbufEmpty(vg)) {
791 580
                        l = vg->m_sz;
792 580
                        vp = VFP_Suck(vc, vg->m_buf, &l);
793 580
                        if (vp == VFP_ERROR)
794 0
                                return (vp);
795 580
                        VGZ_Ibuf(vg, vg->m_buf, l);
796 580
                }
797 1873
                if (!VGZ_IbufEmpty(vg) || vp == VFP_END) {
798 1873
                        vr = VGZ_Gunzip(vg, &dp, &dl);
799 1873
                        if (vr == VGZ_END && !VGZ_IbufEmpty(vg))
800 4
                                return (VFP_Error(vc, "Junk after gzip data"));
801 1869
                        if (vr < VGZ_OK)
802 16
                                return (VFP_Error(vc,
803 8
                                    "Invalid Gzip data: %s", vgz_msg(vg)));
804 1861
                        if (dl > 0) {
805 1573
                                *lp = dl;
806 1573
                                assert(dp == p);
807 1573
                                return (VFP_OK);
808
                        }
809 288
                }
810 288
                AN(VGZ_IbufEmpty(vg));
811 288
        } while (vp == VFP_OK);
812 280
        if (vr != VGZ_END)
813 4
                return (VFP_Error(vc, "Gunzip error at the very end"));
814 276
        return (vp);
815 1865
}
816
817
818
/*--------------------------------------------------------------------
819
 * VFP_GZIP
820
 *
821
 * A VFP for gzip'ing an object as we receive it from the backend
822
 */
823
824
static enum vfp_status v_matchproto_(vfp_pull_f)
825 252
vfp_gzip_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
826
    ssize_t *lp)
827
{
828
        ssize_t l;
829
        struct vgz *vg;
830 252
        enum vgzret_e vr = VGZ_ERROR;
831
        const void *dp;
832
        ssize_t dl;
833 252
        enum vfp_status vp = VFP_ERROR;
834
835 252
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
836 252
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
837 252
        CAST_OBJ_NOTNULL(vg, vfe->priv1, VGZ_MAGIC);
838 252
        AN(p);
839 252
        AN(lp);
840 252
        l = *lp;
841 252
        *lp = 0;
842 252
        VGZ_Obuf(vg, p, l);
843 252
        do {
844 264
                if (VGZ_IbufEmpty(vg)) {
845 248
                        l = vg->m_sz;
846 248
                        vp = VFP_Suck(vc, vg->m_buf, &l);
847 248
                        if (vp == VFP_ERROR)
848 0
                                break;
849 248
                        if (vp == VFP_END)
850 88
                                vg->flag = VGZ_FINISH;
851 248
                        VGZ_Ibuf(vg, vg->m_buf, l);
852 248
                }
853 264
                if (!VGZ_IbufEmpty(vg) || vg->flag == VGZ_FINISH) {
854 264
                        vr = VGZ_Gzip(vg, &dp, &dl, vg->flag);
855 264
                        if (vr < VGZ_OK)
856 0
                                return (VFP_Error(vc, "Gzip failed"));
857 264
                        if (dl > 0) {
858 252
                                VGZ_UpdateObj(vc, vg, vr);
859 252
                                *lp = dl;
860 252
                                assert(dp == p);
861 252
                                if (vr != VGZ_END || !VGZ_IbufEmpty(vg))
862 192
                                        return (VFP_OK);
863 60
                        }
864 72
                }
865 72
                AN(VGZ_IbufEmpty(vg));
866 72
        } while (vg->flag != VGZ_FINISH);
867
868 60
        if (vr != VGZ_END)
869 0
                return (VFP_Error(vc, "Gzip failed"));
870 60
        VGZ_UpdateObj(vc, vg, VGZ_END);
871 60
        return (VFP_END);
872 252
}
873
874
/*--------------------------------------------------------------------
875
 * VFP_TESTGZIP
876
 *
877
 * A VFP for testing that received gzip data is valid, and for
878
 * collecting the magic bits while we're at it.
879
 */
880
881
static enum vfp_status v_matchproto_(vfp_pull_f)
882 150
vfp_testgunzip_pull(struct vfp_ctx *vc, struct vfp_entry *vfe, void *p,
883
    ssize_t *lp)
884
{
885
        struct vgz *vg;
886 150
        enum vgzret_e vr = VGZ_ERROR;
887
        const void *dp;
888
        ssize_t dl;
889
        enum vfp_status vp;
890
891 150
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
892 150
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
893 150
        CAST_OBJ_NOTNULL(vg, vfe->priv1, VGZ_MAGIC);
894 150
        AN(p);
895 150
        AN(lp);
896 150
        vp = VFP_Suck(vc, p, lp);
897 150
        if (vp == VFP_ERROR)
898 8
                return (vp);
899 142
        if (*lp > 0 || vp == VFP_END) {
900 142
                VGZ_Ibuf(vg, p, *lp);
901 142
                do {
902 142
                        VGZ_Obuf(vg, vg->m_buf, vg->m_sz);
903 142
                        vr = VGZ_Gunzip(vg, &dp, &dl);
904 142
                        if (vr == VGZ_END && !VGZ_IbufEmpty(vg))
905 12
                                return (VFP_Error(vc, "Junk after gzip data"));
906 130
                        if (vr < VGZ_OK)
907 0
                                return (VFP_Error(vc,
908 0
                                    "Invalid Gzip data: %s", vgz_msg(vg)));
909 130
                } while (!VGZ_IbufEmpty(vg));
910 130
        }
911 130
        VGZ_UpdateObj(vc, vg, vr);
912 130
        if (vp == VFP_END && vr != VGZ_END)
913 4
                return (VFP_Error(vc, "tGunzip failed"));
914 126
        return (vp);
915 150
}
916
917
/*--------------------------------------------------------------------*/
918
919
static void v_matchproto_(vfp_fini_f)
920 748
vfp_gzip_fini(struct vfp_ctx *vc, struct vfp_entry *vfe)
921
{
922
        struct vgz *vg;
923
924 748
        CHECK_OBJ_NOTNULL(vc, VFP_CTX_MAGIC);
925 748
        CHECK_OBJ_NOTNULL(vfe, VFP_ENTRY_MAGIC);
926
927 748
        if (vfe->priv1 != NULL) {
928 688
                TAKE_OBJ_NOTNULL(vg, &vfe->priv1, VGZ_MAGIC);
929 688
                (void)VGZ_Destroy(vc->wrk, &vg);
930 688
        }
931 748
}
932
933
/*--------------------------------------------------------------------*/
934
935
const struct vfp VFP_gunzip = {
936
        .name = "gunzip",
937
        .init = vfp_gzip_init,
938
        .pull = vfp_gunzip_pull,
939
        .fini = vfp_gzip_fini,
940
        .priv1 = "U F -",
941
};
942
943
const struct vfp VFP_gzip = {
944
        .name = "gzip",
945
        .init = vfp_gzip_init,
946
        .pull = vfp_gzip_pull,
947
        .fini = vfp_gzip_fini,
948
        .priv1 = "G F -",
949
};
950
951
const struct vfp VFP_testgunzip = {
952
        .name = "testgunzip",
953
        .init = vfp_gzip_init,
954
        .pull = vfp_testgunzip_pull,
955
        .fini = vfp_gzip_fini,
956
        .priv1 = "u F -",
957
};