• 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 
16 #ifndef BLOCKS_DIFF_H
17 #define BLOCKS_DIFF_H
18 
19 #include <iostream>
20 #include <vector>
21 #include "bzip2_adapter.h"
22 #include "diffpatch.h"
23 #include "pkg_manager.h"
24 #include "securec.h"
25 #include "update_diff.h"
26 
27 namespace UpdatePatch {
28 template<class DataType>
29 class SuffixArray {
30 public:
31     SuffixArray() = default;
~SuffixArray()32     ~SuffixArray() {}
33 
34     void Init(const BlockBuffer &oldInfo);
35     int64_t Search(const BlockBuffer &newInfo,
36         const BlockBuffer &oldInfo, int64_t start, int64_t end, int64_t &pos) const;
37 private:
38     void InitBuckets(const BlockBuffer &oldInfo,
39         std::vector<DataType> &buckets, std::vector<DataType> &suffixArrayTemp);
40     void Split(std::vector<DataType> &suffixArrayTemp, DataType start, DataType len, DataType h);
41     void SplitForLess(std::vector<DataType> &suffixArrayTemp, DataType start, DataType len, DataType h);
42     int64_t MatchLength(const BlockBuffer &oldBuffer, const BlockBuffer &newBuffer) const;
43 
44     std::vector<DataType> suffixArray_;
45 };
46 
47 class BlocksDiff {
48 public:
49     BlocksDiff() = default;
~BlocksDiff()50     virtual ~BlocksDiff() {}
51 
52     static int32_t MakePatch(const std::string &oldFileName,
53         const std::string &newFileName, const std::string &patchFileName);
54     static int32_t MakePatch(const BlockBuffer &newInfo,
55         const BlockBuffer &oldInfo, std::vector<uint8_t> &patchData, size_t offset, size_t &patchSize);
56     static int32_t MakePatch(const BlockBuffer &newInfo,
57         const BlockBuffer &oldInfo, std::fstream &patchFile, size_t &patchSize);
58 
59     int32_t MakePatch(const BlockBuffer &newInfo, const BlockBuffer &oldInfo, size_t &patchSize);
60 
61 private:
62     virtual std::unique_ptr<DeflateAdapter> CreateBZip2Adapter(size_t patchOffset) = 0;
63     virtual int32_t WritePatchHeader(int64_t controlSize,
64         int64_t diffDataSize, int64_t newSize, size_t &headerLen) = 0;
65 
66     int32_t GetCtrlDatas(const BlockBuffer &newInfo,
67         const BlockBuffer &oldInfo, std::vector<ControlData> &controlDatas);
68     int32_t WriteControlData(const std::vector<ControlData> controlDatas, size_t &patchSize);
69     int32_t WriteDiffData(const std::vector<ControlData> controlDatas, size_t &patchSize);
70     int32_t WriteExtraData(const std::vector<ControlData> controlDatas, size_t &patchSize);
71 
72     void ComputeOldScore(const BlockBuffer &newInfo,
73         const BlockBuffer &oldInfo, int64_t &oldScore, int64_t &matchLen);
74     void ComputeLength(const BlockBuffer &newInfo,
75         const BlockBuffer &oldInfo, int64_t &lengthFront, int64_t &lengthBack);
76 
77     std::unique_ptr<SuffixArray<int32_t>> suffixArray_ {nullptr};
78     std::vector<ControlData> controls_ {};
79     int64_t matchPos_ { 0 };
80     int64_t currentOffset_ { 0 };
81     int64_t lastOffset_ { 0 };
82     int64_t lastScan_ { 0 };
83     int64_t lastPos_ { 0 };
84 };
85 
86 class BlocksStreamDiff : public BlocksDiff {
87 public:
BlocksStreamDiff(std::fstream & stream,size_t offset)88     BlocksStreamDiff(std::fstream &stream, size_t offset) : BlocksDiff(), stream_(stream), offset_(offset) {}
~BlocksStreamDiff()89     ~BlocksStreamDiff() override {}
90 
91 private:
92     std::unique_ptr<DeflateAdapter> CreateBZip2Adapter(size_t patchOffset) override;
93     int32_t WritePatchHeader(int64_t controlSize,
94         int64_t diffDataSize, int64_t newSize, size_t &headerLen) override;
95     std::fstream &stream_;
96     size_t offset_ { 0 };
97 };
98 
99 class BlocksBufferDiff : public BlocksDiff {
100 public:
BlocksBufferDiff(std::vector<uint8_t> & patchData,size_t offset)101     BlocksBufferDiff(std::vector<uint8_t> &patchData, size_t offset)
102         : BlocksDiff(), patchData_(patchData), offset_(offset) {}
~BlocksBufferDiff()103     ~BlocksBufferDiff() override {}
104 
105 private:
106     std::unique_ptr<DeflateAdapter> CreateBZip2Adapter(size_t patchOffset) override;
107     int32_t WritePatchHeader(int64_t controlSize,
108         int64_t diffDataSize, int64_t newSize, size_t &headerLen) override;
109     std::vector<uint8_t> &patchData_;
110     size_t offset_ { 0 };
111 };
112 } // namespace UpdatePatch
113 #endif // BLOCKS_DIFF_H