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 * lstrcmpA (KERNEL.602)
163 INT WINAPI
lstrcmpA( LPCSTR str1
, LPCSTR str2
)
165 return CompareStringA(LOCALE_SYSTEM_DEFAULT
,0,str1
,-1,str2
,-1) - 2 ;
169 /***********************************************************************
170 * lstrcmpW (KERNEL.603)
171 * FIXME : should call CompareStringW, 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 * lstrcmpiA (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 * lstrcmpiW (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 if (!lstrcpyA( PTR_SEG_TO_LIN(dst
), src
)) dst
= 0;
246 /***********************************************************************
247 * lstrcpyA (KERNEL32.608)
249 LPSTR WINAPI
lstrcpyA( LPSTR dst
, LPCSTR src
)
253 /* this is how Windows does it */
254 memmove( dst
, src
, strlen(src
)+1 );
258 ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst
, src
);
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 * lstrcpynA (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 * lstrcpynW (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
);
480 /***********************************************************************
481 * AnsiToOem16 (KEYBOARD.5)
483 INT16 WINAPI
AnsiToOem16( LPCSTR s
, LPSTR d
)
490 /***********************************************************************
491 * OemToAnsi16 (KEYBOARD.6)
493 INT16 WINAPI
OemToAnsi16( LPCSTR s
, LPSTR d
)
500 /***********************************************************************
501 * AnsiToOemBuff16 (KEYBOARD.134)
503 void WINAPI
AnsiToOemBuff16( LPCSTR s
, LPSTR d
, UINT16 len
)
505 if (len
!= 0) CharToOemBuffA( s
, d
, len
);
509 /***********************************************************************
510 * OemToAnsiBuff16 (KEYBOARD.135)
512 void WINAPI
OemToAnsiBuff16( LPCSTR s
, LPSTR d
, UINT16 len
)
514 if (len
!= 0) OemToCharBuffA( s
, d
, len
);
518 /***********************************************************************
519 * CharToOemA (USER32.37)
521 BOOL WINAPI
CharToOemA( LPCSTR s
, LPSTR 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
));
532 /***********************************************************************
533 * CharToOemBuffA (USER32.38)
535 BOOL WINAPI
CharToOemBuffA( LPCSTR s
, LPSTR d
, DWORD len
)
537 while (len
--) *d
++ = ANSI_TO_OEM(*s
++);
542 /***********************************************************************
543 * CharToOemBuffW (USER32.39)
545 BOOL WINAPI
CharToOemBuffW( LPCWSTR s
, LPSTR d
, DWORD len
)
547 while (len
--) *d
++ = ANSI_TO_OEM(*s
++);
552 /***********************************************************************
553 * CharToOemW (USER32.40)
555 BOOL WINAPI
CharToOemW( LPCWSTR s
, LPSTR 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
));
566 /***********************************************************************
567 * OemToCharA (USER32.402)
569 BOOL WINAPI
OemToCharA( LPCSTR s
, LPSTR d
)
572 TRACE("OemToChar %s\n", debugstr_a (s
));
573 while ((*d
++ = OEM_TO_ANSI(*s
++)));
574 TRACE(" to %s\n", debugstr_a (oldd
));
579 /***********************************************************************
580 * OemToCharBuffA (USER32.403)
582 BOOL WINAPI
OemToCharBuffA( LPCSTR s
, LPSTR d
, DWORD len
)
584 TRACE("OemToCharBuff %s\n", debugstr_an (s
, len
));
585 while (len
--) *d
++ = OEM_TO_ANSI(*s
++);
590 /***********************************************************************
591 * OemToCharBuffW (USER32.404)
593 BOOL WINAPI
OemToCharBuffW( LPCSTR s
, LPWSTR d
, DWORD len
)
595 TRACE("OemToCharBuff %s\n", debugstr_an (s
, len
));
596 while (len
--) *d
++ = (WCHAR
)OEM_TO_ANSI(*s
++);
601 /***********************************************************************
602 * OemToCharW (USER32.405)
604 BOOL WINAPI
OemToCharW( LPCSTR s
, LPWSTR d
)
606 while ((*d
++ = (WCHAR
)OEM_TO_ANSI(*s
++)));
610 /***********************************************************************
611 * lstrrchr (Not a Windows API)
613 * This is the implementation meant to be invoked from within
614 * COMCTL32_StrRChrA and shell32(TODO)...
616 * Return a pointer to the last occurence of wMatch in lpStart
617 * not looking further than lpEnd...
619 LPSTR WINAPI
lstrrchr( LPCSTR lpStart
, LPCSTR lpEnd
, WORD wMatch
)
621 LPCSTR lpGotIt
= NULL
;
623 TRACE("(%p, %p, %x)\n", lpStart
, lpEnd
, wMatch
);
625 if (!lpEnd
) lpEnd
= lpStart
+ strlen(lpStart
);
627 for(; lpStart
< lpEnd
; lpStart
= CharNextA(lpStart
))
628 if (!ChrCmpA( GET_WORD(lpStart
), wMatch
))
631 return ((LPSTR
)lpGotIt
);
634 /***********************************************************************
636 * This fuction returns FALSE if both words match, TRUE otherwise...
638 static BOOL
ChrCmpW( WORD word1
, WORD word2
) {
639 return (word1
!= word2
);
642 /***********************************************************************
643 * lstrrchrw (Not a Windows API)
645 * This is the implementation meant to be invoked form within
646 * COMCTL32_StrRChrW and shell32(TODO)...
648 * Return a pointer to the last occurence of wMatch in lpStart
649 * not looking further than lpEnd...
651 LPWSTR WINAPI
lstrrchrw( LPCWSTR lpStart
, LPCWSTR lpEnd
, WORD wMatch
)
653 LPCWSTR lpGotIt
= NULL
;
655 TRACE("(%p, %p, %x)\n", lpStart
, lpEnd
, wMatch
);
656 if (!lpEnd
) lpEnd
= lpStart
+ lstrlenW(lpStart
);
658 for(; lpStart
< lpEnd
; lpStart
= CharNextW(lpStart
))
659 if (!ChrCmpW( GET_WORD(lpStart
), wMatch
))
662 return (LPWSTR
)lpGotIt
;
665 /***********************************************************************
667 * This fuction returns FALSE if both words match, TRUE otherwise...
669 static BOOL
ChrCmpA( WORD word1
, WORD word2
) {
670 if (LOBYTE(word1
) == LOBYTE(word2
)) {
671 if (IsDBCSLeadByte(LOBYTE(word1
))) {
672 return (word1
!= word2
);