• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "cj_request_common.h"
17 
18 #include <cstdlib>
19 #include <sstream>
20 #include <fstream>
21 #include "ffrt.h"
22 #include "cj_request_log.h"
23 #include "securec.h"
24 #include "openssl/sha.h"
25 
26 namespace OHOS::CJSystemapi::Request {
27 using OHOS::Request::ExceptionErrorCode;
28 
ReadBytesFromFile(const std::string & filePath,std::vector<uint8_t> & fileData)29 void ReadBytesFromFile(const std::string &filePath, std::vector<uint8_t> &fileData)
30 {
31     // Ensure filePath validity.
32     std::ifstream inputFile(filePath.c_str(), std::ios::binary);
33     if (inputFile.is_open()) {
34         inputFile.seekg(0, std::ios::end);
35         fileData.resize(inputFile.tellg());
36         inputFile.seekg(0);
37         inputFile.read(reinterpret_cast<char *>(fileData.data()), fileData.size());
38         inputFile.close();
39     } else {
40         REQUEST_HILOGW("Read bytes from file, invalid file path!");
41     }
42     return;
43 }
44 
MallocCString(const std::string & origin)45 char* MallocCString(const std::string& origin)
46 {
47     if (origin.empty()) {
48         return nullptr;
49     }
50     auto len = origin.length() + 1;
51     char* res = static_cast<char *>(malloc(sizeof(char) * len));
52     if (res == nullptr) {
53         return nullptr;
54     }
55     return std::char_traits<char>::copy(res, origin.c_str(), len);
56 }
57 
IsPathValid(const std::string & filePath)58 bool IsPathValid(const std::string &filePath)
59 {
60     auto path = filePath.substr(0, filePath.rfind('/'));
61     char resolvedPath[PATH_MAX + 1] = { 0 };
62     if (path.length() > PATH_MAX || realpath(path.c_str(), resolvedPath) == nullptr ||
63         strncmp(resolvedPath, path.c_str(), path.length()) != 0) {
64         REQUEST_HILOGE("invalid file path!");
65         return false;
66     }
67     return true;
68 }
69 
SHA256(const char * str,size_t len)70 std::string SHA256(const char *str, size_t len)
71 {
72     unsigned char hash[SHA256_DIGEST_LENGTH];
73     SHA256_CTX sha256;
74     SHA256_Init(&sha256);
75     SHA256_Update(&sha256, str, len);
76     SHA256_Final(hash, &sha256);
77     std::stringstream ss;
78     for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
79         // 2 means setting hte width of the output.
80         ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
81     }
82     return ss.str();
83 }
84 
ConvertError(int32_t errorCode)85 ExceptionError ConvertError(int32_t errorCode)
86 {
87     ExceptionError err{};
88     auto generateError = [&err](ExceptionErrorCode errorCode, const std::string &info) {
89         err.code = errorCode;
90         err.errInfo = info;
91         REQUEST_HILOGE("errorCode: %{public}d, errInfo: %{public}s", err.code, err.errInfo.c_str());
92     };
93 
94     switch (errorCode) {
95         case ExceptionErrorCode::E_UNLOADING_SA:
96             generateError(ExceptionErrorCode::E_SERVICE_ERROR, "Service ability is quitting.");
97             break;
98         case ExceptionErrorCode::E_IPC_SIZE_TOO_LARGE:
99             generateError(ExceptionErrorCode::E_SERVICE_ERROR, "Ipc error.");
100             break;
101         case ExceptionErrorCode::E_MIMETYPE_NOT_FOUND:
102             generateError(ExceptionErrorCode::E_OTHER, "Mimetype not found.");
103             break;
104         case ExceptionErrorCode::E_TASK_INDEX_TOO_LARGE:
105             generateError(ExceptionErrorCode::E_TASK_NOT_FOUND, "Task index out of range.");
106             break;
107         default:
108             generateError(static_cast<ExceptionErrorCode>(errorCode), "");
109             break;
110     }
111 
112     return err;
113 }
114 
115 
Convert2CProgress(const Progress & in)116 CProgress Convert2CProgress(const Progress &in)
117 {
118     CProgress out = { 0 };
119     out.state = static_cast<uint32_t>(in.state);
120     out.index = in.index;
121     out.processed = in.processed;
122 
123     if (in.sizes.size() > 0) {
124         out.sizeArr = static_cast<int64_t *>(malloc(sizeof(int64_t) * in.sizes.size()));
125         if (out.sizeArr == nullptr) {
126             return out;
127         }
128         for (std::vector<long>::size_type i = 0; i < in.sizes.size(); ++i) {
129             out.sizeArr[i] = in.sizes[i];
130         }
131         out.sizeArrLen = static_cast<int64_t>(in.sizes.size());
132     }
133 
134     if (in.extras.size() <= 0) {
135         return out;
136     }
137 
138     out.extras.headers = static_cast<CHashStrPair *>(malloc(sizeof(CHashStrPair) * in.extras.size()));
139     if (out.extras.headers == nullptr) {
140         return out;
141     }
142 
143     int index = 0;
144     for (auto iter = in.extras.begin(); iter != in.extras.end(); ++iter) {
145         CHashStrPair *elem = &out.extras.headers[index++];
146         elem->key = MallocCString(iter->first);
147         elem->value = MallocCString(iter->second);
148     }
149     out.extras.size = static_cast<int64_t>(in.extras.size());
150     return out;
151 }
152 
RemoveFile(const std::string & filePath)153 void RemoveFile(const std::string &filePath)
154 {
155     auto removeFile = [filePath]() -> void {
156         std::remove(filePath.c_str());
157         return;
158     };
159     ffrt::submit(removeFile, {}, {}, ffrt::task_attr().name("Os_Request_Rm").qos(ffrt::qos_default));
160 }
161 
162 } // namespace OHOS::CJSystemapi::Request
163