ReactOS 0.4.16-dev-905-gc1b8c4f
mbctype.cpp File Reference
#include <corecrt_internal.h>
#include <locale.h>
#include <corecrt_internal_mbstring.h>
#include <mbctype.h>
#include <winnls.h>
Include dependency graph for mbctype.cpp:

Go to the source code of this file.

Classes

struct  code_page_info
 

Macros

#define _CHINESE_SIMP_CP   936
 
#define _KOREAN_WANGSUNG_CP   949
 
#define _CHINESE_TRAD_CP   950
 
#define _KOREAN_JOHAB_CP   1361
 
#define NUM_CHARS   257 /* -1 through 255 */
 
#define NUM_CTYPES   4 /* table contains 4 types of info */
 
#define MAX_RANGES   8 /* max number of ranges needed given languages so far */
 
#define _MBCTYPE_DEFAULT
 
#define _MBCASEMAP_DEFAULT
 
#define JAPANSE_DEFAULT_LOCALE_NAME_INDEX   0
 
#define CHINESE_SIMPLIFIED_LOCALE_NAME_INDEX   1
 
#define KOREAN_DEFAULT_LOCALE_NAME_INDEX   2
 
#define CHINESE_TRADITIONAL_LOCALE_NAME_INDEX   3
 

Functions

 _CRT_LINKER_FORCE_INCLUDE (__acrt_multibyte_initializer)
 
unsigned char *__cdecl __p__mbctype ()
 
unsigned char *__cdecl __p__mbcasemap ()
 
int __cdecl _setmbcp_nolock (int, __crt_multibyte_data *)
 
static int getSystemCP (int)
 
static void setSBCS (__crt_multibyte_data *ptmbci)
 
static __crt_multibyte_data *__cdecl update_thread_multibyte_data_internal (__acrt_ptd *const ptd, __crt_multibyte_data **const current_multibyte_data) throw ()
 
__crt_multibyte_data *__cdecl __acrt_update_thread_multibyte_data ()
 
static int __cdecl setmbcp_internal (int const requested_codepage, bool const is_for_crt_initialization, __acrt_ptd *const ptd, __crt_multibyte_data **const current_multibyte_data) throw ()
 
static const wchar_tCPtoLocaleName (int codepage)
 
static void setSBUpLow (__crt_multibyte_data *ptmbci)
 
int __cdecl _setmbcp (int const codepage)
 
int __cdecl _getmbcp ()
 
bool __cdecl __acrt_initialize_multibyte ()
 

Variables

__crt_multibyte_data __acrt_initial_multibyte_data
 
static unsigned char _mbctypes [__crt_state_management::state_index_count][NUM_CHARS]
 
static unsigned char _mbcasemaps [__crt_state_management::state_index_count][256]
 
__crt_state_management::dual_state_global< unsigned char * > _mbctype
 
__crt_state_management::dual_state_global< unsigned char * > _mbcasemap
 
__crt_state_management::dual_state_global< __crt_multibyte_data * > __acrt_current_multibyte_data
 
static int fSystemSet
 
static char __rgctypeflag [NUM_CTYPES] = { _MS, _MP, _M1, _M2 }
 
static code_page_info __rgcode_page_info []
 
const wchar_t *const _mb_locale_names []
 

Macro Definition Documentation

◆ _CHINESE_SIMP_CP

#define _CHINESE_SIMP_CP   936

Definition at line 22 of file mbctype.cpp.

◆ _CHINESE_TRAD_CP

#define _CHINESE_TRAD_CP   950

Definition at line 24 of file mbctype.cpp.

◆ _KOREAN_JOHAB_CP

#define _KOREAN_JOHAB_CP   1361

Definition at line 25 of file mbctype.cpp.

◆ _KOREAN_WANGSUNG_CP

#define _KOREAN_WANGSUNG_CP   949

Definition at line 23 of file mbctype.cpp.

◆ _MBCASEMAP_DEFAULT

#define _MBCASEMAP_DEFAULT
Value:
{ \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, \
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, \
0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, \
0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, \
0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00 /* rest is zero */ \
}

Definition at line 101 of file mbctype.cpp.

◆ _MBCTYPE_DEFAULT

#define _MBCTYPE_DEFAULT
Value:
{ \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, \
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, \
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, \
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, \
0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00 /* rest is zero */ \
}

Definition at line 77 of file mbctype.cpp.

