Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

compress_utils.h
Go to the documentation of this file.
1 /*
2 * Compression utility header
3 * (C) 2014 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #ifndef BOTAN_COMPRESSION_UTILS_H__
9 #define BOTAN_COMPRESSION_UTILS_H__
10
11 #include <botan/compression.h>
12 #include <memory>
13 #include <unordered_map>
14
15 namespace Botan {
16
17 /*
18 * Allocation Size Tracking Helper for Zlib/Bzlib/LZMA
19 */
20 class Compression_Alloc_Info
21 {
22 public:
23 template<typename T>
24 static void* malloc(void* self, T n, T size)
25 {
26 return static_cast<Compression_Alloc_Info*>(self)->do_malloc(n, size);
27 }
28
29 static void free(void* self, void* ptr)
30 {
31 static_cast<Compression_Alloc_Info*>(self)->do_free(ptr);
32 }
33
34 private:
35 void* do_malloc(size_t n, size_t size);
36 void do_free(void* ptr);
37
38 std::unordered_map<void*, size_t> m_current_allocs;
39 };
40
41 /**
42 * Wrapper for Zlib/Bzlib/LZMA stream types
43 */
44 template<typename Stream, typename ByteType>
45 class Zlib_Style_Stream : public Compression_Stream
46 {
47 public:
48 void next_in(uint8_t* b, size_t len) override
49 {
50 m_stream.next_in = reinterpret_cast<ByteType*>(b);
51 m_stream.avail_in = len;
52 }
53
54 void next_out(uint8_t* b, size_t len) override
55 {
56 m_stream.next_out = reinterpret_cast<ByteType*>(b);
57 m_stream.avail_out = len;
58 }
59
60 size_t avail_in() const override { return m_stream.avail_in; }
61
62 size_t avail_out() const override { return m_stream.avail_out; }
63
64 Zlib_Style_Stream()
65 {
66 clear_mem(&m_stream, 1);
67 m_allocs.reset(new Compression_Alloc_Info);
68 }
69
70 ~Zlib_Style_Stream()
71 {
72 clear_mem(&m_stream, 1);
73 m_allocs.reset();
74 }
75
76 protected:
77 typedef Stream stream_t;
78
79 stream_t* streamp() { return &m_stream; }
80
81 Compression_Alloc_Info* alloc() { return m_allocs.get(); }
82 private:
83 stream_t m_stream;
84 std::unique_ptr<Compression_Alloc_Info> m_allocs;
85 };
86
87 }
88
89 #endif
Botan::Compression_Stream
Definition: compression.h:109
Botan::Zlib_Style_Stream::alloc
Compression_Alloc_Info * alloc()
Definition: compress_utils.h:81
Botan::Zlib_Style_Stream::next_in
void next_in(uint8_t *b, size_t len) override
Definition: compress_utils.h:48
Botan::Compression_Alloc_Info::free
static void free(void *self, void *ptr)
Definition: compress_utils.h:29
Botan::clear_mem
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:57
Botan::Compression_Alloc_Info
Definition: compress_utils.h:20
Botan::Zlib_Style_Stream::avail_in
size_t avail_in() const override
Definition: compress_utils.h:60
Botan::Zlib_Style_Stream::next_out
void next_out(uint8_t *b, size_t len) override
Definition: compress_utils.h:54
Botan::Zlib_Style_Stream::Zlib_Style_Stream
Zlib_Style_Stream()
Definition: compress_utils.h:64
Botan::Zlib_Style_Stream::stream_t
Stream stream_t
Definition: compress_utils.h:77
Botan
Definition: alg_id.cpp:13
Botan::Zlib_Style_Stream
Definition: compress_utils.h:45
Botan::Compression_Alloc_Info::malloc
static void * malloc(void *self, T n, T size)
Definition: compress_utils.h:24
Botan::Zlib_Style_Stream::avail_out
size_t avail_out() const override
Definition: compress_utils.h:62
Botan::Zlib_Style_Stream::streamp
stream_t * streamp()
Definition: compress_utils.h:79
Botan::Zlib_Style_Stream::~Zlib_Style_Stream
~Zlib_Style_Stream()
Definition: compress_utils.h:70