Coverage Report

Created: 2026-09-14 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curl_setup.h
Line
Count
Source
1
#ifndef HEADER_CURL_SETUP_H
2
#define HEADER_CURL_SETUP_H
3
/***************************************************************************
4
 *                                  _   _ ____  _
5
 *  Project                     ___| | | |  _ \| |
6
 *                             / __| | | | |_) | |
7
 *                            | (__| |_| |  _ <| |___
8
 *                             \___|\___/|_| \_\_____|
9
 *
10
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11
 *
12
 * This software is licensed as described in the file COPYING, which
13
 * you should have received as part of this distribution. The terms
14
 * are also available at https://curl.se/docs/copyright.html.
15
 *
16
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17
 * copies of the Software, and permit persons to whom the Software is
18
 * furnished to do so, under the terms of the COPYING file.
19
 *
20
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21
 * KIND, either express or implied.
22
 *
23
 * SPDX-License-Identifier: curl
24
 *
25
 ***************************************************************************/
26
27
#if defined(BUILDING_LIBCURL) && !defined(CURL_NO_OLDIES)
28
#define CURL_NO_OLDIES
29
#endif
30
31
/* Set default _WIN32_WINNT */
32
#ifdef __MINGW32__
33
#include <_mingw.h>
34
#endif
35
36
/* Workaround for Homebrew gcc 12.4.0, 13.3.0, 14.1.0, 14.2.0 (initial build)
37
   that started advertising the `availability` attribute, which then gets used
38
   by Apple SDK, but, in a way incompatible with gcc, resulting in misc errors
39
   inside SDK headers, e.g.:
40
     error: attributes should be specified before the declarator in a function
41
            definition
42
     error: expected ',' or '}' before
43
   Followed by missing declarations.
44
   Work it around by overriding the built-in feature-check macro used by the
45
   headers to enable the problematic attributes. This makes the feature check
46
   fail. Fixed in 14.2.0_1. Disable the workaround if the fix is detected. */
47
#if defined(__APPLE__) && !defined(__clang__) && defined(__GNUC__) && \
48
  defined(__has_attribute)
49
#  if !defined(__has_feature)  /* Keep this PP check separate from others */
50
#    define availability curl_pp_attribute_disabled
51
#  elif !__has_feature(attribute_availability)
52
#    define availability curl_pp_attribute_disabled
53
#  endif
54
#endif
55
56
#ifdef __APPLE__
57
#include <sys/types.h>
58
#include <TargetConditionals.h>
59
/* Fixup faulty target macro initialization in macOS SDK since v14.4 (as of
60
   15.0 beta). The SDK target detection in `TargetConditionals.h` correctly
61
   detects macOS, but fails to set the macro's old name `TARGET_OS_OSX`, then
62
   continues to set it to a default value of 0. Other parts of the SDK still
63
   rely on the old name, and with this inconsistency our builds fail due to
64
   missing declarations. It happens when using mainline llvm older than v18.
65
   Later versions fixed it by predefining these target macros, avoiding the
66
   faulty dynamic detection. gcc is not affected (for now) because it lacks
67
   the necessary dynamic detection features, so the SDK falls back to
68
   a codepath that sets both the old and new macro to 1. */
69
#if defined(TARGET_OS_MAC) && TARGET_OS_MAC && \
70
  defined(TARGET_OS_OSX) && !TARGET_OS_OSX && \
71
  (!defined(TARGET_OS_IPHONE) || !TARGET_OS_IPHONE) && \
72
  (!defined(TARGET_OS_SIMULATOR) || !TARGET_OS_SIMULATOR)
73
#undef TARGET_OS_OSX
74
#define TARGET_OS_OSX TARGET_OS_MAC
75
#endif
76
#endif
77
78
#if defined(__MINGW32__) && \
79
  (!defined(__MINGW64_VERSION_MAJOR) || (__MINGW64_VERSION_MAJOR < 3))
80
#error "mingw-w64 3.0 or greater required"
81
#endif
82
83
/* Visual Studio 2010 is the minimum Visual Studio version we support.
84
   Workarounds for older versions of Visual Studio have been removed. */
85
#if defined(_MSC_VER) && (_MSC_VER < 1600)
86
#error "Ancient versions of Visual Studio are no longer supported due to bugs."
87
#endif
88
89
#ifdef _MSC_VER
90
/* Disable Visual Studio warnings: 4127 "conditional expression is constant" */
91
#pragma warning(disable:4127)
92
#ifndef _CRT_SECURE_NO_WARNINGS
93
#define _CRT_SECURE_NO_WARNINGS  /* for getenv(), sscanf(), vsnprintf() */
94
#endif
95
#endif /* _MSC_VER */
96
97
#ifdef _WIN32
98
/*
99
 * Do not include unneeded stuff in Windows headers to avoid compiler
100
 * warnings and macro clashes.
101
 * Make sure to define this macro before including any Windows headers.
102
 */
103
#  ifndef WIN32_LEAN_AND_MEAN
104
#  define WIN32_LEAN_AND_MEAN
105
#  endif
106
#  ifndef NOGDI
107
#  define NOGDI
108
#  endif
109
110
/* Detect Windows App environment which has a restricted access
111
 * to the Win32 APIs. */
112
#  if (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0602)) || \
113
     defined(WINAPI_FAMILY)
114
#    include <winapifamily.h>
115
#    if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) &&  \
116
       !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
117
#      define CURL_WINDOWS_UWP
118
#    endif
119
#  endif
120
121
/* Mandatory to define SECURITY_WIN32 or SECURITY_KERNEL to indicating who is
122
   compiling the code. */
123
#undef SECURITY_KERNEL
124
#undef SECURITY_WIN32
125
#define SECURITY_WIN32  /* for <sspi.h> */
126
#endif
127
128
/* Compatibility */
129
#ifdef ENABLE_IPV6
130
#define USE_IPV6 1
131
#endif
132
133
/*
134
 * Include configuration script results or hand-crafted
135
 * configuration file for platforms which lack config tool.
136
 */
137
138
#ifdef HAVE_CONFIG_H
139
140
#include "curl_config.h"
141
142
#else /* HAVE_CONFIG_H */
143
144
#ifdef _WIN32
145
#  include "config-win32.h"
146
#endif
147
148
#ifdef macintosh
149
#  include "config-mac.h"
150
#endif
151
152
#ifdef __OS400__
153
#  include "config-os400.h"
154
#endif
155
156
#endif /* HAVE_CONFIG_H */
157
158
#ifdef _WIN32
159
#  if defined(_WIN32_WINNT) && (_WIN32_WINNT < 0x0600)
160
#    error The minimum build target is Windows Vista (0x0600)
161
#  endif
162
163
#  if !defined(CURL_WINDOWS_UWP) && (defined(_MSC_VER) || defined(__MINGW32__))
164
#    ifndef HAVE_IF_NAMETOINDEX
165
#    define HAVE_IF_NAMETOINDEX
166
#    endif
167
#  endif
168
#endif
169
170
/* ================================================================ */
171
/* Definition of preprocessor macros/symbols which modify compiler  */
172
/* behavior or generated code characteristics must be done here,    */
173
/* as appropriate, before any system header file is included. It is */
174
/* also possible to have them defined in the config file included   */
175
/* before this point. As a result of all this we frown inclusion of */
176
/* system header files in our config files, avoid this at any cost. */
177
/* ================================================================ */
178
179
#ifdef HAVE_LIBZ
180
#  ifndef ZLIB_CONST
181
#  define ZLIB_CONST  /* Use z_const. Supported by v1.2.5.2 or greater. */
182
#  endif
183
#endif
184
185
/*
186
 * AIX 4.3 and newer needs _THREAD_SAFE defined to build
187
 * proper reentrant code. Others may also need it.
188
 */
189
#ifdef NEED_THREAD_SAFE
190
#  ifndef _THREAD_SAFE
191
#  define _THREAD_SAFE
192
#  endif
193
#endif
194
195
/*
196
 * Tru64 needs _REENTRANT set for a few function prototypes and
197
 * things to appear in the system header files. Unixware needs it
198
 * to build proper reentrant code. Others may also need it.
199
 */
200
#ifdef NEED_REENTRANT
201
#  ifndef _REENTRANT
202
#  define _REENTRANT
203
#  endif
204
#endif
205
206
/* Solaris needs this to get a POSIX-conformant getpwuid_r */
207
#if defined(sun) || defined(__sun)
208
#  ifndef _POSIX_PTHREAD_SEMANTICS
209
#  define _POSIX_PTHREAD_SEMANTICS 1
210
#  endif
211
#endif
212
213
/* ================================================================ */
214
/*  If you need to include a system header file for your platform,  */
215
/*  please, do it beyond the point further indicated in this file.  */
216
/* ================================================================ */
217
218
/* Give calloc a chance to be included early, so we do not redefine */
219
#ifdef HAVE_THREADS_POSIX
220
#  include <pthread.h>
221
#endif
222
223
/*
224
 * When HTTP is disabled, disable HTTP-only features
225
 */
