The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ZipCompress Class Reference

#include <zipcomprs.h>

+ Inheritance diagram for ZipCompress:
+ Collaboration diagram for ZipCompress:

Public Member Functions

virtual void decode (void)
 
virtual void encode (void)
 
virtual unsigned long getChars (char *buf, unsigned long len)
 
virtual char * getCompressedBuf (unsigned long *len=0)
 
virtual int getLevel ()
 
virtual char * getUncompressedBuf (unsigned long *len=0)
 
virtual unsigned long sendChars (char *buf, unsigned long len)
 
virtual void setCompressedBuf (unsigned long *len, char *buf=0)
 
virtual void setLevel (int l)
 
virtual void setUncompressedBuf (const char *buf=0, unsigned long *len=0)
 
 ZipCompress ()
 
virtual ~ZipCompress ()
 

Static Public Member Functions

static char unTarGZ (int fd, const char *destPath)
 

Protected Attributes

char * buf
 
char direct
 
int level
 
unsigned long pos
 
unsigned long slen
 
char * zbuf
 
unsigned long zlen
 
unsigned long zpos
 

Detailed Description

Definition at line 35 of file zipcomprs.h.

Constructor & Destructor Documentation

SWORD_NAMESPACE_START ZipCompress::ZipCompress ( )

Definition at line 226 of file zipcomprs.cpp.

226  : SWCompress()
227 {
228 // fprintf(stderr, "init compress\n");
230 }
int level
Definition: swcomprs.h:40
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:148
ZipCompress::~ZipCompress ( )
virtual

Definition at line 237 of file zipcomprs.cpp.

237  {
238 }

Member Function Documentation

void ZipCompress::decode ( void  )
virtual

Reimplemented from SWCompress.

Definition at line 314 of file zipcomprs.cpp.

315 {
316 /*
317 ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
318  const Bytef *source, uLong sourceLen));
319  Decompresses the source buffer into the destination buffer. sourceLen is
320  the byte length of the source buffer. Upon entry, destLen is the total
321  size of the destination buffer, which must be large enough to hold the
322  entire uncompressed data. (The size of the uncompressed data must have
323  been saved previously by the compressor and transmitted to the decompressor
324  by some mechanism outside the scope of this compression library.)
325  Upon exit, destLen is the actual size of the compressed buffer.
326  This function can be used to decompress a whole file at once if the
327  input file is mmap'ed.
328 
329  uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
330  enough memory, Z_BUF_ERROR if there was not enough room in the output
331  buffer, or Z_DATA_ERROR if the input data was corrupted.
332 */
333  direct = 1; // set direction needed by parent [get|send]Chars()
334 
335  // get buffer
336  char chunk[1024];
337  char *zbuf = (char *)calloc(1, 1024);
338  char *chunkbuf = zbuf;
339  int chunklen;
340  unsigned long zlen = 0;
341  while((chunklen = (int)getChars(chunk, 1023))) {
342  memcpy(chunkbuf, chunk, chunklen);
343  zlen += chunklen;
344  if (chunklen < 1023)
345  break;
346  else zbuf = (char *)realloc(zbuf, zlen + 1024);
347  chunkbuf = zbuf + zlen;
348  }
349 
350  //printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
351  if (zlen) {
352  unsigned long blen = zlen*20; // trust compression is less than 1000%
353  char *buf = new char[blen];
354  //printf("Doing decompress {%s}\n", zbuf);
355  slen = 0;
356  switch (uncompress((Bytef*)buf, &blen, (Bytef*)zbuf, zlen)){
357  case Z_OK: sendChars(buf, blen); slen = blen; break;
358  case Z_MEM_ERROR: SWLog::getSystemLog()->logError("ERROR: not enough memory during decompression."); break;
359  case Z_BUF_ERROR: SWLog::getSystemLog()->logError("ERROR: not enough room in the out buffer during decompression."); break;
360  case Z_DATA_ERROR: SWLog::getSystemLog()->logError("ERROR: corrupt data during decompression."); break;
361  default: SWLog::getSystemLog()->logError("ERROR: an unknown error occurred during decompression."); break;
362  }
363  delete [] buf;
364  }
365  else {
366  SWLog::getSystemLog()->logError("ERROR: no buffer to decompress!");
367  }
368  //printf("Finished decoding\n");
369  free (zbuf);
370 }
static SWLog * getSystemLog()
Definition: swlog.cpp:53
unsigned long zlen
Definition: swcomprs.h:39
unsigned long slen
Definition: swcomprs.h:39
unsigned char FAR Bytef
Definition: zconf.h:223
free(preg->fastmap)
char * realloc()
#define Z_DATA_ERROR
Definition: zlib.h:137
char * buf
Definition: swcomprs.h:38
#define Z_MEM_ERROR
Definition: zlib.h:138
virtual unsigned long getChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:121
char direct
Definition: swcomprs.h:38
char * zbuf
Definition: swcomprs.h:38
void logError(const char *fmt,...) const
Definition: swlog.cpp:87
#define Z_BUF_ERROR
Definition: zlib.h:139
#define Z_OK
Definition: zlib.h:132
virtual unsigned long sendChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:141
void ZipCompress::encode ( void  )
virtual