◆ CHINESE_SIMPLIFIED_LOCALE_NAME_INDEX

#define CHINESE_SIMPLIFIED_LOCALE_NAME_INDEX   1

Definition at line 218 of file mbctype.cpp.

◆ CHINESE_TRADITIONAL_LOCALE_NAME_INDEX

#define CHINESE_TRADITIONAL_LOCALE_NAME_INDEX   3

Definition at line 220 of file mbctype.cpp.

◆ JAPANSE_DEFAULT_LOCALE_NAME_INDEX

#define JAPANSE_DEFAULT_LOCALE_NAME_INDEX   0

Definition at line 217 of file mbctype.cpp.

◆ KOREAN_DEFAULT_LOCALE_NAME_INDEX

#define KOREAN_DEFAULT_LOCALE_NAME_INDEX   2

Definition at line 219 of file mbctype.cpp.

◆ MAX_RANGES

#define MAX_RANGES   8 /* max number of ranges needed given languages so far */

Definition at line 30 of file mbctype.cpp.

◆ NUM_CHARS

#define NUM_CHARS   257 /* -1 through 255 */

Definition at line 27 of file mbctype.cpp.

◆ NUM_CTYPES

#define NUM_CTYPES   4 /* table contains 4 types of info */

Definition at line 29 of file mbctype.cpp.

Function Documentation

◆ __acrt_initialize_multibyte()

bool __cdecl __acrt_initialize_multibyte ( void  )

Definition at line 894 of file mbctype.cpp.

895{
896 static bool initialized = false;
897
898 // Synchronization note: it is not possible for a data race to occur here.
899 // In the CRT DLLs, this function is called during CRT startup, befor any
900 // user code using the CRT may run. In the static CRT, this function is
901 // called by a CRT initializer (at the top of this file), so 'initialized'
902 // will be true before any user code can enter the CRT.
903 //
904 // CRT_REFACTOR TODO We should split this function into two parts: one that
905 // does the initialization (without any check), and one that does nothing,
906 // but can be used to cause this object to be linked in.
907 if (!initialized)
908 {
909 // initialize global pointer to the current per-thread mbc information structure
911
912 // initialize mbc pointers
913 _mbcasemap.initialize_from_array(_mbcasemaps);
914 _mbctype .initialize_from_array(_mbctypes);
915
916 // initialize the multibyte globals
917 __acrt_ptd* const ptd_head = __acrt_getptd_head();
918 for (size_t i = 0; i != __crt_state_management::state_index_count; ++i)
919 {
920 setmbcp_internal(_MB_CP_ANSI, true, ptd_head + i, &__acrt_current_multibyte_data.dangerous_get_state_array()[i]);
921 }
922
923 initialized = 1;
924 }
925
926 return true;
927}
__acrt_ptd *__cdecl __acrt_getptd_head(void)
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
__crt_state_management::dual_state_global< unsigned char * > _mbcasemap
Definition: mbctype.cpp:128
static unsigned char _mbctypes[__crt_state_management::state_index_count][NUM_CHARS]
Definition: mbctype.cpp:93
__crt_state_management::dual_state_global< __crt_multibyte_data * > __acrt_current_multibyte_data
Definition: mbctype.cpp:131
__crt_state_management::dual_state_global< unsigned char * > _mbctype
Definition: mbctype.cpp:125
static unsigned char _mbcasemaps[__crt_state_management::state_index_count][256]
Definition: mbctype.cpp:116
__crt_multibyte_data __acrt_initial_multibyte_data
Definition: mbctype.cpp:42
static int __cdecl setmbcp_internal(int const requested_codepage, bool const is_for_crt_initialization, __acrt_ptd *const ptd, __crt_multibyte_data **const current_multibyte_data)
Definition: mbctype.cpp:394
#define _MB_CP_ANSI
Definition: msvcrt.h:830
static BOOL initialized
Definition: syslog.c:39

Referenced by do_locale_initialization(), initialize_multibyte(), and pre_initialize().

◆ __acrt_update_thread_multibyte_data()

__crt_multibyte_data *__cdecl __acrt_update_thread_multibyte_data ( void  )

Definition at line 361 of file mbctype.cpp.

362{
364}
__acrt_ptd *__cdecl __acrt_getptd(void)
static __crt_multibyte_data *__cdecl update_thread_multibyte_data_internal(__acrt_ptd *const ptd, __crt_multibyte_data **const current_multibyte_data)
Definition: mbctype.cpp:305

