Rename tor_gzip_{compress,uncompress} to tor_{compress,uncompress}.

To allow us to use the API name `tor_compress` and `tor_uncompress` as
the main entry-point for all compression/uncompression and not just gzip
and zlib.

See https://bugs.torproject.org/21663
This commit is contained in:
Alexander Færøy 2017-04-17 14:29:10 +02:00
parent e8b025dfc3
commit 44cb86adbe
No known key found for this signature in database
GPG key ID: E15081D5D3C3DB53
7 changed files with 57 additions and 56 deletions

View file

@ -132,7 +132,7 @@ is_compression_bomb(size_t size_in, size_t size_out)
* Return 0 on success, -1 on failure. * Return 0 on success, -1 on failure.
*/ */
int int
tor_gzip_compress(char **out, size_t *out_len, tor_compress(char **out, size_t *out_len,
const char *in, size_t in_len, const char *in, size_t in_len,
compress_method_t method) compress_method_t method)
{ {
@ -252,7 +252,7 @@ tor_gzip_compress(char **out, size_t *out_len,
* or corrupt inputs at <b>protocol_warn_level</b>. * or corrupt inputs at <b>protocol_warn_level</b>.
*/ */
int int
tor_gzip_uncompress(char **out, size_t *out_len, tor_uncompress(char **out, size_t *out_len,
const char *in, size_t in_len, const char *in, size_t in_len,
compress_method_t method, compress_method_t method,
int complete_only, int complete_only,

View file

@ -28,11 +28,11 @@ typedef enum {
} compression_level_t; } compression_level_t;
int int
tor_gzip_compress(char **out, size_t *out_len, tor_compress(char **out, size_t *out_len,
const char *in, size_t in_len, const char *in, size_t in_len,
compress_method_t method); compress_method_t method);
int int
tor_gzip_uncompress(char **out, size_t *out_len, tor_uncompress(char **out, size_t *out_len,
const char *in, size_t in_len, const char *in, size_t in_len,
compress_method_t method, compress_method_t method,
int complete_only, int complete_only,

View file

@ -2096,14 +2096,14 @@ connection_dir_client_reached_eof(dir_connection_t *conn)
} }
/* Try declared compression first if we can. */ /* Try declared compression first if we can. */
if (compression == GZIP_METHOD || compression == ZLIB_METHOD) if (compression == GZIP_METHOD || compression == ZLIB_METHOD)
tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression, tor_uncompress(&new_body, &new_len, body, body_len, compression,
!allow_partial, LOG_PROTOCOL_WARN); !allow_partial, LOG_PROTOCOL_WARN);
/* Okay, if that didn't work, and we think that it was compressed /* Okay, if that didn't work, and we think that it was compressed
* differently, try that. */ * differently, try that. */
if (!new_body && if (!new_body &&
(guessed == GZIP_METHOD || guessed == ZLIB_METHOD) && (guessed == GZIP_METHOD || guessed == ZLIB_METHOD) &&
compression != guessed) compression != guessed)
tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed, tor_uncompress(&new_body, &new_len, body, body_len, guessed,
!allow_partial, LOG_PROTOCOL_WARN); !allow_partial, LOG_PROTOCOL_WARN);
/* If we're pretty sure that we have a compressed directory, and /* If we're pretty sure that we have a compressed directory, and
* we didn't manage to uncompress it, then warn and bail. */ * we didn't manage to uncompress it, then warn and bail. */

View file

@ -1176,7 +1176,7 @@ new_cached_dir(char *s, time_t published)
d->dir = s; d->dir = s;
d->dir_len = strlen(s); d->dir_len = strlen(s);
d->published = published; d->published = published;
if (tor_gzip_compress(&(d->dir_z), &(d->dir_z_len), d->dir, d->dir_len, if (tor_compress(&(d->dir_z), &(d->dir_z_len), d->dir, d->dir_len,
ZLIB_METHOD)) { ZLIB_METHOD)) {
log_warn(LD_BUG, "Error compressing directory"); log_warn(LD_BUG, "Error compressing directory");
} }

View file

@ -607,7 +607,7 @@ test_buffers_zlib_impl(int finalize_with_nil)
tt_int_op(fetch_from_buf(contents, in_len, buf), OP_EQ, 0); tt_int_op(fetch_from_buf(contents, in_len, buf), OP_EQ, 0);
tt_int_op(0, OP_EQ, tor_gzip_uncompress(&expanded, &out_len, tt_int_op(0, OP_EQ, tor_uncompress(&expanded, &out_len,
contents, in_len, contents, in_len,
ZLIB_METHOD, 1, ZLIB_METHOD, 1,
LOG_WARN)); LOG_WARN));
@ -676,8 +676,9 @@ test_buffers_zlib_fin_at_chunk_end(void *arg)
tt_uint_op(in_len, OP_GT, headerjunk); tt_uint_op(in_len, OP_GT, headerjunk);
tt_int_op(0, OP_EQ, tor_gzip_uncompress(&expanded, &out_len, tt_int_op(0, OP_EQ, tor_uncompress(&expanded, &out_len,
contents + headerjunk, in_len - headerjunk, contents + headerjunk,
in_len - headerjunk,
ZLIB_METHOD, 1, ZLIB_METHOD, 1,
LOG_WARN)); LOG_WARN));

View file

@ -1832,7 +1832,7 @@ test_dir_handle_get_status_vote_current_consensus_ns(void* data)
comp_body_used); comp_body_used);
tt_int_op(ZLIB_METHOD, OP_EQ, compression); tt_int_op(ZLIB_METHOD, OP_EQ, compression);
tor_gzip_uncompress(&body, &body_used, comp_body, comp_body_used, tor_uncompress(&body, &body_used, comp_body, comp_body_used,
compression, 0, LOG_PROTOCOL_WARN); compression, 0, LOG_PROTOCOL_WARN);
tt_str_op(NETWORK_STATUS, OP_EQ, body); tt_str_op(NETWORK_STATUS, OP_EQ, body);

View file

@ -2255,13 +2255,13 @@ test_util_gzip(void *arg)
buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ"); buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD); tt_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1, tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
GZIP_METHOD)); GZIP_METHOD));
tt_assert(buf2 != NULL); tt_assert(buf2 != NULL);
tt_int_op(len1, OP_LT, strlen(buf1)); tt_int_op(len1, OP_LT, strlen(buf1));
tt_int_op(detect_compression_method(buf2, len1), OP_EQ, GZIP_METHOD); tt_int_op(detect_compression_method(buf2, len1), OP_EQ, GZIP_METHOD);
tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1,
GZIP_METHOD, 1, LOG_INFO)); GZIP_METHOD, 1, LOG_INFO));
tt_assert(buf3 != NULL); tt_assert(buf3 != NULL);
tt_int_op(strlen(buf1) + 1, OP_EQ, len2); tt_int_op(strlen(buf1) + 1, OP_EQ, len2);
@ -2270,12 +2270,12 @@ test_util_gzip(void *arg)
tor_free(buf2); tor_free(buf2);
tor_free(buf3); tor_free(buf3);
tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1, tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
ZLIB_METHOD)); ZLIB_METHOD));
tt_assert(buf2); tt_assert(buf2);
tt_int_op(detect_compression_method(buf2, len1), OP_EQ, ZLIB_METHOD); tt_int_op(detect_compression_method(buf2, len1), OP_EQ, ZLIB_METHOD);
tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1,
ZLIB_METHOD, 1, LOG_INFO)); ZLIB_METHOD, 1, LOG_INFO));
tt_assert(buf3 != NULL); tt_assert(buf3 != NULL);
tt_int_op(strlen(buf1) + 1, OP_EQ, len2); tt_int_op(strlen(buf1) + 1, OP_EQ, len2);
@ -2285,7 +2285,7 @@ test_util_gzip(void *arg)
tor_free(buf3); tor_free(buf3);
buf2 = tor_reallocarray(buf2, len1, 2); buf2 = tor_reallocarray(buf2, len1, 2);
memcpy(buf2+len1, buf2, len1); memcpy(buf2+len1, buf2, len1);
tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2, tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1*2,
ZLIB_METHOD, 1, LOG_INFO)); ZLIB_METHOD, 1, LOG_INFO));
tt_int_op((strlen(buf1)+1)*2, OP_EQ, len2); tt_int_op((strlen(buf1)+1)*2, OP_EQ, len2);
tt_mem_op(buf3, OP_EQ, tt_mem_op(buf3, OP_EQ,
@ -2300,11 +2300,11 @@ test_util_gzip(void *arg)
/* Check whether we can uncompress partial strings. */ /* Check whether we can uncompress partial strings. */
buf1 = buf1 =
tor_strdup("String with low redundancy that won't be compressed much."); tor_strdup("String with low redundancy that won't be compressed much.");
tt_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1, tt_assert(!tor_compress(&buf2, &len1, buf1, strlen(buf1)+1,
ZLIB_METHOD)); ZLIB_METHOD));
tt_int_op(len1, OP_GT, 16); tt_int_op(len1, OP_GT, 16);
/* when we allow an incomplete string, we should succeed.*/ /* when we allow an incomplete string, we should succeed.*/
tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16, tt_assert(!tor_uncompress(&buf3, &len2, buf2, len1-16,
ZLIB_METHOD, 0, LOG_INFO)); ZLIB_METHOD, 0, LOG_INFO));
tt_assert(len2 > 5); tt_assert(len2 > 5);
buf3[len2]='\0'; buf3[len2]='\0';
@ -2312,7 +2312,7 @@ test_util_gzip(void *arg)
/* when we demand a complete string, this must fail. */ /* when we demand a complete string, this must fail. */
tor_free(buf3); tor_free(buf3);
tt_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16, tt_assert(tor_uncompress(&buf3, &len2, buf2, len1-16,
ZLIB_METHOD, 1, LOG_INFO)); ZLIB_METHOD, 1, LOG_INFO));
tt_assert(buf3 == NULL); tt_assert(buf3 == NULL);
@ -2338,7 +2338,7 @@ test_util_gzip(void *arg)
tt_int_op(0, OP_EQ, len2); tt_int_op(0, OP_EQ, len2);
tt_assert(cp1 > cp2); /* Make sure we really added something. */ tt_assert(cp1 > cp2); /* Make sure we really added something. */
tt_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1, tt_assert(!tor_uncompress(&buf3, &len2, buf1, 1024-len1,
ZLIB_METHOD, 1, LOG_WARN)); ZLIB_METHOD, 1, LOG_WARN));
/* Make sure it compressed right. */ /* Make sure it compressed right. */
tt_str_op(buf3, OP_EQ, "ABCDEFGHIJABCDEFGHIJ"); tt_str_op(buf3, OP_EQ, "ABCDEFGHIJABCDEFGHIJ");
@ -2368,7 +2368,7 @@ test_util_gzip_compression_bomb(void *arg)
/* Make sure we can't produce a compression bomb */ /* Make sure we can't produce a compression bomb */
setup_full_capture_of_logs(LOG_WARN); setup_full_capture_of_logs(LOG_WARN);
tt_int_op(-1, OP_EQ, tor_gzip_compress(&result, &result_len, tt_int_op(-1, OP_EQ, tor_compress(&result, &result_len,
one_mb, one_million, one_mb, one_million,
ZLIB_METHOD)); ZLIB_METHOD));
expect_single_log_msg_containing( expect_single_log_msg_containing(
@ -2381,7 +2381,7 @@ test_util_gzip_compression_bomb(void *arg)
const char compression_bomb[1039] = const char compression_bomb[1039] =
{ 0x78, 0xDA, 0xED, 0xC1, 0x31, 0x01, 0x00, 0x00, 0x00, 0xC2, { 0x78, 0xDA, 0xED, 0xC1, 0x31, 0x01, 0x00, 0x00, 0x00, 0xC2,
0xA0, 0xF5, 0x4F, 0x6D, 0x08, 0x5F, 0xA0 /* .... */ }; 0xA0, 0xF5, 0x4F, 0x6D, 0x08, 0x5F, 0xA0 /* .... */ };
tt_int_op(-1, OP_EQ, tor_gzip_uncompress(&result, &result_len, tt_int_op(-1, OP_EQ, tor_uncompress(&result, &result_len,
compression_bomb, 1039, compression_bomb, 1039,
ZLIB_METHOD, 0, LOG_WARN)); ZLIB_METHOD, 0, LOG_WARN));