Просмотр исходного кода

Merge pull request #1 from zhaojuntao/minilzo-sample

Minilzo sample
Bernard Xiong 8 лет назад
Родитель
Сommit
9620f42a8f
9 измененных файлов с 1709 добавлено и 1286 удалено
  1. 58 0
      README.md
  2. 4 2
      SConscript
  3. BIN
      doc/image/minilzo.png
  4. 0 163
      lzo.c
  5. 120 113
      lzoconf.h
  6. 605 493
      lzodefs.h
  7. 606 494
      minilzo.c
  8. 18 21
      minilzo.h
  9. 298 0
      minilzo_sample.c

+ 58 - 0
README.md

@@ -0,0 +1,58 @@
+# miniLZO
+
+## 1、介绍
+
+LZO 是一个实时数据压缩库, miniLZO 是 LZO 压缩库的精简版本。 他能够提供非常快速的压缩和解压功能, 解压并不需要内存的支持。 这个 [miniLZO](https://github.com/RT-Thread-packages/miniLZO) 库是 RT-thread 针对官方 [miniLZO](http://www.oberhumer.com/opensource/lzo/) 的C库的移植, 有关 miniLZO 的更多信息,请参阅 [http://www.oberhumer.com/opensource/lzo/](http://www.oberhumer.com/opensource/lzo/) 。
+
+## 2、获取方式
+
+- 使用 menuconfig
+```
+  RT-Thread online packages --->
+      miscellaneous packages --->
+          [*] miniLZO: A mini subset of the LZO real-time data compression library
+```
+
+## 3、示例介绍
+
+### 3.1 获取示例
+
+- 配置使能示例选项 `Enable using miniLZO sample`
+- 配置包版本选为最新版 `latest_version`
+
+![](./doc/image/minilzo.png)
+
+### 3.2 运行示例
+该示例为一个简单的文件压缩和解压的例程,需要依赖文件系统,用到的命令有两个` -c` 和 `-d`, `-c` 命令压缩一个文件到另一个文件,`-d` 命令解压一个文件到另一个文件。   
+使用方式:  
+msh cmd 压缩: `minilzo_test -c /file.bin /file.cmprs.bin`  
+msh cmd 解压: `minilzo_test -d /file.cmprs.bin /file_dcmprs.bin`  
+
+    msh />minilzo_test -c /file.bin /file.cmprs.bin
+    [minilzo]compress start : >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+    [minilzo]compressed 469848 bytes into 363495 bytes , compression ratio is 77%!
+    msh />
+    msh />minilzo_test -d /file.cmprs.bin /file_dcmprs.bin
+    [minilzo]decompress start : >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+    [minilzo]decompressed 363495 bytes into 469848 bytes !
+
+## 4、注意事项
+
+### 4.1 与官方源码差异
+
+  miniLZO 源码中定义了一个 `LZO1X_1_MEM_COMPRESS` 大小的压缩内存,占用内存 65536 字节,资源占用高,修改为最小 `8192L * lzo_sizeof_dict_t` 字节,占用内存 32768 字节。
+
+  对源码 `minilzo.h` 进行如下变动,移植官方代码的时候需要注意:
+
+  1. 修改 LZO1X_1_MEM_COMPRESS 大小
+  ```C
+#define LZO1X_1_MEM_COMPRESS    ((lzo_uint32_t) (16384L * lzo_sizeof_dict_t))
+  ```
+替换为
+  ```C
+#define LZO1X_1_MEM_COMPRESS    ((lzo_uint32_t) (8192L * lzo_sizeof_dict_t))
+  ```
+
+## 5、参考资料
+
+- miniLZO 官方网站:[http://www.oberhumer.com](http://www.oberhumer.com)

+ 4 - 2
SConscript

@@ -3,10 +3,12 @@ from building import *
 cwd = GetCurrentDir()
 src = Split('''
 minilzo.c
-lzo.c
 ''')
 CPPPATH = [cwd]
 
-group = DefineGroup('lzo', src, depend = ['PKG_USING_MINILZO'], CPPPATH = CPPPATH)
+if GetDepend('MINILZO_USING_SAMPLE'):
+    src += Glob('minilzo_sample.c')
+
+group = DefineGroup('minilzo', src, depend = ['PKG_USING_MINILZO'], CPPPATH = CPPPATH)
 
 Return('group')

BIN
doc/image/minilzo.png


+ 0 - 163
lzo.c

@@ -1,163 +0,0 @@
-/* lzo.c - location for general purpose minilzo functions
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- * Change Logs:
- * Date           Author       Notes
- */
-#include <rtthread.h>
-#include "minilzo.h"
-
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-#endif
-#define RT_USING_LZO
-#if defined(RT_USING_LZO) && defined(RT_USING_DFS)
-
-#ifdef _WIN32
-#pragma warning(disable: 4996)
-#endif
-#include <dfs_posix.h>
-
-/* the worst of allocation */
-#define LZO1X_WORST(x) ( (x) + ((x)/16) + 64 + 3 ) 
-
-#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);
-
-char* parse_lzo_error_code(int error_code)
-{
-	switch(error_code)
-	{
-	case LZO_E_ERROR:                 return "error";
-	case LZO_E_OUT_OF_MEMORY:         return "out of memory";
-	case LZO_E_NOT_COMPRESSIBLE:      return "not compressible";
-	case LZO_E_INPUT_OVERRUN:         return "input overrun";
-	case LZO_E_OUTPUT_OVERRUN:        return "output overrun";
-	case LZO_E_LOOKBEHIND_OVERRUN:    return "lookbehind overrun";
-	case LZO_E_EOF_NOT_FOUND:         return "eof not found";
-	case LZO_E_INPUT_NOT_CONSUMED:    return "input not consumed";
-	case LZO_E_NOT_YET_IMPLEMENTED:   return "not yet implemented";    /* [not used right now] */
-	case LZO_E_INVALID_ARGUMENT:      return "invalid argument";
-	default: return "none";
-	}
-}
-
-int lzo(char *srcfile, char *destfile)
-{
-	int result;
-	int fd;
-	struct stat s;
-	lzo_bytep in;
-	lzo_bytep out;
-	lzo_uint in_len, out_len;
-
-	rt_memset(&s, 0, sizeof(struct stat));
-	stat(srcfile, &s);
-	in_len = s.st_size; 
-	
-	in = rt_malloc(in_len); 
-	if (in == RT_NULL) return -1;
-	out = rt_malloc(LZO1X_WORST(in_len));
-	if (out == RT_NULL)	return -1;
-
-	fd = open(srcfile, O_RDONLY, 0);
-	if(fd < 0) 
-	{
-		result = -1;
-		goto _exit;
-	}
-	read(fd, in, in_len); 
-	close(fd);
-
-	result = lzo1x_1_compress(in, in_len, out, &out_len, wrkmem);
-	if(result != LZO_E_OK)
-	{
-		rt_kprintf("internal error - compression failed: \nerr_code:(%d) %s, %s.\n", 
-			result, parse_lzo_error_code(result), "Please use the binary access");
-		goto _exit;
-	}
-
-	fd = open(destfile, O_WRONLY | O_BINARY | O_CREAT, 0);
-	if(fd < 0)
-	{
-		result = -1;
-		goto _exit;
-	}
-	
-	write(fd, &in_len, sizeof(lzo_uint));	/* source file len */
-	write(fd, out, out_len); 
-	close(fd);
-	rt_kprintf("compress lzo ok!\n");
-	result = 0;
-
-_exit:
-	rt_free(in);
-	rt_free(out);
-	return result;
-}
-#ifdef RT_USING_FINSH
-FINSH_FUNCTION_EXPORT(lzo, compress a file. usage:lzo(src, dest));
-#endif
-
-int lzode(char *srcfile, char *destfile)
-{
-	int result;
-	int fd;
-	struct stat s;
-	lzo_bytep in=RT_NULL;
-	lzo_bytep out=RT_NULL;
-	lzo_uint in_len, out_len;
-
-	rt_memset(&s, 0, sizeof(struct stat));
-	stat(srcfile, &s);
-	in_len = s.st_size; 
-	
-	fd = open(srcfile, O_RDONLY, 0);
-	if(fd < 0) return 0;
-
-	read(fd, &out_len, sizeof(lzo_uint)); /* source file len */
-	in_len -= sizeof(lzo_uint);
-	in = rt_malloc(in_len); 
-	if (in == RT_NULL) return -1;
-	out = rt_malloc(out_len); 
-	if (out == RT_NULL)	return -1;
-
-	read(fd, in, in_len); 
-	close(fd);
-
-	result = lzo1x_decompress(in, in_len, out, &out_len, RT_NULL);
-	if(result != LZO_E_OK)
-	{
-		rt_kprintf("internal error - decompression failed: \nerr_code:(%d) %s, %s.\n", 
-			result, parse_lzo_error_code(result), "Please use the binary access");
-		goto _exit;
-	}
-
-	fd = open(destfile, O_WRONLY | O_BINARY | O_CREAT, 0);
-	if(fd < 0)
-	{
-		result = -1;
-		goto _exit;
-	}
-	write(fd, out, out_len);
-	close(fd);
-
-	rt_kprintf("decompress lzo ok!\n");
-	result = 0;
-
-_exit:
-	rt_free(in);
-	rt_free(out);
-	return result;
-}
-#ifdef RT_USING_FINSH
-FINSH_FUNCTION_EXPORT(lzode, decompress a file. usage:lzode(src, dest));
-#endif
-
-#endif

+ 120 - 113
lzoconf.h

@@ -2,22 +2,7 @@
 
    This file is part of the LZO real-time data compression library.
 
-   Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+   Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
    All Rights Reserved.
 
    The LZO library is free software; you can redistribute it and/or
@@ -44,9 +29,9 @@
 #ifndef __LZOCONF_H_INCLUDED
 #define __LZOCONF_H_INCLUDED 1
 
-#define LZO_VERSION             0x2060
-#define LZO_VERSION_STRING      "2.06"
-#define LZO_VERSION_DATE        "Aug 12 2011"
+#define LZO_VERSION             0x20a0  /* 2.10 */
+#define LZO_VERSION_STRING      "2.10"
+#define LZO_VERSION_DATE        "Mar 01 2017"
 
 /* internal Autoconf configuration file - only used when building LZO */
 #if defined(LZO_HAVE_CONFIG_H)
@@ -63,7 +48,7 @@
 #if !defined(CHAR_BIT) || (CHAR_BIT != 8)
 #  error "invalid CHAR_BIT"
 #endif
-#if !defined(UCHAR_MAX) || !defined(UINT_MAX) || !defined(ULONG_MAX)
+#if !defined(UCHAR_MAX) || !defined(USHRT_MAX) || !defined(UINT_MAX) || !defined(ULONG_MAX)
 #  error "check your compiler installation"
 #endif
 #if (USHRT_MAX < 1) || (UINT_MAX < 1) || (ULONG_MAX < 1)
@@ -72,7 +57,7 @@
 
 /* get OS and architecture defines */
 #ifndef __LZODEFS_H_INCLUDED
-#include "lzodefs.h"
+#include <lzo/lzodefs.h>
 #endif
 
 
@@ -85,14 +70,6 @@ extern "C" {
 // some core defines
 ************************************************************************/
 
-#if !defined(LZO_UINT32_C)
-#  if (UINT_MAX < LZO_0xffffffffL)
-#    define LZO_UINT32_C(c)     c ## UL
-#  else
-#    define LZO_UINT32_C(c)     ((c) + 0U)
-#  endif
-#endif
-
 /* memory checkers */
 #if !defined(__LZO_CHECKER)
 #  if defined(__BOUNDS_CHECKING_ON)
@@ -111,28 +88,35 @@ extern "C" {
 // integral and pointer types
 ************************************************************************/
 
-/* lzo_uint should match size_t */
+/* lzo_uint must match size_t */
 #if !defined(LZO_UINT_MAX)
-#  if defined(LZO_ABI_LLP64) /* WIN64 */
-#    if defined(LZO_OS_WIN64)
+#  if (LZO_ABI_LLP64)
+#    if (LZO_OS_WIN64)
      typedef unsigned __int64   lzo_uint;
      typedef __int64            lzo_int;
+#    define LZO_TYPEOF_LZO_INT  LZO_TYPEOF___INT64
 #    else
-     typedef unsigned long long lzo_uint;
-     typedef long long          lzo_int;
+     typedef lzo_ullong_t       lzo_uint;
+     typedef lzo_llong_t        lzo_int;
+#    define LZO_TYPEOF_LZO_INT  LZO_TYPEOF_LONG_LONG
 #    endif
+#    define LZO_SIZEOF_LZO_INT  8
 #    define LZO_UINT_MAX        0xffffffffffffffffull
 #    define LZO_INT_MAX         9223372036854775807LL
 #    define LZO_INT_MIN         (-1LL - LZO_INT_MAX)
-#  elif defined(LZO_ABI_IP32L64) /* MIPS R5900 */
+#  elif (LZO_ABI_IP32L64) /* MIPS R5900 */
      typedef unsigned int       lzo_uint;
      typedef int                lzo_int;
+#    define LZO_SIZEOF_LZO_INT  LZO_SIZEOF_INT
+#    define LZO_TYPEOF_LZO_INT  LZO_TYPEOF_INT
 #    define LZO_UINT_MAX        UINT_MAX
 #    define LZO_INT_MAX         INT_MAX
 #    define LZO_INT_MIN         INT_MIN
 #  elif (ULONG_MAX >= LZO_0xffffffffL)
      typedef unsigned long      lzo_uint;
      typedef long               lzo_int;
+#    define LZO_SIZEOF_LZO_INT  LZO_SIZEOF_LONG
+#    define LZO_TYPEOF_LZO_INT  LZO_TYPEOF_LONG
 #    define LZO_UINT_MAX        ULONG_MAX
 #    define LZO_INT_MAX         LONG_MAX
 #    define LZO_INT_MIN         LONG_MIN
@@ -141,63 +125,23 @@ extern "C" {
 #  endif
 #endif
 
-/* Integral types with 32 bits or more. */
-#if !defined(LZO_UINT32_MAX)
-#  if (UINT_MAX >= LZO_0xffffffffL)
-     typedef unsigned int       lzo_uint32;
-     typedef int                lzo_int32;
-#    define LZO_UINT32_MAX      UINT_MAX
-#    define LZO_INT32_MAX       INT_MAX
-#    define LZO_INT32_MIN       INT_MIN
-#  elif (ULONG_MAX >= LZO_0xffffffffL)
-     typedef unsigned long      lzo_uint32;
-     typedef long               lzo_int32;
-#    define LZO_UINT32_MAX      ULONG_MAX
-#    define LZO_INT32_MAX       LONG_MAX
-#    define LZO_INT32_MIN       LONG_MIN
-#  else
-#    error "lzo_uint32"
-#  endif
-#endif
-
-/* Integral types with exactly 64 bits. */
-#if !defined(LZO_UINT64_MAX)
-#  if (LZO_UINT_MAX >= LZO_0xffffffffL)
-#   if ((((LZO_UINT_MAX) >> 31) >> 31) == 3)
-#    define lzo_uint64          lzo_uint
-#    define lzo_int64           lzo_int
-#    define LZO_UINT64_MAX      LZO_UINT_MAX
-#    define LZO_INT64_MAX       LZO_INT_MAX
-#    define LZO_INT64_MIN       LZO_INT_MIN
-#   endif
-#  elif (ULONG_MAX >= LZO_0xffffffffL)
-#   if ((((ULONG_MAX) >> 31) >> 31) == 3)
-     typedef unsigned long      lzo_uint64;
-     typedef long               lzo_int64;
-#    define LZO_UINT64_MAX      ULONG_MAX
-#    define LZO_INT64_MAX       LONG_MAX
-#    define LZO_INT64_MIN       LONG_MIN
-#   endif
-#  endif
-#endif
-
-/* The larger type of lzo_uint and lzo_uint32. */
-#if (LZO_UINT_MAX >= LZO_UINT32_MAX)
+/* The larger type of lzo_uint and lzo_uint32_t. */
+#if (LZO_SIZEOF_LZO_INT >= 4)
 #  define lzo_xint              lzo_uint
 #else
-#  define lzo_xint              lzo_uint32
+#  define lzo_xint              lzo_uint32_t
 #endif
 
-/* Memory model that allows to access memory at offsets of lzo_uint. */
-#if !defined(__LZO_MMODEL)
-#  if (LZO_UINT_MAX <= UINT_MAX)
-#    define __LZO_MMODEL        /*empty*/
-#  elif defined(LZO_HAVE_MM_HUGE_PTR)
-#    define __LZO_MMODEL_HUGE   1
-#    define __LZO_MMODEL        __huge
-#  else
-#    define __LZO_MMODEL        /*empty*/
-#  endif
+typedef int lzo_bool;
+
+/* sanity checks */
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_int)  == LZO_SIZEOF_LZO_INT)
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) == LZO_SIZEOF_LZO_INT)
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_xint) >= sizeof(lzo_uint))
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_xint) >= sizeof(lzo_uint32_t))
+
+#ifndef __LZO_MMODEL
+#define __LZO_MMODEL            /*empty*/
 #endif
 
 /* no typedef here because of const-pointer issues */
