From 0b1f6bad45d5a66ea626387a737cc3d916baa107 Mon Sep 17 00:00:00 2001 From: Zhou Shihui Date: Wed, 30 Oct 2024 14:40:53 +0800 Subject: [PATCH] =?UTF-8?q?fd=E9=AA=8C=E7=AD=BE=E6=8E=A5=E5=8F=A3=E4=BD=BF?= =?UTF-8?q?=E7=94=A8read=E4=BB=A3=E6=9B=BFmmap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Zhou Shihui --- .../include/common/random_access_file.h | 4 + .../src/common/random_access_file.cpp | 77 +++++++++++++++++++ .../appverify/src/interfaces/hap_verify.cpp | 1 + 3 files changed, 82 insertions(+) diff --git a/interfaces/innerkits/appverify/include/common/random_access_file.h b/interfaces/innerkits/appverify/include/common/random_access_file.h index 2fab5b3..e58af92 100644 --- a/interfaces/innerkits/appverify/include/common/random_access_file.h +++ b/interfaces/innerkits/appverify/include/common/random_access_file.h @@ -43,10 +43,14 @@ public: private: long long DoMMap(int32_t bufCapacity, long long offset, MmapInfo& mmapInfo); bool CheckLittleEndian(); + long long ReadFileFullyFromOffsetV2(char buf[], long long offset, int32_t bufCapacity); + long long ReadFileFullyFromOffsetV2(HapByteBuffer& buffer, long long offset); + bool ReadFileFromOffsetAndDigestUpdateV2(const DigestParameter& digestParam, int32_t chunkSize, long long offset); static const int32_t FILE_OPEN_FAIL_ERROR_NUM; static int32_t memoryPageSize; int32_t fd = 0; long long fileLength; + bool readFile = false; }; } // namespace Verify } // namespace Security diff --git a/interfaces/innerkits/appverify/src/common/random_access_file.cpp b/interfaces/innerkits/appverify/src/common/random_access_file.cpp index a28e8b9..6418e03 100644 --- a/interfaces/innerkits/appverify/src/common/random_access_file.cpp +++ b/interfaces/innerkits/appverify/src/common/random_access_file.cpp @@ -85,6 +85,7 @@ bool RandomAccessFile::InitWithFd(const int32_t fileFd) HAPVERIFY_LOG_ERROR("getting fileLength failed: %{public}lld", fileLength); return false; } + readFile = true; return true; } @@ -130,6 +131,9 @@ 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) { + return ReadFileFullyFromOffsetV2(buf, offset, bufCapacity); + } if (buf == nullptr) { return DEST_BUFFER_IS_NULL; } @@ -151,6 +155,9 @@ long long RandomAccessFile::ReadFileFullyFromOffset(char buf[], long long offset long long RandomAccessFile::ReadFileFullyFromOffset(HapByteBuffer& buffer, long long offset) { + if (readFile) { + return ReadFileFullyFromOffsetV2(buffer, offset); + } if (!buffer.HasRemaining()) { return DEST_BUFFER_IS_NULL; } @@ -170,6 +177,9 @@ long long RandomAccessFile::ReadFileFullyFromOffset(HapByteBuffer& buffer, long bool RandomAccessFile::ReadFileFromOffsetAndDigestUpdate(const DigestParameter& digestParam, int32_t chunkSize, long long offset) { + if (readFile) { + return ReadFileFromOffsetAndDigestUpdateV2(digestParam, chunkSize, offset); + } MmapInfo mmapInfo; long long ret = DoMMap(chunkSize, offset, mmapInfo); if (ret < 0) { @@ -182,6 +192,73 @@ bool RandomAccessFile::ReadFileFromOffsetAndDigestUpdate(const DigestParameter& munmap(mmapInfo.mapAddr, mmapInfo.mmapSize); return res; } + +long long RandomAccessFile::ReadFileFullyFromOffsetV2(char buf[], long long offset, int32_t bufCapacity) +{ + if (buf == nullptr) { + HAPVERIFY_LOG_ERROR("buf is null"); + return DEST_BUFFER_IS_NULL; + } + + long long bytesRead = pread(fd, buf, bufCapacity, offset); + if (bytesRead < 0) { + HAPVERIFY_LOG_ERROR("pread failed: %{public}d", errno); + return bytesRead; + } + + return bytesRead; +} + +long long RandomAccessFile::ReadFileFullyFromOffsetV2(HapByteBuffer& buffer, long long offset) +{ + if (!buffer.HasRemaining()) { + HAPVERIFY_LOG_ERROR("buffer has no remaining space"); + return DEST_BUFFER_IS_NULL; + } + + int32_t bufCapacity = buffer.GetCapacity(); + if (bufCapacity <= 0) { + HAPVERIFY_LOG_ERROR("Invalid buffer capacity"); + return DEST_BUFFER_IS_NULL; + } + char* buf = new char[bufCapacity]; + + long long bytesRead = pread(fd, buf, bufCapacity, offset); + if (bytesRead < 0) { + HAPVERIFY_LOG_ERROR("pread failed: %{public}lld", bytesRead); + delete[] buf; + return bytesRead; + } + + buffer.PutData(0, buf, bytesRead); + delete[] buf; + return bytesRead; +} + +bool RandomAccessFile::ReadFileFromOffsetAndDigestUpdateV2(const DigestParameter& digestParam, + int32_t chunkSize, long long offset) +{ + if (chunkSize <= 0) { + HAPVERIFY_LOG_ERROR("Invalid chunkSize"); + return false; + } + unsigned char* buffer = new unsigned char[chunkSize]; + if (buffer == nullptr) { + HAPVERIFY_LOG_ERROR("Failed to allocate memory for buffer"); + return false; + } + + long long bytesRead = pread(fd, buffer, chunkSize, offset); + if (bytesRead < 0) { + HAPVERIFY_LOG_ERROR("pread failed: %{public}lld", bytesRead); + delete[] buffer; + return false; + } + + bool res = HapVerifyOpensslUtils::DigestUpdate(digestParam, buffer, bytesRead); + delete[] buffer; + return res; +} } // namespace Verify } // namespace Security } // namespace OHOS diff --git a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp index 39a78e7..02febe2 100644 --- a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp +++ b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp @@ -108,6 +108,7 @@ int32_t ParseHapSignatureInfo(const std::string& filePath, SignatureInfo &hapSig int32_t ParseBundleNameAndAppIdentifier(const int32_t fileFd, std::string &bundleName, std::string &appIdentifier) { + HAPVERIFY_LOG_INFO("start -n %{public}s", bundleName.c_str()); if (fileFd <= -1) { HAPVERIFY_LOG_ERROR("fd invalid"); return OPEN_FILE_ERROR; -- Gitee