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