1 /***
2 *mbsset.c - Sets all charcaters of string to given character (MBCS)
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 * Sets all charcaters of string to given character (MBCS)
8 *
9 *******************************************************************************/
10 #ifndef _MBCS
11 #error This file should only be compiled with _MBCS defined
12 #endif
13
14 #include <corecrt_internal_mbstring.h>
15 #include <locale.h>
16 #include <string.h>
17
18 /***
19 * mbsset - Sets all charcaters of string to given character (MBCS)
20 *
21 *Purpose:
22 * Sets all of characters in string (except the terminating '/0'
23 * character) equal to the supplied character. Handles MBCS
24 * chars correctly.
25 *
26 *Entry:
27 * unsigned char *string = string to modify
28 * unsigned int val = value to fill string with
29 *
30 *Exit:
31 * returns string = now filled with the specified char
32 *
33 *Uses:
34 *
35 *Exceptions:
36 * Input parameters are validated. Refer to the validation section of the function.
37 *
38 *******************************************************************************/
39
_mbsset_l(unsigned char * string,unsigned int val,_locale_t plocinfo)40 extern "C" unsigned char * __cdecl _mbsset_l(
41 unsigned char *string,
42 unsigned int val,
43 _locale_t plocinfo
44 )
45 {
46 unsigned char *start = string;
47 unsigned char highval, lowval;
48 _LocaleUpdate _loc_update(plocinfo);
49
50 /* validation section */
51 _VALIDATE_RETURN(string != nullptr, EINVAL, nullptr);
52
53 _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
54 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
55 return (unsigned char *)_strset((char *)string, val);
56 _END_SECURE_CRT_DEPRECATION_DISABLE
57
58 highval = static_cast<unsigned char>(val >> 8);
59 if (highval != 0)
60 {
61 /* 2-byte value */
62
63 lowval = (unsigned char)(val & 0x00ff);
64
65 if(lowval=='\0')
66 {
67 _ASSERTE(("invalid MBCS pair passed to mbsset",0));
68
69 /* Ideally we would return nullptr here and signal an error
70 condition. But since this function has no other
71 error modes, there would be a good chance of crashing
72 the caller. So instead we fill the string with spaces
73 to ensure that no information leaks through
74 unexpectedly. Anyway, we do set errno to EINVAL.
75 */
76 errno = EINVAL;
77 lowval=highval=' ';
78 }
79
80 while (*string) {
81
82 *string++ = highval;
83 if (*string)
84 *string++ = lowval;
85 else
86 /* don't orphan lead byte */
87 string[-1] = ' ';
88 }
89
90 }
91
92 else {
93 /* single byte value */
94
95 while (*string)
96 *string++ = (unsigned char)val;
97 }
98
99 return(start);
100 }
101
102 extern "C" unsigned char * (__cdecl _mbsset)(
103 unsigned char *string,
104 unsigned int val
105 )
106 {
107 _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
108 return _mbsset_l(string, val, nullptr);
109 _END_SECURE_CRT_DEPRECATION_DISABLE
110 }
111