zlib基本使用

不關心算法細節,直接用,拿來主義,我們可以直接使用zlib的壓縮解壓的接口的,但是爲了使用我們自己的內存管理器,所以用自己的接口了。


voidpf myalloc(voidpf opaque,unsigned num, unsigned size)//內存分配
{
	return (voidpf) mymemallocator(num*size);
}
void myfree(voidpf opaque, voidpf ptr)//內存回收
{
	mymemfree(ptr);
}
int compress(Bytef* dest, uLongf *destLen, const Bytef *source, unsigned long sourceLen, int level)
{
	z_stream stream;
	int errno;
	stream.next_in = (Bytef*)source;
	stream.avail_in = (uInt)sourceLen;
	stream.next_out = dest;
	stream.avail_out = (uInt)*destLen;
	if((uLong)stream.avail_out != *destLen || (uLong)stream.next_in != sourceLen)return Z_BUF_ERROR;
		
	stream.zalloc = (alloc_func)myalloc;
	stream.zfree =(free_func)myfree;
	stream.opaque =(voidpf)0;
	
	errno =deflateInit(&stream,level);
	if(errno != Z_OK) return errno;
	
	errno = deflate(&stream,Z_FINISH);
	if(errno != Z_STREAM_END)
	{
		deflateEnd(&stream);
		return errno = Z_OK?Z_BUF_ERROR:errno;
	}
	*destLen = stream.total_out;
	errno = deflateEnd(&stream);
	return errno;
}




壓縮使用deflate,解壓就用inflate相關的函數就可以了。過幾天我再把網絡編程中常用的rc5,des,cast,ipp加密解密整理一下。

這些東西就是得經常用,經常看,要不然幾天又給忘光了



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章