securectype.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 internal used macro and data type. The marco of SECUREC_ON_64BITS
  12. * will be determined in this header file, which is a switch for part
  13. * of code. Some macro are used to suppress warning by MS compiler.
  14. * Create: 2014-02-25
  15. * Notes: User can change the value of SECUREC_STRING_MAX_LEN and SECUREC_MEM_MAX_LEN
  16. * macro to meet their special need, but The maximum value should not exceed 2G.
  17. */
  18. /*
  19. * [Standardize-exceptions]: Performance-sensitive
  20. * [reason]: Strict parameter verification has been done before use
  21. */
  22. #ifndef SECURECTYPE_H_A7BBB686_AADA_451B_B9F9_44DACDAE18A7
  23. #define SECURECTYPE_H_A7BBB686_AADA_451B_B9F9_44DACDAE18A7
  24. #ifndef SECUREC_USING_STD_SECURE_LIB
  25. #if defined(_MSC_VER) && _MSC_VER >= 1400
  26. #if defined(__STDC_WANT_SECURE_LIB__) && (!__STDC_WANT_SECURE_LIB__)
  27. /* Security functions have been provided since vs2005, default use of system library functions */
  28. #define SECUREC_USING_STD_SECURE_LIB 0
  29. #else
  30. #define SECUREC_USING_STD_SECURE_LIB 1
  31. #endif
  32. #else
  33. #define SECUREC_USING_STD_SECURE_LIB 0
  34. #endif
  35. #endif
  36. /* Compatibility with older Secure C versions, shielding VC symbol redefinition warning */
  37. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && (!SECUREC_USING_STD_SECURE_LIB)
  38. #ifndef SECUREC_DISABLE_CRT_FUNC
  39. #define SECUREC_DISABLE_CRT_FUNC 1
  40. #endif
  41. #ifndef SECUREC_DISABLE_CRT_IMP
  42. #define SECUREC_DISABLE_CRT_IMP 1
  43. #endif
  44. #else /* MSC VER */
  45. #ifndef SECUREC_DISABLE_CRT_FUNC
  46. #define SECUREC_DISABLE_CRT_FUNC 0
  47. #endif
  48. #ifndef SECUREC_DISABLE_CRT_IMP
  49. #define SECUREC_DISABLE_CRT_IMP 0
  50. #endif
  51. #endif
  52. #if SECUREC_DISABLE_CRT_FUNC
  53. #ifdef __STDC_WANT_SECURE_LIB__
  54. #undef __STDC_WANT_SECURE_LIB__
  55. #endif
  56. #define __STDC_WANT_SECURE_LIB__ 0
  57. #endif
  58. #if SECUREC_DISABLE_CRT_IMP
  59. #ifdef _CRTIMP_ALTERNATIVE
  60. #undef _CRTIMP_ALTERNATIVE
  61. #endif
  62. #define _CRTIMP_ALTERNATIVE /* Comment Microsoft *_s function */
  63. #endif
  64. /* Compile in kernel under macro control */
  65. #ifndef SECUREC_IN_KERNEL
  66. #ifdef __KERNEL__
  67. #define SECUREC_IN_KERNEL 1
  68. #else
  69. #define SECUREC_IN_KERNEL 0
  70. #endif
  71. #endif
  72. /* make kernel symbols of functions available to loadable modules */
  73. #ifndef SECUREC_EXPORT_KERNEL_SYMBOL
  74. #if SECUREC_IN_KERNEL
  75. #define SECUREC_EXPORT_KERNEL_SYMBOL 1
  76. #else
  77. #define SECUREC_EXPORT_KERNEL_SYMBOL 0
  78. #endif
  79. #endif
  80. #if SECUREC_IN_KERNEL
  81. #ifndef SECUREC_ENABLE_SCANF_FILE
  82. #define SECUREC_ENABLE_SCANF_FILE 0
  83. #endif
  84. #ifndef SECUREC_ENABLE_WCHAR_FUNC
  85. #define SECUREC_ENABLE_WCHAR_FUNC 0
  86. #endif
  87. #else /* SECUREC_IN_KERNEL */
  88. #ifndef SECUREC_ENABLE_SCANF_FILE
  89. #define SECUREC_ENABLE_SCANF_FILE 1
  90. #endif
  91. #ifndef SECUREC_ENABLE_WCHAR_FUNC
  92. #define SECUREC_ENABLE_WCHAR_FUNC 1
  93. #endif
  94. #endif
  95. /* Default secure function declaration, default declarations for non-standard functions */
  96. #ifndef SECUREC_SNPRINTF_TRUNCATED
  97. #define SECUREC_SNPRINTF_TRUNCATED 1
  98. #endif
  99. #if SECUREC_USING_STD_SECURE_LIB
  100. #if defined(_MSC_VER) && _MSC_VER >= 1400
  101. /* Declare secure functions that are not available in the VS compiler */
  102. #ifndef SECUREC_ENABLE_MEMSET
  103. #define SECUREC_ENABLE_MEMSET 1
  104. #endif
  105. /* VS 2005 have vsnprintf_s function */
  106. #ifndef SECUREC_ENABLE_VSNPRINTF
  107. #define SECUREC_ENABLE_VSNPRINTF 0
  108. #endif
  109. #ifndef SECUREC_ENABLE_SNPRINTF
  110. /* VS 2005 have vsnprintf_s function Adapt the snprintf_s of the security function */
  111. #define snprintf_s _snprintf_s
  112. #define SECUREC_ENABLE_SNPRINTF 0
  113. #endif
  114. /* Before VS 2010 do not have v functions */
  115. #if _MSC_VER <= 1600 || defined(SECUREC_FOR_V_SCANFS)
  116. #ifndef SECUREC_ENABLE_VFSCANF
  117. #define SECUREC_ENABLE_VFSCANF 1
  118. #endif
  119. #ifndef SECUREC_ENABLE_VSCANF
  120. #define SECUREC_ENABLE_VSCANF 1
  121. #endif
  122. #ifndef SECUREC_ENABLE_VSSCANF
  123. #define SECUREC_ENABLE_VSSCANF 1
  124. #endif
  125. #endif
  126. #else /* MSC VER */
  127. #ifndef SECUREC_ENABLE_MEMSET
  128. #define SECUREC_ENABLE_MEMSET 0
  129. #endif
  130. #ifndef SECUREC_ENABLE_SNPRINTF
  131. #define SECUREC_ENABLE_SNPRINTF 0
  132. #endif
  133. #ifndef SECUREC_ENABLE_VSNPRINTF
  134. #define SECUREC_ENABLE_VSNPRINTF 0
  135. #endif
  136. #endif
  137. #ifndef SECUREC_ENABLE_MEMMOVE
  138. #define SECUREC_ENABLE_MEMMOVE 0
  139. #endif
  140. #ifndef SECUREC_ENABLE_MEMCPY
  141. #define SECUREC_ENABLE_MEMCPY 0
  142. #endif
  143. #ifndef SECUREC_ENABLE_STRCPY
  144. #define SECUREC_ENABLE_STRCPY 0
  145. #endif
  146. #ifndef SECUREC_ENABLE_STRNCPY
  147. #define SECUREC_ENABLE_STRNCPY 0
  148. #endif
  149. #ifndef SECUREC_ENABLE_STRCAT
  150. #define SECUREC_ENABLE_STRCAT 0
  151. #endif
  152. #ifndef SECUREC_ENABLE_STRNCAT
  153. #define SECUREC_ENABLE_STRNCAT 0
  154. #endif
  155. #ifndef SECUREC_ENABLE_SPRINTF
  156. #define SECUREC_ENABLE_SPRINTF 0
  157. #endif
  158. #ifndef SECUREC_ENABLE_VSPRINTF
  159. #define SECUREC_ENABLE_VSPRINTF 0
  160. #endif
  161. #ifndef SECUREC_ENABLE_SSCANF
  162. #define SECUREC_ENABLE_SSCANF 0
  163. #endif
  164. #ifndef SECUREC_ENABLE_VSSCANF
  165. #define SECUREC_ENABLE_VSSCANF 0
  166. #endif
  167. #ifndef SECUREC_ENABLE_SCANF
  168. #define SECUREC_ENABLE_SCANF 0
  169. #endif
  170. #ifndef SECUREC_ENABLE_VSCANF
  171. #define SECUREC_ENABLE_VSCANF 0
  172. #endif
  173. #ifndef SECUREC_ENABLE_FSCANF
  174. #define SECUREC_ENABLE_FSCANF 0
  175. #endif
  176. #ifndef SECUREC_ENABLE_VFSCANF
  177. #define SECUREC_ENABLE_VFSCANF 0
  178. #endif
  179. #ifndef SECUREC_ENABLE_STRTOK
  180. #define SECUREC_ENABLE_STRTOK 0
  181. #endif
  182. #ifndef SECUREC_ENABLE_GETS
  183. #define SECUREC_ENABLE_GETS 0
  184. #endif
  185. #else /* SECUREC USE STD SECURE LIB */
  186. #ifndef SECUREC_ENABLE_MEMSET
  187. #define SECUREC_ENABLE_MEMSET 1
  188. #endif
  189. #ifndef SECUREC_ENABLE_MEMMOVE
  190. #define SECUREC_ENABLE_MEMMOVE 1
  191. #endif
  192. #ifndef SECUREC_ENABLE_MEMCPY
  193. #define SECUREC_ENABLE_MEMCPY 1
  194. #endif
  195. #ifndef SECUREC_ENABLE_STRCPY
  196. #define SECUREC_ENABLE_STRCPY 1
  197. #endif
  198. #ifndef SECUREC_ENABLE_STRNCPY
  199. #define SECUREC_ENABLE_STRNCPY 1
  200. #endif
  201. #ifndef SECUREC_ENABLE_STRCAT
  202. #define SECUREC_ENABLE_STRCAT 1
  203. #endif
  204. #ifndef SECUREC_ENABLE_STRNCAT
  205. #define SECUREC_ENABLE_STRNCAT 1
  206. #endif
  207. #ifndef SECUREC_ENABLE_SPRINTF
  208. #define SECUREC_ENABLE_SPRINTF 1
  209. #endif
  210. #ifndef SECUREC_ENABLE_VSPRINTF
  211. #define SECUREC_ENABLE_VSPRINTF 1
  212. #endif
  213. #ifndef SECUREC_ENABLE_SNPRINTF
  214. #define SECUREC_ENABLE_SNPRINTF 1
  215. #endif
  216. #ifndef SECUREC_ENABLE_VSNPRINTF
  217. #define SECUREC_ENABLE_VSNPRINTF 1
  218. #endif
  219. #ifndef SECUREC_ENABLE_SSCANF
  220. #define SECUREC_ENABLE_SSCANF 1
  221. #endif
  222. #ifndef SECUREC_ENABLE_VSSCANF
  223. #define SECUREC_ENABLE_VSSCANF 1
  224. #endif
  225. #ifndef SECUREC_ENABLE_SCANF
  226. #if SECUREC_ENABLE_SCANF_FILE
  227. #define SECUREC_ENABLE_SCANF 1
  228. #else
  229. #define SECUREC_ENABLE_SCANF 0
  230. #endif
  231. #endif
  232. #ifndef SECUREC_ENABLE_VSCANF
  233. #if SECUREC_ENABLE_SCANF_FILE
  234. #define SECUREC_ENABLE_VSCANF 1
  235. #else
  236. #define SECUREC_ENABLE_VSCANF 0
  237. #endif
  238. #endif
  239. #ifndef SECUREC_ENABLE_FSCANF
  240. #if SECUREC_ENABLE_SCANF_FILE
  241. #define SECUREC_ENABLE_FSCANF 1
  242. #else
  243. #define SECUREC_ENABLE_FSCANF 0
  244. #endif
  245. #endif
  246. #ifndef SECUREC_ENABLE_VFSCANF
  247. #if SECUREC_ENABLE_SCANF_FILE
  248. #define SECUREC_ENABLE_VFSCANF 1
  249. #else
  250. #define SECUREC_ENABLE_VFSCANF 0
  251. #endif
  252. #endif
  253. #ifndef SECUREC_ENABLE_STRTOK
  254. #define SECUREC_ENABLE_STRTOK 1
  255. #endif
  256. #ifndef SECUREC_ENABLE_GETS
  257. #define SECUREC_ENABLE_GETS 1
  258. #endif
  259. #endif /* SECUREC_USE_STD_SECURE_LIB */
  260. #if !SECUREC_ENABLE_SCANF_FILE
  261. #if SECUREC_ENABLE_FSCANF
  262. #undef SECUREC_ENABLE_FSCANF
  263. #define SECUREC_ENABLE_FSCANF 0
  264. #endif
  265. #if SECUREC_ENABLE_VFSCANF
  266. #undef SECUREC_ENABLE_VFSCANF
  267. #define SECUREC_ENABLE_VFSCANF 0
  268. #endif
  269. #if SECUREC_ENABLE_SCANF
  270. #undef SECUREC_ENABLE_SCANF
  271. #define SECUREC_ENABLE_SCANF 0
  272. #endif
  273. #if SECUREC_ENABLE_FSCANF
  274. #undef SECUREC_ENABLE_FSCANF
  275. #define SECUREC_ENABLE_FSCANF 0
  276. #endif
  277. #endif
  278. #if SECUREC_IN_KERNEL
  279. #include <linux/kernel.h>
  280. #include <linux/module.h>
  281. #else
  282. #ifndef SECUREC_HAVE_STDIO_H
  283. #define SECUREC_HAVE_STDIO_H 1
  284. #endif
  285. #ifndef SECUREC_HAVE_STRING_H
  286. #define SECUREC_HAVE_STRING_H 1
  287. #endif
  288. #ifndef SECUREC_HAVE_STDLIB_H
  289. #define SECUREC_HAVE_STDLIB_H 1
  290. #endif
  291. #if SECUREC_HAVE_STDIO_H
  292. #include <stdio.h>
  293. #endif
  294. #if SECUREC_HAVE_STRING_H
  295. #include <string.h>
  296. #endif
  297. #if SECUREC_HAVE_STDLIB_H
  298. #include <stdlib.h>
  299. #endif
  300. #endif
  301. /*
  302. * If you need high performance, enable the SECUREC_WITH_PERFORMANCE_ADDONS macro, default is enable.
  303. * The macro is automatically closed on the windows platform and linux kernel
  304. */
  305. #ifndef SECUREC_WITH_PERFORMANCE_ADDONS
  306. #if SECUREC_IN_KERNEL
  307. #define SECUREC_WITH_PERFORMANCE_ADDONS 0
  308. #else
  309. #define SECUREC_WITH_PERFORMANCE_ADDONS 1
  310. #endif
  311. #endif
  312. /* If enable SECUREC_COMPATIBLE_WIN_FORMAT, the output format will be compatible to Windows. */
  313. #if (defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) && !defined(SECUREC_COMPATIBLE_LINUX_FORMAT)
  314. #ifndef SECUREC_COMPATIBLE_WIN_FORMAT
  315. #define SECUREC_COMPATIBLE_WIN_FORMAT
  316. #endif
  317. #endif
  318. #if defined(SECUREC_COMPATIBLE_WIN_FORMAT)
  319. /* On windows platform, can't use optimized function for there is no __builtin_constant_p like function */
  320. /* If need optimized macro, can define this: define __builtin_constant_p(x) 0 */
  321. #ifdef SECUREC_WITH_PERFORMANCE_ADDONS
  322. #undef SECUREC_WITH_PERFORMANCE_ADDONS
  323. #define SECUREC_WITH_PERFORMANCE_ADDONS 0
  324. #endif
  325. #endif
  326. #if defined(__VXWORKS__) || defined(__vxworks) || defined(__VXWORKS) || defined(_VXWORKS_PLATFORM_) || \
  327. defined(SECUREC_VXWORKS_VERSION_5_4)
  328. #ifndef SECUREC_VXWORKS_PLATFORM
  329. #define SECUREC_VXWORKS_PLATFORM
  330. #endif
  331. #endif
  332. /* If enable SECUREC_COMPATIBLE_LINUX_FORMAT, the output format will be compatible to Linux. */
  333. #if !defined(SECUREC_COMPATIBLE_WIN_FORMAT) && !defined(SECUREC_VXWORKS_PLATFORM)
  334. #ifndef SECUREC_COMPATIBLE_LINUX_FORMAT
  335. #define SECUREC_COMPATIBLE_LINUX_FORMAT
  336. #endif
  337. #endif
  338. #ifdef SECUREC_COMPATIBLE_LINUX_FORMAT
  339. #ifndef SECUREC_HAVE_STDDEF_H
  340. #define SECUREC_HAVE_STDDEF_H 1
  341. #endif
  342. /* Some system may no stddef.h */
  343. #if SECUREC_HAVE_STDDEF_H
  344. #if !SECUREC_IN_KERNEL
  345. #include <stddef.h>
  346. #endif
  347. #endif
  348. #endif
  349. /*
  350. * Add the -DSECUREC_SUPPORT_FORMAT_WARNING=1 compiler option to supoort -Wformat=2.
  351. * Default does not check the format is that the same data type in the actual code.
  352. * In the product is different in the original data type definition of VxWorks and Linux.
  353. */
  354. #ifndef SECUREC_SUPPORT_FORMAT_WARNING
  355. #define SECUREC_SUPPORT_FORMAT_WARNING 0
  356. #endif
  357. #if SECUREC_SUPPORT_FORMAT_WARNING
  358. #define SECUREC_ATTRIBUTE(x, y) __attribute__((format(printf, (x), (y))))
  359. #else
  360. #define SECUREC_ATTRIBUTE(x, y)
  361. #endif
  362. /*
  363. * Add the -DSECUREC_SUPPORT_BUILTIN_EXPECT=0 compiler option, if compiler can not support __builtin_expect.
  364. */
  365. #ifndef SECUREC_SUPPORT_BUILTIN_EXPECT
  366. #define SECUREC_SUPPORT_BUILTIN_EXPECT 1
  367. #endif
  368. #if SECUREC_SUPPORT_BUILTIN_EXPECT && defined(__GNUC__) && ((__GNUC__ > 3) || \
  369. (defined(__GNUC_MINOR__) && (__GNUC__ == 3 && __GNUC_MINOR__ > 3)))
  370. /*
  371. * This is a built-in function that can be used without a declaration, if warning for declaration not found occurred,
  372. * you can add -DSECUREC_NEED_BUILTIN_EXPECT_DECLARE to compiler options
  373. */
  374. #ifdef SECUREC_NEED_BUILTIN_EXPECT_DECLARE
  375. long __builtin_expect(long exp, long c);
  376. #endif
  377. #define SECUREC_LIKELY(x) __builtin_expect(!!(x), 1)
  378. #define SECUREC_UNLIKELY(x) __builtin_expect(!!(x), 0)
  379. #else
  380. #define SECUREC_LIKELY(x) (x)
  381. #define SECUREC_UNLIKELY(x) (x)
  382. #endif
  383. /* Define the max length of the string */
  384. #ifndef SECUREC_STRING_MAX_LEN
  385. #define SECUREC_STRING_MAX_LEN 0x7fffffffUL
  386. #endif
  387. #define SECUREC_WCHAR_STRING_MAX_LEN (SECUREC_STRING_MAX_LEN / sizeof(wchar_t))
  388. /* Add SECUREC_MEM_MAX_LEN for memcpy and memmove */
  389. #ifndef SECUREC_MEM_MAX_LEN
  390. #define SECUREC_MEM_MAX_LEN 0x7fffffffUL
  391. #endif
  392. #define SECUREC_WCHAR_MEM_MAX_LEN (SECUREC_MEM_MAX_LEN / sizeof(wchar_t))
  393. #if SECUREC_STRING_MAX_LEN > 0x7fffffffUL
  394. #error "max string is 2G"
  395. #endif
  396. #if (defined(__GNUC__) && defined(__SIZEOF_POINTER__))
  397. #if (__SIZEOF_POINTER__ != 4) && (__SIZEOF_POINTER__ != 8)
  398. #error "unsupported system"
  399. #endif
  400. #endif
  401. #if defined(_WIN64) || defined(WIN64) || defined(__LP64__) || defined(_LP64)
  402. #define SECUREC_ON_64BITS
  403. #endif
  404. #if (!defined(SECUREC_ON_64BITS) && defined(__GNUC__) && defined(__SIZEOF_POINTER__))
  405. #if __SIZEOF_POINTER__ == 8
  406. #define SECUREC_ON_64BITS
  407. #endif
  408. #endif
  409. #if defined(__SVR4) || defined(__svr4__)
  410. #define SECUREC_ON_SOLARIS
  411. #endif
  412. #if (defined(__hpux) || defined(_AIX) || defined(SECUREC_ON_SOLARIS))
  413. #define SECUREC_ON_UNIX
  414. #endif
  415. /*
  416. * Codes should run under the macro SECUREC_COMPATIBLE_LINUX_FORMAT in unknown system on default,
  417. * and strtold.
  418. * The function strtold is referenced first at ISO9899:1999(C99), and some old compilers can
  419. * not support these functions. Here provides a macro to open these functions:
  420. * SECUREC_SUPPORT_STRTOLD -- If defined, strtold will be used
  421. */
  422. #ifndef SECUREC_SUPPORT_STRTOLD
  423. #define SECUREC_SUPPORT_STRTOLD 0
  424. #if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT))
  425. #if defined(__USE_ISOC99) || \
  426. (defined(_AIX) && defined(_ISOC99_SOURCE)) || \
  427. (defined(__hpux) && defined(__ia64)) || \
  428. (defined(SECUREC_ON_SOLARIS) && (!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
  429. defined(_STDC_C99) || defined(__EXTENSIONS__))
  430. #undef SECUREC_SUPPORT_STRTOLD
  431. #define SECUREC_SUPPORT_STRTOLD 1
  432. #endif
  433. #endif
  434. #if ((defined(SECUREC_WRLINUX_BELOW4) || defined(_WRLINUX_BELOW4_)))
  435. #undef SECUREC_SUPPORT_STRTOLD
  436. #define SECUREC_SUPPORT_STRTOLD 0
  437. #endif
  438. #endif
  439. #if SECUREC_WITH_PERFORMANCE_ADDONS
  440. #ifndef SECUREC_TWO_MIN
  441. #define SECUREC_TWO_MIN(a, b) ((a) < (b) ? (a) : (b))
  442. #endif
  443. /* For strncpy_s performance optimization */
  444. #define SECUREC_STRNCPY_SM(dest, destMax, src, count) \
  445. (((void *)(dest) != NULL && (const void *)(src) != NULL && (size_t)(destMax) > 0 && \
  446. (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \
  447. (SECUREC_TWO_MIN((size_t)(count), strlen(src)) + 1) <= (size_t)(destMax)) ? \
  448. (((size_t)(count) < strlen(src)) ? (memcpy((dest), (src), (count)), *((char *)(dest) + (count)) = '\0', EOK) : \
  449. (memcpy((dest), (src), strlen(src) + 1), EOK)) : (strncpy_error((dest), (destMax), (src), (count))))
  450. #define SECUREC_STRCPY_SM(dest, destMax, src) \
  451. (((void *)(dest) != NULL && (const void *)(src) != NULL && (size_t)(destMax) > 0 && \
  452. (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \
  453. (strlen(src) + 1) <= (size_t)(destMax)) ? (memcpy((dest), (src), strlen(src) + 1), EOK) : \
  454. (strcpy_error((dest), (destMax), (src))))
  455. /* For strcat_s performance optimization */
  456. #if defined(__GNUC__)
  457. #define SECUREC_STRCAT_SM(dest, destMax, src) ({ \
  458. int catRet_ = EOK; \
  459. if ((void *)(dest) != NULL && (const void *)(src) != NULL && (size_t)(destMax) > 0 && \
  460. (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \
  461. char *catTmpDst_ = (char *)(dest); \
  462. size_t catRestSize_ = (destMax); \
  463. while (catRestSize_ > 0 && *catTmpDst_ != '\0') { \
  464. ++catTmpDst_; \
  465. --catRestSize_; \
  466. } \
  467. if (catRestSize_ == 0) { \
  468. catRet_ = EINVAL; \
  469. } else if ((strlen(src) + 1) <= catRestSize_) { \
  470. memcpy(catTmpDst_, (src), strlen(src) + 1); \
  471. catRet_ = EOK; \
  472. } else { \
  473. catRet_ = ERANGE; \
  474. } \
  475. if (catRet_ != EOK) { \
  476. catRet_ = strcat_s((dest), (destMax), (src)); \
  477. } \
  478. } else { \
  479. catRet_ = strcat_s((dest), (destMax), (src)); \
  480. } \
  481. catRet_; \
  482. })
  483. #else
  484. #define SECUREC_STRCAT_SM(dest, destMax, src) strcat_s((dest), (destMax), (src))
  485. #endif
  486. /* For strncat_s performance optimization */
  487. #if defined(__GNUC__)
  488. #define SECUREC_STRNCAT_SM(dest, destMax, src, count) ({ \
  489. int ncatRet_ = EOK; \
  490. if ((void *)(dest) != NULL && (const void *)(src) != NULL && (size_t)(destMax) > 0 && \
  491. (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \
  492. (((unsigned long long)(count) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \
  493. char *ncatTmpDest_ = (char *)(dest); \
  494. size_t ncatRestSize_ = (size_t)(destMax); \
  495. while (ncatRestSize_ > 0 && *ncatTmpDest_ != '\0') { \
  496. ++ncatTmpDest_; \
  497. --ncatRestSize_; \
  498. } \
  499. if (ncatRestSize_ == 0) { \
  500. ncatRet_ = EINVAL; \
  501. } else if ((SECUREC_TWO_MIN((count), strlen(src)) + 1) <= ncatRestSize_) { \
  502. if ((size_t)(count) < strlen(src)) { \
  503. memcpy(ncatTmpDest_, (src), (count)); \
  504. *(ncatTmpDest_ + (count)) = '\0'; \
  505. } else { \
  506. memcpy(ncatTmpDest_, (src), strlen(src) + 1); \
  507. } \
  508. } else { \
  509. ncatRet_ = ERANGE; \
  510. } \
  511. if (ncatRet_ != EOK) { \
  512. ncatRet_ = strncat_s((dest), (destMax), (src), (count)); \
  513. } \
  514. } else { \
  515. ncatRet_ = strncat_s((dest), (destMax), (src), (count)); \
  516. } \
  517. ncatRet_; \
  518. })
  519. #else
  520. #define SECUREC_STRNCAT_SM(dest, destMax, src, count) strncat_s((dest), (destMax), (src), (count))
  521. #endif
  522. /* This macro do not check buffer overlap by default */
  523. #define SECUREC_MEMCPY_SM(dest, destMax, src, count) \
  524. (!(((size_t)(destMax) == 0) || \
  525. (((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \
  526. ((size_t)(count) > (size_t)(destMax)) || ((void *)(dest)) == NULL || ((const void *)(src) == NULL)) ? \
  527. (memcpy((dest), (src), (count)), EOK) : \
  528. (memcpy_s((dest), (destMax), (src), (count))))
  529. #define SECUREC_MEMSET_SM(dest, destMax, c, count) \
  530. (!((((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \
  531. ((void *)(dest) == NULL) || ((size_t)(count) > (size_t)(destMax))) ? \
  532. (memset((dest), (c), (count)), EOK) : \
  533. (memset_s((dest), (destMax), (c), (count))))
  534. #endif
  535. #endif