Referenced by __acrt_update_multibyte_info(), __acrt_update_multibyte_info_explicit(), and _get_current_locale().

◆ __p__mbcasemap()

unsigned char *__cdecl __p__mbcasemap ( void  )

Definition at line 237 of file mbctype.cpp.

238{
239 return _mbcasemap.value();
240}

◆ __p__mbctype()

unsigned char *__cdecl __p__mbctype ( void  )

Definition at line 232 of file mbctype.cpp.

233{
234 return _mbctype.value();
235}

Referenced by Test__mbctype().

◆ _CRT_LINKER_FORCE_INCLUDE()

_CRT_LINKER_FORCE_INCLUDE ( __acrt_multibyte_initializer  )

◆ _getmbcp()

int __cdecl _getmbcp ( void  )

Definition at line 870 of file mbctype.cpp.

871{
872 _locale_t plocinfo = nullptr;
873 _LocaleUpdate _loc_update(plocinfo);
874 if ( _loc_update.GetLocaleT()->mbcinfo->ismbcodepage )
875 return _loc_update.GetLocaleT()->mbcinfo->mbcodepage;
876 else
877 return 0;
878}
_locale_t plocinfo
Definition: ismbbyte.cpp:75

Referenced by test__ismbclx(), test__mbscat_s(), test__mbslwr_s(), test__mbsncpy_s(), test__mbsupr_s(), test_btowc(), test_ismbckata(), test_ismbclegal(), test_mbbtombc(), test_mbcjisjms(), test_mbcjmsjis(), test_mbcp(), test_mbctohira(), test_mbctokata(), test_mbctombb(), test_mbsrev(), test_mbsspn(), test_mbsspnp(), test_splitpath(), and test_wctob().

◆ _setmbcp()

◆ _setmbcp_nolock()

int __cdecl _setmbcp_nolock ( int  codepage,
__crt_multibyte_data ptmbci 
)

Definition at line 692 of file mbctype.cpp.

