⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crc_ficdlg.cpp

📁 CRC32校验
💻 CPP
字号:
// CRC_FICDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CRC_FIC.h"
#include "CRC_FICDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CCRC_FICDlg dialog

CCRC_FICDlg::CCRC_FICDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCRC_FICDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCRC_FICDlg)
	m_csCRC32 = _T("");
	m_csFileName = _T("");
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCRC_FICDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCRC_FICDlg)
	DDX_Text(pDX, IDC_CRC32_RESULT, m_csCRC32);
	DDX_Text(pDX, IDC_FILE_TO_CHECK, m_csFileName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCRC_FICDlg, CDialog)
	//{{AFX_MSG_MAP(CCRC_FICDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DROPFILES()
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCRC_FICDlg message handlers

BOOL CCRC_FICDlg::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);
			//*My codes begin here
                 //Disable the Maximize && Size Sub menus
			pSysMenu->EnableMenuItem(SC_SIZE, MF_GRAYED);
			pSysMenu->EnableMenuItem(SC_MAXIMIZE, MF_GRAYED);
			//My codes end here
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//My Codes  begin here
	// Don't forget to create the lookup table firstly
	Init_CRC32_Table();
	
	// Set the default text for the dialog box
	m_csFileName = "请输入或选择待检测的文件...";
	m_csCRC32 = "0";	
	UpdateData(FALSE);
	
	// Adding file Drag and Drop support
	DragAcceptFiles(TRUE);
	//My Codes end here
	// CG: The following block was added by the ToolTips component.
{
	// Create the ToolTip control.
	m_tooltip.Create(this);
	m_tooltip.Activate(TRUE);
		// TODO: Use one of the following forms to add controls:
	//m_tooltip.AddTool(GetDlgItem(IDC_<name>), <string-table-id>);
	 m_tooltip.AddTool(GetDlgItem(IDC_FILE_TO_CHECK),_T("请输入或选择待检测的文件") );
}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCRC_FICDlg::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 CCRC_FICDlg::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();
	}
}

HCURSOR CCRC_FICDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//* My Codes begin here!!!
void CCRC_FICDlg::OnBrowse() 
{
	// TODO: Add your control notification handler code here
     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 adds file Drag and Drop support.Just for better usage.
void CCRC_FICDlg::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();
			//UpdateData(FALSE);
		}
	}
	// Let CDialog release the memory used in hDropInfo
	CDialog::OnDropFiles(hDropInfo);
}

void CCRC_FICDlg::OnCancel()
{
    if(GetKeyState(VK_ESCAPE) & 128)
        return;
    CDialog::OnCancel();
}

void CCRC_FICDlg::Init_CRC32_Table()
{
   // 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 CCRC_FICDlg::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;
}

// This function uses the crc32_table lookup table to generate a CRC for csData
int CCRC_FICDlg::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;
}

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

	// Open the file and get ready to read it.
	// We're only opening an existing file by using CreateFile() function.
	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 in CRC32_ FIC");
		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 in CRC32_FIC");
		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_csCRC32= ch;
	m_csCRC32.MakeUpper();
	// 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);
}
//My Codes End Here!!!
BOOL CCRC_FICDlg::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following block was added by the ToolTips component.
{
	// Let the ToolTip process this message.
}
return CDialog::PreTranslateMessage(pMsg);	// CG: This was added by the ToolTips component.
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -