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 UPDATER_BLOCKSET_H 16 #define UPDATER_BLOCKSET_H 17 18 #include <string> 19 #include <vector> 20 21 #ifndef SIZE_MAX 22 #define SIZE_MAX (~(size_t)0) 23 #endif 24 25 using BlockPair = std::pair<size_t, size_t>; 26 static constexpr int H_BLOCK_SIZE = 4096; 27 static constexpr int H_CMD_ARGS_LIMIT = 2; 28 static constexpr int H_DIFF_CMD_ARGS_START = 3; 29 static constexpr int H_MOVE_CMD_ARGS_START = 1; 30 static constexpr int H_ZERO_NUMBER = 0; 31 32 33 namespace updater { 34 class Command; 35 36 class BlockSet { 37 public: BlockSet()38 BlockSet() 39 { 40 blockSize_ = 0; 41 } 42 43 explicit BlockSet(std::vector<BlockPair> &&pairs); 44 45 // Insert block to set after parsing from a string type or vector type 46 bool ParserAndInsert(const std::string &blockStr); 47 48 bool ParserAndInsert(const std::vector<std::string> &blockToken); 49 50 // Get a number of ranges 51 size_t CountOfRanges() const; 52 53 // Get total size of blocks 54 size_t TotalBlockSize() const; 55 56 // Get begin iterator of blocks 57 std::vector<BlockPair>::iterator Begin(); 58 59 // Get end iterator of blocks 60 std::vector<BlockPair>::iterator End(); 61 62 // Get const begin iterator of blocks 63 std::vector<BlockPair>::const_iterator CBegin() const; 64 65 // Get const end iterator of blocks 66 std::vector<BlockPair>::const_iterator CEnd() const; 67 68 std::vector<BlockPair>::const_reverse_iterator CrBegin() const; 69 70 std::vector<BlockPair>::const_reverse_iterator CrEnd() const; 71 72 // Get a block by index 73 const BlockPair& operator[] (size_t index) const 74 { 75 return blocks_[index]; 76 } 77 78 static int32_t VerifySha256(const std::vector<uint8_t> &buffer, const size_t size, 79 const std::string &expected); 80 81 static bool IsTwoBlocksOverlap(const BlockSet &source, BlockSet &target); 82 83 static void MoveBlock(std::vector<uint8_t> &target, const BlockSet &locations, 84 const std::vector<uint8_t> &source); 85 86 int32_t LoadTargetBuffer(const Command &cmd, std::vector<uint8_t> &buffer, size_t &blockSize, size_t pos, 87 std::string &srcHash); 88 int32_t WriteZeroToBlock(int fd, bool isErase = true); 89 90 int32_t WriteDiffToBlock(const Command &cmd, std::vector<uint8_t> &sourceBuffer, const size_t srcBlockSize, 91 bool isImgDiff = true); 92 93 // Read data from block 94 size_t ReadDataFromBlock(int fd, std::vector<uint8_t> &buffer); 95 96 // write data to block 97 size_t WriteDataToBlock(int fd, std::vector<uint8_t> &buffer); 98 protected: 99 size_t blockSize_; 100 std::vector<BlockPair> blocks_; 101 private: 102 void PushBack(BlockPair block_pair); 103 void ClearBlocks(); 104 bool CheckReliablePair(BlockPair pair); 105 int32_t LoadSourceBuffer(const Command &cmd, size_t &pos, std::vector<uint8_t> &sourceBuffer, 106 bool &isOverlap, size_t &srcBlockSize); 107 }; 108 } // namespace updater 109 #endif // UPDATER_BLOCKSET_H 110