154aa6d63Sopenharmony_ci/*
254aa6d63Sopenharmony_ci * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
354aa6d63Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
454aa6d63Sopenharmony_ci * you may not use this file except in compliance with the License.
554aa6d63Sopenharmony_ci * You may obtain a copy of the License at
654aa6d63Sopenharmony_ci *
754aa6d63Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
854aa6d63Sopenharmony_ci *
954aa6d63Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1054aa6d63Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1154aa6d63Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1254aa6d63Sopenharmony_ci * See the License for the specific language governing permissions and
1354aa6d63Sopenharmony_ci * limitations under the License.
1454aa6d63Sopenharmony_ci */
1554aa6d63Sopenharmony_ci
1654aa6d63Sopenharmony_ci#include <algorithm>
1754aa6d63Sopenharmony_ci#include <filesystem>
1854aa6d63Sopenharmony_ci
1954aa6d63Sopenharmony_ci#include "file_utils.h"
2054aa6d63Sopenharmony_ci#include "zip_entry.h"
2154aa6d63Sopenharmony_ci#include "zip_signer.h"
2254aa6d63Sopenharmony_ci
2354aa6d63Sopenharmony_cinamespace OHOS {
2454aa6d63Sopenharmony_cinamespace SignatureTools {
2554aa6d63Sopenharmony_cibool ZipSigner::Init(std::ifstream& inputFile)
2654aa6d63Sopenharmony_ci{
2754aa6d63Sopenharmony_ci    if (!inputFile.good()) {
2854aa6d63Sopenharmony_ci        return false;
2954aa6d63Sopenharmony_ci    }
3054aa6d63Sopenharmony_ci
3154aa6d63Sopenharmony_ci    /* 1. get eocd data */
3254aa6d63Sopenharmony_ci    m_endOfCentralDirectory = GetZipEndOfCentralDirectory(inputFile);
3354aa6d63Sopenharmony_ci    if (!m_endOfCentralDirectory) {
3454aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("get eocd data failed.");
3554aa6d63Sopenharmony_ci        return false;
3654aa6d63Sopenharmony_ci    }
3754aa6d63Sopenharmony_ci
3854aa6d63Sopenharmony_ci    m_cDOffset = m_endOfCentralDirectory->GetOffset();
3954aa6d63Sopenharmony_ci
4054aa6d63Sopenharmony_ci    /* 2. use eocd's cd offset, get cd data */
4154aa6d63Sopenharmony_ci    if (!GetZipCentralDirectory(inputFile)) {
4254aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("get zip central directory failed.");
4354aa6d63Sopenharmony_ci        return false;
4454aa6d63Sopenharmony_ci    }
4554aa6d63Sopenharmony_ci
4654aa6d63Sopenharmony_ci    /* 3. use cd's entry offset and file size, get entry data */
4754aa6d63Sopenharmony_ci    if (!GetZipEntries(inputFile)) {
4854aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("get zip entries failed.");
4954aa6d63Sopenharmony_ci        return false;
5054aa6d63Sopenharmony_ci    }
5154aa6d63Sopenharmony_ci
5254aa6d63Sopenharmony_ci    ZipEntry* endEntry = m_zipEntries[m_zipEntries.size() - 1];
5354aa6d63Sopenharmony_ci    CentralDirectory* endCD = endEntry->GetCentralDirectory();
5454aa6d63Sopenharmony_ci    ZipEntryData* endEntryData = endEntry->GetZipEntryData();
5554aa6d63Sopenharmony_ci    m_signingOffset = endCD->GetOffset() + endEntryData->GetLength();
5654aa6d63Sopenharmony_ci
5754aa6d63Sopenharmony_ci    /* 4. file all data - eocd - cd - entry = sign block */
5854aa6d63Sopenharmony_ci    m_signingBlock = GetSigningBlock(inputFile);
5954aa6d63Sopenharmony_ci
6054aa6d63Sopenharmony_ci    return true;
6154aa6d63Sopenharmony_ci}
6254aa6d63Sopenharmony_ci
6354aa6d63Sopenharmony_ciEndOfCentralDirectory* ZipSigner::GetZipEndOfCentralDirectory(std::ifstream& input)
6454aa6d63Sopenharmony_ci{
6554aa6d63Sopenharmony_ci    /* move file pointer to the end */
6654aa6d63Sopenharmony_ci    input.seekg(0, std::ios::end);
6754aa6d63Sopenharmony_ci    uint64_t fileSize = static_cast<uint64_t>(input.tellg());
6854aa6d63Sopenharmony_ci    /* move file pointer to the begin */
6954aa6d63Sopenharmony_ci    input.seekg(0, std::ios::beg);
7054aa6d63Sopenharmony_ci
7154aa6d63Sopenharmony_ci    if (fileSize < EndOfCentralDirectory::EOCD_LENGTH) {
7254aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("find zip eocd failed");
7354aa6d63Sopenharmony_ci        return nullptr;
7454aa6d63Sopenharmony_ci    }
7554aa6d63Sopenharmony_ci
7654aa6d63Sopenharmony_ci    /* try to read EOCD without comment */
7754aa6d63Sopenharmony_ci    int eocdLength = EndOfCentralDirectory::EOCD_LENGTH;
7854aa6d63Sopenharmony_ci    m_eOCDOffset = fileSize - eocdLength;
7954aa6d63Sopenharmony_ci
8054aa6d63Sopenharmony_ci    std::string retStr;
8154aa6d63Sopenharmony_ci    int ret = FileUtils::ReadFileByOffsetAndLength(input, m_eOCDOffset, eocdLength, retStr);
8254aa6d63Sopenharmony_ci    if (0 != ret) {
8354aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("read eocd without comment failed in file");
8454aa6d63Sopenharmony_ci        return nullptr;
8554aa6d63Sopenharmony_ci    }
8654aa6d63Sopenharmony_ci
8754aa6d63Sopenharmony_ci    std::optional<EndOfCentralDirectory*> eocdByBytes = EndOfCentralDirectory::GetEOCDByBytes(retStr);
8854aa6d63Sopenharmony_ci    if (eocdByBytes) {
8954aa6d63Sopenharmony_ci        return eocdByBytes.value();
9054aa6d63Sopenharmony_ci    }
9154aa6d63Sopenharmony_ci
9254aa6d63Sopenharmony_ci    /* try to search EOCD with comment */
9354aa6d63Sopenharmony_ci    uint64_t eocdMaxLength = std::min(static_cast<uint64_t>(EndOfCentralDirectory::EOCD_LENGTH + MAX_COMMENT_LENGTH),
9454aa6d63Sopenharmony_ci        fileSize);
9554aa6d63Sopenharmony_ci    m_eOCDOffset = static_cast<uint64_t>(input.tellg()) - eocdMaxLength;
9654aa6d63Sopenharmony_ci
9754aa6d63Sopenharmony_ci    retStr.clear();
9854aa6d63Sopenharmony_ci    ret = FileUtils::ReadFileByOffsetAndLength(input, m_eOCDOffset, eocdMaxLength, retStr);
9954aa6d63Sopenharmony_ci    if (0 != ret) {
10054aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("read eocd with comment failed in file");
10154aa6d63Sopenharmony_ci        return nullptr;
10254aa6d63Sopenharmony_ci    }
10354aa6d63Sopenharmony_ci
10454aa6d63Sopenharmony_ci    for (uint64_t start = 0; start < eocdMaxLength; start++) {
10554aa6d63Sopenharmony_ci        eocdByBytes = EndOfCentralDirectory::GetEOCDByBytes(retStr, start);
10654aa6d63Sopenharmony_ci        if (eocdByBytes) {
10754aa6d63Sopenharmony_ci            m_eOCDOffset += start;
10854aa6d63Sopenharmony_ci            return eocdByBytes.value();
10954aa6d63Sopenharmony_ci        }
11054aa6d63Sopenharmony_ci    }
11154aa6d63Sopenharmony_ci    SIGNATURE_TOOLS_LOGE("read zip failed: can not find eocd in file");
11254aa6d63Sopenharmony_ci    PrintErrorNumberMsg("ZIP_ERROR", ZIP_ERROR, "can not find eocd in file");
11354aa6d63Sopenharmony_ci    return nullptr;
11454aa6d63Sopenharmony_ci}
11554aa6d63Sopenharmony_ci
11654aa6d63Sopenharmony_cibool ZipSigner::GetZipCentralDirectory(std::ifstream& input)
11754aa6d63Sopenharmony_ci{
11854aa6d63Sopenharmony_ci    input.seekg(0, std::ios::beg);
11954aa6d63Sopenharmony_ci
12054aa6d63Sopenharmony_ci    uint16_t cDtotal = m_endOfCentralDirectory->GetcDTotal();
12154aa6d63Sopenharmony_ci    m_zipEntries.reserve(cDtotal);
12254aa6d63Sopenharmony_ci    /* read full central directory bytes */
12354aa6d63Sopenharmony_ci    std::string retStr;
12454aa6d63Sopenharmony_ci
12554aa6d63Sopenharmony_ci    int ret = FileUtils::ReadFileByOffsetAndLength(input, m_cDOffset, m_endOfCentralDirectory->GetcDSize(), retStr);
12654aa6d63Sopenharmony_ci    if (0 != ret) {
12754aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("read full central directory failed in file");
12854aa6d63Sopenharmony_ci        return false;
12954aa6d63Sopenharmony_ci    }
13054aa6d63Sopenharmony_ci
13154aa6d63Sopenharmony_ci    if (retStr.size() < CentralDirectory::CD_LENGTH) {
13254aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("find zip cd failed");
13354aa6d63Sopenharmony_ci        return false;
13454aa6d63Sopenharmony_ci    }
13554aa6d63Sopenharmony_ci
13654aa6d63Sopenharmony_ci    ByteBuffer bf(retStr.c_str(), retStr.size());
13754aa6d63Sopenharmony_ci
13854aa6d63Sopenharmony_ci    std::string::size_type offset = 0;
13954aa6d63Sopenharmony_ci    /* one by one format central directory */
14054aa6d63Sopenharmony_ci    while (offset < retStr.size()) {
14154aa6d63Sopenharmony_ci        CentralDirectory* cd = new CentralDirectory();
14254aa6d63Sopenharmony_ci        if (!CentralDirectory::GetCentralDirectory(bf, cd)) {
14354aa6d63Sopenharmony_ci            return false;
14454aa6d63Sopenharmony_ci        }
14554aa6d63Sopenharmony_ci        ZipEntry* entry = new ZipEntry();
14654aa6d63Sopenharmony_ci        entry->SetCentralDirectory(cd);
14754aa6d63Sopenharmony_ci        m_zipEntries.emplace_back(entry);
14854aa6d63Sopenharmony_ci        offset += cd->GetLength();
14954aa6d63Sopenharmony_ci    }
15054aa6d63Sopenharmony_ci
15154aa6d63Sopenharmony_ci    if (offset + m_cDOffset != m_eOCDOffset) {
15254aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("cd end offset not equals to eocd offset, maybe this is a zip64 file");
15354aa6d63Sopenharmony_ci        return false;
15454aa6d63Sopenharmony_ci    }
15554aa6d63Sopenharmony_ci    return true;
15654aa6d63Sopenharmony_ci}
15754aa6d63Sopenharmony_ci
15854aa6d63Sopenharmony_cistd::string ZipSigner::GetSigningBlock(std::ifstream& file)
15954aa6d63Sopenharmony_ci{
16054aa6d63Sopenharmony_ci    uint64_t size = m_cDOffset - m_signingOffset;
16154aa6d63Sopenharmony_ci    if (size < 0) {
16254aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("signing offset in front of entry end");
16354aa6d63Sopenharmony_ci        return "";
16454aa6d63Sopenharmony_ci    }
16554aa6d63Sopenharmony_ci    if (size == 0) {
16654aa6d63Sopenharmony_ci        return "";
16754aa6d63Sopenharmony_ci    }
16854aa6d63Sopenharmony_ci
16954aa6d63Sopenharmony_ci    std::string retStr;
17054aa6d63Sopenharmony_ci    int ret = FileUtils::ReadFileByOffsetAndLength(file, m_signingOffset, size, retStr);
17154aa6d63Sopenharmony_ci    if (0 != ret) {
17254aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("read signing block failed in file");
17354aa6d63Sopenharmony_ci        return "";
17454aa6d63Sopenharmony_ci    }
17554aa6d63Sopenharmony_ci    return retStr;
17654aa6d63Sopenharmony_ci}
17754aa6d63Sopenharmony_ci
17854aa6d63Sopenharmony_cibool ZipSigner::GetZipEntries(std::ifstream& input)
17954aa6d63Sopenharmony_ci{
18054aa6d63Sopenharmony_ci    /* use central directory data, find entry data */
18154aa6d63Sopenharmony_ci    for (auto& entry : m_zipEntries) {
18254aa6d63Sopenharmony_ci        CentralDirectory* cd = entry->GetCentralDirectory();
18354aa6d63Sopenharmony_ci        uint32_t offset = cd->GetOffset();
18454aa6d63Sopenharmony_ci        uint32_t unCompressedSize = cd->GetUnCompressedSize();
18554aa6d63Sopenharmony_ci        uint32_t compressedSize = cd->GetCompressedSize();
18654aa6d63Sopenharmony_ci        uint32_t fileSize = cd->GetMethod() == FILE_UNCOMPRESS_METHOD_FLAG ? unCompressedSize : compressedSize;
18754aa6d63Sopenharmony_ci
18854aa6d63Sopenharmony_ci        ZipEntryData* zipEntryData = ZipEntryData::GetZipEntry(input, offset, fileSize);
18954aa6d63Sopenharmony_ci        if (!zipEntryData) {
19054aa6d63Sopenharmony_ci            return false;
19154aa6d63Sopenharmony_ci        }
19254aa6d63Sopenharmony_ci        if (m_cDOffset - offset < zipEntryData->GetLength()) {
19354aa6d63Sopenharmony_ci            SIGNATURE_TOOLS_LOGE("cd offset in front of entry end");
19454aa6d63Sopenharmony_ci            return false;
19554aa6d63Sopenharmony_ci        }
19654aa6d63Sopenharmony_ci        entry->SetZipEntryData(zipEntryData);
19754aa6d63Sopenharmony_ci    }
19854aa6d63Sopenharmony_ci    return true;
19954aa6d63Sopenharmony_ci}
20054aa6d63Sopenharmony_ci
20154aa6d63Sopenharmony_cibool ZipSigner::ToFile(std::ifstream& input, std::ofstream& output)
20254aa6d63Sopenharmony_ci{
20354aa6d63Sopenharmony_ci    SIGNATURE_TOOLS_LOGI("Zip To File begin");
20454aa6d63Sopenharmony_ci    if (!input.good()) {
20554aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("read zip input file failed");
20654aa6d63Sopenharmony_ci        return false;
20754aa6d63Sopenharmony_ci    }
20854aa6d63Sopenharmony_ci    if (!output.good()) {
20954aa6d63Sopenharmony_ci        SIGNATURE_TOOLS_LOGE("read zip output file failed");
21054aa6d63Sopenharmony_ci        return false;
21154aa6d63Sopenharmony_ci    }
21254aa6d63Sopenharmony_ci
21354aa6d63Sopenharmony_ci    for (const auto& entry : m_zipEntries) {
21454aa6d63Sopenharmony_ci        ZipEntryData* zipEntryData = entry->GetZipEntryData();
21554aa6d63Sopenharmony_ci        std::string zipEntryHeaderStr = zipEntryData->GetZipEntryHeader()->ToBytes();
21654aa6d63Sopenharmony_ci        if (!FileUtils::WriteByteToOutFile(zipEntryHeaderStr, output)) {
21754aa6d63Sopenharmony_ci            return false;
21854aa6d63Sopenharmony_ci        }
21954aa6d63Sopenharmony_ci
22054aa6d63Sopenharmony_ci        uint32_t fileOffset = zipEntryData->GetFileOffset();
22154aa6d63Sopenharmony_ci        uint32_t fileSize = zipEntryData->GetFileSize();
22254aa6d63Sopenharmony_ci        bool isSuccess = FileUtils::AppendWriteFileByOffsetToFile(input, output, fileOffset, fileSize);
22354aa6d63Sopenharmony_ci        if (!isSuccess) {
22454aa6d63Sopenharmony_ci            SIGNATURE_TOOLS_LOGE("write zip data failed");
22554aa6d63Sopenharmony_ci            return false;
22654aa6d63Sopenharmony_ci        }
22754aa6d63Sopenharmony_ci        DataDescriptor* dataDescriptor = zipEntryData->GetDataDescriptor();
22854aa6d63Sopenharmony_ci        if (dataDescriptor) {
22954aa6d63Sopenharmony_ci            std::string dataDescriptorStr = dataDescriptor->ToBytes();
23054aa6d63Sopenharmony_ci            if (!FileUtils::WriteByteToOutFile(dataDescriptorStr, output)) {
23154aa6d63Sopenharmony_ci                return false;
23254aa6d63Sopenharmony_ci            }
23354aa6d63Sopenharmony_ci        }
23454aa6d63Sopenharmony_ci    }
23554aa6d63Sopenharmony_ci
23654aa6d63Sopenharmony_ci    if (!m_signingBlock.empty()) {
23754aa6d63Sopenharmony_ci        if (!FileUtils::WriteByteToOutFile(m_signingBlock, output)) {
23854aa6d63Sopenharmony_ci            return false;
23954aa6d63Sopenharmony_ci        }
24054aa6d63Sopenharmony_ci    }
24154aa6d63Sopenharmony_ci
24254aa6d63Sopenharmony_ci    for (const auto& entry : m_zipEntries) {
24354aa6d63Sopenharmony_ci        CentralDirectory* cd = entry->GetCentralDirectory();
24454aa6d63Sopenharmony_ci        if (!FileUtils::WriteByteToOutFile(cd->ToBytes(), output)) {
24554aa6d63Sopenharmony_ci            return false;
24654aa6d63Sopenharmony_ci        }
24754aa6d63Sopenharmony_ci    }
24854aa6d63Sopenharmony_ci
24954aa6d63Sopenharmony_ci    if (!FileUtils::WriteByteToOutFile(m_endOfCentralDirectory->ToBytes(), output)) {
25054aa6d63Sopenharmony_ci        return false;
25154aa6d63Sopenharmony_ci    }
25254aa6d63Sopenharmony_ci
25354aa6d63Sopenharmony_ci    SIGNATURE_TOOLS_LOGI("Zip To File end");
25454aa6d63Sopenharmony_ci    return true;
25554aa6d63Sopenharmony_ci}
25654aa6d63Sopenharmony_ci
25754aa6d63Sopenharmony_civoid ZipSigner::Alignment(int alignment)
25854aa6d63Sopenharmony_ci{
25954aa6d63Sopenharmony_ci    Sort();
26054aa6d63Sopenharmony_ci    bool isFirstUnRunnableFile = true;
26154aa6d63Sopenharmony_ci    for (const auto& entry : m_zipEntries) {
26254aa6d63Sopenharmony_ci        ZipEntryData* zipEntryData = entry->GetZipEntryData();
26354aa6d63Sopenharmony_ci        short method = zipEntryData->GetZipEntryHeader()->GetMethod();
26454aa6d63Sopenharmony_ci        if (method != FILE_UNCOMPRESS_METHOD_FLAG && !isFirstUnRunnableFile) {
26554aa6d63Sopenharmony_ci            /* only align uncompressed entry and the first compress entry. */
26654aa6d63Sopenharmony_ci            break;
26754aa6d63Sopenharmony_ci        }
26854aa6d63Sopenharmony_ci        int alignBytes;
26954aa6d63Sopenharmony_ci        if (method == FILE_UNCOMPRESS_METHOD_FLAG &&
27054aa6d63Sopenharmony_ci            FileUtils::IsRunnableFile(zipEntryData->GetZipEntryHeader()->GetFileName())) {
27154aa6d63Sopenharmony_ci            /* .abc and .so file align 4096 byte. */
27254aa6d63Sopenharmony_ci            alignBytes = 4096;
27354aa6d63Sopenharmony_ci        } else if (isFirstUnRunnableFile) {
27454aa6d63Sopenharmony_ci            /* the first file after runnable file, align 4096 byte. */
27554aa6d63Sopenharmony_ci            alignBytes = 4096;
27654aa6d63Sopenharmony_ci            isFirstUnRunnableFile = false;
27754aa6d63Sopenharmony_ci        } else {
27854aa6d63Sopenharmony_ci            /* normal file align 4 byte. */
27954aa6d63Sopenharmony_ci            alignBytes = alignment;
28054aa6d63Sopenharmony_ci        }
28154aa6d63Sopenharmony_ci        int add = entry->Alignment(alignBytes);
28254aa6d63Sopenharmony_ci        if (add > 0) {
28354aa6d63Sopenharmony_ci            ResetOffset();
28454aa6d63Sopenharmony_ci        }
28554aa6d63Sopenharmony_ci    }
28654aa6d63Sopenharmony_ci}
28754aa6d63Sopenharmony_ci
28854aa6d63Sopenharmony_civoid ZipSigner::RemoveSignBlock()
28954aa6d63Sopenharmony_ci{
29054aa6d63Sopenharmony_ci    m_signingBlock = std::string();
29154aa6d63Sopenharmony_ci    ResetOffset();
29254aa6d63Sopenharmony_ci}
29354aa6d63Sopenharmony_ci
29454aa6d63Sopenharmony_civoid ZipSigner::Sort()
29554aa6d63Sopenharmony_ci{
29654aa6d63Sopenharmony_ci    /* sort uncompress file (so, abc, an) - other uncompress file - compress file */
29754aa6d63Sopenharmony_ci    std::sort(m_zipEntries.begin(), m_zipEntries.end(), [&](ZipEntry* entry1, ZipEntry* entry2) {
29854aa6d63Sopenharmony_ci        short entry1Method = entry1->GetZipEntryData()->GetZipEntryHeader()->GetMethod();
29954aa6d63Sopenharmony_ci        short entry2Method = entry2->GetZipEntryData()->GetZipEntryHeader()->GetMethod();
30054aa6d63Sopenharmony_ci        std::string entry1FileName = entry1->GetZipEntryData()->GetZipEntryHeader()->GetFileName();
30154aa6d63Sopenharmony_ci        std::string entry2FileName = entry2->GetZipEntryData()->GetZipEntryHeader()->GetFileName();
30254aa6d63Sopenharmony_ci        if (entry1Method == FILE_UNCOMPRESS_METHOD_FLAG && entry2Method == FILE_UNCOMPRESS_METHOD_FLAG) {
30354aa6d63Sopenharmony_ci            bool isRunnableFile1 = FileUtils::IsRunnableFile(entry1FileName);
30454aa6d63Sopenharmony_ci            bool isRunnableFile2 = FileUtils::IsRunnableFile(entry2FileName);
30554aa6d63Sopenharmony_ci            if (isRunnableFile1 && isRunnableFile2) {
30654aa6d63Sopenharmony_ci                return entry1FileName < entry2FileName;
30754aa6d63Sopenharmony_ci            } else if (isRunnableFile1) {
30854aa6d63Sopenharmony_ci                return true;
30954aa6d63Sopenharmony_ci            } else if (isRunnableFile2) {
31054aa6d63Sopenharmony_ci                return false;
31154aa6d63Sopenharmony_ci            }
31254aa6d63Sopenharmony_ci        } else if (entry1Method == FILE_UNCOMPRESS_METHOD_FLAG) {
31354aa6d63Sopenharmony_ci            return true;
31454aa6d63Sopenharmony_ci        } else if (entry2Method == FILE_UNCOMPRESS_METHOD_FLAG) {
31554aa6d63Sopenharmony_ci            return false;
31654aa6d63Sopenharmony_ci        }
31754aa6d63Sopenharmony_ci        return entry1FileName < entry2FileName;
31854aa6d63Sopenharmony_ci    });
31954aa6d63Sopenharmony_ci    ResetOffset();
32054aa6d63Sopenharmony_ci}
32154aa6d63Sopenharmony_ci
32254aa6d63Sopenharmony_civoid ZipSigner::ResetOffset()
32354aa6d63Sopenharmony_ci{
32454aa6d63Sopenharmony_ci    uint32_t offset = 0U;
32554aa6d63Sopenharmony_ci    uint32_t cdLength = 0U;
32654aa6d63Sopenharmony_ci    for (const auto& entry : m_zipEntries) {
32754aa6d63Sopenharmony_ci        entry->GetCentralDirectory()->SetOffset(offset);
32854aa6d63Sopenharmony_ci        offset += entry->GetZipEntryData()->GetLength();
32954aa6d63Sopenharmony_ci        cdLength += entry->GetCentralDirectory()->GetLength();
33054aa6d63Sopenharmony_ci    }
33154aa6d63Sopenharmony_ci    if (!m_signingBlock.empty()) {
33254aa6d63Sopenharmony_ci        offset += m_signingBlock.size();
33354aa6d63Sopenharmony_ci    }
33454aa6d63Sopenharmony_ci    m_cDOffset = offset;
33554aa6d63Sopenharmony_ci    m_endOfCentralDirectory->SetOffset(offset);
33654aa6d63Sopenharmony_ci    m_endOfCentralDirectory->SetcDSize(cdLength);
33754aa6d63Sopenharmony_ci    offset += cdLength;
33854aa6d63Sopenharmony_ci    m_eOCDOffset = offset;
33954aa6d63Sopenharmony_ci}
34054aa6d63Sopenharmony_ci
34154aa6d63Sopenharmony_cistd::vector<ZipEntry*>& ZipSigner::GetZipEntries()
34254aa6d63Sopenharmony_ci{
34354aa6d63Sopenharmony_ci    return m_zipEntries;
34454aa6d63Sopenharmony_ci}
34554aa6d63Sopenharmony_ci
34654aa6d63Sopenharmony_civoid ZipSigner::SetZipEntries(const std::vector<ZipEntry*>& zipEntries)
34754aa6d63Sopenharmony_ci{
34854aa6d63Sopenharmony_ci    m_zipEntries = zipEntries;
34954aa6d63Sopenharmony_ci}
35054aa6d63Sopenharmony_ci
35154aa6d63Sopenharmony_ciuint32_t ZipSigner::GetSigningOffset()
35254aa6d63Sopenharmony_ci{
35354aa6d63Sopenharmony_ci    return m_signingOffset;
35454aa6d63Sopenharmony_ci}
35554aa6d63Sopenharmony_ci
35654aa6d63Sopenharmony_civoid ZipSigner::SetSigningOffset(uint32_t signingOffset)
35754aa6d63Sopenharmony_ci{
35854aa6d63Sopenharmony_ci    m_signingOffset = signingOffset;
35954aa6d63Sopenharmony_ci}
36054aa6d63Sopenharmony_ci
36154aa6d63Sopenharmony_cistd::string ZipSigner::GetSigningBlock()
36254aa6d63Sopenharmony_ci{
36354aa6d63Sopenharmony_ci    return m_signingBlock;
36454aa6d63Sopenharmony_ci}
36554aa6d63Sopenharmony_ci
36654aa6d63Sopenharmony_civoid ZipSigner::SetSigningBlock(const std::string& signingBlock)
36754aa6d63Sopenharmony_ci{
36854aa6d63Sopenharmony_ci    m_signingBlock = signingBlock;
36954aa6d63Sopenharmony_ci}
37054aa6d63Sopenharmony_ci
37154aa6d63Sopenharmony_ciuint32_t ZipSigner::GetCDOffset()
37254aa6d63Sopenharmony_ci{
37354aa6d63Sopenharmony_ci    return m_cDOffset;
37454aa6d63Sopenharmony_ci}
37554aa6d63Sopenharmony_ci
37654aa6d63Sopenharmony_civoid ZipSigner::SetCDOffset(uint32_t cDOffset)
37754aa6d63Sopenharmony_ci{
37854aa6d63Sopenharmony_ci    m_cDOffset = cDOffset;
37954aa6d63Sopenharmony_ci}
38054aa6d63Sopenharmony_ci
38154aa6d63Sopenharmony_ciuint32_t ZipSigner::GetEOCDOffset()
38254aa6d63Sopenharmony_ci{
38354aa6d63Sopenharmony_ci    return m_eOCDOffset;
38454aa6d63Sopenharmony_ci}
38554aa6d63Sopenharmony_ci
38654aa6d63Sopenharmony_civoid ZipSigner::SetEOCDOffset(uint32_t eOCDOffset)
38754aa6d63Sopenharmony_ci{
38854aa6d63Sopenharmony_ci    m_eOCDOffset = eOCDOffset;
38954aa6d63Sopenharmony_ci}
39054aa6d63Sopenharmony_ci
39154aa6d63Sopenharmony_ciEndOfCentralDirectory* ZipSigner::GetEndOfCentralDirectory()
39254aa6d63Sopenharmony_ci{
39354aa6d63Sopenharmony_ci    return m_endOfCentralDirectory;
39454aa6d63Sopenharmony_ci}
39554aa6d63Sopenharmony_ci
39654aa6d63Sopenharmony_civoid ZipSigner::SetEndOfCentralDirectory(EndOfCentralDirectory* endOfCentralDirectory)
39754aa6d63Sopenharmony_ci{
39854aa6d63Sopenharmony_ci    m_endOfCentralDirectory = endOfCentralDirectory;
39954aa6d63Sopenharmony_ci}
40054aa6d63Sopenharmony_ci} // namespace SignatureTools
40154aa6d63Sopenharmony_ci} // namespace OHOS