2007-03-14 20:24:57 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* FILE: lib/msvcrt/mbstring/mbsncpy.c
|
|
|
|
* PURPOSE: Copies a string to a maximum of n bytes or characters
|
2008-12-18 22:06:07 +00:00
|
|
|
* PROGRAMERS:
|
|
|
|
* Copyright 1999 Ariadne
|
|
|
|
* Copyright 1999 Alexandre Julliard
|
|
|
|
* Copyright 2000 Jon Griffths
|
|
|
|
*
|
2007-03-14 20:24:57 +00:00
|
|
|
*/
|
|
|
|
|
2009-01-22 14:41:57 +00:00
|
|
|
#include <precomp.h>
|
2007-03-14 20:24:57 +00:00
|
|
|
#include <mbstring.h>
|
|
|
|
|
2012-12-09 03:17:14 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* _mbsncpy(MSVCRT.@)
|
|
|
|
* REMARKS
|
|
|
|
* The parameter n is the number or characters to copy, not the size of
|
|
|
|
* the buffer. Use _mbsnbcpy for a function analogical to strncpy
|
2007-03-14 20:24:57 +00:00
|
|
|
*/
|
2012-12-09 03:17:14 +00:00
|
|
|
unsigned char* CDECL _mbsncpy(unsigned char* dst, const unsigned char* src, size_t n)
|
2007-03-14 20:24:57 +00:00
|
|
|
{
|
2008-12-18 22:06:07 +00:00
|
|
|
unsigned char* ret = dst;
|
|
|
|
if(!n)
|
|
|
|
return dst;
|
2012-12-09 03:17:14 +00:00
|
|
|
if (get_mbcinfo()->ismbcodepage)
|
2008-12-18 22:06:07 +00:00
|
|
|
{
|
|
|
|
while (*src && n)
|
|
|
|
{
|
|
|
|
n--;
|
|
|
|
if (_ismbblead(*src))
|
|
|
|
{
|
|
|
|
if (!*(src+1))
|
|
|
|
{
|
|
|
|
*dst++ = 0;
|
|
|
|
*dst++ = 0;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-14 20:24:57 +00:00
|
|
|
|
2008-12-18 22:06:07 +00:00
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
2007-03-14 20:24:57 +00:00
|
|
|
|
2008-12-18 22:06:07 +00:00
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
n--;
|
|
|
|
if (!(*dst++ = *src++)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n--) *dst++ = 0;
|
|
|
|
return ret;
|
2007-03-14 20:24:57 +00:00
|
|
|
}
|
|
|
|
|
2012-12-09 03:17:14 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* _mbsnbcpy_s(MSVCRT.@)
|
|
|
|
* REMARKS
|
2009-01-22 14:41:57 +00:00
|
|
|
* Unlike _mbsnbcpy this function does not pad the rest of the dest
|
|
|
|
* string with 0
|
2012-12-09 03:17:14 +00:00
|
|
|
*/
|
2009-01-22 14:41:57 +00:00
|
|
|
int CDECL _mbsnbcpy_s(unsigned char* dst, size_t size, const unsigned char* src, size_t n)
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
if(!dst || size == 0)
|
|
|
|
return EINVAL;
|
|
|
|
if(!src)
|
|
|
|
{
|
|
|
|
dst[0] = '\0';
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
if(!n)
|
|
|
|
return 0;
|
|
|
|
|
2012-12-09 03:17:14 +00:00
|
|
|
if(get_mbcinfo()->ismbcodepage)
|
2009-01-22 14:41:57 +00:00
|
|
|
{
|
|
|
|
int is_lead = 0;
|
|
|
|
while (*src && n)
|
|
|
|
{
|
|
|
|
if(pos == size)
|
|
|
|
{
|
|
|
|
dst[0] = '\0';
|
|
|
|
return ERANGE;
|
|
|
|
}
|
|
|
|
is_lead = (!is_lead && _ismbblead(*src));
|
|
|
|
n--;
|
|
|
|
dst[pos++] = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_lead) /* if string ends with a lead, remove it */
|
|
|
|
dst[pos - 1] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
n--;
|
|
|
|
if(pos == size)
|
|
|
|
{
|
|
|
|
dst[0] = '\0';
|
|
|
|
return ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(*src)) break;
|
|
|
|
dst[pos++] = *src++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pos < size)
|
|
|
|
dst[pos] = '\0';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dst[0] = '\0';
|
|
|
|
return ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-09 03:17:14 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _mbsnbcpy(MSVCRT.@)
|
|
|
|
* REMARKS
|
|
|
|
* Like strncpy this function doesn't enforce the string to be
|
|
|
|
* NUL-terminated
|
|
|
|
*/
|
|
|
|
unsigned char* CDECL _mbsnbcpy(unsigned char* dst, const unsigned char* src, size_t n)
|
|
|
|
{
|
|
|
|
unsigned char* ret = dst;
|
|
|
|
if(!n)
|
|
|
|
return dst;
|
|
|
|
if(get_mbcinfo()->ismbcodepage)
|
|
|
|
{
|
|
|
|
int is_lead = 0;
|
|
|
|
while (*src && n)
|
|
|
|
{
|
|
|
|
is_lead = (!is_lead && _ismbblead(*src));
|
|
|
|
n--;
|
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_lead) /* if string ends with a lead, remove it */
|
|
|
|
*(dst - 1) = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
n--;
|
|
|
|
if (!(*dst++ = *src++)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n--) *dst++ = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|