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 =
reinterpret_cast<const Bytef*
>(buf);
64 deflate_zstream->next_out =
reinterpret_cast<Bytef*
>(out);
69 deflate_zstream->avail_out =
static_cast<uInt
>(size);
70 int zerr = deflate(deflate_zstream, Z_FINISH);
71 if (zerr != Z_STREAM_END) {
76 if (deflate_zstream->total_out >= size) {
81 *p_size = deflate_zstream->total_out;
90 inflate_zstream->next_in =
reinterpret_cast<const Bytef*
>(
p);
91 inflate_zstream->avail_in =
static_cast<uInt
>(len);
94 inflate_zstream->next_out = blk;
95 inflate_zstream->avail_out =
static_cast<uInt
>(
sizeof(blk));
96 int err = inflate(inflate_zstream, Z_SYNC_FLUSH);
97 if (err != Z_OK && err != Z_STREAM_END) {
98 if (err == Z_MEM_ERROR)
throw std::bad_alloc();
99 string msg =
"inflate failed";
100 if (inflate_zstream->msg) {
102 msg += inflate_zstream->msg;
108 buf.append(
reinterpret_cast<const char*
>(blk),
109 inflate_zstream->next_out - blk);
110 if (err == Z_STREAM_END)
return true;
111 if (inflate_zstream->avail_in == 0)
return false;
117 if (
usual(deflate_zstream)) {
118 if (
usual(deflateReset(deflate_zstream) == Z_OK))
return;
120 delete deflate_zstream;
123 deflate_zstream =
new z_stream;
125 deflate_zstream->zalloc =
reinterpret_cast<alloc_func
>(0);
126 deflate_zstream->zfree =
reinterpret_cast<free_func
>(0);
127 deflate_zstream->opaque =
static_cast<voidpf
>(0);
131 int err = deflateInit2(deflate_zstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
132 -15, 9, compress_strategy);
133 if (
rare(err != Z_OK)) {
134 if (err == Z_MEM_ERROR) {
135 delete deflate_zstream;
137 throw std::bad_alloc();
139 string msg =
"deflateInit2 failed (";
140 if (deflate_zstream->msg) {
141 msg += deflate_zstream->msg;
146 delete deflate_zstream;
154 if (
usual(inflate_zstream)) {
155 if (
usual(inflateReset(inflate_zstream) == Z_OK))
return;
157 delete inflate_zstream;
160 inflate_zstream =
new z_stream;
162 inflate_zstream->zalloc =
reinterpret_cast<alloc_func
>(0);
163 inflate_zstream->zfree =
reinterpret_cast<free_func
>(0);
165 inflate_zstream->next_in = Z_NULL;
166 inflate_zstream->avail_in = 0;
168 int err = inflateInit2(inflate_zstream, -15);
169 if (
rare(err != Z_OK)) {
170 if (err == Z_MEM_ERROR) {
171 delete inflate_zstream;
173 throw std::bad_alloc();
175 string msg =
"inflateInit2 failed (";
176 if (inflate_zstream->msg) {
177 msg += inflate_zstream->msg;
182 delete inflate_zstream;
void lazy_alloc_inflate_zstream()
Allocate the zstream for inflating, if not already allocated.
bool decompress_chunk(const char *p, int len, std::string &buf)
Returns true if this was the final chunk.
void lazy_alloc_deflate_zstream()
Allocate the zstream for deflating, if not already allocated.
const char * compress(const char *buf, size_t *p_size)
DatabaseError indicates some sort of database related error.
class wrapper around zlib
Hierarchy of classes which Xapian can throw as exceptions.
string str(int value)
Convert int to std::string.
Various assertion macros.
Convert types to std::string.
Various handy string-related helpers.