基于LZO算法的编解码器



一、什么是LZO 

LZO是致力于解压速度的一种数据压缩算法LZO Lempel-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: */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在STM32上移植Mini LZO2.09压缩算法需要以下步骤: 1. 了解Mini LZO2.09压缩算法的原理和实现。Mini LZO2.09是一个轻量级的压缩算法库,它旨在提供高压缩比和快速压缩速度。 2. 配置STM32开发环境。安装相应的开发工具链、IDE,例如Keil MDK或者IAR Embedded Workbench,以及STM32的相关库和驱动。 3. 下载Mini LZO2.09压缩算法库的源代码。通常,源代码会以C语言的形式提供,包含压缩和解压函数以及相关的数据结构。 4. 创建一个新的STM32项目。在开发环境中,创建一个新的STM32项目,并配置相应的硬件设置(例如选择MCU型号、配置时钟、外设等)。 5. 添加Mini LZO2.09压缩算法库的源代码到项目中。将下载的Mini LZO2.09源代码文件添加到STM32项目的源代码文件夹中。 6. 编译项目。使用开发环境提供的编译工具对项目进行编译,确保没有语法错误和编译警告。 7. 进行移植适配。由于Mini LZO2.09压缩算法库可能使用了一些与STM32平台不兼容的函数或者数据结构,需要进行适当的修改和调整。这可能包括更改数据类型、适配存储器访问等。 8. 测试和验证。编写一些测试用例,对移植后的Mini LZO2.09压缩算法进行测试和验证,确保它在STM32平台上正常工作并且具有预期的压缩效果和速度。 9. 优化和调试。根据实际需求,对移植后的Mini LZO2.09压缩算法进行优化和调试,以提高压缩效率和速度,同时确保稳定性和可靠性。 10. 集成到实际项目中。将移植和优化后的Mini LZO2.09压缩算法集成到实际的STM32项目中,并进行实际应用的测试和验证。 以上是将Mini LZO2.09压缩算法移植到STM32的大致步骤。具体的移植过程和细节可能会因为不同的硬件平台和开发环境而有所差异。在移植过程中,需要仔细阅读Mini LZO2.09的文档和源代码,并进行适当的修改和调整,以适应STM32的硬件特性和开发环境的要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值