35 if (deflate_zstream) {
38 (void) deflateEnd(deflate_zstream);
39 delete deflate_zstream;
42 if (inflate_zstream) {
45 (void) inflateEnd(inflate_zstream);
46 delete inflate_zstream;
54 lazy_alloc_deflate_zstream();
55 size_t size = *p_size;
56 if (!out || out_len < size) {
62 deflate_zstream->avail_in =
static_cast<uInt
>(size);
63 deflate_zstream->next_in =
64 reinterpret_cast<Bytef*
>(
const_cast<char*
>(buf));
65 deflate_zstream->next_out =
reinterpret_cast<Bytef*
>(out);
70 deflate_zstream->avail_out =
static_cast<uInt
>(size);
71 int zerr = deflate(deflate_zstream, Z_FINISH);
72 if (zerr != Z_STREAM_END) {
77 if (deflate_zstream->total_out >= size) {
82 *p_size = deflate_zstream->total_out;
91 inflate_zstream->next_in =
92 reinterpret_cast<Bytef*
>(
const_cast<char*
>(p));
93 inflate_zstream->avail_in =
static_cast<uInt
>(len);
96 inflate_zstream->next_out = blk;
97 inflate_zstream->avail_out =
static_cast<uInt
>(
sizeof(blk));
98 int err = inflate(inflate_zstream, Z_SYNC_FLUSH);
99 if (err != Z_OK && err != Z_STREAM_END) {
100 if (err == Z_MEM_ERROR)
throw std::bad_alloc();
101 string msg =
"inflate failed";
102 if (inflate_zstream->msg) {
104 msg += inflate_zstream->msg;
110 buf.append(reinterpret_cast<const char*>(blk),
111 inflate_zstream->next_out - blk);
112 if (err == Z_STREAM_END)
return true;
113 if (inflate_zstream->avail_in == 0)
return false;
119 if (
usual(deflate_zstream)) {
120 if (
usual(deflateReset(deflate_zstream) == Z_OK))
return;
122 delete deflate_zstream;
125 deflate_zstream =
new z_stream;
127 deflate_zstream->zalloc =
reinterpret_cast<alloc_func
>(0);
128 deflate_zstream->zfree =
reinterpret_cast<free_func
>(0);
129 deflate_zstream->opaque =
static_cast<voidpf
>(0);
133 int err = deflateInit2(deflate_zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
134 -15, 9, compress_strategy);
135 if (
rare(err != Z_OK)) {
136 if (err == Z_MEM_ERROR) {
137 delete deflate_zstream;
139 throw std::bad_alloc();
141 string msg =
"deflateInit2 failed (";
142 if (deflate_zstream->msg) {
143 msg += deflate_zstream->msg;
148 delete deflate_zstream;
156 if (
usual(inflate_zstream)) {
157 if (
usual(inflateReset(inflate_zstream) == Z_OK))
return;
159 delete inflate_zstream;
162 inflate_zstream =
new z_stream;
164 inflate_zstream->zalloc =
reinterpret_cast<alloc_func
>(0);
165 inflate_zstream->zfree =
reinterpret_cast<free_func
>(0);
167 inflate_zstream->next_in = Z_NULL;
168 inflate_zstream->avail_in = 0;
170 int err = inflateInit2(inflate_zstream, -15);
171 if (
rare(err != Z_OK)) {
172 if (err == Z_MEM_ERROR) {
173 delete inflate_zstream;
175 throw std::bad_alloc();
177 string msg =
"inflateInit2 failed (";
178 if (inflate_zstream->msg) {
179 msg += inflate_zstream->msg;
184 delete inflate_zstream;
void lazy_alloc_inflate_zstream()
Allocate the zstream for inflating, if not already allocated.
const char * compress(const char *buf, size_t *p_size)
Convert types to std::string.
Hierarchy of classes which Xapian can throw as exceptions.
class wrapper around zlib
void lazy_alloc_deflate_zstream()
Allocate the zstream for deflating, if not already allocated.
bool decompress_chunk(const char *p, int len, std::string &buf)
Returns true if this was the final chunk.
string str(int value)
Convert int to std::string.
Various handy helpers which std::string really should provide.
Various assertion macros.
DatabaseError indicates some sort of database related error.