ReactOS 0.4.16-dev-1028-g8602629
mbsnbcat.cpp
Go to the documentation of this file.
1/***
2*mbsnbcat.c - concatenate string2 onto string1, max length n bytes
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose:
7* defines mbsnbcat() - concatenate maximum of n bytes
8*
9*******************************************************************************/
10#ifndef _MBCS
11 #error This file should only be compiled with _MBCS defined
12#endif
13
14#include <corecrt_internal.h>
16#include <locale.h>
17#include <string.h>
18
19
20/***
21* _mbsnbcat - concatenate max cnt bytes onto dst
22*
23*Purpose:
24* Concatenates src onto dst, with a maximum of cnt bytes copied.
25* Handles 2-byte MBCS characters correctly.
26*
27*Entry:
28* unsigned char *dst - string to concatenate onto
29* unsigned char *src - string to concatenate from
30* int cnt - number of bytes to copy
31*
32*Exit:
33* returns dst, with src (at least part) concatenated on
34*
35*Exceptions:
36* Input parameters are validated. Refer to the validation section of the function.
37*
38*******************************************************************************/
39
40extern "C" unsigned char * __cdecl _mbsnbcat_l(
41 unsigned char *dst,
42 const unsigned char *src,
43 size_t cnt,
45 )
46{
47 unsigned char *start;
48
49 if (!cnt)
50 return(dst);
51
52 /* validation section */
53 _VALIDATE_RETURN(dst != nullptr, EINVAL, nullptr);
54 _VALIDATE_RETURN(src != nullptr, EINVAL, nullptr);
55
56 _LocaleUpdate _loc_update(plocinfo);
57
59 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
60 return (unsigned char *)strncat((char *)dst, (const char *)src, cnt);
62
63 start = dst;
64 while (*dst++)
65 ;
66 --dst; // dst now points to end of dst string
67
68 /* if last char in string is a lead byte, back up pointer */
69 if ( dst!=start && _mbsbtype_l(start, (int) ((dst - start) - 1), _loc_update.GetLocaleT()) == _MBC_LEAD )
70 {
71 --dst;
72 }
73
74 /* copy over the characters */
75
76 while (cnt--) {
77
78 if ( _ismbblead_l(*src, _loc_update.GetLocaleT()) ) {
79 *dst++ = *src++;
80 if (cnt == 0) { /* write null if cnt exhausted */
81 dst[-1] = '\0';
82 break;
83 }
84 cnt--;
85 if ((*dst++ = *src++)=='\0') { /* or if no trail byte */
86 dst[-2] = '\0';
87 break;
88 }
89 }
90 else if ((*dst++ = *src++) == '\0')
91 break;
92
93 }
94
95 if ( dst!=start && _mbsbtype_l(start, (int) ((dst - start) - 1), _loc_update.GetLocaleT()) == _MBC_LEAD )
96 {
97 dst[-1] = '\0';
98 }
99 else
100 {
101 *dst = '\0';
102 }
103
104 return(start);
105}
106
107extern "C" unsigned char * (__cdecl _mbsnbcat)(
108 unsigned char *dst,
109 const unsigned char *src,
110 size_t cnt
111 )
112{
114 return _mbsnbcat_l(dst, src, cnt, nullptr);
116}
#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
_CRTIMP int __cdecl _mbsbtype_l(_In_reads_bytes_(_Pos) _Pre_z_ const unsigned char *_Str, _In_ size_t _Pos, _In_opt_ _locale_t _Locale)
#define _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
#define _END_SECURE_CRT_DEPRECATION_DISABLE
_locale_t plocinfo
Definition: ismbbyte.cpp:75
unsigned char *__cdecl _mbsnbcat_l(unsigned char *dst, const unsigned char *src, size_t cnt, _locale_t plocinfo)
Definition: mbsnbcat.cpp:40
unsigned char *__cdecl _mbsnbcat(unsigned char *dst, const unsigned char *src, size_t cnt)
Definition: mbsnbcat.cpp:107
#define _MBC_LEAD
Definition: msvcrt.h:824
_In_opt_ _Locale strncat
Definition: string.h:263
_In_ size_t cnt
Definition: wcstombs.cpp:43