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.


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