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_FILE_H 16 #define PKG_FILE_H 17 18 #include <map> 19 #include "pkg_algorithm.h" 20 #include "pkg_manager.h" 21 #include "pkg_utils.h" 22 23 namespace Hpackage { 24 class PkgFile; 25 class PkgEntry; 26 using PkgEntryPtr = PkgEntry *; 27 using PkgFilePtr = PkgFile *; 28 29 class PkgEntry { 30 public: PkgEntry(PkgFilePtr pkgFile,uint32_t nodeId)31 PkgEntry(PkgFilePtr pkgFile, uint32_t nodeId) : nodeId_(nodeId), pkgFile_(pkgFile) {} 32 ~PkgEntry()33 virtual ~PkgEntry() {} 34 35 virtual int32_t Init(const PkgManager::FileInfoPtr fileInfo, PkgStreamPtr inStream) = 0; 36 37 virtual int32_t EncodeHeader(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) = 0; 38 39 virtual int32_t Pack(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) = 0; 40 41 virtual int32_t DecodeHeader(const PkgBuffer &buffer, size_t headerOffset, size_t dataOffset, 42 size_t &decodeLen) = 0; 43 44 virtual int32_t Unpack(PkgStreamPtr outStream) = 0; 45 GetFileName()46 virtual const std::string GetFileName() const 47 { 48 return fileName_; 49 }; 50 51 virtual const FileInfo *GetFileInfo() const = 0; 52 GetPkgFile()53 PkgFilePtr GetPkgFile() const 54 { 55 return pkgFile_; 56 } 57 GetNodeId()58 uint32_t GetNodeId() const 59 { 60 return nodeId_; 61 } 62 63 protected: 64 int32_t Init(PkgManager::FileInfoPtr localFileInfo, const PkgManager::FileInfoPtr fileInfo, 65 PkgStreamPtr inStream); 66 67 protected: 68 uint32_t nodeId_ {0}; 69 PkgFilePtr pkgFile_ {nullptr}; 70 size_t headerOffset_ {0}; 71 size_t dataOffset_ {0}; 72 std::string fileName_ {}; 73 }; 74 75 class PkgFile { 76 public: 77 enum PkgType { 78 PKG_TYPE_NONE = PKG_PACK_TYPE_NONE, 79 PKG_TYPE_UPGRADE = PKG_PACK_TYPE_UPGRADE, // 升级包 80 PKG_TYPE_ZIP = PKG_PACK_TYPE_ZIP, // zip压缩包 81 PKG_TYPE_LZ4 = PKG_PACK_TYPE_LZ4, // lz4压缩包 82 PKG_TYPE_GZIP = PKG_PACK_TYPE_GZIP, // gzip压缩包 83 PKG_TYPE_MAX 84 }; 85 using VerifyFunction = std::function<int(const PkgManager::PkgInfoPtr info, 86 const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature)>; 87 88 public: PkgFile(PkgManager::PkgManagerPtr manager,PkgStreamPtr stream,PkgType type)89 PkgFile(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgType type) 90 : type_(type), pkgStream_(stream), pkgManager_(manager) {} 91 92 virtual ~PkgFile(); 93 94 virtual int32_t AddEntry(const PkgManager::FileInfoPtr file, const PkgStreamPtr input) = 0; 95 96 virtual int32_t SavePackage(size_t &signOffset) = 0; 97 98 virtual int32_t ExtractFile(const PkgEntryPtr node, const PkgStreamPtr output); 99 100 virtual int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verifier = nullptr) = 0; 101 102 PkgEntryPtr FindPkgEntry(const std::string &fileName); 103 GetPkgStream()104 PkgStreamPtr GetPkgStream() const 105 { 106 return pkgStream_; 107 } 108 GetPkgInfo()109 virtual const PkgInfo *GetPkgInfo() const 110 { 111 return nullptr; 112 } 113 GetPkgType()114 PkgType GetPkgType() const 115 { 116 return type_; 117 } 118 SetPkgStream()119 void SetPkgStream() 120 { 121 pkgStream_ = nullptr; 122 } 123 124 static int32_t ConvertBufferToString(std::string &fileName, const PkgBuffer &buffer); 125 126 static int32_t ConvertStringToBuffer(const std::string &fileName, const PkgBuffer &buffer, size_t &realLen); 127 128 void AddSignData(uint8_t digestMethod, size_t currOffset, size_t &signOffset); 129 130 protected: 131 PkgEntryPtr AddPkgEntry(const std::string& fileName); 132 bool CheckState(std::vector<uint32_t> states, uint32_t state); 133 134 protected: 135 enum { 136 PKG_FILE_STATE_IDLE = 0, 137 PKG_FILE_STATE_WORKING, // 打包数据的状态 138 PKG_FILE_STATE_CLOSE 139 }; 140 141 PkgType type_ {}; 142 PkgStreamPtr pkgStream_ = nullptr; 143 PkgManager::PkgManagerPtr pkgManager_ = nullptr; 144 uint32_t nodeId_ = 0; 145 std::map<uint32_t, PkgEntryPtr> pkgEntryMapId_ {}; 146 std::multimap<std::string, PkgEntryPtr, std::greater<std::string>> pkgEntryMapFileName_ {}; 147 uint32_t state_ = PKG_FILE_STATE_IDLE; 148 }; 149 } // namespace Hpackage 150 #endif 151