// HSCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "HSC.h"
#include "HSCDlg.h"
#include "afxdialogex.h"
#include <string.h>
#include <string>
#include <vector>


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#pragma warning(disable:4996)��

#define MAXSIZE 512*1024

// CHSCDlg dialog




CString _I2T(int nValue)
{
	CString szValue;
	char ch[20] = { 0 };
	itoa(nValue, ch, 10);
	szValue = ch;

	return szValue;
}

CString _I2T(unsigned int nValue)
{
	CString szValue;
	char ch[20] = { 0 };
	itoa(nValue, ch, 10);
	szValue = ch;

	return szValue;
}


template<size_t _size>
void get_current_time(char(&dest)[_size])
{
	SYSTEMTIME tm = { 0 };
	GetLocalTime(&tm);
	sprintf_s(dest, "%d%.2d%.2d%.2d%.2d%.2d", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
}


template<size_t _size>
void get_current_time_2(char(&dest)[_size])
{
	SYSTEMTIME tm = { 0 };
	GetLocalTime(&tm);
	sprintf_s(dest, "%d-%02d-%02d %02d:%02d:%02d", tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond);
}


CHSCDlg::CHSCDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CHSCDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHSCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_listctrl);
	DDX_Control(pDX, IDC_EDIT2, ED_CURNAME);
	DDX_Control(pDX, IDC_EDIT1, ED_CURVER);
}

BEGIN_MESSAGE_MAP(CHSCDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CHSCDlg::ChooseA)
	ON_BN_CLICKED(IDC_BUTTON2, &CHSCDlg::ChooseB)
	ON_WM_DROPFILES()
	ON_BN_CLICKED(IDC_BUTTON3, &CHSCDlg::Generate)
	ON_BN_CLICKED(IDC_BUTTON4, &CHSCDlg::ChooseC)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, &CHSCDlg::OnNMClickList1)
	ON_BN_CLICKED(IDC_UPDATE, &CHSCDlg::OnBnClickedUpdate)
	ON_BN_CLICKED(IDC_BUTTON6, &CHSCDlg::OnBnClickedButton6)
END_MESSAGE_MAP()


// CHSCDlg message handlers

//�����ַ��������
char MakecodeChar(char c, int key){
	return c = c^key;
}
//�����ַ�����
char CutcodeChar(char c, int key){
	return c^key;
}


//����
void Makecode(char *pstr, int *pkey){
	int len = strlen(pstr);//��ȡ����
	for (int i = 0; i<len; i++)
		*(pstr + i) = MakecodeChar(*(pstr + i), pkey[i % 5]);
}
//����
void Cutecode(char *pstr, int *pkey){
	int len = strlen(pstr);
	for (int i = 0; i<len; i++)
		*(pstr + i) = CutcodeChar(*(pstr + i), pkey[i % 5]);
}


CString Makecode_String(CString str)
{
	int key[] = { 1, 2, 3, 4, 5 };//�����ַ�

	char c[100] = { 0 };
	memcpy(c, str.GetString(), str.GetLength());
	Makecode(c, key);//����
	CString strcode = c;
	return strcode;
}


void writehex(FILE* fw,CString str)
{
	int len = str.GetLength();
	for (int i = 0; i < len;)
	{
		CString s = str.Mid(i, 2);
		char c[5];
		memcpy_s(c, 4, s.GetString(), 4);
		char d[5] = { 0 };
		sscanf_s(c, "%x", d);
		CString ds = CString(d);

		fwrite(d, 1, 1, fw);

		i += 2;
	}
}

void writehex_xor(FILE* fw, CString str,int or)
{
	int len = str.GetLength();

	std::string s = "";// = str;
	for (int i = 0; i < len;i++)
	{
		char c = str.GetAt(i);
		c = or^ c;
		char c2[3];
		itoa(c, c2, 10);
		s += c2;
	}
	str = s.c_str();

	for (int i = 0; i < len;)
	{
		CString s = str.Mid(i, 2);
		char c[5];
		memcpy_s(c, 4, s.GetString(), 4);
		char d[5] = { 0 };
		sscanf_s(c, "%x", d);
		CString ds = CString(d);

		

		fwrite(d, 1, 1, fw);

		i += 2;
	}
}


void writehex_little_end(FILE* fw, CString str)
{
	int len = str.GetLength();
	for (int i = len-2; i >= 0;)
	{
		CString s = str.Mid(i, 2);
		char c[5];
		memcpy_s(c, 4, s.GetString(), 4);
		char d[5] = { 0 };
		sscanf_s(c, "%x", d);
		CString ds = CString(d);

		fwrite(d, 1, 1, fw);

		i -= 2;
	}
}


void writestr(FILE* fw, CString str)
{
	int len = str.GetLength();
	for (int i = 0; i < len;)
	{
		CString s = str.Mid(i, 1);
		char c[3];
		memcpy_s(c, 2, s.GetString(), 2);
		char d[3] = { 0 };
		sscanf_s(c, "%c", d);
		CString ds = CString(d);

		fwrite(d, 1, 1, fw);

		i += 1;
	}
}

CString Int2HexString(int n)
{
	CString str = "";
	while (n / 256 > 0)
	{
		CString s = "";
		s.Format("%02x", n % 256);
		str = s + str;
		n = n / 256;
	}

	if (n % 256 != 0)
	{
		CString s = "";
		s.Format("%02x", n % 256);
		str = s + str;
	}

	return str;
}


CString Int2HexString(int n,int bytenum)
{
	CString str = "";
	while (n / 256 > 0)
	{
		CString s = "";
		s.Format("%02x", n % 256);
		str = s + str;
		n = n / 256;
	}

	if (n % 256 != 0)
	{
		CString s = "";
		s.Format("%02x", n % 256);
		str = s + str;
	}

	int diff = bytenum - str.GetLength() / 2;

	while (diff > 0)
	{
		str = "00" + str;
		diff--;
	}

	return str;
}


