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.


Cleanup of linkage issues for non-Linux hosts
[palacios.git] / palacios / src / palacios / vmm_string.c
1 /*
2  * String library 
3  * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4  * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
26  */
27 /* Modifications by Jack Lange <jarusl@cs.northwestern.edu> */
28
29
30
31 /*
32  * NOTE:
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().
36  */
37
38
39 #include <palacios/vmm_types.h>
40 #include <palacios/vmm_string.h>
41 #include <palacios/vmm.h>
42
43
44 #ifdef V3_CONFIG_BUILT_IN_MEMSET
45 void * memset(void * s, int c, size_t n) {
46     uchar_t * p = (uchar_t *) s;
47
48     while (n > 0) {
49         *p++ = (uchar_t) c;
50         --n;
51     }
52
53     return s;
54 }
55 #endif
56
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;
61
62     while (n > 0) {
63         *d++ = *s++;
64         --n;
65     }
66
67     return dst;
68 }
69 #endif
70
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);
74
75     if (!tmp) {
76         PrintError(info->vm_info, info, "Cannot allocate in built-in memmove\n");
77         return NULL;
78     }
79     
80     memcpy(tmp, src, n);
81     memcpy(dst, tmp, n);
82     
83     V3_Free(tmp);
84     return dst;
85 }
86 #endif
87
88
89 #ifdef V3_CONFIG_BUILT_IN_MEMCMP
90 int memcmp(const void * s1_, const void * s2_, size_t n) {
91     const char * s1 = s1_;
92     const char * s2 = s2_;
93
94     while (n > 0) {
95         int cmp = (*s1 - *s2);
96         
97         if (cmp != 0) {
98             return cmp;
99         }
100
101         ++s1;
102         ++s2;
103     }
104
105     return 0;
106 }
107 #endif
108
109
110 #ifdef V3_CONFIG_BUILT_IN_STRLEN
111 size_t strlen(const char * s) {
112     size_t len = 0;
113
114     while (*s++ != '\0') {
115         ++len;
116     }
117
118     return len;
119 }
120 #endif
121
122
123
124 #ifdef V3_CONFIG_BUILT_IN_STRNLEN
125 /*
126  * This it a GNU extension.
127  * It is like strlen(), but it will check at most maxlen
128  * characters for the terminating nul character,
129  * returning maxlen if it doesn't find a nul.
130  * This is very useful for checking the length of untrusted
131  * strings (e.g., from user space).
132  */
133 size_t strnlen(const char * s, size_t maxlen) {
134     size_t len = 0;
135
136     while ((len < maxlen) && (*s++ != '\0')) {
137         ++len;
138     }
139
140     return len;
141 }
142 #endif
143
144
145 #ifdef V3_CONFIG_BUILT_IN_STRCMP
146 int strcmp(const char * s1, const char * s2) {
147     while (1) {
148         int cmp = (*s1 - *s2);
149         
150         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
151             return cmp;
152         }
153         
154         ++s1;
155         ++s2;
156     }
157 }
158 #endif
159
160 #ifdef V3_CONFIG_BUILT_IN_STRCASECMP
161 int strcasecmp(const char * s1, const char * s2) {
162     while (1) {
163         int cmp = (tolower(*s1) - tolower(*s2));
164
165         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
166             return cmp;
167         }
168
169         ++s1;
170         ++s2;
171     }
172 }
173
174 #endif
175
176
177 #ifdef V3_CONFIG_BUILT_IN_STRNCMP
178 int strncmp(const char * s1, const char * s2, size_t limit) {
179     size_t i = 0;
180
181     while (i < limit) {
182         int cmp = (*s1 - *s2);
183
184         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
185             return cmp;
186         }
187
188         ++s1;
189         ++s2;
190         ++i;
191     }
192
193     /* limit reached and equal */
194     return 0;
195 }
196 #endif
197
198 #ifdef V3_CONFIG_BUILT_IN_STRNCASECMP
199 int strncasecmp(const char * s1, const char * s2, size_t limit) {
200     size_t i = 0;
201
202     while (i < limit) {
203         int cmp = (tolower(*s1) - tolower(*s2));
204
205         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
206             return cmp;
207         }
208
209         ++s1;
210         ++s2;
211         ++i;
212     }
213
214     return 0;
215 }
216 #endif
217
218
219 #ifdef V3_CONFIG_BUILT_IN_STRCAT
220 char * strcat(char * s1, const char * s2) {
221     char * t1 = s1;
222
223     while (*s1) { s1++; }
224     while (*s2) { *s1++ = *s2++; }
225
226     *s1 = '\0';
227
228     return t1;
229 }
230 #endif
231
232
233 #ifdef V3_CONFIG_BUILT_IN_STRNCAT
234 char * strncat(char * s1, const char * s2, size_t limit) {
235     size_t i = 0;
236     char * t1;
237
238     t1 = s1;
239
240     while (*s1) { s1++; }
241
242     while (i < limit) {
243         if (*s2 == '\0') {
244             break;
245         }
246         *s1++ = *s2++;          
247     }
248     *s1 = '\0';
249     return t1;
250 }
251 #endif
252
253
254
255 #ifdef V3_CONFIG_BUILT_IN_STRCPY
256 char * strcpy(char * dest, const char * src)
257 {
258     char *ret = dest;
259
260     while (*src) {
261         *dest++ = *src++;
262     }
263     *dest = '\0';
264
265     return ret;
266 }
267 #endif
268
269
270 #ifdef V3_CONFIG_BUILT_IN_STRNCPY
271 char * strncpy(char * dest, const char * src, size_t limit) {
272     char * ret = dest;
273
274     while ((*src != '\0') && (limit > 0)) {
275         *dest++ = *src++;
276         --limit;
277     }
278
279     if (limit > 0)
280         *dest = '\0';
281
282     return ret;
283 }
284 #endif
285
286
287
288 #ifdef  V3_CONFIG_BUILT_IN_STRDUP
289 char * strdup(const char * s1) {
290     char *ret;
291
292     ret = V3_Malloc(strlen(s1) + 1);
293
294     if (!ret) {
295         PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in built-in strdup\n");
296         return NULL;
297     }
298
299     strcpy(ret, s1);
300
301     return ret;
302 }
303 #endif
304
305
306
307
308 #ifdef V3_CONFIG_BUILT_IN_ATOI
309 int atoi(const char * buf) {
310     int ret = 0;
311
312     while ((*buf >= '0') && (*buf <= '9')) {
313         ret *= 10;
314         ret += (*buf - '0');
315         buf++;
316     }
317
318     return ret;
319 }
320 #endif
321
322
323 #ifdef V3_CONFIG_BUILT_IN_STRTOI
324 int strtoi(const char * nptr, char ** endptr) {
325     int ret = 0;
326     char * buf = (char *)nptr;
327
328     while ((*buf >= '0') && (*buf <= '9')) {
329         ret *= 10;
330         ret += (*buf - '0');
331
332         buf++;
333
334         if (endptr) {
335             *endptr = buf;
336         }
337     }
338
339     return ret;
340 }
341 #endif
342
343 #ifdef V3_CONFIG_BUILT_IN_ATOX
344 uint64_t atox(const char * buf) {
345     uint64_t ret = 0;
346
347     if (*(buf + 1) == 'x') {
348         buf += 2;
349     }
350
351     while (isxdigit(*buf)) {
352         ret <<= 4;
353         
354         if (isdigit(*buf)) {
355             ret += (*buf - '0');
356         } else {
357             ret += tolower(*buf) - 'a' + 10;
358         }
359
360         buf++;
361     }
362
363     return ret;
364 }
365 #endif
366
367 #ifdef V3_CONFIG_BUILT_IN_STRTOX
368 uint64_t strtox(const char * nptr, char ** endptr) {
369     uint64_t ret = 0;
370     char * buf = (char *)nptr;
371
372     if (*(buf + 1) == 'x') {
373         buf += 2;
374     }
375
376     while (isxdigit(*buf)) {
377         ret <<= 4;
378         
379         if (isdigit(*buf)) {
380             ret += (*buf - '0');
381         } else {
382             ret += tolower(*buf) - 'a' + 10;
383         }
384
385         buf++;
386
387         if (endptr) {
388             *endptr = buf;
389         }
390     }
391
392     return ret;
393
394 }
395 #endif
396
397
398 #ifdef V3_CONFIG_BUILT_IN_STRCHR
399 char * strchr(const char * s, int c) {
400     while (*s != '\0') {
401         if (*s == c)
402             return (char *)s;
403         ++s;
404     }
405     return 0;
406 }
407 #endif
408
409
410 #ifdef V3_CONFIG_BUILT_IN_STRRCHR
411 char * strrchr(const char * s, int c) {
412     size_t len = strlen(s);
413     const char * p = s + len;
414
415     while (p > s) {
416         --p;
417
418         if (*p == c) {
419             return (char *)p;
420         }
421     }
422     return 0;
423 }
424 #endif
425
426 #ifdef V3_CONFIG_BUILT_IN_STRPBRK
427 char * strpbrk(const char * s, const char * accept) {
428     size_t setLen = strlen(accept);
429
430     while (*s != '\0') {
431         size_t i;
432         for (i = 0; i < setLen; ++i) {
433             if (*s == accept[i]) {
434                 return (char *)s;
435             }
436         }
437         ++s;
438     }
439
440     return 0;
441 }
442 #endif
443
444 #ifdef V3_CONFIG_BUILT_IN_STRSPN
445 size_t strspn(const char * s, const char * accept) {
446     int match = 1;
447     int cnt = 0;
448     int i = 0;
449     int accept_len = strlen(accept);
450
451     while (match) {
452         match = 0;
453
454         for (i = 0; i < accept_len; i++) {
455             if (s[cnt] == accept[i]) {
456                 match = 1;
457                 cnt++;
458                 break;
459             }
460         }
461     }
462
463     return cnt;
464 }
465 #endif
466
467
468 #ifdef V3_CONFIG_BUILT_IN_STRCSPN
469 size_t strcspn(const char * s, const char * reject) {
470     int match = 0;
471     int cnt = 0;
472     int i = 0;
473     int reject_len = strlen(reject);
474
475     while (!match) {
476         for (i = 0; i < reject_len; i++) {
477             if (s[cnt] == reject[i]) {
478                 match = 1;
479                 break;
480             }
481         }
482
483         if (!match) {
484             cnt++;
485         }
486
487     }
488
489     return cnt;
490 }
491 #endif
492
493
494 #ifdef V3_CONFIG_BUILT_IN_STRSTR
495 char *strstr(const char *haystack, const char *needle)
496 {
497         int l1, l2;
498
499         l2 = strlen(s2);
500         if (!l2)
501                 return (char *)s1;
502         l1 = strlen(s1);
503         while (l1 >= l2) {
504                 l1--;
505                 if (!memcmp(s1, s2, l2))
506                         return (char *)s1;
507                 s1++;
508         }
509         return NULL;
510 }
511 #endif
512
513 #ifdef V3_CONFIG_BUILT_IN_STR_TOLOWER
514 void str_tolower(char * s) {
515     while (isalpha(*s)) {
516         if (!islower(*s)) {
517             *s = tolower(*s);
518         }
519         s++;
520     }
521 }
522 #endif
523
524 #ifdef V3_CONFIG_BUILT_IN_STR_TOUPPER
525 void str_toupper(char * s) {
526     while (isalpha(*s)) {
527         if (!isupper(*s)) {
528             *s = toupper(*s);
529         }
530         s++;
531     }
532 }
533 #endif