LZSS压缩算法C语言实现Word格式.docx

上传人:b****1 文档编号:4463518 上传时间:2023-05-03 格式:DOCX 页数:17 大小:18.50KB
下载 相关 举报
LZSS压缩算法C语言实现Word格式.docx_第1页
第1页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第2页
第2页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第3页
第3页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第4页
第4页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第5页
第5页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第6页
第6页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第7页
第7页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第8页
第8页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第9页
第9页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第10页
第10页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第11页
第11页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第12页
第12页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第13页
第13页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第14页
第14页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第15页
第15页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第16页
第16页 / 共17页
LZSS压缩算法C语言实现Word格式.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

LZSS压缩算法C语言实现Word格式.docx

《LZSS压缩算法C语言实现Word格式.docx》由会员分享,可在线阅读,更多相关《LZSS压缩算法C语言实现Word格式.docx(17页珍藏版)》请在冰点文库上搜索。

LZSS压缩算法C语言实现Word格式.docx

#defineNILN

/*upperlimitforg_match_len.Changedfrom18to16forbinary

compatabilitywithMicrosoftCOMPRESS.EXEandEXPAND.EXE

#defineF18*/

#defineF16

/*encodestringintopositionandlength

ifmatch_lengthisgreaterthanthis:

*/

#defineTHRESHOLD2

/*theseassumelittle-endianCPUlikeIntelx86

--needbyte-swapfunctionforbigendianCPU*/

#defineREAD_LE32(X)*(uint32_t*)(X)

#defineWRITE_LE32(X,Y)*(uint32_t*)(X)=(Y)

/*thisassumessizeof(long)==4*/

typedefunsignedlonguint32_t;

/*text(input)sizecounter,code(output)sizecounter,

andcounterforreportingprogressevery1Kbytes*/

staticunsignedlongg_text_size,g_code_size,g_print_count;

/*ringbufferofsizeN,withextraF-1bytes

tofacilitatestringcomparison*/

staticunsignedcharg_ring_buffer[N+F-1];

/*positionandlengthoflongestmatch;

setbyinsert_node()*/

staticunsignedg_match_pos,g_match_len;

/*left&

rightchildren&

parent--theseconstitutebinarysearchtree*/

staticunsignedg_left_child[N+1],g_right_child[N+257],g_parent[N+1];

/*input&

outputfiles*/

staticFILE*g_infile,*g_outfile;

/*****************************************************************************

initializetrees

*****************************************************************************/

staticvoidinit_tree(void)

