1
1

decomp: cleaned off old compression stuff

I cleared off legacy code from when the compression and decompression
functions were a single unified function. Makes the code easier to read
too.
Этот коммит содержится в:
Daniel Stenberg 2010-11-03 14:52:42 +01:00
родитель 7603c0f854
Коммит a6fc9aeec9
3 изменённых файлов: 23 добавлений и 37 удалений

Просмотреть файл

@ -77,7 +77,6 @@ comp_method_none_comp(LIBSSH2_SESSION *session,
*/ */
static int static int
comp_method_none_decomp(LIBSSH2_SESSION * session, comp_method_none_decomp(LIBSSH2_SESSION * session,
int compress,
unsigned char **dest, unsigned char **dest,
size_t *dest_len, size_t *dest_len,
size_t payload_limit, size_t payload_limit,
@ -86,7 +85,6 @@ comp_method_none_decomp(LIBSSH2_SESSION * session,
size_t src_len, void **abstract) size_t src_len, void **abstract)
{ {
(void) session; (void) session;
(void) compress;
(void) payload_limit; (void) payload_limit;
(void) abstract; (void) abstract;
*dest = (unsigned char *) src; *dest = (unsigned char *) src;
@ -221,7 +219,6 @@ comp_method_zlib_comp(LIBSSH2_SESSION *session,
*/ */
static int static int
comp_method_zlib_decomp(LIBSSH2_SESSION * session, comp_method_zlib_decomp(LIBSSH2_SESSION * session,
int compress,
unsigned char **dest, unsigned char **dest,
size_t *dest_len, size_t *dest_len,
size_t payload_limit, size_t payload_limit,
@ -233,7 +230,7 @@ comp_method_zlib_decomp(LIBSSH2_SESSION * session,
/* A short-term alloc of a full data chunk is better than a series of /* A short-term alloc of a full data chunk is better than a series of
reallocs */ reallocs */
char *out; char *out;
int out_maxlen = compress ? (src_len + 4) : (8 * src_len); int out_maxlen = 8 * src_len;
int limiter = 0; int limiter = 0;
/* If strm is null, then we have not yet been initialized. */ /* If strm is null, then we have not yet been initialized. */
@ -259,34 +256,28 @@ comp_method_zlib_decomp(LIBSSH2_SESSION * session,
strm->next_out = (unsigned char *) LIBSSH2_ALLOC(session, out_maxlen); strm->next_out = (unsigned char *) LIBSSH2_ALLOC(session, out_maxlen);
out = (char *) strm->next_out; out = (char *) strm->next_out;
strm->avail_out = out_maxlen; strm->avail_out = out_maxlen;
if (!strm->next_out) { if (!strm->next_out)
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate compression/decompression " "Unable to allocate decompression buffer");
"buffer");
}
while (strm->avail_in) { while (strm->avail_in) {
int status; int status;
if (compress) {
status = deflate(strm, Z_PARTIAL_FLUSH);
} else {
status = inflate(strm, Z_PARTIAL_FLUSH); status = inflate(strm, Z_PARTIAL_FLUSH);
}
if (status != Z_OK) { if (status != Z_OK) {
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
_libssh2_debug(session, LIBSSH2_TRACE_TRANS, _libssh2_debug(session, LIBSSH2_TRACE_TRANS,
"unhandled zlib error %d", status); "unhandled zlib error %d", status);
return _libssh2_error(session, LIBSSH2_ERROR_ZLIB, return _libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"compress/decompression failure"); "decompression failure");
} }
if (strm->avail_in) { if (strm->avail_in) {
size_t out_ofs = out_maxlen - strm->avail_out; size_t out_ofs = out_maxlen - strm->avail_out;
char *newout; char *newout;
out_maxlen += out_maxlen += 8 * strm->avail_in;
compress ? (strm->avail_in + 4) : (8 * strm->avail_in);
if ((out_maxlen > (int) payload_limit) && !compress && limiter++) { if ((out_maxlen > (int) payload_limit) && limiter++) {
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
return _libssh2_error(session, LIBSSH2_ERROR_ZLIB, return _libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"Excessive growth in decompression phase"); "Excessive growth in decompression phase");
@ -296,20 +287,18 @@ comp_method_zlib_decomp(LIBSSH2_SESSION * session,
if (!newout) { if (!newout) {
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand compress/" "Unable to expand decompression buffer");
"decompression buffer");
} }
out = newout; out = newout;
strm->next_out = (unsigned char *) out + out_ofs; strm->next_out = (unsigned char *) out + out_ofs;
strm->avail_out += strm->avail_out += 8 * strm->avail_in;
compress ? (strm->avail_in + 4) : (8 * strm->avail_in);
} else } else
while (!strm->avail_out) { while (!strm->avail_out) {
/* Done with input, might be a byte or two in internal buffer /* Done with input, might be a byte or two in internal buffer
* during compress. Or potentially many bytes if it's a * during compress. Or potentially many bytes if it's a
* decompress * decompress
*/ */
int grow_size = compress ? 8 : 2048; int grow_size = 2048;
char *newout; char *newout;
if (out_maxlen >= (int) payload_limit) { if (out_maxlen >= (int) payload_limit) {
@ -330,24 +319,21 @@ comp_method_zlib_decomp(LIBSSH2_SESSION * session,
if (!newout) { if (!newout) {
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to expand final compress/" "Unable to expand final "
"decompress buffer"); "decompress buffer");
} }
out = newout; out = newout;
strm->next_out = (unsigned char *) out + out_maxlen - strm->next_out = (unsigned char *) out + out_maxlen -
grow_size; grow_size;
if (compress) {
status = deflate(strm, Z_PARTIAL_FLUSH);
} else {
status = inflate(strm, Z_PARTIAL_FLUSH); status = inflate(strm, Z_PARTIAL_FLUSH);
}
if (status != Z_OK) { if (status != Z_OK) {
LIBSSH2_FREE(session, out); LIBSSH2_FREE(session, out);
_libssh2_debug(session, LIBSSH2_TRACE_TRANS, _libssh2_debug(session, LIBSSH2_TRACE_TRANS,
"unhandled zlib error %d", status); "unhandled zlib error %d", status);
return _libssh2_error(session, LIBSSH2_ERROR_ZLIB, return _libssh2_error(session, LIBSSH2_ERROR_ZLIB,
"compress/decompression failure"); "decompression failure");
} }
} }
} }

Просмотреть файл

@ -890,7 +890,7 @@ struct _LIBSSH2_COMP_METHOD
const unsigned char *src, const unsigned char *src,
size_t src_len, size_t src_len,
void **abstract); void **abstract);
int (*decomp) (LIBSSH2_SESSION *session, int compress, int (*decomp) (LIBSSH2_SESSION *session,
unsigned char **dest, unsigned char **dest,
size_t *dest_len, size_t payload_limit, size_t *dest_len, size_t payload_limit,
int *free_dest, const unsigned char *src, int *free_dest, const unsigned char *src,

Просмотреть файл

@ -200,7 +200,7 @@ fullpacket(LIBSSH2_SESSION * session, int encrypted /* 1 or 0 */ )
size_t data_len; size_t data_len;
int free_payload = 1; int free_payload = 1;
rc = session->remote.comp->decomp(session, 0, rc = session->remote.comp->decomp(session,
&data, &data_len, &data, &data_len,
LIBSSH2_PACKET_MAXDECOMP, LIBSSH2_PACKET_MAXDECOMP,
&free_payload, &free_payload,