فهرست منبع

Z991239-972 #comment feat:download 实体 合并国密代码进来

刘文涛80174520 5 سال پیش
والد
کامیت
574a74d0c2

+ 3 - 0
Module/include/EventCode.h

@@ -66,6 +66,9 @@ static const char* Device_Type_Table[] = {
 #define ERR_SYSTEMCUSTOMIZATION_LOAD_DLL             0x10B10007  //加载DLL错误
 /*----SystemCustomization--modify--by--LZM--at--2020/4/8----*/
 
+#define EVENT_MOD_DOWNLOAD_STARTSYNC			0x10500032		// 开始同步
+#define EVENT_MOD_DOWNLOAD_SYNCHRONIZED			0x10500031		// 完成同步
+
 /*----MaintainWatcher--modify--by--LZM--at--2020/4/8----*/
 #define EVENT_UKEY_INSERTED  						 0x21000001  //维护证书插入,检测到维护证书插入
 #define EVENT_UKEY_LOW_LEVEL  						 0x21000002  //具有维护权,插入的维护证书具有本机器的维护权限,用于驱动本地维护状态

+ 11 - 4
Module/mod_download/CMakeLists.txt

@@ -19,6 +19,13 @@ set(${MODULE_PREFIX}_SRCS
 set(MOD_VERSION_STRING "1.0.0-dev1")
 add_module_libraries(${MODULE_PREFIX} ${MODULE_NAME} ${MOD_VERSION_STRING})
 #附加包含的目录
+
+conan_cmake_run(REQUIRES scew/1.1.7@LR04.02_ThirdParty/stable
+BASIC_SETUP CMAKE_TARGETS
+BUILD missing)
+
+
+
 target_include_directories(${MODULE_NAME} PRIVATE
 	${RVC_FRAMEWORK_INCLUDES_DIR}
 	${CONAN_INCLUDE_DIRS_SCEW}
@@ -28,14 +35,14 @@ target_include_directories(${MODULE_NAME} PRIVATE
 target_link_directories(${MODULE_NAME} PRIVATE
 	${CONAN_LIB_DIRS_SCEW}
 	${CONAN_LIB_DIRS_EXPAT}
-)
-
-target_link_directories(${MODULE_NAME} PRIVATE
 	${ThirdPartyLib}
 )
 
 # 添加实体需要依赖的其他共享库(包括系统库):连接器包含的包
-set(${MODULE_PREFIX}_LIBS ${MODULE_BASE_ALL_LIBS} scew expat Advapi32)
+set(${MODULE_PREFIX}_LIBS ${MODULE_BASE_ALL_LIBS} scew expat)
+if(WIN32)
+target_link_libraries(${MODULE_NAME} ${${MODULE_PREFIX}_LIBS} Advapi32)  
+endif(WIN32)
 
 target_link_libraries(${MODULE_NAME} ${${MODULE_PREFIX}_LIBS})  
 

+ 24 - 2
Module/mod_download/DownloadFSM.cpp

@@ -925,7 +925,8 @@ void DownloadFSM::s3_on_entry()
 
 	if(m_currentFileTask->size()>0){
 		Dbg("sendListreq ,count [%d]",m_currentFileTask->size());
-		m_pConnection->SendListReq();//发送获取文件列表请求
+		//m_pConnection->SendListReq();//发送获取文件列表请求
+		m_pConnection->SendListNReq();//发送获取新国密文件列表请求
 	}else{
 		//无任务时跳回S1
 		Dbg("no new files to download");
@@ -1082,10 +1083,19 @@ unsigned int DownloadFSM::s4_on_event(FSMEvent* event)
 
 				if (m_current->offset_block_id<<15 >= file->length){
 					//因需要计算md5值,故需要先关闭前面连接句柄,否则打开文件失败
+#ifdef RVC_OS_WIN
 					if (m_current->temp_handle != INVALID_HANDLE_VALUE) {
 						CloseHandle(m_current->temp_handle);
 						m_current->temp_handle = INVALID_HANDLE_VALUE;
 					}
+#else
+					if (m_current->temp_handle != NULL) {
+						fclose(m_current->temp_handle);
+						m_current->temp_handle = NULL;
+					}
+#endif // RVC_OS_WIN
+
+
 					//判断是否MD5一致,一致则下载成功,否则删除临时文件,重新等待下次下载
 					int flag = download_check_MD5(m_current,file);
 					if(flag==0){
@@ -1145,7 +1155,13 @@ void DownloadFSM::s5_on_entry()
 		for (i = 0; i < arr->nelts; ++i) {
 			char *path = (char*)ARRAY_IDX(arr, i, char*);
 			if (str_has_suffix(path, ".temp") || str_has_suffix(path, ".info")) {
+#ifdef RVC_OS_WIN
 				DeleteFileA(path);
+#else
+				remove(path);
+#endif // RVC_OS_WIN
+
+				
 			}
 		}
 		toolkit_array_free2(arr);
@@ -1185,11 +1201,17 @@ void DownloadFSM::nextTask(){
 			//打开文件成功,判断是否完成
 			if (m_current->offset_block_id<<15 >= file->length) { // finished!
 				//因需要计算md5值,故需要先关闭前面连接句柄,否则打开文件失败
+#ifdef RVC_OS_WIN
 				if (m_current->temp_handle != INVALID_HANDLE_VALUE) {
 					CloseHandle(m_current->temp_handle);
 					m_current->temp_handle = INVALID_HANDLE_VALUE;
 				}
-
+#else
+				if (m_current->temp_handle != NULL) {
+					fclose(m_current->temp_handle);
+					m_current->temp_handle = NULL;
+				}
+#endif // RVC_OS_WIN
 				int flag = download_check_MD5(m_current,file);
 
 				//判断是否MD5一致,一致则下载成功,否则删除临时文件,重新等待下次下载

+ 76 - 2
Module/mod_download/DownloadFSM.h

@@ -22,6 +22,19 @@ struct ListAns
 	int ResultCode;
 	char ListXmlContent[0]; // no more than 32k
 };
+//新国密文件列表接口20200210 lwt
+// [StructName("LISTNREQ")]
+struct ListNReq
+{
+	char TerminalNo[16];
+};
+//[StructName("LISTNANS")]
+struct ListNAns
+{
+	int ResultCode;
+	char ListXmlContent[0]; // no more than 50k
+};
+
 //[StructName("FILEREQ")]
 struct FileReq
 {
@@ -91,6 +104,37 @@ struct ListAnsEvent : public FSMEvent
 	ListAns *m_reply;
 	int m_xml_length;
 };
+//新国密文件列表回应报文
+struct ListNAnsEvent : public FSMEvent
+{
+	ListNAnsEvent(BYTE *pBuf, int nLen) : FSMEvent(USER_EVT_FILEINO_ANS) {
+		int size = nLen;
+		int nprefix = strlen(XML_US_ASCII_PREFIX);
+		int offset = 0;
+		if (memcmp(pBuf + sizeof(ListAns), "<?", 2) != 0) {
+			size += nprefix;
+		}
+		LPBYTE pData = new BYTE[size+1];
+		if (size != nLen) {
+			memcpy(pData, pBuf, sizeof(ListAns));
+			offset = sizeof(ListAns);
+			memcpy(pData+offset, XML_US_ASCII_PREFIX, nprefix);
+			offset += nprefix;
+			memcpy(pData+offset, pBuf+sizeof(ListAns), nLen-sizeof(ListAns));
+			offset += nLen-sizeof(ListAns);
+			pData[offset] = 0;
+			m_xml_length = nLen - sizeof(ListAns) + nprefix;
+		} else {
+			memcpy(pData, pBuf, nLen);
+			pData[nLen] = 0;
+			m_xml_length = nLen - sizeof(ListAns);
+		}
+		m_reply = (ListAns*)pData;
+	}
+	virtual ~ListNAnsEvent() { delete (LPBYTE)m_reply; }
+	ListAns *m_reply;
+	int m_xml_length;
+};
 
 struct FileAnsEvent : public FSMEvent
 {
@@ -263,6 +307,21 @@ public:
 		SendPackage(pkt);
 	}
 
+	void SendListNReq()
+	{
+		ListNReq req = {0};
+		CSystemStaticInfo si;
+		{
+			m_pEntity->GetFunction()->GetSystemStaticInfo(si);
+		}
+		strcpy(&req.TerminalNo[0], si.strTerminalID);
+
+		CSmartPointer<IPackage> pkt = CreateNewPackage("LISTNREQ");
+		pkt->AddStruct("LISTNREQ", false, false, (LPBYTE)&req, sizeof(ListNReq));
+
+		SendPackage(pkt);
+	}
+
 	void SendFileReq(const char *file, int block_id)
 	{
 		FileReq req = {0};
@@ -314,7 +373,22 @@ protected:
 						Dbg("create invalid fileInfo_Ans packet!");
 						OnDisconnect();
 					}
-					delete pBuf;
+					delete[] pBuf;
+				}
+			} else if (serviceCode == "LISTNREQ") {//新国密列表文件
+				int nLen = pRecvPkg->GetStructLen("LISTNANS");
+				if (nLen > 0) {
+					BYTE *pBuf = new BYTE[nLen];
+					memset(pBuf, 0, nLen);
+					int nArrayNum = 0;
+					if (pRecvPkg->GetStructData("LISTNANS", pBuf, &nLen, &nArrayNum)) {
+						FSMEvent *evt = new ListNAnsEvent(pBuf, nLen);
+						m_pFSM->PostEventFIFO(evt);
+					} else {
+						Dbg("create invalid fileNewInfo_Ans packet!");
+						OnDisconnect();
+					}
+					delete[] pBuf;
 				}
 			} else if (serviceCode == "LFILEREQ") {
 				int nLen = pRecvPkg->GetStructLen("LFILEANS");
@@ -329,7 +403,7 @@ protected:
 						Dbg("create invalid file_Ans packet!");
 						OnDisconnect();
 					}
-					delete pBuf;
+					delete[] pBuf;
 				}
 			} else {
 				Dbg("unknown service code!");

+ 398 - 90
Module/mod_download/download.cpp

@@ -5,12 +5,27 @@
 #include "download.h"
 
 #include <fileutil.h>
-#include <WinCrypt.h>
-
 #include <memutil.h>
+#include "RVCComm.h"
 
 #include "scew.h"
 
+#include <string> 
+#include <locale>  
+#include <codecvt>  
+
+#ifdef RVC_OS_WIN
+#include <WinCrypt.h>
+#else
+#include <sys/stat.h>
+#include <errno.h>
+#endif // RVC_OS_WIN
+
+using namespace std;
+
+
+
+
 static __inline int char2hex(char c)
 {
 	if (c >= '0' && c<= '9')
@@ -23,7 +38,7 @@ static __inline int char2hex(char c)
 		assert(0);
 	return 0;
 }
-
+//废弃方法
 static void parse_md5_hex(const char *str, char md5[16])
 {
 	int i;
@@ -33,26 +48,68 @@ static void parse_md5_hex(const char *str, char md5[16])
 		md5[i] = (h << 4) | l;
 	}
 }
+static void parse_SM3_hex(const char *str, char md5[32])
+{
+	int i;
+	for (i = 0; i < 32; ++i) {
+		int h = char2hex(str[2*i]);
+		int l = char2hex(str[2*i+1]);
+		md5[i] = (h << 4) | l;
+	}
+}
 
+//src必须是UTF8否则抛异常
+wstring utf8_to_wstr(const string& src)
+{
+	wstring_convert<codecvt_utf8<wchar_t>> converter;
+	return converter.from_bytes(src);
+}
 
-char* ConvertUtf8ToGBK(char* strUtf8)
+string wstr_to_utf8(const wstring& src)
 {
-    int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, NULL, 0);
-    WCHAR* wszGBK = new WCHAR[len + 1];
-    memset(wszGBK, 0, len * 2 + 2);
-    MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, wszGBK, len);
+	wstring_convert<codecvt_utf8<wchar_t>> convert;
+	return convert.to_bytes(src);
+}
 
-    len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
-    char* szGBK = new char[len + 1];
-    memset(szGBK, 0, len + 1);
-    WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
-    delete[] wszGBK;
+string utf8_to_gbk(const string& str)
+{
+	wstring wStr = utf8_to_wstr(str);//utf8转wstring
+	wstring_convert<chs_codecvt> converter;
+	return converter.to_bytes(wStr);//wstring转GBK
+}
+
+string gbk_to_utf8(const string& str)
+{
+	wstring_convert<chs_codecvt> converter;
+	wstring wStr = converter.from_bytes(str);//GBK转wstring
+	return wstr_to_utf8(wStr);//wstring 转utf8
+}
+
+
+
+char* ConvertUtf8ToGBK(char* strUtf8)
+{
+#ifdef RVC_OS_WIN
+	int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, NULL, 0);
+	WCHAR* wszGBK = new WCHAR[len + 1];
+	memset(wszGBK, 0, len * 2 + 2);
+	MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, wszGBK, len);
+
+	len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
+	char* szGBK = new char[len + 1];
+	memset(szGBK, 0, len + 1);
+	WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
+	delete[] wszGBK;
 
 	return szGBK;
+#else
+	return NULL;	
+#endif // RVC_OS_WIN
 }
 
 char* ConvertGBKToUtf8(char* gbk, int *n)
 {
+#ifdef RVC_OS_WIN
 	int len = MultiByteToWideChar(CP_ACP, 0, gbk, -1, NULL, 0);
     WCHAR* wszGBK = new WCHAR[len + 1];
     memset(wszGBK, 0, len * 2 + 2);
@@ -66,6 +123,10 @@ char* ConvertGBKToUtf8(char* gbk, int *n)
     delete[] wszGBK;
 	*n = len  - 1;
 	return szUtf8;
+#else:
+	return NULL;
+#endif
+	
 }
 
 
