EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Deflate.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #include "eve-core.h"
27 
28 #include "utils/Deflate.h"
29 
30 const uint8 DeflateHeaderByte = 0x78; //'x'
31 
32 bool IsDeflated( const Buffer& data )
33 {
34  return ( DeflateHeaderByte == data[0] );
35 }
36 
37 bool DeflateData( Buffer& data )
38 {
39  Buffer dataDeflated;
40  if( !DeflateData( data, dataDeflated ) )
41  return false;
42 
43  data = dataDeflated;
44  return true;
45 }
46 
47 bool DeflateData( const Buffer& input, Buffer& output )
48 {
49  const Buffer::iterator<uint8> out = output.end<uint8>();
50 
51  size_t outputSize = compressBound( input.size() );
52  output.ResizeAt( out, outputSize );
53 
54  int res = compress( &*out, (uLongf*)&outputSize, &input[0], input.size() );
55 
56  if( Z_OK == res )
57  {
58  output.ResizeAt( out, outputSize );
59  return true;
60  }
61  else
62  {
63  output.ResizeAt( out, 0 );
64  return false;
65  }
66 }
67 
68 bool InflateData( Buffer& data )
69 {
70  Buffer dataInflated;
71  if( !InflateData( data, dataInflated ) )
72  return false;
73 
74  data = dataInflated;
75  return true;
76 }
77 
78 bool InflateData( const Buffer& input, Buffer& output )
79 {
80  const Buffer::iterator<uint8> out = output.end<uint8>();
81 
82  size_t outputSize = 0;
83  size_t sizeMultiplier = 0;
84 
85  int res = 0;
86  do
87  {
88  outputSize = ( input.size() << ++sizeMultiplier );
89  output.ResizeAt( out, outputSize );
90 
91  res = uncompress( &*out, (uLongf*)&outputSize, &input[0], input.size() );
92  } while( Z_BUF_ERROR == res );
93 
94  if( Z_OK == res )
95  {
96  output.ResizeAt( out, outputSize );
97  return true;
98  }
99  else
100  {
101  output.ResizeAt( out, 0 );
102  return false;
103  }
104 }
void ResizeAt(const_iterator< T > index, size_type requiredCount, const uint8 &fill=0)
Resizes buffer.
Definition: Buffer.h:688
unsigned __int8 uint8
Definition: eve-compat.h:46
Generic class for buffers.
Definition: Buffer.h:40
bool DeflateData(Buffer &data)
Deflates given data.
Definition: Deflate.cpp:37
bool IsDeflated(const Buffer &data)
Checks whether given data is deflated.
Definition: Deflate.cpp:32
bool InflateData(Buffer &data)
Inflates given data.
Definition: Deflate.cpp:68
const uint8 DeflateHeaderByte
Definition: Deflate.cpp:30
size_type size() const
Definition: Buffer.h:610
Buffer's iterator.
Definition: Buffer.h:232
iterator< T > end()
Definition: Buffer.h:387