|
Packit |
875988 |
/*
|
|
Packit |
875988 |
This file is part of libmicrohttpd
|
|
Packit |
875988 |
Copyright (C) 2014-2016 Karlson2k (Evgeny Grin)
|
|
Packit |
875988 |
|
|
Packit |
875988 |
This library is free software; you can redistribute it and/or
|
|
Packit |
875988 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
875988 |
License as published by the Free Software Foundation; either
|
|
Packit |
875988 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
This library is distributed in the hope that it will be useful,
|
|
Packit |
875988 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
875988 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
875988 |
Lesser General Public License for more details.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
875988 |
License along with this library; if not, write to the Free Software
|
|
Packit |
875988 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit |
875988 |
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* @file microhttpd/mhd_compat.c
|
|
Packit |
875988 |
* @brief Implementation of platform missing functions.
|
|
Packit |
875988 |
* @author Karlson2k (Evgeny Grin)
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#include "mhd_compat.h"
|
|
Packit |
875988 |
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
Packit |
875988 |
#include <stdint.h>
|
|
Packit |
875988 |
#include <time.h>
|
|
Packit |
875988 |
#ifndef HAVE_SNPRINTF
|
|
Packit |
875988 |
#include <stdio.h>
|
|
Packit |
875988 |
#include <stdarg.h>
|
|
Packit |
875988 |
#endif /* HAVE_SNPRINTF */
|
|
Packit |
875988 |
#endif /* _WIN32 && !__CYGWIN__ */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifndef HAVE_CALLOC
|
|
Packit |
875988 |
#include <string.h> /* for memset() */
|
|
Packit |
875988 |
#endif /* ! HAVE_CALLOC */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifndef HAVE_SNPRINTF
|
|
Packit |
875988 |
/* Emulate snprintf function on W32 */
|
|
Packit |
875988 |
int
|
|
Packit |
875988 |
W32_snprintf (char *__restrict s,
|
|
Packit |
875988 |
size_t n,
|
|
Packit |
875988 |
const char *__restrict format,
|
|
Packit |
875988 |
...)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
int ret;
|
|
Packit |
875988 |
va_list args;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if ( (0 != n) &&
|
|
Packit |
875988 |
(NULL != s) )
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
va_start (args,
|
|
Packit |
875988 |
format);
|
|
Packit |
875988 |
ret = _vsnprintf (s,
|
|
Packit |
875988 |
n,
|
|
Packit |
875988 |
format,
|
|
Packit |
875988 |
args);
|
|
Packit |
875988 |
va_end (args);
|
|
Packit |
875988 |
if ((int)n == ret)
|
|
Packit |
875988 |
s[n - 1] = 0;
|
|
Packit |
875988 |
if (ret >= 0)
|
|
Packit |
875988 |
return ret;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
va_start(args,
|
|
Packit |
875988 |
format);
|
|
Packit |
875988 |
ret = _vscprintf (format,
|
|
Packit |
875988 |
args);
|
|
Packit |
875988 |
va_end(args);
|
|
Packit |
875988 |
if ( (0 <= ret) &&
|
|
Packit |
875988 |
(0 != n) &&
|
|
Packit |
875988 |
(NULL == s) )
|
|
Packit |
875988 |
return -1;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
return ret;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#endif /* HAVE_SNPRINTF */
|
|
Packit |
875988 |
#endif /* _WIN32 && !__CYGWIN__ */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifndef HAVE_CALLOC
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifdef __has_builtin
|
|
Packit |
875988 |
# if __has_builtin(__builtin_mul_overflow)
|
|
Packit |
875988 |
# define MHD_HAVE_NUL_OVERFLOW 1
|
|
Packit |
875988 |
# endif
|
|
Packit |
875988 |
#elif __GNUC__+0 >= 5
|
|
Packit |
875988 |
# define MHD_HAVE_NUL_OVERFLOW 1
|
|
Packit |
875988 |
#endif /* __GNUC__ >= 5 */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
void *MHD_calloc_(size_t nelem, size_t elsize)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
size_t alloc_size;
|
|
Packit |
875988 |
void *ptr;
|
|
Packit |
875988 |
#ifdef MHD_HAVE_NUL_OVERFLOW
|
|
Packit |
875988 |
if (__builtin_mul_overflow(nelem, elsize, &alloc_size) || 0 == alloc_size)
|
|
Packit |
875988 |
return NULL;
|
|
Packit |
875988 |
#else /* ! MHD_HAVE_NUL_OVERFLOW */
|
|
Packit |
875988 |
alloc_size = nelem * elsize;
|
|
Packit |
875988 |
if (0 == alloc_size || elsize != alloc_size / nelem)
|
|
Packit |
875988 |
return NULL;
|
|
Packit |
875988 |
#endif /* ! MHD_HAVE_NUL_OVERFLOW */
|
|
Packit |
875988 |
ptr = malloc (alloc_size);
|
|
Packit |
875988 |
if (NULL == ptr)
|
|
Packit |
875988 |
return NULL;
|
|
Packit |
875988 |
memset(ptr, 0, alloc_size);
|
|
Packit |
875988 |
return ptr;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
#endif /* ! HAVE_CALLOC */
|