Botan  2.7.0
Crypto and TLS for C++11
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 */
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>
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 
65  {
66  clear_mem(&m_stream, 1);
67  m_allocs.reset(new Compression_Alloc_Info);
68  }
69 
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
Compression_Alloc_Info * alloc()
void next_in(uint8_t *b, size_t len) override
static void free(void *self, void *ptr)
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
size_t avail_in() const override
void next_out(uint8_t *b, size_t len) override
Definition: alg_id.cpp:13
static void * malloc(void *self, T n, T size)
size_t avail_out() const override
fe T
Definition: ge.cpp:37