226
#ifdef CURL_DISABLE_HTTP
227
#  ifndef CURL_DISABLE_ALTSVC
228
#  define CURL_DISABLE_ALTSVC
229
#  endif
230
#  ifndef CURL_DISABLE_AWS
231
#  define CURL_DISABLE_AWS
232
#  endif
233
#  ifndef CURL_DISABLE_BASIC_AUTH
234
#  define CURL_DISABLE_BASIC_AUTH
235
#  endif
236
#  ifndef CURL_DISABLE_BEARER_AUTH
237
#  define CURL_DISABLE_BEARER_AUTH
238
#  endif
239
#  ifndef CURL_DISABLE_COOKIES
240
#  define CURL_DISABLE_COOKIES
241
#  endif
242
#  ifndef CURL_DISABLE_DOH
243
#  define CURL_DISABLE_DOH
244
#  endif
245
#  ifndef CURL_DISABLE_FORM_API
246
#  define CURL_DISABLE_FORM_API
247
#  endif
248
#  ifndef CURL_DISABLE_HEADERS_API
249
#  define CURL_DISABLE_HEADERS_API
250
#  endif
251
#  ifndef CURL_DISABLE_HSTS
252
#  define CURL_DISABLE_HSTS
253
#  endif
254
#  ifndef CURL_DISABLE_HTTPSIG
255
#  define CURL_DISABLE_HTTPSIG
256
#  endif
257
#  ifndef CURL_DISABLE_HTTP_AUTH
258
#  define CURL_DISABLE_HTTP_AUTH
259
#  endif
260
#  ifndef CURL_DISABLE_RTSP
261
#  define CURL_DISABLE_RTSP
262
#  endif
263
#  ifndef CURL_DISABLE_WEBSOCKETS
264
#  define CURL_DISABLE_WEBSOCKETS /* no WebSockets without HTTP present */
265
#  endif
266
#endif
267
268
/* ================================================================ */
269
/* No system header file shall be included in this file before this */
270
/* point.                                                           */
271
/* ================================================================ */
272
273
/*
274
 * OS/400 setup file includes some system headers.
275
 */
276
#ifdef __OS400__
277
#  include "setup-os400.h"
278
#endif
279
280
/*
281
 * VMS setup file includes some system headers.
282
 */
283
#ifdef __VMS
284
#  include "setup-vms.h"
285
#endif
286
287
/*
288
 * Windows setup file includes some system headers.
289
 */
290
#ifdef _WIN32
291
#  include "setup-win32.h"
292
#endif
293
294
#include <curl/system.h>
295
296
/* Helper macro to expand and concatenate two macros.
297
 * Direct macros concatenation does not work because macros
298
 * are not expanded before direct concatenation.
299
 */
300
#define CURL_CONC_MACROS_(A, B) A ## B
301
#define CURL_CONC_MACROS(A, B) CURL_CONC_MACROS_(A, B)
302
303
/* curl uses its own printf() function internally. It understands the GNU
304
 * format. Use this format, so that it matches the GNU format attribute we
305
 * use with the MinGW compiler, allowing it to verify them at compile-time.
306
 */
307
#ifdef __MINGW32__
308
#  undef CURL_FORMAT_CURL_OFF_T
309
#  undef CURL_FORMAT_CURL_OFF_TU
310
#  define CURL_FORMAT_CURL_OFF_T   "lld"
311
#  define CURL_FORMAT_CURL_OFF_TU  "llu"
312
#endif
313
314
/* based on logic in "curl/mprintf.h" */
315
#if (defined(__GNUC__) || defined(__clang__) ||                 \
316
     defined(__IAR_SYSTEMS_ICC__)) &&                           \
317
  defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
318
  !defined(CURL_NO_FMT_CHECKS)
319
#if defined(__MINGW32__) && !defined(__clang__)
320
#define CURL_PRINTF(fmt, arg) __attribute__((format(gnu_printf, fmt, arg)))
321
#else
322
#define CURL_PRINTF(fmt, arg) __attribute__((format(__printf__, fmt, arg)))
323
#endif
324
#else
325
#define CURL_PRINTF(fmt, arg)
326
#endif
327
328
/* Override default printf mask check rules in "curl/mprintf.h" */
329
#define CURL_TEMP_PRINTF CURL_PRINTF
330
331
/* Workaround for mainline llvm v16 and earlier missing a built-in macro
332
   expected by macOS SDK v14 / Xcode v15 (2023) and newer.
333
   gcc (as of v14) is also missing it. */
334
#if defined(__APPLE__) &&                                   \
335
  ((!defined(__apple_build_version__) &&                    \
336
    defined(__clang__) && __clang_major__ < 17) ||          \
337
   (defined(__GNUC__) && __GNUC__ <= 14)) &&                \
338
  defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
339
  !defined(__ENVIRONMENT_OS_VERSION_MIN_REQUIRED__)
340
#define __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__ \
341
  __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
342
#endif
343
344
/*
345
 * Use getaddrinfo to resolve the IPv4 address literal. If the current network
346
 * interface does not support IPv4, but supports IPv6, NAT64, and DNS64,
347
 * performing this task will result in a synthesized IPv6 address.
348
 */
349
#if defined(__APPLE__) && !defined(USE_ARES)
350
#  define USE_RESOLVE_ON_IPS 1
351
#  if TARGET_OS_MAC && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) && \
352
     defined(USE_IPV6)
353
#    define CURL_MACOS_CALL_COPYPROXIES 1
354
#  endif
355
#endif
356
357
#ifdef USE_ARES
358
#  ifndef CARES_NO_DEPRECATED
359
#  define CARES_NO_DEPRECATED  /* for ares_getsock() */
360
#  endif
361
#  if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && defined(_WIN32)
362
#    define CARES_STATICLIB  /* define it before including ares.h */
363
#  endif
364
#endif
365
366
#ifdef USE_LWIPSOCK
367
#  include <lwip/init.h>
368
#  include <lwip/sockets.h>
369
#  include <lwip/netdb.h>
370
#endif
371
372
#ifdef macintosh
373
#  include <extra/stricmp.h>
374
#  include <extra/strdup.h>
375
#endif
376
377
#ifdef __AMIGA__
378
#  ifdef __amigaos4__
379
#    define __USE_INLINE__
380
     /* use our own resolver which uses runtime feature detection */
381
#    define CURLRES_AMIGA
382
     /* getaddrinfo() currently crashes bsdsocket.library, so disable */
383
#    undef HAVE_GETADDRINFO
384
#    if !(defined(__NEWLIB__) || \
385
          (defined(__CLIB2__) && defined(__THREAD_SAFE)))
386
       /* disable threaded resolver with clib2 - requires newlib or clib-ts */
387
#      undef USE_RESOLV_THREADED
388
#    endif
389
#  endif
390
#  include <exec/types.h>
391
#  include <exec/execbase.h>
392
#  include <proto/exec.h>
393
#  include <proto/dos.h>
394
#  include <unistd.h>
395
#  if defined(HAVE_PROTO_BSDSOCKET_H) && \
396
    (!defined(__amigaos4__) || defined(USE_AMISSL))
397
     /* use bsdsocket.library directly, instead of libc networking functions */
398
#    define _SYS_MBUF_H /* m_len define clashes with curl */
399
#    include <proto/bsdsocket.h>
400
#    ifdef __amigaos4__
401
       int Curl_amiga_select(int nfds, fd_set *readfds, fd_set *writefds,
402
                             fd_set *errorfds, struct timeval *timeout);
403
#      define select(a, b, c, d, e) Curl_amiga_select(a, b, c, d, e)
404
#    else
405
#      define select(a, b, c, d, e) WaitSelect(a, b, c, d, e, 0)
406
#    endif
407
     /* must not use libc's fcntl() on bsdsocket.library sockfds! */
408
#    undef HAVE_FCNTL
409
#    undef HAVE_FCNTL_O_NONBLOCK
410
#  else
411
     /* use libc networking and hence close() and fcntl() */
412
#    undef HAVE_CLOSESOCKET_CAMEL
413
#    undef HAVE_IOCTLSOCKET_CAMEL
414
#  endif
415
/*
416
 * In clib2 arpa/inet.h warns that some prototypes may clash
417
 * with bsdsocket.library. This avoids the definition of those.
418
 */
