"Fossies" - the Fresh Open Source Software Archive

Member "unipkg-0.6.5/compression.c" (16 Dec 2005, 8505 Bytes) of package /linux/privat/old/unipkg-0.6.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.


    1 /******************************************************************************\
    2 *                                                                              *
    3 * UniPKG (c) iSteve <isteve@bofh.cz>, 2005                                     *
    4 *                                                                              *
    5 * Universal PacKaGer.                                                          *
    6 * Licensed under GNU/GPL - if you don't like the software, fix it!             *
    7 *                                                                              *
    8 \******************************************************************************/
    9 
   10 #define _GNU_SOURCE
   11 #include <stdlib.h>
   12 #include <stdio.h>
   13 #include <string.h>
   14 #include <unistd.h>
   15 #include <fcntl.h>
   16 
   17 #include <ctype.h>
   18 #include <libgen.h>
   19 
   20 #include <sys/types.h>
   21 #include <sys/mman.h>
   22 #include <sys/stat.h>
   23 
   24 #ifdef HAVE_SYSMACROS_H
   25 #include <sys/sysmacros.h>
   26 #endif
   27 
   28 #include <errno.h>
   29 #include "compression.h"
   30 #include "config.h"
   31 
   32 #ifdef HAVE_LIBZ
   33  #include <zlib.h>
   34 #endif
   35 #ifdef HAVE_LIBBZ2
   36  #include <bzlib.h>
   37 #endif
   38 #ifdef HAVE_LIBLZMADEC
   39  #include <lzmadec.h>
   40 #endif
   41 
   42 int decompression_setup(c_compdata *cdata, void *filemap, unsigned long offset, unsigned long inblocksize, char *obuf, unsigned long outblocksize, int ctype) { 
   43     cdata->c_type = ctype;
   44     switch (cdata->c_type) {
   45         case C_GZIP:
   46             #ifdef HAVE_LIBZ
   47             cdata->cstream = malloc(sizeof(z_stream));
   48             ((z_stream*)(cdata->cstream))->zalloc = (alloc_func)NULL;
   49             ((z_stream*)(cdata->cstream))->zfree = (free_func)NULL;
   50             ((z_stream*)(cdata->cstream))->next_in = (Bytef*)(filemap + offset);
   51             ((z_stream*)(cdata->cstream))->next_out = (Bytef*)obuf;
   52             ((z_stream*)(cdata->cstream))->avail_in = inblocksize;
   53             ((z_stream*)(cdata->cstream))->avail_out = outblocksize;
   54             break;
   55             #else
   56             #warning The zlib is mandatory.
   57             return 1;
   58             #endif
   59         
   60         case C_BZIP2:
   61             #ifdef HAVE_LIBBZ2
   62             cdata->cstream = malloc(sizeof(bz_stream));
   63             ((bz_stream*)(cdata->cstream))->bzalloc = NULL;
   64             ((bz_stream*)(cdata->cstream))->bzfree = NULL;
   65             ((bz_stream*)(cdata->cstream))->next_in = filemap + offset;
   66             ((bz_stream*)(cdata->cstream))->next_out = obuf;
   67             ((bz_stream*)(cdata->cstream))->avail_in = inblocksize;
   68             ((bz_stream*)(cdata->cstream))->avail_out = outblocksize;
   69             break;
   70             #else
   71             return 1;
   72             #endif
   73 
   74         case C_LZMA:
   75             #ifdef HAVE_LIBLZMADEC
   76             cdata->cstream = malloc(sizeof(lzmadec_stream));
   77             ((lzmadec_stream*)(cdata->cstream))->lzma_alloc = NULL;
   78             ((lzmadec_stream*)(cdata->cstream))->lzma_free = NULL;
   79             ((lzmadec_stream*)(cdata->cstream))->next_in = (uint8_t*)(filemap + offset);
   80             ((lzmadec_stream*)(cdata->cstream))->next_out = (uint8_t*)obuf;
   81             ((lzmadec_stream*)(cdata->cstream))->avail_in = inblocksize;
   82             ((lzmadec_stream*)(cdata->cstream))->avail_out = outblocksize;
   83             break;
   84             #else
   85             return 1;
   86             #endif
   87 
   88         case C_NONE:
   89             cdata->cstream = malloc(sizeof(c_nocompression));
   90             ((c_nocompression*)(cdata->cstream))->src = filemap + offset;
   91             ((c_nocompression*)(cdata->cstream))->obuf = obuf;
   92             ((c_nocompression*)(cdata->cstream))->outblocksize = outblocksize;
   93             ((c_nocompression*)(cdata->cstream))->inblocksize = inblocksize;
   94             ((c_nocompression*)(cdata->cstream))->finished = 0;
   95             ((c_nocompression*)(cdata->cstream))->lastoutput = 0;
   96             break;
   97     }
   98     return 0;
   99 }
  100 
  101 int decompression_init(c_compdata *cdata) {
  102     switch (cdata->c_type) {
  103         case C_GZIP:
  104             #ifdef HAVE_LIBZ
  105             return (inflateInit2((z_stream*)(cdata->cstream), 31) == Z_OK) ? CE_OK : CE_CERROR;
  106             #else
  107             return 1;
  108             #endif
  109         case C_BZIP2:
  110             #ifdef HAVE_LIBBZ2
  111             return (BZ2_bzDecompressInit((bz_stream*)(cdata->cstream), 0, 0) == BZ_OK) ? CE_OK : CE_CERROR;
  112             #else
  113             return 1;
  114             #endif
  115         case C_LZMA:
  116             #ifdef HAVE_LIBLZMADEC
  117             return (lzmadec_init((lzmadec_stream*)(cdata->cstream)) == LZMADEC_OK) ? CE_OK : CE_CERROR;
  118             #else
  119             return 1;
  120             #endif
  121         case C_NONE:
  122             break;
  123     }
  124     return 0;
  125 }
  126 
  127 int decompression_decompress(c_compdata *cdata, unsigned long outblocksize, char *obuf) {
  128     switch (cdata->c_type) {
  129         case C_GZIP:
  130             #ifdef HAVE_LIBZ
  131             if (obuf != NULL) 
  132                 ((z_stream*)(cdata->cstream))->next_out = (Bytef*)obuf;
  133             
  134             if (outblocksize != 0) 
  135                 ((z_stream*)(cdata->cstream))->avail_out = outblocksize;
  136             
  137             return (inflate((z_stream*)(cdata->cstream), Z_NO_FLUSH) == Z_OK) ? CE_OK : CE_CERROR;
  138             #else
  139             return 1;
  140             #endif
  141         case C_BZIP2:
  142             #ifdef HAVE_LIBBZ2
  143             if (obuf != NULL) 
  144                 ((bz_stream*)(cdata->cstream))->next_out = obuf;
  145             
  146             if (outblocksize != 0) 
  147                 ((bz_stream*)(cdata->cstream))->avail_out = outblocksize;
  148         
  149             return (BZ2_bzDecompress((bz_stream*)(cdata->cstream)) == BZ_OK) ? CE_OK : CE_CERROR;
  150             #else
  151             return 1;
  152             #endif
  153         case C_LZMA:
  154             #ifdef HAVE_LIBLZMADEC
  155             if (obuf != NULL) 
  156                 ((lzmadec_stream*)(cdata->cstream))->next_out = (uint8_t*)obuf;
  157             
  158             if (outblocksize != 0) 
  159                 ((lzmadec_stream*)(cdata->cstream))->avail_out = outblocksize;
  160             
  161             return (lzmadec_decode((lzmadec_stream*)(cdata->cstream), ((lzmadec_stream*)(cdata->cstream))->avail_in == 0) == LZMADEC_OK) ? CE_OK : CE_CERROR;
  162             #else
  163             return 1;
  164             #endif
  165         case C_NONE:
  166             if (obuf != NULL) 
  167                 ((c_nocompression*)(cdata->cstream))->obuf = obuf;
  168             
  169             if (outblocksize != 0) 
  170                 ((c_nocompression*)(cdata->cstream))->outblocksize = outblocksize;
  171             
  172             ((c_nocompression*)(cdata->cstream))->lastoutput = (((c_nocompression*)(cdata->cstream))->inblocksize - ((c_nocompression*)(cdata->cstream))->finished > ((c_nocompression*)(cdata->cstream))->outblocksize) ? ((c_nocompression*)(cdata->cstream))->outblocksize : ((c_nocompression*)(cdata->cstream))->inblocksize - ((c_nocompression*)(cdata->cstream))->finished;
  173             memcpy(((c_nocompression*)(cdata->cstream))->obuf, ((c_nocompression*)(cdata->cstream))->src + ((c_nocompression*)(cdata->cstream))->finished, ((c_nocompression*)(cdata->cstream))->lastoutput);
  174             ((c_nocompression*)(cdata->cstream))->finished += ((c_nocompression*)(cdata->cstream))->lastoutput;
  175             break;
  176     }
  177     return 0;
  178 }
  179 
  180 int decompression_ammount_left(c_compdata *cdata, unsigned long ammount) {
  181     switch (cdata->c_type) {
  182         case C_GZIP:
  183             #ifdef HAVE_LIBZ
  184             if (ammount == 0) {
  185                 if (((z_stream*)(cdata->cstream))->avail_in > 0)
  186                     return 1;
  187             } else {
  188                 if (((z_stream*)(cdata->cstream))->avail_in >= ammount)
  189                     return 1;
  190             }
  191             break;
  192             #else
  193             return 0;
  194             #endif
  195         case C_BZIP2:
  196             #ifdef HAVE_LIBBZ2
  197             if (ammount == 0) {
  198                 if (((bz_stream*)(cdata->cstream))->avail_in > 0)
  199                     return 1;
  200             } else {
  201                 if (((bz_stream*)(cdata->cstream))->avail_in >= ammount)
  202                     return 1;
  203             }
  204             break;
  205             #else
  206             return 0;
  207             #endif
  208         case C_LZMA:
  209             #ifdef HAVE_LIBLZMADEC
  210             if (ammount == 0) {
  211                 if (((lzmadec_stream*)(cdata->cstream))->avail_in > 0)
  212                     return 1;
  213             } else {
  214                 if (((lzmadec_stream*)(cdata->cstream))->avail_in >= ammount)
  215                     return 1;
  216             }
  217             break;
  218             #else
  219             return 0;
  220             #endif
  221         
  222         case C_NONE:
  223             if (ammount == 0) {
  224                 if (((c_nocompression*)(cdata->cstream))->inblocksize - ((c_nocompression*)(cdata->cstream))->finished > 0)
  225                     return 1;
  226             } else {
  227                 if (((c_nocompression*)(cdata->cstream))->inblocksize - ((c_nocompression*)(cdata->cstream))->finished >= ammount)
  228                     return 1;
  229             }
  230             break;
  231     }
  232     return 0;
  233 }
  234 
  235 unsigned long decompression_ammount_not_processed(c_compdata *cdata) {
  236     switch (cdata->c_type) {
  237         case C_GZIP:
  238             #ifdef HAVE_LIBZ
  239             return ((z_stream*)(cdata->cstream))->avail_out;
  240             #else
  241             return 0;
  242             #endif
  243         case C_BZIP2:
  244             #ifdef HAVE_LIBBZ2
  245             return ((bz_stream*)(cdata->cstream))->avail_out;
  246             #else
  247             return 0;
  248             #endif
  249         case C_LZMA:
  250             #ifdef HAVE_LIBLZMADEC
  251             return ((lzmadec_stream*)(cdata->cstream))->avail_out;
  252             #else
  253             return 0;
  254             #endif
  255         case C_NONE:
  256             return ((c_nocompression*)(cdata->cstream))->outblocksize - ((c_nocompression*)(cdata->cstream))->lastoutput;
  257             break;
  258     }
  259     return 0;
  260 }
  261 
  262 int decompression_cleanup(c_compdata *cdata) {
  263     switch (cdata->c_type) {
  264         case C_GZIP:
  265             #ifdef HAVE_LIBZ
  266             inflateEnd((z_stream*)(cdata->cstream));
  267             free(cdata->cstream);
  268             break;
  269             #else
  270             return 1;
  271             #endif
  272         case C_BZIP2:
  273             #ifdef HAVE_LIBBZ2
  274             BZ2_bzDecompressEnd((bz_stream*)(cdata->cstream));
  275             free(cdata->cstream);
  276             break;
  277             #else
  278             return 1;
  279             #endif
  280         case C_LZMA:
  281             #ifdef HAVE_LIBLZMADEC
  282             lzmadec_end((lzmadec_stream*)(cdata->cstream));
  283             free(cdata->cstream);
  284             break;
  285             #else
  286             return 1;
  287             #endif
  288         case C_NONE:
  289             free(cdata->cstream);
  290             break;
  291     }
  292     return 0;
  293 }