CString get_computer_name()
{
	char buffer[512];
	DWORD name_len = 512;
	CString name;

	GetComputerName((LPSTR)buffer, &name_len);
	name =buffer;

	return name;
}


CString get_user_name()
{
	char buffer[512];
	DWORD name_len = 512;
	CString name;

	GetUserName((LPSTR)buffer, &name_len);
	name = buffer;

	return name;
}




BOOL CHSCDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 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



	//CString str = "20200115cs.bin";

	//BYTE tmp[500] = { 0 };
	//memcpy_s(tmp, str.GetLength(), str.GetString(), str.GetLength());

	//int len = str.GetLength();

	CString name = get_computer_name();
	CString user = get_user_name();

	GetDlgItem(USER_MAIN)->SetWindowText(user);


	m_username = user;


	DWORD style = m_listctrl.GetExtendedStyle();
	m_listctrl.SetExtendedStyle(style | LVS_EX_FLATSB | LVS_EX_FULLROWSELECT);
	
	m_listctrl.SetBkColor(RGB(255, 255, 255));//m_DetailLstCtrl.SetBkColor( RGB(87,91,92) );
	m_listctrl.SetTextBkColor(RGB(255, 255, 255));//m_DetailLstCtrl.SetTextBkColor( RGB(87,91,92) );


	int index = 0;

	m_listctrl.InsertColumn(index++, "����", LVCFMT_LEFT, 150);
	m_listctrl.InsertColumn(index++, "�̼��汾��", LVCFMT_LEFT, 100);
	m_listctrl.InsertColumn(index++, "����汾��", LVCFMT_LEFT, 100);
	m_listctrl.InsertColumn(index++, "����ʱ��", LVCFMT_LEFT, 120);
	m_listctrl.InsertColumn(index++, "����", LVCFMT_LEFT, 100);
	m_listctrl.InsertColumn(index++, "crc", LVCFMT_LEFT, 100);
	m_listctrl.InsertColumn(index++, "��ʼ��ַ", LVCFMT_LEFT, 100);
	

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CHSCDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CHSCDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


char *strlowr(char *str)
{
	char *orign = str;
	for (; *str != '\0'; str++)
		*str = tolower(*str);
	return orign;
}

void CHSCDlg::ChooseA()
{
	
	CString strFile = _T("");

	CFileDialog    dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.bin)|*.bin|All Files (*.*)|*.*||"), NULL);

	if (dlgFile.DoModal())
	{
		m_path_A = dlgFile.GetPathName();
		m_name_A = dlgFile.GetFileName();
		

		//if (m_name_A !="" && m_name_A == m_name_B)
		//{
		//	AfxMessageBox("����һ�̼�ͬ���������м��!");
		//}


		char c[100] = { 0 };
		memcpy(c, m_name_A, m_name_A.GetLength());
		CString strlow = strlowr(c);

		if (strlow.Find("apps") == -1)
		{
			AfxMessageBox("��ѡ�����APPS�ַ������ļ���");
			return;
		}



		GetInfo_A();
	

	}




}


void CHSCDlg::ChooseB()
{
	CString strFile = _T("");

	CFileDialog    dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.bin)|*.bin|All Files (*.*)|*.*||"), NULL);

	if (dlgFile.DoModal())
	{
		m_path_B = dlgFile.GetPathName();
		m_name_B = dlgFile.GetFileName();


		//if (m_name_A != "" && m_name_A == m_name_B)
		//{
		//	AfxMessageBox("����һ�̼�ͬ���������м��!");
		//}

		char c[100] = { 0 };
		memcpy(c, m_name_B, m_name_B.GetLength());
		CString strlow = strlowr(c);
		if (strlow.Find("uiic") == -1)
		{
			AfxMessageBox("��ѡ�����UIIC�ַ������ļ���!");
			return;
		}

		GetInfo_B();


	}
}




SYSTEMTIME OnTime64toSystemTime(__time64_t& itime)
{
	struct tm *temptm = _localtime64(&itime);
	SYSTEMTIME st = { 1900 + temptm->tm_year,
		1 + temptm->tm_mon,
		temptm->tm_wday,
		temptm->tm_mday,
		temptm->tm_hour,
		temptm->tm_min,
		temptm->tm_sec,
		0 };
	return st;
}
void GetModifyDateTime(const wchar_t* strFilename, SYSTEMTIME& stLocal)
{
	struct _stat64i32 statbuf;
	_wstat64i32(strFilename, &statbuf);
	stLocal = OnTime64toSystemTime(statbuf.st_mtime);
}


unsigned short  crc16(unsigned char *pucData, long long nLen)
{
	unsigned char CRC16Lo, CRC16Hi, CH, CL, SaveHi, SaveLo, j;
	long long i;
	CRC16Lo = 0;
	CRC16Hi = 0;
	CH = 0XA0;
	CL = 0X01;
	for (i = 0; i<nLen; i++)  {
		CRC16Lo = CRC16Lo ^ (*(pucData + i));
		for (j = 0; j<8; j++)   {
			SaveHi = CRC16Hi;
			SaveLo = CRC16Lo;
			CRC16Hi = CRC16Hi >> 1;
			CRC16Lo = CRC16Lo >> 1;
			if ((SaveHi & 0x01) == 0x01)
				CRC16Lo = CRC16Lo | 0x80;
			if ((SaveLo & 0x01) == 0x01)  {
				CRC16Hi = CRC16Hi ^ CH;
				CRC16Lo = CRC16Lo ^ CL;
			}
		}
	}
	return CRC16Hi * 256 + CRC16Lo;
}