{

unsignedi;

/*Fori=0toN-1,g_right_child[i]andg_left_child[i]willbetherightand

leftchildrenofnodei.Thesenodesneednotbeinitialized.

Also,g_parent[i]istheparentofnodei.Theseareinitializedto

NIL(=N),whichstandsfor'

notused.'

Fori=0to255,g_right_child[N+i+1]istherootofthetree

forstringsthatbeginwithcharacteri.Theseareinitialized

toNIL.Notethereare256trees.*/

for(i=N+1;

i<

=N+256;

i++)

g_right_child[i]=NIL;

for(i=0;

N;

g_parent[i]=NIL;

}

InsertsstringoflengthF,g_ring_buffer[r..r+F-1],intooneofthe

trees(g_ring_buffer[r]'

thtree)andreturnsthelongest-matchposition

andlengthviatheglobalvariablesg_match_posandg_match_len.

Ifg_match_len=F,thenremovestheoldnodeinfavorofthenew

one,becausetheoldonewillbedeletedsooner.

Noterplaysdoublerole,astreenodeandpositioninbuffer.

staticvoidinsert_node(intr)

unsignedchar*key;

unsignedi,p;

intcmp;

cmp=1;

key=&

g_ring_buffer[r];

p=N+1+key[0];

g_right_child[r]=g_left_child[r]=NIL;

g_match_len=0;

while

(1)

{

if(cmp>

=0)

{

if(g_right_child[p]!

=NIL)

p=g_right_child[p];

else

{

g_right_child[p]=r;

g_parent[r]=p;

return;

}

}

else

if(g_left_child[p]!

p=g_left_child[p];

g_left_child[p]=r;

for(i=1;

F;

cmp=key[i]-g_ring_buffer[p+i];

if(cmp!

break;

if(i>

g_match_len)

g_match_pos=p;

g_match_len=i;

if(g_match_len>

=F)

}

g_parent[r]=g_parent[p];

g_left_child[r]=g_left_child[p];

g_right_child[r]=g_right_child[p];

g_parent[g_left_child[p]]=r;

g_parent[g_right_child[p]]=r;

if(g_right_child[g_parent[p]]==p)

g_right_child[g_parent[p]]=r;

else

g_left_child[g_parent[p]]=r;

g_parent[p]=NIL;

/*removep*/

deletesnodepfromtree

staticvoiddelete_node(unsignedp)

unsignedq;

if(g_parent[p]==NIL)

return;

/*notintree*/

if(g_right_child[p]==NIL)

q=g_left_child[p];

elseif(g_left_child[p]==NIL)

q=g_right_child[p];

if(g_right_child[q]!

doq=g_right_child[q];

while(g_right_child[q]!

=NIL);

g_right_child[g_parent[q]]=g_left_child[q];

g_parent[g_left_child[q]]=g_parent[q];

g_left_child[q]=g_left_child[p];

g_parent[g_left_child[p]]=q;

g_right_child[q]=g_right_child[p];

g_parent[g_right_child[p]]=q;

g_parent[q]=g_parent[p];

g_right_child[g_parent[p]]=q;

g_left_child[g_parent[p]]=q;

staticvoidcompress(void)

unsignedi,len,r,s,last_match_length,code_buf_ptr;

unsignedcharcode_buf[17],mask;

intc;

init_tree();

/*initializetrees*/

/*code_buf[1..16]saveseightunitsofcode,andcode_buf[0]worksas

eightflags,"

1"

representingthattheunitisanunencodedletter(1byte),

"

0"

aposition-and-lengthpair(2bytes).Thus,eightunitsrequireatmost

16bytesofcode.*/

code_buf[0]=0;

code_buf_ptr=mask=1;

s=0;

r=N-F;

/*Clearthebufferwithanycharacterthatwillappearoften.*/

memset(g_ring_buffer+s,'

'

r-s);

/*ReadFbytesintothelastFbytesofthebuffer*/

for(len=0;

len<

len++)

c=getc(g_infile);

if(c==EOF)

break;

g_ring_buffer[r+len]=c;

g_text_size=len;

if(g_text_size==0)/*textofsizezero*/

/*InserttheFstrings,eachofwhichbeginswithoneormore'

space'

characters.Notetheorderinwhichthesestringsareinserted.

Thisway,degeneratetreeswillbelesslikelytooccur.*/

for(i=1;

=F;

insert_node(r-i);

/*Finally,insertthewholestringjustread.Theglobalvariables

g_match_lenandg_match_posareset.*/

insert_node(r);

do

/*g_match_lenmaybespuriouslylongneartheendoftext.*/

if(g_match_len>

len)

g_match_len=len;

/*Notlongenoughmatch.Sendonebyte.*/

if(g_match_len<

=THRESHOLD)

g_match_len=1;

code_buf[0]|=mask;

/*'

sendonebyte'

flag*/

code_buf[code_buf_ptr]=g_ring_buffer[r];

/*Senduncoded.*/

code_buf_ptr++;

/*Sendpositionandlengthpair.Noteg_match_len>

THRESHOLD.*/

code_buf[code_buf_ptr]=(unsignedchar)g_match_pos;

code_buf[code_buf_ptr]=(unsignedchar)

(((g_match_pos>

>

4)&

0xF0)|

(g_match_len-(THRESHOLD+1)));

/*Shiftmaskleftonebit.*/

mask<

<

=1;

if(mask==0)

/*Sendatmost8unitsofcodetogether*/

for(i=0;

code_buf_ptr;

putc(code_buf[i],g_outfile);

g_code_size+=code_buf_ptr;

code_buf[0]=0;

code_buf_ptr=mask=1;

last_match_length=g_match_len;

for(i=0;

last_match_length;

c=getc(g_infile);

if(c==EOF)

/*Deleteoldstringsandreadnewbytes*/

delete_node(s);

g_ring_buffer[s]=c;

/*Ifthepositionisneartheendofbuffer,extendthebuffer

tomakestringcomparisoneasier.*/

if(s<

F-1)

g_ring_buffer[s+N]=c;

/*Sincethisisaringbuffer,incrementthepositionmoduloN.*/

s=(s+1)&

(N-1);

r=(r+1)&

/*Registerthestringing_ring_buffer[r..r+F-1]*/

insert_node(r);

/*Reportsprogresseachtimetheg_text_sizeexceedsmultiplesof1024.*/

g_text_size+=i;

if(g_text_size>

g_print_count)

printf("

%12ld\r"

g_text_size);

g_print_count+=1024;

while(i++<

last_match_length)/*Aftertheendoftext,*/

/*noneedtoread,but*/

len--;

if(len)

insert_node(r);

/*buffermaynotbeempty.*/

}while(len>

0);

/*untillengthofstringtobeprocessediszero*/

/*Sendremainingcode.*/

if(code_buf_ptr>

1)

putc(code_buf[i],g_outfile);

g_code_size+=code_buf_ptr;

/*Encodingisdone.*/

printf("

In:

%ldbytes\n"

Out:

g_code_size);

/*xxx-floating-pointmath:

Out/In:

%.3f\n"

(double)g_code_size/g_text_size);

Justthereverseofcompress()

staticvoiddecompress(void)

unsignedr,flags;

intc,i,j,k;

memset(g_ring_buffer,'

N-F);

for(flags=0;

;

flags>

=1)

/*Getabyte.Foreachbitofthisbyte:

1=copyonebyteliterally,frominputtooutput

0=gettwomorebytesdescribinglength

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2