You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

securecutil.h 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
  17. #define SECURECUTIL_H_46C86578_F8FF_4E49_8E64_9B175241761F
  18. #include "securec.h"
  19. #if (defined(_MSC_VER)) && (_MSC_VER >= 1400)
  20. #define SECUREC_MASK_MSVC_CRT_WARNING __pragma(warning(push)) \
  21. __pragma(warning(disable:4996 4127))
  22. #define SECUREC_END_MASK_MSVC_CRT_WARNING __pragma(warning(pop))
  23. #else
  24. #define SECUREC_MASK_MSVC_CRT_WARNING
  25. #define SECUREC_END_MASK_MSVC_CRT_WARNING
  26. #endif
  27. #define SECUREC_WHILE_ZERO SECUREC_MASK_MSVC_CRT_WARNING while (0) SECUREC_END_MASK_MSVC_CRT_WARNING
  28. #ifndef SECUREC_HAVE_STRNLEN
  29. #if (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L)
  30. #if SECUREC_IN_KERNEL
  31. #define SECUREC_HAVE_STRNLEN 0
  32. #else
  33. #if defined(__GLIBC__) && __GLIBC__ >= 2 && defined(__GLIBC_MINOR__) && __GLIBC_MINOR__ >= 10
  34. #define SECUREC_HAVE_STRNLEN 1
  35. #else
  36. #define SECUREC_HAVE_STRNLEN 0
  37. #endif
  38. #endif
  39. #else
  40. #define SECUREC_HAVE_STRNLEN 0
  41. #endif
  42. #endif
  43. #if SECUREC_IN_KERNEL
  44. /* in kernel disbale functions */
  45. #ifndef SECUREC_ENABLE_SCANF_FILE
  46. #define SECUREC_ENABLE_SCANF_FILE 0
  47. #endif
  48. #ifndef SECUREC_ENABLE_SCANF_FLOAT
  49. #define SECUREC_ENABLE_SCANF_FLOAT 0
  50. #endif
  51. #ifndef SECUREC_ENABLE_SPRINTF_FLOAT
  52. #define SECUREC_ENABLE_SPRINTF_FLOAT 0
  53. #endif
  54. #ifndef SECUREC_HAVE_MBTOWC
  55. #define SECUREC_HAVE_MBTOWC 0
  56. #endif
  57. #ifndef SECUREC_HAVE_WCTOMB
  58. #define SECUREC_HAVE_WCTOMB 0
  59. #endif
  60. #ifndef SECUREC_HAVE_WCHART
  61. #define SECUREC_HAVE_WCHART 0
  62. #endif
  63. #else /* no in kernel */
  64. /* Systems that do not support file, can define this macro to 0. */
  65. #ifndef SECUREC_ENABLE_SCANF_FILE
  66. #define SECUREC_ENABLE_SCANF_FILE 1
  67. #endif
  68. #ifndef SECUREC_ENABLE_SCANF_FLOAT
  69. #define SECUREC_ENABLE_SCANF_FLOAT 1
  70. #endif
  71. /* Systems that do not support float, can define this macro to 0. */
  72. #ifndef SECUREC_ENABLE_SPRINTF_FLOAT
  73. #define SECUREC_ENABLE_SPRINTF_FLOAT 1
  74. #endif
  75. #ifndef SECUREC_HAVE_MBTOWC
  76. #define SECUREC_HAVE_MBTOWC 1
  77. #endif
  78. #ifndef SECUREC_HAVE_WCTOMB
  79. #define SECUREC_HAVE_WCTOMB 1
  80. #endif
  81. #ifndef SECUREC_HAVE_WCHART
  82. #define SECUREC_HAVE_WCHART 1
  83. #endif
  84. #endif
  85. #define SECUREC_INT_MAX 2147483647
  86. #define SECUREC_MUL_SIXTEEN(x) ((x) << 4)
  87. #define SECUREC_MUL_EIGHT(x) ((x) << 3)
  88. #define SECUREC_MUL_TEN(x) ((((x) << 2) + (x)) << 1)
  89. /* Limited format input and output width */
  90. #define SECUREC_MAX_WIDTH_LEN_DIV_TEN 21474836
  91. #define SECUREC_MAX_WIDTH_LEN SECUREC_MUL_TEN(SECUREC_MAX_WIDTH_LEN_DIV_TEN)
  92. /* Is the x multiplied by 10 greater than */
  93. #define SECUREC_MUL_TEN_ADD_BEYOND_MAX(x) (((x) > SECUREC_MAX_WIDTH_LEN_DIV_TEN))
  94. #define SECUREC_FLOAT_BUFSIZE (309 + 40) /* Max length of double value */
  95. #define SECUREC_FLOAT_BUFSIZE_LB (4932 + 40) /* Max length of long double value */
  96. #define SECUREC_FLOAT_DEFAULT_PRECISION 6
  97. /* This macro does not handle pointer equality or integer overflow */
  98. #define SECUREC_MEMORY_NO_OVERLAP(dest, src, count) \
  99. (((src) < (dest) && ((const char *)(src) + (count)) <= (char *)(dest)) || \
  100. ((dest) < (src) && ((char *)(dest) + (count)) <= (const char *)(src)))
  101. #define SECUREC_MEMORY_IS_OVERLAP(dest, src, count) \
  102. (((src) < (dest) && ((const char *)(src) + (count)) > (char *)(dest)) || \
  103. ((dest) < (src) && ((char *)(dest) + (count)) > (const char *)(src)))
  104. /*
  105. * Check whether the strings overlap, len is the length of the string not include terminator
  106. * Length is related to data type char or wchar , do not force conversion of types
  107. */
  108. #define SECUREC_STRING_NO_OVERLAP(dest, src, len) \
  109. (((src) < (dest) && ((src) + (len)) < (dest)) || \
  110. ((dest) < (src) && ((dest) + (len)) < (src)))
  111. /*
  112. * Check whether the strings overlap for strcpy wcscpy function, dest len and src Len are not include terminator
  113. * Length is related to data type char or wchar , do not force conversion of types
  114. */
  115. #define SECUREC_STRING_IS_OVERLAP(dest, src, len) \
  116. (((src) < (dest) && ((src) + (len)) >= (dest)) || \
  117. ((dest) < (src) && ((dest) + (len)) >= (src)))
  118. /*
  119. * Check whether the strings overlap for strcat wcscat function, dest len and src Len are not include terminator
  120. * Length is related to data type char or wchar , do not force conversion of types
  121. */
  122. #define SECUREC_CAT_STRING_IS_OVERLAP(dest, destLen, src, srcLen) \
  123. (((dest) < (src) && ((dest) + (destLen) + (srcLen)) >= (src)) || \
  124. ((src) < (dest) && ((src) + (srcLen)) >= (dest)))
  125. #if SECUREC_HAVE_STRNLEN
  126. #define SECUREC_CALC_STR_LEN(str, maxLen, outLen) do { \
  127. *(outLen) = strnlen((str), (maxLen)); \
  128. } SECUREC_WHILE_ZERO
  129. #define SECUREC_CALC_STR_LEN_OPT(str, maxLen, outLen) do { \
  130. if ((maxLen) > 8) { \
  131. /* Optimization or len less then 8 */ \
  132. if (*((str) + 0) == '\0') { \
  133. *(outLen) = 0; \
  134. } else if (*((str) + 1) == '\0') { \
  135. *(outLen) = 1; \
  136. } else if (*((str) + 2) == '\0') { \
  137. *(outLen) = 2; \
  138. } else if (*((str) + 3) == '\0') { \
  139. *(outLen) = 3; \
  140. } else if (*((str) + 4) == '\0') { \
  141. *(outLen) = 4; \
  142. } else if (*((str) + 5) == '\0') { \
  143. *(outLen) = 5; \
  144. } else if (*((str) + 6) == '\0') { \
  145. *(outLen) = 6; \
  146. } else if (*((str) + 7) == '\0') { \
  147. *(outLen) = 7; \
  148. } else if (*((str) + 8) == '\0') { \
  149. /* Optimization with a length of 8 */ \
  150. *(outLen) = 8; \
  151. } else { \
  152. /* The offset is 8 because the performance of 8 byte alignment is high */ \
  153. *(outLen) = 8 + strnlen((str) + 8, (maxLen) - 8); \
  154. } \
  155. } else { \
  156. SECUREC_CALC_STR_LEN((str), (maxLen), (outLen)); \
  157. } \
  158. } SECUREC_WHILE_ZERO
  159. #else
  160. #define SECUREC_CALC_STR_LEN(str, maxLen, outLen) do { \
  161. const char *strEnd = (const char *)(str); \
  162. size_t availableSize = (size_t)(maxLen); \
  163. while (availableSize > 0 && *strEnd != '\0') { \
  164. --availableSize; \
  165. ++strEnd; \
  166. } \
  167. *(outLen) = (size_t)(strEnd - (str)); \
  168. } SECUREC_WHILE_ZERO
  169. #define SECUREC_CALC_STR_LEN_OPT SECUREC_CALC_STR_LEN
  170. #endif
  171. #define SECUREC_CALC_WSTR_LEN(str, maxLen, outLen) do { \
  172. const wchar_t *strEnd = (const wchar_t *)(str); \
  173. *(outLen) = 0; \
  174. while (*(outLen) < (maxLen) && *strEnd != L'\0') { \
  175. *(outLen) = *(outLen) + 1; \
  176. ++strEnd; \
  177. } \
  178. } SECUREC_WHILE_ZERO
  179. #ifdef SECUREC_FORMAT_OUTPUT_INPUT
  180. #if defined(SECUREC_COMPATIBLE_WIN_FORMAT) || defined(__ARMCC_VERSION)
  181. typedef __int64 SecInt64;
  182. typedef unsigned __int64 SecUnsignedInt64;
  183. #if defined(__ARMCC_VERSION)
  184. typedef unsigned int SecUnsignedInt32;
  185. #else
  186. typedef unsigned __int32 SecUnsignedInt32;
  187. #endif
  188. #else
  189. typedef unsigned int SecUnsignedInt32;
  190. typedef long long SecInt64;
  191. typedef unsigned long long SecUnsignedInt64;
  192. #endif
  193. #ifdef SECUREC_FOR_WCHAR
  194. #if defined(SECUREC_VXWORKS_PLATFORM) && !defined(__WINT_TYPE__)
  195. typedef wchar_t wint_t;
  196. #endif
  197. typedef wchar_t SecChar;
  198. typedef wchar_t SecUnsignedChar;
  199. typedef wint_t SecInt;
  200. typedef wint_t SecUnsignedInt;
  201. #else /* no SECUREC_FOR_WCHAR */
  202. typedef char SecChar;
  203. typedef unsigned char SecUnsignedChar;
  204. typedef int SecInt;
  205. typedef unsigned int SecUnsignedInt;
  206. #endif
  207. #endif
  208. /* Determine whether the address is 8-byte aligned
  209. * Some systems do not have uintptr_t type, so use NULL to clear tool alarm 507
  210. */
  211. #define SECUREC_ADDR_ALIGNED_8(addr) (SecIsAddrAligned8((addr), NULL) == 0)
  212. /* If you define the memory allocation function,
  213. * you need to define the function prototype. You can define this macro as a header file.
  214. */
  215. #if defined(SECUREC_MALLOC_PROTOTYPE)
  216. SECUREC_MALLOC_PROTOTYPE
  217. #endif
  218. #ifndef SECUREC_MALLOC
  219. #define SECUREC_MALLOC(x) malloc((size_t)(x))
  220. #endif
  221. #ifndef SECUREC_FREE
  222. #define SECUREC_FREE(x) free((void *)(x))
  223. #endif
  224. /* struct for performance */
  225. typedef struct {
  226. unsigned char buf[1]; /* Performance optimization code structure assignment length 1 bytes */
  227. } SecStrBuf1;
  228. typedef struct {
  229. unsigned char buf[2]; /* Performance optimization code structure assignment length 2 bytes */
  230. } SecStrBuf2;
  231. typedef struct {
  232. unsigned char buf[3]; /* Performance optimization code structure assignment length 3 bytes */
  233. } SecStrBuf3;
  234. typedef struct {
  235. unsigned char buf[4]; /* Performance optimization code structure assignment length 4 bytes */
  236. } SecStrBuf4;
  237. typedef struct {
  238. unsigned char buf[5]; /* Performance optimization code structure assignment length 5 bytes */
  239. } SecStrBuf5;
  240. typedef struct {
  241. unsigned char buf[6]; /* Performance optimization code structure assignment length 6 bytes */
  242. } SecStrBuf6;
  243. typedef struct {
  244. unsigned char buf[7]; /* Performance optimization code structure assignment length 7 bytes */
  245. } SecStrBuf7;
  246. typedef struct {
  247. unsigned char buf[8]; /* Performance optimization code structure assignment length 8 bytes */
  248. } SecStrBuf8;
  249. typedef struct {
  250. unsigned char buf[9]; /* Performance optimization code structure assignment length 9 bytes */
  251. } SecStrBuf9;
  252. typedef struct {
  253. unsigned char buf[10]; /* Performance optimization code structure assignment length 10 bytes */
  254. } SecStrBuf10;
  255. typedef struct {
  256. unsigned char buf[11]; /* Performance optimization code structure assignment length 11 bytes */
  257. } SecStrBuf11;
  258. typedef struct {
  259. unsigned char buf[12]; /* Performance optimization code structure assignment length 12 bytes */
  260. } SecStrBuf12;
  261. typedef struct {
  262. unsigned char buf[13]; /* Performance optimization code structure assignment length 13 bytes */
  263. } SecStrBuf13;
  264. typedef struct {
  265. unsigned char buf[14]; /* Performance optimization code structure assignment length 14 bytes */
  266. } SecStrBuf14;
  267. typedef struct {
  268. unsigned char buf[15]; /* Performance optimization code structure assignment length 15 bytes */
  269. } SecStrBuf15;
  270. typedef struct {
  271. unsigned char buf[16]; /* Performance optimization code structure assignment length 16 bytes */
  272. } SecStrBuf16;
  273. typedef struct {
  274. unsigned char buf[17]; /* Performance optimization code structure assignment length 17 bytes */
  275. } SecStrBuf17;
  276. typedef struct {
  277. unsigned char buf[18]; /* Performance optimization code structure assignment length 18 bytes */
  278. } SecStrBuf18;
  279. typedef struct {
  280. unsigned char buf[19]; /* Performance optimization code structure assignment length 19 bytes */
  281. } SecStrBuf19;
  282. typedef struct {
  283. unsigned char buf[20]; /* Performance optimization code structure assignment length 20 bytes */
  284. } SecStrBuf20;
  285. typedef struct {
  286. unsigned char buf[21]; /* Performance optimization code structure assignment length 21 bytes */
  287. } SecStrBuf21;
  288. typedef struct {
  289. unsigned char buf[22]; /* Performance optimization code structure assignment length 22 bytes */
  290. } SecStrBuf22;
  291. typedef struct {
  292. unsigned char buf[23]; /* Performance optimization code structure assignment length 23 bytes */
  293. } SecStrBuf23;
  294. typedef struct {
  295. unsigned char buf[24]; /* Performance optimization code structure assignment length 24 bytes */
  296. } SecStrBuf24;
  297. typedef struct {
  298. unsigned char buf[25]; /* Performance optimization code structure assignment length 25 bytes */
  299. } SecStrBuf25;
  300. typedef struct {
  301. unsigned char buf[26]; /* Performance optimization code structure assignment length 26 bytes */
  302. } SecStrBuf26;
  303. typedef struct {
  304. unsigned char buf[27]; /* Performance optimization code structure assignment length 27 bytes */
  305. } SecStrBuf27;
  306. typedef struct {
  307. unsigned char buf[28]; /* Performance optimization code structure assignment length 28 bytes */
  308. } SecStrBuf28;
  309. typedef struct {
  310. unsigned char buf[29]; /* Performance optimization code structure assignment length 29 bytes */
  311. } SecStrBuf29;
  312. typedef struct {
  313. unsigned char buf[30]; /* Performance optimization code structure assignment length 30 bytes */
  314. } SecStrBuf30;
  315. typedef struct {
  316. unsigned char buf[31]; /* Performance optimization code structure assignment length 31 bytes */
  317. } SecStrBuf31;
  318. typedef struct {
  319. unsigned char buf[32]; /* Performance optimization code structure assignment length 32 bytes */
  320. } SecStrBuf32;
  321. typedef struct {
  322. unsigned char buf[33]; /* Performance optimization code structure assignment length 33 bytes */
  323. } SecStrBuf33;
  324. typedef struct {
  325. unsigned char buf[34]; /* Performance optimization code structure assignment length 34 bytes */
  326. } SecStrBuf34;
  327. typedef struct {
  328. unsigned char buf[35]; /* Performance optimization code structure assignment length 35 bytes */
  329. } SecStrBuf35;
  330. typedef struct {
  331. unsigned char buf[36]; /* Performance optimization code structure assignment length 36 bytes */
  332. } SecStrBuf36;
  333. typedef struct {
  334. unsigned char buf[37]; /* Performance optimization code structure assignment length 37 bytes */
  335. } SecStrBuf37;
  336. typedef struct {
  337. unsigned char buf[38]; /* Performance optimization code structure assignment length 38 bytes */
  338. } SecStrBuf38;
  339. typedef struct {
  340. unsigned char buf[39]; /* Performance optimization code structure assignment length 39 bytes */
  341. } SecStrBuf39;
  342. typedef struct {
  343. unsigned char buf[40]; /* Performance optimization code structure assignment length 40 bytes */
  344. } SecStrBuf40;
  345. typedef struct {
  346. unsigned char buf[41]; /* Performance optimization code structure assignment length 41 bytes */
  347. } SecStrBuf41;
  348. typedef struct {
  349. unsigned char buf[42]; /* Performance optimization code structure assignment length 42 bytes */
  350. } SecStrBuf42;
  351. typedef struct {
  352. unsigned char buf[43]; /* Performance optimization code structure assignment length 43 bytes */
  353. } SecStrBuf43;
  354. typedef struct {
  355. unsigned char buf[44]; /* Performance optimization code structure assignment length 44 bytes */
  356. } SecStrBuf44;
  357. typedef struct {
  358. unsigned char buf[45]; /* Performance optimization code structure assignment length 45 bytes */
  359. } SecStrBuf45;
  360. typedef struct {
  361. unsigned char buf[46]; /* Performance optimization code structure assignment length 46 bytes */
  362. } SecStrBuf46;
  363. typedef struct {
  364. unsigned char buf[47]; /* Performance optimization code structure assignment length 47 bytes */
  365. } SecStrBuf47;
  366. typedef struct {
  367. unsigned char buf[48]; /* Performance optimization code structure assignment length 48 bytes */
  368. } SecStrBuf48;
  369. typedef struct {
  370. unsigned char buf[49]; /* Performance optimization code structure assignment length 49 bytes */
  371. } SecStrBuf49;
  372. typedef struct {
  373. unsigned char buf[50]; /* Performance optimization code structure assignment length 50 bytes */
  374. } SecStrBuf50;
  375. typedef struct {
  376. unsigned char buf[51]; /* Performance optimization code structure assignment length 51 bytes */
  377. } SecStrBuf51;
  378. typedef struct {
  379. unsigned char buf[52]; /* Performance optimization code structure assignment length 52 bytes */
  380. } SecStrBuf52;
  381. typedef struct {
  382. unsigned char buf[53]; /* Performance optimization code structure assignment length 53 bytes */
  383. } SecStrBuf53;
  384. typedef struct {
  385. unsigned char buf[54]; /* Performance optimization code structure assignment length 54 bytes */
  386. } SecStrBuf54;
  387. typedef struct {
  388. unsigned char buf[55]; /* Performance optimization code structure assignment length 55 bytes */
  389. } SecStrBuf55;
  390. typedef struct {
  391. unsigned char buf[56]; /* Performance optimization code structure assignment length 56 bytes */
  392. } SecStrBuf56;
  393. typedef struct {
  394. unsigned char buf[57]; /* Performance optimization code structure assignment length 57 bytes */
  395. } SecStrBuf57;
  396. typedef struct {
  397. unsigned char buf[58]; /* Performance optimization code structure assignment length 58 bytes */
  398. } SecStrBuf58;
  399. typedef struct {
  400. unsigned char buf[59]; /* Performance optimization code structure assignment length 59 bytes */
  401. } SecStrBuf59;
  402. typedef struct {
  403. unsigned char buf[60]; /* Performance optimization code structure assignment length 60 bytes */
  404. } SecStrBuf60;
  405. typedef struct {
  406. unsigned char buf[61]; /* Performance optimization code structure assignment length 61 bytes */
  407. } SecStrBuf61;
  408. typedef struct {
  409. unsigned char buf[62]; /* Performance optimization code structure assignment length 62 bytes */
  410. } SecStrBuf62;
  411. typedef struct {
  412. unsigned char buf[63]; /* Performance optimization code structure assignment length 63 bytes */
  413. } SecStrBuf63;
  414. typedef struct {
  415. unsigned char buf[64]; /* Performance optimization code structure assignment length 64 bytes */
  416. } SecStrBuf64;
  417. /* User can change the error handler by modify the following definition,
  418. * such as logging the detail error in file.
  419. */
  420. #if defined(_DEBUG) || defined(DEBUG)
  421. #if defined(SECUREC_ERROR_HANDLER_BY_ASSERT)
  422. #define SECUREC_ERROR_INVALID_PARAMTER(msg) assert(msg "invalid argument" == NULL)
  423. #define SECUREC_ERROR_INVALID_RANGE(msg) assert(msg "invalid dest buffer size" == NULL)
  424. #define SECUREC_ERROR_BUFFER_OVERLAP(msg) assert(msg "buffer overlap" == NULL)
  425. #elif defined(SECUREC_ERROR_HANDLER_BY_PRINTF)
  426. #if SECUREC_IN_KERNEL
  427. #define SECUREC_ERROR_INVALID_PARAMTER(msg) printk("%s invalid argument\n", msg)
  428. #define SECUREC_ERROR_INVALID_RANGE(msg) printk("%s invalid dest buffer size\n", msg)
  429. #define SECUREC_ERROR_BUFFER_OVERLAP(msg) printk("%s buffer overlap\n", msg)
  430. #else
  431. #define SECUREC_ERROR_INVALID_PARAMTER(msg) printf("%s invalid argument\n", msg)
  432. #define SECUREC_ERROR_INVALID_RANGE(msg) printf("%s invalid dest buffer size\n", msg)
  433. #define SECUREC_ERROR_BUFFER_OVERLAP(msg) printf("%s buffer overlap\n", msg)
  434. #endif
  435. #elif defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
  436. #define SECUREC_ERROR_INVALID_PARAMTER(msg) LogSecureCRuntimeError(msg " EINVAL\n")
  437. #define SECUREC_ERROR_INVALID_RANGE(msg) LogSecureCRuntimeError(msg " ERANGE\n")
  438. #define SECUREC_ERROR_BUFFER_OVERLAP(msg) LogSecureCRuntimeError(msg " EOVERLAP\n")
  439. #else /* no HANDLER is defined */
  440. #define SECUREC_ERROR_INVALID_PARAMTER(msg) ((void)0)
  441. #define SECUREC_ERROR_INVALID_RANGE(msg) ((void)0)
  442. #define SECUREC_ERROR_BUFFER_OVERLAP(msg) ((void)0)
  443. #endif
  444. #else /* no DEBUG */
  445. #define SECUREC_ERROR_INVALID_PARAMTER(msg) ((void)0)
  446. #define SECUREC_ERROR_INVALID_RANGE(msg) ((void)0)
  447. #define SECUREC_ERROR_BUFFER_OVERLAP(msg) ((void)0)
  448. #endif
  449. #ifdef __cplusplus
  450. extern "C" {
  451. #endif
  452. /* assembly language memory copy and memory set for X86 or MIPS ... */
  453. #ifdef SECUREC_USE_ASM
  454. extern void *memcpy_opt(void *, const void *, size_t);
  455. extern void *memset_opt(void *, int, size_t);
  456. #endif
  457. #if defined(SECUREC_ERROR_HANDLER_BY_FILE_LOG)
  458. extern void LogSecureCRuntimeError(const char *errDetail);
  459. #endif
  460. #ifdef SECUREC_INLINE_DO_MEMCPY
  461. static void SecDoMemcpy(void *dest, const void *src, size_t count)
  462. {
  463. /*
  464. * if SECUREC_USE_ASM macro is enabled, it will call assembly language function to improve performance.
  465. */
  466. #ifdef SECUREC_USE_ASM
  467. (void)memcpy_opt(dest, src, count);
  468. #else
  469. /* large enough, let system API do it */
  470. (void)memcpy(dest, src, count);
  471. #endif
  472. }
  473. #endif
  474. #ifdef SECUREC_INLINE_DO_MEMSET
  475. static void SecDoMemset(void *dest, int c, size_t count)
  476. {
  477. #ifdef SECUREC_USE_ASM
  478. (void)memset_opt(dest, c, count);
  479. #else
  480. (void)memset(dest, c, count);
  481. #endif
  482. }
  483. #endif
  484. #ifdef SECUREC_INLINE_STR_LEN
  485. /* The function compiler will be inlined and not placed in other files */
  486. static size_t SecStrMinLen(const char *str, size_t maxLen)
  487. {
  488. size_t len;
  489. SECUREC_CALC_STR_LEN(str, maxLen, &len);
  490. return len;
  491. }
  492. #endif
  493. #ifdef SECUREC_INLINE_STR_LEN_OPT
  494. /* The function compiler will be inlined and not placed in other files */
  495. static size_t SecStrMinLenOpt(const char *str, size_t maxLen)
  496. {
  497. size_t len;
  498. SECUREC_CALC_STR_LEN_OPT(str, maxLen, &len);
  499. return len;
  500. }
  501. #endif
  502. #ifdef __cplusplus
  503. }
  504. #endif /* __cplusplus */
  505. #endif