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"
19 #include "wine/unicode.h"
22 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(string
);
27 /* Internaly used by strchr family functions */
28 static BOOL
ChrCmpA( WORD word1
, WORD word2
);
31 /* filter for page-fault exceptions */
32 static WINE_EXCEPTION_FILTER(page_fault
)
34 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
35 return EXCEPTION_EXECUTE_HANDLER
;
36 return EXCEPTION_CONTINUE_SEARCH
;
40 /***********************************************************************
41 * hmemcpy (KERNEL.348)
43 void WINAPI
hmemcpy16( LPVOID dst
, LPCVOID src
, LONG count
)
45 memcpy( dst
, src
, count
);
49 /***********************************************************************
50 * lstrcat16 (KERNEL.89)
52 SEGPTR WINAPI
lstrcat16( SEGPTR dst
, LPCSTR src
)
54 /* Windows does not check for NULL pointers here, so we don't either */
55 strcat( (LPSTR
)PTR_SEG_TO_LIN(dst
), src
);
60 /***********************************************************************
61 * lstrcatA (KERNEL32.599)
63 LPSTR WINAPI
lstrcatA( LPSTR dst
, LPCSTR src
)
71 SetLastError( ERROR_INVALID_PARAMETER
);
79 /***********************************************************************
80 * lstrcatW (KERNEL32.600)
82 LPWSTR WINAPI
lstrcatW( LPWSTR dst
, LPCWSTR src
)
90 SetLastError( ERROR_INVALID_PARAMETER
);
98 /***********************************************************************
99 * lstrcatn16 (KERNEL.352)
101 SEGPTR WINAPI
lstrcatn16( SEGPTR dst
, LPCSTR src
, INT16 n
)
103 LPSTR p
= (LPSTR
)PTR_SEG_TO_LIN(dst
);
106 if ((n
-= (p
- (LPSTR
)PTR_SEG_TO_LIN(dst
))) <= 0) return dst
;
107 lstrcpynA( p
, src
, n
);
112 /***********************************************************************
113 * lstrcmp16 (USER.430)
115 INT16 WINAPI
lstrcmp16( LPCSTR str1
, LPCSTR str2
)
117 return (INT16
)strcmp( str1
, str2
);
121 /***********************************************************************
122 * lstrcmpA (KERNEL.602)
124 INT WINAPI
lstrcmpA( LPCSTR str1
, LPCSTR str2
)
126 return CompareStringA(LOCALE_SYSTEM_DEFAULT
,0,str1
,-1,str2
,-1) - 2 ;
130 /***********************************************************************
131 * lstrcmpW (KERNEL.603)
132 * FIXME : should call CompareStringW, when it is implemented.
133 * This implementation is not "word sort", as it should.
135 INT WINAPI
lstrcmpW( LPCWSTR str1
, LPCWSTR str2
)
138 debugstr_w (str1
), debugstr_w (str2
));
139 if (!str1
|| !str2
) {
140 SetLastError(ERROR_INVALID_PARAMETER
);
143 while (*str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
144 return (INT
)(*str1
- *str2
);
148 /***********************************************************************
149 * lstrcmpi16 (USER.471)
151 INT16 WINAPI
lstrcmpi16( LPCSTR str1
, LPCSTR str2
)
153 return (INT16
)lstrcmpiA( str1
, str2
);
157 /***********************************************************************
158 * lstrcmpiA (KERNEL32.605)
160 INT WINAPI
lstrcmpiA( LPCSTR str1
, LPCSTR str2
)
161 { TRACE("strcmpi %s and %s\n",
162 debugstr_a (str1
), debugstr_a (str2
));
163 return CompareStringA(LOCALE_SYSTEM_DEFAULT
,NORM_IGNORECASE
,str1
,-1,str2
,-1)-2;
167 /***********************************************************************
168 * lstrcmpiW (KERNEL32.606)
170 INT WINAPI
lstrcmpiW( LPCWSTR str1
, LPCWSTR str2
)
172 if (!str1
|| !str2
) {
173 SetLastError(ERROR_INVALID_PARAMETER
);
176 return strcmpiW( str1
, str2
);
180 /***********************************************************************
181 * lstrcpy16 (KERNEL.88)
183 SEGPTR WINAPI
lstrcpy16( SEGPTR dst
, LPCSTR src
)
185 if (!lstrcpyA( PTR_SEG_TO_LIN(dst
), src
)) dst
= 0;
190 /***********************************************************************
191 * lstrcpyA (KERNEL32.608)
193 LPSTR WINAPI
lstrcpyA( LPSTR dst
, LPCSTR src
)
197 /* this is how Windows does it */
198 memmove( dst
, src
, strlen(src
)+1 );
202 ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst
, src
);
203 SetLastError( ERROR_INVALID_PARAMETER
);
211 /***********************************************************************
212 * lstrcpyW (KERNEL32.609)
214 LPWSTR WINAPI
lstrcpyW( LPWSTR dst
, LPCWSTR src
)
222 SetLastError( ERROR_INVALID_PARAMETER
);
230 /***********************************************************************
231 * lstrcpyn16 (KERNEL.353)
233 SEGPTR WINAPI
lstrcpyn16( SEGPTR dst
, LPCSTR src
, INT16 n
)
235 lstrcpynA( (LPSTR
)PTR_SEG_TO_LIN(dst
), src
, n
);
240 /***********************************************************************
241 * lstrcpynA (KERNEL32.611)
242 * Note: this function differs from the UNIX strncpy, it _always_ writes
245 LPSTR WINAPI
lstrcpynA( LPSTR dst
, LPCSTR src
, INT n
)
248 TRACE("(%p, %s, %i)\n", dst
, debugstr_an(src
,n
), n
);
249 /* In real windows the whole function is protected by an exception handler
250 * that returns ERROR_INVALID_PARAMETER on faulty parameters
251 * We currently just check for NULL.
254 SetLastError(ERROR_INVALID_PARAMETER
);
257 while ((n
-- > 1) && *src
) *p
++ = *src
++;
263 /***********************************************************************
264 * lstrcpynW (KERNEL32.612)
265 * Note: this function differs from the UNIX strncpy, it _always_ writes
268 LPWSTR WINAPI
lstrcpynW( LPWSTR dst
, LPCWSTR src
, INT n
)
271 TRACE("(%p, %s, %i)\n", dst
, debugstr_wn(src
,n
), n
);
272 /* In real windows the whole function is protected by an exception handler
273 * that returns ERROR_INVALID_PARAMETER on faulty parameters
274 * We currently just check for NULL.
277 SetLastError(ERROR_INVALID_PARAMETER
);
280 while ((n
-- > 1) && *src
) *p
++ = *src
++;
286 /***********************************************************************
287 * lstrlen16 (KERNEL.90)
289 INT16 WINAPI
lstrlen16( LPCSTR str
)
291 return (INT16
)lstrlenA( str
);
295 /***********************************************************************
296 * lstrlenA (KERNEL32.614)
298 INT WINAPI
lstrlenA( LPCSTR str
)
307 SetLastError( ERROR_INVALID_PARAMETER
);
315 /***********************************************************************
316 * lstrlenW (KERNEL32.615)
318 INT WINAPI
lstrlenW( LPCWSTR str
)
327 SetLastError( ERROR_INVALID_PARAMETER
);
335 /***********************************************************************
336 * lstrcpyAtoW (Not a Windows API)
338 LPWSTR WINAPI
lstrcpyAtoW( LPWSTR dst
, LPCSTR src
)
340 MultiByteToWideChar( CP_ACP
, 0, src
, -1, dst
, 0x7fffffff );
345 /***********************************************************************
346 * lstrcpyWtoA (Not a Windows API)
348 LPSTR WINAPI
lstrcpyWtoA( LPSTR dst
, LPCWSTR src
)
350 WideCharToMultiByte( CP_ACP
, 0, src
, -1, dst
, 0x7fffffff, NULL
, NULL
);
355 /***********************************************************************
356 * lstrcpynAtoW (Not a Windows API)
357 * Note: this function differs from the UNIX strncpy, it _always_ writes
360 LPWSTR WINAPI
lstrcpynAtoW( LPWSTR dst
, LPCSTR src
, INT n
)
362 if (n
> 0 && !MultiByteToWideChar( CP_ACP
, 0, src
, -1, dst
, n
)) dst
[n
-1] = 0;
367 /***********************************************************************
368 * lstrcpynWtoA (Not a Windows API)
369 * Note: this function differs from the UNIX strncpy, it _always_ writes
372 * The terminating zero should be written at the end of the string, not
373 * the end of the buffer, as some programs specify the wrong size for
374 * the buffer (eg. winnt's sol.exe)
376 LPSTR WINAPI
lstrcpynWtoA( LPSTR dst
, LPCWSTR src
, INT n
)
378 if (n
> 0 && !WideCharToMultiByte( CP_ACP
, 0, src
, -1, dst
, n
, NULL
, NULL
)) dst
[n
-1] = 0;
382 /***********************************************************************
383 * UnicodeToAnsi (KERNEL.434)
385 INT16 WINAPI
UnicodeToAnsi16( LPCWSTR src
, LPSTR dst
, INT16 codepage
)
387 if ( codepage
== -1 )
390 return WideCharToMultiByte( codepage
, 0, src
, -1, dst
, 0x7fffffff, NULL
, NULL
);
394 /***********************************************************************
397 void WINAPI
Copy16( LPVOID src
, LPVOID dst
, WORD size
)
399 memcpy( dst
, src
, size
);
402 /***********************************************************************
403 * AnsiToOem16 (KEYBOARD.5)
405 INT16 WINAPI
AnsiToOem16( LPCSTR s
, LPSTR d
)
412 /***********************************************************************
413 * OemToAnsi16 (KEYBOARD.6)
415 INT16 WINAPI
OemToAnsi16( LPCSTR s
, LPSTR d
)
422 /***********************************************************************
423 * AnsiToOemBuff16 (KEYBOARD.134)
425 void WINAPI
AnsiToOemBuff16( LPCSTR s
, LPSTR d
, UINT16 len
)
427 if (len
!= 0) CharToOemBuffA( s
, d
, len
);
431 /***********************************************************************
432 * OemToAnsiBuff16 (KEYBOARD.135)
434 void WINAPI
OemToAnsiBuff16( LPCSTR s
, LPSTR d
, UINT16 len
)
436 if (len
!= 0) OemToCharBuffA( s
, d
, len
);
440 /***********************************************************************
441 * CharToOemA (USER32.37)
443 BOOL WINAPI
CharToOemA( LPCSTR s
, LPSTR d
)
445 return CharToOemBuffA( s
, d
, strlen( s
) + 1 );
449 /***********************************************************************
450 * CharToOemBuffA (USER32.38)
452 BOOL WINAPI
CharToOemBuffA( LPCSTR s
, LPSTR d
, DWORD len
)
456 bufW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
459 MultiByteToWideChar( CP_ACP
, 0, s
, len
, bufW
, len
);
460 WideCharToMultiByte( CP_OEMCP
, 0, bufW
, len
, d
, len
, NULL
, NULL
);
461 HeapFree( GetProcessHeap(), 0, bufW
);
467 /***********************************************************************
468 * CharToOemBuffW (USER32.39)
470 BOOL WINAPI
CharToOemBuffW( LPCWSTR s
, LPSTR d
, DWORD len
)
472 WideCharToMultiByte( CP_OEMCP
, 0, s
, len
, d
, len
, NULL
, NULL
);
477 /***********************************************************************
478 * CharToOemW (USER32.40)
480 BOOL WINAPI
CharToOemW( LPCWSTR s
, LPSTR d
)
482 return CharToOemBuffW( s
, d
, strlenW( s
) + 1 );
486 /***********************************************************************
487 * OemToCharA (USER32.402)
489 BOOL WINAPI
OemToCharA( LPCSTR s
, LPSTR d
)
491 return OemToCharBuffA( s
, d
, strlen( s
) + 1 );
495 /***********************************************************************
496 * OemToCharBuffA (USER32.403)
498 BOOL WINAPI
OemToCharBuffA( LPCSTR s
, LPSTR d
, DWORD len
)
502 bufW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
505 MultiByteToWideChar( CP_OEMCP
, 0, s
, len
, bufW
, len
);
506 WideCharToMultiByte( CP_ACP
, 0, bufW
, len
, d
, len
, NULL
, NULL
);
507 HeapFree( GetProcessHeap(), 0, bufW
);
513 /***********************************************************************
514 * OemToCharBuffW (USER32.404)
516 BOOL WINAPI
OemToCharBuffW( LPCSTR s
, LPWSTR d
, DWORD len
)
518 MultiByteToWideChar( CP_OEMCP
, 0, s
, len
, d
, len
);
523 /***********************************************************************
524 * OemToCharW (USER32.405)
526 BOOL WINAPI
OemToCharW( LPCSTR s
, LPWSTR d
)
528 return OemToCharBuffW( s
, d
, strlen( s
) + 1 );
531 /***********************************************************************
532 * lstrrchr (Not a Windows API)
534 * This is the implementation meant to be invoked from within
535 * COMCTL32_StrRChrA and shell32(TODO)...
537 * Return a pointer to the last occurence of wMatch in lpStart
538 * not looking further than lpEnd...
540 LPSTR WINAPI
lstrrchr( LPCSTR lpStart
, LPCSTR lpEnd
, WORD wMatch
)
542 LPCSTR lpGotIt
= NULL
;
544 TRACE("(%p, %p, %x)\n", lpStart
, lpEnd
, wMatch
);
546 if (!lpEnd
) lpEnd
= lpStart
+ strlen(lpStart
);
548 for(; lpStart
< lpEnd
; lpStart
= CharNextA(lpStart
))
549 if (!ChrCmpA( GET_WORD(lpStart
), wMatch
))
552 return ((LPSTR
)lpGotIt
);
555 /***********************************************************************
557 * This fuction returns FALSE if both words match, TRUE otherwise...
559 static BOOL
ChrCmpW( WORD word1
, WORD word2
) {
560 return (word1
!= word2
);
563 /***********************************************************************
564 * lstrrchrw (Not a Windows API)
566 * This is the implementation meant to be invoked form within
567 * COMCTL32_StrRChrW and shell32(TODO)...
569 * Return a pointer to the last occurence of wMatch in lpStart
570 * not looking further than lpEnd...
572 LPWSTR WINAPI
lstrrchrw( LPCWSTR lpStart
, LPCWSTR lpEnd
, WORD wMatch
)
574 LPCWSTR lpGotIt
= NULL
;
576 TRACE("(%p, %p, %x)\n", lpStart
, lpEnd
, wMatch
);
577 if (!lpEnd
) lpEnd
= lpStart
+ lstrlenW(lpStart
);
579 for(; lpStart
< lpEnd
; lpStart
= CharNextW(lpStart
))
580 if (!ChrCmpW( GET_WORD(lpStart
), wMatch
))
583 return (LPWSTR
)lpGotIt
;
586 /***********************************************************************
588 * This fuction returns FALSE if both words match, TRUE otherwise...
590 static BOOL
ChrCmpA( WORD word1
, WORD word2
) {
591 if (LOBYTE(word1
) == LOBYTE(word2
)) {
592 if (IsDBCSLeadByte(LOBYTE(word1
))) {
593 return (word1
!= word2
);