• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 "profile_utils.h"
17 
18 #include <codecvt>
19 #include <locale>
20 #include <regex>
21 #include <unordered_set>
22 
23 #include "dm_device_info.h"
24 #include "rdb_errno.h"
25 
26 #include "content_sensor_manager_utils.h"
27 #include "distributed_device_profile_constants.h"
28 #include "distributed_device_profile_enums.h"
29 #include "distributed_device_profile_errors.h"
30 #include "distributed_device_profile_log.h"
31 
32 namespace OHOS {
33 namespace DistributedDeviceProfile {
34 using namespace OHOS::DistributedHardware;
35 
36 namespace {
37     const std::string TAG = "ProfileUtils";
38     const std::unordered_set<std::string> NEED_ADD_OH_SUFFIX_DEV_PROFILES { OS_TYPE, OS_VERSION };
39     const std::unordered_set<std::string> NEED_ADD_OH_SUFFIX_SVR_NAMES { "DistributeModemService",
40         "multiScreenAssistantService", "appInfo" };
41 }
42 
GetDbKeyAnonyString(const std::string & dbKey)43 std::string ProfileUtils::GetDbKeyAnonyString(const std::string& dbKey)
44 {
45     constexpr size_t DEVICE_ID_INDEX = 1;
46     std::vector<std::string> splitKeys;
47     if (ProfileUtils::SplitString(dbKey, SEPARATOR, splitKeys) != DP_SUCCESS ||
48         splitKeys.size() <= DEVICE_ID_INDEX) {
49         return GetAnonyString(dbKey);
50     }
51     for (size_t i = DEVICE_ID_INDEX; i < splitKeys.size(); i++) {
52         splitKeys[i] = GetAnonyString(splitKeys[i]);
53     }
54     return JoinString(splitKeys, SEPARATOR);
55 }
56 
GetAnonyString(const std::string & value)57 std::string ProfileUtils::GetAnonyString(const std::string& value)
58 {
59     constexpr size_t INT32_SHORT_ID_LENGTH = 10;
60     constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
61     constexpr size_t INT32_MIN_ID_LENGTH = 3;
62     std::string res;
63     std::string tmpStr("***");
64     size_t strLen = value.length();
65     if (strLen < INT32_MIN_ID_LENGTH) {
66         return tmpStr;
67     }
68 
69     if (strLen <= INT32_SHORT_ID_LENGTH) {
70         res += value[0];
71         res += tmpStr;
72         res += value[strLen - 1];
73     } else {
74         res.append(value, 0, INT32_PLAINTEXT_LENGTH);
75         res += tmpStr;
76         res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
77     }
78     return res;
79 }
80 
GetAnonyInt32(const int32_t value)81 std::string ProfileUtils::GetAnonyInt32(const int32_t value)
82 {
83     std::string tempString = std::to_string(value);
84     size_t length = tempString.length();
85     if (length == 0x01) {
86         tempString[0] = '*';
87         return tempString;
88     }
89     for (size_t i = 1; i < length - 1; i++) {
90         tempString[i] = '*';
91     }
92     return tempString;
93 }
94 
IsP2p(const int32_t authForm)95 bool ProfileUtils::IsP2p(const int32_t authForm)
96 {
97     if (authForm == static_cast<int32_t>(DistributedHardware::DmAuthForm::INVALID_TYPE) ||
98         authForm == static_cast<int32_t>(DistributedHardware::DmAuthForm::IDENTICAL_ACCOUNT)) {
99         HILOGD("authForm: %{public}d!", authForm);
100         return false;
101     }
102     return true;
103 }
104 
GetProfileType(const std::string & key)105 ProfileType ProfileUtils::GetProfileType(const std::string& key)
106 {
107     if (key.length() == 0 || key.length() > MAX_STRING_LEN) {
108         HILOGE("This key is invalid, value: %{public}s!", GetAnonyString(key).c_str());
109         return ProfileType::PROFILE_TYPE_MIN;
110     }
111     ProfileType profileType = ProfileType::PROFILE_TYPE_MIN;
112     if (StartsWith(key, DEV_PREFIX)) {
113         profileType = ProfileType::DEVICE_PROFILE;
114     }
115     if (StartsWith(key, SVR_PREFIX)) {
116         profileType = ProfileType::SERVICE_PROFILE;
117     }
118     if (StartsWith(key, CHAR_PREFIX)) {
119         profileType = ProfileType::CHAR_PROFILE;
120     }
121     return profileType;
122 }
123 
StartsWith(const std::string & str,const std::string & prefix)124 bool ProfileUtils::StartsWith(const std::string& str, const std::string& prefix)
125 {
126     return (str.find(prefix, 0) == 0);
127 }
128 
EndsWith(const std::string & str,const std::string & suffix)129 bool ProfileUtils::EndsWith(const std::string& str, const std::string& suffix)
130 {
131     if (str.length() < suffix.length()) {
132         return false;
133     }
134     return str.substr(str.length() - suffix.length()).compare(suffix) == 0;
135 }
136 
GetProfileKey(const std::string & dbKey)137 std::string ProfileUtils::GetProfileKey(const std::string& dbKey)
138 {
139     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
140         return "";
141     }
142     std::size_t pos = dbKey.find_last_of("#");
143     return dbKey.substr(0, pos);
144 }
145 
GetDeviceIdByDBKey(const std::string & dbKey)146 std::string ProfileUtils::GetDeviceIdByDBKey(const std::string& dbKey)
147 {
148     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
149         return "";
150     }
151     std::vector<std::string> res;
152     if (SplitString(dbKey, SEPARATOR, res) != DP_SUCCESS) {
153         return "";
154     }
155     if (res.size() < NUM_1) {
156         return "";
157     }
158     return res[NUM_1];
159 }
160 
GetServiceNameByDBKey(const std::string & dbKey)161 std::string ProfileUtils::GetServiceNameByDBKey(const std::string& dbKey)
162 {
163     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
164         return "";
165     }
166     std::vector<std::string> res;
167     if (SplitString(dbKey, SEPARATOR, res) != DP_SUCCESS) {
168         return "";
169     }
170     if (res.size() < NUM_2) {
171         return "";
172     }
173     return res[NUM_2];
174 }
175 
GetNonOhSuffixServiceNameByDBKey(const std::string & dbKey)176 std::string ProfileUtils::GetNonOhSuffixServiceNameByDBKey(const std::string& dbKey)
177 {
178     std::string serviceName = GetServiceNameByDBKey(dbKey);
179     if (serviceName.empty()) {
180         return "";
181     }
182     return CheckAndRemoveOhSuffix(serviceName);
183 }
184 
IsNeedAddOhSuffix(const std::string & profileName,bool isSvr)185 bool ProfileUtils::IsNeedAddOhSuffix(const std::string& profileName, bool isSvr)
186 {
187     if (profileName.length() == 0 || profileName.length() > MAX_STRING_LEN) {
188         return false;
189     }
190     if (isSvr && NEED_ADD_OH_SUFFIX_SVR_NAMES.count(profileName) > 0) {
191         return true;
192     }
193     if (!isSvr && NEED_ADD_OH_SUFFIX_DEV_PROFILES.count(profileName) > 0) {
194         return true;
195     }
196     return false;
197 }
198 
CheckAndAddOhSuffix(const std::string & profileName,bool isSvr)199 std::string ProfileUtils::CheckAndAddOhSuffix(const std::string& profileName, bool isSvr)
200 {
201     std::string str = profileName;
202     if (IsNeedAddOhSuffix(str, isSvr)) {
203         str = str + OH_PROFILE_SUFFIX;
204     }
205     return str;
206 }
207 
CheckAndRemoveOhSuffix(const std::string & profileName)208 std::string ProfileUtils::CheckAndRemoveOhSuffix(const std::string& profileName)
209 {
210     std::string str = profileName;
211     if (EndsWith(str, OH_PROFILE_SUFFIX)) {
212         str = str.erase(str.length() - OH_PROFILE_SUFFIX.length());
213     }
214     return str;
215 }
216 
GetCharKeyByDBKey(const std::string & dbKey)217 std::string ProfileUtils::GetCharKeyByDBKey(const std::string& dbKey)
218 {
219     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
220         return "";
221     }
222     std::vector<std::string> res;
223     if (SplitString(dbKey, SEPARATOR, res) != DP_SUCCESS) {
224         return "";
225     }
226     if (res.size() < NUM_3) {
227         return "";
228     }
229     return res[NUM_3];
230 }
231 
SplitString(const std::string & str,const std::string & splits,std::vector<std::string> & res)232 int32_t ProfileUtils::SplitString(const std::string& str, const std::string& splits, std::vector<std::string>& res)
233 {
234     if (str == "") {
235         return DP_INVALID_PARAMS;
236     }
237     std::string strs = str + splits;
238     size_t pos = strs.find(splits);
239     int32_t step = splits.size();
240 
241     while (pos != strs.npos) {
242         std::string temp = strs.substr(0, pos);
243         res.push_back(temp);
244         strs = strs.substr(pos + step, strs.size());
245         pos = strs.find(splits);
246     }
247     return DP_SUCCESS;
248 }
249 
JoinString(const std::vector<std::string> & strs,const std::string & delimiter)250 std::string ProfileUtils::JoinString(const std::vector<std::string>& strs, const std::string& delimiter)
251 {
252     std::string res = EMPTY_STRING;
253     if (strs.empty()) {
254         return res;
255     }
256     for (size_t i = 0; i < strs.size(); i++) {
257         res += strs[i];
258         if (i != strs.size() - 1) {
259             res += delimiter;
260         }
261     }
262     return res;
263 }
264 
IsKeyValid(const std::string & key)265 bool ProfileUtils::IsKeyValid(const std::string& key)
266 {
267     if (key.length() == 0 || key.length() > MAX_STRING_LEN) {
268         return false;
269     }
270     size_t found = key.find(SEPARATOR);
271     if (found != std::string::npos) {
272         return false;
273     }
274     return true;
275 }
276 
IsLocalUdid(const std::string & udid)277 bool ProfileUtils::IsLocalUdid(const std::string& udid)
278 {
279     if (udid.length() == 0 || udid.length() > MAX_STRING_LEN) {
280         return false;
281     }
282     std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid();
283     return localUdid == udid;
284 }
285 
IsDevProfileValid(const DeviceProfile & devProfile)286 bool ProfileUtils::IsDevProfileValid(const DeviceProfile& devProfile)
287 {
288     return IsKeyValid(devProfile.GetDeviceId()) && IsLocalUdid(devProfile.GetDeviceId());
289 }
290 
IsSvrProfileValid(const ServiceProfile & svrProfile)291 bool ProfileUtils::IsSvrProfileValid(const ServiceProfile& svrProfile)
292 {
293     return IsKeyValid(svrProfile.GetDeviceId()) && IsLocalUdid(svrProfile.GetDeviceId()) &&
294         IsKeyValid(svrProfile.GetServiceName());
295 }
296 
IsCharProfileValid(const CharacteristicProfile & charProfile)297 bool ProfileUtils::IsCharProfileValid(const CharacteristicProfile& charProfile)
298 {
299     return IsKeyValid(charProfile.GetDeviceId()) && IsLocalUdid(charProfile.GetDeviceId()) &&
300         IsKeyValid(charProfile.GetServiceName()) && IsKeyValid(charProfile.GetCharacteristicKey());
301 }
302 
IsDeviceProfileValid(const DeviceProfile & devProfile)303 bool ProfileUtils::IsDeviceProfileValid(const DeviceProfile& devProfile)
304 {
305     if (devProfile.GetOsVersion().empty() || devProfile.GetOsType() == MIN_OS_TYPE) {
306         return false;
307     }
308     return true;
309 }
310 
IsServiceProfileValid(const ServiceProfile & svrProfile)311 bool ProfileUtils::IsServiceProfileValid(const ServiceProfile& svrProfile)
312 {
313     if (svrProfile.GetServiceName().empty() || svrProfile.GetServiceType().empty()) {
314         return false;
315     }
316     return true;
317 }
318 
IsCharacteristicProfileValid(const CharacteristicProfile & charProfile)319 bool ProfileUtils::IsCharacteristicProfileValid(const CharacteristicProfile& charProfile)
320 {
321     if (charProfile.GetCharacteristicKey().empty() || charProfile.GetCharacteristicValue().empty()) {
322         return false;
323     }
324     return true;
325 }
326 
GenerateDeviceProfileKey(const std::string & deviceId)327 std::string ProfileUtils::GenerateDeviceProfileKey(const std::string& deviceId)
328 {
329     return DEV_PREFIX + SEPARATOR + deviceId;
330 }
331 
GenerateServiceProfileKey(const std::string & deviceId,const std::string & serviceName)332 std::string ProfileUtils::GenerateServiceProfileKey(const std::string& deviceId, const std::string& serviceName)
333 {
334     return SVR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName;
335 }
336 
GenerateCharProfileKey(const std::string & deviceId,const std::string & serviceName,const std::string & charKey)337 std::string ProfileUtils::GenerateCharProfileKey(const std::string& deviceId, const std::string& serviceName,
338     const std::string& charKey)
339 {
340     return CHAR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR + charKey;
341 }
342 
TrustDeviceProfileToEntries(const TrustDeviceProfile & profile,ValuesBucket & values)343 int32_t ProfileUtils::TrustDeviceProfileToEntries(const TrustDeviceProfile& profile, ValuesBucket& values)
344 {
345     values.PutString(DEVICE_ID, profile.GetDeviceId());
346     values.PutInt(DEVICE_ID_TYPE, profile.GetDeviceIdType());
347     values.PutString(DEVICE_ID_HASH, profile.GetDeviceIdHash());
348     values.PutInt(STATUS, profile.GetStatus());
349     return DP_SUCCESS;
350 }
351 
AccessControlProfileToEntries(const AccessControlProfile & profile,ValuesBucket & values)352 int32_t ProfileUtils::AccessControlProfileToEntries(const AccessControlProfile& profile, ValuesBucket& values)
353 {
354     values.PutLong(ACCESS_CONTROL_ID, profile.GetAccessControlId());
355     values.PutLong(ACCESSER_ID, profile.GetAccesserId());
356     values.PutLong(ACCESSEE_ID, profile.GetAccesseeId());
357     values.PutString(TRUST_DEVICE_ID, profile.GetTrustDeviceId());
358     values.PutInt(AUTHENTICATION_TYPE, profile.GetAuthenticationType());
359     values.PutInt(DEVICE_ID_TYPE, profile.GetDeviceIdType());
360     values.PutString(DEVICE_ID_HASH, profile.GetDeviceIdHash());
361     values.PutInt(BIND_TYPE, profile.GetBindType());
362     values.PutString(SESSION_KEY, profile.GetSessionKey());
363     values.PutInt(LAST_AUTH_TIME, profile.GetLastAuthTime());
364     values.PutInt(VALID_PERIOD, profile.GetValidPeriod());
365     values.PutInt(STATUS, profile.GetStatus());
366     values.PutInt(BIND_LEVEL, profile.GetBindLevel());
367     return DP_SUCCESS;
368 }
369 
AccesserToEntries(const AccessControlProfile & aclProfile,ValuesBucket & values)370 int32_t ProfileUtils::AccesserToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values)
371 {
372     Accesser accesser = aclProfile.GetAccesser();
373     values.PutLong(ACCESSER_ID, accesser.GetAccesserId());
374     values.PutString(ACCESSER_DEVICE_ID, accesser.GetAccesserDeviceId());
375     values.PutInt(ACCESSER_USER_ID, accesser.GetAccesserUserId());
376     values.PutString(ACCESSER_ACCOUNT_ID, accesser.GetAccesserAccountId());
377     values.PutLong(ACCESSER_TOKEN_ID, accesser.GetAccesserTokenId());
378     values.PutString(ACCESSER_BUNDLE_NAME, accesser.GetAccesserBundleName());
379     values.PutString(ACCESSER_HAP_SIGNATURE, accesser.GetAccesserHapSignature());
380     values.PutInt(ACCESSER_BIND_LEVEL, accesser.GetAccesserBindLevel());
381     values.PutString(ACCESSER_DEVICE_NAME, accesser.GetAccesserDeviceName());
382     values.PutString(ACCESSER_SERVICE_NAME, accesser.GetAccesserServiceName());
383     values.PutInt(ACCESSER_CREDENTIAL_ID, accesser.GetAccesserCredentialId());
384     values.PutInt(ACCESSER_STATUS, accesser.GetAccesserStatus());
385     values.PutInt(ACCESSER_SESSION_KEY_ID, accesser.GetAccesserSessionKeyId());
386     values.PutLong(ACCESSER_SESSION_KEY_TIMESTAMP, accesser.GetAccesserSKTimeStamp());
387     return DP_SUCCESS;
388 }
389 
AccesseeToEntries(const AccessControlProfile & aclProfile,ValuesBucket & values)390 int32_t ProfileUtils::AccesseeToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values)
391 {
392     Accessee accessee = aclProfile.GetAccessee();
393     values.PutLong(ACCESSEE_ID, accessee.GetAccesseeId());
394     values.PutString(ACCESSEE_DEVICE_ID, accessee.GetAccesseeDeviceId());
395     values.PutInt(ACCESSEE_USER_ID, accessee.GetAccesseeUserId());
396     values.PutString(ACCESSEE_ACCOUNT_ID, accessee.GetAccesseeAccountId());
397     values.PutLong(ACCESSEE_TOKEN_ID, accessee.GetAccesseeTokenId());
398     values.PutString(ACCESSEE_BUNDLE_NAME, accessee.GetAccesseeBundleName());
399     values.PutString(ACCESSEE_HAP_SIGNATURE, accessee.GetAccesseeHapSignature());
400     values.PutInt(ACCESSEE_BIND_LEVEL, accessee.GetAccesseeBindLevel());
401     values.PutString(ACCESSEE_DEVICE_NAME, accessee.GetAccesseeDeviceName());
402     values.PutString(ACCESSEE_SERVICE_NAME, accessee.GetAccesseeServiceName());
403     values.PutInt(ACCESSEE_CREDENTIAL_ID, accessee.GetAccesseeCredentialId());
404     values.PutInt(ACCESSEE_STATUS, accessee.GetAccesseeStatus());
405     values.PutInt(ACCESSEE_SESSION_KEY_ID, accessee.GetAccesseeSessionKeyId());
406     values.PutLong(ACCESSEE_SESSION_KEY_TIMESTAMP, accessee.GetAccesseeSKTimeStamp());
407     return DP_SUCCESS;
408 }
409 
DeviceProfileToEntries(const DeviceProfile & profile,std::map<std::string,std::string> & values,bool isMultiUser)410 int32_t ProfileUtils::DeviceProfileToEntries(const DeviceProfile& profile, std::map<std::string, std::string>& values,
411     bool isMultiUser)
412 {
413     std::string deviceProfileKey = GenerateDeviceProfileKey(profile.GetDeviceId());
414     if (isMultiUser) {
415         values[GenerateDBKey(deviceProfileKey, OS_SYS_CAPACITY, profile.GetUserId())] = profile.GetOsSysCap();
416         values[GenerateDBKey(deviceProfileKey, OS_VERSION, profile.GetUserId())] = profile.GetOsVersion();
417         values[GenerateDBKey(deviceProfileKey, OS_TYPE, profile.GetUserId())] = std::to_string(profile.GetOsType());
418         values[GenerateDBKey(deviceProfileKey, OS_VERSION + OH_PROFILE_SUFFIX, profile.GetUserId())] =
419             profile.GetOsVersion();
420         values[GenerateDBKey(deviceProfileKey, OS_TYPE + OH_PROFILE_SUFFIX, profile.GetUserId())] =
421             std::to_string(profile.GetOsType());
422     } else {
423         values[GenerateDBKey(deviceProfileKey, OS_SYS_CAPACITY)] = profile.GetOsSysCap();
424         values[GenerateDBKey(deviceProfileKey, OS_VERSION)] = profile.GetOsVersion();
425         values[GenerateDBKey(deviceProfileKey, OS_TYPE)] = std::to_string(profile.GetOsType());
426         values[GenerateDBKey(deviceProfileKey, OS_VERSION + OH_PROFILE_SUFFIX)] = profile.GetOsVersion();
427         values[GenerateDBKey(deviceProfileKey, OS_TYPE + OH_PROFILE_SUFFIX)] = std::to_string(profile.GetOsType());
428     }
429     return DP_SUCCESS;
430 }
431 
ServiceProfileToEntries(const ServiceProfile & profile,std::map<std::string,std::string> & values,bool isMultiUser)432 int32_t ProfileUtils::ServiceProfileToEntries(const ServiceProfile& profile, std::map<std::string,
433     std::string>& values, bool isMultiUser)
434 {
435     std::string serviceName = CheckAndAddOhSuffix(profile.GetServiceName(), true);
436     std::string serviceProfileKey = GenerateServiceProfileKey(profile.GetDeviceId(), serviceName);
437     // value not need add OH suffix
438     if (isMultiUser) {
439         values[GenerateDBKey(serviceProfileKey, SERVICE_NAME, profile.GetUserId())] = profile.GetServiceName();
440         values[GenerateDBKey(serviceProfileKey, SERVICE_TYPE, profile.GetUserId())] = profile.GetServiceType();
441     } else {
442         values[GenerateDBKey(serviceProfileKey, SERVICE_NAME)] = profile.GetServiceName();
443         values[GenerateDBKey(serviceProfileKey, SERVICE_TYPE)] = profile.GetServiceType();
444     }
445     return DP_SUCCESS;
446 }
447 
CharacteristicProfileToEntries(const CharacteristicProfile & profile,std::map<std::string,std::string> & values,bool isMultiUser)448 int32_t ProfileUtils::CharacteristicProfileToEntries(const CharacteristicProfile& profile,
449     std::map<std::string, std::string>& values, bool isMultiUser)
450 {
451     std::string serviceName = CheckAndAddOhSuffix(profile.GetServiceName(), true);
452     std::string charProfileKey = GenerateCharProfileKey(profile.GetDeviceId(), serviceName,
453         profile.GetCharacteristicKey());
454     if (isMultiUser) {
455         values[GenerateDBKey(charProfileKey, CHARACTERISTIC_KEY, profile.GetUserId())] =
456             profile.GetCharacteristicKey();
457         values[GenerateDBKey(charProfileKey, CHARACTERISTIC_VALUE, profile.GetUserId())] =
458             profile.GetCharacteristicValue();
459     } else {
460         values[GenerateDBKey(charProfileKey, CHARACTERISTIC_KEY)] = profile.GetCharacteristicKey();
461         values[GenerateDBKey(charProfileKey, CHARACTERISTIC_VALUE)] = profile.GetCharacteristicValue();
462     }
463     return DP_SUCCESS;
464 }
465 
EntriesToTrustDeviceProfile(const ValuesBucket & values,TrustDeviceProfile & profile)466 int32_t ProfileUtils::EntriesToTrustDeviceProfile(const ValuesBucket& values, TrustDeviceProfile& profile)
467 {
468     ValueObject valueObject;
469     std::string strValue = "";
470     int32_t intValue = 0;
471     if (values.GetObject(DEVICE_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
472         profile.SetDeviceId(strValue);
473     }
474     if (values.GetObject(DEVICE_ID_HASH, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
475         profile.SetDeviceIdHash(strValue);
476     }
477     if (values.GetObject(DEVICE_TYPE_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
478         profile.SetDeviceIdType(intValue);
479     }
480     if (values.GetObject(STATUS, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
481         profile.SetStatus(intValue);
482     }
483     return DP_SUCCESS;
484 }
485 
EntriesToAccessControlProfile(const ValuesBucket & values,AccessControlProfile & profile)486 int32_t ProfileUtils::EntriesToAccessControlProfile(const ValuesBucket& values, AccessControlProfile& profile)
487 {
488     std::string strValue = "";
489     int32_t intValue = 0;
490     int64_t int64Value = 0;
491     if (GetLongValue(values, ACCESS_CONTROL_ID, int64Value)) {
492         profile.SetAccessControlId(int64Value);
493     }
494     if (GetLongValue(values, ACCESSER_ID, int64Value)) {
495         profile.SetAccesserId(int64Value);
496     }
497     if (GetLongValue(values, ACCESSEE_ID, int64Value)) {
498         profile.SetAccesseeId(int64Value);
499     }
500     if (GetStringValue(values, SESSION_KEY, strValue)) {
501         profile.SetSessionKey(strValue);
502     }
503     if (GetIntValue(values, BIND_TYPE, intValue)) {
504         profile.SetBindType(intValue);
505     }
506     if (GetIntValue(values, AUTHENTICATION_TYPE, intValue)) {
507         profile.SetAuthenticationType(intValue);
508     }
509     if (GetIntValue(values, BIND_LEVEL, intValue)) {
510         profile.SetBindLevel(intValue);
511     }
512     if (GetIntValue(values, STATUS, intValue)) {
513         profile.SetStatus(intValue);
514     }
515     if (GetIntValue(values, VALID_PERIOD, intValue)) {
516         profile.SetValidPeriod(intValue);
517     }
518     if (GetIntValue(values, LAST_AUTH_TIME, intValue)) {
519         profile.SetLastAuthTime(intValue);
520     }
521     if (GetStringValue(values, TRUST_DEVICE_ID, strValue)) {
522         profile.SetTrustDeviceId(strValue);
523     }
524     if (GetIntValue(values, DEVICE_TYPE_ID, intValue)) {
525         profile.SetDeviceIdType(intValue);
526     }
527     if (GetStringValue(values, DEVICE_ID_HASH, strValue)) {
528         profile.SetDeviceIdHash(strValue);
529     }
530     return DP_SUCCESS;
531 }
532 
EntriesToAccesser(const ValuesBucket & values,Accesser & accesser)533 int32_t ProfileUtils::EntriesToAccesser(const ValuesBucket& values, Accesser& accesser)
534 {
535     ValueObject valueObject;
536     std::string strValue = "";
537     int32_t intValue = 0;
538     int64_t int64Value = 0;
539     if (values.GetObject(ACCESSER_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
540         accesser.SetAccesserId(int64Value);
541     }
542     if (values.GetObject(ACCESSER_DEVICE_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
543         accesser.SetAccesserDeviceId(strValue);
544     }
545     if (values.GetObject(ACCESSER_USER_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
546         accesser.SetAccesserUserId(intValue);
547     }
548     if (values.GetObject(ACCESSER_ACCOUNT_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
549         accesser.SetAccesserAccountId(strValue);
550     }
551     if (values.GetObject(ACCESSER_TOKEN_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
552         accesser.SetAccesserTokenId(int64Value);
553     }
554     if (values.GetObject(ACCESSER_BUNDLE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
555         accesser.SetAccesserBundleName(strValue);
556     }
557     if (values.GetObject(ACCESSER_BIND_LEVEL, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
558         accesser.SetAccesserBindLevel(intValue);
559     }
560     EntriesToAccesserExt(values, accesser);
561     return DP_SUCCESS;
562 }
563 
EntriesToAccessee(const ValuesBucket & values,Accessee & accessee)564 int32_t ProfileUtils::EntriesToAccessee(const ValuesBucket& values, Accessee& accessee)
565 {
566     ValueObject valueObject;
567     std::string strValue = "";
568     int32_t intValue = 0;
569     int64_t int64Value = 0;
570     if (values.GetObject(ACCESSEE_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
571         accessee.SetAccesseeId(int64Value);
572     }
573     if (values.GetObject(ACCESSEE_DEVICE_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
574         accessee.SetAccesseeDeviceId(strValue);
575     }
576     if (values.GetObject(ACCESSEE_USER_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
577         accessee.SetAccesseeUserId(intValue);
578     }
579     if (values.GetObject(ACCESSEE_ACCOUNT_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
580         accessee.SetAccesseeAccountId(strValue);
581     }
582     if (values.GetObject(ACCESSEE_TOKEN_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
583         accessee.SetAccesseeTokenId(int64Value);
584     }
585     if (values.GetObject(ACCESSEE_BUNDLE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
586         accessee.SetAccesseeBundleName(strValue);
587     }
588     if (values.GetObject(ACCESSEE_BIND_LEVEL, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
589         accessee.SetAccesseeBindLevel(intValue);
590     }
591     EntriesToAccesseeExt(values, accessee);
592     return DP_SUCCESS;
593 }
594 
EntriesToAccesserExt(const ValuesBucket & values,Accesser & accesser)595 int32_t ProfileUtils::EntriesToAccesserExt(const ValuesBucket &values, Accesser &accesser)
596 {
597     ValueObject valueObject;
598     std::string strValue = "";
599     int32_t intValue = 0;
600     int64_t int64Value = 0;
601     if (values.GetObject(ACCESSER_DEVICE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
602         accesser.SetAccesserDeviceName(strValue);
603     }
604     if (values.GetObject(ACCESSER_SERVICE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
605         accesser.SetAccesserServiceName(strValue);
606     }
607     if (values.GetObject(ACCESSER_CREDENTIAL_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
608         accesser.SetAccesserCredentialId(intValue);
609     }
610     if (values.GetObject(ACCESSER_STATUS, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
611         accesser.SetAccesserStatus(intValue);
612     }
613     if (values.GetObject(ACCESSER_SESSION_KEY_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
614         accesser.SetAccesserSessionKeyId(intValue);
615     }
616     if (values.GetObject(ACCESSER_SESSION_KEY_TIMESTAMP, valueObject) &&
617         valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
618         accesser.SetAccesserSKTimeStamp(int64Value);
619     }
620     return DP_SUCCESS;
621 }
622 
EntriesToAccesseeExt(const ValuesBucket & values,Accessee & accessee)623 int32_t ProfileUtils::EntriesToAccesseeExt(const ValuesBucket &values, Accessee &accessee)
624 {
625     ValueObject valueObject;
626     std::string strValue = "";
627     int32_t intValue = 0;
628     int64_t int64Value = 0;
629     if (values.GetObject(ACCESSEE_DEVICE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
630         accessee.SetAccesseeDeviceName(strValue);
631     }
632     if (values.GetObject(ACCESSEE_SERVICE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
633         accessee.SetAccesseeServiceName(strValue);
634     }
635     if (values.GetObject(ACCESSEE_CREDENTIAL_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
636         accessee.SetAccesseeCredentialId(intValue);
637     }
638     if (values.GetObject(ACCESSEE_STATUS, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
639         accessee.SetAccesseeStatus(intValue);
640     }
641     if (values.GetObject(ACCESSEE_SESSION_KEY_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
642         accessee.SetAccesseeSessionKeyId(intValue);
643     }
644     if (values.GetObject(ACCESSEE_SESSION_KEY_TIMESTAMP, valueObject) &&
645         valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
646         accessee.SetAccesseeSKTimeStamp(int64Value);
647     }
648     return DP_SUCCESS;
649 }
650 
EntriesToDeviceProfile(std::map<std::string,std::string> values,DeviceProfile & profile)651 int32_t ProfileUtils::EntriesToDeviceProfile(std::map<std::string, std::string> values, DeviceProfile& profile)
652 {
653     if (values.empty() || values.size() > MAX_DB_RECORD_SIZE) {
654         HILOGI("Entries size is invalid!size: %{public}zu!", values.size());
655         return DP_INVALID_PARAMS;
656     }
657     auto propertiesMap = GetProfilePropertiesMap(values, profile.GetUserId());
658     if (IsPropertyValid(propertiesMap, OS_SYS_CAPACITY, MAX_STRING_LEN)) {
659         profile.SetOsSysCap(propertiesMap[OS_SYS_CAPACITY]);
660     }
661     if (IsPropertyValid(propertiesMap, OS_VERSION, MAX_STRING_LEN)) {
662         profile.SetOsVersion(propertiesMap[OS_VERSION]);
663     }
664     if (IsPropertyValid(propertiesMap, OS_VERSION + OH_PROFILE_SUFFIX, MAX_STRING_LEN)) {
665         profile.SetOsVersion(propertiesMap[OS_VERSION + OH_PROFILE_SUFFIX]);
666     }
667     if (IsPropertyValid(propertiesMap, OS_TYPE, MIN_OS_TYPE, MAX_OS_TYPE)) {
668         int32_t osType = std::atoi(propertiesMap[OS_TYPE].c_str());
669         profile.SetOsType(osType);
670     }
671     if (IsPropertyValid(propertiesMap, OS_TYPE + OH_PROFILE_SUFFIX, MIN_OS_TYPE, MAX_OS_TYPE)) {
672         int32_t osType = std::atoi(propertiesMap[OS_TYPE + OH_PROFILE_SUFFIX].c_str());
673         profile.SetOsType(osType);
674     }
675     return DP_SUCCESS;
676 }
677 
EntriesToServiceProfile(std::map<std::string,std::string> values,ServiceProfile & profile)678 int32_t ProfileUtils::EntriesToServiceProfile(std::map<std::string, std::string> values, ServiceProfile& profile)
679 {
680     if (values.empty() || values.size() > MAX_DB_RECORD_SIZE) {
681         HILOGI("Entries size is invalid!size: %{public}zu!", values.size());
682         return DP_INVALID_PARAMS;
683     }
684     auto iter = values.begin();
685     profile.SetDeviceId(GetDeviceIdByDBKey(iter->first));
686     auto propertiesMap = GetProfilePropertiesMap(values, profile.GetUserId());
687     if (propertiesMap.count(SERVICE_NAME) != 0 && 0 < propertiesMap[SERVICE_NAME].length() &&
688         propertiesMap[SERVICE_NAME].length() < MAX_STRING_LEN) {
689         profile.SetServiceName(CheckAndRemoveOhSuffix(propertiesMap[SERVICE_NAME]));
690     }
691     if (propertiesMap.count(SERVICE_TYPE) != 0 && 0 < propertiesMap[SERVICE_TYPE].length() &&
692         propertiesMap[SERVICE_TYPE].length() < MAX_STRING_LEN) {
693         profile.SetServiceType(propertiesMap[SERVICE_TYPE]);
694     }
695     return DP_SUCCESS;
696 }
697 
EntriesToCharProfile(std::map<std::string,std::string> values,CharacteristicProfile & profile)698 int32_t ProfileUtils::EntriesToCharProfile(std::map<std::string, std::string> values, CharacteristicProfile& profile)
699 {
700     if (values.empty() || values.size() > MAX_DB_RECORD_SIZE) {
701         HILOGI("Entries size is invalid!size : %{public}zu", values.size());
702         return DP_INVALID_PARAMS;
703     }
704     auto iter = values.begin();
705     profile.SetDeviceId(GetDeviceIdByDBKey(iter->first));
706     profile.SetServiceName(GetNonOhSuffixServiceNameByDBKey(iter->first));
707     auto propertiesMap = GetProfilePropertiesMap(values, profile.GetUserId());
708     if (propertiesMap.count(CHARACTERISTIC_KEY) != 0 && 0 < propertiesMap[CHARACTERISTIC_KEY].length() &&
709         propertiesMap[CHARACTERISTIC_KEY].length() < MAX_STRING_LEN) {
710         profile.SetCharacteristicKey(propertiesMap[CHARACTERISTIC_KEY]);
711     }
712     if (propertiesMap.count(CHARACTERISTIC_VALUE) != 0 && 0 < propertiesMap[CHARACTERISTIC_VALUE].length() &&
713         propertiesMap[CHARACTERISTIC_VALUE].length() < MAX_STRING_LEN) {
714         profile.SetCharacteristicValue(propertiesMap[CHARACTERISTIC_VALUE]);
715     }
716     return DP_SUCCESS;
717 }
718 
GenerateDBKey(const std::string & profileKey,const std::string & profileProperty,int32_t userId)719 std::string ProfileUtils::GenerateDBKey(const std::string& profileKey, const std::string& profileProperty,
720     int32_t userId)
721 {
722     std::string DBKey = "";
723     if (userId != DEFAULT_USER_ID) {
724         DBKey = profileKey + SEPARATOR + profileProperty + SEPARATOR + std::to_string(userId);
725     } else {
726         DBKey = profileKey + SEPARATOR + profileProperty;
727     }
728     return DBKey;
729 }
730 
GetProfileProperty(const std::string & dbKey,int32_t userId)731 std::string ProfileUtils::GetProfileProperty(const std::string& dbKey, int32_t userId)
732 {
733     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
734         return "";
735     }
736     int32_t getUserId = GetUserIdFromDbKey(dbKey);
737     std::vector<std::string> splitKeys;
738     if (SplitString(dbKey, SEPARATOR, splitKeys) != DP_SUCCESS || splitKeys.size() < NUM_3) {
739         HILOGE("GetProfileProperty SplitString fail");
740         return "";
741     }
742 
743     if (splitKeys[0] == DEV_PREFIX) {
744         if (userId != DEFAULT_USER_ID && getUserId == DEFAULT_USER_ID) {
745             return splitKeys[NUM_2];
746         }
747         return (userId == getUserId) ? splitKeys[NUM_2] : "";
748     }
749     if (splitKeys[0] == SVR_PREFIX && splitKeys.size() >= NUM_4) {
750         if (userId != DEFAULT_USER_ID && getUserId == DEFAULT_USER_ID) {
751             return splitKeys[NUM_3];
752         }
753         return (userId == getUserId) ? splitKeys[NUM_3] : "";
754     }
755     if (splitKeys[0] == CHAR_PREFIX && splitKeys.size() >= NUM_5) {
756         if (userId != DEFAULT_USER_ID && getUserId == DEFAULT_USER_ID) {
757             return splitKeys[NUM_4];
758         }
759         return (userId == getUserId) ? splitKeys[NUM_4] : "";
760     }
761 
762     HILOGE("dbKey has wrong prefix");
763     return "";
764 }
765 
GetProfilePropertiesMap(std::map<std::string,std::string> dbEntries,int32_t userId)766 std::map<std::string, std::string> ProfileUtils::GetProfilePropertiesMap(std::map<std::string, std::string> dbEntries,
767     int32_t userId)
768 {
769     std::map<std::string, std::string> propertiesWithoutUserId;
770     std::map<std::string, std::string> propertiesWithUserId;
771     if (userId <= 0 && userId != DEFAULT_USER_ID) {
772         HILOGE("userId is invalid, userId: %{public}s", GetAnonyInt32(userId).c_str());
773         return propertiesWithoutUserId;
774     }
775     for (const auto& item : dbEntries) {
776         std::string profileProperty = GetProfileProperty(item.first, userId);
777         if (profileProperty.empty()) {
778             HILOGE("GetProfileProperty fail, %{public}s!", GetDbKeyAnonyString(item.first).c_str());
779             continue;
780         }
781         if (GetUserIdFromDbKey(item.first) == DEFAULT_USER_ID) {
782             propertiesWithoutUserId[profileProperty] = item.second;
783             continue;
784         }
785         propertiesWithUserId[profileProperty] = item.second;
786     }
787 
788     if (userId != DEFAULT_USER_ID && !propertiesWithUserId.empty()) {
789         HILOGI("GetProfile with multi-user");
790         return propertiesWithUserId;
791     }
792 
793     //1. Get profile without multi-user;
794     //2. Get profile with multi-user, but remote device without multi-user;
795     //3. Get profile with multi-user, but can't find in DB;
796     return propertiesWithoutUserId;
797 }
798 
toString(const std::u16string & str16)799 std::string ProfileUtils::toString(const std::u16string& str16)
800 {
801     return std::wstring_convert< std::codecvt_utf8_utf16<char16_t>, char16_t >{}.to_bytes(str16);
802 }
803 
IsPropertyValid(const std::map<std::string,std::string> & propertyMap,const std::string & property,int32_t maxValue)804 bool ProfileUtils::IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
805     int32_t maxValue)
806 {
807     if (propertyMap.count(property) != 0 && 0 < propertyMap.at(property).length() &&
808         (static_cast<int32_t>(propertyMap.at(property).length())) < maxValue) {
809         return true;
810     }
811     HILOGE("property is valid, property : %{public}s", property.c_str());
812     return false;
813 }
814 
IsPropertyValid(const std::map<std::string,std::string> & propertyMap,const std::string & property,int32_t minValue,int32_t maxValue)815 bool ProfileUtils::IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
816     int32_t minValue, int32_t maxValue)
817 {
818     if (property.empty() || propertyMap.find(property) == propertyMap.end()) {
819         HILOGE("property is valid, property : %{public}s", property.c_str());
820         return false;
821     }
822     std::string propertyValue = propertyMap.at(property);
823     if (!IsNumStr(propertyValue)) {
824         HILOGE("%{public}s is not numeric string", GetAnonyString(propertyValue).c_str());
825         return false;
826     }
827     if (minValue < std::atoi(propertyValue.c_str()) && std::atoi(propertyValue.c_str()) < maxValue) {
828         return true;
829     }
830     return false;
831 }
832 
IsPropertyValidInt64(const std::map<std::string,std::string> & propertyMap,const std::string & property)833 bool ProfileUtils::IsPropertyValidInt64(const std::map<std::string,
834     std::string>& propertyMap, const std::string& property)
835 {
836     if (property.empty() || propertyMap.find(property) == propertyMap.end()) {
837         HILOGE("property is valid, property : %{public}s", property.c_str());
838         return false;
839     }
840     std::string propertyValue = propertyMap.at(property);
841     if (!IsNumStr(propertyValue)) {
842         HILOGE("%{public}s is not numeric string", GetAnonyString(propertyValue).c_str());
843         return false;
844     }
845     if (INT64_MIN < std::atoll(propertyValue.c_str()) && std::atoll(propertyValue.c_str()) < INT64_MAX) {
846         return true;
847     }
848     return false;
849 }
850 
IsNumStr(const std::string & inString)851 bool ProfileUtils::IsNumStr(const std::string& inString)
852 {
853     if (inString.empty()) {
854         HILOGE("inString is empty");
855         return false;
856     }
857     std::regex isNumStrRule(IS_NUMSTRING_RULES);
858     return std::regex_match(inString, isNumStrRule);
859 }
860 
GetIntValue(const ValuesBucket & values,const std::string & property,int32_t & value)861 bool ProfileUtils::GetIntValue(const ValuesBucket& values, const std::string& property, int32_t& value)
862 {
863     ValueObject valueObject;
864     if (values.GetObject(property, valueObject) && valueObject.GetInt(value) == NativeRdb::E_OK) {
865         return true;
866     }
867     return false;
868 }
869 
GetStringValue(const ValuesBucket & values,const std::string & property,std::string & value)870 bool ProfileUtils::GetStringValue(const ValuesBucket& values, const std::string& property, std::string& value)
871 {
872     ValueObject valueObject;
873     if (values.GetObject(property, valueObject) && valueObject.GetString(value) == NativeRdb::E_OK) {
874         return true;
875     }
876     return false;
877 }
878 
GetLongValue(const ValuesBucket & values,const std::string & property,int64_t & value)879 bool ProfileUtils::GetLongValue(const ValuesBucket& values, const std::string& property, int64_t& value)
880 {
881     ValueObject valueObject;
882     if (values.GetObject(property, valueObject) && valueObject.GetLong(value) == NativeRdb::E_OK) {
883         return true;
884     }
885     return false;
886 }
887 
GetDbKeyByProfile(const CharacteristicProfile & profile)888 std::string ProfileUtils::GetDbKeyByProfile(const CharacteristicProfile& profile)
889 {
890     if (profile.GetDeviceId().empty() ||
891         profile.GetServiceName().empty() ||
892         profile.GetCharacteristicKey().empty()) {
893         HILOGE("GetDbKeyByProfile fail");
894         return "";
895     }
896     std::string serviceName = CheckAndAddOhSuffix(profile.GetServiceName(), true);
897     std::string dbKey = CHAR_PREFIX + SEPARATOR + profile.GetDeviceId() + SEPARATOR + serviceName +
898         SEPARATOR + profile.GetCharacteristicKey() + SEPARATOR + CHARACTERISTIC_VALUE;
899     return dbKey;
900 }
901 
GetUserIdFromDbKey(const std::string & dbKey)902 int32_t ProfileUtils::GetUserIdFromDbKey(const std::string& dbKey)
903 {
904     int32_t userId = DEFAULT_USER_ID;
905     std::vector<std::string> splitKeys;
906     if (SplitString(dbKey, SEPARATOR, splitKeys) != DP_SUCCESS || splitKeys.size() < NUM_3) {
907         HILOGE("SplitString fail");
908         return userId;
909     }
910     if (splitKeys[0] == DEV_PREFIX && splitKeys.size() > NUM_3 && IsNumStr(splitKeys[NUM_3])) {
911         userId = std::atoi(splitKeys[NUM_3].c_str());
912     }
913     if (splitKeys[0] == SVR_PREFIX && splitKeys.size() > NUM_4 && IsNumStr(splitKeys[NUM_4])) {
914         userId = std::atoi(splitKeys[NUM_4].c_str());
915     }
916     if (splitKeys[0] == CHAR_PREFIX && splitKeys.size() > NUM_5 && IsNumStr(splitKeys[NUM_5])) {
917         userId = std::atoi(splitKeys[NUM_5].c_str());
918     }
919     return userId;
920 }
921 
RemoveUserIdFromDbKey(const std::string & dbKey)922 std::string ProfileUtils::RemoveUserIdFromDbKey(const std::string& dbKey)
923 {
924     std::vector<std::string> splitKeys;
925     if (SplitString(dbKey, SEPARATOR, splitKeys) != DP_SUCCESS || splitKeys.size() < NUM_3) {
926         HILOGE("SplitString fail");
927         return EMPTY_STRING;
928     }
929     if (splitKeys[0] == DEV_PREFIX && splitKeys.size() > NUM_3 && IsNumStr(splitKeys[NUM_3])) {
930         splitKeys.erase(splitKeys.begin() + NUM_3);
931     }
932     if (splitKeys[0] == SVR_PREFIX && splitKeys.size() > NUM_4 && IsNumStr(splitKeys[NUM_4])) {
933         splitKeys.erase(splitKeys.begin() + NUM_4);
934     }
935     if (splitKeys[0] == CHAR_PREFIX && splitKeys.size() > NUM_5 && IsNumStr(splitKeys[NUM_5])) {
936         splitKeys.erase(splitKeys.begin() + NUM_5);
937     }
938     std::string dbKeyWithoutUserId = JoinString(splitKeys, SEPARATOR);
939     if (dbKeyWithoutUserId.empty()) {
940         HILOGE("JoinString fail");
941         return EMPTY_STRING;
942     }
943     return dbKeyWithoutUserId;
944 }
945 
GenerateServiceDBkeys(const std::string & deviceId,const std::string & serviceName,std::vector<std::string> & dbKeys,bool isMultiUser,int32_t userId)946 int32_t ProfileUtils::GenerateServiceDBkeys(const std::string& deviceId, const std::string& serviceName,
947     std::vector<std::string>& dbKeys, bool isMultiUser, int32_t userId)
948 {
949     std::string localServiceName = CheckAndAddOhSuffix(serviceName, true);
950     std::string serviceProfileKey = GenerateServiceProfileKey(deviceId, localServiceName);
951     // value not need add OH suffix
952     if (isMultiUser) {
953         dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_NAME, userId));
954         dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_TYPE, userId));
955     } else {
956         dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_NAME));
957         dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_TYPE));
958     }
959     return DP_SUCCESS;
960 }
961 
GenerateCharacteristicDBkeys(const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,std::vector<std::string> & dbKeys,bool isMultiUser,int32_t userId)962 int32_t ProfileUtils::GenerateCharacteristicDBkeys(const std::string& deviceId, const std::string& serviceName,
963     const std::string& characteristicKey, std::vector<std::string>& dbKeys, bool isMultiUser, int32_t userId)
964 {
965     std::string localServiceName = CheckAndAddOhSuffix(serviceName, true);
966     std::string charProfileKey = GenerateCharProfileKey(deviceId, localServiceName, characteristicKey);
967     if (isMultiUser) {
968         dbKeys.emplace_back(GenerateDBKey(charProfileKey, CHARACTERISTIC_KEY, userId));
969         dbKeys.emplace_back(GenerateDBKey(charProfileKey, CHARACTERISTIC_VALUE, userId));
970     } else {
971         dbKeys.emplace_back(GenerateDBKey(charProfileKey, CHARACTERISTIC_KEY));
972         dbKeys.emplace_back(GenerateDBKey(charProfileKey, CHARACTERISTIC_VALUE));
973     }
974     return DP_SUCCESS;
975 }
976 
ConvertToTrustDeviceProfile(const AccessControlProfile & accessControlProfile,TrustDeviceProfile & trustDeviceProfile)977 int32_t ProfileUtils::ConvertToTrustDeviceProfile(
978     const AccessControlProfile& accessControlProfile, TrustDeviceProfile& trustDeviceProfile)
979 {
980     trustDeviceProfile.SetDeviceId(accessControlProfile.GetTrustDeviceId());
981     trustDeviceProfile.SetDeviceIdType(accessControlProfile.GetDeviceIdType());
982     trustDeviceProfile.SetDeviceIdHash(accessControlProfile.GetDeviceIdHash());
983     trustDeviceProfile.SetStatus(accessControlProfile.GetStatus());
984     trustDeviceProfile.SetBindType(accessControlProfile.GetBindType());
985     std::string peerDeviceId = accessControlProfile.GetTrustDeviceId();
986     int32_t peerUserId = accessControlProfile.GetAccesser().GetAccesserUserId();
987     int32_t localUserId = accessControlProfile.GetAccessee().GetAccesseeUserId();
988     if (accessControlProfile.GetAccessee().GetAccesseeDeviceId() == peerDeviceId) {
989         peerUserId = accessControlProfile.GetAccessee().GetAccesseeUserId();
990         localUserId = accessControlProfile.GetAccesser().GetAccesserUserId();
991     }
992     trustDeviceProfile.SetPeerUserId(peerUserId);
993     trustDeviceProfile.SetLocalUserId(localUserId);
994     return DP_SUCCESS;
995 }
996 
ConvertToAccessControlProfiles(std::shared_ptr<ResultSet> resultSet,std::shared_ptr<ResultSet> accesserResultSet,std::shared_ptr<ResultSet> accesseeResultSet,std::vector<AccessControlProfile> & profile)997 int32_t ProfileUtils::ConvertToAccessControlProfiles(std::shared_ptr<ResultSet> resultSet,
998     std::shared_ptr<ResultSet> accesserResultSet, std::shared_ptr<ResultSet> accesseeResultSet,
999     std::vector<AccessControlProfile>& profile)
1000 {
1001     if (accesserResultSet == nullptr) {
1002         HILOGE("accesserResultSet is nullptr");
1003         return DP_GET_RESULTSET_FAIL;
1004     }
1005     if (accesseeResultSet == nullptr) {
1006         HILOGE("accesseeResultSet is nullptr");
1007         return DP_GET_RESULTSET_FAIL;
1008     }
1009     Accesser accesser;
1010     accesserResultSet->GoToNextRow();
1011     ConvertToAccesser(accesserResultSet, accesser);
1012     Accessee accessee;
1013     accesseeResultSet->GoToNextRow();
1014     ConvertToAccessee(accesseeResultSet, accessee);
1015     if (resultSet == nullptr) {
1016         HILOGE("resultSet is nullptr");
1017         return DP_GET_RESULTSET_FAIL;
1018     }
1019     AccessControlProfile accessControlProfile;
1020     ConvertToAccessControlProfile(resultSet, accessControlProfile);
1021 
1022     accessControlProfile.SetAccesser(accesser);
1023     accessControlProfile.SetAccessee(accessee);
1024     profile.push_back(accessControlProfile);
1025     return DP_SUCCESS;
1026 }
1027 
ConvertToTrustDeviceProfile(std::shared_ptr<ResultSet> trustResultSet,TrustDeviceProfile & trustDeviceProfile)1028 int32_t ProfileUtils::ConvertToTrustDeviceProfile(
1029     std::shared_ptr<ResultSet> trustResultSet, TrustDeviceProfile& trustDeviceProfile)
1030 {
1031     if (trustResultSet == nullptr) {
1032         HILOGE("trustResultSet is nullptr");
1033         return DP_GET_RESULTSET_FAIL;
1034     }
1035     RowEntity rowEntity;
1036     if (trustResultSet->GetRow(rowEntity) != DP_SUCCESS) {
1037         HILOGE("get trustResultSet failed");
1038         return DP_GET_RESULTSET_FAIL;
1039     }
1040     std::string deviceId = rowEntity.Get(DEVICE_ID);
1041     int32_t deviceIdType = rowEntity.Get(DEVICE_ID_TYPE);
1042     std::string deviceIdHash = rowEntity.Get(DEVICE_ID_HASH);
1043     int32_t status = rowEntity.Get(STATUS);
1044 
1045     trustDeviceProfile.SetDeviceId(deviceId);
1046     trustDeviceProfile.SetDeviceIdType(deviceIdType);
1047     trustDeviceProfile.SetDeviceIdHash(deviceIdHash);
1048     trustDeviceProfile.SetStatus(status);
1049     return DP_SUCCESS;
1050 }
1051 
ConvertToAccesser(std::shared_ptr<ResultSet> accesserResultSet,Accesser & accesser)1052 int32_t ProfileUtils::ConvertToAccesser(std::shared_ptr<ResultSet> accesserResultSet,
1053     Accesser& accesser)
1054 {
1055     if (accesserResultSet == nullptr) {
1056         HILOGE("accesserResultSet is nullptr");
1057         return DP_GET_RESULTSET_FAIL;
1058     }
1059     RowEntity rowEntity;
1060     if (accesserResultSet->GetRow(rowEntity) != DP_SUCCESS) {
1061         HILOGE("get accesserResultSet failed");
1062         return DP_GET_RESULTSET_FAIL;
1063     }
1064     int64_t accesserId = rowEntity.Get(ACCESSER_ID);
1065     std::string accesserDeviceId = rowEntity.Get(ACCESSER_DEVICE_ID);
1066     int32_t accesserUserId = rowEntity.Get(ACCESSER_USER_ID);
1067     std::string accesserAccountId = rowEntity.Get(ACCESSER_ACCOUNT_ID);
1068     int64_t accesserTokenId = rowEntity.Get(ACCESSER_TOKEN_ID);
1069     std::string accesserBundleName = rowEntity.Get(ACCESSER_BUNDLE_NAME);
1070     std::string accesserHapSignature = rowEntity.Get(ACCESSER_HAP_SIGNATURE);
1071     int32_t accesserBindLevel = rowEntity.Get(ACCESSER_BIND_LEVEL);
1072     std::string accesserDeviceName = rowEntity.Get(ACCESSER_DEVICE_NAME);
1073     std::string accesserServiceName = rowEntity.Get(ACCESSER_SERVICE_NAME);
1074     int32_t accesserCredentialId = rowEntity.Get(ACCESSER_CREDENTIAL_ID);
1075     int32_t accesserStatus = rowEntity.Get(ACCESSER_STATUS);
1076     int32_t accesserSessionKeyId = rowEntity.Get(ACCESSER_SESSION_KEY_ID);
1077     int64_t accesserSKTimeStamp = rowEntity.Get(ACCESSER_SESSION_KEY_TIMESTAMP);
1078 
1079     accesser.SetAccesserId(accesserId);
1080     accesser.SetAccesserDeviceId(accesserDeviceId);
1081     accesser.SetAccesserUserId(accesserUserId);
1082     accesser.SetAccesserAccountId(accesserAccountId);
1083     accesser.SetAccesserTokenId(accesserTokenId);
1084     accesser.SetAccesserBundleName(accesserBundleName);
1085     accesser.SetAccesserHapSignature(accesserHapSignature);
1086     accesser.SetAccesserBindLevel(accesserBindLevel);
1087     accesser.SetAccesserDeviceName(accesserDeviceName);
1088     accesser.SetAccesserServiceName(accesserServiceName);
1089     accesser.SetAccesserCredentialId(accesserCredentialId);
1090     accesser.SetAccesserStatus(accesserStatus);
1091     accesser.SetAccesserSessionKeyId(accesserSessionKeyId);
1092     accesser.SetAccesserSKTimeStamp(accesserSKTimeStamp);
1093     return DP_SUCCESS;
1094 }
1095 
ConvertToAccessee(std::shared_ptr<ResultSet> accesseeResultSet,Accessee & accessee)1096 int32_t ProfileUtils::ConvertToAccessee(std::shared_ptr<ResultSet> accesseeResultSet,
1097     Accessee& accessee)
1098 {
1099     if (accesseeResultSet == nullptr) {
1100         HILOGE("accesseeResultSet is nullptr");
1101         return DP_GET_RESULTSET_FAIL;
1102     }
1103     RowEntity rowEntity;
1104     if (accesseeResultSet->GetRow(rowEntity) != DP_SUCCESS) {
1105         HILOGE("get accesseeResultSet failed");
1106         return DP_GET_RESULTSET_FAIL;
1107     }
1108     int64_t accesseeId = rowEntity.Get(ACCESSEE_ID);
1109     std::string accesseeDeviceId = rowEntity.Get(ACCESSEE_DEVICE_ID);
1110     int32_t accesseeUserId = rowEntity.Get(ACCESSEE_USER_ID);
1111     std::string accesseeAccountId = rowEntity.Get(ACCESSEE_ACCOUNT_ID);
1112     int64_t accesseeTokenId = rowEntity.Get(ACCESSEE_TOKEN_ID);
1113     std::string accesseeBundleName = rowEntity.Get(ACCESSEE_BUNDLE_NAME);
1114     std::string accesseeHapSignature = rowEntity.Get(ACCESSEE_HAP_SIGNATURE);
1115     int32_t accesseeBindLevel = rowEntity.Get(ACCESSEE_BIND_LEVEL);
1116     std::string accesseeDeviceName = rowEntity.Get(ACCESSEE_DEVICE_NAME);
1117     std::string accesseeServiceName = rowEntity.Get(ACCESSEE_SERVICE_NAME);
1118     int32_t accesseeCredentialId = rowEntity.Get(ACCESSEE_CREDENTIAL_ID);
1119     int32_t accesseeStatus = rowEntity.Get(ACCESSEE_STATUS);
1120     int32_t accesseeSessionKeyId = rowEntity.Get(ACCESSEE_SESSION_KEY_ID);
1121     int64_t accesseeSKTimeStamp = rowEntity.Get(ACCESSEE_SESSION_KEY_TIMESTAMP);
1122 
1123     accessee.SetAccesseeId(accesseeId);
1124     accessee.SetAccesseeDeviceId(accesseeDeviceId);
1125     accessee.SetAccesseeUserId(accesseeUserId);
1126     accessee.SetAccesseeAccountId(accesseeAccountId);
1127     accessee.SetAccesseeTokenId(accesseeTokenId);
1128     accessee.SetAccesseeBundleName(accesseeBundleName);
1129     accessee.SetAccesseeHapSignature(accesseeHapSignature);
1130     accessee.SetAccesseeBindLevel(accesseeBindLevel);
1131     accessee.SetAccesseeDeviceName(accesseeDeviceName);
1132     accessee.SetAccesseeServiceName(accesseeServiceName);
1133     accessee.SetAccesseeCredentialId(accesseeCredentialId);
1134     accessee.SetAccesseeStatus(accesseeStatus);
1135     accessee.SetAccesseeSessionKeyId(accesseeSessionKeyId);
1136     accessee.SetAccesseeSKTimeStamp(accesseeSKTimeStamp);
1137     return DP_SUCCESS;
1138 }
1139 
ConvertToAccessControlProfile(std::shared_ptr<ResultSet> accessControlResultSet,AccessControlProfile & accessControlProfile)1140 int32_t ProfileUtils::ConvertToAccessControlProfile(
1141     std::shared_ptr<ResultSet> accessControlResultSet, AccessControlProfile& accessControlProfile)
1142 {
1143     if (accessControlResultSet == nullptr) {
1144         HILOGE("accessControlResultSet is nullptr");
1145         return DP_GET_RESULTSET_FAIL;
1146     }
1147     RowEntity rowEntity;
1148     if (accessControlResultSet->GetRow(rowEntity) != DP_SUCCESS) {
1149         HILOGE("get accessControlResultSet failed");
1150         return DP_GET_RESULTSET_FAIL;
1151     }
1152     int64_t accessControlId = rowEntity.Get(ACCESS_CONTROL_ID);
1153     int64_t accesserId = rowEntity.Get(ACCESSER_ID);
1154     int64_t accesseeId = rowEntity.Get(ACCESSEE_ID);
1155     std::string trustDeviceId = rowEntity.Get(TRUST_DEVICE_ID);
1156     std::string sessionKey = rowEntity.Get(SESSION_KEY);
1157     int32_t bindType = rowEntity.Get(BIND_TYPE);
1158     int32_t authenticationType = rowEntity.Get(AUTHENTICATION_TYPE);
1159     int32_t deviceIdType = rowEntity.Get(DEVICE_ID_TYPE);
1160     std::string deviceIdHash = rowEntity.Get(DEVICE_ID_HASH);
1161     int32_t status = rowEntity.Get(STATUS);
1162     int32_t validPeriod = rowEntity.Get(VALID_PERIOD);
1163     int32_t lastAuthTime = rowEntity.Get(LAST_AUTH_TIME);
1164     int32_t bindLevel = rowEntity.Get(BIND_LEVEL);
1165 
1166     accessControlProfile.SetAccessControlId(accessControlId);
1167     accessControlProfile.SetAccesserId(accesserId);
1168     accessControlProfile.SetAccesseeId(accesseeId);
1169     accessControlProfile.SetTrustDeviceId(trustDeviceId);
1170     accessControlProfile.SetSessionKey(sessionKey);
1171     accessControlProfile.SetBindType(bindType);
1172     accessControlProfile.SetAuthenticationType(authenticationType);
1173     accessControlProfile.SetDeviceIdType(deviceIdType);
1174     accessControlProfile.SetDeviceIdHash(deviceIdHash);
1175     accessControlProfile.SetStatus(status);
1176     accessControlProfile.SetValidPeriod(validPeriod);
1177     accessControlProfile.SetLastAuthTime(lastAuthTime);
1178     accessControlProfile.SetBindLevel(bindLevel);
1179     return DP_SUCCESS;
1180 }
1181 } // namespace DistributedDeviceProfile
1182 } // namespace OHOS
1183