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.


9160e415f7f0caeda7cb02b271dd1be1d31f3040
[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
156 #ifdef CONFIG_BUILT_IN_STRNCMP
157 int strncmp(const char * s1, const char * s2, size_t limit) {
158     size_t i = 0;
159
160     while (i < limit) {
161         int cmp = (*s1 - *s2);
162
163         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
164             return cmp;
165         }
166
167         ++s1;
168         ++s2;
169         ++i;
170     }
171
172     /* limit reached and equal */
173     return 0;
174 }
175 #endif
176
177
178 #ifdef CONFIG_BUILT_IN_STRCAT
179 char * strcat(char * s1, const char * s2) {
180     char * t1 = s1;
181
182     while (*s1) { s1++; }
183     while (*s2) { *s1++ = *s2++; }
184
185     *s1 = '\0';
186
187     return t1;
188 }
189 #endif
190
191
192 #ifdef CONFIG_BUILT_IN_STRNCAT
193 char * strncat(char * s1, const char * s2, size_t limit) {
194     size_t i = 0;
195     char * t1;
196
197     t1 = s1;
198
199     while (*s1) { s1++; }
200
201     while (i < limit) {
202         if (*s2 == '\0') {
203             break;
204         }
205         *s1++ = *s2++;          
206     }
207     *s1 = '\0';
208     return t1;
209 }
210 #endif
211
212
213
214 #ifdef CONFIG_BUILT_IN_STRCPY
215 char * strcpy(char * dest, const char * src)
216 {
217     char *ret = dest;
218
219     while (*src) {
220         *dest++ = *src++;
221     }
222     *dest = '\0';
223
224     return ret;
225 }
226 #endif
227
228
229 #ifdef CONFIG_BUILT_IN_STRNCPY
230 char * strncpy(char * dest, const char * src, size_t limit) {
231     char * ret = dest;
232
233     while ((*src != '\0') && (limit > 0)) {
234         *dest++ = *src++;
235         --limit;
236     }
237
238     if (limit > 0)
239         *dest = '\0';
240
241     return ret;
242 }
243 #endif
244
245
246
247 #ifdef  CONFIG_BUILT_IN_STRDUP
248 char * strdup(const char * s1) {
249     char *ret;
250
251     ret = V3_Malloc(strlen(s1) + 1);
252     strcpy(ret, s1);
253
254     return ret;
255 }
256 #endif
257
258
259
260
261 #ifdef CONFIG_BUILT_IN_ATOI
262 int atoi(const char * buf) {
263     int ret = 0;
264
265     while ((*buf >= '0') && (*buf <= '9')) {
266         ret *= 10;
267         ret += (*buf - '0');
268         buf++;
269     }
270
271     return ret;
272 }
273 #endif
274
275
276 int strtoi(const char * nptr, char ** endptr) {
277     int ret = 0;
278     char * buf = (char *)nptr;
279
280     while ((*buf >= '0') && (*buf <= '9')) {
281         ret *= 10;
282         ret += (*buf - '0');
283
284         buf++;
285
286         if (endptr) {
287             *endptr = buf;
288         }
289     }
290
291     return ret;
292 }
293
294 uint64_t atox(const char * buf) {
295     uint64_t ret = 0;
296
297     while (isxdigit(*buf)) {
298         ret <<= 4;
299         
300         if (isdigit(*buf)) {
301             ret += (*buf - '0');
302         } else {
303             ret += tolower(*buf) - 'a' + 10;
304         }
305
306         buf++;
307     }
308
309     return ret;
310 }
311
312 uint64_t strtox(const char * nptr, char ** endptr) {
313     uint64_t ret = 0;
314     char * buf = (char *)nptr;
315
316     while (isxdigit(*buf)) {
317         ret <<= 4;
318         
319         if (isdigit(*buf)) {
320             ret += (*buf - '0');
321         } else {
322             ret += tolower(*buf) - 'a' + 10;
323         }
324
325         buf++;
326
327         if (endptr) {
328             *endptr = buf;
329         }
330     }
331
332     return ret;
333
334 }
335
336
337
338 #ifdef CONFIG_BUILT_IN_STRCHR
339 char * strchr(const char * s, int c) {
340     while (*s != '\0') {
341         if (*s == c)
342             return (char *)s;
343         ++s;
344     }
345     return 0;
346 }
347 #endif
348
349
350 #ifdef CONFIG_BUILT_IN_STRRCHR
351 char * strrchr(const char * s, int c) {
352     size_t len = strlen(s);
353     const char * p = s + len;
354
355     while (p > s) {
356         --p;
357
358         if (*p == c) {
359             return (char *)p;
360         }
361     }
362     return 0;
363 }
364 #endif
365
366 #ifdef CONFIG_BUILT_IN_STRPBRK
367 char * strpbrk(const char * s, const char * accept) {
368     size_t setLen = strlen(accept);
369
370     while (*s != '\0') {
371         size_t i;
372         for (i = 0; i < setLen; ++i) {
373             if (*s == accept[i]) {
374                 return (char *)s;
375             }
376         }
377         ++s;
378     }
379
380     return 0;
381 }
382 #endif
383
384 #ifdef CONFIG_BUILT_IN_STRSPN
385 size_t strspn(const char * s, const char * accept) {
386     int match = 1;
387     int cnt = 0;
388     int i = 0;
389     int accept_len = strlen(accept);
390
391     while (match) {
392         match = 0;
393
394         for (i = 0; i < accept_len; i++) {
395             if (s[cnt] == accept[i]) {
396                 match = 1;
397                 cnt++;
398                 break;
399             }
400         }
401     }
402
403     return cnt;
404 }
405 #endif
406
407
408 #ifdef CONFIG_BUILT_IN_STRCSPN
409 size_t strcspn(const char * s, const char * reject) {
410     int match = 0;
411     int cnt = 0;
412     int i = 0;
413     int reject_len = strlen(reject);
414
415     while (!match) {
416         for (i = 0; i < reject_len; i++) {
417             if (s[cnt] == reject[i]) {
418                 match = 1;
419                 cnt++;
420                 break;
421             }
422         }
423     }
424
425     return cnt;
426 }
427 #endif
428
429
430 #ifdef CONFIG_BUILT_IN_STRSTR
431 char *strstr(const char *haystack, const char *needle)
432 {
433         int l1, l2;
434
435         l2 = strlen(s2);
436         if (!l2)
437                 return (char *)s1;
438         l1 = strlen(s1);
439         while (l1 >= l2) {
440                 l1--;
441                 if (!memcmp(s1, s2, l2))
442                         return (char *)s1;
443                 s1++;
444         }
445         return NULL;
446 }
447 #endif