aboutsummaryrefslogtreecommitdiff
path: root/src/lib/mhd_compat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/mhd_compat.c')
-rw-r--r--src/lib/mhd_compat.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/lib/mhd_compat.c b/src/lib/mhd_compat.c
new file mode 100644
index 00000000..3abdc367
--- /dev/null
+++ b/src/lib/mhd_compat.c
@@ -0,0 +1,114 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2014-2016 Karlson2k (Evgeny Grin)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19*/
20
21/**
22 * @file microhttpd/mhd_compat.c
23 * @brief Implementation of platform missing functions.
24 * @author Karlson2k (Evgeny Grin)
25 */
26
27#include "mhd_compat.h"
28#if defined(_WIN32) && !defined(__CYGWIN__)
29#include <stdint.h>
30#include <time.h>
31#ifndef HAVE_SNPRINTF
32#include <stdio.h>
33#include <stdarg.h>
34#endif /* HAVE_SNPRINTF */
35#endif /* _WIN32 && !__CYGWIN__ */
36
37#ifndef HAVE_CALLOC
38#include <string.h> /* for memset() */
39#endif /* ! HAVE_CALLOC */
40
41#if defined(_WIN32) && !defined(__CYGWIN__)
42
43#ifndef HAVE_SNPRINTF
44/* Emulate snprintf function on W32 */
45int
46W32_snprintf (char *__restrict s,
47 size_t n,
48 const char *__restrict format,
49 ...)
50{
51 int ret;
52 va_list args;
53
54 if ( (0 != n) &&
55 (NULL != s) )
56 {
57 va_start (args,
58 format);
59 ret = _vsnprintf (s,
60 n,
61 format,
62 args);
63 va_end (args);
64 if ((int)n == ret)
65 s[n - 1] = 0;
66 if (ret >= 0)
67 return ret;
68 }
69 va_start(args,
70 format);
71 ret = _vscprintf (format,
72 args);
73 va_end(args);
74 if ( (0 <= ret) &&
75 (0 != n) &&
76 (NULL == s) )
77 return -1;
78
79 return ret;
80}
81
82#endif /* HAVE_SNPRINTF */
83#endif /* _WIN32 && !__CYGWIN__ */
84
85#ifndef HAVE_CALLOC
86
87#ifdef __has_builtin
88# if __has_builtin(__builtin_mul_overflow)
89# define MHD_HAVE_NUL_OVERFLOW 1
90# endif
91#elif __GNUC__+0 >= 5
92# define MHD_HAVE_NUL_OVERFLOW 1
93#endif /* __GNUC__ >= 5 */
94
95
96void *MHD_calloc_(size_t nelem, size_t elsize)
97{
98 size_t alloc_size;
99 void *ptr;
100#ifdef MHD_HAVE_NUL_OVERFLOW
101 if (__builtin_mul_overflow(nelem, elsize, &alloc_size) || 0 == alloc_size)
102 return NULL;
103#else /* ! MHD_HAVE_NUL_OVERFLOW */
104 alloc_size = nelem * elsize;
105 if (0 == alloc_size || elsize != alloc_size / nelem)
106 return NULL;
107#endif /* ! MHD_HAVE_NUL_OVERFLOW */
108 ptr = malloc (alloc_size);
109 if (NULL == ptr)
110 return NULL;
111 memset(ptr, 0, alloc_size);
112 return ptr;
113}
114#endif /* ! HAVE_CALLOC */