1 /*
2  * Copyright (c) 2021-2024 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 "device_profile_utils.h"
17 
18 #include <list>
19 #include <string>
20 
21 #include "parcel.h"
22 #include "parcel_helper.h"
23 
24 namespace OHOS {
25 namespace DeviceProfile {
26 namespace {
27 const std::string TAG = "DeviceProfileUtils";
28 
29 constexpr int32_t NON_ANONYMIZED_LENGTH = 6;
30 constexpr uint32_t MAX_EVENT_LEN = 1000000;
31 const std::string EMPTY_DEVICE_ID = "";
32 }
33 
WriteProfileEvents(const std::list<ProfileEvent> & profileEvents,Parcel & parcel)34 bool DeviceProfileUtils::WriteProfileEvents(const std::list<ProfileEvent>& profileEvents, Parcel& parcel)
35 {
36     size_t size = profileEvents.size();
37     PARCEL_WRITE_HELPER_RET(parcel, Uint32, static_cast<uint32_t>(size), false);
38     for (auto profileEvent : profileEvents) {
39         PARCEL_WRITE_HELPER_RET(parcel, Uint32, static_cast<uint32_t>(profileEvent), false);
40     }
41     return true;
42 }
43 
ReadProfileEvents(Parcel & parcel,std::list<ProfileEvent> & profileEvents)44 bool DeviceProfileUtils::ReadProfileEvents(Parcel& parcel, std::list<ProfileEvent>& profileEvents)
45 {
46     uint32_t numEvents = parcel.ReadUint32();
47     if (numEvents > MAX_EVENT_LEN) {
48         return false;
49     }
50 
51     for (uint32_t i = 0; i < numEvents; i++) {
52         ProfileEvent profileEvent = static_cast<ProfileEvent>(parcel.ReadUint32());
53         if (profileEvent >= EVENT_PROFILE_END || profileEvent == EVENT_UNKNOWN) {
54             return false;
55         }
56         profileEvents.emplace_back(profileEvent);
57     }
58     return true;
59 }
60 
AnonymizeDeviceId(const std::string & deviceId)61 std::string DeviceProfileUtils::AnonymizeDeviceId(const std::string& deviceId)
62 {
63     if (deviceId.length() < NON_ANONYMIZED_LENGTH) {
64         return EMPTY_DEVICE_ID;
65     }
66     std::string anonDeviceId = deviceId.substr(0, NON_ANONYMIZED_LENGTH);
67     anonDeviceId.append("******");
68     return anonDeviceId;
69 }
70 
AnonymizeString(const std::string & value)71 std::string DeviceProfileUtils::AnonymizeString(const std::string& value)
72 {
73     constexpr size_t INT32_SHORT_ID_LENGTH = 20;
74     constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
75     constexpr size_t INT32_MIN_ID_LENGTH = 3;
76     std::string res;
77     std::string tmpStr("******");
78     size_t strLen = value.length();
79     if (strLen < INT32_MIN_ID_LENGTH) {
80         return tmpStr;
81     }
82 
83     if (strLen <= INT32_SHORT_ID_LENGTH) {
84         res += value[0];
85         res += tmpStr;
86         res += value[strLen - 1];
87     } else {
88         res.append(value, 0, INT32_PLAINTEXT_LENGTH);
89         res += tmpStr;
90         res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
91     }
92     return res;
93 }
94 } // namespace DeviceProfile
95 } // namespace OHOS
96