Coverage Report

Created: 2026-09-01 06:58

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