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_textrle.h"
24 #include <stdio.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 
32 
33 /*
34 	mode 		- DACT_MODE_COMPR or DACT_MODE_DECMP
35 			    Determine whether to compress or decompress.
36 	prev_block	- Previous (uncompressed) block.
37 	curr_block	- The data to be compressed.
38 	out_block	- Where to put data after compression.
39 	blk_size	- Size of prev_block and curr_block.
40 */
41 
42 #if defined(AS_MODULE) && defined(USE_MODULES)
43 #include "module.h"
44 uint32_t DC_NUM=12;
45 uint32_t DC_TYPE=DACT_MOD_TYPE_COMP;
46 void *DC_ALGO=comp_textrle_algo;
47 char *DC_NAME="Text RLE Compression (MOD)";
48 #endif
49 
comp_textrle_algo(int mode,unsigned char * prev_block,unsigned char * curr_block,char * out_block,int blk_size,int bufsize)50 int comp_textrle_algo(int mode, unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int bufsize) {
51 	switch(mode) {
52 		case DACT_MODE_COMPR:
53 			return(comp_textrle_compress(prev_block, curr_block, out_block, blk_size, bufsize));
54 			break; /* Heh */
55 		case DACT_MODE_DECMP:
56 			return(comp_textrle_decompress(prev_block, curr_block, out_block, blk_size, bufsize));
57 			break;
58 		default:
59 			printf("Unsupported mode: %i\n", mode);
60 			return(-1);
61 	}
62 }
63 
comp_textrle_compress(unsigned char * prev_block,unsigned char * curr_block,char * out_block,int blk_size,int bufsize)64 int comp_textrle_compress(unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int bufsize) {
65 	int i,x=0,m;
66 	unsigned char sentinel=0xff;
67 	unsigned char currchar=0, prevchar;
68 	unsigned char charcnt=0;
69 	unsigned int lowestcnt=0xffff;
70 	unsigned int count[256];
71 
72 	for (i=0;i<256;i++) count[i]=0;
73 
74 	for (i=0;i<blk_size;i++) count[curr_block[i]]++;
75 
76 	for (i=0;i<256;i++) {
77 		if (count[i]<lowestcnt) {
78 			sentinel=i;
79 			lowestcnt=count[i];
80 		}
81 	}
82 
83 	out_block[x++]=sentinel;
84 
85 
86 
87 	return(x);
88 }
89 
comp_textrle_decompress(unsigned char * prev_block,unsigned char * curr_block,char * out_block,int blk_size,int bufsize)90 int comp_textrle_decompress(unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int bufsize) {
91 	int i,x=0,m;
92 	unsigned char sentinel=curr_block[0];
93 	unsigned char currchar;
94 	unsigned char charcnt;
95 
96 	for (i=1;i<blk_size;i++) {
97 		currchar=curr_block[i];
98 		if (currchar==sentinel) {
99 			currchar=curr_block[++i];
100 			charcnt=curr_block[++i];
101 			if ((x+charcnt)>bufsize) {
102 				printf("Error in RLE compression!\n");
103 				return(0);
104 			}
105 			for (m=0;m<charcnt;m++) out_block[x++]=currchar;
106 		} else {
107 			out_block[x++]=currchar;
108 		}
109 	}
110 	return(x);
111 }
112