• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. 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, data struct, and declare internal used function prototype,
12  *              which is used by secure functions.
13  * Create: 2014-02-25
14  */
15 
16 #ifndef SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
17 #define SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
18 #include "securec.h"
19 
20 #if (defined(_MSC_VER)) && (_MSC_VER >= 1400)
21 /* Shield compilation alerts using discarded functions and Constant  expression to maximize code compatibility */
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))
25 #else
26 #define SECUREC_MASK_MSVC_CRT_WARNING
27 #define SECUREC_END_MASK_MSVC_CRT_WARNING
28 #endif
29 #define SECUREC_WHILE_ZERO SECUREC_MASK_MSVC_CRT_WARNING while (0) SECUREC_END_MASK_MSVC_CRT_WARNING
30 
31 /* Automatically identify the platform that supports strnlen function, and use this function to improve performance */
32 #ifndef SECUREC_HAVE_STRNLEN
33 #if (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L)
34 #if SECUREC_IN_KERNEL
35 #define SECUREC_HAVE_STRNLEN 0
36 #else
37 #if defined(__GLIBC__) && __GLIBC__ >= 2 && defined(__GLIBC_MINOR__) && __GLIBC_MINOR__ >= 10
38 #define SECUREC_HAVE_STRNLEN 1
39 #else
40 #define SECUREC_HAVE_STRNLEN 0
41 #endif
42 #endif
43 #else
44 #define SECUREC_HAVE_STRNLEN 0
45 #endif
46 #endif
47 
48 #if SECUREC_IN_KERNEL
49 /* In kernel disable functions */
50 #ifndef SECUREC_ENABLE_SCANF_FILE
51 #define SECUREC_ENABLE_SCANF_FILE 0
52 #endif
53 #ifndef SECUREC_ENABLE_SCANF_FLOAT
54 #define SECUREC_ENABLE_SCANF_FLOAT 0
55 #endif
56 #ifndef SECUREC_ENABLE_SPRINTF_FLOAT
57 #define SECUREC_ENABLE_SPRINTF_FLOAT 0
58 #endif
59 #ifndef SECUREC_HAVE_MBTOWC
60 #define SECUREC_HAVE_MBTOWC 0
61 #endif
62 #ifndef SECUREC_HAVE_WCTOMB
63 #define SECUREC_HAVE_WCTOMB 0
64 #endif
65 #ifndef SECUREC_HAVE_WCHART
66 #define SECUREC_HAVE_WCHART 0
67 #endif
68 #else /* Not in kernel */
69 /* Systems that do not support file, can define this macro to 0. */
70 #ifndef SECUREC_ENABLE_SCANF_FILE
71 #define SECUREC_ENABLE_SCANF_FILE 1
72 #endif
73 #ifndef SECUREC_ENABLE_SCANF_FLOAT
74 #define SECUREC_ENABLE_SCANF_FLOAT 1
75 #endif
76 /* Systems that do not support float, can define this macro to 0. */
77 #ifndef SECUREC_ENABLE_SPRINTF_FLOAT
78 #define SECUREC_ENABLE_SPRINTF_FLOAT 1
79 #endif
80 #ifndef SECUREC_HAVE_MBTOWC
81 #define SECUREC_HAVE_MBTOWC 1
82 #endif
83 #ifndef SECUREC_HAVE_WCTOMB
84 #define SECUREC_HAVE_WCTOMB 1
85 #endif
86 #ifndef SECUREC_HAVE_WCHART
87 #define SECUREC_HAVE_WCHART 1
88 #endif
89 #endif
90 
91 #ifndef SECUREC_ENABLE_INLINE
92 #define SECUREC_ENABLE_INLINE 0
93 #endif
94 
95 #ifndef SECUREC_INLINE
96 #if SECUREC_ENABLE_INLINE
97 #define SECUREC_INLINE static inline
98 #else
99 #define SECUREC_INLINE static
100 #endif
101 #endif
102 
103 #ifndef SECUREC_WARP_OUTPUT
104 #if SECUREC_IN_KERNEL
105 #define SECUREC_WARP_OUTPUT 1
106 #else
107 #define SECUREC_WARP_OUTPUT 0
108 #endif
109 #endif
110 
111 #ifndef SECUREC_STREAM_STDIN
112 #define SECUREC_STREAM_STDIN stdin
113 #endif
114 
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)
118 /* Limited format input and output width, use signed integer */
119 #define SECUREC_MAX_WIDTH_LEN_DIV_TEN       21474836
120 #define SECUREC_MAX_WIDTH_LEN               (SECUREC_MAX_WIDTH_LEN_DIV_TEN * 10)
121 /* Is the x multiplied by 10 greater than */
122 #define SECUREC_MUL_TEN_ADD_BEYOND_MAX(x)   (((x) > SECUREC_MAX_WIDTH_LEN_DIV_TEN))
123 
124 #define SECUREC_FLOAT_BUFSIZE               (309 + 40)  /* Max length of double value */
125 #define SECUREC_FLOAT_BUFSIZE_LB            (4932 + 40) /* Max length of long double value */
126 #define SECUREC_FLOAT_DEFAULT_PRECISION     6
127 
128 /* This macro does not handle pointer equality or integer overflow */
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)))
132 
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)))
136 
137 /*
138  * Check whether the strings overlap, len is the length of the string not include terminator
139  * Length is related to data type char or wchar , do not force conversion of types
140  */
141 #define SECUREC_STRING_NO_OVERLAP(dest, src, len) \
142     (((src) < (dest) && ((src) + (len)) < (dest)) || \
143     ((dest) < (src) && ((dest) + (len)) < (src)))
144 
145 /*
146  * Check whether the strings overlap for strcpy wcscpy function, dest len and src Len are not include terminator
147  * Length is related to data type char or wchar , do not force conversion of types
148  */
149 #define SECUREC_STRING_IS_OVERLAP(dest, src, len) \
150     (((src) < (dest) && ((src) + (len)) >= (dest)) || \
151     ((dest) < (src) && ((dest) + (len)) >= (src)))
152 
153 /*
154  * Check whether the strings overlap for strcat wcscat function, dest len and src Len are not include terminator
155  * Length is related to data type char or wchar , do not force conversion of types
156  */
157 #define SECUREC_CAT_STRING_IS_OVERLAP(dest, destLen, src, srcLen) \
158     (((dest) < (src) && ((dest) + (destLen) + (srcLen)) >= (src)) || \
159     ((src) < (dest) && ((src) + (srcLen)) >= (dest)))
160 
161 #if SECUREC_HAVE_STRNLEN
162 #define SECUREC_CALC_STR_LEN(str, maxLen, outLen) do { \
163     *(outLen) = strnlen((str), (maxLen)); \
164 } SECUREC_WHILE_ZERO
165 #define SECUREC_CALC_STR_LEN_OPT(str, maxLen, outLen) do { \
166     if ((maxLen) > 8) { \
167         /* Optimization or len less then 8 */ \
168         if (*((str) + 0) == '\0') { \
169             *(outLen) = 0; \
170         } else if (*((str) + 1) == '\0') { \
171             *(outLen) = 1; \
172         } else if (*((str) + 2) == '\0') { \
173             *(outLen) = 2; \
174         } else if (*((str) + 3) == '\0') { \
175             *(outLen) = 3; \
176         } else if (*((str) + 4) == '\0') { \
177             *(outLen) = 4; \
178         } else if (*((str) + 5) == '\0') { \
179             *(outLen) = 5; \
180         } else if (*((str) + 6) == '\0') { \
181             *(outLen) = 6; \
182         } else if (*((str) + 7) == '\0') { \
183             *(outLen) = 7; \
184         } else if (*((str) + 8) == '\0') { \
185             /* Optimization with a length of 8 */ \
186             *(outLen) = 8; \
187         } else { \
188             /* The offset is 8 because the performance of 8 byte alignment is high */ \
189             *(outLen) = 8 + strnlen((str) + 8, (maxLen) - 8); \
190         } \
191     } else { \
192         SECUREC_CALC_STR_LEN((str), (maxLen), (outLen)); \
193     } \
194 } SECUREC_WHILE_ZERO
195 #else
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') { \
200         --availableSize_; \
201         ++strEnd_; \
202     } \
203     *(outLen) = (size_t)(strEnd_ - (str)); \
204 } SECUREC_WHILE_ZERO
205 #define SECUREC_CALC_STR_LEN_OPT SECUREC_CALC_STR_LEN
206 #endif
207 
208 #define SECUREC_CALC_WSTR_LEN(str, maxLen, outLen) do { \
209     const wchar_t *strEnd_ = (const wchar_t *)(str); \
210     size_t len_ = 0; \
211     while (len_ < (maxLen) && *strEnd_ != L'\0') { \
212         ++len_; \
213         ++strEnd_; \
214     } \
215     *(outLen) = len_; \
216 } SECUREC_WHILE_ZERO
217 
218 /*
219  * Performance optimization, product may disable inline function.
220  * Using function pointer for MEMSET to prevent compiler optimization when cleaning up memory.
221  */
222 #ifdef SECUREC_USE_ASM
223 #define SECUREC_MEMSET_FUNC_OPT  memset_opt
224 #define SECUREC_MEMCPY_FUNC_OPT  memcpy_opt
225 #else
226 #define SECUREC_MEMSET_FUNC_OPT  memset
227 #define SECUREC_MEMCPY_FUNC_OPT  memcpy
228 #endif
229 
230 #define SECUREC_MEMCPY_WARP_OPT(dest, src, count)    (void)SECUREC_MEMCPY_FUNC_OPT((dest), (src), (count))
231 
232 #ifndef SECUREC_MEMSET_BARRIER
233 #if defined(__GNUC__)
234 /* Can be turned off for scenarios that do not use memory barrier */
235 #define SECUREC_MEMSET_BARRIER 1
236 #else
237 #define SECUREC_MEMSET_BARRIER 0
238 #endif
239 #endif
240 
241 #ifndef SECUREC_MEMSET_INDIRECT_USE
242 /* Can be turned off for scenarios that do not allow pointer calls */
243 #define SECUREC_MEMSET_INDIRECT_USE 1
244 #endif
245 
246 #if SECUREC_MEMSET_BARRIER
247 #define SECUREC_MEMORY_BARRIER(dest) __asm__ __volatile__("": : "r"(dest) : "memory")
248 #else
249 #define SECUREC_MEMORY_BARRIER(dest)
250 #endif
251 
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); \
256 } SECUREC_WHILE_ZERO
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)); \
261 } SECUREC_WHILE_ZERO
262 #else
263 #define SECUREC_MEMSET_PREVENT_DSE(dest, value, count)  (void)SECUREC_MEMSET_FUNC_OPT((dest), (value), (count))
264 #endif
265 
266 #ifdef SECUREC_FORMAT_OUTPUT_INPUT
267 #if defined(SECUREC_COMPATIBLE_WIN_FORMAT) || defined(__ARMCC_VERSION)
268 typedef __int64 SecInt64;
269 typedef unsigned __int64 SecUnsignedInt64;
270 #if defined(__ARMCC_VERSION)
271 typedef unsigned int SecUnsignedInt32;
272 #else
273 typedef unsigned __int32 SecUnsignedInt32;
274 #endif
275 #else
276 typedef unsigned int SecUnsignedInt32;
277 typedef long long SecInt64;
278 typedef unsigned long long SecUnsignedInt64;
279 #endif
280 
281 #ifdef SECUREC_FOR_WCHAR
282 #if defined(SECUREC_VXWORKS_PLATFORM) && !defined(__WINT_TYPE__)
283 typedef wchar_t wint_t;
284 #endif
285 #ifndef WEOF
286 #define WEOF ((wchar_t)(-1))
287 #endif
288 #define SECUREC_CHAR(x) L ## x
289 typedef wchar_t SecChar;
290 typedef wchar_t SecUnsignedChar;
291 typedef wint_t SecInt;
292 typedef wint_t SecUnsignedInt;
293 #else /*  no SECUREC_FOR_WCHAR */
294 #define SECUREC_CHAR(x) (x)
295 typedef char SecChar;
296 typedef unsigned char SecUnsignedChar;
297 typedef int SecInt;
298 typedef unsigned int SecUnsignedInt;
299 #endif
300 #endif
301 
302 /*
303  * Determine whether the address is 8-byte aligned
304  * Some systems do not have uintptr_t type, so  use NULL to clear tool alarm 507
305  */
306 #define SECUREC_ADDR_ALIGNED_8(addr) ((((size_t)(addr)) & 7U) == 0) /* Use 7 to check aligned 8 */
307 
308 /*
309  * If you define the memory allocation function, you need to define the function prototype.
310  * You can define this macro as a header file.
311  */
312 #if defined(SECUREC_MALLOC_PROTOTYPE)
313 SECUREC_MALLOC_PROTOTYPE
314 #endif
315 
316 #ifndef SECUREC_MALLOC
317 #define SECUREC_MALLOC(x) malloc((size_t)(x))
318 #endif
319 
320 #ifndef SECUREC_FREE
321 #define SECUREC_FREE(x)   free((void *)(x))
322 #endif
323 
324 /* Improve performance with struct assignment, buf1 is not defined to avoid tool false positive */
325 #define SECUREC_COPY_VALUE_BY_STRUCT(dest, src, n) do { \
326     *(SecStrBuf##n *)(void *)(dest) = *(const SecStrBuf##n *)(const void *)(src); \
327 } SECUREC_WHILE_ZERO
328 
329 typedef struct {
330     unsigned char buf[2]; /* Performance optimization code structure assignment length 2 bytes */
331 } SecStrBuf2;
332 typedef struct {
333     unsigned char buf[3]; /* Performance optimization code structure assignment length 3 bytes */
334 } SecStrBuf3;
335 typedef struct {
336     unsigned char buf[4]; /* Performance optimization code structure assignment length 4 bytes */
337 } SecStrBuf4;
338 typedef struct {
339     unsigned char buf[5]; /* Performance optimization code structure assignment length 5 bytes */
340 } SecStrBuf5;
341 typedef struct {
342     unsigned char buf[6]; /* Performance optimization code structure assignment length 6 bytes */
343 } SecStrBuf6;
344 typedef struct {
345     unsigned char buf[7]; /* Performance optimization code structure assignment length 7 bytes */
346 } SecStrBuf7;
347 typedef struct {
348     unsigned char buf[8]; /* Performance optimization code structure assignment length 8 bytes */
349 } SecStrBuf8;
350 typedef struct {
351     unsigned char buf[9]; /* Performance optimization code structure assignment length 9 bytes */
352 } SecStrBuf9;
353 typedef struct {
354     unsigned char buf[10]; /* Performance optimization code structure assignment length 10 bytes */
355 } SecStrBuf10;
356 typedef struct {
357     unsigned char buf[11]; /* Performance optimization code structure assignment length 11 bytes */
358 } SecStrBuf11;
359 typedef struct {
360     unsigned char buf[12]; /* Performance optimization code structure assignment length 12 bytes */
361 } SecStrBuf12;
362 typedef struct {
363     unsigned char buf[13]; /* Performance optimization code structure assignment length 13 bytes */
364 } SecStrBuf13;
365 typedef struct {
366     unsigned char buf[14]; /* Performance optimization code structure assignment length 14 bytes */
367 } SecStrBuf14;
368 typedef struct {
369     unsigned char buf[15]; /* Performance optimization code structure assignment length 15 bytes */
370 } SecStrBuf15;
371 typedef struct {
372     unsigned char buf[16]; /* Performance optimization code structure assignment length 16 bytes */
373 } SecStrBuf16;
374 typedef struct {
375     unsigned char buf[17]; /* Performance optimization code structure assignment length 17 bytes */
376 } SecStrBuf17;
377 typedef struct {
378     unsigned char buf[18]; /* Performance optimization code structure assignment length 18 bytes */
379 } SecStrBuf18;
380 typedef struct {
381     unsigned char buf[19]; /* Performance optimization code structure assignment length 19 bytes */
382 } SecStrBuf19;
383 typedef struct {
384     unsigned char buf[20]; /* Performance optimization code structure assignment length 20 bytes */
385 } SecStrBuf20;
386 typedef struct {
387     unsigned char buf[21]; /* Performance optimization code structure assignment length 21 bytes */
388 } SecStrBuf21;
389 typedef struct {
390     unsigned char buf[22]; /* Performance optimization code structure assignment length 22 bytes */
391 } SecStrBuf22;
392 typedef struct {
393     unsigned char buf[23]; /* Performance optimization code structure assignment length 23 bytes */
394 } SecStrBuf23;
395 typedef struct {
396     unsigned char buf[24]; /* Performance optimization code structure assignment length 24 bytes */
397 } SecStrBuf24;
398 typedef struct {
399     unsigned char buf[25]; /* Performance optimization code structure assignment length 25 bytes */
400 } SecStrBuf25;
401 typedef struct {
402     unsigned char buf[26]; /* Performance optimization code structure assignment length 26 bytes */
403 } SecStrBuf26;
404 typedef struct {
405     unsigned char buf[27]; /* Performance optimization code structure assignment length 27 bytes */
406 } SecStrBuf27;
407 typedef struct {
408     unsigned char buf[28]; /* Performance optimization code structure assignment length 28 bytes */
409 } SecStrBuf28;
410 typedef struct {
411     unsigned char buf[29]; /* Performance optimization code structure assignment length 29 bytes */
412 } SecStrBuf29;
413 typedef struct {
414     unsigned char buf[30]; /* Performance optimization code structure assignment length 30 bytes */
415 } SecStrBuf30;
416 typedef struct {
417     unsigned char buf[31]; /* Performance optimization code structure assignment length 31 bytes */
418 } SecStrBuf31;
419 typedef struct {
420     unsigned char buf[32]; /* Performance optimization code structure assignment length 32 bytes */
421 } SecStrBuf32;
422 typedef struct {
423     unsigned char buf[33]; /* Performance optimization code structure assignment length 33 bytes */
424 } SecStrBuf33;
425 typedef struct {
426     unsigned char buf[34]; /* Performance optimization code structure assignment length 34 bytes */
427 } SecStrBuf34;
428 typedef struct {
429     unsigned char buf[35]; /* Performance optimization code structure assignment length 35 bytes */
430 } SecStrBuf35;
431 typedef struct {
432     unsigned char buf[36]; /* Performance optimization code structure assignment length 36 bytes */
433 } SecStrBuf36;
434 typedef struct {
435     unsigned char buf[37]; /* Performance optimization code structure assignment length 37 bytes */
436 } SecStrBuf37;
437 typedef struct {
438     unsigned char buf[38]; /* Performance optimization code structure assignment length 38 bytes */
439 } SecStrBuf38;
440 typedef struct {
441     unsigned char buf[39]; /* Performance optimization code structure assignment length 39 bytes */
442 } SecStrBuf39;
443 typedef struct {
444     unsigned char buf[40]; /* Performance optimization code structure assignment length 40 bytes */
445 } SecStrBuf40;
446 typedef struct {
447     unsigned char buf[41]; /* Performance optimization code structure assignment length 41 bytes */
448 } SecStrBuf41;
449 typedef struct {
450     unsigned char buf[42]; /* Performance optimization code structure assignment length 42 bytes */
451 } SecStrBuf42;
452 typedef struct {
453     unsigned char buf[43]; /* Performance optimization code structure assignment length 43 bytes */
454 } SecStrBuf43;
455 typedef struct {
456     unsigned char buf[44]; /* Performance optimization code structure assignment length 44 bytes */
457 } SecStrBuf44;
458 typedef struct {
459     unsigned char buf[45]; /* Performance optimization code structure assignment length 45 bytes */
460 } SecStrBuf45;
461 typedef struct {
462     unsigned char buf[46]; /* Performance optimization code structure assignment length 46 bytes */
463 } SecStrBuf46;
464 typedef struct {
465     unsigned char buf[47]; /* Performance optimization code structure assignment length 47 bytes */
466 } SecStrBuf47;
467 typedef struct {
468     unsigned char buf[48]; /* Performance optimization code structure assignment length 48 bytes */
469 } SecStrBuf48;
470 typedef struct {
471     unsigned char buf[49]; /* Performance optimization code structure assignment length 49 bytes */
472 } SecStrBuf49;
473 typedef struct {
474     unsigned char buf[50]; /* Performance optimization code structure assignment length 50 bytes */
475 } SecStrBuf50;
476 typedef struct {
477     unsigned char buf[51]; /* Performance optimization code structure assignment length 51 bytes */
478 } SecStrBuf51;
479 typedef struct {
480     unsigned char buf[52]; /* Performance optimization code structure assignment length 52 bytes */
481 } SecStrBuf52;
482 typedef struct {
483     unsigned char buf[53]; /* Performance optimization code structure assignment length 53 bytes */
484 } SecStrBuf53;
485 typedef struct {
486     unsigned char buf[54]; /* Performance optimization code structure assignment length 54 bytes */
487 } SecStrBuf54;
488 typedef struct {
489     unsigned char buf[55]; /* Performance optimization code structure assignment length 55 bytes */
490 } SecStrBuf55;
491 typedef struct {
492     unsigned char buf[56]; /* Performance optimization code structure assignment length 56 bytes */
493 } SecStrBuf56;
494 typedef struct {
495     unsigned char buf[57]; /* Performance optimization code structure assignment length 57 bytes */
496 } SecStrBuf57;
497 typedef struct {
498     unsigned char buf[58]; /* Performance optimization code structure assignment length 58 bytes */
499 } SecStrBuf58;
500 typedef struct {
501     unsigned char buf[59]; /* Performance optimization code structure assignment length 59 bytes */
502 } SecStrBuf59;
503 typedef struct {
504     unsigned char buf[60]; /* Performance optimization code structure assignment length 60 bytes */
505 } SecStrBuf60;
506 typedef struct {
507     unsigned char buf[61]; /* Performance optimization code structure assignment length 61 bytes */
508 } SecStrBuf61;
509 typedef struct {
510     unsigned char buf[62]; /* Performance optimization code structure assignment length 62 bytes */
511 } SecStrBuf62;
512 typedef struct {
513     unsigned char buf[63]; /* Performance optimization code structure assignment length 63 bytes */
514 } SecStrBuf63;
515 typedef struct {
516     unsigned char buf[64]; /* Performance optimization code structure assignment length 64 bytes */
517 } SecStrBuf64;
518 
519 /*
520  * User can change the error handler by modify the following definition,
521  * such as logging the detail error in file.
522  */
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)
529 #if SECUREC_IN_KERNEL
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)
533 #else
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)
537 #endif
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")
542 #endif
543 #endif
544 
545 /* Default handler is none */
546 #ifndef SECUREC_ERROR_INVALID_PARAMTER
547 #define SECUREC_ERROR_INVALID_PARAMTER(msg)
548 #endif
549 #ifndef SECUREC_ERROR_INVALID_RANGE
550 #define SECUREC_ERROR_INVALID_RANGE(msg)
551 #endif
552 #ifndef SECUREC_ERROR_BUFFER_OVERLAP
553 #define SECUREC_ERROR_BUFFER_OVERLAP(msg)
554 #endif
555 
556 #ifdef __cplusplus
557 extern "C" {
558 #endif
559 
560 /* Assembly language memory copy and memory set for X86 or MIPS ... */
561 #ifdef SECUREC_USE_ASM
562 void *memcpy_opt(void *dest, const void *src, size_t n);
563 void *memset_opt(void *s, int c, size_t n);
564 #endif
565 
566 #if defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
567 void LogSecureCRuntimeError(const char *errDetail);
568 #endif
569 
570 #ifdef __cplusplus
571 }
572 #endif /* __cplusplus */
573 #endif
574 
575