693{
694 unsigned int icp;
695 unsigned int irg;
696 unsigned int ich;
697 unsigned char *rgptr;
698 CPINFO cpInfo;
699
701
702 /* user wants 'single-byte' MB code page */
703 if (codepage == _MB_CP_SBCS)
704 {
705 setSBCS(ptmbci);
706 return 0;
707 }
708
709 /* check for CRT code page info */
710 for (icp = 0;
711 icp < (sizeof(__rgcode_page_info) / sizeof(code_page_info));
712 icp++)
713 {
714 /* see if we have info for this code page */
716 {
717 /* clear the table */
718 for (ich = 0; ich < NUM_CHARS; ich++)
719 ptmbci->mbctype[ich] = 0;
720
721 /* for each type of info, load table */
722 for (irg = 0; irg < NUM_CTYPES; irg++)
723 {
724 /* go through all the ranges for each type of info */
725 for (rgptr = (unsigned char *)__rgcode_page_info[icp].rgrange[irg];
726 rgptr[0] && rgptr[1];
727 rgptr += 2)
728 {
729 /* set the type for every character in range */
730 for (ich = rgptr[0]; ich <= rgptr[1] && ich < 256; ich++)
731 ptmbci->mbctype[ich + 1] |= __rgctypeflag[irg];
732 }
733 }
734 /* code page has changed */
735 ptmbci->mbcodepage = codepage;
736 /* all the code pages we keep info for are truly multibyte */
737 ptmbci->ismbcodepage = 1;
738 ptmbci->mblocalename = CPtoLocaleName(ptmbci->mbcodepage);
739 for (irg = 0; irg < NUM_ULINFO; irg++)
740 {
741 ptmbci->mbulinfo[irg] = __rgcode_page_info[icp].mbulinfo[irg];
742 }
743
744 /* return success */
745 setSBUpLow(ptmbci);
746 return 0;
747 }
748 }
749
750 /* verify codepage validity */
751 // Unclear why UTF7 is excluded yet stateful and other complex encodings are not
753 {
754 /* return failure, code page not changed */
755 return -1;
756 }
757
758 // Special case for UTF-8
759 if (codepage == CP_UTF8)
760 {
761 ptmbci->mbcodepage = CP_UTF8;
762 ptmbci->mblocalename = nullptr;
763
764 // UTF-8 does not have lead or trail bytes in the terms
765 // the CRT thinks of it for DBCS codepages, so we'll
766 // clear the flags for all bytes.
767 // Note that this array is 257 bytes because there's a
768 // "-1" that is used someplaces for EOF. So this array
769 // is actually -1 based.
770 for (ich = 0; ich < NUM_ULINFO; ich++)
771 {
772 ptmbci->mbctype[ich] = 0;
773 }
774
775 // not really a multibyte code page, we'll have to test
776 // ptmbci->mbcodepage == CP_UTF8 when we use this structure.
777 ptmbci->ismbcodepage = 0;
778
779 // CJK encodings have some full-width mappings, but not here.
780 for (irg = 0; irg < NUM_ULINFO; irg++)
781 {
782 ptmbci->mbulinfo[irg] = 0;
783 }
784
785 setSBUpLow(ptmbci);
786
787 // return success
788 return 0;
789 }
790 /* code page not supported by CRT, try the OS */\
791 else if (GetCPInfo(codepage, &cpInfo) != 0)
792 {
793 BYTE *lbptr;
794
795 /* clear the table */
796 for (ich = 0; ich < NUM_CHARS; ich++)
797 {
798 ptmbci->mbctype[ich] = 0;
799 }
800
801 ptmbci->mbcodepage = codepage;
802 ptmbci->mblocalename = nullptr;
803
804 // Special case for DBCS where we know there may be a leadbyte/trailbyte pattern
805 if (cpInfo.MaxCharSize == 2)
806 {
807 /* LeadByte range always terminated by two 0's */
808 for (lbptr = cpInfo.LeadByte; *lbptr && *(lbptr + 1); lbptr += 2)
809 {
810 for (ich = *lbptr; ich <= *(lbptr + 1); ich++)
811 ptmbci->mbctype[ich + 1] |= _M1;
812 }
813
814 /* All chars > 1 must be considered valid trail bytes */
815 for (ich = 0x01; ich < 0xFF; ich++)
816 {
817 ptmbci->mbctype[ich + 1] |= _M2;
818 }
819
820 /* code page has changed */
821 ptmbci->mblocalename = CPtoLocaleName(ptmbci->mbcodepage);
822
823 /* really a multibyte code page */
824 ptmbci->ismbcodepage = 1;
825 }
826 else
827 {
828 /* single-byte code page */
829 ptmbci->ismbcodepage = 0;
830 }
831
832 for (irg = 0; irg < NUM_ULINFO; irg++)
833 {
834 ptmbci->mbulinfo[irg] = 0;
835 }
836
837 setSBUpLow(ptmbci);
838 /* return success */
839 return 0;
840 }
841
842
843 /* If system default call, don't fail - set to SBCS */
844 if (fSystemSet)
845 {
846 setSBCS(ptmbci);
847 return 0;
848 }
849
850 /* return failure, code page not changed */
851 return -1;
852}
_Out_opt_ UINT * code_page
#define NUM_ULINFO
BOOL WINAPI IsValidCodePage(UINT codepage)
Definition: locale.c:2079
BOOL WINAPI GetCPInfo(UINT codepage, LPCPINFO cpinfo)
Definition: locale.c:2144
unsigned short WORD
Definition: ntddk_ex.h:93
#define NUM_CHARS
Definition: mbctype.cpp:27
static const wchar_t * CPtoLocaleName(int codepage)
Definition: mbctype.cpp:514
static void setSBCS(__crt_multibyte_data *ptmbci)
Definition: mbctype.cpp:261
static int getSystemCP(int)
Definition: mbctype.cpp:554
static void setSBUpLow(__crt_multibyte_data *ptmbci)
Definition: mbctype.cpp:598
#define NUM_CTYPES
Definition: mbctype.cpp:29
static code_page_info __rgcode_page_info[]
Definition: mbctype.cpp:138
static int fSystemSet
Definition: mbctype.cpp:134
static char __rgctypeflag[NUM_CTYPES]
Definition: mbctype.cpp:136
#define for
Definition: utility.h:88
#define _M2
Definition: msvcrt.h:818
#define _M1
Definition: msvcrt.h:817
#define _MB_CP_SBCS
Definition: msvcrt.h:828
#define CP_UTF8
Definition: nls.h:20
unsigned short mbulinfo[6]
unsigned char mbctype[257]
wchar_t const * mblocalename
BYTE LeadByte[MAX_LEADBYTES]
Definition: winnls.h:585
UINT MaxCharSize
Definition: winnls.h:583
unsigned short mbulinfo[NUM_ULINFO]
Definition: mbctype.cpp:36
unsigned char rgrange[NUM_CTYPES][MAX_RANGES]
Definition: mbctype.cpp:37
#define CP_UTF7
Definition: winnls.h:237
unsigned char BYTE
Definition: xxhash.c:193

