| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "mz.h" |
| 12 |
#include "mz_strm.h" |
| 13 |
#include "mz_strm_bzip.h" |
| 14 |
|
| 15 |
#include "bzlib.h" |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
static mz_stream_vtbl mz_stream_bzip_vtbl = { |
| 20 |
mz_stream_bzip_open, mz_stream_bzip_is_open, mz_stream_bzip_read, mz_stream_bzip_write, |
| 21 |
mz_stream_bzip_tell, mz_stream_bzip_seek, mz_stream_bzip_close, mz_stream_bzip_error, |
| 22 |
mz_stream_bzip_create, mz_stream_bzip_delete, mz_stream_bzip_get_prop_int64, mz_stream_bzip_set_prop_int64}; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
typedef struct mz_stream_bzip_s { |
| 27 |
mz_stream stream; |
| 28 |
bz_stream bzstream; |
| 29 |
int32_t mode; |
| 30 |
int32_t error; |
| 31 |
uint8_t buffer[INT16_MAX]; |
| 32 |
int32_t buffer_len; |
| 33 |
int16_t stream_end; |
| 34 |
int64_t total_in; |
| 35 |
int64_t total_out; |
| 36 |
int64_t max_total_in; |
| 37 |
int8_t initialized; |
| 38 |
int16_t level; |
| 39 |
} mz_stream_bzip; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
int32_t mz_stream_bzip_open(void *stream, const char *path, int32_t mode) { |
| 44 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 45 |
|
| 46 |
MZ_UNUSED(path); |
| 47 |
|
| 48 |
bzip->bzstream.bzalloc = 0; |
| 49 |
bzip->bzstream.bzfree = 0; |
| 50 |
bzip->bzstream.opaque = 0; |
| 51 |
bzip->bzstream.total_in_lo32 = 0; |
| 52 |
bzip->bzstream.total_in_hi32 = 0; |
| 53 |
bzip->bzstream.total_out_lo32 = 0; |
| 54 |
bzip->bzstream.total_out_hi32 = 0; |
| 55 |
|
| 56 |
bzip->total_in = 0; |
| 57 |
bzip->total_out = 0; |
| 58 |
|
| 59 |
if (mode & MZ_OPEN_MODE_WRITE) { |
| 60 |
#ifdef MZ_ZIP_NO_COMPRESSION |
| 61 |
return MZ_SUPPORT_ERROR; |
| 62 |
#else |
| 63 |
bzip->bzstream.next_out = (char *)bzip->buffer; |
| 64 |
bzip->bzstream.avail_out = sizeof(bzip->buffer); |
| 65 |
|
| 66 |
bzip->error = BZ2_bzCompressInit(&bzip->bzstream, bzip->level, 0, 0); |
| 67 |
#endif |
| 68 |
} else if (mode & MZ_OPEN_MODE_READ) { |
| 69 |
#ifdef MZ_ZIP_NO_DECOMPRESSION |
| 70 |
return MZ_SUPPORT_ERROR; |
| 71 |
#else |
| 72 |
bzip->bzstream.next_in = (char *)bzip->buffer; |
| 73 |
bzip->bzstream.avail_in = 0; |
| 74 |
|
| 75 |
bzip->error = BZ2_bzDecompressInit(&bzip->bzstream, 0, 0); |
| 76 |
#endif |
| 77 |
} |
| 78 |
|
| 79 |
if (bzip->error != BZ_OK) |
| 80 |
return MZ_OPEN_ERROR; |
| 81 |
|
| 82 |
bzip->initialized = 1; |
| 83 |
bzip->stream_end = 0; |
| 84 |
bzip->mode = mode; |
| 85 |
return MZ_OK; |
| 86 |
} |
| 87 |
|
| 88 |
int32_t mz_stream_bzip_is_open(void *stream) { |
| 89 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 90 |
if (bzip->initialized != 1) |
| 91 |
return MZ_OPEN_ERROR; |
| 92 |
return MZ_OK; |
| 93 |
} |
| 94 |
|
| 95 |
int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size) { |
| 96 |
#ifdef MZ_ZIP_NO_DECOMPRESSION |
| 97 |
MZ_UNUSED(stream); |
| 98 |
MZ_UNUSED(buf); |
| 99 |
MZ_UNUSED(size); |
| 100 |
return MZ_SUPPORT_ERROR; |
| 101 |
#else |
| 102 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 103 |
uint64_t total_in_before = 0; |
| 104 |
uint64_t total_out_before = 0; |
| 105 |
uint64_t total_in_after = 0; |
| 106 |
uint64_t total_out_after = 0; |
| 107 |
int32_t total_out = 0; |
| 108 |
int32_t in_bytes = 0; |
| 109 |
int32_t out_bytes = 0; |
| 110 |
int32_t bytes_to_read = sizeof(bzip->buffer); |
| 111 |
int32_t read = 0; |
| 112 |
int32_t err = BZ_OK; |
| 113 |
|
| 114 |
if (bzip->stream_end) |
| 115 |
return 0; |
| 116 |
|
| 117 |
bzip->bzstream.next_out = (char *)buf; |
| 118 |
bzip->bzstream.avail_out = (unsigned int)size; |
| 119 |
|
| 120 |
do { |
| 121 |
if (bzip->bzstream.avail_in == 0) { |
| 122 |
if (bzip->max_total_in > 0) { |
| 123 |
if ((int64_t)bytes_to_read > (bzip->max_total_in - bzip->total_in)) |
| 124 |
bytes_to_read = (int32_t)(bzip->max_total_in - bzip->total_in); |
| 125 |
} |
| 126 |
|
| 127 |
read = mz_stream_read(bzip->stream.base, bzip->buffer, bytes_to_read); |
| 128 |
|
| 129 |
if (read < 0) |
| 130 |
return read; |
| 131 |
|
| 132 |
bzip->bzstream.next_in = (char *)bzip->buffer; |
| 133 |
bzip->bzstream.avail_in = (uint32_t)read; |
| 134 |
} |
| 135 |
|
| 136 |
total_in_before = bzip->bzstream.avail_in; |
| 137 |
total_out_before = bzip->bzstream.total_out_lo32 + (((uint64_t)bzip->bzstream.total_out_hi32) << 32); |
| 138 |
|
| 139 |
err = BZ2_bzDecompress(&bzip->bzstream); |
| 140 |
|
| 141 |
total_in_after = bzip->bzstream.avail_in; |
| 142 |
total_out_after = bzip->bzstream.total_out_lo32 + (((uint64_t)bzip->bzstream.total_out_hi32) << 32); |
| 143 |
|
| 144 |
in_bytes = (int32_t)(total_in_before - total_in_after); |
| 145 |
out_bytes = (int32_t)(total_out_after - total_out_before); |
| 146 |
|
| 147 |
total_out += out_bytes; |
| 148 |
|
| 149 |
bzip->total_in += in_bytes; |
| 150 |
bzip->total_out += out_bytes; |
| 151 |
|
| 152 |
if (err == BZ_STREAM_END) { |
| 153 |
bzip->stream_end = 1; |
| 154 |
break; |
| 155 |
} |
| 156 |
if (err != BZ_OK && err != BZ_RUN_OK) { |
| 157 |
bzip->error = err; |
| 158 |
break; |
| 159 |
} |
| 160 |
} while (bzip->bzstream.avail_out > 0); |
| 161 |
|
| 162 |
if (bzip->error != 0) |
| 163 |
return MZ_DATA_ERROR; |
| 164 |
|
| 165 |
return total_out; |
| 166 |
#endif |
| 167 |
} |
| 168 |
|
| 169 |
#ifndef MZ_ZIP_NO_COMPRESSION |
| 170 |
static int32_t mz_stream_bzip_flush(void *stream) { |
| 171 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 172 |
if (mz_stream_write(bzip->stream.base, bzip->buffer, bzip->buffer_len) != bzip->buffer_len) |
| 173 |
return MZ_WRITE_ERROR; |
| 174 |
return MZ_OK; |
| 175 |
} |
| 176 |
|
| 177 |
static int32_t mz_stream_bzip_compress(void *stream, int flush) { |
| 178 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 179 |
uint64_t total_out_before = 0; |
| 180 |
uint64_t total_out_after = 0; |
| 181 |
uint32_t out_bytes = 0; |
| 182 |
int32_t err = BZ_OK; |
| 183 |
|
| 184 |
do { |
| 185 |
if (bzip->bzstream.avail_out == 0) { |
| 186 |
err = mz_stream_bzip_flush(bzip); |
| 187 |
if (err != MZ_OK) |
| 188 |
return err; |
| 189 |
|
| 190 |
bzip->bzstream.avail_out = sizeof(bzip->buffer); |
| 191 |
bzip->bzstream.next_out = (char *)bzip->buffer; |
| 192 |
|
| 193 |
bzip->buffer_len = 0; |
| 194 |
} |
| 195 |
|
| 196 |
total_out_before = bzip->bzstream.total_out_lo32 + (((uint64_t)bzip->bzstream.total_out_hi32) << 32); |
| 197 |
|
| 198 |
err = BZ2_bzCompress(&bzip->bzstream, flush); |
| 199 |
|
| 200 |
total_out_after = bzip->bzstream.total_out_lo32 + (((uint64_t)bzip->bzstream.total_out_hi32) << 32); |
| 201 |
|
| 202 |
out_bytes = (uint32_t)(total_out_after - total_out_before); |
| 203 |
|
| 204 |
bzip->buffer_len += out_bytes; |
| 205 |
bzip->total_out += out_bytes; |
| 206 |
|
| 207 |
if (err == BZ_STREAM_END) |
| 208 |
break; |
| 209 |
if (err < 0) { |
| 210 |
bzip->error = err; |
| 211 |
return MZ_DATA_ERROR; |
| 212 |
} |
| 213 |
} while ((bzip->bzstream.avail_in > 0) || (flush == BZ_FINISH && err == BZ_FINISH_OK)); |
| 214 |
|
| 215 |
return MZ_OK; |
| 216 |
} |
| 217 |
#endif |
| 218 |
|
| 219 |
int32_t mz_stream_bzip_write(void *stream, const void *buf, int32_t size) { |
| 220 |
#ifdef MZ_ZIP_NO_COMPRESSION |
| 221 |
MZ_UNUSED(stream); |
| 222 |
MZ_UNUSED(buf); |
| 223 |
MZ_UNUSED(size); |
| 224 |
return MZ_SUPPORT_ERROR; |
| 225 |
#else |
| 226 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 227 |
int32_t err = MZ_OK; |
| 228 |
|
| 229 |
bzip->bzstream.next_in = (char *)(intptr_t)buf; |
| 230 |
bzip->bzstream.avail_in = (unsigned int)size; |
| 231 |
|
| 232 |
err = mz_stream_bzip_compress(stream, BZ_RUN); |
| 233 |
if (err != MZ_OK) { |
| 234 |
return err; |
| 235 |
} |
| 236 |
|
| 237 |
bzip->total_in += size; |
| 238 |
return size; |
| 239 |
#endif |
| 240 |
} |
| 241 |
|
| 242 |
int64_t mz_stream_bzip_tell(void *stream) { |
| 243 |
MZ_UNUSED(stream); |
| 244 |
|
| 245 |
return MZ_TELL_ERROR; |
| 246 |
} |
| 247 |
|
| 248 |
int32_t mz_stream_bzip_seek(void *stream, int64_t offset, int32_t origin) { |
| 249 |
MZ_UNUSED(stream); |
| 250 |
MZ_UNUSED(offset); |
| 251 |
MZ_UNUSED(origin); |
| 252 |
|
| 253 |
return MZ_SEEK_ERROR; |
| 254 |
} |
| 255 |
|
| 256 |
int32_t mz_stream_bzip_close(void *stream) { |
| 257 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 258 |
|
| 259 |
if (bzip->mode & MZ_OPEN_MODE_WRITE) { |
| 260 |
#ifdef MZ_ZIP_NO_COMPRESSION |
| 261 |
return MZ_SUPPORT_ERROR; |
| 262 |
#else |
| 263 |
mz_stream_bzip_compress(stream, BZ_FINISH); |
| 264 |
mz_stream_bzip_flush(stream); |
| 265 |
|
| 266 |
BZ2_bzCompressEnd(&bzip->bzstream); |
| 267 |
#endif |
| 268 |
} else if (bzip->mode & MZ_OPEN_MODE_READ) { |
| 269 |
#ifdef MZ_ZIP_NO_DECOMPRESSION |
| 270 |
return MZ_SUPPORT_ERROR; |
| 271 |
#else |
| 272 |
BZ2_bzDecompressEnd(&bzip->bzstream); |
| 273 |
#endif |
| 274 |
} |
| 275 |
|
| 276 |
bzip->initialized = 0; |
| 277 |
|
| 278 |
if (bzip->error != BZ_OK) |
| 279 |
return MZ_CLOSE_ERROR; |
| 280 |
return MZ_OK; |
| 281 |
} |
| 282 |
|
| 283 |
int32_t mz_stream_bzip_error(void *stream) { |
| 284 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 285 |
return bzip->error; |
| 286 |
} |
| 287 |
|
| 288 |
int32_t mz_stream_bzip_get_prop_int64(void *stream, int32_t prop, int64_t *value) { |
| 289 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 290 |
switch (prop) { |
| 291 |
case MZ_STREAM_PROP_TOTAL_IN: |
| 292 |
*value = bzip->total_in; |
| 293 |
break; |
| 294 |
case MZ_STREAM_PROP_TOTAL_IN_MAX: |
| 295 |
*value = bzip->max_total_in; |
| 296 |
break; |
| 297 |
case MZ_STREAM_PROP_TOTAL_OUT: |
| 298 |
*value = bzip->total_out; |
| 299 |
break; |
| 300 |
case MZ_STREAM_PROP_HEADER_SIZE: |
| 301 |
*value = 0; |
| 302 |
break; |
| 303 |
default: |
| 304 |
return MZ_EXIST_ERROR; |
| 305 |
} |
| 306 |
return MZ_OK; |
| 307 |
} |
| 308 |
|
| 309 |
int32_t mz_stream_bzip_set_prop_int64(void *stream, int32_t prop, int64_t value) { |
| 310 |
mz_stream_bzip *bzip = (mz_stream_bzip *)stream; |
| 311 |
switch (prop) { |
| 312 |
case MZ_STREAM_PROP_COMPRESS_LEVEL: |
| 313 |
if (value == MZ_COMPRESS_LEVEL_DEFAULT) |
| 314 |
bzip->level = 6; |
| 315 |
else |
| 316 |
bzip->level = (int16_t)value; |
| 317 |
return MZ_OK; |
| 318 |
case MZ_STREAM_PROP_TOTAL_IN_MAX: |
| 319 |
bzip->max_total_in = value; |
| 320 |
return MZ_OK; |
| 321 |
} |
| 322 |
return MZ_EXIST_ERROR; |
| 323 |
} |
| 324 |
|
| 325 |
void *mz_stream_bzip_create(void) { |
| 326 |
mz_stream_bzip *bzip = (mz_stream_bzip *)calloc(1, sizeof(mz_stream_bzip)); |
| 327 |
if (bzip) { |
| 328 |
bzip->stream.vtbl = &mz_stream_bzip_vtbl; |
| 329 |
bzip->level = 6; |
| 330 |
} |
| 331 |
return bzip; |
| 332 |
} |
| 333 |
|
| 334 |
void mz_stream_bzip_delete(void **stream) { |
| 335 |
mz_stream_bzip *bzip = NULL; |
| 336 |
if (!stream) |
| 337 |
return; |
| 338 |
bzip = (mz_stream_bzip *)*stream; |
| 339 |
free(bzip); |
| 340 |
*stream = NULL; |
| 341 |
} |
| 342 |
|
| 343 |
void *mz_stream_bzip_get_interface(void) { |
| 344 |
return (void *)&mz_stream_bzip_vtbl; |
| 345 |
} |
| 346 |
|
| 347 |
extern void bz_internal_error(int errcode) { |
| 348 |
MZ_UNUSED(errcode); |
| 349 |
} |