gloox  1.0.28
compressionzlib.cpp
1 /*
2  Copyright (c) 2005-2023 by Jakob Schröter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #include "compressionzlib.h"
16 
17 #ifdef HAVE_ZLIB
18 
19 namespace gloox
20 {
21 
23  : CompressionBase( cdh )
24  {
25  }
26 
28  {
29  int ret = Z_OK;
30  m_zinflate.zalloc = Z_NULL;
31  m_zinflate.zfree = Z_NULL;
32  m_zinflate.opaque = Z_NULL;
33  m_zinflate.avail_in = 0;
34  m_zinflate.next_in = Z_NULL;
35  ret = inflateInit( &m_zinflate );
36  if( ret != Z_OK )
37  return false;
38 
39  m_zdeflate.zalloc = Z_NULL;
40  m_zdeflate.zfree = Z_NULL;
41  m_zdeflate.opaque = Z_NULL;
42  m_zinflate.avail_in = 0;
43  m_zinflate.next_in = Z_NULL;
44  ret = deflateInit( &m_zdeflate, Z_BEST_COMPRESSION/*Z_DEFAULT_COMPRESSION*/ );
45  if( ret != Z_OK )
46  return false;
47 
48  m_valid = true;
49  return true;
50  }
51 
53  {
54  cleanup();
55  }
56 
57  void CompressionZlib::compress( const std::string& data )
58  {
59  if( !m_valid )
60  init();
61 
62  if( !m_valid || !m_handler || data.empty() )
63  return;
64 
65  long unsigned int CHUNK = data.length() + ( data.length() / 100 ) + 13;
66  Bytef* out = new Bytef[CHUNK];
67  char* in = const_cast<char*>( data.c_str() );
68 
69  m_compressMutex.lock();
70 
71  m_zdeflate.avail_in = static_cast<uInt>( data.length() );
72  m_zdeflate.next_in = reinterpret_cast<Bytef*>( in );
73 
74  std::string result;
75  do {
76  m_zdeflate.avail_out = static_cast<uInt>( CHUNK );
77  m_zdeflate.next_out = reinterpret_cast<Bytef*>( out );
78 
79  deflate( &m_zdeflate, Z_SYNC_FLUSH );
80  result.append( reinterpret_cast<char*>( out ), CHUNK - m_zdeflate.avail_out );
81  } while( m_zdeflate.avail_out == 0 );
82 
83  m_compressMutex.unlock();
84 
85  delete[] out;
86 
88  }
89 
90  void CompressionZlib::decompress( const std::string& data )
91  {
92  if( !m_valid )
93  init();
94 
95  if( !m_valid || !m_handler || data.empty() )
96  return;
97 
98  int CHUNK = 50;
99  char* out = new char[CHUNK];
100  char* in = const_cast<char*>( data.c_str() );
101 
102  m_zinflate.avail_in = static_cast<uInt>( data.length() );
103  m_zinflate.next_in = reinterpret_cast<Bytef*>( in );
104 
105  std::string result;
106  do
107  {
108  m_zinflate.avail_out = CHUNK;
109  m_zinflate.next_out = reinterpret_cast<Bytef*>( out );
110 
111  inflate( &m_zinflate, Z_SYNC_FLUSH );
112  result.append( out, CHUNK - m_zinflate.avail_out );
113  } while( m_zinflate.avail_out == 0 );
114 
115  delete[] out;
116 
118  }
119 
121  {
122  m_compressMutex.lock();
123 
124  if( m_valid )
125  {
126  inflateEnd( &m_zinflate );
127  deflateEnd( &m_zdeflate );
128 
129  m_valid = false;
130  }
131 
132  m_compressMutex.unlock();
133  }
134 
135 }
136 
137 #endif // HAVE_ZLIB
This is an abstract base class for stream compression implementations.
CompressionDataHandler * m_handler
An abstract base class used to receive de/compressed data from a CompressionBase-derived object.
virtual void handleCompressedData(const std::string &data)=0
virtual void handleDecompressedData(const std::string &data)=0
CompressionZlib(CompressionDataHandler *cdh)
virtual void compress(const std::string &data)
virtual void decompress(const std::string &data)
The namespace for the gloox library.
Definition: adhoc.cpp:28