My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <Core/Core.h>
#include <plugin/bz2/bz2.h>

#ifdef flagWIN32
#include "lib/bzlib.h"
#else
#include <bzlib.h>
#endif

NAMESPACE_UPP

static void* bzalloc_new(void *opaque, int items, int size)
{
return new byte[items * size];
}

static void bzfree_new(void *opaque, void *addr)
{
delete[] (byte *)addr;
}

String BZ2Decompress(String s, Gate2<int, int> progress)
{
if(s.IsEmpty())
return s;
bz_stream z;
Zero(z);
z.bzalloc = bzalloc_new;
z.bzfree = bzfree_new;
z.opaque = 0;
if(BZ2_bzDecompressInit(&z, 0, 0) != BZ_OK)
return String::GetVoid();
int buf_size = minmax(s.GetLength() / 2, 1024, 65536);
Buffer<char> output(buf_size);
z.next_in = (char *)s.Begin();
z.avail_in = s.GetLength();
z.next_out = output;
z.avail_out = buf_size;

String out;
while(BZ2_bzDecompress(&z) == BZ_OK)
{
if(z.avail_out == (dword)buf_size)
{ // no output generated - assume error
BZ2_bzDecompressEnd(&z);
return String::GetVoid();
}
out.Cat(output, buf_size - z.avail_out);
z.next_out = output;
z.avail_out = buf_size;
if(progress((int)(uintptr_t)((const char *)z.next_in - ~s), s.GetLength()))
{
BZ2_bzDecompressEnd(&z);
return String::GetVoid();
}
}
if(z.avail_out < (unsigned)buf_size)
out.Cat(output, buf_size - z.avail_out);

BZ2_bzDecompressEnd(&z);

return out;
}

String BZ2Decompress(Stream& strm, Gate2<int, int> progress)
{
StringStream out;
BZ2Decompress(out, strm, progress);
return out;
}

String BZ2Compress(String s, Gate2<int, int> progress)
{
if(s.IsEmpty())
return s;
bz_stream z;
Zero(z);
z.bzalloc = bzalloc_new;
z.bzfree = bzfree_new;
z.opaque = 0;
if(BZ2_bzCompressInit(&z, 9, 0, 30) != BZ_OK)
return String::GetVoid();
int buf_size = minmax(s.GetLength(), 1024, 65536);
Buffer<char> output(buf_size);
z.next_in = (char *)s.Begin();
z.avail_in = s.GetLength();
z.next_out = output;
z.avail_out = buf_size;

String out;
int res;
while((res = BZ2_bzCompress(&z, z.avail_in ? BZ_RUN : BZ_FINISH)) == BZ_RUN_OK || res == BZ_FINISH_OK)
{
out.Cat(output, buf_size - z.avail_out);
z.next_out = output;
z.avail_out = buf_size;
if(progress((int)(uintptr_t)((const char *)z.next_in - ~s), s.GetLength()))
{
BZ2_bzCompressEnd(&z);
return String::GetVoid();
}
}
out.Cat(output, buf_size - z.avail_out);
BZ2_bzCompressEnd(&z);
if(res != BZ_STREAM_END)
out = String::GetVoid();
return out;
}

void BZ2Decompress(Stream& out, Stream& in, Gate2<int, int> progress)
{
enum { BUF_SIZE = 65536 };
Buffer<char> input(BUF_SIZE), output(BUF_SIZE);
int avail = in.Get(input, BUF_SIZE);
if(avail == 0)
return;
bz_stream z;
Zero(z);
z.bzalloc = bzalloc_new;
z.bzfree = bzfree_new;
z.opaque = 0;
if(BZ2_bzDecompressInit(&z, 0, 0) != BZ_OK)
{
out.SetError();
return;
}
z.next_in = input;
z.avail_in = avail;
z.next_out = output;
z.avail_out = BUF_SIZE;
int code;
bool running = true;
int64 total = in.GetLeft();
int done = 0;
do
{
if(z.avail_in == 0 && running)
{
if((z.avail_in = in.Get(z.next_in = input, BUF_SIZE)) == 0)
running = false;
done += z.avail_in;
if(progress(done, (int)total) || in.IsError())
{
BZ2_bzDecompressEnd(&z);
out.SetError();
return;
}
}
code = BZ2_bzDecompress(&z);
if(z.avail_out == 0)
{
out.Put(z.next_out = output, z.avail_out = BUF_SIZE);
if(out.IsError())
{
BZ2_bzDecompressEnd(&z);
return;
}
}
}
while(code == BZ_OK);
if(z.avail_out < BUF_SIZE)
out.Put(output, BUF_SIZE - z.avail_out);
BZ2_bzDecompressEnd(&z);
}

void BZ2Compress(Stream& out, Stream& in, Gate2<int, int> progress)
{
enum { BUF_SIZE = 65536 };
Buffer<char> input(BUF_SIZE), output(BUF_SIZE);
bz_stream z;
z.bzalloc = bzalloc_new;
z.bzfree = bzfree_new;
z.opaque = 0;
if(BZ2_bzCompressInit(&z, 9, 0, 30) != BZ_OK)
{
out.SetError();
return;
}
z.avail_in = 0;
z.avail_out = BUF_SIZE;
z.next_out = output;
int code;
int flush = BZ_RUN;
int64 total = in.GetLeft();
int done = 0;
do
{
if(z.avail_in == 0 && flush == BZ_RUN)
{
z.next_in = input;
if((z.avail_in = in.Get(z.next_in = input, BUF_SIZE)) == 0)
flush = BZ_FINISH;
done += z.avail_in;
if(progress(done, (int)total) || in.IsError())
{
BZ2_bzCompressEnd(&z);
out.SetError();
return;
}
}
code = BZ2_bzCompress(&z, flush);
if(z.avail_out == 0)
{
out.Put(z.next_out = output, z.avail_out = BUF_SIZE);
if(out.IsError())
{
BZ2_bzCompressEnd(&z);
return;
}
}
}
while(code == BZ_RUN_OK || code == BZ_FINISH_OK);
if(z.avail_out < BUF_SIZE)
out.Put(output, BUF_SIZE - z.avail_out);
BZ2_bzCompressEnd(&z);
if(code != BZ_STREAM_END)
out.SetError();
}

END_UPP_NAMESPACE

Change log

r4502 by cxl on Jan 31, 2012   Diff
plugin/bz2: Moving to .so in posix
Go to: 
Project members, sign in to write a code review

Older revisions

r300 by mdelfede on Jun 16, 2008   Diff
new uvs2 releases : uppsrc-2598
tutorial-38  examples-141
reference-113
r281 by mdelfede on Jun 7, 2008   Diff
changed svn layout
r216 by mdelfede on Apr 17, 2008   Diff
new uvs2 releases : uppsrc-2516
tutorial-38  examples-140
reference-110
All revisions of this file

File info

Size: 4701 bytes, 220 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting