struct LZ77InternalContext *st;
int i;
- st = (struct LZ77InternalContext *) smalloc(sizeof(*st));
+ st = snew(struct LZ77InternalContext);
if (!st)
return 0;
while (out->noutbits >= 8) {
if (out->outlen >= out->outsize) {
out->outsize = out->outlen + 64;
- out->outbuf = srealloc(out->outbuf, out->outsize);
+ out->outbuf = sresize(out->outbuf, out->outsize, unsigned char);
}
out->outbuf[out->outlen++] = (unsigned char) (out->outbits & 0xFF);
out->outbits >>= 8;
void *zlib_compress_init(void)
{
struct Outbuf *out;
- struct LZ77Context *ectx = smalloc(sizeof(struct LZ77Context));
+ struct LZ77Context *ectx = snew(struct LZ77Context);
lz77_init(ectx);
ectx->literal = zlib_literal;
ectx->match = zlib_match;
- out = smalloc(sizeof(struct Outbuf));
+ out = snew(struct Outbuf);
out->outbits = out->noutbits = 0;
out->firstblock = 1;
out->comp_disabled = FALSE;
{
struct LZ77Context *ectx = (struct LZ77Context *)handle;
sfree(ectx->userdata);
+ sfree(ectx->ictx);
+ sfree(ectx);
}
/*
* length adjustment (which is only valid for packets < 65536
* bytes, but that seems reasonable enough).
*/
-int zlib_disable_compression(void *handle)
+static int zlib_disable_compression(void *handle)
{
struct LZ77Context *ectx = (struct LZ77Context *)handle;
struct Outbuf *out = (struct Outbuf *) ectx->userdata;
int nsyms,
int pfx, int pfxbits, int bits)
{
- struct zlib_table *tab = smalloc(sizeof(struct zlib_table));
+ struct zlib_table *tab = snew(struct zlib_table);
int pfxmask = (1 << pfxbits) - 1;
int nbits, i, j, code;
- tab->table = smalloc((1 << bits) * sizeof(struct zlib_tableentry));
+ tab->table = snewn(1 << bits, struct zlib_tableentry);
tab->mask = (1 << bits) - 1;
for (code = 0; code <= tab->mask; code++) {
void *zlib_decompress_init(void)
{
- struct zlib_decompress_ctx *dctx =
- smalloc(sizeof(struct zlib_decompress_ctx));
+ struct zlib_decompress_ctx *dctx = snew(struct zlib_decompress_ctx);
unsigned char lengths[288];
memset(lengths, 8, 144);
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);
+ zlib_freetable(&dctx->staticlentable);
+ zlib_freetable(&dctx->staticdisttable);
sfree(dctx);
}
-int zlib_huflookup(unsigned long *bitsp, int *nbitsp,
+static int zlib_huflookup(unsigned long *bitsp, int *nbitsp,
struct zlib_table *tab)
{
unsigned long bits = *bitsp;
*nbitsp = nbits;
return ent->code;
}
+
+ if (!tab) {
+ /*
+ * There was a missing entry in the table, presumably
+ * due to an invalid Huffman table description, and the
+ * subsequent data has attempted to use the missing
+ * entry. Return a decoding failure.
+ */
+ return -2;
+ }
}
}
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->outblk = sresize(dctx->outblk, dctx->outsize, unsigned char);
}
dctx->outblk[dctx->outlen++] = c;
}
zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->lenlentable);
if (code == -1)
goto finished;
+ if (code == -2)
+ goto decode_error;
if (code < 16)
dctx->lengths[dctx->lenptr++] = code;
else {
zlib_huflookup(&dctx->bits, &dctx->nbits, dctx->currlentable);
if (code == -1)
goto finished;
+ if (code == -2)
+ goto decode_error;
if (code < 256)
zlib_emit_char(dctx, code);
else if (code == 256) {
dctx->currdisttable);
if (code == -1)
goto finished;
+ if (code == -2)
+ goto decode_error;
dctx->state = GOTDISTSYM;
dctx->sym = code;
break;
finished:
*outblock = dctx->outblk;
*outlen = dctx->outlen;
-
return 1;
+
+ decode_error:
+ sfree(dctx->outblk);
+ *outblock = dctx->outblk = NULL;
+ *outlen = 0;
+ return 0;
}
const struct ssh_compress ssh_zlib = {