void CHSCDlg::GetInfo_A()
{
	CString name = m_name_A;//dif
	CString path = m_path_A;//dif
	

	//version
	CString strversion;

	char c[100] = { 0 };
	memcpy(c, name, name.GetLength());
	CString strlow = strlowr(c);

	int pos = strlow.Find("_v");
	if (pos == -1)
	{
		AfxMessageBox("�ļ�����ʽ���ԣ�");
		return;
	}
	CString vname = name.Right(name.GetLength() - pos - 2);

	int pos2 = vname.Find(".");
	vname = vname.Left(pos2);

	if (vname.GetLength() < 8)
	{
		AfxMessageBox("�ļ�����ʽ���ԣ�");
		return;
	}

	int count = 1;
	strversion += CString(vname[0]) + CString(vname[1]);
	for (int i = 2; i < vname.GetLength() - 2; i++)
	{
		if (vname[i] == '_')
		{
			count++;
			strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
		}
	}

	if (count != 3 && count != 4)
	{
		AfxMessageBox("�ļ����汾����Ŀ���ԣ�");
		return;
	}


	GetDlgItem(NAME_A)->SetWindowText(name);//dif

	BYTE* v = new BYTE[MAXSIZE];
	int i = 0;

	//open
	FILE *fr;
	fopen_s(&fr, path, "rb");
	if (fr == NULL) //���������
	{
		AfxMessageBox("�򿪹̼�ʧ�ܣ�");
		return;
	}


	//size
	fseek(fr, 0, SEEK_END);
	long int size = ftell(fr);
	fseek(fr, 0, SEEK_SET);
	if (size >= MAXSIZE)
	{
		AfxMessageBox("�ļ�̫��");
		return;
	}

	//scan
	while (fscanf_s(fr, "%c", &v[i]) != EOF) //��ȡ���ݵ����飬ֱ���ļ���β(����EOF)
	{
		i++;
	}
	fclose(fr);





	//crc
	UINT crc = crc16(v,size);


	


	

	//time
	CStringW namew;
	namew = path;
	SYSTEMTIME stLocal;
	GetModifyDateTime(namew, stLocal);


	char time[30] = { 0 };
	//get_current_time_2(time);


	sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
		stLocal.wYear, stLocal.wMonth, stLocal.wDay,
		stLocal.wHour, stLocal.wMinute, stLocal.wSecond);


	//��ʼ��ַ
	int addr = HEAD_LEN; //dif



	//showtext
	GetDlgItem(VERSION_A)->SetWindowText(strversion);	//dif
	GetDlgItem(TIME_A)->SetWindowText(time);			//dif
	GetDlgItem(LEN_A)->SetWindowText(_I2T(size));		//dif
	GetDlgItem(CRC_A)->SetWindowText("0x"+Int2HexString(crc));		//dif
	GetDlgItem(ADDR_A)->SetWindowText("0x" + Int2HexString(addr));		//dif


	m_version_A = strversion;	//dif
	m_time_A = time;		 	//dif
	m_len_A = _I2T(size);	 	//dif
	m_crc_A = _I2T(crc);	 	//dif
	m_addr_A = _I2T(addr);	 	//dif

	m_nLen_A = size;		//dif
	m_nCrc_A = crc;			//dif
	m_nAddr_A = addr;		//dif


	delete v;




	//A���� ��A�ı���ٴμ���B��C����ʼ��ַ

	int addrb = HEAD_LEN + m_nLen_A; //dif
	GetDlgItem(ADDR_B)->SetWindowText("0x" + Int2HexString(addrb));		//dif
	m_addr_B = _I2T(addrb);	 	//dif
	m_nAddr_B = addrb;		//dif


	int addrc = HEAD_LEN + m_nLen_A+ m_nLen_B; //dif
	GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addrc));		//dif
	m_addr_C = _I2T(addrc);	 	//dif
	m_nAddr_C = addrc;		//dif

}


void CHSCDlg::GetInfo_B()
{
	CString name = m_name_B;//dif
	CString path = m_path_B;//dif

	//version
	CString strversion;

	char c[100] = { 0 };
	memcpy(c, name, name.GetLength());
	CString strlow = strlowr(c);

	int pos = strlow.Find("_v");
	if (pos == -1)
	{
		AfxMessageBox("�ļ�����ʽ���ԣ�");
		return;
	}
	CString vname = name.Right(name.GetLength() - pos - 2);

	int pos2 = vname.Find(".");
	vname = vname.Left(pos2);

	if (vname.GetLength() < 8)
	{
		AfxMessageBox("�ļ�����ʽ���ԣ�");
		return;
	}

	int count = 1;
	strversion += CString(vname[0]) + CString(vname[1]);
	for (int i = 2; i < vname.GetLength() - 2; i++)
	{
		if (vname[i] == '_')
		{
			count++;
			strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
		}
	}

	if (count != 3 && count != 4)
	{
		AfxMessageBox("�ļ����汾����Ŀ���ԣ�");
		return;
	}


	GetDlgItem(NAME_B)->SetWindowText(name);//dif



	BYTE* v = new BYTE[MAXSIZE];
	int i = 0;

	//open
	FILE *fr;
	fopen_s(&fr, path, "rb");
	if (fr == NULL) //���������
	{
		AfxMessageBox("�򿪹̼�ʧ�ܣ�");
		return;
	}



	//size
	fseek(fr, 0, SEEK_END);
	long int size = ftell(fr);
	fseek(fr, 0, SEEK_SET);
	if (size >= MAXSIZE)
	{
		AfxMessageBox("�ļ�̫��");
		return;
	}

	//scan
	while (fscanf_s(fr, "%c", &v[i]) != EOF) //��ȡ���ݵ����飬ֱ���ļ���β(����EOF)
	{
		i++;
	}
	fclose(fr);



	//crc
	UINT crc = crc16(v, size);








	//time
	CStringW namew;
	namew = path;
	SYSTEMTIME stLocal;
	GetModifyDateTime(namew, stLocal);


	char time[30] = { 0 };
	//get_current_time_2(time);


	sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
		stLocal.wYear, stLocal.wMonth, stLocal.wDay,
		stLocal.wHour, stLocal.wMinute, stLocal.wSecond);


	//��ʼ��ַ
	int addr = HEAD_LEN + m_nLen_A; //dif

	//showtext
	GetDlgItem(VERSION_B)->SetWindowText(strversion);	//dif
	GetDlgItem(TIME_B)->SetWindowText(time);			//dif
	GetDlgItem(LEN_B)->SetWindowText(_I2T(size));		//dif
	GetDlgItem(CRC_B)->SetWindowText("0x" + Int2HexString(crc));		//dif
	GetDlgItem(ADDR_B)->SetWindowText("0x" + Int2HexString(addr));		//dif

	m_version_B = strversion;	//dif
	m_time_B = time;		 	//dif
	m_len_B = _I2T(size);	 	//dif
	m_crc_B = _I2T(crc);	 	//dif
	m_addr_B = _I2T(addr);	 	//dif

	m_nLen_B = size;		//dif
	m_nCrc_B = crc;			//dif
	m_nAddr_B = addr;		//dif

	delete v;



	//B���� ��B�ı���ٴμ���C����ʼ��ַ

	int addrc = HEAD_LEN + m_nLen_A + m_nLen_B; //dif
	GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addrc));		//dif
	m_addr_C = _I2T(addrc);	 	//dif
	m_nAddr_C = addrc;		//dif
}

