Reactos
at master 189 lines 5.1 kB view raw
1/*** 2*wcsupr.c - routine to map lower-case characters in a wchar_t string 3* to upper-case 4* 5* Copyright (c) Microsoft Corporation. All rights reserved. 6* 7*Purpose: 8* Converts all the lower case characters in a wchar_t string 9* to upper case, in place. 10* 11*******************************************************************************/ 12#include <corecrt_internal.h> 13#include <ctype.h> 14#include <corecrt_internal_securecrt.h> 15#include <locale.h> 16#include <string.h> 17 18#pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018 19 20/*** 21*wchar_t *_wcsupr(string) - map lower-case characters in a string to upper-case 22* 23*Purpose: 24* wcsupr converts lower-case characters in a null-terminated wchar_t 25* string to their upper-case equivalents. The result may be longer or 26* shorter than the original string. Assumes enough space in string 27* to hold the result. 28* 29*Entry: 30* wchar_t *wsrc - wchar_t string to change to upper case 31* 32*Exit: 33* input string address 34* 35*Exceptions: 36* on an error, the original string is unaltered, and errno is set 37* 38*******************************************************************************/ 39 40extern "C" wchar_t * __cdecl _wcsupr_l ( 41 wchar_t * wsrc, 42 _locale_t plocinfo 43 ) 44{ 45 _wcsupr_s_l(wsrc, (size_t)(-1), plocinfo); 46 return wsrc; 47} 48 49extern "C" wchar_t * __cdecl _wcsupr ( 50 wchar_t * wsrc 51 ) 52{ 53 if (!__acrt_locale_changed()) 54 { 55 wchar_t * p; 56 57 /* validation section */ 58 _VALIDATE_RETURN(wsrc != nullptr, EINVAL, nullptr); 59 60 for (p=wsrc; *p; ++p) 61 { 62 if (L'a' <= *p && *p <= L'z') 63 *p += (wchar_t)(L'A' - L'a'); 64 } 65 66 return wsrc; 67 } 68 else 69 { 70 _wcsupr_s_l(wsrc, (size_t)(-1), nullptr); 71 return wsrc; 72 } 73} 74 75/*** 76*errno_t _wcsupr_s(string, size_t) - map lower-case characters in a string to upper-case 77* 78*Purpose: 79* wcsupr converts lower-case characters in a null-terminated wchar_t 80* string to their upper-case equivalents. The result may be longer or 81* shorter than the original string. 82* 83*Entry: 84* wchar_t *wsrc - wchar_t string to change to upper case 85* size_t sizeInWords - size of the destination buffer 86* 87*Exit: 88* the error code 89* 90*Exceptions: 91* on an error, the original string is unaltered, and errno is set 92* 93*******************************************************************************/ 94 95static errno_t __cdecl _wcsupr_s_l_stat ( 96 _Inout_updates_z_(sizeInWords) wchar_t * const wsrc, 97 size_t const sizeInWords, 98 _locale_t const plocinfo 99 ) 100{ 101 102 wchar_t *p; /* traverses string for C locale conversion */ 103 int dstsize; /* size in wide chars of wdst string buffer (include null) */ 104 105 /* validation section */ 106 _VALIDATE_RETURN_ERRCODE(wsrc != nullptr, EINVAL); 107 size_t const stringlen = wcsnlen(wsrc, sizeInWords); 108 if (stringlen >= sizeInWords) 109 { 110 _RESET_STRING(wsrc, sizeInWords); 111 _RETURN_DEST_NOT_NULL_TERMINATED(wsrc, sizeInWords); 112 } 113 _FILL_STRING(wsrc, sizeInWords, stringlen + 1); 114 115 if ( plocinfo->locinfo->locale_name[LC_CTYPE] == nullptr ) 116 { 117 for ( p = wsrc ; *p ; p++ ) 118 { 119 if ( (*p >= (wchar_t)L'a') && (*p <= (wchar_t)L'z') ) 120 *p = *p - (L'a' - L'A'); 121 } 122 return 0; 123 } /* C locale */ 124 125 126 /* Inquire size of wdst string */ 127 if ( (dstsize = __acrt_LCMapStringW( 128 plocinfo->locinfo->locale_name[LC_CTYPE], 129 LCMAP_UPPERCASE, 130 wsrc, 131 -1, 132 nullptr, 133 0 )) == 0 ) 134 { 135 errno = EILSEQ; 136 return errno; 137 } 138 139 if (sizeInWords < (size_t)dstsize) 140 { 141 _RESET_STRING(wsrc, sizeInWords); 142 _RETURN_BUFFER_TOO_SMALL(wsrc, sizeInWords); 143 } 144 145 /* Allocate space for wdst */ 146 __crt_scoped_stack_ptr<wchar_t> const wdst(_malloca_crt_t(wchar_t, dstsize)); 147 if (wdst.get() == nullptr) 148 { 149 errno = ENOMEM; 150 return errno; 151 } 152 153 /* Map wrc string to wide-character wdst string in alternate case */ 154 if (__acrt_LCMapStringW( 155 plocinfo->locinfo->locale_name[LC_CTYPE], 156 LCMAP_UPPERCASE, 157 wsrc, 158 -1, 159 wdst.get(), 160 dstsize ) != 0) 161 { 162 /* Copy wdst string to user string */ 163 return wcscpy_s(wsrc, sizeInWords, wdst.get()); 164 } 165 else 166 { 167 return errno = EILSEQ; 168 } 169} 170 171extern "C" errno_t __cdecl _wcsupr_s_l ( 172 wchar_t * wsrc, 173 size_t sizeInWords, 174 _locale_t plocinfo 175 ) 176{ 177 _LocaleUpdate _loc_update(plocinfo); 178 179 return _wcsupr_s_l_stat(wsrc, sizeInWords, _loc_update.GetLocaleT()); 180} 181 182 183extern "C" errno_t __cdecl _wcsupr_s ( 184 wchar_t * wsrc, 185 size_t sizeInWords 186 ) 187{ 188 return _wcsupr_s_l(wsrc, sizeInWords, nullptr); 189}