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 {
GetAnonyString(const std::string & value)22 std::string GetAnonyString(const std::string &value)
23 {
24 const int32_t INT32_SHORT_ID_LENGTH = 20;
25 const int32_t INT32_PLAINTEXT_LENGTH = 4;
26 const int32_t INT32_MIN_ID_LENGTH = 3;
27
28 std::string tmpStr("******");
29 size_t strLen = value.length();
30 if (strLen < INT32_MIN_ID_LENGTH) {
31 return tmpStr;
32 }
33
34 std::string res;
35 if (strLen <= INT32_SHORT_ID_LENGTH) {
36 res += value[0];
37 res += tmpStr;
38 res += value[strLen - 1];
39 } else {
40 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
41 res += tmpStr;
42 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
43 }
44
45 return res;
46 }
47
GetAnonyInt32(const int32_t value)48 std::string GetAnonyInt32(const int32_t value)
49 {
50 std::string tempString = std::to_string(value);
51 size_t length = tempString.length();
52 if (length == 0x01) {
53 tempString[0] = '*';
54 return tempString;
55 }
56 for (size_t i = 1; i < length - 1; i++) {
57 tempString[i] = '*';
58 }
59 return tempString;
60 }
61
IsNumberString(const std::string & inputString)62 bool IsNumberString(const std::string &inputString)
63 {
64 LOGI("IsNumberString for DeviceManagerNapi");
65 if (inputString.length() == 0) {
66 LOGE("inputString is Null");
67 return false;
68 }
69 const int32_t MIN_ASCLL_NUM = 48;
70 const int32_t MAX_ASCLL_NUM = 57;
71 for (size_t i = 0; i < inputString.length(); i++) {
72 int num = (int)inputString[i];
73 if (num >= MIN_ASCLL_NUM && num <= MAX_ASCLL_NUM) {
74 continue;
75 } else {
76 return false;
77 }
78 }
79 return true;
80 }
81
IsString(const nlohmann::json & jsonObj,const std::string & key)82 bool IsString(const nlohmann::json &jsonObj, const std::string &key)
83 {
84 bool res = jsonObj.contains(key) && jsonObj[key].is_string() && jsonObj[key].size() <= MAX_MESSAGE_LEN;
85 if (!res) {
86 LOGE("the key %s in jsonObj is invalid.", key.c_str());
87 }
88 return res;
89 }
90
IsInt32(const nlohmann::json & jsonObj,const std::string & key)91 bool IsInt32(const nlohmann::json &jsonObj, const std::string &key)
92 {
93 bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT32_MIN &&
94 jsonObj[key] <= INT32_MAX;
95 if (!res) {
96 LOGE("the key %s in jsonObj is invalid.", key.c_str());
97 }
98 return res;
99 }
100
IsInt64(const nlohmann::json & jsonObj,const std::string & key)101 bool IsInt64(const nlohmann::json &jsonObj, const std::string &key)
102 {
103 bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT64_MIN &&
104 jsonObj[key] <= INT64_MAX;
105 if (!res) {
106 LOGE("the key %s in jsonObj is invalid.", key.c_str());
107 }
108 return res;
109 }
110
IsArray(const nlohmann::json & jsonObj,const std::string & key)111 bool IsArray(const nlohmann::json &jsonObj, const std::string &key)
112 {
113 bool res = jsonObj.contains(key) && jsonObj[key].is_array();
114 if (!res) {
115 LOGE("the key %s in jsonObj is invalid.", key.c_str());
116 }
117 return res;
118 }
119
IsBool(const nlohmann::json & jsonObj,const std::string & key)120 bool IsBool(const nlohmann::json &jsonObj, const std::string &key)
121 {
122 bool res = jsonObj.contains(key) && jsonObj[key].is_boolean();
123 if (!res) {
124 LOGE("the key %s in jsonObj is invalid.", key.c_str());
125 }
126 return res;
127 }
128 } // namespace DistributedHardware
129 } // namespace OHOS