Loading [MathJax]/extensions/MathZoom.js
Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
compression.hxx File Reference
#include <zlib.h>
#include <string>
#include "Exception.hxx"
#include "LOG.hxx"
+ Include dependency graph for compression.hxx:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

std::string compressString (const std::string &stringToCompress)
 Compress a given string with zlib. More...
 
std::string decompressString (const std::string &compressedString)
 decompresses given string More...
 

Variables

constexpr int CHUNK_SIZE = 65536
 

Function Documentation

◆ compressString()

std::string compressString ( const std::string stringToCompress)

Compress a given string with zlib.

Compress the given string. Returns an empty string if something went wrong

Parameters
stringToCompressString that should be compressed
Returns
std::string compressed data
Exceptions
CompressionErrorif Zlib stream initialization fails or there was an error during the compression process

Definition at line 17 of file compression.hxx.

18 {
19  z_stream zstream;
20  std::string compressedString;
21  int deflateResult = Z_OK;
22  char writeBuffer[CHUNK_SIZE];
23 
24  // initialize zstream struct with zeroes, to prevent memory access violation during Initialization.
25  zstream.zalloc = Z_NULL;
26  zstream.zfree = Z_NULL;
27  zstream.opaque = Z_NULL;
28 
29  if (deflateInit(&zstream, Z_BEST_COMPRESSION) != Z_OK)
30  {
31  if (zstream.msg)
32  throw CompressionError(TRACE_INFO "Failed initialize Zlib stream: " + string{zstream.msg});
33  throw CompressionError(TRACE_INFO "Failed initialize Zlib stream");
34  }
35 
36  zstream.avail_in = static_cast<unsigned int>(stringToCompress.size());
37  zstream.next_in = (Bytef *)stringToCompress.data();
38 
39  // run decompression until deflate stream ends
40  do
41  {
42  zstream.next_out = reinterpret_cast<Bytef *>(writeBuffer);
43  zstream.avail_out = sizeof(writeBuffer);
44 
45  deflateResult = deflate(&zstream, Z_FINISH);
46 
47  if (compressedString.size() < zstream.total_out)
48  {
49  compressedString.append(writeBuffer, zstream.total_out - compressedString.size());
50  }
51  } while (zstream.avail_out == 0);
52 
53  deflateEnd(&zstream);
54 
55  if (deflateResult != Z_STREAM_END)
56  {
57  if (zstream.msg)
58  throw CompressionError(TRACE_INFO "Error while compressing file: " + string{zstream.msg});
59  throw CompressionError(TRACE_INFO "Error while compressing file");
60  }
61 
62  return compressedString;
63 }

◆ decompressString()

std::string decompressString ( const std::string compressedString)

decompresses given string

Parameters
compressedStringThe String that should be decompressed
Returns
std::string Uncompressed string
Exceptions
CompressionErrorif Zlib stream initialization fails or there was an error during the decompression process

Definition at line 71 of file compression.hxx.

72 {
73  z_stream zstream;
74  std::string uncompressedString;
75  int inflateResult;
76  char readBuffer[CHUNK_SIZE];
77 
78  // initialize zstream struct with zeroes, to prevent memory access violation during Initialization.
79  zstream.zalloc = Z_NULL;
80  zstream.zfree = Z_NULL;
81  zstream.opaque = Z_NULL;
82 
83  if (inflateInit(&zstream) != Z_OK)
84  {
85  if (zstream.msg)
86  throw CompressionError(TRACE_INFO "Failed initialize Zlib stream: " + string{zstream.msg});
87  throw CompressionError(TRACE_INFO "Failed initialize Zlib stream");
88  }
89 
90  zstream.next_in = (Bytef *)compressedString.data();
91  zstream.avail_in = static_cast<unsigned int>(compressedString.size());
92 
93  // run decompression until buffer is empty
94  do
95  {
96  zstream.next_out = reinterpret_cast<Bytef *>(readBuffer);
97  zstream.avail_out = sizeof(readBuffer);
98 
99  inflateResult = inflate(&zstream, 0);
100 
101  if (uncompressedString.size() < zstream.total_out)
102  {
103  uncompressedString.append(readBuffer, zstream.total_out - uncompressedString.size());
104  }
105 
106  } while (inflateResult == Z_OK);
107 
108  inflateEnd(&zstream);
109 
110  if (inflateResult != Z_STREAM_END)
111  {
112  if (zstream.msg)
113  throw CompressionError(TRACE_INFO "Error while decompressing file: " + string{zstream.msg});
114  throw CompressionError(TRACE_INFO "Error while decompressing file");
115  }
116 
117  return uncompressedString;
118 }

Variable Documentation

◆ CHUNK_SIZE

constexpr int CHUNK_SIZE = 65536
constexpr

Definition at line 8 of file compression.hxx.

TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
CompressionError
A compression error occured.
Definition: Exception.hxx:84
CHUNK_SIZE
constexpr int CHUNK_SIZE
Definition: compression.hxx:8
string
std::string string
Definition: AudioConfig.hxx:14