Chinaunix首页 | 论坛 | 博客
  • 博客访问: 681477
  • 博文数量: 199
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 914
  • 用 户 组: 普通用户
  • 注册时间: 2015-09-21 16:48
文章分类

全部博文(199)

文章存档

2025年(4)

2024年(1)

2023年(1)

2022年(3)

2021年(16)

2020年(4)

2019年(5)

2018年(10)

2017年(72)

2016年(36)

2015年(47)

我的朋友

分类: 嵌入式

2025-07-16 17:32:37

转:

一. 前言

 前面我们推荐了miniz压缩库,该库不是特别适合MCU等低资源平台,所以这一篇继续来分享一个适合MCU平台的压缩库miniLZO。    

miniLZO项目见

二. miniLZO特点

该库很久没有更新了,{BANNED}最佳后更新是17年。之前也在不同的MCU项目中用过该库,非常好用。

该库有以下特点:

lminiLZO是一个用ANSI C编写的可移植的无损数据压缩库。

l提供相当快的压缩和极快的解压,特别适合协议传输,数据采集等实时压缩应用场景。

l{BANNED}最佳快的压缩和解压缩算法之一。测试见。

l包括较慢的压缩级别,实现相当有竞争力的压缩比,同时仍然以非常高的速度解压缩。 低于要求解压快的场合非常适用。

l在GNU通用公共许可证(GPL v2+)的条款下发布。LZO Professional 是其商业版本

lminiLZO是LZO库的一个非常轻量级的子集,用于方便地包含在应用程序中。它是从LZO源代码自动生成的,包含了{BANNED}最佳重要的LZO函数。方便快速集成到自己的应用中。

三. 源码

Lzo代码

Minilzo代码

四. 移植

4.1添加源码

解压lzo-2.10.tar.gz

只需要minilzo中的minilzo.c和minlzo.h, testmini.c

还需要include/lzo下的lzoconf.h和lzodefs.h

将上述5个文件添加到自己的项目

4.2配置头文件

在工程级别定义宏MINILZO_HAVE_CONFIG_H

比如-DMINILZO_HAVE_CONFIG_H

Minilzo.h中

/* internal Autoconf configuration file - only used when building miniLZO */#ifdef MINILZO_HAVE_CONFIG_H#  include #endif



我们为了避免文件同名改为

/* internal Autoconf configuration file - only used when building miniLZO */#ifdef MINILZO_HAVE_CONFIG_H#  include "minilzo_config.h"#endif

同样的minilzo.c中的

#ifdef MINILZO_HAVE_CONFIG_H#  include #endif

改为

#ifdef MINILZO_HAVE_CONFIG_H#  include "minilzo_config.h"#endif

Lzoconf.h中的

#if defined(LZO_HAVE_CONFIG_H)#  include #endif

改为

#if defined(LZO_HAVE_CONFIG_H)#  include "minilzo_config.h"#endif

添加自己的”minilzo_config.h”文件

其中按照平台定义以下宏

#define SIZEOF_VOID_P 4

#define LZO_ABI_LITTLE_ENDIAN 1 小端系统

#define LZO_OPT_UNALIGNED16 0 不支持不对齐访问

#define LZO_OPT_UNALIGNED32 0

#define LZO_OPT_UNALIGNED64 0

4.3调试输出

Minilzo.c中

#undef NDEBUG#if (LZO_CFG_FREESTANDING)#  undef LZO_DEBUG#  define NDEBUG 1#  undef assert#  define assert(e) ((void)0)#else#  if !defined(LZO_DEBUG)#    define NDEBUG 1#  endif#  include #endif

定义宏LZO_DEBUG比如

-DLZO_DEBUG

则不会定义宏define NDEBUG 1

此时需要实现宏DVAL_ASSERT

#if !defined(DVAL_ASSERT)#if defined(__LZO_HASH_INCREMENTAL) && !defined(NDEBUG)#if 1 && (LZO_CC_ARMCC_GNUC || LZO_CC_CLANG || (LZO_CC_GNUC >= 0x020700ul) || LZO_CC_INTELC_GNUC || LZO_CC_LLVM || LZO_CC_PATHSCALE || LZO_CC_PGI)static void __attribute__((__unused__))#elsestatic void#endifDVAL_ASSERT(lzo_xint dv, const lzo_bytep p){    lzo_xint df;    DVAL_FIRST(df,(p));    assert(DINDEX(dv,p) == DINDEX(df,p));}#else#  define DVAL_ASSERT(dv,p) ((void) 0)#endif#endif 