void CHSCDlg::GetInfo_C()
{
	CString name = m_name_C;//dif
	CString path = m_path_C;//dif

	//version
	CString strversion;

	char c[100] = { 0 };
	memcpy(c, name, name.GetLength());
	CString strlow = strlowr(c);

	int pos = strlow.Find("_v");
	if (pos == -1)
	{
		AfxMessageBox("�ļ�����ʽ���ԣ�");
		return;
	}
	CString vname = name.Right(name.GetLength() - pos - 2);

	int pos2 = vname.Find(".");
	vname = vname.Left(pos2);

	if (vname.GetLength() < 8)
	{
		AfxMessageBox("�ļ�����ʽ���ԣ�");
		return;
	}

	int count = 1;
	strversion += CString(vname[0]) + CString(vname[1]);
	for (int i = 2; i < vname.GetLength() - 2; i++)
	{
		if (vname[i] == '_')
		{
			count++;
			strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
		}
	}

	if (count != 3 && count != 4)
	{
		AfxMessageBox("�ļ����汾����Ŀ���ԣ�");
		return;
	}


	GetDlgItem(NAME_C)->SetWindowText(name);//dif



	BYTE* v = new BYTE[MAXSIZE];
	int i = 0;

	//open
	FILE *fr;
	fopen_s(&fr, path, "rb");
	if (fr == NULL) //���������
	{
		AfxMessageBox("�򿪹̼�ʧ�ܣ�");
		return;
	}



	//size
	fseek(fr, 0, SEEK_END);
	long int size = ftell(fr);
	fseek(fr, 0, SEEK_SET);
	if (size >= MAXSIZE)
	{
		AfxMessageBox("�ļ�̫��");
		return;
	}

	//scan
	while (fscanf_s(fr, "%c", &v[i]) != EOF) //��ȡ���ݵ����飬ֱ���ļ���β(����EOF)
	{
		i++;
	}
	fclose(fr);



	//crc
	UINT crc = crc16(v, size);








	//time
	CStringW namew;
	namew = path;
	SYSTEMTIME stLocal;
	GetModifyDateTime(namew, stLocal);


	char time[30] = { 0 };
	//get_current_time_2(time);


	sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
		stLocal.wYear, stLocal.wMonth, stLocal.wDay,
		stLocal.wHour, stLocal.wMinute, stLocal.wSecond);


	//��ʼ��ַ
	int addr = HEAD_LEN + m_nLen_A + m_nLen_B; //dif

	//showtext
	GetDlgItem(VERSION_C)->SetWindowText(strversion);	//dif
	GetDlgItem(TIME_C)->SetWindowText(time);			//dif
	GetDlgItem(LEN_C)->SetWindowText(_I2T(size));		//dif
	GetDlgItem(CRC_C)->SetWindowText("0x" + Int2HexString(crc));		//dif
	GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addr));		//dif

	m_version_C = strversion;	//dif
	m_time_C = time;		 	//dif
	m_len_C = _I2T(size);	 	//dif
	m_crc_C = _I2T(crc);	 	//dif
	m_addr_C = _I2T(addr);	 	//dif

	m_nLen_C = size;		//dif
	m_nCrc_C = crc;			//dif
	m_nAddr_C = addr;		//dif

	delete v;
}

char *GetFilename(char *p)
{
	int x = strlen(p);
	char ch = '\\';
	char *q = strrchr(p, ch) + 1;

	return q;
}