419
#  define __NO_NET_API
420
#endif
421
422
/* Whether to use eventfd() */
423
#if defined(HAVE_EVENTFD) && defined(HAVE_SYS_EVENTFD_H)
424
#define USE_EVENTFD
425
#endif
426
427
#ifdef SO_NOSIGPIPE
428
#define USE_SO_NOSIGPIPE
429
#endif
430
431
#include <stdio.h>
432
#include <assert.h>
433
434
#ifdef __TANDEM /* for ns*-tandem-nsk systems */
435
#  ifndef __LP64
436
#    include <floss.h> /* FLOSS is only used for 32-bit builds. */
437
#  endif
438
#endif
439
440
#include <stdint.h>
441
#define HAVE_UINTPTR_T  /* assume uintptr_t is provided by stdint.h */
442
443
#ifdef __DJGPP__
444
/* By default, DJGPP provides this type as a version of 'unsigned long' which
445
   forces us to use a define use it in printf() format strings without
446
   warnings. long and int are both 32 bits for this platform. */
447
#define uint32_t unsigned int
448
#endif
449
450
/* Disable uintptr_t for targets known to miss it from stdint.h */
451
#ifdef __OS400__
452
#undef HAVE_UINTPTR_T
453
#endif
454
455
#include <limits.h>
456
/* Include after setting system macros that may affect type sizes
457
   (e.g. 'off_t' or 'time_t'), or suppress warnings
458
   (e.g. '_CRT_SECURE_NO_WARNINGS`), but before including sys/stat.h */
459
#include <sys/types.h>
460
461
#ifdef _WIN32
462
#  ifdef HAVE_IO_H
463
#  include <io.h>
464
#  endif
465
#  include <sys/stat.h>
466
   /* Large file (>2Gb) support using Win32 functions. */
467
#  define curl_lseek                      _lseeki64
468
#  define LSEEK_ERROR                     ((__int64)-1)
469
#elif defined(__DJGPP__)
470
   /* Requires DJGPP 2.04 */
471
#  include <unistd.h>
472
#  define curl_lseek                      llseek
473
#  define LSEEK_ERROR                     ((offset_t)-1)
474
#elif defined(__AMIGA__)
475
#  define curl_lseek(fd, offset, whence)  lseek(fd, (off_t)(offset), whence)
476
#  define LSEEK_ERROR                     ((off_t)-1)
477
#else
478
0
#  define curl_lseek                      lseek
479
#  define LSEEK_ERROR                     ((off_t)-1)
480
#endif
481
482
#ifndef SIZEOF_TIME_T
483
/* assume default size of time_t to be 32 bits */
484
#define SIZEOF_TIME_T 4
485
#endif
486
487
#ifndef SIZEOF_CURL_SOCKET_T
488
/* configure and cmake check and set the define */
489
#  if defined(USE_WINSOCK) && defined(_WIN64)
490
#    define SIZEOF_CURL_SOCKET_T 8
491
#  else
492
/* default guess */
493
#    define SIZEOF_CURL_SOCKET_T 4
494
#  endif
495
#endif
496
497
#if SIZEOF_CURL_SOCKET_T < 8
498
#ifdef USE_WINSOCK
499
#  define FMT_SOCKET_T "u"
500
#else
501
#  define FMT_SOCKET_T "d"
502
#endif
503
#elif defined(USE_WINSOCK)
504
#  define FMT_SOCKET_T "zu"
505
#else
506
#  define FMT_SOCKET_T "qd"
507
#endif
508
509
/*
510
 * Default sizeof(off_t) in case it has not been defined in config file.
511
 */
512
513
#ifndef SIZEOF_OFF_T
514
#  if defined(__VMS) && !defined(__VAX)
515
#    ifdef _LARGEFILE
516
#      define SIZEOF_OFF_T 8
517
#    endif
518
#  elif defined(__OS400__) && defined(__ILEC400__)
519
#    ifdef _LARGE_FILES
520
#      define SIZEOF_OFF_T 8
521
#    endif
522
#  elif defined(__MVS__) && defined(__IBMC__)
523
#    if defined(_LP64) || defined(_LARGE_FILES)
524
#      define SIZEOF_OFF_T 8
525
#    endif
526
#  elif defined(__370__) && defined(__IBMC__)
527
#    if defined(_LP64) || defined(_LARGE_FILES)
528
#      define SIZEOF_OFF_T 8
529
#    endif
530
#  endif
531
#  ifndef SIZEOF_OFF_T
532
#  define SIZEOF_OFF_T 4
533
#  endif
534
#endif
535
536
#if SIZEOF_CURL_OFF_T < 8
537
#error "too small curl_off_t"
538
#else
539
   /* assume SIZEOF_CURL_OFF_T == 8 */
540
3.78M
#  define CURL_OFF_T_MAX 0x7FFFFFFFFFFFFFFF
541
#endif
542
1.20M
#define CURL_OFF_T_MIN (-CURL_OFF_T_MAX - 1)
543
544
22
#define FMT_OFF_T  CURL_FORMAT_CURL_OFF_T
545
#define FMT_OFF_TU CURL_FORMAT_CURL_OFF_TU
546
547
#if SIZEOF_TIME_T == 4
548
#  ifdef HAVE_TIME_T_UNSIGNED
549
#  define TIME_T_MAX UINT_MAX
550
#  define TIME_T_MIN 0
551
#  else
552
#  define TIME_T_MAX INT_MAX
553
#  define TIME_T_MIN INT_MIN
554
#  endif
555
#else
556
#  ifdef HAVE_TIME_T_UNSIGNED
557
#  define TIME_T_MAX 0xFFFFFFFFFFFFFFFF
558
#  define TIME_T_MIN 0
559
#  else
560
1.43k
#  define TIME_T_MAX 0x7FFFFFFFFFFFFFFF
561
#  define TIME_T_MIN (-TIME_T_MAX - 1)
562
#  endif
563
#endif
564
565
#ifndef SIZE_MAX
566
/* some limits.h headers have this defined, some do not */
567
#if defined(SIZEOF_SIZE_T) && (SIZEOF_SIZE_T > 4)
568
#define SIZE_MAX 18446744073709551615U
569
#else
570
#define SIZE_MAX 4294967295U
571
#endif
572
#endif
573
574
#ifndef SSIZE_MAX
575
/* some limits.h headers have this defined, some do not */
576
#if defined(SIZEOF_SIZE_T) && (SIZEOF_SIZE_T > 4)
577
#define SSIZE_MAX 9223372036854775807
578
#else
579
#define SSIZE_MAX 2147483647
580
#endif
581
#endif
582
583
#if SIZEOF_LONG > SIZEOF_SIZE_T
584
#error "unexpected: 'long' is larger than 'size_t'"
585
#endif
586
587
/*
588
 * Arg 2 type for gethostname in case it has not been defined in config file.
589
 */
590
#ifndef GETHOSTNAME_TYPE_ARG2
591
#  ifdef USE_WINSOCK
592
#    define GETHOSTNAME_TYPE_ARG2 int
593
#  else
594
#    define GETHOSTNAME_TYPE_ARG2 size_t
595
#  endif
596
#endif
597
598
/* Below we define some functions. They should
599
   4. set the SIGALRM signal timeout
600
   5. set dir/file naming defines
601
 */
602
603
#ifdef _WIN32
604
605
#  define DIR_CHAR      "\\"
606
607
#else /* _WIN32 */
608
609
#  ifdef MSDOS  /* Watt-32 */
610
611
#    include <sys/ioctl.h>
612
#    define select(n, r, w, x, t)  select_s(n, r, w, x, t)
613
#    define ioctl(x, y, z)         ioctlsocket(x, y, (char *)(z))
614
#    include <tcp.h>
615
#    undef word
616
#    undef byte
617
618
#  endif /* MSDOS */
619
620
#  ifdef __minix
621
     /* Minix 3 versions up to at least 3.1.3 are missing these prototypes */
622
     extern struct tm *gmtime_r(const time_t * const timep, struct tm *tmp);
623
#  endif
624
625
0
#  define DIR_CHAR      "/"
626
627
#endif /* _WIN32 */
628
629
/* We want to use mutex when available. */
630
#if defined(HAVE_THREADS_POSIX) || defined(_WIN32)
631
#define USE_MUTEX
632
#endif
633
634
/* threaded resolver is the only feature requiring threads. */
635
#ifdef USE_RESOLV_THREADED
636
#define USE_THREADS
637
#endif
638
639
/* ---------------------------------------------------------------- */
640
/*             resolver specialty compile-time defines              */
641
/*         CURLRES_* defines to use in the host*.c sources          */
642
/* ---------------------------------------------------------------- */
643
644
/*
645
 * Mutually exclusive CURLRES_* definitions.
646
 */