可以在minilzo_config.h中

/* testmini.c -- very simple test program for the miniLZO library   This file is part of the LZO real-time data compression library.   Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer   All Rights Reserved.   The LZO library is free software; you can redistribute it and/or   modify it under the terms of the GNU General Public License as   published by the Free Software Foundation; either version 2 of   the License, or (at your option) any later version.   The LZO library is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with the LZO library; see the file COPYING.   If not, write to the Free Software Foundation, Inc.,   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.   Markus F.X.J. Oberhumer       */#include #include /*************************************************************************// This program shows the basic usage of the LZO library.// We will compress a block of data and decompress again.//// For more information, documentation, example programs and other support// files (like Makefiles and build scripts) please download the full LZO// package from//    **************************************************************************//* First let's include "minizo.h". */#include "minilzo.h"/* We want to compress the data block at 'in' with length 'IN_LEN' to * the block at 'out'. Because the input block may be incompressible, * we must provide a little more output space in case that compression * is not possible. */#define IN_LEN      (1*1024ul)#define OUT_LEN     (IN_LEN + IN_LEN / 16 + 64 + 3)static unsigned char __LZO_MMODEL in  [ IN_LEN ];static unsigned char __LZO_MMODEL out [ OUT_LEN ];/* Work-memory needed for compression. Allocate memory in units * of 'lzo_align_t' (instead of 'char') to make sure it is properly aligned. */#define HEAP_ALLOC(var,size) \    lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);/*************************************************************************//**************************************************************************/int minilzo_test(int argc, char *argv[]);int minilzo_test(int argc, char *argv[]){    int r;    lzo_uint in_len;    lzo_uint out_len;    lzo_uint new_len;    if (argc < 0 && argv == NULL)   /* avoid warning about unused args */        return 0;    printf("\nLZO real-time data compression library (v%s, %s).\n",           lzo_version_string(), lzo_version_date());    printf("Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");    printf("wrkmem len %d.\n\n",LZO1X_1_MEM_COMPRESS);/* * Step 1: initialize the LZO library */    if (lzo_init() != LZO_E_OK)    {        printf("internal error - lzo_init() failed !!!\n");        printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");        return 3;    }/* * Step 2: prepare the input block that will get compressed. *         We just fill it with zeros in this example program, *         but you would use your real-world data here. */    in_len = IN_LEN;    lzo_memset(in,0,in_len);/* * Step 3: compress from 'in' to 'out' with LZO1X-1 */    r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);    if (r == LZO_E_OK)        printf("compressed %lu bytes into %lu bytes\n",            (unsigned long) in_len, (unsigned long) out_len);    else    {        /* this should NEVER happen */        printf("internal error - compression failed: %d\n", r);        return 2;    }    /* check for an incompressible block */    if (out_len >= in_len)    {        printf("This block contains incompressible data.\n");        return 0;    }/* * Step 4: decompress again, now going from 'out' to 'in' */    new_len = in_len;    r = lzo1x_decompress(out,out_len,in,&new_len,NULL);    if (r == LZO_E_OK && new_len == in_len)        printf("decompressed %lu bytes back into %lu bytes\n",            (unsigned long) out_len, (unsigned long) in_len);    else    {        /* this should NEVER happen */        printf("internal error - decompression failed: %d\n", r);        return 1;    }    printf("\nminiLZO simple compression test passed.\n");    return 0;}/* vim:set ts=4 sw=4 et: */

#define IN_LEN      (128*1024ul)

#define IN_LEN      (1*1024ul)

#define LZO1X_1_MEM_COMPRESS    ((lzo_uint32_t) (16384L * lzo_sizeof_dict_t))

#define LZO1X_1_MEM_COMPRESS    ((lzo_uint32_t) (16384L * lzo_sizeof_dict_t))

#define LZO1X_1_MEM_COMPRESS    ((lzo_uint32_t) (1024u * lzo_sizeof_dict_t))

阅读(78) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~