1 /*
2  * Copyright (C) 2018-2022 Intel Corporation.
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include <types.h>
6 #include <rtl.h>
7 #include <logmsg.h>
8 
hex_digit_value(char ch)9 static inline char hex_digit_value(char ch)
10 {
11 	char c;
12 	if (('0' <= ch) && (ch <= '9')) {
13 		c = ch - '0';
14 	} else if (('a' <= ch) && (ch <= 'f')) {
15 		c = ch - 'a' + 10;
16 	} else if (('A' <= ch) && (ch <= 'F')) {
17 		c = ch - 'A' + 10;
18 	} else {
19 		c = -1;
20 	}
21 	return c;
22 }
23 
24 /*
25  * Convert a string to an uint64_t integer - hexadecimal support only.
26  */
strtoul_hex(const char * nptr)27 uint64_t strtoul_hex(const char *nptr)
28 {
29 	const char *s = nptr;
30 	char c, digit;
31 	uint64_t acc, cutoff, cutlim;
32 	uint64_t base = 16UL;
33 	int32_t any;
34 
35 	/*
36 	 * See strtol for comments as to the logic used.
37 	 */
38 	do {
39 		c = *s;
40 		s++;
41 	} while (is_space(c));
42 
43 	if ((c == '0') && ((*s == 'x') || (*s == 'X'))) {
44 		c = s[1];
45 		s += 2;
46 	}
47 
48 	cutoff = ULONG_MAX / base;
49 	cutlim = ULONG_MAX % base;
50 	acc = 0UL;
51 	any = 0;
52 	digit = hex_digit_value(c);
53 	while (digit >= 0) {
54 		if ((acc > cutoff) || ((acc == cutoff) && ((uint64_t)digit > cutlim))) {
55 			any = -1;
56 			break;
57 		} else {
58 			acc *= base;
59 			acc += (uint64_t)digit;
60 		}
61 
62 		c = *s;
63 		s++;
64 		digit = hex_digit_value(c);
65 	}
66 
67 	if (any < 0) {
68 		acc = ULONG_MAX;
69 	}
70 	return acc;
71 }
72 
strchr(char * s_arg,char ch)73 char *strchr(char *s_arg, char ch)
74 {
75 	char *s = s_arg;
76 	while ((*s != '\0') && (*s != ch)) {
77 		++s;
78 	}
79 
80 	return ((*s) != '\0') ? s : NULL;
81 }
82 
83 /*
84  * strncpy_s
85  *
86  * description:
87  *    This function copies maximum 'slen'characters from string pointed
88  *    by s to a buffer pointed by d.
89  *
90  * input:
91  *    d         pointer to dest buffer.
92  *
93  *    dmax      maximum length of dest buffer.
94  *
95  *    s         pointer to the source string.
96  *
97  *    slen      the maximum number of characters to copy from source
98  *              string.
99  *
100  * return value:
101  *    0		if source string is copied successfully;
102  *    -1	if there is a runtime-constraint violation.
103  *
104  * notes:
105  *    1) dmax shall not be 0.
106  *    2) both d and s shall not be null pointers.
107  *    3) Copying shall not take place between objects that overlap.
108  *    4) If slen is not less than dmax, then dmax shall be more than strnlen_s(s, dmax).
109  *    5) d[0] shall be set to '\0' if there is a runtime-constraint violation.
110  */
strncpy_s(char * d,size_t dmax,const char * s,size_t slen)111 int32_t strncpy_s(char *d, size_t dmax, const char *s, size_t slen)
112 {
113 	char *dest = d;
114 	int32_t ret = -1;
115 	size_t len = strnlen_s(s, dmax);
116 
117 	if ((slen < dmax) || (dmax > len)) {
118 		ret = memcpy_s(d, dmax, s, len);
119 	}
120 
121 	if (ret == 0) {
122 		*(dest + len) = '\0';
123 	} else {
124 		if ((d != NULL) && (dmax > 0U)) {
125 			*dest = '\0';
126 		}
127 	}
128 
129 	return ret;
130 }
131 
132 /**
133  *
134  *    strnlen_s
135  *
136  * description:
137  *    The function calculates the length of the string pointed
138  *    to by str.
139  *
140  *
141  * input:
142  *    str      pointer to the null-terminated string to be examined.
143  *
144  *    dmax      maximum number of characer to examine.
145  *
146  * return value:
147  *    string length, excluding the null character.
148  *    will return 0 if str is null.
149  */
strnlen_s(const char * str_arg,size_t maxlen_arg)150 size_t strnlen_s(const char *str_arg, size_t maxlen_arg)
151 {
152 	const char *str = str_arg;
153 	size_t count = 0U;
154 
155 	if (str != NULL) {
156 		size_t maxlen = maxlen_arg;
157 		while ((*str) != '\0') {
158 			if (maxlen == 0U) {
159 				break;
160 			}
161 
162 			count++;
163 			maxlen--;
164 			str++;
165 		}
166 	}
167 
168 	return count;
169 }
170 
strcmp(const char * s1_arg,const char * s2_arg)171 int32_t strcmp(const char *s1_arg, const char *s2_arg)
172 {
173 	const char *str1 = s1_arg;
174 	const char *str2 = s2_arg;
175 
176 	while (((*str1) != '\0') && ((*str2) != '\0') && ((*str1) == (*str2))) {
177 		str1++;
178 		str2++;
179 	}
180 
181 	return *str1 - *str2;
182 }
183 
184 /**
185  * @pre n_arg > 0
186  */
strncmp(const char * s1_arg,const char * s2_arg,size_t n_arg)187 int32_t strncmp(const char *s1_arg, const char *s2_arg, size_t n_arg)
188 {
189 	const char *str1 = s1_arg;
190 	const char *str2 = s2_arg;
191 	size_t n = n_arg;
192 	int32_t ret = 0;
193 
194 	if (n > 0U) {
195 		while (((n - 1) != 0U) && ((*str1) != '\0') && ((*str2) != '\0') && ((*str1) == (*str2))) {
196 			str1++;
197 			str2++;
198 			n--;
199 		}
200 		ret = (int32_t) (*str1 - *str2);
201 	}
202 
203 	return ret;
204 }
205 
206 /*
207  * strstr_s
208  *
209  * description:
210  *    Search str2 in str1
211  *
212  * input:
213  *    str1      pointer to string to be searched for the substring.
214  *
215  *    maxlen1   maximum length of str1.
216  *
217  *    str2      pointer to the sub-string.
218  *
219  *    maxlen2   maximum length of str2.
220  *
221  * return value:
222  *     Pointer to the first occurrence of str2 in str1,
223  *     or return null if not found.
224  */
strstr_s(const char * str1,size_t maxlen1,const char * str2,size_t maxlen2)225 char *strstr_s(const char *str1, size_t maxlen1, const char *str2, size_t maxlen2)
226 {
227 	size_t len1, len2;
228 	size_t i;
229 	const char *pstr, *pret;
230 
231 	if ((str1 == NULL) || (str2 == NULL)) {
232 		pret = NULL;
233 	} else if ((maxlen1 == 0U) || (maxlen2 == 0U)) {
234 		pret = NULL;
235 	} else {
236 		len1 = strnlen_s(str1, maxlen1);
237 		len2 = strnlen_s(str2, maxlen2);
238 
239 		if (len1 < len2) {
240 			pret = NULL;
241 		} else if ((str1 == str2) || (len2 == 0U)) {
242 			/* return str1 if str2 equals to str1 or str2 points to a string with zero length*/
243 			pret = str1;
244 		} else {
245 			pret = NULL;
246 			pstr = str1;
247 			while (len1 >= len2) {
248 				for (i = 0U; i < len2; i++) {
249 					if (pstr[i] != str2[i]) {
250 						break;
251 					}
252 				}
253 				if (i == len2) {
254 					pret = pstr;
255 					break;
256 				}
257 				pstr++;
258 				len1--;
259 			}
260 		}
261 	}
262 
263 	return (char *)pret;
264 }
265 
266 /*
267  * strncat_s
268  *
269  * description:
270  *    append src string to the end of dest string
271  *
272  * input:
273  *    dest      pointer to the string to be appended.
274  *
275  *    dmax      maximum length of dest buffer including the NULL char.
276  *
277  *    src       pointer to the string that to be concatenated to string dest.
278  *
279  *    slen      maximum characters to append.
280  *
281  * return value:
282  *     0 for success, -1 for failure.
283  */
strncat_s(char * dest,size_t dmax,const char * src,size_t slen)284 int32_t strncat_s(char *dest, size_t dmax, const char *src, size_t slen)
285 {
286 	int32_t ret = -1;
287 	size_t len_d, len_s;
288 	char *d = dest, *start;
289 
290 	len_d = strnlen_s(dest, dmax);
291 	len_s = strnlen_s(src, slen);
292 	start = dest + len_d;
293 
294 	if ((dest != NULL) && (src != NULL) && (dmax > (len_d + len_s))
295 			&& ((dest > (src + len_s)) || (src > (dest + len_d)))) {
296 		(void)memcpy_s(start, (dmax - len_d), src, len_s);
297 		*(start + len_s) = '\0';
298 		ret = 0;
299 	} else {
300 		if (dest != NULL) {
301 			*d = '\0';	/* set dest[0] to NULL char on runtime-constraint violation */
302 		}
303 	}
304 	return ret;
305 }
306