void CHSCDlg::OnDropFiles(HDROP hDropInfo)
{
	
	UINT count;
	TCHAR filePath[MAX_PATH] = { 0 };

	count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);//�ӳɹ����ϷŲ����м����ļ������ơ���ȡ������ק�ļ�����Ŀ

	/*
	if (count == 1)//���ֻ��קһ���ļ���
	{
		DragQueryFile(hDropInfo, 0, filePath, sizeof(filePath));//�����ק���ļ���
		CString m_szPath = filePath;

		char ch[300] = { 0 };
		memcpy(ch, m_szPath, m_szPath.GetLength());
		CString name = GetFilename(ch);
		char c[100] = { 0 };
		memcpy(c, name, name.GetLength());
		CString strlow = strlowr(c);

		if (strlow.Find("apps") != -1)
		{
			m_path_A = m_szPath;
			m_name_A = name;
			GetInfo_A();
		}
		else if (strlow.Find("uiic") != -1)
		{

			m_path_B = m_szPath;
			m_name_B = name;
			GetInfo_B();
		}
		else if (strlow.Find("com") != -1)
		{

			m_path_C = m_szPath;
			m_name_C = name;
			GetInfo_C();
		}
		else
		{
			AfxMessageBox("û�ҵ�����APPS��UIIC��COM�ַ������ļ���");
		}


		UpdateData(FALSE);
		DragFinish(hDropInfo);//�Ϸųɹ����ͷ��ڴ�

		CDialog::OnDropFiles(hDropInfo);
		return;

	}
	else//�����ק����ļ���
	*/
	{
		//m_vectorFile.clear();
		int bfind = 0;
		for (UINT i = 0; i<count; i++)
		{
			int pathLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath));
			CString m_szPath = filePath;
			m_szPath = filePath;


			char ch[300] = { 0 };
			memcpy(ch, m_szPath, m_szPath.GetLength());
			CString name = GetFilename(ch);
			char c[100] = { 0 };
			memcpy(c, name, name.GetLength());
			CString strlow = strlowr(c);

			HSCDATA data;
			
			if (GetInfo(name, m_szPath, data))
			{
				dataset.insertdata(data);


			}



			if (strlow.Find("apps") != -1)
			{
				m_path_A = m_szPath;
				m_name_A = name;
				GetInfo_A();
				bfind = 1;
			}
			else if (strlow.Find("uiic") != -1)
			{

				m_path_B = m_szPath;
				m_name_B = name;
				GetInfo_B();
				bfind = 1;
			}
			else if (strlow.Find("com") != -1)
			{

				m_path_C = m_szPath;
				m_name_C = name;
				GetInfo_C();
				bfind = 1;
			}


			
			//m_vectorFile.push_back(filePath);
			//break;
		}

		if (!bfind)
		{
			AfxMessageBox("û�ҵ�����APPS��UIIC��COM�ַ������ļ���");
		}

		UpdateData(FALSE);
		DragFinish(hDropInfo);
	}



	updatelistctrl();


	CDialogEx::OnDropFiles(hDropInfo);
}


