2 * COPYRIGHT: See COPYING in the top level directory
4 * PURPOSE: Implementation of mbstowcs_s
5 * FILE: lib/sdk/crt/string/mbstowcs_s.c
6 * PROGRAMMER: Timo Kreuzer
12 _Success_(return!=EINVAL
)
18 _Out_opt_
size_t *pcchConverted
,
19 _Out_writes_to_opt_(sizeInWords
, *pcchConverted
) wchar_t *pwcstr
,
20 _In_
size_t sizeInWords
,
21 _In_reads_or_z_(count
) const char *pmbstr
,
24 size_t cchMax
, cwcWritten
;
27 /* Make sure, either we have a target buffer > 0 bytes, or no buffer */
28 if (!MSVCRT_CHECK_PMT( ((sizeInWords
!= 0) && (pwcstr
!= 0)) ||
29 ((sizeInWords
== 0) && (pwcstr
== 0)) ))
35 /* Check if we have a return value pointer */
38 /* Default to 0 bytes written */
42 if (!MSVCRT_CHECK_PMT((count
== 0) || (pmbstr
!= 0)))
48 /* Check if there is anything to do */
49 if ((pwcstr
== 0) && (pmbstr
== 0))
55 /* Check if we have a multibyte string */
58 /* Check if we also have a wchar buffer */
61 /* Calculate the maximum the we can write */
62 cchMax
= (count
< sizeInWords
) ? count
+ 1 : sizeInWords
;
64 /* Now do the conversion */
65 cwcWritten
= mbstowcs(pwcstr
, pmbstr
, cchMax
);
67 /* Check if the buffer was not zero terminated */
68 if (cwcWritten
== cchMax
)
70 /* Check if we reached the max size of the dest buffer */
71 if (cwcWritten
== sizeInWords
)
73 /* Does the caller allow this? */
74 if (count
!= _TRUNCATE
)
76 /* Not allowed, truncate to 0 length */
84 /* Inform the caller about truncation */
88 /* zero teminate the buffer */
89 pwcstr
[cwcWritten
- 1] = L
'\0';
93 /* The buffer is zero terminated, count the terminating char */
99 /* Get the length of the string, plus 0 terminator */
100 cwcWritten
= _mbsnlen((const unsigned char *)pmbstr
, count
) + 1;
105 cwcWritten
= count
+ 1;
108 /* Check if we have a return value pointer */
111 /* Default to 0 bytes written */
112 *pcchConverted
= cwcWritten
;