xref: /trafficserver/plugins/esi/lib/EsiGzip.cc (revision 19e80414)
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 "EsiGzip.h"
25 #include "gzip.h"
26 #include <cctype>
27 #include <cstdint>
28 
29 using std::string;
30 using namespace EsiLib;
31 
EsiGzip(const char * debug_tag,ComponentBase::Debug debug_func,ComponentBase::Error error_func)32 EsiGzip::EsiGzip(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   // Zlib _zstrm variables are initialized when they are required in runDeflateLoop
36   // coverity[uninit_member]
37   // coverity[uninit_ctor]
38 }
39 
40 template <typename T>
41 inline void
append(string & out,T data)42 append(string &out, T data)
43 {
44   for (unsigned int i = 0; i < sizeof(data); ++i) {
45     out += static_cast<char>(data & 0xff);
46     data = data >> 8;
47   }
48 }
49 
50 inline int
runDeflateLoop(z_stream & zstrm,int flush,std::string & cdata)51 runDeflateLoop(z_stream &zstrm, int flush, std::string &cdata)
52 {
53   char buf[BUF_SIZE];
54   int deflate_result = Z_OK;
55   do {
56     zstrm.next_out  = reinterpret_cast<Bytef *>(buf);
57     zstrm.avail_out = BUF_SIZE;
58     deflate_result  = deflate(&zstrm, flush);
59     if ((deflate_result == Z_OK) || (deflate_result == Z_STREAM_END)) {
60       cdata.append(buf, BUF_SIZE - zstrm.avail_out);
61       if ((deflate_result == Z_STREAM_END) || zstrm.avail_out > 6) {
62         break;
63       }
64     } else {
65       break;
66     }
67   } while (true);
68   return deflate_result;
69 }
70 
71 bool
stream_encode(const char * data,int data_len,std::string & cdata)72 EsiGzip::stream_encode(const char *data, int data_len, std::string &cdata)
73 {
74   if (_downstream_length == 0) {
75     cdata.assign(GZIP_HEADER_SIZE, 0); // reserving space for the header
76     cdata[0] = MAGIC_BYTE_1;
77     cdata[1] = MAGIC_BYTE_2;
78     cdata[2] = Z_DEFLATED;
79     cdata[9] = OS_TYPE;
80 
81     _crc = crc32(0, Z_NULL, 0);
82   }
83 
84   _zstrm.zalloc = Z_NULL;
85   _zstrm.zfree  = Z_NULL;
86   _zstrm.opaque = Z_NULL;
87   if (deflateInit2(&_zstrm, COMPRESSION_LEVEL, Z_DEFLATED, -MAX_WBITS, ZLIB_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {
88     _errorLog("[%s] deflateInit2 failed!", __FUNCTION__);
89     return false;
90   }
91 
92   int deflate_result = Z_OK;
93   if (data && (data_len > 0)) {
94     _zstrm.next_in  = reinterpret_cast<Bytef *>(const_cast<char *>(data));
95     _zstrm.avail_in = data_len;
96     deflate_result  = runDeflateLoop(_zstrm, Z_FULL_FLUSH, cdata);
97     if (deflate_result != Z_OK) {
98       _errorLog("[%s] runDeflateLoop failed!", __FUNCTION__);
99 
100       deflateEnd(&_zstrm);
101 
102       return false;
103     }
104     _crc = crc32(_crc, reinterpret_cast<const Bytef *>(data), data_len);
105     _downstream_length += cdata.size();
106     _total_data_length += data_len;
107   }
108 
109   deflateEnd(&_zstrm);
110 
111   return true;
112 }
113 
114 bool
stream_finish(std::string & cdata,int & downstream_length)115 EsiGzip::stream_finish(std::string &cdata, int &downstream_length)
116 {
117   char buf[BUF_SIZE];
118 
119   _zstrm.zalloc = Z_NULL;
120   _zstrm.zfree  = Z_NULL;
121   _zstrm.opaque = Z_NULL;
122   if (deflateInit2(&_zstrm, COMPRESSION_LEVEL, Z_DEFLATED, -MAX_WBITS, ZLIB_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {
123     _errorLog("[%s] deflateInit2 failed!", __FUNCTION__);
124     return false;
125   }
126 
127   _zstrm.next_in  = reinterpret_cast<Bytef *>(buf);
128   _zstrm.avail_in = 0;
129   // required for the "finish" loop as no data has been given so far
130   int deflate_result = runDeflateLoop(_zstrm, Z_FINISH, cdata);
131   deflateEnd(&_zstrm);
132   if (deflate_result != Z_STREAM_END) {
133     _errorLog("[%s] deflateEnd failed!", __FUNCTION__);
134     downstream_length = 0;
135     return false;
136   }
137   append(cdata, static_cast<uint32_t>(_crc));
138   append(cdata, static_cast<int32_t>(_total_data_length));
139   _downstream_length += cdata.size();
140   downstream_length = _downstream_length;
141   return true;
142 }
143 
~EsiGzip()144 EsiGzip::~EsiGzip()
145 {
146   _downstream_length = 0;
147   _total_data_length = 0;
148 }
149