Reactos
at master 110 lines 2.9 kB view raw
1/*** 2*mbsncat.c - concatenate string2 onto string1, max length n 3* 4* Copyright (c) Microsoft Corporation. All rights reserved. 5* 6*Purpose: 7* defines mbsncat() - concatenate maximum of n characters 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 19/*** 20* _mbsncat - concatenate max cnt characters onto dst 21* 22*Purpose: 23* Concatenates src onto dst, with a maximum of cnt characters copied. 24* Handles 2-byte MBCS characters correctly. 25* 26*Entry: 27* unsigned char *dst - string to concatenate onto 28* unsigned char *src - string to concatenate from 29* int cnt - number of characters to copy 30* 31*Exit: 32* returns dst, with src (at least part) concatenated on 33* 34*Exceptions: 35* 36*******************************************************************************/ 37 38extern "C" unsigned char * __cdecl _mbsncat_l( 39 unsigned char *dst, 40 const unsigned char *src, 41 size_t cnt, 42 _locale_t plocinfo 43 ) 44{ 45 unsigned char *start; 46 47 if (!cnt) 48 return(dst); 49 50 /* validation section */ 51 _VALIDATE_RETURN(dst != nullptr, EINVAL, nullptr); 52 _VALIDATE_RETURN(src != nullptr, EINVAL, nullptr); 53 54 _LocaleUpdate _loc_update(plocinfo); 55 56 _BEGIN_SECURE_CRT_DEPRECATION_DISABLE 57 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0) 58 return (unsigned char *)strncat((char *)dst, (const char *)src, cnt); 59 _END_SECURE_CRT_DEPRECATION_DISABLE 60 61 start = dst; 62 while (*dst++) 63 ; 64 --dst; // dst now points to end of dst string 65 66 67 /* if last char in string is a lead byte, back up pointer */ 68 69 if ( _ismbslead_l(start, dst, _loc_update.GetLocaleT()) ) 70 --dst; 71 72 /* copy over the characters */ 73 74 while (cnt--) { 75 if ( _ismbblead_l(*src, _loc_update.GetLocaleT()) ) { 76 *dst++ = *src++; 77 if ((*dst++ = *src++) == '\0') { 78 dst[-2] = '\0'; 79 break; 80 } 81 } 82 83 else if ((*dst++ = *src++) == '\0') 84 break; 85 86 } 87 88 /* enter final nul, if necessary */ 89 if ( dst!=start && _mbsbtype_l(start, (int) ((dst - start) - 1), _loc_update.GetLocaleT()) == _MBC_LEAD ) 90 { 91 dst[-1] = '\0'; 92 } 93 else 94 { 95 *dst = '\0'; 96 } 97 98 return(start); 99} 100 101extern "C" unsigned char * (__cdecl _mbsncat)( 102 unsigned char *dst, 103 const unsigned char *src, 104 size_t cnt 105 ) 106{ 107 _BEGIN_SECURE_CRT_DEPRECATION_DISABLE 108 return _mbsncat_l(dst, src, cnt, nullptr); 109 _END_SECURE_CRT_DEPRECATION_DISABLE 110}