• 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 #include <cstdlib>
18 #include <fcntl.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <vector>
23 #include "openssl/sha.h"
24 
25 namespace updatepatch {
WriteDataToFile(const std::string & fileName,const std::vector<uint8_t> & data,size_t dataSize)26 int32_t WriteDataToFile(const std::string &fileName, const std::vector<uint8_t> &data, size_t dataSize)
27 {
28     std::ofstream patchFile(fileName, std::ios::out | std::ios::binary);
29     PATCH_CHECK(patchFile, return -1, "Failed to open %s", fileName.c_str());
30     patchFile.write(reinterpret_cast<const char*>(data.data()), dataSize);
31     patchFile.close();
32     return PATCH_SUCCESS;
33 }
34 
PatchMapFile(const std::string & fileName,MemMapInfo & info)35 int32_t PatchMapFile(const std::string &fileName, MemMapInfo &info)
36 {
37     int32_t file = open(fileName.c_str(), O_RDONLY);
38     PATCH_CHECK(file >= 0, return -1, "Failed to open file %s", fileName.c_str());
39     struct stat st {};
40     int32_t ret = fstat(file, &st);
41     PATCH_CHECK(ret >= 0, close(file); return ret, "Failed to fstat");
42 
43     info.memory = static_cast<uint8_t*>(mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, file, 0));
44     PATCH_CHECK(info.memory != nullptr, close(file); return -1, "Failed to memory map");
45     info.length = st.st_size;
46     close(file);
47     return PATCH_SUCCESS;
48 }
49 
GeneraterBufferHash(const BlockBuffer & buffer)50 std::string GeneraterBufferHash(const BlockBuffer &buffer)
51 {
52     SHA256_CTX sha256Ctx;
53     SHA256_Init(&sha256Ctx);
54     SHA256_Update(&sha256Ctx, buffer.buffer, buffer.length);
55     std::vector<uint8_t> digest(SHA256_DIGEST_LENGTH);
56     SHA256_Final(digest.data(), &sha256Ctx);
57     return ConvertSha256Hex({
58             digest.data(), SHA256_DIGEST_LENGTH
59         });
60 }
61 
ConvertSha256Hex(const BlockBuffer & buffer)62 std::string ConvertSha256Hex(const BlockBuffer &buffer)
63 {
64     const std::string hexChars = "0123456789abcdef";
65     std::string haxSha256 = "";
66     unsigned int c;
67     for (size_t i = 0; i < buffer.length; ++i) {
68         auto d = buffer.buffer[i];
69         c = (d >> SHIFT_RIGHT_FOUR_BITS) & 0xf;     // last 4 bits
70         haxSha256.push_back(hexChars[c]);
71         haxSha256.push_back(hexChars[d & 0xf]);
72     }
73     return haxSha256;
74 }
75 } // namespace updatepatch