version 3.0
This commit is contained in:
commit
d837490606
209 changed files with 19662 additions and 0 deletions
0
zlib/Makefile
Normal file
0
zlib/Makefile
Normal file
5
zlib/lib.mk
Normal file
5
zlib/lib.mk
Normal file
|
@ -0,0 +1,5 @@
|
|||
VPATH+= ${TOPDIR}/zlib
|
||||
|
||||
SRCS+= zlib_filter.cc
|
||||
|
||||
LDADD+= -lz
|
163
zlib/zlib_filter.cc
Normal file
163
zlib/zlib_filter.cc
Normal file
|
@ -0,0 +1,163 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// File: zlib_filter.cc //
|
||||
// Description: data filters for zlib inflate/deflate streams //
|
||||
// Project: WANProxy XTech //
|
||||
// Author: Andreu Vidal Bramfeld-Software //
|
||||
// Last modified: 2015-04-01 //
|
||||
// //
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "zlib_filter.h"
|
||||
|
||||
// Deflate
|
||||
|
||||
DeflateFilter::DeflateFilter (int level) : BufferedFilter ("/zlib/deflate")
|
||||
{
|
||||
stream_.zalloc = Z_NULL;
|
||||
stream_.zfree = Z_NULL;
|
||||
stream_.opaque = Z_NULL;
|
||||
stream_.next_in = Z_NULL;
|
||||
stream_.avail_in = 0;
|
||||
stream_.next_out = outbuf;
|
||||
stream_.avail_out = sizeof outbuf;
|
||||
|
||||
if (deflateInit (&stream_, level) != Z_OK)
|
||||
CRITICAL(log_) << "Could not initialize deflate stream.";
|
||||
}
|
||||
|
||||
DeflateFilter::~DeflateFilter ()
|
||||
{
|
||||
if (deflateEnd (&stream_) != Z_OK)
|
||||
ERROR(log_) << "Deflate stream did not end cleanly.";
|
||||
}
|
||||
|
||||
bool DeflateFilter::consume (Buffer& buf)
|
||||
{
|
||||
const BufferSegment* seg;
|
||||
int cnt = 0, i = 0, rv;
|
||||
|
||||
for (Buffer::SegmentIterator it = buf.segments (); ! it.end (); it.next (), ++cnt);
|
||||
pending_.clear ();
|
||||
|
||||
for (Buffer::SegmentIterator it = buf.segments (); ! it.end (); it.next (), ++i)
|
||||
{
|
||||
seg = *it;
|
||||
stream_.next_in = (Bytef*) (uintptr_t) seg->data ();
|
||||
stream_.avail_in = seg->length ();
|
||||
|
||||
while (stream_.avail_in > 0)
|
||||
{
|
||||
rv = deflate (&stream_, (i < cnt - 1 ? Z_NO_FLUSH : Z_SYNC_FLUSH));
|
||||
if (rv == Z_STREAM_ERROR || rv == Z_DATA_ERROR || rv == Z_MEM_ERROR)
|
||||
{
|
||||
ERROR(log_) << "deflate(): " << zError(rv);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (stream_.avail_out < sizeof outbuf)
|
||||
{
|
||||
pending_.append (outbuf, sizeof outbuf - stream_.avail_out);
|
||||
stream_.next_out = outbuf;
|
||||
stream_.avail_out = sizeof outbuf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return produce (pending_);
|
||||
}
|
||||
|
||||
void DeflateFilter::flush (int flg)
|
||||
{
|
||||
pending_.clear ();
|
||||
stream_.next_in = Z_NULL;
|
||||
stream_.avail_in = 0;
|
||||
|
||||
while (deflate (&stream_, Z_FINISH) == Z_OK && stream_.avail_out < sizeof outbuf)
|
||||
{
|
||||
pending_.append (outbuf, sizeof outbuf - stream_.avail_out);
|
||||
stream_.next_out = outbuf;
|
||||
stream_.avail_out = sizeof outbuf;
|
||||
}
|
||||
|
||||
if (! pending_.empty ())
|
||||
produce (pending_);
|
||||
|
||||
Filter::flush (flg);
|
||||
}
|
||||
|
||||
// Inflate
|
||||
|
||||
InflateFilter::InflateFilter () : BufferedFilter ("/zlib/inflate")
|
||||
{
|
||||
stream_.zalloc = Z_NULL;
|
||||
stream_.zfree = Z_NULL;
|
||||
stream_.opaque = Z_NULL;
|
||||
stream_.next_in = Z_NULL;
|
||||
stream_.avail_in = 0;
|
||||
stream_.next_out = outbuf;
|
||||
stream_.avail_out = sizeof outbuf;
|
||||
|
||||
if (inflateInit (&stream_) != Z_OK)
|
||||
CRITICAL(log_) << "Could not initialize inflate stream.";
|
||||
}
|
||||
|
||||
InflateFilter::~InflateFilter()
|
||||
{
|
||||
if (inflateEnd (&stream_) != Z_OK)
|
||||
ERROR(log_) << "Inflate stream did not end cleanly.";
|
||||
}
|
||||
|
||||
bool InflateFilter::consume (Buffer& buf)
|
||||
{
|
||||
const BufferSegment* seg;
|
||||
int cnt = 0, i = 0, rv;
|
||||
|
||||
for (Buffer::SegmentIterator it = buf.segments (); ! it.end (); it.next (), ++cnt);
|
||||
pending_.clear ();
|
||||
|
||||
for (Buffer::SegmentIterator it = buf.segments (); ! it.end (); it.next (), ++i)
|
||||
{
|
||||
seg = *it;
|
||||
stream_.next_in = (Bytef*) (uintptr_t) seg->data ();
|
||||
stream_.avail_in = seg->length ();
|
||||
|
||||
while (stream_.avail_in > 0)
|
||||
{
|
||||
rv = inflate (&stream_, (i < cnt - 1 ? Z_NO_FLUSH : Z_SYNC_FLUSH));
|
||||
if (rv == Z_NEED_DICT || rv == Z_DATA_ERROR || rv == Z_MEM_ERROR)
|
||||
{
|
||||
ERROR(log_) << "inflate(): " << zError(rv);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (stream_.avail_out < sizeof outbuf)
|
||||
{
|
||||
pending_.append (outbuf, sizeof outbuf - stream_.avail_out);
|
||||
stream_.next_out = outbuf;
|
||||
stream_.avail_out = sizeof outbuf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return produce (pending_);
|
||||
}
|
||||
|
||||
void InflateFilter::flush (int flg)
|
||||
{
|
||||
pending_.clear ();
|
||||
stream_.next_in = Z_NULL;
|
||||
stream_.avail_in = 0;
|
||||
|
||||
while (inflate (&stream_, Z_FINISH) == Z_OK && stream_.avail_out < sizeof outbuf)
|
||||
{
|
||||
pending_.append (outbuf, sizeof outbuf - stream_.avail_out);
|
||||
stream_.next_out = outbuf;
|
||||
stream_.avail_out = sizeof outbuf;
|
||||
}
|
||||
|
||||
if (! pending_.empty ())
|
||||
produce (pending_);
|
||||
|
||||
Filter::flush (flg);
|
||||
}
|
48
zlib/zlib_filter.h
Normal file
48
zlib/zlib_filter.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// //
|
||||
// File: zlib_filter.h //
|
||||
// Description: data filters for zlib inflate/deflate streams //
|
||||
// Project: WANProxy XTech //
|
||||
// Author: Andreu Vidal Bramfeld-Software //
|
||||
// Last modified: 2015-04-01 //
|
||||
// //
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef ZLIB_DEFLATE_FILTER_H
|
||||
#define ZLIB_DEFLATE_FILTER_H
|
||||
|
||||
#include <common/filter.h>
|
||||
#include <zlib.h>
|
||||
|
||||
#define DEFLATE_CHUNK_SIZE 0x10000
|
||||
#define INFLATE_CHUNK_SIZE 0x10000
|
||||
|
||||
class DeflateFilter : public BufferedFilter
|
||||
{
|
||||
private:
|
||||
z_stream stream_;
|
||||
uint8_t outbuf[DEFLATE_CHUNK_SIZE];
|
||||
|
||||
public:
|
||||
DeflateFilter (int level = 0);
|
||||
virtual ~DeflateFilter ();
|
||||
|
||||
virtual bool consume (Buffer& buf);
|
||||
virtual void flush (int flg);
|
||||
};
|
||||
|
||||
class InflateFilter : public BufferedFilter
|
||||
{
|
||||
private:
|
||||
z_stream stream_;
|
||||
uint8_t outbuf[INFLATE_CHUNK_SIZE];
|
||||
|
||||
public:
|
||||
InflateFilter ();
|
||||
~InflateFilter ();
|
||||
|
||||
virtual bool consume (Buffer& buf);
|
||||
virtual void flush (int flg);
|
||||
};
|
||||
|
||||
#endif /* !ZLIB_DEFLATE_FILTER_H */
|
Loading…
Add table
Add a link
Reference in a new issue