ReactOS 0.4.16-dev-1025-gd3456f5
mbsncpy.cpp
Go to the documentation of this file.
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
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.
43extern "C" unsigned char * __cdecl _mbsncpy_l(
44 unsigned char *dst,
45 const unsigned char *src,
46 size_t cnt,
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
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);
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}
87extern "C" unsigned char * (__cdecl _mbsncpy)(
88 unsigned char *dst,
89 const unsigned char *src,
90 size_t cnt
91 )
92{
94 return _mbsncpy_l(dst, src, cnt, nullptr);
96}
#define EINVAL
Definition: acclib.h:90
#define __cdecl
Definition: accygwin.h:79
#define _ismbblead_l(_c, p)
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
GLuint start
Definition: gl.h:1545
GLenum src
Definition: glext.h:6340
GLenum GLenum dst
Definition: glext.h:6340
#define _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
#define _END_SECURE_CRT_DEPRECATION_DISABLE
_locale_t plocinfo
Definition: ismbbyte.cpp:75
unsigned char *__cdecl _mbsncpy(unsigned char *dst, const unsigned char *src, size_t cnt)
Definition: mbsncpy.cpp:87
unsigned char *__cdecl _mbsncpy_l(unsigned char *dst, const unsigned char *src, size_t cnt, _locale_t plocinfo)
Definition: mbsncpy.cpp:43
strncpy
Definition: string.h:335
_In_ size_t cnt
Definition: wcstombs.cpp:43