1 /***
2 *mbccpy_s_l.c - Copy one character to another (MBCS)
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 * Copy one MBCS character to another (1 or 2 bytes)
8 *
9 *******************************************************************************/
10
11 #include <corecrt_internal_mbstring.h>
12 #include <corecrt_internal_securecrt.h>
13 #include <locale.h>
14
15 #pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018
16
_mbccpy_s_l(unsigned char * _Dst,size_t _SizeInBytes,int * _PCopied,const unsigned char * _Src,_LOCALE_ARG_DECL)17 errno_t __cdecl _mbccpy_s_l(unsigned char *_Dst, size_t _SizeInBytes, int *_PCopied, const unsigned char *_Src, _LOCALE_ARG_DECL)
18 {
19 /* validation section */
20 _ASSIGN_IF_NOT_NULL(_PCopied, 0);
21 _VALIDATE_STRING(_Dst, _SizeInBytes);
22 if (_Src == nullptr)
23 {
24 *_Dst = '\0';
25 _RETURN_EINVAL;
26 }
27
28 _LOCALE_UPDATE;
29
30 /* copy */
31 if (_ISMBBLEAD(*_Src))
32 {
33 if (_Src[1] == '\0')
34 {
35 /* the source string contained a lead byte followed by the null terminator:
36 we copy only the null terminator and return EILSEQ to indicate the
37 malformed char */
38 *_Dst = '\0';
39 _ASSIGN_IF_NOT_NULL(_PCopied, 1);
40 _RETURN_MBCS_ERROR;
41 }
42 if (_SizeInBytes < 2)
43 {
44 *_Dst = '\0';
45 _RETURN_BUFFER_TOO_SMALL(_Dst, _SizeInBytes);
46 }
47 *_Dst++ = *_Src++;
48 *_Dst = *_Src;
49 _ASSIGN_IF_NOT_NULL(_PCopied, 2);
50 }
51 else
52 {
53 *_Dst = *_Src;
54 _ASSIGN_IF_NOT_NULL(_PCopied, 1);
55 }
56
57 _RETURN_NO_ERROR;
58 }
59