Coverage Report

Created: 2026-07-30 07:03

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