* Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
* Licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* Description: Define macro, data struct, and declare internal used function prototype,
* which is used by secure functions.
* Create: 2014-02-25
*/
#ifndef SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
#define SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
#include "securec.h"
#if (defined(_MSC_VER)) && (_MSC_VER >= 1400)
#define SECUREC_MASK_MSVC_CRT_WARNING __pragma(warning(push)) \
__pragma(warning(disable : 4996 4127))
#define SECUREC_END_MASK_MSVC_CRT_WARNING __pragma(warning(pop))
#else
#define SECUREC_MASK_MSVC_CRT_WARNING
#define SECUREC_END_MASK_MSVC_CRT_WARNING
#endif
#define SECUREC_WHILE_ZERO SECUREC_MASK_MSVC_CRT_WARNING while (0) SECUREC_END_MASK_MSVC_CRT_WARNING
#ifndef SECUREC_HAVE_STRNLEN
#if (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L)
#if SECUREC_IN_KERNEL
#define SECUREC_HAVE_STRNLEN 0
#else
#if defined(__GLIBC__) && __GLIBC__ >= 2 && defined(__GLIBC_MINOR__) && __GLIBC_MINOR__ >= 10
#define SECUREC_HAVE_STRNLEN 1
#else
#define SECUREC_HAVE_STRNLEN 0
#endif
#endif
#else
#define SECUREC_HAVE_STRNLEN 0
#endif
#endif
#if SECUREC_IN_KERNEL
#ifndef SECUREC_ENABLE_SCANF_FILE
#define SECUREC_ENABLE_SCANF_FILE 0
#endif
#ifndef SECUREC_ENABLE_SCANF_FLOAT
#define SECUREC_ENABLE_SCANF_FLOAT 0
#endif
#ifndef SECUREC_ENABLE_SPRINTF_FLOAT
#define SECUREC_ENABLE_SPRINTF_FLOAT 0
#endif
#ifndef SECUREC_HAVE_MBTOWC
#define SECUREC_HAVE_MBTOWC 0
#endif
#ifndef SECUREC_HAVE_WCTOMB
#define SECUREC_HAVE_WCTOMB 0
#endif
#ifndef SECUREC_HAVE_WCHART
#define SECUREC_HAVE_WCHART 0
#endif
#else
#ifndef SECUREC_ENABLE_SCANF_FILE
#define SECUREC_ENABLE_SCANF_FILE 1
#endif
#ifndef SECUREC_ENABLE_SCANF_FLOAT
#define SECUREC_ENABLE_SCANF_FLOAT 1
#endif
#ifndef SECUREC_ENABLE_SPRINTF_FLOAT
#define SECUREC_ENABLE_SPRINTF_FLOAT 1
#endif
#ifndef SECUREC_HAVE_MBTOWC
#define SECUREC_HAVE_MBTOWC 1
#endif
#ifndef SECUREC_HAVE_WCTOMB
#define SECUREC_HAVE_WCTOMB 1
#endif
#ifndef SECUREC_HAVE_WCHART
#define SECUREC_HAVE_WCHART 1
#endif
#endif
#ifndef SECUREC_ENABLE_INLINE
#define SECUREC_ENABLE_INLINE 0
#endif
#ifndef SECUREC_INLINE
#if SECUREC_ENABLE_INLINE
#define SECUREC_INLINE static inline
#else
#define SECUREC_INLINE static
#endif
#endif
#ifndef SECUREC_WARP_OUTPUT
#if SECUREC_IN_KERNEL
#define SECUREC_WARP_OUTPUT 1
#else
#define SECUREC_WARP_OUTPUT 0
#endif
#endif
#ifndef SECUREC_STREAM_STDIN
#define SECUREC_STREAM_STDIN stdin
#endif
#define SECUREC_MUL_SIXTEEN(x) ((x) << 4U)
#define SECUREC_MUL_EIGHT(x) ((x) << 3U)
#define SECUREC_MUL_TEN(x) ((((x) << 2U) + (x)) << 1U)
#define SECUREC_MAX_WIDTH_LEN_DIV_TEN 21474836
#define SECUREC_MAX_WIDTH_LEN (SECUREC_MAX_WIDTH_LEN_DIV_TEN * 10)
#define SECUREC_MUL_TEN_ADD_BEYOND_MAX(x) (((x) > SECUREC_MAX_WIDTH_LEN_DIV_TEN))
#define SECUREC_FLOAT_BUFSIZE (309 + 40)
#define SECUREC_FLOAT_BUFSIZE_LB (4932 + 40)
#define SECUREC_FLOAT_DEFAULT_PRECISION 6
#define SECUREC_MEMORY_NO_OVERLAP(dest, src, count) \
(((src) < (dest) && ((const char *)(src) + (count)) <= (char *)(dest)) || \
((dest) < (src) && ((char *)(dest) + (count)) <= (const char *)(src)))
#define SECUREC_MEMORY_IS_OVERLAP(dest, src, count) \
(((src) < (dest) && ((const char *)(src) + (count)) > (char *)(dest)) || \
((dest) < (src) && ((char *)(dest) + (count)) > (const char *)(src)))
* Check whether the strings overlap, len is the length of the string not include terminator
* Length is related to data type char or wchar , do not force conversion of types
*/
#define SECUREC_STRING_NO_OVERLAP(dest, src, len) \
(((src) < (dest) && ((src) + (len)) < (dest)) || \
((dest) < (src) && ((dest) + (len)) < (src)))
* Check whether the strings overlap for strcpy wcscpy function, dest len and src Len are not include terminator
* Length is related to data type char or wchar , do not force conversion of types
*/
#define SECUREC_STRING_IS_OVERLAP(dest, src, len) \
(((src) < (dest) && ((src) + (len)) >= (dest)) || \
((dest) < (src) && ((dest) + (len)) >= (src)))
* Check whether the strings overlap for strcat wcscat function, dest len and src Len are not include terminator
* Length is related to data type char or wchar , do not force conversion of types
*/
#define SECUREC_CAT_STRING_IS_OVERLAP(dest, destLen, src, srcLen) \
(((dest) < (src) && ((dest) + (destLen) + (srcLen)) >= (src)) || \
((src) < (dest) && ((src) + (srcLen)) >= (dest)))
#if SECUREC_HAVE_STRNLEN
#define SECUREC_CALC_STR_LEN(str, maxLen, outLen) do { \
*(outLen) = strnlen((str), (maxLen)); \
} SECUREC_WHILE_ZERO
#define SECUREC_CALC_STR_LEN_OPT(str, maxLen, outLen) do { \
if ((maxLen) > 8) { \
\
if (*((str) + 0) == '\0') { \
*(outLen) = 0; \
} else if (*((str) + 1) == '\0') { \
*(outLen) = 1; \
} else if (*((str) + 2) == '\0') { \
*(outLen) = 2; \
} else if (*((str) + 3) == '\0') { \
*(outLen) = 3; \
} else if (*((str) + 4) == '\0') { \
*(outLen) = 4; \
} else if (*((str) + 5) == '\0') { \
*(outLen) = 5; \
} else if (*((str) + 6) == '\0') { \
*(outLen) = 6; \
} else if (*((str) + 7) == '\0') { \
*(outLen) = 7; \
} else if (*((str) + 8) == '\0') { \
\
*(outLen) = 8; \
} else { \
\
*(outLen) = 8 + strnlen((str) + 8, (maxLen) - 8); \
} \
} else { \
SECUREC_CALC_STR_LEN((str), (maxLen), (outLen)); \
} \
} SECUREC_WHILE_ZERO
#else
#define SECUREC_CALC_STR_LEN(str, maxLen, outLen) do { \
const char *strEnd_ = (const char *)(str); \
size_t availableSize_ = (size_t)(maxLen); \
while (availableSize_ > 0 && *strEnd_ != '\0') { \
--availableSize_; \
++strEnd_; \
} \
*(outLen) = (size_t)(strEnd_ - (str)); \
} SECUREC_WHILE_ZERO
#define SECUREC_CALC_STR_LEN_OPT SECUREC_CALC_STR_LEN
#endif
#define SECUREC_CALC_WSTR_LEN(str, maxLen, outLen) do { \
const wchar_t *strEnd_ = (const wchar_t *)(str); \
size_t len_ = 0; \
while (len_ < (maxLen) && *strEnd_ != L'\0') { \
++len_; \
++strEnd_; \
} \
*(outLen) = len_; \
} SECUREC_WHILE_ZERO
* Performance optimization, product may disable inline function.
* Using function pointer for MEMSET to prevent compiler optimization when cleaning up memory.
*/
#ifdef SECUREC_USE_ASM
#define SECUREC_MEMSET_FUNC_OPT memset_opt
#define SECUREC_MEMCPY_FUNC_OPT memcpy_opt
#else
#define SECUREC_MEMSET_FUNC_OPT memset
#define SECUREC_MEMCPY_FUNC_OPT memcpy
#endif
#define SECUREC_MEMCPY_WARP_OPT(dest, src, count) (void)SECUREC_MEMCPY_FUNC_OPT((dest), (src), (count))
#ifndef SECUREC_MEMSET_BARRIER
#if defined(__GNUC__)
#define SECUREC_MEMSET_BARRIER 1
#else
#define SECUREC_MEMSET_BARRIER 0
#endif
#endif
#ifndef SECUREC_MEMSET_INDIRECT_USE
#define SECUREC_MEMSET_INDIRECT_USE 1
#endif
#if SECUREC_MEMSET_BARRIER
#define SECUREC_MEMORY_BARRIER(dest) __asm__ __volatile__("": : "r"(dest) : "memory")
#else
#define SECUREC_MEMORY_BARRIER(dest)
#endif
#if SECUREC_MEMSET_BARRIER
#define SECUREC_MEMSET_PREVENT_DSE(dest, value, count) do { \
(void)SECUREC_MEMSET_FUNC_OPT(dest, value, count); \
SECUREC_MEMORY_BARRIER(dest); \
} SECUREC_WHILE_ZERO
#elif SECUREC_MEMSET_INDIRECT_USE
#define SECUREC_MEMSET_PREVENT_DSE(dest, value, count) do { \
void *(* const volatile fn_)(void *s_, int c_, size_t n_) = SECUREC_MEMSET_FUNC_OPT; \
(void)(*fn_)((dest), (value), (count)); \
} SECUREC_WHILE_ZERO
#else
#define SECUREC_MEMSET_PREVENT_DSE(dest, value, count) (void)SECUREC_MEMSET_FUNC_OPT((dest), (value), (count))
#endif
#ifdef SECUREC_FORMAT_OUTPUT_INPUT
#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) || defined(__ARMCC_VERSION)
typedef __int64 SecInt64;
typedef unsigned __int64 SecUnsignedInt64;
#if defined(__ARMCC_VERSION)
typedef unsigned int SecUnsignedInt32;
#else
typedef unsigned __int32 SecUnsignedInt32;
#endif
#else
typedef unsigned int SecUnsignedInt32;
typedef long long SecInt64;
typedef unsigned long long SecUnsignedInt64;
#endif
#ifdef SECUREC_FOR_WCHAR
#if defined(SECUREC_VXWORKS_PLATFORM) && !defined(__WINT_TYPE__)
typedef wchar_t wint_t;
#endif
#ifndef WEOF
#define WEOF ((wchar_t)(-1))
#endif
#define SECUREC_CHAR(x) L ## x
typedef wchar_t SecChar;
typedef wchar_t SecUnsignedChar;
typedef wint_t SecInt;
typedef wint_t SecUnsignedInt;
#else
#define SECUREC_CHAR(x) (x)
typedef char SecChar;
typedef unsigned char SecUnsignedChar;
typedef int SecInt;
typedef unsigned int SecUnsignedInt;
#endif
#endif
* Determine whether the address is 8-byte aligned
* Some systems do not have uintptr_t type, so use NULL to clear tool alarm 507
*/
#define SECUREC_ADDR_ALIGNED_8(addr) ((((size_t)(addr)) & 7U) == 0)
* If you define the memory allocation function, you need to define the function prototype.
* You can define this macro as a header file.
*/
#if defined(SECUREC_MALLOC_PROTOTYPE)
SECUREC_MALLOC_PROTOTYPE
#endif
#ifndef SECUREC_MALLOC
#define SECUREC_MALLOC(x) malloc((size_t)(x))
#endif
#ifndef SECUREC_FREE
#define SECUREC_FREE(x) free((void *)(x))
#endif
#define SECUREC_COPY_VALUE_BY_STRUCT(dest, src, n) do { \
*(SecStrBuf##n *)(void *)(dest) = *(const SecStrBuf##n *)(const void *)(src); \
} SECUREC_WHILE_ZERO
typedef struct {
unsigned char buf[2];
} SecStrBuf2;
typedef struct {
unsigned char buf[3];
} SecStrBuf3;
typedef struct {
unsigned char buf[4];
} SecStrBuf4;
typedef struct {
unsigned char buf[5];
} SecStrBuf5;
typedef struct {
unsigned char buf[6];
} SecStrBuf6;
typedef struct {
unsigned char buf[7];
} SecStrBuf7;
typedef struct {
unsigned char buf[8];
} SecStrBuf8;
typedef struct {
unsigned char buf[9];
} SecStrBuf9;
typedef struct {
unsigned char buf[10];
} SecStrBuf10;
typedef struct {
unsigned char buf[11];
} SecStrBuf11;
typedef struct {
unsigned char buf[12];
} SecStrBuf12;
typedef struct {
unsigned char buf[13];
} SecStrBuf13;
typedef struct {
unsigned char buf[14];
} SecStrBuf14;
typedef struct {
unsigned char buf[15];
} SecStrBuf15;
typedef struct {
unsigned char buf[16];
} SecStrBuf16;
typedef struct {
unsigned char buf[17];
} SecStrBuf17;
typedef struct {
unsigned char buf[18];
} SecStrBuf18;
typedef struct {
unsigned char buf[19];
} SecStrBuf19;
typedef struct {
unsigned char buf[20];
} SecStrBuf20;
typedef struct {
unsigned char buf[21];
} SecStrBuf21;
typedef struct {
unsigned char buf[22];
} SecStrBuf22;
typedef struct {
unsigned char buf[23];
} SecStrBuf23;
typedef struct {
unsigned char buf[24];
} SecStrBuf24;
typedef struct {
unsigned char buf[25];
} SecStrBuf25;
typedef struct {
unsigned char buf[26];
} SecStrBuf26;
typedef struct {
unsigned char buf[27];
} SecStrBuf27;
typedef struct {
unsigned char buf[28];
} SecStrBuf28;
typedef struct {
unsigned char buf[29];
} SecStrBuf29;
typedef struct {
unsigned char buf[30];
} SecStrBuf30;
typedef struct {
unsigned char buf[31];
} SecStrBuf31;
typedef struct {
unsigned char buf[32];
} SecStrBuf32;
typedef struct {
unsigned char buf[33];
} SecStrBuf33;
typedef struct {
unsigned char buf[34];
} SecStrBuf34;
typedef struct {
unsigned char buf[35];
} SecStrBuf35;
typedef struct {
unsigned char buf[36];
} SecStrBuf36;
typedef struct {
unsigned char buf[37];
} SecStrBuf37;
typedef struct {
unsigned char buf[38];
} SecStrBuf38;
typedef struct {
unsigned char buf[39];
} SecStrBuf39;
typedef struct {
unsigned char buf[40];
} SecStrBuf40;
typedef struct {
unsigned char buf[41];
} SecStrBuf41;
typedef struct {
unsigned char buf[42];
} SecStrBuf42;
typedef struct {
unsigned char buf[43];
} SecStrBuf43;
typedef struct {
unsigned char buf[44];
} SecStrBuf44;
typedef struct {
unsigned char buf[45];
} SecStrBuf45;
typedef struct {
unsigned char buf[46];
} SecStrBuf46;
typedef struct {
unsigned char buf[47];
} SecStrBuf47;
typedef struct {
unsigned char buf[48];
} SecStrBuf48;
typedef struct {
unsigned char buf[49];
} SecStrBuf49;
typedef struct {
unsigned char buf[50];
} SecStrBuf50;
typedef struct {
unsigned char buf[51];
} SecStrBuf51;
typedef struct {
unsigned char buf[52];
} SecStrBuf52;
typedef struct {
unsigned char buf[53];
} SecStrBuf53;
typedef struct {
unsigned char buf[54];
} SecStrBuf54;
typedef struct {
unsigned char buf[55];
} SecStrBuf55;
typedef struct {
unsigned char buf[56];
} SecStrBuf56;
typedef struct {
unsigned char buf[57];
} SecStrBuf57;
typedef struct {
unsigned char buf[58];
} SecStrBuf58;
typedef struct {
unsigned char buf[59];
} SecStrBuf59;
typedef struct {
unsigned char buf[60];
} SecStrBuf60;
typedef struct {
unsigned char buf[61];
} SecStrBuf61;
typedef struct {
unsigned char buf[62];
} SecStrBuf62;
typedef struct {
unsigned char buf[63];
} SecStrBuf63;
typedef struct {
unsigned char buf[64];
} SecStrBuf64;
* User can change the error handler by modify the following definition,
* such as logging the detail error in file.
*/
#if defined(_DEBUG) || defined(DEBUG)
#if defined(SECUREC_ERROR_HANDLER_BY_ASSERT)
#define SECUREC_ERROR_INVALID_PARAMTER(msg) assert(msg "invalid argument" == NULL)
#define SECUREC_ERROR_INVALID_RANGE(msg) assert(msg "invalid dest buffer size" == NULL)
#define SECUREC_ERROR_BUFFER_OVERLAP(msg) assert(msg "buffer overlap" == NULL)
#elif defined(SECUREC_ERROR_HANDLER_BY_PRINTF)
#if SECUREC_IN_KERNEL
#define SECUREC_ERROR_INVALID_PARAMTER(msg) printk("%s invalid argument\n", msg)
#define SECUREC_ERROR_INVALID_RANGE(msg) printk("%s invalid dest buffer size\n", msg)
#define SECUREC_ERROR_BUFFER_OVERLAP(msg) printk("%s buffer overlap\n", msg)
#else
#define SECUREC_ERROR_INVALID_PARAMTER(msg) printf("%s invalid argument\n", msg)
#define SECUREC_ERROR_INVALID_RANGE(msg) printf("%s invalid dest buffer size\n", msg)
#define SECUREC_ERROR_BUFFER_OVERLAP(msg) printf("%s buffer overlap\n", msg)
#endif
#elif defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
#define SECUREC_ERROR_INVALID_PARAMTER(msg) LogSecureCRuntimeError(msg " EINVAL\n")
#define SECUREC_ERROR_INVALID_RANGE(msg) LogSecureCRuntimeError(msg " ERANGE\n")
#define SECUREC_ERROR_BUFFER_OVERLAP(msg) LogSecureCRuntimeError(msg " EOVERLAP\n")
#endif
#endif
#ifndef SECUREC_ERROR_INVALID_PARAMTER
#define SECUREC_ERROR_INVALID_PARAMTER(msg)
#endif
#ifndef SECUREC_ERROR_INVALID_RANGE
#define SECUREC_ERROR_INVALID_RANGE(msg)
#endif
#ifndef SECUREC_ERROR_BUFFER_OVERLAP
#define SECUREC_ERROR_BUFFER_OVERLAP(msg)
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef SECUREC_USE_ASM
void *memcpy_opt(void *dest, const void *src, size_t n);
void *memset_opt(void *s, int c, size_t n);
#endif
#if defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
void LogSecureCRuntimeError(const char *errDetail);
#endif
#ifdef __cplusplus
}
#endif
#endif