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