-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressor.cpp
368 lines (336 loc) · 10.9 KB
/
compressor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//
// C++ Implementation: compresser
//
// Description:
//
//
// Author: Jamie Nelson <mordecai4d@gmail.com>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "config.h"
#include "sysdep.h"
#include "zlib.h"
#include "compressor.h"
class MudException;
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif
void basic_mud_log(const char *format, ...);
Compressor compressor;
/* Compress from file source to file dest until EOF on source.
def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
allocated for processing, Z_STREAM_ERROR if an invalid compression
level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
version of the library linked do not match, or Z_ERRNO if there is
an error reading or writing the files. */
unsigned char * Compressor::def(unsigned char *source, size_t &dest) {
int flush;
int ret = Z_OK;
// int have;
read_to = 0;
r_len = 0;
cur_size = 0;
last_size = 0;
// unsigned char *buf_tmp = NULL;
ctr = source;
*out = '\0';
bigs += (r_len = strlen((char *)ctr));
//basic_mud_log("String passed (%d): %s", r_len,source);
deflateReset(&dstrm);
//buf_tmp = new unsigned char[1024];
o_s.clear();
/* compress until end of file */
do {
if (r_len < CHUNK)
read_to = (r_len);
else
read_to = CHUNK;
dstrm.avail_in = read_to;
strlcpy((char*)in, (char *)ctr, read_to);
ctr += read_to;
r_len -= read_to;
flush = (read_to < CHUNK) ? Z_FINISH : Z_NO_FLUSH;
dstrm.next_in = in;
//dlen = deflateBound(&dstrm, read_to);//((cur_size * sizeof(unsigned char)))
//buf_tmp = new unsigned char[dlen];
//dest = (unsigned char*)malloc(dlen);
/* run deflate() on input until output buffer not full, finish
compression if all of source has been read in */
do {
dstrm.avail_out = CHUNK;
dstrm.next_out = out;
ret = deflate(&dstrm, flush); /* no bad return value */
//basic_mud_log(zerr(ret));
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
//have = CHUNK - dstrm.avail_out;
last_size = cur_size;
cur_size += dstrm.total_out;
//basic_mud_log("Avail In: %d In size: %d Out Size: %d Ret: %s",dstrm.avail_in, read_to, cur_size * sizeof(unsigned char), zerr(ret));
/* if (buf_tmp == NULL)
else
buf_tmp = (unsigned char*)realloc(buf_tmp, (cur_size * sizeof(unsigned char)));
if (buf_tmp == NULL) {
basic_mud_log("Memory Allocation error in Compression");
break;
}*/
//strncpy((char *)buf_tmp + (last_size-1), (char *)out, dstrm.total_out+1);
o_s.append((const char*)out);
/*
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
}*/
} while (dstrm.avail_out == 0);
if (dstrm.avail_in != 0) {
//deflateReset(&dstrm);
//basic_mud_log("Cur_size: %d os.size(): %d", cur_size, o_s.size());
throw MudException("Can't parse the compression 1.");
}
// assert(dstrm.avail_in == 0); /* all input will be used */
/* done when last data in file processed */
} while (flush != Z_FINISH);
// basic_mud_log(zerr(ret));
assert(ret == Z_STREAM_END); /* stream will be complete */
// basic_mud_log("Cur_size: %d os.size(): %d", cur_size, o_s.size());
if (cur_size != o_s.size()) {
// deflateReset(&dstrm);
basic_mud_log(zerr(ret));
throw MudException("Can't parse the compression 2.");
}
//tmp = (unsigned char*)malloc(cur_size+1);
//strncpy((char *)tmp, (char *)o_s.c_str(), cur_size+1);
//tmp[cur_size+1] = 0;
//deflateReset(&dstrm);
smalls += (dest = o_s.size());//o_s.size();
tmp = (unsigned char*)strdup(o_s.c_str());
o_s.clear();
return tmp;
}
/* Decompress from file source to file dest until stream ends or EOF.
inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
allocated for processing, Z_DATA_ERROR if the deflate data is
invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
the version of the library linked do not match, or Z_ERRNO if there
is an error reading or writing the files. */
unsigned char * Compressor::inf(unsigned char *source, size_t &dest) {
int ret = Z_OK;
read_to = 0;
r_len = 0;
//int have;
cur_size = 0;
last_size = 0;
if (!source)
return NULL;
ctr = source;
r_len = dest;
o_s.clear();
tmp = NULL;
*out = '\0';
/* decompress until deflate stream ends or end of file */
do {
if (r_len < CHUNK)
read_to = (r_len);
else
read_to = CHUNK;
memcpy((void *)in, (void *)ctr, read_to);
istrm.avail_in = read_to;
ctr += read_to;
r_len -= read_to;
if (istrm.avail_in == 0)
break;
istrm.next_in = in;
/* run inflate() on input until output buffer not full */
do {
istrm.avail_out = CHUNK;
istrm.next_out = out;
ret = inflate(&istrm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateReset(&istrm);
return NULL;
}
//have = CHUNK - istrm.avail_out;
last_size = cur_size;
cur_size += istrm.total_out;
//tmp = (unsigned char*)realloc(tmp, cur_size * sizeof(unsigned char));
//strncpy((char *)tmp + (last_size), (char *)out, istrm.total_out+1);
out[cur_size+1] = '\0';
o_s.append((char *)out);
/*if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}*/
} while (istrm.avail_out == 0);
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);
/* clean up and return */
(void)inflateReset(&istrm);
//tmp[cur_size+1] = '\0';
tmp = (unsigned char*)strdup(o_s.c_str());
o_s.clear();
return tmp;
}
/* report a zlib or i/o error */
const char * Compressor::zerr(int ret) {
switch (ret) {
case Z_ERRNO:
return "zpipe: error writing stdout";
break;
case Z_STREAM_ERROR:
return "zpipe: invalid compression level";
break;
case Z_DATA_ERROR:
return "zpipe: invalid or incomplete deflate data";
break;
case Z_MEM_ERROR:
return "zpipe: out of memory";
break;
case Z_VERSION_ERROR:
return "zpipe: zlib version mismatch!";
}
return "zpipe: no error.";
}
long Compressor::CompressToId(const char *in) {
long newId;
if (!in)
return -1;
compress_map_data *cmps = new compress_map_data();
if (strlen(in) < 80) {
cmps->parsed = false;
cmps->saved = (unsigned char *)strdup(in);
} else {
try {
cmps->parsed = true;
cmps->saved = def((unsigned char*)in, cmps->length);
} catch (MudException &e) {
cmps->parsed = false;
cmps->saved = (unsigned char*)strdup(in);
}
}
newId = TopID++;
zmap[newId] = cmps;
return newId;
}
long Compressor::CompressToId(const char *in, bool comp) {
long newId;
if (!in)
return -1;
compress_map_data *cmps = new compress_map_data();
if (strlen(in) < 80 || !comp) {
cmps->parsed = false;
cmps->saved = (unsigned char *)strdup(in);
} else {
try {
cmps->parsed = true;
cmps->saved = def((unsigned char*)in, cmps->length);
} catch (MudException &e) {
cmps->parsed = false;
cmps->saved = (unsigned char*)strdup(in);
}
}
newId = TopID++;
zmap[newId] = cmps;
return newId;
}
long Compressor::CompressToId(const char *_in, long i) {
map<long, compress_map_data *>::iterator it;
if (!_in)
return -1;
compress_map_data *cmps = new compress_map_data();
if (strlen(_in) < 80) {
cmps->parsed = false;
cmps->saved = (unsigned char*)strdup(_in);
} else {
try {
cmps->parsed = true;
cmps->saved = def((unsigned char*)_in, cmps->length);
} catch (MudException &e) {
cmps->parsed = false;
cmps->saved = (unsigned char*)strdup(_in);
}
}
it = zmap.find(i);
if (it != zmap.end())
delete zmap[i];
if (i >= TopID)
TopID = i+1;
zmap[i] = cmps;
return i;
}
long Compressor::CompressToId(const char *_in, long i, bool comp) {
map<long, compress_map_data *>::iterator it;
if (!_in)
return -1;
compress_map_data *cmps = new compress_map_data();
if (strlen(_in) < 80 || !comp) {
cmps->parsed = false;
cmps->saved = (unsigned char*)strdup(_in);
} else {
try {
cmps->parsed = true;
cmps->saved = def((unsigned char*)_in, cmps->length);
} catch (MudException &e) {
cmps->parsed = false;
cmps->saved = (unsigned char*)strdup(_in);
}
}
it = zmap.find(i);
if (it != zmap.end())
delete zmap[i];
if (i >= TopID)
TopID = i+1;
zmap[i] = cmps;
return i;
}
char *Compressor::InflateFromId(long i) {
map<long, compress_map_data *>::iterator it;
it = zmap.find(i);
if (it == zmap.end())
return NULL;
else {
compress_map_data *cmps = it->second;
cmps->last_accessed = time(0);
if (cmps->parsed == false)
return (char*)cmps->saved;
else if (cmps->inflated) {
return (char*)cmps->inflated;
} else {
cmps->inflated = inf(cmps->saved, cmps->length);
return (char*)cmps->inflated;
}
}
}
bool Compressor::DeleteId(long i) {
map<long, compress_map_data *>::iterator it;
it = zmap.find(i);
if (it == zmap.end())
return false;
delete zmap[i];
zmap.erase(i);
return true;
}
int Compressor::cleanInflated() {
map<long, compress_map_data *>::iterator it;
time_t tnow = time(0);
int count = 0;
for (it = zmap.begin();it != zmap.end();it++)
if (it->second->inflated && it->second->last_accessed < (tnow - (60*60))) {
free(it->second->inflated);
it->second->inflated = NULL;
count++;
}
return count;
}