static int __cdecl __crtLCMapStringA_stat( _locale_t plocinfo, LPCWSTR LocaleName, DWORD dwMapFlags, LPCSTR lpSrcStr, int cchSrc, LPSTR lpDestStr, int cchDest, int code_page, BOOL bError ) { /* * LCMapString will map past NULL. Must find NULL if in string * before cchSrc characters. */ if (cchSrc > 0) { int cchSrcCnt = strncnt(lpSrcStr, cchSrc); /* * Include NULL in cchSrc if lpSrcStr terminated within cchSrc bytes. */ if (cchSrcCnt < cchSrc) { cchSrc = cchSrcCnt + 1; } else { cchSrc = cchSrcCnt; } } int retval = 0; int inbuff_size; int outbuff_size; wchar_t *inwbuffer = NULL; wchar_t *outwbuffer = NULL; /* * Convert string and return the requested information. Note that * we are converting to a wide string so there is not a * one-to-one correspondence between number of wide chars in the * input string and the number of *bytes* in the buffer. However, * there had *better be* a one-to-one correspondence between the * number of wide characters and the number of multibyte characters * or the resulting mapped string will be worthless to the user. */ /* * Use __lc_codepage for conversion if code_page not specified */ if (0 == code_page) code_page = plocinfo->locinfo->lc_codepage; /* find out how big a buffer we need (includes NULL if any) */ if ( 0 == (inbuff_size = MultiByteToWideChar( code_page, bError ? MB_PRECOMPOSED | MB_ERR_INVALID_CHARS : MB_PRECOMPOSED, lpSrcStr, cchSrc, NULL, 0 )) ) return 0; /* allocate enough space for wide chars */ inwbuffer = (wchar_t *)_calloca( inbuff_size, sizeof(wchar_t) ); if ( inwbuffer == NULL ) { return 0; } /* do the conversion */ if ( 0 == MultiByteToWideChar( code_page, MB_PRECOMPOSED, lpSrcStr, cchSrc, inwbuffer, inbuff_size) ) goto error_cleanup; /* get size required for string mapping */ if ( 0 == (retval = __crtLCMapStringEx( LocaleName, dwMapFlags, inwbuffer, inbuff_size, NULL, 0)) ) goto error_cleanup; if (dwMapFlags & LCMAP_SORTKEY) { /* retval is size in BYTES */ if (0 != cchDest) { if (retval > cchDest) goto error_cleanup; /* do string mapping */ if ( 0 == __crtLCMapStringEx( LocaleName, dwMapFlags, inwbuffer, inbuff_size, (LPWSTR)lpDestStr, cchDest) ) goto error_cleanup; } } else { /* retval is size in wide chars */ outbuff_size = retval; /* allocate enough space for wide chars (includes NULL if any) */ outwbuffer = (wchar_t *)_calloca( outbuff_size, sizeof(wchar_t) ); if ( outwbuffer == NULL ) { goto error_cleanup; } /* do string mapping */ if ( 0 == __crtLCMapStringEx( LocaleName, dwMapFlags, inwbuffer, inbuff_size, outwbuffer, outbuff_size) ) goto error_cleanup; if (0 == cchDest) { /* get size required */ if ( 0 == (retval = WideCharToMultiByte( code_page, 0, outwbuffer, outbuff_size, NULL, 0, NULL, NULL )) ) goto error_cleanup; } else { /* convert mapping */ if ( 0 == (retval = WideCharToMultiByte( code_page, 0, outwbuffer, outbuff_size, lpDestStr, cchDest, NULL, NULL )) ) goto error_cleanup; } } error_cleanup: if ( outwbuffer != NULL ) _freea(outwbuffer); _freea(inwbuffer); return retval; }
static int __cdecl __crtLCMapStringW_stat( _locale_t plocinfo, LCID Locale, DWORD dwMapFlags, LPCWSTR lpSrcStr, int cchSrc, LPWSTR lpDestStr, int cchDest, int code_page ) { static int f_use = 0; /* * Look for unstubbed 'preferred' flavor. Otherwise use available flavor. * Must actually call the function to ensure it's not a stub. */ if (0 == f_use) { if (0 != LCMapStringW(0, LCMAP_LOWERCASE, L"\0", 1, NULL, 0)) { f_use = USE_W; } else if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) { f_use = USE_A; } } /* * LCMapString will map past NULL. Must find NULL if in string * before cchSrc wide characters. */ if (cchSrc > 0) { cchSrc = wcsncnt(lpSrcStr, cchSrc); } /* Use "W" version */ if (USE_W == f_use) { return LCMapStringW(Locale, dwMapFlags, lpSrcStr, cchSrc, lpDestStr, cchDest); } /* Use "A" version */ if (USE_A == f_use || f_use == 0) { int retval = 0; int inbuff_size; int outbuff_size; unsigned char* inbuffer = NULL; unsigned char* outbuffer = NULL; int AnsiCP = 0; /* * Convert string and return the requested information. Note that * we are converting to a multibyte string so there is not a * one-to-one correspondence between number of wide chars in the * input string and the number of *bytes* in the buffer. However, * there had *better be* a one-to-one correspondence between the * number of wide characters and the number of multibyte characters * (enforced by WC_SEPCHARS) in the buffer or the resulting mapped * string will be worthless to the user. * */ /* * Use __lc_codepage for conversion if code_page not specified */ if (0 == Locale) { Locale = plocinfo->locinfo->lc_handle[LC_CTYPE]; } if (0 == code_page) { code_page = plocinfo->locinfo->lc_codepage; } /* * Always use Ansi codepage with Ansi WinAPI because they use * Ansi codepage */ if (code_page != (AnsiCP = __ansicp(Locale))) { if (AnsiCP != -1) { code_page = AnsiCP; } } /* find out how big a buffer we need (includes NULL if any) */ if (0 == (inbuff_size = WideCharToMultiByte(code_page, 0, lpSrcStr, cchSrc, NULL, 0, NULL, NULL))) { return 0; } /* allocate enough space for chars */ inbuffer = (unsigned char*)_calloca(inbuff_size, sizeof(char)); if (inbuffer == NULL) { return 0; } /* do the conversion */ if (0 == WideCharToMultiByte(code_page, 0, lpSrcStr, cchSrc, (char*)inbuffer, inbuff_size, NULL, NULL)) { goto error_cleanup; } /* get size required for string mapping */ if (0 == (outbuff_size = LCMapStringA(Locale, dwMapFlags, (const char*)inbuffer, inbuff_size, NULL, 0))) { goto error_cleanup; } /* allocate enough space for chars and NULL */ outbuffer = (unsigned char*)_calloca(outbuff_size, sizeof(char)); if (outbuffer == NULL) { goto error_cleanup; } /* do string mapping */ if (0 == LCMapStringA(Locale, dwMapFlags, (const char*)inbuffer, inbuff_size, (char*)outbuffer, outbuff_size)) { goto error_cleanup; } if (dwMapFlags & LCMAP_SORTKEY) { /* outbuff_size > cchDest is allowed */ retval = outbuff_size; if (0 != cchDest) /* SORTKEY returns BYTES, just copy */ _ERRCHECK(strncpy_s((char*)lpDestStr, cchDest, (char*)outbuffer, cchDest <= outbuff_size ? cchDest - 1 : outbuff_size)); } else { if (0 == cchDest) { /* get size required */ if (0 == (retval = MultiByteToWideChar(code_page, MB_PRECOMPOSED, (const char*)outbuffer, outbuff_size, NULL, 0))) { goto error_cleanup; } } else { /* convert mapping */ if (0 == (retval = MultiByteToWideChar(code_page, MB_PRECOMPOSED, (const char*)outbuffer, outbuff_size, lpDestStr, cchDest))) { goto error_cleanup; } } } error_cleanup: if (outbuffer != NULL) { _freea(outbuffer); } _freea(inbuffer); return retval; } else { /* f_use is neither USE_A nor USE_W */ return 0; } }
static int __cdecl __crtGetLocaleInfoW_stat( _locale_t plocinfo, LCID Locale, LCTYPE LCType, LPWSTR lpLCData, int cchData, int code_page ) { static int f_use = 0; /* * Look for unstubbed 'preferred' flavor. Otherwise use available flavor. * Must actually call the function to ensure it's not a stub. */ if (0 == f_use) { if (0 != GetLocaleInfoW(0, LOCALE_ILANGUAGE, NULL, 0)) { f_use = USE_W; } else if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) { f_use = USE_A; } } /* Use "W" version */ if (USE_W == f_use) { return GetLocaleInfoW(Locale, LCType, lpLCData, cchData); } /* Use "A" version */ if (USE_A == f_use || f_use == 0) { int retval = 0; int buff_size; unsigned char* buffer; /* * Use __lc_codepage for conversion if code_page not specified */ if (0 == code_page) { code_page = plocinfo->locinfo->lc_codepage; } /* find out how big buffer needs to be */ if (0 == (buff_size = GetLocaleInfoA(Locale, LCType, NULL, 0))) { return 0; } /* allocate buffer */ buffer = (unsigned char*)_calloca(buff_size, sizeof(char)); if (buffer == NULL) { return 0; } /* get the info in ANSI format */ if (0 == GetLocaleInfoA(Locale, LCType, (char*)buffer, buff_size)) { goto error_cleanup; } if (0 == cchData) { /* find out how much space needed */ retval = MultiByteToWideChar(code_page, MB_PRECOMPOSED, (const char*)buffer, -1, NULL, 0); } else { /* convert into user buffer */ retval = MultiByteToWideChar(code_page, MB_PRECOMPOSED, (const char*)buffer, -1, lpLCData, cchData); } error_cleanup: _freea(buffer); return retval; } else { /* f_use is neither USE_A nor USE_W */ return 0; } }
static errno_t __cdecl _wcslwr_s_l_stat ( wchar_t * wsrc, size_t sizeInWords, _locale_t plocinfo ) { wchar_t *p; /* traverses string for C locale conversion */ wchar_t *wdst; /* wide version of string in alternate case */ int dstsize; /* size in wide chars of wdst string buffer (include null) */ errno_t e = 0; size_t stringlen; /* validation section */ _VALIDATE_RETURN_ERRCODE(wsrc != NULL, EINVAL); stringlen = wcsnlen(wsrc, sizeInWords); if (stringlen >= sizeInWords) { _RESET_STRING(wsrc, sizeInWords); _RETURN_DEST_NOT_NULL_TERMINATED(wsrc, sizeInWords); } _FILL_STRING(wsrc, sizeInWords, stringlen + 1); if ( plocinfo->locinfo->locale_name[LC_CTYPE] == NULL) { for ( p = wsrc ; *p ; p++ ) { if ( (*p >= (wchar_t)L'A') && (*p <= (wchar_t)L'Z') ) { *p -= L'A' - L'a'; } } return 0; } /* C locale */ /* Inquire size of wdst string */ if ( (dstsize = __crtLCMapStringW( plocinfo->locinfo->locale_name[LC_CTYPE], LCMAP_LOWERCASE, wsrc, -1, NULL, 0 )) == 0 ) { errno = EILSEQ; return errno; } if (sizeInWords < (size_t)dstsize) { _RESET_STRING(wsrc, sizeInWords); _RETURN_BUFFER_TOO_SMALL(wsrc, sizeInWords); } /* Allocate space for wdst */ wdst = (wchar_t *)_calloca(dstsize, sizeof(wchar_t)); if (wdst == NULL) { errno = ENOMEM; return errno; } /* Map wrc string to wide-character wdst string in alternate case */ if (__crtLCMapStringW( plocinfo->locinfo->locale_name[LC_CTYPE], LCMAP_LOWERCASE, wsrc, -1, wdst, dstsize ) != 0) { /* Copy wdst string to user string */ e = wcscpy_s(wsrc, sizeInWords, wdst); } else { e = errno = EILSEQ; } _freea(wdst); return e; }
static BOOL __cdecl __crtGetStringTypeA_stat( _locale_t plocinfo, DWORD dwInfoType, LPCSTR lpSrcStr, int cchSrc, LPWORD lpCharType, int code_page, int lcid, BOOL bError ) { static int f_use = 0; /* * Look for unstubbed 'preferred' flavor. Otherwise use available * flavor. Must actually call the function to ensure it's not a stub. * (Always try wide version first so WinNT can process codepage correctly.) */ if (0 == f_use) { unsigned short dummy; if (0 != GetStringTypeW(CT_CTYPE1, L"\0", 1, &dummy)) f_use = USE_W; else if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) f_use = USE_A; } /* Use "A" version */ if (USE_A == f_use || f_use == 0) { char *cbuffer = NULL; int ret; int AnsiCP; if (0 == lcid) lcid = plocinfo->locinfo->lc_handle[LC_CTYPE]; if (0 == code_page) code_page = plocinfo->locinfo->lc_codepage; if ( -1 == (AnsiCP = __ansicp(lcid))) return FALSE; /* If current code-page is not ansi code page, convert it to ansi code page * as GetStringTypeA uses ansi code page to find the strig type. */ if ( AnsiCP != code_page) { cbuffer = __convertcp(code_page, AnsiCP, lpSrcStr, &cchSrc, NULL, 0); if (cbuffer == NULL) return FALSE; lpSrcStr = cbuffer; } ret = GetStringTypeA(lcid, dwInfoType, lpSrcStr, cchSrc, lpCharType); if ( cbuffer != NULL) _free_crt(cbuffer); return ret; } /* Use "W" version */ if (USE_W == f_use) { int retval1; int buff_size; wchar_t *wbuffer; BOOL retval2 = FALSE; /* * Convert string and return the requested information. Note that * we are converting to a wide character string so there is not a * one-to-one correspondence between number of multibyte chars in the * input string and the number of wide chars in the buffer. However, * there had *better be* a one-to-one correspondence between the * number of multibyte characters and the number of WORDs in the * return buffer. */ /* * Use __lc_codepage for conversion if code_page not specified */ if (0 == code_page) code_page = plocinfo->locinfo->lc_codepage; /* find out how big a buffer we need */ if ( 0 == (buff_size = MultiByteToWideChar( code_page, bError ? MB_PRECOMPOSED | MB_ERR_INVALID_CHARS : MB_PRECOMPOSED, lpSrcStr, cchSrc, NULL, 0 )) ) return FALSE; /* allocate enough space for wide chars */ wbuffer = (wchar_t *)_calloca( sizeof(wchar_t), buff_size ); if ( wbuffer == NULL ) { return FALSE; } (void)memset( wbuffer, 0, sizeof(wchar_t) * buff_size ); /* do the conversion */ if ( 0 != (retval1 = MultiByteToWideChar( code_page, MB_PRECOMPOSED, lpSrcStr, cchSrc, wbuffer, buff_size )) ) /* obtain result */ retval2 = GetStringTypeW( dwInfoType, wbuffer, retval1, lpCharType ); _freea(wbuffer); return retval2; } else /* f_use is neither USE_A nor USE_W */ return FALSE; }
int __cdecl _kbhit_nolock ( void ) { DWORD NumPending; DWORD NumPeeked; int ret = FALSE; PINPUT_RECORD pIRBuf=NULL; /* * if a character has been pushed back, return TRUE */ if ( chbuf != -1 ) return TRUE; /* * _coninpfh, the handle to the console input, is created the first * time that either _getch() or _cgets() or _kbhit() is called. */ if ( _coninpfh == -2 ) __initconin(); /* * Peek all pending console events */ if ( (_coninpfh == -1) || !GetNumberOfConsoleInputEvents((HANDLE)_coninpfh, &NumPending) || (NumPending == 0)) { return FALSE; } pIRBuf=(PINPUT_RECORD)_calloca(NumPending, sizeof(INPUT_RECORD)); if ( pIRBuf == NULL ) { return FALSE; } if ( PeekConsoleInput( (HANDLE)_coninpfh, pIRBuf, NumPending, &NumPeeked ) && (NumPeeked != 0L) && (NumPeeked <= NumPending) ) { /* * Scan all of the peeked events to determine if any is a key event * which should be recognized. */ PINPUT_RECORD pIR; for ( pIR = pIRBuf ; NumPeeked > 0 ; NumPeeked--, pIR++ ) { if ( (pIR->EventType == KEY_EVENT) && (pIR->Event.KeyEvent.bKeyDown) && ( pIR->Event.KeyEvent.uChar.AsciiChar || _getextendedkeycode( &(pIR->Event.KeyEvent) ) ) ) { /* * Key event corresponding to an ASCII character or an * extended code. In either case, success! */ ret = TRUE; } } } _freea( pIRBuf ); return ret; }