@@ -206,21 +150,52 @@ extern "C" {
 #define lzo_voidp               void __LZO_MMODEL *
 #define lzo_shortp              short __LZO_MMODEL *
 #define lzo_ushortp             unsigned short __LZO_MMODEL *
-#define lzo_uint32p             lzo_uint32 __LZO_MMODEL *
-#define lzo_int32p              lzo_int32 __LZO_MMODEL *
-#if defined(LZO_UINT64_MAX)
-#define lzo_uint64p             lzo_uint64 __LZO_MMODEL *
-#define lzo_int64p              lzo_int64 __LZO_MMODEL *
-#endif
-#define lzo_uintp               lzo_uint __LZO_MMODEL *
 #define lzo_intp                lzo_int __LZO_MMODEL *
+#define lzo_uintp               lzo_uint __LZO_MMODEL *
 #define lzo_xintp               lzo_xint __LZO_MMODEL *
 #define lzo_voidpp              lzo_voidp __LZO_MMODEL *
 #define lzo_bytepp              lzo_bytep __LZO_MMODEL *
-/* deprecated - use 'lzo_bytep' instead of 'lzo_byte *' */
-#define lzo_byte                unsigned char __LZO_MMODEL
 
-typedef int lzo_bool;
+#define lzo_int8_tp             lzo_int8_t __LZO_MMODEL *
+#define lzo_uint8_tp            lzo_uint8_t __LZO_MMODEL *
+#define lzo_int16_tp            lzo_int16_t __LZO_MMODEL *
+#define lzo_uint16_tp           lzo_uint16_t __LZO_MMODEL *
+#define lzo_int32_tp            lzo_int32_t __LZO_MMODEL *
+#define lzo_uint32_tp           lzo_uint32_t __LZO_MMODEL *
+#if defined(lzo_int64_t)
+#define lzo_int64_tp            lzo_int64_t __LZO_MMODEL *
+#define lzo_uint64_tp           lzo_uint64_t __LZO_MMODEL *
+#endif
+
+/* Older LZO versions used to support ancient systems and memory models
+ * such as 16-bit MSDOS with __huge pointers or Cray PVP, but these
+ * obsolete configurations are not supported any longer.
+ */
+#if defined(__LZO_MMODEL_HUGE)
+#error "__LZO_MMODEL_HUGE memory model is unsupported"
+#endif
+#if (LZO_MM_PVP)
+#error "LZO_MM_PVP memory model is unsupported"
+#endif
+#if (LZO_SIZEOF_INT < 4)
+#error "LZO_SIZEOF_INT < 4 is unsupported"
+#endif
+#if (__LZO_UINTPTR_T_IS_POINTER)
+#error "__LZO_UINTPTR_T_IS_POINTER is unsupported"
+#endif
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(int) >= 4)
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) >= 4)
+/* Strange configurations where sizeof(lzo_uint) != sizeof(size_t) should
+ * work but have not received much testing lately, so be strict here.
+ */
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) == sizeof(size_t))
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) == sizeof(ptrdiff_t))
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) == sizeof(lzo_uintptr_t))
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(void *)   == sizeof(lzo_uintptr_t))
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(char *)   == sizeof(lzo_uintptr_t))
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(long *)   == sizeof(lzo_uintptr_t))
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(void *)   == sizeof(lzo_voidp))
+LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(char *)   == sizeof(lzo_bytep))
 
 
 /***********************************************************************
@@ -251,13 +226,13 @@ typedef int lzo_bool;
 
 /* __cdecl calling convention for public C and assembly functions */
 #if !defined(LZO_PUBLIC)
