1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2014-2020. 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 * Author: lishunda 15 * Create: 2014-02-25 16 */ 17 18 #ifndef SECUREPRINTOUTPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C 19 #define SECUREPRINTOUTPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C 20 #include "securecutil.h" 21 22 /* 23 * Flag definitions. 24 * Using macros instead of enumerations is because some of the enumerated types under the compiler are 16bit. 25 */ 26 #define SECUREC_FLAG_SIGN 0x00001U 27 #define SECUREC_FLAG_SIGN_SPACE 0x00002U 28 #define SECUREC_FLAG_LEFT 0x00004U 29 #define SECUREC_FLAG_LEADZERO 0x00008U 30 #define SECUREC_FLAG_LONG 0x00010U 31 #define SECUREC_FLAG_SHORT 0x00020U 32 #define SECUREC_FLAG_SIGNED 0x00040U 33 #define SECUREC_FLAG_ALTERNATE 0x00080U 34 #define SECUREC_FLAG_NEGATIVE 0x00100U 35 #define SECUREC_FLAG_FORCE_OCTAL 0x00200U 36 #define SECUREC_FLAG_LONG_DOUBLE 0x00400U 37 #define SECUREC_FLAG_WIDECHAR 0x00800U 38 #define SECUREC_FLAG_LONGLONG 0x01000U 39 #define SECUREC_FLAG_CHAR 0x02000U 40 #define SECUREC_FLAG_POINTER 0x04000U 41 #define SECUREC_FLAG_I64 0x08000U 42 #define SECUREC_FLAG_PTRDIFF 0x10000U 43 #define SECUREC_FLAG_SIZE 0x20000U 44 #ifdef SECUREC_COMPATIBLE_LINUX_FORMAT 45 #define SECUREC_FLAG_INTMAX 0x40000U 46 #endif 47 48 /* State definitions. Identify the status of the current format */ 49 typedef enum { 50 STAT_NORMAL, 51 STAT_PERCENT, 52 STAT_FLAG, 53 STAT_WIDTH, 54 STAT_DOT, 55 STAT_PRECIS, 56 STAT_SIZE, 57 STAT_TYPE, 58 STAT_INVALID 59 } SecFmtState; 60 61 /* Format output buffer pointer and available size */ 62 typedef struct { 63 int count; 64 char *cur; 65 } SecPrintfStream; 66 67 #ifndef SECUREC_BUFFER_SIZE 68 #if SECUREC_IN_KERNEL 69 #define SECUREC_BUFFER_SIZE 32 70 #elif defined(SECUREC_STACK_SIZE_LESS_THAN_1K) 71 /* 72 * SECUREC BUFFER SIZE Can not be less than 23 73 * The length of the octal representation of 64-bit integers with zero lead 74 */ 75 #define SECUREC_BUFFER_SIZE 256 76 #else 77 #define SECUREC_BUFFER_SIZE 512 78 #endif 79 #endif 80 #if SECUREC_BUFFER_SIZE < 23 81 #error SECUREC_BUFFER_SIZE Can not be less than 23 82 #endif 83 /* Buffer size for wchar, use 4 to make the compiler aligns as 8 bytes as possible */ 84 #define SECUREC_WCHAR_BUFFER_SIZE 4 85 86 #define SECUREC_MAX_PRECISION SECUREC_BUFFER_SIZE 87 /* Max. # bytes in multibyte char ,see MB_LEN_MAX */ 88 #define SECUREC_MB_LEN 16 89 /* The return value of the internal function, which is returned when truncated */ 90 #define SECUREC_PRINTF_TRUNCATE (-2) 91 92 #define SECUREC_VSPRINTF_PARAM_ERROR(format, strDest, destMax, maxLimit) \ 93 ((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) 94 95 #define SECUREC_VSPRINTF_CLEAR_DEST(strDest, destMax, maxLimit) do { \ 96 if ((strDest) != NULL && (destMax) > 0 && (destMax) <= (maxLimit)) { \ 97 *(strDest) = '\0'; \ 98 } \ 99 } SECUREC_WHILE_ZERO 100 101 #ifdef SECUREC_COMPATIBLE_WIN_FORMAT 102 #define SECUREC_VSNPRINTF_PARAM_ERROR(format, strDest, destMax, count, maxLimit) \ 103 (((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) || \ 104 ((count) > (SECUREC_STRING_MAX_LEN - 1) && (count) != (size_t)(-1))) 105 106 #else 107 #define SECUREC_VSNPRINTF_PARAM_ERROR(format, strDest, destMax, count, maxLimit) \ 108 (((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) || \ 109 ((count) > (SECUREC_STRING_MAX_LEN - 1))) 110 #endif 111 112 #ifdef __cplusplus 113 extern "C" { 114 #endif 115 int SecVsnprintfImpl(char *string, size_t count, const char *format, va_list argList); 116 #ifdef SECUREC_FOR_WCHAR 117 int SecVswprintfImpl(wchar_t *string, size_t sizeInWchar, const wchar_t *format, va_list argList); 118 #endif 119 #ifdef __cplusplus 120 } 121 #endif 122 123 #endif 124 125