1 /* 2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 */ 18 19 /* * 20 * @defgroup iot_secure_c Secure C library APIs 21 * @ingroup iot_secure 22 */ 23 24 #ifndef __HI_STDLIB_H__ 25 #define __HI_STDLIB_H__ 26 #include <hi_types_base.h> 27 28 HI_START_HEADER 29 30 #ifndef EOK 31 #define EOK 0 32 #endif 33 34 #ifndef errno_t 35 typedef int errno_t; 36 #endif 37 38 #ifndef size_t 39 typedef unsigned int size_t; 40 #endif 41 42 /* * 43 * @ingroup iot_secure_c 44 * @brief Copies the source string to the destination buffer.CNcomment:复制源字符串到目的缓冲区。CNend 45 * 46 * @par 描述: Copies the source string to the destination buffer. 47 CNcomment:复制源字符串到目的缓冲区。CNend 48 * 49 * @attention None 50 * 51 * @param dest [OUT] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 52 * @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 53 * @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 54 * 55 * @retval #EOK Success 56 * @retval #Other Failure 57 * 58 * @par 依赖: 59 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 60 * @see None 61 * @since Hi3861_V100R001C00 62 */ 63 extern errno_t strcpy_s(char *dest, size_t dest_max, const char *src); 64 65 /* * 66 * @ingroup iot_secure_c 67 * @brief Copies the source string of a specified length to the destination buffer. 68 CNcomment:复制指定长度源字符串到目的缓冲区。CNend 69 * 70 * @par 描述:Copies the source string of a specified length to the destination buffer. 71 CNcomment:复制指定长度源字符串到目的缓冲区。CNend 72 * @attention None 73 * 74 * @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 75 * @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 76 * @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 77 * @param count [IN] type #size_t, Number of characters copied from the source buffer. 78 CNcomment:从源缓冲区中复制的字符数。CNend 79 * 80 * @retval #EOK Success 81 * @retval #Other Failure 82 * 83 * @par 依赖: 84 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 85 * @see None 86 * @since Hi3861_V100R001C00 87 */ 88 extern errno_t strncpy_s(char *dest, size_t dest_max, const char *src, size_t count); 89 90 /* * 91 * @ingroup iot_secure_c 92 * @brief Concatenates the source string to the end of the destination string. 93 CNcomment:将源字符串连接到目的字符串后面CNend 94 * 95 * @par 描述:Concatenates the source string to the end of the destination string. 96 CNcomment:将源字符串连接到目的字符串后面。CNend 97 * @attention None 98 * 99 * @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 100 * @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 101 * @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 102 * 103 * @retval #EOK Success 104 * @retval #Other Failure 105 * 106 * @par 依赖: 107 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 108 * @see None 109 * @since Hi3861_V100R001C00 110 */ 111 extern errno_t strcat_s(char *dest, size_t dest_max, const char *src); 112 113 /* * 114 * @ingroup iot_secure_c 115 * @brief Concatenates the source string of a specified length to the end of the destination string. 116 CNcomment:将指定长度的源字符串连接到目的字符串后面。CNend 117 * 118 * @par 描述: Concatenates the source string of a specified length to the end of the destination string. 119 CNcomment:将指定长度的源字符串连接到目的字符串后面。CNend 120 * @attention None 121 * 122 * @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 123 * @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 124 * @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 125 * @param count [IN] type #size_t, Number of characters copied from the source buffer. 126 CNcomment:从源缓冲区连接的字符数。CNend 127 * 128 * @retval #EOK Success 129 * @retval #Other Failure 130 * 131 * @par 依赖: 132 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 133 * @see None 134 * @since Hi3861_V100R001C00 135 */ 136 extern errno_t strncat_s(char *dest, size_t dest_max, const char *src, size_t count); 137 138 /* * 139 * @ingroup iot_secure_c 140 * @brief Copies the data from the source buffer to the destination buffer. 141 CNcomment:复制源缓冲区的数据到目的缓冲区。CNend 142 * 143 * @par 描述: Copies the data from the source buffer to the destination buffer. 144 CNcomment:复制源缓冲区的数据到目的缓冲区。CNend 145 * @attention None 146 * 147 * @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 148 * @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 149 * @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 150 * @param count [IN] type #size_t, Number of characters copied from the source buffer. 151 CNcomment:从源缓冲区中复制的字符数。CNend 152 * 153 * @retval #EOK Success 154 * @retval #Other Failure 155 * 156 * @par 依赖: 157 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 158 * @see None 159 * @since Hi3861_V100R001C00 160 */ 161 extern errno_t memcpy_s(void *dest, size_t dest_max, const void *src, size_t count); 162 163 /* * 164 * @ingroup iot_secure_c 165 * @brief Sets the size of the destination buffer to a specific value. 166 CNcomment:设置目的缓冲区为特定值。CNend 167 * 168 * @par 描述: Sets the size of the destination buffer to a specific value. 169 CNcomment:设置目的缓冲区为特定值。CNend 170 * @attention None 171 * 172 * @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 173 * @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 174 * @param c [IN] type #const #char *, Source buffer.CNcomment:特定值。CNend 175 * @param count [IN] type #size_t, Number of characters copied from the source buffer. 176 CNcomment:设置为特定值的字符数。CNend 177 * 178 * @retval #EOK Success 179 * @retval #Other Failure 180 * 181 * @par 依赖: 182 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 183 * @see None 184 * @since Hi3861_V100R001C00 185 */ 186 extern errno_t memset_s(void *dest, size_t dest_max, int c, size_t count); 187 188 /* * 189 * @ingroup iot_secure_c 190 * @brief Moves the data from the source buffer to the destination buffer. 191 CNcomment:移动源缓冲区的数据到目的缓冲区。CNend 192 * 193 * @par 描述:Moves the data from the source buffer to the destination buffer. 194 CNcomment:移动源缓冲区的数据到目的缓冲区。CNend 195 * @attention None 196 * 197 * @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 198 * @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 199 * @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 200 * @param count [IN] type #size_t, Number of characters copied from the source buffer. 201 CNcomment:从源缓冲区中移动的字符数。CNend 202 * 203 * @retval #EOK Success 204 * @retval #Other Failure 205 * 206 * @par 依赖: 207 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 208 * @see None 209 * @since Hi3861_V100R001C00 210 */ 211 extern errno_t memmove_s(void *dest, size_t dest_max, const void *src, size_t count); 212 213 /* * 214 * @ingroup iot_secure_c 215 * @brief Splits a string into substrings according to the specified separators. 216 CNcomment:将字符串按照指定的分隔符分割成子字符串。CNend 217 * 218 * @par 描述: Splits a string into substrings according to the specified separators. 219 CNcomment:将字符串按照指定的分隔符分割成子字符串。CNend 220 * @attention None 221 * 222 * @param token [IN] type #char *。 String to be split.CNcomment:要分割的字符串。CNend 223 * @param delimit [IN] type #const char *。 String separator.CNcomment:字符串分隔符。CNend 224 * @param context [IN] type #char* 。Position information after a call to HI_strtok_s is saved. 225 CNcomment:保存调用HI_strtok_s后的位置信息。CNend 226 * 227 * @retval #char* Point to the next token. CNcomment:指向在token中的下一个token。CNend 228 * @retval #HI_NULL A specified substring is not found or an error occurs. 229 CNcomment:没有找到指定的子字符串或者发生错误。CNend 230 * 231 * @par 依赖: 232 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 233 * @see None 234 * @since Hi3861_V100R001C00 235 */ 236 extern char *strtok_s(char *token, const char *delimit, char **context); 237 238 /* * 239 * @ingroup iot_secure_c 240 * @brief Formats the data and outputs it to the destination buffer. 241 CNcomment:将数据格式化输出到目的缓冲区。CNend 242 * 243 * @par 描述: Formats the data and outputs it to the destination buffer. 244 CNcomment:将数据格式化输出到目的缓冲区。CNend 245 * @attention None 246 * 247 * @param dest [OUT] type #char *。 Destination buffer.CNcomment:目的缓冲区。CNend 248 * @param dest_max [IN] type #size_t。 Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 249 * @param format [IN] type #const #char *。 Formatting control string.CNcomment:格式化控制字符串。CNend 250 * @param ... [IN] Optional parameter CNcomment:可选参数。CNend 251 * 252 * @retval #>=0 Return the number of bytes stored in dest, not counting the terminating null character. 253 CNcomment:返回存储在dest的字节数,不包括结束符CNend 254 * @retval #-1 Failure 255 * 256 * @par 依赖: 257 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 258 * @see None 259 * @since Hi3861_V100R001C00 260 */ 261 extern int sprintf_s(char *dest, size_t dest_max, const char *format, ...); 262 263 /* * 264 * @ingroup iot_secure_c 265 * @brief Formats the data according to a specified length and outputs the data to the destination buffer. 266 CNcomment:将数据按照指定长度格式化输出到目的缓冲区。CNend 267 * 268 * @par 描述: Formats the data according to a specified length and outputs the data to the destination buffer. 269 CNcomment:将数据按照指定长度格式化输出到目的缓冲区。CNend 270 * @attention None 271 * 272 * @param dest [OUT] type #char *。 Destination buffer.CNcomment:目的缓冲区。CNend 273 * @param dest_max [IN] type #size_t。 Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 274 * @param count [IN] type #size_t。 Number of formatted characters to be output to the destination buffer. 275 CNcomment:要输出到目的缓冲区的格式化字符个数。CNend 276 * @param format [IN] type #const #char *。 Formatting control string.CNcomment:格式化控制字符串。CNend 277 * @param ... [IN] Optional parameter CNcomment:可选参数。CNend 278 * 279 * @retval #>=0 Return the number of bytes stored in dest, not counting the terminating null character. 280 CNcomment:返回存储在dest的字节数,不包括结束符CNend 281 * @retval #-1 Failure 282 * 283 * @par 依赖: 284 * @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 285 * @see None 286 * @since Hi3861_V100R001C00 287 */ 288 extern int snprintf_s(char *dest, size_t dest_max, size_t count, const char *format, ...); 289 290 /* 291 * C库接口 292 */ 293 extern int memcmp(const void *str1, const void *str2, size_t n); 294 extern size_t strlen(const char *src); 295 extern int strcmp(const char *str1, const char *str2); 296 extern int strncmp(const char *str1, const char *str2, size_t n); 297 298 #if defined(HAVE_PCLINT_CHECK) 299 #define UT_CONST const 300 #else 301 #define UT_CONST 302 #endif 303 304 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION) 305 #if (LINUX_VERSION_CODE < KERNEL_VERSION(5,0,0)) 306 extern unsigned long strtoul(const char *nptr, char **endptr, int base); 307 #endif 308 #endif 309 extern UT_CONST char *strstr(const char *str1, const char *str2); 310 extern UT_CONST char *strchr(const char *s, int c); 311 312 HI_END_HEADER 313 #endif /* __HI_STDLIB_H__ */ 314