1/*
2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef SIGNATRUETOOLS_ZIP_SIGNER_H
17#define SIGNATRUETOOLS_ZIP_SIGNER_H
18
19#include <fstream>
20#include <optional>
21#include <string>
22#include <vector>
23
24#include "endof_central_directory.h"
25#include "signature_tools_log.h"
26#include "zip_entry.h"
27
28namespace OHOS {
29namespace SignatureTools {
30class ZipSigner {
31public:
32    /* file is uncompress file flag */
33    static constexpr int FILE_UNCOMPRESS_METHOD_FLAG = 0;
34
35    /* max comment length */
36    static constexpr int MAX_COMMENT_LENGTH = 65535;
37
38    ZipSigner()
39    {
40        m_endOfCentralDirectory = nullptr;
41    }
42
43    ~ZipSigner()
44    {
45        delete m_endOfCentralDirectory;
46        for (auto& zipEntry : m_zipEntries) {
47            delete zipEntry;
48        }
49    }
50
51    bool Init(std::ifstream& inputFile);
52
53    /**
54     * output zip to zip file
55     *
56     * @param outFile file path
57     */
58    bool ToFile(std::ifstream& input, std::ofstream& output);
59
60    /**
61     * alignment uncompress entry
62     *
63     * @param alignment int alignment
64     */
65    void Alignment(int alignment);
66
67    void RemoveSignBlock();
68
69    std::vector<ZipEntry*>& GetZipEntries();
70
71    void SetZipEntries(const std::vector<ZipEntry*>& zipEntries);
72
73    uint32_t GetSigningOffset();
74
75    void SetSigningOffset(uint32_t signingOffset);
76
77    std::string GetSigningBlock();
78
79    void SetSigningBlock(const std::string& signingBlock);
80
81    uint32_t GetCDOffset();
82
83    void SetCDOffset(uint32_t cDOffset);
84
85    uint32_t GetEOCDOffset();
86
87    void SetEOCDOffset(uint32_t eOCDOffset);
88
89    EndOfCentralDirectory* GetEndOfCentralDirectory();
90
91    void SetEndOfCentralDirectory(EndOfCentralDirectory* endOfCentralDirectory);
92
93private:
94    EndOfCentralDirectory* GetZipEndOfCentralDirectory(std::ifstream& input);
95
96    bool GetZipCentralDirectory(std::ifstream& input);
97
98    std::string GetSigningBlock(std::ifstream& input);
99
100    bool GetZipEntries(std::ifstream& input);
101
102    /* sort uncompress entry in the front. */
103    void Sort();
104
105    void ResetOffset();
106
107    std::vector<ZipEntry*> m_zipEntries;
108
109    uint32_t m_signingOffset = 0;
110
111    std::string m_signingBlock;
112
113    uint32_t m_cDOffset = 0;
114
115    uint32_t m_eOCDOffset = 0;
116
117    EndOfCentralDirectory* m_endOfCentralDirectory;
118};
119} // namespace SignatureTools
120} // namespace OHOS
121#endif // SIGNATRUETOOLS_ZIP_SIGNER_H
122