10 #include <botan/zlib.h>
11 #include <botan/exceptn.h>
36 void* zlib_malloc(
void* info_ptr,
unsigned int n,
unsigned int size)
38 Zlib_Alloc_Info* info =
static_cast<Zlib_Alloc_Info*
>(info_ptr);
39 void* ptr = info->alloc->allocate(n * size);
40 info->current_allocs[ptr] = n * size;
47 void zlib_free(
void* info_ptr,
void* ptr)
49 Zlib_Alloc_Info* info =
static_cast<Zlib_Alloc_Info*
>(info_ptr);
50 std::map<void*, size_t>::const_iterator i = info->current_allocs.find(ptr);
51 if(i == info->current_allocs.end())
53 info->alloc->deallocate(ptr, i->second);
74 std::memset(&stream, 0,
sizeof(z_stream));
75 stream.zalloc = zlib_malloc;
76 stream.zfree = zlib_free;
77 stream.opaque =
new Zlib_Alloc_Info;
85 Zlib_Alloc_Info* info =
static_cast<Zlib_Alloc_Info*
>(stream.opaque);
87 std::memset(&stream, 0,
sizeof(z_stream));
95 level((l >= 9) ? 9 : l), buffer(DEFAULT_BUFFERSIZE)
106 zlib =
new Zlib_Stream;
107 if(deflateInit(&(zlib->stream), level) != Z_OK)
116 zlib->stream.next_in =
static_cast<Bytef*
>(
const_cast<byte*
>(input));
117 zlib->stream.avail_in = length;
119 while(zlib->stream.avail_in != 0)
121 zlib->stream.next_out =
static_cast<Bytef*
>(buffer.
begin());
122 zlib->stream.avail_out = buffer.
size();
123 deflate(&(zlib->stream), Z_NO_FLUSH);
124 send(buffer.
begin(), buffer.
size() - zlib->stream.avail_out);
133 zlib->stream.next_in = 0;
134 zlib->stream.avail_in = 0;
137 while(rc != Z_STREAM_END)
139 zlib->stream.next_out =
reinterpret_cast<Bytef*
>(buffer.
begin());
140 zlib->stream.avail_out = buffer.
size();
142 rc = deflate(&(zlib->stream), Z_FINISH);
143 send(buffer.
begin(), buffer.
size() - zlib->stream.avail_out);
154 zlib->stream.next_in = 0;
155 zlib->stream.avail_in = 0;
159 zlib->stream.avail_out = buffer.
size();
160 zlib->stream.next_out =
reinterpret_cast<Bytef*
>(buffer.
begin());
162 deflate(&(zlib->stream), Z_FULL_FLUSH);
163 send(buffer.
begin(), buffer.
size() - zlib->stream.avail_out);
165 if(zlib->stream.avail_out == buffer.
size())
173 void Zlib_Compression::clear()
179 deflateEnd(&(zlib->stream));
200 zlib =
new Zlib_Stream;
201 if(inflateInit(&(zlib->stream)) != Z_OK)
210 if(length) no_writes =
false;
213 Bytef* input =
reinterpret_cast<Bytef*
>(
const_cast<byte*
>(input_arr));
215 zlib->stream.next_in = input;
216 zlib->stream.avail_in = length;
218 while(zlib->stream.avail_in != 0)
220 zlib->stream.next_out =
reinterpret_cast<Bytef*
>(buffer.
begin());
221 zlib->stream.avail_out = buffer.
size();
223 int rc = inflate(&(zlib->stream), Z_SYNC_FLUSH);
225 if(rc != Z_OK && rc != Z_STREAM_END)
228 if(rc == Z_DATA_ERROR)
230 else if(rc == Z_NEED_DICT)
231 throw Decoding_Error(
"Zlib_Decompression: Need preset dictionary");
232 else if(rc == Z_MEM_ERROR)
235 throw std::runtime_error(
"Zlib decompression: Unknown error");
238 send(buffer.
begin(), buffer.
size() - zlib->stream.avail_out);
240 if(rc == Z_STREAM_END)
242 size_t read_from_block = length - zlib->stream.avail_in;
245 zlib->stream.next_in = input + read_from_block;
246 zlib->stream.avail_in = length - read_from_block;
248 input += read_from_block;
249 length -= read_from_block;
259 if(no_writes)
return;
260 zlib->stream.next_in = 0;
261 zlib->stream.avail_in = 0;
265 while(rc != Z_STREAM_END)
267 zlib->stream.next_out =
reinterpret_cast<Bytef*
>(buffer.
begin());
268 zlib->stream.avail_out = buffer.
size();
269 rc = inflate(&(zlib->stream), Z_SYNC_FLUSH);
271 if(rc != Z_OK && rc != Z_STREAM_END)
277 send(buffer.
begin(), buffer.
size() - zlib->stream.avail_out);
286 void Zlib_Decompression::clear()
294 inflateEnd(&(zlib->stream));
void write(const byte input[], size_t length)
std::invalid_argument Invalid_Argument
static Allocator * get(bool locking)
void send(const byte in[], size_t length)
std::map< void *, size_t > current_allocs
void write(const byte input[], size_t length)
Zlib_Compression(size_t level=6)
void zeroise(MemoryRegion< T > &vec)