Files
acrn-hypervisor/hypervisor/lib/string.c
Yifan Liu 390db422d8 hv: lib: Add several missing standard string/memory APIs
Integrating libfdt requires the presence of these APIs.

Tracked-On: #8838
Signed-off-by: Yifan Liu <yifan1.liu@intel.com>
Acked-by: Wang Yu1 <yu1.wang@intel.com>
2025-11-04 10:59:30 +08:00

366 lines
6.7 KiB
C

/*
* Copyright (C) 2018-2022 Intel Corporation.
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <types.h>
#include <rtl.h>
#include <logmsg.h>
uint64_t strtoul(const char * nptr, char **endptr, int base)
{
const char *s;
unsigned long acc, cutoff;
int c;
int neg, any, cutlim;
/*
* See strtol for comments as to the logic used.
*/
s = nptr;
do {
c = (unsigned char) *s++;
} while (is_space(c));
if (c == '-') {
neg = 1;
c = *s++;
} else {
neg = 0;
if (c == '+')
c = *s++;
}
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0) {
base = c == '0' ? 8 : 10;
}
cutoff = ULONG_MAX / (unsigned long)base;
cutlim = ULONG_MAX % (unsigned long)base;
for (acc = 0, any = 0;; c = (unsigned char) *s++) {
if (c >= '0' && c <= '9') {
c -= '0';
} else if (c >= 'A' && c <= 'Z') {
c -= 'A' - 10;
} else if (c >= 'a' && c <= 'z') {
c -= 'a' - 10;
} else {
break;
}
if (c >= base) {
break;
}
if (any < 0) {
continue;
}
if (acc > cutoff || (acc == cutoff && c > cutlim)) {
any = -1;
acc = ULONG_MAX;
} else {
any = 1;
acc *= (unsigned long)base;
acc += c;
}
}
if (neg && any > 0) {
acc = -acc;
}
if (endptr != 0) {
*endptr = (char *) (any ? s - 1 : nptr);
}
return (acc);
}
/*
* Convert a string to an uint64_t integer - hexadecimal support only.
*/
uint64_t strtoul_hex(const char *nptr)
{
return strtoul(nptr, NULL, 16);
}
char *strchr(const char *s_arg, char ch)
{
const char *s = s_arg;
while ((*s != '\0') && (*s != ch)) {
++s;
}
return ((*s) != '\0') ? (char *)s : NULL;
}
size_t strlen(const char *str)
{
unsigned long ret;
for (ret = 0; *str != '\0'; str++) {
ret++;
}
return ret;
}
size_t strnlen(const char *str, size_t count)
{
unsigned long ret;
for (ret = 0; (*str != '\0') && (ret < count); str++) {
ret++;
}
return ret;
}
char *strrchr(const char *s, char ch)
{
const char *last = s + strlen(s);
while (last > s && *last != ch) {
last--;
}
return (*last != ch) ? NULL : (char *)last;
}
char *strcpy(char *d, const char *s)
{
char *ret = d;
while ((*d++ = *s++) != '\0') { }
return ret;
}
/*
* strncpy_s
*
* description:
* This function copies maximum 'slen'characters from string pointed
* by s to a buffer pointed by d.
*
* input:
* d pointer to dest buffer.
*
* dmax maximum length of dest buffer.
*
* s pointer to the source string.
*
* slen the maximum number of characters to copy from source
* string.
*
* return value:
* 0 if source string is copied successfully;
* -1 if there is a runtime-constraint violation.
*
* notes:
* 1) dmax shall not be 0.
* 2) both d and s shall not be null pointers.
* 3) Copying shall not take place between objects that overlap.
* 4) If slen is not less than dmax, then dmax shall be more than strnlen_s(s, dmax).
* 5) d[0] shall be set to '\0' if there is a runtime-constraint violation.
*/
int32_t strncpy_s(char *d, size_t dmax, const char *s, size_t slen)
{
char *dest = d;
int32_t ret = -1;
size_t len = strnlen_s(s, dmax);
if ((slen < dmax) || (dmax > len)) {
ret = memcpy_s(d, dmax, s, len);
}
if (ret == 0) {
*(dest + len) = '\0';
} else {
if ((d != NULL) && (dmax > 0U)) {
*dest = '\0';
}
}
return ret;
}
/**
*
* strnlen_s
*
* description:
* The function calculates the length of the string pointed
* to by str.
*
*
* input:
* str pointer to the null-terminated string to be examined.
*
* dmax maximum number of characer to examine.
*
* return value:
* string length, excluding the null character.
* will return 0 if str is null.
*/
size_t strnlen_s(const char *str_arg, size_t maxlen_arg)
{
const char *str = str_arg;
size_t count = 0U;
if (str != NULL) {
size_t maxlen = maxlen_arg;
while ((*str) != '\0') {
if (maxlen == 0U) {
break;
}
count++;
maxlen--;
str++;
}
}
return count;
}
int32_t strcmp(const char *s1_arg, const char *s2_arg)
{
const char *str1 = s1_arg;
const char *str2 = s2_arg;
while (((*str1) != '\0') && ((*str2) != '\0') && ((*str1) == (*str2))) {
str1++;
str2++;
}
return *str1 - *str2;
}
/**
* @pre n_arg > 0
*/
int32_t strncmp(const char *s1_arg, const char *s2_arg, size_t n_arg)
{
const char *str1 = s1_arg;
const char *str2 = s2_arg;
size_t n = n_arg;
int32_t ret = 0;
if (n > 0U) {
while (((n - 1) != 0U) && ((*str1) != '\0') && ((*str2) != '\0') && ((*str1) == (*str2))) {
str1++;
str2++;
n--;
}
ret = (int32_t) (*str1 - *str2);
}
return ret;
}
/*
* strstr_s
*
* description:
* Search str2 in str1
*
* input:
* str1 pointer to string to be searched for the substring.
*
* maxlen1 maximum length of str1.
*
* str2 pointer to the sub-string.
*
* maxlen2 maximum length of str2.
*
* return value:
* Pointer to the first occurrence of str2 in str1,
* or return null if not found.
*/
char *strstr_s(const char *str1, size_t maxlen1, const char *str2, size_t maxlen2)
{
size_t len1, len2;
size_t i;
const char *pstr, *pret;
if ((str1 == NULL) || (str2 == NULL)) {
pret = NULL;
} else if ((maxlen1 == 0U) || (maxlen2 == 0U)) {
pret = NULL;
} else {
len1 = strnlen_s(str1, maxlen1);
len2 = strnlen_s(str2, maxlen2);
if (len1 < len2) {
pret = NULL;
} else if ((str1 == str2) || (len2 == 0U)) {
/* return str1 if str2 equals to str1 or str2 points to a string with zero length*/
pret = str1;
} else {
pret = NULL;
pstr = str1;
while (len1 >= len2) {
for (i = 0U; i < len2; i++) {
if (pstr[i] != str2[i]) {
break;
}
}
if (i == len2) {
pret = pstr;
break;
}
pstr++;
len1--;
}
}
}
return (char *)pret;
}
/*
* strncat_s
*
* description:
* append src string to the end of dest string
*
* input:
* dest pointer to the string to be appended.
*
* dmax maximum length of dest buffer including the NULL char.
*
* src pointer to the string that to be concatenated to string dest.
*
* slen maximum characters to append.
*
* return value:
* 0 for success, -1 for failure.
*/
int32_t strncat_s(char *dest, size_t dmax, const char *src, size_t slen)
{
int32_t ret = -1;
size_t len_d, len_s;
char *d = dest, *start;
len_d = strnlen_s(dest, dmax);
len_s = strnlen_s(src, slen);
start = dest + len_d;
if ((dest != NULL) && (src != NULL) && (dmax > (len_d + len_s))
&& ((dest > (src + len_s)) || (src > (dest + len_d)))) {
(void)memcpy_s(start, (dmax - len_d), src, len_s);
*(start + len_s) = '\0';
ret = 0;
} else {
if (dest != NULL) {
*d = '\0'; /* set dest[0] to NULL char on runtime-constraint violation */
}
}
return ret;
}