1
// This file may be redistributed and modified only under the terms of
2
// the GNU Lesser General Public License (See COPYING for details).
3
// Copyright (C) 2000 Dmitry Derevyanko
9
#if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
11
#include <Atlas/Filters/Gzip.h>
15
#define ASSERT(exp) assert(exp)
18
using Atlas::Filters::Gzip;
20
const int DEFAULT_LEVEL = 6;
24
incoming.next_in = Z_NULL;
25
incoming.avail_in = 0;
26
incoming.zalloc = Z_NULL;
27
incoming.zfree = Z_NULL;
29
outgoing.zalloc = Z_NULL;
30
outgoing.zfree = Z_NULL;
32
inflateInit(&incoming);
33
deflateInit(&outgoing, DEFAULT_LEVEL);
38
inflateEnd(&incoming);
39
deflateEnd(&outgoing);
42
std::string Gzip::encode(const std::string& data)
44
std::string out_string;
49
outgoing.next_in = (unsigned char *)data.data();
50
outgoing.avail_in = data.size();
54
outgoing.next_out = buf;
55
outgoing.avail_out = sizeof(buf);
57
status = deflate(&outgoing, Z_SYNC_FLUSH);
59
ASSERT(status == Z_OK);
61
out_string.append((char*)buf, sizeof(buf) - outgoing.avail_out);
63
} while (outgoing.avail_out == 0);
68
std::string Gzip::decode(const std::string& data)
70
std::string out_string;
75
incoming.next_in = (unsigned char*)data.data();
76
incoming.avail_in = data.size();
80
incoming.next_out = buf;
81
incoming.avail_out = sizeof(buf);
83
status = inflate(&incoming, Z_SYNC_FLUSH);
85
ASSERT(status == Z_OK);
87
out_string.append((char*)buf, sizeof(buf) - incoming.avail_out);
89
} while(incoming.avail_out == 0);
94
#endif // HAVE_ZLIB_H && HAVE_LIBZ