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#include "diffpatch.h" 17fb299fa2Sopenharmony_ci#ifndef __WIN32 18fb299fa2Sopenharmony_ci#include <climits> 19fb299fa2Sopenharmony_ci#include <sys/mman.h> 20fb299fa2Sopenharmony_ci#endif 21fb299fa2Sopenharmony_ci#include <cstdlib> 22fb299fa2Sopenharmony_ci#include <fcntl.h> 23fb299fa2Sopenharmony_ci#include <sys/stat.h> 24fb299fa2Sopenharmony_ci#include <unistd.h> 25fb299fa2Sopenharmony_ci#include <vector> 26fb299fa2Sopenharmony_ci#include "openssl/sha.h" 27fb299fa2Sopenharmony_ci#include "pkg_utils.h" 28fb299fa2Sopenharmony_ci 29fb299fa2Sopenharmony_cinamespace UpdatePatch { 30fb299fa2Sopenharmony_ciint32_t WriteDataToFile(const std::string &fileName, const std::vector<uint8_t> &data, size_t dataSize) 31fb299fa2Sopenharmony_ci{ 32fb299fa2Sopenharmony_ci std::ofstream patchFile(fileName, std::ios::out | std::ios::binary); 33fb299fa2Sopenharmony_ci if (!patchFile) { 34fb299fa2Sopenharmony_ci PATCH_LOGE("Failed to open %s", fileName.c_str()); 35fb299fa2Sopenharmony_ci return -1; 36fb299fa2Sopenharmony_ci } 37fb299fa2Sopenharmony_ci patchFile.write(reinterpret_cast<const char*>(data.data()), dataSize); 38fb299fa2Sopenharmony_ci patchFile.close(); 39fb299fa2Sopenharmony_ci return PATCH_SUCCESS; 40fb299fa2Sopenharmony_ci} 41fb299fa2Sopenharmony_ci 42fb299fa2Sopenharmony_ciint32_t PatchMapFile(const std::string &fileName, MemMapInfo &info) 43fb299fa2Sopenharmony_ci{ 44fb299fa2Sopenharmony_ci char realPath[PATH_MAX] = { 0 }; 45fb299fa2Sopenharmony_ci#ifdef _WIN32 46fb299fa2Sopenharmony_ci if (_fullpath(realPath, fileName.c_str(), PATH_MAX) == nullptr) { 47fb299fa2Sopenharmony_ci#else 48fb299fa2Sopenharmony_ci if (realpath(fileName.c_str(), realPath) == nullptr) { 49fb299fa2Sopenharmony_ci#endif 50fb299fa2Sopenharmony_ci PATCH_LOGE("Failed to get realpath %s", fileName.c_str()); 51fb299fa2Sopenharmony_ci return -1; 52fb299fa2Sopenharmony_ci } 53fb299fa2Sopenharmony_ci info.fd = open(realPath, O_RDONLY); 54fb299fa2Sopenharmony_ci if (info.fd < 0) { 55fb299fa2Sopenharmony_ci PATCH_LOGE("Failed to open file %s", fileName.c_str()); 56fb299fa2Sopenharmony_ci return -1; 57fb299fa2Sopenharmony_ci } 58fb299fa2Sopenharmony_ci struct stat st {}; 59fb299fa2Sopenharmony_ci int32_t ret = fstat(info.fd, &st); 60fb299fa2Sopenharmony_ci if (ret < 0) { 61fb299fa2Sopenharmony_ci PATCH_LOGE("Failed to fstat"); 62fb299fa2Sopenharmony_ci return ret; 63fb299fa2Sopenharmony_ci } 64fb299fa2Sopenharmony_ci if (S_ISBLK(st.st_mode)) { 65fb299fa2Sopenharmony_ci st.st_size = lseek(info.fd, 0, SEEK_END); 66fb299fa2Sopenharmony_ci lseek(info.fd, 0, SEEK_SET); 67fb299fa2Sopenharmony_ci } 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_ci void *mappedData = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, info.fd, 0); 70fb299fa2Sopenharmony_ci if (mappedData == MAP_FAILED) { 71fb299fa2Sopenharmony_ci close(info.fd); 72fb299fa2Sopenharmony_ci PATCH_LOGE("Failed to memory map"); 73fb299fa2Sopenharmony_ci return -1; 74fb299fa2Sopenharmony_ci } 75fb299fa2Sopenharmony_ci info.memory = static_cast<uint8_t*>(mappedData); 76fb299fa2Sopenharmony_ci info.length = static_cast<size_t>(st.st_size); 77fb299fa2Sopenharmony_ci return PATCH_SUCCESS; 78fb299fa2Sopenharmony_ci} 79fb299fa2Sopenharmony_ci 80fb299fa2Sopenharmony_cistd::string GeneraterBufferHash(const BlockBuffer &buffer) 81fb299fa2Sopenharmony_ci{ 82fb299fa2Sopenharmony_ci SHA256_CTX sha256Ctx; 83fb299fa2Sopenharmony_ci SHA256_Init(&sha256Ctx); 84fb299fa2Sopenharmony_ci SHA256_Update(&sha256Ctx, buffer.buffer, buffer.length); 85fb299fa2Sopenharmony_ci std::vector<uint8_t> digest(SHA256_DIGEST_LENGTH); 86fb299fa2Sopenharmony_ci SHA256_Final(digest.data(), &sha256Ctx); 87fb299fa2Sopenharmony_ci return ConvertSha256Hex({ 88fb299fa2Sopenharmony_ci digest.data(), SHA256_DIGEST_LENGTH 89fb299fa2Sopenharmony_ci }); 90fb299fa2Sopenharmony_ci} 91fb299fa2Sopenharmony_ci 92fb299fa2Sopenharmony_cistd::string ConvertSha256Hex(const BlockBuffer &buffer) 93fb299fa2Sopenharmony_ci{ 94fb299fa2Sopenharmony_ci const std::string hexChars = "0123456789abcdef"; 95fb299fa2Sopenharmony_ci std::string haxSha256 = ""; 96fb299fa2Sopenharmony_ci unsigned int c; 97fb299fa2Sopenharmony_ci for (size_t i = 0; i < buffer.length; ++i) { 98fb299fa2Sopenharmony_ci auto d = buffer.buffer[i]; 99fb299fa2Sopenharmony_ci c = (d >> SHIFT_RIGHT_FOUR_BITS) & 0xf; // last 4 bits 100fb299fa2Sopenharmony_ci haxSha256.push_back(hexChars[c]); 101fb299fa2Sopenharmony_ci haxSha256.push_back(hexChars[d & 0xf]); 102fb299fa2Sopenharmony_ci } 103fb299fa2Sopenharmony_ci return haxSha256; 104fb299fa2Sopenharmony_ci} 105fb299fa2Sopenharmony_ci} // namespace UpdatePatch 106