16#ifndef SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
17#define SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
20#if (defined(_MSC_VER)) && (_MSC_VER >= 1400)
22#define SECUREC_MASK_MSVC_CRT_WARNING __pragma(warning(push)) \
23 __pragma(warning(disable : 4996 4127))
24#define SECUREC_END_MASK_MSVC_CRT_WARNING __pragma(warning(pop))
26#define SECUREC_MASK_MSVC_CRT_WARNING
27#define SECUREC_END_MASK_MSVC_CRT_WARNING
29#define SECUREC_WHILE_ZERO SECUREC_MASK_MSVC_CRT_WARNING while (0) SECUREC_END_MASK_MSVC_CRT_WARNING
32#ifndef SECUREC_HAVE_STRNLEN
33#if (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L)
35#define SECUREC_HAVE_STRNLEN 0
37#if defined(__GLIBC__) && __GLIBC__ >= 2 && defined(__GLIBC_MINOR__) && __GLIBC_MINOR__ >= 10
38#define SECUREC_HAVE_STRNLEN 1
40#define SECUREC_HAVE_STRNLEN 0
44#define SECUREC_HAVE_STRNLEN 0
50#ifndef SECUREC_ENABLE_SCANF_FILE
51#define SECUREC_ENABLE_SCANF_FILE 0
53#ifndef SECUREC_ENABLE_SCANF_FLOAT
54#define SECUREC_ENABLE_SCANF_FLOAT 0
56#ifndef SECUREC_ENABLE_SPRINTF_FLOAT
57#define SECUREC_ENABLE_SPRINTF_FLOAT 0
59#ifndef SECUREC_HAVE_MBTOWC
60#define SECUREC_HAVE_MBTOWC 0
62#ifndef SECUREC_HAVE_WCTOMB
63#define SECUREC_HAVE_WCTOMB 0
65#ifndef SECUREC_HAVE_WCHART
66#define SECUREC_HAVE_WCHART 0
70#ifndef SECUREC_ENABLE_SCANF_FILE
71#define SECUREC_ENABLE_SCANF_FILE 1
73#ifndef SECUREC_ENABLE_SCANF_FLOAT
74#define SECUREC_ENABLE_SCANF_FLOAT 1
77#ifndef SECUREC_ENABLE_SPRINTF_FLOAT
78#define SECUREC_ENABLE_SPRINTF_FLOAT 1
80#ifndef SECUREC_HAVE_MBTOWC
81#define SECUREC_HAVE_MBTOWC 1
83#ifndef SECUREC_HAVE_WCTOMB
84#define SECUREC_HAVE_WCTOMB 1
86#ifndef SECUREC_HAVE_WCHART
87#define SECUREC_HAVE_WCHART 1
91#ifndef SECUREC_ENABLE_INLINE
92#define SECUREC_ENABLE_INLINE 0
96#if SECUREC_ENABLE_INLINE
97#define SECUREC_INLINE static inline
99#define SECUREC_INLINE static
103#ifndef SECUREC_WARP_OUTPUT
105#define SECUREC_WARP_OUTPUT 1
107#define SECUREC_WARP_OUTPUT 0
111#ifndef SECUREC_STREAM_STDIN
112#define SECUREC_STREAM_STDIN stdin
115#define SECUREC_MUL_SIXTEEN(x) ((x) << 4U)
116#define SECUREC_MUL_EIGHT(x) ((x) << 3U)
117#define SECUREC_MUL_TEN(x) ((((x) << 2U) + (x)) << 1U)
119#define SECUREC_MAX_WIDTH_LEN_DIV_TEN 21474836
120#define SECUREC_MAX_WIDTH_LEN (SECUREC_MAX_WIDTH_LEN_DIV_TEN * 10)
122#define SECUREC_MUL_TEN_ADD_BEYOND_MAX(x) (((x) > SECUREC_MAX_WIDTH_LEN_DIV_TEN))
124#define SECUREC_FLOAT_BUFSIZE (309 + 40)
125#define SECUREC_FLOAT_BUFSIZE_LB (4932 + 40)
126#define SECUREC_FLOAT_DEFAULT_PRECISION 6
129#define SECUREC_MEMORY_NO_OVERLAP(dest, src, count) \
130 (((src) < (dest) && ((const char *)(src) + (count)) <= (char *)(dest)) || \
131 ((dest) < (src) && ((char *)(dest) + (count)) <= (const char *)(src)))
133#define SECUREC_MEMORY_IS_OVERLAP(dest, src, count) \
134 (((src) < (dest) && ((const char *)(src) + (count)) > (char *)(dest)) || \
135 ((dest) < (src) && ((char *)(dest) + (count)) > (const char *)(src)))
141#define SECUREC_STRING_NO_OVERLAP(dest, src, len) \
142 (((src) < (dest) && ((src) + (len)) < (dest)) || \
143 ((dest) < (src) && ((dest) + (len)) < (src)))
149#define SECUREC_STRING_IS_OVERLAP(dest, src, len) \
150 (((src) < (dest) && ((src) + (len)) >= (dest)) || \
151 ((dest) < (src) && ((dest) + (len)) >= (src)))
157#define SECUREC_CAT_STRING_IS_OVERLAP(dest, destLen, src, srcLen) \
158 (((dest) < (src) && ((dest) + (destLen) + (srcLen)) >= (src)) || \
159 ((src) < (dest) && ((src) + (srcLen)) >= (dest)))
161#if SECUREC_HAVE_STRNLEN
162#define SECUREC_CALC_STR_LEN(str, maxLen, outLen) do { \
163 *(outLen) = strnlen((str), (maxLen)); \
165#define SECUREC_CALC_STR_LEN_OPT(str, maxLen, outLen) do { \
166 if ((maxLen) > 8) { \
168 if (*((str) + 0) == '\0') { \
170 } else if (*((str) + 1) == '\0') { \
172 } else if (*((str) + 2) == '\0') { \
174 } else if (*((str) + 3) == '\0') { \
176 } else if (*((str) + 4) == '\0') { \
178 } else if (*((str) + 5) == '\0') { \
180 } else if (*((str) + 6) == '\0') { \
182 } else if (*((str) + 7) == '\0') { \
184 } else if (*((str) + 8) == '\0') { \
189 *(outLen) = 8 + strnlen((str) + 8, (maxLen) - 8); \
192 SECUREC_CALC_STR_LEN((str), (maxLen), (outLen)); \
196#define SECUREC_CALC_STR_LEN(str, maxLen, outLen) do { \
197 const char *strEnd_ = (const char *)(str); \
198 size_t availableSize_ = (size_t)(maxLen); \
199 while (availableSize_ > 0 && *strEnd_ != '\0') { \
203 *(outLen) = (size_t)(strEnd_ - (str)); \
205#define SECUREC_CALC_STR_LEN_OPT SECUREC_CALC_STR_LEN
208#define SECUREC_CALC_WSTR_LEN(str, maxLen, outLen) do { \
209 const wchar_t *strEnd_ = (const wchar_t *)(str); \
211 while (len_ < (maxLen) && *strEnd_ != L'\0') { \
222#ifdef SECUREC_USE_ASM
223#define SECUREC_MEMSET_FUNC_OPT memset_opt
224#define SECUREC_MEMCPY_FUNC_OPT memcpy_opt
226#define SECUREC_MEMSET_FUNC_OPT memset
227#define SECUREC_MEMCPY_FUNC_OPT memcpy
230#define SECUREC_MEMCPY_WARP_OPT(dest, src, count) (void)SECUREC_MEMCPY_FUNC_OPT((dest), (src), (count))
232#ifndef SECUREC_MEMSET_BARRIER
235#define SECUREC_MEMSET_BARRIER 1
237#define SECUREC_MEMSET_BARRIER 0
241#ifndef SECUREC_MEMSET_INDIRECT_USE
243#define SECUREC_MEMSET_INDIRECT_USE 1
246#if SECUREC_MEMSET_BARRIER
247#define SECUREC_MEMORY_BARRIER(dest) __asm__ __volatile__("": : "r"(dest) : "memory")
249#define SECUREC_MEMORY_BARRIER(dest)
252#if SECUREC_MEMSET_BARRIER
253#define SECUREC_MEMSET_PREVENT_DSE(dest, value, count) do { \
254 (void)SECUREC_MEMSET_FUNC_OPT(dest, value, count); \
255 SECUREC_MEMORY_BARRIER(dest); \
257#elif SECUREC_MEMSET_INDIRECT_USE
258#define SECUREC_MEMSET_PREVENT_DSE(dest, value, count) do { \
259 void *(* const volatile fn_)(void *s_, int c_, size_t n_) = SECUREC_MEMSET_FUNC_OPT; \
260 (void)(*fn_)((dest), (value), (count)); \
263#define SECUREC_MEMSET_PREVENT_DSE(dest, value, count) (void)SECUREC_MEMSET_FUNC_OPT((dest), (value), (count))
266#ifdef SECUREC_FORMAT_OUTPUT_INPUT
267#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) || defined(__ARMCC_VERSION)
268typedef __int64 SecInt64;
269typedef unsigned __int64 SecUnsignedInt64;
270#if defined(__ARMCC_VERSION)
271typedef unsigned int SecUnsignedInt32;
273typedef unsigned __int32 SecUnsignedInt32;
276typedef unsigned int SecUnsignedInt32;
277typedef long long SecInt64;
278typedef unsigned long long SecUnsignedInt64;
281#ifdef SECUREC_FOR_WCHAR
282#if defined(SECUREC_VXWORKS_PLATFORM) && !defined(__WINT_TYPE__)
283typedef wchar_t wint_t;
286#define WEOF ((wchar_t)(-1))
288#define SECUREC_CHAR(x) L ## x
289typedef wchar_t SecChar;
290typedef wchar_t SecUnsignedChar;
291typedef wint_t SecInt;
292typedef wint_t SecUnsignedInt;
294#define SECUREC_CHAR(x) (x)
296typedef unsigned char SecUnsignedChar;
298typedef unsigned int SecUnsignedInt;
306#define SECUREC_ADDR_ALIGNED_8(addr) ((((size_t)(addr)) & 7U) == 0)
312#if defined(SECUREC_MALLOC_PROTOTYPE)
313SECUREC_MALLOC_PROTOTYPE
316#ifndef SECUREC_MALLOC
317#define SECUREC_MALLOC(x) malloc((size_t)(x))
321#define SECUREC_FREE(x) free((void *)(x))
325#define SECUREC_COPY_VALUE_BY_STRUCT(dest, src, n) do { \
326 *(SecStrBuf##n *)(void *)(dest) = *(const SecStrBuf##n *)(const void *)(src); \
330 unsigned char buf[2];
333 unsigned char buf[3];
336 unsigned char buf[4];
339 unsigned char buf[5];
342 unsigned char buf[6];
345 unsigned char buf[7];
348 unsigned char buf[8];
351 unsigned char buf[9];
354 unsigned char buf[10];
357 unsigned char buf[11];
360 unsigned char buf[12];
363 unsigned char buf[13];
366 unsigned char buf[14];
369 unsigned char buf[15];
372 unsigned char buf[16];
375 unsigned char buf[17];
378 unsigned char buf[18];
381 unsigned char buf[19];
384 unsigned char buf[20];
387 unsigned char buf[21];
390 unsigned char buf[22];
393 unsigned char buf[23];
396 unsigned char buf[24];
399 unsigned char buf[25];
402 unsigned char buf[26];
405 unsigned char buf[27];
408 unsigned char buf[28];
411 unsigned char buf[29];
414 unsigned char buf[30];
417 unsigned char buf[31];
420 unsigned char buf[32];
423 unsigned char buf[33];
426 unsigned char buf[34];
429 unsigned char buf[35];
432 unsigned char buf[36];
435 unsigned char buf[37];
438 unsigned char buf[38];
441 unsigned char buf[39];
444 unsigned char buf[40];
447 unsigned char buf[41];
450 unsigned char buf[42];
453 unsigned char buf[43];
456 unsigned char buf[44];
459 unsigned char buf[45];
462 unsigned char buf[46];
465 unsigned char buf[47];
468 unsigned char buf[48];
471 unsigned char buf[49];
474 unsigned char buf[50];
477 unsigned char buf[51];
480 unsigned char buf[52];
483 unsigned char buf[53];
486 unsigned char buf[54];
489 unsigned char buf[55];
492 unsigned char buf[56];
495 unsigned char buf[57];
498 unsigned char buf[58];
501 unsigned char buf[59];
504 unsigned char buf[60];
507 unsigned char buf[61];
510 unsigned char buf[62];
513 unsigned char buf[63];
516 unsigned char buf[64];
523#if defined(_DEBUG) || defined(DEBUG)
524#if defined(SECUREC_ERROR_HANDLER_BY_ASSERT)
525#define SECUREC_ERROR_INVALID_PARAMTER(msg) assert(msg "invalid argument" == NULL)
526#define SECUREC_ERROR_INVALID_RANGE(msg) assert(msg "invalid dest buffer size" == NULL)
527#define SECUREC_ERROR_BUFFER_OVERLAP(msg) assert(msg "buffer overlap" == NULL)
528#elif defined(SECUREC_ERROR_HANDLER_BY_PRINTF)
530#define SECUREC_ERROR_INVALID_PARAMTER(msg) printk("%s invalid argument\n", msg)
531#define SECUREC_ERROR_INVALID_RANGE(msg) printk("%s invalid dest buffer size\n", msg)
532#define SECUREC_ERROR_BUFFER_OVERLAP(msg) printk("%s buffer overlap\n", msg)
534#define SECUREC_ERROR_INVALID_PARAMTER(msg) printf("%s invalid argument\n", msg)
535#define SECUREC_ERROR_INVALID_RANGE(msg) printf("%s invalid dest buffer size\n", msg)
536#define SECUREC_ERROR_BUFFER_OVERLAP(msg) printf("%s buffer overlap\n", msg)
538#elif defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
539#define SECUREC_ERROR_INVALID_PARAMTER(msg) LogSecureCRuntimeError(msg " EINVAL\n")
540#define SECUREC_ERROR_INVALID_RANGE(msg) LogSecureCRuntimeError(msg " ERANGE\n")
541#define SECUREC_ERROR_BUFFER_OVERLAP(msg) LogSecureCRuntimeError(msg " EOVERLAP\n")
546#ifndef SECUREC_ERROR_INVALID_PARAMTER
547#define SECUREC_ERROR_INVALID_PARAMTER(msg)
549#ifndef SECUREC_ERROR_INVALID_RANGE
550#define SECUREC_ERROR_INVALID_RANGE(msg)
552#ifndef SECUREC_ERROR_BUFFER_OVERLAP
553#define SECUREC_ERROR_BUFFER_OVERLAP(msg)
561#ifdef SECUREC_USE_ASM
562void *memcpy_opt(
void *dest,
const void *src,
size_t n);
563void *memset_opt(
void *s,
int c,
size_t n);
566#if defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
567void LogSecureCRuntimeError(
const char *errDetail);
Definition securecutil.h:353
Definition securecutil.h:356
Definition securecutil.h:359
Definition securecutil.h:362
Definition securecutil.h:365
Definition securecutil.h:368
Definition securecutil.h:371
Definition securecutil.h:374
Definition securecutil.h:377
Definition securecutil.h:380
Definition securecutil.h:383
Definition securecutil.h:386
Definition securecutil.h:389
Definition securecutil.h:392
Definition securecutil.h:395
Definition securecutil.h:398
Definition securecutil.h:401
Definition securecutil.h:404
Definition securecutil.h:407
Definition securecutil.h:410
Definition securecutil.h:329
Definition securecutil.h:413
Definition securecutil.h:416
Definition securecutil.h:419
Definition securecutil.h:422
Definition securecutil.h:425
Definition securecutil.h:428
Definition securecutil.h:431
Definition securecutil.h:434
Definition securecutil.h:437
Definition securecutil.h:440
Definition securecutil.h:332
Definition securecutil.h:443
Definition securecutil.h:446
Definition securecutil.h:449
Definition securecutil.h:452
Definition securecutil.h:455
Definition securecutil.h:458
Definition securecutil.h:461
Definition securecutil.h:464
Definition securecutil.h:467
Definition securecutil.h:470
Definition securecutil.h:335
Definition securecutil.h:473
Definition securecutil.h:476
Definition securecutil.h:479
Definition securecutil.h:482
Definition securecutil.h:485
Definition securecutil.h:488
Definition securecutil.h:491
Definition securecutil.h:494
Definition securecutil.h:497
Definition securecutil.h:500
Definition securecutil.h:338
Definition securecutil.h:503
Definition securecutil.h:506
Definition securecutil.h:509
Definition securecutil.h:512
Definition securecutil.h:515
Definition securecutil.h:341
Definition securecutil.h:344
Definition securecutil.h:347
Definition securecutil.h:350