程序由五个模块组成lzw.h定义了一些基本的数据结构,常量,还有变量的初始化等。#ifndef__LZW_H__#define__LZW_H__//------------------------------------------------------------------------------#includestdio.h#includestdlib.h#includewindows.h#includememory.h//------------------------------------------------------------------------------#defineLZW_BASE0x102//Thecodebase#defineCODE_LEN12//Maxcodelength#defineTABLE_LEN4099//Itmustbeprimenumberandbiggerthan2^CODE_LEN=4096.//Suchas5051isalsook.#defineBUFFERSIZE1024//------------------------------------------------------------------------------typedefstruct{HANDLEh_sour;//Sourcefilehandle.HANDLEh_dest;//Destinationfilehandle.HANDLEh_suffix;//Suffixtablehandle.HANDLEh_prefix;//Prefixtablehandle.HANDLEh_code;//Codetablehandle.LPWORDlp_prefix;//Prefixtableheadpointer.LPBYTElp_suffix;//Suffixtableheadpointer.LPWORDlp_code;//Codetableheadpointer.WORDcode;WORDprefix;BYTEsuffix;BYTEcur_code_len;//Currentcodelength.[usedinDynamic-Code-Lengthmode]}LZW_DATA,*PLZW_DATA;typedefstruct{WORDtop;WORDindex;LPBYTElp_buffer;HANDLEh_buffer;BYTEby_left;DWORDdw_buffer;BOOLend_flag;}BUFFER_DATA,*PBUFFER_DATA;typedefstruct//Stackusedindecode{WORDindex;HANDLEh_stack;LPBYTElp_stack;}STACK_DATA,*PSTACK_DATA;//------------------------------------------------------------------------------VOIDstack_create(PSTACK_DATAstack){stack-h_stack=GlobalAlloc(GHND,TABLE_LEN*sizeof(BYTE));stack-lp_stack=GlobalLock(stack-h_stack);stack-index=0;}//------------------------------------------------------------------------------VOIDstack_destory(PSTACK_DATAstack){GlobalUnlock(stack-h_stack);GlobalFree(stack-h_stack);}//------------------------------------------------------------------------------VOIDbuffer_create(PBUFFER_DATAbuffer){buffer-h_buffer=GlobalAlloc(GHND,BUFFERSIZE*sizeof(BYTE));buffer-lp_buffer=GlobalLock(buffer-h_buffer);buffer-top=0;buffer-index=0;buffer-by_left=0;buffer-dw_buffer=0;buffer-end_flag=FALSE;}//------------------------------------------------------------------------------VOIDbuffer_destory(PBUFFER_DATAbuffer){GlobalUnlock(buffer-h_buffer);GlobalFree(buffer-h_buffer);}//------------------------------------------------------------------------------VOIDre_init_lzw(PLZW_DATAlzw)//Whencodetablereacheditstopitshould{//bereinitialized.memset(lzw-lp_code,0xFFFF,TABLE_LEN*sizeof(WORD));lzw-code=LZW_BASE;lzw-cur_code_len=9;}//------------------------------------------------------------------------------VOIDlzw_create(PLZW_DATAlzw,HANDLEh_sour,HANDLEh_dest){WORDi;lzw-h_code=GlobalAlloc(GHND,TABLE_LEN*sizeof(WORD));lzw-h_prefix=GlobalAlloc(GHND,TABLE_LEN*sizeof(WORD));lzw-h_suffix=GlobalAlloc(GHND,TABLE_LEN*sizeof(BYTE));lzw-lp_code=GlobalLock(lzw-h_code);lzw-lp_prefix=GlobalLock(lzw-h_prefix);lzw-lp_suffix=GlobalLock(lzw-h_suffix);lzw-code=LZW_BASE;lzw-cur_code_len=9;lzw-h_sour=h_sour;lzw-h_dest=h_dest;memset(lzw-lp_code,0xFFFF,TABLE_LEN*sizeof(WORD));}//------------------------------------------------------------------------------VOIDlzw_destory(PLZW_DATAlzw){GlobalUnlock(lzw-h_code);GlobalUnlock(lzw-h_prefix);GlobalUnlock(lzw-h_suffix);GlobalFree(lzw-h_code);GlobalFree(lzw-h_prefix);GlobalFree(lzw-h_suffix);}//------------------------------------------------------------------------------#endiffileio.h定义了一些文件操作#ifndef__FILEIO_H__#define__FILEIO_H__//------------------------------------------------------------------------------#includestdio.h#includestdlib.h#includewindows.h//------------------------------------------------------------------------------HANDLEfile_handle(CHAR*file_name){HANDLEh_file;h_file=CreateFile(file_name,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,0,NULL);returnh_file;}//------------------------------------------------------------------------------WORDload_buffer(HANDLEh_sour,PBUFFER_DATAbuffer)//Loadfiletobuffer{DWORDret;ReadFile(h_sour,buffer-lp_buffer,BUFFERSIZE,&ret,NULL);buffer-index=0;buffer-top=(WORD)ret;return(WORD)ret;}//------------------------------------------------------------------------------WORDempty_buffer(PLZW_DATAlzw,PBUFFER_DATAbuffer)//Outputbuffertofile{DWORDret;if(buffer-end_flag)//Theflagmarktheendofdecode{if(buffer-by_left){buffer-lp_buffer[buffer-index++]=(BYTE)(buffer-dw_buffer32-buffer-by_left)(8-buffer-by_left);}}WriteFile(lzw-h_dest,buffer-lp_buffer,buffer-index,&ret,NULL);buffer-index=0;buffer-top=ret;return(WORD)ret;}//------------------------------------------------------------------------------#endifhash.h定义了压缩时所用的码表操作函数,为了快速查找使用了hash算法,还有处理hash冲突的函数#ifndef__HASH_H__#define__HASH_H__//------------------------------------------------------------------------------#includestdio.h#includestdlib.h#includewindows.h//------------------------------------------------------------------------------#defineDIVTABLE_LEN#defineHASHSTEP13//Itshouldbiggerthan0.//------------------------------------------------------------------------------WORDget_hash_index(PLZW_DATAlzw){DWORDtmp;WORDresu