LZ77 compression algorithm (C language version)

xiaoxiao2021-03-06  63

Testing a 425K file requires 9.4 seconds, the compressed file is 177K.

/ ************************************************** ****************************** Project Description: * lz77 compression / decompression algorithm. ************************* *********************************************************** ** / # include #include #include #define offset_coding_length (10) #define max_wnd_size 1024 // # define max_wnd_size (1 << OFFSET_CODING_LENGTH) #define OFFSET_MASK_CODE (MAX_WND_SIZE-1) const ULONG m = 3; UCHAR __buffer1 __ [0x200000]; UCHAR __buffer2 __ [0x200000]; voidWrite1ToBitStream (PUCHAR pBuffer, ULONG ulBitOffset) {ULONG ulByteBoundary; ULONG ulOffsetInByte; ulByteBoundary = ulBitOffset >> 3; ulOffsetInByte = ulBitOffset & 7; * (pBuffer ulByteBoundary) | = (1 << ulOffsetInByte);} voidWrite0ToBitStream (PUCHAR pBuffer, ULONG ulBitOffset) {ULONG ulByteBoundary; ULONG ulOffsetInByte; ulByteBoundary = ulBitOffset >> 3; ulOffsetInByte = ulBitOffset & 7; * (pBuffer Ulbyteb oundary) & = (~ (1 << ulOffsetInByte));} ULONGReadBitFromBitStream (PUCHAR pBuffer, ULONG ulBitOffset) {ULONG ulByteBoundary; ULONG ulOffsetInByte; ulByteBoundary = ulBitOffset >> 3; ulOffsetInByte = ulBitOffset & 7; return ((* (PULONG) (pBuffer ulbyteboundary) >> ULOFFSETINBYTE) & 1;} Ulong WinApiWritegolombCode (Ulong X, PuChar PBuffer, Ulong UlbitOffset) {Ulong Q, R; INT I; Q = (X-1) >> m; R = X- (Q <

} Write0ToBitStream (pBuffer, ulBitOffset); ulBitOffset ; for (i = 0; i > i) & 1) {Write1ToBitStream (pBuffer, ulBitOffset);} else {Write0ToBitStream (pBuffer, Ulbitoffset;}} Return M Q 1;} UlongreadGolombCode (Pulong PulcodingLombCode (Pulong PulcodingLomb Code (Pulong PulcodingLomb "{Ulong Q, R; Ulong bit; Int i; for (q = 0;; q ) {bit = (Ulong ReadbitFromBitStream (PBUFFER, ULbitOffset); UlbitOffset ; if (! Bit) {Break;}} for (i = 0, r = 0; (ulong) i

pSourceString; while (ulMaxLength> 0) {length = CompareStrings (pSrc, pString, ulMaxLength); if (length> * pulSubstringLength) {* pulSubstringLength = length; * pulSubstringOffset = offset;} pSrc ; offset ; ulMaxLength--;}} / * voidFindLongestSubstring (PUCHAR pSourceString, PUCHAR pString, ULONG ulSourceStringLength, PULONG pulSubstringOffset, PULONG pulSubstringLength) {PUCHAR pCurrentOffset; PUCHAR p1, p2; ULONG offset, length; pCurrentOffset = pSourceString; * pulSubstringOffset = offset = 0; * pulSubstringLength = length = 0; while (pCurrentOffset * pulSubstringLength) {* pulSubstringLength = length; * pulSubstringOffset = (ULONG) pCurrentOffset - (ULONG) pSourceString;} pCurrentOffset ;}} * / voidWriteBits (PUCHAR pDataBuffer, ULONG ulOffsetToWrite, ULONG ulBits, ULONG ulBitLength) {ULONG ulDwordsOffset; ULONG ulBitsOffset, ulBitsRemained; ulDwordsOffset = ulOffsetToWrite >> 5; ulBitsOffset = ulOffsetToWrite & 31; ulBitsRemained = 32 - ulBitsOffset; if (0 == ulBitsOffset) {* ((PULONG) pDataBuffer

ulDwordsOffset) = ulBits;} else if (ulBitsRemained> = ulBitLength) {* ((PULONG) pDataBuffer ulDwordsOffset) | = (ulBits << ulBitsOffset);} else {* ((PULONG) pDataBuffer ulDwordsOffset) | = (ulBits < > ulBitsRemained;}} voidReadBits (PUCHAR pDataBuffer, ULONG ulOffsetToRead, PULONG pulBits) {ULONG ulDwordsOffset; ULONG ulBitsOffset, ulBitsLength; ulDwordsOffset = ulOffsetToRead >> 5; ulBitsOffset = ulOffsetToRead & 31; ulBitsLength = 32 - ulBitsOffset; * pulBits = * ((PULONG) pDataBuffer ulDwordsOffset); if (0 = ulBitsOffset!) {(* pulBits) >> = ulBitsOffset; (* pulBits) | = (* (( PULONG) pDataBuffer ulDwordsOffset 1)) << ulBitsLength;}} voidlz77compress (PUCHAR pDataBuffer, ULONG ulDataLength, PUCHAR pOutputBuffer, PULONG pulNumberOfBits) {LONG iSlideWindowPtr; ULONG ulBytesCoded; ULONG ulMaxlength; PUCHAR pSlideWindowPtr ; PUCHAR pUnprocessedDataPtr; ULONG offset; ULONG length; ULONG ulCodingLength; ULONG ulBitOffset; UCHAR cc; int i; iSlideWindowPtr = -MAX_WND_SIZE; pSlideWindowPtr = NULL; ulBitOffset = 0; ulBytesCoded = 0; while (ulBytesCoded = 0) {pSlideWindowPtr = pDataBuffer iSlideWindowPtr; ulMaxlength = MAX_WND_SIZE;} else if (iSlideWindowPtr> = - MAX_WND_SIZE) {pSlideWindowPtr = pDataBuffer; ulMaxlength = MAX_WND_SIZE

iSlideWindowPtr;} else {pSlideWindowPtr = NULL; ulMaxlength = 0;} pUnprocessedDataPtr = pDataBuffer ulBytesCoded; if (ulMaxlength> ulDataLength-ulBytesCoded) {ulMaxlength = ulDataLength-ulBytesCoded;} FindLongestSubstring (pSlideWindowPtr, pUnprocessedDataPtr, ulMaxlength, & offset, & length); assert (length <= MAX_WND_SIZE); assert (offset 1) {Write1ToBitStream (pOutputBuffer, ulBitOffset); ulBitOffset ; for (i = 0; i > i) & 1) {Write1TobitStream (PoutputBuffit, UlbitOffset);} else {Write0TOBITSTREAM (PoutputBuffer, UlbitOffset);}} ulcodingLength = Writegolom bCode (length, pOutputBuffer, ulBitOffset); ulBitOffset = ulCodingLength; iSlideWindowPtr = length; ulBytesCoded = length;} else {Write0ToBitStream (pOutputBuffer, ulBitOffset); ulBitOffset ; cc = (* pUnprocessedDataPtr); for (i = 0; i <8; i , ulbitoffset ) {IF ((CC >> I) & 1) {Write1TobitStream (PoutputBuffer, UlbitOffset);} else {Write0TOBITSTREAM (PoutputBuffer, UlbitOffset);

}} ISlideWindowPtr ; ulBytesCoded ;}} if (ulBytesCoded = ulDataLength!) {Assert (ulBytesCoded == ulDataLength);} * pulNumberOfBits = ulBitOffset;} void lz77decompress (PUCHAR pDataBuffer, ULONG ulNumberOfBits, PUCHAR pOutputBuffer, PULONG pulNumberOfBytes) {LONG iSlideWindowPtr; PUCHAR pSlideWindowPtr; ULONG length, offset; ULONG bit; UCHAR cc; int i; ULONG ulBytesDecoded; ULONG ulBitOffset; ULONG ulCodingLength; PUCHAR pWrite; iSlideWindowPtr = -MAX_WND_SIZE; pWrite = (PUCHAR) pOutputBuffer; ulBitOffset = 0; ulBytesDecoded = 0; while (ulBitOffset = 0) {pSlideWindowPtr = pOutputBuffer iSlideWindowPtr;} else if (iSlideWindowPtr> = - MAX_WND_SIZE) { pSlideWindowPtr = pOutputBuffer;} else {pSlideWindowPtr = NULL;} for (i = 0, offset = 0; i MAX_WND_SIZE) {assert (length <= MAX_WND_SIZE);} ulBitOffset

= UlCodingLength; RtlMoveMemory (pWrite, pSlideWindowPtr offset, length); pWrite = length; iSlideWindowPtr = length; ulBytesDecoded = length;} else {for (i = 0, cc = 0; i <8; i , ulBitOffset ) {bit = ReadBitFromBitStream (pDataBuffer, ulBitOffset); cc | = ((UCHAR) bit << i);} * pWrite = cc; iSlideWindowPtr ; ulBytesDecoded ;}} * pulNumberOfBytes = ulBytesDecoded;} extern "C" void WINAPILZ77Compress (PUCHAR __pDataBuffer, ULONG __ulDataLength , PUCHAR __pOutputBuffer, PULONG __pulNumberOfBits); extern "C" void WINAPILZ77Decompress (PUCHAR __pDataBuffer, ULONG __ulNumberOfBits, PUCHAR __pOutputBuffer, PULONG __pulNumberOfBytes); intmain (int argc, char * argv []) {FILE * fp = NULL; FILE * fp1; Ulong fsize; ulong ulnumberofbits; ulong ulfilecompressedsize; ulong ulfiledecompressed Size; SystemTime T1, T2; IF (3! = Argc) {Printf ("USAGE: LZ77 [/ C | / D] filename / n"); return -1;} // char s1 [] = "abcdabcdefgabcdefaffaSDA"; // Ulong A, B; // FindlongestSubString ((puchar) S1, (PuChar) S1 11, 11, & A, & B); // Return 0; fp = fopen (Argv [2], "RB"); if (! fp) {return -1;} fseek (fp, 0, seek_ek; fsize = ftell (fp); fseek (fp, 0, seek_set); FREAD (__ buffer1__, 1, fsize, fp); getSystemTime (& T1) LZ77Compress (__ buffer1__, fsize, __buffer2__, & ulnumberofbits);

转载请注明原文地址:https://www.9cbs.com/read-111558.html

New Post(0)