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
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 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 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 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     memcpy(tmp, src, n);
76     memcpy(dst, tmp, n);
77     
78     V3_Free(tmp);
79     return dst;
80 }
81 #endif
82
83
84 #ifdef 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_;
88
89     while (n > 0) {
90         int cmp = (*s1 - *s2);
91         
92         if (cmp != 0) {
93             return cmp;
94         }
95
96         ++s1;
97         ++s2;
98     }
99
100     return 0;
101 }
102 #endif
103
104
105 #ifdef CONFIG_BUILT_IN_STRLEN
106 size_t strlen(const char * s) {
107     size_t len = 0;
108
109     while (*s++ != '\0') {
110         ++len;
111     }
112
113     return len;
114 }
115 #endif
116
117
118
119 #ifdef CONFIG_BUILT_IN_STRNLEN
120 /*
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).
127  */
128 size_t strnlen(const char * s, size_t maxlen) {
129     size_t len = 0;
130
131     while ((len < maxlen) && (*s++ != '\0')) {
132         ++len;
133     }
134
135     return len;
136 }
137 #endif
138
139
140 #ifdef CONFIG_BUILT_IN_STRCMP
141 int strcmp(const char * s1, const char * s2) {
142     while (1) {
143         int cmp = (*s1 - *s2);
144         
145         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
146             return cmp;
147         }
148         
149         ++s1;
150         ++s2;
151     }
152 }
153 #endif
154
155 #ifdef CONFIG_BUILT_IN_STRCASECMP
156 int strcasecmp(const char * s1, const char * s2) {
157     while (1) {
158         int cmp = (tolower(*s1) - tolower(*s2));
159
160         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
161             return cmp;
162         }
163
164         ++s1;
165         ++s2;
166     }
167 }
168
169 #endif
170
171
172 #ifdef CONFIG_BUILT_IN_STRNCMP
173 int strncmp(const char * s1, const char * s2, size_t limit) {
174     size_t i = 0;
175
176     while (i < limit) {
177         int cmp = (*s1 - *s2);
178
179         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
180             return cmp;
181         }
182
183         ++s1;
184         ++s2;
185         ++i;
186     }
187
188     /* limit reached and equal */
189     return 0;
190 }
191 #endif
192
193 #ifdef CONFIG_BUILT_IN_STRNCASECMP
194 int strncasecmp(const char * s1, const char * s2, size_t limit) {
195     size_t i = 0;
196
197     while (i < limit) {
198         int cmp = (tolower(*s1) - tolower(*s2));
199
200         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
201             return cmp;
202         }
203
204         ++s1;
205         ++s2;
206         ++i;
207     }
208
209     return 0;
210 }
211 #endif
212
213
214 #ifdef CONFIG_BUILT_IN_STRCAT
215 char * strcat(char * s1, const char * s2) {
216     char * t1 = s1;
217
218     while (*s1) { s1++; }
219     while (*s2) { *s1++ = *s2++; }
220
221     *s1 = '\0';
222
223     return t1;
224 }
225 #endif
226
227
228 #ifdef CONFIG_BUILT_IN_STRNCAT
229 char * strncat(char * s1, const char * s2, size_t limit) {
230     size_t i = 0;
231     char * t1;
232
233     t1 = s1;
234
235     while (*s1) { s1++; }
236
237     while (i < limit) {
238         if (*s2 == '\0') {
239             break;
240         }
241         *s1++ = *s2++;          
242     }
243     *s1 = '\0';
244     return t1;
245 }
246 #endif
247
248
249
250 #ifdef CONFIG_BUILT_IN_STRCPY
251 char * strcpy(char * dest, const char * src)
252 {
253     char *ret = dest;
254
255     while (*src) {
256         *dest++ = *src++;
257     }
258     *dest = '\0';
259
260     return ret;
261 }
262 #endif
263
264
265 #ifdef CONFIG_BUILT_IN_STRNCPY
266 char * strncpy(char * dest, const char * src, size_t limit) {
267     char * ret = dest;
268
269     while ((*src != '\0') && (limit > 0)) {
270         *dest++ = *src++;
271         --limit;
272     }
273
274     if (limit > 0)
275         *dest = '\0';
276
277     return ret;
278 }
279 #endif
280
281
282
283 #ifdef  CONFIG_BUILT_IN_STRDUP
284 char * strdup(const char * s1) {
285     char *ret;
286
287     ret = V3_Malloc(strlen(s1) + 1);
288     strcpy(ret, s1);
289
290     return ret;
291 }
292 #endif
293
294
295
296
297 #ifdef CONFIG_BUILT_IN_ATOI
298 int atoi(const char * buf) {
299     int ret = 0;
300
301     while ((*buf >= '0') && (*buf <= '9')) {
302         ret *= 10;
303         ret += (*buf - '0');
304         buf++;
305     }
306
307     return ret;
308 }
309 #endif
310
311
312 int strtoi(const char * nptr, char ** endptr) {
313     int ret = 0;
314     char * buf = (char *)nptr;
315
316     while ((*buf >= '0') && (*buf <= '9')) {
317         ret *= 10;
318         ret += (*buf - '0');
319
320         buf++;
321
322         if (endptr) {
323             *endptr = buf;
324         }
325     }
326
327     return ret;
328 }
329
330 uint64_t atox(const char * buf) {
331     uint64_t ret = 0;
332
333     if (*(buf + 1) == 'x') {
334         buf += 2;
335     }
336
337     while (isxdigit(*buf)) {
338         ret <<= 4;
339         
340         if (isdigit(*buf)) {
341             ret += (*buf - '0');
342         } else {
343             ret += tolower(*buf) - 'a' + 10;
344         }
345
346         buf++;
347     }
348
349     return ret;
350 }
351
352 uint64_t strtox(const char * nptr, char ** endptr) {
353     uint64_t ret = 0;
354     char * buf = (char *)nptr;
355
356     if (*(buf + 1) == 'x') {
357         buf += 2;
358     }
359
360     while (isxdigit(*buf)) {
361         ret <<= 4;
362         
363         if (isdigit(*buf)) {
364             ret += (*buf - '0');
365         } else {
366             ret += tolower(*buf) - 'a' + 10;
367         }
368
369         buf++;
370
371         if (endptr) {
372             *endptr = buf;
373         }
374     }
375
376     return ret;
377
378 }
379
380
381
382 #ifdef CONFIG_BUILT_IN_STRCHR
383 char * strchr(const char * s, int c) {
384     while (*s != '\0') {
385         if (*s == c)
386             return (char *)s;
387         ++s;
388     }
389     return 0;
390 }
391 #endif
392
393
394 #ifdef CONFIG_BUILT_IN_STRRCHR
395 char * strrchr(const char * s, int c) {
396     size_t len = strlen(s);
397     const char * p = s + len;
398
399     while (p > s) {
400         --p;
401
402         if (*p == c) {
403             return (char *)p;
404         }
405     }
406     return 0;
407 }
408 #endif
409
410 #ifdef CONFIG_BUILT_IN_STRPBRK
411 char * strpbrk(const char * s, const char * accept) {
412     size_t setLen = strlen(accept);
413
414     while (*s != '\0') {
415         size_t i;
416         for (i = 0; i < setLen; ++i) {
417             if (*s == accept[i]) {
418                 return (char *)s;
419             }
420         }
421         ++s;
422     }
423
424     return 0;
425 }
426 #endif
427
428 #ifdef CONFIG_BUILT_IN_STRSPN
429 size_t strspn(const char * s, const char * accept) {
430     int match = 1;
431     int cnt = 0;
432     int i = 0;
433     int accept_len = strlen(accept);
434
435     while (match) {
436         match = 0;
437
438         for (i = 0; i < accept_len; i++) {
439             if (s[cnt] == accept[i]) {
440                 match = 1;
441                 cnt++;
442                 break;
443             }
444         }
445     }
446
447     return cnt;
448 }
449 #endif
450
451
452 #ifdef CONFIG_BUILT_IN_STRCSPN
453 size_t strcspn(const char * s, const char * reject) {
454     int match = 0;
455     int cnt = 0;
456     int i = 0;
457     int reject_len = strlen(reject);
458
459     while (!match) {
460         for (i = 0; i < reject_len; i++) {
461             if (s[cnt] == reject[i]) {
462                 match = 1;
463                 cnt++;
464                 break;
465             }
466         }
467     }
468
469     return cnt;
470 }
471 #endif
472
473
474 #ifdef CONFIG_BUILT_IN_STRSTR
475 char *strstr(const char *haystack, const char *needle)
476 {
477         int l1, l2;
478
479         l2 = strlen(s2);
480         if (!l2)
481                 return (char *)s1;
482         l1 = strlen(s1);
483         while (l1 >= l2) {
484                 l1--;
485                 if (!memcmp(s1, s2, l2))
486                         return (char *)s1;
487                 s1++;
488         }
489         return NULL;
490 }
491 #endif
492
493
494 void str_tolower(char * s) {
495     while (isalpha(*s)) {
496         if (!islower(*s)) {
497             *s = tolower(*s);
498         }
499         s++;
500     }
501 }
502
503
504 void str_toupper(char * s) {
505     while (isalpha(*s)) {
506         if (!isupper(*s)) {
507             *s = toupper(*s);
508         }
509         s++;
510     }
511 }