1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #include "EsiGunzip.h"
25 #include "gzip.h"
26 #include <cctype>
27 #include <cstdint>
28 
29 using std::string;
30 using namespace EsiLib;
31 
EsiGunzip(const char * debug_tag,ComponentBase::Debug debug_func,ComponentBase::Error error_func)32 EsiGunzip::EsiGunzip(const char *debug_tag, ComponentBase::Debug debug_func, ComponentBase::Error error_func)
33   : ComponentBase(debug_tag, debug_func, error_func), _downstream_length(0), _total_data_length(0)
34 {
35   _init    = false;
36   _success = true;
37   // zlib _zstrm variables are initialized when they are required in stream_decode
38   // coverity[uninit_member]
39   // coverity[uninit_ctor]
40 }
41 
42 bool
stream_finish()43 EsiGunzip::stream_finish()
44 {
45   if (_init) {
46     if (inflateEnd(&_zstrm) != Z_OK) {
47       _errorLog("[%s] inflateEnd failed!", __FUNCTION__);
48       _success = false;
49     }
50     _init = false;
51   }
52 
53   return _success;
54 }
55 
56 bool
stream_decode(const char * data,int data_len,std::string & udata)57 EsiGunzip::stream_decode(const char *data, int data_len, std::string &udata)
58 {
59   BufferList buf_list;
60 
61   if (!_init) {
62     _zstrm.zalloc   = Z_NULL;
63     _zstrm.zfree    = Z_NULL;
64     _zstrm.opaque   = Z_NULL;
65     _zstrm.next_in  = nullptr;
66     _zstrm.avail_in = 0;
67 
68     if (inflateInit2(&_zstrm, MAX_WBITS + 16) != Z_OK) {
69       _errorLog("[%s] inflateInit2 failed!", __FUNCTION__);
70       _success = false;
71       return false;
72     }
73     _init = true;
74   }
75 
76   if (data && (data_len > 0)) {
77     _zstrm.next_in  = reinterpret_cast<Bytef *>(const_cast<char *>(data));
78     _zstrm.avail_in = data_len;
79     char raw_buf[BUF_SIZE];
80     int inflate_result;
81     int32_t unzipped_data_size = 0;
82     int32_t curr_buf_size;
83 
84     do {
85       _zstrm.next_out  = reinterpret_cast<Bytef *>(raw_buf);
86       _zstrm.avail_out = BUF_SIZE;
87       inflate_result   = inflate(&_zstrm, Z_SYNC_FLUSH);
88       curr_buf_size    = -1;
89       if ((inflate_result == Z_OK) || (inflate_result == Z_BUF_ERROR)) {
90         curr_buf_size = BUF_SIZE - _zstrm.avail_out;
91       } else if (inflate_result == Z_STREAM_END) {
92         curr_buf_size = BUF_SIZE - _zstrm.avail_out;
93       }
94       if (curr_buf_size > BUF_SIZE) {
95         _errorLog("[%s] buf too large", __FUNCTION__);
96         break;
97       }
98       if (curr_buf_size < 1) {
99         _errorLog("[%s] buf below zero", __FUNCTION__);
100         break;
101       }
102       unzipped_data_size += curr_buf_size;
103 
104       // push empty object onto list and add data to in-list object to
105       // avoid data copy for temporary
106       buf_list.push_back(string());
107       string &curr_buf = buf_list.back();
108       curr_buf.assign(raw_buf, curr_buf_size);
109 
110       if (inflate_result == Z_STREAM_END) {
111         break;
112       }
113     } while (_zstrm.avail_in > 0);
114 
115     _total_data_length += data_len;
116   }
117 
118   for (auto &iter : buf_list) {
119     udata.append(iter.data(), iter.size());
120   }
121 
122   return true;
123 }
124 
~EsiGunzip()125 EsiGunzip::~EsiGunzip()
126 {
127   _downstream_length = 0;
128   _total_data_length = 0;
129   _init              = false;
130   _success           = true;
131 }
132