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.


01c408c8c3ccbd10bd98cd81b2591e8e9ba97b86
[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 int strtoi(const char * nptr, char ** endptr) {
324     int ret = 0;
325     char * buf = (char *)nptr;
326
327     while ((*buf >= '0') && (*buf <= '9')) {
328         ret *= 10;
329         ret += (*buf - '0');
330
331         buf++;
332
333         if (endptr) {
334             *endptr = buf;
335         }
336     }
337
338     return ret;
339 }
340
341 uint64_t atox(const char * buf) {
342     uint64_t ret = 0;
343
344     if (*(buf + 1) == 'x') {
345         buf += 2;
346     }
347
348     while (isxdigit(*buf)) {
349         ret <<= 4;
350         
351         if (isdigit(*buf)) {
352             ret += (*buf - '0');
353         } else {
354             ret += tolower(*buf) - 'a' + 10;
355         }
356
357         buf++;
358     }
359
360     return ret;
361 }
362
363 uint64_t strtox(const char * nptr, char ** endptr) {
364     uint64_t ret = 0;
365     char * buf = (char *)nptr;
366
367     if (*(buf + 1) == 'x') {
368         buf += 2;
369     }
370
371     while (isxdigit(*buf)) {
372         ret <<= 4;
373         
374         if (isdigit(*buf)) {
375             ret += (*buf - '0');
376         } else {
377             ret += tolower(*buf) - 'a' + 10;
378         }
379
380         buf++;
381
382         if (endptr) {
383             *endptr = buf;
384         }
385     }
386
387     return ret;
388
389 }
390
391
392
393 #ifdef V3_CONFIG_BUILT_IN_STRCHR
394 char * strchr(const char * s, int c) {
395     while (*s != '\0') {
396         if (*s == c)
397             return (char *)s;
398         ++s;
399     }
400     return 0;
401 }
402 #endif
403
404
405 #ifdef V3_CONFIG_BUILT_IN_STRRCHR
406 char * strrchr(const char * s, int c) {
407     size_t len = strlen(s);
408     const char * p = s + len;
409
410     while (p > s) {
411         --p;
412
413         if (*p == c) {
414             return (char *)p;
415         }
416     }
417     return 0;
418 }
419 #endif
420
421 #ifdef V3_CONFIG_BUILT_IN_STRPBRK
422 char * strpbrk(const char * s, const char * accept) {
423     size_t setLen = strlen(accept);
424
425     while (*s != '\0') {
426         size_t i;
427         for (i = 0; i < setLen; ++i) {
428             if (*s == accept[i]) {
429                 return (char *)s;
430             }
431         }
432         ++s;
433     }
434
435     return 0;
436 }
437 #endif
438
439 #ifdef V3_CONFIG_BUILT_IN_STRSPN
440 size_t strspn(const char * s, const char * accept) {
441     int match = 1;
442     int cnt = 0;
443     int i = 0;
444     int accept_len = strlen(accept);
445
446     while (match) {
447         match = 0;
448
449         for (i = 0; i < accept_len; i++) {
450             if (s[cnt] == accept[i]) {
451                 match = 1;
452                 cnt++;
453                 break;
454             }
455         }
456     }
457
458     return cnt;
459 }
460 #endif
461
462
463 #ifdef V3_CONFIG_BUILT_IN_STRCSPN
464 size_t strcspn(const char * s, const char * reject) {
465     int match = 0;
466     int cnt = 0;
467     int i = 0;
468     int reject_len = strlen(reject);
469
470     while (!match) {
471         for (i = 0; i < reject_len; i++) {
472             if (s[cnt] == reject[i]) {
473                 match = 1;
474                 break;
475             }
476         }
477
478         if (!match) {
479             cnt++;
480         }
481
482     }
483
484     return cnt;
485 }
486 #endif
487
488
489 #ifdef V3_CONFIG_BUILT_IN_STRSTR
490 char *strstr(const char *haystack, const char *needle)
491 {
492         int l1, l2;
493
494         l2 = strlen(s2);
495         if (!l2)
496                 return (char *)s1;
497         l1 = strlen(s1);
498         while (l1 >= l2) {
499                 l1--;
500                 if (!memcmp(s1, s2, l2))
501                         return (char *)s1;
502                 s1++;
503         }
504         return NULL;
505 }
506 #endif
507
508
509 void str_tolower(char * s) {
510     while (isalpha(*s)) {
511         if (!islower(*s)) {
512             *s = tolower(*s);
513         }
514         s++;
515     }
516 }
517
518
519 void str_toupper(char * s) {
520     while (isalpha(*s)) {
521         if (!isupper(*s)) {
522             *s = toupper(*s);
523         }
524         s++;
525     }
526 }