您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > LZ77压缩算法C语言实现
/***********************************************************************Projectdescription:*Lz77compression/decompressionalgorithm.**********************************************************************/#includewindows.h#includeconio.h#includestdio.h#includeassert.h#defineOFFSET_CODING_LENGTH(10)#defineMAX_WND_SIZE1024//#defineMAX_WND_SIZE(1OFFSET_CODING_LENGTH)#defineOFFSET_MASK_CODE(MAX_WND_SIZE-1)constULONGm=3;UCHAR__buffer1__[0x200000];UCHAR__buffer2__[0x200000];////////////////////////////////////////////////////////////////////////////////voidWrite1ToBitStream(PUCHARpBuffer,ULONGulBitOffset){ULONGulByteBoundary;ULONGulOffsetInByte;ulByteBoundary=ulBitOffset3;ulOffsetInByte=ulBitOffset&7;*(pBuffer+ulByteBoundary)|=(1ulOffsetInByte);}voidWrite0ToBitStream(PUCHARpBuffer,ULONGulBitOffset){ULONGulByteBoundary;ULONGulOffsetInByte;ulByteBoundary=ulBitOffset3;ulOffsetInByte=ulBitOffset&7;*(pBuffer+ulByteBoundary)&=(~(1ulOffsetInByte));}ULONGReadBitFromBitStream(PUCHARpBuffer,ULONGulBitOffset){ULONGulByteBoundary;ULONGulOffsetInByte;ulByteBoundary=ulBitOffset3;ulOffsetInByte=ulBitOffset&7;return((*(PULONG)(pBuffer+ulByteBoundary))ulOffsetInByte)&1;}ULONGWINAPIWriteGolombCode(ULONGx,PUCHARpBuffer,ULONGulBitOffset){ULONGq,r;inti;q=(x-1)m;r=x-(qm)-1;for(i=0;(ULONG)iq;i++,ulBitOffset++){Write1ToBitStream(pBuffer,ulBitOffset);}Write0ToBitStream(pBuffer,ulBitOffset);ulBitOffset++;for(i=0;im;i++,ulBitOffset++){if((ri)&1){Write1ToBitStream(pBuffer,ulBitOffset);}else{Write0ToBitStream(pBuffer,ulBitOffset);}}returnm+q+1;}ULONGReadGolombCode(PULONGpulCodingLength,PUCHARpBuffer,ULONGulBitOffset){ULONGq,r;ULONGbit;inti;for(q=0;;q++){bit=(ULONG)ReadBitFromBitStream(pBuffer,ulBitOffset);ulBitOffset++;if(!bit){break;}}for(i=0,r=0;(ULONG)im;i++,ulBitOffset++){bit=(ULONG)ReadBitFromBitStream(pBuffer,ulBitOffset);bit=i;r|=bit;}*pulCodingLength=m+q+1;returnr+(qm)+1;}ULONGCompareStrings(PUCHARstring1,PUCHARstring2,ULONGlength){ULONGi;PUCHARp1,p2;p1=string1;p2=string2;for(i=0;ilength;i++){if(*p1==*p2){p1++;p2++;}else{break;}}returnp1-string1;}voidWINAPIFindLongestSubstring(PUCHARpSourceString,PUCHARpString,ULONGulSourceStringLength,PULONGpulSubstringOffset,PULONGpulSubstringLength){PUCHARpSrc;ULONGoffset,length;ULONGulMaxLength;*pulSubstringOffset=offset=0;*pulSubstringLength=0;if(NULL==pSourceString||NULL==pString){return;}ulMaxLength=ulSourceStringLength;pSrc=pSourceString;while(ulMaxLength0){length=CompareStrings(pSrc,pString,ulMaxLength);if(length*pulSubstringLength){*pulSubstringLength=length;*pulSubstringOffset=offset;}pSrc++;offset++;ulMaxLength--;}}/*voidFindLongestSubstring(PUCHARpSourceString,PUCHARpString,ULONGulSourceStringLength,PULONGpulSubstringOffset,PULONGpulSubstringLength){PUCHARpCurrentOffset;PUCHARp1,p2;ULONGoffset,length;pCurrentOffset=pSourceString;*pulSubstringOffset=offset=0;*pulSubstringLength=length=0;while(pCurrentOffsetpSourceString+ulSourceStringLength){p1=pCurrentOffset;p2=pString;if(*p1==*p2){while(p1pSourceString+ulSourceStringLength&&*p1==*p2){p1++;p2++;}length=p1-pCurrentOffset;}else{length=0;}if(length*pulSubstringLength){*pulSubstringLength=length;*pulSubstringOffset=(ULONG)pCurrentOffset-(ULONG)pSourceString;}pCurrentOffset++;}}*/voidWriteBits(PUCHARpDataBuffer,ULONGulOffsetToWrite,ULONGulBits,ULONGulBitLength){ULONGulDwordsOffset;ULONGulBitsOffset,ulBitsRemained;ulDwordsOffset=ulOffsetToWrite5;ulBitsOffset=ulOffsetToWrite&31;ulBitsRemained=32-ulBitsOffset;if(0==ulBitsOffset){*((PULONG)pDataBuffer+ulDwordsOffset)=ulBits;}elseif(ulBitsRemained=ulBitLength){*((PULONG)pDataBuffer+ulDwordsOffset)|=(ulBitsulBitsOffset);}else{*((PULONG)pDataBuffer+ulDwordsOffset)|=(ulBitsulBitsOffset);*((PULONG)pDataBuffer+ulDwordsOffset+1)=ulBitsulBitsRemained;}}voidReadBits(PUCHARpDataBuffer,ULONGulOffsetToRead,PULONGpulBits){ULONGulDwordsOffset;ULONGulBitsOffset,ulBitsLength;ulDwordsOffset=ulOffsetToRead5;ulBitsOffset=ulOffsetToRead&31;ulBitsLength=32-ulBitsOffset;*pulBits=*((PULONG)pDataBuffer+ulDwordsOffset);if(0!=ulBitsOffset){(*pulBits)=ulBitsOffset;(*pulBits)|=(*((PULONG)pDataBuffer+ulDwordsOffset+1))ulBitsLength;}}voidlz77compress(PUCHARpDataBuffer,ULONGulDataLength,PUCHARpOutputBuffer,PULONGpulNumberOfBits){LONGiSlideWindowPtr;ULONGulBytesCoded;ULONGulMaxlength;PUCHARpSlideWindowPtr;PUCHARpUnprocessedDataPtr;ULONGoffset;ULONGlength;ULONGulCodingLength;ULONGulBitOffset;UCHARcc;inti;iSlideWindowPtr=-MAX_WND_SIZE;pSlideWindowPtr=NULL;ulBitOffset=0;ulBytesCoded=0;while(ulBytesCodedulDataLength){if(iSlideWindowPtr=0){pSlideWindowPtr=pDataBuffer+iSlideWindowPtr;ulMaxlength=MAX_WND_SIZE;}elseif(iSlideWindowPtr=-MAX_WND_SIZE){pSlideWindowPtr=pDataBuffer;ulMaxlength=MAX_WND_SIZE+iSlideWindowPtr;}else{pSlideWindowPtr=NULL;ulMaxlength=0;}pUnprocessedDataPtr=pDataBuffer+ulBytesCoded;if(ulMaxlengthulDataLength-ulBytesCoded){ulMaxlength=ulDataLength-ulBytesCoded;}FindLongestSubstring(pSlideWindowPtr,pUnprocessedDataPtr,ulMaxlength,&offset,&length);assert(length=
本文标题:LZ77压缩算法C语言实现
链接地址:https://www.777doc.com/doc-3316460 .html