aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/compression/comp_util.h
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-11-18 13:42:36 +0000
committerlloyd <[email protected]>2014-11-18 13:42:36 +0000
commitdec2e0d9e493b53c48ca26b47028ab309d3cc586 (patch)
tree2242f756ece5e1a9cc728dc208c154e694314d5a /src/lib/compression/comp_util.h
parentb5e2a1ce3044084e43ff523f148b0cc8e95ce283 (diff)
Convert compression filters to in-place transforms and refactor
to minimize the amount of logic needed in the files specific to each library.
Diffstat (limited to 'src/lib/compression/comp_util.h')
-rw-r--r--src/lib/compression/comp_util.h90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/lib/compression/comp_util.h b/src/lib/compression/comp_util.h
new file mode 100644
index 000000000..15fc23418
--- /dev/null
+++ b/src/lib/compression/comp_util.h
@@ -0,0 +1,90 @@
+/*
+* Shared code for compression libraries
+* (C) 2014 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_COMPRESSION_UTILS_H__
+#define BOTAN_COMPRESSION_UTILS_H__
+
+#include <botan/compression.h>
+#include <cstring>
+#include <memory>
+#include <unordered_map>
+
+namespace Botan {
+
+/*
+* Allocation Size Tracking Helper for Zlib/Bzlib/LZMA
+*/
+class Compression_Alloc_Info
+ {
+ public:
+ template<typename T>
+ static void* malloc(void* self, T n, T size)
+ {
+ return static_cast<Compression_Alloc_Info*>(self)->do_malloc(n, size);
+ }
+
+ static void free(void* self, void* ptr)
+ {
+ static_cast<Compression_Alloc_Info*>(self)->do_free(ptr);
+ }
+
+ private:
+ void* do_malloc(size_t n, size_t size);
+ void do_free(void* ptr);
+
+ std::unordered_map<void*, size_t> m_current_allocs;
+ };
+
+/**
+* Wrapper for Zlib/Bzlib/LZMA stream types
+*/
+template<typename Stream, typename ByteType>
+class Zlib_Style_Stream : public Compression_Stream
+ {
+ public:
+ void next_in(byte* b, size_t len) override
+ {
+ m_stream.next_in = reinterpret_cast<ByteType*>(b);
+ m_stream.avail_in = len;
+ }
+
+ void next_out(byte* b, size_t len) override
+ {
+ m_stream.next_out = reinterpret_cast<ByteType*>(b);
+ m_stream.avail_out = len;
+ }
+
+ size_t avail_in() const override { return m_stream.avail_in; }
+
+ size_t avail_out() const override { return m_stream.avail_out; }
+
+ Zlib_Style_Stream()
+ {
+ std::memset(&m_stream, 0, sizeof(stream_t));
+ m_allocs.reset(new Compression_Alloc_Info);
+ }
+
+ ~Zlib_Style_Stream()
+ {
+ std::memset(&m_stream, 0, sizeof(stream_t));
+ m_allocs.reset();
+ }
+
+ protected:
+ typedef Stream stream_t;
+
+ stream_t* streamp() { return &m_stream; }
+
+ Compression_Alloc_Info* alloc() { return m_allocs.get(); }
+ private:
+ stream_t m_stream;
+ std::unique_ptr<Compression_Alloc_Info> m_allocs;
+ };
+
+}
+
+#endif