|
|
@@ -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;
|
|
|
}
|
|
|
|