Blame examples/dict.c

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