hi_securec.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 __HI_SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27
  16. #define __HI_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 "securec.h"
  21. #include <stdarg.h>
  22. /*if you need export the function of this library in Win32 dll, use __declspec(dllexport) */
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. /**
  28. * @Description:The wmemcpy_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest.
  29. * @param dest - destination address
  30. * @param destMax -The maximum length of destination buffer
  31. * @param src -source address
  32. * @param count -copies count wide characters from the src
  33. * @return EOK if there was no runtime-constraint violation
  34. */
  35. static inline int hi_wmemcpy(wchar_t* dest, size_t destMax, const wchar_t* src, size_t count)
  36. {
  37. return wmemcpy_s(dest, destMax, src, count);
  38. }
  39. /**
  40. * @Description:The memmove_s function copies n characters from the object pointed to by src into the object pointed to by dest.
  41. * @param dest - destination address
  42. * @param destMax -The maximum length of destination buffer
  43. * @param src -source address
  44. * @param count -copies count wide characters from the src
  45. * @return EOK if there was no runtime-constraint violation
  46. */
  47. static inline int hi_memmove(void* dest, size_t destMax, const void* src, size_t count)
  48. {
  49. return memmove_s(dest, destMax, src, count);
  50. }
  51. /**
  52. * @Description:The wmemmove_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest.
  53. * @param dest - destination address
  54. * @param destMax -The maximum length of destination buffer
  55. * @param src -source address
  56. * @param count -copies count wide characters from the src
  57. * @return EOK if there was no runtime-constraint violation
  58. */
  59. static inline int hi_wmemmove(wchar_t* dest, size_t destMax, const wchar_t* src, size_t count)
  60. {
  61. return wmemmove_s(dest, destMax, src, count);
  62. }
  63. /**
  64. * @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
  65. * @param strDest - destination address
  66. * @param destMax -The maximum length of destination buffer
  67. * @param strSrc -source address
  68. * @return EOK if there was no runtime-constraint violation
  69. */
  70. static inline int hi_wcscpy(wchar_t* strDest, size_t destMax, const wchar_t* strSrc)
  71. {
  72. return wcscpy_s(strDest, destMax, strSrc);
  73. }
  74. /**
  75. * @Description:The wcsncpy_s function copies not more than n successive wide characters (not including the terminating null wide character)
  76. * from the array pointed to by strSrc to the array pointed to by strDest
  77. * @param strDest - destination address
  78. * @param destMax -The maximum length of destination buffer(including the terminating wide character)
  79. * @param strSrc -source address
  80. * @param count -copies count wide characters from the src
  81. * @return EOK if there was no runtime-constraint violation
  82. */
  83. static inline int hi_wcsncpy(wchar_t* strDest, size_t destMax, const wchar_t* strSrc, size_t count)
  84. {
  85. return wcsncpy_s(strDest, destMax, strSrc, count);
  86. }
  87. /**
  88. * @Description:The wcscat_s function appends a copy of the wide string pointed to by strSrc (including the terminating null wide character)
  89. * to the end of the wide string pointed to by strDest
  90. * @param strDest - destination address
  91. * @param destMax -The maximum length of destination buffer(including the terminating wide character)
  92. * @param strSrc -source address
  93. * @return EOK if there was no runtime-constraint violation
  94. */
  95. static inline int hi_wcscat(wchar_t* strDest, size_t destMax, const wchar_t* strSrc)
  96. {
  97. return wcscat_s(strDest, destMax, strSrc);
  98. }
  99. /**
  100. * @Description:The wcsncat_s function appends not more than n successive wide characters (not including the terminating null wide character)
  101. * from the array pointed to by strSrc to the end of the wide string pointed to by strDest.
  102. * @param strDest - destination address
  103. * @param destMax -The maximum length of destination buffer(including the terminating wide character)
  104. * @param strSrc -source address
  105. * @param count -copies count wide characters from the src
  106. * @return EOK if there was no runtime-constraint violation
  107. */
  108. static inline int hi_wcsncat(wchar_t* strDest, size_t destMax, const wchar_t* strSrc, size_t count)
  109. {
  110. return wcsncat_s(strDest, destMax, strSrc, count);
  111. }
  112. /**
  113. * @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.
  114. * In each subsequent call that should parse the same string, strToken should be NULL
  115. * @param strToken - the string to be delimited
  116. * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string
  117. * @param context -is a pointer to a char * variable that is used internally by strtok_s function
  118. * @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.
  119. */
  120. static inline char* hi_strtok(char* strToken, const char* strDelimit, char** context)
  121. {
  122. return strtok_s(strToken, strDelimit, context);
  123. }
  124. /**
  125. * @Description: The wcstok_s function is the wide-character equivalent of the strtok_s function
  126. * @param strToken - the string to be delimited
  127. * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string
  128. * @param context -is a pointer to a char * variable that is used internally by strtok_s function
  129. * @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.
  130. */
  131. #define hi_wcstok(strToken, strDelimit, context) \
  132. wcstok_s(strToken, strDelimit, context)
  133. /**
  134. * @Description: The sprintf_s function is equivalent to the sprintf function except for the parameter destMax and the explicit runtime-constraints violation
  135. * @param strDest - produce output according to a format ,write to the character string strDest
  136. * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0'))
  137. * @param format - fromat string
  138. * @return:success the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.
  139. */
  140. #define hi_sprintf(strDest, destMax, format, ...) \
  141. sprintf_s(strDest, destMax, format, ##__VA_ARGS__)
  142. /**
  143. * @Description: The swprintf_s function is the wide-character equivalent of the sprintf_s function
  144. * @param strDest - produce output according to a format ,write to the character string strDest
  145. * @param destMax - The maximum length of destination buffer(including the terminating null )
  146. * @param format - fromat string
  147. * @return:success the number of characters printed(not including the terminating null wide characte), If an error occurred return -1.
  148. */
  149. #define hi_swprintf(strDest, destMax, format, ...) \
  150. swprintf_s(strDest, destMax, format, ##__VA_ARGS__)
  151. /**
  152. * @Description: The snprintf_s function is equivalent to the snprintf function except for the parameter destMax/count and the explicit runtime-constraints violation
  153. * @param strDest - produce output according to a format ,write to the character string strDest
  154. * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0'))
  155. * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0'))
  156. * @param format - fromat string
  157. * @param argptr - instead of a variable number of arguments
  158. * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.
  159. */
  160. #define hi_snprintf(strDest, destMax, count, format, ...) \
  161. snprintf_s(strDest, destMax, count, format, ##__VA_ARGS__)
  162. /**
  163. * @Description: The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s
  164. * @param format - fromat string
  165. * @return:returns the number of input items assigned, If an error occurred return -1.
  166. */
  167. #define hi_scanf(format, ...) scanf_s(format, ##__VA_ARGS__)
  168. /**
  169. * @Description: The wscanf_s function is the wide-character equivalent of the scanf_s function
  170. * @param format - fromat string
  171. * @return:returns the number of input items assigned, If an error occurred return -1.
  172. */
  173. #define hi_wscanf(format, ...) wscanf_s(format, ##__VA_ARGS__)
  174. /**
  175. * @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*)
  176. * @param stream - stdio file stream
  177. * @param format - fromat string
  178. * @return:returns the number of input items assigned, If an error occurred return -1.
  179. */
  180. #define hi_fscanf(stream, format, ...) fscanf_s(stream, format, ##__VA_ARGS__)
  181. /**
  182. * @Description: The fwscanf_s function is the wide-character equivalent of the fscanf_s function
  183. * @param stream - stdio file stream
  184. * @param format - fromat string
  185. * @return:returns the number of input items assigned, If an error occurred return -1.
  186. */
  187. #define hi_fwscanf(stream, format, ...) fwscanf_s(stream, format, ##__VA_ARGS__)
  188. /**
  189. * @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
  190. * @param buffer - read character from buffer
  191. * @param format - fromat string
  192. * @return:returns the number of input items assigned, If an error occurred return -1.
  193. */
  194. #define hi_sscanf(buffer, format, ...) sscanf_s(buffer, format, ##__VA_ARGS__)
  195. /**
  196. * @Description: The swscanf_s function is the wide-character equivalent of the sscanf_s function
  197. * @param buffer - read character from buffer
  198. * @param format - fromat string
  199. * @return:returns the number of input items assigned, If an error occurred return -1.
  200. */
  201. #define hi_swscanf(buffer, format, ...) swscanf_s(buffer, format, ##__VA_ARGS__)
  202. /**
  203. * @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
  204. * @param buffer - destination address
  205. * @param destMax -The maximum length of destination buffer(including the terminating null character)
  206. * @return EOK if there was no runtime-constraint violation
  207. */
  208. static inline char* hi_gets(char* buffer, size_t destMax)
  209. {
  210. return gets_s(buffer, destMax);
  211. }
  212. /**
  213. * @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.
  214. * @param dest - destination address
  215. * @param destMax -The maximum length of destination buffer
  216. * @param c - the value to be copied
  217. * @param count -copies fisrt count characters of dest
  218. * @return EOK if there was no runtime-constraint violation
  219. */
  220. static inline int hi_memset(void* dest, size_t destMax, int c, size_t count)
  221. {
  222. return memset_s(dest, destMax, c, count);
  223. }
  224. /**
  225. * @Description:The memcpy_s function copies n characters from the object pointed to by src into the object pointed to by dest.
  226. * @param dest - destination address
  227. * @param destMax -The maximum length of destination buffer
  228. * @param src -source address
  229. * @param count -copies count characters from the src
  230. * @return EOK if there was no runtime-constraint violation
  231. */
  232. static inline int hi_memcpy(void* dest, size_t destMax, const void* src, size_t count)
  233. {
  234. return memcpy_s(dest, destMax, src, count);
  235. }
  236. /**
  237. * @Description:The strcpy_s function copies the string pointed to by strSrc (including the terminating null character) into the array pointed to by strDest
  238. * @param strDest - destination address
  239. * @param destMax -The maximum length of destination buffer(including the terminating null character)
  240. * @param strSrc -source address
  241. * @return EOK if there was no runtime-constraint violation
  242. */
  243. static inline int hi_strcpy(char* strDest, size_t destMax, const char* strSrc)
  244. {
  245. return strcpy_s(strDest, destMax, strSrc);
  246. }
  247. /**
  248. * @Description:The strncpy_s function copies not more than n successive characters (not including the terminating null character)
  249. * from the array pointed to by strSrc to the array pointed to by strDest
  250. * @param strDest - destination address
  251. * @param destMax -The maximum length of destination buffer(including the terminating null character)
  252. * @param strSrc -source address
  253. * @param count -copies count characters from the src
  254. * @return EOK if there was no runtime-constraint violation
  255. */
  256. static inline int hi_strncpy(char* strDest, size_t destMax, const char* strSrc, size_t count)
  257. {
  258. return strncpy_s(strDest, destMax, strSrc, count);
  259. }
  260. /**
  261. * @Description:The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character)
  262. * to the end of the string pointed to by strDest
  263. * @param strDest - destination address
  264. * @param destMax -The maximum length of destination buffer(including the terminating null wide character)
  265. * @param strSrc -source address
  266. * @return EOK if there was no runtime-constraint violation
  267. */
  268. static inline int hi_strcat(char* strDest, size_t destMax, const char* strSrc)
  269. {
  270. return strcat_s(strDest, destMax, strSrc);
  271. }
  272. /**
  273. * @Description:The strncat_s function appends not more than n successive characters (not including the terminating null character)
  274. * from the array pointed to by strSrc to the end of the string pointed to by strDest.
  275. * @param strDest - destination address
  276. * @param destMax -The maximum length of destination buffer(including the terminating null character)
  277. * @param strSrc -source address
  278. * @param count -copies count characters from the src
  279. * @return EOK if there was no runtime-constraint violation
  280. */
  281. static inline int hi_strncat(char* strDest, size_t destMax, const char* strSrc, size_t count)
  282. {
  283. return strncat_s(strDest, destMax, strSrc, count);
  284. }
  285. #ifdef __cplusplus
  286. }
  287. #endif /* __cplusplus */
  288. #endif/* __HI_SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 */