Rewrote codepage support to use the new codepage tables.
[wine/multimedia.git] / memory / string.c
blobf61932ec6fdd89c1f0127c79d66ba745ccfbd38a
1 /*
2 * String functions
4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <ctype.h>
9 #include <string.h>
11 #include "windef.h"
12 #include "winbase.h"
13 #include "wingdi.h"
14 #include "winuser.h"
15 #include "ntddk.h"
16 #include "wine/winbase16.h"
17 #include "wine/winuser16.h"
18 #include "wine/keyboard16.h"
19 #include "wine/exception.h"
20 #include "winerror.h"
21 #include "crtdll.h"
22 #include "ldt.h"
23 #include "debugtools.h"
24 #include "winnls.h"
26 DEFAULT_DEBUG_CHANNEL(string)
28 static const BYTE STRING_Oem2Ansi[256] =
29 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\244"
30 "\020\021\022\023\266\247\026\027\030\031\032\033\034\035\036\037"
31 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
32 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
33 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
34 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
35 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
36 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
37 "\307\374\351\342\344\340\345\347\352\353\350\357\356\354\304\305"
38 "\311\346\306\364\366\362\373\371\377\326\334\242\243\245\120\203"
39 "\341\355\363\372\361\321\252\272\277\137\254\275\274\241\253\273"
40 "\137\137\137\246\246\246\246\053\053\246\246\053\053\053\053\053"
41 "\053\055\055\053\055\053\246\246\053\053\055\055\246\055\053\055"
42 "\055\055\055\053\053\053\053\053\053\053\053\137\137\246\137\137"
43 "\137\337\137\266\137\137\265\137\137\137\137\137\137\137\137\137"
44 "\137\261\137\137\137\137\367\137\260\225\267\137\156\262\137\137";
46 static const BYTE STRING_Ansi2Oem[256] =
47 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
48 "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
49 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
50 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
51 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
52 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
53 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
54 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
55 "\200\201\054\237\054\137\375\374\210\045\123\074\117\215\216\217"
56 "\220\140\047\042\042\371\055\137\230\231\163\076\157\235\236\131"
57 "\040\255\233\234\017\235\335\025\042\143\246\256\252\055\162\137"
58 "\370\361\375\063\047\346\024\372\054\061\247\257\254\253\137\250"
59 "\101\101\101\101\216\217\222\200\105\220\105\105\111\111\111\111"
60 "\104\245\117\117\117\117\231\170\117\125\125\125\232\131\137\341"
61 "\205\240\203\141\204\206\221\207\212\202\210\211\215\241\214\213"
62 "\144\244\225\242\223\157\224\366\157\227\243\226\201\171\137\230";
64 #define OEM_TO_ANSI(ch) (STRING_Oem2Ansi[(unsigned char)(ch)])
65 #define ANSI_TO_OEM(ch) (STRING_Ansi2Oem[(unsigned char)(ch)])
67 /* Internaly used by strchr family functions */
68 static BOOL ChrCmpA( WORD word1, WORD word2);
71 /* filter for page-fault exceptions */
72 static WINE_EXCEPTION_FILTER(page_fault)
74 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
75 return EXCEPTION_EXECUTE_HANDLER;
76 return EXCEPTION_CONTINUE_SEARCH;
80 /***********************************************************************
81 * hmemcpy (KERNEL.348)
83 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
85 memcpy( dst, src, count );
89 /***********************************************************************
90 * lstrcat16 (KERNEL.89)
92 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
94 /* Windows does not check for NULL pointers here, so we don't either */
95 strcat( (LPSTR)PTR_SEG_TO_LIN(dst), src );
96 return dst;
100 /***********************************************************************
101 * lstrcatA (KERNEL32.599)
103 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
105 __TRY
107 strcat( dst, src );
109 __EXCEPT(page_fault)
111 SetLastError( ERROR_INVALID_PARAMETER );
112 return NULL;
114 __ENDTRY
115 return dst;
119 /***********************************************************************
120 * lstrcatW (KERNEL32.600)
122 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
124 __TRY
126 CRTDLL_wcscat( dst, src );
128 __EXCEPT(page_fault)
130 SetLastError( ERROR_INVALID_PARAMETER );
131 return NULL;
133 __ENDTRY
134 return dst;
138 /***********************************************************************
139 * lstrcatn16 (KERNEL.352)
141 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
143 LPSTR p = (LPSTR)PTR_SEG_TO_LIN(dst);
145 while (*p) p++;
146 if ((n -= (p - (LPSTR)PTR_SEG_TO_LIN(dst))) <= 0) return dst;
147 lstrcpynA( p, src, n );
148 return dst;
152 /***********************************************************************
153 * lstrcmp16 (USER.430)
155 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
157 return (INT16)strcmp( str1, str2 );
161 /***********************************************************************
162 * lstrcmpA (KERNEL.602)
164 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
166 return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
170 /***********************************************************************
171 * lstrcmpW (KERNEL.603)
172 * FIXME : should call CompareStringW, when it is implemented.
173 * This implementation is not "word sort", as it should.
175 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
177 TRACE("%s and %s\n",
178 debugstr_w (str1), debugstr_w (str2));
179 if (!str1 || !str2) {
180 SetLastError(ERROR_INVALID_PARAMETER);
181 return 0;
183 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
184 return (INT)(*str1 - *str2);
188 /***********************************************************************
189 * lstrcmpi16 (USER.471)
191 INT16 WINAPI lstrcmpi16( LPCSTR str1, LPCSTR str2 )
193 return (INT16)lstrcmpiA( str1, str2 );
197 /***********************************************************************
198 * lstrcmpiA (KERNEL32.605)
200 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
201 { TRACE("strcmpi %s and %s\n",
202 debugstr_a (str1), debugstr_a (str2));
203 return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
207 /***********************************************************************
208 * lstrcmpiW (KERNEL32.606)
210 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
212 INT res;
214 #if 0
215 /* Too much! (From registry loading.) */
216 TRACE("strcmpi %s and %s\n",
217 debugstr_w (str1), debugstr_w (str2));
218 #endif
219 if (!str1 || !str2) {
220 SetLastError(ERROR_INVALID_PARAMETER);
221 return 0;
223 while (*str1)
225 if ((*str1<0x100 ) && (*str2<0x100)) {
226 if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
227 } else {
228 if ((res = NTDLL_towupper(*str1) - NTDLL_towupper(*str2)) != 0) return res;
230 str1++;
231 str2++;
233 return NTDLL_towupper(*str1) - NTDLL_towupper(*str2);
237 /***********************************************************************
238 * lstrcpy16 (KERNEL.88)
240 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
242 if (!lstrcpyA( PTR_SEG_TO_LIN(dst), src )) dst = 0;
243 return dst;
247 /***********************************************************************
248 * lstrcpyA (KERNEL32.608)
250 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
252 __TRY
254 /* this is how Windows does it */
255 memmove( dst, src, strlen(src)+1 );
257 __EXCEPT(page_fault)
259 ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
260 SetLastError( ERROR_INVALID_PARAMETER );
261 return NULL;
263 __ENDTRY
264 return dst;
268 /***********************************************************************
269 * lstrcpyW (KERNEL32.609)
271 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
273 __TRY
275 CRTDLL_wcscpy( dst, src );
277 __EXCEPT(page_fault)
279 SetLastError( ERROR_INVALID_PARAMETER );
280 return NULL;
282 __ENDTRY
283 return dst;
287 /***********************************************************************
288 * lstrcpyn16 (KERNEL.353)
290 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
292 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
293 return dst;
297 /***********************************************************************
298 * lstrcpynA (KERNEL32.611)
299 * Note: this function differs from the UNIX strncpy, it _always_ writes
300 * a terminating \0
302 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
304 LPSTR p = dst;
305 TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
306 /* In real windows the whole function is protected by an exception handler
307 * that returns ERROR_INVALID_PARAMETER on faulty parameters
308 * We currently just check for NULL.
310 if (!dst || !src) {
311 SetLastError(ERROR_INVALID_PARAMETER);
312 return 0;
314 while ((n-- > 1) && *src) *p++ = *src++;
315 if (n >= 0) *p = 0;
316 return dst;
320 /***********************************************************************
321 * lstrcpynW (KERNEL32.612)
322 * Note: this function differs from the UNIX strncpy, it _always_ writes
323 * a terminating \0
325 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
327 LPWSTR p = dst;
328 TRACE("(%p, %s, %i)\n", dst, debugstr_wn(src,n), n);
329 /* In real windows the whole function is protected by an exception handler
330 * that returns ERROR_INVALID_PARAMETER on faulty parameters
331 * We currently just check for NULL.
333 if (!dst || !src) {
334 SetLastError(ERROR_INVALID_PARAMETER);
335 return 0;
337 while ((n-- > 1) && *src) *p++ = *src++;
338 if (n >= 0) *p = 0;
339 return dst;
343 /***********************************************************************
344 * lstrlen16 (KERNEL.90)
346 INT16 WINAPI lstrlen16( LPCSTR str )
348 return (INT16)lstrlenA( str );
352 /***********************************************************************
353 * lstrlenA (KERNEL32.614)
355 INT WINAPI lstrlenA( LPCSTR str )
357 INT ret;
358 __TRY
360 ret = strlen(str);
362 __EXCEPT(page_fault)
364 SetLastError( ERROR_INVALID_PARAMETER );
365 return 0;
367 __ENDTRY
368 return ret;
372 /***********************************************************************
373 * lstrlenW (KERNEL32.615)
375 INT WINAPI lstrlenW( LPCWSTR str )
377 INT ret;
378 __TRY
380 ret = CRTDLL_wcslen(str);
382 __EXCEPT(page_fault)
384 SetLastError( ERROR_INVALID_PARAMETER );
385 return 0;
387 __ENDTRY
388 return ret;
392 /***********************************************************************
393 * lstrcpyAtoW (Not a Windows API)
395 LPWSTR WINAPI lstrcpyAtoW( LPWSTR dst, LPCSTR src )
397 MultiByteToWideChar( CP_ACP, 0, src, -1, dst, 0x7fffffff );
398 return dst;
402 /***********************************************************************
403 * lstrcpyWtoA (Not a Windows API)
405 LPSTR WINAPI lstrcpyWtoA( LPSTR dst, LPCWSTR src )
407 WideCharToMultiByte( CP_ACP, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
408 return dst;
412 /***********************************************************************
413 * lstrcpynAtoW (Not a Windows API)
414 * Note: this function differs from the UNIX strncpy, it _always_ writes
415 * a terminating \0
417 LPWSTR WINAPI lstrcpynAtoW( LPWSTR dst, LPCSTR src, INT n )
419 if (n > 0 && !MultiByteToWideChar( CP_ACP, 0, src, -1, dst, n )) dst[n-1] = 0;
420 return dst;
424 /***********************************************************************
425 * lstrcpynWtoA (Not a Windows API)
426 * Note: this function differs from the UNIX strncpy, it _always_ writes
427 * a terminating \0
429 * The terminating zero should be written at the end of the string, not
430 * the end of the buffer, as some programs specify the wrong size for
431 * the buffer (eg. winnt's sol.exe)
433 LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT n )
435 if (n > 0 && !WideCharToMultiByte( CP_ACP, 0, src, -1, dst, n, NULL, NULL )) dst[n-1] = 0;
436 return dst;
439 /***********************************************************************
440 * UnicodeToAnsi (KERNEL.434)
442 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
444 if ( codepage != -1 )
445 FIXME("codepage %d not supported\n", codepage );
447 lstrcpyWtoA( dst, src );
449 return (INT16)lstrlenA( dst );
453 /***********************************************************************
454 * Copy (GDI.250)
456 void WINAPI Copy16( LPVOID src, LPVOID dst, WORD size )
458 memcpy( dst, src, size );
461 /***********************************************************************
462 * AnsiToOem16 (KEYBOARD.5)
464 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
466 CharToOemA( s, d );
467 return -1;
471 /***********************************************************************
472 * OemToAnsi16 (KEYBOARD.6)
474 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
476 OemToCharA( s, d );
477 return -1;
481 /***********************************************************************
482 * AnsiToOemBuff16 (KEYBOARD.134)
484 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
486 if (len != 0) CharToOemBuffA( s, d, len );
490 /***********************************************************************
491 * OemToAnsiBuff16 (KEYBOARD.135)
493 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
495 if (len != 0) OemToCharBuffA( s, d, len );
499 /***********************************************************************
500 * CharToOemA (USER32.37)
502 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
504 LPSTR oldd = d;
505 if (!s || !d) return TRUE;
506 TRACE("CharToOem %s\n", debugstr_a (s));
507 while ((*d++ = ANSI_TO_OEM(*s++)));
508 TRACE(" to %s\n", debugstr_a (oldd));
509 return TRUE;
513 /***********************************************************************
514 * CharToOemBuffA (USER32.38)
516 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
518 while (len--) *d++ = ANSI_TO_OEM(*s++);
519 return TRUE;
523 /***********************************************************************
524 * CharToOemBuffW (USER32.39)
526 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
528 while (len--) *d++ = ANSI_TO_OEM(*s++);
529 return TRUE;
533 /***********************************************************************
534 * CharToOemW (USER32.40)
536 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
538 LPSTR oldd = d;
539 if (!s || !d) return TRUE;
540 TRACE("CharToOem %s\n", debugstr_w (s));
541 while ((*d++ = ANSI_TO_OEM(*s++)));
542 TRACE(" to %s\n", debugstr_a (oldd));
543 return TRUE;
547 /***********************************************************************
548 * OemToCharA (USER32.402)
550 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
552 LPSTR oldd = d;
553 TRACE("OemToChar %s\n", debugstr_a (s));
554 while ((*d++ = OEM_TO_ANSI(*s++)));
555 TRACE(" to %s\n", debugstr_a (oldd));
556 return TRUE;
560 /***********************************************************************
561 * OemToCharBuffA (USER32.403)
563 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
565 TRACE("OemToCharBuff %s\n", debugstr_an (s, len));
566 while (len--) *d++ = OEM_TO_ANSI(*s++);
567 return TRUE;
571 /***********************************************************************
572 * OemToCharBuffW (USER32.404)
574 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
576 TRACE("OemToCharBuff %s\n", debugstr_an (s, len));
577 while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
578 return TRUE;
582 /***********************************************************************
583 * OemToCharW (USER32.405)
585 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
587 while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++)));
588 return TRUE;
591 /***********************************************************************
592 * lstrrchr (Not a Windows API)
594 * This is the implementation meant to be invoked from within
595 * COMCTL32_StrRChrA and shell32(TODO)...
597 * Return a pointer to the last occurence of wMatch in lpStart
598 * not looking further than lpEnd...
600 LPSTR WINAPI lstrrchr( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
602 LPCSTR lpGotIt = NULL;
604 TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
606 if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
608 for(; lpStart < lpEnd; lpStart = CharNextA(lpStart))
609 if (!ChrCmpA( GET_WORD(lpStart), wMatch))
610 lpGotIt = lpStart;
612 return ((LPSTR)lpGotIt);
615 /***********************************************************************
616 * ChrCmpW
617 * This fuction returns FALSE if both words match, TRUE otherwise...
619 static BOOL ChrCmpW( WORD word1, WORD word2) {
620 return (word1 != word2);
623 /***********************************************************************
624 * lstrrchrw (Not a Windows API)
626 * This is the implementation meant to be invoked form within
627 * COMCTL32_StrRChrW and shell32(TODO)...
629 * Return a pointer to the last occurence of wMatch in lpStart
630 * not looking further than lpEnd...
632 LPWSTR WINAPI lstrrchrw( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch )
634 LPCWSTR lpGotIt = NULL;
636 TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
637 if (!lpEnd) lpEnd = lpStart + lstrlenW(lpStart);
639 for(; lpStart < lpEnd; lpStart = CharNextW(lpStart))
640 if (!ChrCmpW( GET_WORD(lpStart), wMatch))
641 lpGotIt = lpStart;
643 return (LPWSTR)lpGotIt;
646 /***********************************************************************
647 * ChrCmpA
648 * This fuction returns FALSE if both words match, TRUE otherwise...
650 static BOOL ChrCmpA( WORD word1, WORD word2) {
651 if (LOBYTE(word1) == LOBYTE(word2)) {
652 if (IsDBCSLeadByte(LOBYTE(word1))) {
653 return (word1 != word2);
655 return FALSE;
657 return TRUE;