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.


c286a6dc6c734cdc6784d62c8c5bb92e471868b9
[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
72 #ifdef CONFIG_BUILT_IN_MEMCMP
73 int memcmp(const void * s1_, const void * s2_, size_t n) {
74     const char * s1 = s1_;
75     const char * s2 = s2_;
76
77     while (n > 0) {
78         int cmp = (*s1 - *s2);
79         
80         if (cmp != 0) {
81             return cmp;
82         }
83
84         ++s1;
85         ++s2;
86     }
87
88     return 0;
89 }
90 #endif
91
92
93 #ifdef CONFIG_BUILT_IN_STRLEN
94 size_t strlen(const char * s) {
95     size_t len = 0;
96
97     while (*s++ != '\0') {
98         ++len;
99     }
100
101     return len;
102 }
103 #endif
104
105
106
107 #ifdef CONFIG_BUILT_IN_STRNLEN
108 /*
109  * This it a GNU extension.
110  * It is like strlen(), but it will check at most maxlen
111  * characters for the terminating nul character,
112  * returning maxlen if it doesn't find a nul.
113  * This is very useful for checking the length of untrusted
114  * strings (e.g., from user space).
115  */
116 size_t strnlen(const char * s, size_t maxlen) {
117     size_t len = 0;
118
119     while ((len < maxlen) && (*s++ != '\0')) {
120         ++len;
121     }
122
123     return len;
124 }
125 #endif
126
127
128 #ifdef CONFIG_BUILT_IN_STRCMP
129 int strcmp(const char * s1, const char * s2) {
130     while (1) {
131         int cmp = (*s1 - *s2);
132         
133         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
134             return cmp;
135         }
136         
137         ++s1;
138         ++s2;
139     }
140 }
141 #endif
142
143
144 #ifdef CONFIG_BUILT_IN_STRNCMP
145 int strncmp(const char * s1, const char * s2, size_t limit) {
146     size_t i = 0;
147
148     while (i < limit) {
149         int cmp = (*s1 - *s2);
150
151         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
152             return cmp;
153         }
154
155         ++s1;
156         ++s2;
157         ++i;
158     }
159
160     /* limit reached and equal */
161     return 0;
162 }
163 #endif
164
165
166 #ifdef CONFIG_BUILT_IN_STRCAT
167 char * strcat(char * s1, const char * s2) {
168     char * t1 = s1;
169
170     while (*s1) { s1++; }
171     while (*s2) { *s1++ = *s2++; }
172
173     *s1 = '\0';
174
175     return t1;
176 }
177 #endif
178
179
180 #ifdef CONFIG_BUILT_IN_STRNCAT
181 char * strncat(char * s1, const char * s2, size_t limit) {
182     size_t i = 0;
183     char * t1;
184
185     t1 = s1;
186
187     while (*s1) { s1++; }
188
189     while (i < limit) {
190         if (*s2 == '\0') {
191             break;
192         }
193         *s1++ = *s2++;          
194     }
195     *s1 = '\0';
196     return t1;
197 }
198 #endif
199
200
201
202 #ifdef CONFIG_BUILT_IN_STRCPY
203 char * strcpy(char * dest, const char * src)
204 {
205     char *ret = dest;
206
207     while (*src) {
208         *dest++ = *src++;
209     }
210     *dest = '\0';
211
212     return ret;
213 }
214 #endif
215
216
217 #ifdef CONFIG_BUILT_IN_STRNCPY
218 char * strncpy(char * dest, const char * src, size_t limit) {
219     char * ret = dest;
220
221     while ((*src != '\0') && (limit > 0)) {
222         *dest++ = *src++;
223         --limit;
224     }
225
226     if (limit > 0)
227         *dest = '\0';
228
229     return ret;
230 }
231 #endif
232
233
234
235 #ifdef  CONFIG_BUILT_IN_STRDUP
236 char * strdup(const char * s1) {
237     char *ret;
238
239     ret = V3_Malloc(strlen(s1) + 1);
240     strcpy(ret, s1);
241
242     return ret;
243 }
244 #endif
245
246
247
248
249 #ifdef CONFIG_BUILT_IN_ATOI
250 int atoi(const char * buf) {
251     int ret = 0;
252
253     while ((*buf >= '0') && (*buf <= '9')) {
254         ret *= 10;
255         ret += (*buf - '0');
256         buf++;
257     }
258
259     return ret;
260 }
261 #endif
262
263
264 #ifdef CONFIG_BUILT_IN_STRCHR
265 char * strchr(const char * s, int c) {
266     while (*s != '\0') {
267         if (*s == c)
268             return (char *)s;
269         ++s;
270     }
271     return 0;
272 }
273 #endif
274
275
276 #ifdef CONFIG_BUILT_IN_STRRCHR
277 char * strrchr(const char * s, int c) {
278     size_t len = strlen(s);
279     const char * p = s + len;
280
281     while (p > s) {
282         --p;
283
284         if (*p == c) {
285             return (char *)p;
286         }
287     }
288     return 0;
289 }
290 #endif
291
292 #ifdef CONFIG_BUILT_IN_STRPBRK
293 char * strpbrk(const char * s, const char * accept) {
294     size_t setLen = strlen(accept);
295
296     while (*s != '\0') {
297         size_t i;
298         for (i = 0; i < setLen; ++i) {
299             if (*s == accept[i]) {
300                 return (char *)s;
301             }
302         }
303         ++s;
304     }
305
306     return 0;
307 }
308 #endif
309