Referenced by _wcreate_locale(), and setmbcp_internal().

◆ CPtoLocaleName()

static const wchar_t * CPtoLocaleName ( int  codepage)
static

Definition at line 514 of file mbctype.cpp.

515{
516 switch (codepage) {
517 case 932:
519 case 936:
521 case 949:
523 case 950:
525 }
526
527 return 0;
528}
#define CHINESE_SIMPLIFIED_LOCALE_NAME_INDEX
Definition: mbctype.cpp:218
#define KOREAN_DEFAULT_LOCALE_NAME_INDEX
Definition: mbctype.cpp:219
#define JAPANSE_DEFAULT_LOCALE_NAME_INDEX
Definition: mbctype.cpp:217
#define CHINESE_TRADITIONAL_LOCALE_NAME_INDEX
Definition: mbctype.cpp:220
const wchar_t *const _mb_locale_names[]
Definition: mbctype.cpp:222

Referenced by _setmbcp_nolock().

◆ getSystemCP()

static int getSystemCP ( int  codepage)
static

Definition at line 554 of file mbctype.cpp.

555{
556 _locale_t plocinfo = nullptr;
557 _LocaleUpdate _loc_update(plocinfo);
558 fSystemSet = 0;
559
560 /* get system code page values if requested */
561
562 if (codepage == _MB_CP_OEM)
563 {
564 fSystemSet = 1;
565 return GetOEMCP();
566 }
567 else if (codepage == _MB_CP_ANSI)
568 {
569 fSystemSet = 1;
570 return GetACP();
571 }
572 else if (codepage == _MB_CP_LOCALE)
573 {
574 fSystemSet = 1;
575 return _loc_update.GetLocaleT()->locinfo->_public._locale_lc_codepage;
576 }
577
578 return codepage;
579}
UINT WINAPI GetACP(void)
Definition: locale.c:2021
UINT WINAPI GetOEMCP(void)
Definition: locale.c:2060
#define _MB_CP_OEM
Definition: msvcrt.h:829
#define _MB_CP_LOCALE
Definition: msvcrt.h:831

Referenced by _setmbcp_nolock(), and setmbcp_internal().

◆ setmbcp_internal()

static int __cdecl setmbcp_internal ( int const  requested_codepage,
bool const  is_for_crt_initialization,
__acrt_ptd *const  ptd,
__crt_multibyte_data **const  current_multibyte_data 
)
throw (
)
static

Definition at line 394 of file mbctype.cpp.

