3 * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without restriction,
9 * including without limitation the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18 * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21 * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
22 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
25 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 /* Modifications by Jack Lange <jarusl@cs.northwestern.edu> */
33 * These are slow and simple implementations of a subset of
34 * the standard C library string functions.
35 * We also have an implementation of snprintf().
39 #include <palacios/vmm_types.h>
40 #include <palacios/vmm_string.h>
41 #include <palacios/vmm.h>
44 #ifdef V3_CONFIG_BUILT_IN_MEMSET
45 void * memset(void * s, int c, size_t n) {
46 uchar_t * p = (uchar_t *) s;
57 #ifdef V3_CONFIG_BUILT_IN_MEMCPY
58 void * memcpy(void * dst, const void * src, size_t n) {
59 uchar_t * d = (uchar_t *) dst;
60 const uchar_t * s = (const uchar_t *)src;
71 #ifdef V3_CONFIG_BUILT_IN_MEMMOVE
72 void * memmove(void * dst, const void * src, size_t n) {
73 uint8_t * tmp = (uint8_t *)V3_Malloc(n);
84 #ifdef V3_CONFIG_BUILT_IN_MEMCMP
85 int memcmp(const void * s1_, const void * s2_, size_t n) {
86 const char * s1 = s1_;
87 const char * s2 = s2_;
90 int cmp = (*s1 - *s2);
105 #ifdef V3_CONFIG_BUILT_IN_STRLEN
106 size_t strlen(const char * s) {
109 while (*s++ != '\0') {
119 #ifdef V3_CONFIG_BUILT_IN_STRNLEN
121 * This it a GNU extension.
122 * It is like strlen(), but it will check at most maxlen
123 * characters for the terminating nul character,
124 * returning maxlen if it doesn't find a nul.
125 * This is very useful for checking the length of untrusted
126 * strings (e.g., from user space).
128 size_t strnlen(const char * s, size_t maxlen) {
131 while ((len < maxlen) && (*s++ != '\0')) {
140 #ifdef V3_CONFIG_BUILT_IN_STRCMP
141 int strcmp(const char * s1, const char * s2) {
143 int cmp = (*s1 - *s2);
145 if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
155 #ifdef V3_CONFIG_BUILT_IN_STRCASECMP
156 int strcasecmp(const char * s1, const char * s2) {
158 int cmp = (tolower(*s1) - tolower(*s2));
160 if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
172 #ifdef V3_CONFIG_BUILT_IN_STRNCMP
173 int strncmp(const char * s1, const char * s2, size_t limit) {
177 int cmp = (*s1 - *s2);
179 if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
188 /* limit reached and equal */
193 #ifdef V3_CONFIG_BUILT_IN_STRNCASECMP
194 int strncasecmp(const char * s1, const char * s2, size_t limit) {
198 int cmp = (tolower(*s1) - tolower(*s2));
200 if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
214 #ifdef V3_CONFIG_BUILT_IN_STRCAT
215 char * strcat(char * s1, const char * s2) {
218 while (*s1) { s1++; }
219 while (*s2) { *s1++ = *s2++; }
228 #ifdef V3_CONFIG_BUILT_IN_STRNCAT
229 char * strncat(char * s1, const char * s2, size_t limit) {
235 while (*s1) { s1++; }
250 #ifdef V3_CONFIG_BUILT_IN_STRCPY
251 char * strcpy(char * dest, const char * src)
265 #ifdef V3_CONFIG_BUILT_IN_STRNCPY
266 char * strncpy(char * dest, const char * src, size_t limit) {
269 while ((*src != '\0') && (limit > 0)) {
283 #ifdef V3_CONFIG_BUILT_IN_STRDUP
284 char * strdup(const char * s1) {
287 ret = V3_Malloc(strlen(s1) + 1);
297 #ifdef V3_CONFIG_BUILT_IN_ATOI
298 int atoi(const char * buf) {
301 while ((*buf >= '0') && (*buf <= '9')) {
312 int strtoi(const char * nptr, char ** endptr) {
314 char * buf = (char *)nptr;
316 while ((*buf >= '0') && (*buf <= '9')) {
330 uint64_t atox(const char * buf) {
333 if (*(buf + 1) == 'x') {
337 while (isxdigit(*buf)) {
343 ret += tolower(*buf) - 'a' + 10;
352 uint64_t strtox(const char * nptr, char ** endptr) {
354 char * buf = (char *)nptr;
356 if (*(buf + 1) == 'x') {
360 while (isxdigit(*buf)) {
366 ret += tolower(*buf) - 'a' + 10;
382 #ifdef V3_CONFIG_BUILT_IN_STRCHR
383 char * strchr(const char * s, int c) {
394 #ifdef V3_CONFIG_BUILT_IN_STRRCHR
395 char * strrchr(const char * s, int c) {
396 size_t len = strlen(s);
397 const char * p = s + len;
410 #ifdef V3_CONFIG_BUILT_IN_STRPBRK
411 char * strpbrk(const char * s, const char * accept) {
412 size_t setLen = strlen(accept);
416 for (i = 0; i < setLen; ++i) {
417 if (*s == accept[i]) {
428 #ifdef V3_CONFIG_BUILT_IN_STRSPN
429 size_t strspn(const char * s, const char * accept) {
433 int accept_len = strlen(accept);
438 for (i = 0; i < accept_len; i++) {
439 if (s[cnt] == accept[i]) {
452 #ifdef V3_CONFIG_BUILT_IN_STRCSPN
453 size_t strcspn(const char * s, const char * reject) {
457 int reject_len = strlen(reject);
460 for (i = 0; i < reject_len; i++) {
461 if (s[cnt] == reject[i]) {
478 #ifdef V3_CONFIG_BUILT_IN_STRSTR
479 char *strstr(const char *haystack, const char *needle)
489 if (!memcmp(s1, s2, l2))
498 void str_tolower(char * s) {
499 while (isalpha(*s)) {
508 void str_toupper(char * s) {
509 while (isalpha(*s)) {