1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci#ifndef PKG_FILE_H
16fb299fa2Sopenharmony_ci#define PKG_FILE_H
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include <map>
19fb299fa2Sopenharmony_ci#include "pkg_algorithm.h"
20fb299fa2Sopenharmony_ci#include "pkg_manager.h"
21fb299fa2Sopenharmony_ci#include "pkg_info_utils.h"
22fb299fa2Sopenharmony_ci#include "pkg_utils.h"
23fb299fa2Sopenharmony_ci
24fb299fa2Sopenharmony_cinamespace Hpackage {
25fb299fa2Sopenharmony_ciclass PkgFileImpl : public PkgFile {
26fb299fa2Sopenharmony_cipublic:
27fb299fa2Sopenharmony_ci    PkgFileImpl(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgType type)
28fb299fa2Sopenharmony_ci        : type_(type), pkgStream_(stream), pkgManager_(manager) {}
29fb299fa2Sopenharmony_ci
30fb299fa2Sopenharmony_ci    virtual ~PkgFileImpl();
31fb299fa2Sopenharmony_ci
32fb299fa2Sopenharmony_ci    int32_t AddEntry(const FileInfoPtr file, const PkgStreamPtr input) override
33fb299fa2Sopenharmony_ci    {
34fb299fa2Sopenharmony_ci        UNUSED(file);
35fb299fa2Sopenharmony_ci        UNUSED(input);
36fb299fa2Sopenharmony_ci        return PKG_SUCCESS;
37fb299fa2Sopenharmony_ci    }
38fb299fa2Sopenharmony_ci
39fb299fa2Sopenharmony_ci    int32_t SavePackage(size_t &signOffset) override
40fb299fa2Sopenharmony_ci    {
41fb299fa2Sopenharmony_ci        UNUSED(signOffset);
42fb299fa2Sopenharmony_ci        return PKG_SUCCESS;
43fb299fa2Sopenharmony_ci    }
44fb299fa2Sopenharmony_ci
45fb299fa2Sopenharmony_ci    int32_t ExtractFile(const PkgEntryPtr node, const PkgStreamPtr output) override;
46fb299fa2Sopenharmony_ci
47fb299fa2Sopenharmony_ci    int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verifier = nullptr) override
48fb299fa2Sopenharmony_ci    {
49fb299fa2Sopenharmony_ci        UNUSED(fileNames);
50fb299fa2Sopenharmony_ci        UNUSED(verifier);
51fb299fa2Sopenharmony_ci        return PKG_SUCCESS;
52fb299fa2Sopenharmony_ci    }
53fb299fa2Sopenharmony_ci
54fb299fa2Sopenharmony_ci    int32_t ParseComponents(std::vector<std::string> &fileNames) override
55fb299fa2Sopenharmony_ci    {
56fb299fa2Sopenharmony_ci        UNUSED(fileNames);
57fb299fa2Sopenharmony_ci        return PKG_SUCCESS;
58fb299fa2Sopenharmony_ci    }
59fb299fa2Sopenharmony_ci
60fb299fa2Sopenharmony_ci    PkgEntryPtr FindPkgEntry(const std::string &fileName) override;
61fb299fa2Sopenharmony_ci
62fb299fa2Sopenharmony_ci    PkgStreamPtr GetPkgStream() const override
63fb299fa2Sopenharmony_ci    {
64fb299fa2Sopenharmony_ci        return pkgStream_;
65fb299fa2Sopenharmony_ci    }
66fb299fa2Sopenharmony_ci
67fb299fa2Sopenharmony_ci    const PkgInfo *GetPkgInfo() const override
68fb299fa2Sopenharmony_ci    {
69fb299fa2Sopenharmony_ci        return nullptr;
70fb299fa2Sopenharmony_ci    }
71fb299fa2Sopenharmony_ci
72fb299fa2Sopenharmony_ci    PkgType GetPkgType() const override
73fb299fa2Sopenharmony_ci    {
74fb299fa2Sopenharmony_ci        return type_;
75fb299fa2Sopenharmony_ci    }
76fb299fa2Sopenharmony_ci
77fb299fa2Sopenharmony_ci    void ClearPkgStream() override
78fb299fa2Sopenharmony_ci    {
79fb299fa2Sopenharmony_ci        pkgStream_ = nullptr;
80fb299fa2Sopenharmony_ci    }
81fb299fa2Sopenharmony_ci
82fb299fa2Sopenharmony_ci    static int32_t ConvertBufferToString(std::string &fileName, const PkgBuffer &buffer);
83fb299fa2Sopenharmony_ci
84fb299fa2Sopenharmony_ci    static int32_t ConvertStringToBuffer(const std::string &fileName, const PkgBuffer &buffer, size_t &realLen);
85fb299fa2Sopenharmony_ci
86fb299fa2Sopenharmony_ci    void AddSignData(uint8_t digestMethod, size_t currOffset, size_t &signOffset);
87fb299fa2Sopenharmony_ci
88fb299fa2Sopenharmony_ciprotected:
89fb299fa2Sopenharmony_ci    PkgEntryPtr AddPkgEntry(const std::string& fileName);
90fb299fa2Sopenharmony_ci    bool CheckState(std::vector<uint32_t> states, uint32_t state);
91fb299fa2Sopenharmony_ci
92fb299fa2Sopenharmony_ciprotected:
93fb299fa2Sopenharmony_ci    enum {
94fb299fa2Sopenharmony_ci        PKG_FILE_STATE_IDLE = 0,
95fb299fa2Sopenharmony_ci        PKG_FILE_STATE_WORKING, // 打包数据的状态
96fb299fa2Sopenharmony_ci        PKG_FILE_STATE_CLOSE
97fb299fa2Sopenharmony_ci    };
98fb299fa2Sopenharmony_ci
99fb299fa2Sopenharmony_ci    PkgType type_ {};
100fb299fa2Sopenharmony_ci    PkgStreamPtr pkgStream_ = nullptr;
101fb299fa2Sopenharmony_ci    PkgManager::PkgManagerPtr pkgManager_ = nullptr;
102fb299fa2Sopenharmony_ci    uint32_t nodeId_ = 0;
103fb299fa2Sopenharmony_ci    std::map<uint32_t, PkgEntryPtr> pkgEntryMapId_ {};
104fb299fa2Sopenharmony_ci    std::multimap<std::string, PkgEntryPtr, std::greater<std::string>> pkgEntryMapFileName_ {};
105fb299fa2Sopenharmony_ci    uint32_t state_ = PKG_FILE_STATE_IDLE;
106fb299fa2Sopenharmony_ci};
107fb299fa2Sopenharmony_ci} // namespace Hpackage
108fb299fa2Sopenharmony_ci#endif
109