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