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_mzlib.h"
24 
25 #ifdef HAVE_LIBZ
26 #include <stdio.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36 #ifdef HAVE_ZLIB_H
37 #include <zlib.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=5;
53 uint32_t DC_TYPE=DACT_MOD_TYPE_COMP;
54 void *DC_ALGO=comp_mzlib_algo;
55 char *DC_NAME="Modifed Zlib Compression (MOD)";
56 #endif
57 
comp_mzlib_algo(int mode,unsigned char * prev_block,unsigned char * curr_block,unsigned char * out_block,int blk_size,int bufsize)58 int comp_mzlib_algo(int mode, unsigned char *prev_block, unsigned char *curr_block, unsigned char *out_block, int blk_size, int bufsize) {
59 	switch(mode) {
60 		case DACT_MODE_COMPR:
61 			return(comp_mzlib_compress(prev_block, curr_block, out_block, blk_size, bufsize));
62 			break; /* Heh */
63 		case DACT_MODE_DECMP:
64 			return(comp_mzlib_decompress(prev_block, curr_block, out_block, blk_size, bufsize));
65 			break;
66 		default:
67 			printf("Unsupported mode: %i\n", mode);
68 			return(-1);
69 	}
70 }
71 
comp_mzlib_compress(unsigned char * prev_block,unsigned char * curr_block,unsigned char * out_block,int blk_size,int bufsize)72 int comp_mzlib_compress(unsigned char *prev_block, unsigned char *curr_block, unsigned char *out_block, int blk_size, int bufsize) {
73 	unsigned long dest_size;
74 	unsigned char *tmp_block;
75 	int retval;
76 	int i;
77 
78 	dest_size=(int) ((float) (blk_size*1.02)+15);
79 
80 	if ((tmp_block=malloc(dest_size))==NULL) {
81 		return(-1);
82 	}
83 	memcpy(tmp_block,curr_block,blk_size);
84 
85 	for (i=0;i<blk_size;i++) {
86 /*
87 		tmp_block[i]=( \
88 			(  (tmp_block[i]&0x80) >> 7 ) + \
89 			(  (tmp_block[i]&0x20) >> 4 ) + \
90 			(  (tmp_block[i]&0x08) >> 1 ) + \
91 			(  (tmp_block[i]&0x02) << 2 ) + \
92 			(  (tmp_block[i]&0x40) >> 2 ) + \
93 			(  (tmp_block[i]&0x10) << 1 ) + \
94 			(  (tmp_block[i]&0x04) << 4 ) + \
95 			(  (tmp_block[i]&0x01) << 7 ) \
96 		);
97 */
98 		tmp_block[i]=( ((tmp_block[i]&0xf0)>>4) + (  (tmp_block[i]&0x0f)  <<4) );
99 	}
100 #ifdef HAVE_COMPRESS2
101 	retval=compress2(out_block, &dest_size, tmp_block, blk_size, Z_BEST_COMPRESSION);
102 #else
103 	retval=compress(out_block, &dest_size, tmp_block, blk_size);
104 #endif
105 	if (retval!=Z_OK) {
106 		return(-1);
107 	}
108 	free(tmp_block);
109 	return(dest_size);
110 }
111 
comp_mzlib_decompress(unsigned char * prev_block,unsigned char * curr_block,unsigned char * out_block,int blk_size,int bufsize)112 int comp_mzlib_decompress(unsigned char *prev_block, unsigned char *curr_block, unsigned char *out_block, int blk_size, int bufsize) {
113 	unsigned long dest_size=(blk_size*2);
114 	int retval;
115 	int i;
116 
117 	retval=uncompress(out_block,&dest_size,curr_block,blk_size);
118 
119 	if (retval!=Z_OK) {
120 		return(0);
121 	}
122 
123 	for (i=0;i<dest_size;i++) {
124 		out_block[i]=( ( (out_block[i]&0xf0)>>4) + ((out_block[i]&0x0f)<<4) );
125 	}
126 
127 	return(dest_size);
128 }
129 #endif
130