minilzo使用流程

  1 /* testmini.c -- very simple test program for the miniLZO library
  2 
  3    This file is part of the LZO real-time data compression library.
  4 
  5    Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
  6    All Rights Reserved.
  7 
  8    The LZO library is free software; you can redistribute it and/or
  9    modify it under the terms of the GNU General Public License as
 10    published by the Free Software Foundation; either version 2 of
 11    the License, or (at your option) any later version.
 12 
 13    The LZO library is distributed in the hope that it will be useful,
 14    but WITHOUT ANY WARRANTY; without even the implied warranty of
 15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16    GNU General Public License for more details.
 17 
 18    You should have received a copy of the GNU General Public License
 19    along with the LZO library; see the file COPYING.
 20    If not, write to the Free Software Foundation, Inc.,
 21    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 22 
 23    Markus F.X.J. Oberhumer
 24    <markus@oberhumer.com>
 25    http://www.oberhumer.com/opensource/lzo/
 26  */
 27 
 28 
 29 #include <stdio.h>
 30 #include <stdlib.h>
 31 
 32 
 33 /*************************************************************************
 34 // This program shows the basic usage of the LZO library.
 35 // We will compress a block of data and decompress again.
 36 //
 37 // For more information, documentation, example programs and other support
 38 // files (like Makefiles and build scripts) please download the full LZO
 39 // package from
 40 //    http://www.oberhumer.com/opensource/lzo/
 41 **************************************************************************/
 42 
 43 /* First let's include "minizo.h". */
 44 
 45 #include "minilzo.h"
 46 
 47 
 48 /* We want to compress the data block at 'in' with length 'IN_LEN' to
 49  * the block at 'out'. Because the input block may be incompressible,
 50  * we must provide a little more output space in case that compression
 51  * is not possible.
 52  */
 53 
 54 #define IN_LEN      (128*1024ul)
 55 #define OUT_LEN     (IN_LEN + IN_LEN / 16 + 64 + 3)
 56 
 57 static unsigned char __LZO_MMODEL in  [ IN_LEN ];
 58 static unsigned char __LZO_MMODEL out [ OUT_LEN ];
 59 
 60 
 61 /* Work-memory needed for compression. Allocate memory in units
 62  * of 'lzo_align_t' (instead of 'char') to make sure it is properly aligned.
 63  */
 64 
 65 #define HEAP_ALLOC(var,size) \
 66     lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
 67 
 68 static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
 69 
 70 
 71 /*************************************************************************
 72 //
 73 **************************************************************************/
 74 
 75 int main(int argc, char *argv[])
 76 {
 77     int r;
 78     lzo_uint in_len;
 79     lzo_uint out_len;
 80     lzo_uint new_len;
 81 
 82     if (argc < 0 && argv == NULL)   /* avoid warning about unused args */
 83         return 0;
 84 
 85     printf("\nLZO real-time data compression library (v%s, %s).\n",
 86            lzo_version_string(), lzo_version_date());
 87     printf("Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
 88 
 89 
 90 /*
 91  * Step 1: initialize the LZO library
 92  */
 93     if (lzo_init() != LZO_E_OK)
 94     {
 95         printf("internal error - lzo_init() failed !!!\n");
 96         printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
 97         return 3;
 98     }
 99 
100 /*
101  * Step 2: prepare the input block that will get compressed.
102  *         We just fill it with zeros in this example program,
103  *         but you would use your real-world data here.
104  */
105     in_len = IN_LEN;
106     lzo_memset(in,0,in_len);
107 
108 /*
109  * Step 3: compress from 'in' to 'out' with LZO1X-1
110  */
111     r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);
112     if (r == LZO_E_OK)
113         printf("compressed %lu bytes into %lu bytes\n",
114             (unsigned long) in_len, (unsigned long) out_len);
115     else
116     {
117         /* this should NEVER happen */
118         printf("internal error - compression failed: %d\n", r);
119         return 2;
120     }
121     /* check for an incompressible block */
122     if (out_len >= in_len)
123     {
124         printf("This block contains incompressible data.\n");
125         return 0;
126     }
127 
128 /*
129  * Step 4: decompress again, now going from 'out' to 'in'
130  */
131     new_len = in_len;
132     r = lzo1x_decompress(out,out_len,in,&new_len,NULL);
133     if (r == LZO_E_OK && new_len == in_len)
134         printf("decompressed %lu bytes back into %lu bytes\n",
135             (unsigned long) out_len, (unsigned long) in_len);
136     else
137     {
138         /* this should NEVER happen */
139         printf("internal error - decompression failed: %d\n", r);
140         return 1;
141     }
142 
143     printf("\nminiLZO simple compression test passed.\n");
144     return 0;
145 }
146 
147 
148 /* vim:set ts=4 sw=4 et: */
testmini.c

首先需要准备好数组,然后知道长度。

第一步:初始化LZO库

    if (lzo_init() != LZO_E_OK) {
        ...
    }
lzo_init

 

第二步:清空数组,准备存放的缓存

    in_len = IN_LEN;
    lzo_memset(in,0,in_len);
lzo_memset

 

第三步:压缩后数据放入缓存中

    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;
    }
lzo1x_1_compress

 

posted @ 2019-11-27 14:48  习惯就好233  阅读(745)  评论(0编辑  收藏  举报