Reactos
1/***
2*mbsset_s_l.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 <corecrt_internal_securecrt.h>
16
17errno_t __cdecl _mbsset_s_l(unsigned char *_Dst, size_t _SizeInBytes, unsigned int _Value, _LOCALE_ARG_DECL)
18{
19 int mbcs_error = 0;
20 unsigned char *p;
21 size_t available;
22 unsigned char highval, lowval;
23
24 /* validation section */
25 _VALIDATE_STRING(_Dst, _SizeInBytes);
26
27 _LOCALE_UPDATE;
28 if (_LOCALE_SHORTCUT_TEST)
29 {
30 return _strset_s((char *)_Dst, _SizeInBytes, (int)_Value);
31 }
32
33 p = _Dst;
34 available = _SizeInBytes;
35 highval = (unsigned char)(_Value >> 8);
36 lowval = (unsigned char)(_Value & 0x00ff);
37
38 /* ensure _Value is a valid mbchar */
39 if ((highval != 0 && (lowval == 0 || !_ISMBBLEAD(highval))) ||
40 (highval == 0 && _ISMBBLEAD(lowval)))
41 {
42 _RESET_STRING(_Dst, _SizeInBytes);
43 _RETURN_MBCS_ERROR;
44 }
45
46 if (highval != 0)
47 {
48 while (*p != 0 && --available > 0)
49 {
50 if (p[1] == 0)
51 {
52 /* do not orphan leadbyte */
53 *p = 0;
54 ++available;
55 mbcs_error = 1;
56 break;
57 }
58 *p++ = highval;
59 if (--available == 0)
60 {
61 break;
62 }
63 *p++ = lowval;
64 }
65 }
66 else
67 {
68 while (*p != 0 && --available > 0)
69 {
70 *p++ = lowval;
71 }
72 }
73
74 if (available == 0)
75 {
76 _RESET_STRING(_Dst, _SizeInBytes);
77 _RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInBytes);
78 }
79 _FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
80
81 if (mbcs_error)
82 {
83 _RETURN_MBCS_ERROR;
84 }
85 else
86 {
87 _RETURN_NO_ERROR;
88 }
89}