• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
3  * Licensed under Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *          http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  * Description: Define macro, enum, data struct, and declare internal used function
12  *              prototype, which is used by output.inl, secureprintoutput_w.c and
13  *              secureprintoutput_a.c.
14  * Create: 2014-02-25
15  */
16 
17 #ifndef SECUREPRINTOUTPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C
18 #define SECUREPRINTOUTPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C
19 #include "securecutil.h"
20 
21 /* Shield compilation alerts about using sprintf without format attribute to format float value. */
22 #ifndef SECUREC_HANDLE_WFORMAT
23 #define SECUREC_HANDLE_WFORMAT 1
24 #endif
25 
26 #if SECUREC_HANDLE_WFORMAT && defined(__GNUC__) && ((__GNUC__ >= 5) || \
27     (defined(__GNUC_MINOR__) && (__GNUC__ == 4 && __GNUC_MINOR__ > 7)))
28 #if defined(__clang__)
29 #define SECUREC_MASK_WFORMAT_WARNING  _Pragma("GCC diagnostic push") \
30     _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
31 #else
32 #define SECUREC_MASK_WFORMAT_WARNING  _Pragma("GCC diagnostic push") \
33     _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"") \
34     _Pragma("GCC diagnostic ignored \"-Wmissing-format-attribute\"") \
35     _Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
36 #endif
37 #define SECUREC_END_MASK_WFORMAT_WARNING  _Pragma("GCC diagnostic pop")
38 #else
39 #define SECUREC_MASK_WFORMAT_WARNING
40 #define SECUREC_END_MASK_WFORMAT_WARNING
41 #endif
42 
43 #define SECUREC_MASK_VSPRINTF_WARNING  SECUREC_MASK_WFORMAT_WARNING \
44     SECUREC_MASK_MSVC_CRT_WARNING
45 
46 #define SECUREC_END_MASK_VSPRINTF_WARNING  SECUREC_END_MASK_WFORMAT_WARNING \
47     SECUREC_END_MASK_MSVC_CRT_WARNING
48 
49 /*
50  * Flag definitions.
51  * Using macros instead of enumerations is because some of the enumerated types under the compiler are 16bit.
52  */
53 #define SECUREC_FLAG_SIGN           0x00001U
54 #define SECUREC_FLAG_SIGN_SPACE     0x00002U
55 #define SECUREC_FLAG_LEFT           0x00004U
56 #define SECUREC_FLAG_LEADZERO       0x00008U
57 #define SECUREC_FLAG_LONG           0x00010U
58 #define SECUREC_FLAG_SHORT          0x00020U
59 #define SECUREC_FLAG_SIGNED         0x00040U
60 #define SECUREC_FLAG_ALTERNATE      0x00080U
61 #define SECUREC_FLAG_NEGATIVE       0x00100U
62 #define SECUREC_FLAG_FORCE_OCTAL    0x00200U
63 #define SECUREC_FLAG_LONG_DOUBLE    0x00400U
64 #define SECUREC_FLAG_WIDECHAR       0x00800U
65 #define SECUREC_FLAG_LONGLONG       0x01000U
66 #define SECUREC_FLAG_CHAR           0x02000U
67 #define SECUREC_FLAG_POINTER        0x04000U
68 #define SECUREC_FLAG_I64            0x08000U
69 #define SECUREC_FLAG_PTRDIFF        0x10000U
70 #define SECUREC_FLAG_SIZE           0x20000U
71 #ifdef  SECUREC_COMPATIBLE_LINUX_FORMAT
72 #define SECUREC_FLAG_INTMAX         0x40000U
73 #endif
74 
75 /* State definitions. Identify the status of the current format */
76 typedef enum {
77     STAT_NORMAL,
78     STAT_PERCENT,
79     STAT_FLAG,
80     STAT_WIDTH,
81     STAT_DOT,
82     STAT_PRECIS,
83     STAT_SIZE,
84     STAT_TYPE,
85     STAT_INVALID
86 } SecFmtState;
87 
88 #ifndef SECUREC_BUFFER_SIZE
89 #if SECUREC_IN_KERNEL
90 #define SECUREC_BUFFER_SIZE    32
91 #elif defined(SECUREC_STACK_SIZE_LESS_THAN_1K)
92 /*
93  * SECUREC BUFFER SIZE Can not be less than 23
94  * The length of the octal representation of 64-bit integers with zero lead
95  */
96 #define SECUREC_BUFFER_SIZE    256
97 #else
98 #define SECUREC_BUFFER_SIZE    512
99 #endif
100 #endif
101 #if SECUREC_BUFFER_SIZE < 23
102 #error SECUREC_BUFFER_SIZE Can not be less than 23
103 #endif
104 /* Buffer size for wchar, use 4 to make the compiler aligns as 8 bytes as possible */
105 #define SECUREC_WCHAR_BUFFER_SIZE 4
106 
107 #define SECUREC_MAX_PRECISION  SECUREC_BUFFER_SIZE
108 /* Max. # bytes in multibyte char,see MB_LEN_MAX */
109 #define SECUREC_MB_LEN 16
110 /* The return value of the internal function, which is returned when truncated */
111 #define SECUREC_PRINTF_TRUNCATE (-2)
112 
113 #define SECUREC_VSPRINTF_PARAM_ERROR(format, strDest, destMax, maxLimit) \
114     ((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit))
115 
116 #define SECUREC_VSPRINTF_CLEAR_DEST(strDest, destMax, maxLimit) do { \
117     if ((strDest) != NULL && (destMax) > 0 && (destMax) <= (maxLimit)) { \
118         *(strDest) = '\0'; \
119     } \
120 } SECUREC_WHILE_ZERO
121 
122 #ifdef SECUREC_COMPATIBLE_WIN_FORMAT
123 #define SECUREC_VSNPRINTF_PARAM_ERROR(format, strDest, destMax, count, maxLimit) \
124     (((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) || \
125     ((count) > (SECUREC_STRING_MAX_LEN - 1) && (count) != (size_t)(-1)))
126 
127 #else
128 #define SECUREC_VSNPRINTF_PARAM_ERROR(format, strDest, destMax, count, maxLimit) \
129     (((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) || \
130     ((count) > (SECUREC_STRING_MAX_LEN - 1)))
131 #endif
132 
133 #ifdef __cplusplus
134 extern "C" {
135 #endif
136 #ifdef SECUREC_FOR_WCHAR
137 int SecVswprintfImpl(wchar_t *string, size_t count, const wchar_t *format, va_list argList);
138 #else
139 int SecVsnprintfImpl(char *string, size_t count, const char *format, va_list argList);
140 #endif
141 #ifdef __cplusplus
142 }
143 #endif
144 
145 #endif
146 
147