ReactOS 0.4.16-dev-889-g9563c07
wcrtomb.cpp File Reference
#include <corecrt_internal_mbstring.h>
#include <corecrt_internal_ptd_propagation.h>
#include <corecrt_internal_securecrt.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
Include dependency graph for wcrtomb.cpp:

Go to the source code of this file.

Functions

 _Success_ (return==0) static errno_t __cdecl _wcrtomb_internal(int *const return_value
 
 __out_bcount_z_opt (destination_count) char *const destination
 
 _ASSERTE (locale->locinfo->_public._locale_mb_cur_max==1||locale->locinfo->_public._locale_mb_cur_max==2||locale->locinfo->_public._locale_lc_codepage==CP_UTF8)
 
 if (state)
 
 if (locale->locinfo->_public._locale_lc_codepage==CP_UTF8)
 
 if (!locale->locinfo->locale_name[LC_CTYPE])
 
 if (size==0||default_used)
 
 if (return_value)
 
static errno_t __cdecl wcrtomb_s_internal (size_t *const return_value, char *const destination, size_t const destination_count, wchar_t const wchar, mbstate_t *const state, __crt_cached_ptd_host &ptd)
 
errno_t __cdecl wcrtomb_s (size_t *const return_value, char *const destination, size_t const destination_count, wchar_t const wchar, mbstate_t *const state)
 
size_t __cdecl wcrtomb (char *const destination, wchar_t const wchar, mbstate_t *const state)
 
static size_t __cdecl _wcsrtombs_internal (_Pre_maybenull_ _Post_z_ char *destination, _Inout_ _Deref_prepost_z_ wchar_t const **const source, _In_ size_t n, _Out_opt_ mbstate_t *const state, _Inout_ __crt_cached_ptd_host &ptd) throw ()
 
size_t __cdecl wcsrtombs (char *const destination, wchar_t const **const source, size_t const n, mbstate_t *const state)
 
errno_t __cdecl wcsrtombs_s (size_t *const return_value, char *const destination, size_t const destination_count, wchar_t const **const source, size_t const n, mbstate_t *const state)
 
int __cdecl wctob (wint_t const wchar)
 

Variables

size_t const destination_count
 
size_t const wchar_t const wchar
 
size_t const wchar_t const mbstate_t *const state
 
size_t const wchar_t const mbstate_t *const _Inout_ __crt_cached_ptd_host & ptd
 
_locale_t const locale = ptd.get_locale()
 
BOOL default_used {}
 
int const size
 
 return
 

Function Documentation

◆ __out_bcount_z_opt()

__out_bcount_z_opt ( destination_count  ) const

◆ _ASSERTE()

_ASSERTE ( locale->locinfo->_public.  _locale_mb_cur_max = =1||locale->locinfo->_public._locale_mb_cur_max==2||locale->locinfo->_public._locale_lc_codepage==CP_UTF8)

◆ _Success_()

_Success_ ( return  = = 0) const

◆ _wcsrtombs_internal()

static size_t __cdecl _wcsrtombs_internal ( _Pre_maybenull_ _Post_z_ char destination,
_Inout_ _Deref_prepost_z_ wchar_t const **const  source,
_In_ size_t  n,
_Out_opt_ mbstate_t *const  state,
_Inout_ __crt_cached_ptd_host &  ptd 
)
throw (
)
static

Definition at line 227 of file wcrtomb.cpp.

234{
235 /* validation section */
236 _UCRT_VALIDATE_RETURN(ptd, source != nullptr, EINVAL, (size_t)-1);
237
238 _locale_t const locale = ptd.get_locale();
239
240 if (locale->locinfo->_public._locale_lc_codepage == CP_UTF8)
241 {
242 return __wcsrtombs_utf8(destination, source, n, state, ptd);
243 }
244
245 char buf[MB_LEN_MAX];
246 int i = 0;
247 size_t nc = 0;
248 wchar_t const* wcs = *source;
249
250 if (!destination)
251 {
252 for (; ; nc += i, ++wcs)
253 {
254 /* translate but don't store */
255 _wcrtomb_internal(&i, buf, MB_LEN_MAX, *wcs, state, ptd);
256 if (i <= 0)
257 {
258 return static_cast<size_t>(-1);
259 }
260 else if (buf[i - 1] == '\0')
261 {
262 return nc + i - 1;
263 }
264 }
265 }
266
267 for (; 0 < n; nc += i, ++wcs, destination += i, n -= i)
268 {
269 /* translate and store */
270 char *t = nullptr;
271
272 if (n < (size_t)locale->locinfo->_public._locale_mb_cur_max)
273 {
274 t = buf;
275 }
276 else
277 {
278 t = destination;
279 }
280
281 _wcrtomb_internal(&i, t, MB_LEN_MAX, *wcs, state, ptd);
282 if (i <= 0)
283 {
284 /* encountered invalid sequence */
285 nc = (size_t)-1;
286 break;
287 }
288
289 if (destination == t)
290 {
291 /* do nothing */
292 }
293 else if (n < static_cast<size_t>(i))
294 {
295 break; // Won't all fit
296 }
297 else
298 {
299 memcpy_s(destination, n, buf, i);
300 }
301
302 if (destination[i - 1] == '\0')
303 {
304 // Encountered terminating null
305 *source = 0;
306 return nc + i - 1;
307 }
308 }
309
310 *source = wcs;
311 return nc;
312}
#define EINVAL
Definition: acclib.h:90
Definition: _locale.h:75
#define _UCRT_VALIDATE_RETURN(ptd, expr, errorcode, retexpr)
__kernel_size_t size_t
Definition: linux.h:237
GLdouble GLdouble t
Definition: gl.h:2047
GLdouble n
Definition: glext.h:7729
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define MB_LEN_MAX
Definition: stdlib.h:19
size_t __cdecl __wcsrtombs_utf8(char *dst, const wchar_t **src, size_t len, mbstate_t *ps, __crt_cached_ptd_host &ptd)
Definition: wcrtomb.cpp:439
#define CP_UTF8
Definition: nls.h:20
int CDECL memcpy_s(void *dest, size_t numberOfElements, const void *src, size_t count)
Definition: heap.c:800
size_t const wchar_t const mbstate_t *const _Inout_ __crt_cached_ptd_host & ptd
Definition: wcrtomb.cpp:57
size_t const wchar_t const mbstate_t *const state
Definition: wcrtomb.cpp:54
wchar_t wcs[5]

Referenced by wcsrtombs(), and wcsrtombs_s().

◆ if() [1/5]

if ( !locale->locinfo->  locale_name[LC_CTYPE])

Definition at line 93 of file wcrtomb.cpp.

94 {
95 if (wchar > 255) // Validate high byte
96 {
97 if (return_value)
98 *return_value = -1;
99
100 return ptd.get_errno().set(EILSEQ);
101 }
102
103 *destination = static_cast<char>(wchar);
104 if (return_value)
105 {
106 *return_value = 1;
107 }
108
109 return 0;
110 }
#define EILSEQ
Definition: errno.h:109
size_t const wchar_t const wchar
Definition: wcrtomb.cpp:53

◆ if() [2/5]

if ( locale->locinfo->_public.  _locale_lc_codepage = CP_UTF8)

Definition at line 72 of file wcrtomb.cpp.

73 {
74 // Unlike c16rtomb. wctomb/wcrtomb have no ability to process a partial code point.
75 // So, we could call c16rtomb and check for a lone surrogate or other error, or for simplicity
76 // We can instead just call c32rtomb and check for any error. I choose the latter.
77 static mbstate_t local_state{};
78 int result = static_cast<int>(__crt_mbstring::__c32rtomb_utf8(destination, static_cast<char32_t>(wchar), (state != nullptr ? state : &local_state), ptd));
79 if (return_value != nullptr)
80 {
81 *return_value = result;
82 }
83 if (result <= 4)
84 {
85 return 0;
86 }
87 else
88 {
89 return ptd.get_errno().value_or(0);
90 }
91 }
GLuint64EXT * result
Definition: glext.h:11304
size_t __cdecl __c32rtomb_utf8(char *s, char32_t c32, mbstate_t *ps, __crt_cached_ptd_host &ptd)
Definition: c32rtomb.cpp:21

◆ if() [3/5]

if ( return_value  )

Definition at line 133 of file wcrtomb.cpp.

134 {
135 *return_value = size;
136 }
int const size
Definition: wcrtomb.cpp:113

◆ if() [4/5]

if ( size  = = 0 || default_used)

Definition at line 123 of file wcrtomb.cpp.

124 {
125 if (return_value)
126 {
127 *return_value = -1;
128 }
129
130 return ptd.get_errno().set(EILSEQ);
131 }

◆ if() [5/5]

if ( state  )

Definition at line 67 of file wcrtomb.cpp.

68 {
69 state->_Wchar = 0;
70 }

◆ wcrtomb()

size_t __cdecl wcrtomb ( char *const  destination,
wchar_t const  wchar,
mbstate_t *const  state 
)

Definition at line 200 of file wcrtomb.cpp.

205{
206 size_t return_value = static_cast<size_t>(-1);
207 wcrtomb_s(&return_value, destination, (destination == nullptr ? 0 : MB_LEN_MAX), wchar, state);
208 return return_value;
209}
errno_t __cdecl wcrtomb_s(size_t *const return_value, char *const destination, size_t const destination_count, wchar_t const wchar, mbstate_t *const state)
Definition: wcrtomb.cpp:188

◆ wcrtomb_s()

errno_t __cdecl wcrtomb_s ( size_t *const  return_value,
char *const  destination,
size_t const  destination_count,
wchar_t const  wchar,
mbstate_t *const  state 
)

Definition at line 188 of file wcrtomb.cpp.

195{
196 __crt_cached_ptd_host ptd;
197 return wcrtomb_s_internal(return_value, destination, destination_count, wchar, state, ptd);
198}
size_t const destination_count
Definition: wcrtomb.cpp:52
static errno_t __cdecl wcrtomb_s_internal(size_t *const return_value, char *const destination, size_t const destination_count, wchar_t const wchar, mbstate_t *const state, __crt_cached_ptd_host &ptd)
Definition: wcrtomb.cpp:154

Referenced by wcrtomb().

◆ wcrtomb_s_internal()

static errno_t __cdecl wcrtomb_s_internal ( size_t *const  return_value,
char *const  destination,
size_t const  destination_count,
wchar_t const  wchar,
mbstate_t *const  state,
__crt_cached_ptd_host &  ptd 
)
static

Definition at line 154 of file wcrtomb.cpp.

162{
163 // Note that we do not force destination_count > 0 in the destination !=
164 // nullptr case because we do not need to add a null terminator, due to
165 // the fact that the destination will receive a character and not a string.
166 _UCRT_VALIDATE_RETURN_ERRCODE(ptd, (destination == nullptr && destination_count == 0) || (destination != nullptr), EINVAL);
167
168 errno_t e = 0;
169 int int_return_value = -1;
170 if (destination == nullptr)
171 {
172 char buf[MB_LEN_MAX];
173 e = _wcrtomb_internal(&int_return_value, buf, MB_LEN_MAX, wchar, state, ptd);
174 }
175 else
176 {
177 e = _wcrtomb_internal(&int_return_value, destination, destination_count, wchar, state, ptd);
178 }
179
180 if (return_value != nullptr)
181 {
182 *return_value = static_cast<size_t>(int_return_value);
183 }
184
185 return e;
186}
#define _UCRT_VALIDATE_RETURN_ERRCODE(ptd, expr, errorcode)
#define e
Definition: ke_i.h:82
int errno_t
Definition: corecrt.h:615

Referenced by wcrtomb_s().

◆ wcsrtombs()

size_t __cdecl wcsrtombs ( char *const  destination,
wchar_t const **const  source,
size_t const  n,
mbstate_t *const  state 
)

Definition at line 314 of file wcrtomb.cpp.

320{
321 __crt_cached_ptd_host ptd;
322 return _wcsrtombs_internal(destination, source, n, state, ptd);
323}
static size_t __cdecl _wcsrtombs_internal(_Pre_maybenull_ _Post_z_ char *destination, _Inout_ _Deref_prepost_z_ wchar_t const **const source, _In_ size_t n, _Out_opt_ mbstate_t *const state, _Inout_ __crt_cached_ptd_host &ptd)
Definition: wcrtomb.cpp:227

◆ wcsrtombs_s()

errno_t __cdecl wcsrtombs_s ( size_t *const  return_value,
char *const  destination,
size_t const  destination_count,
wchar_t const **const  source,
size_t const  n,
mbstate_t *const  state 
)

Definition at line 351 of file wcrtomb.cpp.

359{
360 __crt_cached_ptd_host ptd;
361
362 if (return_value != nullptr)
363 {
364 *return_value = static_cast<size_t>(-1);
365 }
366
368 ptd,
369 (destination == nullptr && destination_count == 0) ||
370 (destination != nullptr && destination_count > 0),
371 EINVAL);
372
373 if (destination != nullptr)
374 {
375 _RESET_STRING(destination, destination_count);
376 }
377
379
380 size_t retsize = _wcsrtombs_internal(destination, source, (n > destination_count ? destination_count : n), state, ptd);
381 if (retsize == static_cast<size_t>(-1))
382 {
383 if (destination != nullptr)
384 {
385 _RESET_STRING(destination, destination_count);
386 }
387
388 return ptd.get_errno().value_or(0);
389 }
390
391 ++retsize; // Account for the null terminator
392
393 if (destination != nullptr)
394 {
395 // Return error if the string does not fit:
396 if (retsize > destination_count)
397 {
398 _RESET_STRING(destination, destination_count);
400 }
401
402 // Ensure the string is null terminated:
403 destination[retsize - 1] = '\0';
404 }
405
406 if (return_value != nullptr)
407 {
408 *return_value = retsize;
409 }
410
411 return 0;
412}
#define ERANGE
Definition: acclib.h:92
#define _RESET_STRING(_String, _Size)

