Release 981025.
[wine/gsoc-2012-control.git] / memory / string.c
blobe7c83ccf49918d739aae207ca3936612975ed1cc
1 /*
2 * String functions
4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <ctype.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "winerror.h"
12 #include "ldt.h"
13 #include "debug.h"
14 #include "winnls.h"
16 static const BYTE STRING_Oem2Ansi[256] =
17 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\244"
18 "\020\021\022\023\266\247\026\027\030\031\032\033\034\035\036\037"
19 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
20 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
21 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
22 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
23 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
24 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
25 "\307\374\351\342\344\340\345\347\352\353\350\357\356\354\304\305"
26 "\311\346\306\364\366\362\373\371\377\326\334\242\243\245\120\203"
27 "\341\355\363\372\361\321\252\272\277\137\254\275\274\241\253\273"
28 "\137\137\137\246\246\246\246\053\053\246\246\053\053\053\053\053"
29 "\053\055\055\053\055\053\246\246\053\053\055\055\246\055\053\055"
30 "\055\055\055\053\053\053\053\053\053\053\053\137\137\246\137\137"
31 "\137\337\137\266\137\137\265\137\137\137\137\137\137\137\137\137"
32 "\137\261\137\137\137\137\367\137\260\225\267\137\156\262\137\137";
34 static const BYTE STRING_Ansi2Oem[256] =
35 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
36 "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
37 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
38 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
39 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
40 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
41 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
42 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
43 "\200\201\054\237\054\137\375\374\210\045\123\074\117\215\216\217"
44 "\220\140\047\042\042\371\055\137\230\231\163\076\157\235\236\131"
45 "\040\255\233\234\017\235\335\025\042\143\246\256\252\055\162\137"
46 "\370\361\375\063\047\346\024\372\054\061\247\257\254\253\137\250"
47 "\101\101\101\101\216\217\222\200\105\220\105\105\111\111\111\111"
48 "\104\245\117\117\117\117\231\170\117\125\125\125\232\131\137\341"
49 "\205\240\203\141\204\206\221\207\212\202\210\211\215\241\214\213"
50 "\144\244\225\242\223\157\224\366\157\227\243\226\201\171\137\230";
52 #define OEM_TO_ANSI(ch) (STRING_Oem2Ansi[(unsigned char)(ch)])
53 #define ANSI_TO_OEM(ch) (STRING_Ansi2Oem[(unsigned char)(ch)])
56 /***********************************************************************
57 * hmemcpy (KERNEL.348)
59 void WINAPI hmemcpy( LPVOID dst, LPCVOID src, LONG count )
61 memcpy( dst, src, count );
65 /***********************************************************************
66 * lstrcat16 (KERNEL.89)
68 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
70 lstrcat32A( (LPSTR)PTR_SEG_TO_LIN(dst), src );
71 return dst;
75 /***********************************************************************
76 * lstrcat32A (KERNEL32.599)
78 LPSTR WINAPI lstrcat32A( LPSTR dst, LPCSTR src )
80 TRACE(string,"Append %s to %s\n",
81 debugstr_a (src), debugstr_a (dst));
82 /* Windows does not check for NULL pointers here, so we don't either */
83 strcat( dst, src );
84 return dst;
88 /***********************************************************************
89 * lstrcat32W (KERNEL32.600)
91 LPWSTR WINAPI lstrcat32W( LPWSTR dst, LPCWSTR src )
93 register LPWSTR p = dst;
94 TRACE(string,"Append L%s to L%s\n",
95 debugstr_w (src), debugstr_w (dst));
96 /* Windows does not check for NULL pointers here, so we don't either */
97 while (*p) p++;
98 while ((*p++ = *src++));
99 return dst;
103 /***********************************************************************
104 * lstrcatn16 (KERNEL.352)
106 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
108 lstrcatn32A( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
109 return dst;
113 /***********************************************************************
114 * lstrcatn32A (Not a Windows API)
116 LPSTR WINAPI lstrcatn32A( LPSTR dst, LPCSTR src, INT32 n )
118 register LPSTR p = dst;
119 TRACE(string,"strcatn add %d chars from %s to %s\n",
120 n, debugstr_an (src, n), debugstr_a (dst));
121 while (*p) p++;
122 if ((n -= (INT32)(p - dst)) <= 0) return dst;
123 lstrcpyn32A( p, src, n );
124 return dst;
128 /***********************************************************************
129 * lstrcatn32W (Not a Windows API)
131 LPWSTR WINAPI lstrcatn32W( LPWSTR dst, LPCWSTR src, INT32 n )
133 register LPWSTR p = dst;
134 TRACE(string,"strcatn add %d chars from L%s to L%s\n",
135 n, debugstr_wn (src, n), debugstr_w (dst));
136 while (*p) p++;
137 if ((n -= (INT32)(p - dst)) <= 0) return dst;
138 lstrcpyn32W( p, src, n );
139 return dst;
143 /***********************************************************************
144 * lstrcmp16 (USER.430)
146 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
148 return (INT16)strcmp( str1, str2 );
152 /***********************************************************************
153 * lstrcmp32A (KERNEL.602)
155 INT32 WINAPI lstrcmp32A( LPCSTR str1, LPCSTR str2 )
157 return CompareString32A(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
161 /***********************************************************************
162 * lstrcmp32W (KERNEL.603)
163 * FIXME : should call CompareString32W, when it is implemented.
164 * This implementation is not "word sort", as it should.
166 INT32 WINAPI lstrcmp32W( LPCWSTR str1, LPCWSTR str2 )
168 TRACE(string,"L%s and L%s\n",
169 debugstr_w (str1), debugstr_w (str2));
170 if (!str1 || !str2) {
171 SetLastError(ERROR_INVALID_PARAMETER);
172 return 0;
174 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
175 return (INT32)(*str1 - *str2);
179 /***********************************************************************
180 * lstrcmpi16 (USER.471)
182 INT16 WINAPI lstrcmpi16( LPCSTR str1, LPCSTR str2 )
184 return (INT16)lstrcmpi32A( str1, str2 );
188 /***********************************************************************
189 * lstrcmpi32A (KERNEL32.605)
191 INT32 WINAPI lstrcmpi32A( LPCSTR str1, LPCSTR str2 )
192 { TRACE(string,"strcmpi %s and %s\n",
193 debugstr_a (str1), debugstr_a (str2));
194 return CompareString32A(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
198 /***********************************************************************
199 * lstrcmpi32W (KERNEL32.606)
201 INT32 WINAPI lstrcmpi32W( LPCWSTR str1, LPCWSTR str2 )
203 INT32 res;
205 #if 0
206 /* Too much! (From registry loading.) */
207 TRACE(string,"strcmpi L%s and L%s\n",
208 debugstr_w (str1), debugstr_w (str2));
209 #endif
210 if (!str1 || !str2) {
211 SetLastError(ERROR_INVALID_PARAMETER);
212 return 0;
214 while (*str1)
216 if ((*str1<0x100 ) && (*str2<0x100)) {
217 if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
218 } else {
219 if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
221 str1++;
222 str2++;
224 return towupper(*str1) - towupper(*str2);
228 /***********************************************************************
229 * lstrcpy16 (KERNEL.88)
231 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
233 lstrcpy32A( (LPSTR)PTR_SEG_TO_LIN(dst), src );
234 return dst;
238 /***********************************************************************
239 * lstrcpy32A (KERNEL32.608)
241 LPSTR WINAPI lstrcpy32A( LPSTR dst, LPCSTR src )
243 TRACE(string,"strcpy %s\n", debugstr_a (src));
244 /* Windows does not check for NULL pointers here, so we don't either */
245 strcpy( dst, src );
246 return dst;
250 /***********************************************************************
251 * lstrcpy32W (KERNEL32.609)
253 LPWSTR WINAPI lstrcpy32W( LPWSTR dst, LPCWSTR src )
255 register LPWSTR p = dst;
256 TRACE(string,"strcpy L%s\n", debugstr_w (src));
257 /* Windows does not check for NULL pointers here, so we don't either */
258 while ((*p++ = *src++));
259 return dst;
263 /***********************************************************************
264 * lstrcpyn16 (KERNEL.353)
266 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
268 lstrcpyn32A( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
269 return dst;
273 /***********************************************************************
274 * lstrcpyn32A (KERNEL32.611)
275 * Note: this function differs from the UNIX strncpy, it _always_ writes
276 * a terminating \0
278 LPSTR WINAPI lstrcpyn32A( LPSTR dst, LPCSTR src, INT32 n )
280 LPSTR p = dst;
281 TRACE(string,"strcpyn %s for %d chars\n",
282 debugstr_an (src,n), n);
283 /* Windows does not check for NULL pointers here, so we don't either */
284 while ((n-- > 1) && *src) *p++ = *src++;
285 if (n >= 0) *p = 0;
286 return dst;
290 /***********************************************************************
291 * lstrcpyn32W (KERNEL32.612)
292 * Note: this function differs from the UNIX strncpy, it _always_ writes
293 * a terminating \0
295 LPWSTR WINAPI lstrcpyn32W( LPWSTR dst, LPCWSTR src, INT32 n )
297 LPWSTR p = dst;
298 TRACE(string,"strcpyn L%s for %d chars\n",
299 debugstr_wn (src,n), n);
300 /* Windows does not check for NULL pointers here, so we don't either */
301 while ((n-- > 1) && *src) *p++ = *src++;
302 if (n >= 0) *p = 0;
303 return dst;
307 /***********************************************************************
308 * lstrlen16 (KERNEL.90)
310 INT16 WINAPI lstrlen16( LPCSTR str )
312 return (INT16)lstrlen32A( str );
316 /***********************************************************************
317 * lstrlen32A (KERNEL32.614)
319 INT32 WINAPI lstrlen32A( LPCSTR str )
321 /* looks weird, but win3.1 KERNEL got a GeneralProtection handler
322 * in lstrlen() ... we check only for NULL pointer reference.
323 * - Marcus Meissner
325 TRACE(string,"strlen %s\n", debugstr_a (str));
326 if (!str) return 0;
327 return (INT32)strlen(str);
331 /***********************************************************************
332 * lstrlen32W (KERNEL32.615)
334 INT32 WINAPI lstrlen32W( LPCWSTR str )
336 INT32 len = 0;
337 TRACE(string,"strlen L%s\n", debugstr_w (str));
338 if (!str) return 0;
339 while (*str++) len++;
340 return len;
344 /***********************************************************************
345 * lstrncmp32A (Not a Windows API)
347 INT32 WINAPI lstrncmp32A( LPCSTR str1, LPCSTR str2, INT32 n )
349 TRACE(string,"strncmp %s and %s for %d chars\n",
350 debugstr_an (str1, n), debugstr_an (str2, n), n);
351 return (INT32)strncmp( str1, str2, n );
355 /***********************************************************************
356 * lstrncmp32W (Not a Windows API)
358 INT32 WINAPI lstrncmp32W( LPCWSTR str1, LPCWSTR str2, INT32 n )
360 TRACE(string,"strncmp L%s and L%s for %d chars\n",
361 debugstr_wn (str1, n), debugstr_wn (str2, n), n);
362 if (!n) return 0;
363 while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
364 return (INT32)(*str1 - *str2);
368 /***********************************************************************
369 * lstrncmpi32A (Not a Windows API)
371 INT32 WINAPI lstrncmpi32A( LPCSTR str1, LPCSTR str2, INT32 n )
373 INT32 res;
375 TRACE(string,"strncmpi %s and %s for %d chars\n",
376 debugstr_an (str1, n), debugstr_an (str2, n), n);
377 if (!n) return 0;
378 while ((--n > 0) && *str1)
379 if ( (res = toupper(*str1++) - toupper(*str2++)) ) return res;
381 return toupper(*str1) - toupper(*str2);
385 /***********************************************************************
386 * lstrncmpi32W (Not a Windows API)
388 INT32 WINAPI lstrncmpi32W( LPCWSTR str1, LPCWSTR str2, INT32 n )
390 INT32 res;
392 TRACE(string,"strncmpi L%s and L%s for %d chars\n",
393 debugstr_wn (str1, n), debugstr_wn (str2, n), n);
394 if (!n) return 0;
395 while ((--n > 0) && *str1)
397 if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
398 str1++;
399 str2++;
401 return towupper(*str1) - towupper(*str2);
405 /***********************************************************************
406 * lstrcpyAtoW (Not a Windows API)
408 LPWSTR WINAPI lstrcpyAtoW( LPWSTR dst, LPCSTR src )
410 register LPWSTR p = dst;
412 TRACE(string,"%s\n",src);
414 while ((*p++ = (WCHAR)(unsigned char)*src++));
415 return dst;
419 /***********************************************************************
420 * lstrcpyWtoA (Not a Windows API)
422 LPSTR WINAPI lstrcpyWtoA( LPSTR dst, LPCWSTR src )
424 register LPSTR p = dst;
426 TRACE(string,"L%s\n",debugstr_w(src));
428 while ((*p++ = (CHAR)*src++));
429 return dst;
433 /***********************************************************************
434 * lstrcpynAtoW (Not a Windows API)
435 * Note: this function differs from the UNIX strncpy, it _always_ writes
436 * a terminating \0
438 LPWSTR WINAPI lstrcpynAtoW( LPWSTR dst, LPCSTR src, INT32 n )
440 LPWSTR p = dst;
442 TRACE(string,"%s %i\n",src, n);
444 while ((n-- > 1) && *src) *p++ = (WCHAR)(unsigned char)*src++;
445 if (n >= 0) *p = 0;
446 return dst;
450 /***********************************************************************
451 * lstrcpynWtoA (Not a Windows API)
452 * Note: this function differs from the UNIX strncpy, it _always_ writes
453 * a terminating \0
455 LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT32 n )
457 LPSTR p = dst;
459 TRACE(string,"L%s %i\n",debugstr_w(src), n);
461 while ((n-- > 1) && *src) *p++ = (CHAR)*src++;
462 if (n >= 0) *p = 0;
463 return dst;
467 /***********************************************************************
468 * Copy (GDI.250)
470 void WINAPI Copy( LPVOID src, LPVOID dst, WORD size )
472 memcpy( dst, src, size );
476 /***********************************************************************
477 * RtlFillMemory (KERNEL32.441)
479 VOID WINAPI RtlFillMemory( LPVOID ptr, UINT32 len, UINT32 fill )
481 memset( ptr, fill, len );
485 /***********************************************************************
486 * RtlMoveMemory (KERNEL32.442)
488 VOID WINAPI RtlMoveMemory( LPVOID dst, LPCVOID src, UINT32 len )
490 memmove( dst, src, len );
494 /***********************************************************************
495 * RtlZeroMemory (KERNEL32.444)
497 VOID WINAPI RtlZeroMemory( LPVOID ptr, UINT32 len )
499 memset( ptr, 0, len );
503 /***********************************************************************
504 * AnsiToOem16 (KEYBOARD.5)
506 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
508 CharToOem32A( s, d );
509 return -1;
513 /***********************************************************************
514 * OemToAnsi16 (KEYBOARD.6)
516 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
518 OemToChar32A( s, d );
519 return -1;
523 /***********************************************************************
524 * AnsiToOemBuff16 (KEYBOARD.134)
526 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
528 CharToOemBuff32A( s, d, len ? len : 65536 );
532 /***********************************************************************
533 * OemToAnsiBuff16 (KEYBOARD.135)
535 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
537 OemToCharBuff32A( s, d, len ? len : 65536 );
541 /***********************************************************************
542 * CharToOem32A (USER32.37)
544 BOOL32 WINAPI CharToOem32A( LPCSTR s, LPSTR d )
546 LPSTR oldd = d;
547 if (!s || !d) return TRUE;
548 TRACE(string,"CharToOem %s\n", debugstr_a (s));
549 while ((*d++ = ANSI_TO_OEM(*s++)));
550 TRACE(string," to %s\n", debugstr_a (oldd));
551 return TRUE;
555 /***********************************************************************
556 * CharToOemBuff32A (USER32.38)
558 BOOL32 WINAPI CharToOemBuff32A( LPCSTR s, LPSTR d, DWORD len )
560 while (len--) *d++ = ANSI_TO_OEM(*s++);
561 return TRUE;
565 /***********************************************************************
566 * CharToOemBuff32W (USER32.39)
568 BOOL32 WINAPI CharToOemBuff32W( LPCWSTR s, LPSTR d, DWORD len )
570 while (len--) *d++ = ANSI_TO_OEM(*s++);
571 return TRUE;
575 /***********************************************************************
576 * CharToOem32W (USER32.40)
578 BOOL32 WINAPI CharToOem32W( LPCWSTR s, LPSTR d )
580 LPSTR oldd = d;
581 if (!s || !d) return TRUE;
582 TRACE(string,"CharToOem L%s\n", debugstr_w (s));
583 while ((*d++ = ANSI_TO_OEM(*s++)));
584 TRACE(string," to %s\n", debugstr_a (oldd));
585 return TRUE;
589 /***********************************************************************
590 * OemToChar32A (USER32.402)
592 BOOL32 WINAPI OemToChar32A( LPCSTR s, LPSTR d )
594 LPSTR oldd = d;
595 TRACE(string,"OemToChar %s\n", debugstr_a (s));
596 while ((*d++ = OEM_TO_ANSI(*s++)));
597 TRACE(string," to %s\n", debugstr_a (oldd));
598 return TRUE;
602 /***********************************************************************
603 * OemToCharBuff32A (USER32.403)
605 BOOL32 WINAPI OemToCharBuff32A( LPCSTR s, LPSTR d, DWORD len )
607 TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
608 while (len--) *d++ = OEM_TO_ANSI(*s++);
609 return TRUE;
613 /***********************************************************************
614 * OemToCharBuff32W (USER32.404)
616 BOOL32 WINAPI OemToCharBuff32W( LPCSTR s, LPWSTR d, DWORD len )
618 TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
619 while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
620 return TRUE;
624 /***********************************************************************
625 * OemToChar32W (USER32.405)
627 BOOL32 WINAPI OemToChar32W( LPCSTR s, LPWSTR d )
629 while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++)));
630 return TRUE;
633 /***********************************************************************
634 * WideCharToLocal (Not a Windows API)
635 * similar lstrcpyWtoA, should handle codepages properly
637 * RETURNS
638 * strlen of the destination string
641 INT32 WINAPI WideCharToLocal32(
642 LPSTR pLocal,
643 LPWSTR pWide,
644 INT32 dwChars)
645 { *pLocal = 0;
646 TRACE(string,"(%p, %s, %i)\n", pLocal, debugstr_w(pWide),dwChars);
647 WideCharToMultiByte(CP_ACP,0,pWide,-1,pLocal,dwChars,NULL,NULL);
648 return strlen(pLocal);
650 /***********************************************************************
651 * LocalToWideChar (Not a Windows API)
652 * similar lstrcpyAtoW, should handle codepages properly
654 * RETURNS
655 * strlen of the destination string
657 INT32 WINAPI LocalToWideChar32(
658 LPWSTR pWide,
659 LPSTR pLocal,
660 INT32 dwChars)
661 { *pWide = 0;
662 TRACE(string,"(%p, %s, %i)\n",pWide, pLocal, dwChars);
663 MultiByteToWideChar(CP_ACP,0,pLocal,-1,pWide,dwChars);
664 return lstrlen32W(pWide);