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#ifndef NGHTTP2_GZIP_H
262c593315Sopenharmony_ci
272c593315Sopenharmony_ci#  ifdef HAVE_CONFIG_H
282c593315Sopenharmony_ci#    include <config.h>
292c593315Sopenharmony_ci#  endif /* HAVE_CONFIG_H */
302c593315Sopenharmony_ci#  include <zlib.h>
312c593315Sopenharmony_ci
322c593315Sopenharmony_ci#  include <nghttp2/nghttp2.h>
332c593315Sopenharmony_ci
342c593315Sopenharmony_ci#  ifdef __cplusplus
352c593315Sopenharmony_ciextern "C" {
362c593315Sopenharmony_ci#  endif
372c593315Sopenharmony_ci
382c593315Sopenharmony_ci/**
392c593315Sopenharmony_ci * @struct
402c593315Sopenharmony_ci *
412c593315Sopenharmony_ci * The gzip stream to inflate data.
422c593315Sopenharmony_ci */
432c593315Sopenharmony_citypedef struct {
442c593315Sopenharmony_ci  z_stream zst;
452c593315Sopenharmony_ci  int8_t finished;
462c593315Sopenharmony_ci} nghttp2_gzip;
472c593315Sopenharmony_ci
482c593315Sopenharmony_ci/**
492c593315Sopenharmony_ci * @function
502c593315Sopenharmony_ci *
512c593315Sopenharmony_ci * A helper function to set up a per request gzip stream to inflate
522c593315Sopenharmony_ci * data.
532c593315Sopenharmony_ci *
542c593315Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
552c593315Sopenharmony_ci */
562c593315Sopenharmony_ciint nghttp2_gzip_inflate_new(nghttp2_gzip **inflater_ptr);
572c593315Sopenharmony_ci
582c593315Sopenharmony_ci/**
592c593315Sopenharmony_ci * @function
602c593315Sopenharmony_ci *
612c593315Sopenharmony_ci * Frees the inflate stream.  The |inflater| may be ``NULL``.
622c593315Sopenharmony_ci */
632c593315Sopenharmony_civoid nghttp2_gzip_inflate_del(nghttp2_gzip *inflater);
642c593315Sopenharmony_ci
652c593315Sopenharmony_ci/**
662c593315Sopenharmony_ci * @function
672c593315Sopenharmony_ci *
682c593315Sopenharmony_ci * Inflates data in |in| with the length |*inlen_ptr| and stores the
692c593315Sopenharmony_ci * inflated data to |out| which has allocated size at least
702c593315Sopenharmony_ci * |*outlen_ptr|.  On return, |*outlen_ptr| is updated to represent
712c593315Sopenharmony_ci * the number of data written in |out|.  Similarly, |*inlen_ptr| is
722c593315Sopenharmony_ci * updated to represent the number of input bytes processed.
732c593315Sopenharmony_ci *
742c593315Sopenharmony_ci * This function returns 0 if it succeeds, or -1.
752c593315Sopenharmony_ci *
762c593315Sopenharmony_ci * The example follows::
772c593315Sopenharmony_ci *
782c593315Sopenharmony_ci *     void on_data_chunk_recv_callback(nghttp2_session *session,
792c593315Sopenharmony_ci *                                      uint8_t flags,
802c593315Sopenharmony_ci *                                      int32_t stream_id,
812c593315Sopenharmony_ci *                                      const uint8_t *data, size_t len,
822c593315Sopenharmony_ci *                                      void *user_data)
832c593315Sopenharmony_ci *     {
842c593315Sopenharmony_ci *         ...
852c593315Sopenharmony_ci *         req = nghttp2_session_get_stream_user_data(session, stream_id);
862c593315Sopenharmony_ci *         nghttp2_gzip *inflater = req->inflater;
872c593315Sopenharmony_ci *         while(len > 0) {
882c593315Sopenharmony_ci *             uint8_t out[MAX_OUTLEN];
892c593315Sopenharmony_ci *             size_t outlen = MAX_OUTLEN;
902c593315Sopenharmony_ci *             size_t tlen = len;
912c593315Sopenharmony_ci *             int rv;
922c593315Sopenharmony_ci *             rv = nghttp2_gzip_inflate(inflater, out, &outlen, data, &tlen);
932c593315Sopenharmony_ci *             if(rv != 0) {
942c593315Sopenharmony_ci *                 nghttp2_submit_rst_stream(session, stream_id,
952c593315Sopenharmony_ci *                                           NGHTTP2_INTERNAL_ERROR);
962c593315Sopenharmony_ci *                 break;
972c593315Sopenharmony_ci *             }
982c593315Sopenharmony_ci *             ... Do stuff ...
992c593315Sopenharmony_ci *             data += tlen;
1002c593315Sopenharmony_ci *             len -= tlen;
1012c593315Sopenharmony_ci *         }
1022c593315Sopenharmony_ci *         ....
1032c593315Sopenharmony_ci *     }
1042c593315Sopenharmony_ci */
1052c593315Sopenharmony_ciint nghttp2_gzip_inflate(nghttp2_gzip *inflater, uint8_t *out,
1062c593315Sopenharmony_ci                         size_t *outlen_ptr, const uint8_t *in,
1072c593315Sopenharmony_ci                         size_t *inlen_ptr);
1082c593315Sopenharmony_ci
1092c593315Sopenharmony_ci/**
1102c593315Sopenharmony_ci * @function
1112c593315Sopenharmony_ci *
1122c593315Sopenharmony_ci * Returns nonzero if |inflater| sees the end of deflate stream.
1132c593315Sopenharmony_ci * After this function returns nonzero, `nghttp2_gzip_inflate()` with
1142c593315Sopenharmony_ci * |inflater| gets to return error.
1152c593315Sopenharmony_ci */
1162c593315Sopenharmony_ciint nghttp2_gzip_inflate_finished(nghttp2_gzip *inflater);
1172c593315Sopenharmony_ci
1182c593315Sopenharmony_ci#  ifdef __cplusplus
1192c593315Sopenharmony_ci}
1202c593315Sopenharmony_ci#  endif
1212c593315Sopenharmony_ci
1222c593315Sopenharmony_ci#endif /* NGHTTP2_GZIP_H */
123