4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
12 #include "wine/keyboard16.h"
18 DEFAULT_DEBUG_CHANNEL(string)
20 static const BYTE STRING_Oem2Ansi[256] =
21 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\244"
22 "\020\021\022\023\266\247\026\027\030\031\032\033\034\035\036\037"
23 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
24 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
25 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
26 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
27 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
28 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
29 "\307\374\351\342\344\340\345\347\352\353\350\357\356\354\304\305"
30 "\311\346\306\364\366\362\373\371\377\326\334\242\243\245\120\203"
31 "\341\355\363\372\361\321\252\272\277\137\254\275\274\241\253\273"
32 "\137\137\137\246\246\246\246\053\053\246\246\053\053\053\053\053"
33 "\053\055\055\053\055\053\246\246\053\053\055\055\246\055\053\055"
34 "\055\055\055\053\053\053\053\053\053\053\053\137\137\246\137\137"
35 "\137\337\137\266\137\137\265\137\137\137\137\137\137\137\137\137"
36 "\137\261\137\137\137\137\367\137\260\225\267\137\156\262\137\137";
38 static const BYTE STRING_Ansi2Oem[256] =
39 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
40 "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
41 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
42 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
43 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
44 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
45 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
46 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
47 "\200\201\054\237\054\137\375\374\210\045\123\074\117\215\216\217"
48 "\220\140\047\042\042\371\055\137\230\231\163\076\157\235\236\131"
49 "\040\255\233\234\017\235\335\025\042\143\246\256\252\055\162\137"
50 "\370\361\375\063\047\346\024\372\054\061\247\257\254\253\137\250"
51 "\101\101\101\101\216\217\222\200\105\220\105\105\111\111\111\111"
52 "\104\245\117\117\117\117\231\170\117\125\125\125\232\131\137\341"
53 "\205\240\203\141\204\206\221\207\212\202\210\211\215\241\214\213"
54 "\144\244\225\242\223\157\224\366\157\227\243\226\201\171\137\230";
56 #define OEM_TO_ANSI(ch) (STRING_Oem2Ansi[(unsigned char)(ch)])
57 #define ANSI_TO_OEM(ch) (STRING_Ansi2Oem[(unsigned char)(ch)])
59 /* Internaly used by strchr family functions */
60 static BOOL ChrCmpA( WORD word1, WORD word2);
61 static BOOL ChrCmpW( WORD word1, WORD word2);
63 extern LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar);
66 /***********************************************************************
67 * hmemcpy (KERNEL.348)
69 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
71 memcpy( dst, src, count );
75 /***********************************************************************
76 * lstrcat16 (KERNEL.89)
78 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
80 lstrcatA( (LPSTR)PTR_SEG_TO_LIN(dst), src );
85 /***********************************************************************
86 * lstrcat32A (KERNEL32.599)
88 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
90 TRACE(string,"Append %s to %s\n",
91 debugstr_a (src), debugstr_a (dst));
92 /* Windows does not check for NULL pointers here, so we don't either */
98 /***********************************************************************
99 * lstrcat32W (KERNEL32.600)
101 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
103 register LPWSTR p = dst;
104 TRACE(string,"Append L%s to L%s\n",
105 debugstr_w (src), debugstr_w (dst));
106 /* Windows does not check for NULL pointers here, so we don't either */
108 while ((*p++ = *src++));
113 /***********************************************************************
114 * lstrcatn16 (KERNEL.352)
116 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
118 lstrcatnA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
123 /***********************************************************************
124 * lstrcatn32A (Not a Windows API)
126 LPSTR WINAPI lstrcatnA( LPSTR dst, LPCSTR src, INT n )
128 register LPSTR p = dst;
129 TRACE(string,"strcatn add %d chars from %s to %s\n",
130 n, debugstr_an (src, n), debugstr_a (dst));
132 if ((n -= (INT)(p - dst)) <= 0) return dst;
133 lstrcpynA( p, src, n );
138 /***********************************************************************
139 * lstrcatn32W (Not a Windows API)
141 LPWSTR WINAPI lstrcatnW( LPWSTR dst, LPCWSTR src, INT n )
143 register LPWSTR p = dst;
144 TRACE(string,"strcatn add %d chars from L%s to L%s\n",
145 n, debugstr_wn (src, n), debugstr_w (dst));
147 if ((n -= (INT)(p - dst)) <= 0) return dst;
148 lstrcpynW( p, src, n );
153 /***********************************************************************
154 * lstrcmp16 (USER.430)
156 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
158 return (INT16)strcmp( str1, str2 );
162 /***********************************************************************
163 * lstrcmp32A (KERNEL.602)
165 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
167 return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
171 /***********************************************************************
172 * lstrcmp32W (KERNEL.603)
173 * FIXME : should call CompareString32W, when it is implemented.
174 * This implementation is not "word sort", as it should.
176 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
178 TRACE(string,"L%s and L%s\n",
179 debugstr_w (str1), debugstr_w (str2));
180 if (!str1 || !str2) {
181 SetLastError(ERROR_INVALID_PARAMETER);
184 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
185 return (INT)(*str1 - *str2);
189 /***********************************************************************
190 * lstrcmpi16 (USER.471)
192 INT16 WINAPI lstrcmpi16( LPCSTR str1, LPCSTR str2 )
194 return (INT16)lstrcmpiA( str1, str2 );
198 /***********************************************************************
199 * lstrcmpi32A (KERNEL32.605)
201 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
202 { TRACE(string,"strcmpi %s and %s\n",
203 debugstr_a (str1), debugstr_a (str2));
204 return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
208 /***********************************************************************
209 * lstrcmpi32W (KERNEL32.606)
211 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
216 /* Too much! (From registry loading.) */
217 TRACE(string,"strcmpi L%s and L%s\n",
218 debugstr_w (str1), debugstr_w (str2));
220 if (!str1 || !str2) {
221 SetLastError(ERROR_INVALID_PARAMETER);
226 if ((*str1<0x100 ) && (*str2<0x100)) {
227 if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
229 if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
234 return towupper(*str1) - towupper(*str2);
238 /***********************************************************************
239 * lstrcpy16 (KERNEL.88)
241 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
243 lstrcpyA( (LPSTR)PTR_SEG_TO_LIN(dst), src );
248 /***********************************************************************
249 * lstrcpyA (KERNEL32.608)
251 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
253 TRACE(string,"strcpy %s\n", debugstr_a (src));
254 /* In real windows the whole function is protected by an exception handler
255 * that returns ERROR_INVALID_PARAMETER on faulty parameters
256 * We currently just check for NULL.
259 SetLastError(ERROR_INVALID_PARAMETER);
262 /* this is how Windows does it */
263 memmove( dst, src, strlen(src)+1 );
268 /***********************************************************************
269 * lstrcpy32W (KERNEL32.609)
271 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
273 register LPWSTR p = dst;
274 TRACE(string,"strcpy L%s\n", debugstr_w (src));
275 /* In real windows the whole function is protected by an exception handler
276 * that returns ERROR_INVALID_PARAMETER on faulty parameters
277 * We currently just check for NULL.
280 SetLastError(ERROR_INVALID_PARAMETER);
283 while ((*p++ = *src++));
288 /***********************************************************************
289 * lstrcpyn16 (KERNEL.353)
291 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
293 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
298 /***********************************************************************
299 * lstrcpyn32A (KERNEL32.611)
300 * Note: this function differs from the UNIX strncpy, it _always_ writes
303 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
306 TRACE(string,"strcpyn %s for %d chars\n",
307 debugstr_an (src,n), n);
308 /* In real windows the whole function is protected by an exception handler
309 * that returns ERROR_INVALID_PARAMETER on faulty parameters
310 * We currently just check for NULL.
313 SetLastError(ERROR_INVALID_PARAMETER);
316 while ((n-- > 1) && *src) *p++ = *src++;
322 /***********************************************************************
323 * lstrcpyn32W (KERNEL32.612)
324 * Note: this function differs from the UNIX strncpy, it _always_ writes
327 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
330 TRACE(string,"strcpyn L%s for %d chars\n",
331 debugstr_wn (src,n), n);
332 /* In real windows the whole function is protected by an exception handler
333 * that returns ERROR_INVALID_PARAMETER on faulty parameters
334 * We currently just check for NULL.
337 SetLastError(ERROR_INVALID_PARAMETER);
340 while ((n-- > 1) && *src) *p++ = *src++;
346 /***********************************************************************
347 * lstrlen16 (KERNEL.90)
349 INT16 WINAPI lstrlen16( LPCSTR str )
351 return (INT16)lstrlenA( str );
355 /***********************************************************************
356 * lstrlen32A (KERNEL32.614)
358 INT WINAPI lstrlenA( LPCSTR str )
360 /* looks weird, but win3.1 KERNEL got a GeneralProtection handler
361 * in lstrlen() ... we check only for NULL pointer reference.
364 TRACE(string,"strlen %s\n", debugstr_a (str));
366 return (INT)strlen(str);
370 /***********************************************************************
371 * lstrlen32W (KERNEL32.615)
373 INT WINAPI lstrlenW( LPCWSTR str )
376 TRACE(string,"strlen L%s\n", debugstr_w (str));
378 while (*str++) len++;
383 /***********************************************************************
384 * lstrncmp32A (Not a Windows API)
386 INT WINAPI lstrncmpA( LPCSTR str1, LPCSTR str2, INT n )
388 TRACE(string,"strncmp %s and %s for %d chars\n",
389 debugstr_an (str1, n), debugstr_an (str2, n), n);
390 return (INT)strncmp( str1, str2, n );
394 /***********************************************************************
395 * lstrncmp32W (Not a Windows API)
397 INT WINAPI lstrncmpW( LPCWSTR str1, LPCWSTR str2, INT n )
399 TRACE(string,"strncmp L%s and L%s for %d chars\n",
400 debugstr_wn (str1, n), debugstr_wn (str2, n), n);
402 while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
403 return (INT)(*str1 - *str2);
407 /***********************************************************************
408 * lstrncmpi32A (Not a Windows API)
410 INT WINAPI lstrncmpiA( LPCSTR str1, LPCSTR str2, INT n )
414 TRACE(string,"strncmpi %s and %s for %d chars\n",
415 debugstr_an (str1, n), debugstr_an (str2, n), n);
417 while ((--n > 0) && *str1)
418 if ( (res = toupper(*str1++) - toupper(*str2++)) ) return res;
420 return toupper(*str1) - toupper(*str2);
424 /***********************************************************************
425 * lstrncmpi32W (Not a Windows API)
427 INT WINAPI lstrncmpiW( LPCWSTR str1, LPCWSTR str2, INT n )
431 TRACE(string,"strncmpi L%s and L%s for %d chars\n",
432 debugstr_wn (str1, n), debugstr_wn (str2, n), n);
434 while ((--n > 0) && *str1)
436 if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
440 return towupper(*str1) - towupper(*str2);
444 /***********************************************************************
445 * lstrcpyAtoW (Not a Windows API)
447 LPWSTR WINAPI lstrcpyAtoW( LPWSTR dst, LPCSTR src )
449 register LPWSTR p = dst;
451 TRACE(string,"%s\n",src);
453 while ((*p++ = (WCHAR)(unsigned char)*src++));
458 /***********************************************************************
459 * lstrcpyWtoA (Not a Windows API)
461 LPSTR WINAPI lstrcpyWtoA( LPSTR dst, LPCWSTR src )
463 register LPSTR p = dst;
465 TRACE(string,"L%s\n",debugstr_w(src));
467 while ((*p++ = (CHAR)*src++));
472 /***********************************************************************
473 * lstrcpynAtoW (Not a Windows API)
474 * Note: this function differs from the UNIX strncpy, it _always_ writes
477 LPWSTR WINAPI lstrcpynAtoW( LPWSTR dst, LPCSTR src, INT n )
481 TRACE(string,"%s %i\n",src, n);
483 while ((n-- > 1) && *src) *p++ = (WCHAR)(unsigned char)*src++;
489 /***********************************************************************
490 * lstrcpynWtoA (Not a Windows API)
491 * Note: this function differs from the UNIX strncpy, it _always_ writes
494 LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT n )
498 TRACE(string,"L%s %i\n",debugstr_w(src), n);
500 while ((n-- > 1) && *src) *p++ = (CHAR)*src++;
505 /***********************************************************************
506 * UnicodeToAnsi (KERNEL.434)
508 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
510 if ( codepage != -1 )
511 FIXME( string, "codepage %d not supported\n", codepage );
513 lstrcpyWtoA( dst, src );
515 return (INT16)lstrlenA( dst );
519 /***********************************************************************
522 void WINAPI Copy16( LPVOID src, LPVOID dst, WORD size )
524 memcpy( dst, src, size );
528 /***********************************************************************
529 * RtlFillMemory (KERNEL32.441)
531 VOID WINAPI RtlFillMemory( LPVOID ptr, UINT len, UINT fill )
533 memset( ptr, fill, len );
537 /***********************************************************************
538 * RtlMoveMemory (KERNEL32.442)
540 VOID WINAPI RtlMoveMemory( LPVOID dst, LPCVOID src, UINT len )
542 memmove( dst, src, len );
546 /***********************************************************************
547 * RtlZeroMemory (KERNEL32.444)
549 VOID WINAPI RtlZeroMemory( LPVOID ptr, UINT len )
551 memset( ptr, 0, len );
555 /***********************************************************************
556 * AnsiToOem16 (KEYBOARD.5)
558 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
565 /***********************************************************************
566 * OemToAnsi16 (KEYBOARD.6)
568 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
575 /***********************************************************************
576 * AnsiToOemBuff16 (KEYBOARD.134)
578 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
580 if (len != 0) CharToOemBuffA( s, d, len );
584 /***********************************************************************
585 * OemToAnsiBuff16 (KEYBOARD.135)
587 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
589 if (len != 0) OemToCharBuffA( s, d, len );
593 /***********************************************************************
594 * CharToOem32A (USER32.37)
596 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
599 if (!s || !d) return TRUE;
600 TRACE(string,"CharToOem %s\n", debugstr_a (s));
601 while ((*d++ = ANSI_TO_OEM(*s++)));
602 TRACE(string," to %s\n", debugstr_a (oldd));
607 /***********************************************************************
608 * CharToOemBuff32A (USER32.38)
610 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
612 while (len--) *d++ = ANSI_TO_OEM(*s++);
617 /***********************************************************************
618 * CharToOemBuff32W (USER32.39)
620 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
622 while (len--) *d++ = ANSI_TO_OEM(*s++);
627 /***********************************************************************
628 * CharToOem32W (USER32.40)
630 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
633 if (!s || !d) return TRUE;
634 TRACE(string,"CharToOem L%s\n", debugstr_w (s));
635 while ((*d++ = ANSI_TO_OEM(*s++)));
636 TRACE(string," to %s\n", debugstr_a (oldd));
641 /***********************************************************************
642 * OemToChar32A (USER32.402)
644 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
647 TRACE(string,"OemToChar %s\n", debugstr_a (s));
648 while ((*d++ = OEM_TO_ANSI(*s++)));
649 TRACE(string," to %s\n", debugstr_a (oldd));
654 /***********************************************************************
655 * OemToCharBuff32A (USER32.403)
657 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
659 TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
660 while (len--) *d++ = OEM_TO_ANSI(*s++);
665 /***********************************************************************
666 * OemToCharBuff32W (USER32.404)
668 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
670 TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
671 while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
676 /***********************************************************************
677 * OemToChar32W (USER32.405)
679 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
681 while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++)));
685 /***********************************************************************
686 * WideCharToLocal (Not a Windows API)
687 * similar lstrcpyWtoA, should handle codepages properly
690 * strlen of the destination string
693 INT WINAPI WideCharToLocal(
698 TRACE(string,"(%p, %s, %i)\n", pLocal, debugstr_w(pWide),dwChars);
699 WideCharToMultiByte(CP_ACP,0,pWide,-1,pLocal,dwChars,NULL,NULL);
700 return strlen(pLocal);
702 /***********************************************************************
703 * LocalToWideChar (Not a Windows API)
704 * similar lstrcpyAtoW, should handle codepages properly
707 * strlen of the destination string
709 INT WINAPI LocalToWideChar(
714 TRACE(string,"(%p, %s, %i)\n",pWide, pLocal, dwChars);
715 MultiByteToWideChar(CP_ACP,0,pLocal,-1,pWide,dwChars);
716 return lstrlenW(pWide);
720 /***********************************************************************
721 * lstrrchr (Not a Windows API)
723 * This is the implementation meant to be invoked form within
724 * COMCTL32_StrRChrA and shell32(TODO)...
726 * Return a pointer to the last occurence of wMatch in lpStart
727 * not looking further than lpEnd...
729 LPSTR WINAPI lstrrchr( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
731 LPCSTR lpGotIt = NULL;
733 TRACE(string,"(%s, %s)\n", lpStart, lpEnd);
735 if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
737 for(; lpStart < lpEnd; lpStart = CharNextA(lpStart))
738 if (!ChrCmpA( GET_WORD(lpStart), wMatch))
741 return ((LPSTR)lpGotIt);
744 /***********************************************************************
745 * lstrrchrw (Not a Windows API)
747 * This is the implementation meant to be invoked form within
748 * COMCTL32_StrRChrW and shell32(TODO)...
750 * Return a pointer to the last occurence of wMatch in lpStart
751 * not looking further than lpEnd...
753 LPWSTR WINAPI lstrrchrw( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch )
755 LPCWSTR lpGotIt = NULL;
757 TRACE(string,"(%p, %p, %c)\n", lpStart, lpEnd, wMatch);
758 if (!lpEnd) lpEnd = lpStart + lstrlenW(lpStart);
760 for(; lpStart < lpEnd; lpStart = CharNextW(lpStart))
761 if (!ChrCmpW( GET_WORD(lpStart), wMatch))
764 return (LPWSTR)lpGotIt;
767 /***********************************************************************
768 * strstrw (Not a Windows API)
770 * This is the implementation meant to be invoked form within
771 * COMCTL32_StrStrW and shell32(TODO)...
774 LPWSTR WINAPI strstrw( LPCWSTR lpFirst, LPCWSTR lpSrch) {
775 UINT uSrchLen = (UINT)lstrlenW(lpSrch);
776 WORD wMatchBeg = *(WORD*)lpSrch;
778 TRACE(string,"(%p, %p)\n", lpFirst, lpSrch);
781 ((lpFirst=CRTDLL_wcschr(lpFirst, wMatchBeg))!=0) &&
782 lstrncmpW(lpFirst, lpSrch, uSrchLen);
786 return (LPWSTR)lpFirst;
790 /***********************************************************************
792 * This fuction returns FALSE if both words match, TRUE otherwise...
794 static BOOL ChrCmpA( WORD word1, WORD word2) {
795 if (LOBYTE(word1) == LOBYTE(word2)) {
796 if (IsDBCSLeadByte(LOBYTE(word1))) {
797 return (word1 != word2);
804 /***********************************************************************
806 * This fuction returns FALSE if both words match, TRUE otherwise...
808 static BOOL ChrCmpW( WORD word1, WORD word2) {
809 return (word1 != word2);