void CHSCDlg::Generate()
{
	if (m_name_A == "" || m_name_B == "" || m_name_C == "")
	{
		AfxMessageBox("��ѡ�������̼���");
		return;
	}


	//1
	BYTE* v1 = new BYTE[MAXSIZE];
	int i = 0;
	FILE *fr1;
	fopen_s(&fr1, m_path_A, "rb");
	if (fr1 == NULL) //���������
	{
		AfxMessageBox("�򿪹̼�ʧ�ܣ�");
		return;
	}

	fseek(fr1, 0, SEEK_END);
	long int size = ftell(fr1);
	fseek(fr1, 0, SEEK_SET);
	if (size >= MAXSIZE)
	{
		AfxMessageBox("�ļ�̫��");
		return;
	}

	while (fscanf(fr1, "%c", &v1[i]) != EOF) //��ȡ���ݵ����飬ֱ���ļ���β(����EOF)
	{
		i++;
	}

	int count1 = i;
	fclose(fr1);



	//2
	char* v2 = new char[MAXSIZE];
	i = 0;
	FILE *fr2;
	fopen_s(&fr2, m_path_B, "rb");
	if (fr2 == NULL) //���������
	{
		AfxMessageBox("�򿪹̼�ʧ�ܣ�");
		return;
	}

	fseek(fr2, 0, SEEK_END);
	size = ftell(fr2);
	fseek(fr2, 0, SEEK_SET);
	if (size >= MAXSIZE)
	{
		AfxMessageBox("�ļ�̫��");
		return;
	}

	while (fscanf(fr2, "%c", &v2[i]) != EOF) //��ȡ���ݵ����飬ֱ���ļ���β(����EOF)
	{
		i++;
	}

	int count2 = i;
	fclose(fr2);


	//3
	char* v3 = new char[MAXSIZE];
	i = 0;
	FILE *fr3;
	fopen_s(&fr3, m_path_C, "rb");
	if (fr3 == NULL) //���������
	{
		AfxMessageBox("�򿪹̼�ʧ�ܣ�");
		return;
	}

	fseek(fr3, 0, SEEK_END);
	size = ftell(fr3);
	fseek(fr3, 0, SEEK_SET);
	if (size >= MAXSIZE)
	{
		AfxMessageBox("�ļ�̫��");
		return;
	}

	while (fscanf(fr3, "%c", &v3[i]) != EOF) //��ȡ���ݵ����飬ֱ���ļ���β(����EOF)
	{
		i++;
	}

	int count3 = i;
	fclose(fr3);


	//
	CString strV;
	GetDlgItem(VERSION_MAIN)->GetWindowText(strV);
	if (strV.GetLength() != 8)
	{
		AfxMessageBox("������8λ�����汾�ţ�");
		return;
	}


	CString strVA;
	GetDlgItem(VERSION2_A)->GetWindowText(strVA);
	if (strVA.GetLength() != 8)
	{
		AfxMessageBox("������8λ������̼��汾�ţ�");
		return;
	}

	CString strVB;
	GetDlgItem(VERSION2_B)->GetWindowText(strVB);
	if (strVB.GetLength() != 8)
	{
		AfxMessageBox("������8λ��UI�̼��汾�ţ�");
		return;
	}


	CString strVC;
	GetDlgItem(VERSION2_C)->GetWindowText(strVC);
	if (strVC.GetLength() != 8)
	{
		AfxMessageBox("������8λ��COM�̼��汾�ţ�");
		return;
	}


	CString firmname = "firmware_" + strV.Mid(0, 2) + "_" + strV.Mid(2, 2) 
		+ "_" + strV.Mid(4, 2) + "_" + strV.Mid(6, 2) + ".hsc";


	FILE *fini;
	fopen_s(&fini, "HSC.ini", "wb");
	fwrite(firmname, firmname.GetLength(),1, fini);
	fclose(fini);



	FILE *fw;
	fopen_s(&fw, firmname, "wb");

	//writehex(fw, "00012345");
	//writehex_little_end(fw, "00012345");




	//��¼�ļ����汾��	
	writehex(fw, strV);

	//��¼�ļ�����ʱ��
	char time[15];
	get_current_time(time);
	GetDlgItem(TIME_MAIN)->SetWindowText(time);
	CString strtime = "00" + CString(time);

	writehex(fw, strtime);

	//��¼�ļ������������ڵ����û���
	CString struser = m_username;
	if (struser.GetLength() > 32)
	{
		struser = struser.Left(32);
	}

	int nullnum = 32 - struser.GetLength();
	for (auto i = 0; i < nullnum; i++)
	{
		writehex(fw, "00");
	}
	
	writestr(fw, struser);


	

	//�̼�A�汾��
	if (m_version_A.GetLength() == 6)
	{
		writehex(fw, "00");
	}
	writehex(fw, m_version_A);

	//����Ӳ��A�汾��
	writehex(fw, strVA);

	//�̼�A����ʱ��

	CString stime = m_time_A;
	CString strtimeA = "00" + stime.Mid(0, 4) + stime.Mid(5, 2) + stime.Mid(8, 2) + stime.Mid(11, 2) + stime.Mid(14, 2) + stime.Mid(17, 2);
	writehex(fw, strtimeA);

	//�̼�A����
	CString strLenA = Int2HexString(m_nLen_A,4);
	writehex_little_end(fw, strLenA);

	//�̼�A��CRC16
	CString strCrcA = Int2HexString(m_nCrc_A,4);
	writehex_little_end(fw, strCrcA);

	//�̼�A���ļ��е���ʼ��ַ
	CString strAddrA = Int2HexString(m_nAddr_A,4);
	writehex_little_end(fw, strAddrA);




	//�̼�B�汾��
	if (m_version_B.GetLength() == 6)
	{
		writehex(fw, "00");
	}
	writehex(fw, m_version_B);

	//����Ӳ��B�汾��
	writehex(fw, strVB);

	//�̼�B����ʱ��
	stime = m_time_B;
	CString strtimeB = "00" + stime.Mid(0, 4) + stime.Mid(5, 2) + stime.Mid(8, 2) + stime.Mid(11, 2) + stime.Mid(14, 2) + stime.Mid(17, 2);
	writehex(fw, strtimeB);

	//�̼�B����
	CString strLenB = Int2HexString(m_nLen_B, 4);
	writehex_little_end(fw, strLenB);

	//�̼�B��CRC16
	CString strCrcB = Int2HexString(m_nCrc_B, 4);
	writehex_little_end(fw, strCrcB);

	//�̼�B���ļ��е���ʼ��ַ
	CString strAddrB = Int2HexString(m_nAddr_B, 4);
	writehex_little_end(fw, strAddrB);



	//�̼�C�汾��
	if (m_version_C.GetLength() == 6)
	{
		writehex(fw, "00");
	}
	writehex(fw, m_version_C);

	//����Ӳ��C�汾��
	writehex(fw, strVC);

	//�̼�C����ʱ��
	stime = m_time_C;
	CString strtimeC = "00" + stime.Mid(0, 4) + stime.Mid(5, 2) + stime.Mid(8, 2) + stime.Mid(11, 2) + stime.Mid(14, 2) + stime.Mid(17, 2);
	writehex(fw, strtimeC);

	//�̼�C����
	CString strLenC = Int2HexString(m_nLen_C, 4);
	writehex_little_end(fw, strLenC);

	//�̼�C��CRC16
	CString strCrcC = Int2HexString(m_nCrc_C, 4);
	writehex_little_end(fw, strCrcC);

	//�̼�C���ļ��е���ʼ��ַ
	CString strAddrC = Int2HexString(m_nAddr_C, 4);
	writehex_little_end(fw, strAddrC);



	/*�̼���CRC16����ʹ�ó�ʼԴ�ļ���������������CRC16��
	����д�뵽��Ӧ�̼����ƫ�Ƶ�ַΪ0x1C��4���ֽ���*/
	int crc_beg = 0x1c;


	v1[crc_beg] = m_nCrc_A % 256;
	v1[crc_beg + 1] = m_nCrc_A / 256;

	v2[crc_beg] = m_nCrc_B % 256;
	v2[crc_beg + 1] = m_nCrc_B / 256;

	v3[crc_beg] = m_nCrc_C % 256;
	v3[crc_beg + 1] = m_nCrc_C / 256;


	/*�̼����ܷ�ʽ��ʹ���ļ����ȣ�ȡ��͵�һ�ֽڣ���Դ�ļ����ݽ���������㣬
	��ǰ200���ֽ����ν������Ȼ���޸������µĹ̼������ٷ�����¼�ļ������С�
	���磺�ļ�����Ϊ0x7452��29778bytes����ȡ0x52��Դ�ļ����ݵ�ǰ200�ֽ����ν���������㣬
	�ٸ��ǵ�ԭ���ֽڵ�λ���ϣ����ɵ��ļ����Ǽ��ܺ�����ݿ�*/

	//���ܴ�����Ĺ̼�A���ݿ�
	int lowbyte = count1 & 0xff;
	for (int i = 0; i < 200 && i < count1; i++)
	{
		v1[i] = lowbyte ^ v1[i];
	}

	for (int i = 0; i < count1; i++)
	{
		fwrite(&v1[i], 1, 1, fw);
	}


	//���ܴ�����Ĺ̼�B���ݿ�
	lowbyte = count2 & 0xff;
	for (int i = 0; i < 200 && i < count2; i++)
	{
		v2[i] = lowbyte ^ v2[i];
	}

	for (int i = 0; i < count2; i++)
	{
		fwrite(&v2[i], 1, 1, fw);
	}


	//���ܴ�����Ĺ̼�C���ݿ�
	lowbyte = count3 & 0xff;
	for (int i = 0; i < 200 && i < count3; i++)
	{
		v3[i] = lowbyte ^ v3[i];
	}

	for (int i = 0; i < count3; i++)
	{
		fwrite(&v3[i], 1, 1, fw);
	}


	fclose(fw);



	delete v1;
	delete v2;
	delete v3;


	AfxMessageBox("�����ļ��ɹ���");
}


