xref: /reactos/sdk/lib/crt/string/mbstowcs_nt.c (revision 84ccccab)
1 #include <ndk/umtypes.h>
2 #include <ndk/rtlfuncs.h>
3 #include <string.h>
4 
5 #undef MB_CUR_MAX
6 #define MB_CUR_MAX 2
7 
8 /*
9  * @implemented
10  */
11 int mbtowc (wchar_t *wchar, const char *mbchar, size_t count)
12 {
13 	UCHAR mbarr[MB_CUR_MAX] = { 0 };
14 	PUCHAR mbs = mbarr;
15 	WCHAR wc;
16 
17 	if (mbchar == NULL)
18 		return 0;
19 
20 	if (wchar == NULL)
21 		return 0;
22 
23 	memcpy(mbarr, mbchar, min(count, sizeof mbarr));
24 
25 	wc = RtlAnsiCharToUnicodeChar(&mbs);
26 
27 	if (wc == L' ' && mbarr[0] != ' ')
28 		return -1;
29 
30 	*wchar = wc;
31 
32 	return (int)(mbs - mbarr);
33 }
34 
35 /*
36  * @implemented
37  */
38 size_t mbstowcs (wchar_t *wcstr, const char *mbstr, size_t count)
39 {
40 	NTSTATUS Status;
41 	ULONG Size;
42 	ULONG Length;
43 
44 	Length = (ULONG)strlen (mbstr);
45 
46 	if (wcstr == NULL)
47 	{
48 		RtlMultiByteToUnicodeSize (&Size,
49 		                           mbstr,
50 		                           Length);
51 
52 		return (size_t)(Size / sizeof(wchar_t));
53 	}
54 
55 	Status = RtlMultiByteToUnicodeN (wcstr,
56 	                                 (ULONG)count * sizeof(WCHAR),
57 	                                 &Size,
58 	                                 mbstr,
59 	                                 Length);
60 	if (!NT_SUCCESS(Status))
61 		return -1;
62 
63 	return (size_t)(Size / sizeof(wchar_t));
64 }
65 
66 /* EOF */
67