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