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 the rest 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 ptr_cur++;
43 continue;
44 }
45 for (i = 0; i < NUMBER; i++) {
46 if (strncmp(ptr_cur, g_property[i], g_property_len[i]) != 0)
47 continue;
48
49 /* add 1 is for to copy char '%' */
50 ret = strncpy_s(&dest[index], size - index, ptr, ptr_cur - ptr + 1);
51 if (ret != EOK) {
52 printk("%s strncpy_s error %d", __func__, ret);
53 return false;
54 }
55 index += (ptr_cur - ptr + 1);
56 ptr = ptr_cur + g_property_len[i];
57 ptr_cur = ptr;
58 break;
59 }
60 if (i == NUMBER)
61 ptr_cur++;
62 }
63 ret = strcat_s(&dest[index], size - index, ptr);
64 if (ret != EOK) {
65 printk("%s strcat_s error %d", __func__, ret);
66 return false;
67 }
68 return true;
69 }
70 EXPORT_SYMBOL(deal_format);
71