9 static const char rcsid[] __attribute__((used)) =
"$Id: Compress.cpp 1653 2016-02-28 19:54:59Z sella $";
12 #include "CommonMacro.h"
22 #include <sys/types.h>
25 #define SET_BINARY_MODE(file)
30 using namespace sella::util;
32 Compress::Compress(
int level) : c(NULL), un(NULL), clen(0), unlen(0) {
36 Compress::Compress(
const Compress &other) :
38 c(malloc(other.clen)),
39 un(malloc(other.unlen)),
44 memcpy(this->c, other.c, this->clen);
45 memcpy(this->un, other.un, this->unlen);
48 Compress::~Compress() {
54 this->level = other.level;
55 this->clen = other.clen;
56 this->unlen = other.unlen;
59 c = malloc(this->clen);
60 memcpy(this->c, other.c, this->clen);
62 un = malloc(this->unlen);
63 memcpy(this->un, other.un, this->unlen);
68 void Compress::clear(
void) {
76 void Compress::setLevel(
int level) {
80 int Compress::getLevel(
void)
const {
84 bool Compress::deflate(
const void *uncompressed,
size_t len) {
87 unsigned int avail_out;
94 printf(
"deflate: uncompressed(%d): 0x", len);
95 for (
int i = 0; i < len; i++) {
96 printf(
"%.2x", ((
unsigned char*) uncompressed)[i]);
101 if (uncompressed == NULL || len < 1) {
106 strm.zalloc = Z_NULL;
108 strm.opaque = Z_NULL;
110 if ((ret = deflateInit(&strm, level)) != Z_OK) {
114 avail_out = deflateBound(&strm, len);
116 if ((c = malloc(avail_out)) == NULL) {
122 strm.next_in = (Bytef*) uncompressed;
124 strm.next_out = (Bytef*) c;
125 strm.avail_out = avail_out;
127 if ((ret = ::deflate(&strm, Z_FINISH)) == Z_STREAM_ERROR) {
133 if (strm.avail_out == 0) {
139 clen = strm.total_out;
142 printf(
"deflate: compressed(%d): 0x", clen);
143 for (
int i = 0; i < clen; i++) {
144 printf(
"%.2x", ((
unsigned char*) c)[i]);
150 (void) deflateEnd(&strm);
160 bool Compress::inflate(
const void *compressed,
size_t len) {
165 unsigned char buf[CHUNK];
172 printf(
"inflate: compressed(%d): 0x", len);
173 for (
int i = 0; i < len; i++) {
174 printf(
"%.2x", ((
unsigned char*) compressed)[i]);
179 if (compressed == NULL || len < 1) {
184 strm.zalloc = Z_NULL;
186 strm.opaque = Z_NULL;
188 strm.next_in = Z_NULL;
190 if ((ret = inflateInit(&strm)) != Z_OK) {
195 strm.next_in = (Bytef*) compressed;
198 strm.avail_out = CHUNK;
201 ret = ::inflate(&strm, Z_NO_FLUSH);
202 if (ret != Z_OK && ret != Z_STREAM_END) {
209 un = realloc(un, strm.total_out);
210 delta = CHUNK - strm.avail_out;
211 ptr = (Bytef*) un + (strm.total_out - delta);
212 memcpy(ptr, buf, delta);
213 }
while (strm.avail_out == 0);
216 (void) inflateEnd(&strm);
226 const void* Compress::getCompressed(
void)
const {
230 const void* Compress::getUncompressed(
void)
const {
234 size_t Compress::getCompressedLen(
void)
const {
238 size_t Compress::getUncompressedLen(
void)
const {