Check for NULL in lstrcpy* (Windows uses real exception handlers).
[wine] / memory / string.c
1 /*
2  * String functions
3  *
4  * Copyright 1993 Yngvi Sigurjonsson
5  * Copyright 1996 Alexandre Julliard
6  */
7
8 #include <ctype.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "winerror.h"
12 #include "ldt.h"
13 #include "debug.h"
14 #include "winnls.h"
15
16 static const BYTE STRING_Oem2Ansi[256] =
17 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\244"
18 "\020\021\022\023\266\247\026\027\030\031\032\033\034\035\036\037"
19 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
20 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
21 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
22 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
23 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
24 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
25 "\307\374\351\342\344\340\345\347\352\353\350\357\356\354\304\305"
26 "\311\346\306\364\366\362\373\371\377\326\334\242\243\245\120\203"
27 "\341\355\363\372\361\321\252\272\277\137\254\275\274\241\253\273"
28 "\137\137\137\246\246\246\246\053\053\246\246\053\053\053\053\053"
29 "\053\055\055\053\055\053\246\246\053\053\055\055\246\055\053\055"
30 "\055\055\055\053\053\053\053\053\053\053\053\137\137\246\137\137"
31 "\137\337\137\266\137\137\265\137\137\137\137\137\137\137\137\137"
32 "\137\261\137\137\137\137\367\137\260\225\267\137\156\262\137\137";
33
34 static const BYTE STRING_Ansi2Oem[256] =
35 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
36 "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
37 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
38 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
39 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
40 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
41 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
42 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
43 "\200\201\054\237\054\137\375\374\210\045\123\074\117\215\216\217"
44 "\220\140\047\042\042\371\055\137\230\231\163\076\157\235\236\131"
45 "\040\255\233\234\017\235\335\025\042\143\246\256\252\055\162\137"
46 "\370\361\375\063\047\346\024\372\054\061\247\257\254\253\137\250"
47 "\101\101\101\101\216\217\222\200\105\220\105\105\111\111\111\111"
48 "\104\245\117\117\117\117\231\170\117\125\125\125\232\131\137\341"
49 "\205\240\203\141\204\206\221\207\212\202\210\211\215\241\214\213"
50 "\144\244\225\242\223\157\224\366\157\227\243\226\201\171\137\230";
51
52 #define OEM_TO_ANSI(ch) (STRING_Oem2Ansi[(unsigned char)(ch)])
53 #define ANSI_TO_OEM(ch) (STRING_Ansi2Oem[(unsigned char)(ch)])
54
55 /* Internaly used by strchr family functions */
56 static BOOL32 ChrCmpA( WORD word1, WORD word2);
57 static BOOL32 ChrCmpW( WORD word1, WORD word2);
58
59 extern LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar);
60
61
62 /***********************************************************************
63  *           hmemcpy   (KERNEL.348)
64  */
65 void WINAPI hmemcpy( LPVOID dst, LPCVOID src, LONG count )
66 {
67     memcpy( dst, src, count );
68 }
69
70
71 /***********************************************************************
72  *           lstrcat16   (KERNEL.89)
73  */
74 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
75 {
76     lstrcat32A( (LPSTR)PTR_SEG_TO_LIN(dst), src );
77     return dst;
78 }
79
80
81 /***********************************************************************
82  *           lstrcat32A   (KERNEL32.599)
83  */
84 LPSTR WINAPI lstrcat32A( LPSTR dst, LPCSTR src )
85 {
86     TRACE(string,"Append %s to %s\n",
87                    debugstr_a (src), debugstr_a (dst));
88     /* Windows does not check for NULL pointers here, so we don't either */
89     strcat( dst, src );
90     return dst;
91 }
92
93
94 /***********************************************************************
95  *           lstrcat32W   (KERNEL32.600)
96  */
97 LPWSTR WINAPI lstrcat32W( LPWSTR dst, LPCWSTR src )
98 {
99     register LPWSTR p = dst;
100     TRACE(string,"Append L%s to L%s\n",
101                    debugstr_w (src), debugstr_w (dst));
102     /* Windows does not check for NULL pointers here, so we don't either */
103     while (*p) p++;
104     while ((*p++ = *src++));
105     return dst;
106 }
107
108
109 /***********************************************************************
110  *           lstrcatn16   (KERNEL.352)
111  */
112 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
113 {
114     lstrcatn32A( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
115     return dst;
116 }
117
118
119 /***********************************************************************
120  *           lstrcatn32A   (Not a Windows API)
121  */
122 LPSTR WINAPI lstrcatn32A( LPSTR dst, LPCSTR src, INT32 n )
123 {
124     register LPSTR p = dst;
125     TRACE(string,"strcatn add %d chars from %s to %s\n",
126                    n, debugstr_an (src, n), debugstr_a (dst));
127     while (*p) p++;
128     if ((n -= (INT32)(p - dst)) <= 0) return dst;
129     lstrcpyn32A( p, src, n );
130     return dst;
131 }
132
133
134 /***********************************************************************
135  *           lstrcatn32W   (Not a Windows API)
136  */
137 LPWSTR WINAPI lstrcatn32W( LPWSTR dst, LPCWSTR src, INT32 n )
138 {
139     register LPWSTR p = dst;
140     TRACE(string,"strcatn add %d chars from L%s to L%s\n",
141                    n, debugstr_wn (src, n), debugstr_w (dst));
142     while (*p) p++;
143     if ((n -= (INT32)(p - dst)) <= 0) return dst;
144     lstrcpyn32W( p, src, n );
145     return dst;
146 }
147
148
149 /***********************************************************************
150  *           lstrcmp16   (USER.430)
151  */
152 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
153 {
154     return (INT16)strcmp( str1, str2 );
155 }
156
157
158 /***********************************************************************
159  *           lstrcmp32A   (KERNEL.602)
160  */
161 INT32 WINAPI lstrcmp32A( LPCSTR str1, LPCSTR str2 )
162 {
163     return CompareString32A(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
164 }
165
166
167 /***********************************************************************
168  *           lstrcmp32W   (KERNEL.603)
169  * FIXME : should call CompareString32W, when it is implemented.
170  *    This implementation is not "word sort", as it should.
171  */
172 INT32 WINAPI lstrcmp32W( LPCWSTR str1, LPCWSTR str2 )
173 {
174     TRACE(string,"L%s and L%s\n",
175                    debugstr_w (str1), debugstr_w (str2));
176     if (!str1 || !str2) {
177         SetLastError(ERROR_INVALID_PARAMETER);
178         return 0;
179     }
180     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
181     return (INT32)(*str1 - *str2);
182 }
183
184
185 /***********************************************************************
186  *           lstrcmpi16   (USER.471)
187  */
188 INT16 WINAPI lstrcmpi16( LPCSTR str1, LPCSTR str2 )
189 {
190     return (INT16)lstrcmpi32A( str1, str2 );
191 }
192
193
194 /***********************************************************************
195  *           lstrcmpi32A   (KERNEL32.605)
196  */
197 INT32 WINAPI lstrcmpi32A( LPCSTR str1, LPCSTR str2 )
198 {    TRACE(string,"strcmpi %s and %s\n",
199                    debugstr_a (str1), debugstr_a (str2));
200     return CompareString32A(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
201 }
202
203
204 /***********************************************************************
205  *           lstrcmpi32W   (KERNEL32.606)
206  */
207 INT32 WINAPI lstrcmpi32W( LPCWSTR str1, LPCWSTR str2 )
208 {
209     INT32 res;
210
211 #if 0
212     /* Too much!  (From registry loading.)  */
213     TRACE(string,"strcmpi L%s and L%s\n",
214                    debugstr_w (str1), debugstr_w (str2));
215 #endif
216     if (!str1 || !str2) {
217         SetLastError(ERROR_INVALID_PARAMETER);
218         return 0;
219     }
220     while (*str1)
221     {
222         if ((*str1<0x100 ) && (*str2<0x100)) {
223             if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
224         } else {
225             if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
226         }
227         str1++;
228         str2++;
229     }
230     return towupper(*str1) - towupper(*str2);
231 }
232
233
234 /***********************************************************************
235  *           lstrcpy16   (KERNEL.88)
236  */
237 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
238 {
239     lstrcpy32A( (LPSTR)PTR_SEG_TO_LIN(dst), src );
240     return dst;
241 }
242
243
244 /***********************************************************************
245  *           lstrcpy32A   (KERNEL32.608)
246  */
247 LPSTR WINAPI lstrcpy32A( LPSTR dst, LPCSTR src )
248 {
249     TRACE(string,"strcpy %s\n", debugstr_a (src));
250     /* In real windows the whole function is protected by an exception handler
251      * that returns ERROR_INVALID_PARAMETER on faulty parameters
252      * We currently just check for NULL.
253      */
254     if (!dst || !src) {
255         SetLastError(ERROR_INVALID_PARAMETER);
256         return 0;
257     }
258     strcpy( dst, src );
259     return dst;
260 }
261
262
263 /***********************************************************************
264  *           lstrcpy32W   (KERNEL32.609)
265  */
266 LPWSTR WINAPI lstrcpy32W( LPWSTR dst, LPCWSTR src )
267 {
268     register LPWSTR p = dst;
269     TRACE(string,"strcpy L%s\n", debugstr_w (src));
270     /* In real windows the whole function is protected by an exception handler
271      * that returns ERROR_INVALID_PARAMETER on faulty parameters
272      * We currently just check for NULL.
273      */
274     if (!dst || !src) {
275         SetLastError(ERROR_INVALID_PARAMETER);
276         return 0;
277     }
278     while ((*p++ = *src++));
279     return dst;
280 }
281
282
283 /***********************************************************************
284  *           lstrcpyn16   (KERNEL.353)
285  */
286 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
287 {
288     lstrcpyn32A( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
289     return dst;
290 }
291
292
293 /***********************************************************************
294  *           lstrcpyn32A   (KERNEL32.611)
295  * Note: this function differs from the UNIX strncpy, it _always_ writes
296  * a terminating \0
297  */
298 LPSTR WINAPI lstrcpyn32A( LPSTR dst, LPCSTR src, INT32 n )
299 {
300     LPSTR p = dst;
301     TRACE(string,"strcpyn %s for %d chars\n",
302                    debugstr_an (src,n), n);
303     /* In real windows the whole function is protected by an exception handler
304      * that returns ERROR_INVALID_PARAMETER on faulty parameters
305      * We currently just check for NULL.
306      */
307     if (!dst || !src) {
308         SetLastError(ERROR_INVALID_PARAMETER);
309         return 0;
310     }
311     while ((n-- > 1) && *src) *p++ = *src++;
312     if (n >= 0) *p = 0;
313     return dst;
314 }
315
316
317 /***********************************************************************
318  *           lstrcpyn32W   (KERNEL32.612)
319  * Note: this function differs from the UNIX strncpy, it _always_ writes
320  * a terminating \0
321  */
322 LPWSTR WINAPI lstrcpyn32W( LPWSTR dst, LPCWSTR src, INT32 n )
323 {
324     LPWSTR p = dst;
325     TRACE(string,"strcpyn L%s for %d chars\n",
326                    debugstr_wn (src,n), n);
327     /* In real windows the whole function is protected by an exception handler
328      * that returns ERROR_INVALID_PARAMETER on faulty parameters
329      * We currently just check for NULL.
330      */
331     if (!dst || !src) {
332         SetLastError(ERROR_INVALID_PARAMETER);
333         return 0;
334     }
335     while ((n-- > 1) && *src) *p++ = *src++;
336     if (n >= 0) *p = 0;
337     return dst;
338 }
339
340
341 /***********************************************************************
342  *           lstrlen16   (KERNEL.90)
343  */
344 INT16 WINAPI lstrlen16( LPCSTR str )
345 {
346     return (INT16)lstrlen32A( str );
347 }
348
349
350 /***********************************************************************
351  *           lstrlen32A   (KERNEL32.614)
352  */
353 INT32 WINAPI lstrlen32A( LPCSTR str )
354 {
355     /* looks weird, but win3.1 KERNEL got a GeneralProtection handler
356      * in lstrlen() ... we check only for NULL pointer reference.
357      * - Marcus Meissner
358      */
359     TRACE(string,"strlen %s\n", debugstr_a (str));
360     if (!str) return 0;
361     return (INT32)strlen(str);
362 }
363
364
365 /***********************************************************************
366  *           lstrlen32W   (KERNEL32.615)
367  */
368 INT32 WINAPI lstrlen32W( LPCWSTR str )
369 {
370     INT32 len = 0;
371     TRACE(string,"strlen L%s\n", debugstr_w (str));
372     if (!str) return 0;
373     while (*str++) len++;
374     return len;
375 }
376
377
378 /***********************************************************************
379  *           lstrncmp32A   (Not a Windows API)
380  */
381 INT32 WINAPI lstrncmp32A( LPCSTR str1, LPCSTR str2, INT32 n )
382 {
383     TRACE(string,"strncmp %s and %s for %d chars\n",
384                    debugstr_an (str1, n), debugstr_an (str2, n), n);
385     return (INT32)strncmp( str1, str2, n );
386 }
387
388
389 /***********************************************************************
390  *           lstrncmp32W   (Not a Windows API)
391  */
392 INT32 WINAPI lstrncmp32W( LPCWSTR str1, LPCWSTR str2, INT32 n )
393 {
394     TRACE(string,"strncmp L%s and L%s for %d chars\n",
395                    debugstr_wn (str1, n), debugstr_wn (str2, n), n);
396     if (!n) return 0;
397     while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
398     return (INT32)(*str1 - *str2);
399 }
400
401
402 /***********************************************************************
403  *           lstrncmpi32A   (Not a Windows API)
404  */
405 INT32 WINAPI lstrncmpi32A( LPCSTR str1, LPCSTR str2, INT32 n )
406 {
407     INT32 res;
408
409     TRACE(string,"strncmpi %s and %s for %d chars\n",
410                    debugstr_an (str1, n), debugstr_an (str2, n), n);
411     if (!n) return 0;
412     while ((--n > 0) && *str1)
413       if ( (res = toupper(*str1++) - toupper(*str2++)) ) return res;
414
415     return toupper(*str1) - toupper(*str2);
416 }
417
418
419 /***********************************************************************
420  *           lstrncmpi32W   (Not a Windows API)
421  */
422 INT32 WINAPI lstrncmpi32W( LPCWSTR str1, LPCWSTR str2, INT32 n )
423 {
424     INT32 res;
425
426     TRACE(string,"strncmpi L%s and L%s for %d chars\n",
427                    debugstr_wn (str1, n), debugstr_wn (str2, n), n);
428     if (!n) return 0;
429     while ((--n > 0) && *str1)
430     {
431         if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
432         str1++;
433         str2++;
434     }
435     return towupper(*str1) - towupper(*str2);
436 }
437
438
439 /***********************************************************************
440  *           lstrcpyAtoW   (Not a Windows API)
441  */
442 LPWSTR WINAPI lstrcpyAtoW( LPWSTR dst, LPCSTR src )
443 {
444     register LPWSTR p = dst;
445
446     TRACE(string,"%s\n",src);
447
448     while ((*p++ = (WCHAR)(unsigned char)*src++));
449     return dst;
450 }
451
452
453 /***********************************************************************
454  *           lstrcpyWtoA   (Not a Windows API)
455  */
456 LPSTR WINAPI lstrcpyWtoA( LPSTR dst, LPCWSTR src )
457 {
458     register LPSTR p = dst;
459
460     TRACE(string,"L%s\n",debugstr_w(src));
461
462     while ((*p++ = (CHAR)*src++));
463     return dst;
464 }
465
466
467 /***********************************************************************
468  *           lstrcpynAtoW   (Not a Windows API)
469  * Note: this function differs from the UNIX strncpy, it _always_ writes
470  * a terminating \0
471  */
472 LPWSTR WINAPI lstrcpynAtoW( LPWSTR dst, LPCSTR src, INT32 n )
473 {
474     LPWSTR p = dst;
475
476     TRACE(string,"%s %i\n",src, n);
477
478     while ((n-- > 1) && *src) *p++ = (WCHAR)(unsigned char)*src++;
479     if (n >= 0) *p = 0;
480     return dst;
481 }
482
483
484 /***********************************************************************
485  *           lstrcpynWtoA   (Not a Windows API)
486  * Note: this function differs from the UNIX strncpy, it _always_ writes
487  * a terminating \0
488  */
489 LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT32 n )
490 {
491     LPSTR p = dst;
492
493     TRACE(string,"L%s %i\n",debugstr_w(src), n);
494
495     while ((n-- > 1) && *src) *p++ = (CHAR)*src++;
496     if (n >= 0) *p = 0;
497     return dst;
498 }
499
500
501 /***********************************************************************
502  *           Copy   (GDI.250)
503  */
504 void WINAPI Copy( LPVOID src, LPVOID dst, WORD size )
505 {
506     memcpy( dst, src, size );
507 }
508
509
510 /***********************************************************************
511  *           RtlFillMemory   (KERNEL32.441)
512  */
513 VOID WINAPI RtlFillMemory( LPVOID ptr, UINT32 len, UINT32 fill )
514 {
515     memset( ptr, fill, len );
516 }
517
518
519 /***********************************************************************
520  *           RtlMoveMemory   (KERNEL32.442)
521  */
522 VOID WINAPI RtlMoveMemory( LPVOID dst, LPCVOID src, UINT32 len )
523 {
524     memmove( dst, src, len );
525 }
526
527
528 /***********************************************************************
529  *           RtlZeroMemory   (KERNEL32.444)
530  */
531 VOID WINAPI RtlZeroMemory( LPVOID ptr, UINT32 len )
532 {
533     memset( ptr, 0, len );
534 }
535
536
537 /***********************************************************************
538  *           AnsiToOem16   (KEYBOARD.5)
539  */
540 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
541 {
542     CharToOem32A( s, d );
543     return -1;
544 }
545
546
547 /***********************************************************************
548  *           OemToAnsi16   (KEYBOARD.6)
549  */
550 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
551 {
552     OemToChar32A( s, d );
553     return -1;
554 }
555
556
557 /***********************************************************************
558  *           AnsiToOemBuff16   (KEYBOARD.134)
559  */
560 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
561 {
562     CharToOemBuff32A( s, d, len ? len : 65536 );
563 }
564
565
566 /***********************************************************************
567  *           OemToAnsiBuff16   (KEYBOARD.135)
568  */
569 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
570 {
571     OemToCharBuff32A( s, d, len ? len : 65536 );
572 }
573
574
575 /***********************************************************************
576  *           CharToOem32A   (USER32.37)
577  */
578 BOOL32 WINAPI CharToOem32A( LPCSTR s, LPSTR d )
579 {
580     LPSTR oldd = d;
581     if (!s || !d) return TRUE;
582     TRACE(string,"CharToOem %s\n", debugstr_a (s));
583     while ((*d++ = ANSI_TO_OEM(*s++)));
584     TRACE(string,"       to %s\n", debugstr_a (oldd));
585     return TRUE;
586 }
587
588
589 /***********************************************************************
590  *           CharToOemBuff32A   (USER32.38)
591  */
592 BOOL32 WINAPI CharToOemBuff32A( LPCSTR s, LPSTR d, DWORD len )
593 {
594     while (len--) *d++ = ANSI_TO_OEM(*s++);
595     return TRUE;
596 }
597
598
599 /***********************************************************************
600  *           CharToOemBuff32W   (USER32.39)
601  */
602 BOOL32 WINAPI CharToOemBuff32W( LPCWSTR s, LPSTR d, DWORD len )
603 {
604     while (len--) *d++ = ANSI_TO_OEM(*s++);
605     return TRUE;
606 }
607
608
609 /***********************************************************************
610  *           CharToOem32W   (USER32.40)
611  */
612 BOOL32 WINAPI CharToOem32W( LPCWSTR s, LPSTR d )
613 {
614     LPSTR oldd = d;
615     if (!s || !d) return TRUE;
616     TRACE(string,"CharToOem L%s\n", debugstr_w (s));
617     while ((*d++ = ANSI_TO_OEM(*s++)));
618     TRACE(string,"       to %s\n", debugstr_a (oldd));
619     return TRUE;
620 }
621
622
623 /***********************************************************************
624  *           OemToChar32A   (USER32.402)
625  */
626 BOOL32 WINAPI OemToChar32A( LPCSTR s, LPSTR d )
627 {
628     LPSTR oldd = d;
629     TRACE(string,"OemToChar %s\n", debugstr_a (s));
630     while ((*d++ = OEM_TO_ANSI(*s++)));
631     TRACE(string,"       to %s\n", debugstr_a (oldd));
632     return TRUE;
633 }
634
635
636 /***********************************************************************
637  *           OemToCharBuff32A   (USER32.403)
638  */
639 BOOL32 WINAPI OemToCharBuff32A( LPCSTR s, LPSTR d, DWORD len )
640 {
641     TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
642     while (len--) *d++ = OEM_TO_ANSI(*s++);
643     return TRUE;
644 }
645
646
647 /***********************************************************************
648  *           OemToCharBuff32W   (USER32.404)
649  */
650 BOOL32 WINAPI OemToCharBuff32W( LPCSTR s, LPWSTR d, DWORD len )
651 {
652     TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
653     while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
654     return TRUE;
655 }
656
657
658 /***********************************************************************
659  *           OemToChar32W   (USER32.405)
660  */
661 BOOL32 WINAPI OemToChar32W( LPCSTR s, LPWSTR d )
662 {
663     while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++)));
664     return TRUE;
665 }
666
667 /***********************************************************************
668  *  WideCharToLocal (Not a Windows API)
669  *  similar lstrcpyWtoA, should handle codepages properly
670  *
671  *  RETURNS
672  *    strlen of the destination string
673  */
674  
675 INT32 WINAPI WideCharToLocal32(
676     LPSTR pLocal, 
677                 LPWSTR pWide, 
678                 INT32 dwChars)
679 { *pLocal = 0;
680   TRACE(string,"(%p, %s, %i)\n",        pLocal, debugstr_w(pWide),dwChars);
681   WideCharToMultiByte(CP_ACP,0,pWide,-1,pLocal,dwChars,NULL,NULL);
682   return strlen(pLocal);
683 }
684 /***********************************************************************
685  *  LocalToWideChar (Not a Windows API)
686  *  similar lstrcpyAtoW, should handle codepages properly
687  *
688  *  RETURNS
689  *    strlen of the destination string
690  */
691 INT32 WINAPI LocalToWideChar32(
692     LPWSTR pWide, 
693                 LPSTR pLocal, 
694                 INT32 dwChars)
695 { *pWide = 0;
696   TRACE(string,"(%p, %s, %i)\n",pWide,  pLocal, dwChars);
697         MultiByteToWideChar(CP_ACP,0,pLocal,-1,pWide,dwChars); 
698   return lstrlen32W(pWide);
699 }
700
701
702 /***********************************************************************
703  *           lstrrchr   (Not a Windows API)
704  *
705  * This is the implementation meant to be invoked form within
706  * COMCTL32_StrRChrA and shell32(TODO)...
707  *
708  * Return a pointer to the last occurence of wMatch in lpStart
709  * not looking further than lpEnd...
710  */
711 LPSTR WINAPI lstrrchr( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
712 {
713   LPCSTR lpGotIt = NULL;
714
715   TRACE(string,"(%s, %s)\n", lpStart, lpEnd);
716
717   if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
718
719   for(; lpStart < lpEnd; lpStart = CharNext32A(lpStart)) 
720     if (!ChrCmpA( GET_WORD(lpStart), wMatch)) 
721       lpGotIt = lpStart;
722     
723   return ((LPSTR)lpGotIt);
724 }
725
726 /***********************************************************************
727  *           lstrrchrw    (Not a Windows API)
728  *
729  * This is the implementation meant to be invoked form within
730  * COMCTL32_StrRChrW and shell32(TODO)...
731  *
732  * Return a pointer to the last occurence of wMatch in lpStart
733  * not looking further than lpEnd...
734  */  
735 LPWSTR WINAPI lstrrchrw( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch )
736 {
737   LPCWSTR lpGotIt = NULL;
738
739   TRACE(string,"(%p, %p, %c)\n", lpStart,      lpEnd, wMatch);
740   if (!lpEnd) lpEnd = lpStart + lstrlen32W(lpStart);
741
742   for(; lpStart < lpEnd; lpStart = CharNext32W(lpStart)) 
743     if (!ChrCmpW( GET_WORD(lpStart), wMatch)) 
744       lpGotIt = lpStart;
745     
746   return (LPWSTR)lpGotIt;
747 }
748
749 /***********************************************************************
750  *           strstrw   (Not a Windows API)
751  *
752  * This is the implementation meant to be invoked form within
753  * COMCTL32_StrStrW and shell32(TODO)...
754  *
755  */
756 LPWSTR WINAPI strstrw( LPCWSTR lpFirst, LPCWSTR lpSrch) {
757   UINT32 uSrchLen  = (UINT32)lstrlen32W(lpSrch);
758   WORD wMatchBeg   = *(WORD*)lpSrch;
759
760   TRACE(string,"(%p, %p)\n", lpFirst,  lpSrch);
761
762   for(; 
763     ((lpFirst=CRTDLL_wcschr(lpFirst, wMatchBeg))!=0) && 
764       lstrncmp32W(lpFirst, lpSrch, uSrchLen); 
765     lpFirst++) {
766       continue;
767   }
768   return (LPWSTR)lpFirst;
769 }
770
771
772 /***********************************************************************
773  *           ChrCmpA   
774  * This fuction returns FALSE if both words match, TRUE otherwise...
775  */
776 static BOOL32 ChrCmpA( WORD word1, WORD word2) {
777   if (LOBYTE(word1) == LOBYTE(word2)) {
778     if (IsDBCSLeadByte32(LOBYTE(word1))) {
779       return (word1 != word2);
780     }
781     return FALSE;
782   }
783   return TRUE;
784 }
785
786 /***********************************************************************
787  *           ChrCmpW   
788  * This fuction returns FALSE if both words match, TRUE otherwise...
789  */
790 static BOOL32 ChrCmpW( WORD word1, WORD word2) {
791   return (word1 != word2);
792 }
793
794