forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decompress.cc
72 lines (68 loc) · 1.6 KB
/
decompress.cc
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
#include <string.h>
#include "decompress.hh"
#include "zlib.h"
#include "bzlib.h"
string decompressZlib( char * bufptr, unsigned length )
{
z_stream zs;
char buf[2048];
string str;
int res;
memset( &zs, 0, sizeof(zs) );
zs.next_in = (Bytef *)bufptr;
zs.avail_in = length;
while( 1 )
{
res = inflateInit( &zs );
if( res != Z_OK )
break;
while( res != Z_STREAM_END )
{
memset( buf, 0, sizeof(buf) );
zs.next_out = (Bytef *)buf;
zs.avail_out = 2047;
res = inflate( &zs, Z_SYNC_FLUSH );
str += buf;
if( res != Z_OK && res != Z_STREAM_END )
break;
}
break;
}
inflateEnd( &zs );
if( res != Z_STREAM_END )
str.clear();
return str;
}
string decompressBzip2( char * bufptr, unsigned length )
{
bz_stream zs;
char buf[2048];
string str;
int res;
memset( &zs, 0, sizeof(zs) );
zs.next_in = bufptr;
zs.avail_in = length;
zs.total_in_lo32 = length;
while( 1 )
{
res = BZ2_bzDecompressInit( &zs, 0, 0 );
if( res != BZ_OK )
break;
while( res != BZ_STREAM_END )
{
memset( buf, 0, sizeof(buf) );
zs.next_out = buf;
zs.avail_out = 2047;
zs.total_out_lo32 = length;
res = BZ2_bzDecompress( &zs );
str += buf;
if( res != BZ_OK && res != BZ_STREAM_END )
break;
}
break;
}
BZ2_bzDecompressEnd( &zs );
if( res != BZ_STREAM_END )
str.clear();
return str;
}