@@ -77,9 +138,15 @@ static download_file_t *parse_file_object(scew_element *elem)
 		if (attr) {
 			const char *str = scew_attribute_value(attr);
 			if (str) {
+#ifdef RVC_OS_WIN
 				char *pGBK = ConvertUtf8ToGBK((char*)str);
 				strncpy(file->name, pGBK, sizeof(file->name)-1);
 				delete[] pGBK;
+#else
+				string inputUtf8Str = str;
+				string outGBkStr = utf8_to_gbk(inputUtf8Str);
+				strncpy(file->name, outGBkStr.c_str(), sizeof(file->name) - 1);
+#endif
 			} else {
 				goto on_error;
 			}
@@ -101,7 +168,9 @@ static download_file_t *parse_file_object(scew_element *elem)
 		if (attr) {
 			const char *str = scew_attribute_value(attr);
 			if (str) {
-				parse_md5_hex(str, file->md5);
+				//采用新国密SM3解析接口
+				//parse_md5_hex(str, file->md5);
+				parse_SM3_hex(str,file->md5);
 			} else {
 				goto on_error;
 			}
@@ -124,6 +193,8 @@ array_header_t* download_parse_filelist1(const char *xml, int n)
 	scew_tree *tree = NULL;
 	array_header_t *arr = NULL;
 	scew_element *root;	
+	scew_error code;
+	scew_list* lst = NULL;
 	
 	reader = scew_reader_buffer_create(xml, n);
 	if (!reader) {			
@@ -133,7 +204,7 @@ array_header_t* download_parse_filelist1(const char *xml, int n)
 	parser = scew_parser_create();
 	tree = scew_parser_load(parser, reader);
 	if (!tree) {
-		scew_error code = scew_error_code();
+		code = scew_error_code();
 		if (code == scew_error_expat) {
 		  enum XML_Error expat_code = scew_error_expat_code(parser);
 		  Dbg("scew parse error:%d, line:%d column:%d %s", expat_code, scew_error_expat_line(parser), 
@@ -149,7 +220,7 @@ array_header_t* download_parse_filelist1(const char *xml, int n)
 		goto on_error;
 	}
 
-	scew_list *lst = scew_element_children(root);
+	lst = scew_element_children(root);
 	if (lst) {
 		arr = array_make(scew_list_size(lst), sizeof(download_file_t*));
 		for (scew_list *it = scew_list_first(lst); it; it = scew_list_next(it)) {
@@ -184,7 +255,18 @@ array_header_t* download_parse_filelist(const char *xml, int n)
 	memcpy(pBuf, xml, n);
 	
 	int nLen = 0;
-	char *pUtf8 = ConvertGBKToUtf8(pBuf, &nLen);
+#ifdef RVC_OS_WIN
+	char* pUtf8 = ConvertGBKToUtf8(pBuf, &nLen);
+#else
+	string inputGBKStr = pBuf;
+	string outUtf8Str = gbk_to_utf8(inputGBKStr);
+	char* pUtf8 = new char[inputGBKStr.length() + 1];
+	memset(pUtf8, 0, inputGBKStr.length() + 1);
+	memcpy(pUtf8, inputGBKStr.c_str(), inputGBKStr.length());
+	nLen = strlen(pUtf8);
+#endif // RVC_OS_WIN
+
+	
 	auto arr = download_parse_filelist1(pUtf8,  nLen);
 
 	if (arr == NULL)
@@ -208,14 +290,16 @@ void download_free_filelist(array_header_t* arr)
 		free(arr);
 	}
 }
+//废弃方法
 static int download_get_file_md5(const char *path, char md5[16])
 {
+#ifdef RVC_OS_WIN
 	HCRYPTPROV hCryptProv;
-	if (CryptAcquireContextA(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT|CRYPT_MACHINE_KEYSET)) {
+	if (CryptAcquireContextA(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) {
 		int rc = Error_Unexpect;
 		HCRYPTHASH hHash;
 		if (CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)) {
-			HANDLE hFile = CreateFileA(path, FILE_GENERIC_READ, 
+			HANDLE hFile = CreateFileA(path, FILE_GENERIC_READ,
 				FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 			if (hFile != INVALID_HANDLE_VALUE) {
 				char tmp[8192];
@@ -231,58 +315,103 @@ static int download_get_file_md5(const char *path, char md5[16])
 				DWORD dwLen = 16;
 				CryptGetHashParam(hHash, HP_HASHVAL, (LPBYTE)&md5[0], &dwLen, 0);
 				rc = Error_Succeed;
-			} else {
+			}
+			else {
 				Dbg("file open failed, may be %s does not exist! GetLastError:%d", path, GetLastError());
 			}
 			CryptDestroyHash(hHash);
-		} else {
+		}
+		else {
 			Dbg("create hash failed!GetLastError:%d", GetLastError());
 		}
 		CryptReleaseContext(hCryptProv, 0);
 		return (ErrorCodeEnum)rc;
-	} else {
+	}
+	else {
 		Dbg("acquire ctx failed!GetLastError:%d", GetLastError());
 		return Error_Unexpect;
 	}
+#else
+	//等国密版本
+#endif // RVC_OS_WIN
+	return Error_Unexpect;
+	
 }
-
-int download_check_filelist(const char *base_dir, array_header_t* arr)
+static int new_download_get_file_md5(const char *path, BYTE md5[32])
 {
-	int i;
-	char tmp[MAX_PATH];
-	assert(base_dir);
-	assert(arr);
-	i = 0;
-	while (i < arr->nelts) {
-		download_file_t *file = (download_file_t*)ARRAY_IDX(arr, i, download_file_t*);
-		assert(file);
-		sprintf(tmp, "%s\\%s", base_dir, file->name);
-		WIN32_FILE_ATTRIBUTE_DATA attr_data;
-		if (GetFileAttributesExA(tmp, GetFileExInfoStandard, &attr_data)) {
-			if (attr_data.nFileSizeHigh == 0 && attr_data.nFileSizeLow == file->length) { 
-				char md5[16];
-				int rc = download_get_file_md5(tmp, md5);
-				if (rc == 0) {
-					if (memcmp(md5, file->md5, 16) == 0) { // already have remove this file
-						Dbg("file %s the same, remove it!", file->name);
-						ARRAY_DEL(arr, i, download_file_t*);
-						free(file);
-					} else {
-						++i;
-					}
-				} else {
-					++i;
-				}
-			} else {
-				++i;
-			}
-		} else {
-			++i;
+	if(path==NULL){
+		Dbg("file path is null");
+		return -1;
+	}
+	int nlen = strlen(path);
+	char* pchar = new char[nlen+1];
+	strcpy(pchar,path);
+	try
+	{
+		if(SM3File(pchar,md5)){
+			delete pchar;
+			return 0;
+		}else{
+			delete pchar;
+			Dbg("下载文件sm3国密加密失败,file=%s",path);
+			return -1;
 		}
 	}
-	return 0;
+	catch (...)
+	{
+		delete pchar;
+		Dbg("下载文件sm3国密加密异常失败,file=%s",path);
+		return -2;
+	}
+	
 }
 
+//废弃方法
+//int download_check_filelist(const char *base_dir, array_header_t* arr)
+//{
+//	int i;
+//	char tmp[MAX_PATH];
+//	assert(base_dir);
+//	assert(arr);
+//	i = 0;
+//	while (i < arr->nelts) {
+//		download_file_t *file = (download_file_t*)ARRAY_IDX(arr, i, download_file_t*);
+//		assert(file);
+//		sprintf(tmp, "%s\\%s", base_dir, file->name);
+//#ifdef RVC_OS_WIN
+//		WIN32_FILE_ATTRIBUTE_DATA attr_data;
+//		if (GetFileAttributesExA(tmp, GetFileExInfoStandard, &attr_data)) {
+//			if (attr_data.nFileSizeHigh == 0 && attr_data.nFileSizeLow == file->length) {
+//				char md5[16];
+//				int rc = download_get_file_md5(tmp, md5);
+//				if (rc == 0) {
+//					if (memcmp(md5, file->md5, 16) == 0) { // already have remove this file
+//						Dbg("file %s the same, remove it!", file->name);
+//						ARRAY_DEL(arr, i, download_file_t*);
+//						free(file);
+//					}
+//					else {
+//						++i;
+//					}
+//				}
+//				else {
+//					++i;
+//				}
+//			}
+//			else {
+//				++i;
+//			}
+//		}
+//		else {
+//			++i;
+//		}
+//#endif // RVC_OS_WIN
+//
+//		
+//	}
+//	return 0;
+//}
+
 int new_download_check_filelist(const char *base_dir, array_header_t* arr,download_file_t *downloadFile){
 	int ret=0;	//表示下载的文件在文件列表里不存在
 	int i;
@@ -293,67 +422,177 @@ int new_download_check_filelist(const char *base_dir, array_header_t* arr,downlo
 	while (i<arr->nelts){
 		download_file_t *file = (download_file_t*)ARRAY_IDX(arr, i, download_file_t*);
 		assert(file);
-		Dbg("download_check_filelist request downloadFile->name:[%s] ,fileList->name : [%s]",downloadFile->name,(LPCSTR)file->name);
-		if(strcmp((LPCSTR)downloadFile->name,(LPCSTR)file->name)==0){
+
+		Dbg("download_check_filelist request downloadFile->name:[%s] ,fileList->name : [%s]", downloadFile->name, (const char*)file->name);
+		if (strcmp((const char* )downloadFile->name, (const char*)file->name) == 0) {
 			sprintf(tmp, "%s\\%s", base_dir, downloadFile->name);
+#ifdef RVC_OS_WIN
 			WIN32_FILE_ATTRIBUTE_DATA attr_data;
-
 			if (GetFileAttributesExA(tmp, GetFileExInfoStandard, &attr_data)) {
 				if (attr_data.nFileSizeHigh == 0 && attr_data.nFileSizeLow == file->length) { 
-					char md5[16];
-					int rc = download_get_file_md5(tmp, md5);
+					//char md5[16];
+					BYTE md5[32];
+					int rc = new_download_get_file_md5(tmp, md5);
 					if(rc==0){
-						if (memcmp(md5, file->md5, 16) == 0) {// already have remove this file
+						//if (memcmp(md5, file->md5, 16) == 0) {
+						if (memcmp(md5, file->md5, 32) == 0) {
 							//文件长度,md5完全相同,告知下载完成
 							Dbg("file %s the same, remove it!", file->name);
-							ret=2;
+							ret = 2;
 							break;
 						}else{
 							//md5不相等,需要下载
-							ret=1;
+							ret = 1;
 							downloadFile->length = file->length;
-							memcpy(downloadFile->md5,file->md5,16);
+							memcpy(downloadFile->md5, file->md5, 16);
 							break;
 						}
 					}else{
 						//获取md5报错,需要下载
-						ret=1;
+						ret = 1;
 						downloadFile->length = file->length;
-						memcpy(downloadFile->md5,file->md5,16);
+						//memcpy(downloadFile->md5,file->md5,16);
+						memcpy(downloadFile->md5,file->md5,32);
 						break;
 					}
 				}else{
 					//文件长度不相同,需要下载
-					ret=1;
+					ret = 1;
 					downloadFile->length = file->length;
-					memcpy(downloadFile->md5,file->md5,16);
+					//memcpy(downloadFile->md5,file->md5,16);
+					memcpy(downloadFile->md5,file->md5,32);
 					break;
 				}
 			}else{
 				//获取不到文件,需要下载
-				Dbg("file is not exist , fileName is [%s]",(LPCSTR)tmp);
-				ret=1;
+				Dbg("file is not exist , fileName is [%s]", (LPCSTR)tmp);
+				ret = 1;
 				downloadFile->length = file->length;
-				memcpy(downloadFile->md5,file->md5,16);
+				//memcpy(downloadFile->md5,file->md5,16);
+				memcpy(downloadFile->md5,file->md5,32);
 				break;
 			}
-		}else{
+#else   
+//需要修改
+			struct stat attr_of_src;
+			if (lstat(tmp, &attr_of_src) == 0)
+			{
+				if (attr_of_src.st_size== file->length) {
+					char md5[16];
+					int rc = download_get_file_md5(tmp, md5);
+					if (rc == 0) {
+						if (memcmp(md5, file->md5, 16) == 0) {// already have remove this file
+							//文件长度,md5完全相同,告知下载完成
+							Dbg("file %s the same, remove it!", file->name);
+							ret = 2;
+							break;
+						}
+						else {
+							//md5不相等,需要下载
+							ret = 1;
+							downloadFile->length = file->length;
+							memcpy(downloadFile->md5, file->md5, 16);
+							break;
+						}
+					}
+					else {
+						//获取md5报错,需要下载
+						ret = 1;
+						downloadFile->length = file->length;
+						memcpy(downloadFile->md5, file->md5, 16);
+						break;
+					}
+				}
+				else {
+					//文件长度不相同,需要下载
+					ret = 1;
+					downloadFile->length = file->length;
+					memcpy(downloadFile->md5, file->md5, 16);
+					break;
+				}
+			}
+			else 
+			{
+				//获取不到文件,需要下载
+				Dbg("file is not exist , fileName is [%s]", (const char*)tmp);
+				ret = 1;
+				downloadFile->length = file->length;
+				memcpy(downloadFile->md5, file->md5, 16);
+				break;
+			}
+#endif
+		}
+		else {
 			i++;
 		}
+
+		
 	}
 	return ret;
 }
 
+//SM3Byte is 16 or 32
+//SM3_len is 32 or 64
+char* SM3_Str (BYTE * SM3Byte, int SM3_len)  
+{
+	if(SM3Byte == NULL){
+		return NULL;
+	}
+	int i; 
+	char* file_SM3 = (char*)malloc((SM3_len + 1) * sizeof(char)); 
+	if(file_SM3 == NULL)  
+	{  
+		fprintf(stderr, "SM3 malloc failed.\n");  
+		return NULL;  
+	} 
+	memset(file_SM3, 0, (SM3_len + 1));  
+
+	if(SM3_len == 32)  
+	{  
+		for(i=0; i<16; i++)  
+		{  
+			sprintf(&file_SM3[i*2], "%02X", SM3Byte[i]);  
+		}  
+	}  
+	else if(SM3_len == 64)  
+	{  
+		for(i=0; i<32; i++)  
+		{  
+			sprintf(&file_SM3[i*2], "%02X", SM3Byte[i]);  
+		}  
+	}  
+	else  
+	{  
+		free(file_SM3);  
+		return NULL;  
+	}   
+	return file_SM3;  
+}
+
 int download_check_MD5(download_storage_t *storage,download_file_t *dfile){
 	int ret=0;
-	char md5[16];
-	int rc = download_get_file_md5(storage->temp_path, md5);//计算出临时文件md5值
+	//char md5[16];
+	BYTE md5[32]={0};
+	//计算sm3的加密时间
+	CSmallDateTime beginT = CSmallDateTime::GetNow();
+	int rc = new_download_get_file_md5(storage->temp_path, md5);//计算出临时文件md5值
+	CSmallDateTime endT = CSmallDateTime::GetNow();
+	Dbg("下载文件国密获取的SM3耗时%d秒",(DWORD)(endT-beginT));
+	
 	if(rc==0){
-		if (memcmp(md5, dfile->md5, 16) == 0) {
+		//试着打印已下载文件的SM3的16进制值
+		char* fSM3 = SM3_Str(md5,64);
+		if(fSM3!=NULL){
+			Dbg("下载文件国密获取的SM3=%s",fSM3);
+			free(fSM3);
+		}
+		//if (memcmp(md5, dfile->md5, 16) == 0) {
+		if (memcmp(md5, dfile->md5, 32) == 0) {
 			//下载临时文件和文件列表md5一致
 			ret=0;
 		}else{
 			//下载临时文件和文件列表md5不一致,需要下载
+			Dbg("下载文件SM3值和服务器文件SM3值不同,需要重新下载");
 			ret=1;
 		}
 	}else{
@@ -373,15 +612,30 @@ int download_delete_file(const char *base_dir,download_file_t *downloadFile){
 		return 1;
 	}else{
 		fclose(fh);//先关闭文件
-		if(DeleteFileA(tmp)){
-			Dbg("same download file [%s] delete success ",(LPCSTR)tmp);
+#ifdef RVC_OS_WIN
+		if (DeleteFileA(tmp)) {
+			Dbg("same download file [%s] delete success ", (LPCSTR)tmp);
 			return 1;
-		}else{
+		}
+		else {
 			DWORD err = GetLastError();
-			char * pErrmsg = strerror((int)err);
-			Dbg("same download file [%s] delete fail , errno [%d] , errmsg [%s]",(LPCSTR)tmp,err,(LPCSTR)pErrmsg);
+			char* pErrmsg = strerror((int)err);
+			Dbg("same download file [%s] delete fail , errno [%d] , errmsg [%s]", (LPCSTR)tmp, err, (LPCSTR)pErrmsg);
+			return 0;
+		}
+#else
+		if (remove(tmp)) {
+			Dbg("same download file [%s] delete success ", (const char*)tmp);
+			return 1;
+		}
+		else {
+			char* pErrmsg = strerror(errno);
+			Dbg("same download file [%s] delete fail , errno [%d] , errmsg [%s]", (const char*)tmp, errno, (const char*)pErrmsg);
 			return 0;
 		}
+#endif // RVC_OS_WIN
+
+		
 	}
 
 }
@@ -400,7 +654,8 @@ int download_get_storage_block_id(const char *base_dir, download_file_t *file, i
 
 typedef struct storage_info_t {
 	int block_id;
-	char md5[16];
+	//char md5[16];
+	char md5[32];
 	int length;
 }storage_info_t;
 
@@ -408,6 +663,7 @@ download_storage_t *download_storage_open(const char *base_dir, download_file_t
 {
 	if (base_dir && file) {
 		download_storage_t *storage = ZALLOC_T(download_storage_t);//清零初始化
+#ifdef RVC_OS_WIN
 		if (storage) {
 			sprintf(storage->temp_path, "%s\\%s.temp", base_dir, file->name);//临时内容文件
 			sprintf(storage->info_path, "%s\\%s.info", base_dir, file->name);//控制信息文件
@@ -421,7 +677,8 @@ download_storage_t *download_storage_open(const char *base_dir, download_file_t
 					storage_info_t info;
 					DWORD dwLen;
 					info.length = file->length;
-					memcpy(info.md5, file->md5, 16);
+					//memcpy(info.md5, file->md5, 16);
+					memcpy(info.md5, file->md5, 32);
 					info.block_id = 0;
 					WriteFile(storage->info_handle, &info, sizeof(info), &dwLen, NULL);
 					FlushFileBuffers(storage->info_handle);
@@ -431,13 +688,15 @@ download_storage_t *download_storage_open(const char *base_dir, download_file_t
 				DWORD dwLen;
 				BOOL bRet = ReadFile(storage->info_handle, &info, sizeof(info), &dwLen, NULL);
 				if (bRet && dwLen == sizeof(info)) {
-					if (info.length == file->length && memcmp(info.md5, file->md5, 16) == 0) {
+					//if (info.length == file->length && memcmp(info.md5, file->md5, 16) == 0) {
+                    if (info.length == file->length && memcmp(info.md5, file->md5, 32) == 0) {
 						// 续传
 						storage->offset_block_id = info.block_id;
 					} else {
 						// 文件已改变,重新传输
 						info.length = file->length;
-						memcpy(info.md5, file->md5, 16);
+						//memcpy(info.md5, file->md5, 16);
+						memcpy(info.md5, file->md5, 32);
 						info.block_id = 0;
 						WriteFile(storage->info_handle, &info, sizeof(info), &dwLen, NULL);
 						FlushFileBuffers(storage->info_handle);
@@ -445,7 +704,8 @@ download_storage_t *download_storage_open(const char *base_dir, download_file_t
 				} else {
 					//新文件,新传输
 					info.length = file->length;
-					memcpy(info.md5, file->md5, 16);
+					//memcpy(info.md5, file->md5, 16);
+					memcpy(info.md5, file->md5, 32);
 					info.block_id = 0;
 					WriteFile(storage->info_handle, &info, sizeof(info), &dwLen, NULL);
 					FlushFileBuffers(storage->info_handle);
@@ -477,6 +737,9 @@ download_storage_t *download_storage_open(const char *base_dir, download_file_t
 				storage=NULL;
 			}
 		}
+#else
+
+#endif // RVC_OS_WIN
 		return storage;
 	} else {
 		return NULL;
@@ -486,6 +749,7 @@ download_storage_t *download_storage_open(const char *base_dir, download_file_t
 //renameFlag:下载完是否改名称,deleteFlag:删除临时文件
 void download_storage_close(download_storage_t *storage,bool renameFlag,bool deleteFlag)
 {
+#ifdef RVC_OS_WIN
 	if (storage) {
 		if (storage->info_handle != INVALID_HANDLE_VALUE) {
 			CloseHandle(storage->info_handle);
@@ -495,28 +759,63 @@ void download_storage_close(download_storage_t *storage,bool renameFlag,bool del
 			CloseHandle(storage->temp_handle);
 			storage->temp_handle = INVALID_HANDLE_VALUE;
 		}
-		if(renameFlag){
-			if (storage->offset_block_id<<15 >= storage->length) {
+		if (renameFlag) {
+			if (storage->offset_block_id << 15 >= storage->length) {
 				char tmp[MAX_PATH];
 				strcpy(tmp, storage->temp_path);
-				tmp[strlen(tmp)-5] = 0;
-				/*CopyFileA(storage->temp_path, tmp, TRUE);			
+				tmp[strlen(tmp) - 5] = 0;
+				/*CopyFileA(storage->temp_path, tmp, TRUE);
 				DeleteFileA(storage->temp_path);*/
 				MoveFileA(storage->temp_path, tmp);
 				DeleteFileA(storage->info_path);
 			}
 		}
-		if(deleteFlag){
+		if (deleteFlag) {
 			DeleteFileA(storage->temp_path);
 			DeleteFileA(storage->info_path);
 		}
 		free(storage);
 	}
+#else
+	if (storage) {
+		if (storage->info_handle != NULL) {
+			fclose(storage->info_handle);
+			storage->info_handle = NULL;
+		}
+		if (storage->temp_handle != NULL) {
+			fclose(storage->temp_handle);
+			storage->temp_handle = NULL;
+		}
+		if (renameFlag) {
+			if (storage->offset_block_id << 15 >= storage->length) {
+				char tmp[MAX_PATH];
+				strcpy(tmp, storage->temp_path);
+				tmp[strlen(tmp) - 5] = 0;
+				/*CopyFileA(storage->temp_path, tmp, TRUE);
+				DeleteFileA(storage->temp_path);*/
+
+				//MoveFileA(storage->temp_path, tmp);
+				rename(storage->temp_path, tmp);
+				remove(storage->info_path);
+				//DeleteFileA(storage->info_path);
+			}
+		}
+		if (deleteFlag) {
+			//DeleteFileA(storage->temp_path);
+			//DeleteFileA(storage->info_path);
+			remove(storage->temp_path);
+			remove(storage->info_path);
+
+		}
+		free(storage);
+	}
+#endif // RVC_OS_WIN
 }
 
 int download_storage_update(download_storage_t *storage, char *buf, int n)
 {
 	assert(storage);
+#ifdef RVC_OS_WIN
 	DWORD dwLen;
 	WriteFile(storage->temp_handle, buf, n, &dwLen, NULL);
 	FlushFileBuffers(storage->temp_handle);
@@ -524,6 +823,15 @@ int download_storage_update(download_storage_t *storage, char *buf, int n)
 	SetFilePointer(storage->info_handle, 0, NULL, FILE_BEGIN);
 	WriteFile(storage->info_handle, &storage->offset_block_id, 4, &dwLen, NULL);		// 只更新块号字段
 	FlushFileBuffers(storage->info_handle);
+#else
+	fwrite(buf, n, 1, storage->temp_handle);
+	fflush(storage->temp_handle);
+	storage->offset_block_id++;
+	fseek(storage->info_handle, 0, SEEK_SET);
+	fwrite(&storage->offset_block_id, 4, 1, storage->info_handle);
+	fflush(storage->info_handle);
+#endif // RVC_OS_WIN
+
 	return 0;
 }
 

+ 23 - 2
Module/mod_download/download.h

@@ -1,19 +1,35 @@
 #pragma once
 
 #include <array.h>
+#include <locale>
+#include <codecvt> 
+#include <string>
+
+//GBK转UTF码类
+class chs_codecvt : public std::codecvt_byname<wchar_t, char, std::mbstate_t> {
+public:
+	//因不同平台,名称不同,故这里做转换,统一取出不同平台下面的GBK码
+#ifdef RVC_OS_WIN
+	chs_codecvt() : codecvt_byname("chs") { }//zh_CN.GBK or .936
+#else
+	chs_codecvt() : codecvt_byname("zh_CN.GBK") { }
+#endif // RVC_OS_WIN
+};
+
 
 
 typedef struct download_file_t {
 	//char name[64];
 	char name[256];
 	int length;
-	char md5[16];
+	//char md5[16];
+	char md5[32];
 }download_file_t;
 
 // xml must be have <?xml version="1.0" encoding="utf-8" ?>
 array_header_t* download_parse_filelist(const char *xml, int n);
 void download_free_filelist(array_header_t* arr);
-int download_check_filelist(const char *base_dir, array_header_t* arr);
+//int download_check_filelist(const char *base_dir, array_header_t* arr);
 
 //0:表示下载的文件在文件列表里不存在 1:需要下载 2:下载完成
 int new_download_check_filelist(const char *base_dir, array_header_t* arr,download_file_t *downloadFile);
@@ -24,8 +40,13 @@ int download_delete_file(const char *base_dir,download_file_t *downloadFile);
 typedef struct download_storage_t {
 	char temp_path[MAX_PATH];
 	char info_path[MAX_PATH];
+#ifdef RVC_OS_WIN
 	HANDLE temp_handle;
 	HANDLE info_handle;
+#else
+	FILE* temp_handle;
+	FILE* info_handle;
+#endif 
 	int offset_block_id;
 	int length;
 }download_storage_t;

+ 27 - 12
Module/mod_download/mod_download.h

@@ -142,11 +142,12 @@ public:
 			ctx->Ans.iDownloadState = 2;
 			return Error_Succeed;
 		}
+		return Error_Succeed;
 
 		CSimpleStringA str;
 		GetFunction()->GetPath("Download", str);
 		char tmp[MAX_PATH];
-		WIN32_FIND_DATAA fd;
+		
 		strcpy(tmp, str.GetData());
 		if (str_has_suffix(str.GetData(), "\\")) {
 			strcat(tmp, filename);
@@ -155,29 +156,43 @@ public:
 			strcat(tmp, "\\");
 			strcat(tmp, filename);
 		}
-
-		HANDLE hFind = FindFirstFileA(tmp, &fd);
-
-		if (hFind != INVALID_HANDLE_VALUE) {
-			FindClose(hFind);
+		//WIN32_FIND_DATAA fd;
+		//HANDLE hFind = FindFirstFileA(tmp, &fd);
+
+		//if (hFind != INVALID_HANDLE_VALUE) {
+		//	FindClose(hFind);
+		//	ctx->Ans.iDownloadState = 3;
+		//	return Error_Succeed;
+		//}
+		//else {
+		//	FindClose(hFind);
+		//	ctx->Ans.iDownloadState = 4;
+		//	return Error_Succeed;
+		//}
+		FILE* fp = fopen(tmp, "rb");
+		if(fp!=NULL)
+		{
+			fclose(fp);
 			ctx->Ans.iDownloadState = 3;
 			return Error_Succeed;
 		}
-		else {
-			FindClose(hFind);
+		else
+		{ 
 			ctx->Ans.iDownloadState = 4;
 			return Error_Succeed;
 		}
+
+		
 	}
 
 private:
-	ErrorCodeEnum MakeFileList(std::list<CSimpleStringA>& fileList)
+	/*ErrorCodeEnum MakeFileList(std::list<CSimpleStringA>& fileList)
 	{
 		CSimpleStringA str;
 		GetFunction()->GetPath("Download", str);
 		return __MakeFileList(str, fileList);
-	}
-	ErrorCodeEnum __MakeFileList(const char* base_dir, std::list<CSimpleStringA>& fileList)
+	}*/
+	/*ErrorCodeEnum __MakeFileList(const char* base_dir, std::list<CSimpleStringA>& fileList)
 	{
 		char tmp[MAX_PATH];
 		WIN32_FIND_DATAA fd;
@@ -198,7 +213,7 @@ private:
 			FindClose(hFind);
 		}
 		return Error_Succeed;
-	}
+	}*/
 
 
 private:

+ 6 - 6
Module/mod_upload/CMakeLists.txt

@@ -39,15 +39,15 @@ set(MOD_VERSION_STRING "1.0.0-dev1")
 add_module_libraries(${MODULE_PREFIX} ${MODULE_NAME} ${MOD_VERSION_STRING})
 #¸½¼Ó°üº¬µÄĿ¼
 
-if(WIN32)
-conan_cmake_run(REQUIRES zlib/1.2.11@LR04.02_ThirdParty/testing
-BASIC_SETUP CMAKE_TARGETS
-BUILD missing)
-else(WIN32)
+#if(WIN32)
+#conan_cmake_run(REQUIRES zlib/1.2.11@LR04.02_ThirdParty/testing
+#BASIC_SETUP CMAKE_TARGETS
+#BUILD missing)
+#else(WIN32)
 conan_cmake_run(REQUIRES zlib/1.2.11@LR04.02_ThirdParty/stable
 BASIC_SETUP CMAKE_TARGETS
 BUILD missing)
-endif(WIN32)
+#endif(WIN32)
 
 target_include_directories(${MODULE_NAME} PRIVATE
 	${RVC_FRAMEWORK_INCLUDES_DIR}