1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#ifndef UPDATE_PATCH_H 17fb299fa2Sopenharmony_ci#define UPDATE_PATCH_H 18fb299fa2Sopenharmony_ci#include <fstream> 19fb299fa2Sopenharmony_ci#include <iostream> 20fb299fa2Sopenharmony_ci#include "package/pkg_manager.h" 21fb299fa2Sopenharmony_ci#include "openssl/sha.h" 22fb299fa2Sopenharmony_ci 23fb299fa2Sopenharmony_cinamespace UpdatePatch { 24fb299fa2Sopenharmony_cistruct PatchParam { 25fb299fa2Sopenharmony_ci uint8_t* oldBuff; 26fb299fa2Sopenharmony_ci size_t oldSize; 27fb299fa2Sopenharmony_ci uint8_t* patch; 28fb299fa2Sopenharmony_ci size_t patchSize; 29fb299fa2Sopenharmony_ci}; 30fb299fa2Sopenharmony_ci 31fb299fa2Sopenharmony_cistruct PatchBuffer { 32fb299fa2Sopenharmony_ci uint8_t *buffer; 33fb299fa2Sopenharmony_ci size_t start; 34fb299fa2Sopenharmony_ci size_t length; 35fb299fa2Sopenharmony_ci}; 36fb299fa2Sopenharmony_ci 37fb299fa2Sopenharmony_ciusing BlockBuffer = Hpackage::PkgBuffer; 38fb299fa2Sopenharmony_ci 39fb299fa2Sopenharmony_ciclass UpdatePatchWriter { 40fb299fa2Sopenharmony_cipublic: 41fb299fa2Sopenharmony_ci UpdatePatchWriter() = default; 42fb299fa2Sopenharmony_ci virtual ~UpdatePatchWriter() {} 43fb299fa2Sopenharmony_ci 44fb299fa2Sopenharmony_ci virtual int32_t Init() = 0; 45fb299fa2Sopenharmony_ci virtual int32_t Write(size_t start, const BlockBuffer &buffer, size_t len) = 0; 46fb299fa2Sopenharmony_ci virtual int32_t Finish() = 0; 47fb299fa2Sopenharmony_ci}; 48fb299fa2Sopenharmony_ci 49fb299fa2Sopenharmony_ciusing UpdatePatchWriterPtr = UpdatePatchWriter *; 50fb299fa2Sopenharmony_ci 51fb299fa2Sopenharmony_ciclass UpdateApplyPatch { 52fb299fa2Sopenharmony_cipublic: 53fb299fa2Sopenharmony_ci using ImageProcessor = std::function<int(size_t start, const BlockBuffer &data, size_t size)>; 54fb299fa2Sopenharmony_ci 55fb299fa2Sopenharmony_ci static int32_t ApplyImagePatch(const PatchParam ¶m, const std::vector<uint8_t> &bonusData, 56fb299fa2Sopenharmony_ci ImageProcessor writer, const std::string& expected); 57fb299fa2Sopenharmony_ci static int32_t ApplyImagePatch(const PatchParam ¶m, 58fb299fa2Sopenharmony_ci UpdatePatchWriterPtr writer, const std::vector<uint8_t> &bonusData); 59fb299fa2Sopenharmony_ci static bool PreCheck(const PatchParam ¶m, const UpdatePatchWriterPtr writer); 60fb299fa2Sopenharmony_ci static int32_t ApplyBlockPatch(const PatchBuffer &patchInfo, 61fb299fa2Sopenharmony_ci const BlockBuffer &oldInfo, UpdatePatchWriterPtr writer); 62fb299fa2Sopenharmony_ci static int32_t ApplyBlockPatch(const PatchBuffer &patchInfo, 63fb299fa2Sopenharmony_ci const BlockBuffer &oldInfo, ImageProcessor writer, const std::string& expected); 64fb299fa2Sopenharmony_ci static int32_t ApplyBlockPatch(const PatchBuffer &patchInfo, 65fb299fa2Sopenharmony_ci const BlockBuffer &oldInfo, std::vector<uint8_t> &newData); 66fb299fa2Sopenharmony_ci static int32_t ApplyBlockPatch(const PatchBuffer &patchInfo, 67fb299fa2Sopenharmony_ci Hpackage::PkgManager::StreamPtr stream, UpdatePatchWriterPtr writer); 68fb299fa2Sopenharmony_ci static int32_t ApplyPatch(const std::string &patchName, const std::string &oldfile, const std::string &newFile); 69fb299fa2Sopenharmony_ci}; 70fb299fa2Sopenharmony_ci 71fb299fa2Sopenharmony_ciclass FilePatchWriter : public UpdatePatchWriter { 72fb299fa2Sopenharmony_cipublic: 73fb299fa2Sopenharmony_ci FilePatchWriter(const std::string &newFileName) : UpdatePatchWriter(), newFileName_(newFileName) {} 74fb299fa2Sopenharmony_ci ~FilePatchWriter() override {} 75fb299fa2Sopenharmony_ci 76fb299fa2Sopenharmony_ci int32_t Init() override; 77fb299fa2Sopenharmony_ci int32_t Write(size_t start, const BlockBuffer &buffer, size_t len) override; 78fb299fa2Sopenharmony_ci int32_t Finish() override; 79fb299fa2Sopenharmony_ciprivate: 80fb299fa2Sopenharmony_ci bool init_ { false }; 81fb299fa2Sopenharmony_ci std::string newFileName_; 82fb299fa2Sopenharmony_ci std::ofstream stream_; 83fb299fa2Sopenharmony_ci}; 84fb299fa2Sopenharmony_ci 85fb299fa2Sopenharmony_ciclass ImagePatchWriter : public UpdatePatchWriter { 86fb299fa2Sopenharmony_cipublic: 87fb299fa2Sopenharmony_ci ImagePatchWriter(UpdateApplyPatch::ImageProcessor writer, 88fb299fa2Sopenharmony_ci const std::string &expected, const std::string &partitionName) : UpdatePatchWriter(), 89fb299fa2Sopenharmony_ci writer_(writer), expected_(expected), partitionName_(partitionName) {} 90fb299fa2Sopenharmony_ci ~ImagePatchWriter() override {} 91fb299fa2Sopenharmony_ci 92fb299fa2Sopenharmony_ci int32_t Init() override; 93fb299fa2Sopenharmony_ci int32_t Write(size_t start, const BlockBuffer &buffer, size_t len) override; 94fb299fa2Sopenharmony_ci int32_t Finish() override; 95fb299fa2Sopenharmony_ciprivate: 96fb299fa2Sopenharmony_ci bool init_ { false }; 97fb299fa2Sopenharmony_ci SHA256_CTX sha256Ctx_ {}; 98fb299fa2Sopenharmony_ci UpdateApplyPatch::ImageProcessor writer_; 99fb299fa2Sopenharmony_ci std::string expected_; 100fb299fa2Sopenharmony_ci std::string partitionName_; 101fb299fa2Sopenharmony_ci}; 102fb299fa2Sopenharmony_ci} // namespace UpdatePatch 103fb299fa2Sopenharmony_ci#endif // UPDATE_PATCH_H 104