4581470 [rkeene@sledge /home/rkeene/devel/dact]$ cat -n comp_zlib.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 /*
 23     Example block compression routine for interfacing with DACT.
 24 
 25     Uses zlib.
 26 */
 27 
 28 
 29 #include "dact.h"
 30 #include "comp_zlib.h"
 31 
 32 #ifdef HAVE_LIBZ
 33 #include <stdio.h>
 34 #ifdef HAVE_UNISTD_H
 35 #include <unistd.h>
 36 #endif
 37 #ifdef HAVE_STDLIB_H
 38 #include <stdlib.h>
 39 #endif
 40 #ifdef HAVE_STRING_H
 41 #include <string.h>
 42 #endif
 43 #ifdef HAVE_ZLIB_H
 44 #include <zlib.h>
 45 #endif
 46 
 47 
 48 /*
 49     mode        - DACT_MODE_COMPR or DACT_MODE_DECMP
 50                 Determine whether to compress or decompress.
 51     prev_block  - Previous (uncompressed) block.
 52     curr_block  - The data to be compressed.
 53     out_block   - Where to put data after compression.
 54     blk_size    - Size of prev_block and curr_block.
 55 */
 56 
 57 #if defined(USE_MODULES) && defined(AS_MODULE)
 58 #include "module.h"
 59 uint32_t DC_NUM=4;
 60 uint32_t DC_TYPE=DACT_MOD_TYPE_COMP;
 61 void *DC_ALGO=comp_zlib_algo;
 62 char *DC_NAME="Zlib Compression (MOD)";
 63 #endif
 64 
 65 int comp_zlib_algo(int mode, unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int
	bufsize) {
 66     switch(mode) {
 67         case DACT_MODE_COMPR:
 68             return(comp_zlib_compress(prev_block, curr_block, out_block, blk_size, bufsize));
 69             break; /* Heh */
 70         case DACT_MODE_DECMP:
 71             return(comp_zlib_decompress(prev_block, curr_block, out_block, blk_size, bufsize));
 72             break;
 73         default:
 74             printf("Unsupported mode: %i\n", mode);
 75             return(-1);
 76     }
 77 }
 78 
 79 int comp_zlib_compress(unsigned char *prev_block, unsigned char *curr_block, unsigned char *out_block, int blk_size, int
	bufsize) {
 80     unsigned long dest_size;
 81     int retval;
 82     dest_size=(int) ((float) (blk_size*1.01)+13);
 83 
 84 #ifdef HAVE_COMPRESS2
 85     retval=compress2(out_block, &dest_size, curr_block, blk_size, Z_BEST_COMPRESSION);
 86 #else
 87     retval=compress(out_block, &dest_size, curr_block, blk_size);
 88 #endif
 89     if (retval!=Z_OK) return(-1);
 90 /* Remove the 120,218 header */
 91     dest_size-=2;
 92     memmove(out_block, out_block+2, dest_size);
 93 
 94     return(dest_size);
 95 }
 96 
 97 int comp_zlib_decompress(unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int
	bufsize) {
 98     unsigned long dest_size;
 99     unsigned char *tmpbuf;
100     int retval;
101 
102 /* Replant the header. */
103     tmpbuf=malloc(blk_size+2);
104     tmpbuf[0]=120;
105     tmpbuf[1]=218;
106     memcpy(tmpbuf+2, curr_block, blk_size);
107 
108     dest_size=bufsize;
109     retval=uncompress(out_block,&dest_size,tmpbuf,blk_size+2);
110 
111     free(tmpbuf);
112 
113     if (retval!=Z_OK) return(0);
114 
115     return(dest_size);
116 }
117 #endif
4581471 [rkeene@sledge /home/rkeene/devel/dact]$

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