13d58139fSopenharmony_ci/*
23d58139fSopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
33d58139fSopenharmony_ci * Licensed under Mulan PSL v2.
43d58139fSopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2.
53d58139fSopenharmony_ci * You may obtain a copy of Mulan PSL v2 at:
63d58139fSopenharmony_ci *          http://license.coscl.org.cn/MulanPSL2
73d58139fSopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
83d58139fSopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
93d58139fSopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
103d58139fSopenharmony_ci * See the Mulan PSL v2 for more details.
113d58139fSopenharmony_ci * Description: Define macro, enum, data struct, and declare internal used function
123d58139fSopenharmony_ci *              prototype, which is used by output.inl, secureprintoutput_w.c and
133d58139fSopenharmony_ci *              secureprintoutput_a.c.
143d58139fSopenharmony_ci * Create: 2014-02-25
153d58139fSopenharmony_ci */
163d58139fSopenharmony_ci
173d58139fSopenharmony_ci#ifndef SECUREPRINTOUTPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C
183d58139fSopenharmony_ci#define SECUREPRINTOUTPUT_H_E950DA2C_902F_4B15_BECD_948E99090D9C
193d58139fSopenharmony_ci#include "securecutil.h"
203d58139fSopenharmony_ci
213d58139fSopenharmony_ci/* Shield compilation alerts about using sprintf without format attribute to format float value. */
223d58139fSopenharmony_ci#ifndef SECUREC_HANDLE_WFORMAT
233d58139fSopenharmony_ci#define SECUREC_HANDLE_WFORMAT 1
243d58139fSopenharmony_ci#endif
253d58139fSopenharmony_ci
263d58139fSopenharmony_ci#if defined(__clang__)
273d58139fSopenharmony_ci#if SECUREC_HANDLE_WFORMAT && defined(__GNUC__) && ((__GNUC__ >= 5) || \
283d58139fSopenharmony_ci    (defined(__GNUC_MINOR__) && (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)))
293d58139fSopenharmony_ci#define SECUREC_MASK_WFORMAT_WARNING  _Pragma("GCC diagnostic push") \
303d58139fSopenharmony_ci    _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
313d58139fSopenharmony_ci#define SECUREC_END_MASK_WFORMAT_WARNING  _Pragma("GCC diagnostic pop")
323d58139fSopenharmony_ci#else
333d58139fSopenharmony_ci#define SECUREC_MASK_WFORMAT_WARNING
343d58139fSopenharmony_ci#define SECUREC_END_MASK_WFORMAT_WARNING
353d58139fSopenharmony_ci#endif
363d58139fSopenharmony_ci#else
373d58139fSopenharmony_ci#if SECUREC_HANDLE_WFORMAT && defined(__GNUC__) && ((__GNUC__ >= 5 ) || \
383d58139fSopenharmony_ci    (defined(__GNUC_MINOR__) && (__GNUC__ == 4 && __GNUC_MINOR__ > 7)))
393d58139fSopenharmony_ci#define SECUREC_MASK_WFORMAT_WARNING  _Pragma("GCC diagnostic push") \
403d58139fSopenharmony_ci    _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"") \
413d58139fSopenharmony_ci    _Pragma("GCC diagnostic ignored \"-Wmissing-format-attribute\"") \
423d58139fSopenharmony_ci    _Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"")
433d58139fSopenharmony_ci#define SECUREC_END_MASK_WFORMAT_WARNING  _Pragma("GCC diagnostic pop")
443d58139fSopenharmony_ci#else
453d58139fSopenharmony_ci#define SECUREC_MASK_WFORMAT_WARNING
463d58139fSopenharmony_ci#define SECUREC_END_MASK_WFORMAT_WARNING
473d58139fSopenharmony_ci#endif
483d58139fSopenharmony_ci#endif
493d58139fSopenharmony_ci
503d58139fSopenharmony_ci#define SECUREC_MASK_VSPRINTF_WARNING  SECUREC_MASK_WFORMAT_WARNING \
513d58139fSopenharmony_ci    SECUREC_MASK_MSVC_CRT_WARNING
523d58139fSopenharmony_ci
533d58139fSopenharmony_ci#define SECUREC_END_MASK_VSPRINTF_WARNING  SECUREC_END_MASK_WFORMAT_WARNING \
543d58139fSopenharmony_ci    SECUREC_END_MASK_MSVC_CRT_WARNING
553d58139fSopenharmony_ci
563d58139fSopenharmony_ci/*
573d58139fSopenharmony_ci * Flag definitions.
583d58139fSopenharmony_ci * Using macros instead of enumerations is because some of the enumerated types under the compiler are 16bit.
593d58139fSopenharmony_ci */
603d58139fSopenharmony_ci#define SECUREC_FLAG_SIGN           0x00001U
613d58139fSopenharmony_ci#define SECUREC_FLAG_SIGN_SPACE     0x00002U
623d58139fSopenharmony_ci#define SECUREC_FLAG_LEFT           0x00004U
633d58139fSopenharmony_ci#define SECUREC_FLAG_LEADZERO       0x00008U
643d58139fSopenharmony_ci#define SECUREC_FLAG_LONG           0x00010U
653d58139fSopenharmony_ci#define SECUREC_FLAG_SHORT          0x00020U
663d58139fSopenharmony_ci#define SECUREC_FLAG_SIGNED         0x00040U
673d58139fSopenharmony_ci#define SECUREC_FLAG_ALTERNATE      0x00080U
683d58139fSopenharmony_ci#define SECUREC_FLAG_NEGATIVE       0x00100U
693d58139fSopenharmony_ci#define SECUREC_FLAG_FORCE_OCTAL    0x00200U
703d58139fSopenharmony_ci#define SECUREC_FLAG_LONG_DOUBLE    0x00400U
713d58139fSopenharmony_ci#define SECUREC_FLAG_WIDECHAR       0x00800U
723d58139fSopenharmony_ci#define SECUREC_FLAG_LONGLONG       0x01000U
733d58139fSopenharmony_ci#define SECUREC_FLAG_CHAR           0x02000U
743d58139fSopenharmony_ci#define SECUREC_FLAG_POINTER        0x04000U
753d58139fSopenharmony_ci#define SECUREC_FLAG_I64            0x08000U
763d58139fSopenharmony_ci#define SECUREC_FLAG_PTRDIFF        0x10000U
773d58139fSopenharmony_ci#define SECUREC_FLAG_SIZE           0x20000U
783d58139fSopenharmony_ci#ifdef  SECUREC_COMPATIBLE_LINUX_FORMAT
793d58139fSopenharmony_ci#define SECUREC_FLAG_INTMAX         0x40000U
803d58139fSopenharmony_ci#endif
813d58139fSopenharmony_ci
823d58139fSopenharmony_ci/* State definitions. Identify the status of the current format */
833d58139fSopenharmony_citypedef enum {
843d58139fSopenharmony_ci    STAT_NORMAL,
853d58139fSopenharmony_ci    STAT_PERCENT,
863d58139fSopenharmony_ci    STAT_FLAG,
873d58139fSopenharmony_ci    STAT_WIDTH,
883d58139fSopenharmony_ci    STAT_DOT,
893d58139fSopenharmony_ci    STAT_PRECIS,
903d58139fSopenharmony_ci    STAT_SIZE,
913d58139fSopenharmony_ci    STAT_TYPE,
923d58139fSopenharmony_ci    STAT_INVALID
933d58139fSopenharmony_ci} SecFmtState;
943d58139fSopenharmony_ci
953d58139fSopenharmony_ci#ifndef SECUREC_BUFFER_SIZE
963d58139fSopenharmony_ci#if SECUREC_IN_KERNEL
973d58139fSopenharmony_ci#define SECUREC_BUFFER_SIZE    32
983d58139fSopenharmony_ci#elif defined(SECUREC_STACK_SIZE_LESS_THAN_1K)
993d58139fSopenharmony_ci/*
1003d58139fSopenharmony_ci * SECUREC BUFFER SIZE Can not be less than 23
1013d58139fSopenharmony_ci * The length of the octal representation of 64-bit integers with zero lead
1023d58139fSopenharmony_ci */
1033d58139fSopenharmony_ci#define SECUREC_BUFFER_SIZE    256
1043d58139fSopenharmony_ci#else
1053d58139fSopenharmony_ci#define SECUREC_BUFFER_SIZE    512
1063d58139fSopenharmony_ci#endif
1073d58139fSopenharmony_ci#endif
1083d58139fSopenharmony_ci#if SECUREC_BUFFER_SIZE < 23
1093d58139fSopenharmony_ci#error SECUREC_BUFFER_SIZE Can not be less than 23
1103d58139fSopenharmony_ci#endif
1113d58139fSopenharmony_ci/* Buffer size for wchar, use 4 to make the compiler aligns as 8 bytes as possible */
1123d58139fSopenharmony_ci#define SECUREC_WCHAR_BUFFER_SIZE 4
1133d58139fSopenharmony_ci
1143d58139fSopenharmony_ci#define SECUREC_MAX_PRECISION  SECUREC_BUFFER_SIZE
1153d58139fSopenharmony_ci/* Max. # bytes in multibyte char,see MB_LEN_MAX */
1163d58139fSopenharmony_ci#define SECUREC_MB_LEN 16
1173d58139fSopenharmony_ci/* The return value of the internal function, which is returned when truncated */
1183d58139fSopenharmony_ci#define SECUREC_PRINTF_TRUNCATE (-2)
1193d58139fSopenharmony_ci
1203d58139fSopenharmony_ci#define SECUREC_VSPRINTF_PARAM_ERROR(format, strDest, destMax, maxLimit) \
1213d58139fSopenharmony_ci    ((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit))
1223d58139fSopenharmony_ci
1233d58139fSopenharmony_ci#define SECUREC_VSPRINTF_CLEAR_DEST(strDest, destMax, maxLimit) do { \
1243d58139fSopenharmony_ci    if ((strDest) != NULL && (destMax) > 0 && (destMax) <= (maxLimit)) { \
1253d58139fSopenharmony_ci        *(strDest) = '\0'; \
1263d58139fSopenharmony_ci    } \
1273d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
1283d58139fSopenharmony_ci
1293d58139fSopenharmony_ci#ifdef SECUREC_COMPATIBLE_WIN_FORMAT
1303d58139fSopenharmony_ci#define SECUREC_VSNPRINTF_PARAM_ERROR(format, strDest, destMax, count, maxLimit) \
1313d58139fSopenharmony_ci    (((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) || \
1323d58139fSopenharmony_ci    ((count) > (SECUREC_STRING_MAX_LEN - 1) && (count) != (size_t)(-1)))
1333d58139fSopenharmony_ci
1343d58139fSopenharmony_ci#else
1353d58139fSopenharmony_ci#define SECUREC_VSNPRINTF_PARAM_ERROR(format, strDest, destMax, count, maxLimit) \
1363d58139fSopenharmony_ci    (((format) == NULL || (strDest) == NULL || (destMax) == 0 || (destMax) > (maxLimit)) || \
1373d58139fSopenharmony_ci    ((count) > (SECUREC_STRING_MAX_LEN - 1)))
1383d58139fSopenharmony_ci#endif
1393d58139fSopenharmony_ci
1403d58139fSopenharmony_ci#ifdef __cplusplus
1413d58139fSopenharmony_ciextern "C" {
1423d58139fSopenharmony_ci#endif
1433d58139fSopenharmony_ci#ifdef SECUREC_FOR_WCHAR
1443d58139fSopenharmony_ciint SecVswprintfImpl(wchar_t *string, size_t count, const wchar_t *format, va_list argList);
1453d58139fSopenharmony_ci#else
1463d58139fSopenharmony_ciint SecVsnprintfImpl(char *string, size_t count, const char *format, va_list argList);
1473d58139fSopenharmony_ci#endif
1483d58139fSopenharmony_ci#ifdef __cplusplus
1493d58139fSopenharmony_ci}
1503d58139fSopenharmony_ci#endif
1513d58139fSopenharmony_ci
1523d58139fSopenharmony_ci#endif
1533d58139fSopenharmony_ci
154