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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/*
* zlibdl.c -- zlib dynamic load interface
*
* zlibdl.c is a part of binkd project
*
* Copyright (C) 2003 val khokhlov, FIDONet 2:550/180
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. See COPYING.
*/
#if defined(ZLIBDL) && defined(_MSC_VER)
#pragma optimize("", off)
#endif
#include <stdlib.h>
#include "sys.h"
#include "zlibdl.h"
#include "compress.h"
#include "tools.h"
int compress_init(int type, int lvl, void **data)
{
switch (type) {
#ifdef WITH_BZLIB2
case 2: {
*data = calloc(1, sizeof(bz_stream));
if (*data == NULL) {
Log (1, "compress_init: not enough memory (%lu needed)", sizeof(bz_stream));
return BZ_MEM_ERROR;
}
/* if (lvl <= 0) */ lvl = 1; /* default is small (100K) buffer */
return BZ2_bzCompressInit((bz_stream *)*data, lvl, 0, 0);
}
#endif
#ifdef WITH_ZLIB
case 1: {
*data = calloc(1, sizeof(z_stream));
if (*data == NULL) {
Log (1, "compress_init: not enough memory (%lu needed)", sizeof(z_stream));
return Z_MEM_ERROR;
}
/* 0 is default compression level */
/* no compression means send file without GZ flag, not 0 compress level */
if (lvl <= 0) lvl = Z_DEFAULT_COMPRESSION;
return deflateInit((z_stream *)*data, lvl);
}
#endif
default:
Log (1, "Unknown compression method: %d; data lost", type);
}
return -1;
}
int do_compress(int type, char *dst, int *dst_len, char *src, int *src_len,
int finish, void *data) {
int rc;
switch (type) {
#ifdef WITH_BZLIB2
case 2: {
bz_stream *zstrm = (bz_stream *)data;
zstrm->next_in = (char *)src;
zstrm->avail_in = (unsigned int)*src_len;
zstrm->next_out = (char *)dst;
zstrm->avail_out = (unsigned int)*dst_len;
rc = BZ2_bzCompress(zstrm, finish ? BZ_FINISH : 0);
*src_len -= (int)zstrm->avail_in;
*dst_len -= (int)zstrm->avail_out;
if (rc == BZ_RUN_OK || rc == BZ_FLUSH_OK || rc == BZ_FINISH_OK) rc = 0;
if (rc == BZ_STREAM_END) rc = 1;
return rc;
}
#endif
#ifdef WITH_ZLIB
case 1: {
z_stream *zstrm = (z_stream *)data;
zstrm->next_in = (Bytef *)src;
zstrm->avail_in = (uLong)*src_len;
zstrm->next_out = (Bytef *)dst;
zstrm->avail_out = (uLong)*dst_len;
rc = deflate(zstrm, finish ? Z_FINISH : 0);
*src_len -= (int)zstrm->avail_in;
*dst_len -= (int)zstrm->avail_out;
if (rc == Z_STREAM_END) rc = 1;
return rc;
}
#endif
default:
Log (1, "Unknown compression method: %d; data lost", type);
}
return -1;
}
void compress_deinit(int type, void *data)
{
switch (type) {
#ifdef WITH_BZLIB2
case 2: {
int rc = BZ2_bzCompressEnd((bz_stream *)data);
if (rc < 0) Log (1, "BZ2_bzCompressEnd error: %d", rc);
break;
}
#endif
#ifdef WITH_ZLIB
case 1: {
int rc = deflateEnd((z_stream *)data);
if (rc < 0) Log (1, "deflateEnd error: %d", rc);
break;
}
#endif
default:
Log (1, "Unknown compression method: %d", type);
}
free(data);
return;
}
void compress_abort(int type, void *data)
{
char buf[1024];
int i, j;
if (data) {
Log (4, "Purge compress buffers");
do {
i=sizeof(buf);
j=0;
} while (do_compress(type, buf, &i, NULL, &j, 1, data) == 0 && i > 0);
compress_deinit(type, data);
}
}
int decompress_init(int type, void **data)
{
switch (type) {
#ifdef WITH_BZLIB2
case 2: {
*data = calloc(1, sizeof(bz_stream));
if (*data == NULL) {
Log (1, "decompress_init: not enough memory (%lu needed)", sizeof(bz_stream));
return BZ_MEM_ERROR;
}
return BZ2_bzDecompressInit((bz_stream *)*data, 0, 0);
}
#endif
#ifdef WITH_ZLIB
case 1: {
*data = calloc(1, sizeof(z_stream));
if (*data == NULL) {
Log (1, "decompress_init: not enough memory (%lu needed)", sizeof(z_stream));
return Z_MEM_ERROR;
}
return inflateInit((z_stream *)*data);
}
#endif
default:
Log (1, "Unknown compression method: %d; data lost", type);
}
return -1;
}
int do_decompress(int type, char *dst, int *dst_len, char *src, int *src_len, void *data) {
int rc;
switch (type) {
#ifdef WITH_BZLIB2
case 2: {
bz_stream *zstrm = (bz_stream *)data;
zstrm->next_in = (char *)src;
zstrm->avail_in = (unsigned int)*src_len;
zstrm->next_out = (char *)dst;
zstrm->avail_out = (unsigned int)*dst_len;
rc = BZ2_bzDecompress(zstrm);
*src_len -= (int)zstrm->avail_in;
*dst_len -= (int)zstrm->avail_out;
if (rc == BZ_RUN_OK || rc == BZ_FLUSH_OK) rc = 0;
if (rc == BZ_STREAM_END) rc = 1;
return rc;
}
#endif
#ifdef WITH_ZLIB
case 1: {
z_stream *zstrm = (z_stream *)data;
zstrm->next_in = (Bytef *)src;
zstrm->avail_in = (uLong)*src_len;
zstrm->next_out = (Bytef *)dst;
zstrm->avail_out = (uLong)*dst_len;
rc = inflate(zstrm, 0);
*src_len -= (int)zstrm->avail_in;
*dst_len -= (int)zstrm->avail_out;
if (rc == Z_STREAM_END) rc = 1;
return rc;
}
#endif
default:
Log (1, "Unknown compression method: %d; data lost", type);
}
return 0;
}
int decompress_deinit(int type, void *data)
{
int rc = -1;
switch (type) {
#ifdef WITH_BZLIB2
case 2: {
rc = BZ2_bzDecompressEnd((bz_stream *)data);
break;
}
#endif
#ifdef WITH_ZLIB
case 1: {
rc = inflateEnd((z_stream *)data);
break;
}
#endif
default:
Log (1, "Unknown compression method: %d", type);
break;
}
free(data);
return rc;
}
int decompress_abort(int type, void *data) {
char buf[1024];
int i, j;
if (data) {
Log (4, "Purge decompress buffers");
do {
i=sizeof(buf);
j=0;
} while (do_decompress(type, buf, &i, NULL, &j, data) == 0 && i > 0);
return decompress_deinit(type, data);
}
return 0;
}
|