• 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_constants.h"
18 #include "dm_log.h"
19 
20 namespace OHOS {
21 namespace DistributedHardware {
22 
23 constexpr uint32_t MAX_MESSAGE_LEN = 40 * 1024 * 1024;
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 %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 %s in jsonObj is invalid.", key.c_str());
100     }
101     return res;
102 }
103 
IsInt64(const nlohmann::json & jsonObj,const std::string & key)104 bool IsInt64(const nlohmann::json &jsonObj, const std::string &key)
105 {
106     bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT64_MIN &&
107         jsonObj[key] <= INT64_MAX;
108     if (!res) {
109         LOGE("the key %s in jsonObj is invalid.", key.c_str());
110     }
111     return res;
112 }
113 
IsArray(const nlohmann::json & jsonObj,const std::string & key)114 bool IsArray(const nlohmann::json &jsonObj, const std::string &key)
115 {
116     bool res = jsonObj.contains(key) && jsonObj[key].is_array();
117     if (!res) {
118         LOGE("the key %s in jsonObj is invalid.", key.c_str());
119     }
120     return res;
121 }
122 
IsBool(const nlohmann::json & jsonObj,const std::string & key)123 bool IsBool(const nlohmann::json &jsonObj, const std::string &key)
124 {
125     bool res = jsonObj.contains(key) && jsonObj[key].is_boolean();
126     if (!res) {
127         LOGE("the key %s in jsonObj is invalid.", key.c_str());
128     }
129     return res;
130 }
131 } // namespace DistributedHardware
132 } // namespace OHOS