World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
ZlibWrapper.cpp
См. документацию.
1 #include "Engine/ZlibWrapper.h"
2 #include <string.h>
3 
4 namespace zlib {
5 #include <zlib.h>
6 
7 int Uncompress(void *dest, unsigned int *destLen, const void *source, unsigned int sourceLen) {
8  return uncompress((Bytef *)dest, (uLongf *)destLen, (Bytef *)source, sourceLen);
9 }
10 
11 int Compress(void *dest, unsigned int *destLen, void *source, unsigned int sourceLen) {
12  return compress((Bytef *)dest, (uLongf *)destLen, (Bytef *)source, sourceLen);
13 }
14 
16  uLongf destLen = source->GetSize();
17  Bytef *dest = nullptr;
18  int res = Z_BUF_ERROR;
19  while (res == Z_BUF_ERROR) {
20  if (dest != nullptr) {
21  free(dest);
22  dest = nullptr;
23  destLen *= 2;
24  }
25  dest = (Bytef*)malloc(destLen);
26  res = compress(dest, &destLen, (Bytef*)source->GetData(), source->GetSize());
27  }
28 
29  PMemBuffer result = nullptr;
30  if (res == Z_OK) {
31  result = AllocMemBuffer(destLen);
32  memcpy((void*)result->GetData(), dest, destLen);
33  }
34  free(dest);
35  return result;
36 }
37 
39  uLongf destLen = source->GetSize() * 4;
40  Bytef *dest = nullptr;
41  int res = Z_BUF_ERROR;
42  while (res == Z_BUF_ERROR) {
43  if (dest != nullptr) {
44  free(dest);
45  dest = nullptr;
46  destLen *= 2;
47  }
48  dest = (Bytef*)malloc(destLen);
49  res = uncompress(dest, &destLen, (Bytef*)source->GetData(), source->GetSize());
50  }
51 
52  PMemBuffer result = nullptr;
53  if (res == Z_OK) {
54  result = AllocMemBuffer(destLen);
55  memcpy((void*)result->GetData(), dest, destLen);
56  }
57  free(dest);
58  return result;
59 }
60 
61 }; // namespace zlib
Bytef
Byte FAR Bytef
Definition: zconf.h:402
source
GLsizei GLsizei GLchar * source
Definition: SDL_opengl_glext.h:680
zlib::Compress
int Compress(void *dest, unsigned int *destLen, void *source, unsigned int sourceLen)
Definition: ZlibWrapper.cpp:11
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9435
AllocMemBuffer
PMemBuffer AllocMemBuffer(size_t size)
Definition: MemBuffer.cpp:20
ZlibWrapper.h
uLongf
uLong FAR uLongf
Definition: zconf.h:407
zlib
Definition: ZlibWrapper.cpp:4
zlib::Uncompress
int Uncompress(void *dest, unsigned int *destLen, const void *source, unsigned int sourceLen)
Definition: ZlibWrapper.cpp:7
res
GLuint res
Definition: SDL_opengl_glext.h:7940
PMemBuffer
std::shared_ptr< IMemBuffer > PMemBuffer
Definition: MemBuffer.h:13
zlib.h