Return to 32 Bit CRC File Calculation
// CRCfileDlg.cpp : implementation file
//
// Copyright © 2000 - 2008 Richard A. Ellingson
// http://www.createwindow.com
// mailto:CreateWindow.com@comcast.net

#include "stdafx.h"
#include "CRCfile.h"
#include "CRCfileDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
     CAboutDlg();

// Dialog Data
     //{{AFX_DATA(CAboutDlg)
     enum { IDD = IDD_ABOUTBOX };
     //}}AFX_DATA

     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CAboutDlg)
     protected:
     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
     //}}AFX_VIRTUAL

// Implementation
protected:
     //{{AFX_MSG(CAboutDlg)
     //}}AFX_MSG
     DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
     //{{AFX_DATA_INIT(CAboutDlg)
     //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
     CDialog::DoDataExchange(pDX);
     //{{AFX_DATA_MAP(CAboutDlg)
     //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
     //{{AFX_MSG_MAP(CAboutDlg)
          // No message handlers
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCRCfileDlg dialog

CCRCfileDlg::CCRCfileDlg(CWnd* pParent /*=NULL*/)
     : CDialog(CCRCfileDlg::IDD, pParent)
{
     //{{AFX_DATA_INIT(CCRCfileDlg)
     m_csCRCtext = _T("");
     m_csFileName = _T("");
     m_csURL = _T("");
     //}}AFX_DATA_INIT
     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCRCfileDlg::DoDataExchange(CDataExchange* pDX)
{
     CDialog::DoDataExchange(pDX);
     //{{AFX_DATA_MAP(CCRCfileDlg)
     DDX_Text(pDX, IDC_CRCTEXT, m_csCRCtext);
     DDX_Text(pDX, IDC_FILENAME, m_csFileName);
     DDX_Text(pDX, IDC_URL, m_csURL);
     //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCRCfileDlg, CDialog)
     //{{AFX_MSG_MAP(CCRCfileDlg)
     ON_WM_SYSCOMMAND()
     ON_WM_PAINT()
     ON_WM_QUERYDRAGICON()
     ON_BN_CLICKED(IDC_OPEN, OnOpen)
     ON_WM_DROPFILES()
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCRCfileDlg message handlers

BOOL CCRCfileDlg::OnInitDialog()
{
     CDialog::OnInitDialog();

     // Add "About..." menu item to system menu.

     // IDM_ABOUTBOX must be in the system command range.
     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
     ASSERT(IDM_ABOUTBOX < 0xF000);

     CMenu* pSysMenu = GetSystemMenu(FALSE);
     if (pSysMenu != NULL)
     {
          CString strAboutMenu;
          strAboutMenu.LoadString(IDS_ABOUTBOX);
          if (!strAboutMenu.IsEmpty())
          {
               pSysMenu->AppendMenu(MF_SEPARATOR);
               pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
               // ******* Begin custom code *******

               // We have a Minimize box on the dialog, but we don't want
               // the Size and Maximize options on the system menu so disable them.
               pSysMenu->EnableMenuItem(SC_SIZE, MF_GRAYED);
               pSysMenu->EnableMenuItem(SC_MAXIMIZE, MF_GRAYED);

               // ******* End custom code *******
          }
     }

     // Set the icon for this dialog. The framework does this automatically
     // when the application's main window is not a dialog
     SetIcon(m_hIcon, TRUE);               // Set big icon
     SetIcon(m_hIcon, FALSE);          // Set small icon
     
     // TODO: Add extra initialization here

// ******* Begin custom code *******

     // Don't forget to create the lookup table first
     Init_CRC32_Table();

     // Set the default text for the dialog box
     m_csFileName = "no file selected";
     m_csCRCtext = "0";
     m_csURL = "http://www.createwindow.com";
     UpdateData(FALSE);

     // For convenience, we'll add file Drag and Drop support
     DragAcceptFiles(TRUE);

// ******* End custom code *******
     
     return TRUE; // return TRUE unless you set the focus to a control
}

void CCRCfileDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
     {
          CAboutDlg dlgAbout;
          dlgAbout.DoModal();
     }
     else
     {
          CDialog::OnSysCommand(nID, lParam);
     }
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CCRCfileDlg::OnPaint()
{
     if (IsIconic())
     {
          CPaintDC dc(this); // device context for painting

          SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

          // Center icon in client rectangle
          int cxIcon = GetSystemMetrics(SM_CXICON);
          int cyIcon = GetSystemMetrics(SM_CYICON);
          CRect rect;
          GetClientRect(&rect);
          int x = (rect.Width() - cxIcon + 1) / 2;
          int y = (rect.Height() - cyIcon + 1) / 2;

          // Draw the icon
          dc.DrawIcon(x, y, m_hIcon);
     }
     else
     {
          CDialog::OnPaint();
     }
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCRCfileDlg::OnQueryDragIcon()
{
     return (HCURSOR) m_hIcon;
}
// Everything above this line is AppWizard generated code
// except for a few lines in OnInitDialog()

// ******* Begin custom code *******

// Call this function only once to initialize the CRC table.
void CCRCfileDlg::Init_CRC32_Table()
{// Called by OnInitDialog()

     // This is the official polynomial used by CRC-32
     // in PKZip, WinZip and Ethernet.
     ULONG ulPolynomial = 0x04c11db7;

     // 256 values representing ASCII character codes.
     for(int i = 0; i <= 0xFF; i++)
     {
          crc32_table[i]=Reflect(i, 8) << 24;
          for (int j = 0; j < 8; j++)
               crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1 << 31) ? ulPolynomial : 0);
          crc32_table[i] = Reflect(crc32_table[i], 32);
     }
}

// Reflection is a requirement for the official CRC-32 standard.
// You can create CRCs without it, but they won't conform to the standard.
ULONG CCRCfileDlg::Reflect(ULONG ref, char ch)
{// Used only by Init_CRC32_Table()

     ULONG value(0);

     // Swap bit 0 for bit 7
     // bit 1 for bit 6, etc.
     for(int i = 1; i < (ch + 1); i++)
     {
          if(ref & 1)
               value |= 1 << (ch - i);
          ref >>= 1;
     }
     return value;
}

// Handler for the "Select a file" button.
void CCRCfileDlg::OnOpen()
{
     CFileDialog dlg(TRUE, NULL, "(all files)", OFN_HIDEREADONLY,
          "All Files (*.*)|*.*||", this);

     if(IDOK != dlg.DoModal())
          return;

     m_csFileName = dlg.GetPathName();
     // Open the file and read it.
     Open_File();
     
}

// This function uses the crc32_table lookup table
// to generate a CRC for csData
int CCRCfileDlg::Get_CRC(CString &csData, DWORD dwSize)
{
     // Be sure to use unsigned variables,
     // because negative values introduce high bits
     // where zero bits are required.
     ULONG crc(0xffffffff);
     int len;
     unsigned char* buffer;

     len = dwSize;
     // Save the text in the buffer.
     buffer = (unsigned char*)(LPCTSTR)csData;
     // Perform the algorithm on each character
     // in the string, using the lookup table values.
     while(len--)
          crc = (crc >> 8) ^ crc32_table[(crc & 0xFF) ^ *buffer++];
     // Exclusive OR the result with the beginning value.
     return crc^0xffffffff;
}

// Handler for Close program messages
void CCRCfileDlg::OnCancel()
{
     // TODO: Add extra cleanup here
     
     // I like to override this function so the dialog
     // doesn't close when the Escape key is pressed.
     if(GetKeyState(VK_ESCAPE) & 128)
          return;

     CDialog::OnCancel();
}

// This function adds file Drag and Drop support.
// It isn't needed to calculate the CRC.
void CCRCfileDlg::OnDropFiles(HDROP hDropInfo)
{
     char lpszFile[MAX_PATH];
     CFileFind file;

     DragQueryFile(hDropInfo, 0, lpszFile, MAX_PATH);

     BOOL trufal = file.FindFile(lpszFile);
     if(trufal)
     {
          file.FindNextFile();
          if(!file.IsDirectory())
          {
               m_csFileName = lpszFile;
               //Open the file and read it.
               Open_File();
          }
     }
     // Let CDialog release the memory used in hDropInfo
     CDialog::OnDropFiles(hDropInfo);
}

// Open the file, read it, and obtain the CRC from Get_CRC()
void CCRCfileDlg::Open_File()
{
     HANDLE hFile = {NULL};
     DWORD dwSize, bytes_read;

     // Open the file and get ready to read it.
     // Don't be confused by the CreateFile() function,
     // we're only opening an existing file.
     hFile = CreateFile(m_csFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
          NULL, OPEN_EXISTING,
          FILE_FLAG_SEQUENTIAL_SCAN, NULL);
     if(hFile == INVALID_HANDLE_VALUE)
     {
          MessageBox("Failed to open the file.\n\n" + m_csFileName, "Error - CRC File Demo");
          return;
     }
     // Get the file size so we know how long to make our buffer.
     dwSize = GetFileSize(hFile, NULL);
     // Create a CString buffer of the proper length
     // and fill it with spaces.
     CString csData(' ', dwSize);
     // Read the file into the buffer
     ReadFile(hFile, csData.GetBuffer(dwSize), dwSize, &bytes_read, NULL);
     // We're done with the file handle so close it.
     CloseHandle(hFile);
     // Always release the buffer after writing to it.
     csData.ReleaseBuffer();
     // Check to be sure we read the whole file.
     if(dwSize != bytes_read)
     {
          MessageBox("Failed to read the file.\n\n" + m_csFileName, "Error - CRC File Demo");
          return;
     }
     // Note: At this point, we can't use normal string functions
     // on csData because it might contain NULL characters.
     // In other words, GetLength() will only return the count
     // up to the first NULL. Always include dwSize when passing
     // the string so we know the full length.
     int nCRC = Get_CRC(csData, dwSize);
     // Convert the returned integer into a character string.
     char ch[20];
     itoa(nCRC, ch, 16); // Note that the integer is a 16 bit hex
     // Send the CRC string to the dialog.
     m_csCRCtext = ch;
     // If the file path is likely to be too long to display shorten it.
     int nMax = 60;
     if(m_csFileName.GetLength() > nMax)
          m_csFileName = m_csFileName.Left(3) + "..." + m_csFileName.Right(nMax -6);
     UpdateData(FALSE);
}
// ******* End custom code *******