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