-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGbCompress.cpp
87 lines (70 loc) · 2 KB
/
GbCompress.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "GbCompress.h"
#include "Mem.h"
#include "Log.h"
#include <string.h>
static void *malloc_replace(void *, unsigned int nitems, unsigned int size) {
return g_mem.gbmalloc(size*nitems,"zlib");
}
static void free_replace(void *, void *s) {
g_mem.gbfree(s,"zlib", 0, false);
}
int gbuncompress(unsigned char *dest, uint32_t *destLen,
const unsigned char *source, uint32_t sourceLen)
{
z_stream stream;
memset(&stream,0,sizeof(stream));
stream.next_in = (Bytef*)source;
stream.avail_in = (uInt)sourceLen;
stream.next_out = dest;
stream.avail_out = (uInt)*destLen;
stream.zalloc = malloc_replace;
stream.zfree = free_replace;
//we can be gzip or deflate
int err = inflateInit2(&stream, 47);
if(err != Z_OK)
return err;
err = inflate(&stream, Z_FINISH);
if(err != Z_STREAM_END) {
inflateEnd(&stream);
if (err == Z_NEED_DICT ||
(err == Z_BUF_ERROR && stream.avail_in == 0))
return Z_DATA_ERROR;
return err;
}
*destLen = stream.total_out;
err = inflateEnd(&stream);
return err;
}
int gbcompress(unsigned char *dest, uint32_t *destLen,
const unsigned char *source, uint32_t sourceLen)
{
z_stream stream;
memset(&stream,0,sizeof(stream));
stream.next_in = (Bytef*)source;
stream.avail_in = (uInt)sourceLen;
stream.next_out = dest;
stream.avail_out = (uInt)*destLen;
stream.zalloc = malloc_replace;
stream.zfree = free_replace;
stream.opaque = (voidpf)0;
//we can be gzip or deflate
int err = deflateInit (&stream, Z_DEFAULT_COMPRESSION);
if(err != Z_OK) {
// zlib's incompatible version error?
if ( err == -6 ) {
log("zlib: zlib did you forget to add #pragma pack(4) to "
"zlib.h when compiling libz.a so it aligns on 4-byte "
"boundaries because we have that pragma in "
"gb-include.h so its used when including zlib.h");
}
return err;
}
err = deflate(&stream, Z_FINISH);
if(err != Z_STREAM_END) {
deflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err;
}
*destLen = stream.total_out;
err = deflateEnd(&stream);
return err;
}