1 /*
2 * Copyright (c) 2022 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 "dm_anonymous.h"
17 #include "dm_log.h"
18 #include <sstream>
19 namespace OHOS {
20 namespace DistributedHardware {
21 namespace {
22 constexpr uint32_t MAX_MESSAGE_LEN = 40 * 1024 * 1024;
23 }
24
GetAnonyString(const std::string & value)25 std::string GetAnonyString(const std::string &value)
26 {
27 const int32_t INT32_SHORT_ID_LENGTH = 20;
28 const int32_t INT32_PLAINTEXT_LENGTH = 4;
29 const int32_t INT32_MIN_ID_LENGTH = 3;
30
31 std::string tmpStr("******");
32 size_t strLen = value.length();
33 if (strLen < INT32_MIN_ID_LENGTH) {
34 return tmpStr;
35 }
36
37 std::string res;
38 if (strLen <= INT32_SHORT_ID_LENGTH) {
39 res += value[0];
40 res += tmpStr;
41 res += value[strLen - 1];
42 } else {
43 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
44 res += tmpStr;
45 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
46 }
47
48 return res;
49 }
50
GetAnonyInt32(const int32_t value)51 std::string GetAnonyInt32(const int32_t value)
52 {
53 std::string tempString = std::to_string(value);
54 size_t length = tempString.length();
55 if (length == 0x01) {
56 tempString[0] = '*';
57 return tempString;
58 }
59 for (size_t i = 1; i < length - 1; i++) {
60 tempString[i] = '*';
61 }
62 return tempString;
63 }
64
IsNumberString(const std::string & inputString)65 bool IsNumberString(const std::string &inputString)
66 {
67 LOGI("IsNumberString for DeviceManagerNapi");
68 if (inputString.length() == 0) {
69 LOGE("inputString is Null");
70 return false;
71 }
72 const int32_t MIN_ASCLL_NUM = 48;
73 const int32_t MAX_ASCLL_NUM = 57;
74 for (size_t i = 0; i < inputString.length(); i++) {
75 int num = (int)inputString[i];
76 if (num >= MIN_ASCLL_NUM && num <= MAX_ASCLL_NUM) {
77 continue;
78 } else {
79 return false;
80 }
81 }
82 return true;
83 }
84
IsString(const nlohmann::json & jsonObj,const std::string & key)85 bool IsString(const nlohmann::json &jsonObj, const std::string &key)
86 {
87 bool res = jsonObj.contains(key) && jsonObj[key].is_string() && jsonObj[key].size() <= MAX_MESSAGE_LEN;
88 if (!res) {
89 LOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
90 }
91 return res;
92 }
93
IsInt32(const nlohmann::json & jsonObj,const std::string & key)94 bool IsInt32(const nlohmann::json &jsonObj, const std::string &key)
95 {
96 bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT32_MIN &&
97 jsonObj[key] <= INT32_MAX;
98 if (!res) {
99 LOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
100 }
101 return res;
102 }
103
IsUint32(const nlohmann::json & jsonObj,const std::string & key)104 bool IsUint32(const nlohmann::json &jsonObj, const std::string &key)
105 {
106 bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] >= 0 &&
107 jsonObj[key] <= UINT32_MAX;
108 if (!res) {
109 LOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
110 }
111 return res;
112 }
113
IsInt64(const nlohmann::json & jsonObj,const std::string & key)114 bool IsInt64(const nlohmann::json &jsonObj, const std::string &key)
115 {
116 bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT64_MIN &&
117 jsonObj[key] <= INT64_MAX;
118 if (!res) {
119 LOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
120 }
121 return res;
122 }
123
IsArray(const nlohmann::json & jsonObj,const std::string & key)124 bool IsArray(const nlohmann::json &jsonObj, const std::string &key)
125 {
126 bool res = jsonObj.contains(key) && jsonObj[key].is_array();
127 if (!res) {
128 LOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
129 }
130 return res;
131 }
132
IsBool(const nlohmann::json & jsonObj,const std::string & key)133 bool IsBool(const nlohmann::json &jsonObj, const std::string &key)
134 {
135 bool res = jsonObj.contains(key) && jsonObj[key].is_boolean();
136 if (!res) {
137 LOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
138 }
139 return res;
140 }
141
ConvertMapToJsonString(const std::map<std::string,std::string> & paramMap)142 std::string ConvertMapToJsonString(const std::map<std::string, std::string> ¶mMap)
143 {
144 std::string jsonStr = "";
145 if (!paramMap.empty()) {
146 nlohmann::json jsonObj;
147 for (const auto &it : paramMap) {
148 jsonObj[it.first] = it.second;
149 }
150 jsonStr = jsonObj.dump();
151 }
152 return jsonStr;
153 }
154
ParseMapFromJsonString(const std::string & jsonStr,std::map<std::string,std::string> & paramMap)155 void ParseMapFromJsonString(const std::string &jsonStr, std::map<std::string, std::string> ¶mMap)
156 {
157 if (jsonStr.empty()) {
158 return;
159 }
160 nlohmann::json paramJson = nlohmann::json::parse(jsonStr, nullptr, false);
161 if (paramJson.is_discarded()) {
162 return;
163 }
164 for (auto &element : paramJson.items()) {
165 if (element.value().is_string()) {
166 paramMap.insert(std::pair<std::string, std::string>(element.key(), element.value()));
167 }
168 }
169 }
170
IsInvalidPeerTargetId(const PeerTargetId & targetId)171 bool IsInvalidPeerTargetId(const PeerTargetId &targetId)
172 {
173 return targetId.deviceId.empty() && targetId.brMac.empty() && targetId.bleMac.empty() && targetId.wifiIp.empty();
174 }
175
ConvertCharArray2String(const char * srcData,uint32_t srcLen)176 std::string ConvertCharArray2String(const char *srcData, uint32_t srcLen)
177 {
178 if (srcData == nullptr || srcLen == 0 || srcLen >= MAX_MESSAGE_LEN) {
179 LOGE("Invalid parameter.");
180 return "";
181 }
182 char *dstData = new char[srcLen + 1]();
183 if (memcpy_s(dstData, srcLen + 1, srcData, srcLen) != 0) {
184 LOGE("memcpy_s failed.");
185 delete[] dstData;
186 return "";
187 }
188 std::string temp(dstData);
189 delete[] dstData;
190 return temp;
191 }
192
VersionSplitToInt(const std::string & str,const char split,std::vector<int> & numVec)193 void VersionSplitToInt(const std::string &str, const char split, std::vector<int> &numVec)
194 {
195 std::istringstream iss(str);
196 std::string item = "";
197 while (getline(iss, item, split)) {
198 numVec.push_back(atoi(item.c_str()));
199 }
200 }
201
CompareVecNum(const std::vector<int32_t> & srcVecNum,const std::vector<int32_t> & sinkVecNum)202 bool CompareVecNum(const std::vector<int32_t> &srcVecNum, const std::vector<int32_t> &sinkVecNum)
203 {
204 for (uint32_t index = 0; index < std::min(srcVecNum.size(), sinkVecNum.size()); index++) {
205 if (srcVecNum[index] > sinkVecNum[index]) {
206 return true;
207 } else if (srcVecNum[index] < sinkVecNum[index]) {
208 return false;
209 } else {
210 continue;
211 }
212 }
213 if (srcVecNum.size() > sinkVecNum.size()) {
214 return true;
215 }
216 return false;
217 }
218
StringToInt(const std::string & str,int32_t base)219 int32_t StringToInt(const std::string &str, int32_t base)
220 {
221 if (str.empty()) {
222 LOGE("Str is empty.");
223 return 0;
224 }
225 char *nextPtr = nullptr;
226 long result = strtol(str.c_str(), &nextPtr, base);
227 if (errno == ERANGE || *nextPtr != '\0') {
228 LOGE("parse int error");
229 return 0;
230 }
231 return static_cast<int32_t>(result);
232 }
233 } // namespace DistributedHardware
234 } // namespace OHOS