Blame examples/dict.c

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