Click here to Skip to main content
12,350,143 members (26,409 online)
Click here to Skip to main content

Stats

98.5K views
5.9K downloads
94 bookmarked
Posted

An Analysis of the Windows PE Checksum Algorithm

, 7 Mar 2008 CPOL
An Analysis Demonstrating the PE Checksum is an Additive Checksum based on RFC 1071
// CRC32.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "crc.h"
#include "hex.h"

void Usage( void );

int _tmain(int argc, _TCHAR* argv[])
{
    if( argc != 2 )
    {
        Usage();
        return -1;
    }

    HANDLE hFile = INVALID_HANDLE_VALUE;
    HANDLE hFileMapping = NULL;
    PVOID pBaseAddress = NULL;
    DWORD dwFileLength = 0;

    try
    {
        /////////////////////////////////////////////////////////////
        hFile = CreateFile( argv[ 1 ], GENERIC_READ, FILE_SHARE_READ,
            NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
        if ( INVALID_HANDLE_VALUE == hFile ||
            NULL == hFile) { throw GetLastError(); }

        /////////////////////////////////////////////////////////////
        hFileMapping = CreateFileMapping(hFile, NULL,
            PAGE_READONLY, 0, 0, NULL);
        if ( NULL == hFileMapping )
        {
            throw GetLastError();
        }

        /////////////////////////////////////////////////////////////
        pBaseAddress = MapViewOfFile( hFileMapping,
            FILE_MAP_READ, 0, 0, 0);
        if ( NULL == pBaseAddress )
        {
            throw GetLastError();
        }

        /////////////////////////////////////////////////////////////
        DWORD dwSize = 0;
        LARGE_INTEGER liSize = { 0, 0 };
        if( TRUE == GetFileSizeEx( hFile, &liSize ) )
        {
            dwSize = liSize.LowPart;
        }

        CryptoPP::CRC32 crc;
        byte digest[ CryptoPP::CRC32::DIGESTSIZE ] = { 0 };
        crc.CalculateDigest( digest, (byte*)pBaseAddress, dwSize );
        
        std::string sink;
        CryptoPP::HexEncoder encoder( new CryptoPP::StringSink( sink ), true );
        encoder.Put( digest, CryptoPP::CRC32::DIGESTSIZE );
        encoder.MessageEnd();

        std::cout << sink << std::endl;

    }
    catch(...)
    {
        std::wcerr << "Error computing CRC-32" << std::endl;
    }

    UnmapViewOfFile( hFileMapping );
    CloseHandle( hFile );

    return 0;
}

void Usage( void )
{
    std::tcout << _T("crc32.exe <filename>") << std::endl;
    std::tcout << _T(" filename to have CRC-32 computed.") << std::endl;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jeffrey Walton
Systems / Hardware Administrator
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 8 Mar 2008
Article Copyright 2007 by Jeffrey Walton
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid