4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
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)])
55 /* Internaly used by strchr family functions */
56 static BOOL32
ChrCmpA( WORD word1
, WORD word2
);
57 static BOOL32
ChrCmpW( WORD word1
, WORD word2
);
59 extern LPWSTR __cdecl
CRTDLL_wcschr(LPCWSTR str
,WCHAR xchar
);
62 /***********************************************************************
63 * hmemcpy (KERNEL.348)
65 void WINAPI
hmemcpy( LPVOID dst
, LPCVOID src
, LONG count
)
67 memcpy( dst
, src
, count
);
71 /***********************************************************************
72 * lstrcat16 (KERNEL.89)
74 SEGPTR WINAPI
lstrcat16( SEGPTR dst
, LPCSTR src
)
76 lstrcat32A( (LPSTR
)PTR_SEG_TO_LIN(dst
), src
);
81 /***********************************************************************
82 * lstrcat32A (KERNEL32.599)
84 LPSTR WINAPI
lstrcat32A( LPSTR dst
, LPCSTR src
)
86 TRACE(string
,"Append %s to %s\n",
87 debugstr_a (src
), debugstr_a (dst
));
88 /* Windows does not check for NULL pointers here, so we don't either */
94 /***********************************************************************
95 * lstrcat32W (KERNEL32.600)
97 LPWSTR WINAPI
lstrcat32W( LPWSTR dst
, LPCWSTR src
)
99 register LPWSTR p
= dst
;
100 TRACE(string
,"Append L%s to L%s\n",
101 debugstr_w (src
), debugstr_w (dst
));
102 /* Windows does not check for NULL pointers here, so we don't either */
104 while ((*p
++ = *src
++));
109 /***********************************************************************
110 * lstrcatn16 (KERNEL.352)
112 SEGPTR WINAPI
lstrcatn16( SEGPTR dst
, LPCSTR src
, INT16 n
)
114 lstrcatn32A( (LPSTR
)PTR_SEG_TO_LIN(dst
), src
, n
);
119 /***********************************************************************
120 * lstrcatn32A (Not a Windows API)
122 LPSTR WINAPI
lstrcatn32A( LPSTR dst
, LPCSTR src
, INT32 n
)
124 register LPSTR p
= dst
;
125 TRACE(string
,"strcatn add %d chars from %s to %s\n",
126 n
, debugstr_an (src
, n
), debugstr_a (dst
));
128 if ((n
-= (INT32
)(p
- dst
)) <= 0) return dst
;
129 lstrcpyn32A( p
, src
, n
);
134 /***********************************************************************
135 * lstrcatn32W (Not a Windows API)
137 LPWSTR WINAPI
lstrcatn32W( LPWSTR dst
, LPCWSTR src
, INT32 n
)
139 register LPWSTR p
= dst
;
140 TRACE(string
,"strcatn add %d chars from L%s to L%s\n",
141 n
, debugstr_wn (src
, n
), debugstr_w (dst
));
143 if ((n
-= (INT32
)(p
- dst
)) <= 0) return dst
;
144 lstrcpyn32W( p
, src
, n
);
149 /***********************************************************************
150 * lstrcmp16 (USER.430)
152 INT16 WINAPI
lstrcmp16( LPCSTR str1
, LPCSTR str2
)
154 return (INT16
)strcmp( str1
, str2
);
158 /***********************************************************************
159 * lstrcmp32A (KERNEL.602)
161 INT32 WINAPI
lstrcmp32A( LPCSTR str1
, LPCSTR str2
)
163 return CompareString32A(LOCALE_SYSTEM_DEFAULT
,0,str1
,-1,str2
,-1) - 2 ;
167 /***********************************************************************
168 * lstrcmp32W (KERNEL.603)
169 * FIXME : should call CompareString32W, when it is implemented.
170 * This implementation is not "word sort", as it should.
172 INT32 WINAPI
lstrcmp32W( LPCWSTR str1
, LPCWSTR str2
)
174 TRACE(string
,"L%s and L%s\n",
175 debugstr_w (str1
), debugstr_w (str2
));
176 if (!str1
|| !str2
) {
177 SetLastError(ERROR_INVALID_PARAMETER
);
180 while (*str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
181 return (INT32
)(*str1
- *str2
);
185 /***********************************************************************
186 * lstrcmpi16 (USER.471)
188 INT16 WINAPI
lstrcmpi16( LPCSTR str1
, LPCSTR str2
)
190 return (INT16
)lstrcmpi32A( str1
, str2
);
194 /***********************************************************************
195 * lstrcmpi32A (KERNEL32.605)
197 INT32 WINAPI
lstrcmpi32A( LPCSTR str1
, LPCSTR str2
)
198 { TRACE(string
,"strcmpi %s and %s\n",
199 debugstr_a (str1
), debugstr_a (str2
));
200 return CompareString32A(LOCALE_SYSTEM_DEFAULT
,NORM_IGNORECASE
,str1
,-1,str2
,-1)-2;
204 /***********************************************************************
205 * lstrcmpi32W (KERNEL32.606)
207 INT32 WINAPI
lstrcmpi32W( LPCWSTR str1
, LPCWSTR str2
)
212 /* Too much! (From registry loading.) */
213 TRACE(string
,"strcmpi L%s and L%s\n",
214 debugstr_w (str1
), debugstr_w (str2
));
216 if (!str1
|| !str2
) {
217 SetLastError(ERROR_INVALID_PARAMETER
);
222 if ((*str1
<0x100 ) && (*str2
<0x100)) {
223 if ((res
= toupper(*str1
) - toupper(*str2
)) != 0) return res
;
225 if ((res
= towupper(*str1
) - towupper(*str2
)) != 0) return res
;
230 return towupper(*str1
) - towupper(*str2
);
234 /***********************************************************************
235 * lstrcpy16 (KERNEL.88)
237 SEGPTR WINAPI
lstrcpy16( SEGPTR dst
, LPCSTR src
)
239 lstrcpy32A( (LPSTR
)PTR_SEG_TO_LIN(dst
), src
);
244 /***********************************************************************
245 * lstrcpy32A (KERNEL32.608)
247 LPSTR WINAPI
lstrcpy32A( LPSTR dst
, LPCSTR src
)
249 TRACE(string
,"strcpy %s\n", debugstr_a (src
));
250 /* Windows does not check for NULL pointers here, so we don't either */
256 /***********************************************************************
257 * lstrcpy32W (KERNEL32.609)
259 LPWSTR WINAPI
lstrcpy32W( LPWSTR dst
, LPCWSTR src
)
261 register LPWSTR p
= dst
;
262 TRACE(string
,"strcpy L%s\n", debugstr_w (src
));
263 /* Windows does not check for NULL pointers here, so we don't either */
264 while ((*p
++ = *src
++));
269 /***********************************************************************
270 * lstrcpyn16 (KERNEL.353)
272 SEGPTR WINAPI
lstrcpyn16( SEGPTR dst
, LPCSTR src
, INT16 n
)
274 lstrcpyn32A( (LPSTR
)PTR_SEG_TO_LIN(dst
), src
, n
);
279 /***********************************************************************
280 * lstrcpyn32A (KERNEL32.611)
281 * Note: this function differs from the UNIX strncpy, it _always_ writes
284 LPSTR WINAPI
lstrcpyn32A( LPSTR dst
, LPCSTR src
, INT32 n
)
287 TRACE(string
,"strcpyn %s for %d chars\n",
288 debugstr_an (src
,n
), n
);
289 /* Windows does not check for NULL pointers here, so we don't either */
290 while ((n
-- > 1) && *src
) *p
++ = *src
++;
296 /***********************************************************************
297 * lstrcpyn32W (KERNEL32.612)
298 * Note: this function differs from the UNIX strncpy, it _always_ writes
301 LPWSTR WINAPI
lstrcpyn32W( LPWSTR dst
, LPCWSTR src
, INT32 n
)
304 TRACE(string
,"strcpyn L%s for %d chars\n",
305 debugstr_wn (src
,n
), n
);
306 /* Windows does not check for NULL pointers here, so we don't either */
307 while ((n
-- > 1) && *src
) *p
++ = *src
++;
313 /***********************************************************************
314 * lstrlen16 (KERNEL.90)
316 INT16 WINAPI
lstrlen16( LPCSTR str
)
318 return (INT16
)lstrlen32A( str
);
322 /***********************************************************************
323 * lstrlen32A (KERNEL32.614)
325 INT32 WINAPI
lstrlen32A( LPCSTR str
)
327 /* looks weird, but win3.1 KERNEL got a GeneralProtection handler
328 * in lstrlen() ... we check only for NULL pointer reference.
331 TRACE(string
,"strlen %s\n", debugstr_a (str
));
333 return (INT32
)strlen(str
);
337 /***********************************************************************
338 * lstrlen32W (KERNEL32.615)
340 INT32 WINAPI
lstrlen32W( LPCWSTR str
)
343 TRACE(string
,"strlen L%s\n", debugstr_w (str
));
345 while (*str
++) len
++;
350 /***********************************************************************
351 * lstrncmp32A (Not a Windows API)
353 INT32 WINAPI
lstrncmp32A( LPCSTR str1
, LPCSTR str2
, INT32 n
)
355 TRACE(string
,"strncmp %s and %s for %d chars\n",
356 debugstr_an (str1
, n
), debugstr_an (str2
, n
), n
);
357 return (INT32
)strncmp( str1
, str2
, n
);
361 /***********************************************************************
362 * lstrncmp32W (Not a Windows API)
364 INT32 WINAPI
lstrncmp32W( LPCWSTR str1
, LPCWSTR str2
, INT32 n
)
366 TRACE(string
,"strncmp L%s and L%s for %d chars\n",
367 debugstr_wn (str1
, n
), debugstr_wn (str2
, n
), n
);
369 while ((--n
> 0) && *str1
&& (*str1
== *str2
)) { str1
++; str2
++; }
370 return (INT32
)(*str1
- *str2
);
374 /***********************************************************************
375 * lstrncmpi32A (Not a Windows API)
377 INT32 WINAPI
lstrncmpi32A( LPCSTR str1
, LPCSTR str2
, INT32 n
)
381 TRACE(string
,"strncmpi %s and %s for %d chars\n",
382 debugstr_an (str1
, n
), debugstr_an (str2
, n
), n
);
384 while ((--n
> 0) && *str1
)
385 if ( (res
= toupper(*str1
++) - toupper(*str2
++)) ) return res
;
387 return toupper(*str1
) - toupper(*str2
);
391 /***********************************************************************
392 * lstrncmpi32W (Not a Windows API)
394 INT32 WINAPI
lstrncmpi32W( LPCWSTR str1
, LPCWSTR str2
, INT32 n
)
398 TRACE(string
,"strncmpi L%s and L%s for %d chars\n",
399 debugstr_wn (str1
, n
), debugstr_wn (str2
, n
), n
);
401 while ((--n
> 0) && *str1
)
403 if ((res
= towupper(*str1
) - towupper(*str2
)) != 0) return res
;
407 return towupper(*str1
) - towupper(*str2
);
411 /***********************************************************************
412 * lstrcpyAtoW (Not a Windows API)
414 LPWSTR WINAPI
lstrcpyAtoW( LPWSTR dst
, LPCSTR src
)
416 register LPWSTR p
= dst
;
418 TRACE(string
,"%s\n",src
);
420 while ((*p
++ = (WCHAR
)(unsigned char)*src
++));
425 /***********************************************************************
426 * lstrcpyWtoA (Not a Windows API)
428 LPSTR WINAPI
lstrcpyWtoA( LPSTR dst
, LPCWSTR src
)
430 register LPSTR p
= dst
;
432 TRACE(string
,"L%s\n",debugstr_w(src
));
434 while ((*p
++ = (CHAR
)*src
++));
439 /***********************************************************************
440 * lstrcpynAtoW (Not a Windows API)
441 * Note: this function differs from the UNIX strncpy, it _always_ writes
444 LPWSTR WINAPI
lstrcpynAtoW( LPWSTR dst
, LPCSTR src
, INT32 n
)
448 TRACE(string
,"%s %i\n",src
, n
);
450 while ((n
-- > 1) && *src
) *p
++ = (WCHAR
)(unsigned char)*src
++;
456 /***********************************************************************
457 * lstrcpynWtoA (Not a Windows API)
458 * Note: this function differs from the UNIX strncpy, it _always_ writes
461 LPSTR WINAPI
lstrcpynWtoA( LPSTR dst
, LPCWSTR src
, INT32 n
)
465 TRACE(string
,"L%s %i\n",debugstr_w(src
), n
);
467 while ((n
-- > 1) && *src
) *p
++ = (CHAR
)*src
++;
473 /***********************************************************************
476 void WINAPI
Copy( LPVOID src
, LPVOID dst
, WORD size
)
478 memcpy( dst
, src
, size
);
482 /***********************************************************************
483 * RtlFillMemory (KERNEL32.441)
485 VOID WINAPI
RtlFillMemory( LPVOID ptr
, UINT32 len
, UINT32 fill
)
487 memset( ptr
, fill
, len
);
491 /***********************************************************************
492 * RtlMoveMemory (KERNEL32.442)
494 VOID WINAPI
RtlMoveMemory( LPVOID dst
, LPCVOID src
, UINT32 len
)
496 memmove( dst
, src
, len
);
500 /***********************************************************************
501 * RtlZeroMemory (KERNEL32.444)
503 VOID WINAPI
RtlZeroMemory( LPVOID ptr
, UINT32 len
)
505 memset( ptr
, 0, len
);
509 /***********************************************************************
510 * AnsiToOem16 (KEYBOARD.5)
512 INT16 WINAPI
AnsiToOem16( LPCSTR s
, LPSTR d
)
514 CharToOem32A( s
, d
);
519 /***********************************************************************
520 * OemToAnsi16 (KEYBOARD.6)
522 INT16 WINAPI
OemToAnsi16( LPCSTR s
, LPSTR d
)
524 OemToChar32A( s
, d
);
529 /***********************************************************************
530 * AnsiToOemBuff16 (KEYBOARD.134)
532 void WINAPI
AnsiToOemBuff16( LPCSTR s
, LPSTR d
, UINT16 len
)
534 CharToOemBuff32A( s
, d
, len
? len
: 65536 );
538 /***********************************************************************
539 * OemToAnsiBuff16 (KEYBOARD.135)
541 void WINAPI
OemToAnsiBuff16( LPCSTR s
, LPSTR d
, UINT16 len
)
543 OemToCharBuff32A( s
, d
, len
? len
: 65536 );
547 /***********************************************************************
548 * CharToOem32A (USER32.37)
550 BOOL32 WINAPI
CharToOem32A( LPCSTR s
, LPSTR d
)
553 if (!s
|| !d
) return TRUE
;
554 TRACE(string
,"CharToOem %s\n", debugstr_a (s
));
555 while ((*d
++ = ANSI_TO_OEM(*s
++)));
556 TRACE(string
," to %s\n", debugstr_a (oldd
));
561 /***********************************************************************
562 * CharToOemBuff32A (USER32.38)
564 BOOL32 WINAPI
CharToOemBuff32A( LPCSTR s
, LPSTR d
, DWORD len
)
566 while (len
--) *d
++ = ANSI_TO_OEM(*s
++);
571 /***********************************************************************
572 * CharToOemBuff32W (USER32.39)
574 BOOL32 WINAPI
CharToOemBuff32W( LPCWSTR s
, LPSTR d
, DWORD len
)
576 while (len
--) *d
++ = ANSI_TO_OEM(*s
++);
581 /***********************************************************************
582 * CharToOem32W (USER32.40)
584 BOOL32 WINAPI
CharToOem32W( LPCWSTR s
, LPSTR d
)
587 if (!s
|| !d
) return TRUE
;
588 TRACE(string
,"CharToOem L%s\n", debugstr_w (s
));
589 while ((*d
++ = ANSI_TO_OEM(*s
++)));
590 TRACE(string
," to %s\n", debugstr_a (oldd
));
595 /***********************************************************************
596 * OemToChar32A (USER32.402)
598 BOOL32 WINAPI
OemToChar32A( LPCSTR s
, LPSTR d
)
601 TRACE(string
,"OemToChar %s\n", debugstr_a (s
));
602 while ((*d
++ = OEM_TO_ANSI(*s
++)));
603 TRACE(string
," to %s\n", debugstr_a (oldd
));
608 /***********************************************************************
609 * OemToCharBuff32A (USER32.403)
611 BOOL32 WINAPI
OemToCharBuff32A( LPCSTR s
, LPSTR d
, DWORD len
)
613 TRACE(string
,"OemToCharBuff %s\n", debugstr_an (s
, len
));
614 while (len
--) *d
++ = OEM_TO_ANSI(*s
++);
619 /***********************************************************************
620 * OemToCharBuff32W (USER32.404)
622 BOOL32 WINAPI
OemToCharBuff32W( LPCSTR s
, LPWSTR d
, DWORD len
)
624 TRACE(string
,"OemToCharBuff %s\n", debugstr_an (s
, len
));
625 while (len
--) *d
++ = (WCHAR
)OEM_TO_ANSI(*s
++);
630 /***********************************************************************
631 * OemToChar32W (USER32.405)
633 BOOL32 WINAPI
OemToChar32W( LPCSTR s
, LPWSTR d
)
635 while ((*d
++ = (WCHAR
)OEM_TO_ANSI(*s
++)));
639 /***********************************************************************
640 * WideCharToLocal (Not a Windows API)
641 * similar lstrcpyWtoA, should handle codepages properly
644 * strlen of the destination string
647 INT32 WINAPI
WideCharToLocal32(
652 TRACE(string
,"(%p, %s, %i)\n", pLocal
, debugstr_w(pWide
),dwChars
);
653 WideCharToMultiByte(CP_ACP
,0,pWide
,-1,pLocal
,dwChars
,NULL
,NULL
);
654 return strlen(pLocal
);
656 /***********************************************************************
657 * LocalToWideChar (Not a Windows API)
658 * similar lstrcpyAtoW, should handle codepages properly
661 * strlen of the destination string
663 INT32 WINAPI
LocalToWideChar32(
668 TRACE(string
,"(%p, %s, %i)\n",pWide
, pLocal
, dwChars
);
669 MultiByteToWideChar(CP_ACP
,0,pLocal
,-1,pWide
,dwChars
);
670 return lstrlen32W(pWide
);
674 /***********************************************************************
675 * lstrrchr (Not a Windows API)
677 * This is the implementation meant to be invoked form within
678 * COMCTL32_StrRChrA and shell32(TODO)...
680 * Return a pointer to the last occurence of wMatch in lpStart
681 * not looking further than lpEnd...
683 LPSTR WINAPI
lstrrchr( LPCSTR lpStart
, LPCSTR lpEnd
, WORD wMatch
)
685 LPCSTR lpGotIt
= NULL
;
687 TRACE(string
,"(%s, %s)\n", lpStart
, lpEnd
);
689 if (!lpEnd
) lpEnd
= lpStart
+ strlen(lpStart
);
691 for(; lpStart
< lpEnd
; lpStart
= CharNext32A(lpStart
))
692 if (!ChrCmpA( GET_WORD(lpStart
), wMatch
))
695 return ((LPSTR
)lpGotIt
);
698 /***********************************************************************
699 * lstrrchrw (Not a Windows API)
701 * This is the implementation meant to be invoked form within
702 * COMCTL32_StrRChrW and shell32(TODO)...
704 * Return a pointer to the last occurence of wMatch in lpStart
705 * not looking further than lpEnd...
707 LPWSTR WINAPI
lstrrchrw( LPCWSTR lpStart
, LPCWSTR lpEnd
, WORD wMatch
)
709 LPCWSTR lpGotIt
= NULL
;
711 TRACE(string
,"(%p, %p, %c)\n", lpStart
, lpEnd
, wMatch
);
712 if (!lpEnd
) lpEnd
= lpStart
+ lstrlen32W(lpStart
);
714 for(; lpStart
< lpEnd
; lpStart
= CharNext32W(lpStart
))
715 if (!ChrCmpW( GET_WORD(lpStart
), wMatch
))
718 return (LPWSTR
)lpGotIt
;
721 /***********************************************************************
722 * strstrw (Not a Windows API)
724 * This is the implementation meant to be invoked form within
725 * COMCTL32_StrStrW and shell32(TODO)...
728 LPWSTR WINAPI
strstrw( LPCWSTR lpFirst
, LPCWSTR lpSrch
) {
729 UINT32 uSrchLen
= (UINT32
)lstrlen32W(lpSrch
);
730 WORD wMatchBeg
= *(WORD
*)lpSrch
;
732 TRACE(string
,"(%p, %p)\n", lpFirst
, lpSrch
);
735 ((lpFirst
=CRTDLL_wcschr(lpFirst
, wMatchBeg
))!=0) &&
736 lstrncmp32W(lpFirst
, lpSrch
, uSrchLen
);
740 return (LPWSTR
)lpFirst
;
744 /***********************************************************************
746 * This fuction returns FALSE if both words match, TRUE otherwise...
748 static BOOL32
ChrCmpA( WORD word1
, WORD word2
) {
749 if (LOBYTE(word1
) == LOBYTE(word2
)) {
750 if (IsDBCSLeadByte32(LOBYTE(word1
))) {
751 return (word1
!= word2
);
758 /***********************************************************************
760 * This fuction returns FALSE if both words match, TRUE otherwise...
762 static BOOL32
ChrCmpW( WORD word1
, WORD word2
) {
763 return (word1
!= word2
);