1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include "hitls_build.h"
17 #ifdef HITLS_BSL_SAL_STR
18
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include "securec.h"
23
24 #include "bsl_errno.h"
25
BSL_SAL_StrcaseCmp(const char * str1,const char * str2)26 int32_t BSL_SAL_StrcaseCmp(const char *str1, const char *str2)
27 {
28 if (str1 == NULL || str2 == NULL) {
29 return BSL_NULL_INPUT;
30 }
31 const char *tmpStr1 = str1;
32 const char *tmpStr2 = str2;
33 uint8_t t1 = 0;
34 uint8_t t2 = 0;
35 uint8_t sub = 'a' - 'A';
36
37 for (; (*tmpStr1 != '\0') && (*tmpStr2 != '\0'); tmpStr1++, tmpStr2++) {
38 t1 = (uint8_t)*tmpStr1;
39 t2 = (uint8_t)*tmpStr2;
40 if (t1 >= 'A' && t1 <= 'Z') {
41 t1 = t1 + sub;
42 }
43 if (t2 >= 'A' && t2 <= 'Z') {
44 t2 = t2 + sub;
45 }
46 if (t1 != t2) {
47 break;
48 }
49 }
50 return (int32_t)(*tmpStr1) - (int32_t)(*tmpStr2);
51 }
52
BSL_SAL_Memchr(const char * str,int32_t character,size_t count)53 void *BSL_SAL_Memchr(const char *str, int32_t character, size_t count)
54 {
55 if (str == NULL) {
56 return NULL;
57 }
58
59 for (size_t i = 0; i < count; i++) {
60 if ((int32_t)str[i] == character) {
61 return (void *)(uintptr_t)(str + i);
62 }
63 }
64 return NULL;
65 }
66
BSL_SAL_Atoi(const char * str)67 int32_t BSL_SAL_Atoi(const char *str)
68 {
69 int val = 0;
70 if (str == NULL) {
71 return 0;
72 }
73 if (sscanf_s(str, "%d", &val) != -1) {
74 return (int32_t)val;
75 }
76 return 0;
77 }
78
BSL_SAL_Strnlen(const char * string,uint32_t count)79 uint32_t BSL_SAL_Strnlen(const char *string, uint32_t count)
80 {
81 uint32_t n;
82 const char *pscTemp = string;
83 if (pscTemp == NULL) {
84 return 0;
85 }
86
87 for (n = 0; (n < count) && (*pscTemp != '\0'); n++) {
88 pscTemp++;
89 }
90
91 return n;
92 }
93 #endif
94