647
#if defined(USE_IPV6) && defined(HAVE_GETADDRINFO)
648
#  define CURLRES_IPV6
649
#elif defined(USE_IPV6) && (defined(_WIN32) || defined(__CYGWIN__))
650
/* assume on Windows that IPv6 without getaddrinfo is a broken build */
651
#  error "Unexpected build: IPv6 is enabled but getaddrinfo was not found."
652
#else
653
#  define CURLRES_IPV4
654
#endif
655
656
#ifdef USE_RESOLV_THREADED
657
#  define CURLRES_ASYNCH
658
#elif defined(USE_RESOLV_ARES)
659
#  define CURLRES_ASYNCH
660
/* now undef the stock libc functions to avoid them being used */
661
#  undef HAVE_GETADDRINFO
662
#  undef HAVE_FREEADDRINFO
663
#else
664
#  define CURLRES_SYNCH
665
#endif
666
667
/* ---------------------------------------------------------------- */
668
669
#if defined(HAVE_LIBIDN2) && defined(HAVE_IDN2_H) && \
670
  !defined(USE_WIN32_IDN) && !defined(USE_APPLE_IDN)
671
/* The lib and header are present */
672
#define USE_LIBIDN2
673
#endif
674
675
#if defined(USE_LIBIDN2) && (defined(USE_WIN32_IDN) || defined(USE_APPLE_IDN))
676
#error "libidn2 cannot be enabled with WinIDN or AppleIDN, choose one."
677
#endif
678
679
#if defined(USE_GNUTLS) || defined(USE_OPENSSL) || defined(USE_MBEDTLS) || \
680
  defined(USE_WOLFSSL) || defined(USE_SCHANNEL) || defined(USE_RUSTLS)
681
#define USE_SSL    /* SSL support has been enabled */
682
#endif
683
684
#if defined(USE_OPENSSL) && defined(USE_WOLFSSL)
685
#ifndef OPENSSL_COEXIST
686
#define OPENSSL_COEXIST
687
#endif
688
#endif
689
690
#if defined(USE_WOLFSSL) && defined(USE_GNUTLS)
691
/* Avoid defining unprefixed wolfSSL SHA macros colliding with nettle ones */
692
#define NO_OLD_WC_NAMES
693
#endif
694
695
/* Single point where USE_SPNEGO definition might be defined */
696
#if !defined(CURL_DISABLE_NEGOTIATE_AUTH) &&          \
697
  (defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI))
698
#define USE_SPNEGO
699
#endif
700
701
/* Single point where USE_KERBEROS5 definition might be defined */
702
#if !defined(CURL_DISABLE_KERBEROS_AUTH) &&           \
703
  (defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI))
704
#define USE_KERBEROS5
705
#endif
706
707
/* Single point where USE_NTLM definition might be defined */
708
#ifdef CURL_ENABLE_NTLM
709
#  if (defined(USE_OPENSSL) && defined(HAVE_DES_ECB_ENCRYPT)) ||   \
710
  defined(USE_GNUTLS) ||                                           \
711
  (defined(USE_MBEDTLS) && defined(HAVE_MBEDTLS_DES_CRYPT_ECB)) || \
712
  defined(USE_OS400CRYPTO) || defined(USE_WIN32_CRYPTO) ||         \
713
  (defined(USE_WOLFSSL) && defined(HAVE_WC_DES_ECBENCRYPT))
714
#    define USE_CURL_NTLM_CORE
715
#  endif
716
#  if defined(USE_CURL_NTLM_CORE) || defined(USE_WINDOWS_SSPI)
717
#    define USE_NTLM
718
#  endif
719
#endif
720
721
#if defined(USE_LIBSSH2) || defined(USE_LIBSSH)
722
#define USE_SSH
723
#endif
724
725
/* GCC <4.6 does not support '#pragma GCC diagnostic push' and does not support
726
   'pragma GCC diagnostic' inside functions.
727
   Use CURL_HAVE_DIAG to guard the above in the curl codebase, instead of
728
   defined(__GNUC__) || defined(__clang__).
729
 */
730
#if defined(__clang__) || (defined(__GNUC__) && \
731
  ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))))
732
#define CURL_HAVE_DIAG
733
#endif
734
735
/*
736
 * Provide a mechanism to silence picky compilers, such as gcc 4.6+.
737
 * Parameters should of course normally not be unused, but for example when
738
 * we have multiple implementations of the same interface it may happen.
739
 */
740
#if defined(__GNUC__) && ((__GNUC__ >= 3) || \
741
  ((__GNUC__ == 2) && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ >= 7)))
742
#  define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
743
#elif defined(__IAR_SYSTEMS_ICC__) && (__VER__ >= 9040001)
744
#  define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
745
#else
746
#  define WARN_UNUSED_RESULT
747
#endif
748
749
/* noreturn attribute */
750
751
#ifndef CURL_NORETURN
752
#if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__clang__) || \
753
  defined(__IAR_SYSTEMS_ICC__)
754
#  define CURL_NORETURN  __attribute__((__noreturn__))
755
#elif defined(_MSC_VER)
756
#  define CURL_NORETURN  __declspec(noreturn)
757
#else
758
#  define CURL_NORETURN
759
#endif
760
#endif
761
762
/* fallthrough attribute */
763
764
#ifndef FALLTHROUGH
765
#if (defined(__GNUC__) && __GNUC__ >= 7) ||     \
766
  (defined(__clang__) && __clang_major__ >= 10)
767
1.34M
#  define FALLTHROUGH()  __attribute__((fallthrough))
768
#else
769
#  define FALLTHROUGH()  do {} while(0)
770
#endif
771
#endif
772
773
/*
774
 * Inclusion of common header files.
775
 */
776
777
#include <stdlib.h>
778
#include <string.h>
779
#include <stdarg.h>
780
#include <time.h>
781
#include <errno.h>
782
783
#include <sys/stat.h>
784
785
#if !defined(_WIN32) || defined(__MINGW32__)
786
#include <sys/time.h>
787
#endif
788
789
#ifdef HAVE_IO_H
790
#include <io.h>
791
#endif
792
793
#ifdef HAVE_FCNTL_H
794
#include <fcntl.h>
795
#endif
796
797
#if defined(HAVE_STDBOOL_H) && defined(HAVE_BOOL_T)
798
#include <stdbool.h>
799
#endif
800
801
#ifdef HAVE_UNISTD_H
802
#include <unistd.h>
803
#endif
804
805
/* Macro to strip 'const' without triggering a compiler warning.
806
   Use it for APIs that do not or cannot support the const qualifier. */
807
#ifdef HAVE_UINTPTR_T
808
1.64M
#define CURL_UNCONST(p) ((void *)(uintptr_t)(const void *)(p))
809
#else
810
#define CURL_UNCONST(p) ((void *)(p))  /* Fall back to simple cast */
811
#endif
812
813
#ifdef USE_SCHANNEL
814
/* Must set this before <schannel.h> is included directly or indirectly by
815
   another Windows header. */
816
#  define SCHANNEL_USE_BLACKLISTS  /* for SCH_CREDENTIALS */
817
#  include <subauth.h>  /* for [P]UNICODE_STRING in SCH_CREDENTIALS */
818
#endif
819
820
#ifdef __hpux
821
#  if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
822
#    ifdef _APP32_64BIT_OFF_T
823
#      define OLD_APP32_64BIT_OFF_T _APP32_64BIT_OFF_T
824
#      undef _APP32_64BIT_OFF_T
825
#    else
826
#      undef OLD_APP32_64BIT_OFF_T
827
#    endif
828
#  endif
829
#endif
830
831
#ifndef _WIN32
832
#include <sys/socket.h>  /* also for MSG_NOSIGNAL */
833
#endif
834
835
#include "functypes.h"
836
837
#ifdef __hpux
838
#  if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
839
#    ifdef OLD_APP32_64BIT_OFF_T
840
#      define _APP32_64BIT_OFF_T OLD_APP32_64BIT_OFF_T
841
#      undef OLD_APP32_64BIT_OFF_T
842
#    endif
843
#  endif
844
#endif
845
846
/*
847
 * Definition of timeval struct for platforms that do not have it.
848
 */
849
#ifndef HAVE_STRUCT_TIMEVAL
850
struct timeval {
851
  long tv_sec;
852
  long tv_usec;
853
};
854
#endif
855
856
/*
857
 * If we have the MSG_NOSIGNAL define, make sure we use
858
 * it as the fourth argument of function send()
859
 */
860
#ifdef MSG_NOSIGNAL
861
49.7k
#define SEND_4TH_ARG MSG_NOSIGNAL
862
#else
863
#define SEND_4TH_ARG 0
864
#endif
865
866
#ifdef __minix
867
/* Minix does not support recv on TCP sockets */
868
#define sread(x, y, z) (ssize_t)read((RECV_TYPE_ARG1)(x), \
869
                                     (RECV_TYPE_ARG2)(y), \
870
                                     (RECV_TYPE_ARG3)(z))
