Skip to content

Commit 28e9450

Browse files
committed
Zlib moved into separate module.
1 parent 6133aff commit 28e9450

File tree

5 files changed

+122
-48
lines changed

5 files changed

+122
-48
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ set(mapper_HDRS
5757
PixelAttributes.h
5858
PlayerAttributes.h
5959
TileGenerator.h
60+
ZlibDecompressor.h
6061
)
6162

6263
set(mapper_SRCS
6364
PixelAttributes.cpp
6465
PlayerAttributes.cpp
6566
TileGenerator.cpp
67+
ZlibDecompressor.cpp
6668
mapper.cpp
6769
)
6870

TileGenerator.cpp

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#include <gdfontmb.h>
1414
#include <iostream>
1515
#include <sstream>
16-
#include <zlib.h>
1716
#include "config.h"
1817
#include "PlayerAttributes.h"
1918
#include "TileGenerator.h"
19+
#include "ZlibDecompressor.h"
2020
#include "colors.h"
2121

2222
using namespace std;
@@ -70,40 +70,6 @@ static inline int readBlockContent(const unsigned char *mapData, int version, in
7070
}
7171
}
7272

73-
static inline std::string zlibDecompress(const unsigned char *data, std::size_t size, std::size_t *processed)
74-
{
75-
string buffer;
76-
const size_t BUFSIZE = 128 * 1024;
77-
uint8_t temp_buffer[BUFSIZE];
78-
79-
z_stream strm;
80-
strm.zalloc = Z_NULL;
81-
strm.zfree = Z_NULL;
82-
strm.opaque = Z_NULL;
83-
strm.next_in = Z_NULL;
84-
strm.avail_in = size;
85-
86-
if (inflateInit(&strm) != Z_OK) {
87-
throw DecompressError();
88-
}
89-
90-
strm.next_in = const_cast<unsigned char *>(data);
91-
int ret = 0;
92-
do {
93-
strm.avail_out = BUFSIZE;
94-
strm.next_out = temp_buffer;
95-
ret = inflate(&strm, Z_NO_FLUSH);
96-
buffer += string(reinterpret_cast<char *>(temp_buffer), BUFSIZE - strm.avail_out);
97-
} while (ret == Z_OK);
98-
if (ret != Z_STREAM_END) {
99-
throw DecompressError();
100-
}
101-
*processed = strm.next_in - data;
102-
(void)inflateEnd(&strm);
103-
104-
return buffer;
105-
}
106-
10773
static inline int colorSafeBounds(int color)
10874
{
10975
if (color > 255) {
@@ -371,11 +337,12 @@ void TileGenerator::renderMap()
371337
else {
372338
dataOffset = 2;
373339
}
374-
size_t processed;
375-
string mapData = zlibDecompress(data + dataOffset, length - dataOffset, &processed);
376-
dataOffset += processed;
377-
string mapMetadata = zlibDecompress(data + dataOffset, length - dataOffset, &processed);
378-
dataOffset += processed;
340+
341+
ZlibDecompressor decompressor(data, length);
342+
decompressor.setSeekPos(dataOffset);
343+
ZlibDecompressor::string mapData = decompressor.decompress();
344+
ZlibDecompressor::string mapMetadata = decompressor.decompress();
345+
dataOffset = decompressor.seekPos();
379346

380347
// Skip unused data
381348
if (version <= 21) {
@@ -455,11 +422,11 @@ void TileGenerator::renderMap()
455422
}
456423
}
457424

458-
inline void TileGenerator::renderMapBlock(const std::string &mapBlock, const BlockPos &pos, int version)
425+
inline void TileGenerator::renderMapBlock(const unsigned_string &mapBlock, const BlockPos &pos, int version)
459426
{
460427
int xBegin = (pos.x - m_xMin) * 16;
461428
int zBegin = (m_zMax - pos.z) * 16;
462-
const unsigned char *mapData = reinterpret_cast<const unsigned char *>(mapBlock.c_str());
429+
const unsigned char *mapData = mapBlock.c_str();
463430
for (int z = 0; z < 16; ++z) {
464431
int imageY = getImageY(zBegin + 15 - z);
465432
for (int x = 0; x < 16; ++x) {
@@ -608,7 +575,7 @@ std::map<int, TileGenerator::BlockList> TileGenerator::getBlocksOnZ(int zPos, sq
608575
const unsigned char *data = reinterpret_cast<const unsigned char *>(sqlite3_column_blob(statement, 1));
609576
int size = sqlite3_column_bytes(statement, 1);
610577
BlockPos pos = decodeBlockPos(blocknum);
611-
blocks[pos.x].push_back(Block(pos, std::basic_string<unsigned char>(data, size)));
578+
blocks[pos.x].push_back(Block(pos, unsigned_string(data, size)));
612579
}
613580
else {
614581
break;

TileGenerator.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,16 @@ class DbError {
6262
class ColorError {
6363
};
6464

65-
class DecompressError {
66-
};
67-
6865
class VersionError {
6966
};
7067

7168

7269
class TileGenerator
7370
{
7471
private:
72+
typedef std::basic_string<unsigned char> unsigned_string;
7573
typedef std::map<std::string, Color> ColorMap;
76-
typedef std::pair<BlockPos, std::basic_string<unsigned char> > Block;
74+
typedef std::pair<BlockPos, unsigned_string> Block;
7775
typedef std::list<Block> BlockList;
7876

7977
public:
@@ -98,7 +96,7 @@ class TileGenerator
9896
void renderMap();
9997
std::list<int> getZValueList() const;
10098
std::map<int, BlockList> getBlocksOnZ(int zPos, sqlite3_stmt *statement) const;
101-
void renderMapBlock(const std::string &mapBlock, const BlockPos &pos, int version);
99+
void renderMapBlock(const unsigned_string &mapBlock, const BlockPos &pos, int version);
102100
void renderShading(int zPos);
103101
void renderScale();
104102
void renderOrigin();

ZlibDecompressor.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* =====================================================================
3+
* Version: 1.0
4+
* Created: 18.09.2012 10:20:47
5+
* Author: Miroslav Bendík
6+
* Company: LinuxOS.sk
7+
* =====================================================================
8+
*/
9+
10+
#include <zlib.h>
11+
#include "ZlibDecompressor.h"
12+
13+
ZlibDecompressor::ZlibDecompressor(const unsigned char *data, std::size_t size):
14+
m_data(data),
15+
m_seekPos(0),
16+
m_size(size)
17+
{
18+
}
19+
20+
ZlibDecompressor::~ZlibDecompressor()
21+
{
22+
}
23+
24+
void ZlibDecompressor::setSeekPos(std::size_t seekPos)
25+
{
26+
m_seekPos = seekPos;
27+
}
28+
29+
std::size_t ZlibDecompressor::seekPos() const
30+
{
31+
return m_seekPos;
32+
}
33+
34+
ZlibDecompressor::string ZlibDecompressor::decompress()
35+
{
36+
const unsigned char *data = m_data + m_seekPos;
37+
const std::size_t size = m_size - m_seekPos;
38+
39+
string buffer;
40+
const size_t BUFSIZE = 128 * 1024;
41+
uint8_t temp_buffer[BUFSIZE];
42+
43+
z_stream strm;
44+
strm.zalloc = Z_NULL;
45+
strm.zfree = Z_NULL;
46+
strm.opaque = Z_NULL;
47+
strm.next_in = Z_NULL;
48+
strm.avail_in = size;
49+
50+
if (inflateInit(&strm) != Z_OK) {
51+
throw DecompressError();
52+
}
53+
54+
strm.next_in = const_cast<unsigned char *>(data);
55+
int ret = 0;
56+
do {
57+
strm.avail_out = BUFSIZE;
58+
strm.next_out = temp_buffer;
59+
ret = inflate(&strm, Z_NO_FLUSH);
60+
buffer += string(reinterpret_cast<unsigned char *>(temp_buffer), BUFSIZE - strm.avail_out);
61+
} while (ret == Z_OK);
62+
if (ret != Z_STREAM_END) {
63+
throw DecompressError();
64+
}
65+
m_seekPos += strm.next_in - data;
66+
(void)inflateEnd(&strm);
67+
68+
return buffer;
69+
}
70+

ZlibDecompressor.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* =====================================================================
3+
* Version: 1.0
4+
* Created: 18.09.2012 10:20:51
5+
* Author: Miroslav Bendík
6+
* Company: LinuxOS.sk
7+
* =====================================================================
8+
*/
9+
10+
#ifndef ZLIBDECOMPRESSOR_H_ZQL1PN8Q
11+
#define ZLIBDECOMPRESSOR_H_ZQL1PN8Q
12+
13+
#include <cstdlib>
14+
#include <string>
15+
16+
17+
class ZlibDecompressor
18+
{
19+
public:
20+
typedef std::basic_string<unsigned char> string;
21+
class DecompressError {
22+
};
23+
24+
ZlibDecompressor(const unsigned char *data, std::size_t size);
25+
~ZlibDecompressor();
26+
void setSeekPos(std::size_t seekPos);
27+
std::size_t seekPos() const;
28+
string decompress();
29+
30+
private:
31+
const unsigned char *m_data;
32+
std::size_t m_seekPos;
33+
std::size_t m_size;
34+
}; /* ----- end of class ZlibDecompressor ----- */
35+
36+
#endif /* end of include guard: ZLIBDECOMPRESSOR_H_ZQL1PN8Q */
37+

0 commit comments

Comments
 (0)