Add base extractor.

Signed-off-by: dy_study <dingyao5@huawei.com>
Change-Id: I732f8048df77f193030ca79977f20f5d0053e3b2
This commit is contained in:
dy_study
2022-08-23 18:33:58 +08:00
parent 0a1d1e4245
commit c8d5d35f2c
16 changed files with 1335 additions and 123 deletions
@@ -78,7 +78,7 @@ JsTestRunner::~JsTestRunner() = default;
bool JsTestRunner::Initialize()
{
if (isFaJsModel_) {
if (!jsRuntime_.RunScript("/system/etc/strip.native.min.abc", hapPath_)) {
if (!jsRuntime_.RunScript("/system/etc/strip.native.min.abc", "")) {
HILOG_ERROR("RunScript err");
return false;
}
+137
View File
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "base_extractor.h"
#include <fstream>
#include "hilog_wrapper.h"
#include "string_ex.h"
namespace OHOS {
namespace AbilityRuntime {
namespace {
constexpr const char* MODULE_PROFILE_NAME = "module.json";
}
BaseExtractor::BaseExtractor(const std::string &source) : sourceFile_(source), zipFile_(source)
{
HILOG_DEBUG("BaseExtractor instance is created");
}
BaseExtractor::~BaseExtractor()
{
HILOG_DEBUG("BaseExtractor instance is destroyed");
}
bool BaseExtractor::Init()
{
if (!zipFile_.Open()) {
HILOG_ERROR("open zip file failed");
return false;
}
ZipEntry zipEntry;
isNewVersion_ = zipFile_.GetEntry(MODULE_PROFILE_NAME, zipEntry);
initial_ = true;
HILOG_DEBUG("success");
return true;
}
bool BaseExtractor::HasEntry(const std::string &fileName) const
{
if (!initial_) {
HILOG_ERROR("extractor is not initial");
return false;
}
return zipFile_.HasEntry(fileName);
}
bool BaseExtractor::IsDirExist(const std::string &dir) const
{
if (!initial_) {
HILOG_ERROR("extractor is not initial");
return false;
}
if (dir.empty()) {
HILOG_ERROR("param dir empty");
return false;
}
return zipFile_.IsDirExist(dir);
}
bool BaseExtractor::ExtractByName(const std::string &fileName, std::ostream &dest) const
{
if (!initial_) {
HILOG_ERROR("extractor is not initial");
return false;
}
if (!zipFile_.ExtractFile(fileName, dest)) {
HILOG_ERROR("extractor is not ExtractFile");
return false;
}
return true;
}
bool BaseExtractor::ExtractFile(const std::string &fileName, const std::string &targetPath) const
{
HILOG_DEBUG("begin to extract %{public}s file into %{private}s targetPath", fileName.c_str(), targetPath.c_str());
std::ofstream fileStream;
fileStream.open(targetPath, std::ios_base::out | std::ios_base::binary);
if (!fileStream.is_open()) {
HILOG_ERROR("fail to open %{private}s file to write", targetPath.c_str());
return false;
}
if ((!ExtractByName(fileName, fileStream)) || (!fileStream.good())) {
HILOG_ERROR("fail to extract %{public}s zip file into stream", fileName.c_str());
fileStream.clear();
fileStream.close();
if (remove(targetPath.c_str()) != 0) {
HILOG_ERROR("fail to remove %{private}s file which writes stream error", targetPath.c_str());
}
return false;
}
fileStream.clear();
fileStream.close();
return true;
}
bool BaseExtractor::GetZipFileNames(std::vector<std::string> &fileNames)
{
auto &entryMap = zipFile_.GetAllEntries();
for (auto &entry : entryMap) {
fileNames.emplace_back(entry.first);
}
return true;
}
bool BaseExtractor::IsStageBasedModel(std::string abilityName)
{
auto &entryMap = zipFile_.GetAllEntries();
std::vector<std::string> splitStrs;
OHOS::SplitStr(abilityName, ".", splitStrs);
std::string name = splitStrs.empty() ? abilityName : splitStrs.back();
std::string entry = "assets/js/" + name + "/" + name + ".js";
bool isStageBasedModel = entryMap.find(entry) != entryMap.end();
HILOG_DEBUG("name:%{public}s isStageBasedModel:%{public}d", abilityName.c_str(), isStageBasedModel);
return isStageBasedModel;
}
bool BaseExtractor::IsNewVersion() const
{
return isNewVersion_;
}
} // namespace AbilityRuntime
} // namespace OHOS
+133
View File
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "extractor_utils.h"
#include <regex>
#include "ability_constants.h"
#include "hilog_wrapper.h"
#include "runtime_extractor.h"
namespace OHOS {
namespace AbilityRuntime {
namespace {
inline bool StringStartWith(const std::string& str, const char* startStr, size_t startStrLen)
{
return ((str.length() >= startStrLen) && (str.compare(0, startStrLen, startStr) == 0));
}
} // namespace
std::shared_ptr<RuntimeExtractor> InitRuntimeExtractor(const std::string& hapPath)
{
if (hapPath.empty()) {
HILOG_ERROR("InitRuntimeExtractor::hapPath is nullptr");
return nullptr;
}
std::string loadPath;
if (!StringStartWith(hapPath, Constants::SYSTEM_APP_PATH, sizeof(Constants::SYSTEM_APP_PATH) - 1)) {
std::regex hapPattern(std::string(Constants::ABS_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
loadPath = std::regex_replace(hapPath, hapPattern, "");
loadPath = std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR) +
loadPath.substr(loadPath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
} else {
loadPath = hapPath;
}
auto runtimeExtractor = std::make_shared<RuntimeExtractor>(loadPath, hapPath);
if (!runtimeExtractor->Init()) {
HILOG_ERROR("InitRuntimeExtractor::Runtime extractor init failed");
return nullptr;
}
return runtimeExtractor;
}
bool GetFileBuffer(
const std::shared_ptr<RuntimeExtractor>& runtimeExtractor, const std::string& srcPath, std::ostringstream &dest)
{
if (runtimeExtractor == nullptr || srcPath.empty()) {
HILOG_ERROR("GetFileBuffer::runtimeExtractor or srcPath is nullptr");
return false;
}
std::regex srcPattern(std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
std::string relativePath = std::regex_replace(srcPath, srcPattern, "");
relativePath = relativePath.substr(relativePath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
if (!runtimeExtractor->ExtractByName(relativePath, dest)) {
HILOG_ERROR("GetFileBuffer::Extract file failed");
return false;
}
return true;
}
bool GetFileBufferFromHap(const std::string& hapPath, const std::string& srcPath, std::ostringstream &dest)
{
if (hapPath.empty() || srcPath.empty()) {
HILOG_ERROR("GetFileBufferFromHap::hapPath or srcPath is nullptr");
return false;
}
return GetFileBuffer(InitRuntimeExtractor(hapPath), srcPath, dest);
}
bool GetFileListFromHap(const std::string& hapPath, const std::string& srcPath, std::vector<std::string>& assetList)
{
if (hapPath.empty() || srcPath.empty()) {
HILOG_ERROR("GetFileListFromHap::hapPath or srcPath is nullptr");
return false;
}
std::string loadPath;
if (!StringStartWith(hapPath, Constants::SYSTEM_APP_PATH, sizeof(Constants::SYSTEM_APP_PATH) - 1)) {
std::regex hapPattern(std::string(Constants::ABS_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
loadPath = std::regex_replace(hapPath, hapPattern, "");
loadPath = std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR) +
loadPath.substr(loadPath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
} else {
loadPath = hapPath;
}
RuntimeExtractor runtimeExtractor(loadPath);
if (!runtimeExtractor.Init()) {
HILOG_ERROR("GetFileListFromHap::Runtime extractor init failed");
return false;
}
std::regex srcPattern(std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
std::string relativePath = std::regex_replace(srcPath, srcPattern, "");
relativePath = relativePath.substr(relativePath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
std::vector<std::string> fileList;
if (!runtimeExtractor.GetZipFileNames(fileList)) {
HILOG_ERROR("GetFileListFromHap::Get file list failed");
return false;
}
std::regex replacePattern(relativePath);
for (auto value : fileList) {
if (StringStartWith(value, relativePath.c_str(), sizeof(relativePath.c_str()) - 1)) {
std::string realpath = std::regex_replace(value, replacePattern, "");
if (realpath.find(Constants::FILE_SEPARATOR) != std::string::npos) {
continue;
}
assetList.emplace_back(value);
}
}
return true;
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -15,6 +15,7 @@
#include "js_module_reader.h"
#include "extractor_utils.h"
#include "hilog_wrapper.h"
#include "js_runtime_utils.h"
#include "runtime_extractor.h"
+1
View File
@@ -24,6 +24,7 @@
#include "connect_server_manager.h"
#include "event_handler.h"
#include "extractor_utils.h"
#include "hdc_register.h"
#include "hilog_wrapper.h"
#include "js_console_log.h"
@@ -16,13 +16,9 @@
#include "js_runtime_utils.h"
#include <fstream>
#include <regex>
#include <string>
#include "ability_constants.h"
#include "hilog_wrapper.h"
#include "js_runtime.h"
#include "runtime_extractor.h"
#ifdef WINDOWS_PLATFORM
#include <io.h>
@@ -653,105 +649,5 @@ std::string NormalizeUri(
FixExtName(newJsModulePath);
return newJsModulePath;
}
std::shared_ptr<RuntimeExtractor> InitRuntimeExtractor(const std::string& hapPath)
{
if (hapPath.empty()) {
HILOG_ERROR("InitRuntimeExtractor::hapPath is nullptr");
return nullptr;
}
std::string loadPath;
if (!StringStartWith(hapPath, Constants::SYSTEM_APP_PATH, sizeof(Constants::SYSTEM_APP_PATH) - 1)) {
std::regex hapPattern(std::string(Constants::ABS_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
loadPath = std::regex_replace(hapPath, hapPattern, "");
loadPath = std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR) +
loadPath.substr(loadPath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
} else {
loadPath = hapPath;
}
auto runtimeExtractor = std::make_shared<RuntimeExtractor>(loadPath, hapPath);
if (!runtimeExtractor->Init()) {
HILOG_ERROR("GetFileBufferFromHap::Runtime extractor init failed");
return nullptr;
}
return runtimeExtractor;
}
bool GetFileBuffer(
const std::shared_ptr<RuntimeExtractor>& runtimeExtractor, const std::string& srcPath, std::ostream &dest)
{
if (runtimeExtractor == nullptr || srcPath.empty()) {
HILOG_ERROR("GetFileBuffer::runtimeExtractor or srcPath is nullptr");
return false;
}
std::regex srcPattern(std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
std::string relativePath = std::regex_replace(srcPath, srcPattern, "");
relativePath = relativePath.substr(relativePath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
if (!runtimeExtractor->ExtractByName(relativePath, dest)) {
HILOG_ERROR("GetFileBufferFromHap::Extract file failed");
return false;
}
return true;
}
bool GetFileBufferFromHap(const std::string& hapPath, const std::string& srcPath, std::ostream &dest)
{
if (hapPath.empty() || srcPath.empty()) {
HILOG_ERROR("GetFileBufferFromHap::hapPath or srcPath is nullptr");
return false;
}
return GetFileBuffer(InitRuntimeExtractor(hapPath), srcPath, dest);
}
bool GetFileListFromHap(const std::string& hapPath, const std::string& srcPath, std::vector<std::string>& assetList)
{
if (hapPath.empty() || srcPath.empty()) {
HILOG_ERROR("GetFileListFromHap::hapPath or srcPath is nullptr");
return false;
}
std::string loadPath;
if (!StringStartWith(hapPath, Constants::SYSTEM_APP_PATH, sizeof(Constants::SYSTEM_APP_PATH) - 1)) {
std::regex hapPattern(std::string(Constants::ABS_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
loadPath = std::regex_replace(hapPath, hapPattern, "");
loadPath = std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR) +
loadPath.substr(loadPath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
} else {
loadPath = hapPath;
}
RuntimeExtractor runtimeExtractor(loadPath);
if (!runtimeExtractor.Init()) {
HILOG_ERROR("GetFileListFromHap::Runtime extractor init failed");
return false;
}
std::regex srcPattern(std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR));
std::string relativePath = std::regex_replace(srcPath, srcPattern, "");
relativePath = relativePath.substr(relativePath.find(std::string(Constants::FILE_SEPARATOR)) + 1);
std::vector<std::string> fileList;
if (!runtimeExtractor.GetZipFileNames(fileList)) {
HILOG_ERROR("GetFileListFromHap::Get file list failed");
return false;
}
std::regex replacePattern(relativePath);
for (auto value : fileList) {
if (StringStartWith(value, relativePath.c_str(), sizeof(relativePath.c_str()) - 1)) {
std::string realpath = std::regex_replace(value, replacePattern, "");
if (realpath.find(Constants::FILE_SEPARATOR) != std::string::npos) {
continue;
}
assetList.emplace_back(value);
}
}
return true;
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -19,13 +19,13 @@
namespace OHOS {
namespace AbilityRuntime {
RuntimeExtractor::RuntimeExtractor(const std::string &source) : AppExecFwk::BaseExtractor(source)
RuntimeExtractor::RuntimeExtractor(const std::string &source) : BaseExtractor(source)
{
HILOG_DEBUG("RuntimeExtractor is created");
}
RuntimeExtractor::RuntimeExtractor(
const std::string &source, const std::string &hapPath) : AppExecFwk::BaseExtractor(source)
const std::string &source, const std::string &hapPath) : BaseExtractor(source)
{
hapPath_ = hapPath;
}
+614
View File
@@ -0,0 +1,614 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "zip_file.h"
#include <cassert>
#include <cstring>
#include <ostream>
#include "hilog_wrapper.h"
#include "securec.h"
#include "zlib.h"
namespace OHOS {
namespace AbilityRuntime {
namespace {
constexpr uint32_t MAX_FILE_NAME = 256;
constexpr uint32_t UNZIP_BUFFER_SIZE = 1024;
constexpr uint32_t UNZIP_BUF_IN_LEN = 160 * UNZIP_BUFFER_SIZE; // in buffer length: 160KB
constexpr uint32_t UNZIP_BUF_OUT_LEN = 320 * UNZIP_BUFFER_SIZE; // out buffer length: 320KB
constexpr uint32_t LOCAL_HEADER_SIGNATURE = 0x04034b50;
constexpr uint32_t CENTRAL_SIGNATURE = 0x02014b50;
constexpr uint32_t EOCD_SIGNATURE = 0x06054b50;
constexpr uint32_t DATA_DESC_SIGNATURE = 0x08074b50;
constexpr uint32_t FLAG_DATA_DESC = 0x8;
constexpr size_t FILE_READ_COUNT = 1;
constexpr uint8_t INFLATE_ERROR_TIMES = 5;
const char FILE_SEPARATOR_CHAR = '/';
} // namespace
ZipEntry::ZipEntry(const CentralDirEntry &centralEntry)
{
compressionMethod = centralEntry.compressionMethod;
uncompressedSize = centralEntry.uncompressedSize;
compressedSize = centralEntry.compressedSize;
localHeaderOffset = centralEntry.localHeaderOffset;
crc = centralEntry.crc;
flags = centralEntry.flags;
}
ZipFile::ZipFile(const std::string &pathName) : pathName_(pathName)
{
HILOG_DEBUG("create instance from %{private}s", pathName_.c_str());
}
ZipFile::~ZipFile()
{
Close();
}
void ZipFile::SetContentLocation(const ZipPos start, const size_t length)
{
HILOG_DEBUG("set content location start position(%{public}llu), length(%{public}zu)", start, length);
fileStartPos_ = start;
fileLength_ = length;
}
bool ZipFile::CheckEndDir(const EndDir &endDir) const
{
size_t lenEndDir = sizeof(EndDir);
if ((endDir.numDisk != 0) || (endDir.signature != EOCD_SIGNATURE) || (endDir.startDiskOfCentralDir != 0) ||
(endDir.offset >= fileLength_) || (endDir.totalEntriesInThisDisk != endDir.totalEntries) ||
(endDir.commentLen != 0) ||
// central dir can't overlap end of central dir
((endDir.offset + endDir.sizeOfCentralDir + lenEndDir) > fileLength_)) {
HILOG_ERROR("end dir format error");
return false;
}
return true;
}
bool ZipFile::ParseEndDirectory()
{
size_t endDirLen = sizeof(EndDir);
size_t endFilePos = fileStartPos_ + fileLength_;
if (fileLength_ <= endDirLen) {
HILOG_ERROR("parse EOCD file length(%{public}llu) <= end dir length(%{public}llu)", fileStartPos_, fileLength_);
return false;
}
size_t eocdPos = endFilePos - endDirLen;
if (fseek(file_, eocdPos, SEEK_SET) != 0) {
HILOG_ERROR("locate EOCD seek failed, error: %{public}d", errno);
return false;
}
if (fread(&endDir_, sizeof(EndDir), FILE_READ_COUNT, file_) != FILE_READ_COUNT) {
HILOG_ERROR("read EOCD struct failed, error: %{public}d", errno);
return false;
}
centralDirPos_ = endDir_.offset + fileStartPos_;
HILOG_DEBUG("parse EOCD offset(0x%{public}08x) file start position(0x%{public}08llx)",
endDir_.offset, fileStartPos_);
return CheckEndDir(endDir_);
}
bool ZipFile::ParseAllEntries()
{
bool ret = true;
ZipPos currentPos = centralDirPos_;
CentralDirEntry directoryEntry = {0};
size_t fileLength = 0;
for (uint16_t i = 0; i < endDir_.totalEntries; i++) {
std::string fileName;
fileName.reserve(MAX_FILE_NAME);
fileName.resize(MAX_FILE_NAME - 1);
if (fseek(file_, currentPos, SEEK_SET) != 0) {
HILOG_ERROR("parse entry(%{public}d) seek zipEntry failed, error: %{public}d", i, errno);
ret = false;
break;
}
if (fread(&directoryEntry, sizeof(CentralDirEntry), FILE_READ_COUNT, file_) != FILE_READ_COUNT) {
HILOG_ERROR("parse entry(%{public}d) read ZipEntry failed, error: %{public}d", i, errno);
ret = false;
break;
}
if (directoryEntry.signature != CENTRAL_SIGNATURE) {
HILOG_ERROR("parse entry(%{public}d) check signature(0x%08x) at pos(0x%08llx) failed",
i,
directoryEntry.signature,
currentPos);
ret = false;
break;
}
fileLength = (directoryEntry.nameSize >= MAX_FILE_NAME) ? (MAX_FILE_NAME - 1) : directoryEntry.nameSize;
if (fread(&(fileName[0]), fileLength, FILE_READ_COUNT, file_) != FILE_READ_COUNT) {
HILOG_ERROR("parse entry(%{public}d) read file name failed, error: %{public}d", i, errno);
ret = false;
break;
}
fileName.resize(fileLength);
ZipEntry currentEntry(directoryEntry);
currentEntry.fileName = fileName;
entriesMap_[fileName] = currentEntry;
currentPos += sizeof(directoryEntry);
currentPos += directoryEntry.nameSize + directoryEntry.extraSize + directoryEntry.commentSize;
}
HILOG_DEBUG("parse %{public}d central entries from %{private}s", endDir_.totalEntries, pathName_.c_str());
return ret;
}
bool ZipFile::Open()
{
HILOG_DEBUG("open: %{private}s", pathName_.c_str());
if (isOpen_) {
HILOG_ERROR("has already opened");
return true;
}
if (pathName_.length() > PATH_MAX) {
HILOG_ERROR("path length(%{public}u) longer than max path length(%{public}d)",
static_cast<unsigned int>(pathName_.length()),
PATH_MAX);
return false;
}
std::string realPath;
realPath.reserve(PATH_MAX);
realPath.resize(PATH_MAX - 1);
if (realpath(pathName_.c_str(), &(realPath[0])) == nullptr) {
HILOG_ERROR("transform real path error: %{public}d", errno);
return false;
}
FILE *tmpFile = fopen(realPath.c_str(), "rb");
if (tmpFile == nullptr) {
HILOG_ERROR("open file(%{private}s) failed, error: %{public}d", pathName_.c_str(), errno);
return false;
}
if (fileLength_ == 0) {
if (fseek(tmpFile, 0, SEEK_END) != 0) {
HILOG_ERROR("file seek failed, error: %{public}d", errno);
fclose(tmpFile);
return false;
}
int64_t fileLength = ftell(tmpFile);
if (fileLength == -1) {
HILOG_ERROR("open file %{private}s failed", pathName_.c_str());
fclose(tmpFile);
return false;
}
fileLength_ = static_cast<ZipPos>(fileLength);
if (fileStartPos_ >= fileLength_) {
HILOG_ERROR("open start pos > length failed");
fclose(tmpFile);
return false;
}
fileLength_ -= fileStartPos_;
}
file_ = tmpFile;
bool result = ParseEndDirectory();
if (result) {
result = ParseAllEntries();
}
// it means open file success.
isOpen_ = true;
return result;
}
void ZipFile::Close()
{
HILOG_DEBUG("close: %{private}s", pathName_.c_str());
if (!isOpen_ || file_ == nullptr) {
HILOG_WARN("file is not opened");
return;
}
entriesMap_.clear();
pathName_ = "";
isOpen_ = false;
if (fclose(file_) != 0) {
HILOG_WARN("close failed, error: %{public}d", errno);
}
file_ = nullptr;
}
// Get all file zipEntry in this file
const ZipEntryMap &ZipFile::GetAllEntries() const
{
return entriesMap_;
}
bool ZipFile::HasEntry(const std::string &entryName) const
{
return entriesMap_.find(entryName) != entriesMap_.end();
}
bool ZipFile::IsDirExist(const std::string &dir) const
{
HILOG_DEBUG("target dir: %{public}s", dir.c_str());
if (dir.empty()) {
HILOG_ERROR("target dir is empty");
return false;
}
auto tempDir = dir;
if (tempDir.back() != FILE_SEPARATOR_CHAR) {
tempDir.push_back(FILE_SEPARATOR_CHAR);
}
for (const auto &item : entriesMap_) {
if (item.first.find(tempDir) == 0) {
HILOG_DEBUG("find target dir, fileName : %{public}s", item.first.c_str());
return true;
}
}
HILOG_DEBUG("target dir not found, dir : %{public}s", dir.c_str());
return false;
}
bool ZipFile::GetEntry(const std::string &entryName, ZipEntry &resultEntry) const
{
HILOG_DEBUG("get entry by name: %{public}s", entryName.c_str());
auto iter = entriesMap_.find(entryName);
if (iter != entriesMap_.end()) {
resultEntry = iter->second;
HILOG_DEBUG("get entry succeed");
return true;
}
HILOG_ERROR("get entry failed");
return false;
}
size_t ZipFile::GetLocalHeaderSize(const uint16_t nameSize, const uint16_t extraSize) const
{
return sizeof(LocalHeader) + nameSize + extraSize;
}
bool ZipFile::CheckDataDesc(const ZipEntry &zipEntry, const LocalHeader &localHeader) const
{
uint32_t crcLocal = 0;
uint32_t compressedLocal = 0;
uint32_t uncompressedLocal = 0;
if (localHeader.flags & FLAG_DATA_DESC) { // use data desc
DataDesc dataDesc;
auto descPos = zipEntry.localHeaderOffset + GetLocalHeaderSize(localHeader.nameSize, localHeader.extraSize);
descPos += fileStartPos_ + zipEntry.compressedSize;
if (fseek(file_, descPos, SEEK_SET) != 0) {
HILOG_ERROR("check local header seek datadesc failed, error: %{public}d", errno);
return false;
}
if (fread(&dataDesc, sizeof(DataDesc), FILE_READ_COUNT, file_) != FILE_READ_COUNT) {
HILOG_ERROR("check local header read datadesc failed, error: %{public}d", errno);
return false;
}
if (dataDesc.signature != DATA_DESC_SIGNATURE) {
HILOG_ERROR("check local header check datadesc signature failed");
return false;
}
crcLocal = dataDesc.crc;
compressedLocal = dataDesc.compressedSize;
uncompressedLocal = dataDesc.uncompressedSize;
} else {
crcLocal = localHeader.crc;
compressedLocal = localHeader.compressedSize;
uncompressedLocal = localHeader.uncompressedSize;
}
if ((zipEntry.crc != crcLocal) || (zipEntry.compressedSize != compressedLocal) ||
(zipEntry.uncompressedSize != uncompressedLocal)) {
HILOG_ERROR("check local header compressed size corrupted");
return false;
}
return true;
}
bool ZipFile::CheckCoherencyLocalHeader(const ZipEntry &zipEntry, uint16_t &extraSize) const
{
LocalHeader localHeader = {0};
if (zipEntry.localHeaderOffset >= fileLength_) {
HILOG_ERROR("check local file header offset is overflow %{public}d", zipEntry.localHeaderOffset);
return false;
}
if (fseek(file_, fileStartPos_ + zipEntry.localHeaderOffset, SEEK_SET) != 0) {
HILOG_ERROR("check local header seek failed, error: %{public}d", errno);
return false;
}
if (fread(&localHeader, sizeof(LocalHeader), FILE_READ_COUNT, file_) != FILE_READ_COUNT) {
HILOG_ERROR("check local header read localheader failed, error: %{public}d", errno);
return false;
}
if ((localHeader.signature != LOCAL_HEADER_SIGNATURE) ||
(zipEntry.compressionMethod != localHeader.compressionMethod)) {
HILOG_ERROR("check local header signature or compressionMethod failed");
return false;
}
// current only support store and Z_DEFLATED method
if ((zipEntry.compressionMethod != Z_DEFLATED) && (zipEntry.compressionMethod != 0)) {
HILOG_ERROR("check local header compressionMethod(%{public}d) not support", zipEntry.compressionMethod);
return false;
}
std::string fileName;
fileName.reserve(MAX_FILE_NAME);
fileName.resize(MAX_FILE_NAME - 1);
size_t fileLength = (localHeader.nameSize >= MAX_FILE_NAME) ? (MAX_FILE_NAME - 1) : localHeader.nameSize;
if (fileLength != zipEntry.fileName.length()) {
HILOG_ERROR("check local header file name size failed");
return false;
}
if (fread(&(fileName[0]), fileLength, FILE_READ_COUNT, file_) != FILE_READ_COUNT) {
HILOG_ERROR("check local header read file name failed, error: %{public}d", errno);
return false;
}
fileName.resize(fileLength);
if (zipEntry.fileName != fileName) {
HILOG_ERROR("check local header file name corrupted");
return false;
}
if (!CheckDataDesc(zipEntry, localHeader)) {
HILOG_ERROR("check data desc failed");
return false;
}
extraSize = localHeader.extraSize;
return true;
}
bool ZipFile::SeekToEntryStart(const ZipEntry &zipEntry, const uint16_t extraSize) const
{
ZipPos startOffset = zipEntry.localHeaderOffset;
// get data offset, add signature+localheader+namesize+extrasize
startOffset += GetLocalHeaderSize(zipEntry.fileName.length(), extraSize);
if (startOffset + zipEntry.compressedSize > fileLength_) {
HILOG_ERROR("startOffset(%{public}lld)+entryCompressedSize(%{public}ud) > fileLength(%{public}llu)",
startOffset,
zipEntry.compressedSize,
fileLength_);
return false;
}
startOffset += fileStartPos_; // add file start relative to file stream
HILOG_DEBUG("seek to entry start 0x%{public}08llx", startOffset);
if (fseek(file_, startOffset, SEEK_SET) != 0) {
HILOG_ERROR("seek failed, error: %{public}d", errno);
return false;
}
return true;
}
bool ZipFile::UnzipWithStore(const ZipEntry &zipEntry, const uint16_t extraSize, std::ostream &dest) const
{
HILOG_DEBUG("unzip with store");
if (!SeekToEntryStart(zipEntry, extraSize)) {
HILOG_ERROR("seek to entry start failed");
return false;
}
uint32_t remainSize = zipEntry.compressedSize;
std::string readBuffer;
readBuffer.reserve(UNZIP_BUF_OUT_LEN);
readBuffer.resize(UNZIP_BUF_OUT_LEN - 1);
while (remainSize > 0) {
size_t readBytes;
size_t readLen = (remainSize > UNZIP_BUF_OUT_LEN) ? UNZIP_BUF_OUT_LEN : remainSize;
readBytes = fread(&(readBuffer[0]), sizeof(Byte), readLen, file_);
if (readBytes == 0) {
HILOG_ERROR("unzip store read failed, error: %{public}d", ferror(file_));
return false;
}
remainSize -= readBytes;
dest.write(&(readBuffer[0]), readBytes);
}
return true;
}
bool ZipFile::InitZStream(z_stream &zstream) const
{
// init zlib stream
if (memset_s(&zstream, sizeof(z_stream), 0, sizeof(z_stream))) {
HILOG_ERROR("unzip stream buffer init failed");
return false;
}
int32_t zlibErr = inflateInit2(&zstream, -MAX_WBITS);
if (zlibErr != Z_OK) {
HILOG_ERROR("unzip inflated init failed");
return false;
}
BytePtr bufOut = new (std::nothrow) Byte[UNZIP_BUF_OUT_LEN];
if (bufOut == nullptr) {
HILOG_ERROR("unzip inflated new out buffer failed");
return false;
}
BytePtr bufIn = new (std::nothrow) Byte[UNZIP_BUF_IN_LEN];
if (bufIn == nullptr) {
HILOG_ERROR("unzip inflated new in buffer failed");
delete[] bufOut;
return false;
}
zstream.next_out = bufOut;
zstream.next_in = bufIn;
zstream.avail_out = UNZIP_BUF_OUT_LEN;
return true;
}
bool ZipFile::ReadZStream(const BytePtr &buffer, z_stream &zstream, uint32_t &remainCompressedSize) const
{
if (zstream.avail_in == 0) {
size_t readBytes;
size_t remainBytes = (remainCompressedSize > UNZIP_BUF_IN_LEN) ? UNZIP_BUF_IN_LEN : remainCompressedSize;
readBytes = fread(buffer, sizeof(Byte), remainBytes, file_);
if (readBytes == 0) {
HILOG_ERROR("unzip inflated read failed, error: %{public}d", ferror(file_));
return false;
}
remainCompressedSize -= readBytes;
zstream.avail_in = readBytes;
zstream.next_in = buffer;
}
return true;
}
bool ZipFile::UnzipWithInflated(const ZipEntry &zipEntry, const uint16_t extraSize, std::ostream &dest) const
{
HILOG_DEBUG("unzip with inflated");
z_stream zstream;
if (!SeekToEntryStart(zipEntry, extraSize) || !InitZStream(zstream)) {
return false;
}
BytePtr bufIn = zstream.next_in;
BytePtr bufOut = zstream.next_out;
bool ret = true;
int32_t zlibErr = Z_OK;
uint32_t remainCompressedSize = zipEntry.compressedSize;
size_t inflateLen = 0;
uint8_t errorTimes = 0;
while ((remainCompressedSize > 0) || (zstream.avail_in > 0)) {
if (!ReadZStream(bufIn, zstream, remainCompressedSize)) {
ret = false;
break;
}
zlibErr = inflate(&zstream, Z_SYNC_FLUSH);
if ((zlibErr >= Z_OK) && (zstream.msg != nullptr)) {
HILOG_ERROR("unzip inflated inflate, error: %{public}d, err msg: %{public}s", zlibErr, zstream.msg);
ret = false;
break;
}
inflateLen = UNZIP_BUF_OUT_LEN - zstream.avail_out;
if (inflateLen > 0) {
dest.write((const char *)bufOut, inflateLen);
zstream.next_out = bufOut;
zstream.avail_out = UNZIP_BUF_OUT_LEN;
errorTimes = 0;
} else {
errorTimes++;
}
if (errorTimes >= INFLATE_ERROR_TIMES) {
HILOG_ERROR("unzip inflated data is abnormal!");
ret = false;
break;
}
}
// free all dynamically allocated data structures except the next_in and next_out for this stream.
zlibErr = inflateEnd(&zstream);
if (zlibErr != Z_OK) {
HILOG_ERROR("unzip inflateEnd error, error: %{public}d", zlibErr);
ret = false;
}
delete[] bufOut;
delete[] bufIn;
return ret;
}
ZipPos ZipFile::GetEntryDataOffset(const ZipEntry &zipEntry, const uint16_t extraSize) const
{
// get entry data offset relative file
ZipPos offset = zipEntry.localHeaderOffset;
offset += GetLocalHeaderSize(zipEntry.fileName.length(), extraSize);
offset += fileStartPos_;
return offset;
}
bool ZipFile::GetDataOffsetRelative(const std::string &file, ZipPos &offset, uint32_t &length) const
{
HILOG_DEBUG("get data relative offset for file %{private}s", file.c_str());
ZipEntry zipEntry;
if (!GetEntry(file, zipEntry)) {
HILOG_ERROR("extract file: not find file");
return false;
}
uint16_t extraSize = 0;
if (!CheckCoherencyLocalHeader(zipEntry, extraSize)) {
HILOG_ERROR("check coherency local header failed");
return false;
}
offset = GetEntryDataOffset(zipEntry, extraSize);
length = zipEntry.compressedSize;
return true;
}
bool ZipFile::ExtractFile(const std::string &file, std::ostream &dest) const
{
HILOG_DEBUG("extract file %{private}s", file.c_str());
ZipEntry zipEntry;
if (!GetEntry(file, zipEntry)) {
HILOG_ERROR("extract file: not find file");
return false;
}
uint16_t extraSize = 0;
if (!CheckCoherencyLocalHeader(zipEntry, extraSize)) {
HILOG_ERROR("check coherency local header failed");
return false;
}
bool ret = true;
if (zipEntry.compressionMethod == 0) {
ret = UnzipWithStore(zipEntry, extraSize, dest);
} else {
ret = UnzipWithInflated(zipEntry, extraSize, dest);
}
return ret;
}
} // namespace AbilityRuntime
} // namespace OHOS
@@ -67,14 +67,16 @@ ohos_shared_library("ability_simulator") {
sources = [
"//foundation/ability/ability_runtime/frameworks/native/runtime/js_console_log.cpp",
"//foundation/ability/ability_runtime/frameworks/native/runtime/js_module_reader.cpp",
"//foundation/ability/ability_runtime/frameworks/native/runtime/js_module_searcher.cpp",
"//foundation/ability/ability_runtime/frameworks/native/runtime/js_runtime_utils.cpp",
"src/js_timer.cpp",
"src/simulator.cpp",
]
public_configs = [ ":ability_simulator_public_config" ]
public_configs = [
":ability_simulator_public_config",
"${ability_runtime_services_path}/common:common_config",
]
configs = [ "//arkcompiler/ets_runtime:ark_jsruntime_public_config" ]
@@ -84,7 +86,6 @@ ohos_shared_library("ability_simulator") {
"//arkcompiler/ets_runtime/ecmascript/tooling:libark_ecma_debugger",
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_$platform",
"//foundation/ability/ability_runtime/frameworks/simulator/osal:simulator_osal",
"//foundation/ability/ability_runtime/interfaces/inner_api/runtime:runtime_extractor",
"//foundation/arkui/ace_engine/frameworks/bridge/js_frontend/engine/jsi/debugger:ark_debugger",
"//foundation/arkui/napi:ace_napi",
"//foundation/arkui/napi:ace_napi_ark",
@@ -348,7 +348,6 @@ bool SimulatorImpl::OnInit()
std::bind(&DebuggerTask::OnPostTask, &debuggerTask_, std::placeholders::_1));
panda::JSNApi::SetHostResolvePathTracker(vm_, JsModuleSearcher(""));
panda::JSNApi::SetHostResolveBufferTracker(vm_, JsModuleReader("", ""));
auto nativeEngine = std::make_unique<ArkNativeEngine>(vm_, nullptr);
HandleScope handleScope(*nativeEngine);
+29 -6
View File
@@ -34,6 +34,7 @@ config("runtime_public_config") {
ohos_shared_library("runtime") {
sources = [
"${ability_runtime_native_path}/runtime/connect_server_manager.cpp",
"${ability_runtime_native_path}/runtime/extractor_utils.cpp",
"${ability_runtime_native_path}/runtime/hdc_register.cpp",
"${ability_runtime_native_path}/runtime/js_console_log.cpp",
"${ability_runtime_native_path}/runtime/js_data_struct_converter.cpp",
@@ -51,7 +52,10 @@ ohos_shared_library("runtime") {
"//arkcompiler/ets_runtime:ark_jsruntime_public_config",
]
public_configs = [ ":runtime_public_config" ]
public_configs = [
":runtime_public_config",
"${ability_runtime_services_path}/common:common_config",
]
deps = [
":runtime_extractor",
@@ -93,21 +97,40 @@ ohos_shared_library("runtime") {
part_name = "ability_runtime"
}
config("ability_extractor_config") {
include_dirs = [
"include",
"//third_party/json/include",
"//third_party/zlib/contrib/minizip",
"//third_party/zlib",
]
}
ohos_source_set("runtime_extractor") {
include_dirs = [ "include" ]
sources = [ "${ability_runtime_native_path}/runtime/runtime_extractor.cpp" ]
configs = [ "${ability_runtime_services_path}/common:common_config" ]
sources = [
"${ability_runtime_native_path}/runtime/base_extractor.cpp",
"${ability_runtime_native_path}/runtime/runtime_extractor.cpp",
"${ability_runtime_native_path}/runtime/zip_file.cpp",
]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
public_deps = [ "${bundlefwk_services_path}/bundlemgr:parser_common" ]
public_configs = [
":ability_extractor_config",
"${ability_runtime_services_path}/common:common_config",
]
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
deps = [ "//third_party/zlib:shared_libz" ]
external_deps = [
"c_utils:utils",
"hiviewdfx_hilog_native:libhilog",
]
subsystem_name = "ability"
part_name = "ability_runtime"
+73
View File
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_ABILITY_RUNTIME_BASE_EXTRACTOR_H
#define OHOS_ABILITY_RUNTIME_BASE_EXTRACTOR_H
#include <string>
#include "zip_file.h"
namespace OHOS {
namespace AbilityRuntime {
class BaseExtractor {
public:
explicit BaseExtractor(const std::string &source);
virtual ~BaseExtractor();
/**
* @brief Open compressed file.
* @return Returns true if the file is successfully opened; returns false otherwise.
*/
virtual bool Init();
/**
* @brief Extract to dest stream by file name.
* @param fileName Indicates the file name.
* @param dest Indicates the obtained std::ostream object.
* @return Returns true if the file extracted successfully; returns false otherwise.
*/
bool ExtractByName(const std::string &fileName, std::ostream &dest) const;
/**
* @brief Extract to dest path on filesystem.
* @param fileName Indicates the file name.
* @param targetPath Indicates the target Path.
* @return Returns true if the file extracted to filesystem successfully; returns false otherwise.
*/
bool ExtractFile(const std::string &fileName, const std::string &targetPath) const;
/**
* @brief Get all file names in a hap file.
* @param fileName Indicates the obtained file names in hap.
* @return Returns true if the file names obtained successfully; returns false otherwise.
*/
bool GetZipFileNames(std::vector<std::string> &fileNames);
/**
* @brief Has entry by name.
* @param entryName Indicates the entry name.
* @return Returns true if the ZipEntry is successfully finded; returns false otherwise.
*/
bool HasEntry(const std::string &fileName) const;
bool IsDirExist(const std::string &dir) const;
bool IsStageBasedModel(std::string abilityName);
bool IsNewVersion() const;
protected:
const std::string sourceFile_;
ZipFile zipFile_;
bool initial_ = false;
private:
bool isNewVersion_ = true;
};
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_BASE_EXTRACTOR_H
+31
View File
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_ABILITY_RUNTIME_EXTRACTOR_UTILS_H
#define OHOS_ABILITY_RUNTIME_EXTRACTOR_UTILS_H
#include "js_runtime_utils.h"
namespace OHOS {
namespace AbilityRuntime {
std::shared_ptr<RuntimeExtractor> InitRuntimeExtractor(const std::string& hapPath);
bool GetFileBuffer(
const std::shared_ptr<RuntimeExtractor>& runtimeExtractor, const std::string& srcPath, std::ostringstream &dest);
bool GetFileBufferFromHap(const std::string& hapPath, const std::string& srcPath, std::ostringstream &dest);
bool GetFileListFromHap(const std::string& hapPath, const std::string& srcPath, std::vector<std::string>& assetList);
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_EXTRACTOR_UTILS_H
@@ -200,12 +200,7 @@ std::string ParseJsModuleUri(const std::string& curJsModulePath, const std::stri
bool MakeFilePath(const std::string& codePath, const std::string& modulePath, std::string& fileName);
std::string NormalizeUri(
const std::string& bundleName, const std::string& curJsModulePath, const std::string& newJsModuleUri);
std::shared_ptr<RuntimeExtractor> InitRuntimeExtractor(const std::string& hapPath);
std::string ParseHapPath(const std::string& hapPath);
bool GetFileBuffer(
const std::shared_ptr<RuntimeExtractor>& runtimeExtractor, const std::string& srcPath, std::ostream &dest);
bool GetFileBufferFromHap(const std::string& hapPath, const std::string& srcPath, std::ostream &dest);
bool GetFileListFromHap(const std::string& hapPath, const std::string& srcPath, std::vector<std::string>& assetList);
} // namespace AbilityRuntime
} // namespace OHOS
@@ -20,7 +20,7 @@
namespace OHOS {
namespace AbilityRuntime {
class RuntimeExtractor : public AppExecFwk::BaseExtractor {
class RuntimeExtractor : public BaseExtractor {
public:
explicit RuntimeExtractor(const std::string &source);
explicit RuntimeExtractor(const std::string &source, const std::string &hapPath);
+308
View File
@@ -0,0 +1,308 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_ABILITY_RUNTIME_ZIP_FILE_H
#define OHOS_ABILITY_RUNTIME_ZIP_FILE_H
#include <cstdint>
#include <map>
#include <string>
#include "unzip.h"
namespace OHOS {
namespace AbilityRuntime {
struct CentralDirEntry;
struct ZipEntry;
using ZipPos = ZPOS64_T;
using ZipEntryMap = std::map<std::string, ZipEntry>;
using BytePtr = Byte *;
// Local file header: descript in APPNOTE-6.3.4
// local file header signature 4 bytes (0x04034b50)
// version needed to extract 2 bytes
// general purpose bit flag 2 bytes
// compression method 2 bytes 10
// last mod file time 2 bytes
// last mod file date 2 bytes
// crc-32 4 bytes
// compressed size 4 bytes 22
// uncompressed size 4 bytes
// file name length 2 bytes
// extra field length 2 bytes 30
struct __attribute__((packed)) LocalHeader {
uint32_t signature = 0;
uint16_t versionNeeded = 0;
uint16_t flags = 0;
uint16_t compressionMethod = 0;
uint16_t modifiedTime = 0;
uint16_t modifiedDate = 0;
uint32_t crc = 0;
uint32_t compressedSize = 0;
uint32_t uncompressedSize = 0;
uint16_t nameSize = 0;
uint16_t extraSize = 0;
};
// central file header
// Central File header:
// central file header signature 4 bytes (0x02014b50)
// version made by 2 bytes
// version needed to extract 2 bytes
// general purpose bit flag 2 bytes 10
// compression method 2 bytes
// last mod file time 2 bytes
// last mod file date 2 bytes
// crc-32 4 bytes 20
// compressed size 4 bytes
// uncompressed size 4 bytes
// file name length 2 bytes 30
// extra field length 2 bytes
// file comment length 2 bytes
// disk number start 2 bytes
// internal file attributes 2 bytes
// external file attributes 4 bytes
// relative offset of local header 4 bytes 46byte
struct __attribute__((packed)) CentralDirEntry {
uint32_t signature = 0;
uint16_t versionMade = 0;
uint16_t versionNeeded = 0;
uint16_t flags = 0; // general purpose bit flag
uint16_t compressionMethod = 0;
uint16_t modifiedTime = 0;
uint16_t modifiedDate = 0;
uint32_t crc = 0;
uint32_t compressedSize = 0;
uint32_t uncompressedSize = 0;
uint16_t nameSize = 0;
uint16_t extraSize = 0;
uint16_t commentSize = 0;
uint16_t diskNumStart = 0;
uint16_t internalAttr = 0;
uint32_t externalAttr = 0;
uint32_t localHeaderOffset = 0;
};
// end of central directory packed structure
// end of central dir signature 4 bytes (0x06054b50)
// number of this disk 2 bytes
// number of the disk with the
// start of the central directory 2 bytes
// total number of entries in the
// central directory on this disk 2 bytes
// total number of entries in
// the central directory 2 bytes
// size of the central directory 4 bytes
// offset of start of central
// directory with respect to
// the starting disk number 4 bytes
// .ZIP file comment length 2 bytes
struct __attribute__((packed)) EndDir {
uint32_t signature = 0;
uint16_t numDisk = 0;
uint16_t startDiskOfCentralDir = 0;
uint16_t totalEntriesInThisDisk = 0;
uint16_t totalEntries = 0;
uint32_t sizeOfCentralDir = 0;
uint32_t offset = 0;
uint16_t commentLen = 0;
};
// Data descriptor:
// data descriptor signature 4 bytes (0x06054b50)
// crc-32 4 bytes
// compressed size 4 bytes
// uncompressed size 4 bytes
// This descriptor MUST exist if bit 3 of the general purpose bit flag is set (see below).
// It is byte aligned and immediately follows the last byte of compressed data.
struct __attribute__((packed)) DataDesc {
uint32_t signature = 0;
uint32_t crc = 0;
uint32_t compressedSize = 0;
uint32_t uncompressedSize = 0;
};
struct ZipEntry {
ZipEntry() = default;
explicit ZipEntry(const CentralDirEntry &centralEntry);
~ZipEntry() = default; // for CodeDEX warning
uint16_t compressionMethod = 0;
uint32_t uncompressedSize = 0;
uint32_t compressedSize = 0;
uint32_t localHeaderOffset = 0;
uint32_t crc = 0;
uint16_t flags = 0;
std::string fileName;
};
// zip file extract class for bundle format.
class ZipFile {
public:
explicit ZipFile(const std::string &pathName);
~ZipFile();
/**
* @brief Open zip file.
* @return Returns true if the zip file is successfully opened; returns false otherwise.
*/
bool Open();
/**
* @brief Close zip file.
*/
void Close();
/**
* @brief Set this zip content start offset and length in the zip file form pathName.
* @param start Indicates the zip content location start position.
* @param length Indicates the zip content length.
*/
void SetContentLocation(ZipPos start, size_t length);
/**
* @brief Get all entries in the zip file.
* @param start Indicates the zip content location start position.
* @param length Indicates the zip content length.
* @return Returns the ZipEntryMap object cotain all entries.
*/
const ZipEntryMap &GetAllEntries() const;
/**
* @brief Has entry by name.
* @param entryName Indicates the entry name.
* @return Returns true if the ZipEntry is successfully finded; returns false otherwise.
*/
bool HasEntry(const std::string &entryName) const;
bool IsDirExist(const std::string &dir) const;
/**
* @brief Get entry by name.
* @param entryName Indicates the entry name.
* @param resultEntry Indicates the obtained ZipEntry object.
* @return Returns true if the ZipEntry is successfully finded; returns false otherwise.
*/
bool GetEntry(const std::string &entryName, ZipEntry &resultEntry) const;
/**
* @brief Get data relative offset for file.
* @param file Indicates the entry name.
* @param offset Indicates the obtained offset.
* @param length Indicates the length.
* @return Returns true if this function is successfully called; returns false otherwise.
*/
bool GetDataOffsetRelative(const std::string &file, ZipPos &offset, uint32_t &length) const;
/**
* @brief Get data relative offset for file.
* @param file Indicates the entry name.
* @param dest Indicates the obtained ostream object.
* @return Returns true if file is successfully extracted; returns false otherwise.
*/
bool ExtractFile(const std::string &file, std::ostream &dest) const;
private:
/**
* @brief Check the EndDir object.
* @param endDir Indicates the EndDir object to check.
* @return Returns true if successfully checked; returns false otherwise.
*/
bool CheckEndDir(const EndDir &endDir) const;
/**
* @brief Parse the EndDir.
* @return Returns true if successfully Parsed; returns false otherwise.
*/
bool ParseEndDirectory();
/**
* @brief Parse all Entries.
* @return Returns true if successfully parsed; returns false otherwise.
*/
bool ParseAllEntries();
/**
* @brief Get LocalHeader object size.
* @param nameSize Indicates the nameSize.
* @param extraSize Indicates the extraSize.
* @return Returns size of LocalHeader.
*/
size_t GetLocalHeaderSize(const uint16_t nameSize = 0, const uint16_t extraSize = 0) const;
/**
* @brief Get entry data offset.
* @param zipEntry Indicates the ZipEntry object.
* @param extraSize Indicates the extraSize.
* @return Returns position.
*/
ZipPos GetEntryDataOffset(const ZipEntry &zipEntry, const uint16_t extraSize) const;
/**
* @brief Check data description.
* @param zipEntry Indicates the ZipEntry object.
* @param localHeader Indicates the localHeader object.
* @return Returns true if successfully checked; returns false otherwise.
*/
bool CheckDataDesc(const ZipEntry &zipEntry, const LocalHeader &localHeader) const;
/**
* @brief Check coherency LocalHeader object.
* @param zipEntry Indicates the ZipEntry object.
* @param extraSize Indicates the obtained size.
* @return Returns true if successfully checked; returns false otherwise.
*/
bool CheckCoherencyLocalHeader(const ZipEntry &zipEntry, uint16_t &extraSize) const;
/**
* @brief Unzip ZipEntry object to ostream.
* @param zipEntry Indicates the ZipEntry object.
* @param extraSize Indicates the size.
* @param dest Indicates the obtained ostream object.
* @return Returns true if successfully Unzip; returns false otherwise.
*/
bool UnzipWithStore(const ZipEntry &zipEntry, const uint16_t extraSize, std::ostream &dest) const;
/**
* @brief Unzip ZipEntry object to ostream.
* @param zipEntry Indicates the ZipEntry object.
* @param extraSize Indicates the size.
* @param dest Indicates the obtained ostream object.
* @return Returns true if successfully Unzip; returns false otherwise.
*/
bool UnzipWithInflated(const ZipEntry &zipEntry, const uint16_t extraSize, std::ostream &dest) const;
/**
* @brief Seek to Entry start.
* @param zipEntry Indicates the ZipEntry object.
* @param extraSize Indicates the extra size.
* @return Returns true if successfully Seeked; returns false otherwise.
*/
bool SeekToEntryStart(const ZipEntry &zipEntry, const uint16_t extraSize) const;
/**
* @brief Init zlib stream.
* @param zstream Indicates the obtained z_stream object.
* @return Returns true if successfully init; returns false otherwise.
*/
bool InitZStream(z_stream &zstream) const;
/**
* @brief Read zlib stream.
* @param buffer Indicates the buffer to read.
* @param zstream Indicates the obtained z_stream object.
* @param remainCompressedSize Indicates the obtained size.
* @return Returns true if successfully read; returns false otherwise.
*/
bool ReadZStream(const BytePtr &buffer, z_stream &zstream, uint32_t &remainCompressedSize) const;
private:
std::string pathName_;
FILE *file_ = nullptr;
EndDir endDir_;
ZipEntryMap entriesMap_;
// offset of central directory relative to zip file.
ZipPos centralDirPos_ = 0;
// this zip content start offset relative to zip file.
ZipPos fileStartPos_ = 0;
// this zip content length in the zip file.
ZipPos fileLength_ = 0;
bool isOpen_ = false;
};
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_ZIP_FILE_H