Francois Methot (Macadamian)
[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
11 #include "windef.h"
12 #include "winbase.h"
13 #include "wingdi.h"
14 #include "winuser.h"
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "wine/keyboard16.h"
18 #include "wine/exception.h"
19 #include "winerror.h"
20 #include "crtdll.h"
21 #include "ldt.h"
22 #include "debugtools.h"
23 #include "winnls.h"
24
25 DEFAULT_DEBUG_CHANNEL(string)
26
27 static const BYTE STRING_Oem2Ansi[256] =
28 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\244"
29 "\020\021\022\023\266\247\026\027\030\031\032\033\034\035\036\037"
30 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
31 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
32 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
33 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
34 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
35 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
36 "\307\374\351\342\344\340\345\347\352\353\350\357\356\354\304\305"
37 "\311\346\306\364\366\362\373\371\377\326\334\242\243\245\120\203"
38 "\341\355\363\372\361\321\252\272\277\137\254\275\274\241\253\273"
39 "\137\137\137\246\246\246\246\053\053\246\246\053\053\053\053\053"
40 "\053\055\055\053\055\053\246\246\053\053\055\055\246\055\053\055"
41 "\055\055\055\053\053\053\053\053\053\053\053\137\137\246\137\137"
42 "\137\337\137\266\137\137\265\137\137\137\137\137\137\137\137\137"
43 "\137\261\137\137\137\137\367\137\260\225\267\137\156\262\137\137";
44
45 static const BYTE STRING_Ansi2Oem[256] =
46 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
47 "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
48 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
49 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
50 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
51 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
52 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
53 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
54 "\200\201\054\237\054\137\375\374\210\045\123\074\117\215\216\217"
55 "\220\140\047\042\042\371\055\137\230\231\163\076\157\235\236\131"
56 "\040\255\233\234\017\235\335\025\042\143\246\256\252\055\162\137"
57 "\370\361\375\063\047\346\024\372\054\061\247\257\254\253\137\250"
58 "\101\101\101\101\216\217\222\200\105\220\105\105\111\111\111\111"
59 "\104\245\117\117\117\117\231\170\117\125\125\125\232\131\137\341"
60 "\205\240\203\141\204\206\221\207\212\202\210\211\215\241\214\213"
61 "\144\244\225\242\223\157\224\366\157\227\243\226\201\171\137\230";
62
63 #define OEM_TO_ANSI(ch) (STRING_Oem2Ansi[(unsigned char)(ch)])
64 #define ANSI_TO_OEM(ch) (STRING_Ansi2Oem[(unsigned char)(ch)])
65
66 /* Internaly used by strchr family functions */
67 static BOOL ChrCmpA( WORD word1, WORD word2);
68
69
70 /* filter for page-fault exceptions */
71 static WINE_EXCEPTION_FILTER(page_fault)
72 {
73     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
74         return EXCEPTION_EXECUTE_HANDLER;
75     return EXCEPTION_CONTINUE_SEARCH;
76 }
77
78
79 /***********************************************************************
80  *           hmemcpy   (KERNEL.348)
81  */
82 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
83 {
84     memcpy( dst, src, count );
85 }
86
87
88 /***********************************************************************
89  *           lstrcat16   (KERNEL.89)
90  */
91 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
92 {
93     /* Windows does not check for NULL pointers here, so we don't either */
94     strcat( (LPSTR)PTR_SEG_TO_LIN(dst), src );
95     return dst;
96 }
97
98
99 /***********************************************************************
100  *           lstrcatA   (KERNEL32.599)
101  */
102 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
103 {
104     __TRY
105     {
106         strcat( dst, src );
107     }
108     __EXCEPT(page_fault)
109     {
110         SetLastError( ERROR_INVALID_PARAMETER );
111         return NULL;
112     }
113     __ENDTRY
114     return dst;
115 }
116
117
118 /***********************************************************************
119  *           lstrcatW   (KERNEL32.600)
120  */
121 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
122 {
123     __TRY
124     {
125         CRTDLL_wcscat( dst, src );
126     }
127     __EXCEPT(page_fault)
128     {
129         SetLastError( ERROR_INVALID_PARAMETER );
130         return NULL;
131     }
132     __ENDTRY
133     return dst;
134 }
135
136
137 /***********************************************************************
138  *           lstrcatn16   (KERNEL.352)
139  */
140 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
141 {
142     LPSTR p = (LPSTR)PTR_SEG_TO_LIN(dst);
143
144     while (*p) p++;
145     if ((n -= (p - (LPSTR)PTR_SEG_TO_LIN(dst))) <= 0) return dst;
146     lstrcpynA( p, src, n );
147     return dst;
148 }
149
150
151 /***********************************************************************
152  *           lstrcmp16   (USER.430)
153  */
154 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
155 {
156     return (INT16)strcmp( str1, str2 );
157 }
158
159
160 /***********************************************************************
161  *           lstrcmpA   (KERNEL.602)
162  */
163 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
164 {
165     return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
166 }
167
168
169 /***********************************************************************
170  *           lstrcmpW   (KERNEL.603)
171  * FIXME : should call CompareStringW, when it is implemented.
172  *    This implementation is not "word sort", as it should.
173  */
174 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
175 {
176     TRACE("%s and %s\n",
177                    debugstr_w (str1), debugstr_w (str2));
178     if (!str1 || !str2) {
179         SetLastError(ERROR_INVALID_PARAMETER);
180         return 0;
181     }
182     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
183     return (INT)(*str1 - *str2);
184 }
185
186
187 /***********************************************************************
188  *           lstrcmpi16   (USER.471)
189  */
190 INT16 WINAPI lstrcmpi16( LPCSTR str1, LPCSTR str2 )
191 {
192     return (INT16)lstrcmpiA( str1, str2 );
193 }
194
195
196 /***********************************************************************
197  *           lstrcmpiA   (KERNEL32.605)
198  */
199 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
200 {    TRACE("strcmpi %s and %s\n",
201                    debugstr_a (str1), debugstr_a (str2));
202     return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
203 }
204
205
206 /***********************************************************************
207  *           lstrcmpiW   (KERNEL32.606)
208  */
209 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
210 {
211     INT res;
212
213 #if 0
214     /* Too much!  (From registry loading.)  */
215     TRACE("strcmpi %s and %s\n",
216                    debugstr_w (str1), debugstr_w (str2));
217 #endif
218     if (!str1 || !str2) {
219         SetLastError(ERROR_INVALID_PARAMETER);
220         return 0;
221     }
222     while (*str1)
223     {
224         if ((*str1<0x100 ) && (*str2<0x100)) {
225             if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
226         } else {
227             if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
228         }
229         str1++;
230         str2++;
231     }
232     return towupper(*str1) - towupper(*str2);
233 }
234
235
236 /***********************************************************************
237  *           lstrcpy16   (KERNEL.88)
238  */
239 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
240 {
241     if (!lstrcpyA( PTR_SEG_TO_LIN(dst), src )) dst = 0;
242     return dst;
243 }
244
245
246 /***********************************************************************
247  *           lstrcpyA   (KERNEL32.608)
248  */
249 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
250 {
251     __TRY
252     {
253         /* this is how Windows does it */
254         memmove( dst, src, strlen(src)+1 );
255     }
256     __EXCEPT(page_fault)
257     {
258         ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
259         SetLastError( ERROR_INVALID_PARAMETER );
260         return NULL;
261     }
262     __ENDTRY
263     return dst;
264 }
265
266
267 /***********************************************************************
268  *           lstrcpyW   (KERNEL32.609)
269  */
270 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
271 {
272     __TRY
273     {
274         CRTDLL_wcscpy( dst, src );
275     }
276     __EXCEPT(page_fault)
277     {
278         SetLastError( ERROR_INVALID_PARAMETER );
279         return NULL;
280     }
281     __ENDTRY
282     return dst;
283 }
284
285
286 /***********************************************************************
287  *           lstrcpyn16   (KERNEL.353)
288  */
289 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
290 {
291     lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
292     return dst;
293 }
294
295
296 /***********************************************************************
297  *           lstrcpynA   (KERNEL32.611)
298  * Note: this function differs from the UNIX strncpy, it _always_ writes
299  * a terminating \0
300  */
301 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
302 {
303     LPSTR p = dst;
304     TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
305     /* In real windows the whole function is protected by an exception handler
306      * that returns ERROR_INVALID_PARAMETER on faulty parameters
307      * We currently just check for NULL.
308      */
309     if (!dst || !src) {
310         SetLastError(ERROR_INVALID_PARAMETER);
311         return 0;
312     }
313     while ((n-- > 1) && *src) *p++ = *src++;
314     if (n >= 0) *p = 0;
315     return dst;
316 }
317
318
319 /***********************************************************************
320  *           lstrcpynW   (KERNEL32.612)
321  * Note: this function differs from the UNIX strncpy, it _always_ writes
322  * a terminating \0
323  */
324 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
325 {
326     LPWSTR p = dst;
327     TRACE("(%p, %s, %i)\n", dst,  debugstr_wn(src,n), n);
328     /* In real windows the whole function is protected by an exception handler
329      * that returns ERROR_INVALID_PARAMETER on faulty parameters
330      * We currently just check for NULL.
331      */
332     if (!dst || !src) {
333         SetLastError(ERROR_INVALID_PARAMETER);
334         return 0;
335     }
336     while ((n-- > 1) && *src) *p++ = *src++;
337     if (n >= 0) *p = 0;
338     return dst;
339 }
340
341
342 /***********************************************************************
343  *           lstrlen16   (KERNEL.90)
344  */
345 INT16 WINAPI lstrlen16( LPCSTR str )
346 {
347     return (INT16)lstrlenA( str );
348 }
349
350
351 /***********************************************************************
352  *           lstrlenA   (KERNEL32.614)
353  */
354 INT WINAPI lstrlenA( LPCSTR str )
355 {
356     INT ret;
357     __TRY
358     {
359         ret = strlen(str);
360     }
361     __EXCEPT(page_fault)
362     {
363         SetLastError( ERROR_INVALID_PARAMETER );
364         return 0;
365     }
366     __ENDTRY
367     return ret;
368 }
369
370
371 /***********************************************************************
372  *           lstrlenW   (KERNEL32.615)
373  */
374 INT WINAPI lstrlenW( LPCWSTR str )
375 {
376     INT ret;
377     __TRY
378     {
379         ret = CRTDLL_wcslen(str);
380     }
381     __EXCEPT(page_fault)
382     {
383         SetLastError( ERROR_INVALID_PARAMETER );
384         return 0;
385     }
386     __ENDTRY
387     return ret;
388 }
389
390
391 /***********************************************************************
392  *           lstrcpyAtoW   (Not a Windows API)
393  */
394 LPWSTR WINAPI lstrcpyAtoW( LPWSTR dst, LPCSTR src )
395 {
396     register LPWSTR p = dst;
397
398     TRACE("(%p, %s)\n", dst, debugstr_a(src));
399
400     while ((*p++ = (WCHAR)(unsigned char)*src++));
401     return dst;
402 }
403
404
405 /***********************************************************************
406  *           lstrcpyWtoA   (Not a Windows API)
407  */
408 LPSTR WINAPI lstrcpyWtoA( LPSTR dst, LPCWSTR src )
409 {
410     register LPSTR p = dst;
411
412     TRACE("(%p, %s)\n", dst, debugstr_w(src));
413
414     while ((*p++ = (CHAR)*src++));
415     return dst;
416 }
417
418
419 /***********************************************************************
420  *           lstrcpynAtoW   (Not a Windows API)
421  * Note: this function differs from the UNIX strncpy, it _always_ writes
422  * a terminating \0
423  */
424 LPWSTR WINAPI lstrcpynAtoW( LPWSTR dst, LPCSTR src, INT n )
425 {
426     LPWSTR p = dst;
427
428     TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
429
430     while ((n-- > 1) && *src) *p++ = (WCHAR)(unsigned char)*src++;
431     if (n >= 0) *p = 0;
432     return dst;
433 }
434
435
436 /***********************************************************************
437  *           lstrcpynWtoA   (Not a Windows API)
438  * Note: this function differs from the UNIX strncpy, it _always_ writes
439  * a terminating \0
440  *
441  * The terminating zero should be written at the end of the string, not
442  * the end of the buffer, as some programs specify the wrong size for 
443  * the buffer (eg. winnt's sol.exe)
444  */
445 LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT n )
446 {
447     if (--n >= 0)
448     {
449         TRACE("(%p, %s, %i)\n", dst, debugstr_wn(src,n), n);
450         n = CRTDLL_wcstombs( dst, src, n );
451         if(n<0)
452                  n=0;
453         dst[n] = 0;
454     }
455     return dst;
456 }
457
458 /***********************************************************************
459  *           UnicodeToAnsi   (KERNEL.434)
460  */
461 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
462 {
463     if ( codepage != -1 )
464         FIXME("codepage %d not supported\n", codepage );
465
466     lstrcpyWtoA( dst, src );
467
468     return (INT16)lstrlenA( dst );
469 }
470
471
472 /***********************************************************************
473  *           Copy   (GDI.250)
474  */
475 void WINAPI Copy16( LPVOID src, LPVOID dst, WORD size )
476 {
477     memcpy( dst, src, size );
478 }
479
480 /***********************************************************************
481  *           AnsiToOem16   (KEYBOARD.5)
482  */
483 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
484 {
485     CharToOemA( s, d );
486     return -1;
487 }
488
489
490 /***********************************************************************
491  *           OemToAnsi16   (KEYBOARD.6)
492  */
493 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
494 {
495     OemToCharA( s, d );
496     return -1;
497 }
498
499
500 /***********************************************************************
501  *           AnsiToOemBuff16   (KEYBOARD.134)
502  */
503 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
504 {
505     if (len != 0) CharToOemBuffA( s, d, len );
506 }
507
508
509 /***********************************************************************
510  *           OemToAnsiBuff16   (KEYBOARD.135)
511  */
512 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
513 {
514     if (len != 0) OemToCharBuffA( s, d, len );
515 }
516
517
518 /***********************************************************************
519  *           CharToOemA   (USER32.37)
520  */
521 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
522 {
523     LPSTR oldd = d;
524     if (!s || !d) return TRUE;
525     TRACE("CharToOem %s\n", debugstr_a (s));
526     while ((*d++ = ANSI_TO_OEM(*s++)));
527     TRACE("       to %s\n", debugstr_a (oldd));
528     return TRUE;
529 }
530
531
532 /***********************************************************************
533  *           CharToOemBuffA   (USER32.38)
534  */
535 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
536 {
537     while (len--) *d++ = ANSI_TO_OEM(*s++);
538     return TRUE;
539 }
540
541
542 /***********************************************************************
543  *           CharToOemBuffW   (USER32.39)
544  */
545 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
546 {
547     while (len--) *d++ = ANSI_TO_OEM(*s++);
548     return TRUE;
549 }
550
551
552 /***********************************************************************
553  *           CharToOemW   (USER32.40)
554  */
555 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
556 {
557     LPSTR oldd = d;
558     if (!s || !d) return TRUE;
559     TRACE("CharToOem %s\n", debugstr_w (s));
560     while ((*d++ = ANSI_TO_OEM(*s++)));
561     TRACE("       to %s\n", debugstr_a (oldd));
562     return TRUE;
563 }
564
565
566 /***********************************************************************
567  *           OemToCharA   (USER32.402)
568  */
569 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
570 {
571     LPSTR oldd = d;
572     TRACE("OemToChar %s\n", debugstr_a (s));
573     while ((*d++ = OEM_TO_ANSI(*s++)));
574     TRACE("       to %s\n", debugstr_a (oldd));
575     return TRUE;
576 }
577
578
579 /***********************************************************************
580  *           OemToCharBuffA   (USER32.403)
581  */
582 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
583 {
584     TRACE("OemToCharBuff %s\n", debugstr_an (s, len));
585     while (len--) *d++ = OEM_TO_ANSI(*s++);
586     return TRUE;
587 }
588
589
590 /***********************************************************************
591  *           OemToCharBuffW   (USER32.404)
592  */
593 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
594 {
595     TRACE("OemToCharBuff %s\n", debugstr_an (s, len));
596     while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
597     return TRUE;
598 }
599
600
601 /***********************************************************************
602  *           OemToCharW   (USER32.405)
603  */
604 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
605 {
606     while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++)));
607     return TRUE;
608 }
609
610 /***********************************************************************
611  *           lstrrchr   (Not a Windows API)
612  *
613  * This is the implementation meant to be invoked from within
614  * COMCTL32_StrRChrA and shell32(TODO)...
615  *
616  * Return a pointer to the last occurence of wMatch in lpStart
617  * not looking further than lpEnd...
618  */
619 LPSTR WINAPI lstrrchr( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
620 {
621   LPCSTR lpGotIt = NULL;
622
623   TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
624
625   if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
626
627   for(; lpStart < lpEnd; lpStart = CharNextA(lpStart)) 
628     if (!ChrCmpA( GET_WORD(lpStart), wMatch)) 
629       lpGotIt = lpStart;
630     
631   return ((LPSTR)lpGotIt);
632 }
633
634 /***********************************************************************
635  *           ChrCmpW   
636  * This fuction returns FALSE if both words match, TRUE otherwise...
637  */
638 static BOOL ChrCmpW( WORD word1, WORD word2) {
639   return (word1 != word2);
640 }
641
642 /***********************************************************************
643  *           lstrrchrw    (Not a Windows API)
644  *
645  * This is the implementation meant to be invoked form within
646  * COMCTL32_StrRChrW and shell32(TODO)...
647  *
648  * Return a pointer to the last occurence of wMatch in lpStart
649  * not looking further than lpEnd...
650  */  
651 LPWSTR WINAPI lstrrchrw( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch )
652 {
653   LPCWSTR lpGotIt = NULL;
654
655   TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
656   if (!lpEnd) lpEnd = lpStart + lstrlenW(lpStart);
657
658   for(; lpStart < lpEnd; lpStart = CharNextW(lpStart)) 
659     if (!ChrCmpW( GET_WORD(lpStart), wMatch)) 
660       lpGotIt = lpStart;
661     
662   return (LPWSTR)lpGotIt;
663 }
664
665 /***********************************************************************
666  *           ChrCmpA   
667  * This fuction returns FALSE if both words match, TRUE otherwise...
668  */
669 static BOOL ChrCmpA( WORD word1, WORD word2) {
670   if (LOBYTE(word1) == LOBYTE(word2)) {
671     if (IsDBCSLeadByte(LOBYTE(word1))) {
672       return (word1 != word2);
673     }
674     return FALSE;
675   }
676   return TRUE;
677 }