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
nghttp2_gzip_inflate_new(nghttp2_gzip ** inflater_ptr)29 int nghttp2_gzip_inflate_new(nghttp2_gzip **inflater_ptr) {
30 int rv;
31 *inflater_ptr = malloc(sizeof(nghttp2_gzip));
32 if (*inflater_ptr == NULL) {
33 return -1;
34 }
35 (*inflater_ptr)->finished = 0;
36 (*inflater_ptr)->zst.next_in = Z_NULL;
37 (*inflater_ptr)->zst.avail_in = 0;
38 (*inflater_ptr)->zst.zalloc = Z_NULL;
39 (*inflater_ptr)->zst.zfree = Z_NULL;
40 (*inflater_ptr)->zst.opaque = Z_NULL;
41 rv = inflateInit2(&(*inflater_ptr)->zst, 47);
42 if (rv != Z_OK) {
43 free(*inflater_ptr);
44 return -1;
45 }
46 return 0;
47 }
48
nghttp2_gzip_inflate_del(nghttp2_gzip * inflater)49 void nghttp2_gzip_inflate_del(nghttp2_gzip *inflater) {
50 if (inflater != NULL) {
51 inflateEnd(&inflater->zst);
52 free(inflater);
53 }
54 }
55
nghttp2_gzip_inflate(nghttp2_gzip * inflater,uint8_t * out,size_t * outlen_ptr,const uint8_t * in,size_t * inlen_ptr)56 int nghttp2_gzip_inflate(nghttp2_gzip *inflater, uint8_t *out,
57 size_t *outlen_ptr, const uint8_t *in,
58 size_t *inlen_ptr) {
59 int rv;
60 if (inflater->finished) {
61 return -1;
62 }
63 inflater->zst.avail_in = (unsigned int)*inlen_ptr;
64 inflater->zst.next_in = (unsigned char *)in;
65 inflater->zst.avail_out = (unsigned int)*outlen_ptr;
66 inflater->zst.next_out = out;
67
68 rv = inflate(&inflater->zst, Z_NO_FLUSH);
69
70 *inlen_ptr -= inflater->zst.avail_in;
71 *outlen_ptr -= inflater->zst.avail_out;
72 switch (rv) {
73 case Z_STREAM_END:
74 inflater->finished = 1;
75 /* FALL THROUGH */
76 case Z_OK:
77 case Z_BUF_ERROR:
78 return 0;
79 case Z_DATA_ERROR:
80 case Z_STREAM_ERROR:
81 case Z_NEED_DICT:
82 case Z_MEM_ERROR:
83 return -1;
84 default:
85 assert(0);
86 /* We need this for some compilers */
87 return 0;
88 }
89 }
90
nghttp2_gzip_inflate_finished(nghttp2_gzip * inflater)91 int nghttp2_gzip_inflate_finished(nghttp2_gzip *inflater) {
92 return inflater->finished;
93 }
94