871
872
#elif defined(HAVE_RECV)
873
/*
874
 * The definitions for the return type and arguments types
875
 * of functions recv() and send() belong and come from the
876
 * configuration file. Do not define them in any other place.
877
 *
878
 * HAVE_RECV is defined if you have a function named recv()
879
 * which is used to read incoming data from sockets. If your
880
 * function has another name then do not define HAVE_RECV.
881
 *
882
 * If HAVE_RECV is defined then RECV_TYPE_ARG1, RECV_TYPE_ARG2,
883
 * RECV_TYPE_ARG3, RECV_TYPE_ARG4 and RECV_TYPE_RETV must also
884
 * be defined.
885
 *
886
 * HAVE_SEND is defined if you have a function named send()
887
 * which is used to write outgoing data on a connected socket.
888
 * If yours has another name then do not define HAVE_SEND.
889
 *
890
 * If HAVE_SEND is defined then SEND_TYPE_ARG1, SEND_TYPE_ARG2,
891
 * SEND_TYPE_ARG3, SEND_TYPE_ARG4 and SEND_TYPE_RETV must also
892
 * be defined. SEND_NONCONST_ARG2 must also be defined if ARG2
893
 * does not accept const.
894
 */
895
896
492k
#define sread(x, y, z) (ssize_t)recv((RECV_TYPE_ARG1)(x), \
897
492k
                                     (RECV_TYPE_ARG2)(y), \
898
492k
                                     (RECV_TYPE_ARG3)(z), \
899
492k
                                     (RECV_TYPE_ARG4)(0))
900
#else /* HAVE_RECV */
901
#ifndef sread
902
#error "Missing definition of macro sread"
903
#endif
904
#endif /* HAVE_RECV */
905
906
#ifdef __minix
907
/* Minix does not support send on TCP sockets */
908
#define swrite(x, y, z) (ssize_t)write((SEND_TYPE_ARG1)(x), \
909
                                       (SEND_TYPE_ARG2)CURL_UNCONST(y), \
910
                                       (SEND_TYPE_ARG3)(z))
911
#elif defined(HAVE_SEND)
912
#ifdef SEND_NONCONST_ARG2
913
#define swrite(x, y, z) (ssize_t)send((SEND_TYPE_ARG1)(x), \
914
                                      (SEND_TYPE_ARG2)CURL_UNCONST(y), \
915
                                      (SEND_TYPE_ARG3)(z), \
916
                                      (SEND_TYPE_ARG4)(SEND_4TH_ARG))
917
#else
918
49.7k
#define swrite(x, y, z) (ssize_t)send((SEND_TYPE_ARG1)(x), \
919
49.7k
                                      (const SEND_TYPE_ARG2)(y), \
920
49.7k
                                      (SEND_TYPE_ARG3)(z), \
921
49.7k
                                      (SEND_TYPE_ARG4)(SEND_4TH_ARG))
922
#endif /* SEND_NONCONST_ARG2 */
923
#else /* HAVE_SEND */
924
#ifndef swrite
925
#error "Missing definition of macro swrite"
926
#endif
927
#endif /* HAVE_SEND */
928
929
/*
930
 * Function-like macro definition used to close a socket.
931
 */
932
#ifdef HAVE_CLOSESOCKET
933
#  define CURL_SCLOSE(x)  closesocket(x)
934
#elif defined(HAVE_CLOSESOCKET_CAMEL)
935
#  define CURL_SCLOSE(x)  CloseSocket(x)
936
#elif defined(MSDOS)  /* Watt-32 */
937
#  define CURL_SCLOSE(x)  close_s(x)
938
#elif defined(USE_LWIPSOCK)
939
#  define CURL_SCLOSE(x)  lwip_close(x)
940
#else
941
42.4k
#  define CURL_SCLOSE(x)  close(x)
942
#endif
943
944
/*
945
 * Stack-independent version of fcntl() on sockets:
946
 */
947
#ifdef USE_LWIPSOCK
948
#  define sfcntl  lwip_fcntl
949
#else
950
20.9k
#  define sfcntl  fcntl
951
#endif
952
953
/*
954
 * 'bool' stuff compatible with HP-UX headers.
955
 */
956
#if defined(__hpux) && !defined(HAVE_BOOL_T)
957
typedef int bool;
958
#  define false 0
959
#  define true 1
960
#  define HAVE_BOOL_T
961
#endif
962
963
/*
964
 * 'bool' exists on platforms with <stdbool.h>, i.e. C99 platforms.
965
 * On non-C99 platforms there is no bool, so define an enum for that.
966
 * On C99 platforms 'false' and 'true' also exist. Enum uses a
967
 * global namespace though, so use bool_false and bool_true.
968
 */
969
#ifndef HAVE_BOOL_T
970
typedef enum {
971
  bool_false = 0,
972
  bool_true = 1
973
} bool;
974
975
/*
976
 * Use a define to let 'true' and 'false' use those enums. There
977
 * are currently no use of true and false in libcurl proper, but
978
 * there are some in the examples. This will cater for any later
979
 * code happening to use true and false.
980
 */
981
#  define false bool_false
982
#  define true  bool_true
983
#  define HAVE_BOOL_T
984
#endif
985
986
/* the type we use for storing a single boolean bit */
987
typedef unsigned int curl_bit;
988
#define BIT(x) curl_bit x:1
989
990
/*
991
 * Redefine TRUE and FALSE too, to catch current use. With this
992
 * change, 'bool found = 1' will give a warning on MIPSPro, but
993
 * 'bool found = TRUE' will not. Change tested on IRIX/MIPSPro,
994
 * AIX 5.1/Xlc, Tru64 5.1/cc, w/make test too.
995
 */
996
#ifndef TRUE
997
12.9M
#define TRUE true
998
#endif
999
#ifndef FALSE
1000
45.1M
#define FALSE false
1001
#endif
1002
1003
#include "curl_ctype.h"
1004
1005
#if defined(DEBUGBUILD) && defined(NDEBUG)
1006
#error "Debug-enabled builds cannot be combined with NDEBUG"
1007
#endif
1008
1009
/*
1010
 * Macro used to include code only in debug builds.
1011
 */
1012
#ifdef DEBUGBUILD
1013
626k
#define DEBUGF(x) x
1014
#else
1015
#define DEBUGF(x) do {} while(0)
1016
#endif
1017
1018
/*
1019
 * Macro used to include assertion code only in debug builds.
1020
 */
1021
#undef DEBUGASSERT
1022
#ifdef DEBUGBUILD
1023
#ifdef CURL_DEBUGASSERT
1024
/* External assertion handler for custom integrations */
1025
#define DEBUGASSERT(x) CURL_DEBUGASSERT(x)
1026
#else
1027
0
#define DEBUGASSERT(x) assert(x)
1028
#endif
1029
#else
1030
#define DEBUGASSERT(x) do {} while(0)
1031
#endif
1032
1033
/*
1034
 * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
1035
 * (or equivalent) on this platform to hide platform details to code using it.
1036
 */
1037
#ifdef USE_WINSOCK
1038
#define SOCKERRNO         ((int)WSAGetLastError())
1039
#define SET_SOCKERRNO(x)  WSASetLastError((int)(x))
1040
#else
1041
35.4k
#define SOCKERRNO         errno
1042
0
#define SET_SOCKERRNO(x)  (errno = (x))
1043
#endif
1044
1045
/*
1046
 * Portable error number symbolic names defined to Winsock error codes.
1047
 */
1048
#ifdef USE_WINSOCK
1049
#define SOCKEACCES        WSAEACCES
1050
#define SOCKEADDRINUSE    WSAEADDRINUSE
1051
#define SOCKEADDRNOTAVAIL WSAEADDRNOTAVAIL
1052
#define SOCKEAFNOSUPPORT  WSAEAFNOSUPPORT
1053
#define SOCKEBADF         WSAEBADF
1054
#define SOCKECONNREFUSED  WSAECONNREFUSED
1055
#define SOCKECONNRESET    WSAECONNRESET
1056
#define SOCKEINPROGRESS   WSAEINPROGRESS
1057
#define SOCKEINTR         WSAEINTR
1058
#define SOCKEINVAL        WSAEINVAL
1059
#define SOCKEISCONN       WSAEISCONN
1060
#define SOCKEMSGSIZE      WSAEMSGSIZE
1061
/* Use literal value to work around clang-tidy <=20 misreporting
1062
  'readability-uppercase-literal-suffix' with mingw-w64 headers */
