• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         info.fd = -1;
73         PATCH_LOGE("Failed to memory map");
74         return -1;
75     }
76     info.memory = static_cast<uint8_t*>(mappedData);
77     info.length = static_cast<size_t>(st.st_size);
78     return PATCH_SUCCESS;
79 }
80 
81 std::string GeneraterBufferHash(const BlockBuffer &buffer)
82 {
83     SHA256_CTX sha256Ctx;
84     SHA256_Init(&sha256Ctx);
85     SHA256_Update(&sha256Ctx, buffer.buffer, buffer.length);
86     std::vector<uint8_t> digest(SHA256_DIGEST_LENGTH);
87     SHA256_Final(digest.data(), &sha256Ctx);
88     return ConvertSha256Hex({
89             digest.data(), SHA256_DIGEST_LENGTH
90         });
91 }
92 
93 std::string ConvertSha256Hex(const BlockBuffer &buffer)
94 {
95     const std::string hexChars = "0123456789abcdef";
96     std::string haxSha256 = "";
97     unsigned int c;
98     for (size_t i = 0; i < buffer.length; ++i) {
99         auto d = buffer.buffer[i];
100         c = (d >> SHIFT_RIGHT_FOUR_BITS) & 0xf;     // last 4 bits
101         haxSha256.push_back(hexChars[c]);
102         haxSha256.push_back(hexChars[d & 0xf]);
103     }
104     return haxSha256;
105 }
106 } // namespace UpdatePatch
107