out = smalloc(sizeof(struct Outbuf));
out->outbits = out->noutbits = 0;
out->firstblock = 1;
out->comp_disabled = FALSE;
out = smalloc(sizeof(struct Outbuf));
out->outbits = out->noutbits = 0;
out->firstblock = 1;
out->comp_disabled = FALSE;
* length adjustment (which is only valid for packets < 65536
* bytes, but that seems reasonable enough).
*/
* length adjustment (which is only valid for packets < 65536
* bytes, but that seems reasonable enough).
*/
-int zlib_compress_block(unsigned char *block, int len,
+int zlib_compress_block(void *handle, unsigned char *block, int len,
- lz77_compress(&ectx, block, blen, FALSE);
+ lz77_compress(ectx, block, blen, FALSE);
- lz77_compress(&ectx, block, len, TRUE);
+ lz77_compress(ectx, block, len, TRUE);
struct zlib_table *staticlentable, *staticdisttable;
struct zlib_table *currlentable, *currdisttable, *lenlentable;
enum {
struct zlib_table *staticlentable, *staticdisttable;
struct zlib_table *currlentable, *currdisttable, *lenlentable;
enum {
memset(lengths, 8, 144);
memset(lengths + 144, 9, 256 - 144);
memset(lengths + 256, 7, 280 - 256);
memset(lengths + 280, 8, 288 - 280);
memset(lengths, 8, 144);
memset(lengths + 144, 9, 256 - 144);
memset(lengths + 256, 7, 280 - 256);
memset(lengths + 280, 8, 288 - 280);
- dctx.staticdisttable = zlib_mktable(lengths, 32);
- dctx.state = START; /* even before header */
- dctx.currlentable = dctx.currdisttable = dctx.lenlentable = NULL;
- dctx.bits = 0;
- dctx.nbits = 0;
- logevent("Initialised zlib (RFC1950) decompression");
+ dctx->staticdisttable = zlib_mktable(lengths, 32);
+ dctx->state = START; /* even before header */
+ dctx->currlentable = dctx->currdisttable = dctx->lenlentable = NULL;
+ dctx->bits = 0;
+ dctx->nbits = 0;
+ dctx->winpos = 0;
+
+ return dctx;
+}
+
+void zlib_decompress_cleanup(void *handle)
+{
+ struct zlib_decompress_ctx *dctx = (struct zlib_decompress_ctx *)handle;
+
+ if (dctx->currlentable && dctx->currlentable != dctx->staticlentable)
+ zlib_freetable(&dctx->currlentable);
+ if (dctx->currdisttable && dctx->currdisttable != dctx->staticdisttable)
+ zlib_freetable(&dctx->currdisttable);
+ if (dctx->lenlentable)
+ zlib_freetable(&dctx->lenlentable);
+ sfree(dctx);
- dctx.window[dctx.winpos] = c;
- dctx.winpos = (dctx.winpos + 1) & (WINSIZE - 1);
- if (dctx.outlen >= dctx.outsize) {
- dctx.outsize = dctx.outlen + 512;
- dctx.outblk = srealloc(dctx.outblk, dctx.outsize);
+ dctx->window[dctx->winpos] = c;
+ dctx->winpos = (dctx->winpos + 1) & (WINSIZE - 1);
+ if (dctx->outlen >= dctx->outsize) {
+ dctx->outsize = dctx->outlen + 512;
+ dctx->outblk = srealloc(dctx->outblk, dctx->outsize);
-int zlib_decompress_block(unsigned char *block, int len,
+int zlib_decompress_block(void *handle, unsigned char *block, int len,
const coderecord *rec;
int code, blktype, rep, dist, nlen;
static const unsigned char lenlenmap[] = {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
};
const coderecord *rec;
int code, blktype, rep, dist, nlen;
static const unsigned char lenlenmap[] = {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
};
- while (len > 0 || dctx.nbits > 0) {
- while (dctx.nbits < 24 && len > 0) {
- dctx.bits |= (*block++) << dctx.nbits;
- dctx.nbits += 8;
+ while (len > 0 || dctx->nbits > 0) {
+ while (dctx->nbits < 24 && len > 0) {
+ dctx->bits |= (*block++) << dctx->nbits;
+ dctx->nbits += 8;
- dctx.lenptr = 0;
- dctx.state = TREES_LENLEN;
- memset(dctx.lenlen, 0, sizeof(dctx.lenlen));
+ dctx->lenptr = 0;
+ dctx->state = TREES_LENLEN;
+ memset(dctx->lenlen, 0, sizeof(dctx->lenlen));
- while (dctx.lenptr < dctx.hclen && dctx.nbits >= 3) {
- dctx.lenlen[lenlenmap[dctx.lenptr++]] =
- (unsigned char) (dctx.bits & 7);
+ while (dctx->lenptr < dctx->hclen && dctx->nbits >= 3) {
+ dctx->lenlen[lenlenmap[dctx->lenptr++]] =
+ (unsigned char) (dctx->bits & 7);
- if (dctx.lenptr == dctx.hclen) {
- dctx.lenlentable = zlib_mktable(dctx.lenlen, 19);
- dctx.state = TREES_LEN;
- dctx.lenptr = 0;
+ if (dctx->lenptr == dctx->hclen) {
+ dctx->lenlentable = zlib_mktable(dctx->lenlen, 19);
+ dctx->state = TREES_LEN;
+ dctx->lenptr = 0;
- if (dctx.lenptr >= dctx.hlit + dctx.hdist) {
- dctx.currlentable = zlib_mktable(dctx.lengths, dctx.hlit);
- dctx.currdisttable = zlib_mktable(dctx.lengths + dctx.hlit,
- dctx.hdist);
- zlib_freetable(&dctx.lenlentable);
- dctx.state = INBLK;
+ if (dctx->lenptr >= dctx->hlit + dctx->hdist) {
+ dctx->currlentable = zlib_mktable(dctx->lengths, dctx->hlit);
+ dctx->currdisttable = zlib_mktable(dctx->lengths + dctx->hlit,
+ dctx->hdist);
+ zlib_freetable(&dctx->lenlentable);
+ dctx->lenlentable = NULL;
+ dctx->state = INBLK;
- dctx.lenextrabits = (code == 16 ? 2 : code == 17 ? 3 : 7);
- dctx.lenaddon = (code == 18 ? 11 : 3);
- dctx.lenrep = (code == 16 && dctx.lenptr > 0 ?
- dctx.lengths[dctx.lenptr - 1] : 0);
- dctx.state = TREES_LENREP;
+ dctx->lenextrabits = (code == 16 ? 2 : code == 17 ? 3 : 7);
+ dctx->lenaddon = (code == 18 ? 11 : 3);
+ dctx->lenrep = (code == 16 && dctx->lenptr > 0 ?
+ dctx->lengths[dctx->lenptr - 1] : 0);
+ dctx->state = TREES_LENREP;
- dctx.lenaddon +
- (dctx.bits & ((1 << dctx.lenextrabits) - 1));
- EATBITS(dctx.lenextrabits);
- while (rep > 0 && dctx.lenptr < dctx.hlit + dctx.hdist) {
- dctx.lengths[dctx.lenptr] = dctx.lenrep;
- dctx.lenptr++;
+ dctx->lenaddon +
+ (dctx->bits & ((1 << dctx->lenextrabits) - 1));
+ EATBITS(dctx->lenextrabits);
+ while (rep > 0 && dctx->lenptr < dctx->hlit + dctx->hdist) {
+ dctx->lengths[dctx->lenptr] = dctx->lenrep;
+ dctx->lenptr++;
- dctx.state = OUTSIDEBLK;
- if (dctx.currlentable != dctx.staticlentable)
- zlib_freetable(&dctx.currlentable);
- if (dctx.currdisttable != dctx.staticdisttable)
- zlib_freetable(&dctx.currdisttable);
+ dctx->state = OUTSIDEBLK;
+ if (dctx->currlentable != dctx->staticlentable) {
+ zlib_freetable(&dctx->currlentable);
+ dctx->currlentable = NULL;
+ }
+ if (dctx->currdisttable != dctx->staticdisttable) {
+ zlib_freetable(&dctx->currdisttable);
+ dctx->currdisttable = NULL;
+ }