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