wcscpy(): Fixed crash on ElectricFence
[reactos.git] / ntoskrnl / rtl / wstring.c
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS kernel
4  * FILE:            ntoskrnl/rtl/wstring.c
5  * PURPOSE:         Wide string functions
6  * PROGRAMMER:      David Welch (welch@cwcom.net)
7  * UPDATE HISTORY:
8  *                  Created 22/05/98
9  *   1998/12/04  RJJ    Cleaned up and added i386 def checks.
10  *   1999/07/29  ekohl  Added missing functions.
11  */
12
13 /* INCLUDES *****************************************************************/
14
15 #include <ddk/ntddk.h>
16
17 #define NDEBUG
18 #include <internal/debug.h>
19
20 /* FUNCTIONS *****************************************************************/
21
22 int _wcsicmp (const wchar_t* cs, const wchar_t* ct)
23 {
24         while (*cs != '\0' && *ct != '\0' && towupper(*cs) == towupper(*ct))
25         {
26                 cs++;
27                 ct++;
28         }
29         return *cs - *ct;
30 }
31
32 #ifndef LIBCAPTIVE
33
34 wchar_t *_wcslwr (wchar_t *x)
35 {
36         wchar_t  *y=x;
37
38         while (*y)
39         {
40                 *y=towlower(*y);
41                 y++;
42         }
43         return x;
44 }
45
46
47 int _wcsnicmp (const wchar_t * cs,const wchar_t * ct,size_t count)
48 {
49         if (count == 0)
50                 return 0;
51         do {
52                 if (towupper(*cs) != towupper(*ct++))
53                         return towupper(*cs) - towupper(*--ct);
54                 if (*cs++ == 0)
55                         break;
56         } while (--count != 0);
57         return 0;
58 }
59
60
61 wchar_t *_wcsnset (wchar_t* wsToFill, wchar_t wcFill, size_t sizeMaxFill)
62 {
63         wchar_t *t = wsToFill;
64         int i = 0;
65         while( *wsToFill != 0 && i < sizeMaxFill)
66         {
67                 *wsToFill = wcFill;
68                 wsToFill++;
69                 i++;
70         }
71         return t;
72 }
73
74
75 wchar_t *_wcsrev(wchar_t *s)
76 {
77         wchar_t  *e;
78         wchar_t   a;
79         e=s;
80         while (*e)
81                 e++;
82         while (s<e)
83         {
84                 a=*s;
85                 *s=*e;
86                 *e=a;
87                 s++;
88                 e--;
89         }
90         return s;
91 }
92
93
94 wchar_t *_wcsupr(wchar_t *x)
95 {
96         wchar_t *y=x;
97
98         while (*y)
99         {
100                 *y=towupper(*y);
101                 y++;
102         }
103         return x;
104 }
105
106 #endif /* LIBCAPTIVE */
107
108 wchar_t * wcscat(wchar_t *dest, const wchar_t *src)
109 {
110   int i, j;
111    
112   for (j = 0; dest[j] != 0; j++)
113     ;
114   for (i = 0; src[i] != 0; i++)
115     {
116       dest[j + i] = src[i];
117     }
118   dest[j + i] = 0;
119
120   return dest;
121 }
122
123 wchar_t * wcschr(const wchar_t *str, wchar_t ch)
124 {
125   while ((*str) != ((wchar_t) 0))
126     {
127       if ((*str) == ch)
128         {
129           return (wchar_t *) str;
130         }
131       str++;
132     }
133
134   return NULL;
135 }
136
137 int wcscmp(const wchar_t *cs, const wchar_t *ct)
138 {
139   while (*cs != '\0' && *ct != '\0' && *cs == *ct)
140     {
141       cs++;
142       ct++;
143     }
144   return *cs - *ct;
145 }
146
147 wchar_t* wcscpy(wchar_t* str1, const wchar_t* str2)
148 {
149    wchar_t* s = str1;
150    /* Never dump 'str1' here as it can crash due to unitialized memory access (no end-terminator). */
151    DPRINT("wcscpy(str1 <undef>, str2 %S)\n",str2);
152    while ((*str2)!=0)
153      {
154         *s = *str2;
155         s++;
156         str2++;
157      }
158    *s = 0;
159    return(str1);
160 }
161
162 #ifndef LIBCAPTIVE
163
164 size_t wcscspn(const wchar_t *str,const wchar_t *reject)
165 {
166         wchar_t *s;
167         wchar_t *t;
168         s=(wchar_t *)str;
169         do {
170                 t=(wchar_t *)reject;
171                 while (*t) { 
172                         if (*t==*s) 
173                                 break;
174                         t++;
175                 }
176                 if (*t) 
177                         break;
178                 s++;
179         } while (*s);
180         return s-str; /* nr of wchars */
181 }
182
183 #endif /* LIBCAPTIVE */
184
185 size_t wcslen(const wchar_t *s)
186 {
187   unsigned int len = 0;
188
189   while (s[len] != 0) 
190     {
191       len++;
192     }
193
194   return len;
195 }
196
197 #ifndef LIBCAPTIVE
198
199 wchar_t * wcsncat(wchar_t *dest, const wchar_t *src, size_t count)
200 {
201   int i, j;
202    
203   for (j = 0; dest[j] != 0; j++)
204     ;
205   for (i = 0; i < count; i++)
206     {
207       dest[j + i] = src[i];
208       if (src[i] == 0)
209         {
210           return dest;
211         }
212     }
213   dest[j + i] = 0;
214
215   return dest;
216 }
217
218
219 int wcsncmp(const wchar_t *cs, const wchar_t *ct, size_t count)
220 {
221   while (*cs != '\0' && *ct != '\0' && *cs == *ct && --count)
222     {
223       cs++;
224       ct++;
225     }
226   return *cs - *ct;
227 }
228
229
230 wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t count)
231 {
232   int i;
233    
234   for (i = 0; i < count; i++)
235     {
236       dest[i] = src[i];
237       if (src[i] == 0)
238         {
239           return dest;
240         }
241     }
242   dest[i] = 0;
243
244   return dest;
245 }
246
247 #endif /* LIBCAPTIVE */
248
249 wchar_t *wcsrchr(const wchar_t *str, wchar_t ch)
250 {
251   unsigned int len = 0;
252   while (str[len] != ((wchar_t)0))
253     {
254       len++;
255     }
256    
257   for (; len > 0; len--)
258     {
259       if (str[len-1]==ch)
260         {
261           return (wchar_t *) &str[len - 1];
262         }
263     }
264
265   return NULL;
266 }
267
268 #ifndef LIBCAPTIVE
269
270 size_t wcsspn(const wchar_t *str,const wchar_t *accept)
271 {
272         wchar_t  *s;
273         wchar_t  *t;
274         s=(wchar_t *)str;
275         do
276         {
277                 t=(wchar_t *)accept;
278                 while (*t)
279                 {
280                         if (*t==*s)
281                                 break;
282                         t++;
283                 }
284                 if (!*t)
285                         break;
286                 s++;
287         } while (*s);
288         return s-str; /* nr of wchars */
289 }
290
291
292 wchar_t *wcsstr(const wchar_t *s,const wchar_t *b)
293 {
294         wchar_t *x;
295         wchar_t *y;
296         wchar_t *c;
297         x=(wchar_t *)s;
298         while (*x)
299         {
300                 if (*x==*b)
301                 {
302                         y=x;
303                         c=(wchar_t *)b;
304                         while (*y && *c && *y==*c)
305                         {
306                                 c++;
307                                 y++;
308                         }
309                         if (!*c)
310                                 return x;
311                 }
312                 x++;
313         }
314         return NULL;
315 }
316
317 #endif /* LIBCAPTIVE */