Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | |||
29 | #include "gate/encode/bzip2streams.h" | ||
30 | #include "gate/memalloc.h" | ||
31 | #include "gate/atomics.h" | ||
32 | #include "gate/results.h" | ||
33 | #include "gate/debugging.h" | ||
34 | |||
35 | #if defined(GATE_EXTLIB_BZIP2) && !defined(GATE_ENCODE_NO_ADVANCED_COMPRESSION) | ||
36 | # define GATE_ENCODE_BZIP2_USE_LIBBZ2 1 | ||
37 | #else | ||
38 | # define GATE_ENCODE_BZIP2_NO_IMPL 1 | ||
39 | #endif | ||
40 | |||
41 | |||
42 | |||
43 | #if defined(GATE_ENCODE_BZIP2_USE_LIBBZ2) | ||
44 | #include "bzlib.h" | ||
45 | |||
46 | #include <assert.h> | ||
47 | |||
48 | /* output stream encoder implementation */ | ||
49 | |||
50 | static void gate_bzip2encoder_release_impl(void* self); | ||
51 | static int gate_bzip2encoder_retain_impl(void* self); | ||
52 | ✗ | static char const* gate_bzip2encoder_interface_name_impl(void* self) | |
53 | { | ||
54 | (void)self; | ||
55 | ✗ | return GATE_INTERFACE_NAME_STREAM; | |
56 | } | ||
57 | static gate_result_t gate_bzip2encoder_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned); | ||
58 | static gate_result_t gate_bzip2encoder_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned); | ||
59 | static gate_result_t gate_bzip2encoder_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written); | ||
60 | static gate_result_t gate_bzip2encoder_flush_impl(void* self); | ||
61 | |||
62 | static GATE_INTERFACE_VTBL(gate_stream) gate_bzip2encoder_vtbl; | ||
63 | 1 | static void gate_init_bzip2encoder_vtbl() | |
64 | { | ||
65 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!gate_bzip2encoder_vtbl.get_interface_name) |
66 | { | ||
67 | GATE_INTERFACE_VTBL(gate_stream) const local_vtbl = | ||
68 | { | ||
69 | &gate_bzip2encoder_interface_name_impl, | ||
70 | &gate_bzip2encoder_release_impl, | ||
71 | &gate_bzip2encoder_retain_impl, | ||
72 | &gate_bzip2encoder_read_impl, | ||
73 | &gate_bzip2encoder_peek_impl, | ||
74 | &gate_bzip2encoder_write_impl, | ||
75 | &gate_bzip2encoder_flush_impl | ||
76 | }; | ||
77 | 1 | gate_bzip2encoder_vtbl = local_vtbl; | |
78 | } | ||
79 | 1 | } | |
80 | |||
81 | typedef struct gate_bzip2encoder_impl | ||
82 | { | ||
83 | GATE_INTERFACE_VTBL(gate_stream)* vtbl; | ||
84 | |||
85 | gate_atomic_int_t ref_counter; | ||
86 | gate_stream_t* dest_stream; | ||
87 | bz_stream encoder; | ||
88 | char data_buffer[GATE_MAX_BLOCK_COPYBUFFER_LENGTH]; | ||
89 | gate_size_t data_buffer_used; | ||
90 | |||
91 | } gate_bzip2encoder_impl_t; | ||
92 | |||
93 | 4 | static void* gate_bzip2_bzalloc(void* opaque, int items, int size) | |
94 | { | ||
95 | 4 | void* ret = NULL; | |
96 | 4 | gate_size_t mem_alloc_size = (gate_size_t)items * (gate_size_t)size; | |
97 | |||
98 | (void)opaque; | ||
99 | 4 | ret = gate_mem_alloc(mem_alloc_size); | |
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!ret) |
101 | { | ||
102 | ✗ | GATE_DEBUG_ASSERT("bzalloc() failed"); | |
103 | } | ||
104 | 4 | return ret; | |
105 | } | ||
106 | |||
107 | 4 | static void gate_bzip2_bzfree(void* opaque, void* addr) | |
108 | { | ||
109 | (void)opaque; | ||
110 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (addr) |
111 | { | ||
112 | 4 | gate_mem_dealloc(addr); | |
113 | } | ||
114 | 4 | } | |
115 | |||
116 | 1 | gate_result_t gate_bzip2_encoder_create(gate_stream_t* output, gate_int16_t compression, gate_stream_t** ptr2streamptr) | |
117 | { | ||
118 | gate_result_t ret; | ||
119 | gate_bzip2encoder_impl_t* impl; | ||
120 | int bzresult; | ||
121 | 1 | int level = 5; | |
122 | do | ||
123 | { | ||
124 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if ((output == NULL) || (ptr2streamptr == NULL)) |
125 | { | ||
126 | ✗ | GATE_DEBUG_TRACE("bzip2_encoder null pointer"); | |
127 | ✗ | ret = GATE_RESULT_NULLPOINTER; | |
128 | ✗ | break; | |
129 | } | ||
130 | |||
131 | 1 | impl = (gate_bzip2encoder_impl_t*)gate_mem_alloc(sizeof(gate_bzip2encoder_impl_t)); | |
132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (impl == NULL) |
133 | { | ||
134 | ✗ | GATE_DEBUG_TRACE("failed to allocate bzip2encoder"); | |
135 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
136 | ✗ | break; | |
137 | } | ||
138 | |||
139 | 1 | gate_mem_clear(impl, sizeof(gate_bzip2encoder_impl_t)); | |
140 | 1 | gate_init_bzip2encoder_vtbl(); | |
141 | 1 | impl->vtbl = &gate_bzip2encoder_vtbl; | |
142 | 1 | gate_atomic_int_init(&impl->ref_counter, 1); | |
143 | 1 | impl->encoder.bzalloc = &gate_bzip2_bzalloc; | |
144 | 1 | impl->encoder.bzfree = &gate_bzip2_bzfree; | |
145 | 1 | impl->dest_stream = output; | |
146 | |||
147 |
1/6✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1 | switch (compression) |
148 | { | ||
149 | ✗ | case GATE_BZIP2_COMPRESSION_MINIMUM: level = 1; break; | |
150 | ✗ | case GATE_BZIP2_COMPRESSION_LOW: level = 3; break; | |
151 | 1 | case GATE_BZIP2_COMPRESSION_MEDIUM: level = 5; break; | |
152 | ✗ | case GATE_BZIP2_COMPRESSION_HIGH: level = 7; break; | |
153 | ✗ | case GATE_BZIP2_COMPRESSION_MAXIMUM: level = 9; break; | |
154 | } | ||
155 | 1 | bzresult = BZ2_bzCompressInit(&impl->encoder, level, 0, 0); | |
156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (bzresult != BZ_OK) |
157 | { | ||
158 | ✗ | GATE_DEBUG_TRACE_MSG_VALUE("BZ2_bzCompressInit() failed", bzresult); | |
159 | ✗ | gate_mem_dealloc(impl); | |
160 | ✗ | ret = GATE_RESULT_FAILED; | |
161 | ✗ | break; | |
162 | } | ||
163 | |||
164 | 1 | impl->data_buffer_used = 0; | |
165 | |||
166 | 1 | gate_object_retain(impl->dest_stream); | |
167 | 1 | *ptr2streamptr = (gate_stream_t*)impl; | |
168 | |||
169 | 1 | ret = GATE_RESULT_OK; | |
170 | } while (0); | ||
171 | |||
172 | 1 | return ret; | |
173 | } | ||
174 | |||
175 | 1 | static void gate_bzip2encoder_release_impl(void* self) | |
176 | { | ||
177 | 1 | gate_bzip2encoder_impl_t* impl = (gate_bzip2encoder_impl_t*)self; | |
178 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (gate_atomic_int_dec(&impl->ref_counter) == 0) |
179 | { | ||
180 | 1 | gate_bzip2encoder_flush_impl(self); | |
181 | 1 | BZ2_bzCompressEnd(&impl->encoder); | |
182 | 1 | gate_object_release(impl->dest_stream); | |
183 | 1 | gate_mem_dealloc(impl); | |
184 | } | ||
185 | 1 | } | |
186 | ✗ | static int gate_bzip2encoder_retain_impl(void* self) | |
187 | { | ||
188 | ✗ | gate_bzip2encoder_impl_t* impl = (gate_bzip2encoder_impl_t*)self; | |
189 | ✗ | return gate_atomic_int_inc(&impl->ref_counter); | |
190 | } | ||
191 | ✗ | static gate_result_t gate_bzip2encoder_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned) | |
192 | { | ||
193 | (void)self; | ||
194 | (void)buffer; | ||
195 | (void)bufferlength; | ||
196 | (void)returned; | ||
197 | ✗ | return GATE_RESULT_NOTSUPPORTED; | |
198 | } | ||
199 | ✗ | static gate_result_t gate_bzip2encoder_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned) | |
200 | { | ||
201 | (void)self; | ||
202 | (void)buffer; | ||
203 | (void)bufferlength; | ||
204 | (void)returned; | ||
205 | ✗ | return GATE_RESULT_NOTSUPPORTED; | |
206 | } | ||
207 | 10 | static gate_result_t gate_bzip2encoder_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written) | |
208 | { | ||
209 | 10 | gate_result_t ret = GATE_RESULT_OK; | |
210 | 10 | gate_bzip2encoder_impl_t* impl = (gate_bzip2encoder_impl_t*)self; | |
211 | char compressed_buffer[GATE_MAX_STACK_COPYBUFFER_LENGTH]; | ||
212 | 10 | gate_size_t bytesavailable = sizeof(impl->data_buffer) - impl->data_buffer_used; | |
213 | 10 | gate_size_t bytesaccepted = bytesavailable < bufferlength ? bytesavailable : bufferlength; | |
214 | int bzresult; | ||
215 | gate_size_t bytescompressed; | ||
216 | gate_size_t byteswritten; | ||
217 | gate_size_t bytesremaining; | ||
218 | |||
219 | do | ||
220 | { | ||
221 | 10 | gate_mem_copy(&impl->data_buffer[impl->data_buffer_used], buffer, bytesaccepted); | |
222 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (written != NULL) |
223 | { | ||
224 | 10 | *written = bytesaccepted; | |
225 | } | ||
226 | 10 | impl->data_buffer_used += bytesaccepted; | |
227 | |||
228 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (impl->data_buffer_used == 0) |
229 | { | ||
230 | ✗ | ret = GATE_RESULT_INVALIDARG; | |
231 | ✗ | break; | |
232 | } | ||
233 | |||
234 | 10 | impl->encoder.avail_in = (unsigned int)impl->data_buffer_used; | |
235 | 10 | impl->encoder.next_in = &impl->data_buffer[0]; | |
236 | |||
237 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | while (impl->encoder.avail_in != 0) |
238 | { | ||
239 | 10 | impl->encoder.avail_out = sizeof(compressed_buffer); | |
240 | 10 | impl->encoder.next_out = &compressed_buffer[0]; | |
241 | |||
242 | 10 | bzresult = BZ2_bzCompress(&impl->encoder, BZ_RUN); | |
243 |
2/6✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
10 | if ((bzresult != BZ_OK) && (bzresult != BZ_RUN_OK) && (bzresult != BZ_STREAM_END)) |
244 | { | ||
245 | ✗ | ret = GATE_RESULT_FAILED; | |
246 | ✗ | break; | |
247 | } | ||
248 | 10 | bytescompressed = sizeof(compressed_buffer) - impl->encoder.avail_out; | |
249 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (bytescompressed > 0) |
250 | { | ||
251 | ✗ | ret = gate_stream_write_block(impl->dest_stream, &compressed_buffer[0], bytescompressed, &byteswritten); | |
252 | ✗ | if (GATE_FAILED(ret)) | |
253 | { | ||
254 | ✗ | break; | |
255 | } | ||
256 | } | ||
257 | else | ||
258 | { | ||
259 | 10 | break; | |
260 | } | ||
261 | } | ||
262 | |||
263 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (impl->encoder.avail_in != 0) |
264 | { | ||
265 | ✗ | bytesremaining = impl->encoder.avail_in; | |
266 | ✗ | gate_mem_move(&impl->data_buffer[0], &impl->data_buffer[impl->data_buffer_used - bytesremaining], bytesremaining); | |
267 | ✗ | impl->data_buffer_used = bytesremaining; | |
268 | } | ||
269 | else | ||
270 | { | ||
271 | 10 | impl->data_buffer_used = 0; | |
272 | } | ||
273 | } while (0); | ||
274 | 10 | return ret; | |
275 | } | ||
276 | 2 | static gate_result_t gate_bzip2encoder_flush_impl(void* self) | |
277 | { | ||
278 | 2 | gate_result_t ret = GATE_RESULT_OK; | |
279 | 2 | gate_bzip2encoder_impl_t* impl = (gate_bzip2encoder_impl_t*)self; | |
280 | char compressed_buffer[GATE_MAX_STACK_COPYBUFFER_LENGTH]; | ||
281 | int bzresult; | ||
282 | gate_size_t bytescompressed; | ||
283 | gate_size_t byteswritten; | ||
284 | gate_size_t bytesoffset; | ||
285 | |||
286 | do | ||
287 | { | ||
288 | 2 | impl->encoder.next_in = &impl->data_buffer[0]; | |
289 | 2 | impl->encoder.avail_in = (unsigned int)impl->data_buffer_used; | |
290 | |||
291 | 2 | impl->encoder.avail_out = sizeof(compressed_buffer); | |
292 | 2 | impl->encoder.next_out = &compressed_buffer[0]; | |
293 | |||
294 | 2 | bzresult = BZ2_bzCompress(&impl->encoder, BZ_FINISH); | |
295 |
4/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
|
2 | if ((bzresult != BZ_OK) && (bzresult != BZ_FINISH_OK) && (bzresult != BZ_STREAM_END)) |
296 | { | ||
297 | 1 | ret = GATE_RESULT_FAILED; | |
298 | 1 | break; | |
299 | } | ||
300 | |||
301 | 1 | bytescompressed = sizeof(compressed_buffer) - impl->encoder.avail_out; | |
302 | 1 | ret = gate_stream_write_block(impl->dest_stream, compressed_buffer, bytescompressed, &byteswritten); | |
303 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (GATE_FAILED(ret)) |
304 | { | ||
305 | ✗ | break; | |
306 | } | ||
307 | |||
308 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (impl->encoder.avail_in < impl->data_buffer_used) |
309 | { | ||
310 | ✗ | bytesoffset = impl->data_buffer_used - impl->encoder.avail_in; | |
311 | ✗ | gate_mem_move(&impl->data_buffer[0], &impl->data_buffer[bytesoffset], impl->encoder.avail_in); | |
312 | ✗ | impl->data_buffer_used = impl->encoder.avail_in; | |
313 | } | ||
314 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | } while (impl->encoder.avail_out == 0); |
315 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (GATE_SUCCEEDED(ret)) |
316 | { | ||
317 | 1 | ret = gate_stream_flush(impl->dest_stream); | |
318 | } | ||
319 | 2 | return ret; | |
320 | } | ||
321 | |||
322 | |||
323 | |||
324 | |||
325 | /* input stream decoder implementation */ | ||
326 | |||
327 | static void gate_bzip2decoder_release_impl(void* self); | ||
328 | static int gate_bzip2decoder_retain_impl(void* self); | ||
329 | ✗ | static char const* gate_bzip2decoder_interface_name_impl(void* self) | |
330 | { | ||
331 | GATE_UNUSED_ARG(self); | ||
332 | ✗ | return GATE_INTERFACE_NAME_STREAM; | |
333 | } | ||
334 | static gate_result_t gate_bzip2decoder_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned); | ||
335 | static gate_result_t gate_bzip2decoder_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned); | ||
336 | static gate_result_t gate_bzip2decoder_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written); | ||
337 | static gate_result_t gate_bzip2decoder_flush_impl(void* self); | ||
338 | |||
339 | static GATE_INTERFACE_VTBL(gate_stream) gate_bzip2decoder_vtbl; | ||
340 | 1 | static void gate_init_bzip2decoder_vtbl() | |
341 | { | ||
342 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!gate_bzip2decoder_vtbl.get_interface_name) |
343 | { | ||
344 | GATE_INTERFACE_VTBL(gate_stream) const local_vtbl = | ||
345 | { | ||
346 | &gate_bzip2decoder_interface_name_impl, | ||
347 | &gate_bzip2decoder_release_impl, | ||
348 | &gate_bzip2decoder_retain_impl, | ||
349 | &gate_bzip2decoder_read_impl, | ||
350 | &gate_bzip2decoder_peek_impl, | ||
351 | &gate_bzip2decoder_write_impl, | ||
352 | &gate_bzip2decoder_flush_impl | ||
353 | }; | ||
354 | 1 | gate_bzip2decoder_vtbl = local_vtbl; | |
355 | } | ||
356 | 1 | } | |
357 | |||
358 | typedef struct gate_bzip2decoder_impl | ||
359 | { | ||
360 | GATE_INTERFACE_VTBL(gate_stream)* vtbl; | ||
361 | |||
362 | gate_atomic_int_t ref_counter; | ||
363 | gate_stream_t* source_stream; | ||
364 | bz_stream decoder; | ||
365 | char source_buffer[GATE_MAX_BLOCK_COPYBUFFER_LENGTH]; | ||
366 | char decoded_buffer[GATE_MAX_BLOCK_COPYBUFFER_LENGTH]; | ||
367 | gate_size_t decoded_buffer_length; | ||
368 | gate_size_t decoded_buffer_offset; | ||
369 | |||
370 | } gate_bzip2decoder_impl_t; | ||
371 | |||
372 | 1 | gate_result_t gate_bzip2_decoder_create(gate_stream_t* input, gate_stream_t** ptr2streamptr) | |
373 | { | ||
374 | gate_result_t ret; | ||
375 | gate_bzip2decoder_impl_t* impl; | ||
376 | int bzresult; | ||
377 | |||
378 | do | ||
379 | { | ||
380 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if ((input == NULL) || (ptr2streamptr == NULL)) |
381 | { | ||
382 | ✗ | ret = GATE_RESULT_NULLPOINTER; | |
383 | ✗ | break; | |
384 | } | ||
385 | 1 | impl = (gate_bzip2decoder_impl_t*)gate_mem_alloc(sizeof(gate_bzip2decoder_impl_t)); | |
386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (impl == NULL) |
387 | { | ||
388 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
389 | ✗ | break; | |
390 | } | ||
391 | |||
392 | 1 | gate_mem_clear(impl, sizeof(gate_bzip2decoder_impl_t)); | |
393 | 1 | gate_init_bzip2decoder_vtbl(); | |
394 | 1 | impl->vtbl = &gate_bzip2decoder_vtbl; | |
395 | 1 | gate_atomic_int_init(&impl->ref_counter, 1); | |
396 | 1 | impl->source_stream = input; | |
397 | |||
398 | 1 | bzresult = BZ2_bzDecompressInit(&impl->decoder, 0, 0); | |
399 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (bzresult != BZ_OK) |
400 | { | ||
401 | ✗ | gate_mem_dealloc(impl); | |
402 | ✗ | ret = GATE_RESULT_FAILED; | |
403 | ✗ | break; | |
404 | } | ||
405 | |||
406 | 1 | gate_object_retain(impl->source_stream); | |
407 | 1 | *ptr2streamptr = (gate_stream_t*)impl; | |
408 | |||
409 | 1 | ret = GATE_RESULT_OK; | |
410 | } while (0); | ||
411 | |||
412 | 1 | return ret; | |
413 | } | ||
414 | |||
415 | |||
416 | 1 | void gate_bzip2decoder_release_impl(void* self) | |
417 | { | ||
418 | 1 | gate_bzip2decoder_impl_t* impl = (gate_bzip2decoder_impl_t*)self; | |
419 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (gate_atomic_int_dec(&impl->ref_counter) == 0) |
420 | { | ||
421 | 1 | BZ2_bzDecompressEnd(&impl->decoder); | |
422 | 1 | gate_object_release(impl->source_stream); | |
423 | 1 | gate_mem_dealloc(impl); | |
424 | } | ||
425 | 1 | } | |
426 | ✗ | int gate_bzip2decoder_retain_impl(void* self) | |
427 | { | ||
428 | ✗ | gate_bzip2decoder_impl_t* impl = (gate_bzip2decoder_impl_t*)self; | |
429 | ✗ | return gate_atomic_int_inc(&impl->ref_counter); | |
430 | } | ||
431 | |||
432 | 11 | gate_result_t gate_bzip2decoder_peek_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned) | |
433 | { | ||
434 | 11 | gate_bzip2decoder_impl_t* impl = (gate_bzip2decoder_impl_t*)self; | |
435 | |||
436 | 11 | gate_bool_t end_of_stream = false; | |
437 | gate_size_t bytecount; | ||
438 | gate_result_t result; | ||
439 | gate_size_t diff; | ||
440 | int bzresult; | ||
441 | |||
442 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
12 | while ((impl->decoded_buffer_length == 0) && (!end_of_stream)) |
443 | { | ||
444 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (impl->decoder.avail_in == 0) |
445 | { | ||
446 | 2 | result = gate_stream_read(impl->source_stream, impl->source_buffer, sizeof(impl->source_buffer), &bytecount); | |
447 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (GATE_FAILED(result)) |
448 | { | ||
449 | ✗ | return result; | |
450 | } | ||
451 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (bytecount == 0) |
452 | { | ||
453 | 1 | break; | |
454 | } | ||
455 | 1 | impl->decoder.next_in = &impl->source_buffer[0]; | |
456 | 1 | impl->decoder.avail_in = (unsigned int)bytecount; | |
457 | } | ||
458 | |||
459 | |||
460 | 1 | impl->decoder.avail_out = sizeof(impl->decoded_buffer); | |
461 | 1 | impl->decoder.next_out = &impl->decoded_buffer[0]; | |
462 | |||
463 | 1 | bzresult = BZ2_bzDecompress(&impl->decoder); | |
464 | |||
465 |
1/3✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
1 | switch (bzresult) |
466 | { | ||
467 | 1 | case BZ_STREAM_END: | |
468 | { | ||
469 | 1 | end_of_stream = true; | |
470 | 1 | break; | |
471 | } | ||
472 | ✗ | case BZ_OK: | |
473 | { | ||
474 | /* success -> continue */ | ||
475 | ✗ | break; | |
476 | } | ||
477 | ✗ | default: | |
478 | { | ||
479 | ✗ | return GATE_RESULT_FAILED; | |
480 | } | ||
481 | } | ||
482 | |||
483 | 1 | GATE_DEBUG_ASSERT(sizeof(impl->decoded_buffer) >= impl->decoder.avail_out); | |
484 | 1 | diff = sizeof(impl->decoded_buffer) - impl->decoder.avail_out; | |
485 | 1 | impl->decoded_buffer_length = diff; | |
486 | 1 | impl->decoded_buffer_offset = 0; | |
487 | } | ||
488 | |||
489 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
|
11 | if (bufferlength < impl->decoded_buffer_length) |
490 | { | ||
491 | 9 | gate_mem_copy(buffer, &impl->decoded_buffer[impl->decoded_buffer_offset], bufferlength); | |
492 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | if (returned != NULL) |
493 | { | ||
494 | 9 | *returned = bufferlength; | |
495 | } | ||
496 | } | ||
497 | else | ||
498 | { | ||
499 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (impl->decoded_buffer_length > 0) |
500 | { | ||
501 | 1 | gate_mem_copy(buffer, &impl->decoded_buffer[impl->decoded_buffer_offset], impl->decoded_buffer_length); | |
502 | } | ||
503 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (returned != NULL) |
504 | { | ||
505 | 2 | *returned = impl->decoded_buffer_length; | |
506 | } | ||
507 | } | ||
508 | 11 | return GATE_RESULT_OK; | |
509 | } | ||
510 | |||
511 | 11 | gate_result_t gate_bzip2decoder_read_impl(void* self, char* buffer, gate_size_t bufferlength, gate_size_t* returned) | |
512 | { | ||
513 | 11 | gate_bzip2decoder_impl_t* impl = (gate_bzip2decoder_impl_t*)self; | |
514 | 11 | gate_size_t tmp = 0; | |
515 | 11 | gate_result_t ret = gate_bzip2decoder_peek_impl(self, buffer, bufferlength, &tmp); | |
516 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (returned != NULL) |
517 | { | ||
518 | 11 | *returned = tmp; | |
519 | } | ||
520 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (GATE_SUCCEEDED(ret)) |
521 | { | ||
522 | 11 | impl->decoded_buffer_offset += tmp; | |
523 | 11 | impl->decoded_buffer_length -= tmp; | |
524 | } | ||
525 | 11 | return ret; | |
526 | } | ||
527 | ✗ | gate_result_t gate_bzip2decoder_write_impl(void* self, char const* buffer, gate_size_t bufferlength, gate_size_t* written) | |
528 | { | ||
529 | (void)self; | ||
530 | (void)buffer; | ||
531 | (void)bufferlength; | ||
532 | (void)written; | ||
533 | ✗ | return GATE_RESULT_NOTSUPPORTED; | |
534 | } | ||
535 | ✗ | gate_result_t gate_bzip2decoder_flush_impl(void* self) | |
536 | { | ||
537 | (void)self; | ||
538 | ✗ | return GATE_RESULT_NOTSUPPORTED; | |
539 | } | ||
540 | |||
541 | |||
542 | #endif /* GATE_ENCODE_BZIP2_USE_LIBBZ2 */ | ||
543 | |||
544 | #if defined(GATE_ENCODE_BZIP2_NO_IMPL) | ||
545 | |||
546 | gate_result_t gate_bzip2_encoder_create(gate_stream_t* output, gate_int16_t compression, gate_stream_t** ptr2streamptr) | ||
547 | { | ||
548 | (void)output; | ||
549 | (void)compression; | ||
550 | (void)ptr2streamptr; | ||
551 | return GATE_RESULT_NOTIMPLEMENTED; | ||
552 | } | ||
553 | |||
554 | gate_result_t gate_bzip2_decoder_create(gate_stream_t* input, gate_stream_t** ptr2streamptr) | ||
555 | { | ||
556 | (void)input; | ||
557 | (void)ptr2streamptr; | ||
558 | return GATE_RESULT_NOTIMPLEMENTED; | ||
559 | } | ||
560 | |||
561 | #endif /* GATE_ENCODE_BZIP2_NO_IMPL */ | ||
562 | |||
563 |