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 #include "CompressMan.hpp"
00034
00035 #ifdef XDEMILL
00036 #include "UnCompCont.hpp"
00037 #endif
00038
00039
00040
00041
00042 extern MemStreamer blockmem;
00043
00044
00045 #define RUNLENGTH_ITEM_MINSIZE 16
00046
00047
00048
00049
00050
00051
00052 struct CurRunLengthItem
00053
00054 {
00055 CurRunLengthItem *next;
00056 unsigned short len;
00057 unsigned short size;
00058
00059 char *GetStrPtr() { return (char *)(this+1); }
00060
00061 };
00062
00063 struct CurRunLengthState
00064
00065 {
00066 CurRunLengthItem *items;
00067 unsigned short totallen;
00068 short runlencount;
00069
00070 void KeepNewString(unsigned char *str,unsigned short len)
00071
00072
00073
00074 {
00075 CurRunLengthItem **itemref;
00076 unsigned allocsize;
00077
00078 #ifdef XMILL
00079 runlencount=0;
00080 #endif
00081
00082 totallen=len;
00083
00084 itemref=&items;
00085
00086
00087
00088
00089 while(*itemref!=NULL)
00090 {
00091 (*itemref)->len = ((*itemref)->size < len) ? (*itemref)->size : len;
00092
00093 mymemcpy((*itemref)->GetStrPtr(),(char *)str,(*itemref)->len);
00094
00095 str+=(*itemref)->len;
00096 len-=(*itemref)->len;
00097
00098 itemref=&((*itemref)->next);
00099
00100 if(len==0)
00101
00102
00103 {
00104 while(*itemref!=NULL)
00105 {
00106 (*itemref)->len=0;
00107 itemref=&((*itemref)->next);
00108 }
00109 break;
00110 }
00111 }
00112
00113 if(len>0)
00114
00115
00116 {
00117 if(len<RUNLENGTH_ITEM_MINSIZE)
00118 allocsize=RUNLENGTH_ITEM_MINSIZE;
00119 else
00120 allocsize=((len-1)|3)+1;
00121
00122 *itemref=(CurRunLengthItem *)blockmem.GetByteBlock(sizeof(CurRunLengthItem)+allocsize);
00123
00124 (*itemref)->size=allocsize;
00125 (*itemref)->len=len;
00126
00127 mymemcpy((*itemref)->GetStrPtr(),(char *)str,len);
00128
00129 (*itemref)->next=NULL;
00130 }
00131 }
00132 };
00133
00134 #ifdef XMILL
00135
00136 class RunLengthCompressor : public UserCompressor
00137 {
00138 public:
00139 RunLengthCompressor()
00140 {
00141 datasize=sizeof(CurRunLengthState);
00142 contnum=1;
00143 isrejecting=0;
00144 canoverlap=0;
00145 isfixedlen=0;
00146 }
00147
00148
00149
00150 void InitCompress(CompressContainer *cont,char *dataptr)
00151 {
00152 ((CurRunLengthState *)dataptr)->items=NULL;
00153 ((CurRunLengthState *)dataptr)->runlencount=-1;
00154 }
00155
00156
00157
00158
00159 void CompressString(char *str,unsigned len,CompressContainer *cont,char *dataptr)
00160 {
00161 CurRunLengthState *state=(CurRunLengthState *)dataptr;
00162
00163 if(state->runlencount!=-1)
00164
00165 {
00166 CurRunLengthItem *item=state->items;
00167 char *curptr=str;
00168 unsigned curlen=len;
00169
00170
00171 while(item!=NULL)
00172 {
00173 if((item->len>curlen)||
00174 (memcmp(item->GetStrPtr(),curptr,item->len)!=0))
00175 break;
00176
00177 curptr+=item->len;
00178 curlen-=item->len;
00179
00180 item=item->next;
00181
00182 if((curlen==0)&&(item==NULL))
00183
00184 {
00185 state->runlencount++;
00186 return;
00187 }
00188 }
00189
00190
00191
00192 cont->StoreUInt32(state->totallen);
00193
00194 item=state->items;
00195
00196 while(item!=NULL)
00197 {
00198 cont->StoreData(item->GetStrPtr(),item->len);
00199 item=item->next;
00200 }
00201 cont->StoreUInt32(state->runlencount);
00202 }
00203 else
00204 dataptr=NULL;
00205
00206
00207
00208 state->KeepNewString((unsigned char *)str,len);
00209 }
00210
00211 void FinishCompress(CompressContainer *cont,char *dataptr)
00212
00213
00214 {
00215 CurRunLengthState *state=(CurRunLengthState *)dataptr;
00216
00217 if(state->runlencount!=-1)
00218
00219 {
00220 CurRunLengthItem *item;
00221
00222
00223 cont->StoreUInt32(state->totallen);
00224
00225 item=state->items;
00226
00227 while(item!=NULL)
00228 {
00229 cont->StoreData(item->GetStrPtr(),item->len);
00230 item=item->next;
00231 }
00232 cont->StoreUInt32(state->runlencount);
00233 }
00234 }
00235 };
00236
00237 #endif
00238
00239
00240
00241
00242
00243 #ifdef XDEMILL
00244
00245 class RunLengthUncompressor : public UserUncompressor
00246 {
00247 public:
00248 RunLengthUncompressor()
00249 {
00250 datasize=sizeof(CurRunLengthState);
00251 contnum=1;
00252 }
00253
00254
00255
00256 void InitUncompress(UncompressContainer *cont,char *dataptr)
00257
00258 {
00259 ((CurRunLengthState *)dataptr)->items=NULL;
00260 ((CurRunLengthState *)dataptr)->runlencount=-1;
00261 }
00262
00263 void UncompressItem(UncompressContainer *cont,char *dataptr,XMLOutput *output)
00264 {
00265 CurRunLengthState *state=(CurRunLengthState *)dataptr;
00266
00267 if(state->runlencount==-1)
00268
00269 {
00270
00271
00272
00273 int len=cont->LoadUInt32();
00274 unsigned char *ptr=cont->GetDataPtr(len);
00275
00276 output->characters((char *)ptr,len);
00277
00278 state->runlencount=((short)cont->LoadUInt32())-1;
00279
00280 state->KeepNewString(ptr,len);
00281 }
00282 else
00283 {
00284
00285
00286 CurRunLengthItem *item=state->items;
00287
00288 while(item!=NULL)
00289 {
00290 output->characters(item->GetStrPtr(),item->len);
00291 item=item->next;
00292 }
00293 state->runlencount--;
00294 }
00295 }
00296 };
00297
00298 #endif
00299
00300
00301
00302
00303
00304 class RunLengthCompressorFactory : public UserCompressorFactory
00305 {
00306 #ifdef XDEMILL
00307 RunLengthUncompressor uncompressor;
00308 #endif
00309
00310 public:
00311 char *GetName() { return "rl"; }
00312 char *GetDescription() { return "Run-length encoder for arbitrary text strings"; }
00313
00314 #ifdef XMILL
00315 UserCompressor *InstantiateCompressor(char *paramstr,int len)
00316 {
00317 if(paramstr!=NULL)
00318 {
00319 Error("Run length compressor 'rl' should not have any arguments ('");
00320 ErrorCont(paramstr,len);
00321 Error("')");
00322 Exit();
00323 }
00324 return new RunLengthCompressor();
00325 }
00326 #endif
00327
00328 #ifdef XDEMILL
00329 UserUncompressor *InstantiateUncompressor(char *paramstr,int len)
00330 {
00331 return &uncompressor;
00332 }
00333 #endif
00334 };
00335
00336 RunLengthCompressorFactory runlengthcompressor;