1/** 2* @file hi_stdlib.h 3* 4* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 5* Licensed under the Apache License, Version 2.0 (the "License"); 6* you may not use this file except in compliance with the License. 7* You may obtain a copy of the License at 8* 9* http://www.apache.org/licenses/LICENSE-2.0 10* 11* Unless required by applicable law or agreed to in writing, software 12* distributed under the License is distributed on an "AS IS" BASIS, 13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14* See the License for the specific language governing permissions and 15* limitations under the License. 16* 17* Description: A parameter is added to the security C APIs based on the standard C interface, that is, the upper 18* limit of the write operation address space to prevent out-of-bounds write. 19CNcomment:安全C接口主要是基于标准C接口增加了一个参数,即写操作地址空间上限,从而防止写越界。CNend \n 20* Create: 2019-12-18 21*/ 22 23/** 24 * @defgroup iot_secure_c Secure C library APIs 25 * @ingroup iot_secure 26 */ 27 28#ifndef __HI_STDLIB_H__ 29#define __HI_STDLIB_H__ 30#include <hi_types_base.h> 31 32HI_START_HEADER 33 34#ifndef EOK 35#define EOK 0 36#endif 37 38#ifndef errno_t 39typedef int errno_t; 40#endif 41 42#ifndef size_t 43typedef unsigned int size_t; 44#endif 45 46/** 47* @ingroup iot_secure_c 48* @brief Copies the source string to the destination buffer.CNcomment:复制源字符串到目的缓冲区。CNend 49* 50* @par 描述: Copies the source string to the destination buffer. 51CNcomment:复制源字符串到目的缓冲区。CNend 52* 53* @attention None 54* 55* @param dest [OUT] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 56* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 57* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 58* 59* @retval #EOK Success 60* @retval #Other Failure 61* 62* @par 依赖: 63* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 64* @see None 65*/ 66extern errno_t strcpy_s(char *dest, size_t dest_max, const char *src); 67 68/** 69* @ingroup iot_secure_c 70* @brief Copies the source string of a specified length to the destination buffer. 71CNcomment:复制指定长度源字符串到目的缓冲区。CNend 72* 73* @par 描述:Copies the source string of a specified length to the destination buffer. 74CNcomment:复制指定长度源字符串到目的缓冲区。CNend 75* @attention None 76* 77* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 78* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 79* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 80* @param count [IN] type #size_t, Number of characters copied from the source buffer. 81CNcomment:从源缓冲区中复制的字符数。CNend 82* 83* @retval #EOK Success 84* @retval #Other Failure 85* 86* @par 依赖: 87* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 88* @see None 89*/ 90extern errno_t strncpy_s(char *dest, size_t dest_max, const char *src, size_t count); 91 92/** 93* @ingroup iot_secure_c 94* @brief Concatenates the source string to the end of the destination string. 95CNcomment:将源字符串连接到目的字符串后面CNend 96* 97* @par 描述:Concatenates the source string to the end of the destination string. 98CNcomment:将源字符串连接到目的字符串后面。CNend 99* @attention None 100* 101* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 102* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 103* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 104* 105* @retval #EOK Success 106* @retval #Other Failure 107* 108* @par 依赖: 109* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 110* @see None 111*/ 112extern errno_t strcat_s(char *dest, size_t dest_max, const char *src); 113 114/** 115* @ingroup iot_secure_c 116* @brief Concatenates the source string of a specified length to the end of the destination string. 117CNcomment:将指定长度的源字符串连接到目的字符串后面。CNend 118* 119* @par 描述: Concatenates the source string of a specified length to the end of the destination string. 120CNcomment:将指定长度的源字符串连接到目的字符串后面。CNend 121* @attention None 122* 123* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 124* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 125* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 126* @param count [IN] type #size_t, Number of characters copied from the source buffer. 127CNcomment:从源缓冲区连接的字符数。CNend 128* 129* @retval #EOK Success 130* @retval #Other Failure 131* 132* @par 依赖: 133* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 134* @see None 135*/ 136extern 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. 141CNcomment:复制源缓冲区的数据到目的缓冲区。CNend 142* 143* @par 描述: Copies the data from the source buffer to the destination buffer. 144CNcomment:复制源缓冲区的数据到目的缓冲区。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. 151CNcomment:从源缓冲区中复制的字符数。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*/ 160extern errno_t memcpy_s(void *dest, size_t dest_max, const void *src, size_t count); 161 162/** 163* @ingroup iot_secure_c 164* @brief Sets the size of the destination buffer to a specific value. 165CNcomment:设置目的缓冲区为特定值。CNend 166* 167* @par 描述: Sets the size of the destination buffer to a specific value. 168CNcomment:设置目的缓冲区为特定值。CNend 169* @attention None 170* 171* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 172* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 173* @param c [IN] type #const #char *, Source buffer.CNcomment:特定值。CNend 174* @param count [IN] type #size_t, Number of characters copied from the source buffer. 175CNcomment:设置为特定值的字符数。CNend 176* 177* @retval #EOK Success 178* @retval #Other Failure 179* 180* @par 依赖: 181* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 182* @see None 183*/ 184extern errno_t memset_s(void *dest, size_t dest_max, int c, size_t count); 185 186/** 187* @ingroup iot_secure_c 188* @brief Moves the data from the source buffer to the destination buffer. 189CNcomment:移动源缓冲区的数据到目的缓冲区。CNend 190* 191* @par 描述:Moves the data from the source buffer to the destination buffer. 192CNcomment:移动源缓冲区的数据到目的缓冲区。CNend 193* @attention None 194* 195* @param dest [IN] type #char *, Destination buffer.CNcomment:目的缓冲区。CNend 196* @param dest_max [IN] type #size_t, Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 197* @param src [IN] type #const #char *, Source buffer.CNcomment:源缓冲区。CNend 198* @param count [IN] type #size_t, Number of characters copied from the source buffer. 199CNcomment:从源缓冲区中移动的字符数。CNend 200* 201* @retval #EOK Success 202* @retval #Other Failure 203* 204* @par 依赖: 205* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 206* @see None 207*/ 208extern errno_t memmove_s(void *dest, size_t dest_max, const void *src, size_t count); 209 210/** 211* @ingroup iot_secure_c 212* @brief Splits a string into substrings according to the specified separators. 213CNcomment:将字符串按照指定的分隔符分割成子字符串。CNend 214* 215* @par 描述: Splits a string into substrings according to the specified separators. 216CNcomment:将字符串按照指定的分隔符分割成子字符串。CNend 217* @attention None 218* 219* @param token [IN] type #char *。 String to be split.CNcomment:要分割的字符串。CNend 220* @param delimit [IN] type #const char *。 String separator.CNcomment:字符串分隔符。CNend 221* @param context [IN] type #char** 。Position information after a call to HI_strtok_s is saved. 222CNcomment:保存调用HI_strtok_s后的位置信息。CNend 223* 224* @retval #char* Point to the next token. CNcomment:指向在token中的下一个token。CNend 225* @retval #HI_NULL A specified substring is not found or an error occurs. 226CNcomment:没有找到指定的子字符串或者发生错误。CNend 227* 228* @par 依赖: 229* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 230* @see None 231*/ 232extern char *strtok_s(char *token, const char *delimit, char **context); 233 234/** 235* @ingroup iot_secure_c 236* @brief Formats the data and outputs it to the destination buffer. 237CNcomment:将数据格式化输出到目的缓冲区。CNend 238* 239* @par 描述: Formats the data and outputs it to the destination buffer. 240CNcomment:将数据格式化输出到目的缓冲区。CNend 241* @attention None 242* 243* @param dest [OUT] type #char *。 Destination buffer.CNcomment:目的缓冲区。CNend 244* @param dest_max [IN] type #size_t。 Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 245* @param format [IN] type #const #char *。 Formatting control string.CNcomment:格式化控制字符串。CNend 246* @param ... [IN] Optional parameter CNcomment:可选参数。CNend 247* 248* @retval #>=0 Return the number of bytes stored in dest, not counting the terminating null character. 249CNcomment:返回存储在dest的字节数,不包括结束符CNend 250* @retval #-1 Failure 251* 252* @par 依赖: 253* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 254* @see None 255*/ 256extern int sprintf_s(char *dest, size_t dest_max, const char *format, ...); 257 258/** 259* @ingroup iot_secure_c 260* @brief Formats the data according to a specified length and outputs the data to the destination buffer. 261CNcomment:将数据按照指定长度格式化输出到目的缓冲区。CNend 262* 263* @par 描述: Formats the data according to a specified length and outputs the data to the destination buffer. 264CNcomment:将数据按照指定长度格式化输出到目的缓冲区。CNend 265* @attention None 266* 267* @param dest [OUT] type #char *。 Destination buffer.CNcomment:目的缓冲区。CNend 268* @param dest_max [IN] type #size_t。 Size of the destination buffer.CNcomment:目的缓冲区大小。CNend 269* @param count [IN] type #size_t。 Number of formatted characters to be output to the destination buffer. 270CNcomment:要输出到目的缓冲区的格式化字符个数。CNend 271* @param format [IN] type #const #char *。 Formatting control string.CNcomment:格式化控制字符串。CNend 272* @param ... [IN] Optional parameter CNcomment:可选参数。CNend 273* 274* @retval #>=0 Return the number of bytes stored in dest, not counting the terminating null character. 275CNcomment:返回存储在dest的字节数,不包括结束符CNend 276* @retval #-1 Failure 277* 278* @par 依赖: 279* @li hi_stdlib.h: This file declares the APIs.CNcomment:该接口声明所在的头文件。CNend 280* @see None 281*/ 282extern int snprintf_s(char *dest, size_t dest_max, size_t count, const char *format, ...); 283 284extern int memcmp(const void *str1, const void *str2, size_t n); 285extern size_t strlen(const char *src); 286extern int strcmp(const char *str1, const char *str2); 287extern int strncmp(const char *str1, const char *str2, size_t n); 288 289#if defined(HAVE_PCLINT_CHECK) 290#define UT_CONST const 291#else 292#define UT_CONST 293#endif 294 295extern unsigned long strtoul(const char *nptr, char **endptr, int base); 296extern UT_CONST char *strstr(const char *str1, const char *str2); 297extern UT_CONST char *strchr(const char *s, int c); 298extern char *strcpy(char* dest, const char *src); 299extern char* strsep(char** __s_ptr, const char* __delimiter); 300extern long strtol(const char *nptr, char **endptr, int base); 301 302extern int atoi (const char *s); 303 304extern void *memset(void *s, int ch, size_t n); 305extern void* memmove(void* dst, const void* src, size_t n); 306extern void* memcpy(void* dst, const void* src, size_t n); 307 308 309HI_END_HEADER 310#endif /* __HI_STDLIB_H__ */ 311 312