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_rle.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=1;
45 uint32_t DC_TYPE=DACT_MOD_TYPE_COMP;
46 void *DC_ALGO=comp_rle_algo;
47 char *DC_NAME="RLE Compression (MOD)";
48 char *DC_SIGN="RGlnaXRhbFNpZ25hdHVyZQ==";
49 char *DC_URL_GET="http://www.rkeene.org/projects/compression/dact/@@OSNM@@-@@ARCH@@/comp_rle.dll";
50 char *DC_URL_VER="http://www.rkeene.org/projects/compression/dact/@@OSNM@@-@@ARCH@@/comp_rle.ver";
51 uint32_t DC_VER=0x00080d;
52 uint32_t DC_REQUIRE=DACT_MOD_REQ_ATLEAST|(0x00080d);
53 #endif
54
comp_rle_algo(int mode,unsigned char * prev_block,unsigned char * curr_block,char * out_block,int blk_size,int bufsize)55 int comp_rle_algo(int mode, unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int bufsize) {
56 switch(mode) {
57 case DACT_MODE_COMPR:
58 return(comp_rle_compress(prev_block, curr_block, out_block, blk_size, bufsize));
59 break; /* Heh */
60 case DACT_MODE_DECMP:
61 return(comp_rle_decompress(prev_block, curr_block, out_block, blk_size, bufsize));
62 break;
63 default:
64 printf("Unsupported mode: %i\n", mode);
65 return(-1);
66 }
67 }
68
comp_rle_compress(unsigned char * prev_block,unsigned char * curr_block,char * out_block,int blk_size,int bufsize)69 int comp_rle_compress(unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int bufsize) {
70 int i,x=0,m;
71 unsigned char sentinel=0xff;
72 unsigned char currchar=0, prevchar;
73 unsigned char charcnt=0;
74 unsigned int lowestcnt=0xffff;
75 unsigned int count[256];
76
77 for (i=0;i<256;i++) count[i]=0;
78
79 for (i=0;i<blk_size;i++) count[curr_block[i]]++;
80
81 for (i=0;i<256;i++) {
82 if (count[i]<lowestcnt) {
83 sentinel=i;
84 lowestcnt=count[i];
85 }
86 }
87
88 out_block[x++]=sentinel;
89
90 prevchar=curr_block[0];
91 for (i=0;i<(blk_size+1);i++) {
92 if (i!=blk_size) { currchar=curr_block[i]; } else { currchar++; }
93 if (prevchar!=currchar || charcnt==255 || i>=(blk_size)) {
94 if (charcnt<=2 && prevchar!=sentinel) {
95 for (m=0;m<charcnt;m++) out_block[x++]=prevchar;
96 } else {
97 out_block[x++]=sentinel;
98 out_block[x++]=prevchar;
99 out_block[x++]=charcnt;
100 }
101 charcnt=0;
102 }
103 charcnt++;
104 prevchar=currchar;
105 }
106 return(x);
107 }
108
comp_rle_decompress(unsigned char * prev_block,unsigned char * curr_block,char * out_block,int blk_size,int bufsize)109 int comp_rle_decompress(unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int bufsize) {
110 int i,x=0,m;
111 unsigned char sentinel=curr_block[0];
112 unsigned char currchar;
113 unsigned char charcnt;
114
115 for (i=1;i<blk_size;i++) {
116 currchar=curr_block[i];
117 if (currchar==sentinel) {
118 currchar=curr_block[++i];
119 charcnt=curr_block[++i];
120 if ((x+charcnt)>bufsize) {
121 printf("Error in RLE compression!\n");
122 return(0);
123 }
124 for (m=0;m<charcnt;m++) out_block[x++]=currchar;
125 } else {
126 out_block[x++]=currchar;
127 }
128 }
129 return(x);
130 }
131