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.


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