Ejemplo n.º 1
0
errno_t memcpy_s(void* dest, size_t destMax, const void* src, size_t count)
{
    if (destMax == 0 || destMax > SECUREC_MEM_MAX_LEN )
    {
        SECUREC_ERROR_INVALID_RANGE("memcpy_s");
        return ERANGE;
    }

    if (dest == NULL || src == NULL)
    {
        if (dest != NULL )
        {
            (void)memset(dest, 0, destMax);
        }
        SECUREC_ERROR_INVALID_PARAMTER("memcpy_s");
        return EINVAL;
    }
    if (count > destMax)
    {
        (void)memset(dest, 0, destMax);
        SECUREC_ERROR_INVALID_RANGE("memcpy_s");
        return ERANGE;
    }
    if (dest == src)
    {
        return EOK;
    }
    if ((dest > src && dest < (void*)((uint8_t*)src + count)) ||
        (src > dest && src < (void*)((uint8_t*)dest + count)) )
    {
        (void)memset(dest, 0, destMax);
        SECUREC_ERROR_BUFFER_OVERLAP("memcpy_s");
        return EOVERLAP;
    }

#ifdef CALL_LIBC_COR_API
    /*use underlying memcpy for performance consideration*/
    (void)memcpy(dest, src, count);
#else
    /*
       User can use gcc's __SIZEOF_POINTER__ macro to copy memory by single byte, 4 bytes or 8 bytes.
       User can reference memcpy_32b() and memcpy_64b() in securecutil.c
    */
    memcpy_8b(dest, src, count );
#endif

    return EOK;
}
Ejemplo n.º 2
0
errno_t wcsncpy_s(wchar_t* strDest,    size_t destMax, const wchar_t* strSrc, size_t count)
{
    wchar_t*  pHeader = strDest;
    size_t maxSize = destMax;
    IN_REGISTER const wchar_t* overlapGuard = NULL;

    if ( destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN )
    {
        SECUREC_ERROR_INVALID_RANGE("wcsncpy_s");
        return ERANGE;
    }

    if (strDest == NULL || strSrc == NULL )
    {
        SECUREC_ERROR_INVALID_PARAMTER("wcsncpy_s");
        if (strDest != NULL)
        {
            pHeader[0] = '\0';
            return EINVAL_AND_RESET;
        }
        return EINVAL;
    }
#ifdef  COMPATIBLE_WIN_FORMAT
    if (count > SECUREC_WCHAR_STRING_MAX_LEN && count !=(size_t)-1)
#else
    if (count > SECUREC_WCHAR_STRING_MAX_LEN)
#endif    
    {
        pHeader[0] = '\0'; /*clear dest string*/
        SECUREC_ERROR_INVALID_RANGE("wcsncpy_s");
        return ERANGE_AND_RESET;
    }

    if (count == 0)
    {
        pHeader[0] = '\0';
        return EOK;
    }

    if (strDest < strSrc)
    {
        overlapGuard = strSrc;
        while ((*(strDest++) = *(strSrc++)) != '\0' && --maxSize > 0 && --count > 0)
        {
            if ( strDest == overlapGuard)
            {
                pHeader[0] = '\0';
                SECUREC_ERROR_BUFFER_OVERLAP("wcsncpy_s");
                return EOVERLAP_AND_RESET;
            }
        }
    }
    else
    {
        overlapGuard = strDest;
        while ((*(strDest++) = *(strSrc++)) != '\0' && --maxSize > 0 && --count > 0)
        {
            if ( strSrc == overlapGuard)
            {
                pHeader[0] = '\0';
                SECUREC_ERROR_BUFFER_OVERLAP("wcsncpy_s");
                return EOVERLAP_AND_RESET;
            }
        }
    }
    if (count == 0)
    {
        *strDest = '\0';
    }

    if (maxSize == 0)
    {
        pHeader[0] = '\0';
        SECUREC_ERROR_INVALID_RANGE("wcsncpy_s");
        return ERANGE_AND_RESET;
    }

    return EOK;
}