1 /* 2 * Copyright (c) 2023 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 #ifndef SOFTBUS_SESSION_UTILS_H 17 #define SOFTBUS_SESSION_UTILS_H 18 19 #include <string> 20 #include "cJSON.h" 21 #include "migrate_avsession_constant.h" 22 23 namespace OHOS::AVSession { 24 class SoftbusSessionUtils { 25 public: AnonymizeDeviceId(const std::string & deviceId)26 static std::string AnonymizeDeviceId(const std::string &deviceId) 27 { 28 if (deviceId == "") { 29 return ""; 30 } 31 if (deviceId.length() <= DEVICE_ANONYMIZE_LENGTH) { 32 return std::string(DEVICE_ANONYMIZE_ID); 33 } else { 34 unsigned long index = 35 std::min<unsigned long>(deviceId.length() - DEVICE_ANONYMIZE_LENGTH, DEVICE_ANONYMIZE_LENGTH); 36 std::string prefix = deviceId.substr(0, index); 37 std::string suffix = deviceId.substr(deviceId.length() - index, deviceId.length()); 38 return prefix + std::string(DEVICE_ANONYMIZE_ID) + suffix; 39 } 40 } 41 TransferJsonToStr(cJSON * jsonValue,std::string & jsonStr)42 static inline void TransferJsonToStr(cJSON* jsonValue, std::string& jsonStr) 43 { 44 if (jsonValue == nullptr || cJSON_IsInvalid(jsonValue)) { 45 jsonStr += "ERR_JSON"; 46 return; 47 } 48 char* jsonValueStr = cJSON_Print(jsonValue); 49 if (jsonValueStr != nullptr) { 50 jsonStr.append(jsonValueStr); 51 cJSON_free(jsonValueStr); 52 } 53 } 54 TransferStrToJson(const std::string & jsonStr,cJSON * & jsonValue)55 static inline bool TransferStrToJson(const std::string& jsonStr, cJSON*& jsonValue) 56 { 57 jsonValue = cJSON_Parse(jsonStr.c_str()); 58 if (jsonValue == nullptr) { 59 return false; 60 } 61 if (cJSON_IsInvalid(jsonValue) || cJSON_IsNull(jsonValue)) { 62 cJSON_Delete(jsonValue); 63 return false; 64 } 65 return true; 66 } 67 AddStringToJson(cJSON * item,const char * key,const std::string & value)68 static inline bool AddStringToJson(cJSON* item, const char* key, const std::string& value) 69 { 70 if (item == nullptr || cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 71 return false; 72 } 73 cJSON_AddStringToObject(item, key, value.c_str()); 74 if (cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 75 return false; 76 } 77 return true; 78 } 79 AddIntToJson(cJSON * item,const char * key,int value)80 static inline bool AddIntToJson(cJSON* item, const char* key, int value) 81 { 82 if (item == nullptr || cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 83 return false; 84 } 85 cJSON_AddNumberToObject(item, key, static_cast<long long>(value)); 86 if (cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 87 return false; 88 } 89 return true; 90 } 91 AddBoolToJson(cJSON * item,const char * key,bool value)92 static inline bool AddBoolToJson(cJSON* item, const char* key, bool value) 93 { 94 if (item == nullptr || cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 95 return false; 96 } 97 cJSON_AddBoolToObject(item, key, value); 98 if (cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 99 return false; 100 } 101 return true; 102 } 103 AddJsonArrayToJson(cJSON * & item,const char * key,cJSON * & jsonArray)104 static inline bool AddJsonArrayToJson(cJSON*& item, const char* key, cJSON*& jsonArray) 105 { 106 if (item == nullptr || cJSON_IsInvalid(item) || cJSON_IsNull(item) || 107 jsonArray == nullptr || cJSON_IsInvalid(jsonArray) || !cJSON_IsArray(jsonArray)) { 108 cJSON_Delete(jsonArray); 109 return false; 110 } 111 cJSON_AddItemToObject(item, key, jsonArray); 112 if (cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 113 return false; 114 } 115 return true; 116 } 117 AddJsonToJsonArray(cJSON * & jsonArray,int32_t index,cJSON * & item)118 static inline bool AddJsonToJsonArray(cJSON*& jsonArray, int32_t index, cJSON*& item) 119 { 120 if (jsonArray == nullptr || cJSON_IsInvalid(jsonArray) || !cJSON_IsArray(jsonArray) || 121 item == nullptr || cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 122 cJSON_Delete(item); 123 return false; 124 } 125 cJSON_InsertItemInArray(jsonArray, index, item); 126 if (cJSON_IsInvalid(jsonArray) || cJSON_IsNull(jsonArray)) { 127 return false; 128 } 129 return true; 130 } 131 GetStringFromJson(cJSON * item,const char * key)132 static inline std::string GetStringFromJson(cJSON* item, const char* key) 133 { 134 if (item == nullptr || cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 135 return ""; 136 } 137 cJSON* value = cJSON_GetObjectItem(item, key); 138 if (value == nullptr || cJSON_IsInvalid(value) || !cJSON_IsString(value)) { 139 return ""; 140 } 141 return std::string(value->valuestring); 142 } 143 GetIntFromJson(cJSON * item,const char * key)144 static inline int GetIntFromJson(cJSON* item, const char* key) 145 { 146 if (item == nullptr || cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 147 return -1; 148 } 149 cJSON* value = cJSON_GetObjectItem(item, key); 150 if (value == nullptr || cJSON_IsInvalid(value) || !cJSON_IsNumber(value)) { 151 return -1; 152 } 153 return value->valueint; 154 } 155 GetBoolFromJson(cJSON * item,const char * key)156 static inline bool GetBoolFromJson(cJSON* item, const char* key) 157 { 158 if (item == nullptr || cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 159 return false; 160 } 161 cJSON* value = cJSON_GetObjectItem(item, key); 162 if (value == nullptr || cJSON_IsInvalid(value) || !cJSON_IsBool(value)) { 163 return false; 164 } 165 return cJSON_IsTrue(value); 166 } 167 GetNewCJSONObject()168 static inline cJSON* GetNewCJSONObject() 169 { 170 cJSON* value = cJSON_CreateObject(); 171 if (value == nullptr) { 172 return nullptr; 173 } 174 if (cJSON_IsInvalid(value) || cJSON_IsNull(value)) { 175 cJSON_Delete(value); 176 return nullptr; 177 } 178 return value; 179 } 180 GetNewCJSONArray()181 static inline cJSON* GetNewCJSONArray() 182 { 183 cJSON* valueArray = cJSON_CreateArray(); 184 if (valueArray == nullptr) { 185 return nullptr; 186 } 187 if (cJSON_IsInvalid(valueArray) || !cJSON_IsArray(valueArray)) { 188 cJSON_Delete(valueArray); 189 return nullptr; 190 } 191 return valueArray; 192 } 193 CheckCJSONObjectEmpty(cJSON * item)194 static bool CheckCJSONObjectEmpty(cJSON* item) 195 { 196 if (item == nullptr) { 197 return true; 198 } 199 if (cJSON_IsInvalid(item) || cJSON_IsNull(item)) { 200 cJSON_Delete(item); 201 return true; 202 } 203 if (item->child == nullptr) { 204 cJSON_Delete(item); 205 return true; 206 } 207 return false; 208 } 209 CopyCJSONObject(cJSON * & fromItem,cJSON * & toItem)210 static bool CopyCJSONObject(cJSON*& fromItem, cJSON*& toItem) 211 { 212 if (toItem != nullptr) { 213 cJSON_Delete(toItem); 214 toItem = nullptr; 215 } 216 if (fromItem == nullptr || cJSON_IsInvalid(fromItem) || cJSON_IsNull(fromItem)) { 217 return false; 218 } 219 toItem = cJSON_Duplicate(fromItem, true); 220 return true; 221 } 222 GetEncryptAddr(const std::string & addr)223 static std::string GetEncryptAddr(const std::string& addr) 224 { 225 if (addr.empty() || addr.length() != addressStrLen) { 226 return std::string(""); 227 } 228 std::string tmp = "**:**:**:**:**:**"; 229 std::string out = addr; 230 for (int i = startPos; i <= endPos; i++) { 231 out[i] = tmp[i]; 232 } 233 return out; 234 } 235 AnonymizeMacAddressInSoftBusMsg(const std::string & input)236 static std::string AnonymizeMacAddressInSoftBusMsg(const std::string& input) 237 { 238 std::string output = input; 239 std::string macKey = "\"" + std::string(AUDIO_MAC_ADDRESS) + "\":\""; 240 std::string endKey = "\""; 241 size_t startPos = 0; 242 243 while ((startPos = output.find(macKey, startPos)) != std::string::npos) { 244 startPos += macKey.length(); 245 size_t endPos = output.find(endKey, startPos); 246 if (endPos != std::string::npos) { 247 std::string macAddress = output.substr(startPos, endPos - startPos); 248 std::string encryptedMac = GetEncryptAddr(macAddress); 249 output.replace(startPos, endPos - startPos, encryptedMac); 250 } 251 } 252 return output; 253 } 254 255 private: 256 static constexpr const char *DEVICE_ANONYMIZE_ID = "******"; 257 static constexpr const int DEVICE_ANONYMIZE_LENGTH = 6; 258 259 static constexpr const int addressStrLen = 17; 260 static constexpr const int startPos = 6; 261 static constexpr const int endPos = 13; 262 }; 263 } // namespace OHOS::AVSession 264 265 #endif // SOFTBUS_SESSION_UTILS_H