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.


formatting fixes
[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 #define NEED_MEMSET 0
40 #define NEED_MEMCPY 0
41 #define NEED_MEMCMP 0
42 #define NEED_STRLEN 0
43 #define NEED_STRNLEN 0
44 #define NEED_STRCMP 0
45 #define NEED_STRNCMP 0
46 #define NEED_STRCAT 0
47 #define NEED_STRNCAT 0
48 #define NEED_STRCPY 0
49 #define NEED_STRNCPY 0
50 #define NEED_STRDUP 0
51 #define NEED_ATOI 0
52 #define NEED_STRCHR 0
53 #define NEED_STRRCHR 0
54 #define NEED_STRPBRK 0
55
56
57
58 #include <palacios/vmm_types.h>
59 #include <palacios/vmm_string.h>
60 #include <palacios/vmm.h>
61
62
63 #if NEED_MEMSET
64 void * memset(void * s, int c, size_t n) {
65     uchar_t * p = (uchar_t *) s;
66
67     while (n > 0) {
68         *p++ = (uchar_t) c;
69         --n;
70     }
71
72     return s;
73 }
74 #endif
75
76 #if NEED_MEMCPY
77 void * memcpy(void * dst, const void * src, size_t n) {
78     uchar_t * d = (uchar_t *) dst;
79     const uchar_t * s = (const uchar_t *)src;
80
81     while (n > 0) {
82         *d++ = *s++;
83         --n;
84     }
85
86     return dst;
87 }
88 #endif
89
90
91 #if NEED_CMP
92 int memcmp(const void * s1_, const void * s2_, size_t n) {
93     const char * s1 = s1_;
94     const char * s2 = s2_;
95
96     while (n > 0) {
97         int cmp = (*s1 - *s2);
98         
99         if (cmp != 0) {
100             return cmp;
101         }
102
103         ++s1;
104         ++s2;
105     }
106
107     return 0;
108 }
109 #endif
110
111
112 #if NEED_STRLEN
113 size_t strlen(const char * s) {
114     size_t len = 0;
115
116     while (*s++ != '\0') {
117         ++len;
118     }
119
120     return len;
121 }
122 #endif
123
124
125
126 #if NEED_STRNLEN
127 /*
128  * This it a GNU extension.
129  * It is like strlen(), but it will check at most maxlen
130  * characters for the terminating nul character,
131  * returning maxlen if it doesn't find a nul.
132  * This is very useful for checking the length of untrusted
133  * strings (e.g., from user space).
134  */
135 size_t strnlen(const char * s, size_t maxlen) {
136     size_t len = 0;
137
138     while ((len < maxlen) && (*s++ != '\0')) {
139         ++len;
140     }
141
142     return len;
143 }
144 #endif
145
146
147 #if NEED_STRCMP
148 int strcmp(const char * s1, const char * s2) {
149     while (1) {
150         int cmp = (*s1 - *s2);
151         
152         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
153             return cmp;
154         }
155         
156         ++s1;
157         ++s2;
158     }
159 }
160 #endif
161
162
163 #if NEED_STRNCMP
164 int strncmp(const char * s1, const char * s2, size_t limit) {
165     size_t i = 0;
166
167     while (i < limit) {
168         int cmp = (*s1 - *s2);
169
170         if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
171             return cmp;
172         }
173
174         ++s1;
175         ++s2;
176         ++i;
177     }
178
179     /* limit reached and equal */
180     return 0;
181 }
182 #endif
183
184
185 #if NEED_STRCAT
186 char * strcat(char * s1, const char * s2) {
187     char * t1 = s1;
188
189     while (*s1) { s1++; }
190     while (*s2) { *s1++ = *s2++; }
191
192     *s1 = '\0';
193
194     return t1;
195 }
196 #endif
197
198
199 #if NEED_STRNCAT
200 char * strncat(char * s1, const char * s2, size_t limit) {
201     size_t i = 0;
202     char * t1;
203
204     t1 = s1;
205
206     while (*s1) { s1++; }
207
208     while (i < limit) {
209         if (*s2 == '\0') {
210             break;
211         }
212         *s1++ = *s2++;          
213     }
214     *s1 = '\0';
215     return t1;
216 }
217 #endif
218
219
220
221 #if NEED_STRCPY
222 char * strcpy(char * dest, const char * src)
223 {
224     char *ret = dest;
225
226     while (*src) {
227         *dest++ = *src++;
228     }
229     *dest = '\0';
230
231     return ret;
232 }
233 #endif
234
235
236 #if NEED_STRNCPY
237 char * strncpy(char * dest, const char * src, size_t limit) {
238     char * ret = dest;
239
240     while ((*src != '\0') && (limit > 0)) {
241         *dest++ = *src++;
242         --limit;
243     }
244
245     if (limit > 0)
246         *dest = '\0';
247
248     return ret;
249 }
250 #endif
251
252
253
254 #if NEED_STRDUP
255 char * strdup(const char * s1) {
256     char *ret;
257
258     ret = V3_Malloc(strlen(s1) + 1);
259     strcpy(ret, s1);
260
261     return ret;
262 }
263 #endif
264
265
266
267
268 #if NEED_ATOI
269 int atoi(const char * buf) {
270     int ret = 0;
271
272     while ((*buf >= '0') && (*buf <= '9')) {
273         ret *= 10;
274         ret += (*buf - '0');
275         buf++;
276     }
277
278     return ret;
279 }
280 #endif
281
282
283 #if NEED_STRCHR
284 char * strchr(const char * s, int c) {
285     while (*s != '\0') {
286         if (*s == c)
287             return (char *)s;
288         ++s;
289     }
290     return 0;
291 }
292 #endif
293
294
295 #if NEED_STRRCHR
296 char * strrchr(const char * s, int c) {
297     size_t len = strlen(s);
298     const char * p = s + len;
299
300     while (p > s) {
301         --p;
302
303         if (*p == c) {
304             return (char *)p;
305         }
306     }
307     return 0;
308 }
309 #endif
310
311 #if NEED_STRPBRK
312 char * strpbrk(const char * s, const char * accept) {
313     size_t setLen = strlen(accept);
314
315     while (*s != '\0') {
316         size_t i;
317         for (i = 0; i < setLen; ++i) {
318             if (*s == accept[i]) {
319                 return (char *)s;
320             }
321         }
322         ++s;
323     }
324
325     return 0;
326 }
327 #endif
328