基于LZO算法的编解码器

来源:互联网 发布:什么叫网络销售 编辑:程序博客网 时间:2024/05/16 01:27


一、什么是LZO 

LZO是致力于解压速度的一种数据压缩算法LZOLempel-Ziv-Oberhumer的缩写。这个算法是无损算法,参考实现程序是线程安全的。实现它的一个自由软件工具是lzop。最初的库是用 ANSI C 编写、并且遵从 GNU通用公共许可证发布的。现在 LZO 有用于 PerlPython以及Java的各种版本。代码版权的所有者是 Markus F. X. J.Oberhumer

对于游戏开发人员来说LZO有以下优点。

1)解压缩速度非常快。

2)不需要额外的内存解压缩。

3)能设置不同的压缩参数改变压缩率,但设置这些压缩参数不会降低解压速度。

4LZO是无损压缩,压缩后的数据能准确还原。

MiniLZO是一个轻量级的压缩/解压缩库,设计MiniLZO的目的是为了某些只需包含一个小型的压缩/解压缩功能,而不想包含LZO全部代码的程序而设计的。编译后的MiniLZO库少于5KB,非常适合内嵌在主程序中。

 

源码地址:

C: http://www.oberhumer.com/opensource/lzo/

c#:http://lzohelper.codeplex.com/

本次主要调试miniLZO,下载后,解压,在vs中新建一个win32控制台,然后分别把三个头文件和两个源文件加到对应的文件夹,如图:

点击testmini.c

调试成功,是无损压缩

分析部分程序:

/* malloc & free函数功能类型 */
typedef lzo_voidp (__LZO_CDECL *lzo_alloc_func_t)
    (lzo_callback_p self, lzo_uint items, lzo_uint size);
typedef void      (__LZO_CDECL *lzo_free_func_t)
    (lzo_callback_p self, lzo_voidp ptr);


/*
进度指示回调函数 */
typedef void (__LZO_CDECL *lzo_progress_func_t)
    (lzo_callback_p, lzo_uint, lzo_uint, int);


struct lzo_callback_t
{
    /* custom allocators (set to 0 to disable) */
    lzo_alloc_func_t nalloc;               /* [not used right now] */
    lzo_free_func_t nfree;                 /* [not used right now] */


    /* a progress indicator callback function (set to 0 to disable)*/
    lzo_progress_func_t nprogress;


    /* INFO: nalloc/nfree/nprogress
的第一个参数“self”
     *
回调指向这个结构,所以可以自由存储
     *
以下变量可以有额外的信息 */
    lzo_voidp user1;
    lzo_xint user2;
    lzo_xint user3;
};




/***********************************************************************
// error codes and prototypes
************************************************************************/


/* Error codes for the compression/decompression functions. Negative
 * values are errors, positive values will be used for special but
 * normal events.
 */
#define LZO_E_OK                   0
#define LZO_E_ERROR                (-1)
#define LZO_E_OUT_OF_MEMORY         (-2)    /*[lzo_alloc_func_t failure] */
#define LZO_E_NOT_COMPRESSIBLE      (-3)    /* [notused right now] */
#define LZO_E_INPUT_OVERRUN         (-4)
#define LZO_E_OUTPUT_OVERRUN        (-5)
#define LZO_E_LOOKBEHIND_OVERRUN    (-6)
#define LZO_E_EOF_NOT_FOUND         (-7)
#define LZO_E_INPUT_NOT_CONSUMED    (-8)
#define LZO_E_NOT_YET_IMPLEMENTED   (-9)    /* [not used rightnow] */
#define LZO_E_INVALID_ARGUMENT      (-10)
#define LZO_E_INVALID_ALIGNMENT     (-11)   /* pointer argumentis not properly aligned */
#define LZO_E_OUTPUT_NOT_CONSUMED   (-12)
#define LZO_E_INTERNAL_ERROR        (-99)




#ifndef lzo_sizeof_dict_t
#  define lzo_sizeof_dict_t     ((unsigned)sizeof(lzo_bytep))
#endif


/* lzo_init()
是调用的第一个函数
 * Check the return code !
 *
 * lzo_init() is a macro to allow checking that the library and the
 * compiler's view of various types are consistent.
 */
#define lzo_init() __lzo_init_v2(LZO_VERSION,(int)sizeof(short),(int)sizeof(int),\
   (int)sizeof(long),(int)sizeof(lzo_uint32_t),(int)sizeof(lzo_uint),\
    (int)lzo_sizeof_dict_t,(int)sizeof(char*),(int)sizeof(lzo_voidp),\
    (int)sizeof(lzo_callback_t))
LZO_EXTERN(int) __lzo_init_v2(unsigned,int,int,int,int,int,int,int,int,int);


