12c593315Sopenharmony_ci/*
22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library
32c593315Sopenharmony_ci *
42c593315Sopenharmony_ci * Copyright (c) 2012 Tatsuhiro Tsujikawa
52c593315Sopenharmony_ci *
62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the
82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
122c593315Sopenharmony_ci * the following conditions:
132c593315Sopenharmony_ci *
142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be
152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software.
162c593315Sopenharmony_ci *
172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
242c593315Sopenharmony_ci */
252c593315Sopenharmony_ci#include "nghttp2_gzip.h"
262c593315Sopenharmony_ci
272c593315Sopenharmony_ci#include <assert.h>
282c593315Sopenharmony_ci
292c593315Sopenharmony_ciint nghttp2_gzip_inflate_new(nghttp2_gzip **inflater_ptr) {
302c593315Sopenharmony_ci  int rv;
312c593315Sopenharmony_ci  *inflater_ptr = calloc(1, sizeof(nghttp2_gzip));
322c593315Sopenharmony_ci  if (*inflater_ptr == NULL) {
332c593315Sopenharmony_ci    return -1;
342c593315Sopenharmony_ci  }
352c593315Sopenharmony_ci  rv = inflateInit2(&(*inflater_ptr)->zst, 47);
362c593315Sopenharmony_ci  if (rv != Z_OK) {
372c593315Sopenharmony_ci    free(*inflater_ptr);
382c593315Sopenharmony_ci    return -1;
392c593315Sopenharmony_ci  }
402c593315Sopenharmony_ci  return 0;
412c593315Sopenharmony_ci}
422c593315Sopenharmony_ci
432c593315Sopenharmony_civoid nghttp2_gzip_inflate_del(nghttp2_gzip *inflater) {
442c593315Sopenharmony_ci  if (inflater != NULL) {
452c593315Sopenharmony_ci    inflateEnd(&inflater->zst);
462c593315Sopenharmony_ci    free(inflater);
472c593315Sopenharmony_ci  }
482c593315Sopenharmony_ci}
492c593315Sopenharmony_ci
502c593315Sopenharmony_ciint nghttp2_gzip_inflate(nghttp2_gzip *inflater, uint8_t *out,
512c593315Sopenharmony_ci                         size_t *outlen_ptr, const uint8_t *in,
522c593315Sopenharmony_ci                         size_t *inlen_ptr) {
532c593315Sopenharmony_ci  int rv;
542c593315Sopenharmony_ci  if (inflater->finished) {
552c593315Sopenharmony_ci    return -1;
562c593315Sopenharmony_ci  }
572c593315Sopenharmony_ci  inflater->zst.avail_in = (unsigned int)*inlen_ptr;
582c593315Sopenharmony_ci  inflater->zst.next_in = (unsigned char *)in;
592c593315Sopenharmony_ci  inflater->zst.avail_out = (unsigned int)*outlen_ptr;
602c593315Sopenharmony_ci  inflater->zst.next_out = out;
612c593315Sopenharmony_ci
622c593315Sopenharmony_ci  rv = inflate(&inflater->zst, Z_NO_FLUSH);
632c593315Sopenharmony_ci
642c593315Sopenharmony_ci  *inlen_ptr -= inflater->zst.avail_in;
652c593315Sopenharmony_ci  *outlen_ptr -= inflater->zst.avail_out;
662c593315Sopenharmony_ci  switch (rv) {
672c593315Sopenharmony_ci  case Z_STREAM_END:
682c593315Sopenharmony_ci    inflater->finished = 1;
692c593315Sopenharmony_ci  /* FALL THROUGH */
702c593315Sopenharmony_ci  case Z_OK:
712c593315Sopenharmony_ci  case Z_BUF_ERROR:
722c593315Sopenharmony_ci    return 0;
732c593315Sopenharmony_ci  case Z_DATA_ERROR:
742c593315Sopenharmony_ci  case Z_STREAM_ERROR:
752c593315Sopenharmony_ci  case Z_NEED_DICT:
762c593315Sopenharmony_ci  case Z_MEM_ERROR:
772c593315Sopenharmony_ci    return -1;
782c593315Sopenharmony_ci  default:
792c593315Sopenharmony_ci    assert(0);
802c593315Sopenharmony_ci    /* We need this for some compilers */
812c593315Sopenharmony_ci    return 0;
822c593315Sopenharmony_ci  }
832c593315Sopenharmony_ci}
842c593315Sopenharmony_ci
852c593315Sopenharmony_ciint nghttp2_gzip_inflate_finished(nghttp2_gzip *inflater) {
862c593315Sopenharmony_ci  return inflater->finished;
872c593315Sopenharmony_ci}
88