00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "params.h"
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 unsigned char* rle_compress(unsigned char *buf, int len, int &compressedLen)
00048 {
00049 int i;
00050
00051
00052 int outBufSize = len+(len*4)/3;
00053 unsigned char *out = new unsigned char[outBufSize];
00054 unsigned char *outPtr = out;
00055
00056 unsigned char byte1;
00057 unsigned char byte2;
00058 unsigned char frame_size;
00059 unsigned char array[129];
00060
00061 while (len)
00062 {
00063 byte1 = *buf;
00064 buf++;
00065 len--;
00066 frame_size = 1;
00067
00068 if (len)
00069 {
00070 byte2 = *buf;
00071 buf++;
00072 len--;
00073 frame_size = 2;
00074 do
00075 {
00076 if (byte1 == byte2)
00077 {
00078 while (len && (byte1 == byte2) && (frame_size < 129))
00079 {
00080 byte2 = *buf;
00081 buf++;
00082 len--;
00083 frame_size++;
00084 }
00085
00086 if (byte1 == byte2)
00087 {
00088 *outPtr = frame_size+126;
00089 outPtr++;
00090 *outPtr = byte1;
00091 outPtr++;
00092 if (len)
00093 {
00094 byte1=*buf;
00095 buf++;
00096 len--;
00097 frame_size = 1;
00098 }
00099 else
00100 {
00101 frame_size = 0;
00102 }
00103 }
00104 else
00105 {
00106 *outPtr = 125+frame_size;
00107 outPtr++;
00108 *outPtr = byte1;
00109 outPtr++;
00110 byte1 = byte2;
00111 frame_size = 1;
00112 }
00113
00114 if (len)
00115 {
00116 byte2 = *buf;
00117 buf++;
00118 len--;
00119 frame_size = 2;
00120 }
00121 }
00122 else
00123
00124 {
00125 *array = byte1;
00126 array[1] = byte2;
00127 while (len && (array[frame_size-2] != array[frame_size-1]) && (frame_size < 128))
00128 {
00129 array[frame_size] = *buf;
00130 buf++;
00131 len--;
00132 frame_size++;
00133 }
00134
00135
00136 if (array[frame_size-2] == array[frame_size-1])
00137 {
00138
00139 *outPtr = frame_size-3;
00140 outPtr++;
00141
00142 for (i=0; i<frame_size-2; i++)
00143 {
00144 *outPtr = array[i];
00145 outPtr++;
00146 }
00147
00148 byte1 = array[frame_size-2];
00149 byte2 = byte1;
00150 frame_size = 2;
00151 }
00152 else
00153 {
00154 *outPtr = frame_size-1;
00155 outPtr++;
00156
00157 for (i=0; i<frame_size; i++)
00158 {
00159 *outPtr = array[i];
00160 outPtr++;
00161 }
00162
00163 if (!len)
00164 {
00165 frame_size = 0;
00166 }
00167 else
00168 {
00169 byte1 = *buf;
00170 buf++;
00171 len--;
00172 if (!len)
00173 {
00174 frame_size = 1;
00175 }
00176 else
00177 {
00178 byte2 = *buf;
00179 buf++;
00180 len--;
00181 frame_size = 2;
00182 }
00183 }
00184 }
00185 }
00186 }
00187 while (len || (frame_size >= 2));
00188
00189 if (frame_size == 1)
00190 {
00191 *outPtr = 0;
00192 outPtr++;
00193 *outPtr = byte1;
00194 outPtr++;
00195 }
00196 }
00197 }
00198
00199 compressedLen = outPtr-out;
00200 return out;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 int rle_decompress(unsigned char *buf, int len, unsigned char *out, int &uncompressLen)
00215 {
00216 unsigned char header;
00217 unsigned char *outPtr = out;
00218 unsigned char i;
00219 int outSize = 0;
00220
00221 while (len)
00222 {
00223 header = *buf;
00224 buf++;
00225 len--;
00226
00227 if (!(header & 128))
00228 {
00229
00230 for (i=0; i<=header; i++)
00231 {
00232 if (outSize >= uncompressLen) return -1;
00233 *outPtr = *buf;
00234 outPtr++;
00235 outSize++;
00236 buf++;
00237 len--;
00238 }
00239 }
00240 else
00241 {
00242 const unsigned int n = (header & 127) + 2;
00243 for (i=0; i<n; i++)
00244 {
00245 if (outSize >= uncompressLen) return -1;
00246 *outPtr = *buf;
00247 outPtr++;
00248 outSize++;
00249 }
00250 buf++;
00251 len--;
00252 }
00253 }
00254
00255 uncompressLen = outSize;
00256 return 0;
00257 }