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 UPDATE_PATCH_H 17 #define UPDATE_PATCH_H 18 #include <fstream> 19 #include <iostream> 20 #include "package/pkg_manager.h" 21 #include "openssl/sha.h" 22 23 namespace UpdatePatch { 24 struct PatchParam { 25 uint8_t* oldBuff; 26 size_t oldSize; 27 uint8_t* patch; 28 size_t patchSize; 29 }; 30 31 struct PatchBuffer { 32 uint8_t *buffer; 33 size_t start; 34 size_t length; 35 }; 36 37 using BlockBuffer = Hpackage::PkgBuffer; 38 39 class UpdatePatchWriter { 40 public: 41 UpdatePatchWriter() = default; ~UpdatePatchWriter()42 virtual ~UpdatePatchWriter() {} 43 44 virtual int32_t Init() = 0; 45 virtual int32_t Write(size_t start, const BlockBuffer &buffer, size_t len) = 0; 46 virtual int32_t Finish() = 0; 47 }; 48 49 using UpdatePatchWriterPtr = UpdatePatchWriter *; 50 51 class UpdateApplyPatch { 52 public: 53 using ImageProcessor = std::function<int(size_t start, const BlockBuffer &data, size_t size)>; 54 55 static int32_t ApplyImagePatch(const PatchParam ¶m, const std::vector<uint8_t> &bonusData, 56 ImageProcessor writer, const std::string& expected); 57 static int32_t ApplyImagePatch(const PatchParam ¶m, 58 UpdatePatchWriterPtr writer, const std::vector<uint8_t> &bonusData); 59 60 static int32_t ApplyBlockPatch(const PatchBuffer &patchInfo, 61 const BlockBuffer &oldInfo, UpdatePatchWriterPtr writer); 62 static int32_t ApplyBlockPatch(const PatchBuffer &patchInfo, 63 const BlockBuffer &oldInfo, ImageProcessor writer, const std::string& expected); 64 static int32_t ApplyBlockPatch(const PatchBuffer &patchInfo, 65 const BlockBuffer &oldInfo, std::vector<uint8_t> &newData); 66 static int32_t ApplyBlockPatch(const PatchBuffer &patchInfo, 67 Hpackage::PkgManager::StreamPtr stream, UpdatePatchWriterPtr writer); 68 static int32_t ApplyPatch(const std::string &patchName, const std::string &oldfile, const std::string &newFile); 69 }; 70 71 class FilePatchWriter : public UpdatePatchWriter { 72 public: FilePatchWriter(const std::string & newFileName)73 FilePatchWriter(const std::string &newFileName) : UpdatePatchWriter(), newFileName_(newFileName) {} ~FilePatchWriter()74 ~FilePatchWriter() override {} 75 76 int32_t Init() override; 77 int32_t Write(size_t start, const BlockBuffer &buffer, size_t len) override; 78 int32_t Finish() override; 79 private: 80 bool init_ { false }; 81 std::string newFileName_; 82 std::ofstream stream_; 83 }; 84 85 class ImagePatchWriter : public UpdatePatchWriter { 86 public: ImagePatchWriter(UpdateApplyPatch::ImageProcessor writer,const std::string & expected,const std::string & partitionName)87 ImagePatchWriter(UpdateApplyPatch::ImageProcessor writer, 88 const std::string &expected, const std::string &partitionName) : UpdatePatchWriter(), 89 writer_(writer), expected_(expected), partitionName_(partitionName) {} ~ImagePatchWriter()90 ~ImagePatchWriter() override {} 91 92 int32_t Init() override; 93 int32_t Write(size_t start, const BlockBuffer &buffer, size_t len) override; 94 int32_t Finish() override; 95 private: 96 bool init_ { false }; 97 SHA256_CTX sha256Ctx_ {}; 98 UpdateApplyPatch::ImageProcessor writer_; 99 std::string expected_; 100 std::string partitionName_; 101 }; 102 } // namespace UpdatePatch 103 #endif // UPDATE_PATCH_H 104