Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


integrated new configuration system
[palacios.git] / palacios / src / palacios / vmm_string.c
index 274f5be..effaf32 100644 (file)
  */
 
 
-#define NEED_MEMSET 0
-#define NEED_MEMCPY 0
-#define NEED_MEMCMP 0
-#define NEED_STRLEN 0
-#define NEED_STRNLEN 0
-#define NEED_STRCMP 0
-#define NEED_STRNCMP 0
-#define NEED_STRCAT 0
-#define NEED_STRNCAT 0
-#define NEED_STRCPY 0
-#define NEED_STRNCPY 0
-#define NEED_STRDUP 0
-#define NEED_ATOI 0
-#define NEED_STRCHR 0
-#define NEED_STRRCHR 0
-#define NEED_STRPBRK 0
-
-
-
 #include <palacios/vmm_types.h>
 #include <palacios/vmm_string.h>
 #include <palacios/vmm.h>
 
 
-#if NEED_MEMSET
+#ifdef CONFIG_BUILT_IN_MEMSET
 void * memset(void * s, int c, size_t n) {
     uchar_t * p = (uchar_t *) s;
 
@@ -73,7 +54,7 @@ void * memset(void * s, int c, size_t n) {
 }
 #endif
 
-#if NEED_MEMCPY
+#ifdef CONFIG_BUILT_IN_MEMCPY
 void * memcpy(void * dst, const void * src, size_t n) {
     uchar_t * d = (uchar_t *) dst;
     const uchar_t * s = (const uchar_t *)src;
@@ -87,8 +68,20 @@ void * memcpy(void * dst, const void * src, size_t n) {
 }
 #endif
 
+#ifdef CONFIG_BUILT_IN_MEMMOVE
+void * memmove(void * dst, const void * src, size_t n) {
+    uint8_t * tmp = (uint8_t *)V3_Malloc(n);
+    
+    memcpy(tmp, src, n);
+    memcpy(dst, tmp, n);
+    
+    V3_Free(tmp);
+    return dst;
+}
+#endif
+
 
-#if NEED_CMP
+#ifdef CONFIG_BUILT_IN_MEMCMP
 int memcmp(const void * s1_, const void * s2_, size_t n) {
     const char * s1 = s1_;
     const char * s2 = s2_;
@@ -109,7 +102,7 @@ int memcmp(const void * s1_, const void * s2_, size_t n) {
 #endif
 
 
-#if NEED_STRLEN
+#ifdef CONFIG_BUILT_IN_STRLEN
 size_t strlen(const char * s) {
     size_t len = 0;
 
@@ -123,7 +116,7 @@ size_t strlen(const char * s) {
 
 
 
-#if NEED_STRNLEN
+#ifdef CONFIG_BUILT_IN_STRNLEN
 /*
  * This it a GNU extension.
  * It is like strlen(), but it will check at most maxlen
@@ -144,7 +137,7 @@ size_t strnlen(const char * s, size_t maxlen) {
 #endif
 
 
-#if NEED_STRCMP
+#ifdef CONFIG_BUILT_IN_STRCMP
 int strcmp(const char * s1, const char * s2) {
     while (1) {
        int cmp = (*s1 - *s2);
@@ -159,8 +152,24 @@ int strcmp(const char * s1, const char * s2) {
 }
 #endif
 
+#ifdef CONFIG_BUILT_IN_STRCASECMP
+int strcasecmp(const char * s1, const char * s2) {
+    while (1) {
+       int cmp = (tolower(*s1) - tolower(*s2));
+
+       if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
+           return cmp;
+       }
+
+       ++s1;
+       ++s2;
+    }
+}
+
+#endif
+
 
-#if NEED_STRNCMP
+#ifdef CONFIG_BUILT_IN_STRNCMP
 int strncmp(const char * s1, const char * s2, size_t limit) {
     size_t i = 0;
 
@@ -181,8 +190,28 @@ int strncmp(const char * s1, const char * s2, size_t limit) {
 }
 #endif
 
+#ifdef CONFIG_BUILT_IN_STRNCASECMP
+int strncasecmp(const char * s1, const char * s2, size_t limit) {
+    size_t i = 0;
+
+    while (i < limit) {
+       int cmp = (tolower(*s1) - tolower(*s2));
 
-#if NEED_STRCAT
+       if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
+           return cmp;
+       }
+
+       ++s1;
+       ++s2;
+       ++i;
+    }
+
+    return 0;
+}
+#endif
+
+
+#ifdef CONFIG_BUILT_IN_STRCAT
 char * strcat(char * s1, const char * s2) {
     char * t1 = s1;
 
@@ -196,7 +225,7 @@ char * strcat(char * s1, const char * s2) {
 #endif
 
 
-#if NEED_STRNCAT
+#ifdef CONFIG_BUILT_IN_STRNCAT
 char * strncat(char * s1, const char * s2, size_t limit) {
     size_t i = 0;
     char * t1;
@@ -218,7 +247,7 @@ char * strncat(char * s1, const char * s2, size_t limit) {
 
 
 
-#if NEED_STRCPY
+#ifdef CONFIG_BUILT_IN_STRCPY
 char * strcpy(char * dest, const char * src)
 {
     char *ret = dest;
@@ -233,7 +262,7 @@ char * strcpy(char * dest, const char * src)
 #endif
 
 
-#if NEED_STRNCPY
+#ifdef CONFIG_BUILT_IN_STRNCPY
 char * strncpy(char * dest, const char * src, size_t limit) {
     char * ret = dest;
 
@@ -251,7 +280,7 @@ char * strncpy(char * dest, const char * src, size_t limit) {
 
 
 
-#if NEED_STRDUP
+#ifdef  CONFIG_BUILT_IN_STRDUP
 char * strdup(const char * s1) {
     char *ret;
 
@@ -265,7 +294,7 @@ char * strdup(const char * s1) {
 
 
 
-#if NEED_ATOI
+#ifdef CONFIG_BUILT_IN_ATOI
 int atoi(const char * buf) {
     int ret = 0;
 
@@ -280,7 +309,77 @@ int atoi(const char * buf) {
 #endif
 
 
-#if NEED_STRCHR
+int strtoi(const char * nptr, char ** endptr) {
+    int ret = 0;
+    char * buf = (char *)nptr;
+
+    while ((*buf >= '0') && (*buf <= '9')) {
+       ret *= 10;
+       ret += (*buf - '0');
+
+       buf++;
+
+       if (endptr) {
+           *endptr = buf;
+       }
+    }
+
+    return ret;
+}
+
+uint64_t atox(const char * buf) {
+    uint64_t ret = 0;
+
+    if (*(buf + 1) == 'x') {
+       buf += 2;
+    }
+
+    while (isxdigit(*buf)) {
+       ret <<= 4;
+       
+       if (isdigit(*buf)) {
+           ret += (*buf - '0');
+       } else {
+           ret += tolower(*buf) - 'a' + 10;
+       }
+
+       buf++;
+    }
+
+    return ret;
+}
+
+uint64_t strtox(const char * nptr, char ** endptr) {
+    uint64_t ret = 0;
+    char * buf = (char *)nptr;
+
+    if (*(buf + 1) == 'x') {
+       buf += 2;
+    }
+
+    while (isxdigit(*buf)) {
+       ret <<= 4;
+       
+       if (isdigit(*buf)) {
+           ret += (*buf - '0');
+       } else {
+           ret += tolower(*buf) - 'a' + 10;
+       }
+
+       buf++;
+
+       if (endptr) {
+           *endptr = buf;
+       }
+    }
+
+    return ret;
+
+}
+
+
+
+#ifdef CONFIG_BUILT_IN_STRCHR
 char * strchr(const char * s, int c) {
     while (*s != '\0') {
        if (*s == c)
@@ -292,7 +391,7 @@ char * strchr(const char * s, int c) {
 #endif
 
 
-#if NEED_STRRCHR
+#ifdef CONFIG_BUILT_IN_STRRCHR
 char * strrchr(const char * s, int c) {
     size_t len = strlen(s);
     const char * p = s + len;
@@ -308,7 +407,7 @@ char * strrchr(const char * s, int c) {
 }
 #endif
 
-#if NEED_STRPBRK
+#ifdef CONFIG_BUILT_IN_STRPBRK
 char * strpbrk(const char * s, const char * accept) {
     size_t setLen = strlen(accept);
 
@@ -326,3 +425,87 @@ char * strpbrk(const char * s, const char * accept) {
 }
 #endif
 
+#ifdef CONFIG_BUILT_IN_STRSPN
+size_t strspn(const char * s, const char * accept) {
+    int match = 1;
+    int cnt = 0;
+    int i = 0;
+    int accept_len = strlen(accept);
+
+    while (match) {
+       match = 0;
+
+       for (i = 0; i < accept_len; i++) {
+           if (s[cnt] == accept[i]) {
+               match = 1;
+               cnt++;
+               break;
+           }
+       }
+    }
+
+    return cnt;
+}
+#endif
+
+
+#ifdef CONFIG_BUILT_IN_STRCSPN
+size_t strcspn(const char * s, const char * reject) {
+    int match = 0;
+    int cnt = 0;
+    int i = 0;
+    int reject_len = strlen(reject);
+
+    while (!match) {
+       for (i = 0; i < reject_len; i++) {
+           if (s[cnt] == reject[i]) {
+               match = 1;
+               cnt++;
+               break;
+           }
+       }
+    }
+
+    return cnt;
+}
+#endif
+
+
+#ifdef CONFIG_BUILT_IN_STRSTR
+char *strstr(const char *haystack, const char *needle)
+{
+        int l1, l2;
+
+        l2 = strlen(s2);
+        if (!l2)
+                return (char *)s1;
+        l1 = strlen(s1);
+        while (l1 >= l2) {
+                l1--;
+                if (!memcmp(s1, s2, l2))
+                        return (char *)s1;
+                s1++;
+        }
+        return NULL;
+}
+#endif
+
+
+void str_tolower(char * s) {
+    while (isalpha(*s)) {
+       if (!islower(*s)) {
+           *s = tolower(*s);
+       }
+       s++;
+    }
+}
+
+
+void str_toupper(char * s) {
+    while (isalpha(*s)) {
+       if (!isupper(*s)) {
+           *s = toupper(*s);
+       }
+       s++;
+    }
+}