4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "wine/keyboard16.h"
18 #include "wine/exception.h"
22 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(string)
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";
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";
63 #define OEM_TO_ANSI(ch) (STRING_Oem2Ansi[(unsigned char)(ch)])
64 #define ANSI_TO_OEM(ch) (STRING_Ansi2Oem[(unsigned char)(ch)])
66 /* Internaly used by strchr family functions */
67 static BOOL ChrCmpA( WORD word1, WORD word2);
70 /* filter for page-fault exceptions */
71 static WINE_EXCEPTION_FILTER(page_fault)
73 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
74 return EXCEPTION_EXECUTE_HANDLER;
75 return EXCEPTION_CONTINUE_SEARCH;
79 /***********************************************************************
80 * hmemcpy (KERNEL.348)
82 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
84 memcpy( dst, src, count );
88 /***********************************************************************
89 * lstrcat16 (KERNEL.89)
91 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
93 /* Windows does not check for NULL pointers here, so we don't either */
94 strcat( (LPSTR)PTR_SEG_TO_LIN(dst), src );
99 /***********************************************************************
100 * lstrcatA (KERNEL32.599)
102 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
110 SetLastError( ERROR_INVALID_PARAMETER );
118 /***********************************************************************
119 * lstrcatW (KERNEL32.600)
121 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
125 CRTDLL_wcscat( dst, src );
129 SetLastError( ERROR_INVALID_PARAMETER );
137 /***********************************************************************
138 * lstrcatn16 (KERNEL.352)
140 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
142 LPSTR p = (LPSTR)PTR_SEG_TO_LIN(dst);
145 if ((n -= (p - (LPSTR)PTR_SEG_TO_LIN(dst))) <= 0) return dst;
146 lstrcpynA( p, src, n );
151 /***********************************************************************
152 * lstrcmp16 (USER.430)
154 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
156 return (INT16)strcmp( str1, str2 );
160 /***********************************************************************
161 * lstrcmp32A (KERNEL.602)
163 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
165 return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
169 /***********************************************************************
170 * lstrcmp32W (KERNEL.603)
171 * FIXME : should call CompareString32W, when it is implemented.
172 * This implementation is not "word sort", as it should.
174 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
177 debugstr_w (str1), debugstr_w (str2));
178 if (!str1 || !str2) {
179 SetLastError(ERROR_INVALID_PARAMETER);
182 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
183 return (INT)(*str1 - *str2);
187 /***********************************************************************
188 * lstrcmpi16 (USER.471)
190 INT16 WINAPI lstrcmpi16( LPCSTR str1, LPCSTR str2 )
192 return (INT16)lstrcmpiA( str1, str2 );
196 /***********************************************************************
197 * lstrcmpi32A (KERNEL32.605)
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;
206 /***********************************************************************
207 * lstrcmpi32W (KERNEL32.606)
209 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
214 /* Too much! (From registry loading.) */
215 TRACE("strcmpi %s and %s\n",
216 debugstr_w (str1), debugstr_w (str2));
218 if (!str1 || !str2) {
219 SetLastError(ERROR_INVALID_PARAMETER);
224 if ((*str1<0x100 ) && (*str2<0x100)) {
225 if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
227 if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
232 return towupper(*str1) - towupper(*str2);
236 /***********************************************************************
237 * lstrcpy16 (KERNEL.88)
239 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
241 /* this is how Windows does it */
242 memmove( (LPSTR)PTR_SEG_TO_LIN(dst), src, strlen(src)+1 );
247 /***********************************************************************
248 * lstrcpyA (KERNEL32.608)
250 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
254 /* this is how Windows does it */
255 memmove( dst, src, strlen(src)+1 );
259 SetLastError( ERROR_INVALID_PARAMETER );
267 /***********************************************************************
268 * lstrcpyW (KERNEL32.609)
270 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
274 CRTDLL_wcscpy( dst, src );
278 SetLastError( ERROR_INVALID_PARAMETER );
286 /***********************************************************************
287 * lstrcpyn16 (KERNEL.353)
289 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
291 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
296 /***********************************************************************
297 * lstrcpyn32A (KERNEL32.611)
298 * Note: this function differs from the UNIX strncpy, it _always_ writes
301 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
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.
310 SetLastError(ERROR_INVALID_PARAMETER);
313 while ((n-- > 1) && *src) *p++ = *src++;
319 /***********************************************************************
320 * lstrcpyn32W (KERNEL32.612)
321 * Note: this function differs from the UNIX strncpy, it _always_ writes
324 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
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.
333 SetLastError(ERROR_INVALID_PARAMETER);
336 while ((n-- > 1) && *src) *p++ = *src++;
342 /***********************************************************************
343 * lstrlen16 (KERNEL.90)
345 INT16 WINAPI lstrlen16( LPCSTR str )
347 return (INT16)lstrlenA( str );
351 /***********************************************************************
352 * lstrlenA (KERNEL32.614)
354 INT WINAPI lstrlenA( LPCSTR str )
363 SetLastError( ERROR_INVALID_PARAMETER );
371 /***********************************************************************
372 * lstrlenW (KERNEL32.615)
374 INT WINAPI lstrlenW( LPCWSTR str )
379 ret = CRTDLL_wcslen(str);
383 SetLastError( ERROR_INVALID_PARAMETER );
391 /***********************************************************************
392 * lstrcpyAtoW (Not a Windows API)
394 LPWSTR WINAPI lstrcpyAtoW( LPWSTR dst, LPCSTR src )
396 register LPWSTR p = dst;
398 TRACE("(%p, %s)\n", dst, debugstr_a(src));
400 while ((*p++ = (WCHAR)(unsigned char)*src++));
405 /***********************************************************************
406 * lstrcpyWtoA (Not a Windows API)
408 LPSTR WINAPI lstrcpyWtoA( LPSTR dst, LPCWSTR src )
410 register LPSTR p = dst;
412 TRACE("(%p, %s)\n", dst, debugstr_w(src));
414 while ((*p++ = (CHAR)*src++));
419 /***********************************************************************
420 * lstrcpynAtoW (Not a Windows API)
421 * Note: this function differs from the UNIX strncpy, it _always_ writes
424 LPWSTR WINAPI lstrcpynAtoW( LPWSTR dst, LPCSTR src, INT n )
428 TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
430 while ((n-- > 1) && *src) *p++ = (WCHAR)(unsigned char)*src++;
436 /***********************************************************************
437 * lstrcpynWtoA (Not a Windows API)
438 * Note: this function differs from the UNIX strncpy, it _always_ writes
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)
445 LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT n )
449 TRACE("(%p, %s, %i)\n", dst, debugstr_wn(src,n), n);
450 n = CRTDLL_wcstombs( dst, src, n );
458 /***********************************************************************
459 * UnicodeToAnsi (KERNEL.434)
461 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
463 if ( codepage != -1 )
464 FIXME("codepage %d not supported\n", codepage );
466 lstrcpyWtoA( dst, src );
468 return (INT16)lstrlenA( dst );
472 /***********************************************************************
475 void WINAPI Copy16( LPVOID src, LPVOID dst, WORD size )
477 memcpy( dst, src, size );
481 /***********************************************************************
482 * RtlFillMemory (KERNEL32.441)
484 VOID WINAPI RtlFillMemory( LPVOID ptr, UINT len, UINT fill )
486 memset( ptr, fill, len );
490 /***********************************************************************
491 * RtlMoveMemory (KERNEL32.442)
493 VOID WINAPI RtlMoveMemory( LPVOID dst, LPCVOID src, UINT len )
495 memmove( dst, src, len );
499 /***********************************************************************
500 * RtlZeroMemory (KERNEL32.444)
502 VOID WINAPI RtlZeroMemory( LPVOID ptr, UINT len )
504 memset( ptr, 0, len );
508 /***********************************************************************
509 * AnsiToOem16 (KEYBOARD.5)
511 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
518 /***********************************************************************
519 * OemToAnsi16 (KEYBOARD.6)
521 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
528 /***********************************************************************
529 * AnsiToOemBuff16 (KEYBOARD.134)
531 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
533 if (len != 0) CharToOemBuffA( s, d, len );
537 /***********************************************************************
538 * OemToAnsiBuff16 (KEYBOARD.135)
540 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
542 if (len != 0) OemToCharBuffA( s, d, len );
546 /***********************************************************************
547 * CharToOem32A (USER32.37)
549 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
552 if (!s || !d) return TRUE;
553 TRACE("CharToOem %s\n", debugstr_a (s));
554 while ((*d++ = ANSI_TO_OEM(*s++)));
555 TRACE(" to %s\n", debugstr_a (oldd));
560 /***********************************************************************
561 * CharToOemBuff32A (USER32.38)
563 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
565 while (len--) *d++ = ANSI_TO_OEM(*s++);
570 /***********************************************************************
571 * CharToOemBuff32W (USER32.39)
573 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
575 while (len--) *d++ = ANSI_TO_OEM(*s++);
580 /***********************************************************************
581 * CharToOem32W (USER32.40)
583 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
586 if (!s || !d) return TRUE;
587 TRACE("CharToOem %s\n", debugstr_w (s));
588 while ((*d++ = ANSI_TO_OEM(*s++)));
589 TRACE(" to %s\n", debugstr_a (oldd));
594 /***********************************************************************
595 * OemToChar32A (USER32.402)
597 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
600 TRACE("OemToChar %s\n", debugstr_a (s));
601 while ((*d++ = OEM_TO_ANSI(*s++)));
602 TRACE(" to %s\n", debugstr_a (oldd));
607 /***********************************************************************
608 * OemToCharBuff32A (USER32.403)
610 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
612 TRACE("OemToCharBuff %s\n", debugstr_an (s, len));
613 while (len--) *d++ = OEM_TO_ANSI(*s++);
618 /***********************************************************************
619 * OemToCharBuff32W (USER32.404)
621 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
623 TRACE("OemToCharBuff %s\n", debugstr_an (s, len));
624 while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
629 /***********************************************************************
630 * OemToChar32W (USER32.405)
632 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
634 while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++)));
638 /***********************************************************************
639 * lstrrchr (Not a Windows API)
641 * This is the implementation meant to be invoked from within
642 * COMCTL32_StrRChrA and shell32(TODO)...
644 * Return a pointer to the last occurence of wMatch in lpStart
645 * not looking further than lpEnd...
647 LPSTR WINAPI lstrrchr( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
649 LPCSTR lpGotIt = NULL;
651 TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
653 if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
655 for(; lpStart < lpEnd; lpStart = CharNextA(lpStart))
656 if (!ChrCmpA( GET_WORD(lpStart), wMatch))
659 return ((LPSTR)lpGotIt);
662 /***********************************************************************
664 * This fuction returns FALSE if both words match, TRUE otherwise...
666 static BOOL ChrCmpW( WORD word1, WORD word2) {
667 return (word1 != word2);
670 /***********************************************************************
671 * lstrrchrw (Not a Windows API)
673 * This is the implementation meant to be invoked form within
674 * COMCTL32_StrRChrW and shell32(TODO)...
676 * Return a pointer to the last occurence of wMatch in lpStart
677 * not looking further than lpEnd...
679 LPWSTR WINAPI lstrrchrw( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch )
681 LPCWSTR lpGotIt = NULL;
683 TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
684 if (!lpEnd) lpEnd = lpStart + lstrlenW(lpStart);
686 for(; lpStart < lpEnd; lpStart = CharNextW(lpStart))
687 if (!ChrCmpW( GET_WORD(lpStart), wMatch))
690 return (LPWSTR)lpGotIt;
693 /***********************************************************************
695 * This fuction returns FALSE if both words match, TRUE otherwise...
697 static BOOL ChrCmpA( WORD word1, WORD word2) {
698 if (LOBYTE(word1) == LOBYTE(word2)) {
699 if (IsDBCSLeadByte(LOBYTE(word1))) {
700 return (word1 != word2);