400{
401 update_thread_multibyte_data_internal(ptd, current_multibyte_data);
402 int const system_codepage = getSystemCP(requested_codepage);
403
404 // If it's not a new codepage, just return success:
405 if (system_codepage == ptd->_multibyte_info->mbcodepage)
406 {
407 return 0;
408 }
409
410 // Always allocate space so that we don't have to take a lock for any update:
411 __crt_unique_heap_ptr<__crt_multibyte_data> mb_data(_malloc_crt_t(__crt_multibyte_data, 1));
412 if (!mb_data)
413 {
414 return -1;
415 }
416
417 // Initialize the new multibyte data structure from the current multibyte
418 // data structure for this thread, resetting the reference count (since it
419 // is not actually referenced by anything yet).
420 *mb_data.get() = *ptd->_multibyte_info;
421 mb_data.get()->refcount = 0;
422
423 // Actually initialize the new multibyte data using the new codepage:
424 // CRT_REFACTOR TODO _setmbcp_nolock is a terrible name.
425 int const setmbcp_status = _setmbcp_nolock(system_codepage, mb_data.get());
426 if (setmbcp_status == -1)
427 {
428 errno = EINVAL;
429 return -1;
430 }
431
432 // At this point, we have a valid, new set of multibyte data to swap in. If
433 // this is not the initial codepage initialization during process startup,
434 // we need to toggle the locale-changed state:
435 if (!is_for_crt_initialization)
436 {
438 }
439
440 if (InterlockedDecrement(&ptd->_multibyte_info->refcount) == 0 &&
441 ptd->_multibyte_info != &__acrt_initial_multibyte_data)
442 {
443 _free_crt(ptd->_multibyte_info);
444 }
445
446 // Update the multibyte codepage for this thread:
447 mb_data.get()->refcount = 1;
448 ptd->_multibyte_info = mb_data.detach();
449
450 // If this thread has its own locale, do not update the global codepage:
451 if (!__acrt_should_sync_with_global_locale(ptd))
452 {
453 return setmbcp_status;
454 }
455
456 // Otherwise, update the global codepage:
457 __acrt_lock_and_call(__acrt_multibyte_cp_lock, [&]
458 {
459 memcpy_s(_mbctype.value(), sizeof(_mbctypes[0]), ptd->_multibyte_info->mbctype, sizeof(ptd->_multibyte_info->mbctype));
460 memcpy_s(_mbcasemap.value(), sizeof(_mbcasemaps[0]), ptd->_multibyte_info->mbcasemap, sizeof(ptd->_multibyte_info->mbcasemap));
461
462 if (InterlockedDecrement(&(*current_multibyte_data)->refcount) == 0 &&
463 (*current_multibyte_data) != &__acrt_initial_multibyte_data)
464 {
465 _free_crt(*current_multibyte_data);
466 }
467
468 *current_multibyte_data = ptd->_multibyte_info;
469 InterlockedIncrement(&ptd->_multibyte_info->refcount);
470 });
471
472 if (is_for_crt_initialization)
473 {
474 __acrt_initial_locale_pointers.mbcinfo = *current_multibyte_data;
475 }
476
477 return setmbcp_status;
478}
#define EINVAL
Definition: acclib.h:90
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
__crt_locale_pointers __acrt_initial_locale_pointers
Definition: nlsdata.cpp:137
@ __acrt_multibyte_cp_lock
void __cdecl __acrt_set_locale_changed(void)
Definition: wsetlocale.cpp:54
_In_ size_t const _In_ int _In_ bool const _In_ unsigned const _In_ __acrt_rounding_mode const _Inout_ __crt_cached_ptd_host & ptd
Definition: cvt.cpp:355
#define _free_crt
int __cdecl _setmbcp_nolock(int, __crt_multibyte_data *)
Definition: mbctype.cpp:692
#define errno
Definition: errno.h:18
int CDECL memcpy_s(void *dest, size_t numberOfElements, const void *src, size_t count)
Definition: heap.c:800
struct __crt_multibyte_data * mbcinfo
Definition: corecrt.h:631

Referenced by __acrt_initialize_multibyte(), and _setmbcp().

◆ setSBCS()

static void setSBCS ( __crt_multibyte_data ptmbci)
static

Definition at line 261 of file mbctype.cpp.

262{
263 int i;
264
265 /* set for single-byte code page */
266 for (i = 0; i < NUM_CHARS; i++)
267 ptmbci->mbctype[i] = 0;
268
269 /* code page has changed, set global flag */
270 ptmbci->mbcodepage = 0;
271
272 /* clear flag to indicate single-byte code */
273 ptmbci->ismbcodepage = 0;
274
275 ptmbci->mblocalename = nullptr;
276
277 for (i = 0; i < NUM_ULINFO; i++)
278 ptmbci->mbulinfo[i] = 0;
279
280 for ( i = 0 ; i < 257 ; i++ )
282
283 for ( i = 0 ; i < 256 ; i++ )
285}
unsigned char mbcasemap[256]

Referenced by _setmbcp_nolock().

◆ setSBUpLow()

static void setSBUpLow ( __crt_multibyte_data ptmbci)
static

Definition at line 598 of file mbctype.cpp.