void CHSCDlg::ChooseC()
{
	CString strFile = _T("");

	CFileDialog    dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.bin)|*.bin|All Files (*.*)|*.*||"), NULL);

	if (dlgFile.DoModal())
	{
		m_path_C = dlgFile.GetPathName();
		m_name_C = dlgFile.GetFileName();


		//if (m_name_A != "" && m_name_A == m_name_B)
		//{
		//	AfxMessageBox("����һ�̼�ͬ���������м��!");
		//}

		char c[100] = { 0 };
		memcpy(c, m_name_C, m_name_C.GetLength());
		CString strlow = strlowr(c);
		if (strlow.Find("com") == -1)
		{
			AfxMessageBox("��ѡ�����COM�ַ������ļ���!");
			return;
		}

		GetInfo_C();


	}
}


int CHSCDlg::GetInfo(CString name, CString path, HSCDATA& data)
{
	data.name = name;
	data.path = path;

	//version
	CString strversion;

	char c[100] = { 0 };
	memcpy(c, name, name.GetLength());
	CString strlow = strlowr(c);

	int pos = strlow.Find("_v");
	if (pos == -1)
	{
		AfxMessageBox("�ļ�����ʽ���ԣ�");
		return 0;
	}
	CString vname = name.Right(name.GetLength() - pos - 2);

	int pos2 = vname.Find(".");
	vname = vname.Left(pos2);

	if (vname.GetLength() < 8)
	{
		AfxMessageBox("�ļ�����ʽ���ԣ�");
		return 0;
	}

	int count = 1;
	strversion += CString(vname[0]) + CString(vname[1]);
	for (int i = 2; i < vname.GetLength() - 2; i++)
	{
		if (vname[i] == '_')
		{
			count++;
			strversion += CString(vname[i + 1]) + CString(vname[i + 2]);
		}
	}

	if (count != 3 && count != 4)
	{
		AfxMessageBox("�ļ����汾����Ŀ���ԣ�");
		return 0;
	}


	//GetDlgItem(NAME_A)->SetWindowText(name);//dif

	BYTE* v = new BYTE[MAXSIZE];
	int i = 0;

	//open
	FILE *fr;
	fopen_s(&fr, path, "rb");
	if (fr == NULL) //���������
	{
		AfxMessageBox("�򿪹̼�ʧ�ܣ�");
		return 0;
	}


	//size
	fseek(fr, 0, SEEK_END);
	long int size = ftell(fr);
	fseek(fr, 0, SEEK_SET);
	if (size >= MAXSIZE)
	{
		AfxMessageBox("�ļ�̫��");
		return 0;
	}

	//scan
	while (fscanf_s(fr, "%c", &v[i]) != EOF) //��ȡ���ݵ����飬ֱ���ļ���β(����EOF)
	{
		i++;
	}
	fclose(fr);





	//crc
	UINT crc = crc16(v, size);





	//time
	CStringW namew;
	namew = path;
	SYSTEMTIME stLocal;
	GetModifyDateTime(namew, stLocal);


	char time[30] = { 0 };
	//get_current_time_2(time);


	sprintf(time, "%d-%02d-%02d %02d:%02d:%02d",
		stLocal.wYear, stLocal.wMonth, stLocal.wDay,
		stLocal.wHour, stLocal.wMinute, stLocal.wSecond);


	//��ʼ��ַ
	//int addr = dataset.getbeginaddr();



	//showtext
	//GetDlgItem(VERSION_A)->SetWindowText(strversion);	//dif
	//GetDlgItem(TIME_A)->SetWindowText(time);			//dif
	//GetDlgItem(LEN_A)->SetWindowText(_I2T(size));		//dif
	//GetDlgItem(CRC_A)->SetWindowText("0x" + Int2HexString(crc));		//dif
	//GetDlgItem(ADDR_A)->SetWindowText("0x" + Int2HexString(addr));		//dif


	data.version = strversion;	
	data.time = time;		 	
	data.len = _I2T(size);	 	
	data.crc = _I2T(crc);	 	
	//data.addr = _I2T(addr);	 	

	data.nLen = size;		
	data.nCrc = crc;			
	//data.nAddr = addr;		


	delete v;

	return 1;


	////A���� ��A�ı���ٴμ���B��C����ʼ��ַ

	//int addrb = HEAD_LEN + m_nLen_A; //dif
	//GetDlgItem(ADDR_B)->SetWindowText("0x" + Int2HexString(addrb));		//dif
	//m_addr_B = _I2T(addrb);	 	//dif
	//m_nAddr_B = addrb;		//dif


	//int addrc = HEAD_LEN + m_nLen_A + m_nLen_B; //dif
	//GetDlgItem(ADDR_C)->SetWindowText("0x" + Int2HexString(addrc));		//dif
	//m_addr_C = _I2T(addrc);	 	//dif
	//m_nAddr_C = addrc;		//dif

}

void CHSCDlg::updatelistctrl()
{
	m_listctrl.DeleteAllItems();
	int index = 0;

	for (auto data:dataset.getdatas())
	{
		m_listctrl.InsertItem(index, _T(""));
		m_listctrl.SetItemText(index, 0, data.name);
		m_listctrl.SetItemText(index, 1, data.version);
		m_listctrl.SetItemText(index, 2, data.version_add);
		m_listctrl.SetItemText(index, 3, data.time);
		m_listctrl.SetItemText(index, 4, data.len);

		

		m_listctrl.SetItemText(index, 5, "0x" + Int2HexString(data.nCrc));
		m_listctrl.SetItemText(index, 6, "0x" + Int2HexString(data.nAddr));

		index++;
	}
}

int g_cursel = -1;
void CHSCDlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: Add your control notification handler code here
	
	
	CString str;
	g_cursel = m_listctrl.GetSelectionMark();

	if (g_cursel >= 0 && g_cursel < dataset.getcount())
	{
		HSCDATA data = dataset.m_datas[g_cursel];


		ED_CURNAME.SetWindowText(data.name);
		ED_CURVER.SetWindowText(data.version_add);

	}
	
	
	
	
	*pResult = 0;
}


