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