Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int LzssEncode(u8 *source, u8 *&dest, int size)
- {
- BIN_BUFFER lzss_out_f;
- u16 position, p, pos;
- int len, seek_in=0;
- // ring buffer of size N, with extra F-1 bytes to facilitate string comparison
- u8 text_buf[N];
- // create destination buffer
- dest=new BYTE[size*2];
- // attach to binary structure
- Buf_Bopen(&lzss_out_f,dest,size*2);
- // write header data
- Buf_Bwrite_I(LZSS_MAGIC,32,&lzss_out_f);
- Buf_Bwrite_I(size,32,&lzss_out_f);
- Buf_Bwrite_I(GetChecksum(source,size),32,&lzss_out_f);
- text_buf[0]=source[seek_in++];
- do
- {
- u32 pos_f=seek_in;
- pos=(_int16)(seek_in-1)%LZSS_BUFFER;
- len=0;
- // search for a match
- for(p=__max(0,((signed)pos_f-LZSS_BUFFER+LZSS_LENGTH)); p<pos_f-1; p++)
- {
- int l=0;
- while(l<LZSS_LENGTH && pos+l<LZSS_BUFFER && p%LZSS_BUFFER<LZSS_BUFFER-2
- && text_buf[(p+l)%LZSS_BUFFER]==text_buf[(pos+l)%LZSS_BUFFER])
- {
- l++;
- if(l>len) text_buf[(pos+l)%LZSS_BUFFER]=source[seek_in++];
- }
- if(l>=len)
- {
- len=l;
- position=p%LZSS_BUFFER;
- }
- }
- // match found!
- if(len>=LZSS_LG_MIN)
- {
- Buf_Bwrite_M(CODE,1,&lzss_out_f);
- Buf_Bwrite_M(position+1,LZSS_BUFFER_B,&lzss_out_f);
- Buf_Bwrite_M(len-LZSS_LG_MIN,LZSS_LENGTH_B,&lzss_out_f);
- }
- // no match found
- else
- {
- if(len==0) text_buf[pos+1]=source[seek_in++];
- Buf_Bwrite_M(LITERAL,1,&lzss_out_f);
- Buf_Bwrite_M(text_buf[pos],8,&lzss_out_f);
- }
- } while(seek_in<=size);
- // flush remaining bits
- Buf_Bflush(&lzss_out_f);
- Buf_Bwrite_M(0,16,&lzss_out_f);
- return Buf_Btell(&lzss_out_f);
- }
Advertisement
Add Comment
Please, Sign In to add comment