◆ wctob()

int __cdecl wctob ( wint_t const  wchar)

Definition at line 417 of file wcrtomb.cpp.

418{
419 __crt_cached_ptd_host ptd;
420
421 if (wchar == WEOF)
422 {
423 return EOF;
424 }
425
426 int return_value = -1;
427 char local_buffer[MB_LEN_MAX];
428
430 errno_t const e = _wcrtomb_internal(&return_value, local_buffer, MB_LEN_MAX, wchar, &state, ptd);
431 if (e == 0 && return_value == 1)
432 {
433 return local_buffer[0];
434 }
435
436 return EOF;
437}
#define EOF
Definition: stdio.h:24
#define WEOF
Definition: conio.h:185

Variable Documentation

◆ default_used

BOOL default_used {}

Definition at line 112 of file wcrtomb.cpp.

Referenced by _wctomb_internal().

◆ destination_count

◆ locale

Definition at line 60 of file wcrtomb.cpp.

◆ ptd

size_t const wchar_t const mbstate_t* const _Inout_ __crt_cached_ptd_host& ptd
Initial value:
{
_ASSERTE(destination != nullptr && destination_count > 0)
#define _ASSERTE(expr)
Definition: crtdbg.h:114

Definition at line 55 of file wcrtomb.cpp.

Referenced by __crt_mbstring::__wcsrtombs_utf8(), _wcsrtombs_internal(), if(), wcrtomb_s(), wcrtomb_s_internal(), wcsrtombs(), wcsrtombs_s(), and wctob().

◆ return

return

Definition at line 138 of file wcrtomb.cpp.

◆ size

Initial value:
locale->locinfo->_public._locale_lc_codepage,
0,
1,
destination,
static_cast<int>(destination_count),
nullptr,
return __acrt_WideCharToMultiByte(code_page, 0, buffer.get(), -1, result_size !=0 ? result :nullptr, result_size, nullptr, nullptr)
BOOL default_used
Definition: wcrtomb.cpp:112

Definition at line 113 of file wcrtomb.cpp.

Referenced by if().

◆ state

◆ wchar