acl/lib_acl_cpp/include/acl_cpp/stdlib/zlib_stream.hpp

273 lines
9.8 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "pipe_stream.hpp"
2014-11-19 00:25:21 +08:00
typedef struct z_stream_s z_stream;
namespace acl {
/**
*
*
2014-11-19 00:25:21 +08:00
*/
typedef enum
{
zlib_default = -1, // 缺省的压缩比
zlib_level0 = 0, // 最低的压缩比,其实就是不压缩
zlib_best_speed = 1, // 压缩速度最快的压缩比
2014-11-19 00:25:21 +08:00
zlib_level1 = zlib_best_speed,
zlib_level2 = 2,
zlib_level3 = 3,
zlib_level4 = 4,
zlib_level5 = 5,
zlib_level6 = 6,
zlib_level7 = 7,
zlib_level8 = 8,
zlib_best_compress = 9, // 最高的压缩比,最低的压缩速度
2014-11-19 00:25:21 +08:00
zlib_level9 = zlib_best_compress
} zlib_level_t;
/**
*
* HTTP -zlib_wbits_t
*/
enum
{
zlib_wbits_8 = 8,
zlib_wbits_9 = 9,
zlib_wbits_10 = 10,
zlib_wbits_11 = 11,
zlib_wbits_12 = 12,
zlib_wbits_13 = 13,
zlib_wbits_14 = 14,
zlib_wbits_15 = 15,
};
/**
* 使
*/
typedef enum
{
zlib_mlevel_1 = 1,
zlib_mlevel_2 = 2,
zlib_mlevel_3 = 3,
zlib_mlevel_4 = 4,
zlib_mlevel_5 = 5,
zlib_mlevel_6 = 6,
zlib_mlevel_7 = 7,
zlib_mlevel_8 = 8,
zlib_mlevel_9 = 9,
} zlib_mlevel_t;
2014-11-19 00:25:21 +08:00
/**
*
* zlib_flush_off
2014-11-19 00:25:21 +08:00
*/
typedef enum
{
zlib_flush_off = 0, // 不立即刷新至用户缓存
zlib_flush_partial = 1, // 刷新部分至用户缓存
zlib_flush_sync = 2, // 同步刷新
zlib_flush_full = 3, // 完全刷新
zlib_flush_finish = 4 // 完全刷新并停止压缩或解压过程
2014-11-19 00:25:21 +08:00
} zlib_flush_t;
class string;
class ACL_CPP_API zlib_stream : public pipe_stream
{
public:
zlib_stream();
~zlib_stream();
/**
*
* @param in {const char*}
* @param len {int}
* @param out {string*}
* @param level {zlib_level_t}
*
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool zlib_compress(const char* in, int len, string* out,
zlib_level_t level = zlib_default);
/**
*
* @param in {const char*}
* @param len {int}
* @param out {string*}
* @param have_zlib_header {bool} zlib_header
* HTTP false
* @param wsize {int}
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool zlib_uncompress(const char* in, int len, string* out,
bool have_zlib_header = true, int wsize = 15);
///////////////////////////////////////////////////////////////
//
// 以下为流式压缩和流式解压缩过程
2014-11-19 00:25:21 +08:00
//
///////////////////////////////////////////////////////////////
/**
*
* zip_begin->zip_update->zip_finish
* zip_reset
* @param level {zlib_level_t}
*
* @param wbits {zlib_wbits_t}
* 使 HTTP
* -zlib_wbits_15
* @param mlevel {zlib_mlevel_t}
* 使
* @return {bool}
*
2014-11-19 00:25:21 +08:00
*/
bool zip_begin(zlib_level_t level = zlib_default,
int wbits = zlib_wbits_15,
zlib_mlevel_t mlevel = zlib_mlevel_9);
2014-11-19 00:25:21 +08:00
/**
*
* @param in {const char*}
* @param len {int}
* @param out {string*}
*
*
* zlib_flush_t
*
* @param flag {zlib_flush_t}
* zlib_flush_off:
* zlib
* zlib_flush_partial:
* zlib_flush_sync:
* zlib_flush_full: zlib
* zlib_flush_finish:
*
* zip_finish
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool zip_update(const char* in, int len, string* out,
zlib_flush_t flag = zlib_flush_off);
/**
*
* @param out {string} zlib
*
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool zip_finish(string* out);
/**
*
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool zip_reset();
2015-08-22 18:05:46 +08:00
/**
* 使 crc32
* @param n {unsigned} 0
* @param buf {const void*} 使 NULL
* @param dlen {size_t} buf 使 0
* @return {unsinged}
2015-08-22 18:05:46 +08:00
*/
unsigned crc32_update(unsigned n, const void* buf, size_t dlen);
2014-11-19 00:25:21 +08:00
/**
*
* unzip_begin->unzip_update->unzip_finish
* unzip_reset
* @param have_zlib_header {bool} zlib_header
* HTTP false
* @param wsize {int}
* @return {bool}
*
2014-11-19 00:25:21 +08:00
*/
bool unzip_begin(bool have_zlib_header = true, int wsize = 15);
/**
*
* @param in {const char*}
* @param len {int}
* @param out {string*}
*
*
* zlib_flush_t
*
* @param flag {zlib_flush_t}
* zlib_flush_off:
* zlib
* zlib_flush_partial:
* zlib_flush_sync:
* zlib_flush_full: zlib
* zlib_flush_finish:
*
* zip_finish
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool unzip_update(const char* in, int len, string* out,
zlib_flush_t flag = zlib_flush_off);
/**
*
* @param out {string} zlib
*
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool unzip_finish(string* out);
/**
*
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool unzip_reset();
/**
* zstream
* @return {z_stream*}
*/
z_stream* get_zstream() const
{
return zstream_;
}
2015-08-22 18:05:46 +08:00
/**
* 使
2015-08-22 18:05:46 +08:00
*/
static void set_loadpath(const char* path);
/**
*
* @return {const char*} NULL
2015-08-22 18:05:46 +08:00
*/
static const char* get_loadpath();
2014-11-19 00:25:21 +08:00
///////////////////////////////////////////////////////////////
bool pipe_zip_begin(zlib_level_t level = zlib_default,
zlib_flush_t flag = zlib_flush_off);
bool pipe_unzip_begin(zlib_flush_t flag = zlib_flush_off);
// pipe_stream 虚函数重载
2014-11-19 00:25:21 +08:00
virtual int push_pop(const char* in, size_t len,
string* out, size_t max = 0);
virtual int pop_end(string* out, size_t max = 0);
virtual void clear();
2014-11-19 00:25:21 +08:00
private:
z_stream* zstream_;
bool finished_;
bool is_compress_;
zlib_flush_t flush_;
bool update(int (*func)(z_stream*, int), zlib_flush_t flag,
const char* in, int len, string* out);
bool flush_out(int (*func)(z_stream*, int),
zlib_flush_t flag, string* out);
};
} // namespace acl