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