4556420 [rkeene@sledge /home/rkeene/devel/dact-0.8.37]$ cat -n comp_bzlib.c
  1 /*
  2  * Copyright (C) 2001, 2002, and 2003  Roy Keene
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, write to the Free Software
 16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 17  *
 18  *      email: dact@rkeene.org
 19  */
 20 
 21 
 22 #include "dact.h"
 23 #include "comp_bzlib.h"
 24 
 25 #ifdef HAVE_LIBBZ2
 26 #ifdef HAVE_UNISTD_H
 27 #include <unistd.h>
 28 #endif
 29 #ifdef HAVE_STDLIB_H
 30 #include <stdlib.h>
 31 #endif
 32 #ifdef HAVE_STRING_H
 33 #include <string.h>
 34 #endif
 35 #include <stdio.h>
 36 #ifdef HAVE_BZLIB_H
 37 #include <bzlib.h>
 38 #endif
 39 
 40 
 41 /*
 42     mode        - DACT_MODE_COMPR or DACT_MODE_DECMP
 43                 Determine whether to compress or decompress.
 44     prev_block  - Previous (uncompressed) block.
 45     curr_block  - The data to be compressed.
 46     out_block   - Where to put data after compression.
 47     blk_size    - Size of prev_block and curr_block.
 48 */
 49 
 50 #if defined(USE_MODULES) && defined(AS_MODULE)
 51 #include "module.h"
 52 uint32_t DC_NUM=9;
 53 uint32_t DC_TYPE=DACT_MOD_TYPE_COMP;
 54 void *DC_ALGO=comp_bzlib_algo;
 55 char *DC_NAME="Bzip2 Compression (MOD)";
 56 #endif
 57 
 58 int comp_bzlib_algo(int mode, unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int
	bufsize) {
 59     switch(mode) {
 60         case DACT_MODE_COMPR:
 61             return(comp_bzlib_compress(prev_block, curr_block, out_block, blk_size, bufsize));
 62             break; /* Heh */
 63         case DACT_MODE_DECMP:
 64             return(comp_bzlib_decompress(prev_block, curr_block, out_block, blk_size, bufsize));
 65             break;
 66         default:
 67             fprintf(stderr, "Unsupported mode: %i\n", mode);
 68             return(-1);
 69     }
 70 }
 71 
 72 int comp_bzlib_compress(unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int
	bufsize) {
 73     unsigned int dest_size=bufsize;
 74     int retval;
 75 
 76 #ifdef HAVE_OLD_BZ2
 77     retval=bzBuffToBuffCompress(out_block, &dest_size, curr_block, blk_size, 9, 0, 9);
 78 #else
 79     retval=BZ2_bzBuffToBuffCompress(out_block, &dest_size, curr_block, blk_size, 9, 0, 9);
 80 #endif
 81 /* Remove the "BZh9" header. */
 82     dest_size-=4;
 83     memmove(out_block, out_block+4, dest_size);
 84 
 85     if (retval!=BZ_OK) return(-1);
 86 
 87     return(dest_size);
 88 }
 89 
 90 int comp_bzlib_decompress(unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int
	bufsize) {
 91     unsigned int dest_size=bufsize;
 92     char *tmpbuf;
 93     int retval;
 94 
 95 /* Replant the header. */
 96     tmpbuf=malloc(blk_size+4);
 97     memcpy(tmpbuf, "BZh9", 4);
 98     memcpy(tmpbuf+4, curr_block, blk_size);
 99 
100 #ifdef HAVE_OLD_BZ2
101     retval=bzBuffToBuffDecompress(out_block, &dest_size, tmpbuf, blk_size+4, 0, 0);
102 #else
103     retval=BZ2_bzBuffToBuffDecompress(out_block, &dest_size, tmpbuf, blk_size+4, 0, 0);
104 #endif
105 
106     free(tmpbuf);
107 
108     if (retval!=BZ_OK) return(0);
109     return(dest_size);
110 }
111 #endif
4556421 [rkeene@sledge /home/rkeene/devel/dact-0.8.37]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 2004-04-04 07:01:49