1 /*
2 * osal_deal_log_format.c
3 *
4 * osal driver
5 *
6 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19 #include <linux/printk.h>
20 #include "securec.h"
21
22 #define NUMBER 2
23 static const char *g_property[NUMBER] = {"%{private}", "%{public}"};
24 static size_t g_property_len[NUMBER] = {10, 9};
25
26 /* remove "{private}" and "{public}" from fmt and copy to dest */
deal_format(const char * fmt,char * dest,size_t size)27 bool deal_format(const char *fmt, char *dest, size_t size)
28 {
29 const char *ptr = fmt;
30 const char *ptr_cur = ptr;
31 errno_t ret;
32 size_t index = 0;
33 size_t i;
34
35 if (fmt == NULL || dest == NULL || size == 0 || strlen(fmt) >= (size - 1)) {
36 printk("%s invalid para", __func__);
37 return false;
38 }
39
40 while (ptr_cur != NULL && *ptr_cur != '\0') {
41 if (*ptr_cur == '%') {
42 for (i = 0; i < NUMBER; i++) {
43 if (strncmp(ptr_cur, g_property[i], g_property_len[i]) == 0) {
44 /* add 1 is for to copy char '%' */
45 ret = strncpy_s(&dest[index], size - index, ptr, ptr_cur - ptr + 1);
46 if (ret != EOK) {
47 printk("%s strncpy_s error %d", __func__, ret);
48 return false;
49 }
50 index += (ptr_cur - ptr + 1);
51 ptr = ptr_cur + g_property_len[i];
52 ptr_cur = ptr;
53 break;
54 }
55 }
56 if (i == NUMBER)
57 ptr_cur++;
58 } else {
59 ptr_cur++;
60 }
61 }
62 ret = strcat_s(&dest[index], size - index, ptr);
63 if (ret != EOK) {
64 printk("%s strcat_s error %d", __func__, ret);
65 return false;
66 }
67 return true;
68 }
69 EXPORT_SYMBOL(deal_format);