8#include <botan/internal/compress_utils.h>
10#include <botan/exceptn.h>
11#include <botan/mem_ops.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/int_utils.h>
19 Exception(
fmt(
"Compression API {} failed with return code {}", func_name, rc)), m_type(type), m_rc(rc) {}
21void* Compression_Alloc_Info::do_malloc(
size_t n,
size_t size) {
28 void* ptr = std::calloc(n, size);
39 m_current_allocs[ptr] = n * size;
45void Compression_Alloc_Info::do_free(
void* ptr) {
47 auto i = m_current_allocs.find(ptr);
49 if(i == m_current_allocs.end()) {
50 throw Internal_Error(
"Compression_Alloc_Info::free got pointer not allocated by us");
55 m_current_allocs.erase(i);
63void Stream_Compression::start(
size_t level) {
64 m_stream = make_stream(level);
72 if(buf.size() == offset && flags == m_stream->run_flag()) {
76 if(m_buffer.size() < buf.size() + offset) {
77 m_buffer.resize(buf.size() + offset);
85 if(m_buffer.empty()) {
89 m_stream->next_in(buf.data() + offset, buf.size() - offset);
90 m_stream->next_out(m_buffer.data() + offset, m_buffer.size() - offset);
93 const bool stream_end = m_stream->run(flags);
96 BOTAN_ASSERT(m_stream->avail_in() == 0,
"After stream is done, no input remains to be processed");
97 m_buffer.resize(m_buffer.size() - m_stream->avail_out());
99 }
else if(m_stream->avail_out() == 0) {
100 const size_t added = 8 + m_buffer.size();
101 m_buffer.resize(m_buffer.size() + added);
102 m_stream->next_out(m_buffer.data() + m_buffer.size() - added, added);
103 }
else if(m_stream->avail_in() == 0) {
104 m_buffer.resize(m_buffer.size() - m_stream->avail_out());
109 copy_mem(m_buffer.data(), buf.data(), offset);
115 process(buf, offset, flush ? m_stream->flush_flag() : m_stream->run_flag());
120 process(buf, offset, m_stream->finish_flag());
128void Stream_Decompression::start() {
129 m_stream = make_stream();
136 if(m_buffer.size() < buf.size() + offset) {
137 m_buffer.resize(buf.size() + offset);
140 m_stream->next_in(buf.data() + offset, buf.size() - offset);
141 m_stream->next_out(m_buffer.data() + offset, m_buffer.size() - offset);
144 const bool stream_end = m_stream->run(flags);
147 if(m_stream->avail_in() == 0) {
149 m_buffer.resize(m_buffer.size() - m_stream->avail_out());
155 const size_t read = (buf.size() - offset) - m_stream->avail_in();
157 m_stream->next_in(buf.data() + offset + read, buf.size() - offset - read);
160 if(m_stream->avail_out() == 0) {
161 const size_t added = 8 + m_buffer.size();
162 m_buffer.resize(m_buffer.size() + added);
163 m_stream->next_out(m_buffer.data() + m_buffer.size() - added, added);
164 }
else if(m_stream->avail_in() == 0) {
165 m_buffer.resize(m_buffer.size() - m_stream->avail_out());
170 copy_mem(m_buffer.data(), buf.data(), offset);
175 process(buf, offset, m_stream->run_flag());
179 if(buf.size() != offset || m_stream.get()) {
180 process(buf, offset, m_stream->finish_flag());
#define BOTAN_ASSERT(expr, assertion_made)
Compression_Error(const char *func_name, ErrorType type, int rc)
virtual std::string name() const =0
void update(secure_vector< uint8_t > &buf, size_t offset, bool flush) final
void finish(secure_vector< uint8_t > &buf, size_t offset) final
void update(secure_vector< uint8_t > &buf, size_t offset) final
void finish(secure_vector< uint8_t > &buf, size_t offset) final
std::string fmt(std::string_view format, const T &... args)
void secure_scrub_memory(void *ptr, size_t n)
constexpr std::optional< T > checked_mul(T a, T b)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void copy_mem(T *out, const T *in, size_t n)