-#  define LZO_PUBLIC(_rettype)  __LZO_EXPORT1 _rettype __LZO_EXPORT2 __LZO_CDECL
+#  define LZO_PUBLIC(r)         __LZO_EXPORT1 r __LZO_EXPORT2 __LZO_CDECL
 #endif
 #if !defined(LZO_EXTERN)
-#  define LZO_EXTERN(_rettype)  __LZO_EXTERN_C LZO_PUBLIC(_rettype)
+#  define LZO_EXTERN(r)         __LZO_EXTERN_C LZO_PUBLIC(r)
 #endif
 #if !defined(LZO_PRIVATE)
-#  define LZO_PRIVATE(_rettype) static _rettype __LZO_CDECL
+#  define LZO_PRIVATE(r)        static r  __LZO_CDECL
 #endif
 
 /* function types */
@@ -315,7 +290,7 @@ struct lzo_callback_t
     /* a progress indicator callback function (set to 0 to disable) */
     lzo_progress_func_t nprogress;
 
-    /* NOTE: the first parameter "self" of the nalloc/nfree/nprogress
+    /* INFO: the first parameter "self" of the nalloc/nfree/nprogress
      * callbacks points back to this struct, so you are free to store
      * some extra info in the following variables. */
     lzo_voidp user1;
