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_MANAGER_IMPL_H
16fb299fa2Sopenharmony_ci#define PKG_MANAGER_IMPL_H
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include <functional>
19fb299fa2Sopenharmony_ci#include <map>
20fb299fa2Sopenharmony_ci#include <memory>
21fb299fa2Sopenharmony_ci#include <mutex>
22fb299fa2Sopenharmony_ci#include "pkg_lz4file.h"
23fb299fa2Sopenharmony_ci#include "pkg_manager.h"
24fb299fa2Sopenharmony_ci#include "pkg_pkgfile.h"
25fb299fa2Sopenharmony_ci#include "pkg_stream.h"
26fb299fa2Sopenharmony_ci#include "pkg_utils.h"
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_cinamespace Hpackage {
29fb299fa2Sopenharmony_ciclass PkgManagerImpl : public PkgManager {
30fb299fa2Sopenharmony_cipublic:
31fb299fa2Sopenharmony_ci    PkgManagerImpl();
32fb299fa2Sopenharmony_ci
33fb299fa2Sopenharmony_ci    ~PkgManagerImpl() override;
34fb299fa2Sopenharmony_ci
35fb299fa2Sopenharmony_ci    void RegisterPkgFileCreator(const std::string &fileType, PkgFileConstructor constructor) override;
36fb299fa2Sopenharmony_ci
37fb299fa2Sopenharmony_ci    int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
38fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ZipFileInfo>> &files) override;
39fb299fa2Sopenharmony_ci
40fb299fa2Sopenharmony_ci    int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
41fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ComponentInfo>> &files) override;
42fb299fa2Sopenharmony_ci
43fb299fa2Sopenharmony_ci    int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
44fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, Lz4FileInfo>> &files) override;
45fb299fa2Sopenharmony_ci
46fb299fa2Sopenharmony_ci    int32_t VerifyPackage(const std::string &packagePath, const std::string &keyPath, const std::string &version,
47fb299fa2Sopenharmony_ci        const PkgBuffer &digest, VerifyCallback cb) override;
48fb299fa2Sopenharmony_ci
49fb299fa2Sopenharmony_ci    int32_t LoadPackage(const std::string &packagePath, const std::string &keyPath,
50fb299fa2Sopenharmony_ci        std::vector<std::string> &fileIds) override;
51fb299fa2Sopenharmony_ci
52fb299fa2Sopenharmony_ci    int32_t ExtractFile(const std::string &path, PkgManager::StreamPtr output) override;
53fb299fa2Sopenharmony_ci
54fb299fa2Sopenharmony_ci    const FileInfo *GetFileInfo(const std::string &path) override;
55fb299fa2Sopenharmony_ci
56fb299fa2Sopenharmony_ci    const PkgInfo *GetPackageInfo(const std::string &packagePath) override;
57fb299fa2Sopenharmony_ci
58fb299fa2Sopenharmony_ci    PkgEntryPtr GetPkgEntry(const std::string &path);
59fb299fa2Sopenharmony_ci
60fb299fa2Sopenharmony_ci    int32_t DecompressBuffer(FileInfoPtr info, const PkgBuffer &buffer, StreamPtr stream) const override;
61fb299fa2Sopenharmony_ci
62fb299fa2Sopenharmony_ci    int32_t CompressBuffer(FileInfoPtr info, const PkgBuffer &buffer, StreamPtr stream) const override;
63fb299fa2Sopenharmony_ci
64fb299fa2Sopenharmony_ci    int32_t LoadPackageWithoutUnPack(const std::string &packagePath, std::vector<std::string> &fileIds) override;
65fb299fa2Sopenharmony_ci
66fb299fa2Sopenharmony_ci    int32_t LoadPackageWithStream(const std::string &packagePath, const std::string &keyPath,
67fb299fa2Sopenharmony_ci        std::vector<std::string> &fileIds, uint8_t type, StreamPtr stream) override;
68fb299fa2Sopenharmony_ci
69fb299fa2Sopenharmony_ci    int32_t ParsePackage(StreamPtr stream, std::vector<std::string> &fileIds, int32_t type) override;
70fb299fa2Sopenharmony_ci
71fb299fa2Sopenharmony_ci    int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName, const PkgBuffer &buffer) override;
72fb299fa2Sopenharmony_ci
73fb299fa2Sopenharmony_ci    int32_t CreatePkgStream(PkgStreamPtr &stream, const std::string &fileName,
74fb299fa2Sopenharmony_ci        PkgStream::ExtractFileProcessor processor, const void *context) override;
75fb299fa2Sopenharmony_ci
76fb299fa2Sopenharmony_ci    int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName,
77fb299fa2Sopenharmony_ci        uint64_t fileLen, Updater::RingBuffer *buffer) override;
78fb299fa2Sopenharmony_ci    int32_t VerifyAccPackage(const std::string &packagePath, const std::string &keyPath) override;
79fb299fa2Sopenharmony_ci    int32_t VerifyOtaPackage(const std::string &packagePath) override;
80fb299fa2Sopenharmony_ci    int32_t VerifyOtaPackage(const std::string &devPath, uint64_t offset, size_t size) override;
81fb299fa2Sopenharmony_ci    int32_t VerifyBinFile(const std::string &packagePath, const std::string &keyPath,
82fb299fa2Sopenharmony_ci        const std::string &version, const PkgBuffer &digest) override;
83fb299fa2Sopenharmony_ci
84fb299fa2Sopenharmony_ci    void SetPkgDecodeProgress(PkgDecodeProgress decodeProgress) override
85fb299fa2Sopenharmony_ci    {
86fb299fa2Sopenharmony_ci        decodeProgress_ = decodeProgress;
87fb299fa2Sopenharmony_ci    }
88fb299fa2Sopenharmony_ci
89fb299fa2Sopenharmony_ci    void PostDecodeProgress(int type, size_t writeDataLen, const void *context) override;
90fb299fa2Sopenharmony_ci
91fb299fa2Sopenharmony_ci    PkgManager::StreamPtr GetPkgFileStream(const std::string &fileName) override;
92fb299fa2Sopenharmony_ci
93fb299fa2Sopenharmony_ci    int32_t CreatePkgStream(PkgStreamPtr &stream, const std::string &fileName, size_t size, int32_t type) override;
94fb299fa2Sopenharmony_ci
95fb299fa2Sopenharmony_ci    void ClosePkgStream(PkgStreamPtr &stream) override;
96fb299fa2Sopenharmony_ci
97fb299fa2Sopenharmony_ci    int32_t ParseComponents(const std::string &packagePath, std::vector<std::string> &fileName) override;
98fb299fa2Sopenharmony_ci
99fb299fa2Sopenharmony_ci    int32_t LoadPackage(const std::string &packagePath,
100fb299fa2Sopenharmony_ci        std::vector<std::string> &fileIds, PkgFile::PkgType type) override;
101fb299fa2Sopenharmony_ciprivate:
102fb299fa2Sopenharmony_ci    PkgFilePtr CreatePackage(PkgStreamPtr stream, PkgFile::PkgType type, PkgInfoPtr header = nullptr);
103fb299fa2Sopenharmony_ci
104fb299fa2Sopenharmony_ci    template<class T>
105fb299fa2Sopenharmony_ci    PkgFilePtr CreatePackage(const std::string &path,
106fb299fa2Sopenharmony_ci        PkgInfoPtr header, std::vector<std::pair<std::string, T>> &files, size_t &offset);
107fb299fa2Sopenharmony_ci
108fb299fa2Sopenharmony_ci    int32_t ExtraAndLoadPackage(const std::string &path, const std::string &name, PkgFile::PkgType,
109fb299fa2Sopenharmony_ci        std::vector<std::string> &fileIds);
110fb299fa2Sopenharmony_ci
111fb299fa2Sopenharmony_ci    int32_t LoadPackageWithStream(const std::string &packagePath,
112fb299fa2Sopenharmony_ci        std::vector<std::string> &fileIds, PkgFile::PkgType type, PkgStreamPtr stream);
113fb299fa2Sopenharmony_ci
114fb299fa2Sopenharmony_ci    std::string GetPkgName(const std::string &path);
115fb299fa2Sopenharmony_ci
116fb299fa2Sopenharmony_ci    PkgFile::PkgType GetPkgTypeByName(const std::string &path);
117fb299fa2Sopenharmony_ci
118fb299fa2Sopenharmony_ci    int32_t Sign(PkgStreamPtr stream, size_t offset, const PkgInfoPtr &info);
119fb299fa2Sopenharmony_ci
120fb299fa2Sopenharmony_ci    int32_t Verify(uint8_t digestMethod, const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature);
121fb299fa2Sopenharmony_ci
122fb299fa2Sopenharmony_ci    int32_t DoGenerateFileDigest(PkgStreamPtr stream, uint8_t flags, const size_t fileLen, PkgBuffer &buff,
123fb299fa2Sopenharmony_ci        std::pair<DigestAlgorithm::DigestAlgorithmPtr, DigestAlgorithm::DigestAlgorithmPtr> &algorithm);
124fb299fa2Sopenharmony_ci
125fb299fa2Sopenharmony_ci    int32_t GenerateFileDigest(PkgStreamPtr stream,
126fb299fa2Sopenharmony_ci        uint8_t digestMethod, uint8_t flags, std::vector<std::vector<uint8_t>> &digestInfos, size_t hashBufferLen = 0);
127fb299fa2Sopenharmony_ci
128fb299fa2Sopenharmony_ci    void ClearPkgFile();
129fb299fa2Sopenharmony_ci
130fb299fa2Sopenharmony_ci    int32_t SetSignVerifyKeyName(const std::string &keyName);
131fb299fa2Sopenharmony_ci
132fb299fa2Sopenharmony_ci    int32_t DoCreatePkgStream(PkgStreamPtr &stream, const std::string &fileName, int32_t type);
133fb299fa2Sopenharmony_ci
134fb299fa2Sopenharmony_ci    const std::string GetExtraPath(const std::string &path);
135fb299fa2Sopenharmony_ci
136fb299fa2Sopenharmony_ciprivate:
137fb299fa2Sopenharmony_ci    bool unzipToFile_ {false};
138fb299fa2Sopenharmony_ci    std::vector<PkgFilePtr> pkgFiles_ {};
139fb299fa2Sopenharmony_ci    std::map<std::string, PkgFileConstructor> pkgFileCreator_ {};
140fb299fa2Sopenharmony_ci    std::mutex mapLock_ {};
141fb299fa2Sopenharmony_ci    std::map<std::string, PkgStreamPtr> pkgStreams_ {};
142fb299fa2Sopenharmony_ci    std::string signVerifyKeyName_ {};
143fb299fa2Sopenharmony_ci    PkgDecodeProgress decodeProgress_ { nullptr };
144fb299fa2Sopenharmony_ci};
145fb299fa2Sopenharmony_ci} // namespace Hpackage
146fb299fa2Sopenharmony_ci#endif // PKG_MANAGER_IMPL_H
147