1 /*
2   Copyright (c) 2015, 2021, Oracle and/or its affiliates.
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2.0,
6   as published by the Free Software Foundation.
7 
8   This program is also distributed with certain software (including
9   but not limited to OpenSSL) that is licensed under separate terms,
10   as designated in a particular file or component or in included license
11   documentation.  The authors of MySQL hereby grant you an additional
12   permission to link the program and your derivative works with the
13   separately licensed software that they have included with MySQL.
14 
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License, version 2.0, for more details.
19 
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "compression_zlib_writer.h"
26 
27 using namespace Mysql::Tools::Dump;
28 
process_buffer(bool flush_stream)29 void Compression_zlib_writer::process_buffer(bool flush_stream)
30 {
31   do
32   {
33     m_compression_context.avail_out= Compression_zlib_writer::buffer_size;
34     m_compression_context.next_out= (Bytef*)&m_buffer[0];
35 
36     int res= deflate(&m_compression_context,
37       flush_stream ? Z_FINISH : Z_NO_FLUSH);
38 
39     if (res == Z_STREAM_ERROR)
40     {
41       this->pass_message(Mysql::Tools::Base::Message_data(
42         0, "zlib compression failed",
43         Mysql::Tools::Base::Message_type_error));
44     }
45 
46     this->append_output(std::string(&m_buffer[0],
47       Compression_zlib_writer::buffer_size - m_compression_context.avail_out));
48   }
49   while (m_compression_context.avail_in > 0);
50 }
51 
append(const std::string & data_to_append)52 void Compression_zlib_writer::append(const std::string& data_to_append)
53 {
54   my_boost::mutex::scoped_lock lock(m_zlib_mutex);
55   m_compression_context.avail_in= data_to_append.size();
56   m_compression_context.next_in= (Bytef*)data_to_append.c_str();
57   this->process_buffer(false);
58 }
59 
~Compression_zlib_writer()60 Compression_zlib_writer::~Compression_zlib_writer()
61 {
62   my_boost::mutex::scoped_lock lock(m_zlib_mutex);
63   this->process_buffer(true);
64   deflateEnd(&m_compression_context);
65 }
66 
Compression_zlib_writer(Mysql::I_callable<bool,const Mysql::Tools::Base::Message_data &> * message_handler,Simple_id_generator * object_id_generator,uint compression_level)67 Compression_zlib_writer::Compression_zlib_writer(
68   Mysql::I_callable<bool, const Mysql::Tools::Base::Message_data&>*
69     message_handler, Simple_id_generator* object_id_generator,
70     uint compression_level)
71   : Abstract_output_writer_wrapper(message_handler, object_id_generator)
72 {
73   memset(&m_compression_context, 0, sizeof(m_compression_context));
74   m_buffer.resize(Compression_zlib_writer::buffer_size);
75   int ret = deflateInit(&m_compression_context, compression_level);
76   if (ret != Z_OK)
77   {
78     this->pass_message(Mysql::Tools::Base::Message_data(
79       0, "zlib compression initialization failed",
80       Mysql::Tools::Base::Message_type_error));
81   }
82 }
83