@@ -343,6 +318,9 @@ struct lzo_callback_t
 #define LZO_E_INPUT_NOT_CONSUMED    (-8)
 #define LZO_E_NOT_YET_IMPLEMENTED   (-9)    /* [not used right now] */
 #define LZO_E_INVALID_ARGUMENT      (-10)
+#define LZO_E_INVALID_ALIGNMENT     (-11)   /* pointer argument is not properly aligned */
+#define LZO_E_OUTPUT_NOT_CONSUMED   (-12)
+#define LZO_E_INTERNAL_ERROR        (-99)
 
 
 #ifndef lzo_sizeof_dict_t
@@ -356,7 +334,7 @@ struct lzo_callback_t
  * 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),(int)sizeof(lzo_uint),\
+    (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);
@@ -379,18 +357,22 @@ LZO_EXTERN(lzo_voidp)
     lzo_memset(lzo_voidp buf, int c, lzo_uint len);
 
 /* checksum functions */
-LZO_EXTERN(lzo_uint32)
-    lzo_adler32(lzo_uint32 c, const lzo_bytep buf, lzo_uint len);
-LZO_EXTERN(lzo_uint32)
-    lzo_crc32(lzo_uint32 c, const lzo_bytep buf, lzo_uint len);
-LZO_EXTERN(const lzo_uint32p)
+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_bytep p; lzo_uint u; } __lzo_pu_u;
-typedef union { lzo_bytep p; lzo_uint32 u32; } __lzo_pu32_u;
-typedef union { void *vp; lzo_bytep bp; lzo_uint u; lzo_uint32 u32; unsigned long l; } lzo_align_t;
+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;
 
 /* align a char pointer on a boundary that is a multiple of 'size' */
 LZO_EXTERN(unsigned) __lzo_align_gap(const lzo_voidp p, lzo_uint size);
