CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Compression error

Hybrid View

  1. #1
    Join Date
    May 2015
    Posts
    316

    Compression error

    Hello,

    I am trying to debug the legacy code. The code compresses the file (with different sections) during some operation. And later tries to decompress the same.
    Now when new sections are added as part of another feature, the legacy code, throws exception for this code, as the SECTION_ERROR. It s due to some MAGIC number written for this section is not matching .

    that is just background.

    Now, I was trying to debug in release mode and found that, when t is writting the magic number:
    Code:
    size_t MemMappedCFileWriteLock::Write(const void * pBuffer, size_t nSize, size_t nCount)
    {
    	size_t stWritten = 0;
    	size_t stTotal = nCount * nSize; // nSize should be 1 unless someone changes AIPerist
    	size_t stRemain = stTotal;
    	while ( stWritten < stTotal)
    	{
    		DWORD dwWriteCount = 0;
    		DWORD dwWritten;
    		if (stRemain > ULONG_MAX)
    		{
    			dwWriteCount = ULONG_MAX;
    		}
    		else
    		{
    			dwWriteCount = (DWORD)stRemain; // <= ULONG_MAX
    		}
    		bool bRet = (WriteFile(m_hFile, (char*)pBuffer + stWritten, dwWriteCount, &dwWritten, NULL) != 0);
    		if (!bRet)
    			break;
    		stWritten += (size_t)dwWriteCount;
    		stRemain -= (size_t)dwWriteCount;
    	}
    	return stWritten;
    }
    
    +++++++++++++++++++++++++++++++
    WriteFile(
        _In_ HANDLE hFile,
        _In_reads_bytes_opt_(nNumberOfBytesToWrite) LPCVOID lpBuffer,
        _In_ DWORD nNumberOfBytesToWrite,
        _Out_opt_ LPDWORD lpNumberOfBytesWritten,
        _Inout_opt_ LPOVERLAPPED lpOverlapped
        );
    The debugger showed the value of nsize and nCount as 1, 4.
    But the dwWriteCount = (DWORD)stRemain; , sends the no of bytes as 1 to the WriteFile function.

    Does this casting look suspicious ? SShould it not send, 4 bytes to WriteFile function !

    Thanks a lot
    pdk

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,795

    Re: Compression error

    Quote Originally Posted by pdk5 View Post
    ...
    The debugger showed the value of nsize and nCount as 1, 4.
    But the dwWriteCount = (DWORD)stRemain; , sends the no of bytes as 1 to the WriteFile function.
    ...
    How did you check this "no of bytes as 1 to the WriteFile"?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2015
    Posts
    316

    Re: Compression error

    thanks Victor,
    with the debugger (some variables still get displayed, unless they are optimised in release mode )

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,263

    Re: Compression error

    If nsize = 1, ncount = 4 then

    stwritten = 0
    sttotal = 4 * 1 = 4
    stremain = 4

    stwritten < sttotal
    dwwritecount = 0
    dwwritten = ?? (say 987654)

    stremain < ULONG_MAX
    dwritecount = 4 (cast is not the problem)


    So write() will try to write 4 bytes from the memory pointed to by buffer to the file.

    Assuming write succeeded, then
    bret = true
    dwritten = 4
    stwritten = 0 + 4 = 4
    stremain = 4 - 4 = 0

    stwritten not < sttotal
    returns with value 4


    Have you examined the 4 bytes referenced by buffer and the bytes written to the file? Are these different?

    If you are compiling as release, I won't be using the debugger. I would be displaying variable values to the console

    Are you compiling as 32 or 64 bit?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.4)

  5. #5
    Join Date
    May 2015
    Posts
    316

    Re: Compression error

    thankyou very much kaud.

    The above code was legacy code, i was trying to step through, just to follow. Yes, as you said, that maynot be the issue.

    Looks like the issue may be due to some new enums added. My senior collegue looks like has found issue and hopefully that may solve (just waiting for his changes to be checkked in )

    Anyway the compression/decompression code is written bit complicated way, not fully understood yet, But it was helpful for me to step through and understand . It is bit difficult without any design or any other docs

    Btw we are using 64 bit builds

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,263

    Re: Compression error

    Btw we are using 64 bit builds
    That explains the use of size_t rather than DWORD and why the cast is required. Note that both size_t and DWORD are unsigned types so care has to be taken with decrement as you can't have a negative unsigned type. For unsigned, decrementing 0 gives a very large positive number!

    IMO, the code could be improved. Consider (NOT tried):

    Code:
    size_t MemMappedCFileWriteLock::Write(const void* pBuffer, size_t nSize, size_t nCount)
    {
    	const size_t stTotal = nCount * nSize; // nSize should be 1 unless someone changes AIPerist
    
    	size_t stWritten = 0;
    
    	for (size_t stRemain = stTotal; stRemain > 0; )
    	{
    		DWORD dwWritten = 0;
    
    		if (!WriteFile(m_hFile, (char*)pBuffer + stWritten, (stRemain > ULONG_MAX) ? ULONG_MAX : (DWORD)stRemain, &dwWritten, NULL))
    			break;
    
    		stWritten += dwWritten;
    		stRemain -= (stRemain >= dwWritten) ? dwWritten : stRemain;
    	}
    
    	return stWritten;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.4)

  7. #7
    Join Date
    May 2015
    Posts
    316

    Re: Compression error

    Thanks a lot kaud. very sorry for the delay in response

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)