1 /* 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * Description: OS Abstract Layer. 15 */ 16 17 /** 18 * @defgroup osal_string osal_string 19 */ 20 #ifndef __OSAL_STRING_H__ 21 #define __OSAL_STRING_H__ 22 23 #ifdef __cplusplus 24 #if __cplusplus 25 extern "C" { 26 #endif 27 #endif 28 29 #define OSAL_BASE_DEC 10 30 #define OSAL_BASE_HEX 16 31 32 /** 33 * @ingroup osal_string 34 * @brief Compare memory areas. 35 * 36 * @par Description: 37 * This API is used to compare the size bytes of the memory areas buf1 and buf2. 38 * 39 * @param buf1 [in] the memory areas to be compared. 40 * @param buf2 [in] the memory areas to be compared. 41 * @param size [in] the size of the memory areas. 42 * 43 * @return Returns an integer less than, equal to, or greater than zero if the first n bytes of buf1 is found, 44 * respectively, to be less than, to match, or be greater than the first n bytes of buf2. 45 * 46 * @par Support System: 47 * linux liteos. 48 * 49 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 50 */ 51 int osal_memncmp(const void *buf1, const void *buf2, unsigned long size); 52 53 /** 54 * @ingroup osal_string 55 * @brief Compare two strings. 56 * 57 * @par Description: 58 * This API is used to compare two strings. 59 * 60 * @param s1 [in] the string to be compared. 61 * @param s2 [in] the string to be compared. 62 * 63 * @return Returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, 64 * to match, or be greater than s2. 65 * 66 * @par Support System: 67 * linux liteos freertos. 68 * 69 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 70 */ 71 int osal_strcmp(const char *s1, const char *s2); 72 73 /** 74 * @ingroup osal_string 75 * @brief Compare two strings. 76 * 77 * @par Description: 78 * This API is used to compare first size bytes of two strings. 79 * 80 * @param s1 [in] the string to be compared. 81 * @param s2 [in] the string to be compared. 82 * @param size [in] the size of the string. 83 * 84 * @return Returns an integer less than, equal to, or greater than zero if the first size bytes of s1 is found, 85 * respectively, to be less than, to match, or be greater than s2. 86 * 87 * @par Support System: 88 * linux liteos. 89 * 90 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 91 */ 92 int osal_strncmp(const char *s1, const char *s2, unsigned long size); 93 94 /** 95 * @ingroup osal_string 96 * @brief Compare two strings ignoring case. 97 * 98 * @par Description: 99 * This API is used to compare strings s1 and s2 byte-by-byte, ignoring the case of the characters. 100 * 101 * @param s1 [in] the string to be compared. 102 * @param s2 [in] the string to be compared. 103 * 104 * @return Returns an integer less than, equal to, or greater than zero if s1 is found, 105 * respectively, to be less than, to match, or be greater than s2. 106 * 107 * @par Support System: 108 * linux liteos freertos. 109 * 110 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 111 */ 112 int osal_strcasecmp(const char *s1, const char *s2); 113 114 /** 115 * @ingroup osal_string 116 * @brief Compare two strings ignoring case. 117 * 118 * @par Description: 119 * This API is used to compare first size bytes of strings s1 and s2 byte-by-byte, ignoring the case of the characters. 120 * 121 * @param s1 [in] the string to be compared. 122 * @param s2 [in] the string to be compared. 123 * @param size [in] the size of the string. 124 * 125 * @return Returns an integer less than, equal to, or greater than zero if the first size bytes of s1 is found, 126 * respectively, to be less than, to match, or be greater than s2. 127 * 128 * @par Support System: 129 * linux liteos. 130 * 131 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 132 */ 133 int osal_strncasecmp(const char *s1, const char *s2, unsigned long size); 134 135 /** 136 * @ingroup osal_string 137 * @brief Locate character in string. 138 * 139 * @par Description: 140 * This API is used to locate character in string. 141 * 142 * @param s [in] the string to be searched. 143 * @param n [in] the character to search for. 144 * 145 * @return Returns a pointer to the first occurrence of the character n in the string s. 146 * 147 * @par Support System: 148 * linux liteos. 149 * 150 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 151 */ 152 char *osal_strchr(const char *s, int n); 153 154 /** 155 * @ingroup osal_string 156 * @brief Find a character in a length limited string. 157 * 158 * @par Description: 159 * This API is used to find a character in a length limited string. 160 * 161 * @param s [in] the string to be searched. 162 * @param count [in] the number of characters to be searched. 163 * @param c [in] the character to search for. 164 * 165 * @return Returns a pointer to the first occurrence of the character c in the string s. 166 * 167 * @par Support System: 168 * linux liteos. 169 */ 170 char *osal_strnchr(const char *s, int count, int c); 171 172 /** 173 * @ingroup osal_string 174 * @brief Find the last occurrence of a character in a string. 175 * 176 * @par Description: 177 * This API is used to find a character in a length limited string. 178 * 179 * @param s [in] the string to be searched. 180 * @param c [in] the character to search for. 181 * 182 * @return Returns a pointer to the last occurrence of the character c in the string s. 183 * 184 * @par Support System: 185 * linux liteos. 186 * 187 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 188 */ 189 char *osal_strrchr(const char *s, int c); 190 191 /** 192 * @ingroup osal_string 193 * @brief Locate a substring. 194 * 195 * @par Description: 196 * This API is used to locate a substring. 197 * 198 * @param s1 [in] the string to be searched. 199 * @param s2 [in] the substring to search for. 200 * 201 * @return Returns a pointer to the first occurrence of the substring s2 in the string s1. 202 * 203 * @par Support System: 204 * linux liteos. 205 * 206 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 207 */ 208 char *osal_strstr(const char *s1, const char *s2); 209 210 /** 211 * @ingroup osal_string 212 * @brief Locate a substring in a length limited string. 213 * 214 * @par Description: 215 * This API is used to locate a substring in a length limited string. 216 * 217 * @param s1 [in] the string to be searched. 218 * @param s2 [in] the substring to search for. 219 * @param n [in] the length of string. 220 * 221 * @return Returns a pointer to the first occurrence of the substring s2 in the string s1. 222 * 223 * @par Support System: 224 * linux liteos. 225 */ 226 char *osal_strnstr(const char *s1, const char *s2, int n); 227 228 /** 229 * @ingroup osal_string 230 * @brief Calculate the length of a string. 231 * 232 * @par Description: 233 * This API is used to calculate the length of a string. 234 * 235 * @param s [in] the string to be calculated. 236 * 237 * @return Returns the number of characters in the string pointed to by s. 238 * 239 * @par Support System: 240 * linux liteos freertos. 241 * 242 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 243 */ 244 unsigned int osal_strlen(const char *s); 245 246 /** 247 * @ingroup osal_string 248 * @brief Determine the length of a fixed-size string. 249 * 250 * @par Description: 251 * This API is used to determine the length of a fixed-size string. 252 * 253 * @param s [in] the string to be calculated. 254 * @param size [in] the length of string. 255 * 256 * @return Returns the number of characters in the string pointed to by s, excluding the terminating null byte ('\0'), 257 * but at most size. In doing this, strnlen() looks only at the first size characters in the string pointed to by s 258 * and never beyond s+size. 259 * 260 * @par Support System: 261 * linux liteos. 262 * 263 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 264 */ 265 unsigned int osal_strnlen(const char *s, unsigned int size); 266 267 /** 268 * @ingroup osal_string 269 * @brief Search a string for any of a set of bytes. 270 * 271 * @par Description: 272 * This API is used to search a string for any of a set of bytes. 273 * 274 * @param s1 [in] The string to be searched. 275 * @param s2 [in] The characters to search for. 276 * 277 * @return Returns a pointer to the byte in s1 that matches one of the bytes in s2, or NULL if no such byte is found. 278 * 279 * @par Support System: 280 * linux liteos. 281 * 282 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 283 */ 284 char *osal_strpbrk(const char *s1, const char *s2); 285 286 /** 287 * @ingroup osal_string 288 * @brief Extract token from string. 289 * 290 * @par Description: 291 * This API is used to extract token from string. 292 * 293 * @param s [in/out] The string to be searched. 294 * @param ct [in] The characters to search for. 295 * 296 * @return Returns a pointer to the token, that is, it returns the original value of *s. 297 * 298 * @par Support System: 299 * linux liteos. 300 * 301 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 302 */ 303 char *osal_strsep(char **s, const char *ct); 304 305 /** 306 * @ingroup osal_string 307 * @brief Get length of a prefix substring. 308 * 309 * @par Description: 310 * This API is used to calculate the length of the initial substring of s which only contain letters in accept. 311 * 312 * @param s [in] The string to be searched. 313 * @param accept [in] The characters to search for. 314 * 315 * @return Returns the number of bytes in the initial segment of s which consist only of bytes from accept. 316 * 317 * @par Support System: 318 * linux liteos. 319 * 320 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 321 */ 322 unsigned int osal_strspn(const char *s, const char *accept); 323 324 /** 325 * @ingroup osal_string 326 * @brief Get length of a prefix substring. 327 * 328 * @par Description: 329 * This API is used to calculate the length of the initial substring of s which does not contain letters in reject. 330 * 331 * @param s [in] The string to be searched. 332 * @param reject [in] The string to avoid. 333 * 334 * @return Returns the number of bytes in the initial segment of s which are not in the string reject. 335 * 336 * @par Support System: 337 * linux liteos. 338 * 339 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 340 */ 341 unsigned int osal_strcspn(const char *s, const char *reject); 342 343 /** 344 * @ingroup osal_string 345 * @brief Find a character in an area of memory. 346 * 347 * @par Description: 348 * This API is used to find a character in an area of memory. 349 * 350 * @param addr [in] The memory area. 351 * @param c [in] The byte to search for. 352 * @param size [in] The size of the area. 353 * 354 * @return Returns the address of the first occurrence of c, or 1 byte past the area if c is not found. 355 * 356 * @par Support System: 357 * linux liteos. 358 */ 359 void *osal_memscan(void *addr, int c, int size); 360 361 /** 362 * @ingroup osal_string 363 * @brief Compare two areas of memory. 364 * 365 * @par Description: 366 * This API is used to compare two areas of memory. 367 * 368 * @param cs [in] One area of memory. 369 * @param ct [in] Another area of memory. 370 * @param count [in] The size of the area. 371 * 372 * @return Returns an integer less than, equal to, or greater than zero if the first count bytes of cs is found, 373 * respectively, to be less than, to match, or be greater than the first count bytes of ct. 374 * 375 * @par Support System: 376 * linux liteos freertos. 377 * 378 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 379 */ 380 int osal_memcmp(const void *cs, const void *ct, int count); 381 382 /** 383 * @ingroup osal_string 384 * @brief Find a character in an area of memory. 385 * 386 * @par Description: 387 * This API is used to find a character in an area of memory. 388 * 389 * @param s [in] The memory area. 390 * @param c [in] The byte to search for. 391 * @param n [in] The size of the area. 392 * 393 * @return Returns the address of the first occurrence of c, or NULL if c is not found. 394 * 395 * @par Support System: 396 * linux liteos. 397 * 398 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 399 */ 400 void *osal_memchr(const void *s, int c, int n); 401 402 /** 403 * @ingroup osal_string 404 * @brief Find an unmatching character in an area of memory. 405 * 406 * @par Description: 407 * This API is used to find an unmatching character in an area of memory. 408 * 409 * @param s [in] The memory area. 410 * @param c [in] Find a character other than c. 411 * @param n [in] The size of the area. 412 * 413 * @return Returns the address of the first character other than c, or NULL if the whole buffer contains just c. 414 * 415 * @par Support System: 416 * linux liteos. 417 */ 418 void *osal_memchr_inv(const void *s, int c, int n); 419 420 /** 421 * @ingroup osal_string 422 * @brief Convert a string to an unsigned long long integer. 423 * 424 * @par Description: 425 * The strtoull() function converts the initial part of the string in cp to an unsigned long long int value according to 426 * the given base, which must be between 2 and 36 inclusive, or be the special value 0. 427 * The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single 428 * optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be 429 * read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case 430 * it is taken as 8 (octal). 431 * The remainder of the string is converted to an unsigned long long int value in the obvious manner, stopping 432 * at the first character which is not a valid digit in the given base. 433 * (In bases above 10, the letter 'A' in either uppercase or lowercase represents 10, 'B' represents 11, and so forth, 434 * with 'Z' representing 35.) 435 * If endp is not NULL, strtoull() stores the address of the first invalid character in *endp. 436 * If there were no digits at all, strtoull() stores the original value of cp in *endp (and returns 0). 437 * In particular, if *cp is not '\0' but **endp is '\0' on return, the entire string is valid. 438 * 439 * @param cp [in] The string to be converted. 440 * @param endp [in/out] Stores the result. 441 * @param base [in] The base of conversion. 442 * 443 * @return Returns either the result of the conversion or, if there was a leading minus sign, the negation of the result 444 * of the conversion represented as an unsigned value, unless the original (nonnegated) value would overflow; in 445 * the latter case, strtoull() returns ULLONG_MAX and sets errno to ERANGE. 446 * 447 * @par Support System: 448 * linux. 449 */ 450 unsigned long long osal_strtoull(const char *cp, char **endp, unsigned int base); 451 452 /** 453 * @ingroup osal_string 454 * @brief Convert a string to an unsigned long integer. 455 * 456 * @par Description: 457 * The strtoul() function converts the initial part of the string in cp to an unsigned long int value according to 458 * the given base, which must be between 2 and 36 inclusive, or be the special value 0. 459 * The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single 460 * optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be 461 * read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case 462 * it is taken as 8 (octal). 463 * The remainder of the string is converted to an unsigned long int value in the obvious manner, stopping 464 * at the first character which is not a valid digit in the given base. 465 * (In bases above 10, the letter 'A' in either uppercase or lowercase represents 10, 'B' represents 11, and so forth, 466 * with 'Z' representing 35.) 467 * If endp is not NULL, osal_strtoul() stores the address of the first invalid character in *endp. 468 * If there were no digits at all, osal_strtoul() stores the original value of cp in *endp (and returns 0). 469 * In particular, if *cp is not '\0' but **endp is '\0' on return, the entire string is valid. 470 * 471 * @param cp [in] The string to be converted. 472 * @param endp [in/out] Stores the result. 473 * @param base [in] The base of conversion. 474 * 475 * @return Returns either the result of the conversion or, if there was a leading minus sign, the negation of the result 476 * of the conversion represented as an unsigned value, unless the original (nonnegated) value would overflow; in 477 * the latter case, osal_strtoul() returns ULONG_MAX and sets errno to ERANGE. 478 * 479 * @par Support System: 480 * linux liteos. 481 * 482 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 483 */ 484 unsigned long osal_strtoul(const char *cp, char **endp, unsigned int base); 485 486 /** 487 * @ingroup osal_string 488 * @brief Convert a string to a long integer. 489 * 490 * @par Description: 491 * The strtoll() function converts the initial part of the string in cp to a long int value according to 492 * the given base, which must be between 2 and 36 inclusive, or be the special value 0. 493 * The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single 494 * optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be 495 * read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case 496 * it is taken as 8 (octal). 497 * The remainder of the string is converted to a long int value in the obvious manner, stopping at the first 498 * character which is not a valid digit in the given base. 499 * (In bases above 10, the letter 'A' in either uppercase or lowercase represents 10, 'B' represents 11, and so forth, 500 * with 'Z' representing 35.) 501 * If endp is not NULL, strtol() stores the address of the first invalid character in *endp. 502 * If there were no digits at all, strtol() stores the original value of cp in *endp (and returns 0). 503 * In particular, if *cp is not '\0' but **endp is '\0' on return, the entire string is valid. 504 * 505 * @param cp [in] The string to be converted. 506 * @param endp [in/out] Stores the result. 507 * @param base [in] The base of conversion. 508 * 509 * @return Returns either the result of the conversion or, if there was a leading minus sign, the negation of the result 510 * of the conversion represented as an unsigned value, unless the original (nonnegated) value would overflow; in 511 * the latter case, strtol() returns LONG_MAX and sets errno to ERANGE. 512 * 513 * @par Support System: 514 * linux liteos. 515 * 516 * @attention the implementation is in libc, the parameters should be valid as calling libc interface. 517 */ 518 long osal_strtol(const char *cp, char **endp, unsigned int base); 519 520 /** 521 * @ingroup osal_string 522 * @brief Convert a string to a long long integer. 523 * 524 * @par Description: 525 * The strtoll() function converts the initial part of the string in cp to a long long int value according to 526 * the given base, which must be between 2 and 36 inclusive, or be the special value 0. 527 * The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single 528 * optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be 529 * read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case 530 * it is taken as 8 (octal). 531 * The remainder of the string is converted to a long long int value in the obvious manner, stopping at the first 532 * character which is not a valid digit in the given base. 533 * (In bases above 10, the letter 'A' in either uppercase or lowercase represents 10, 'B' represents 11, and so forth, 534 * with 'Z' representing 35.) 535 * If endp is not NULL, strtoll() stores the address of the first invalid character in *endp. 536 * If there were no digits at all, strtoll() stores the original value of cp in *endp (and returns 0). 537 * In particular, if *cp is not '\0' but **endp is '\0' on return, the entire string is valid. 538 * 539 * @param cp [in] The string to be converted. 540 * @param endp [in/out] Stores the result. 541 * @param base [in] The base of conversion. 542 * 543 * @return Returns either the result of the conversion or, if there was a leading minus sign, the negation of the result 544 * of the conversion represented as an unsigned value, unless the original (nonnegated) value would overflow; in 545 * the latter case, strtoll() returns LLONG_MAX and sets errno to ERANGE. 546 * 547 * @par Support System: 548 * linux. 549 */ 550 long long osal_strtoll(const char *cp, char **endp, unsigned int base); 551 552 #ifdef __cplusplus 553 #if __cplusplus 554 } 555 #endif 556 #endif 557 #endif /* __OSAL_STRING_H__ */ 558