Gemini

Untitled

Jun 25th, 2011
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. int LzssEncode(u8 *source, u8 *&dest, int size)
  2. {
  3.     BIN_BUFFER lzss_out_f;
  4.     u16 position, p, pos;
  5.     int len, seek_in=0;
  6.     // ring buffer of size N, with extra F-1 bytes to facilitate string comparison
  7.     u8 text_buf[N];
  8.     // create destination buffer
  9.     dest=new BYTE[size*2];
  10.     // attach to binary structure
  11.     Buf_Bopen(&lzss_out_f,dest,size*2);
  12.     // write header data
  13.     Buf_Bwrite_I(LZSS_MAGIC,32,&lzss_out_f);
  14.     Buf_Bwrite_I(size,32,&lzss_out_f);
  15.     Buf_Bwrite_I(GetChecksum(source,size),32,&lzss_out_f);
  16.  
  17.     text_buf[0]=source[seek_in++];
  18.     do
  19.     {
  20.         u32 pos_f=seek_in;
  21.         pos=(_int16)(seek_in-1)%LZSS_BUFFER;
  22.         len=0;
  23.         // search for a match
  24.         for(p=__max(0,((signed)pos_f-LZSS_BUFFER+LZSS_LENGTH)); p<pos_f-1; p++)
  25.         {
  26.             int l=0;
  27.             while(l<LZSS_LENGTH && pos+l<LZSS_BUFFER && p%LZSS_BUFFER<LZSS_BUFFER-2
  28.                 && text_buf[(p+l)%LZSS_BUFFER]==text_buf[(pos+l)%LZSS_BUFFER])
  29.             {
  30.                 l++;
  31.                 if(l>len) text_buf[(pos+l)%LZSS_BUFFER]=source[seek_in++];
  32.             }
  33.             if(l>=len)
  34.             {
  35.                 len=l;
  36.                 position=p%LZSS_BUFFER;
  37.             }
  38.         }
  39.  
  40.         // match found!
  41.         if(len>=LZSS_LG_MIN)
  42.         {
  43.             Buf_Bwrite_M(CODE,1,&lzss_out_f);
  44.             Buf_Bwrite_M(position+1,LZSS_BUFFER_B,&lzss_out_f);
  45.             Buf_Bwrite_M(len-LZSS_LG_MIN,LZSS_LENGTH_B,&lzss_out_f);
  46.         }
  47.         // no match found
  48.         else
  49.         {
  50.             if(len==0) text_buf[pos+1]=source[seek_in++];
  51.             Buf_Bwrite_M(LITERAL,1,&lzss_out_f);
  52.             Buf_Bwrite_M(text_buf[pos],8,&lzss_out_f);
  53.         }
  54.     } while(seek_in<=size);
  55.  
  56.     // flush remaining bits
  57.     Buf_Bflush(&lzss_out_f);
  58.     Buf_Bwrite_M(0,16,&lzss_out_f);
  59.  
  60.     return Buf_Btell(&lzss_out_f);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment