kusano 7d535a
/* dict.c -- example program: how to use preset dictionaries
kusano 7d535a
kusano 7d535a
   This file is part of the LZO real-time data compression library.
kusano 7d535a
kusano 7d535a
   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   All Rights Reserved.
kusano 7d535a
kusano 7d535a
   The LZO library is free software; you can redistribute it and/or
kusano 7d535a
   modify it under the terms of the GNU General Public License as
kusano 7d535a
   published by the Free Software Foundation; either version 2 of
kusano 7d535a
   the License, or (at your option) any later version.
kusano 7d535a
kusano 7d535a
   The LZO library is distributed in the hope that it will be useful,
kusano 7d535a
   but WITHOUT ANY WARRANTY; without even the implied warranty of
kusano 7d535a
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
kusano 7d535a
   GNU General Public License for more details.
kusano 7d535a
kusano 7d535a
   You should have received a copy of the GNU General Public License
kusano 7d535a
   along with the LZO library; see the file COPYING.
kusano 7d535a
   If not, write to the Free Software Foundation, Inc.,
kusano 7d535a
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
kusano 7d535a
kusano 7d535a
   Markus F.X.J. Oberhumer
kusano 7d535a
   <markus@oberhumer.com></markus@oberhumer.com>
kusano 7d535a
   http://www.oberhumer.com/opensource/lzo/
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
// This program shows how to use preset dictionaries.
kusano 7d535a
//
kusano 7d535a
// Please study LZO.FAQ and simple.c first.
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
#include "lzo/lzoconf.h"
kusano 7d535a
#include "lzo/lzo1x.h"
kusano 7d535a
kusano 7d535a
/* portability layer */
kusano 7d535a
#define WANT_LZO_MALLOC 1
kusano 7d535a
#define WANT_LZO_FREAD 1
kusano 7d535a
#define WANT_LZO_WILDARGV 1
kusano 7d535a
#include "examples/portab.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
static const char *progname = NULL;
kusano 7d535a
kusano 7d535a
#define DICT_LEN    0xbfff
kusano 7d535a
static lzo_bytep    dict;
kusano 7d535a
static lzo_uint     dict_len = 0;
kusano 7d535a
static lzo_uint32   dict_adler32;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
//
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
static lzo_uint total_n = 0;
kusano 7d535a
static lzo_uint total_c_len = 0;
kusano 7d535a
static lzo_uint total_d_len = 0;
kusano 7d535a
kusano 7d535a
static void print_file ( const char *name, lzo_uint d_len, lzo_uint c_len )
kusano 7d535a
{
kusano 7d535a
    double perc;
kusano 7d535a
kusano 7d535a
    perc = (d_len > 0) ? c_len * 100.0 / d_len : 0;
kusano 7d535a
    printf("  |  %-30s   %9ld -> %9ld   %7.2f%%  |\n",
kusano 7d535a
            name, (long) d_len, (long) c_len, perc);
kusano 7d535a
kusano 7d535a
    total_n++;
kusano 7d535a
    total_c_len += c_len;
kusano 7d535a
    total_d_len += d_len;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
//
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
int do_file ( const char *in_name, int level )
kusano 7d535a
{
kusano 7d535a
    int r;
kusano 7d535a
    lzo_bytep in;
kusano 7d535a
    lzo_bytep out;
kusano 7d535a
    lzo_bytep newb;
kusano 7d535a
    lzo_bytep wrkmem;
kusano 7d535a
    lzo_uint in_len;
kusano 7d535a
    lzo_uint out_len;
kusano 7d535a
    lzo_uint new_len;
kusano 7d535a
    long l;
kusano 7d535a
    FILE *f;
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 1: open the input file
kusano 7d535a
 */
kusano 7d535a
    f = fopen(in_name,"rb");
kusano 7d535a
    if (f == 0)
kusano 7d535a
    {
kusano 7d535a
        printf("%s: cannot open file %s\n", progname, in_name);
kusano 7d535a
        return 0;   /* no error */
kusano 7d535a
    }
kusano 7d535a
    fseek(f,0,SEEK_END);
kusano 7d535a
    l = ftell(f);
kusano 7d535a
    fseek(f,0,SEEK_SET);
kusano 7d535a
    if (l <= 0)
kusano 7d535a
    {
kusano 7d535a
        printf("%s: %s: empty file -- skipping\n", progname, in_name);
kusano 7d535a
        fclose(f);
kusano 7d535a
        return 0;
kusano 7d535a
    }
kusano 7d535a
    in_len = (lzo_uint) l;
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 2: allocate compression buffers and read the file
kusano 7d535a
 */
kusano 7d535a
    in = (lzo_bytep) lzo_malloc(in_len);
kusano 7d535a
    out = (lzo_bytep) lzo_malloc(in_len + in_len / 16 + 64 + 3);
kusano 7d535a
    newb = (lzo_bytep) lzo_malloc(in_len);
kusano 7d535a
    wrkmem = (lzo_bytep) lzo_malloc(LZO1X_999_MEM_COMPRESS);
kusano 7d535a
    if (in == NULL || out == NULL || newb == NULL || wrkmem == NULL)
kusano 7d535a
    {
kusano 7d535a
        printf("%s: out of memory\n", progname);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
    in_len = (lzo_uint) lzo_fread(f,in,in_len);
kusano 7d535a
    fclose(f);
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 3: compress from `in' to `out' with LZO1X-999
kusano 7d535a
 */
kusano 7d535a
    r = lzo1x_999_compress_level(in,in_len,out,&out_len,wrkmem,
kusano 7d535a
                                 dict, dict_len, 0, level);
kusano 7d535a
    if (r != LZO_E_OK)
kusano 7d535a
    {
kusano 7d535a
        /* this should NEVER happen */
kusano 7d535a
        printf("internal error - compression failed: %d\n", r);
kusano 7d535a
        return 1;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    print_file(in_name,in_len,out_len);
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 4: decompress again, now going from `out' to `newb'
kusano 7d535a
 */
kusano 7d535a
    new_len = in_len;
kusano 7d535a
    r = lzo1x_decompress_dict_safe(out,out_len,newb,&new_len,NULL,dict,dict_len);
kusano 7d535a
    if (r != LZO_E_OK)
kusano 7d535a
    {
kusano 7d535a
        /* this should NEVER happen */
kusano 7d535a
        printf("internal error - decompression failed: %d\n", r);
kusano 7d535a
        return 1;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 5: verify decompression
kusano 7d535a
 */
kusano 7d535a
    if (new_len != in_len || lzo_memcmp(in,newb,in_len) != 0)
kusano 7d535a
    {
kusano 7d535a
        /* this should NEVER happen */
kusano 7d535a
        printf("internal error - decompression data error\n");
kusano 7d535a
        return 1;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    /* free buffers in reverse order to help malloc() */
kusano 7d535a
    lzo_free(wrkmem);
kusano 7d535a
    lzo_free(newb);
kusano 7d535a
    lzo_free(out);
kusano 7d535a
    lzo_free(in);
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
//
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
int __lzo_cdecl_main main(int argc, char *argv[])
kusano 7d535a
{
kusano 7d535a
    int i = 1;
kusano 7d535a
    int r;
kusano 7d535a
    const char *dict_name;
kusano 7d535a
    FILE *f;
kusano 7d535a
    time_t t_total;
kusano 7d535a
    int level = 7;
kusano 7d535a
kusano 7d535a
    lzo_wildargv(&argc, &argv);
kusano 7d535a
kusano 7d535a
    printf("\nLZO real-time data compression library (v%s, %s).\n",
kusano 7d535a
           lzo_version_string(), lzo_version_date());
kusano 7d535a
    printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
kusano 7d535a
kusano 7d535a
    progname = argv[0];
kusano 7d535a
kusano 7d535a
    if (i < argc && argv[i][0] == '-' && isdigit(argv[i][1]))
kusano 7d535a
        level = atoi(&argv[i++][1]);
kusano 7d535a
kusano 7d535a
    if (i + 1 >= argc || level < 1 || level > 9)
kusano 7d535a
    {
kusano 7d535a
        printf("usage: %s [-level] [ dictionary-file | -n ]  file...\n", progname);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
    printf("Compression level is LZO1X-999/%d\n", level);
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 1: initialize the LZO library
kusano 7d535a
 */
kusano 7d535a
    if (lzo_init() != LZO_E_OK)
kusano 7d535a
    {
kusano 7d535a
        printf("internal error - lzo_init() failed !!!\n");
kusano 7d535a
        printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n");
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 2: prepare the dictionary
kusano 7d535a
 */
kusano 7d535a
    dict = (lzo_bytep) lzo_malloc(DICT_LEN);
kusano 7d535a
    if (dict == NULL)
kusano 7d535a
    {
kusano 7d535a
        printf("%s: out of memory\n", progname);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
    dict_name = argv[i++];
kusano 7d535a
    if (strcmp(dict_name,"-n") == 0)
kusano 7d535a
    {
kusano 7d535a
        dict_name = "empty";
kusano 7d535a
        dict_len = 0;
kusano 7d535a
    }
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        f = fopen(dict_name,"rb");
kusano 7d535a
        if (!f)
kusano 7d535a
        {
kusano 7d535a
            printf("%s: cannot open dictionary file %s\n", progname, dict_name);
kusano 7d535a
            exit(1);
kusano 7d535a
        }
kusano 7d535a
        dict_len = (lzo_uint) lzo_fread(f,dict,DICT_LEN);
kusano 7d535a
        fclose(f);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    dict_adler32 = lzo_adler32(0,NULL,0);
kusano 7d535a
    dict_adler32 = lzo_adler32(dict_adler32,dict,dict_len);
kusano 7d535a
    printf("Using dictionary '%s', %ld bytes, ID 0x%08lx.\n",
kusano 7d535a
            dict_name, (long) dict_len, (long) dict_adler32);
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 3: process files
kusano 7d535a
 */
kusano 7d535a
    t_total = time(NULL);
kusano 7d535a
    for (r = 0; r == 0 && i < argc; i++)
kusano 7d535a
        r = do_file(argv[i], level);
kusano 7d535a
    t_total = time(NULL) - t_total;
kusano 7d535a
kusano 7d535a
    lzo_free(dict);
kusano 7d535a
kusano 7d535a
    if (total_n > 1)
kusano 7d535a
        print_file("***TOTALS***",total_d_len,total_c_len);
kusano 7d535a
kusano 7d535a
    printf("Dictionary compression test %s, execution time %lu seconds.\n",
kusano 7d535a
            r == 0 ? "passed" : "FAILED", (unsigned long) t_total);
kusano 7d535a
    return r;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/
kusano 7d535a