/*
版本函数功能*/
LZO_EXTERN(unsigned) lzo_version(void);
LZO_EXTERN(const char *) lzo_version_string(void);
LZO_EXTERN(const char *) lzo_version_date(void);
LZO_EXTERN(const lzo_charp) _lzo_version_string(void);
LZO_EXTERN(const lzo_charp) _lzo_version_date(void);


/*
字符串函数 */
LZO_EXTERN(int)
    lzo_memcmp(const lzo_voidp a, const lzo_voidp b, lzo_uint len);
LZO_EXTERN(lzo_voidp)
    lzo_memcpy(lzo_voidp dst, const lzo_voidp src, lzo_uint len);
LZO_EXTERN(lzo_voidp)
    lzo_memmove(lzo_voidp dst, const lzo_voidp src, lzo_uint len);
LZO_EXTERN(lzo_voidp)
    lzo_memset(lzo_voidp buf, int c, lzo_uint len);


/*
校验功能 */
LZO_EXTERN(lzo_uint32_t)
    lzo_adler32(lzo_uint32_t c, const lzo_bytep buf, lzo_uint len);
LZO_EXTERN(lzo_uint32_t)
    lzo_crc32(lzo_uint32_t c, const lzo_bytep buf, lzo_uint len);
LZO_EXTERN(const lzo_uint32_tp)
    lzo_get_crc32_table(void);


/* misc. */
LZO_EXTERN(int) _lzo_config_check(void);
typedef union {
    lzo_voidp a00; lzo_bytep a01; lzo_uint a02; lzo_xint a03;lzo_uintptr_t a04;
    void *a05; unsigned char *a06; unsigned long a07; size_t a08;ptrdiff_t a09;
#if defined(lzo_int64_t)
    lzo_uint64_t a10;
#endif
} lzo_align_t;


/*
将一个char指针对齐到一个具有多个大小的边界上*/
LZO_EXTERN(unsigned) __lzo_align_gap(const lzo_voidp p, lzo_uint size);
#define LZO_PTR_ALIGN_UP(p,size) \
    ((p) + (lzo_uint) __lzo_align_gap((constlzo_voidp)(p),(lzo_uint)(size)))




/***********************************************************************
// deprecated macros - only for backward compatibility
************************************************************************/


/* deprecated - use 'lzo_bytep' instead of 'lzo_byte *' */
#define lzo_byte               unsigned char
/* deprecated type names */
#define lzo_int32               lzo_int32_t
#define lzo_uint32              lzo_uint32_t
#define lzo_int32p              lzo_int32_t__LZO_MMODEL *
#define lzo_uint32p             lzo_uint32_t__LZO_MMODEL *
#define LZO_INT32_MAX          LZO_INT32_C(2147483647)
#define LZO_UINT32_MAX          LZO_UINT32_C(4294967295)
#if defined(lzo_int64_t)
#define lzo_int64               lzo_int64_t
#define lzo_uint64              lzo_uint64_t
#define lzo_int64p              lzo_int64_t__LZO_MMODEL *
#define lzo_uint64p             lzo_uint64_t__LZO_MMODEL *
#define LZO_INT64_MAX          LZO_INT64_C(9223372036854775807)
#define LZO_UINT64_MAX         LZO_UINT64_C(18446744073709551615)
#endif
/*
弃用类型 */
typedef union { lzo_bytep a; lzo_uint b; } __lzo_pu_u;
typedef union { lzo_bytep a; lzo_uint32_t b; } __lzo_pu32_u;
/* deprecated defines */
#if !defined(LZO_SIZEOF_LZO_UINT)
#  define LZO_SIZEOF_LZO_UINT   LZO_SIZEOF_LZO_INT
#endif


#if defined(LZO_CFG_COMPAT)


#define __LZOCONF_H 1


#if defined(LZO_ARCH_I086)
#  define __LZO_i386 1
#elif defined(LZO_ARCH_I386)
#  define __LZO_i386 1
#endif


#if defined(LZO_OS_DOS16)
#  define __LZO_DOS 1
#  define __LZO_DOS16 1
#elif defined(LZO_OS_DOS32)
#  define __LZO_DOS 1
#elif defined(LZO_OS_WIN16)
#  define __LZO_WIN 1
#  define __LZO_WIN16 1
#elif defined(LZO_OS_WIN32)
#  define __LZO_WIN 1
#endif


#define __LZO_CMODEL            /*empty*/
#define __LZO_DMODEL            /*empty*/
#define __LZO_ENTRY             __LZO_CDECL
#define LZO_EXTERN_CDECL        LZO_EXTERN
#define LZO_ALIGN              LZO_PTR_ALIGN_UP


#define lzo_compress_asm_t      lzo_compress_t
#define lzo_decompress_asm_t    lzo_decompress_t


#endif /* LZO_CFG_COMPAT */




#ifdef __cplusplus
} /* extern "C" */
#endif


#endif /* already included */




/* vim:set ts=4 sw=4 et: */

 

原创粉丝点击