1063
#define SOCKENOMEM        8L  /* WSA_NOT_ENOUGH_MEMORY */
1064
#define SOCKETIMEDOUT     WSAETIMEDOUT
1065
#define SOCKEWOULDBLOCK   WSAEWOULDBLOCK
1066
#else
1067
0
#define SOCKEACCES        EACCES
1068
0
#define SOCKEADDRINUSE    EADDRINUSE
1069
0
#define SOCKEADDRNOTAVAIL EADDRNOTAVAIL
1070
0
#define SOCKEAFNOSUPPORT  EAFNOSUPPORT
1071
#define SOCKEBADF         EBADF
1072
#define SOCKECONNREFUSED  ECONNREFUSED
1073
0
#define SOCKECONNRESET    ECONNRESET
1074
0
#define SOCKEINPROGRESS   EINPROGRESS
1075
28.0k
#define SOCKEINTR         EINTR
1076
#define SOCKEINVAL        EINVAL
1077
0
#define SOCKEISCONN       EISCONN
1078
#define SOCKEMSGSIZE      EMSGSIZE
1079
0
#define SOCKENOMEM        ENOMEM
1080
#ifdef ETIMEDOUT
1081
467
#define SOCKETIMEDOUT     ETIMEDOUT
1082
#endif
1083
34.9k
#define SOCKEWOULDBLOCK   EWOULDBLOCK
1084
#endif
1085
1086
/* The socket error may be EWOULDBLOCK or on some systems EAGAIN when
1087
   it returned due to its inability to send/read data without blocking.
1088
   We treat both error codes the same here. */
1089
#if !defined(USE_WINSOCK) && EAGAIN != SOCKEWOULDBLOCK
1090
#define SOCK_EAGAIN(e) ((e) == SOCKEWOULDBLOCK || (e) == EAGAIN)
1091
#else
1092
41.9k
#define SOCK_EAGAIN(e) ((e) == SOCKEWOULDBLOCK)
1093
#endif
1094
1095
/*
1096
 * Macro argv_item_t hides platform details to code using it.
1097
 */
1098
#ifdef __VMS
1099
#define argv_item_t  __char_ptr32
1100
#elif defined(_UNICODE)
1101
#define argv_item_t  wchar_t *
1102
#else
1103
#define argv_item_t  char *
1104
#endif
1105
1106
/*
1107
 * We use this ZERO_NULL to avoid picky compiler warnings,
1108
 * when assigning a NULL pointer to a function pointer var.
1109
 */
1110
177k
#define ZERO_NULL 0
1111
1112
/*
1113
 * Macros and functions to safely suppress warnings
1114
 */
1115
#include "curlx/warnless.h"
1116
1117
#ifdef _WIN32
1118
#  undef read
1119
#  define read(fd, buf, count)  (ssize_t)_read(fd, buf, curlx_uztoui(count))
1120
#  undef write
1121
#  define write(fd, buf, count) (ssize_t)_write(fd, buf, curlx_uztoui(count))
1122
/* Avoid VS2005+ _CRT_NONSTDC_NO_DEPRECATE warnings about non-portable funcs */
1123
#  undef fileno
1124
#  define fileno(fh) _fileno(fh)
1125
#  undef unlink
1126
#  define unlink(fn) _unlink(fn)
1127
#  undef isatty
1128
#  define isatty(fd) _isatty(fd)
1129
#endif
1130
1131
/*
1132
 * Definition of our NOP statement Object-like macro
1133
 */
1134
#ifndef Curl_nop_stmt
1135
387k
#define Curl_nop_stmt do {} while(0)
1136
#endif
1137
1138
/*
1139
 * Ensure that Winsock and lwIP TCP/IP stacks are not mixed.
1140
 */
1141
#if defined(__LWIP_OPT_H__) || defined(LWIP_HDR_OPT_H)
1142
#  if defined(SOCKET) || defined(USE_WINSOCK)
1143
#    error "Winsock and lwIP TCP/IP stack definitions shall not coexist"
1144
#  endif
1145
#endif
1146
1147
/*
1148
 * shutdown() flags for systems that do not define them
1149
 */
1150
#ifndef SHUT_RD
1151
#define SHUT_RD 0x00
1152
#endif
1153
1154
#ifndef SHUT_WR
1155
#define SHUT_WR 0x01
1156
#endif
1157
1158
#ifndef SHUT_RDWR
1159
#define SHUT_RDWR 0x02
1160
#endif
1161
1162
/* Define S_ISREG if not defined by system headers, e.g. MSVC */
1163
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
1164
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
1165
#endif
1166
1167
/* Define S_ISDIR if not defined by system headers, e.g. MSVC */
1168
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
1169
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1170
#endif
1171
1172
/* For MSVC (all versions as of VS2022) */
1173
#ifndef STDIN_FILENO
1174
#define STDIN_FILENO  fileno(stdin)
1175
#endif
1176
#ifndef STDOUT_FILENO
1177
#define STDOUT_FILENO  fileno(stdout)
1178
#endif
1179
#ifndef STDERR_FILENO
1180
#define STDERR_FILENO  fileno(stderr)
1181
#endif
1182
1183
/* Since O_BINARY is used in bitmasks, setting it to zero makes it usable in
1184
   source code but yet it does not ruin anything */
1185
#ifdef _O_BINARY  /* for _WIN32 || MSDOS */
1186
#define CURL_O_BINARY _O_BINARY
1187
#elif defined(O_BINARY)  /* __CYGWIN__ */
1188
#define CURL_O_BINARY O_BINARY
1189
#else
1190
0
#define CURL_O_BINARY 0
1191
#endif
1192
1193
/* Requires io.h when available */
1194
#ifdef MSDOS
1195
#define CURL_BINMODE(stream) (void)setmode(fileno(stream), CURL_O_BINARY)
1196
#elif defined(_WIN32) || defined(__CYGWIN__)
1197
#define CURL_BINMODE(stream) (void)_setmode(fileno(stream), CURL_O_BINARY)
1198
#else
1199
#define CURL_BINMODE(stream) (void)(stream)
1200
#endif
1201
1202
/* In Windows the default file mode is text but an application can override it.
1203
   Therefore we specify it explicitly. https://github.com/curl/curl/pull/258 */
1204
#if defined(_WIN32) || defined(MSDOS)
1205
#define FOPEN_READTEXT   "rt"
1206
#define FOPEN_WRITETEXT  "wt"
1207
#define FOPEN_APPENDTEXT "at"
1208
#elif defined(__CYGWIN__)
1209
/* Cygwin has specific behavior we need to address when _WIN32 is not defined.
1210
   https://cygwin.com/cygwin-ug-net/using-textbinary.html
1211
   For write we want our output to have line endings of LF and be compatible
1212
   with other Cygwin utilities. For read we want to handle input that may have
1213
   line endings either CRLF or LF so 't' is appropriate. */
1214
#define FOPEN_READTEXT   "rt"
1215
#define FOPEN_WRITETEXT  "w"
1216
#define FOPEN_APPENDTEXT "a"
1217
#else
1218
#define FOPEN_READTEXT   "r"
1219
0
#define FOPEN_WRITETEXT  "w"
1220
#define FOPEN_APPENDTEXT "a"
1221
#endif
1222
1223
/* for systems that do not detect this in configure */
1224
#ifndef CURL_SA_FAMILY_T
1225
#  ifdef USE_WINSOCK
1226
#    define CURL_SA_FAMILY_T ADDRESS_FAMILY
1227
#  elif defined(HAVE_SA_FAMILY_T)
1228
0
#    define CURL_SA_FAMILY_T sa_family_t
1229
#  elif defined(__AMIGA__)
1230
#    define CURL_SA_FAMILY_T unsigned char
1231
#  else
1232
/* use a sensible default */
1233
#    define CURL_SA_FAMILY_T unsigned short
1234
#  endif
1235
#endif
1236
1237
/* Some convenience macros to get the larger/smaller value out of two given.
1238
   We prefix with CURL to prevent name collisions. */
1239
30.7k
#define CURLMAX(x, y) ((x) > (y) ? (x) : (y))
1240
858k
#define CURLMIN(x, y) ((x) < (y) ? (x) : (y))
1241
1242
/* Convenience macro to provide the length of a string literal size without
1243
   the null-terminator. Equivalent to strlen() for constant strings. */
1244
6.89M
#define CURL_CSTRLEN(x) (sizeof(x) - 1)
1245
1246
/* A convenience macro to provide both the string literal and the length of
1247
   the string literal in one go, useful for functions that take "string,len"
1248
   as their argument */
1249
6.80M
#define STRCONST(x) x, CURL_CSTRLEN(x)
1250
1251
3.33M
#define CURL_ARRAYSIZE(A) (sizeof(A) / sizeof((A)[0]))
1252
1253
/* Buffer size for error messages retrieved via
1254
   curlx_strerror() and Curl_sspi_strerror() */
1255
#define STRERROR_LEN 256
1256
1257
#ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */
1258
/*
1259
 * The following memory function replacement typedef's are COPIED from
1260
 * curl/curl.h and MUST match the originals. We copy them to avoid having to
1261
 * include curl/curl.h here. We avoid that include since it includes stdio.h
1262
 * and other headers that may get messed up with defines done here.
1263
 */
