1 /***
2 *mbsncpy.c - Copy one string to another, n chars only (MBCS)
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 * Copy one string to another, n chars only (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 #pragma warning(disable:__WARNING_INCORRECT_VALIDATION __WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26014 26018
19
20 /***
21 * _mbsncpy - Copy one string to another, n chars only (MBCS)
22 *
23 *Purpose:
24 * Copies exactly cnt character from src to dst. If strlen(src) < cnt, the
25 * remaining character are padded with null bytes. If strlen >= cnt, no
26 * terminating null byte is added. 2-byte MBCS characters are handled
27 * correctly.
28 *
29 *Entry:
30 * unsigned char *dst = destination for copy
31 * unsigned char *src = source for copy
32 * int cnt = number of characters to copy
33 *
34 *Exit:
35 * returns dst = destination of copy
36 *
37 *Exceptions:
38 * Input parameters are validated. Refer to the validation section of the function.
39 *
40 *******************************************************************************/
41
42 #pragma warning(suppress:6101) // Returning uninitialized memory '*dst'. A successful path through the function does not set the named _Out_ parameter.
_mbsncpy_l(unsigned char * dst,const unsigned char * src,size_t cnt,_locale_t plocinfo)43 extern "C" unsigned char * __cdecl _mbsncpy_l(
44 unsigned char *dst,
45 const unsigned char *src,
46 size_t cnt,
47 _locale_t plocinfo
48 )
49 {
50 unsigned char *start = dst;
51 _LocaleUpdate _loc_update(plocinfo);
52
53 /* validation section */
54 _VALIDATE_RETURN(dst != nullptr || cnt == 0, EINVAL, nullptr);
55 _VALIDATE_RETURN(src != nullptr || cnt == 0, EINVAL, nullptr);
56
57 _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
58 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
59 #pragma warning(suppress:__WARNING_BANNED_API_USAGE)
60 return (unsigned char *)strncpy((char *)dst, (const char *)src, cnt);
61 _END_SECURE_CRT_DEPRECATION_DISABLE
62
63 while (cnt) {
64
65 cnt--;
66 if ( _ismbblead_l(*src, _loc_update.GetLocaleT()) ) {
67 *dst++ = *src++;
68 if ((*dst++ = *src++) == '\0') {
69 dst[-2] = '\0';
70 break;
71 }
72 }
73 else
74 if ((*dst++ = *src++) == '\0')
75 break;
76
77 }
78
79 /* pad with nulls as needed */
80
81 while (cnt--)
82 *dst++ = '\0';
83
84 #pragma warning(suppress:__WARNING_POSTCONDITION_NULLTERMINATION_VIOLATION) // 26036 REVIEW annotation mistake?
85 return start;
86 }
87 extern "C" unsigned char * (__cdecl _mbsncpy)(
88 unsigned char *dst,
89 const unsigned char *src,
90 size_t cnt
91 )
92 {
93 _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
94 return _mbsncpy_l(dst, src, cnt, nullptr);
95 _END_SECURE_CRT_DEPRECATION_DISABLE
96 }
97