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 protected: 63 int32_t Init(PkgManager::FileInfoPtr localFileInfo, const PkgManager::FileInfoPtr fileInfo, 64 PkgStreamPtr inStream); 65 66 protected: 67 uint32_t nodeId_ {0}; 68 PkgFilePtr pkgFile_ {nullptr}; 69 size_t headerOffset_ {0}; 70 size_t dataOffset_ {0}; 71 std::string fileName_ {}; 72 }; 73 74 class PkgFile { 75 public: 76 enum PkgType { 77 PKG_TYPE_NONE = PKG_PACK_TYPE_NONE, 78 PKG_TYPE_UPGRADE = PKG_PACK_TYPE_UPGRADE, // 升级包 79 PKG_TYPE_ZIP = PKG_PACK_TYPE_ZIP, // zip压缩包 80 PKG_TYPE_LZ4 = PKG_PACK_TYPE_LZ4, // lz4压缩包 81 PKG_TYPE_GZIP = PKG_PACK_TYPE_GZIP, // gzip压缩包 82 PKG_TYPE_MAX 83 }; 84 using VerifyFunction = std::function<int(const PkgManager::PkgInfoPtr info, 85 const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature)>; 86 87 public: PkgFile(PkgManager::PkgManagerPtr manager,PkgStreamPtr stream,PkgType type)88 PkgFile(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgType type) 89 : type_(type), pkgStream_(stream), pkgManager_(manager) {} 90 91 virtual ~PkgFile(); 92 93 virtual int32_t AddEntry(const PkgManager::FileInfoPtr file, const PkgStreamPtr input) = 0; 94 95 virtual int32_t SavePackage(size_t &signOffset) = 0; 96 97 virtual int32_t ExtractFile(const PkgEntryPtr node, const PkgStreamPtr output); 98 99 virtual int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verifier = nullptr) = 0; 100 101 PkgEntryPtr FindPkgEntry(const std::string &fileName); 102 GetPkgStream()103 PkgStreamPtr GetPkgStream() const 104 { 105 return pkgStream_; 106 } 107 GetPkgInfo()108 virtual const PkgInfo *GetPkgInfo() const 109 { 110 return nullptr; 111 } 112 GetPkgType()113 PkgType GetPkgType() const 114 { 115 return type_; 116 } 117 SetPkgStream()118 void SetPkgStream() 119 { 120 pkgStream_ = nullptr; 121 } 122 123 static int32_t ConvertBufferToString(std::string &fileName, const PkgBuffer &buffer); 124 125 static int32_t ConvertStringToBuffer(const std::string &fileName, const PkgBuffer &buffer, size_t &realLen); 126 127 void AddSignData(uint8_t digestMethod, size_t currOffset, size_t &signOffset); 128 protected: 129 PkgEntryPtr AddPkgEntry(const std::string& fileName); 130 bool CheckState(std::vector<uint32_t> states, uint32_t state); 131 protected: 132 enum { 133 PKG_FILE_STATE_IDLE = 0, 134 PKG_FILE_STATE_WORKING, // 打包数据的状态 135 PKG_FILE_STATE_CLOSE 136 }; 137 138 PkgType type_ {}; 139 PkgStreamPtr pkgStream_ = nullptr; 140 PkgManager::PkgManagerPtr pkgManager_ = nullptr; 141 uint32_t nodeId_ = 0; 142 std::map<uint32_t, PkgEntryPtr> pkgEntryMapId_ {}; 143 std::multimap<std::string, PkgEntryPtr, std::greater<std::string>> pkgEntryMapFileName_ {}; 144 uint32_t state_ = PKG_FILE_STATE_IDLE; 145 }; 146 } // namespace hpackage 147 #endif 148