1264
typedef void *(*curl_malloc_callback)(size_t size);
1265
typedef void (*curl_free_callback)(void *ptr);
1266
typedef void *(*curl_realloc_callback)(void *ptr, size_t size);
1267
typedef char *(*curl_strdup_callback)(const char *str);
1268
typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size);
1269
#define CURL_DID_MEMORY_FUNC_TYPEDEFS
1270
#endif
1271
1272
extern curl_malloc_callback Curl_cmalloc;
1273
extern curl_free_callback Curl_cfree;
1274
extern curl_realloc_callback Curl_crealloc;
1275
extern curl_strdup_callback Curl_cstrdup;
1276
extern curl_calloc_callback Curl_ccalloc;
1277
1278
/*
1279
 * curlx_safefree() defined as a macro to allow MemoryTracking feature
1280
 * to log free() calls at same location where curlx_safefree() is used.
1281
 * This macro also assigns NULL to given pointer when free'd.
1282
 */
1283
#define curlx_safefree(ptr) \
1284
8.70M
  do {                      \
1285
8.65M
    curlx_free(ptr);        \
1286
8.65M
    (ptr) = NULL;           \
1287
8.65M
  } while(0)
1288
1289
#include <curl/curl.h> /* for CURL_EXTERN, curl_socket_t, mprintf.h */
1290
1291
#ifdef DEBUGBUILD
1292
#define CURL_MEMDEBUG
1293
#endif
1294
1295
#ifdef CURL_MEMDEBUG
1296
#ifdef __clang__
1297
#  define ALLOC_FUNC         __attribute__((__malloc__))
1298
#  if __clang_major__ >= 4
1299
#  define ALLOC_SIZE(s)      __attribute__((__alloc_size__(s)))
1300
#  define ALLOC_SIZE2(n, s)  __attribute__((__alloc_size__(n, s)))
1301
#  else
1302
#  define ALLOC_SIZE(s)
1303
#  define ALLOC_SIZE2(n, s)
1304
#  endif
1305
#elif defined(__GNUC__) && __GNUC__ >= 3
1306
#  define ALLOC_FUNC         __attribute__((__malloc__))
1307
#  define ALLOC_SIZE(s)      __attribute__((__alloc_size__(s)))
1308
#  define ALLOC_SIZE2(n, s)  __attribute__((__alloc_size__(n, s)))
1309
#elif defined(_MSC_VER)
1310
#  define ALLOC_FUNC         __declspec(restrict)
1311
#  define ALLOC_SIZE(s)
1312
#  define ALLOC_SIZE2(n, s)
1313
#else
1314
#  define ALLOC_FUNC
1315
#  define ALLOC_SIZE(s)
1316
#  define ALLOC_SIZE2(n, s)
1317
#endif
1318
1319
extern FILE *curl_dbg_logfile;
1320
1321
/* memory functions */
1322
CURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source);
1323
CURL_EXTERN ALLOC_FUNC ALLOC_SIZE(1)
1324
  void *curl_dbg_malloc(size_t wantedsize, int line, const char *source);
1325
CURL_EXTERN ALLOC_FUNC ALLOC_SIZE2(1, 2)
1326
  void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
1327
                        int line, const char *source);
1328
CURL_EXTERN ALLOC_SIZE(2)
1329
  void *curl_dbg_realloc(void *ptr, size_t wantedsize, int line,
1330
                         const char *source);
1331
CURL_EXTERN ALLOC_FUNC
1332
  char *curl_dbg_strdup(const char *str, int line, const char *source);
1333
#if defined(_WIN32) && defined(UNICODE)
1334
CURL_EXTERN ALLOC_FUNC
1335
  wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, const char *source);
1336
#endif
1337
1338
CURL_EXTERN void curl_dbg_memdebug(const char *logname);
1339
CURL_EXTERN void curl_dbg_memlimit(long limit);
1340
CURL_EXTERN void curl_dbg_log(const char *format, ...) CURL_PRINTF(1, 2);
1341
1342
/* file descriptor manipulators */
1343
CURL_EXTERN curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
1344
                                          int line, const char *source);
1345
CURL_EXTERN void curl_dbg_mark_sclose(curl_socket_t sockfd,
1346
                                      int line, const char *source);
1347
CURL_EXTERN int curl_dbg_sclose(curl_socket_t sockfd,
1348
                                int line, const char *source);
1349
CURL_EXTERN curl_socket_t curl_dbg_accept(curl_socket_t s,
1350
                                          void *saddr, void *saddrlen,
1351
                                          int line, const char *source);
1352
#ifdef HAVE_ACCEPT4
1353
CURL_EXTERN curl_socket_t curl_dbg_accept4(curl_socket_t s, void *saddr,
1354
                                           void *saddrlen, int flags,
1355
                                           int line, const char *source);
1356
#endif
1357
#ifdef HAVE_SOCKETPAIR
1358
CURL_EXTERN int curl_dbg_socketpair(int domain, int type, int protocol,
1359
                                    curl_socket_t socket_vector[2],
1360
                                    int line, const char *source);
1361
#endif
1362
1363
/* FILE functions */
1364
CURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source);
1365
CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
1366
                                            int line, const char *source);
1367
CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_freopen(const char *file,
1368
                                              const char *mode, FILE *fh,
1369
                                              int line, const char *source);
1370
CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
1371
                                             int line, const char *source);
1372
1373
42.4k
#define sclose(sockfd) curl_dbg_sclose(sockfd, __LINE__, __FILE__)
1374
#define fake_sclose(sockfd) curl_dbg_mark_sclose(sockfd, __LINE__, __FILE__)
1375
1376
#define CURL_GETADDRINFO(host, serv, hint, res) \
1377
0
  curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
1378
#define CURL_FREEADDRINFO(data) \
1379
0
  curl_dbg_freeaddrinfo(data, __LINE__, __FILE__)
1380
#define CURL_SOCKET(domain, type, protocol) \
1381
21.4k
  curl_dbg_socket((int)(domain), type, protocol, __LINE__, __FILE__)
1382
#ifdef HAVE_SOCKETPAIR
1383
#define CURL_SOCKETPAIR(domain, type, protocol, socket_vector) \
1384
  curl_dbg_socketpair((int)(domain), type, protocol, socket_vector, \
1385
                      __LINE__, __FILE__)
1386
#endif
1387
#define CURL_ACCEPT(sock, addr, len) \
1388
  curl_dbg_accept(sock, addr, len, __LINE__, __FILE__)
1389
#ifdef HAVE_ACCEPT4
1390
#define CURL_ACCEPT4(sock, addr, len, flags) \
1391
0
  curl_dbg_accept4(sock, addr, len, flags, __LINE__, __FILE__)
1392
#endif
1393
1394
#else /* !CURL_MEMDEBUG */
1395
1396
#define sclose(x) CURL_SCLOSE(x)
1397
#define fake_sclose(x) Curl_nop_stmt
1398
1399
#define CURL_GETADDRINFO getaddrinfo
1400
#define CURL_FREEADDRINFO freeaddrinfo
1401
#define CURL_SOCKET socket
1402
#ifdef HAVE_SOCKETPAIR
1403
#define CURL_SOCKETPAIR socketpair
1404
#endif
1405
#define CURL_ACCEPT accept
1406
#ifdef HAVE_ACCEPT4
1407
#define CURL_ACCEPT4 accept4
1408
#endif
1409
1410
#endif /* CURL_MEMDEBUG */
1411
1412
/* Allocator macros */
1413
1414
#ifdef _WIN32
1415
#define CURLX_STRDUP_LOW _strdup
1416
#else
1417
1
#define CURLX_STRDUP_LOW strdup
1418
#endif
1419
1420
#ifdef CURL_MEMDEBUG
1421
1422
257k
#define curlx_strdup(ptr)          curl_dbg_strdup(ptr, __LINE__, __FILE__)
1423
1.51M
#define curlx_malloc(size)         curl_dbg_malloc(size, __LINE__, __FILE__)
1424
#define curlx_calloc(nbelem, size) \
1425
1.69M
  curl_dbg_calloc(nbelem, size, __LINE__, __FILE__)
1426
#define curlx_realloc(ptr, size) \
1427
884k
  curl_dbg_realloc(ptr, size, __LINE__, __FILE__)
