aboutsummaryrefslogtreecommitdiff
path: root/src/lib/compress
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-06-28 16:25:06 -0400
committerNick Mathewson <nickm@torproject.org>2018-06-28 16:28:08 -0400
commitd8b34e0886c9fd08fb51e0de4fadca7da82056ff (patch)
tree5b2a8cea0d0a683f7ab1e71a158c28021f9dfb99 /src/lib/compress
parentbe40ad51b68fa87bafb88e55db7856d373274a4d (diff)
downloadtor-d8b34e0886c9fd08fb51e0de4fadca7da82056ff.tar.gz
tor-d8b34e0886c9fd08fb51e0de4fadca7da82056ff.zip
Move buffers into container
Split the network-only and compression-only parts of buffers into the appropriate modules.
Diffstat (limited to 'src/lib/compress')
-rw-r--r--src/lib/compress/.may_include1
-rw-r--r--src/lib/compress/compress.h4
-rw-r--r--src/lib/compress/compress_buf.c77
-rw-r--r--src/lib/compress/include.am3
4 files changed, 84 insertions, 1 deletions
diff --git a/src/lib/compress/.may_include b/src/lib/compress/.may_include
index 0361774699..68fe9f1c54 100644
--- a/src/lib/compress/.may_include
+++ b/src/lib/compress/.may_include
@@ -2,6 +2,7 @@ orconfig.h
lib/arch/*.h
lib/cc/*.h
lib/compress/*.h
+lib/container/*.h
lib/ctime/*.h
lib/intmath/*.h
lib/log/*.h
diff --git a/src/lib/compress/compress.h b/src/lib/compress/compress.h
index f88cf2cb2d..ae98e1aaef 100644
--- a/src/lib/compress/compress.h
+++ b/src/lib/compress/compress.h
@@ -92,4 +92,8 @@ size_t tor_compress_state_size(const tor_compress_state_t *state);
void tor_compress_init(void);
void tor_compress_log_init_warnings(void);
+struct buf_t;
+int buf_add_compress(struct buf_t *buf, struct tor_compress_state_t *state,
+ const char *data, size_t data_len, int done);
+
#endif /* !defined(TOR_COMPRESS_H) */
diff --git a/src/lib/compress/compress_buf.c b/src/lib/compress/compress_buf.c
new file mode 100644
index 0000000000..4b88772382
--- /dev/null
+++ b/src/lib/compress/compress_buf.c
@@ -0,0 +1,77 @@
+/* Copyright (c) 2003, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define BUFFERS_PRIVATE
+#include "lib/cc/compat_compiler.h"
+#include "lib/container/buffers.h"
+#include "lib/compress/compress.h"
+#include "lib/log/util_bug.h"
+
+#ifdef PARANOIA
+/** Helper: If PARANOIA is defined, assert that the buffer in local variable
+ * <b>buf</b> is well-formed. */
+#define check() STMT_BEGIN buf_assert_ok(buf); STMT_END
+#else
+#define check() STMT_NIL
+#endif /* defined(PARANOIA) */
+
+/** Compress or uncompress the <b>data_len</b> bytes in <b>data</b> using the
+ * compression state <b>state</b>, appending the result to <b>buf</b>. If
+ * <b>done</b> is true, flush the data in the state and finish the
+ * compression/uncompression. Return -1 on failure, 0 on success. */
+int
+buf_add_compress(buf_t *buf, tor_compress_state_t *state,
+ const char *data, size_t data_len,
+ const int done)
+{
+ char *next;
+ size_t old_avail, avail;
+ int over = 0;
+
+ do {
+ int need_new_chunk = 0;
+ if (!buf->tail || ! CHUNK_REMAINING_CAPACITY(buf->tail)) {
+ size_t cap = data_len / 4;
+ buf_add_chunk_with_capacity(buf, cap, 1);
+ }
+ next = CHUNK_WRITE_PTR(buf->tail);
+ avail = old_avail = CHUNK_REMAINING_CAPACITY(buf->tail);
+ switch (tor_compress_process(state, &next, &avail,
+ &data, &data_len, done)) {
+ case TOR_COMPRESS_DONE:
+ over = 1;
+ break;
+ case TOR_COMPRESS_ERROR:
+ return -1;
+ case TOR_COMPRESS_OK:
+ if (data_len == 0) {
+ tor_assert_nonfatal(!done);
+ over = 1;
+ }
+ break;
+ case TOR_COMPRESS_BUFFER_FULL:
+ if (avail) {
+ /* The compression module says we need more room
+ * (TOR_COMPRESS_BUFFER_FULL). Start a new chunk automatically,
+ * whether were going to or not. */
+ need_new_chunk = 1;
+ }
+ if (data_len == 0 && !done) {
+ /* We've consumed all the input data, though, so there's no
+ * point in forging ahead right now. */
+ over = 1;
+ }
+ break;
+ }
+ buf->datalen += old_avail - avail;
+ buf->tail->datalen += old_avail - avail;
+ if (need_new_chunk) {
+ buf_add_chunk_with_capacity(buf, data_len/4, 1);
+ }
+
+ } while (!over);
+ check();
+ return 0;
+}
diff --git a/src/lib/compress/include.am b/src/lib/compress/include.am
index eb3a89c355..75c9032bd2 100644
--- a/src/lib/compress/include.am
+++ b/src/lib/compress/include.am
@@ -5,8 +5,9 @@ if UNITTESTS_ENABLED
noinst_LIBRARIES += src/lib/libtor-compress-testing.a
endif
-src_lib_libtor_compress_a_SOURCES = \
+src_lib_libtor_compress_a_SOURCES = \
src/lib/compress/compress.c \
+ src/lib/compress/compress_buf.c \
src/lib/compress/compress_lzma.c \
src/lib/compress/compress_none.c \
src/lib/compress/compress_zlib.c \