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 "av_trans_log.h"
17 
18 #include "securec.h"
19 
20 #ifdef HI_LOG_ENABLE
21 #include "hilog/log.h"
22 #else
23 #include <cstdio>
24 #endif
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 namespace {
29     constexpr size_t INT32_SHORT_ID_LENGTH = 20;
30     constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
31     constexpr size_t INT32_MIN_ID_LENGTH = 3;
32     constexpr int32_t INT32_STRING_LENGTH = 40;
33 }
GetAnonyString(const std::string & value)34 std::string GetAnonyString(const std::string &value)
35 {
36     std::string res;
37     std::string tmpStr("******");
38     size_t strLen = value.length();
39     if (strLen < INT32_MIN_ID_LENGTH) {
40         return tmpStr;
41     }
42 
43     if (strLen <= INT32_SHORT_ID_LENGTH) {
44         res += value[0];
45         res += tmpStr;
46         res += value[strLen - 1];
47     } else {
48         res.append(value, 0, INT32_PLAINTEXT_LENGTH);
49         res += tmpStr;
50         res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
51     }
52 
53     return res;
54 }
55 
GetAnonyInt32(const int32_t value)56 std::string GetAnonyInt32(const int32_t value)
57 {
58     char tempBuffer[INT32_STRING_LENGTH] = "";
59     int32_t secRet = sprintf_s(tempBuffer, INT32_STRING_LENGTH, "%d", value);
60     if (secRet <= 0) {
61         std::string nullString("");
62         return nullString;
63     }
64     size_t length = strlen(tempBuffer);
65     if (length <= 1) {
66         std::string nullString("");
67         return nullString;
68     }
69     for (size_t i = 1; i <= length - 1; i++) {
70         tempBuffer[i] = '*';
71     }
72     if (length == 0x01) {
73         tempBuffer[0] = '*';
74     }
75 
76     std::string tempString(tempBuffer);
77     return tempString;
78 }
79 } // namespace DistributedHardware
80 } // namespace OHOS