xref: /third_party/nghttp2/src/nghttp2_gzip.c (revision 2c593315)
1/*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25#include "nghttp2_gzip.h"
26
27#include <assert.h>
28
29int nghttp2_gzip_inflate_new(nghttp2_gzip **inflater_ptr) {
30  int rv;
31  *inflater_ptr = calloc(1, sizeof(nghttp2_gzip));
32  if (*inflater_ptr == NULL) {
33    return -1;
34  }
35  rv = inflateInit2(&(*inflater_ptr)->zst, 47);
36  if (rv != Z_OK) {
37    free(*inflater_ptr);
38    return -1;
39  }
40  return 0;
41}
42
43void nghttp2_gzip_inflate_del(nghttp2_gzip *inflater) {
44  if (inflater != NULL) {
45    inflateEnd(&inflater->zst);
46    free(inflater);
47  }
48}
49
50int nghttp2_gzip_inflate(nghttp2_gzip *inflater, uint8_t *out,
51                         size_t *outlen_ptr, const uint8_t *in,
52                         size_t *inlen_ptr) {
53  int rv;
54  if (inflater->finished) {
55    return -1;
56  }
57  inflater->zst.avail_in = (unsigned int)*inlen_ptr;
58  inflater->zst.next_in = (unsigned char *)in;
59  inflater->zst.avail_out = (unsigned int)*outlen_ptr;
60  inflater->zst.next_out = out;
61
62  rv = inflate(&inflater->zst, Z_NO_FLUSH);
63
64  *inlen_ptr -= inflater->zst.avail_in;
65  *outlen_ptr -= inflater->zst.avail_out;
66  switch (rv) {
67  case Z_STREAM_END:
68    inflater->finished = 1;
69  /* FALL THROUGH */
70  case Z_OK:
71  case Z_BUF_ERROR:
72    return 0;
73  case Z_DATA_ERROR:
74  case Z_STREAM_ERROR:
75  case Z_NEED_DICT:
76  case Z_MEM_ERROR:
77    return -1;
78  default:
79    assert(0);
80    /* We need this for some compilers */
81    return 0;
82  }
83}
84
85int nghttp2_gzip_inflate_finished(nghttp2_gzip *inflater) {
86  return inflater->finished;
87}
88