• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 namespace OHOS {
20 namespace DistributedHardware {
21 
22 constexpr uint32_t MAX_MESSAGE_LEN = 40 * 1024 * 1024;
23 
GetAnonyString(const std::string & value)24 std::string GetAnonyString(const std::string &value)
25 {
26     const int32_t INT32_SHORT_ID_LENGTH = 20;
27     const int32_t INT32_PLAINTEXT_LENGTH = 4;
28     const int32_t INT32_MIN_ID_LENGTH = 3;
29 
30     std::string tmpStr("******");
31     size_t strLen = value.length();
32     if (strLen < INT32_MIN_ID_LENGTH) {
33         return tmpStr;
34     }
35 
36     std::string res;
37     if (strLen <= INT32_SHORT_ID_LENGTH) {
38         res += value[0];
39         res += tmpStr;
40         res += value[strLen - 1];
41     } else {
42         res.append(value, 0, INT32_PLAINTEXT_LENGTH);
43         res += tmpStr;
44         res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
45     }
46 
47     return res;
48 }
49 
GetAnonyInt32(const int32_t value)50 std::string GetAnonyInt32(const int32_t value)
51 {
52     std::string tempString = std::to_string(value);
53     size_t length = tempString.length();
54     if (length == 0x01) {
55         tempString[0] = '*';
56         return tempString;
57     }
58     for (size_t i = 1; i < length - 1; i++) {
59         tempString[i] = '*';
60     }
61     return tempString;
62 }
63 
IsNumberString(const std::string & inputString)64 bool IsNumberString(const std::string &inputString)
65 {
66     LOGI("IsNumberString for DeviceManagerNapi");
67     if (inputString.length() == 0) {
68         LOGE("inputString is Null");
69         return false;
70     }
71     const int32_t MIN_ASCLL_NUM = 48;
72     const int32_t MAX_ASCLL_NUM = 57;
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 %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 %s in jsonObj is invalid.", key.c_str());
99     }
100     return res;
101 }
102 
IsInt64(const nlohmann::json & jsonObj,const std::string & key)103 bool IsInt64(const nlohmann::json &jsonObj, const std::string &key)
104 {
105     bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT64_MIN &&
106         jsonObj[key] <= INT64_MAX;
107     if (!res) {
108         LOGE("the key %s in jsonObj is invalid.", key.c_str());
109     }
110     return res;
111 }
112 
IsArray(const nlohmann::json & jsonObj,const std::string & key)113 bool IsArray(const nlohmann::json &jsonObj, const std::string &key)
114 {
115     bool res = jsonObj.contains(key) && jsonObj[key].is_array();
116     if (!res) {
117         LOGE("the key %s in jsonObj is invalid.", key.c_str());
118     }
119     return res;
120 }
121 
IsBool(const nlohmann::json & jsonObj,const std::string & key)122 bool IsBool(const nlohmann::json &jsonObj, const std::string &key)
123 {
124     bool res = jsonObj.contains(key) && jsonObj[key].is_boolean();
125     if (!res) {
126         LOGE("the key %s in jsonObj is invalid.", key.c_str());
127     }
128     return res;
129 }
130 
ConvertMapToJsonString(const std::map<std::string,std::string> & paramMap)131 std::string ConvertMapToJsonString(const std::map<std::string, std::string> &paramMap)
132 {
133     std::string jsonStr = "";
134     if (!paramMap.empty()) {
135         nlohmann::json jsonObj;
136         for (const auto &it : paramMap) {
137             jsonObj[it.first] = it.second;
138         }
139         jsonStr = jsonObj.dump();
140     }
141     return jsonStr;
142 }
143 
ParseMapFromJsonString(const std::string & jsonStr,std::map<std::string,std::string> & paramMap)144 void ParseMapFromJsonString(const std::string &jsonStr, std::map<std::string, std::string> &paramMap)
145 {
146     if (jsonStr.empty()) {
147         return;
148     }
149     nlohmann::json paramJson = nlohmann::json::parse(jsonStr, nullptr, false);
150     if (paramJson.is_discarded()) {
151         return;
152     }
153     for (auto &element : paramJson.items()) {
154         paramMap.insert(std::pair<std::string, std::string>(element.key(), element.value()));
155     }
156 }
157 
IsInvalidPeerTargetId(const PeerTargetId & targetId)158 bool IsInvalidPeerTargetId(const PeerTargetId &targetId)
159 {
160     return targetId.deviceId.empty() && targetId.brMac.empty() && targetId.bleMac.empty() && targetId.wifiIp.empty();
161 }
162 } // namespace DistributedHardware
163 } // namespace OHOS