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
|
//
// HtZlibCodec.cc
//
// HtZlibCodec: Provide a generic access to the zlib compression routines.
// If zlib is not present, encode and decode are simply
// assignment functions.
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Copyright (c) 1995-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: HtZlibCodec.cc,v 1.4 2004/05/28 13:15:12 lha Exp $
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#include "HtZlibCodec.h"
#include "defaults.h" // For "config"
#if defined(HAVE_LIBZ) && defined(HAVE_ZLIB_H)
#include <zlib.h>
#endif
HtZlibCodec::HtZlibCodec()
{
}
HtZlibCodec::~HtZlibCodec()
{
}
String HtZlibCodec::encode(const String &str) const
{
String s = str;
#if defined(HAVE_LIBZ) && defined(HAVE_ZLIB_H)
HtConfiguration* config= HtConfiguration::config();
static int cf=config->Value("compression_level",0);
if (cf) {
//
// Now compress s into c_s
//
unsigned char c_buffer[16384];
String c_s;
z_stream c_stream; /* compression stream */
c_stream.zalloc=(alloc_func)0;
c_stream.zfree=(free_func)0;
c_stream.opaque=(voidpf)0;
// Get compression factor, default to best
if (cf<-1) cf=-1; else if (cf>9) cf=9;
int err=deflateInit(&c_stream,cf);
if (err!=Z_OK) return 0;
int len=s.length();
c_stream.next_in=(Bytef*)(char *)s;
c_stream.avail_in=len;
while (err==Z_OK && c_stream.total_in!=(uLong)len) {
c_stream.next_out=c_buffer;
c_stream.avail_out=sizeof(c_buffer);
err=deflate(&c_stream,Z_NO_FLUSH);
c_s.append((char *)c_buffer,c_stream.next_out-c_buffer);
}
// Finish the stream
for (;;) {
c_stream.next_out=c_buffer;
c_stream.avail_out=sizeof(c_buffer);
err=deflate(&c_stream,Z_FINISH);
c_s.append((char *)c_buffer,c_stream.next_out-c_buffer);
if (err==Z_STREAM_END) break;
//CHECK_ERR(err, "deflate");
}
err=deflateEnd(&c_stream);
s=c_s;
}
#endif // HAVE_LIBZ && HAVE_ZLIB_H
return s;
}
String HtZlibCodec::decode(const String &str) const
{
String s = str;
#if defined(HAVE_LIBZ) && defined(HAVE_ZLIB_H)
HtConfiguration* config= HtConfiguration::config();
static int cf=config->Value("compression_level",0);
if (cf) {
String c_s;
// Decompress stream
unsigned char c_buffer[16384];
z_stream d_stream;
d_stream.zalloc=(alloc_func)0;
d_stream.zfree=(free_func)0;
d_stream.opaque=(voidpf)0;
unsigned int len=s.length();
d_stream.next_in=(Bytef*)(char *)s;
d_stream.avail_in=len;
int err=inflateInit(&d_stream);
if (err!=Z_OK) return 1;
while (err==Z_OK && d_stream.total_in<len) {
d_stream.next_out=c_buffer;
d_stream.avail_out=sizeof(c_buffer);
err=inflate(&d_stream,Z_NO_FLUSH);
c_s.append((char *)c_buffer,d_stream.next_out-c_buffer);
if (err==Z_STREAM_END) break;
}
err=inflateEnd(&d_stream);
s=c_s;
}
#endif // HAVE_LIBZ && HAVE_ZLIB_H
return s;
}
// Canonical singleton interface.
HtZlibCodec *
HtZlibCodec::instance()
{
static HtZlibCodec *_instance = 0;
if (_instance == 0)
{
_instance = new HtZlibCodec();
}
return _instance;
}
// End of HtZlibCodec.cc
|