@@ -399,9 +381,34 @@ LZO_EXTERN(unsigned) __lzo_align_gap(const lzo_voidp p, lzo_uint size);
 
 
 /***********************************************************************
-// deprecated macros - only for backward compatibility with LZO v1.xx
+// 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
+/* deprecated types */
+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
@@ -443,4 +450,4 @@ LZO_EXTERN(unsigned) __lzo_align_gap(const lzo_voidp p, lzo_uint size);
 #endif /* already included */
 
 
-/* vim:set ts=4 et: */
+/* vim:set ts=4 sw=4 et: */

Разница между файлами не показана из-за своего большого размера
+ 605 - 493
lzodefs.h


Разница между файлами не показана из-за своего большого размера
+ 606 - 494
minilzo.c


+ 18 - 21
minilzo.h

@@ -2,22 +2,7 @@
 
    This file is part of the LZO real-time data compression library.
 
-   Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
-   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+   Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
    All Rights Reserved.
 
    The LZO library is free software; you can redistribute it and/or
@@ -47,15 +32,25 @@
  */
 
 
-#ifndef __MINILZO_H
-#define __MINILZO_H 1
+#ifndef __MINILZO_H_INCLUDED
+#define __MINILZO_H_INCLUDED 1
 
-#define MINILZO_VERSION         0x2060
+#define MINILZO_VERSION         0x20a0  /* 2.10 */
 