void CHSCDlg::OnBnClickedUpdate()
{

	if (g_cursel >= 0 && g_cursel < dataset.getcount())
	{

		CString strV;
		ED_CURVER.GetWindowText(strV);
		if (strV.GetLength() != 8)
		{
			AfxMessageBox("������8λ�Ĺ̼��汾�ţ�");
			return;
		}

		dataset.m_datas[g_cursel].version_add = strV;

		updatelistctrl();

	}
	else
	{
		AfxMessageBox("ûѡ�У�");
	}


}


void CHSCDlg::OnBnClickedButton6()
{
	
	for (auto data : dataset.m_datas)
	{
		if (data.version_add.GetLength() != 8)
		{
			AfxMessageBox("������8λ�Ĺ̼��汾�ţ�");
			return;
		}
	}








	//
	CString strV;
	GetDlgItem(VERSION_MAIN)->GetWindowText(strV);
	if (strV.GetLength() != 8)
	{
		AfxMessageBox("������8λ�����汾�ţ�");
		return;
	}


	CString firmname = "firmware_" + strV.Mid(0, 2) + "_" + strV.Mid(2, 2)
		+ "_" + strV.Mid(4, 2) + "_" + strV.Mid(6, 2) + ".hsc";


	FILE *fini;
	fopen_s(&fini, "HSC.ini", "wb");
	fwrite(firmname, firmname.GetLength(), 1, fini);
	fclose(fini);



	FILE *fw;
	fopen_s(&fw, firmname, "wb");

	//��¼�ļ����汾��	
	writehex(fw, strV);

	//��¼�ļ�����ʱ��
	char time[15];
	get_current_time(time);
	GetDlgItem(TIME_MAIN)->SetWindowText(time);
	CString strtime = "00" + CString(time);

	writehex(fw, strtime);

	//��¼�ļ������������ڵ����û���
	CString struser = m_username;
	if (struser.GetLength() > 32)
	{
		struser = struser.Left(32);
	}

	int nullnum = 32 - struser.GetLength();
	for (auto i = 0; i < nullnum; i++)
	{
		writehex(fw, "00");
	}

	writestr(fw, struser);


	//����crc
	for (auto &data : dataset.m_datas)
	{
		BYTE* v1 = new BYTE[MAXSIZE];
		int i = 0;
		FILE *fr1;
		fopen_s(&fr1, data.path, "rb");
		if (fr1 == NULL) //���������
		{
			AfxMessageBox("�򿪹̼�ʧ�ܣ�");
			return;
		}

		fseek(fr1, 0, SEEK_END);
		long int size = ftell(fr1);
		fseek(fr1, 0, SEEK_SET);
		if (size >= MAXSIZE)
		{
			AfxMessageBox("�ļ�̫��");
			return;
		}

		while (fscanf(fr1, "%c", &v1[i]) != EOF) //��ȡ���ݵ����飬ֱ���ļ���β(����EOF)
		{
			i++;
		}

		data.count = i;
		fclose(fr1);


		int crc_beg = 0x1c;

		v1[crc_beg] = data.nCrc % 256;
		v1[crc_beg + 1] = data.nCrc / 256;

		data.newcrc = crc16(v1, size);

		delete v1;

	}



	for (auto data : dataset.m_datas)
	{
		//�̼��汾��
		if (data.version.GetLength() == 6)
		{
			writehex(fw, "00");
		}
		writehex(fw, data.version);

		//����Ӳ���汾��
		writehex(fw, data.version_add);

		//�̼�����ʱ��

		CString stime = data.time;
		CString strtimeA = "00" + stime.Mid(0, 4) + stime.Mid(5, 2) + stime.Mid(8, 2) + stime.Mid(11, 2) + stime.Mid(14, 2) + stime.Mid(17, 2);
		writehex(fw, strtimeA);

		//�̼�����
		CString strLenA = Int2HexString(data.nLen, 4);
		writehex_little_end(fw, strLenA);

		//�̼���CRC16
		//CString strCrcA = Int2HexString(data.nCrc, 4);
		CString strCrcA = Int2HexString(data.newcrc, 4);//��crc
		writehex_little_end(fw, strCrcA);

		//�̼����ļ��е���ʼ��ַ
		CString strAddrA = Int2HexString(data.nAddr, 4);
		writehex_little_end(fw, strAddrA);

	}


	for (auto data : dataset.m_datas)
	{
		BYTE* v1 = new BYTE[MAXSIZE];
		int i = 0;
		FILE *fr1;
		fopen_s(&fr1, data.path, "rb");
		if (fr1 == NULL) //���������
		{
			AfxMessageBox("�򿪹̼�ʧ�ܣ�");
			return;
		}

		fseek(fr1, 0, SEEK_END);
		long int size = ftell(fr1);
		fseek(fr1, 0, SEEK_SET);
		if (size >= MAXSIZE)
		{
			AfxMessageBox("�ļ�̫��");
			return;
		}

		while (fscanf(fr1, "%c", &v1[i]) != EOF) //��ȡ���ݵ����飬ֱ���ļ���β(����EOF)
		{
			i++;
		}

		data.count = i;
		fclose(fr1);


		int crc_beg = 0x1c;

		v1[crc_beg] = data.nCrc % 256;
		v1[crc_beg + 1] = data.nCrc / 256;


		int lowbyte = data.count & 0xff;
		for (int i = 0; i < 200 && i < data.count; i++)
		{
			v1[i] = lowbyte ^ v1[i];
		}

		//
		UINT newcrc = crc16(v1, size);



		for (int i = 0; i < data.count; i++)
		{
			fwrite(&v1[i], 1, 1, fw);
		}

		delete v1;

	}

	fclose(fw);
	AfxMessageBox("�����ļ��ɹ���");
}