1
/******************************************************************************
2
* swcomprs.cpp - code for class 'ZipCompress'- a driver class that provides
3
* compression utilities. - using zlib
14
/******************************************************************************
15
* ZipCompress Constructor - Initializes data for instance of ZipCompress
19
ZipCompress::ZipCompress() : SWCompress()
21
// fprintf(stderr, "init compress\n");
25
/******************************************************************************
26
* ZipCompress Destructor - Cleans up instance of ZipCompress
29
ZipCompress::~ZipCompress() {
33
/******************************************************************************
34
* ZipCompress::Encode - This function "encodes" the input stream into the
36
* The GetChars() and SendChars() functions are
37
* used to separate this method from the actual
39
* NOTE: must set zlen for parent class to know length of
43
void ZipCompress::Encode(void)
46
ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
47
const Bytef *source, uLong sourceLen));
48
Compresses the source buffer into the destination buffer. sourceLen is
49
the byte length of the source buffer. Upon entry, destLen is the total
50
size of the destination buffer, which must be at least 0.1% larger than
51
sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
53
This function can be used to compress a whole file at once if the
54
input file is mmap'ed.
55
compress returns Z_OK if success, Z_MEM_ERROR if there was not
56
enough memory, Z_BUF_ERROR if there was not enough room in the output
59
direct = 0; // set direction needed by parent [Get|Send]Chars()
63
char *buf = (char *)calloc(1, 1024);
65
unsigned long chunklen;
66
unsigned long len = 0;
67
while((chunklen = GetChars(chunk, 1023))) {
68
memcpy(chunkbuf, chunk, chunklen);
72
else buf = (char *)realloc(buf, len + 1024);
77
zlen = (long) (len*1.001)+15;
78
char *zbuf = new char[zlen+1];
81
//printf("Doing compress\n");
82
if (compress((Bytef*)zbuf, &zlen, (const Bytef*)buf, len)!=Z_OK)
84
printf("ERROR in compression\n");
87
SendChars(zbuf, zlen);
92
fprintf(stderr, "No buffer to compress\n");
99
/******************************************************************************
100
* ZipCompress::Decode - This function "decodes" the input stream into the
102
* The GetChars() and SendChars() functions are
103
* used to separate this method from the actual
107
void ZipCompress::Decode(void)
110
ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
111
const Bytef *source, uLong sourceLen));
112
Decompresses the source buffer into the destination buffer. sourceLen is
113
the byte length of the source buffer. Upon entry, destLen is the total
114
size of the destination buffer, which must be large enough to hold the
115
entire uncompressed data. (The size of the uncompressed data must have
116
been saved previously by the compressor and transmitted to the decompressor
117
by some mechanism outside the scope of this compression library.)
118
Upon exit, destLen is the actual size of the compressed buffer.
119
This function can be used to decompress a whole file at once if the
120
input file is mmap'ed.
122
uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
123
enough memory, Z_BUF_ERROR if there was not enough room in the output
124
buffer, or Z_DATA_ERROR if the input data was corrupted.
129
char *zbuf = (char *)calloc(1, 1024);
130
char *chunkbuf = zbuf;
132
unsigned long zlen = 0;
133
while((chunklen = GetChars(chunk, 1023))) {
134
memcpy(chunkbuf, chunk, chunklen);
138
else zbuf = (char *)realloc(zbuf, zlen + 1024);
139
chunkbuf = zbuf + zlen;
142
//printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
144
unsigned long blen = zlen*20; // trust compression is less than 1000%
145
char *buf = new char[blen];
146
//printf("Doing decompress {%s}\n", zbuf);
147
if (uncompress((Bytef*)buf, &blen, (Bytef*)zbuf, zlen) != Z_OK) {
148
fprintf(stderr, "no room in outbuffer to during decompression. see zipcomp.cpp\n");
150
SendChars(buf, blen);
155
fprintf(stderr, "No buffer to decompress!\n");
157
//printf("Finished decoding\n");