securec.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*******************************************************************************
  2. * Copyright @ Huawei Technologies Co., Ltd. 1998-2014. All rights reserved.
  3. * File name: securec.h
  4. * Decription:
  5. * the user of this secure c library should include this header file
  6. * in you source code. This header file declare all supported API
  7. * prototype of the library, such as memcpy_s, strcpy_s, wcscpy_s,
  8. * strcat_s, strncat_s, sprintf_s, scanf_s, and so on.
  9. * History:
  10. * 1. Date:
  11. * Author:
  12. * Modification:
  13. ********************************************************************************
  14. */
  15. #ifndef __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27
  16. #define __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27
  17. /* If you need high performance, enable the WITH_PERFORMANCE_ADDONS macro! */
  18. //#define WITH_PERFORMANCE_ADDONS
  19. #include "securectype.h" /*lint !e537*/
  20. #include <stdarg.h>
  21. /* If stack size on some embedded platform is limited, you can define the following macro
  22. * which will put some variables on heap instead of stack.
  23. #define STACK_SIZE_LESS_THAN_1K
  24. */
  25. /* for performance consideration, the following macro will call the corresponding API
  26. * of libC for memcpy, memmove and memset
  27. */
  28. #define CALL_LIBC_COR_API
  29. /* codes should run under the macro COMPATIBLE_LINUX_FORMAT in unknow system on default,
  30. and strtold. The function
  31. strtold is referenced first at ISO9899:1999(C99), and some old compilers can
  32. not support these functions. Here provides a macro to open these functions:
  33. SECUREC_SUPPORT_STRTOLD -- if defined, strtold will be used
  34. */
  35. /*define error code*/
  36. #ifndef errno_t
  37. typedef int errno_t;
  38. #endif
  39. /* success */
  40. #define EOK (0)
  41. /* invalid parameter */
  42. #ifdef EINVAL
  43. #undef EINVAL
  44. #endif
  45. #define EINVAL (22)
  46. #define EINVAL_AND_RESET (22 | 0X80)
  47. /* invalid parameter range */
  48. #ifdef ERANGE
  49. #undef ERANGE /* to avoid redefinition */
  50. #endif
  51. #define ERANGE (34)
  52. #define ERANGE_AND_RESET (34 | 0X80)
  53. #ifdef EOVERLAP_AND_RESET
  54. #undef EOVERLAP_AND_RESET
  55. #endif
  56. /*Once the buffer overlap is detected, the dest buffer must be reseted! */
  57. #define EOVERLAP_AND_RESET (54 | 0X80)
  58. /*if you need export the function of this library in Win32 dll, use __declspec(dllexport) */
  59. #ifdef __cplusplus
  60. extern "C"
  61. {
  62. #endif
  63. /**
  64. * @Description:The getHwSecureCVersion function get SecureC Version string and version number .
  65. * @param verStr - address to store verison string
  66. * @param bufSize -The maximum length of dest
  67. * @param verNumber - to store version number
  68. * @return EOK if there was no runtime-constraint violation
  69. */
  70. void getHwSecureCVersion(char* verStr, int bufSize, unsigned short* verNumber);
  71. /**
  72. * @Description:The wmemcpy_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest.
  73. * @param dest - destination address
  74. * @param destMax -The maximum length of destination buffer
  75. * @param src -source address
  76. * @param count -copies count wide characters from the src
  77. * @return EOK if there was no runtime-constraint violation
  78. */
  79. errno_t wmemcpy_s(wchar_t* dest, size_t destMax, const wchar_t* src, size_t count);
  80. /**
  81. * @Description:The memmove_s function copies n characters from the object pointed to by src into the object pointed to by dest.
  82. * @param dest - destination address
  83. * @param destMax -The maximum length of destination buffer
  84. * @param src -source address
  85. * @param count -copies count wide characters from the src
  86. * @return EOK if there was no runtime-constraint violation
  87. */
  88. errno_t memmove_s(void* dest, size_t destMax, const void* src, size_t count);
  89. /**
  90. * @Description:The wmemmove_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest.
  91. * @param dest - destination address
  92. * @param destMax -The maximum length of destination buffer
  93. * @param src -source address
  94. * @param count -copies count wide characters from the src
  95. * @return EOK if there was no runtime-constraint violation
  96. */
  97. errno_t wmemmove_s(wchar_t* dest, size_t destMax, const wchar_t* src, size_t count);
  98. /**
  99. * @Description:The wcscpy_s function copies the wide string pointed to by strSrc (including theterminating null wide character) into the array pointed to by strDest
  100. * @param strDest - destination address
  101. * @param destMax -The maximum length of destination buffer
  102. * @param strSrc -source address
  103. * @return EOK if there was no runtime-constraint violation
  104. */
  105. errno_t wcscpy_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc);
  106. /**
  107. * @Description:The wcsncpy_s function copies not more than n successive wide characters (not including the terminating null wide character)
  108. * from the array pointed to by strSrc to the array pointed to by strDest
  109. * @param strDest - destination address
  110. * @param destMax -The maximum length of destination buffer(including the terminating wide character)
  111. * @param strSrc -source address
  112. * @param count -copies count wide characters from the src
  113. * @return EOK if there was no runtime-constraint violation
  114. */
  115. errno_t wcsncpy_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc, size_t count);
  116. /**
  117. * @Description:The wcscat_s function appends a copy of the wide string pointed to by strSrc (including the terminating null wide character)
  118. * to the end of the wide string pointed to by strDest
  119. * @param strDest - destination address
  120. * @param destMax -The maximum length of destination buffer(including the terminating wide character)
  121. * @param strSrc -source address
  122. * @return EOK if there was no runtime-constraint violation
  123. */
  124. errno_t wcscat_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc);
  125. /**
  126. * @Description:The wcsncat_s function appends not more than n successive wide characters (not including the terminating null wide character)
  127. * from the array pointed to by strSrc to the end of the wide string pointed to by strDest.
  128. * @param strDest - destination address
  129. * @param destMax -The maximum length of destination buffer(including the terminating wide character)
  130. * @param strSrc -source address
  131. * @param count -copies count wide characters from the src
  132. * @return EOK if there was no runtime-constraint violation
  133. */
  134. errno_t wcsncat_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc, size_t count);
  135. /**
  136. * @Description: The strtok_s function parses a string into a sequence of tokens,On the first call to strtok_s the string to be parsed should be specified in strToken.
  137. * In each subsequent call that should parse the same string, strToken should be NULL
  138. * @param strToken - the string to be delimited
  139. * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string
  140. * @param context -is a pointer to a char * variable that is used internally by strtok_s function
  141. * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation.
  142. */
  143. char* strtok_s(char* strToken, const char* strDelimit, char** context);
  144. /**
  145. * @Description: The wcstok_s function is the wide-character equivalent of the strtok_s function
  146. * @param strToken - the string to be delimited
  147. * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string
  148. * @param context -is a pointer to a char * variable that is used internally by strtok_s function
  149. * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation.
  150. */
  151. wchar_t* wcstok_s(wchar_t* strToken, const wchar_t* strDelimit, wchar_t** context);
  152. /**
  153. * @Description: The sprintf_s function is equivalent to the sprintf function except for the parameter destMax and the explicit runtime-constraints violation
  154. * @param strDest - produce output according to a format ,write to the character string strDest
  155. * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0'))
  156. * @param format - fromat string
  157. * @return:success the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.
  158. */
  159. int sprintf_s(char* strDest, size_t destMax, const char* format, ...) SECUREC_ATTRIBUTE(3,4);
  160. /**
  161. * @Description: The swprintf_s function is the wide-character equivalent of the sprintf_s function
  162. * @param strDest - produce output according to a format ,write to the character string strDest
  163. * @param destMax - The maximum length of destination buffer(including the terminating null )
  164. * @param format - fromat string
  165. * @return:success the number of characters printed(not including the terminating null wide characte), If an error occurred return -1.
  166. */
  167. int swprintf_s(wchar_t* strDest, size_t destMax, const wchar_t* format, ...);
  168. /**
  169. * @Description: The vsprintf_s function is equivalent to the vsprintf function except for the parameter destMax and the explicit runtime-constraints violation
  170. * @param strDest - produce output according to a format ,write to the character string strDest
  171. * @param destMax - The maximum length of destination buffer(including the terminating null wide characte)
  172. * @param format - fromat string
  173. * @param argptr - instead of a variable number of arguments
  174. * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.
  175. */
  176. int vsprintf_s(char* strDest, size_t destMax, const char* format, va_list argptr) SECUREC_ATTRIBUTE(3,0);
  177. /**
  178. * @Description: The vswprintf_s function is the wide-character equivalent of the vsprintf_s function
  179. * @param strDest - produce output according to a format ,write to the character string strDest
  180. * @param destMax - The maximum length of destination buffer(including the terminating null )
  181. * @param format - fromat string
  182. * @param argptr - instead of a variable number of arguments
  183. * @return:return the number of characters printed(not including the terminating null wide characte), If an error occurred return -1.
  184. */
  185. int vswprintf_s(wchar_t* strDest, size_t destMax, const wchar_t* format, va_list argptr);
  186. /**
  187. * @Description: The vsnprintf_s function is equivalent to the vsnprintf function except for the parameter destMax/count and the explicit runtime-constraints violation
  188. * @param strDest - produce output according to a format ,write to the character string strDest
  189. * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0'))
  190. * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0'))
  191. * @param format - fromat string
  192. * @param argptr - instead of a variable number of arguments
  193. * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.
  194. */
  195. int vsnprintf_s(char* strDest, size_t destMax, size_t count, const char* format, va_list arglist) SECUREC_ATTRIBUTE(4,0);
  196. /**
  197. * @Description: The snprintf_s function is equivalent to the snprintf function except for the parameter destMax/count and the explicit runtime-constraints violation
  198. * @param strDest - produce output according to a format ,write to the character string strDest
  199. * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0'))
  200. * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0'))
  201. * @param format - fromat string
  202. * @param argptr - instead of a variable number of arguments
  203. * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.
  204. */
  205. int snprintf_s(char* strDest, size_t destMax, size_t count, const char* format, ...) SECUREC_ATTRIBUTE(4,5);
  206. /**
  207. * @Description: The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s
  208. * @param format - fromat string
  209. * @return:returns the number of input items assigned, If an error occurred return -1.
  210. */
  211. int scanf_s(const char* format, ...);
  212. /**
  213. * @Description: The wscanf_s function is the wide-character equivalent of the scanf_s function
  214. * @param format - fromat string
  215. * @return:returns the number of input items assigned, If an error occurred return -1.
  216. */
  217. int wscanf_s(const wchar_t* format, ...);
  218. /**
  219. * @Description: The vscanf_s function is equivalent to scanf_s, with the variable argument list replaced by arglist,
  220. * @param format - fromat string
  221. * @param arglist - instead of a variable number of arguments
  222. * @return:returns the number of input items assigned, If an error occurred return -1.
  223. */
  224. int vscanf_s(const char* format, va_list arglist);
  225. /**
  226. * @Description: The vwscanf_s function is the wide-character equivalent of the vscanf_s function
  227. * @param format - fromat string
  228. * @param arglist - instead of a variable number of arguments
  229. * @return:returns the number of input items assigned, If an error occurred return -1.
  230. */
  231. int vwscanf_s(const wchar_t* format, va_list arglist);
  232. /**
  233. * @Description: The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a*)
  234. * @param stream - stdio file stream
  235. * @param format - fromat string
  236. * @return:returns the number of input items assigned, If an error occurred return -1.
  237. */
  238. int fscanf_s(FILE* stream, const char* format, ...);
  239. /**
  240. * @Description: The fwscanf_s function is the wide-character equivalent of the fscanf_s function
  241. * @param stream - stdio file stream
  242. * @param format - fromat string
  243. * @return:returns the number of input items assigned, If an error occurred return -1.
  244. */
  245. int fwscanf_s(FILE* stream, const wchar_t* format, ...);
  246. /**
  247. * @Description: The vfscanf_s function is equivalent to fscanf_s, with the variable argument list replaced by arglist
  248. * @param stream - stdio file stream
  249. * @param format - fromat string
  250. * @param arglist - instead of a variable number of arguments
  251. * @return:returns the number of input items assigned, If an error occurred return -1.
  252. */
  253. int vfscanf_s(FILE* stream, const char* format, va_list arglist);
  254. /**
  255. * @Description: The vfwscanf_s function is the wide-character equivalent of the vfscanf_s function
  256. * @param stream - stdio file stream
  257. * @param format - fromat string
  258. * @param arglist - instead of a variable number of arguments
  259. * @return:returns the number of input items assigned, If an error occurred return -1.
  260. */
  261. int vfwscanf_s(FILE* stream, const wchar_t* format, va_list arglist);
  262. /**
  263. * @Description: The sscanf_s function is equivalent to fscanf_s, except that input is obtained from a string (specified by the argument buffer) rather than from a stream
  264. * @param buffer - read character from buffer
  265. * @param format - fromat string
  266. * @return:returns the number of input items assigned, If an error occurred return -1.
  267. */
  268. int sscanf_s(const char* buffer, const char* format, ...);
  269. /**
  270. * @Description: The swscanf_s function is the wide-character equivalent of the sscanf_s function
  271. * @param buffer - read character from buffer
  272. * @param format - fromat string
  273. * @return:returns the number of input items assigned, If an error occurred return -1.
  274. */
  275. int swscanf_s(const wchar_t* buffer, const wchar_t* format, ...);
  276. /**
  277. * @Description: The vsscanf_s function is equivalent to sscanf_s, with the variable argument list replaced by argptr
  278. * @param buffer - read character from buffer
  279. * @param format - fromat string
  280. * @param arglist - instead of a variable number of arguments
  281. * @return:returns the number of input items assigned, If an error occurred return -1.
  282. */
  283. int vsscanf_s(const char* buffer, const char* format, va_list argptr);
  284. /**
  285. * @Description: The vswscanf_s function is the wide-character equivalent of the vsscanf_s function
  286. * @param buffer - read character from buffer
  287. * @param format - fromat string
  288. * @param arglist - instead of a variable number of arguments
  289. * @return:returns the number of input items assigned, If an error occurred return -1.
  290. */
  291. int vswscanf_s(const wchar_t* buffer, const wchar_t* format, va_list arglist);
  292. /**
  293. * @Description:The gets_s function reads at most one less than the number of characters specified by destMax from the stream pointed to by stdin, into the array pointed to by buffer
  294. * @param buffer - destination address
  295. * @param destMax -The maximum length of destination buffer(including the terminating null character)
  296. * @return EOK if there was no runtime-constraint violation
  297. */
  298. char* gets_s(char* buffer, size_t destMax);
  299. /**
  300. * @Description:The memset_s function copies the value of c (converted to an unsigned char) into each of the first count characters of the object pointed to by dest.
  301. * @param dest - destination address
  302. * @param destMax -The maximum length of destination buffer
  303. * @param c - the value to be copied
  304. * @param count -copies fisrt count characters of dest
  305. * @return EOK if there was no runtime-constraint violation
  306. */
  307. errno_t memset_s(void* dest, size_t destMax, int c, size_t count);
  308. /**
  309. * @Description:The memcpy_s function copies n characters from the object pointed to by src into the object pointed to by dest.
  310. * @param dest - destination address
  311. * @param destMax -The maximum length of destination buffer
  312. * @param src -source address
  313. * @param count -copies count characters from the src
  314. * @return EOK if there was no runtime-constraint violation
  315. */
  316. errno_t memcpy_s(void* dest, size_t destMax, const void* src, size_t count);
  317. /**
  318. * @Description:The strcpy_s function copies the string pointed to by strSrc (including the terminating null character) into the array pointed to by strDest
  319. * @param strDest - destination address
  320. * @param destMax -The maximum length of destination buffer(including the terminating null character)
  321. * @param strSrc -source address
  322. * @return EOK if there was no runtime-constraint violation
  323. */
  324. errno_t strcpy_s(char* strDest, size_t destMax, const char* strSrc);
  325. /**
  326. * @Description:The strncpy_s function copies not more than n successive characters (not including the terminating null character)
  327. * from the array pointed to by strSrc to the array pointed to by strDest
  328. * @param strDest - destination address
  329. * @param destMax -The maximum length of destination buffer(including the terminating null character)
  330. * @param strSrc -source address
  331. * @param count -copies count characters from the src
  332. * @return EOK if there was no runtime-constraint violation
  333. */
  334. errno_t strncpy_s(char* strDest, size_t destMax, const char* strSrc, size_t count);
  335. /**
  336. * @Description:The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character)
  337. * to the end of the string pointed to by strDest
  338. * @param strDest - destination address
  339. * @param destMax -The maximum length of destination buffer(including the terminating null wide character)
  340. * @param strSrc -source address
  341. * @return EOK if there was no runtime-constraint violation
  342. */
  343. errno_t strcat_s(char* strDest, size_t destMax, const char* strSrc);
  344. /**
  345. * @Description:The strncat_s function appends not more than n successive characters (not including the terminating null character)
  346. * from the array pointed to by strSrc to the end of the string pointed to by strDest.
  347. * @param strDest - destination address
  348. * @param destMax -The maximum length of destination buffer(including the terminating null character)
  349. * @param strSrc -source address
  350. * @param count -copies count characters from the src
  351. * @return EOK if there was no runtime-constraint violation
  352. */
  353. errno_t strncat_s(char* strDest, size_t destMax, const char* strSrc, size_t count);
  354. /* those functions are used by macro */
  355. errno_t strncpy_error(char* strDest, size_t destMax, const char* strSrc, size_t count);
  356. errno_t strcpy_error(char* strDest, size_t destMax, const char* strSrc);
  357. #if defined(WITH_PERFORMANCE_ADDONS)
  358. /* those functions are used by macro */
  359. errno_t memset_sOptAsm(void* dest, size_t destMax, int c, size_t count);
  360. errno_t memset_sOptTc(void* dest, size_t destMax, int c, size_t count);
  361. errno_t memcpy_sOptAsm(void* dest, size_t destMax, const void* src, size_t count);
  362. errno_t memcpy_sOptTc(void* dest, size_t destMax, const void* src, size_t count);
  363. /* strcpy_sp is a macro, NOT a function in performance optimization mode. */
  364. #define strcpy_sp(dest, destMax, src) /*lint -save -e506 -e1055 -e650 -e778 -e802 */ (( __builtin_constant_p((destMax)) && __builtin_constant_p((src))) ? \
  365. STRCPY_SM((dest), (destMax), (src)) : strcpy_s((dest), (destMax), (src)) ) /*lint -restore */
  366. /* strncpy_sp is a macro, NOT a function in performance optimization mode. */
  367. #define strncpy_sp(dest, destMax, src, count) /*lint -save -e506 -e1055 -e666 -e650 -e778 -e802 */ ((__builtin_constant_p((count)) && __builtin_constant_p((destMax)) && __builtin_constant_p((src))) ? \
  368. STRNCPY_SM((dest), (destMax), (src), (count)) : strncpy_s((dest), (destMax), (src), (count)) ) /*lint -restore */
  369. /* strcat_sp is a macro, NOT a function in performance optimization mode. */
  370. #define strcat_sp(dest, destMax, src) /*lint -save -e506 -e1055 -e650 -e778 -e802 */ (( __builtin_constant_p((destMax)) && __builtin_constant_p((src))) ? \
  371. STRCAT_SM((dest), (destMax), (src)) : strcat_s((dest), (destMax), (src)) ) /*lint -restore */
  372. /* strncat_sp is a macro, NOT a function in performance optimization mode. */
  373. #define strncat_sp(dest, destMax, src, count) /*lint -save -e506 -e1055 -e666 -e650 -e778 -e802 */ ((__builtin_constant_p((count)) && __builtin_constant_p((destMax)) && __builtin_constant_p((src))) ? \
  374. STRNCAT_SM((dest), (destMax), (src), (count)) : strncat_s((dest), (destMax), (src), (count)) ) /*lint -restore */
  375. /* memcpy_sp is a macro, NOT a function in performance optimization mode. */
  376. #define memcpy_sp(dest, destMax, src, count) /*lint -save -e506 -e1055 -e650 -e778 -e802 */ (__builtin_constant_p((count)) ? (MEMCPY_SM((dest), (destMax), (src), (count))) : \
  377. (__builtin_constant_p((destMax)) ? (((size_t)(destMax) > 0 && (((UINT64T)(destMax) & (UINT64T)(-2)) < SECUREC_MEM_MAX_LEN)) ? memcpy_sOptTc((dest), (destMax), (src), (count)) : ERANGE ) : memcpy_sOptAsm((dest), (destMax), (src), (count)))) /*lint -restore */
  378. /* memset_sp is a macro, NOT a function in performance optimization mode. */
  379. #define memset_sp(dest, destMax, c, count) /*lint -save -e506 -e1055 -e650 -e778 -e802 */ (__builtin_constant_p((count)) ? (MEMSET_SM((dest), (destMax), (c), (count))) : \
  380. (__builtin_constant_p((destMax)) ? (((size_t)(destMax) > 0 && (((UINT64T)(destMax) & (UINT64T)(-2)) < SECUREC_MEM_MAX_LEN)) ? memset_sOptTc((dest), (destMax), (c), (count)) : ERANGE ) : memset_sOptAsm((dest), (destMax), (c), (count)))) /*lint -restore */
  381. #endif
  382. #ifdef __cplusplus
  383. }
  384. #endif /* __cplusplus */
  385. #endif/* __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 */