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