Botan 3.6.1
Crypto and TLS for C&
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
13#include <botan/mem_ops.h>
14#include <memory>
15#include <unordered_map>
16
17namespace Botan {
18
19/*
20* Allocation Size Tracking Helper for Zlib/Bzlib/LZMA
21*/
23 public:
24 template <typename T>
25 static void* malloc(void* self, T n, T size) {
26 return static_cast<Compression_Alloc_Info*>(self)->do_malloc(n, size);
27 }
28
29 static void free(void* self, void* ptr) { static_cast<Compression_Alloc_Info*>(self)->do_free(ptr); }
30
31 private:
32 void* do_malloc(size_t n, size_t size);
33 void do_free(void* ptr);
34
35 std::unordered_map<void*, size_t> m_current_allocs;
36};
37
38/**
39* Wrapper for Zlib/Bzlib/LZMA stream types
40*/
41template <typename Stream, typename ByteType, typename StreamLenType = size_t>
43 public:
44 void next_in(uint8_t* b, size_t len) override {
45 m_stream.next_in = reinterpret_cast<ByteType*>(b);
46 m_stream.avail_in = static_cast<StreamLenType>(len);
47 }
48
49 void next_out(uint8_t* b, size_t len) override {
50 m_stream.next_out = reinterpret_cast<ByteType*>(b);
51 m_stream.avail_out = static_cast<StreamLenType>(len);
52 }
53
54 size_t avail_in() const override { return m_stream.avail_in; }
55
56 size_t avail_out() const override { return m_stream.avail_out; }
57
58 Zlib_Style_Stream() : m_allocs(std::make_unique<Compression_Alloc_Info>()) { clear_mem(&m_stream, 1); }
59
60 Zlib_Style_Stream(const Zlib_Style_Stream& other) = delete;
64
65 ~Zlib_Style_Stream() override { clear_mem(&m_stream, 1); }
66
67 protected:
68 typedef Stream stream_t;
69
70 stream_t* streamp() { return &m_stream; }
71
72 Compression_Alloc_Info* alloc() { return m_allocs.get(); }
73
74 private:
75 stream_t m_stream;
76 std::unique_ptr<Compression_Alloc_Info> m_allocs;
77};
78
79} // namespace Botan
80
81#endif
static void free(void *self, void *ptr)
static void * malloc(void *self, T n, T size)
size_t avail_out() const override
Compression_Alloc_Info * alloc()
Zlib_Style_Stream(Zlib_Style_Stream &&other)=delete
size_t avail_in() const override
void next_out(uint8_t *b, size_t len) override
Zlib_Style_Stream & operator=(Zlib_Style_Stream &&other)=delete
Zlib_Style_Stream & operator=(const Zlib_Style_Stream &other)=delete
void next_in(uint8_t *b, size_t len) override
Zlib_Style_Stream(const Zlib_Style_Stream &other)=delete
int(* final)(unsigned char *, CTX *)
FE_25519 T
Definition ge.cpp:34
const SIMD_8x32 & b
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:120