diff --git a/interfaces/innerkits/appverify/include/common/random_access_file.h b/interfaces/innerkits/appverify/include/common/random_access_file.h index e58af928d4152026c2060b4dd308685beef4725b..3e7c79914b7247c68f1d4d7626ba06ae7e72352b 100644 --- a/interfaces/innerkits/appverify/include/common/random_access_file.h +++ b/interfaces/innerkits/appverify/include/common/random_access_file.h @@ -33,7 +33,7 @@ class RandomAccessFile { public: DLL_EXPORT RandomAccessFile(); DLL_EXPORT ~RandomAccessFile(); - DLL_EXPORT bool Init(const std::string& filePath); + DLL_EXPORT bool Init(const std::string& filePath, bool readFile = false); DLL_EXPORT bool InitWithFd(const int32_t fileFd); DLL_EXPORT long long GetLength() const; DLL_EXPORT long long ReadFileFullyFromOffset(HapByteBuffer& buffer, long long offset); @@ -50,7 +50,7 @@ private: static int32_t memoryPageSize; int32_t fd = 0; long long fileLength; - bool readFile = false; + bool readFile_ = false; }; } // namespace Verify } // namespace Security diff --git a/interfaces/innerkits/appverify/include/interfaces/hap_verify.h b/interfaces/innerkits/appverify/include/interfaces/hap_verify.h index 2c85893b15faa55d889ff03e960e30420e41634a..919767509dd4fbf85ee8a344dda8e4da19f4a0d0 100644 --- a/interfaces/innerkits/appverify/include/interfaces/hap_verify.h +++ b/interfaces/innerkits/appverify/include/interfaces/hap_verify.h @@ -26,7 +26,7 @@ namespace Security { namespace Verify { DLL_EXPORT bool EnableDebugMode(); DLL_EXPORT void DisableDebugMode(); -DLL_EXPORT int32_t HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult); +DLL_EXPORT int32_t HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult, bool readFile = false); DLL_EXPORT int32_t ParseHapProfile(const std::string& filePath, HapVerifyResult& hapVerifyV1Result); DLL_EXPORT int32_t ParseHapSignatureInfo(const std::string& filePath, SignatureInfo &hapSignInfo); extern "C" DLL_EXPORT int32_t ParseBundleNameAndAppIdentifier(const int32_t fileFd, std::string &bundleName, diff --git a/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h b/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h index c0ced011ddee1d01f8e79d08b308c65a48cb1b29..c2aabbb24bdd04564239a44417585c86d42b0b3f 100644 --- a/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h +++ b/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h @@ -29,7 +29,7 @@ namespace Security { namespace Verify { class HapVerifyV2 { public: - int32_t Verify(const std::string& filePath, HapVerifyResult& hapVerifyV1Result); + int32_t Verify(const std::string& filePath, HapVerifyResult& hapVerifyV1Result, bool readFile = false); int32_t Verify(const int32_t fileFd, HapVerifyResult& hapVerifyV1Result); int32_t ParseHapProfile(const std::string& filePath, HapVerifyResult& hapVerifyV1Result); int32_t ParseHapSignatureInfo(const std::string& filePath, SignatureInfo &hapSignInfo); diff --git a/interfaces/innerkits/appverify/src/common/random_access_file.cpp b/interfaces/innerkits/appverify/src/common/random_access_file.cpp index 6418e03aedfe7e8c646ac53f35a16f4b3adc7697..d654f6630b09aaff6db6a69defa38af7ab7a2f5a 100644 --- a/interfaces/innerkits/appverify/src/common/random_access_file.cpp +++ b/interfaces/innerkits/appverify/src/common/random_access_file.cpp @@ -43,7 +43,7 @@ RandomAccessFile::~RandomAccessFile() } } -bool RandomAccessFile::Init(const std::string& filePath) +bool RandomAccessFile::Init(const std::string& filePath, bool readFile) { fd = open(filePath.c_str(), O_RDONLY); if (fd == FILE_OPEN_FAIL_ERROR_NUM) { @@ -60,6 +60,7 @@ bool RandomAccessFile::Init(const std::string& filePath) HAPVERIFY_LOG_ERROR("getting fileLength failed: %{public}lld", fileLength); return false; } + readFile_ = readFile; return true; } @@ -85,7 +86,7 @@ bool RandomAccessFile::InitWithFd(const int32_t fileFd) HAPVERIFY_LOG_ERROR("getting fileLength failed: %{public}lld", fileLength); return false; } - readFile = true; + readFile_ = true; return true; } @@ -131,7 +132,7 @@ long long RandomAccessFile::DoMMap(int32_t bufCapacity, long long offset, MmapIn long long RandomAccessFile::ReadFileFullyFromOffset(char buf[], long long offset, int32_t bufCapacity) { - if (readFile) { + if (readFile_) { return ReadFileFullyFromOffsetV2(buf, offset, bufCapacity); } if (buf == nullptr) { @@ -155,7 +156,7 @@ long long RandomAccessFile::ReadFileFullyFromOffset(char buf[], long long offset long long RandomAccessFile::ReadFileFullyFromOffset(HapByteBuffer& buffer, long long offset) { - if (readFile) { + if (readFile_) { return ReadFileFullyFromOffsetV2(buffer, offset); } if (!buffer.HasRemaining()) { @@ -177,7 +178,7 @@ long long RandomAccessFile::ReadFileFullyFromOffset(HapByteBuffer& buffer, long bool RandomAccessFile::ReadFileFromOffsetAndDigestUpdate(const DigestParameter& digestParam, int32_t chunkSize, long long offset) { - if (readFile) { + if (readFile_) { return ReadFileFromOffsetAndDigestUpdateV2(digestParam, chunkSize, offset); } MmapInfo mmapInfo; @@ -221,7 +222,11 @@ long long RandomAccessFile::ReadFileFullyFromOffsetV2(HapByteBuffer& buffer, lon HAPVERIFY_LOG_ERROR("Invalid buffer capacity"); return DEST_BUFFER_IS_NULL; } - char* buf = new char[bufCapacity]; + char* buf = new (std::nothrow) char[bufCapacity]; + if (buf == nullptr) { + HAPVERIFY_LOG_ERROR("Failed to allocate memory for buffer"); + return DEST_BUFFER_IS_NULL; + } long long bytesRead = pread(fd, buf, bufCapacity, offset); if (bytesRead < 0) { @@ -242,7 +247,7 @@ bool RandomAccessFile::ReadFileFromOffsetAndDigestUpdateV2(const DigestParameter HAPVERIFY_LOG_ERROR("Invalid chunkSize"); return false; } - unsigned char* buffer = new unsigned char[chunkSize]; + unsigned char* buffer = new (std::nothrow) unsigned char[chunkSize]; if (buffer == nullptr) { HAPVERIFY_LOG_ERROR("Failed to allocate memory for buffer"); return false; diff --git a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp index 02febe284888674f01575b7465c2ae04549419b4..e0b3bed915835087f8d523f7f1a9039f8c67afa7 100644 --- a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp +++ b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp @@ -84,13 +84,13 @@ void SetDevMode(DevMode mode) g_mtx.unlock(); } -int32_t HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult) +int32_t HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult, bool readFile) { if (!g_isInit && !HapVerifyInit()) { return VERIFY_SOURCE_INIT_FAIL; } HapVerifyV2 hapVerifyV2; - return hapVerifyV2.Verify(filePath, hapVerifyResult); + return hapVerifyV2.Verify(filePath, hapVerifyResult, readFile); } int32_t ParseHapProfile(const std::string& filePath, HapVerifyResult& hapVerifyV1Result) diff --git a/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp b/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp index 65f0a680b29b00d23746ee6fdd1db676738ddb5a..a1193f8f1c24c60111a6bcd4338630fb55caf089 100644 --- a/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp +++ b/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp @@ -41,7 +41,7 @@ const std::string HapVerifyV2::HAP_APP_PATTERN = "[^]*.hap$"; const std::string HapVerifyV2::HQF_APP_PATTERN = "[^]*.hqf$"; const std::string HapVerifyV2::HSP_APP_PATTERN = "[^]*.hsp$"; -int32_t HapVerifyV2::Verify(const std::string& filePath, HapVerifyResult& hapVerifyV1Result) +int32_t HapVerifyV2::Verify(const std::string& filePath, HapVerifyResult& hapVerifyV1Result, bool readFile) { HAPVERIFY_LOG_DEBUG("Start Verify"); std::string standardFilePath; @@ -50,7 +50,7 @@ int32_t HapVerifyV2::Verify(const std::string& filePath, HapVerifyResult& hapVer } RandomAccessFile hapFile; - if (!hapFile.Init(standardFilePath)) { + if (!hapFile.Init(standardFilePath, readFile)) { HAPVERIFY_LOG_ERROR("open standard file failed"); return OPEN_FILE_ERROR; }