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, data struct, and declare internal used function prototype,
123d58139fSopenharmony_ci *              which is used by secure functions.
133d58139fSopenharmony_ci * Create: 2014-02-25
143d58139fSopenharmony_ci */
153d58139fSopenharmony_ci
163d58139fSopenharmony_ci#ifndef SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
173d58139fSopenharmony_ci#define SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
183d58139fSopenharmony_ci#include "securec.h"
193d58139fSopenharmony_ci
203d58139fSopenharmony_ci#if (defined(_MSC_VER)) && (_MSC_VER >= 1400)
213d58139fSopenharmony_ci/* Shield compilation alerts using discarded functions and Constant  expression to maximize code compatibility */
223d58139fSopenharmony_ci#define SECUREC_MASK_MSVC_CRT_WARNING __pragma(warning(push)) \
233d58139fSopenharmony_ci    __pragma(warning(disable : 4996 4127))
243d58139fSopenharmony_ci#define SECUREC_END_MASK_MSVC_CRT_WARNING  __pragma(warning(pop))
253d58139fSopenharmony_ci#else
263d58139fSopenharmony_ci#define SECUREC_MASK_MSVC_CRT_WARNING
273d58139fSopenharmony_ci#define SECUREC_END_MASK_MSVC_CRT_WARNING
283d58139fSopenharmony_ci#endif
293d58139fSopenharmony_ci#define SECUREC_WHILE_ZERO SECUREC_MASK_MSVC_CRT_WARNING while (0) SECUREC_END_MASK_MSVC_CRT_WARNING
303d58139fSopenharmony_ci
313d58139fSopenharmony_ci/* Automatically identify the platform that supports strnlen function, and use this function to improve performance */
323d58139fSopenharmony_ci#ifndef SECUREC_HAVE_STRNLEN
333d58139fSopenharmony_ci#if (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L)
343d58139fSopenharmony_ci#if SECUREC_IN_KERNEL
353d58139fSopenharmony_ci#define SECUREC_HAVE_STRNLEN 0
363d58139fSopenharmony_ci#else
373d58139fSopenharmony_ci#if defined(__GLIBC__) && __GLIBC__ >= 2 && defined(__GLIBC_MINOR__) && __GLIBC_MINOR__ >= 10
383d58139fSopenharmony_ci#define SECUREC_HAVE_STRNLEN 1
393d58139fSopenharmony_ci#else
403d58139fSopenharmony_ci#define SECUREC_HAVE_STRNLEN 0
413d58139fSopenharmony_ci#endif
423d58139fSopenharmony_ci#endif
433d58139fSopenharmony_ci#else
443d58139fSopenharmony_ci#define SECUREC_HAVE_STRNLEN 0
453d58139fSopenharmony_ci#endif
463d58139fSopenharmony_ci#endif
473d58139fSopenharmony_ci
483d58139fSopenharmony_ci#if SECUREC_IN_KERNEL
493d58139fSopenharmony_ci/* In kernel disable functions */
503d58139fSopenharmony_ci#ifndef SECUREC_ENABLE_SCANF_FILE
513d58139fSopenharmony_ci#define SECUREC_ENABLE_SCANF_FILE 0
523d58139fSopenharmony_ci#endif
533d58139fSopenharmony_ci#ifndef SECUREC_ENABLE_SCANF_FLOAT
543d58139fSopenharmony_ci#define SECUREC_ENABLE_SCANF_FLOAT 0
553d58139fSopenharmony_ci#endif
563d58139fSopenharmony_ci#ifndef SECUREC_ENABLE_SPRINTF_FLOAT
573d58139fSopenharmony_ci#define SECUREC_ENABLE_SPRINTF_FLOAT 0
583d58139fSopenharmony_ci#endif
593d58139fSopenharmony_ci#ifndef SECUREC_HAVE_MBTOWC
603d58139fSopenharmony_ci#define SECUREC_HAVE_MBTOWC 0
613d58139fSopenharmony_ci#endif
623d58139fSopenharmony_ci#ifndef SECUREC_HAVE_WCTOMB
633d58139fSopenharmony_ci#define SECUREC_HAVE_WCTOMB 0
643d58139fSopenharmony_ci#endif
653d58139fSopenharmony_ci#ifndef SECUREC_HAVE_WCHART
663d58139fSopenharmony_ci#define SECUREC_HAVE_WCHART 0
673d58139fSopenharmony_ci#endif
683d58139fSopenharmony_ci#else /* Not in kernel */
693d58139fSopenharmony_ci/* Systems that do not support file, can define this macro to 0. */
703d58139fSopenharmony_ci#ifndef SECUREC_ENABLE_SCANF_FILE
713d58139fSopenharmony_ci#define SECUREC_ENABLE_SCANF_FILE 1
723d58139fSopenharmony_ci#endif
733d58139fSopenharmony_ci#ifndef SECUREC_ENABLE_SCANF_FLOAT
743d58139fSopenharmony_ci#define SECUREC_ENABLE_SCANF_FLOAT 1
753d58139fSopenharmony_ci#endif
763d58139fSopenharmony_ci/* Systems that do not support float, can define this macro to 0. */
773d58139fSopenharmony_ci#ifndef SECUREC_ENABLE_SPRINTF_FLOAT
783d58139fSopenharmony_ci#define SECUREC_ENABLE_SPRINTF_FLOAT 1
793d58139fSopenharmony_ci#endif
803d58139fSopenharmony_ci#ifndef SECUREC_HAVE_MBTOWC
813d58139fSopenharmony_ci#define SECUREC_HAVE_MBTOWC 1
823d58139fSopenharmony_ci#endif
833d58139fSopenharmony_ci#ifndef SECUREC_HAVE_WCTOMB
843d58139fSopenharmony_ci#define SECUREC_HAVE_WCTOMB 1
853d58139fSopenharmony_ci#endif
863d58139fSopenharmony_ci#ifndef SECUREC_HAVE_WCHART
873d58139fSopenharmony_ci#define SECUREC_HAVE_WCHART 1
883d58139fSopenharmony_ci#endif
893d58139fSopenharmony_ci#endif
903d58139fSopenharmony_ci
913d58139fSopenharmony_ci#ifndef SECUREC_ENABLE_INLINE
923d58139fSopenharmony_ci#define SECUREC_ENABLE_INLINE 0
933d58139fSopenharmony_ci#endif
943d58139fSopenharmony_ci
953d58139fSopenharmony_ci#ifndef SECUREC_INLINE
963d58139fSopenharmony_ci#if SECUREC_ENABLE_INLINE
973d58139fSopenharmony_ci#define SECUREC_INLINE static inline
983d58139fSopenharmony_ci#else
993d58139fSopenharmony_ci#define SECUREC_INLINE static
1003d58139fSopenharmony_ci#endif
1013d58139fSopenharmony_ci#endif
1023d58139fSopenharmony_ci
1033d58139fSopenharmony_ci#ifndef SECUREC_WARP_OUTPUT
1043d58139fSopenharmony_ci#if SECUREC_IN_KERNEL
1053d58139fSopenharmony_ci#define SECUREC_WARP_OUTPUT 1
1063d58139fSopenharmony_ci#else
1073d58139fSopenharmony_ci#define SECUREC_WARP_OUTPUT 0
1083d58139fSopenharmony_ci#endif
1093d58139fSopenharmony_ci#endif
1103d58139fSopenharmony_ci
1113d58139fSopenharmony_ci#ifndef SECUREC_STREAM_STDIN
1123d58139fSopenharmony_ci#define SECUREC_STREAM_STDIN stdin
1133d58139fSopenharmony_ci#endif
1143d58139fSopenharmony_ci
1153d58139fSopenharmony_ci#define SECUREC_MUL_SIXTEEN(x)              ((x) << 4U)
1163d58139fSopenharmony_ci#define SECUREC_MUL_EIGHT(x)                ((x) << 3U)
1173d58139fSopenharmony_ci#define SECUREC_MUL_TEN(x)                  ((((x) << 2U) + (x)) << 1U)
1183d58139fSopenharmony_ci/* Limited format input and output width, use signed integer */
1193d58139fSopenharmony_ci#define SECUREC_MAX_WIDTH_LEN_DIV_TEN       21474836
1203d58139fSopenharmony_ci#define SECUREC_MAX_WIDTH_LEN               (SECUREC_MAX_WIDTH_LEN_DIV_TEN * 10)
1213d58139fSopenharmony_ci/* Is the x multiplied by 10 greater than */
1223d58139fSopenharmony_ci#define SECUREC_MUL_TEN_ADD_BEYOND_MAX(x)   (((x) > SECUREC_MAX_WIDTH_LEN_DIV_TEN))
1233d58139fSopenharmony_ci
1243d58139fSopenharmony_ci#define SECUREC_FLOAT_BUFSIZE               (309 + 40)  /* Max length of double value */
1253d58139fSopenharmony_ci#define SECUREC_FLOAT_BUFSIZE_LB            (4932 + 40) /* Max length of long double value */
1263d58139fSopenharmony_ci#define SECUREC_FLOAT_DEFAULT_PRECISION     6
1273d58139fSopenharmony_ci
1283d58139fSopenharmony_ci/* This macro does not handle pointer equality or integer overflow */
1293d58139fSopenharmony_ci#define SECUREC_MEMORY_NO_OVERLAP(dest, src, count) \
1303d58139fSopenharmony_ci    (((src) < (dest) && ((const char *)(src) + (count)) <= (char *)(dest)) || \
1313d58139fSopenharmony_ci    ((dest) < (src) && ((char *)(dest) + (count)) <= (const char *)(src)))
1323d58139fSopenharmony_ci
1333d58139fSopenharmony_ci#define SECUREC_MEMORY_IS_OVERLAP(dest, src, count) \
1343d58139fSopenharmony_ci    (((src) < (dest) && ((const char *)(src) + (count)) > (char *)(dest)) || \
1353d58139fSopenharmony_ci    ((dest) < (src) && ((char *)(dest) + (count)) > (const char *)(src)))
1363d58139fSopenharmony_ci
1373d58139fSopenharmony_ci/*
1383d58139fSopenharmony_ci * Check whether the strings overlap, len is the length of the string not include terminator
1393d58139fSopenharmony_ci * Length is related to data type char or wchar , do not force conversion of types
1403d58139fSopenharmony_ci */
1413d58139fSopenharmony_ci#define SECUREC_STRING_NO_OVERLAP(dest, src, len) \
1423d58139fSopenharmony_ci    (((src) < (dest) && ((src) + (len)) < (dest)) || \
1433d58139fSopenharmony_ci    ((dest) < (src) && ((dest) + (len)) < (src)))
1443d58139fSopenharmony_ci
1453d58139fSopenharmony_ci/*
1463d58139fSopenharmony_ci * Check whether the strings overlap for strcpy wcscpy function, dest len and src Len are not include terminator
1473d58139fSopenharmony_ci * Length is related to data type char or wchar , do not force conversion of types
1483d58139fSopenharmony_ci */
1493d58139fSopenharmony_ci#define SECUREC_STRING_IS_OVERLAP(dest, src, len) \
1503d58139fSopenharmony_ci    (((src) < (dest) && ((src) + (len)) >= (dest)) || \
1513d58139fSopenharmony_ci    ((dest) < (src) && ((dest) + (len)) >= (src)))
1523d58139fSopenharmony_ci
1533d58139fSopenharmony_ci/*
1543d58139fSopenharmony_ci * Check whether the strings overlap for strcat wcscat function, dest len and src Len are not include terminator
1553d58139fSopenharmony_ci * Length is related to data type char or wchar , do not force conversion of types
1563d58139fSopenharmony_ci */
1573d58139fSopenharmony_ci#define SECUREC_CAT_STRING_IS_OVERLAP(dest, destLen, src, srcLen) \
1583d58139fSopenharmony_ci    (((dest) < (src) && ((dest) + (destLen) + (srcLen)) >= (src)) || \
1593d58139fSopenharmony_ci    ((src) < (dest) && ((src) + (srcLen)) >= (dest)))
1603d58139fSopenharmony_ci
1613d58139fSopenharmony_ci#if SECUREC_HAVE_STRNLEN
1623d58139fSopenharmony_ci#define SECUREC_CALC_STR_LEN(str, maxLen, outLen) do { \
1633d58139fSopenharmony_ci    *(outLen) = strnlen((str), (maxLen)); \
1643d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
1653d58139fSopenharmony_ci#define SECUREC_CALC_STR_LEN_OPT(str, maxLen, outLen) do { \
1663d58139fSopenharmony_ci    if ((maxLen) > 8) { \
1673d58139fSopenharmony_ci        /* Optimization or len less then 8 */ \
1683d58139fSopenharmony_ci        if (*((str) + 0) == '\0') { \
1693d58139fSopenharmony_ci            *(outLen) = 0; \
1703d58139fSopenharmony_ci        } else if (*((str) + 1) == '\0') { \
1713d58139fSopenharmony_ci            *(outLen) = 1; \
1723d58139fSopenharmony_ci        } else if (*((str) + 2) == '\0') { \
1733d58139fSopenharmony_ci            *(outLen) = 2; \
1743d58139fSopenharmony_ci        } else if (*((str) + 3) == '\0') { \
1753d58139fSopenharmony_ci            *(outLen) = 3; \
1763d58139fSopenharmony_ci        } else if (*((str) + 4) == '\0') { \
1773d58139fSopenharmony_ci            *(outLen) = 4; \
1783d58139fSopenharmony_ci        } else if (*((str) + 5) == '\0') { \
1793d58139fSopenharmony_ci            *(outLen) = 5; \
1803d58139fSopenharmony_ci        } else if (*((str) + 6) == '\0') { \
1813d58139fSopenharmony_ci            *(outLen) = 6; \
1823d58139fSopenharmony_ci        } else if (*((str) + 7) == '\0') { \
1833d58139fSopenharmony_ci            *(outLen) = 7; \
1843d58139fSopenharmony_ci        } else if (*((str) + 8) == '\0') { \
1853d58139fSopenharmony_ci            /* Optimization with a length of 8 */ \
1863d58139fSopenharmony_ci            *(outLen) = 8; \
1873d58139fSopenharmony_ci        } else { \
1883d58139fSopenharmony_ci            /* The offset is 8 because the performance of 8 byte alignment is high */ \
1893d58139fSopenharmony_ci            *(outLen) = 8 + strnlen((str) + 8, (maxLen) - 8); \
1903d58139fSopenharmony_ci        } \
1913d58139fSopenharmony_ci    } else { \
1923d58139fSopenharmony_ci        SECUREC_CALC_STR_LEN((str), (maxLen), (outLen)); \
1933d58139fSopenharmony_ci    } \
1943d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
1953d58139fSopenharmony_ci#else
1963d58139fSopenharmony_ci#define SECUREC_CALC_STR_LEN(str, maxLen, outLen) do { \
1973d58139fSopenharmony_ci    const char *strEnd_ = (const char *)(str); \
1983d58139fSopenharmony_ci    size_t availableSize_ = (size_t)(maxLen); \
1993d58139fSopenharmony_ci    while (availableSize_ > 0 && *strEnd_ != '\0') { \
2003d58139fSopenharmony_ci        --availableSize_; \
2013d58139fSopenharmony_ci        ++strEnd_; \
2023d58139fSopenharmony_ci    } \
2033d58139fSopenharmony_ci    *(outLen) = (size_t)(strEnd_ - (str)); \
2043d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
2053d58139fSopenharmony_ci#define SECUREC_CALC_STR_LEN_OPT SECUREC_CALC_STR_LEN
2063d58139fSopenharmony_ci#endif
2073d58139fSopenharmony_ci
2083d58139fSopenharmony_ci#define SECUREC_CALC_WSTR_LEN(str, maxLen, outLen) do { \
2093d58139fSopenharmony_ci    const wchar_t *strEnd_ = (const wchar_t *)(str); \
2103d58139fSopenharmony_ci    size_t len_ = 0; \
2113d58139fSopenharmony_ci    while (len_ < (maxLen) && *strEnd_ != L'\0') { \
2123d58139fSopenharmony_ci        ++len_; \
2133d58139fSopenharmony_ci        ++strEnd_; \
2143d58139fSopenharmony_ci    } \
2153d58139fSopenharmony_ci    *(outLen) = len_; \
2163d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
2173d58139fSopenharmony_ci
2183d58139fSopenharmony_ci/*
2193d58139fSopenharmony_ci * Performance optimization, product may disable inline function.
2203d58139fSopenharmony_ci * Using function pointer for MEMSET to prevent compiler optimization when cleaning up memory.
2213d58139fSopenharmony_ci */
2223d58139fSopenharmony_ci#ifdef SECUREC_USE_ASM
2233d58139fSopenharmony_ci#define SECUREC_MEMSET_FUNC_OPT  memset_opt
2243d58139fSopenharmony_ci#define SECUREC_MEMCPY_FUNC_OPT  memcpy_opt
2253d58139fSopenharmony_ci#else
2263d58139fSopenharmony_ci#define SECUREC_MEMSET_FUNC_OPT  memset
2273d58139fSopenharmony_ci#define SECUREC_MEMCPY_FUNC_OPT  memcpy
2283d58139fSopenharmony_ci#endif
2293d58139fSopenharmony_ci
2303d58139fSopenharmony_ci#define SECUREC_MEMCPY_WARP_OPT(dest, src, count)    (void)SECUREC_MEMCPY_FUNC_OPT((dest), (src), (count))
2313d58139fSopenharmony_ci
2323d58139fSopenharmony_ci#ifndef SECUREC_MEMSET_BARRIER
2333d58139fSopenharmony_ci#if defined(__GNUC__)
2343d58139fSopenharmony_ci/* Can be turned off for scenarios that do not use memory barrier */
2353d58139fSopenharmony_ci#define SECUREC_MEMSET_BARRIER 1
2363d58139fSopenharmony_ci#else
2373d58139fSopenharmony_ci#define SECUREC_MEMSET_BARRIER 0
2383d58139fSopenharmony_ci#endif
2393d58139fSopenharmony_ci#endif
2403d58139fSopenharmony_ci
2413d58139fSopenharmony_ci#ifndef SECUREC_MEMSET_INDIRECT_USE
2423d58139fSopenharmony_ci/* Can be turned off for scenarios that do not allow pointer calls */
2433d58139fSopenharmony_ci#define SECUREC_MEMSET_INDIRECT_USE 1
2443d58139fSopenharmony_ci#endif
2453d58139fSopenharmony_ci
2463d58139fSopenharmony_ci#if SECUREC_MEMSET_BARRIER
2473d58139fSopenharmony_ci#define SECUREC_MEMORY_BARRIER(dest) __asm__ __volatile__("": : "r"(dest) : "memory")
2483d58139fSopenharmony_ci#else
2493d58139fSopenharmony_ci#define SECUREC_MEMORY_BARRIER(dest)
2503d58139fSopenharmony_ci#endif
2513d58139fSopenharmony_ci
2523d58139fSopenharmony_ci#if SECUREC_MEMSET_BARRIER
2533d58139fSopenharmony_ci#define SECUREC_MEMSET_PREVENT_DSE(dest, value, count)  do { \
2543d58139fSopenharmony_ci        (void)SECUREC_MEMSET_FUNC_OPT(dest, value, count); \
2553d58139fSopenharmony_ci        SECUREC_MEMORY_BARRIER(dest); \
2563d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
2573d58139fSopenharmony_ci#elif SECUREC_MEMSET_INDIRECT_USE
2583d58139fSopenharmony_ci#define SECUREC_MEMSET_PREVENT_DSE(dest, value, count)  do { \
2593d58139fSopenharmony_ci    void *(* const volatile fn_)(void *s_, int c_, size_t n_) = SECUREC_MEMSET_FUNC_OPT; \
2603d58139fSopenharmony_ci    (void)(*fn_)((dest), (value), (count)); \
2613d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
2623d58139fSopenharmony_ci#else
2633d58139fSopenharmony_ci#define SECUREC_MEMSET_PREVENT_DSE(dest, value, count)  (void)SECUREC_MEMSET_FUNC_OPT((dest), (value), (count))
2643d58139fSopenharmony_ci#endif
2653d58139fSopenharmony_ci
2663d58139fSopenharmony_ci#ifdef SECUREC_FORMAT_OUTPUT_INPUT
2673d58139fSopenharmony_ci#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) || defined(__ARMCC_VERSION)
2683d58139fSopenharmony_citypedef __int64 SecInt64;
2693d58139fSopenharmony_citypedef unsigned __int64 SecUnsignedInt64;
2703d58139fSopenharmony_ci#if defined(__ARMCC_VERSION)
2713d58139fSopenharmony_citypedef unsigned int SecUnsignedInt32;
2723d58139fSopenharmony_ci#else
2733d58139fSopenharmony_citypedef unsigned __int32 SecUnsignedInt32;
2743d58139fSopenharmony_ci#endif
2753d58139fSopenharmony_ci#else
2763d58139fSopenharmony_citypedef unsigned int SecUnsignedInt32;
2773d58139fSopenharmony_citypedef long long SecInt64;
2783d58139fSopenharmony_citypedef unsigned long long SecUnsignedInt64;
2793d58139fSopenharmony_ci#endif
2803d58139fSopenharmony_ci
2813d58139fSopenharmony_ci#ifdef SECUREC_FOR_WCHAR
2823d58139fSopenharmony_ci#if defined(SECUREC_VXWORKS_PLATFORM) && !defined(__WINT_TYPE__)
2833d58139fSopenharmony_citypedef wchar_t wint_t;
2843d58139fSopenharmony_ci#endif
2853d58139fSopenharmony_ci#ifndef WEOF
2863d58139fSopenharmony_ci#define WEOF ((wchar_t)(-1))
2873d58139fSopenharmony_ci#endif
2883d58139fSopenharmony_ci#define SECUREC_CHAR(x) L ## x
2893d58139fSopenharmony_citypedef wchar_t SecChar;
2903d58139fSopenharmony_citypedef wchar_t SecUnsignedChar;
2913d58139fSopenharmony_citypedef wint_t SecInt;
2923d58139fSopenharmony_citypedef wint_t SecUnsignedInt;
2933d58139fSopenharmony_ci#else /*  no SECUREC_FOR_WCHAR */
2943d58139fSopenharmony_ci#define SECUREC_CHAR(x) (x)
2953d58139fSopenharmony_citypedef char SecChar;
2963d58139fSopenharmony_citypedef unsigned char SecUnsignedChar;
2973d58139fSopenharmony_citypedef int SecInt;
2983d58139fSopenharmony_citypedef unsigned int SecUnsignedInt;
2993d58139fSopenharmony_ci#endif
3003d58139fSopenharmony_ci#endif
3013d58139fSopenharmony_ci
3023d58139fSopenharmony_ci/*
3033d58139fSopenharmony_ci * Determine whether the address is 8-byte aligned
3043d58139fSopenharmony_ci * Some systems do not have uintptr_t type, so  use NULL to clear tool alarm 507
3053d58139fSopenharmony_ci */
3063d58139fSopenharmony_ci#define SECUREC_ADDR_ALIGNED_8(addr) ((((size_t)(addr)) & 7U) == 0) /* Use 7 to check aligned 8 */
3073d58139fSopenharmony_ci
3083d58139fSopenharmony_ci/*
3093d58139fSopenharmony_ci * If you define the memory allocation function, you need to define the function prototype.
3103d58139fSopenharmony_ci * You can define this macro as a header file.
3113d58139fSopenharmony_ci */
3123d58139fSopenharmony_ci#if defined(SECUREC_MALLOC_PROTOTYPE)
3133d58139fSopenharmony_ciSECUREC_MALLOC_PROTOTYPE
3143d58139fSopenharmony_ci#endif
3153d58139fSopenharmony_ci
3163d58139fSopenharmony_ci#ifndef SECUREC_MALLOC
3173d58139fSopenharmony_ci#define SECUREC_MALLOC(x) malloc((size_t)(x))
3183d58139fSopenharmony_ci#endif
3193d58139fSopenharmony_ci
3203d58139fSopenharmony_ci#ifndef SECUREC_FREE
3213d58139fSopenharmony_ci#define SECUREC_FREE(x)   free((void *)(x))
3223d58139fSopenharmony_ci#endif
3233d58139fSopenharmony_ci
3243d58139fSopenharmony_ci/* Improve performance with struct assignment, buf1 is not defined to avoid tool false positive */
3253d58139fSopenharmony_ci#define SECUREC_COPY_VALUE_BY_STRUCT(dest, src, n) do { \
3263d58139fSopenharmony_ci    *(SecStrBuf##n *)(void *)(dest) = *(const SecStrBuf##n *)(const void *)(src); \
3273d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
3283d58139fSopenharmony_ci
3293d58139fSopenharmony_citypedef struct {
3303d58139fSopenharmony_ci    unsigned char buf[2]; /* Performance optimization code structure assignment length 2 bytes */
3313d58139fSopenharmony_ci} SecStrBuf2;
3323d58139fSopenharmony_citypedef struct {
3333d58139fSopenharmony_ci    unsigned char buf[3]; /* Performance optimization code structure assignment length 3 bytes */
3343d58139fSopenharmony_ci} SecStrBuf3;
3353d58139fSopenharmony_citypedef struct {
3363d58139fSopenharmony_ci    unsigned char buf[4]; /* Performance optimization code structure assignment length 4 bytes */
3373d58139fSopenharmony_ci} SecStrBuf4;
3383d58139fSopenharmony_citypedef struct {
3393d58139fSopenharmony_ci    unsigned char buf[5]; /* Performance optimization code structure assignment length 5 bytes */
3403d58139fSopenharmony_ci} SecStrBuf5;
3413d58139fSopenharmony_citypedef struct {
3423d58139fSopenharmony_ci    unsigned char buf[6]; /* Performance optimization code structure assignment length 6 bytes */
3433d58139fSopenharmony_ci} SecStrBuf6;
3443d58139fSopenharmony_citypedef struct {
3453d58139fSopenharmony_ci    unsigned char buf[7]; /* Performance optimization code structure assignment length 7 bytes */
3463d58139fSopenharmony_ci} SecStrBuf7;
3473d58139fSopenharmony_citypedef struct {
3483d58139fSopenharmony_ci    unsigned char buf[8]; /* Performance optimization code structure assignment length 8 bytes */
3493d58139fSopenharmony_ci} SecStrBuf8;
3503d58139fSopenharmony_citypedef struct {
3513d58139fSopenharmony_ci    unsigned char buf[9]; /* Performance optimization code structure assignment length 9 bytes */
3523d58139fSopenharmony_ci} SecStrBuf9;
3533d58139fSopenharmony_citypedef struct {
3543d58139fSopenharmony_ci    unsigned char buf[10]; /* Performance optimization code structure assignment length 10 bytes */
3553d58139fSopenharmony_ci} SecStrBuf10;
3563d58139fSopenharmony_citypedef struct {
3573d58139fSopenharmony_ci    unsigned char buf[11]; /* Performance optimization code structure assignment length 11 bytes */
3583d58139fSopenharmony_ci} SecStrBuf11;
3593d58139fSopenharmony_citypedef struct {
3603d58139fSopenharmony_ci    unsigned char buf[12]; /* Performance optimization code structure assignment length 12 bytes */
3613d58139fSopenharmony_ci} SecStrBuf12;
3623d58139fSopenharmony_citypedef struct {
3633d58139fSopenharmony_ci    unsigned char buf[13]; /* Performance optimization code structure assignment length 13 bytes */
3643d58139fSopenharmony_ci} SecStrBuf13;
3653d58139fSopenharmony_citypedef struct {
3663d58139fSopenharmony_ci    unsigned char buf[14]; /* Performance optimization code structure assignment length 14 bytes */
3673d58139fSopenharmony_ci} SecStrBuf14;
3683d58139fSopenharmony_citypedef struct {
3693d58139fSopenharmony_ci    unsigned char buf[15]; /* Performance optimization code structure assignment length 15 bytes */
3703d58139fSopenharmony_ci} SecStrBuf15;
3713d58139fSopenharmony_citypedef struct {
3723d58139fSopenharmony_ci    unsigned char buf[16]; /* Performance optimization code structure assignment length 16 bytes */
3733d58139fSopenharmony_ci} SecStrBuf16;
3743d58139fSopenharmony_citypedef struct {
3753d58139fSopenharmony_ci    unsigned char buf[17]; /* Performance optimization code structure assignment length 17 bytes */
3763d58139fSopenharmony_ci} SecStrBuf17;
3773d58139fSopenharmony_citypedef struct {
3783d58139fSopenharmony_ci    unsigned char buf[18]; /* Performance optimization code structure assignment length 18 bytes */
3793d58139fSopenharmony_ci} SecStrBuf18;
3803d58139fSopenharmony_citypedef struct {
3813d58139fSopenharmony_ci    unsigned char buf[19]; /* Performance optimization code structure assignment length 19 bytes */
3823d58139fSopenharmony_ci} SecStrBuf19;
3833d58139fSopenharmony_citypedef struct {
3843d58139fSopenharmony_ci    unsigned char buf[20]; /* Performance optimization code structure assignment length 20 bytes */
3853d58139fSopenharmony_ci} SecStrBuf20;
3863d58139fSopenharmony_citypedef struct {
3873d58139fSopenharmony_ci    unsigned char buf[21]; /* Performance optimization code structure assignment length 21 bytes */
3883d58139fSopenharmony_ci} SecStrBuf21;
3893d58139fSopenharmony_citypedef struct {
3903d58139fSopenharmony_ci    unsigned char buf[22]; /* Performance optimization code structure assignment length 22 bytes */
3913d58139fSopenharmony_ci} SecStrBuf22;
3923d58139fSopenharmony_citypedef struct {
3933d58139fSopenharmony_ci    unsigned char buf[23]; /* Performance optimization code structure assignment length 23 bytes */
3943d58139fSopenharmony_ci} SecStrBuf23;
3953d58139fSopenharmony_citypedef struct {
3963d58139fSopenharmony_ci    unsigned char buf[24]; /* Performance optimization code structure assignment length 24 bytes */
3973d58139fSopenharmony_ci} SecStrBuf24;
3983d58139fSopenharmony_citypedef struct {
3993d58139fSopenharmony_ci    unsigned char buf[25]; /* Performance optimization code structure assignment length 25 bytes */
4003d58139fSopenharmony_ci} SecStrBuf25;
4013d58139fSopenharmony_citypedef struct {
4023d58139fSopenharmony_ci    unsigned char buf[26]; /* Performance optimization code structure assignment length 26 bytes */
4033d58139fSopenharmony_ci} SecStrBuf26;
4043d58139fSopenharmony_citypedef struct {
4053d58139fSopenharmony_ci    unsigned char buf[27]; /* Performance optimization code structure assignment length 27 bytes */
4063d58139fSopenharmony_ci} SecStrBuf27;
4073d58139fSopenharmony_citypedef struct {
4083d58139fSopenharmony_ci    unsigned char buf[28]; /* Performance optimization code structure assignment length 28 bytes */
4093d58139fSopenharmony_ci} SecStrBuf28;
4103d58139fSopenharmony_citypedef struct {
4113d58139fSopenharmony_ci    unsigned char buf[29]; /* Performance optimization code structure assignment length 29 bytes */
4123d58139fSopenharmony_ci} SecStrBuf29;
4133d58139fSopenharmony_citypedef struct {
4143d58139fSopenharmony_ci    unsigned char buf[30]; /* Performance optimization code structure assignment length 30 bytes */
4153d58139fSopenharmony_ci} SecStrBuf30;
4163d58139fSopenharmony_citypedef struct {
4173d58139fSopenharmony_ci    unsigned char buf[31]; /* Performance optimization code structure assignment length 31 bytes */
4183d58139fSopenharmony_ci} SecStrBuf31;
4193d58139fSopenharmony_citypedef struct {
4203d58139fSopenharmony_ci    unsigned char buf[32]; /* Performance optimization code structure assignment length 32 bytes */
4213d58139fSopenharmony_ci} SecStrBuf32;
4223d58139fSopenharmony_citypedef struct {
4233d58139fSopenharmony_ci    unsigned char buf[33]; /* Performance optimization code structure assignment length 33 bytes */
4243d58139fSopenharmony_ci} SecStrBuf33;
4253d58139fSopenharmony_citypedef struct {
4263d58139fSopenharmony_ci    unsigned char buf[34]; /* Performance optimization code structure assignment length 34 bytes */
4273d58139fSopenharmony_ci} SecStrBuf34;
4283d58139fSopenharmony_citypedef struct {
4293d58139fSopenharmony_ci    unsigned char buf[35]; /* Performance optimization code structure assignment length 35 bytes */
4303d58139fSopenharmony_ci} SecStrBuf35;
4313d58139fSopenharmony_citypedef struct {
4323d58139fSopenharmony_ci    unsigned char buf[36]; /* Performance optimization code structure assignment length 36 bytes */
4333d58139fSopenharmony_ci} SecStrBuf36;
4343d58139fSopenharmony_citypedef struct {
4353d58139fSopenharmony_ci    unsigned char buf[37]; /* Performance optimization code structure assignment length 37 bytes */
4363d58139fSopenharmony_ci} SecStrBuf37;
4373d58139fSopenharmony_citypedef struct {
4383d58139fSopenharmony_ci    unsigned char buf[38]; /* Performance optimization code structure assignment length 38 bytes */
4393d58139fSopenharmony_ci} SecStrBuf38;
4403d58139fSopenharmony_citypedef struct {
4413d58139fSopenharmony_ci    unsigned char buf[39]; /* Performance optimization code structure assignment length 39 bytes */
4423d58139fSopenharmony_ci} SecStrBuf39;
4433d58139fSopenharmony_citypedef struct {
4443d58139fSopenharmony_ci    unsigned char buf[40]; /* Performance optimization code structure assignment length 40 bytes */
4453d58139fSopenharmony_ci} SecStrBuf40;
4463d58139fSopenharmony_citypedef struct {
4473d58139fSopenharmony_ci    unsigned char buf[41]; /* Performance optimization code structure assignment length 41 bytes */
4483d58139fSopenharmony_ci} SecStrBuf41;
4493d58139fSopenharmony_citypedef struct {
4503d58139fSopenharmony_ci    unsigned char buf[42]; /* Performance optimization code structure assignment length 42 bytes */
4513d58139fSopenharmony_ci} SecStrBuf42;
4523d58139fSopenharmony_citypedef struct {
4533d58139fSopenharmony_ci    unsigned char buf[43]; /* Performance optimization code structure assignment length 43 bytes */
4543d58139fSopenharmony_ci} SecStrBuf43;
4553d58139fSopenharmony_citypedef struct {
4563d58139fSopenharmony_ci    unsigned char buf[44]; /* Performance optimization code structure assignment length 44 bytes */
4573d58139fSopenharmony_ci} SecStrBuf44;
4583d58139fSopenharmony_citypedef struct {
4593d58139fSopenharmony_ci    unsigned char buf[45]; /* Performance optimization code structure assignment length 45 bytes */
4603d58139fSopenharmony_ci} SecStrBuf45;
4613d58139fSopenharmony_citypedef struct {
4623d58139fSopenharmony_ci    unsigned char buf[46]; /* Performance optimization code structure assignment length 46 bytes */
4633d58139fSopenharmony_ci} SecStrBuf46;
4643d58139fSopenharmony_citypedef struct {
4653d58139fSopenharmony_ci    unsigned char buf[47]; /* Performance optimization code structure assignment length 47 bytes */
4663d58139fSopenharmony_ci} SecStrBuf47;
4673d58139fSopenharmony_citypedef struct {
4683d58139fSopenharmony_ci    unsigned char buf[48]; /* Performance optimization code structure assignment length 48 bytes */
4693d58139fSopenharmony_ci} SecStrBuf48;
4703d58139fSopenharmony_citypedef struct {
4713d58139fSopenharmony_ci    unsigned char buf[49]; /* Performance optimization code structure assignment length 49 bytes */
4723d58139fSopenharmony_ci} SecStrBuf49;
4733d58139fSopenharmony_citypedef struct {
4743d58139fSopenharmony_ci    unsigned char buf[50]; /* Performance optimization code structure assignment length 50 bytes */
4753d58139fSopenharmony_ci} SecStrBuf50;
4763d58139fSopenharmony_citypedef struct {
4773d58139fSopenharmony_ci    unsigned char buf[51]; /* Performance optimization code structure assignment length 51 bytes */
4783d58139fSopenharmony_ci} SecStrBuf51;
4793d58139fSopenharmony_citypedef struct {
4803d58139fSopenharmony_ci    unsigned char buf[52]; /* Performance optimization code structure assignment length 52 bytes */
4813d58139fSopenharmony_ci} SecStrBuf52;
4823d58139fSopenharmony_citypedef struct {
4833d58139fSopenharmony_ci    unsigned char buf[53]; /* Performance optimization code structure assignment length 53 bytes */
4843d58139fSopenharmony_ci} SecStrBuf53;
4853d58139fSopenharmony_citypedef struct {
4863d58139fSopenharmony_ci    unsigned char buf[54]; /* Performance optimization code structure assignment length 54 bytes */
4873d58139fSopenharmony_ci} SecStrBuf54;
4883d58139fSopenharmony_citypedef struct {
4893d58139fSopenharmony_ci    unsigned char buf[55]; /* Performance optimization code structure assignment length 55 bytes */
4903d58139fSopenharmony_ci} SecStrBuf55;
4913d58139fSopenharmony_citypedef struct {
4923d58139fSopenharmony_ci    unsigned char buf[56]; /* Performance optimization code structure assignment length 56 bytes */
4933d58139fSopenharmony_ci} SecStrBuf56;
4943d58139fSopenharmony_citypedef struct {
4953d58139fSopenharmony_ci    unsigned char buf[57]; /* Performance optimization code structure assignment length 57 bytes */
4963d58139fSopenharmony_ci} SecStrBuf57;
4973d58139fSopenharmony_citypedef struct {
4983d58139fSopenharmony_ci    unsigned char buf[58]; /* Performance optimization code structure assignment length 58 bytes */
4993d58139fSopenharmony_ci} SecStrBuf58;
5003d58139fSopenharmony_citypedef struct {
5013d58139fSopenharmony_ci    unsigned char buf[59]; /* Performance optimization code structure assignment length 59 bytes */
5023d58139fSopenharmony_ci} SecStrBuf59;
5033d58139fSopenharmony_citypedef struct {
5043d58139fSopenharmony_ci    unsigned char buf[60]; /* Performance optimization code structure assignment length 60 bytes */
5053d58139fSopenharmony_ci} SecStrBuf60;
5063d58139fSopenharmony_citypedef struct {
5073d58139fSopenharmony_ci    unsigned char buf[61]; /* Performance optimization code structure assignment length 61 bytes */
5083d58139fSopenharmony_ci} SecStrBuf61;
5093d58139fSopenharmony_citypedef struct {
5103d58139fSopenharmony_ci    unsigned char buf[62]; /* Performance optimization code structure assignment length 62 bytes */
5113d58139fSopenharmony_ci} SecStrBuf62;
5123d58139fSopenharmony_citypedef struct {
5133d58139fSopenharmony_ci    unsigned char buf[63]; /* Performance optimization code structure assignment length 63 bytes */
5143d58139fSopenharmony_ci} SecStrBuf63;
5153d58139fSopenharmony_citypedef struct {
5163d58139fSopenharmony_ci    unsigned char buf[64]; /* Performance optimization code structure assignment length 64 bytes */
5173d58139fSopenharmony_ci} SecStrBuf64;
5183d58139fSopenharmony_ci
5193d58139fSopenharmony_ci/*
5203d58139fSopenharmony_ci * User can change the error handler by modify the following definition,
5213d58139fSopenharmony_ci * such as logging the detail error in file.
5223d58139fSopenharmony_ci */
5233d58139fSopenharmony_ci#if defined(_DEBUG) || defined(DEBUG)
5243d58139fSopenharmony_ci#if defined(SECUREC_ERROR_HANDLER_BY_ASSERT)
5253d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_PARAMTER(msg) assert(msg "invalid argument" == NULL)
5263d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_RANGE(msg)    assert(msg "invalid dest buffer size" == NULL)
5273d58139fSopenharmony_ci#define SECUREC_ERROR_BUFFER_OVERLAP(msg)   assert(msg "buffer overlap" == NULL)
5283d58139fSopenharmony_ci#elif defined(SECUREC_ERROR_HANDLER_BY_PRINTF)
5293d58139fSopenharmony_ci#if SECUREC_IN_KERNEL
5303d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_PARAMTER(msg) printk("%s invalid argument\n", msg)
5313d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_RANGE(msg)    printk("%s invalid dest buffer size\n", msg)
5323d58139fSopenharmony_ci#define SECUREC_ERROR_BUFFER_OVERLAP(msg)   printk("%s buffer overlap\n", msg)
5333d58139fSopenharmony_ci#else
5343d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_PARAMTER(msg) printf("%s invalid argument\n", msg)
5353d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_RANGE(msg)    printf("%s invalid dest buffer size\n", msg)
5363d58139fSopenharmony_ci#define SECUREC_ERROR_BUFFER_OVERLAP(msg)   printf("%s buffer overlap\n", msg)
5373d58139fSopenharmony_ci#endif
5383d58139fSopenharmony_ci#elif defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
5393d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_PARAMTER(msg) LogSecureCRuntimeError(msg " EINVAL\n")
5403d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_RANGE(msg)    LogSecureCRuntimeError(msg " ERANGE\n")
5413d58139fSopenharmony_ci#define SECUREC_ERROR_BUFFER_OVERLAP(msg)   LogSecureCRuntimeError(msg " EOVERLAP\n")
5423d58139fSopenharmony_ci#endif
5433d58139fSopenharmony_ci#endif
5443d58139fSopenharmony_ci
5453d58139fSopenharmony_ci/* Default handler is none */
5463d58139fSopenharmony_ci#ifndef SECUREC_ERROR_INVALID_PARAMTER
5473d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_PARAMTER(msg)
5483d58139fSopenharmony_ci#endif
5493d58139fSopenharmony_ci#ifndef SECUREC_ERROR_INVALID_RANGE
5503d58139fSopenharmony_ci#define SECUREC_ERROR_INVALID_RANGE(msg)
5513d58139fSopenharmony_ci#endif
5523d58139fSopenharmony_ci#ifndef SECUREC_ERROR_BUFFER_OVERLAP
5533d58139fSopenharmony_ci#define SECUREC_ERROR_BUFFER_OVERLAP(msg)
5543d58139fSopenharmony_ci#endif
5553d58139fSopenharmony_ci
5563d58139fSopenharmony_ci#ifdef __cplusplus
5573d58139fSopenharmony_ciextern "C" {
5583d58139fSopenharmony_ci#endif
5593d58139fSopenharmony_ci
5603d58139fSopenharmony_ci/* Assembly language memory copy and memory set for X86 or MIPS ... */
5613d58139fSopenharmony_ci#ifdef SECUREC_USE_ASM
5623d58139fSopenharmony_civoid *memcpy_opt(void *dest, const void *src, size_t n);
5633d58139fSopenharmony_civoid *memset_opt(void *s, int c, size_t n);
5643d58139fSopenharmony_ci#endif
5653d58139fSopenharmony_ci
5663d58139fSopenharmony_ci#if defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
5673d58139fSopenharmony_civoid LogSecureCRuntimeError(const char *errDetail);
5683d58139fSopenharmony_ci#endif
5693d58139fSopenharmony_ci
5703d58139fSopenharmony_ci#ifdef __cplusplus
5713d58139fSopenharmony_ci}
5723d58139fSopenharmony_ci#endif /* __cplusplus */
5733d58139fSopenharmony_ci#endif
5743d58139fSopenharmony_ci
575