1428
12.9M
#define curlx_free(ptr)            curl_dbg_free(ptr, __LINE__, __FILE__)
1429
1430
#ifdef _WIN32
1431
#ifdef UNICODE
1432
#define curlx_tcsdup(ptr)          curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
1433
#else
1434
#define curlx_tcsdup               curlx_strdup
1435
#endif
1436
#endif /* _WIN32 */
1437
1438
#else /* !CURL_MEMDEBUG */
1439
1440
#ifdef BUILDING_LIBCURL
1441
#define curlx_strdup               Curl_cstrdup
1442
#define curlx_malloc               Curl_cmalloc
1443
#define curlx_calloc               Curl_ccalloc
1444
#define curlx_realloc              Curl_crealloc
1445
#define curlx_free                 Curl_cfree
1446
#else /* !BUILDING_LIBCURL */
1447
#define curlx_strdup               CURLX_STRDUP_LOW
1448
#define curlx_malloc               malloc
1449
#define curlx_calloc               calloc
1450
#define curlx_realloc              realloc
1451
#define curlx_free                 free
1452
#endif /* BUILDING_LIBCURL */
1453
1454
#ifdef _WIN32
1455
#ifdef UNICODE
1456
#define curlx_tcsdup               curlx_wcsdup
1457
#else
1458
#define curlx_tcsdup               curlx_strdup
1459
#endif
1460
#endif /* _WIN32 */
1461
1462
#endif /* CURL_MEMDEBUG */
1463
1464
/* Some versions of the Android NDK is missing the declaration */
1465
#if defined(HAVE_GETPWUID_R) && \
1466
  defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
1467
struct passwd;
1468
int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf,
1469
               size_t buflen, struct passwd **result);
1470
#endif
1471
1472
#ifdef UNITTESTS
1473
#define UNITTEST
1474
#else
1475
#define UNITTEST static
1476
#endif
1477
1478
#ifdef USE_NGHTTP2
1479
#define USE_HTTP2
1480
#endif
1481
1482
#if (defined(USE_NGTCP2) && defined(USE_NGHTTP3)) || defined(USE_QUICHE)
1483
1484
#ifdef CURL_WITH_MULTI_SSL
1485
#error "MultiSSL combined with QUIC is not supported"
1486
#endif
1487
1488
#define USE_HTTP3
1489
#endif
1490
1491
/* WebAssembly builds have TCP_NODELAY, but runtime support is missing. */
1492
#ifndef __EMSCRIPTEN__
1493
#define CURL_TCP_NODELAY_SUPPORTED
1494
#endif
1495
1496
/* Certain Windows implementations are not aligned with what curl expects,
1497
   so always use the local one on this platform. E.g. the mingw-w64
1498
   implementation can return wrong results for non-ASCII inputs. */
1499
#if defined(HAVE_BASENAME) && defined(_WIN32)
1500
#undef HAVE_BASENAME
1501
#endif
1502
1503
#if defined(USE_UNIX_SOCKETS) && defined(_WIN32)
1504
/* Offered by mingw-w64 v10+, MS SDK 10.0.16299.0/VS2017 15.4+ */
1505
#if defined(__MINGW32__) && (__MINGW64_VERSION_MAJOR >= 10)
1506
#  include <afunix.h>
1507
#elif !defined(UNIX_PATH_MAX) /* Replicate logic present in afunix.h */
1508
#  define UNIX_PATH_MAX 108
1509
/* !checksrc! disable TYPEDEFSTRUCT 1 */
1510
typedef struct sockaddr_un {
1511
  CURL_SA_FAMILY_T sun_family;
1512
  char sun_path[UNIX_PATH_MAX];
1513
} SOCKADDR_UN, *PSOCKADDR_UN;
1514
#endif
1515
#endif
1516
1517
#ifdef USE_OPENSSL
1518
/* OpenSSL 3 marks these functions deprecated but we have no replacements (yet)
1519
   so tell the compiler to not warn for them:
1520
   - DES_* (for NTLM)
1521
   - EVP_PKEY_get1_RSA, RSA_flags, RSA_free (auto-skipped for OpenSSL built
1522
     with no-deprecated) */
1523
#  define OPENSSL_SUPPRESS_DEPRECATED
1524
#  ifdef _WIN32
1525
/* Silence LibreSSL warnings about wincrypt.h collision. Works in 3.8.2+ */
1526
#    ifndef LIBRESSL_DISABLE_OVERRIDE_WINCRYPT_DEFINES_WARNING
1527
#    define LIBRESSL_DISABLE_OVERRIDE_WINCRYPT_DEFINES_WARNING
1528
#    endif
1529
#  endif
1530
#endif
1531
1532
#ifdef CURL_INLINE
1533
/* 'CURL_INLINE' defined, use as-is */
1534
#elif defined(inline)
1535
#  define CURL_INLINE inline /* 'inline' defined, assumed correct */
1536
#elif defined(__cplusplus)
1537
/* The code is compiled with C++ compiler.
1538
   C++ always supports 'inline'. */
1539
#  define CURL_INLINE inline /* 'inline' keyword supported */
1540
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
1541
/* C99 (and later) supports 'inline' keyword */
1542
#  define CURL_INLINE inline /* 'inline' keyword supported */
1543
#elif defined(__GNUC__) && __GNUC__ >= 3
1544
/* GCC supports '__inline__' as an extension */
1545
#  define CURL_INLINE __inline__
1546
#elif defined(_MSC_VER)
1547
#  define CURL_INLINE __inline
1548
#else
1549
/* Probably 'inline' is not supported by compiler.
1550
   Define to the empty string to be on the safe side. */
1551
#  define CURL_INLINE /* empty */
1552
#endif
1553
1554
/* Detect if compiler supports C99 variadic macros */
1555
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
1556
  defined(_MSC_VER)
1557
#define CURL_HAVE_MACRO_VARARG
1558
#endif
1559
1560
#if !defined(CURL_HAVE_MACRO_VARARG) || \
1561
  (defined(CURL_HAVE_MACRO_VARARG) && !defined(CURL_DISABLE_VERBOSE_STRINGS))
1562
#define CURLVERBOSE
1563
289k
#define VERBOSE(x) x
1564
387k
#define NOVERBOSE(x) Curl_nop_stmt
1565
#else
1566
#define VERBOSE(x) Curl_nop_stmt
1567
#define NOVERBOSE(x) x
1568
#endif
1569
1570
/* For FreeBSD it is included from curl/curl.h */
1571
#if defined(__DragonFly__) || defined(__OpenBSD__) || defined(__NetBSD__)
1572
#include <sys/param.h>  /* for __DragonFly_version, OpenBSD,
1573
                           __NetBSD_Version__ */
1574
#endif
1575
1576
/* NetBSD before 6.1 did not set SS_NBIO for SOCK_NONBLOCK. */
1577
#if defined(SOCK_NONBLOCK) && \
1578
  (!defined(__NetBSD__) || (__NetBSD_Version__ >= 601000000))
1579
#define CURL_USE_SOCK_NONBLOCK
1580
#endif
1581
1582
#ifndef _CURL_LOCAL_MEMZERO /* to be removed after a couple of releases */
1583
#ifdef _WIN32
1584
#if defined(_MSC_VER) && defined(NTDDI_VERSION) && \
1585
  (NTDDI_VERSION >= 0x0A000010) /* MS SDK 10.0.26100.0+ */
1586
#pragma comment(lib, "volatileaccessu.lib")
1587
#define curlx_memzero_low(buf, size)  SecureZeroMemory2(buf, size)
1588
#else
1589
#define curlx_memzero_low(buf, size)  SecureZeroMemory(buf, size)
1590
#endif
1591
#elif defined(HAVE_MEMSET_S)
1592
#define curlx_memzero_low(buf, size)  (void)memset_s(buf, size, 0, size)
1593
#elif defined(HAVE_MEMSET_EXPLICIT)
1594
#define curlx_memzero_low(buf, size)  (void)memset_explicit(buf, 0, size)
1595
#elif defined(__CYGWIN__) || \
1596
  (defined(__NEWLIB__) && !defined(__CLIB2__)) || \
1597
  (defined(__GLIBC__) && \
1598
    (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))) || \
1599
  (defined(__DragonFly__) && __DragonFly_version >= 500600 /* 5.6+ */) || \
1600
  (defined(__FreeBSD__) && __FreeBSD_version >= 1100037 /* 11.0+ */) || \
1601
  (defined(__OpenBSD__) && OpenBSD >= 201405 /* 5.5+ */)
1602
71.3k
#define curlx_memzero_low(buf, size)  explicit_bzero(buf, size)
1603
#elif defined(__NetBSD__) && __NetBSD_Version__ >= 702000000 /* 7.2+ */
1604
#define curlx_memzero_low(buf, size)  (void)explicit_memset(buf, 0, size)
1605
#endif
1606
#endif /* !_CURL_LOCAL_MEMZERO */
1607
1608
#ifndef curlx_memzero_low
1609
#define USE_CURLX_MEMZERO
1610
void curlx_memzero_low(void *buf, size_t size);
1611
#endif
1612
void curlx_memzero(void *buf, size_t size);
1613
void curlx_strzero(void *buf);
1614
1615
#endif /* HEADER_CURL_SETUP_H */