4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
11 #include "wine/winbase16.h"
12 #include "wine/winuser16.h"
15 #include "wine/keyboard16.h"
16 #include "wine/exception.h"
20 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(string)
25 static const BYTE STRING_Oem2Ansi[256] =
26 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\244"
27 "\020\021\022\023\266\247\026\027\030\031\032\033\034\035\036\037"
28 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
29 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
30 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
31 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
32 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
33 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
34 "\307\374\351\342\344\340\345\347\352\353\350\357\356\354\304\305"
35 "\311\346\306\364\366\362\373\371\377\326\334\242\243\245\120\203"
36 "\341\355\363\372\361\321\252\272\277\137\254\275\274\241\253\273"
37 "\137\137\137\246\246\246\246\053\053\246\246\053\053\053\053\053"
38 "\053\055\055\053\055\053\246\246\053\053\055\055\246\055\053\055"
39 "\055\055\055\053\053\053\053\053\053\053\053\137\137\246\137\137"
40 "\137\337\137\266\137\137\265\137\137\137\137\137\137\137\137\137"
41 "\137\261\137\137\137\137\367\137\260\225\267\137\156\262\137\137";
43 static const BYTE STRING_Ansi2Oem[256] =
44 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
45 "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
46 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
47 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
48 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
49 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
50 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
51 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
52 "\200\201\054\237\054\137\375\374\210\045\123\074\117\215\216\217"
53 "\220\140\047\042\042\371\055\137\230\231\163\076\157\235\236\131"
54 "\040\255\233\234\017\235\335\025\042\143\246\256\252\055\162\137"
55 "\370\361\375\063\047\346\024\372\054\061\247\257\254\253\137\250"
56 "\101\101\101\101\216\217\222\200\105\220\105\105\111\111\111\111"
57 "\104\245\117\117\117\117\231\170\117\125\125\125\232\131\137\341"
58 "\205\240\203\141\204\206\221\207\212\202\210\211\215\241\214\213"
59 "\144\244\225\242\223\157\224\366\157\227\243\226\201\171\137\230";
61 #define OEM_TO_ANSI(ch) (STRING_Oem2Ansi[(unsigned char)(ch)])
62 #define ANSI_TO_OEM(ch) (STRING_Ansi2Oem[(unsigned char)(ch)])
64 /* Internaly used by strchr family functions */
65 static BOOL ChrCmpA( WORD word1, WORD word2);
68 /* filter for page-fault exceptions */
69 static WINE_EXCEPTION_FILTER(page_fault)
71 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
72 return EXCEPTION_EXECUTE_HANDLER;
73 return EXCEPTION_CONTINUE_SEARCH;
77 /***********************************************************************
78 * hmemcpy (KERNEL.348)
80 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
82 memcpy( dst, src, count );
86 /***********************************************************************
87 * lstrcat16 (KERNEL.89)
89 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
91 /* Windows does not check for NULL pointers here, so we don't either */
92 strcat( (LPSTR)PTR_SEG_TO_LIN(dst), src );
97 /***********************************************************************
98 * lstrcatA (KERNEL32.599)
100 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
108 SetLastError( ERROR_INVALID_PARAMETER );
116 /***********************************************************************
117 * lstrcatW (KERNEL32.600)
119 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
123 CRTDLL_wcscat( dst, src );
127 SetLastError( ERROR_INVALID_PARAMETER );
135 /***********************************************************************
136 * lstrcatn16 (KERNEL.352)
138 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
140 LPSTR p = (LPSTR)PTR_SEG_TO_LIN(dst);
143 if ((n -= (p - (LPSTR)PTR_SEG_TO_LIN(dst))) <= 0) return dst;
144 lstrcpynA( p, src, n );
149 /***********************************************************************
150 * lstrcmp16 (USER.430)
152 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
154 return (INT16)strcmp( str1, str2 );
158 /***********************************************************************
159 * lstrcmp32A (KERNEL.602)
161 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
163 return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
167 /***********************************************************************
168 * lstrcmp32W (KERNEL.603)
169 * FIXME : should call CompareString32W, when it is implemented.
170 * This implementation is not "word sort", as it should.
172 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
174 TRACE("L%s and L%s\n",
175 debugstr_w (str1), debugstr_w (str2));
176 if (!str1 || !str2) {
177 SetLastError(ERROR_INVALID_PARAMETER);
180 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
181 return (INT)(*str1 - *str2);
185 /***********************************************************************
186 * lstrcmpi16 (USER.471)
188 INT16 WINAPI lstrcmpi16( LPCSTR str1, LPCSTR str2 )
190 return (INT16)lstrcmpiA( str1, str2 );
194 /***********************************************************************
195 * lstrcmpi32A (KERNEL32.605)
197 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
198 { TRACE("strcmpi %s and %s\n",
199 debugstr_a (str1), debugstr_a (str2));
200 return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
204 /***********************************************************************
205 * lstrcmpi32W (KERNEL32.606)
207 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
212 /* Too much! (From registry loading.) */
213 TRACE("strcmpi L%s and L%s\n",
214 debugstr_w (str1), debugstr_w (str2));
216 if (!str1 || !str2) {
217 SetLastError(ERROR_INVALID_PARAMETER);
222 if ((*str1<0x100 ) && (*str2<0x100)) {
223 if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
225 if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
230 return towupper(*str1) - towupper(*str2);
234 /***********************************************************************
235 * lstrcpy16 (KERNEL.88)
237 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
239 /* this is how Windows does it */
240 memmove( (LPSTR)PTR_SEG_TO_LIN(dst), src, strlen(src)+1 );
245 /***********************************************************************
246 * lstrcpyA (KERNEL32.608)
248 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
252 /* this is how Windows does it */
253 memmove( dst, src, strlen(src)+1 );
257 SetLastError( ERROR_INVALID_PARAMETER );
265 /***********************************************************************
266 * lstrcpyW (KERNEL32.609)
268 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
272 CRTDLL_wcscpy( dst, src );
276 SetLastError( ERROR_INVALID_PARAMETER );
284 /***********************************************************************
285 * lstrcpyn16 (KERNEL.353)
287 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
289 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
294 /***********************************************************************
295 * lstrcpyn32A (KERNEL32.611)
296 * Note: this function differs from the UNIX strncpy, it _always_ writes
299 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
302 TRACE("strcpyn %s for %d chars\n",
303 debugstr_an (src,n), n);
304 /* In real windows the whole function is protected by an exception handler
305 * that returns ERROR_INVALID_PARAMETER on faulty parameters
306 * We currently just check for NULL.
309 SetLastError(ERROR_INVALID_PARAMETER);
312 while ((n-- > 1) && *src) *p++ = *src++;
318 /***********************************************************************
319 * lstrcpyn32W (KERNEL32.612)
320 * Note: this function differs from the UNIX strncpy, it _always_ writes
323 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
326 TRACE("strcpyn L%s for %d chars\n",
327 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;
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("L%s\n",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("%s %i\n",src, 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 n = CRTDLL_wcstombs( dst, src, n );
457 /***********************************************************************
458 * UnicodeToAnsi (KERNEL.434)
460 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
462 if ( codepage != -1 )
463 FIXME("codepage %d not supported\n", codepage );
465 lstrcpyWtoA( dst, src );
467 return (INT16)lstrlenA( dst );
471 /***********************************************************************
474 void WINAPI Copy16( LPVOID src, LPVOID dst, WORD size )
476 memcpy( dst, src, size );
480 /***********************************************************************
481 * RtlFillMemory (KERNEL32.441)
483 VOID WINAPI RtlFillMemory( LPVOID ptr, UINT len, UINT fill )
485 memset( ptr, fill, len );
489 /***********************************************************************
490 * RtlMoveMemory (KERNEL32.442)
492 VOID WINAPI RtlMoveMemory( LPVOID dst, LPCVOID src, UINT len )
494 memmove( dst, src, len );
498 /***********************************************************************
499 * RtlZeroMemory (KERNEL32.444)
501 VOID WINAPI RtlZeroMemory( LPVOID ptr, UINT len )
503 memset( ptr, 0, len );
507 /***********************************************************************
508 * AnsiToOem16 (KEYBOARD.5)
510 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
517 /***********************************************************************
518 * OemToAnsi16 (KEYBOARD.6)
520 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
527 /***********************************************************************
528 * AnsiToOemBuff16 (KEYBOARD.134)
530 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
532 if (len != 0) CharToOemBuffA( s, d, len );
536 /***********************************************************************
537 * OemToAnsiBuff16 (KEYBOARD.135)
539 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
541 if (len != 0) OemToCharBuffA( s, d, len );
545 /***********************************************************************
546 * CharToOem32A (USER32.37)
548 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
551 if (!s || !d) return TRUE;
552 TRACE("CharToOem %s\n", debugstr_a (s));
553 while ((*d++ = ANSI_TO_OEM(*s++)));
554 TRACE(" to %s\n", debugstr_a (oldd));
559 /***********************************************************************
560 * CharToOemBuff32A (USER32.38)
562 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
564 while (len--) *d++ = ANSI_TO_OEM(*s++);
569 /***********************************************************************
570 * CharToOemBuff32W (USER32.39)
572 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
574 while (len--) *d++ = ANSI_TO_OEM(*s++);
579 /***********************************************************************
580 * CharToOem32W (USER32.40)
582 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
585 if (!s || !d) return TRUE;
586 TRACE("CharToOem L%s\n", debugstr_w (s));
587 while ((*d++ = ANSI_TO_OEM(*s++)));
588 TRACE(" to %s\n", debugstr_a (oldd));
593 /***********************************************************************
594 * OemToChar32A (USER32.402)
596 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
599 TRACE("OemToChar %s\n", debugstr_a (s));
600 while ((*d++ = OEM_TO_ANSI(*s++)));
601 TRACE(" to %s\n", debugstr_a (oldd));
606 /***********************************************************************
607 * OemToCharBuff32A (USER32.403)
609 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
611 TRACE("OemToCharBuff %s\n", debugstr_an (s, len));
612 while (len--) *d++ = OEM_TO_ANSI(*s++);
617 /***********************************************************************
618 * OemToCharBuff32W (USER32.404)
620 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
622 TRACE("OemToCharBuff %s\n", debugstr_an (s, len));
623 while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
628 /***********************************************************************
629 * OemToChar32W (USER32.405)
631 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
633 while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++)));
637 /***********************************************************************
638 * WideCharToLocal (Not a Windows API)
639 * similar lstrcpyWtoA, should handle codepages properly
642 * strlen of the destination string
645 INT WINAPI WideCharToLocal(
650 TRACE("(%p, %s, %i)\n", pLocal, debugstr_w(pWide),dwChars);
651 WideCharToMultiByte(CP_ACP,0,pWide,-1,pLocal,dwChars,NULL,NULL);
652 return strlen(pLocal);
654 /***********************************************************************
655 * LocalToWideChar (Not a Windows API)
656 * similar lstrcpyAtoW, should handle codepages properly
659 * strlen of the destination string
661 INT WINAPI LocalToWideChar(
666 TRACE("(%p, %s, %i)\n",pWide, pLocal, dwChars);
667 MultiByteToWideChar(CP_ACP,0,pLocal,-1,pWide,dwChars);
668 return lstrlenW(pWide);
672 /***********************************************************************
673 * lstrrchr (Not a Windows API)
675 * This is the implementation meant to be invoked form within
676 * COMCTL32_StrRChrA and shell32(TODO)...
678 * Return a pointer to the last occurence of wMatch in lpStart
679 * not looking further than lpEnd...
681 LPSTR WINAPI lstrrchr( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
683 LPCSTR lpGotIt = NULL;
685 TRACE("(%s, %s)\n", lpStart, lpEnd);
687 if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
689 for(; lpStart < lpEnd; lpStart = CharNextA(lpStart))
690 if (!ChrCmpA( GET_WORD(lpStart), wMatch))
693 return ((LPSTR)lpGotIt);
696 /***********************************************************************
698 * This fuction returns FALSE if both words match, TRUE otherwise...
700 static BOOL ChrCmpW( WORD word1, WORD word2) {
701 return (word1 != word2);
704 /***********************************************************************
705 * lstrrchrw (Not a Windows API)
707 * This is the implementation meant to be invoked form within
708 * COMCTL32_StrRChrW and shell32(TODO)...
710 * Return a pointer to the last occurence of wMatch in lpStart
711 * not looking further than lpEnd...
713 LPWSTR WINAPI lstrrchrw( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch )
715 LPCWSTR lpGotIt = NULL;
717 TRACE("(%p, %p, %c)\n", lpStart, lpEnd, wMatch);
718 if (!lpEnd) lpEnd = lpStart + lstrlenW(lpStart);
720 for(; lpStart < lpEnd; lpStart = CharNextW(lpStart))
721 if (!ChrCmpW( GET_WORD(lpStart), wMatch))
724 return (LPWSTR)lpGotIt;
727 /***********************************************************************
729 * This fuction returns FALSE if both words match, TRUE otherwise...
731 static BOOL ChrCmpA( WORD word1, WORD word2) {
732 if (LOBYTE(word1) == LOBYTE(word2)) {
733 if (IsDBCSLeadByte(LOBYTE(word1))) {
734 return (word1 != word2);