From 0e2c519f304de43ddc6900801ea469895f85fe86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=97=8B=E9=A3=8Elc?= Date: Thu, 5 Dec 2024 16:18:37 +0800 Subject: [PATCH 1/4] optimize multi-threaded code signature processing Signed-off-by: liuchang --- .../codesigning/sign/include/code_signing.h | 2 +- .../codesigning/sign/src/code_signing.cpp | 52 +++++++++++-------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/hapsigntool_cpp/codesigning/sign/include/code_signing.h b/hapsigntool_cpp/codesigning/sign/include/code_signing.h index ae008d0a..7bf8f771 100644 --- a/hapsigntool_cpp/codesigning/sign/include/code_signing.h +++ b/hapsigntool_cpp/codesigning/sign/include/code_signing.h @@ -77,7 +77,7 @@ public: private: static bool CheckUnzParam(unzFile& zFile, unz_file_info& zFileInfo, char fileName[], size_t* nameLen); - static bool CheckFileName(unzFile& zFile, char fileName[], size_t* nameLen); + static bool CheckFileName(char fileName[], size_t* nameLen); bool HandleZipGlobalInfo(const std::string& packageName, unzFile& zFile, unz_global_info& zGlobalInfo, UnzipHandleParam& param); bool DoNativeLibVerify(std::string fileName, std::stringbuf& sb, diff --git a/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp b/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp index 1afff098..33c80445 100644 --- a/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp +++ b/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp @@ -279,14 +279,21 @@ bool CodeSigning::GetSingleFileStreamFromZip(unzFile &zFile, char fileName[], unz_file_info &zFileInfo, int &readFileSize, std::stringbuf &sb) { - char szReadBuffer[BUFFER_SIZE] = { 0 }; + if (UNZ_OK != unzOpenCurrentFile(zFile)) { + PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, + "unzOpenCurrentFile zipFile error"); + unzClose(zFile); + return false; + } + char szReadBuffer[BUFFER_SIZE] = { 0 }; long fileLength = zFileInfo.uncompressed_size; int nReadFileSize; do { nReadFileSize = 0; if (memset_s(szReadBuffer, BUFFER_SIZE, 0, BUFFER_SIZE) != EOK) { SIGNATURE_TOOLS_LOGE("memset_s failed"); + unzCloseCurrentFile(zFile); unzClose(zFile); return false; } @@ -300,6 +307,7 @@ bool CodeSigning::GetSingleFileStreamFromZip(unzFile &zFile, char fileName[], if (fileLength) { PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "zlib read stream from " + std::string(fileName) + " failed."); + unzCloseCurrentFile(zFile); unzClose(zFile); return false; } @@ -316,14 +324,6 @@ bool CodeSigning::RunParseZipInfo(const std::string& packageName, UnzipHandlePar PrintErrorNumberMsg("IO_ERROR", IO_ERROR, "zlib open file: " + packageName + " failed."); return false; } - // get zipFile all paramets - unz_global_info zGlobalInfo; - int getRet = unzGetGlobalInfo(zFile, &zGlobalInfo); - if (getRet != UNZ_OK) { - PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "zlib get global info failed."); - unzClose(zFile); - return false; - } for (uLong i = 0; i < index; ++i) { int ret = unzGoToNextFile(zFile); if (ret != UNZ_OK) { @@ -344,7 +344,7 @@ bool CodeSigning::RunParseZipInfo(const std::string& packageName, UnzipHandlePar unzClose(zFile); return false; } - if (!CheckFileName(zFile, fileName, &nameLen)) { + if (!CheckFileName(fileName, &nameLen)) { unzClose(zFile); return true; } @@ -365,9 +365,26 @@ bool CodeSigning::HandleZipGlobalInfo(const std::string& packageName, unzFile& z { std::vector> thread_results; SIGNATURE_TOOLS_LOGI("zGlobalInfo.number_entry = %lu", zGlobalInfo.number_entry); + for (uLong i = 0; i < zGlobalInfo.number_entry; ++i) { - thread_results.push_back(mPools->Enqueue(&CodeSigning::RunParseZipInfo, this, - std::ref(packageName), std::ref(param), i)); + unz_file_info zFileInfo; + char fileName[FILE_NAME_SIZE]; + size_t nameLen = 0; + if (memset_s(fileName, FILE_NAME_SIZE, 0, FILE_NAME_SIZE) != 0) { + return false; + } + if (!CheckUnzParam(zFile, zFileInfo, fileName, &nameLen)) { + return false; + } + if (CheckFileName(fileName, &nameLen)) { + thread_results.push_back(mPools->Enqueue(&CodeSigning::RunParseZipInfo, this, + std::ref(packageName), std::ref(param), i)); + } + + int ret = unzGoToNextFile(zFile); + if (ret != UNZ_OK && i != zGlobalInfo.number_entry - 1) { + return false; + } } bool result = true; @@ -460,28 +477,19 @@ bool CodeSigning::CheckUnzParam(unzFile& zFile, unz_file_info& zFileInfo, PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "Open zipFile fileName is null"); return false; } - if (UNZ_OK != unzOpenCurrentFile(zFile)) { - PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, - "unzOpenCurrentFile zipFile error"); - return false; - } return true; } -bool CodeSigning::CheckFileName(unzFile& zFile, char fileName[], size_t* nameLen) +bool CodeSigning::CheckFileName(char fileName[], size_t* nameLen) { if (fileName[*nameLen - 1] == '/') { SIGNATURE_TOOLS_LOGI("It is dictionary."); - unzCloseCurrentFile(zFile); - unzGoToNextFile(zFile); return false; } std::string str(fileName); bool nativeFileFlag = IsNativeFile(str); if (!nativeFileFlag) { SIGNATURE_TOOLS_LOGI("Suffix mismatched."); - unzCloseCurrentFile(zFile); - unzGoToNextFile(zFile); return false; } return true; -- Gitee From 0b67a868c1547f76afebe112a6f68550396ab005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=97=8B=E9=A3=8Elc?= Date: Mon, 9 Dec 2024 16:43:47 +0800 Subject: [PATCH 2/4] modify Signed-off-by: liuchang --- hapsigntool_cpp/codesigning/sign/src/code_signing.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp b/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp index 33c80445..935ce811 100644 --- a/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp +++ b/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp @@ -282,7 +282,6 @@ bool CodeSigning::GetSingleFileStreamFromZip(unzFile &zFile, char fileName[], if (UNZ_OK != unzOpenCurrentFile(zFile)) { PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "unzOpenCurrentFile zipFile error"); - unzClose(zFile); return false; } @@ -294,7 +293,6 @@ bool CodeSigning::GetSingleFileStreamFromZip(unzFile &zFile, char fileName[], if (memset_s(szReadBuffer, BUFFER_SIZE, 0, BUFFER_SIZE) != EOK) { SIGNATURE_TOOLS_LOGE("memset_s failed"); unzCloseCurrentFile(zFile); - unzClose(zFile); return false; } nReadFileSize = unzReadCurrentFile(zFile, szReadBuffer, BUFFER_SIZE); @@ -308,12 +306,10 @@ bool CodeSigning::GetSingleFileStreamFromZip(unzFile &zFile, char fileName[], PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "zlib read stream from " + std::string(fileName) + " failed."); unzCloseCurrentFile(zFile); - unzClose(zFile); return false; } unzCloseCurrentFile(zFile); - unzClose(zFile); return true; } @@ -336,6 +332,7 @@ bool CodeSigning::RunParseZipInfo(const std::string& packageName, UnzipHandlePar int readFileSize = 0; std::stringbuf sb; if (memset_s(fileName, FILE_NAME_SIZE, 0, FILE_NAME_SIZE) != 0) { + SIGNATURE_TOOLS_LOGE("memory of fileName memset_s failed"); unzClose(zFile); return false; } @@ -349,6 +346,7 @@ bool CodeSigning::RunParseZipInfo(const std::string& packageName, UnzipHandlePar return true; } bool flag = GetSingleFileStreamFromZip(zFile, fileName, zFileInfo, readFileSize, sb); + unzClose(zFile); if (!flag) { return false; } @@ -371,6 +369,7 @@ bool CodeSigning::HandleZipGlobalInfo(const std::string& packageName, unzFile& z char fileName[FILE_NAME_SIZE]; size_t nameLen = 0; if (memset_s(fileName, FILE_NAME_SIZE, 0, FILE_NAME_SIZE) != 0) { + SIGNATURE_TOOLS_LOGE("memory of fileName memset_s failed"); return false; } if (!CheckUnzParam(zFile, zFileInfo, fileName, &nameLen)) { -- Gitee From 9722eb501938af77a1b11a9056f236a9ef2ee153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=97=8B=E9=A3=8Elc?= Date: Mon, 9 Dec 2024 17:07:07 +0800 Subject: [PATCH 3/4] modify Signed-off-by: liuchang --- hapsigntool_cpp/codesigning/sign/src/code_signing.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp b/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp index 935ce811..3c81f39c 100644 --- a/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp +++ b/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp @@ -323,6 +323,7 @@ bool CodeSigning::RunParseZipInfo(const std::string& packageName, UnzipHandlePar for (uLong i = 0; i < index; ++i) { int ret = unzGoToNextFile(zFile); if (ret != UNZ_OK) { + PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "zlib go to next file failed."); unzClose(zFile); return false; } @@ -382,6 +383,7 @@ bool CodeSigning::HandleZipGlobalInfo(const std::string& packageName, unzFile& z int ret = unzGoToNextFile(zFile); if (ret != UNZ_OK && i != zGlobalInfo.number_entry - 1) { + PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "zlib go to next file failed."); return false; } } -- Gitee From 04faef422316a24e97eae91a74f854ef0994c8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E6=97=8B=E9=A3=8Elc?= Date: Mon, 9 Dec 2024 20:20:35 +0800 Subject: [PATCH 4/4] modify Signed-off-by: liuchang --- hapsigntool_cpp/codesigning/sign/src/code_signing.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp b/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp index 3c81f39c..fb9afde8 100644 --- a/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp +++ b/hapsigntool_cpp/codesigning/sign/src/code_signing.cpp @@ -380,11 +380,12 @@ bool CodeSigning::HandleZipGlobalInfo(const std::string& packageName, unzFile& z thread_results.push_back(mPools->Enqueue(&CodeSigning::RunParseZipInfo, this, std::ref(packageName), std::ref(param), i)); } - - int ret = unzGoToNextFile(zFile); - if (ret != UNZ_OK && i != zGlobalInfo.number_entry - 1) { - PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "zlib go to next file failed."); - return false; + if (i != zGlobalInfo.number_entry - 1) { + int ret = unzGoToNextFile(zFile); + if (ret != UNZ_OK) { + PrintErrorNumberMsg("SIGN_ERROR", SIGN_ERROR, "zlib go to next file failed."); + return false; + } } } -- Gitee