1 /*
2  * Copyright (C) 2021-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 "log_helper.h"
17 #include <securec.h>
18 #include <stdio.h>
19 #include <string.h>
20 
21 const int MAX_REMAIN_CHARACTER_NUM = 3;
22 
EncryptLogMsg(const char * srcMsg,char * desMsg,int desLen)23 void EncryptLogMsg(const char *srcMsg, char *desMsg, int desLen)
24 {
25     if (desMsg == NULL || desLen <= 0) {
26         return;
27     }
28     if (srcMsg == NULL || strlen(srcMsg) == 0) {
29         desMsg[0] = '\0';
30         return;
31     }
32     int srcLen = strlen(srcMsg);
33     if (strncpy_s(desMsg, desLen, srcMsg, srcLen) != EOK) {
34         desMsg[0] = '\0';
35         return;
36     }
37     desMsg[desLen - 1] = '\0';
38     int i = 0;
39     int j = (srcLen > (desLen - 1)) ? (desLen - 1) : srcLen;
40     j -= 1;
41     int k = MAX_REMAIN_CHARACTER_NUM;
42     while (k > 0 && i < j) {
43         ++i;
44         --j;
45         --k;
46     }
47     if (k == 0) {
48         if (i > j) {
49             desMsg[i] = '*';
50             desMsg[j] = '*';
51         }
52         while (i <= j) {
53             desMsg[i] = '*';
54             ++i;
55         }
56     } else {
57         if (i > j) {
58             desMsg[i] = '*';
59             desMsg[j] = '*';
60         } else {
61             desMsg[i] = '*';
62         }
63     }
64     return;
65 }
66