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 WritePatchData(const std::vector<ControlData> &controlDatas, 69 const BlockBuffer &newInfo, size_t &patchSize); 70 int32_t WriteControlData(const std::vector<ControlData> controlDatas, size_t &patchSize); 71 int32_t WriteDiffData(const std::vector<ControlData> controlDatas, size_t &patchSize); 72 int32_t WriteExtraData(const std::vector<ControlData> controlDatas, size_t &patchSize); 73 74 void ComputeOldScore(const BlockBuffer &newInfo, 75 const BlockBuffer &oldInfo, int64_t &oldScore, int64_t &matchLen); 76 void ComputeLength(const BlockBuffer &newInfo, 77 const BlockBuffer &oldInfo, int64_t &lengthFront, int64_t &lengthBack); 78 79 std::unique_ptr<SuffixArray<int32_t>> suffixArray_ {nullptr}; 80 std::vector<ControlData> controls_ {}; 81 int64_t matchPos_ { 0 }; 82 int64_t currentOffset_ { 0 }; 83 int64_t lastOffset_ { 0 }; 84 int64_t lastScan_ { 0 }; 85 int64_t lastPos_ { 0 }; 86 }; 87 88 class BlocksStreamDiff : public BlocksDiff { 89 public: BlocksStreamDiff(std::fstream & stream,size_t offset)90 BlocksStreamDiff(std::fstream &stream, size_t offset) : BlocksDiff(), stream_(stream), offset_(offset) {} ~BlocksStreamDiff()91 ~BlocksStreamDiff() override {} 92 93 private: 94 std::unique_ptr<DeflateAdapter> CreateBZip2Adapter(size_t patchOffset) override; 95 int32_t WritePatchHeader(int64_t controlSize, 96 int64_t diffDataSize, int64_t newSize, size_t &headerLen) override; 97 std::fstream &stream_; 98 size_t offset_ { 0 }; 99 }; 100 101 class BlocksBufferDiff : public BlocksDiff { 102 public: BlocksBufferDiff(std::vector<uint8_t> & patchData,size_t offset)103 BlocksBufferDiff(std::vector<uint8_t> &patchData, size_t offset) 104 : BlocksDiff(), patchData_(patchData), offset_(offset) {} ~BlocksBufferDiff()105 ~BlocksBufferDiff() override {} 106 107 private: 108 std::unique_ptr<DeflateAdapter> CreateBZip2Adapter(size_t patchOffset) override; 109 int32_t WritePatchHeader(int64_t controlSize, 110 int64_t diffDataSize, int64_t newSize, size_t &headerLen) override; 111 std::vector<uint8_t> &patchData_; 112 size_t offset_ { 0 }; 113 }; 114 } // namespace UpdatePatch 115 #endif // BLOCKS_DIFF_H