599{
600 BYTE * pbPair;
601 UINT ich;
602 CPINFO cpInfo;
603 UCHAR sbVector[256];
604 UCHAR upVector[256];
605 UCHAR lowVector[256];
606 USHORT wVector[512];
607
608 // test if codepage exists
609 if (ptmbci->mbcodepage != CP_UTF8 && GetCPInfo(ptmbci->mbcodepage, &cpInfo) != 0)
610 {
611 // This code attempts to generate casing tables for characters 0-255
612 // For DBCS codepages that will be basically ASCII casing but won't help DBCS mapping.
613 // For SBCS codepages that will include the codepage-specific characters.
614 // Mappings do not appear to include Turkish-i variations.
615
616 // if so, create vector 0-255
617 for (ich = 0; ich < 256; ich++)
618 sbVector[ich] = (UCHAR) ich;
619
620 // set byte 0 and any leading byte value to non-alpha char ' '
621 sbVector[0] = (UCHAR)' ';
622 for (pbPair = &cpInfo.LeadByte[0]; *pbPair; pbPair += 2)
623 // make sure ich within a valid range
624 for (ich = *pbPair; ich <= *(pbPair + 1) && ich < 256; ich++)
625 sbVector[ich] = (UCHAR)' ';
626
627 // get char type for character vector
628
629 __acrt_GetStringTypeA(nullptr, CT_CTYPE1, (LPCSTR)sbVector, 256, wVector,
630 ptmbci->mbcodepage, FALSE);
631
632 // get lower case mappings for character vector
633
634 __acrt_LCMapStringA(nullptr, ptmbci->mblocalename, LCMAP_LOWERCASE, (LPCSTR)sbVector, 256,
635 (LPSTR)lowVector, 256, ptmbci->mbcodepage, FALSE);
636
637 // get upper case mappings for character vector
638
639 __acrt_LCMapStringA(nullptr, ptmbci->mblocalename, LCMAP_UPPERCASE, (LPCSTR)sbVector, 256,
640 (LPSTR)upVector, 256, ptmbci->mbcodepage, FALSE);
641
642 // set _SBUP, _SBLOW in ptmbci->mbctype if type is upper. lower
643 // set mapping array with lower or upper mapping value
644
645 for (ich = 0; ich < 256; ich++)
646 {
647 if (wVector[ich] & _UPPER)
648 {
649 // WARNING: +1 because the mbctype array starts with a -1 EOF character
650 ptmbci->mbctype[ich + 1] |= _SBUP;
651 ptmbci->mbcasemap[ich] = lowVector[ich];
652 }
653 else if (wVector[ich] & _LOWER)
654 {
655 // WARNING: +1 because the mbctype array starts with a -1 EOF character
656 ptmbci->mbctype[ich + 1] |= _SBLOW;
657 ptmbci->mbcasemap[ich] = upVector[ich];
658 }
659 else
660 ptmbci->mbcasemap[ich] = 0;
661 }
662 }
663 else
664 {
665 // Either no codepage or UTF-8 (which looks a lot like ASCII in the lower bits)
666 // Set 'A'-'Z' as upper, 'a'-'z' as lower (eg: ASCII casing)
667 for (ich = 0; ich < 256; ich++)
668 {
669 if (ich >= (UINT)'A' && ich <= (UINT)'Z')
670 {
671 // WARNING: +1 because the mbctype array starts with a -1 EOF character
672 ptmbci->mbctype[ich + 1] |= _SBUP;
673 ptmbci->mbcasemap[ich] = static_cast<unsigned char>(ich + ('a' - 'A'));
674 }
675 else if (ich >= (UINT)'a' && ich <= (UINT)'z')
676 {
677 // WARNING: +1 because the mbctype array starts with a -1 EOF character
678 ptmbci->mbctype[ich + 1] |= _SBLOW;
679 ptmbci->mbcasemap[ich] = static_cast<unsigned char>(ich - ('a' - 'A'));
680 }
681 else
682 ptmbci->mbcasemap[ich] = 0;
683 }
684 }
685}
BOOL __cdecl __acrt_GetStringTypeA(_locale_t const locale, DWORD const info_type, LPCSTR const string, int const string_size_in_bytes, unsigned short *const char_type, int const code_page, BOOL const error)
int __cdecl __acrt_LCMapStringA(_locale_t const plocinfo, PCWSTR const LocaleName, DWORD const dwMapFlags, PCCH const lpSrcStr, int const cchSrc, PCH const lpDestStr, int const cchDest, int const code_page, BOOL const bError)
#define FALSE
Definition: types.h:117
#define _LOWER
Definition: ctype.h:66
#define _UPPER
Definition: ctype.h:65
#define _SBLOW
Definition: msvcrt.h:821
#define _SBUP
Definition: msvcrt.h:820
unsigned int UINT
Definition: ndis.h:50
unsigned short USHORT
Definition: pedump.c:61
#define CT_CTYPE1
Definition: winnls.h:239
#define LCMAP_UPPERCASE
Definition: winnls.h:187
#define LCMAP_LOWERCASE
Definition: winnls.h:186
const char * LPCSTR
Definition: xmlstorage.h:183
char * LPSTR
Definition: xmlstorage.h:182
unsigned char UCHAR
Definition: xmlstorage.h:181

Referenced by _setmbcp_nolock().

◆ update_thread_multibyte_data_internal()

static __crt_multibyte_data *__cdecl update_thread_multibyte_data_internal ( __acrt_ptd *const  ptd,
__crt_multibyte_data **const  current_multibyte_data 
)
throw (
)
static

Definition at line 305 of file mbctype.cpp.

309{
310 __crt_multibyte_data* ptmbci = nullptr;
311
312 if (__acrt_should_sync_with_global_locale(ptd) || ptd->_locale_info == nullptr)
313 {
315 __try
316 {
317 ptmbci = ptd->_multibyte_info;
318 if (ptmbci != *current_multibyte_data)
319 {
320 /*
321 * Decrement the reference count in the old mbc info structure
322 * and free it, if necessary
323 */
324 if (ptmbci != nullptr &&
325 InterlockedDecrement(&ptmbci->refcount) == 0 &&
327 {
328 /*
329 * Free it
330 */
331 _free_crt(ptmbci);
332 }
333
334 /*
335 * Point to the current mbc info structure and increment its
336 * reference count.
337 */
338 ptmbci = ptd->_multibyte_info = *current_multibyte_data;
340 }
341 }
343 {
345 }
347 }
348 else
349 {
350 ptmbci = ptd->_multibyte_info;
351 }
352
353 if (!ptmbci)
354 {
355 abort();
356 }
357
358 return ptmbci;
359}
void __cdecl __acrt_unlock(_In_ __acrt_lock_id lock)
Definition: locks.cpp:57
__acrt_lock(__acrt_heap_lock)
#define abort()
Definition: i386-dis.c:34
#define __try
Definition: pseh2_64.h:188
#define __endtry
Definition: pseh2_64.h:191
#define __finally
Definition: pseh2_64.h:190

Referenced by __acrt_update_thread_multibyte_data(), and setmbcp_internal().

Variable Documentation

◆ __acrt_current_multibyte_data

◆ __acrt_initial_multibyte_data

◆ __rgcode_page_info

code_page_info __rgcode_page_info[]
static

Definition at line 138 of file mbctype.cpp.

Referenced by _setmbcp_nolock().

◆ __rgctypeflag

char __rgctypeflag[NUM_CTYPES] = { _MS, _MP, _M1, _M2 }
static

Definition at line 136 of file mbctype.cpp.

Referenced by _setmbcp_nolock().

◆ _mb_locale_names

const wchar_t* const _mb_locale_names[]
Initial value:
=
{
L"ja-JP",
L"zh-CN",
L"ko-KR",
L"zh-TW",
}
#define L(x)
Definition: ntvdm.h:50

Definition at line 222 of file mbctype.cpp.

Referenced by CPtoLocaleName().

◆ _mbcasemap

__crt_state_management::dual_state_global<unsigned char*> _mbcasemap

Definition at line 128 of file mbctype.cpp.

Referenced by __acrt_initialize_multibyte(), __p__mbcasemap(), and setmbcp_internal().

◆ _mbcasemaps

unsigned char _mbcasemaps[__crt_state_management::state_index_count][256]
static
Initial value:
=
{
}
#define _MBCASEMAP_DEFAULT
Definition: mbctype.cpp:101

Definition at line 116 of file mbctype.cpp.

Referenced by __acrt_initialize_multibyte(), and setmbcp_internal().

◆ _mbctype

__crt_state_management::dual_state_global<unsigned char*> _mbctype

This file has no copyright assigned and is placed in the Public Domain. This file is part of the w64 mingw-runtime package. No warranty is given; refer to the file DISCLAIMER within this package.

Definition at line 125 of file mbctype.cpp.

Referenced by __acrt_initialize_multibyte(), __p__mbctype(), and setmbcp_internal().

◆ _mbctypes

unsigned char _mbctypes[__crt_state_management::state_index_count][NUM_CHARS]
static
Initial value:
=
{
}
#define _MBCTYPE_DEFAULT
Definition: mbctype.cpp:77

Definition at line 93 of file mbctype.cpp.

Referenced by __acrt_initialize_multibyte(), and setmbcp_internal().

◆ fSystemSet

int fSystemSet
static

Definition at line 134 of file mbctype.cpp.

Referenced by _setmbcp_nolock(), and getSystemCP().