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 defined(__clang__) 27 #if SECUREC_HANDLE_WFORMAT && defined(__GNUC__) && ((__GNUC__ >= 5) || \ 28 (defined(__GNUC_MINOR__) && (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))) 29 #define SECUREC_MASK_WFORMAT_WARNING _Pragma("GCC diagnostic push") \ 30 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"") 31 #define SECUREC_END_MASK_WFORMAT_WARNING _Pragma("GCC diagnostic pop") 32 #else 33 #define SECUREC_MASK_WFORMAT_WARNING 34 #define SECUREC_END_MASK_WFORMAT_WARNING 35 #endif 36 #else 37 #if SECUREC_HANDLE_WFORMAT && defined(__GNUC__) && ((__GNUC__ >= 5 ) || \ 38 (defined(__GNUC_MINOR__) && (__GNUC__ == 4 && __GNUC_MINOR__ > 7))) 39 #define SECUREC_MASK_WFORMAT_WARNING _Pragma("GCC diagnostic push") \ 40 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"") \ 41 _Pragma("GCC diagnostic ignored \"-Wmissing-format-attribute\"") \ 42 _Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"") 43 #define SECUREC_END_MASK_WFORMAT_WARNING _Pragma("GCC diagnostic pop") 44 #else 45 #define SECUREC_MASK_WFORMAT_WARNING 46 #define SECUREC_END_MASK_WFORMAT_WARNING 47 #endif 48 #endif 49 50 #define SECUREC_MASK_VSPRINTF_WARNING SECUREC_MASK_WFORMAT_WARNING \ 51 SECUREC_MASK_MSVC_CRT_WARNING 52 53 #define SECUREC_END_MASK_VSPRINTF_WARNING SECUREC_END_MASK_WFORMAT_WARNING \ 54 SECUREC_END_MASK_MSVC_CRT_WARNING 55 56 /* 57 * Flag definitions. 58 * Using macros instead of enumerations is because some of the enumerated types under the compiler are 16bit. 59 */ 60 #define SECUREC_FLAG_SIGN 0x00001U 61 #define SECUREC_FLAG_SIGN_SPACE 0x00002U 62 #define SECUREC_FLAG_LEFT 0x00004U 63 #define SECUREC_FLAG_LEADZERO 0x00008U 64 #define SECUREC_FLAG_LONG 0x00010U 65 #define SECUREC_FLAG_SHORT 0x00020U 66 #define SECUREC_FLAG_SIGNED 0x00040U 67 #define SECUREC_FLAG_ALTERNATE 0x00080U 68 #define SECUREC_FLAG_NEGATIVE 0x00100U 69 #define SECUREC_FLAG_FORCE_OCTAL 0x00200U 70 #define SECUREC_FLAG_LONG_DOUBLE 0x00400U 71 #define SECUREC_FLAG_WIDECHAR 0x00800U 72 #define SECUREC_FLAG_LONGLONG 0x01000U 73 #define SECUREC_FLAG_CHAR 0x02000U 74 #define SECUREC_FLAG_POINTER 0x04000U 75 #define SECUREC_FLAG_I64 0x08000U 76 #define SECUREC_FLAG_PTRDIFF 0x10000U 77 #define SECUREC_FLAG_SIZE 0x20000U 78 #ifdef SECUREC_COMPATIBLE_LINUX_FORMAT 79 #define SECUREC_FLAG_INTMAX 0x40000U 80 #endif 81 82 /* State definitions. Identify the status of the current format */ 83 typedef enum { 84 STAT_NORMAL, 85 STAT_PERCENT, 86 STAT_FLAG, 87 STAT_WIDTH, 88 STAT_DOT, 89 STAT_PRECIS, 90 STAT_SIZE, 91 STAT_TYPE, 92 STAT_INVALID 93 } SecFmtState; 94 95 #ifndef SECUREC_BUFFER_SIZE 96 #if SECUREC_IN_KERNEL 97 #define SECUREC_BUFFER_SIZE 32 98 #elif defined(SECUREC_STACK_SIZE_LESS_THAN_1K) 99 /* 100 * SECUREC BUFFER SIZE Can not be less than 23 101 * The length of the octal representation of 64-bit integers with zero lead 102 */ 103 #define SECUREC_BUFFER_SIZE 256 104 #else 105 #define SECUREC_BUFFER_SIZE 512 106 #endif 107 #endif 108 #if SECUREC_BUFFER_SIZE < 23 109 #error SECUREC_BUFFER_SIZE Can not be less than 23 110 #endif 111 /* Buffer size for wchar, use 4 to make the compiler aligns as 8 bytes as possible */ 112 #define SECUREC_WCHAR_BUFFER_SIZE 4 113 114 #define SECUREC_MAX_PRECISION SECUREC_BUFFER_SIZE 115 /* Max. # bytes in multibyte char,see MB_LEN_MAX */ 116 #define SECUREC_MB_LEN 16 117 /* The return value of the internal function, which is returned when truncated */ 118 #define SECUREC_PRINTF_TRUNCATE (-2) 119 120 #define SECUREC_VSPRINTF_PARAM_ERROR(format, strDest, destMax, maxLimit) \ 121 ((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) 122 123 #define SECUREC_VSPRINTF_CLEAR_DEST(strDest, destMax, maxLimit) do { \ 124 if ((strDest) != NULL && (destMax) > 0 && (destMax) <= (maxLimit)) { \ 125 *(strDest) = '\0'; \ 126 } \ 127 } SECUREC_WHILE_ZERO 128 129 #ifdef SECUREC_COMPATIBLE_WIN_FORMAT 130 #define SECUREC_VSNPRINTF_PARAM_ERROR(format, strDest, destMax, count, maxLimit) \ 131 (((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) || \ 132 ((count) > (SECUREC_STRING_MAX_LEN - 1) && (count) != (size_t)(-1))) 133 134 #else 135 #define SECUREC_VSNPRINTF_PARAM_ERROR(format, strDest, destMax, count, maxLimit) \ 136 (((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) || \ 137 ((count) > (SECUREC_STRING_MAX_LEN - 1))) 138 #endif 139 140 #ifdef __cplusplus 141 extern "C" { 142 #endif 143 #ifdef SECUREC_FOR_WCHAR 144 int SecVswprintfImpl(wchar_t *string, size_t count, const wchar_t *format, va_list argList); 145 #else 146 int SecVsnprintfImpl(char *string, size_t count, const char *format, va_list argList); 147 #endif 148 #ifdef __cplusplus 149 } 150 #endif 151 152 #endif 153 154