10 #include <botan/bzip2.h>
11 #include <botan/exceptn.h>
37 void* bzip_malloc(
void* info_ptr,
int n,
int size)
39 Bzip_Alloc_Info* info =
static_cast<Bzip_Alloc_Info*
>(info_ptr);
40 void* ptr = info->alloc->allocate(n * size);
41 info->current_allocs[ptr] = n * size;
48 void bzip_free(
void* info_ptr,
void* ptr)
50 Bzip_Alloc_Info* info =
static_cast<Bzip_Alloc_Info*
>(info_ptr);
51 std::map<void*, size_t>::const_iterator i = info->current_allocs.find(ptr);
52 if(i == info->current_allocs.end())
54 info->alloc->deallocate(ptr, i->second);
75 std::memset(&stream, 0,
sizeof(bz_stream));
76 stream.bzalloc = bzip_malloc;
77 stream.bzfree = bzip_free;
78 stream.opaque =
new Bzip_Alloc_Info;
86 Bzip_Alloc_Info* info =
static_cast<Bzip_Alloc_Info*
>(stream.opaque);
88 std::memset(&stream, 0,
sizeof(bz_stream));
96 level((l >= 9) ? 9 : l), buffer(DEFAULT_BUFFERSIZE)
107 bz =
new Bzip_Stream;
108 if(BZ2_bzCompressInit(&(bz->stream), level, 0, 0) != BZ_OK)
117 bz->stream.next_in =
reinterpret_cast<char*
>(
const_cast<byte*
>(input));
118 bz->stream.avail_in = length;
120 while(bz->stream.avail_in != 0)
122 bz->stream.next_out =
reinterpret_cast<char*
>(buffer.
begin());
123 bz->stream.avail_out = buffer.
size();
124 BZ2_bzCompress(&(bz->stream), BZ_RUN);
125 send(buffer, buffer.
size() - bz->stream.avail_out);
134 bz->stream.next_in = 0;
135 bz->stream.avail_in = 0;
138 while(rc != BZ_STREAM_END)
140 bz->stream.next_out =
reinterpret_cast<char*
>(buffer.
begin());
141 bz->stream.avail_out = buffer.
size();
142 rc = BZ2_bzCompress(&(bz->stream), BZ_FINISH);
143 send(buffer, buffer.
size() - bz->stream.avail_out);
153 bz->stream.next_in = 0;
154 bz->stream.avail_in = 0;
157 while(rc != BZ_RUN_OK)
159 bz->stream.next_out =
reinterpret_cast<char*
>(buffer.
begin());
160 bz->stream.avail_out = buffer.
size();
161 rc = BZ2_bzCompress(&(bz->stream), BZ_FLUSH);
162 send(buffer, buffer.
size() - bz->stream.avail_out);
169 void Bzip_Compression::clear()
175 BZ2_bzCompressEnd(&(bz->stream));
185 small_mem(s), buffer(DEFAULT_BUFFERSIZE)
196 if(length) no_writes =
false;
198 char* input =
reinterpret_cast<char*
>(
const_cast<byte*
>(input_arr));
200 bz->stream.next_in = input;
201 bz->stream.avail_in = length;
203 while(bz->stream.avail_in != 0)
205 bz->stream.next_out =
reinterpret_cast<char*
>(buffer.
begin());
206 bz->stream.avail_out = buffer.
size();
208 int rc = BZ2_bzDecompress(&(bz->stream));
210 if(rc != BZ_OK && rc != BZ_STREAM_END)
214 if(rc == BZ_DATA_ERROR)
216 else if(rc == BZ_DATA_ERROR_MAGIC)
218 else if(rc == BZ_MEM_ERROR)
221 throw std::runtime_error(
"Bzip2 decompression: Unknown error");
224 send(buffer, buffer.
size() - bz->stream.avail_out);
226 if(rc == BZ_STREAM_END)
228 size_t read_from_block = length - bz->stream.avail_in;
230 bz->stream.next_in = input + read_from_block;
231 bz->stream.avail_in = length - read_from_block;
232 input += read_from_block;
233 length -= read_from_block;
244 bz =
new Bzip_Stream;
246 if(BZ2_bzDecompressInit(&(bz->stream), 0, small_mem) != BZ_OK)
257 if(no_writes)
return;
258 bz->stream.next_in = 0;
259 bz->stream.avail_in = 0;
262 while(rc != BZ_STREAM_END)
264 bz->stream.next_out =
reinterpret_cast<char*
>(buffer.
begin());
265 bz->stream.avail_out = buffer.
size();
266 rc = BZ2_bzDecompress(&(bz->stream));
268 if(rc != BZ_OK && rc != BZ_STREAM_END)
274 send(buffer, buffer.
size() - bz->stream.avail_out);
283 void Bzip_Decompression::clear()
289 BZ2_bzDecompressEnd(&(bz->stream));
std::invalid_argument Invalid_Argument
static Allocator * get(bool locking)
void send(const byte in[], size_t length)
void write(const byte input[], size_t length)
void write(const byte input[], size_t length)
std::map< void *, size_t > current_allocs
Bzip_Compression(size_t=9)
Bzip_Decompression(bool=false)
void zeroise(MemoryRegion< T > &vec)