Reimplemented from SWCompress.

Definition at line 251 of file zipcomprs.cpp.

252 {
253 /*
254 ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
255  const Bytef *source, uLong sourceLen,
256  int level));
257 
258  Compresses the source buffer into the destination buffer. The level
259  parameter has the same meaning as in deflateInit. sourceLen is the byte
260  length of the source buffer. Upon entry, destLen is the total size of the
261  destination buffer, which must be at least the value returned by
262  compressBound(sourceLen). Upon exit, destLen is the actual size of the
263  compressed buffer.
264 
265  compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
266  memory, Z_BUF_ERROR if there was not enough room in the output buffer,
267  Z_STREAM_ERROR if the level parameter is invalid.
268 */
269  direct = 0; // set direction needed by parent [get|send]Chars()
270 
271  // get buffer
272  char chunk[1024];
273  char *buf = (char *)calloc(1, 1024);
274  char *chunkbuf = buf;
275  unsigned long chunklen;
276  unsigned long len = 0;
277  while((chunklen = getChars(chunk, 1023))) {
278  memcpy(chunkbuf, chunk, chunklen);
279  len += chunklen;
280  if (chunklen < 1023)
281  break;
282  else buf = (char *)realloc(buf, len + 1024);
283  chunkbuf = buf+len;
284  }
285 
286 
287  zlen = (long) (len*1.001)+15;
288  char *zbuf = new char[zlen+1];
289  if (len) {
290  //printf("Doing compress\n");
291  if (compress2((Bytef*)zbuf, &zlen, (const Bytef*)buf, len, level) != Z_OK) {
292  SWLog::getSystemLog()->logError("ERROR in compression");
293  }
294  else {
295  sendChars(zbuf, zlen);
296  }
297  }
298  else {
299  SWLog::getSystemLog()->logError("ERROR: no buffer to compress");
300  }
301  delete [] zbuf;
302  free(buf);
303 }
static SWLog * getSystemLog()
Definition: swlog.cpp:53
unsigned long zlen
Definition: swcomprs.h:39
unsigned char FAR Bytef
Definition: zconf.h:223
int level
Definition: swcomprs.h:40
free(preg->fastmap)
char * realloc()
char * buf
Definition: swcomprs.h:38
virtual unsigned long getChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:121
char direct
Definition: swcomprs.h:38
char * zbuf
Definition: swcomprs.h:38
void logError(const char *fmt,...) const
Definition: swlog.cpp:87
#define Z_OK
Definition: zlib.h:132
virtual unsigned long sendChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:141
unsigned long SWCompress::getChars ( char *  buf,
unsigned long  len 
)
virtualinherited

Definition at line 121 of file swcomprs.cpp.

121  {
122  if (direct) {
123  len = (((zlen - zpos) > (unsigned)len) ? len : zlen - zpos);
124  if (len > 0) {
125  memmove(ibuf, &zbuf[zpos], len);
126  zpos += len;
127  }
128  }
129  else {
130 // slen = strlen(buf);
131  len = (((slen - pos) > (unsigned)len) ? len : slen - pos);
132  if (len > 0) {
133  memmove(ibuf, &buf[pos], len);
134  pos += len;
135  }
136  }
137  return len;
138 }
unsigned long zlen
Definition: swcomprs.h:39
unsigned long slen
Definition: swcomprs.h:39
char * buf
Definition: swcomprs.h:38
unsigned long zpos
Definition: swcomprs.h:39
char direct
Definition: swcomprs.h:38
unsigned long pos
Definition: swcomprs.h:39
char * zbuf
Definition: swcomprs.h:38
char * SWCompress::getCompressedBuf ( unsigned long *  len = 0)
virtualinherited

Definition at line 111 of file swcomprs.cpp.

111  {
112  if (!zbuf) {
113  direct = 0;
114  encode();
115  }
116  if (len) *len = zlen;
117  return zbuf;
118 }
unsigned long zlen
Definition: swcomprs.h:39
virtual void encode(void)
Definition: swcomprs.cpp:180
char direct
Definition: swcomprs.h:38
char * zbuf
Definition: swcomprs.h:38
virtual int SWCompress::getLevel ( )
inlinevirtualinherited

Definition at line 54 of file swcomprs.h.

54 {return level;};
int level
Definition: swcomprs.h:40
char * SWCompress::getUncompressedBuf ( unsigned long *  len = 0)
virtualinherited

Definition at line 90 of file swcomprs.cpp.

