• 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 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_NEW_CMD_ARGS_START = 1;
29 static constexpr int H_DIFF_CMD_ARGS_START = 3;
30 static constexpr int H_MOVE_CMD_ARGS_START = 1;
31 static constexpr int H_COPY_CMD_ARGS_START = 1;
32 static constexpr int H_ZERO_NUMBER = 0;
33 
34 
35 namespace Updater {
36 class Command;
37 
38 class BlockSet {
39 public:
BlockSet()40     BlockSet()
41     {
42         blockSize_ = 0;
43     }
44 
45     explicit BlockSet(std::vector<BlockPair> &&pairs);
46 
~BlockSet()47     ~BlockSet() {}
48 
49     // Insert block to set after parsing from a string type or vector type
50     bool ParserAndInsert(const std::string &blockStr);
51 
52     bool ParserAndInsert(const std::vector<std::string> &blockToken);
53 
54     // Get a number of ranges
55     size_t CountOfRanges() const;
56 
57     // Get total size of blocks
58     size_t TotalBlockSize() const;
59 
60     // Get begin iterator of blocks
61     std::vector<BlockPair>::iterator Begin();
62 
63     // Get end iterator of blocks
64     std::vector<BlockPair>::iterator End();
65 
66     // Get const begin iterator of blocks
67     std::vector<BlockPair>::const_iterator CBegin() const;
68 
69     // Get const end iterator of blocks
70     std::vector<BlockPair>::const_iterator CEnd() const;
71 
72     std::vector<BlockPair>::const_reverse_iterator CrBegin() const;
73 
74     std::vector<BlockPair>::const_reverse_iterator CrEnd() const;
75 
76     // Get a block by index
77     const BlockPair& operator[] (size_t index) const
78     {
79         return blocks_[index];
80     }
81 
82     static int32_t VerifySha256(const std::vector<uint8_t> &buffer, const size_t size,
83         const std::string &expected);
84 
85     static bool IsTwoBlocksOverlap(const BlockSet &source, BlockSet &target);
86 
87     static void MoveBlock(std::vector<uint8_t> &target, const BlockSet &locations,
88         const std::vector<uint8_t> &source);
89 
90     int32_t LoadTargetBuffer(const Command &cmd, std::vector<uint8_t> &buffer, size_t &blockSize, size_t pos,
91         std::string &srcHash);
92     int32_t WriteZeroToBlock(int fd, bool isErase = true);
93 
94     int32_t WriteDiffToBlock(const Command &cmd, std::vector<uint8_t> &sourceBuffer, uint8_t *patchBuffer,
95                              size_t patchLength, bool isImgDiff = true);
96 
97     // Read data from block
98     size_t ReadDataFromBlock(int fd, std::vector<uint8_t> &buffer);
99 
100     // write data to block
101     size_t WriteDataToBlock(int fd, std::vector<uint8_t> &buffer);
102 
103 protected:
104     size_t blockSize_;
105     std::vector<BlockPair> blocks_;
106 
107 private:
108     void PushBack(BlockPair block_pair);
109     void ClearBlocks();
110     bool CheckReliablePair(BlockPair pair);
111     int32_t LoadSourceBuffer(const Command &cmd, size_t &pos, std::vector<uint8_t> &sourceBuffer,
112         bool &isOverlap, size_t &srcBlockSize);
113 };
114 
115 #ifdef __cplusplus
116 #if __cplusplus
117 extern "C" {
118 #endif
119 #endif /* __cpluscplus */
120 int32_t BlockVerify(const Command &cmd, std::vector<uint8_t> &buffer,
121     const size_t size, const std::string srcHash, size_t &pos);
122 #ifdef __cplusplus
123 #if __cplusplus
124 }
125 #endif
126 #endif /* __cpluscplus */
127 
128 } // namespace Updater
129 #endif // UPDATER_BLOCKSET_H
130