-#ifdef __LZOCONF_H
+#if defined(__LZOCONF_H_INCLUDED)
 #  error "you cannot use both LZO and miniLZO"
 #endif
 
+/* internal Autoconf configuration file - only used when building miniLZO */
+#ifdef MINILZO_HAVE_CONFIG_H
+#  include <config.h>
+#endif
+#include <limits.h>
+#include <stddef.h>
+
+#ifndef __LZODEFS_H_INCLUDED
+#include "lzodefs.h"
+#endif
 #undef LZO_HAVE_CONFIG_H
 #include "lzoconf.h"
 
@@ -78,7 +73,7 @@ extern "C" {
  */
 
 #define LZO1X_MEM_COMPRESS      LZO1X_1_MEM_COMPRESS
-#define LZO1X_1_MEM_COMPRESS    ((lzo_uint32) (16384L * lzo_sizeof_dict_t))
+#define LZO1X_1_MEM_COMPRESS    ((lzo_uint32_t) (8192L * lzo_sizeof_dict_t))
 #define LZO1X_MEM_DECOMPRESS    (0)
 
 
@@ -107,3 +102,5 @@ lzo1x_decompress_safe   ( const lzo_bytep src, lzo_uint  src_len,
 
 #endif /* already included */
 
+
+/* vim:set ts=4 sw=4 et: */

+ 298 - 0
minilzo_sample.c

@@ -0,0 +1,298 @@
+/*
+ * File : minilzo_sample.c
+ * this example is a very simple test program for the minilzo library,
+ * using non-stream compress and decompress. If you want to use stream compress,
+ * you need at least 100K of ROM for history buffer(not recommend), or you can custom  
+ * header to storage the compress block size, and carry out stream compress by non-stream.
+ *
+ * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
+ *
+ *  This program 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.
+ *
+ *  This program 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 this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date          Author          Notes
+ * 2018-02-05    chenyong     first version
+ * 2018-02-11    Murphy        Adapted minilzo
+ */
+ 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <rtthread.h>
+
+#include <dfs_posix.h>
+
+#include "minilzo.h"
+
+#define malloc     rt_malloc
+#define free       rt_free
+
+#define BLOCK_HEADER_SIZE              4
+
+/* The output buffer can not be smaller than 66 bytes */
+#define COMPRESS_BUFFER_SIZE           4096
+#define DCOMPRESS_BUFFER_SIZE          4096
+
+/*  we must provide a little more output space in case that compression is not possible */
+#define COMPRESS_BUFFER_PADDING        (COMPRESS_BUFFER_SIZE / 16 + 64 + 3)
+
+static int minilzo_compress_file(int fd_in, int fd_out)
+{
+    /* Start to compress file  */
+    rt_uint8_t *cmprs_buffer = RT_NULL, *buffer = RT_NULL;
+    rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
+    int cmprs_size = 0, block_size = 0, totle_cmprs_size = 0;
+    size_t file_size = 0, i = 0;
+    int ret = 0;
+
+    /* Work-memory needed for compression */
+    lzo_voidp wrkmem = (lzo_voidp)malloc(LZO1X_1_MEM_COMPRESS);
+    memset(wrkmem, 0x00, LZO1X_1_MEM_COMPRESS);
+
+    file_size = lseek(fd_in, 0, SEEK_END);
+    lseek(fd_in, 0, SEEK_SET);
+
+    cmprs_buffer = (rt_uint8_t *) malloc(COMPRESS_BUFFER_SIZE + COMPRESS_BUFFER_PADDING);
+    buffer = (rt_uint8_t *) malloc(COMPRESS_BUFFER_SIZE);
+    if (!cmprs_buffer || !buffer)
+    {
+        rt_kprintf("[minilzo] No memory for cmprs_buffer or buffer!\n");
+        ret = -1;
+        goto _exit;
+    }
+
+    rt_kprintf("[minilzo]compress start : ");
+    for (i = 0; i < file_size; i += COMPRESS_BUFFER_SIZE)
+    {
+        if ((file_size - i) < COMPRESS_BUFFER_SIZE)
+        {
+            block_size = file_size - i;
+        }
+        else
+        {
+            block_size = COMPRESS_BUFFER_SIZE;
+        }
+        
+        memset(buffer, 0x00, COMPRESS_BUFFER_SIZE);
+        memset(cmprs_buffer, 0x00, COMPRESS_BUFFER_SIZE + COMPRESS_BUFFER_PADDING);
+
+        read(fd_in, buffer, block_size);
+
+        /* The destination buffer must be at least size + 400 bytes large because incompressible data may increase in size. */
+        ret = lzo1x_1_compress(buffer, block_size, cmprs_buffer, &cmprs_size, wrkmem);
+        if (ret != LZO_E_OK)
+        {
+            ret = -1;
+            goto _exit;            
+        }
+
+        /* Store compress block size to the block header (4 byte). */
+        buffer_hdr[3] = cmprs_size % (1 << 8);
+        buffer_hdr[2] = (cmprs_size % (1 << 16)) / (1 << 8);
+        buffer_hdr[1] = (cmprs_size % (1 << 24)) / (1 << 16);
+        buffer_hdr[0] = cmprs_size / (1 << 24);
+
+        write(fd_out, buffer_hdr, BLOCK_HEADER_SIZE);
+        write(fd_out, cmprs_buffer, cmprs_size);
+
+        totle_cmprs_size += cmprs_size + BLOCK_HEADER_SIZE;
+        rt_kprintf(">");
+    }
+
+    rt_kprintf("\n");
+    rt_kprintf("[minilzo]compressed %d bytes into %d bytes , compression ratio is %d%!\n", file_size, totle_cmprs_size,
+            (totle_cmprs_size * 100) / file_size);
+_exit:
+    if (cmprs_buffer)
+    {
+        free(cmprs_buffer);
+    }
+
+    if (buffer)
+    {
+        free(buffer);
+    }
+
+    if (wrkmem)
+    {
+        free(wrkmem);
+    }
+
+    return ret;
+}
+
+
+static int minilzo_decompress_file(int fd_in, int fd_out)
+{
+    /* Start to decompress file  */
+    rt_uint8_t *dcmprs_buffer = RT_NULL, *buffer = RT_NULL;
+    rt_uint8_t buffer_hdr[BLOCK_HEADER_SIZE] = { 0 };
+    size_t dcmprs_size = 0, block_size = 0, total_dcmprs_size = 0;
+    size_t file_size = 0, i = 0;
+    int ret = 0;
+
+    file_size = lseek(fd_in, 0, SEEK_END);
+    lseek(fd_in, 0, SEEK_SET);
+
+    if (file_size <= BLOCK_HEADER_SIZE)
+    {
+        rt_kprintf("[minilzo] decomprssion file size : %d error!\n", file_size);
+        ret = -1;
+        goto _dcmprs_exit;
+    }
+
+    dcmprs_buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE);
+    buffer = (rt_uint8_t *) malloc(DCOMPRESS_BUFFER_SIZE + COMPRESS_BUFFER_PADDING);
+    if (!dcmprs_buffer || !buffer)
+    {
+        rt_kprintf("[minilzo] No memory for dcmprs_buffer or buffer!\n");
+        ret = -1;
+        goto _dcmprs_exit;
+    }
+
+    rt_kprintf("[minilzo]decompress start : ");
+    for (i = 0; i < file_size; i += BLOCK_HEADER_SIZE + block_size)
+    {
+        /* Get the decompress block size from the block header. */
+        read(fd_in, buffer_hdr, BLOCK_HEADER_SIZE);
+        block_size = buffer_hdr[0] * (1 << 24) + buffer_hdr[1] * (1 << 16) + buffer_hdr[2] * (1 << 8) + buffer_hdr[3];
+
+        memset(buffer, 0x00, COMPRESS_BUFFER_SIZE + COMPRESS_BUFFER_PADDING);
+        memset(dcmprs_buffer, 0x00, DCOMPRESS_BUFFER_SIZE);
+
+        read(fd_in, buffer, block_size);
+
+        ret = lzo1x_decompress(buffer, block_size, dcmprs_buffer, &dcmprs_size, NULL);
+        if (ret != LZO_E_OK)
+        {
+            ret = -1;
+            goto _dcmprs_exit;            
+        }
+        
+        write(fd_out, dcmprs_buffer, dcmprs_size);
+
+        total_dcmprs_size += dcmprs_size;
+        rt_kprintf(">");
+    }
+    rt_kprintf("\n");
+    rt_kprintf("decompressed %d bytes into %d bytes !\n", file_size, total_dcmprs_size);
+
+_dcmprs_exit:
+    if (dcmprs_buffer)
+    {
+        free(dcmprs_buffer);
+    }
+
+    if(buffer)
+    {
+        free(buffer);
+    }
+
+    return ret;
+}
+
+int minilzo_test(int argc, char ** argv)
+{
+    int fd_in = -1 , fd_out = -1;
+    int ret  = 0;
+
+    if (argc != 4)
+    {
+        rt_kprintf("Usage:\n");
+        rt_kprintf("minilzo_test -c [file] [cmprs_file]          -compress \"file\" to \"cmprs_file\" \n");
+        rt_kprintf("minilzo_test -d [cmprs_file] [dcmprs_file]   -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
+        
+        ret = -1;
+        goto _exit;
+    }
+
+    rt_kprintf("\nminiLZO real-time data compression library (v%s, %s).\n",
+           lzo_version_string(), lzo_version_date());
+
+    /*
+    * Initialize the LZO library
+    */
+    if (lzo_init() != LZO_E_OK)
+    {
+        rt_kprintf("internal error - lzo_init() failed !!!\n");        
+        return 3;
+    }    
+
+    fd_in = open(argv[2], O_RDONLY, 0);
+    if (fd_in < 0)
+    {
+        rt_kprintf("[minilzo] open the input file : %s error!\n", argv[2]);
+        ret = -1;
+        goto _exit;
+    }
+
+    fd_out = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, 0);
+    if (fd_out < 0)
+    {
+        rt_kprintf("[minilzo] open the output file : %s error!\n", argv[3]);
+        ret = -1;
+        goto _exit;
+    }
+
+    if(memcmp("-c", argv[1], strlen(argv[1])) == 0)
+    {
+
+        if(minilzo_compress_file(fd_in, fd_out) < 0)
+        {
+            rt_kprintf("[minilzo] minilzo compress file error!\n");
+        }
+
+    }
+    else if(memcmp("-d", argv[1], strlen(argv[1])) == 0)
+    {
+
+        if(minilzo_decompress_file(fd_in, fd_out) < 0)
+        {
+            rt_kprintf("[minilzo] minilzo decompress file error!\n");
+        }
+    }
+    else
+    {
+        rt_kprintf("Usage:\n");
+        rt_kprintf("minilzo_test -c [file] [cmprs_file]          -compress \"file\" to \"cmprs_file\" \n");
+        rt_kprintf("minilzo_test -d [cmprs_file] [dcmprs_file]   -dcompress \"cmprs_file\" to \"dcmprs_file\" \n");
+        
+        ret = -1;
+        goto _exit;
+    }
+
+_exit:
+    if(fd_in >= 0)
+    {
+        close(fd_in);
+    }
+
+    if(fd_out >= 0)
+    {
+        close(fd_out);
+    }
+
+    return ret;
+}
+
+#ifdef RT_USING_FINSH
+#ifdef FINSH_USING_MSH
+
+#include <finsh.h>
+
+MSH_CMD_EXPORT(minilzo_test, minilzo compress and decompress test);
+#endif
+#endif

Некоторые файлы не были показаны из-за большого количества измененных файлов