天天看点

quicklz 接口函数

翻译自quicklz c手册

///////////////////////////////////////////

压缩函数:

size_t qlz_compress(

const void *source, 

char *destination, 

size_t size, 

qlz_state_compress *state_compress)

source,源字符地址

destination,压缩后的数据存储起始地址,其大小至少为size+400字节

size,需要压缩的源字符串长度,必须在1到2^32-1之间

qlz_state_compress在quicklz.h中定义,用于压缩算法过程中的临时状态存储

注意qlz_state_compress结构体非常庞大,因此不应该在函数局部定义,以防止产生栈溢出。

解压函数:

size_t qlz_decompress(

const char *source, 

void *destination, 

qlz_state_decompress *state_decompress)

解压source并将解压结果写入到destination中。

size_t qlz_size_compressed(const char *source)

获取压缩数据的大小,

int qlz_get_setting(int settings)

用于获取quiklz的设置信息,根据传参来决定

qlz_compression_level

1

sizeof(qlz_state_compress)

2

sizeof(qlz_state_decompress)

3

qlz_streaming_buffer

6

1 if qlz_memomry_safe is defined, otherwise 0

7

qlz_version_major

8

qlz_version_minor

9

qlz_version_revision 

压缩级别:

quicklz里的压缩级别需要在编译时确定,不可通过参数来调整

#define qlz_compression_level 1//#define qlz_compression_level 2

//#define qlz_compression_level 3

默认压缩级别为1,注意解压时,也要以相同的压缩级别来进行解压。

由于lz压缩是基于寻找重复字符串来进行的,如果数据被分割成多个小的packet(小于10-50kb),每个packet被单独压缩,这回降低压缩比。将qlz_streaming_buffer 设置成一个非0值,允许开启streaming模式,这种情况下,quicklz会存储qlz_streaming_mode个字节的历史缓存。历史缓存存储在state结构体中。

#define qlz_streaming_buffer 0

//#define qlz_streaming_buffer 100000

//#define qlz_streaming_buffer 1000000

当使用qlz_streaming_buffer 时,需要注意:

1.packets在解压时的顺序要和压缩时的顺序一样

2.qlz_compress() 或者 qlz_decompress()的state变量在调用时被传递

3.state初始化需要置0

同样的在压缩和解压时,都必须使用相同的qlz_streaming_buffer 

1.5版本的quicklz支持解压重叠,来节约内存。

假设

将压缩数据放在解压缓冲的最右端

← lower addresses

higher addresses →

d + (d >> 3) + 400 – c bytes of space

c bytes of compressed data

↑ destination

这种情况下,数据解压到目标地址,可能重写一部分压缩数据。

例如如下例子:

#include “quicklz.h”int main()

{

char *destination;

char original[] = “test of overlapping decompression.

five, six, seven, eight, nine, fifteen, sixteen, seventeen, fifteen, sixteen, seventeen.“;    int d = strlen(original);

    qlz_state_compress *state_compress = (qlz_state_compress *)malloc(sizeof(qlz_state_compress));

    qlz_state_decompress *state_decompress = (qlz_state_decompress *)malloc(sizeof(qlz_state_decompress));

    char *compressed = (char *)malloc(d + 400);    int c = qlz_compress(original, compressed, d, state_compress);

destination = (char *)malloc(d + (d >> 3) + 400);

memmove(destination + d + (d >> 3) + 400 – c, compressed, c);

qlz_decompress(destination + d + (d >> 3) + 400 – c, destination, state_decompress);

destination[d] = 0;

printf(“%s”, destination);

return 0;

}

state结构需要占有的内存:

sizeof(char *)

32 bits

64 bits

36868

34820

266244

36872

67592

528392

20484