90  {
91  if (!buf) {
92  buf = (char *)calloc(1,1); // be sure we at least allocate an empty buf for return;
93  direct = 1;
94  decode();
95  }
96  if (len) *len = slen;
97  return buf;
98 }
virtual void decode(void)
Definition: swcomprs.cpp:193
unsigned long slen
Definition: swcomprs.h:39
char * buf
Definition: swcomprs.h:38
char direct
Definition: swcomprs.h:38
unsigned long SWCompress::sendChars ( char *  buf,
unsigned long  len 
)
virtualinherited

Definition at line 141 of file swcomprs.cpp.

141  {
142  if (direct) {
143  if (buf) {
144 // slen = strlen(buf);
145  if ((pos + len) > (unsigned)slen) {
146  buf = (char *) realloc(buf, pos + len + 1024);
147  memset(&buf[pos], 0, len + 1024);
148  }
149  }
150  else buf = (char *)calloc(1, len + 1024);
151  memmove(&buf[pos], ibuf, len);
152  pos += len;
153  }
154  else {
155  if (zbuf) {
156  if ((zpos + len) > zlen) {
157  zbuf = (char *) realloc(zbuf, zpos + len + 1024);
158  zlen = zpos + len + 1024;
159  }
160  }
161  else {
162  zbuf = (char *)calloc(1, len + 1024);
163  zlen = len + 1024;
164  }
165  memmove(&zbuf[zpos], ibuf, len);
166  zpos += len;
167  }
168  return len;
169 }
unsigned long zlen
Definition: swcomprs.h:39
unsigned long slen
Definition: swcomprs.h:39
char * realloc()
char * buf
Definition: swcomprs.h:38
unsigned long zpos
Definition: swcomprs.h:39
char direct
Definition: swcomprs.h:38
unsigned long pos
Definition: swcomprs.h:39
char * zbuf
Definition: swcomprs.h:38
void SWCompress::setCompressedBuf ( unsigned long *  len,
char *  buf = 0 
)
virtualinherited

Definition at line 101 of file swcomprs.cpp.

101  {
102  if (ibuf) {
103  init();
104  zbuf = (char *) malloc(*len);
105  memcpy(zbuf, ibuf, *len);
106  zlen = *len;
107  }
108  *len = zlen;
109 }
unsigned long zlen
Definition: swcomprs.h:39
char * malloc()
void init()
Definition: swcomprs.cpp:57
char * zbuf
Definition: swcomprs.h:38
virtual void SWCompress::setLevel ( int  l)
inlinevirtualinherited

Reimplemented in XzCompress.

Definition at line 53 of file swcomprs.h.

53 {level = l;};
int level
Definition: swcomprs.h:40
void SWCompress::setUncompressedBuf ( const char *  buf = 0,
unsigned long *  len = 0 
)
virtualinherited

Definition at line 75 of file swcomprs.cpp.

75  {
76  if (ibuf) {
77  init();
78  slen = (len) ? *len : strlen(ibuf);
79  buf = (char *) calloc(slen + 1, 1);
80  memcpy(buf, ibuf, slen);
81  }
82  if (!buf) {
83  buf = (char *)calloc(1,1); // be sure we at least allocate an empty buf for return;
84  direct = 1;
85  decode();
86  if (len) *len = slen;
87  }
88 }
virtual void decode(void)
Definition: swcomprs.cpp:193
unsigned long slen
Definition: swcomprs.h:39
char * buf
Definition: swcomprs.h:38
void init()
Definition: swcomprs.cpp:57
char direct
Definition: swcomprs.h:38
char ZipCompress::unTarGZ ( int  fd,
const char *  destPath 
)
static

Definition at line 373 of file zipcomprs.cpp.

373  {
374  gzFile f;
375 
376  f = gzdopen(fd, "rb");
377  if (f == NULL) {
378  SWLog::getSystemLog()->logError("Couldn't gzopen file");
379  return 1;
380  }
381 
382  return untar(f, destPath);
383 }
static SWLog * getSystemLog()
Definition: swlog.cpp:53
voidp gzFile
Definition: zlib.h:657
return NULL
Definition: regex.c:7953
void logError(const char *fmt,...) const
Definition: swlog.cpp:87

Member Data Documentation

char* SWCompress::buf
mutableprotectedinherited

Definition at line 38 of file swcomprs.h.

char SWCompress::direct
mutableprotectedinherited

Definition at line 38 of file swcomprs.h.

int SWCompress::level
protectedinherited

Definition at line 40 of file swcomprs.h.

unsigned long SWCompress::pos
protectedinherited

Definition at line 39 of file swcomprs.h.

unsigned long SWCompress::slen
protectedinherited

Definition at line 39 of file swcomprs.h.

char * SWCompress::zbuf
mutableprotectedinherited

Definition at line 38 of file swcomprs.h.

unsigned long SWCompress::zlen
protectedinherited

Definition at line 39 of file swcomprs.h.

unsigned long SWCompress::zpos
protectedinherited

Definition at line 39 of file swcomprs.h.


The documentation for this class was generated from the following files: