mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2025-02-23 14:40:51 +01:00
Add basic wrappers for zlib/gzip
svn:r2324
This commit is contained in:
parent
dccc4b7415
commit
3590eb535a
4 changed files with 302 additions and 7 deletions
|
@ -3,6 +3,6 @@ noinst_LIBRARIES = libor.a
|
|||
|
||||
#CFLAGS = -Wall -Wpointer-arith -O2
|
||||
|
||||
libor_a_SOURCES = log.c crypto.c fakepoll.c util.c aes.c tortls.c
|
||||
libor_a_SOURCES = log.c crypto.c fakepoll.c util.c aes.c tortls.c torgzip.c
|
||||
|
||||
noinst_HEADERS = log.h crypto.h fakepoll.h test.h util.h aes.h torint.h tortls.h strlcpy.c strlcat.c
|
||||
noinst_HEADERS = log.h crypto.h fakepoll.h test.h util.h aes.h torint.h tortls.h strlcpy.c strlcat.c torgzip.h
|
||||
|
|
221
src/common/torgzip.c
Normal file
221
src/common/torgzip.c
Normal file
|
@ -0,0 +1,221 @@
|
|||
/* Copyright 2004 Roger Dingledine */
|
||||
/* See LICENSE for licensing information */
|
||||
/* $Id$ */
|
||||
|
||||
/**
|
||||
* \file torgzip.c
|
||||
*
|
||||
* \brief Simple in-memory gzip implementation.
|
||||
**/
|
||||
|
||||
#include "orconfig.h"
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <zlib.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "log.h"
|
||||
#include "torgzip.h"
|
||||
|
||||
static int gzip_is_supported = -1;
|
||||
|
||||
int
|
||||
is_gzip_supported(void)
|
||||
{
|
||||
if (gzip_is_supported >= 0)
|
||||
return gzip_is_supported;
|
||||
|
||||
if (!strcmpstart(ZLIB_VERSION, "0.") ||
|
||||
!strcmpstart(ZLIB_VERSION, "1.0") ||
|
||||
!strcmpstart(ZLIB_VERSION, "1.1"))
|
||||
gzip_is_supported = 0;
|
||||
else
|
||||
gzip_is_supported = 1;
|
||||
|
||||
return gzip_is_supported;
|
||||
}
|
||||
|
||||
static INLINE int
|
||||
method_bits(compress_method_t method)
|
||||
{
|
||||
/* Bits+16 means "use gzip" in zlib >= 1.2 */
|
||||
return method == GZIP_METHOD ? 15+16 : 15;
|
||||
}
|
||||
|
||||
int
|
||||
tor_gzip_compress(char **out, size_t *out_len,
|
||||
const char *in, size_t in_len,
|
||||
compress_method_t method)
|
||||
{
|
||||
struct z_stream_s *stream = NULL;
|
||||
size_t out_size;
|
||||
off_t offset;
|
||||
|
||||
tor_assert(out && out_len && in);
|
||||
|
||||
if (method == GZIP_METHOD && !is_gzip_supported()) {
|
||||
/* Old zlib version don't support gzip in deflateInit2 */
|
||||
log_fn(LOG_WARN, "Gzip not supported with zlib %s", ZLIB_VERSION);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = NULL;
|
||||
|
||||
stream = tor_malloc_zero(sizeof(struct z_stream_s));
|
||||
stream->zalloc = Z_NULL;
|
||||
stream->zfree = Z_NULL;
|
||||
stream->opaque = NULL;
|
||||
stream->next_in = (unsigned char*) in;
|
||||
stream->avail_in = in_len;
|
||||
|
||||
if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
|
||||
method_bits(method),
|
||||
8, Z_DEFAULT_STRATEGY) != Z_OK) {
|
||||
printf("Z");
|
||||
log_fn(LOG_WARN, "Error from deflateInit2: %s",
|
||||
stream->msg?stream->msg:"<no message>");
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Guess 50% compression. */
|
||||
out_size = in_len / 2;
|
||||
if (out_size < 1024) out_size = 1024;
|
||||
*out = tor_malloc(out_size);
|
||||
stream->next_out = *out;
|
||||
stream->avail_out = out_size;
|
||||
|
||||
while (1) {
|
||||
switch (deflate(stream, Z_FINISH))
|
||||
{
|
||||
case Z_STREAM_END:
|
||||
goto done;
|
||||
case Z_OK:
|
||||
/* In case zlib doesn't work as I think .... */
|
||||
if (stream->avail_out >= stream->avail_in+16)
|
||||
break;
|
||||
case Z_BUF_ERROR:
|
||||
offset = stream->next_out - ((unsigned char*)*out);
|
||||
out_size *= 2;
|
||||
*out = tor_realloc(*out, out_size);
|
||||
stream->next_out = *out + offset;
|
||||
stream->avail_out = out_size - offset;
|
||||
break;
|
||||
default:
|
||||
log_fn(LOG_WARN, "Gzip compression didn't finish: %s",
|
||||
stream->msg ? stream->msg : "<no message>");
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
done:
|
||||
*out_len = stream->total_out;
|
||||
if (deflateEnd(stream)!=Z_OK) {
|
||||
log_fn(LOG_WARN, "Error freeing gzip structures");
|
||||
goto err;
|
||||
}
|
||||
tor_free(stream);
|
||||
|
||||
return 0;
|
||||
err:
|
||||
if (stream) {
|
||||
deflateEnd(stream);
|
||||
tor_free(stream);
|
||||
}
|
||||
if (*out) {
|
||||
tor_free(*out);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
tor_gzip_uncompress(char **out, size_t *out_len,
|
||||
const char *in, size_t in_len,
|
||||
compress_method_t method)
|
||||
{
|
||||
struct z_stream_s *stream = NULL;
|
||||
size_t out_size;
|
||||
off_t offset;
|
||||
|
||||
tor_assert(out && out_len && in);
|
||||
|
||||
if (method == GZIP_METHOD && !is_gzip_supported()) {
|
||||
/* Old zlib version don't support gzip in inflateInit2 */
|
||||
log_fn(LOG_WARN, "Gzip not supported with zlib %s", ZLIB_VERSION);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = NULL;
|
||||
|
||||
stream = tor_malloc_zero(sizeof(struct z_stream_s));
|
||||
stream->zalloc = Z_NULL;
|
||||
stream->zfree = Z_NULL;
|
||||
stream->opaque = NULL;
|
||||
stream->next_in = (unsigned char*) in;
|
||||
stream->avail_in = in_len;
|
||||
|
||||
if (inflateInit2(stream,
|
||||
method_bits(method)) != Z_OK) {
|
||||
log_fn(LOG_WARN, "Error from inflateInit2: %s",
|
||||
stream->msg?stream->msg:"<no message>");
|
||||
goto err;
|
||||
}
|
||||
|
||||
out_size = in_len * 2; /* guess 50% compression. */
|
||||
if (out_size < 1024) out_size = 1024;
|
||||
|
||||
*out = tor_malloc(out_size);
|
||||
stream->next_out = *out;
|
||||
stream->avail_out = out_size;
|
||||
|
||||
while (1) {
|
||||
switch(inflate(stream, Z_FINISH))
|
||||
{
|
||||
case Z_STREAM_END:
|
||||
goto done;
|
||||
case Z_OK:
|
||||
/* In case zlib doesn't work as I think.... */
|
||||
if (stream->avail_out >= stream->avail_in+16)
|
||||
break;
|
||||
case Z_BUF_ERROR:
|
||||
offset = stream->next_out - ((unsigned char*)*out);
|
||||
out_size *= 2;
|
||||
*out = tor_realloc(*out, out_size);
|
||||
stream->next_out = *out + offset;
|
||||
stream->avail_out = out_size - offset;
|
||||
break;
|
||||
default:
|
||||
log_fn(LOG_WARN, "Gzip decompression returned an error: %s",
|
||||
stream->msg ? stream->msg : "<no message>");
|
||||
goto err;
|
||||
}
|
||||
|
||||
}
|
||||
done:
|
||||
*out_len = stream->total_out;
|
||||
if (inflateEnd(stream)!=Z_OK) {
|
||||
log_fn(LOG_WARN, "Error freeing gzip structures");
|
||||
goto err;
|
||||
}
|
||||
tor_free(stream);
|
||||
|
||||
return 0;
|
||||
err:
|
||||
if (stream) {
|
||||
inflateEnd(stream);
|
||||
tor_free(stream);
|
||||
}
|
||||
if (*out) {
|
||||
tor_free(*out);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
Local Variables:
|
||||
mode:c
|
||||
indent-tabs-mode:nil
|
||||
c-basic-offset:2
|
||||
End:
|
||||
*/
|
26
src/common/torgzip.h
Normal file
26
src/common/torgzip.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
/* Copyright 2003 Roger Dingledine */
|
||||
/* See LICENSE for licensing information */
|
||||
/* $Id$ */
|
||||
|
||||
/**
|
||||
* \file torgzip.h
|
||||
* \brief Headers for torgzip.h
|
||||
**/
|
||||
|
||||
#ifndef __TORGZIP_H
|
||||
#define __TORGZIP_H
|
||||
|
||||
typedef enum { GZIP_METHOD, ZLIB_METHOD } compress_method_t;
|
||||
|
||||
int
|
||||
tor_gzip_compress(char **out, size_t *out_len,
|
||||
const char *in, size_t in_len,
|
||||
compress_method_t method);
|
||||
int
|
||||
tor_gzip_uncompress(char **out, size_t *out_len,
|
||||
const char *in, size_t in_len,
|
||||
compress_method_t method);
|
||||
|
||||
int is_gzip_supported(void);
|
||||
|
||||
#endif
|
|
@ -14,6 +14,7 @@
|
|||
#include <dirent.h>
|
||||
#include "or.h"
|
||||
#include "../common/test.h"
|
||||
#include "../common/torgzip.h"
|
||||
|
||||
extern or_options_t options;
|
||||
|
||||
|
@ -552,15 +553,15 @@ test_util() {
|
|||
test_eq((void*)555, smartlist_get(sl,5));
|
||||
|
||||
smartlist_clear(sl);
|
||||
smartlist_split_string(sl, "abc", ":", 0);
|
||||
smartlist_split_string(sl, "abc", ":", 0, 0);
|
||||
test_eq(1, smartlist_len(sl));
|
||||
test_streq("abc", smartlist_get(sl, 0));
|
||||
smartlist_split_string(sl, "a::bc::", "::", 0);
|
||||
smartlist_split_string(sl, "a::bc::", "::", 0, 0);
|
||||
test_eq(4, smartlist_len(sl));
|
||||
test_streq("a", smartlist_get(sl, 1));
|
||||
test_streq("bc", smartlist_get(sl, 2));
|
||||
test_streq("", smartlist_get(sl, 3));
|
||||
smartlist_split_string(sl, "/def/ /ghijk", "/", 0);
|
||||
smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
|
||||
test_eq(8, smartlist_len(sl));
|
||||
test_streq("", smartlist_get(sl, 4));
|
||||
test_streq("def", smartlist_get(sl, 5));
|
||||
|
@ -569,12 +570,12 @@ test_util() {
|
|||
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
||||
smartlist_clear(sl);
|
||||
|
||||
smartlist_split_string(sl, "a,bbd,cdef", ",", 1);
|
||||
smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
|
||||
test_eq(3, smartlist_len(sl));
|
||||
test_streq("a", smartlist_get(sl,0));
|
||||
test_streq("bbd", smartlist_get(sl,1));
|
||||
test_streq("cdef", smartlist_get(sl,2));
|
||||
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", 1);
|
||||
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE, 0);
|
||||
test_eq(8, smartlist_len(sl));
|
||||
test_streq("z", smartlist_get(sl,3));
|
||||
test_streq("zhasd", smartlist_get(sl,4));
|
||||
|
@ -583,11 +584,57 @@ test_util() {
|
|||
test_streq("", smartlist_get(sl,7));
|
||||
|
||||
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
||||
smartlist_clear(sl);
|
||||
|
||||
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
||||
test_eq(3, smartlist_len(sl));
|
||||
test_streq("z", smartlist_get(sl, 0));
|
||||
test_streq("zhasd", smartlist_get(sl, 1));
|
||||
test_streq("bnud", smartlist_get(sl, 2));
|
||||
smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
|
||||
test_eq(5, smartlist_len(sl));
|
||||
test_streq("z", smartlist_get(sl, 3));
|
||||
test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
|
||||
|
||||
|
||||
/* XXXX test older functions. */
|
||||
smartlist_free(sl);
|
||||
}
|
||||
|
||||
void
|
||||
test_gzip() {
|
||||
char *buf1, *buf2=NULL, *buf3=NULL;
|
||||
size_t len1, len2;
|
||||
|
||||
buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
|
||||
if (is_gzip_supported()) {
|
||||
test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
|
||||
GZIP_METHOD));
|
||||
test_assert(buf2);
|
||||
test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
|
||||
|
||||
test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, GZIP_METHOD));
|
||||
test_assert(buf3);
|
||||
test_streq(buf1,buf3);
|
||||
|
||||
tor_free(buf2);
|
||||
tor_free(buf3);
|
||||
}
|
||||
|
||||
test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
|
||||
ZLIB_METHOD));
|
||||
test_assert(buf2);
|
||||
test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
|
||||
|
||||
test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, ZLIB_METHOD));
|
||||
test_assert(buf3);
|
||||
test_streq(buf1,buf3);
|
||||
|
||||
tor_free(buf2);
|
||||
tor_free(buf3);
|
||||
tor_free(buf1);
|
||||
}
|
||||
|
||||
static void* _squareAndRemoveK4(const char *key, void*val, void *data)
|
||||
{
|
||||
int *ip = (int*)data;
|
||||
|
@ -1037,6 +1084,7 @@ main(int c, char**v){
|
|||
test_crypto();
|
||||
test_crypto_dh();
|
||||
puts("\n========================= Util ============================");
|
||||
test_gzip();
|
||||
test_util();
|
||||
test_strmap();
|
||||
puts("\n========================= Onion Skins =====================");
|
||||
|
|
Loading…
Add table
Reference in a new issue