2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* MSVCRT string functions
|
|
|
|
*
|
|
|
|
* Copyright 1996,1998 Marcus Meissner
|
|
|
|
* Copyright 1996 Jukka Iivonen
|
|
|
|
* Copyright 1997,2000 Uwe Bonnes
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
|
|
|
|
2020-04-28 19:21:57 +02:00
|
|
|
#include <assert.h>
|
2006-01-14 17:40:29 +01:00
|
|
|
#include <stdlib.h>
|
2010-05-14 12:14:12 +02:00
|
|
|
#include <stdio.h>
|
2010-04-26 12:33:28 +02:00
|
|
|
#include <math.h>
|
|
|
|
#include <limits.h>
|
2020-11-26 19:19:05 +01:00
|
|
|
#include <locale.h>
|
2020-11-27 14:17:01 +01:00
|
|
|
#include <float.h>
|
2001-04-23 20:20:55 +02:00
|
|
|
#include "msvcrt.h"
|
2020-07-11 18:41:13 +02:00
|
|
|
#include "bnum.h"
|
2011-05-12 11:39:10 +02:00
|
|
|
#include "winnls.h"
|
2021-01-26 19:42:03 +01:00
|
|
|
#include "wine/asm.h"
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
2001-07-11 20:56:41 +02:00
|
|
|
* _mbsdup (MSVCRT.@)
|
2001-01-11 00:59:25 +01:00
|
|
|
* _strdup (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* CDECL _strdup(const char* str)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2006-06-21 10:55:11 +02:00
|
|
|
if(str)
|
|
|
|
{
|
2020-12-01 13:51:32 +01:00
|
|
|
char * ret = malloc(strlen(str)+1);
|
2006-06-21 10:55:11 +02:00
|
|
|
if (ret) strcpy( ret, str );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else return 0;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
|
2010-10-11 12:25:23 +02:00
|
|
|
/*********************************************************************
|
2011-05-12 11:38:41 +02:00
|
|
|
* _strlwr_s_l (MSVCRT.@)
|
2010-10-11 12:25:23 +02:00
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strlwr_s_l(char *str, size_t len, _locale_t locale)
|
2010-10-11 12:25:23 +02:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2010-10-11 12:25:23 +02:00
|
|
|
char *ptr = str;
|
|
|
|
|
|
|
|
if (!str || !len)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-10-11 12:25:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (len && *ptr)
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
{
|
|
|
|
str[0] = '\0';
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-10-11 12:25:23 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 10:04:46 +01:00
|
|
|
if(!locale)
|
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
if(!locinfo->lc_handle[LC_CTYPE])
|
2019-12-04 10:04:46 +01:00
|
|
|
{
|
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
if (*str >= 'A' && *str <= 'Z')
|
|
|
|
*str -= 'A' - 'a';
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2010-10-11 12:25:23 +02:00
|
|
|
{
|
2019-12-04 10:04:46 +01:00
|
|
|
while (*str)
|
|
|
|
{
|
2020-12-01 13:52:34 +01:00
|
|
|
*str = _tolower_l((unsigned char)*str, locale);
|
2019-12-04 10:04:46 +01:00
|
|
|
str++;
|
|
|
|
}
|
2010-10-11 12:25:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-12 11:38:41 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _strlwr_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strlwr_s(char *str, size_t len)
|
2011-05-12 11:38:41 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strlwr_s_l(str, len, NULL);
|
2011-05-12 11:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strlwr_l (MSVCRT.@)
|
|
|
|
*/
|
2020-11-26 19:19:18 +01:00
|
|
|
char* CDECL _strlwr_l(char *str, _locale_t locale)
|
2011-05-12 11:38:41 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
_strlwr_s_l(str, -1, locale);
|
2011-05-16 16:22:46 +02:00
|
|
|
return str;
|
2011-05-12 11:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strlwr (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* CDECL _strlwr(char *str)
|
2011-05-12 11:38:41 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
_strlwr_s_l(str, -1, NULL);
|
2011-05-16 16:22:46 +02:00
|
|
|
return str;
|
2011-05-12 11:38:41 +02:00
|
|
|
}
|
|
|
|
|
2011-05-12 11:38:52 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _strupr_s_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strupr_s_l(char *str, size_t len, _locale_t locale)
|
2011-05-12 11:38:52 +02:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2011-05-12 11:38:52 +02:00
|
|
|
char *ptr = str;
|
|
|
|
|
|
|
|
if (!str || !len)
|
|
|
|
{
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2011-05-12 11:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (len && *ptr)
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
{
|
|
|
|
str[0] = '\0';
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2011-05-12 11:38:52 +02:00
|
|
|
}
|
|
|
|
|
2018-03-22 18:51:50 +01:00
|
|
|
if(!locale)
|
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
if(!locinfo->lc_handle[LC_CTYPE])
|
2011-05-12 11:38:52 +02:00
|
|
|
{
|
2018-03-22 18:51:50 +01:00
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
if (*str >= 'a' && *str <= 'z')
|
|
|
|
*str -= 'a' - 'A';
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (*str)
|
|
|
|
{
|
2020-12-01 13:52:34 +01:00
|
|
|
*str = _toupper_l((unsigned char)*str, locale);
|
2018-03-22 18:51:50 +01:00
|
|
|
str++;
|
|
|
|
}
|
2011-05-12 11:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strupr_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strupr_s(char *str, size_t len)
|
2011-05-12 11:38:52 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strupr_s_l(str, len, NULL);
|
2011-05-12 11:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strupr_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* CDECL _strupr_l(char *str, _locale_t locale)
|
2011-05-12 11:38:52 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
_strupr_s_l(str, -1, locale);
|
2011-05-16 16:22:46 +02:00
|
|
|
return str;
|
2011-05-12 11:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strupr (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* CDECL _strupr(char *str)
|
2011-05-12 11:38:52 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
_strupr_s_l(str, -1, NULL);
|
2011-05-16 16:22:46 +02:00
|
|
|
return str;
|
2011-05-12 11:38:52 +02:00
|
|
|
}
|
|
|
|
|
2014-09-02 13:50:44 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _strnset_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strnset_s(char *str, size_t size, int c, size_t count)
|
2014-09-02 13:50:44 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t i;
|
2014-09-02 13:50:44 +02:00
|
|
|
|
|
|
|
if(!str && !size && !count) return 0;
|
2020-11-30 14:37:27 +01:00
|
|
|
if(!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
|
|
|
|
if(!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
|
2014-09-02 13:50:44 +02:00
|
|
|
|
|
|
|
for(i=0; i<size-1 && i<count; i++) {
|
|
|
|
if(!str[i]) return 0;
|
|
|
|
str[i] = c;
|
|
|
|
}
|
|
|
|
for(; i<size; i++)
|
|
|
|
if(!str[i]) return 0;
|
|
|
|
|
|
|
|
str[0] = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
_invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*_errno() = EINVAL;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2014-09-02 13:50:44 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _strnset (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* CDECL _strnset(char* str, int value, size_t len)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (len > 0 && str)
|
|
|
|
while (*str && len--)
|
|
|
|
*str++ = value;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strrev (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* CDECL _strrev(char* str)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char * p1;
|
|
|
|
char * p2;
|
|
|
|
|
|
|
|
if (str && *str)
|
|
|
|
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
|
|
|
|
{
|
|
|
|
*p1 ^= *p2;
|
|
|
|
*p2 ^= *p1;
|
|
|
|
*p1 ^= *p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strset (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 11:21:19 +02:00
|
|
|
char* CDECL _strset(char* str, int value)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
char *ptr = str;
|
|
|
|
while (*ptr)
|
|
|
|
*ptr++ = value;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2006-01-14 17:24:49 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strtok (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char * CDECL strtok( char *str, const char *delim )
|
2006-01-14 17:24:49 +01:00
|
|
|
{
|
|
|
|
thread_data_t *data = msvcrt_get_thread_data();
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
if (!(str = data->strtok_next)) return NULL;
|
|
|
|
|
|
|
|
while (*str && strchr( delim, *str )) str++;
|
2021-05-25 18:00:15 +02:00
|
|
|
if (!*str)
|
|
|
|
{
|
|
|
|
data->strtok_next = str;
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-01-14 17:24:49 +01:00
|
|
|
ret = str++;
|
|
|
|
while (*str && !strchr( delim, *str )) str++;
|
|
|
|
if (*str) *str++ = 0;
|
|
|
|
data->strtok_next = str;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-04-26 14:09:59 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* strtok_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char * CDECL strtok_s(char *str, const char *delim, char **ctx)
|
2010-04-26 14:09:59 +02:00
|
|
|
{
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) return NULL;
|
2010-04-26 14:09:59 +02:00
|
|
|
|
|
|
|
if(!str)
|
|
|
|
str = *ctx;
|
|
|
|
|
|
|
|
while(*str && strchr(delim, *str))
|
|
|
|
str++;
|
|
|
|
if(!*str)
|
2016-08-18 11:49:04 +02:00
|
|
|
{
|
|
|
|
*ctx = str;
|
2010-04-26 14:09:59 +02:00
|
|
|
return NULL;
|
2016-08-18 11:49:04 +02:00
|
|
|
}
|
2010-04-26 14:09:59 +02:00
|
|
|
|
|
|
|
*ctx = str+1;
|
|
|
|
while(**ctx && !strchr(delim, **ctx))
|
|
|
|
(*ctx)++;
|
|
|
|
if(**ctx)
|
|
|
|
*(*ctx)++ = 0;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2006-01-14 17:24:49 +01:00
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _swab (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
void CDECL _swab(char* src, char* dst, int len)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
|
|
|
if (len > 1)
|
|
|
|
{
|
|
|
|
len = (unsigned)len >> 1;
|
|
|
|
|
|
|
|
while (len--) {
|
2005-08-19 12:01:42 +02:00
|
|
|
char s0 = src[0];
|
|
|
|
char s1 = src[1];
|
|
|
|
*dst++ = s1;
|
|
|
|
*dst++ = s0;
|
|
|
|
src = src + 2;
|
2001-01-11 00:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-14 17:40:29 +01:00
|
|
|
|
2020-07-23 15:38:28 +02:00
|
|
|
static struct fpnum fpnum(int sign, int exp, ULONGLONG m, enum fpmod mod)
|
|
|
|
{
|
|
|
|
struct fpnum ret;
|
|
|
|
|
|
|
|
ret.sign = sign;
|
|
|
|
ret.exp = exp;
|
|
|
|
ret.m = m;
|
|
|
|
ret.mod = mod;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fpnum_double(struct fpnum *fp, double *d)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
|
|
|
ULONGLONG bits = 0;
|
|
|
|
|
2020-07-23 15:38:28 +02:00
|
|
|
if (fp->mod == FP_VAL_INFINITY)
|
|
|
|
{
|
|
|
|
*d = fp->sign * INFINITY;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fp->mod == FP_VAL_NAN)
|
|
|
|
{
|
|
|
|
bits = ~0;
|
|
|
|
if (fp->sign == 1)
|
|
|
|
bits &= ~((ULONGLONG)1 << (MANT_BITS + EXP_BITS - 1));
|
|
|
|
*d = *(double*)&bits;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("%c %s *2^%d (round %d)\n", fp->sign == -1 ? '-' : '+',
|
|
|
|
wine_dbgstr_longlong(fp->m), fp->exp, fp->mod);
|
|
|
|
if (!fp->m)
|
|
|
|
{
|
|
|
|
*d = fp->sign * 0.0;
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-22 16:41:30 +01:00
|
|
|
|
|
|
|
/* make sure that we don't overflow modifying exponent */
|
2020-07-23 15:38:28 +02:00
|
|
|
if (fp->exp > 1<<EXP_BITS)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
*d = fp->sign * INFINITY;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
2020-07-23 15:38:28 +02:00
|
|
|
if (fp->exp < -(1<<EXP_BITS))
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
*d = fp->sign * 0.0;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
2020-07-23 15:38:28 +02:00
|
|
|
fp->exp += MANT_BITS - 1;
|
2019-11-22 16:41:30 +01:00
|
|
|
|
|
|
|
/* normalize mantissa */
|
2020-07-23 15:38:28 +02:00
|
|
|
while(fp->m < (ULONGLONG)1 << (MANT_BITS-1))
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
fp->m <<= 1;
|
|
|
|
fp->exp--;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
2020-07-23 15:38:28 +02:00
|
|
|
while(fp->m >= (ULONGLONG)1 << MANT_BITS)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
if (fp->m & 1 || fp->mod != FP_ROUND_ZERO)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
if (!(fp->m & 1)) fp->mod = FP_ROUND_DOWN;
|
|
|
|
else if(fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
|
|
|
|
else fp->mod = FP_ROUND_UP;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
2020-07-23 15:38:28 +02:00
|
|
|
fp->m >>= 1;
|
|
|
|
fp->exp++;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
2020-07-23 15:38:28 +02:00
|
|
|
fp->exp += (1 << (EXP_BITS-1)) - 1;
|
2019-11-22 16:41:30 +01:00
|
|
|
|
|
|
|
/* handle subnormals */
|
2020-07-23 15:38:28 +02:00
|
|
|
if (fp->exp <= 0)
|
2020-07-23 15:38:46 +02:00
|
|
|
{
|
|
|
|
if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
|
|
|
|
else if (fp->m & 1) fp->mod = FP_ROUND_UP;
|
|
|
|
else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
|
2020-07-23 15:38:28 +02:00
|
|
|
fp->m >>= 1;
|
2020-07-23 15:38:46 +02:00
|
|
|
}
|
2020-07-23 15:38:28 +02:00
|
|
|
while(fp->m && fp->exp<0)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:46 +02:00
|
|
|
if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
|
|
|
|
else if (fp->m & 1) fp->mod = FP_ROUND_UP;
|
|
|
|
else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
|
2020-07-23 15:38:28 +02:00
|
|
|
fp->m >>= 1;
|
|
|
|
fp->exp++;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* round mantissa */
|
2020-07-23 15:38:28 +02:00
|
|
|
if (fp->mod == FP_ROUND_UP || (fp->mod == FP_ROUND_EVEN && fp->m & 1))
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
fp->m++;
|
2020-07-23 15:38:46 +02:00
|
|
|
|
|
|
|
/* handle subnormal that falls into regular range due to rounding */
|
|
|
|
if (fp->m == (ULONGLONG)1 << (MANT_BITS - 1))
|
|
|
|
{
|
|
|
|
fp->exp++;
|
|
|
|
}
|
|
|
|
else if (fp->m >= (ULONGLONG)1 << MANT_BITS)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
fp->exp++;
|
|
|
|
fp->m >>= 1;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:38:28 +02:00
|
|
|
if (fp->exp >= (1<<EXP_BITS)-1)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
*d = fp->sign * INFINITY;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
2020-07-23 15:38:28 +02:00
|
|
|
if (!fp->m || fp->exp < 0)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
*d = fp->sign * 0.0;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:38:28 +02:00
|
|
|
if (fp->sign == -1)
|
|
|
|
bits |= (ULONGLONG)1 << (MANT_BITS + EXP_BITS - 1);
|
|
|
|
bits |= (ULONGLONG)fp->exp << (MANT_BITS - 1);
|
|
|
|
bits |= fp->m & (((ULONGLONG)1 << (MANT_BITS - 1)) - 1);
|
2019-11-22 16:41:30 +01:00
|
|
|
|
|
|
|
TRACE("returning %s\n", wine_dbgstr_longlong(bits));
|
2020-07-23 15:38:28 +02:00
|
|
|
*d = *(double*)&bits;
|
|
|
|
return 0;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:38:40 +02:00
|
|
|
#define LDBL_EXP_BITS 15
|
|
|
|
#define LDBL_MANT_BITS 64
|
|
|
|
int fpnum_ldouble(struct fpnum *fp, MSVCRT__LDOUBLE *d)
|
|
|
|
{
|
|
|
|
if (fp->mod == FP_VAL_INFINITY)
|
|
|
|
{
|
|
|
|
d->x80[0] = 0;
|
|
|
|
d->x80[1] = 0x80000000;
|
|
|
|
d->x80[2] = (1 << LDBL_EXP_BITS) - 1;
|
|
|
|
if (fp->sign == -1)
|
|
|
|
d->x80[2] |= 1 << LDBL_EXP_BITS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fp->mod == FP_VAL_NAN)
|
|
|
|
{
|
|
|
|
d->x80[0] = ~0;
|
|
|
|
d->x80[1] = ~0;
|
|
|
|
d->x80[2] = (1 << LDBL_EXP_BITS) - 1;
|
|
|
|
if (fp->sign == -1)
|
|
|
|
d->x80[2] |= 1 << LDBL_EXP_BITS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("%c %s *2^%d (round %d)\n", fp->sign == -1 ? '-' : '+',
|
|
|
|
wine_dbgstr_longlong(fp->m), fp->exp, fp->mod);
|
|
|
|
if (!fp->m)
|
|
|
|
{
|
|
|
|
d->x80[0] = 0;
|
|
|
|
d->x80[1] = 0;
|
|
|
|
d->x80[2] = 0;
|
|
|
|
if (fp->sign == -1)
|
|
|
|
d->x80[2] |= 1 << LDBL_EXP_BITS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure that we don't overflow modifying exponent */
|
|
|
|
if (fp->exp > 1<<LDBL_EXP_BITS)
|
|
|
|
{
|
|
|
|
d->x80[0] = 0;
|
|
|
|
d->x80[1] = 0x80000000;
|
|
|
|
d->x80[2] = (1 << LDBL_EXP_BITS) - 1;
|
|
|
|
if (fp->sign == -1)
|
|
|
|
d->x80[2] |= 1 << LDBL_EXP_BITS;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2020-07-23 15:38:40 +02:00
|
|
|
}
|
|
|
|
if (fp->exp < -(1<<LDBL_EXP_BITS))
|
|
|
|
{
|
|
|
|
d->x80[0] = 0;
|
|
|
|
d->x80[1] = 0;
|
|
|
|
d->x80[2] = 0;
|
|
|
|
if (fp->sign == -1)
|
|
|
|
d->x80[2] |= 1 << LDBL_EXP_BITS;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2020-07-23 15:38:40 +02:00
|
|
|
}
|
|
|
|
fp->exp += LDBL_MANT_BITS - 1;
|
|
|
|
|
|
|
|
/* normalize mantissa */
|
|
|
|
while(fp->m < (ULONGLONG)1 << (LDBL_MANT_BITS-1))
|
|
|
|
{
|
|
|
|
fp->m <<= 1;
|
|
|
|
fp->exp--;
|
|
|
|
}
|
|
|
|
fp->exp += (1 << (LDBL_EXP_BITS-1)) - 1;
|
|
|
|
|
|
|
|
/* handle subnormals */
|
|
|
|
if (fp->exp <= 0)
|
|
|
|
{
|
|
|
|
if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
|
|
|
|
else if (fp->m & 1) fp->mod = FP_ROUND_UP;
|
|
|
|
else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
|
|
|
|
fp->m >>= 1;
|
|
|
|
}
|
|
|
|
while(fp->m && fp->exp<0)
|
|
|
|
{
|
|
|
|
if (fp->m & 1 && fp->mod == FP_ROUND_ZERO) fp->mod = FP_ROUND_EVEN;
|
|
|
|
else if (fp->m & 1) fp->mod = FP_ROUND_UP;
|
|
|
|
else if (fp->mod != FP_ROUND_ZERO) fp->mod = FP_ROUND_DOWN;
|
|
|
|
fp->m >>= 1;
|
|
|
|
fp->exp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* round mantissa */
|
|
|
|
if (fp->mod == FP_ROUND_UP || (fp->mod == FP_ROUND_EVEN && fp->m & 1))
|
|
|
|
{
|
2020-11-27 14:17:01 +01:00
|
|
|
if (fp->m == UI64_MAX)
|
2020-07-23 15:38:40 +02:00
|
|
|
{
|
|
|
|
fp->m = (ULONGLONG)1 << (LDBL_MANT_BITS - 1);
|
|
|
|
fp->exp++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fp->m++;
|
|
|
|
|
|
|
|
/* handle subnormal that falls into regular range due to rounding */
|
|
|
|
if ((fp->m ^ (fp->m - 1)) & ((ULONGLONG)1 << (LDBL_MANT_BITS - 1))) fp->exp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fp->exp >= (1<<LDBL_EXP_BITS)-1)
|
|
|
|
{
|
|
|
|
d->x80[0] = 0;
|
|
|
|
d->x80[1] = 0x80000000;
|
|
|
|
d->x80[2] = (1 << LDBL_EXP_BITS) - 1;
|
|
|
|
if (fp->sign == -1)
|
|
|
|
d->x80[2] |= 1 << LDBL_EXP_BITS;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2020-07-23 15:38:40 +02:00
|
|
|
}
|
|
|
|
if (!fp->m || fp->exp < 0)
|
|
|
|
{
|
|
|
|
d->x80[0] = 0;
|
|
|
|
d->x80[1] = 0;
|
|
|
|
d->x80[2] = 0;
|
|
|
|
if (fp->sign == -1)
|
|
|
|
d->x80[2] |= 1 << LDBL_EXP_BITS;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2020-07-23 15:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d->x80[0] = fp->m;
|
|
|
|
d->x80[1] = fp->m >> 32;
|
|
|
|
d->x80[2] = fp->exp;
|
|
|
|
if (fp->sign == -1)
|
|
|
|
d->x80[2] |= 1 << LDBL_EXP_BITS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:23:47 +01:00
|
|
|
#if _MSVCR_VER >= 140
|
|
|
|
|
2019-11-22 16:41:30 +01:00
|
|
|
static inline int hex2int(char c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
return c - 'a' + 10;
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
return c - 'A' + 10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
static struct fpnum fpnum_parse16(wchar_t get(void *ctx), void unget(void *ctx),
|
2020-11-26 19:19:09 +01:00
|
|
|
void *ctx, int sign, pthreadlocinfo locinfo)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2019-12-31 19:19:07 +01:00
|
|
|
BOOL found_digit = FALSE, found_dp = FALSE;
|
2020-07-23 15:38:28 +02:00
|
|
|
enum fpmod round = FP_ROUND_ZERO;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t nch;
|
2019-11-22 16:41:30 +01:00
|
|
|
ULONGLONG m = 0;
|
|
|
|
int val, exp = 0;
|
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2020-11-27 14:17:01 +01:00
|
|
|
while(m < UI64_MAX/16)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2019-12-31 19:19:07 +01:00
|
|
|
val = hex2int(nch);
|
2019-11-22 16:41:30 +01:00
|
|
|
if (val == -1) break;
|
|
|
|
found_digit = TRUE;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2019-11-22 16:41:30 +01:00
|
|
|
|
|
|
|
m = m*16 + val;
|
|
|
|
}
|
|
|
|
while(1)
|
|
|
|
{
|
2019-12-31 19:19:07 +01:00
|
|
|
val = hex2int(nch);
|
2019-11-22 16:41:30 +01:00
|
|
|
if (val == -1) break;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2019-11-22 16:41:30 +01:00
|
|
|
exp += 4;
|
|
|
|
|
2020-07-23 15:38:28 +02:00
|
|
|
if (val || round != FP_ROUND_ZERO)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
if (val < 8) round = FP_ROUND_DOWN;
|
|
|
|
else if (val == 8 && round == FP_ROUND_ZERO) round = FP_ROUND_EVEN;
|
|
|
|
else round = FP_ROUND_UP;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
if(nch == *locinfo->lconv->decimal_point)
|
|
|
|
{
|
|
|
|
found_dp = TRUE;
|
|
|
|
nch = get(ctx);
|
|
|
|
}
|
2019-11-22 16:41:30 +01:00
|
|
|
else if (!found_digit)
|
|
|
|
{
|
2020-11-30 14:37:05 +01:00
|
|
|
if(nch!=WEOF) unget(ctx);
|
2019-12-31 19:19:07 +01:00
|
|
|
unget(ctx);
|
2020-07-23 15:38:28 +02:00
|
|
|
return fpnum(0, 0, 0, 0);
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:17:01 +01:00
|
|
|
while(m <= UI64_MAX/16)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2019-12-31 19:19:07 +01:00
|
|
|
val = hex2int(nch);
|
2019-11-22 16:41:30 +01:00
|
|
|
if (val == -1) break;
|
|
|
|
found_digit = TRUE;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2019-11-22 16:41:30 +01:00
|
|
|
|
|
|
|
m = m*16 + val;
|
|
|
|
exp -= 4;
|
|
|
|
}
|
|
|
|
while(1)
|
|
|
|
{
|
2019-12-31 19:19:07 +01:00
|
|
|
val = hex2int(nch);
|
2019-11-22 16:41:30 +01:00
|
|
|
if (val == -1) break;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2019-11-22 16:41:30 +01:00
|
|
|
|
2020-07-23 15:38:28 +02:00
|
|
|
if (val || round != FP_ROUND_ZERO)
|
2019-11-22 16:41:30 +01:00
|
|
|
{
|
2020-07-23 15:38:28 +02:00
|
|
|
if (val < 8) round = FP_ROUND_DOWN;
|
|
|
|
else if (val == 8 && round == FP_ROUND_ZERO) round = FP_ROUND_EVEN;
|
|
|
|
else round = FP_ROUND_UP;
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found_digit)
|
|
|
|
{
|
2020-11-30 14:37:05 +01:00
|
|
|
if (nch != WEOF) unget(ctx);
|
2019-12-31 19:19:07 +01:00
|
|
|
if (found_dp) unget(ctx);
|
|
|
|
unget(ctx);
|
2020-07-23 15:38:28 +02:00
|
|
|
return fpnum(0, 0, 0, 0);
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
if(nch=='p' || nch=='P') {
|
|
|
|
BOOL found_sign = FALSE;
|
2019-11-22 16:41:30 +01:00
|
|
|
int e=0, s=1;
|
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
|
|
|
if(nch == '-') {
|
|
|
|
found_sign = TRUE;
|
2019-11-22 16:41:30 +01:00
|
|
|
s = -1;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
|
|
|
} else if(nch == '+') {
|
|
|
|
found_sign = TRUE;
|
|
|
|
nch = get(ctx);
|
|
|
|
}
|
|
|
|
if(nch>='0' && nch<='9') {
|
|
|
|
while(nch>='0' && nch<='9') {
|
2020-05-01 11:58:11 +02:00
|
|
|
if(e>INT_MAX/10 || e*10>INT_MAX-nch+'0')
|
2019-11-22 16:41:30 +01:00
|
|
|
e = INT_MAX;
|
2020-05-01 11:58:11 +02:00
|
|
|
else
|
|
|
|
e = e*10+nch-'0';
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
2020-11-30 14:37:05 +01:00
|
|
|
if((nch!=WEOF) && (nch < '0' || nch > '9')) unget(ctx);
|
2019-11-22 16:41:30 +01:00
|
|
|
e *= s;
|
|
|
|
|
2020-04-28 19:21:40 +02:00
|
|
|
if(e<0 && exp<INT_MIN-e) exp = INT_MIN;
|
|
|
|
else if(e>0 && exp>INT_MAX-e) exp = INT_MAX;
|
2019-11-22 16:41:30 +01:00
|
|
|
else exp += e;
|
|
|
|
} else {
|
2020-11-30 14:37:05 +01:00
|
|
|
if(nch != WEOF) unget(ctx);
|
2019-12-31 19:19:07 +01:00
|
|
|
if(found_sign) unget(ctx);
|
|
|
|
unget(ctx);
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:38:28 +02:00
|
|
|
return fpnum(sign, exp, m, round);
|
2019-11-22 16:41:30 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-07-23 15:38:34 +02:00
|
|
|
/* Converts first 3 limbs to ULONGLONG */
|
|
|
|
/* Return FALSE on overflow */
|
|
|
|
static inline BOOL bnum_to_mant(struct bnum *b, ULONGLONG *m)
|
|
|
|
{
|
2020-11-27 14:17:01 +01:00
|
|
|
if(UI64_MAX / LIMB_MAX / LIMB_MAX < b->data[bnum_idx(b, b->e-1)]) return FALSE;
|
2020-07-23 15:38:34 +02:00
|
|
|
*m = (ULONGLONG)b->data[bnum_idx(b, b->e-1)] * LIMB_MAX * LIMB_MAX;
|
|
|
|
if(b->b == b->e-1) return TRUE;
|
2020-11-27 14:17:01 +01:00
|
|
|
if(UI64_MAX - *m < (ULONGLONG)b->data[bnum_idx(b, b->e-2)] * LIMB_MAX) return FALSE;
|
2020-07-23 15:38:34 +02:00
|
|
|
*m += (ULONGLONG)b->data[bnum_idx(b, b->e-2)] * LIMB_MAX;
|
|
|
|
if(b->b == b->e-2) return TRUE;
|
2020-11-27 14:17:01 +01:00
|
|
|
if(UI64_MAX - *m < b->data[bnum_idx(b, b->e-3)]) return FALSE;
|
2020-07-23 15:38:34 +02:00
|
|
|
*m += b->data[bnum_idx(b, b->e-3)];
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
static struct fpnum fpnum_parse_bnum(wchar_t (*get)(void *ctx), void (*unget)(void *ctx),
|
2020-11-26 19:19:09 +01:00
|
|
|
void *ctx, pthreadlocinfo locinfo, BOOL ldouble, struct bnum *b)
|
2010-04-21 11:26:22 +02:00
|
|
|
{
|
2019-12-31 19:19:07 +01:00
|
|
|
#if _MSVCR_VER >= 140
|
2020-11-27 14:16:53 +01:00
|
|
|
const wchar_t _infinity[] = L"infinity";
|
|
|
|
const wchar_t _nan[] = L"nan";
|
|
|
|
const wchar_t *str_match = NULL;
|
2019-12-31 19:19:07 +01:00
|
|
|
int matched=0;
|
|
|
|
#endif
|
|
|
|
BOOL found_digit = FALSE, found_dp = FALSE, found_sign = FALSE;
|
2020-04-28 19:21:57 +02:00
|
|
|
int e2 = 0, dp=0, sign=1, off, limb_digits = 0, i;
|
2020-07-23 15:38:28 +02:00
|
|
|
enum fpmod round = FP_ROUND_ZERO;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t nch;
|
2020-07-23 15:38:34 +02:00
|
|
|
ULONGLONG m;
|
2010-04-21 11:26:22 +02:00
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
|
|
|
if(nch == '-') {
|
|
|
|
found_sign = TRUE;
|
2010-04-26 12:33:28 +02:00
|
|
|
sign = -1;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
|
|
|
} else if(nch == '+') {
|
|
|
|
found_sign = TRUE;
|
|
|
|
nch = get(ctx);
|
|
|
|
}
|
2010-04-26 12:33:28 +02:00
|
|
|
|
2015-09-29 13:40:12 +02:00
|
|
|
#if _MSVCR_VER >= 140
|
2020-12-01 13:52:34 +01:00
|
|
|
if(nch == _infinity[0] || nch == _toupper(_infinity[0]))
|
2019-12-31 19:19:07 +01:00
|
|
|
str_match = _infinity;
|
2020-12-01 13:52:34 +01:00
|
|
|
if(nch == _nan[0] || nch == _toupper(_nan[0]))
|
2019-12-31 19:19:07 +01:00
|
|
|
str_match = _nan;
|
2020-11-30 14:37:05 +01:00
|
|
|
while(str_match && nch != WEOF &&
|
2020-12-01 13:52:34 +01:00
|
|
|
(nch == str_match[matched] || nch == _toupper(str_match[matched]))) {
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
|
|
|
matched++;
|
|
|
|
}
|
|
|
|
if(str_match) {
|
|
|
|
int keep = 0;
|
|
|
|
if(matched >= 8) keep = 8;
|
|
|
|
else if(matched >= 3) keep = 3;
|
2020-11-30 14:37:05 +01:00
|
|
|
if(nch != WEOF) unget(ctx);
|
2019-12-31 19:19:07 +01:00
|
|
|
for (; matched > keep; matched--) {
|
|
|
|
unget(ctx);
|
|
|
|
}
|
|
|
|
if(keep) {
|
2020-07-23 15:38:28 +02:00
|
|
|
if (str_match == _infinity)
|
|
|
|
return fpnum(sign, 0, 0, FP_VAL_INFINITY);
|
|
|
|
if (str_match == _nan)
|
|
|
|
return fpnum(sign, 0, 0, FP_VAL_NAN);
|
2020-01-07 15:35:52 +01:00
|
|
|
} else if(found_sign) {
|
|
|
|
unget(ctx);
|
2019-12-31 19:19:07 +01:00
|
|
|
}
|
2020-01-07 15:35:52 +01:00
|
|
|
|
2020-07-23 15:38:28 +02:00
|
|
|
return fpnum(0, 0, 0, 0);
|
2019-12-31 19:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(nch == '0') {
|
2020-01-03 19:41:56 +01:00
|
|
|
found_digit = TRUE;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
|
|
|
if(nch == 'x' || nch == 'X')
|
2020-07-23 15:38:28 +02:00
|
|
|
return fpnum_parse16(get, unget, ctx, sign, locinfo);
|
2015-09-29 13:40:12 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-04 11:30:24 +02:00
|
|
|
while(nch == '0') {
|
|
|
|
found_digit = TRUE;
|
|
|
|
nch = get(ctx);
|
|
|
|
}
|
2020-04-28 19:21:57 +02:00
|
|
|
|
2020-07-23 15:38:25 +02:00
|
|
|
b->b = 0;
|
|
|
|
b->e = 1;
|
|
|
|
b->data[0] = 0;
|
2019-12-31 19:19:07 +01:00
|
|
|
while(nch>='0' && nch<='9') {
|
2010-07-22 12:35:08 +02:00
|
|
|
found_digit = TRUE;
|
2020-04-28 19:21:57 +02:00
|
|
|
if(limb_digits == LIMB_DIGITS) {
|
2020-07-23 15:38:25 +02:00
|
|
|
if(bnum_idx(b, b->b-1) == bnum_idx(b, b->e)) break;
|
2020-04-28 19:21:57 +02:00
|
|
|
else {
|
2020-07-23 15:38:25 +02:00
|
|
|
b->b--;
|
|
|
|
b->data[bnum_idx(b, b->b)] = 0;
|
2020-04-28 19:21:57 +02:00
|
|
|
limb_digits = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:38:25 +02:00
|
|
|
b->data[bnum_idx(b, b->b)] = b->data[bnum_idx(b, b->b)] * 10 + nch - '0';
|
2020-04-28 19:21:57 +02:00
|
|
|
limb_digits++;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2020-04-28 19:21:57 +02:00
|
|
|
dp++;
|
2010-04-26 12:33:28 +02:00
|
|
|
}
|
2019-12-31 19:19:07 +01:00
|
|
|
while(nch>='0' && nch<='9') {
|
2020-07-23 15:38:25 +02:00
|
|
|
if(nch != '0') b->data[bnum_idx(b, b->b)] |= 1;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2020-04-28 19:21:57 +02:00
|
|
|
dp++;
|
2010-04-21 11:26:22 +02:00
|
|
|
}
|
2010-04-26 12:33:28 +02:00
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
if(nch == *locinfo->lconv->decimal_point) {
|
|
|
|
found_dp = TRUE;
|
|
|
|
nch = get(ctx);
|
|
|
|
}
|
2010-04-21 11:26:22 +02:00
|
|
|
|
2020-04-28 19:21:57 +02:00
|
|
|
/* skip leading '0' */
|
2020-07-23 15:38:25 +02:00
|
|
|
if(nch=='0' && !limb_digits && !b->b) {
|
2020-04-28 19:21:57 +02:00
|
|
|
found_digit = TRUE;
|
|
|
|
while(nch == '0') {
|
|
|
|
nch = get(ctx);
|
|
|
|
dp--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
while(nch>='0' && nch<='9') {
|
2010-07-22 12:35:08 +02:00
|
|
|
found_digit = TRUE;
|
2020-04-28 19:21:57 +02:00
|
|
|
if(limb_digits == LIMB_DIGITS) {
|
2020-07-23 15:38:25 +02:00
|
|
|
if(bnum_idx(b, b->b-1) == bnum_idx(b, b->e)) break;
|
2020-04-28 19:21:57 +02:00
|
|
|
else {
|
2020-07-23 15:38:25 +02:00
|
|
|
b->b--;
|
|
|
|
b->data[bnum_idx(b, b->b)] = 0;
|
2020-04-28 19:21:57 +02:00
|
|
|
limb_digits = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:38:25 +02:00
|
|
|
b->data[bnum_idx(b, b->b)] = b->data[bnum_idx(b, b->b)] * 10 + nch - '0';
|
2020-04-28 19:21:57 +02:00
|
|
|
limb_digits++;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2010-04-26 12:33:28 +02:00
|
|
|
}
|
2020-04-28 19:21:57 +02:00
|
|
|
while(nch>='0' && nch<='9') {
|
2020-07-23 15:38:25 +02:00
|
|
|
if(nch != '0') b->data[bnum_idx(b, b->b)] |= 1;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2020-04-28 19:21:57 +02:00
|
|
|
}
|
2010-04-21 11:26:22 +02:00
|
|
|
|
2010-07-22 12:35:08 +02:00
|
|
|
if(!found_digit) {
|
2020-11-30 14:37:05 +01:00
|
|
|
if(nch != WEOF) unget(ctx);
|
2019-12-31 19:19:07 +01:00
|
|
|
if(found_dp) unget(ctx);
|
|
|
|
if(found_sign) unget(ctx);
|
2020-07-23 15:38:28 +02:00
|
|
|
return fpnum(0, 0, 0, 0);
|
2010-04-26 12:33:28 +02:00
|
|
|
}
|
2010-04-21 11:26:22 +02:00
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
if(nch=='e' || nch=='E' || nch=='d' || nch=='D') {
|
2010-04-26 12:33:28 +02:00
|
|
|
int e=0, s=1;
|
2010-04-21 11:26:22 +02:00
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
|
|
|
if(nch == '-') {
|
|
|
|
found_sign = TRUE;
|
2010-04-26 12:33:28 +02:00
|
|
|
s = -1;
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
|
|
|
} else if(nch == '+') {
|
|
|
|
found_sign = TRUE;
|
|
|
|
nch = get(ctx);
|
|
|
|
} else {
|
|
|
|
found_sign = FALSE;
|
|
|
|
}
|
2010-04-21 11:26:22 +02:00
|
|
|
|
2019-12-31 19:19:07 +01:00
|
|
|
if(nch>='0' && nch<='9') {
|
|
|
|
while(nch>='0' && nch<='9') {
|
2020-05-01 11:58:11 +02:00
|
|
|
if(e>INT_MAX/10 || e*10>INT_MAX-nch+'0')
|
2010-04-26 12:33:28 +02:00
|
|
|
e = INT_MAX;
|
2020-05-01 11:58:11 +02:00
|
|
|
else
|
|
|
|
e = e*10+nch-'0';
|
2019-12-31 19:19:07 +01:00
|
|
|
nch = get(ctx);
|
2010-04-26 12:33:28 +02:00
|
|
|
}
|
2020-11-30 14:37:05 +01:00
|
|
|
if(nch != WEOF) unget(ctx);
|
2010-04-26 12:33:28 +02:00
|
|
|
e *= s;
|
2010-04-21 11:26:22 +02:00
|
|
|
|
2020-04-28 19:21:57 +02:00
|
|
|
if(e<0 && dp<INT_MIN-e) dp = INT_MIN;
|
|
|
|
else if(e>0 && dp>INT_MAX-e) dp = INT_MAX;
|
|
|
|
else dp += e;
|
2010-04-26 12:33:28 +02:00
|
|
|
} else {
|
2020-11-30 14:37:05 +01:00
|
|
|
if(nch != WEOF) unget(ctx);
|
2019-12-31 19:19:07 +01:00
|
|
|
if(found_sign) unget(ctx);
|
|
|
|
unget(ctx);
|
2010-04-26 12:33:28 +02:00
|
|
|
}
|
2020-11-30 14:37:05 +01:00
|
|
|
} else if(nch != WEOF) {
|
2020-01-02 19:01:56 +01:00
|
|
|
unget(ctx);
|
2010-04-26 12:33:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:38:28 +02:00
|
|
|
if(!b->data[bnum_idx(b, b->e-1)])
|
|
|
|
return fpnum(sign, 0, 0, 0);
|
2020-04-28 19:21:57 +02:00
|
|
|
|
|
|
|
/* Fill last limb with 0 if needed */
|
2020-07-23 15:38:25 +02:00
|
|
|
if(b->b+1 != b->e) {
|
2020-04-28 19:21:57 +02:00
|
|
|
for(; limb_digits != LIMB_DIGITS; limb_digits++)
|
2020-07-23 15:38:25 +02:00
|
|
|
b->data[bnum_idx(b, b->b)] *= 10;
|
2020-04-28 19:21:57 +02:00
|
|
|
}
|
2020-07-23 15:38:25 +02:00
|
|
|
for(; bnum_idx(b, b->b) < bnum_idx(b, b->e); b->b++) {
|
|
|
|
if(b->data[bnum_idx(b, b->b)]) break;
|
2020-04-28 19:21:57 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 12:18:21 +02:00
|
|
|
/* move decimal point to limb boundary */
|
2020-07-23 15:38:25 +02:00
|
|
|
if(limb_digits==dp && b->b==b->e-1)
|
2020-07-23 15:38:28 +02:00
|
|
|
return fpnum(sign, 0, b->data[bnum_idx(b, b->e-1)], FP_ROUND_ZERO);
|
2020-04-28 19:21:57 +02:00
|
|
|
off = (dp - limb_digits) % LIMB_DIGITS;
|
|
|
|
if(off < 0) off += LIMB_DIGITS;
|
2020-07-23 15:38:25 +02:00
|
|
|
if(off) bnum_mult(b, p10s[off]);
|
2020-04-28 19:21:57 +02:00
|
|
|
|
2020-11-27 14:17:01 +01:00
|
|
|
if(dp-1 > (ldouble ? DBL80_MAX_10_EXP : DBL_MAX_10_EXP))
|
2020-07-23 15:38:28 +02:00
|
|
|
return fpnum(sign, INT_MAX, 1, FP_ROUND_ZERO);
|
2019-12-30 13:23:47 +01:00
|
|
|
/* Count part of exponent stored in denormalized mantissa. */
|
|
|
|
/* Increase exponent range to handle subnormals. */
|
2020-11-27 14:17:01 +01:00
|
|
|
if(dp-1 < (ldouble ? DBL80_MIN_10_EXP : DBL_MIN_10_EXP-DBL_DIG-18))
|
2020-07-23 15:38:28 +02:00
|
|
|
return fpnum(sign, INT_MIN, 1, FP_ROUND_ZERO);
|
2020-04-28 19:21:57 +02:00
|
|
|
|
2020-07-23 15:38:34 +02:00
|
|
|
while(dp > 3*LIMB_DIGITS) {
|
2020-07-23 15:38:25 +02:00
|
|
|
if(bnum_rshift(b, 9)) dp -= LIMB_DIGITS;
|
2020-04-28 19:21:57 +02:00
|
|
|
e2 += 9;
|
|
|
|
}
|
2020-07-23 15:38:34 +02:00
|
|
|
while(dp <= 2*LIMB_DIGITS) {
|
2020-07-23 15:38:25 +02:00
|
|
|
if(bnum_lshift(b, 29)) dp += LIMB_DIGITS;
|
2020-04-28 19:21:57 +02:00
|
|
|
e2 -= 29;
|
|
|
|
}
|
2020-07-23 15:38:34 +02:00
|
|
|
/* Make sure most significant mantissa bit will be set */
|
|
|
|
while(b->data[bnum_idx(b, b->e-1)] <= 9) {
|
2020-07-23 15:38:25 +02:00
|
|
|
bnum_lshift(b, 1);
|
2020-04-28 19:21:57 +02:00
|
|
|
e2--;
|
|
|
|
}
|
2020-07-23 15:38:34 +02:00
|
|
|
while(!bnum_to_mant(b, &m)) {
|
|
|
|
bnum_rshift(b, 1);
|
|
|
|
e2++;
|
|
|
|
}
|
2020-04-28 19:21:57 +02:00
|
|
|
|
2020-07-23 15:38:37 +02:00
|
|
|
if(b->e-4 >= b->b && b->data[bnum_idx(b, b->e-4)]) {
|
|
|
|
if(b->data[bnum_idx(b, b->e-4)] > LIMB_MAX/2) round = FP_ROUND_UP;
|
|
|
|
else if(b->data[bnum_idx(b, b->e-4)] == LIMB_MAX/2) round = FP_ROUND_EVEN;
|
|
|
|
else round = FP_ROUND_DOWN;
|
|
|
|
}
|
|
|
|
if(round == FP_ROUND_ZERO || round == FP_ROUND_EVEN) {
|
|
|
|
for(i=b->e-5; i>=b->b; i--) {
|
|
|
|
if(!b->data[bnum_idx(b, b->b)]) continue;
|
|
|
|
if(round == FP_ROUND_EVEN) round = FP_ROUND_UP;
|
|
|
|
else round = FP_ROUND_DOWN;
|
|
|
|
}
|
2020-04-28 19:21:57 +02:00
|
|
|
}
|
2019-12-30 13:23:47 +01:00
|
|
|
|
2020-07-23 15:38:34 +02:00
|
|
|
return fpnum(sign, e2, m, round);
|
2010-04-21 11:26:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
struct fpnum fpnum_parse(wchar_t (*get)(void *ctx), void (*unget)(void *ctx),
|
2020-11-26 19:19:09 +01:00
|
|
|
void *ctx, pthreadlocinfo locinfo, BOOL ldouble)
|
2020-07-23 15:38:40 +02:00
|
|
|
{
|
|
|
|
if(!ldouble) {
|
|
|
|
BYTE bnum_data[FIELD_OFFSET(struct bnum, data[BNUM_PREC64])];
|
|
|
|
struct bnum *b = (struct bnum*)bnum_data;
|
|
|
|
|
|
|
|
b->size = BNUM_PREC64;
|
|
|
|
return fpnum_parse_bnum(get, unget, ctx, locinfo, ldouble, b);
|
|
|
|
} else {
|
|
|
|
BYTE bnum_data[FIELD_OFFSET(struct bnum, data[BNUM_PREC80])];
|
|
|
|
struct bnum *b = (struct bnum*)bnum_data;
|
|
|
|
|
|
|
|
b->size = BNUM_PREC80;
|
|
|
|
return fpnum_parse_bnum(get, unget, ctx, locinfo, ldouble, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
static wchar_t strtod_str_get(void *ctx)
|
2019-12-31 19:19:07 +01:00
|
|
|
{
|
|
|
|
const char **p = ctx;
|
2020-11-30 14:37:05 +01:00
|
|
|
if (!**p) return WEOF;
|
2019-12-31 19:19:07 +01:00
|
|
|
return *(*p)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void strtod_str_unget(void *ctx)
|
|
|
|
{
|
|
|
|
const char **p = ctx;
|
|
|
|
(*p)--;
|
|
|
|
}
|
|
|
|
|
2020-11-26 19:19:18 +01:00
|
|
|
static inline double strtod_helper(const char *str, char **end, _locale_t locale, int *perr)
|
2019-12-31 19:19:07 +01:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2019-12-31 19:19:07 +01:00
|
|
|
const char *beg, *p;
|
2020-07-23 15:38:28 +02:00
|
|
|
struct fpnum fp;
|
2019-12-31 19:19:07 +01:00
|
|
|
double ret;
|
2020-07-23 15:38:28 +02:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (perr) *perr = 0;
|
|
|
|
#if _MSVCR_VER == 0
|
2020-12-01 13:51:23 +01:00
|
|
|
else *_errno() = 0;
|
2020-07-23 15:38:28 +02:00
|
|
|
#endif
|
2019-12-31 19:19:07 +01:00
|
|
|
|
|
|
|
if (!MSVCRT_CHECK_PMT(str != NULL)) {
|
|
|
|
if (end) *end = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!locale)
|
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
|
|
|
|
|
|
|
p = str;
|
2020-12-01 13:52:34 +01:00
|
|
|
while(_isspace_l((unsigned char)*p, locale))
|
2019-12-31 19:19:07 +01:00
|
|
|
p++;
|
|
|
|
beg = p;
|
|
|
|
|
2020-07-23 15:38:40 +02:00
|
|
|
fp = fpnum_parse(strtod_str_get, strtod_str_unget, &p, locinfo, FALSE);
|
2019-12-31 19:19:07 +01:00
|
|
|
if (end) *end = (p == beg ? (char*)str : (char*)p);
|
2020-07-23 15:38:28 +02:00
|
|
|
|
|
|
|
err = fpnum_double(&fp, &ret);
|
|
|
|
if (perr) *perr = err;
|
2020-12-01 13:51:23 +01:00
|
|
|
else if(err) *_errno() = err;
|
2019-12-31 19:19:07 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-17 15:18:54 +01:00
|
|
|
/*********************************************************************
|
2020-12-02 00:54:09 +01:00
|
|
|
* _strtod_l (MSVCRT.@)
|
2012-12-17 15:18:54 +01:00
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
double CDECL _strtod_l(const char *str, char **end, _locale_t locale)
|
2012-12-17 15:18:54 +01:00
|
|
|
{
|
|
|
|
return strtod_helper(str, end, locale, NULL);
|
|
|
|
}
|
|
|
|
|
2006-01-14 17:40:29 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strtod (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
double CDECL strtod( const char *str, char **end )
|
2006-01-14 17:40:29 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtod_l( str, end, NULL );
|
2006-01-14 17:40:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 18:46:54 +01:00
|
|
|
#if _MSVCR_VER>=120
|
|
|
|
|
2016-02-18 16:10:00 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strtof_l (MSVCR120.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
float CDECL _strtof_l( const char *str, char **end, _locale_t locale )
|
2016-02-18 16:10:00 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtod_l(str, end, locale);
|
2016-02-18 16:10:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* strtof (MSVCR120.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
float CDECL strtof( const char *str, char **end )
|
2016-02-18 16:10:00 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtof_l(str, end, NULL);
|
2016-02-18 16:10:00 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 18:46:54 +01:00
|
|
|
#endif /* _MSVCR_VER>=120 */
|
|
|
|
|
2010-04-26 12:33:49 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* atof (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
double CDECL atof( const char *str )
|
2010-04-26 12:33:49 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtod_l(str, NULL, NULL);
|
2010-04-26 12:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _atof_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
double CDECL _atof_l( const char *str, _locale_t locale)
|
2010-04-26 12:33:49 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtod_l(str, NULL, locale);
|
2010-04-26 12:33:49 +02:00
|
|
|
}
|
|
|
|
|
2011-01-18 17:06:07 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _atoflt_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _atoflt_l(_CRT_FLOAT *value, char *str, _locale_t locale)
|
2011-01-18 17:06:07 +01:00
|
|
|
{
|
2012-12-17 15:18:54 +01:00
|
|
|
double d;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
d = strtod_helper(str, NULL, locale, &err);
|
|
|
|
value->f = d;
|
|
|
|
if(isinf(value->f))
|
2020-11-30 14:37:36 +01:00
|
|
|
return _OVERFLOW;
|
|
|
|
if((d!=0 || err) && value->f>-FLT_MIN && value->f<FLT_MIN)
|
|
|
|
return _UNDERFLOW;
|
2012-12-17 15:18:54 +01:00
|
|
|
return 0;
|
2011-01-18 17:06:07 +01:00
|
|
|
}
|
|
|
|
|
2014-04-16 16:15:21 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _atoflt (MSVCR100.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _atoflt(_CRT_FLOAT *value, char *str)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _atoflt_l(value, str, NULL);
|
2014-04-16 16:15:21 +02:00
|
|
|
}
|
|
|
|
|
2012-12-17 15:19:07 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _atodbl_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _atodbl_l(_CRT_DOUBLE *value, char *str, _locale_t locale)
|
2012-12-17 15:19:07 +01:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
value->x = strtod_helper(str, NULL, locale, &err);
|
|
|
|
if(isinf(value->x))
|
2020-11-30 14:37:36 +01:00
|
|
|
return _OVERFLOW;
|
|
|
|
if((value->x!=0 || err) && value->x>-DBL_MIN && value->x<DBL_MIN)
|
|
|
|
return _UNDERFLOW;
|
2012-12-17 15:19:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _atodbl (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _atodbl(_CRT_DOUBLE *value, char *str)
|
2012-12-17 15:19:07 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _atodbl_l(value, str, NULL);
|
2012-12-17 15:19:07 +01:00
|
|
|
}
|
|
|
|
|
2011-05-12 11:39:10 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _strcoll_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strcoll_l( const char* str1, const char* str2, _locale_t locale )
|
2011-05-12 11:39:10 +02:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2011-05-24 17:22:32 +02:00
|
|
|
|
2011-05-12 11:39:10 +02:00
|
|
|
if(!locale)
|
2011-05-24 17:22:32 +02:00
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
2011-05-12 11:39:10 +02:00
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
if(!locinfo->lc_handle[LC_COLLATE])
|
2020-11-19 13:28:52 +01:00
|
|
|
return strcmp(str1, str2);
|
2020-11-30 14:37:48 +01:00
|
|
|
return CompareStringA(locinfo->lc_handle[LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
|
2011-05-12 11:39:10 +02:00
|
|
|
}
|
|
|
|
|
2006-01-14 17:40:29 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strcoll (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL strcoll( const char* str1, const char* str2 )
|
2006-01-14 17:40:29 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strcoll_l(str1, str2, NULL);
|
2006-01-14 17:40:29 +01:00
|
|
|
}
|
|
|
|
|
2011-05-12 11:39:22 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _stricoll_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _stricoll_l( const char* str1, const char* str2, _locale_t locale )
|
2011-05-12 11:39:22 +02:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2011-05-24 17:22:32 +02:00
|
|
|
|
2011-05-12 11:39:22 +02:00
|
|
|
if(!locale)
|
2011-05-24 17:22:32 +02:00
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
2011-05-12 11:39:22 +02:00
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
if(!locinfo->lc_handle[LC_COLLATE])
|
2020-12-02 00:54:09 +01:00
|
|
|
return _stricmp(str1, str2);
|
2020-11-30 14:37:48 +01:00
|
|
|
return CompareStringA(locinfo->lc_handle[LC_COLLATE], NORM_IGNORECASE,
|
2012-06-20 23:40:33 +02:00
|
|
|
str1, -1, str2, -1)-CSTR_EQUAL;
|
2011-05-12 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _stricoll (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _stricoll( const char* str1, const char* str2 )
|
2011-05-12 11:39:22 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _stricoll_l(str1, str2, NULL);
|
2011-05-12 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
2011-05-12 11:39:31 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _strncoll_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strncoll_l( const char* str1, const char* str2, size_t count, _locale_t locale )
|
2011-05-12 11:39:31 +02:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2011-05-24 17:22:32 +02:00
|
|
|
|
2011-05-12 11:39:31 +02:00
|
|
|
if(!locale)
|
2011-05-24 17:22:32 +02:00
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
2011-05-12 11:39:31 +02:00
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
if(!locinfo->lc_handle[LC_COLLATE])
|
2020-12-02 00:54:09 +01:00
|
|
|
return strncmp(str1, str2, count);
|
2020-11-30 14:37:48 +01:00
|
|
|
return CompareStringA(locinfo->lc_handle[LC_COLLATE], 0,
|
2020-12-02 00:54:09 +01:00
|
|
|
str1, strnlen(str1, count),
|
|
|
|
str2, strnlen(str2, count))-CSTR_EQUAL;
|
2011-05-12 11:39:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2013-04-29 06:45:16 +02:00
|
|
|
* _strncoll (MSVCRT.@)
|
2011-05-12 11:39:31 +02:00
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strncoll( const char* str1, const char* str2, size_t count )
|
2011-05-12 11:39:31 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strncoll_l(str1, str2, count, NULL);
|
2011-05-12 11:39:31 +02:00
|
|
|
}
|
|
|
|
|
2011-05-12 11:39:43 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _strnicoll_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strnicoll_l( const char* str1, const char* str2, size_t count, _locale_t locale )
|
2011-05-12 11:39:43 +02:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2011-05-24 17:22:32 +02:00
|
|
|
|
2011-05-12 11:39:43 +02:00
|
|
|
if(!locale)
|
2011-05-24 17:22:32 +02:00
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
2011-05-12 11:39:43 +02:00
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
if(!locinfo->lc_handle[LC_COLLATE])
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strnicmp(str1, str2, count);
|
2020-11-30 14:37:48 +01:00
|
|
|
return CompareStringA(locinfo->lc_handle[LC_COLLATE], NORM_IGNORECASE,
|
2020-12-02 00:54:09 +01:00
|
|
|
str1, strnlen(str1, count),
|
|
|
|
str2, strnlen(str2, count))-CSTR_EQUAL;
|
2011-05-12 11:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strnicoll (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _strnicoll( const char* str1, const char* str2, size_t count )
|
2011-05-12 11:39:43 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strnicoll_l(str1, str2, count, NULL);
|
2011-05-12 11:39:43 +02:00
|
|
|
}
|
|
|
|
|
2013-03-27 16:12:34 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strncpy (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* __cdecl strncpy(char *dst, const char *src, size_t len)
|
2013-03-27 16:12:34 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t i;
|
2013-03-27 16:12:34 +01:00
|
|
|
|
|
|
|
for(i=0; i<len; i++)
|
|
|
|
if((dst[i] = src[i]) == '\0') break;
|
|
|
|
|
2013-08-13 21:47:20 +02:00
|
|
|
while (i < len) dst[i++] = 0;
|
|
|
|
|
2013-03-27 16:12:34 +01:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:23:13 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* strcpy (MSVCRT.@)
|
|
|
|
*/
|
2020-11-19 13:28:52 +01:00
|
|
|
char* CDECL strcpy(char *dst, const char *src)
|
2017-08-05 18:23:13 +02:00
|
|
|
{
|
|
|
|
char *ret = dst;
|
|
|
|
while ((*dst++ = *src++));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-11-15 16:11:26 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strcpy_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL strcpy_s( char* dst, size_t elem, const char* src )
|
2007-11-15 16:11:26 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t i;
|
2020-11-30 14:37:27 +01:00
|
|
|
if(!elem) return EINVAL;
|
|
|
|
if(!dst) return EINVAL;
|
2007-11-15 16:11:26 +01:00
|
|
|
if(!src)
|
|
|
|
{
|
|
|
|
dst[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2007-11-15 16:11:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < elem; i++)
|
|
|
|
{
|
|
|
|
if((dst[i] = src[i]) == '\0') return 0;
|
|
|
|
}
|
|
|
|
dst[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2007-11-15 16:11:26 +01:00
|
|
|
}
|
|
|
|
|
2007-11-16 11:36:03 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strcat_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL strcat_s( char* dst, size_t elem, const char* src )
|
2007-11-16 11:36:03 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t i, j;
|
2020-11-30 14:37:27 +01:00
|
|
|
if(!dst) return EINVAL;
|
|
|
|
if(elem == 0) return EINVAL;
|
2007-11-16 11:36:03 +01:00
|
|
|
if(!src)
|
|
|
|
{
|
|
|
|
dst[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2007-11-16 11:36:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < elem; i++)
|
|
|
|
{
|
|
|
|
if(dst[i] == '\0')
|
|
|
|
{
|
|
|
|
for(j = 0; (j + i) < elem; j++)
|
|
|
|
{
|
|
|
|
if((dst[j + i] = src[j]) == '\0') return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Set the first element to 0, not the first element after the skipped part */
|
|
|
|
dst[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2007-11-16 11:36:03 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 11:10:35 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* strcat (MSVCRT.@)
|
|
|
|
*/
|
2020-11-19 13:28:52 +01:00
|
|
|
char* __cdecl strcat( char *dst, const char *src )
|
2020-07-22 11:10:35 +02:00
|
|
|
{
|
|
|
|
char *d = dst;
|
|
|
|
while (*d) d++;
|
|
|
|
while ((*d++ = *src++));
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2010-11-15 01:04:42 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strncat_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL strncat_s( char* dst, size_t elem, const char* src, size_t count )
|
2010-11-15 01:04:42 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t i, j;
|
2012-07-26 11:05:32 +02:00
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(dst != 0)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(elem != 0)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(src != 0))
|
2010-11-15 01:04:42 +01:00
|
|
|
{
|
|
|
|
dst[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-11-15 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < elem; i++)
|
|
|
|
{
|
|
|
|
if(dst[i] == '\0')
|
|
|
|
{
|
|
|
|
for(j = 0; (j + i) < elem; j++)
|
|
|
|
{
|
2020-12-04 15:50:17 +01:00
|
|
|
if(count == _TRUNCATE && j + i == elem - 1)
|
2010-11-15 01:04:42 +01:00
|
|
|
{
|
|
|
|
dst[j + i] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return STRUNCATE;
|
2010-11-15 01:04:42 +01:00
|
|
|
}
|
|
|
|
if(j == count || (dst[j + i] = src[j]) == '\0')
|
|
|
|
{
|
|
|
|
dst[j + i] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Set the first element to 0, not the first element after the skipped part */
|
|
|
|
dst[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2010-11-15 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
2013-09-17 14:45:44 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* strncat (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* __cdecl strncat(char *dst, const char *src, size_t len)
|
2013-09-17 14:45:44 +02:00
|
|
|
{
|
2020-07-22 11:10:42 +02:00
|
|
|
char *d = dst;
|
|
|
|
while (*d) d++;
|
|
|
|
for ( ; len && *src; d++, src++, len--) *d = *src;
|
|
|
|
*d = 0;
|
|
|
|
return dst;
|
2013-09-17 14:45:44 +02:00
|
|
|
}
|
|
|
|
|
2014-06-12 17:52:40 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _strxfrm_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
size_t CDECL _strxfrm_l( char *dest, const char *src,
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t len, _locale_t locale )
|
2014-06-12 17:52:40 +02:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2014-06-12 17:52:40 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
|
|
|
|
if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
|
|
|
|
|
|
|
|
if(len > INT_MAX) {
|
|
|
|
FIXME("len > INT_MAX not supported\n");
|
|
|
|
len = INT_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!locale)
|
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
if(!locinfo->lc_handle[LC_COLLATE]) {
|
2020-12-02 00:54:09 +01:00
|
|
|
strncpy(dest, src, len);
|
2014-06-12 17:52:40 +02:00
|
|
|
return strlen(src);
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
ret = LCMapStringA(locinfo->lc_handle[LC_COLLATE],
|
2014-06-12 17:52:40 +02:00
|
|
|
LCMAP_SORTKEY, src, -1, NULL, 0);
|
|
|
|
if(!ret) {
|
|
|
|
if(len) dest[0] = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EILSEQ;
|
2014-06-12 17:52:40 +02:00
|
|
|
return INT_MAX;
|
|
|
|
}
|
|
|
|
if(!len) return ret-1;
|
|
|
|
|
|
|
|
if(ret > len) {
|
|
|
|
dest[0] = 0;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2014-06-12 17:52:40 +02:00
|
|
|
return ret-1;
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
return LCMapStringA(locinfo->lc_handle[LC_COLLATE],
|
2014-06-12 17:52:40 +02:00
|
|
|
LCMAP_SORTKEY, src, -1, dest, len) - 1;
|
|
|
|
}
|
|
|
|
|
2006-01-14 17:40:29 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strxfrm (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
size_t CDECL strxfrm( char *dest, const char *src, size_t len )
|
2006-01-14 17:40:29 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strxfrm_l(dest, src, len, NULL);
|
2006-01-14 17:40:29 +01:00
|
|
|
}
|
|
|
|
|
2006-12-17 18:56:50 +01:00
|
|
|
/********************************************************************
|
2020-07-23 15:38:21 +02:00
|
|
|
* __STRINGTOLD_L (MSVCR80.@)
|
2006-12-17 18:56:50 +01:00
|
|
|
*/
|
2020-07-23 15:38:21 +02:00
|
|
|
int CDECL __STRINGTOLD_L( MSVCRT__LDOUBLE *value, char **endptr,
|
2020-11-26 19:19:18 +01:00
|
|
|
const char *str, int flags, _locale_t locale )
|
2006-12-17 18:56:50 +01:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2020-07-23 15:38:40 +02:00
|
|
|
const char *beg, *p;
|
|
|
|
int err, ret = 0;
|
|
|
|
struct fpnum fp;
|
|
|
|
|
|
|
|
if (flags) FIXME("flags not supported: %x\n", flags);
|
|
|
|
|
|
|
|
if (!locale)
|
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
|
|
|
|
|
|
|
p = str;
|
2020-12-01 13:52:34 +01:00
|
|
|
while (_isspace_l((unsigned char)*p, locale))
|
2020-07-23 15:38:40 +02:00
|
|
|
p++;
|
|
|
|
beg = p;
|
|
|
|
|
|
|
|
fp = fpnum_parse(strtod_str_get, strtod_str_unget, &p, locinfo, TRUE);
|
|
|
|
if (endptr) *endptr = (p == beg ? (char*)str : (char*)p);
|
|
|
|
if (p == beg) ret = 4;
|
|
|
|
|
|
|
|
err = fpnum_ldouble(&fp, value);
|
|
|
|
if (err) ret = (value->x80[2] & 0x7fff ? 2 : 1);
|
|
|
|
return ret;
|
2006-12-17 18:56:50 +01:00
|
|
|
}
|
2007-09-19 17:52:40 +02:00
|
|
|
|
|
|
|
/********************************************************************
|
2020-07-23 15:38:21 +02:00
|
|
|
* __STRINGTOLD (MSVCRT.@)
|
2007-09-19 17:52:40 +02:00
|
|
|
*/
|
|
|
|
int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
|
|
|
|
{
|
2020-07-23 15:38:21 +02:00
|
|
|
return __STRINGTOLD_L( value, endptr, str, flags, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* _atoldbl_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _atoldbl_l( MSVCRT__LDOUBLE *value, char *str, _locale_t locale )
|
2020-07-23 15:38:21 +02:00
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
switch(__STRINGTOLD_L( value, &endptr, str, 0, locale ))
|
|
|
|
{
|
2020-11-30 14:37:36 +01:00
|
|
|
case 1: return _UNDERFLOW;
|
|
|
|
case 2: return _OVERFLOW;
|
2020-07-23 15:38:21 +02:00
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* _atoldbl (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _atoldbl(_LDOUBLE *value, char *str)
|
2020-07-23 15:38:21 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _atoldbl_l( (MSVCRT__LDOUBLE*)value, str, NULL );
|
2007-09-19 17:52:40 +02:00
|
|
|
}
|
2009-08-31 21:36:45 +02:00
|
|
|
|
2013-09-17 14:45:29 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* strlen (MSVCRT.@)
|
|
|
|
*/
|
2020-11-19 13:28:52 +01:00
|
|
|
size_t __cdecl strlen(const char *str)
|
2013-09-17 14:45:29 +02:00
|
|
|
{
|
2020-07-22 11:10:40 +02:00
|
|
|
const char *s = str;
|
|
|
|
while (*s) s++;
|
|
|
|
return s - str;
|
2013-09-17 14:45:29 +02:00
|
|
|
}
|
|
|
|
|
2010-03-24 22:39:28 +01:00
|
|
|
/******************************************************************
|
|
|
|
* strnlen (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
size_t CDECL strnlen(const char *s, size_t maxlen)
|
2010-03-24 22:39:28 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t i;
|
2010-03-24 22:39:28 +01:00
|
|
|
|
|
|
|
for(i=0; i<maxlen; i++)
|
|
|
|
if(!s[i]) break;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
2010-03-29 00:04:03 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
2010-04-28 17:44:51 +02:00
|
|
|
* _strtoi64_l (MSVCRT.@)
|
2010-03-29 00:04:03 +02:00
|
|
|
*
|
|
|
|
* FIXME: locale parameter is ignored
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 CDECL _strtoi64_l(const char *nptr, char **endptr, int base, _locale_t locale)
|
2010-03-29 00:04:03 +02:00
|
|
|
{
|
2015-06-08 12:00:55 +02:00
|
|
|
const char *p = nptr;
|
2010-03-29 00:04:03 +02:00
|
|
|
BOOL negative = FALSE;
|
2015-06-08 12:00:55 +02:00
|
|
|
BOOL got_digit = FALSE;
|
2010-03-29 00:04:03 +02:00
|
|
|
__int64 ret = 0;
|
|
|
|
|
2011-09-19 23:29:47 +02:00
|
|
|
TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
|
2010-03-29 00:04:03 +02:00
|
|
|
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
|
|
|
|
if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
|
|
|
|
if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
|
2010-03-29 00:04:03 +02:00
|
|
|
|
2020-12-01 13:52:34 +01:00
|
|
|
while(_isspace_l((unsigned char)*nptr, locale)) nptr++;
|
2010-03-29 00:04:03 +02:00
|
|
|
|
|
|
|
if(*nptr == '-') {
|
|
|
|
negative = TRUE;
|
|
|
|
nptr++;
|
|
|
|
} else if(*nptr == '+')
|
|
|
|
nptr++;
|
|
|
|
|
2020-12-01 13:52:34 +01:00
|
|
|
if((base==0 || base==16) && *nptr=='0' && _tolower_l(*(nptr+1), locale)=='x') {
|
2010-03-29 00:04:03 +02:00
|
|
|
base = 16;
|
|
|
|
nptr += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(base == 0) {
|
|
|
|
if(*nptr=='0')
|
|
|
|
base = 8;
|
|
|
|
else
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(*nptr) {
|
2020-12-01 13:52:34 +01:00
|
|
|
char cur = _tolower_l(*nptr, locale);
|
2010-03-29 00:04:03 +02:00
|
|
|
int v;
|
|
|
|
|
2016-02-18 16:09:55 +01:00
|
|
|
if(cur>='0' && cur<='9') {
|
2010-03-29 00:04:03 +02:00
|
|
|
if(cur >= '0'+base)
|
|
|
|
break;
|
|
|
|
v = cur-'0';
|
|
|
|
} else {
|
|
|
|
if(cur<'a' || cur>='a'+base-10)
|
|
|
|
break;
|
|
|
|
v = cur-'a'+10;
|
|
|
|
}
|
2015-06-08 12:00:55 +02:00
|
|
|
got_digit = TRUE;
|
2010-03-29 00:04:03 +02:00
|
|
|
|
|
|
|
if(negative)
|
|
|
|
v = -v;
|
|
|
|
|
|
|
|
nptr++;
|
|
|
|
|
2020-11-27 14:17:01 +01:00
|
|
|
if(!negative && (ret>I64_MAX/base || ret*base>I64_MAX-v)) {
|
|
|
|
ret = I64_MAX;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-27 14:17:01 +01:00
|
|
|
} else if(negative && (ret<I64_MIN/base || ret*base<I64_MIN-v)) {
|
|
|
|
ret = I64_MIN;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2010-03-29 00:04:03 +02:00
|
|
|
} else
|
|
|
|
ret = ret*base + v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(endptr)
|
2015-06-08 12:00:55 +02:00
|
|
|
*endptr = (char*)(got_digit ? nptr : p);
|
2010-03-29 00:04:03 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2010-04-28 17:44:51 +02:00
|
|
|
* _strtoi64 (MSVCRT.@)
|
2010-03-29 00:04:03 +02:00
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 CDECL _strtoi64(const char *nptr, char **endptr, int base)
|
2010-03-29 00:04:03 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtoi64_l(nptr, endptr, base, NULL);
|
2010-03-29 00:04:03 +02:00
|
|
|
}
|
2010-03-29 00:04:32 +02:00
|
|
|
|
2012-12-12 11:16:19 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _atoi_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int __cdecl _atoi_l(const char *str, _locale_t locale)
|
2012-12-12 11:16:19 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 ret = _strtoi64_l(str, NULL, 10, locale);
|
2012-12-12 11:16:19 +01:00
|
|
|
|
|
|
|
if(ret > INT_MAX) {
|
|
|
|
ret = INT_MAX;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2012-12-12 11:16:19 +01:00
|
|
|
} else if(ret < INT_MIN) {
|
|
|
|
ret = INT_MIN;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2012-12-12 11:16:19 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-27 16:11:49 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* atoi (MSVCRT.@)
|
|
|
|
*/
|
2014-04-21 13:08:20 +02:00
|
|
|
#if _MSVCR_VER == 0
|
2020-12-02 00:54:09 +01:00
|
|
|
int __cdecl atoi(const char *str)
|
2013-03-27 16:11:49 +01:00
|
|
|
{
|
2013-06-04 15:52:10 +02:00
|
|
|
BOOL minus = FALSE;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if(!str)
|
|
|
|
return 0;
|
|
|
|
|
2020-12-01 13:52:34 +01:00
|
|
|
while(_isspace_l((unsigned char)*str, NULL)) str++;
|
2013-06-04 15:52:10 +02:00
|
|
|
|
|
|
|
if(*str == '+') {
|
|
|
|
str++;
|
|
|
|
}else if(*str == '-') {
|
|
|
|
minus = TRUE;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(*str>='0' && *str<='9') {
|
|
|
|
ret = ret*10+*str-'0';
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return minus ? -ret : ret;
|
2013-03-27 16:11:49 +01:00
|
|
|
}
|
2014-04-21 13:08:20 +02:00
|
|
|
#else
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL atoi(const char *str)
|
2014-04-16 16:15:21 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _atoi_l(str, NULL);
|
2014-04-16 16:15:21 +02:00
|
|
|
}
|
2014-04-21 13:08:20 +02:00
|
|
|
#endif
|
2014-04-16 16:15:21 +02:00
|
|
|
|
2018-03-26 21:20:48 +02:00
|
|
|
/******************************************************************
|
|
|
|
* _atoi64_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 CDECL _atoi64_l(const char *str, _locale_t locale)
|
2018-03-26 21:20:48 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtoi64_l(str, NULL, 10, locale);
|
2018-03-26 21:20:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 15:22:51 +02:00
|
|
|
/******************************************************************
|
|
|
|
* _atoi64 (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 CDECL _atoi64(const char *str)
|
2018-10-17 15:22:51 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtoi64_l(str, NULL, 10, NULL);
|
2018-10-17 15:22:51 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 21:20:22 +02:00
|
|
|
/******************************************************************
|
|
|
|
* _atol_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__msvcrt_long CDECL _atol_l(const char *str, _locale_t locale)
|
2018-03-26 21:20:22 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 ret = _strtoi64_l(str, NULL, 10, locale);
|
2018-03-26 21:20:22 +02:00
|
|
|
|
2020-11-27 14:17:01 +01:00
|
|
|
if(ret > LONG_MAX) {
|
|
|
|
ret = LONG_MAX;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-27 14:17:01 +01:00
|
|
|
} else if(ret < LONG_MIN) {
|
|
|
|
ret = LONG_MIN;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2018-03-26 21:20:22 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:23:00 +02:00
|
|
|
/******************************************************************
|
|
|
|
* atol (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__msvcrt_long CDECL atol(const char *str)
|
2018-10-17 15:23:00 +02:00
|
|
|
{
|
2019-03-16 14:49:08 +01:00
|
|
|
#if _MSVCR_VER == 0
|
2020-12-02 00:54:09 +01:00
|
|
|
return atoi(str);
|
2019-03-16 14:49:08 +01:00
|
|
|
#else
|
2020-12-02 00:54:09 +01:00
|
|
|
return _atol_l(str, NULL);
|
2019-03-16 14:49:08 +01:00
|
|
|
#endif
|
2018-10-17 15:23:00 +02:00
|
|
|
}
|
|
|
|
|
2017-12-28 18:46:54 +01:00
|
|
|
#if _MSVCR_VER>=120
|
|
|
|
|
2016-02-09 14:29:07 +01:00
|
|
|
/******************************************************************
|
|
|
|
* _atoll_l (MSVCR120.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 CDECL _atoll_l(const char* str, _locale_t locale)
|
2016-02-09 14:29:07 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtoi64_l(str, NULL, 10, locale);
|
2016-02-09 14:29:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* atoll (MSVCR120.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 CDECL atoll(const char* str)
|
2016-02-09 14:29:07 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _atoll_l(str, NULL);
|
2016-02-09 14:29:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:54:09 +01:00
|
|
|
#endif /* _MSVCR_VER>=120 */
|
2017-12-28 18:46:54 +01:00
|
|
|
|
2013-09-25 14:41:40 +02:00
|
|
|
/******************************************************************
|
2015-12-21 11:22:45 +01:00
|
|
|
* _strtol_l (MSVCRT.@)
|
2013-09-25 14:41:40 +02:00
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__msvcrt_long CDECL _strtol_l(const char* nptr,
|
2020-11-26 19:19:18 +01:00
|
|
|
char** end, int base, _locale_t locale)
|
2013-09-25 14:41:40 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 ret = _strtoi64_l(nptr, end, base, locale);
|
2013-09-25 14:41:40 +02:00
|
|
|
|
2020-11-27 14:17:01 +01:00
|
|
|
if(ret > LONG_MAX) {
|
|
|
|
ret = LONG_MAX;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-27 14:17:01 +01:00
|
|
|
} else if(ret < LONG_MIN) {
|
|
|
|
ret = LONG_MIN;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2013-09-25 14:41:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-21 11:22:45 +01:00
|
|
|
/******************************************************************
|
|
|
|
* strtol (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__msvcrt_long CDECL strtol(const char* nptr, char** end, int base)
|
2015-12-21 11:22:45 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtol_l(nptr, end, base, NULL);
|
2015-12-21 11:22:45 +01:00
|
|
|
}
|
|
|
|
|
2013-09-25 14:41:52 +02:00
|
|
|
/******************************************************************
|
2014-09-02 17:42:33 +02:00
|
|
|
* _strtoul_l (MSVCRT.@)
|
2013-09-25 14:41:52 +02:00
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__msvcrt_ulong CDECL _strtoul_l(const char* nptr, char** end, int base, _locale_t locale)
|
2013-09-25 14:41:52 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
__int64 ret = _strtoi64_l(nptr, end, base, locale);
|
2013-09-25 14:41:52 +02:00
|
|
|
|
2020-11-27 14:17:01 +01:00
|
|
|
if(ret > ULONG_MAX) {
|
|
|
|
ret = ULONG_MAX;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-27 14:17:01 +01:00
|
|
|
}else if(ret < -(__int64)ULONG_MAX) {
|
2013-09-25 14:41:52 +02:00
|
|
|
ret = 1;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2013-09-25 14:41:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-02 17:42:33 +02:00
|
|
|
/******************************************************************
|
|
|
|
* strtoul (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
__msvcrt_ulong CDECL strtoul(const char* nptr, char** end, int base)
|
2014-09-02 17:42:33 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtoul_l(nptr, end, base, NULL);
|
2014-09-02 17:42:33 +02:00
|
|
|
}
|
|
|
|
|
2010-03-29 00:04:32 +02:00
|
|
|
/*********************************************************************
|
2010-04-28 17:44:51 +02:00
|
|
|
* _strtoui64_l (MSVCRT.@)
|
2010-03-29 00:04:32 +02:00
|
|
|
*
|
|
|
|
* FIXME: locale parameter is ignored
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
unsigned __int64 CDECL _strtoui64_l(const char *nptr, char **endptr, int base, _locale_t locale)
|
2010-03-29 00:04:32 +02:00
|
|
|
{
|
2015-06-08 12:00:55 +02:00
|
|
|
const char *p = nptr;
|
2010-03-29 00:04:32 +02:00
|
|
|
BOOL negative = FALSE;
|
2015-06-08 12:00:55 +02:00
|
|
|
BOOL got_digit = FALSE;
|
2010-03-29 00:04:32 +02:00
|
|
|
unsigned __int64 ret = 0;
|
|
|
|
|
2011-09-19 23:29:47 +02:00
|
|
|
TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
|
2010-03-29 00:04:32 +02:00
|
|
|
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
|
|
|
|
if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
|
|
|
|
if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
|
2010-03-29 00:04:32 +02:00
|
|
|
|
2020-12-01 13:52:34 +01:00
|
|
|
while(_isspace_l((unsigned char)*nptr, locale)) nptr++;
|
2010-03-29 00:04:32 +02:00
|
|
|
|
|
|
|
if(*nptr == '-') {
|
|
|
|
negative = TRUE;
|
|
|
|
nptr++;
|
|
|
|
} else if(*nptr == '+')
|
|
|
|
nptr++;
|
|
|
|
|
2020-12-01 13:52:34 +01:00
|
|
|
if((base==0 || base==16) && *nptr=='0' && _tolower_l(*(nptr+1), locale)=='x') {
|
2010-03-29 00:04:32 +02:00
|
|
|
base = 16;
|
|
|
|
nptr += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(base == 0) {
|
|
|
|
if(*nptr=='0')
|
|
|
|
base = 8;
|
|
|
|
else
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(*nptr) {
|
2020-12-01 13:52:34 +01:00
|
|
|
char cur = _tolower_l(*nptr, locale);
|
2010-03-29 00:04:32 +02:00
|
|
|
int v;
|
|
|
|
|
2016-02-18 16:09:55 +01:00
|
|
|
if(cur>='0' && cur<='9') {
|
2010-03-29 00:04:32 +02:00
|
|
|
if(cur >= '0'+base)
|
|
|
|
break;
|
|
|
|
v = *nptr-'0';
|
|
|
|
} else {
|
|
|
|
if(cur<'a' || cur>='a'+base-10)
|
|
|
|
break;
|
|
|
|
v = cur-'a'+10;
|
|
|
|
}
|
2015-06-08 12:00:55 +02:00
|
|
|
got_digit = TRUE;
|
2010-03-29 00:04:32 +02:00
|
|
|
|
|
|
|
nptr++;
|
|
|
|
|
2020-11-27 14:17:01 +01:00
|
|
|
if(ret>UI64_MAX/base || ret*base>UI64_MAX-v) {
|
|
|
|
ret = UI64_MAX;
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2010-03-29 00:04:32 +02:00
|
|
|
} else
|
|
|
|
ret = ret*base + v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(endptr)
|
2015-06-08 12:00:55 +02:00
|
|
|
*endptr = (char*)(got_digit ? nptr : p);
|
2010-03-29 00:04:32 +02:00
|
|
|
|
|
|
|
return negative ? -ret : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2010-04-28 17:44:51 +02:00
|
|
|
* _strtoui64 (MSVCRT.@)
|
2010-03-29 00:04:32 +02:00
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
unsigned __int64 CDECL _strtoui64(const char *nptr, char **endptr, int base)
|
2010-03-29 00:04:32 +02:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strtoui64_l(nptr, endptr, base, NULL);
|
2010-03-29 00:04:32 +02:00
|
|
|
}
|
2010-05-05 13:37:33 +02:00
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
static int ltoa_helper(__msvcrt_long value, char *str, size_t size, int radix)
|
2010-10-11 12:25:15 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
__msvcrt_ulong val;
|
2010-11-15 21:59:55 +01:00
|
|
|
unsigned int digit;
|
2013-09-27 08:43:20 +02:00
|
|
|
BOOL is_negative;
|
2010-10-11 12:25:15 +02:00
|
|
|
char buffer[33], *pos;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (value < 0 && radix == 10)
|
|
|
|
{
|
2013-09-27 08:43:20 +02:00
|
|
|
is_negative = TRUE;
|
2010-10-11 12:25:15 +02:00
|
|
|
val = -value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-27 08:43:20 +02:00
|
|
|
is_negative = FALSE;
|
2010-10-11 12:25:15 +02:00
|
|
|
val = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = buffer + 32;
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
digit = val % radix;
|
|
|
|
val /= radix;
|
|
|
|
|
|
|
|
if (digit < 10)
|
|
|
|
*--pos = '0' + digit;
|
|
|
|
else
|
|
|
|
*--pos = 'a' + digit - 10;
|
|
|
|
}
|
|
|
|
while (val != 0);
|
|
|
|
|
|
|
|
if (is_negative)
|
|
|
|
*--pos = '-';
|
|
|
|
|
|
|
|
len = buffer + 33 - pos;
|
|
|
|
if (len > size)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
char *p = str;
|
|
|
|
|
|
|
|
/* Copy the temporary buffer backwards up to the available number of
|
|
|
|
* characters. Don't copy the negative sign if present. */
|
|
|
|
|
|
|
|
if (is_negative)
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pos = buffer + 31, i = 0; i < size; i++)
|
|
|
|
*p++ = *pos--;
|
|
|
|
|
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
|
|
|
|
return ERANGE;
|
2010-10-11 12:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(str, pos, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-15 16:29:00 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _ltoa_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _ltoa_s(__msvcrt_long value, char *str, size_t size, int radix)
|
2013-10-15 16:29:00 +02:00
|
|
|
{
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
|
2013-10-15 16:29:00 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
|
|
|
|
{
|
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2013-10-15 16:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ltoa_helper(value, str, size, radix);
|
|
|
|
}
|
|
|
|
|
2010-11-02 22:02:52 +01:00
|
|
|
/*********************************************************************
|
2010-11-15 21:59:55 +01:00
|
|
|
* _ltow_s (MSVCRT.@)
|
2010-11-02 22:02:52 +01:00
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _ltow_s(__msvcrt_long value, wchar_t *str, size_t size, int radix)
|
2010-11-02 22:02:52 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
__msvcrt_ulong val;
|
2010-11-15 21:59:55 +01:00
|
|
|
unsigned int digit;
|
2013-09-27 08:43:20 +02:00
|
|
|
BOOL is_negative;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t buffer[33], *pos;
|
2010-11-02 22:02:52 +01:00
|
|
|
size_t len;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
|
2010-11-02 22:02:52 +01:00
|
|
|
{
|
2012-07-26 11:05:32 +02:00
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-11-02 22:02:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0 && radix == 10)
|
|
|
|
{
|
2013-09-27 08:43:20 +02:00
|
|
|
is_negative = TRUE;
|
2010-11-02 22:02:52 +01:00
|
|
|
val = -value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-27 08:43:20 +02:00
|
|
|
is_negative = FALSE;
|
2010-11-02 22:02:52 +01:00
|
|
|
val = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = buffer + 32;
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
digit = val % radix;
|
|
|
|
val /= radix;
|
|
|
|
|
|
|
|
if (digit < 10)
|
|
|
|
*--pos = '0' + digit;
|
|
|
|
else
|
|
|
|
*--pos = 'a' + digit - 10;
|
|
|
|
}
|
|
|
|
while (val != 0);
|
|
|
|
|
|
|
|
if (is_negative)
|
|
|
|
*--pos = '-';
|
|
|
|
|
|
|
|
len = buffer + 33 - pos;
|
|
|
|
if (len > size)
|
|
|
|
{
|
|
|
|
size_t i;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t *p = str;
|
2010-11-02 22:02:52 +01:00
|
|
|
|
|
|
|
/* Copy the temporary buffer backwards up to the available number of
|
|
|
|
* characters. Don't copy the negative sign if present. */
|
|
|
|
|
|
|
|
if (is_negative)
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pos = buffer + 31, i = 0; i < size; i++)
|
|
|
|
*p++ = *pos--;
|
|
|
|
|
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
|
|
|
|
return ERANGE;
|
2010-11-02 22:02:52 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
memcpy(str, pos, len * sizeof(wchar_t));
|
2010-11-02 22:02:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-15 21:59:55 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _itoa_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _itoa_s(int value, char *str, size_t size, int radix)
|
2010-11-15 21:59:55 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _ltoa_s(value, str, size, radix);
|
2010-11-15 21:59:55 +01:00
|
|
|
}
|
|
|
|
|
2013-10-01 11:28:09 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _itoa (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* CDECL _itoa(int value, char *str, int radix)
|
2013-10-01 11:28:09 +02:00
|
|
|
{
|
2020-11-27 14:17:01 +01:00
|
|
|
return ltoa_helper(value, str, SIZE_MAX, radix) ? NULL : str;
|
2013-10-01 11:28:09 +02:00
|
|
|
}
|
|
|
|
|
2010-11-15 21:59:55 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _itow_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _itow_s(int value, wchar_t *str, size_t size, int radix)
|
2010-11-15 21:59:55 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _ltow_s(value, str, size, radix);
|
2010-11-15 21:59:55 +01:00
|
|
|
}
|
|
|
|
|
2010-05-05 13:37:33 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _ui64toa_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _ui64toa_s(unsigned __int64 value, char *str,
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t size, int radix)
|
2010-05-05 13:37:33 +02:00
|
|
|
{
|
|
|
|
char buffer[65], *pos;
|
|
|
|
int digit;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
|
|
|
|
{
|
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-05-05 13:37:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = buffer+64;
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
do {
|
|
|
|
digit = value%radix;
|
|
|
|
value /= radix;
|
|
|
|
|
|
|
|
if(digit < 10)
|
|
|
|
*--pos = '0'+digit;
|
|
|
|
else
|
|
|
|
*--pos = 'a'+digit-10;
|
|
|
|
}while(value != 0);
|
|
|
|
|
|
|
|
if(buffer-pos+65 > size) {
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT("str[size] is too small", EINVAL);
|
|
|
|
return EINVAL;
|
2010-05-05 13:37:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(str, pos, buffer-pos+65);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-14 12:14:12 +02:00
|
|
|
|
2010-12-29 08:13:53 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _ui64tow_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _ui64tow_s( unsigned __int64 value, wchar_t *str,
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t size, int radix )
|
2010-12-29 08:13:53 +01:00
|
|
|
{
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t buffer[65], *pos;
|
2010-12-29 08:13:53 +01:00
|
|
|
int digit;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
|
|
|
|
{
|
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-12-29 08:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = &buffer[64];
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
do {
|
|
|
|
digit = value % radix;
|
|
|
|
value = value / radix;
|
|
|
|
if (digit < 10)
|
|
|
|
*--pos = '0' + digit;
|
|
|
|
else
|
|
|
|
*--pos = 'a' + digit - 10;
|
|
|
|
} while (value != 0);
|
|
|
|
|
|
|
|
if(buffer-pos+65 > size) {
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT("str[size] is too small", EINVAL);
|
|
|
|
return EINVAL;
|
2010-12-29 08:13:53 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
memcpy(str, pos, (buffer-pos+65)*sizeof(wchar_t));
|
2010-12-29 08:13:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-21 09:57:44 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* _ultoa_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _ultoa_s(__msvcrt_ulong value, char *str, size_t size, int radix)
|
2010-10-21 09:57:44 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
__msvcrt_ulong digit;
|
2010-10-21 09:57:44 +02:00
|
|
|
char buffer[33], *pos;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!str || !size || radix < 2 || radix > 36)
|
|
|
|
{
|
|
|
|
if (str && size)
|
|
|
|
str[0] = '\0';
|
|
|
|
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-10-21 09:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = buffer + 32;
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
digit = value % radix;
|
|
|
|
value /= radix;
|
|
|
|
|
|
|
|
if (digit < 10)
|
|
|
|
*--pos = '0' + digit;
|
|
|
|
else
|
|
|
|
*--pos = 'a' + digit - 10;
|
|
|
|
}
|
|
|
|
while (value != 0);
|
|
|
|
|
|
|
|
len = buffer + 33 - pos;
|
|
|
|
if (len > size)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
char *p = str;
|
|
|
|
|
|
|
|
/* Copy the temporary buffer backwards up to the available number of
|
|
|
|
* characters. */
|
|
|
|
|
|
|
|
for (pos = buffer + 31, i = 0; i < size; i++)
|
|
|
|
*p++ = *pos--;
|
|
|
|
|
|
|
|
str[0] = '\0';
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2010-10-21 09:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(str, pos, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-28 21:53:38 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _ultow_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _ultow_s(__msvcrt_ulong value, wchar_t *str, size_t size, int radix)
|
2011-11-28 21:53:38 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
__msvcrt_ulong digit;
|
2011-11-28 21:53:38 +01:00
|
|
|
WCHAR buffer[33], *pos;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!str || !size || radix < 2 || radix > 36)
|
|
|
|
{
|
|
|
|
if (str && size)
|
|
|
|
str[0] = '\0';
|
|
|
|
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = EINVAL;
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2011-11-28 21:53:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = buffer + 32;
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
digit = value % radix;
|
|
|
|
value /= radix;
|
|
|
|
|
|
|
|
if (digit < 10)
|
|
|
|
*--pos = '0' + digit;
|
|
|
|
else
|
|
|
|
*--pos = 'a' + digit - 10;
|
|
|
|
}
|
|
|
|
while (value != 0);
|
|
|
|
|
|
|
|
len = buffer + 33 - pos;
|
|
|
|
if (len > size)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
WCHAR *p = str;
|
|
|
|
|
|
|
|
/* Copy the temporary buffer backwards up to the available number of
|
|
|
|
* characters. */
|
|
|
|
|
|
|
|
for (pos = buffer + 31, i = 0; i < size; i++)
|
|
|
|
*p++ = *pos--;
|
|
|
|
|
|
|
|
str[0] = '\0';
|
2020-12-01 13:51:23 +01:00
|
|
|
*_errno() = ERANGE;
|
2020-11-30 14:37:27 +01:00
|
|
|
return ERANGE;
|
2011-11-28 21:53:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
memcpy(str, pos, len * sizeof(wchar_t));
|
2011-11-28 21:53:38 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-15 22:00:01 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _i64toa_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _i64toa_s(__int64 value, char *str, size_t size, int radix)
|
2010-11-15 22:00:01 +01:00
|
|
|
{
|
|
|
|
unsigned __int64 val;
|
|
|
|
unsigned int digit;
|
2013-09-27 08:43:20 +02:00
|
|
|
BOOL is_negative;
|
2010-11-15 22:00:01 +01:00
|
|
|
char buffer[65], *pos;
|
|
|
|
size_t len;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
|
2010-11-15 22:00:01 +01:00
|
|
|
{
|
2012-07-26 11:05:32 +02:00
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-11-15 22:00:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0 && radix == 10)
|
|
|
|
{
|
2013-09-27 08:43:20 +02:00
|
|
|
is_negative = TRUE;
|
2010-11-15 22:00:01 +01:00
|
|
|
val = -value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-27 08:43:20 +02:00
|
|
|
is_negative = FALSE;
|
2010-11-15 22:00:01 +01:00
|
|
|
val = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = buffer + 64;
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
digit = val % radix;
|
|
|
|
val /= radix;
|
|
|
|
|
|
|
|
if (digit < 10)
|
|
|
|
*--pos = '0' + digit;
|
|
|
|
else
|
|
|
|
*--pos = 'a' + digit - 10;
|
|
|
|
}
|
|
|
|
while (val != 0);
|
|
|
|
|
|
|
|
if (is_negative)
|
|
|
|
*--pos = '-';
|
|
|
|
|
|
|
|
len = buffer + 65 - pos;
|
|
|
|
if (len > size)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
char *p = str;
|
|
|
|
|
|
|
|
/* Copy the temporary buffer backwards up to the available number of
|
|
|
|
* characters. Don't copy the negative sign if present. */
|
|
|
|
|
|
|
|
if (is_negative)
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pos = buffer + 63, i = 0; i < size; i++)
|
|
|
|
*p++ = *pos--;
|
|
|
|
|
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
|
|
|
|
return ERANGE;
|
2010-11-15 22:00:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(str, pos, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _i64tow_s (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL _i64tow_s(__int64 value, wchar_t *str, size_t size, int radix)
|
2010-11-15 22:00:01 +01:00
|
|
|
{
|
|
|
|
unsigned __int64 val;
|
|
|
|
unsigned int digit;
|
2013-09-27 08:43:20 +02:00
|
|
|
BOOL is_negative;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t buffer[65], *pos;
|
2010-11-15 22:00:01 +01:00
|
|
|
size_t len;
|
|
|
|
|
2020-11-30 14:37:27 +01:00
|
|
|
if (!MSVCRT_CHECK_PMT(str != NULL)) return EINVAL;
|
|
|
|
if (!MSVCRT_CHECK_PMT(size > 0)) return EINVAL;
|
2012-07-26 11:05:32 +02:00
|
|
|
if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
|
2010-11-15 22:00:01 +01:00
|
|
|
{
|
2012-07-26 11:05:32 +02:00
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
return EINVAL;
|
2010-11-15 22:00:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0 && radix == 10)
|
|
|
|
{
|
2013-09-27 08:43:20 +02:00
|
|
|
is_negative = TRUE;
|
2010-11-15 22:00:01 +01:00
|
|
|
val = -value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-27 08:43:20 +02:00
|
|
|
is_negative = FALSE;
|
2010-11-15 22:00:01 +01:00
|
|
|
val = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = buffer + 64;
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
digit = val % radix;
|
|
|
|
val /= radix;
|
|
|
|
|
|
|
|
if (digit < 10)
|
|
|
|
*--pos = '0' + digit;
|
|
|
|
else
|
|
|
|
*--pos = 'a' + digit - 10;
|
|
|
|
}
|
|
|
|
while (val != 0);
|
|
|
|
|
|
|
|
if (is_negative)
|
|
|
|
*--pos = '-';
|
|
|
|
|
|
|
|
len = buffer + 65 - pos;
|
|
|
|
if (len > size)
|
|
|
|
{
|
|
|
|
size_t i;
|
2020-11-27 14:16:49 +01:00
|
|
|
wchar_t *p = str;
|
2010-11-15 22:00:01 +01:00
|
|
|
|
|
|
|
/* Copy the temporary buffer backwards up to the available number of
|
|
|
|
* characters. Don't copy the negative sign if present. */
|
|
|
|
|
|
|
|
if (is_negative)
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (pos = buffer + 63, i = 0; i < size; i++)
|
|
|
|
*p++ = *pos--;
|
|
|
|
|
|
|
|
str[0] = '\0';
|
2020-11-30 14:37:27 +01:00
|
|
|
MSVCRT_INVALID_PMT("str[size] is too small", ERANGE);
|
|
|
|
return ERANGE;
|
2010-11-15 22:00:01 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 14:16:49 +01:00
|
|
|
memcpy(str, pos, len * sizeof(wchar_t));
|
2010-11-15 22:00:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-14 12:14:12 +02:00
|
|
|
#define I10_OUTPUT_MAX_PREC 21
|
|
|
|
/* Internal structure used by $I10_OUTPUT */
|
|
|
|
struct _I10_OUTPUT_DATA {
|
|
|
|
short pos;
|
|
|
|
char sign;
|
|
|
|
BYTE len;
|
|
|
|
char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* $I10_OUTPUT (MSVCRT.@)
|
2010-12-01 22:26:26 +01:00
|
|
|
* ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
|
2010-05-14 12:14:12 +02:00
|
|
|
* prec - precision of part, we're interested in
|
|
|
|
* flag - 0 for first prec digits, 1 for fractional part
|
|
|
|
* data - data to be populated
|
|
|
|
*
|
|
|
|
* return value
|
|
|
|
* 0 if given double is NaN or INF
|
|
|
|
* 1 otherwise
|
|
|
|
*
|
|
|
|
* FIXME
|
|
|
|
* Native sets last byte of data->str to '0' or '9', I don't know what
|
|
|
|
* it means. Current implementation sets it always to '0'.
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int CDECL I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
|
2010-05-14 12:14:12 +02:00
|
|
|
{
|
2020-11-17 10:13:31 +01:00
|
|
|
struct fpnum num;
|
2010-12-01 22:26:26 +01:00
|
|
|
double d;
|
2010-05-14 12:14:12 +02:00
|
|
|
char format[8];
|
|
|
|
char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
|
|
|
|
char *p;
|
|
|
|
|
2020-11-17 10:13:31 +01:00
|
|
|
if ((ld80.x80[2] & 0x7fff) == 0x7fff)
|
|
|
|
{
|
|
|
|
if (ld80.x80[0] == 0 && ld80.x80[1] == 0x80000000)
|
|
|
|
strcpy( data->str, "1#INF" );
|
|
|
|
else
|
|
|
|
strcpy( data->str, (ld80.x80[1] & 0x40000000) ? "1#QNAN" : "1#SNAN" );
|
|
|
|
data->pos = 1;
|
|
|
|
data->sign = (ld80.x80[2] & 0x8000) ? '-' : ' ';
|
|
|
|
data->len = strlen(data->str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
num.sign = (ld80.x80[2] & 0x8000) ? -1 : 1;
|
|
|
|
num.exp = (ld80.x80[2] & 0x7fff) - 0x3fff - 63;
|
|
|
|
num.m = ld80.x80[0] | ((ULONGLONG)ld80.x80[1] << 32);
|
|
|
|
num.mod = FP_ROUND_EVEN;
|
|
|
|
fpnum_double( &num, &d );
|
2010-05-14 12:14:12 +02:00
|
|
|
TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
|
|
|
|
|
|
|
|
if(d<0) {
|
|
|
|
data->sign = '-';
|
|
|
|
d = -d;
|
|
|
|
} else
|
|
|
|
data->sign = ' ';
|
|
|
|
|
|
|
|
if(flag&1) {
|
2020-12-03 19:10:57 +01:00
|
|
|
int exp = 1 + floor(log10(d));
|
2010-05-14 12:14:12 +02:00
|
|
|
|
|
|
|
prec += exp;
|
|
|
|
if(exp < 0)
|
|
|
|
prec--;
|
|
|
|
}
|
|
|
|
prec--;
|
|
|
|
|
|
|
|
if(prec+1 > I10_OUTPUT_MAX_PREC)
|
|
|
|
prec = I10_OUTPUT_MAX_PREC-1;
|
|
|
|
else if(prec < 0) {
|
|
|
|
d = 0.0;
|
|
|
|
prec = 0;
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:10:54 +01:00
|
|
|
sprintf(format, "%%.%dle", prec);
|
|
|
|
sprintf(buf, format, d);
|
2010-05-14 12:14:12 +02:00
|
|
|
|
|
|
|
buf[1] = buf[0];
|
2020-12-02 00:54:09 +01:00
|
|
|
data->pos = atoi(buf+prec+3);
|
2010-05-14 12:14:12 +02:00
|
|
|
if(buf[1] != '0')
|
|
|
|
data->pos++;
|
|
|
|
|
|
|
|
for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
|
|
|
|
data->len = p-buf;
|
|
|
|
|
|
|
|
memcpy(data->str, buf+1, data->len);
|
|
|
|
data->str[data->len] = '\0';
|
|
|
|
|
|
|
|
if(buf[1]!='0' && prec-data->len+1>0)
|
|
|
|
memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#undef I10_OUTPUT_MAX_PREC
|
2012-12-14 05:01:48 +01:00
|
|
|
|
2013-09-17 14:44:56 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* memcmp (MSVCRT.@)
|
|
|
|
*/
|
2020-11-19 13:28:52 +01:00
|
|
|
int __cdecl memcmp(const void *ptr1, const void *ptr2, size_t n)
|
2013-09-17 14:44:56 +02:00
|
|
|
{
|
2020-07-22 11:10:22 +02:00
|
|
|
const unsigned char *p1, *p2;
|
|
|
|
|
|
|
|
for (p1 = ptr1, p2 = ptr2; n; n--, p1++, p2++)
|
|
|
|
{
|
|
|
|
if (*p1 < *p2) return -1;
|
|
|
|
if (*p1 > *p2) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-09-17 14:44:56 +02:00
|
|
|
}
|
|
|
|
|
2021-01-26 19:42:03 +01:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
|
|
|
|
#ifdef __i386__
|
|
|
|
|
|
|
|
#define DEST_REG "%edi"
|
|
|
|
#define SRC_REG "%esi"
|
|
|
|
#define LEN_REG "%ecx"
|
|
|
|
#define TMP_REG "%edx"
|
|
|
|
|
|
|
|
#define MEMMOVE_INIT \
|
|
|
|
"pushl " SRC_REG "\n\t" \
|
|
|
|
__ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
|
|
|
|
"pushl " DEST_REG "\n\t" \
|
|
|
|
__ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
|
|
|
|
"movl 12(%esp), " DEST_REG "\n\t" \
|
|
|
|
"movl 16(%esp), " SRC_REG "\n\t" \
|
|
|
|
"movl 20(%esp), " LEN_REG "\n\t"
|
|
|
|
|
|
|
|
#define MEMMOVE_CLEANUP \
|
|
|
|
"movl 12(%esp), %eax\n\t" \
|
|
|
|
"popl " DEST_REG "\n\t" \
|
|
|
|
__ASM_CFI(".cfi_adjust_cfa_offset -4\n\t") \
|
|
|
|
"popl " SRC_REG "\n\t" \
|
|
|
|
__ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define DEST_REG "%rdi"
|
|
|
|
#define SRC_REG "%rsi"
|
|
|
|
#define LEN_REG "%r8"
|
|
|
|
#define TMP_REG "%r9"
|
|
|
|
|
|
|
|
#define MEMMOVE_INIT \
|
|
|
|
"pushq " SRC_REG "\n\t" \
|
|
|
|
__ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
|
|
|
|
"pushq " DEST_REG "\n\t" \
|
|
|
|
__ASM_CFI(".cfi_adjust_cfa_offset 8\n\t") \
|
|
|
|
"movq %rcx, " DEST_REG "\n\t" \
|
|
|
|
"movq %rdx, " SRC_REG "\n\t"
|
|
|
|
|
|
|
|
#define MEMMOVE_CLEANUP \
|
|
|
|
"movq %rcx, %rax\n\t" \
|
|
|
|
"popq " DEST_REG "\n\t" \
|
|
|
|
__ASM_CFI(".cfi_adjust_cfa_offset -8\n\t") \
|
|
|
|
"popq " SRC_REG "\n\t" \
|
|
|
|
__ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void * __cdecl sse2_memmove(void *dst, const void *src, size_t n);
|
|
|
|
__ASM_GLOBAL_FUNC( sse2_memmove,
|
|
|
|
MEMMOVE_INIT
|
|
|
|
"mov " DEST_REG ", " TMP_REG "\n\t" /* check copying direction */
|
|
|
|
"sub " SRC_REG ", " TMP_REG "\n\t"
|
|
|
|
"cmp " LEN_REG ", " TMP_REG "\n\t"
|
|
|
|
"jb copy_bwd\n\t"
|
|
|
|
/* copy forwards */
|
|
|
|
"cmp $4, " LEN_REG "\n\t" /* 4-bytes align */
|
|
|
|
"jb copy_fwd3\n\t"
|
|
|
|
"mov " DEST_REG ", " TMP_REG "\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"movsb\n\t"
|
|
|
|
"dec " LEN_REG "\n\t"
|
|
|
|
"inc " TMP_REG "\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"movsw\n\t"
|
|
|
|
"sub $2, " LEN_REG "\n\t"
|
|
|
|
"inc " TMP_REG "\n\t"
|
|
|
|
"1:\n\t" /* 16-bytes align */
|
|
|
|
"cmp $16, " LEN_REG "\n\t"
|
|
|
|
"jb copy_fwd15\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"movsl\n\t"
|
|
|
|
"sub $4, " LEN_REG "\n\t"
|
|
|
|
"inc " TMP_REG "\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"movsl\n\t"
|
|
|
|
"movsl\n\t"
|
|
|
|
"sub $8, " LEN_REG "\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"cmp $64, " LEN_REG "\n\t"
|
|
|
|
"jb copy_fwd63\n\t"
|
|
|
|
"1:\n\t" /* copy 64-bytes blocks in loop, dest 16-bytes aligned */
|
|
|
|
"movdqu 0x00(" SRC_REG "), %xmm0\n\t"
|
|
|
|
"movdqu 0x10(" SRC_REG "), %xmm1\n\t"
|
|
|
|
"movdqu 0x20(" SRC_REG "), %xmm2\n\t"
|
|
|
|
"movdqu 0x30(" SRC_REG "), %xmm3\n\t"
|
|
|
|
"movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
|
|
|
|
"movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
|
|
|
|
"movdqa %xmm2, 0x20(" DEST_REG ")\n\t"
|
|
|
|
"movdqa %xmm3, 0x30(" DEST_REG ")\n\t"
|
|
|
|
"add $64, " SRC_REG "\n\t"
|
|
|
|
"add $64, " DEST_REG "\n\t"
|
|
|
|
"sub $64, " LEN_REG "\n\t"
|
|
|
|
"cmp $64, " LEN_REG "\n\t"
|
|
|
|
"jae 1b\n\t"
|
|
|
|
"copy_fwd63:\n\t" /* copy last 63 bytes, dest 16-bytes aligned */
|
|
|
|
"mov " LEN_REG ", " TMP_REG "\n\t"
|
|
|
|
"and $15, " LEN_REG "\n\t"
|
|
|
|
"shr $5, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"movdqu 0(" SRC_REG "), %xmm0\n\t"
|
|
|
|
"movdqa %xmm0, 0(" DEST_REG ")\n\t"
|
|
|
|
"add $16, " SRC_REG "\n\t"
|
|
|
|
"add $16, " DEST_REG "\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc copy_fwd15\n\t"
|
|
|
|
"movdqu 0x00(" SRC_REG "), %xmm0\n\t"
|
|
|
|
"movdqu 0x10(" SRC_REG "), %xmm1\n\t"
|
|
|
|
"movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
|
|
|
|
"movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
|
|
|
|
"add $32, " SRC_REG "\n\t"
|
|
|
|
"add $32, " DEST_REG "\n\t"
|
|
|
|
"copy_fwd15:\n\t" /* copy last 15 bytes, dest 4-bytes aligned */
|
|
|
|
"mov " LEN_REG ", " TMP_REG "\n\t"
|
|
|
|
"and $3, " LEN_REG "\n\t"
|
|
|
|
"shr $3, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"movsl\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc copy_fwd3\n\t"
|
|
|
|
"movsl\n\t"
|
|
|
|
"movsl\n\t"
|
|
|
|
"copy_fwd3:\n\t" /* copy last 3 bytes */
|
|
|
|
"shr $1, " LEN_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"movsb\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " LEN_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"movsw\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
MEMMOVE_CLEANUP
|
|
|
|
"ret\n\t"
|
|
|
|
"copy_bwd:\n\t"
|
|
|
|
"lea (" DEST_REG ", " LEN_REG "), " DEST_REG "\n\t"
|
|
|
|
"lea (" SRC_REG ", " LEN_REG "), " SRC_REG "\n\t"
|
|
|
|
"cmp $4, " LEN_REG "\n\t" /* 4-bytes align */
|
|
|
|
"jb copy_bwd3\n\t"
|
|
|
|
"mov " DEST_REG ", " TMP_REG "\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"dec " SRC_REG "\n\t"
|
|
|
|
"dec " DEST_REG "\n\t"
|
|
|
|
"movb (" SRC_REG "), %al\n\t"
|
|
|
|
"movb %al, (" DEST_REG ")\n\t"
|
|
|
|
"dec " LEN_REG "\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"sub $2, " SRC_REG "\n\t"
|
|
|
|
"sub $2, " DEST_REG "\n\t"
|
|
|
|
"movw (" SRC_REG "), %ax\n\t"
|
|
|
|
"movw %ax, (" DEST_REG ")\n\t"
|
|
|
|
"sub $2, " LEN_REG "\n\t"
|
|
|
|
"1:\n\t" /* 16-bytes align */
|
|
|
|
"cmp $16, " LEN_REG "\n\t"
|
|
|
|
"jb copy_bwd15\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"sub $4, " SRC_REG "\n\t"
|
|
|
|
"sub $4, " DEST_REG "\n\t"
|
|
|
|
"movl (" SRC_REG "), %eax\n\t"
|
|
|
|
"movl %eax, (" DEST_REG ")\n\t"
|
|
|
|
"sub $4, " LEN_REG "\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"sub $8, " SRC_REG "\n\t"
|
|
|
|
"sub $8, " DEST_REG "\n\t"
|
|
|
|
"movl 4(" SRC_REG "), %eax\n\t"
|
|
|
|
"movl %eax, 4(" DEST_REG ")\n\t"
|
|
|
|
"movl (" SRC_REG "), %eax\n\t"
|
|
|
|
"movl %eax, (" DEST_REG ")\n\t"
|
|
|
|
"sub $8, " LEN_REG "\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"cmp $64, " LEN_REG "\n\t"
|
|
|
|
"jb copy_bwd63\n\t"
|
|
|
|
"1:\n\t" /* copy 64-bytes blocks in loop, dest 16-bytes aligned */
|
|
|
|
"sub $64, " SRC_REG "\n\t"
|
|
|
|
"sub $64, " DEST_REG "\n\t"
|
|
|
|
"movdqu 0x00(" SRC_REG "), %xmm0\n\t"
|
|
|
|
"movdqu 0x10(" SRC_REG "), %xmm1\n\t"
|
|
|
|
"movdqu 0x20(" SRC_REG "), %xmm2\n\t"
|
|
|
|
"movdqu 0x30(" SRC_REG "), %xmm3\n\t"
|
|
|
|
"movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
|
|
|
|
"movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
|
|
|
|
"movdqa %xmm2, 0x20(" DEST_REG ")\n\t"
|
|
|
|
"movdqa %xmm3, 0x30(" DEST_REG ")\n\t"
|
|
|
|
"sub $64, " LEN_REG "\n\t"
|
|
|
|
"cmp $64, " LEN_REG "\n\t"
|
|
|
|
"jae 1b\n\t"
|
|
|
|
"copy_bwd63:\n\t" /* copy last 63 bytes, dest 16-bytes aligned */
|
|
|
|
"mov " LEN_REG ", " TMP_REG "\n\t"
|
|
|
|
"and $15, " LEN_REG "\n\t"
|
|
|
|
"shr $5, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"sub $16, " SRC_REG "\n\t"
|
|
|
|
"sub $16, " DEST_REG "\n\t"
|
|
|
|
"movdqu (" SRC_REG "), %xmm0\n\t"
|
|
|
|
"movdqa %xmm0, (" DEST_REG ")\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc copy_bwd15\n\t"
|
|
|
|
"sub $32, " SRC_REG "\n\t"
|
|
|
|
"sub $32, " DEST_REG "\n\t"
|
|
|
|
"movdqu 0x00(" SRC_REG "), %xmm0\n\t"
|
|
|
|
"movdqu 0x10(" SRC_REG "), %xmm1\n\t"
|
|
|
|
"movdqa %xmm0, 0x00(" DEST_REG ")\n\t"
|
|
|
|
"movdqa %xmm1, 0x10(" DEST_REG ")\n\t"
|
|
|
|
"copy_bwd15:\n\t" /* copy last 15 bytes, dest 4-bytes aligned */
|
|
|
|
"mov " LEN_REG ", " TMP_REG "\n\t"
|
|
|
|
"and $3, " LEN_REG "\n\t"
|
|
|
|
"shr $3, " TMP_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"sub $4, " SRC_REG "\n\t"
|
|
|
|
"sub $4, " DEST_REG "\n\t"
|
|
|
|
"movl (" SRC_REG "), %eax\n\t"
|
|
|
|
"movl %eax, (" DEST_REG ")\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " TMP_REG "\n\t"
|
|
|
|
"jnc copy_bwd3\n\t"
|
|
|
|
"sub $8, " SRC_REG "\n\t"
|
|
|
|
"sub $8, " DEST_REG "\n\t"
|
|
|
|
"movl 4(" SRC_REG "), %eax\n\t"
|
|
|
|
"movl %eax, 4(" DEST_REG ")\n\t"
|
|
|
|
"movl (" SRC_REG "), %eax\n\t"
|
|
|
|
"movl %eax, (" DEST_REG ")\n\t"
|
|
|
|
"copy_bwd3:\n\t" /* copy last 3 bytes */
|
|
|
|
"shr $1, " LEN_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"dec " SRC_REG "\n\t"
|
|
|
|
"dec " DEST_REG "\n\t"
|
|
|
|
"movb (" SRC_REG "), %al\n\t"
|
|
|
|
"movb %al, (" DEST_REG ")\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"shr $1, " LEN_REG "\n\t"
|
|
|
|
"jnc 1f\n\t"
|
|
|
|
"movw -2(" SRC_REG "), %ax\n\t"
|
|
|
|
"movw %ax, -2(" DEST_REG ")\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
MEMMOVE_CLEANUP
|
|
|
|
"ret" )
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-12-14 05:01:48 +01:00
|
|
|
/*********************************************************************
|
2020-07-22 11:10:26 +02:00
|
|
|
* memmove (MSVCRT.@)
|
2012-12-14 05:01:48 +01:00
|
|
|
*/
|
2020-08-20 20:00:42 +02:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
# define MERGE(w1, sh1, w2, sh2) ((w1 << sh1) | (w2 >> sh2))
|
|
|
|
#else
|
|
|
|
# define MERGE(w1, sh1, w2, sh2) ((w1 >> sh1) | (w2 << sh2))
|
|
|
|
#endif
|
2020-11-19 13:28:52 +01:00
|
|
|
void * __cdecl memmove(void *dst, const void *src, size_t n)
|
2012-12-14 05:01:48 +01:00
|
|
|
{
|
2021-01-26 19:42:03 +01:00
|
|
|
#ifdef __x86_64__
|
|
|
|
return sse2_memmove(dst, src, n);
|
|
|
|
#else
|
2020-08-20 20:00:42 +02:00
|
|
|
unsigned char *d = dst;
|
2020-07-22 11:10:26 +02:00
|
|
|
const unsigned char *s = src;
|
2020-08-20 20:00:42 +02:00
|
|
|
int sh1;
|
|
|
|
|
2021-01-26 19:42:03 +01:00
|
|
|
#ifdef __i386__
|
|
|
|
if (sse2_supported)
|
|
|
|
return sse2_memmove(dst, src, n);
|
|
|
|
#endif
|
|
|
|
|
2020-08-20 20:00:42 +02:00
|
|
|
if (!n) return dst;
|
2020-07-22 11:10:26 +02:00
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
if ((size_t)dst - (size_t)src >= n)
|
2020-07-22 11:10:26 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
for (; (size_t)d % sizeof(size_t) && n; n--) *d++ = *s++;
|
2020-08-20 20:00:42 +02:00
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
sh1 = 8 * ((size_t)s % sizeof(size_t));
|
2020-08-20 20:00:42 +02:00
|
|
|
if (!sh1)
|
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
while (n >= sizeof(size_t))
|
2020-08-20 20:00:42 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
*(size_t*)d = *(size_t*)s;
|
|
|
|
s += sizeof(size_t);
|
|
|
|
d += sizeof(size_t);
|
|
|
|
n -= sizeof(size_t);
|
2020-08-20 20:00:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-27 14:17:05 +01:00
|
|
|
else if (n >= 2 * sizeof(size_t))
|
2020-08-20 20:00:42 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
int sh2 = 8 * sizeof(size_t) - sh1;
|
|
|
|
size_t x, y;
|
2020-08-20 20:00:42 +02:00
|
|
|
|
|
|
|
s -= sh1 / 8;
|
2020-11-27 14:17:05 +01:00
|
|
|
x = *(size_t*)s;
|
2020-08-20 20:00:42 +02:00
|
|
|
do
|
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
s += sizeof(size_t);
|
|
|
|
y = *(size_t*)s;
|
|
|
|
*(size_t*)d = MERGE(x, sh1, y, sh2);
|
|
|
|
d += sizeof(size_t);
|
|
|
|
|
|
|
|
s += sizeof(size_t);
|
|
|
|
x = *(size_t*)s;
|
|
|
|
*(size_t*)d = MERGE(y, sh1, x, sh2);
|
|
|
|
d += sizeof(size_t);
|
|
|
|
|
|
|
|
n -= 2 * sizeof(size_t);
|
|
|
|
} while (n >= 2 * sizeof(size_t));
|
2020-08-20 20:00:42 +02:00
|
|
|
s += sh1 / 8;
|
|
|
|
}
|
2020-07-22 11:10:26 +02:00
|
|
|
while (n--) *d++ = *s++;
|
2020-08-20 20:00:42 +02:00
|
|
|
return dst;
|
2020-07-22 11:10:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-20 20:00:42 +02:00
|
|
|
d += n;
|
|
|
|
s += n;
|
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
for (; (size_t)d % sizeof(size_t) && n; n--) *--d = *--s;
|
2020-08-20 20:00:42 +02:00
|
|
|
|
2020-11-27 14:17:05 +01:00
|
|
|
sh1 = 8 * ((size_t)s % sizeof(size_t));
|
2020-08-20 20:00:42 +02:00
|
|
|
if (!sh1)
|
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
while (n >= sizeof(size_t))
|
2020-08-20 20:00:42 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
s -= sizeof(size_t);
|
|
|
|
d -= sizeof(size_t);
|
|
|
|
*(size_t*)d = *(size_t*)s;
|
|
|
|
n -= sizeof(size_t);
|
2020-08-20 20:00:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-27 14:17:05 +01:00
|
|
|
else if (n >= 2 * sizeof(size_t))
|
2020-08-20 20:00:42 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
int sh2 = 8 * sizeof(size_t) - sh1;
|
|
|
|
size_t x, y;
|
2020-08-20 20:00:42 +02:00
|
|
|
|
|
|
|
s -= sh1 / 8;
|
2020-11-27 14:17:05 +01:00
|
|
|
x = *(size_t*)s;
|
2020-08-20 20:00:42 +02:00
|
|
|
do
|
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
s -= sizeof(size_t);
|
|
|
|
y = *(size_t*)s;
|
|
|
|
d -= sizeof(size_t);
|
|
|
|
*(size_t*)d = MERGE(y, sh1, x, sh2);
|
|
|
|
|
|
|
|
s -= sizeof(size_t);
|
|
|
|
x = *(size_t*)s;
|
|
|
|
d -= sizeof(size_t);
|
|
|
|
*(size_t*)d = MERGE(x, sh1, y, sh2);
|
|
|
|
|
|
|
|
n -= 2 * sizeof(size_t);
|
|
|
|
} while (n >= 2 * sizeof(size_t));
|
2020-08-20 20:00:42 +02:00
|
|
|
s += sh1 / 8;
|
|
|
|
}
|
|
|
|
while (n--) *--d = *--s;
|
2020-07-22 11:10:26 +02:00
|
|
|
}
|
|
|
|
return dst;
|
2021-01-26 19:42:03 +01:00
|
|
|
#endif
|
2012-12-14 05:01:48 +01:00
|
|
|
}
|
2020-08-20 20:00:42 +02:00
|
|
|
#undef MERGE
|
2012-12-24 12:10:21 +01:00
|
|
|
|
2013-09-17 14:45:11 +02:00
|
|
|
/*********************************************************************
|
2020-07-22 11:10:26 +02:00
|
|
|
* memcpy (MSVCRT.@)
|
2013-09-17 14:45:11 +02:00
|
|
|
*/
|
2020-11-19 13:28:52 +01:00
|
|
|
void * __cdecl memcpy(void *dst, const void *src, size_t n)
|
2013-09-17 14:45:11 +02:00
|
|
|
{
|
2020-11-19 13:28:52 +01:00
|
|
|
return memmove(dst, src, n);
|
2013-09-17 14:45:11 +02:00
|
|
|
}
|
|
|
|
|
2013-03-27 16:11:59 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* memset (MSVCRT.@)
|
|
|
|
*/
|
2020-11-19 13:28:52 +01:00
|
|
|
void* __cdecl memset(void *dst, int c, size_t n)
|
2013-03-27 16:11:59 +01:00
|
|
|
{
|
2020-07-22 11:10:28 +02:00
|
|
|
volatile unsigned char *d = dst; /* avoid gcc optimizations */
|
|
|
|
while (n--) *d++ = c;
|
|
|
|
return dst;
|
2013-03-27 16:11:59 +01:00
|
|
|
}
|
|
|
|
|
2013-03-27 16:12:09 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strchr (MSVCRT.@)
|
|
|
|
*/
|
2020-11-19 13:28:52 +01:00
|
|
|
char* __cdecl strchr(const char *str, int c)
|
2013-03-27 16:12:09 +01:00
|
|
|
{
|
2020-07-22 11:10:33 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (*str == (char)c) return (char*)str;
|
|
|
|
} while (*str++);
|
|
|
|
return NULL;
|
2013-03-27 16:12:09 +01:00
|
|
|
}
|
|
|
|
|
2013-09-17 14:45:53 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* strrchr (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* __cdecl strrchr(const char *str, int c)
|
2013-09-17 14:45:53 +02:00
|
|
|
{
|
2020-07-22 11:10:56 +02:00
|
|
|
char *ret = NULL;
|
|
|
|
do { if (*str == (char)c) ret = (char*)str; } while (*str++);
|
|
|
|
return ret;
|
2013-09-17 14:45:53 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 15:39:23 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* memchr (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
void* __cdecl memchr(const void *ptr, int c, size_t n)
|
2013-06-21 15:39:23 +02:00
|
|
|
{
|
2020-07-22 11:10:19 +02:00
|
|
|
const unsigned char *p = ptr;
|
|
|
|
|
2020-12-04 17:41:50 +01:00
|
|
|
for (p = ptr; n; n--, p++) if (*p == (unsigned char)c) return (void *)(ULONG_PTR)p;
|
2020-07-22 11:10:19 +02:00
|
|
|
return NULL;
|
2013-06-21 15:39:23 +02:00
|
|
|
}
|
|
|
|
|
2013-09-17 14:45:20 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* strcmp (MSVCRT.@)
|
|
|
|
*/
|
2020-11-19 13:28:52 +01:00
|
|
|
int __cdecl strcmp(const char *str1, const char *str2)
|
2013-09-17 14:45:20 +02:00
|
|
|
{
|
2019-10-31 09:35:40 +01:00
|
|
|
while (*str1 && *str1 == *str2) { str1++; str2++; }
|
2020-01-16 11:37:03 +01:00
|
|
|
if ((unsigned char)*str1 > (unsigned char)*str2) return 1;
|
|
|
|
if ((unsigned char)*str1 < (unsigned char)*str2) return -1;
|
2019-10-31 09:35:40 +01:00
|
|
|
return 0;
|
2013-09-17 14:45:20 +02:00
|
|
|
}
|
|
|
|
|
2013-03-27 16:12:19 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* strncmp (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int __cdecl strncmp(const char *str1, const char *str2, size_t len)
|
2013-03-27 16:12:19 +01:00
|
|
|
{
|
2019-10-31 09:35:40 +01:00
|
|
|
if (!len) return 0;
|
|
|
|
while (--len && *str1 && *str1 == *str2) { str1++; str2++; }
|
2020-01-16 11:37:03 +01:00
|
|
|
return (unsigned char)*str1 - (unsigned char)*str2;
|
2013-03-27 16:12:19 +01:00
|
|
|
}
|
|
|
|
|
2012-12-24 12:10:21 +01:00
|
|
|
/*********************************************************************
|
2012-12-26 12:04:59 +01:00
|
|
|
* _strnicmp_l (MSVCRT.@)
|
2012-12-24 12:10:21 +01:00
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int __cdecl _strnicmp_l(const char *s1, const char *s2,
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t count, _locale_t locale)
|
2012-12-24 12:10:21 +01:00
|
|
|
{
|
2020-11-26 19:19:09 +01:00
|
|
|
pthreadlocinfo locinfo;
|
2014-09-03 14:43:02 +02:00
|
|
|
int c1, c2;
|
2012-12-24 12:10:21 +01:00
|
|
|
|
2013-01-28 11:40:36 +01:00
|
|
|
if(s1==NULL || s2==NULL)
|
2020-12-04 15:50:17 +01:00
|
|
|
return _NLSCMPERROR;
|
2012-12-24 12:10:21 +01:00
|
|
|
|
2012-12-26 12:04:59 +01:00
|
|
|
if(!count)
|
|
|
|
return 0;
|
|
|
|
|
2020-03-09 13:54:29 +01:00
|
|
|
if(!locale)
|
|
|
|
locinfo = get_locinfo();
|
|
|
|
else
|
|
|
|
locinfo = locale->locinfo;
|
|
|
|
|
2020-11-30 14:37:48 +01:00
|
|
|
if(!locinfo->lc_handle[LC_CTYPE])
|
2020-03-09 13:54:29 +01:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
if ((c1 = *s1++) >= 'A' && c1 <= 'Z')
|
|
|
|
c1 -= 'A' - 'a';
|
|
|
|
if ((c2 = *s2++) >= 'A' && c2 <= 'Z')
|
|
|
|
c2 -= 'A' - 'a';
|
|
|
|
}while(--count && c1 && c1==c2);
|
|
|
|
|
|
|
|
return c1-c2;
|
|
|
|
}
|
|
|
|
|
2012-12-24 12:10:21 +01:00
|
|
|
do {
|
2020-12-01 13:52:34 +01:00
|
|
|
c1 = _tolower_l((unsigned char)*s1++, locale);
|
|
|
|
c2 = _tolower_l((unsigned char)*s2++, locale);
|
2012-12-26 12:04:59 +01:00
|
|
|
}while(--count && c1 && c1==c2);
|
2012-12-24 12:10:21 +01:00
|
|
|
|
|
|
|
return c1-c2;
|
|
|
|
}
|
|
|
|
|
2012-12-26 12:04:59 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _stricmp_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int __cdecl _stricmp_l(const char *s1, const char *s2, _locale_t locale)
|
2012-12-26 12:04:59 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strnicmp_l(s1, s2, -1, locale);
|
2012-12-26 12:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strnicmp (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int __cdecl _strnicmp(const char *s1, const char *s2, size_t count)
|
2012-12-26 12:04:59 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strnicmp_l(s1, s2, count, NULL);
|
2012-12-26 12:04:59 +01:00
|
|
|
}
|
|
|
|
|
2012-12-24 12:10:21 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* _stricmp (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int __cdecl _stricmp(const char *s1, const char *s2)
|
2012-12-24 12:10:21 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _strnicmp_l(s1, s2, -1, NULL);
|
2012-12-24 12:10:21 +01:00
|
|
|
}
|
2013-09-17 14:46:07 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* strstr (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* __cdecl strstr(const char *haystack, const char *needle)
|
2013-09-17 14:46:07 +02:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t i, j, len, needle_len, lps_len;
|
2019-10-28 13:29:15 +01:00
|
|
|
BYTE lps[256];
|
|
|
|
|
2020-11-19 13:28:52 +01:00
|
|
|
needle_len = strlen(needle);
|
2019-10-28 13:29:15 +01:00
|
|
|
if (!needle_len) return (char*)haystack;
|
|
|
|
lps_len = needle_len > ARRAY_SIZE(lps) ? ARRAY_SIZE(lps) : needle_len;
|
|
|
|
|
|
|
|
lps[0] = 0;
|
|
|
|
len = 0;
|
|
|
|
i = 1;
|
|
|
|
while (i < lps_len)
|
|
|
|
{
|
|
|
|
if (needle[i] == needle[len]) lps[i++] = ++len;
|
|
|
|
else if (len) len = lps[len-1];
|
|
|
|
else lps[i++] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = j = 0;
|
|
|
|
while (haystack[i])
|
|
|
|
{
|
|
|
|
while (j < lps_len && haystack[i] && haystack[i] == needle[j])
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j == needle_len) return (char*)haystack + i - j;
|
|
|
|
else if (j)
|
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
if (j == ARRAY_SIZE(lps) && !strncmp(haystack + i, needle + j, needle_len - j))
|
2019-10-28 13:29:15 +01:00
|
|
|
return (char*)haystack + i - j;
|
|
|
|
j = lps[j-1];
|
|
|
|
}
|
|
|
|
else if (haystack[i]) i++;
|
|
|
|
}
|
|
|
|
return NULL;
|
2013-09-17 14:46:07 +02:00
|
|
|
}
|
2017-11-08 10:26:16 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _memicmp_l (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int __cdecl _memicmp_l(const void *v1, const void *v2, size_t len, _locale_t locale)
|
2017-11-08 10:26:16 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
const char *s1 = v1, *s2 = v2;
|
2017-11-08 10:26:16 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
#if _MSVCR_VER == 0 || _MSVCR_VER >= 80
|
|
|
|
if (!s1 || !s2)
|
|
|
|
{
|
|
|
|
if (len)
|
|
|
|
MSVCRT_INVALID_PMT(NULL, EINVAL);
|
2020-12-04 15:50:17 +01:00
|
|
|
return len ? _NLSCMPERROR : 0;
|
2017-11-08 10:26:16 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (len--)
|
|
|
|
{
|
2020-12-01 13:52:34 +01:00
|
|
|
if ((ret = _tolower_l(*s1, locale) - _tolower_l(*s2, locale)))
|
2017-11-08 10:26:16 +01:00
|
|
|
break;
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _memicmp (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
int __cdecl _memicmp(const void *s1, const void *s2, size_t len)
|
2017-11-08 10:26:16 +01:00
|
|
|
{
|
2020-12-02 00:54:09 +01:00
|
|
|
return _memicmp_l(s1, s2, len, NULL);
|
2017-11-08 10:26:16 +01:00
|
|
|
}
|
2018-10-17 15:23:29 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* strcspn (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
size_t __cdecl strcspn(const char *str, const char *reject)
|
2018-10-17 15:23:29 +02:00
|
|
|
{
|
2019-10-28 13:29:01 +01:00
|
|
|
BOOL rejects[256];
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
memset(rejects, 0, sizeof(rejects));
|
|
|
|
|
|
|
|
p = reject;
|
|
|
|
while(*p)
|
|
|
|
{
|
|
|
|
rejects[(unsigned char)*p] = TRUE;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
while(*p && !rejects[(unsigned char)*p]) p++;
|
|
|
|
return p - str;
|
2018-10-17 15:23:29 +02:00
|
|
|
}
|
2018-10-17 15:23:37 +02:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* strpbrk (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
char* __cdecl strpbrk(const char *str, const char *accept)
|
2018-10-17 15:23:37 +02:00
|
|
|
{
|
2020-07-22 11:10:45 +02:00
|
|
|
for (; *str; str++) if (strchr( accept, *str )) return (char*)str;
|
|
|
|
return NULL;
|
2018-10-17 15:23:37 +02:00
|
|
|
}
|
2018-10-30 14:01:09 +01:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* __strncnt (MSVCRT.@)
|
|
|
|
*/
|
2020-12-02 00:54:09 +01:00
|
|
|
size_t __cdecl __strncnt(const char *str, size_t size)
|
2018-10-30 14:01:09 +01:00
|
|
|
{
|
2020-11-27 14:17:05 +01:00
|
|
|
size_t ret = 0;
|
2018-10-30 14:01:09 +01:00
|
|
|
|
|
|
|
#if _MSVCR_VER >= 140
|
|
|
|
while (*str++ && size--)
|
|
|
|
#else
|
|
|
|
while (size-- && *str++)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-03-29 14:06:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef _CRTDLL
|
|
|
|
/*********************************************************************
|
|
|
|
* _strdec (CRTDLL.@)
|
|
|
|
*/
|
|
|
|
char * CDECL _strdec(const char *str1, const char *str2)
|
|
|
|
{
|
|
|
|
return (char *)(str2 - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strinc (CRTDLL.@)
|
|
|
|
*/
|
|
|
|
char * CDECL _strinc(const char *str)
|
|
|
|
{
|
|
|
|
return (char *)(str + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strnextc (CRTDLL.@)
|
|
|
|
*/
|
|
|
|
unsigned int CDECL _strnextc(const char *str)
|
|
|
|
{
|
|
|
|
return (unsigned char)str[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strninc (CRTDLL.@)
|
|
|
|
*/
|
|
|
|
char * CDECL _strninc(const char *str, size_t len)
|
|
|
|
{
|
|
|
|
return (char *)(str + len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strspnp (CRTDLL.@)
|
|
|
|
*/
|
|
|
|
char * CDECL _strspnp( const char *str1, const char *str2)
|
|
|
|
{
|
|
|
|
str1 += strspn( str1, str2 );
|
|
|
|
return *str1 ? (char*)str1 : NULL;
|
|
|
|
}
|
|
|
|
#endif
|