Coverage Report

Created: 2026-08-13 07:18

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
0
#  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
0
#  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
0
#  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
0
#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
607k
#define TRUE true
1054
#endif
1055
#ifndef FALSE
1056
7.68M
#define FALSE false
1057
#endif
1058
1059
#include "curl_ctype.h"
1060
1061
#if defined(DEBUGBUILD) && defined(NDEBUG)
1062
#error "Debug-enabled builds cannot be combined with NDEBUG"
1063
#endif
1064
1065
/*
1066
 * Macro used to include code only in debug builds.
1067
 */
1068
#ifdef DEBUGBUILD
1069
0
#define DEBUGF(x) x
1070
#else
1071
#define DEBUGF(x) do {} while(0)
1072
#endif
1073
1074
/*
1075
 * Macro used to include assertion code only in debug builds.
1076
 */
1077
#undef DEBUGASSERT
1078
#ifdef DEBUGBUILD
1079
#ifdef CURL_DEBUGASSERT
1080
/* External assertion handler for custom integrations */
1081
#define DEBUGASSERT(x) CURL_DEBUGASSERT(x)
1082
#else
1083
0
#define DEBUGASSERT(x) assert(x)
1084
#endif
1085
#else
1086
#define DEBUGASSERT(x) do {} while(0)
1087
#endif
1088
1089
/*
1090
 * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
1091
 * (or equivalent) on this platform to hide platform details to code using it.
1092
 */
1093
#ifdef USE_WINSOCK
1094
#define SOCKERRNO         ((int)WSAGetLastError())
1095
#define SET_SOCKERRNO(x)  WSASetLastError((int)(x))
1096
#else
1097
0
#define SOCKERRNO         errno
1098
0
#define SET_SOCKERRNO(x)  (errno = (x))
1099
#endif
1100
1101
/*
1102
 * Portable error number symbolic names defined to Winsock error codes.
1103
 */
1104
#ifdef USE_WINSOCK
1105
#define SOCKEACCES        WSAEACCES
1106
#define SOCKEADDRINUSE    WSAEADDRINUSE
1107
#define SOCKEADDRNOTAVAIL WSAEADDRNOTAVAIL
1108
#define SOCKEAFNOSUPPORT  WSAEAFNOSUPPORT
1109
#define SOCKEBADF         WSAEBADF
1110
#define SOCKECONNREFUSED  WSAECONNREFUSED
1111
#define SOCKECONNRESET    WSAECONNRESET
1112
#define SOCKEINPROGRESS   WSAEINPROGRESS
1113
#define SOCKEINTR         WSAEINTR
1114
#define SOCKEINVAL        WSAEINVAL
1115
#define SOCKEISCONN       WSAEISCONN
1116
#define SOCKEMSGSIZE      WSAEMSGSIZE
1117
/* Use literal value to work around clang-tidy <=20 misreporting
1118
  'readability-uppercase-literal-suffix' with mingw-w64 headers */
1119
#define SOCKENOMEM        8L  /* WSA_NOT_ENOUGH_MEMORY */
1120
#define SOCKETIMEDOUT     WSAETIMEDOUT
1121
#define SOCKEWOULDBLOCK   WSAEWOULDBLOCK
1122
#else
1123
0
#define SOCKEACCES        EACCES
1124
0
#define SOCKEADDRINUSE    EADDRINUSE
1125
0
#define SOCKEADDRNOTAVAIL EADDRNOTAVAIL
1126
0
#define SOCKEAFNOSUPPORT  EAFNOSUPPORT
1127
#define SOCKEBADF         EBADF
1128
#define SOCKECONNREFUSED  ECONNREFUSED
1129
0
#define SOCKECONNRESET    ECONNRESET
1130
0
#define SOCKEINPROGRESS   EINPROGRESS
1131
0
#define SOCKEINTR         EINTR
1132
#define SOCKEINVAL        EINVAL
1133
0
#define SOCKEISCONN       EISCONN
1134
#define SOCKEMSGSIZE      EMSGSIZE
1135
0
#define SOCKENOMEM        ENOMEM
1136
#ifdef ETIMEDOUT
1137
0
#define SOCKETIMEDOUT     ETIMEDOUT
1138
#endif
1139
0
#define SOCKEWOULDBLOCK   EWOULDBLOCK
1140
#endif
1141
1142
/* The socket error may be EWOULDBLOCK or on some systems EAGAIN when
1143
   it returned due to its inability to send/read data without blocking.
1144
   We treat both error codes the same here. */
1145
#if !defined(USE_WINSOCK) && EAGAIN != SOCKEWOULDBLOCK
1146
#define SOCK_EAGAIN(e) ((e) == SOCKEWOULDBLOCK || (e) == EAGAIN)
1147
#else
1148
0
#define SOCK_EAGAIN(e) ((e) == SOCKEWOULDBLOCK)
1149
#endif
1150
1151
/*
1152
 * Macro argv_item_t hides platform details to code using it.
1153
 */
1154
#ifdef __VMS
1155
#define argv_item_t  __char_ptr32
1156
#elif defined(_UNICODE)
1157
#define argv_item_t  wchar_t *
1158
#else
1159
#define argv_item_t  char *
1160
#endif
1161
1162
/*
1163
 * We use this ZERO_NULL to avoid picky compiler warnings,
1164
 * when assigning a NULL pointer to a function pointer var.
1165
 */
1166
0
#define ZERO_NULL 0
1167
1168
/*
1169
 * Macros and functions to safely suppress warnings
1170
 */
1171
#include "curlx/warnless.h"
1172
1173
#ifdef _WIN32
1174
#  undef read
1175
#  define read(fd, buf, count)  (ssize_t)_read(fd, buf, curlx_uztoui(count))
1176
#  undef write
1177
#  define write(fd, buf, count) (ssize_t)_write(fd, buf, curlx_uztoui(count))
1178
/* Avoid VS2005+ _CRT_NONSTDC_NO_DEPRECATE warnings about non-portable funcs */
1179
#  undef fileno
1180
#  define fileno(fh) _fileno(fh)
1181
#  undef unlink
1182
#  define unlink(fn) _unlink(fn)
1183
#  undef isatty
1184
#  define isatty(fd) _isatty(fd)
1185
#endif
1186
1187
/*
1188
 * Definition of our NOP statement Object-like macro
1189
 */
1190
#ifndef Curl_nop_stmt
1191
0
#define Curl_nop_stmt do {} while(0)
1192
#endif
1193
1194
/*
1195
 * Ensure that Winsock and lwIP TCP/IP stacks are not mixed.
1196
 */
1197
#if defined(__LWIP_OPT_H__) || defined(LWIP_HDR_OPT_H)
1198
#  if defined(SOCKET) || defined(USE_WINSOCK)
1199
#    error "Winsock and lwIP TCP/IP stack definitions shall not coexist"
1200
#  endif
1201
#endif
1202
1203
/*
1204
 * shutdown() flags for systems that do not define them
1205
 */
1206
#ifndef SHUT_RD
1207
#define SHUT_RD 0x00
1208
#endif
1209
1210
#ifndef SHUT_WR
1211
#define SHUT_WR 0x01
1212
#endif
1213
1214
#ifndef SHUT_RDWR
1215
#define SHUT_RDWR 0x02
1216
#endif
1217
1218
/* Define S_ISREG if not defined by system headers, e.g. MSVC */
1219
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
1220
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
1221
#endif
1222
1223
/* Define S_ISDIR if not defined by system headers, e.g. MSVC */
1224
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
1225
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1226
#endif
1227
1228
/* For MSVC (all versions as of VS2022) */
1229
#ifndef STDIN_FILENO
1230
#define STDIN_FILENO  fileno(stdin)
1231
#endif
1232
#ifndef STDOUT_FILENO
1233
#define STDOUT_FILENO  fileno(stdout)
1234
#endif
1235
#ifndef STDERR_FILENO
1236
#define STDERR_FILENO  fileno(stderr)
1237
#endif
1238
1239
/* Since O_BINARY is used in bitmasks, setting it to zero makes it usable in
1240
   source code but yet it does not ruin anything */
1241
#ifdef _O_BINARY  /* for _WIN32 || MSDOS */
1242
#define CURL_O_BINARY _O_BINARY
1243
#elif defined(O_BINARY)  /* __CYGWIN__ */
1244
#define CURL_O_BINARY O_BINARY
1245
#else
1246
0
#define CURL_O_BINARY 0
1247
#endif
1248
1249
/* Requires io.h when available */
1250
#ifdef MSDOS
1251
#define CURL_BINMODE(stream) (void)setmode(fileno(stream), CURL_O_BINARY)
1252
#elif defined(_WIN32) || defined(__CYGWIN__)
1253
#define CURL_BINMODE(stream) (void)_setmode(fileno(stream), CURL_O_BINARY)
1254
#else
1255
#define CURL_BINMODE(stream) (void)(stream)
1256
#endif
1257
1258
/* In Windows the default file mode is text but an application can override it.
1259
   Therefore we specify it explicitly. https://github.com/curl/curl/pull/258 */
1260
#if defined(_WIN32) || defined(MSDOS)
1261
#define FOPEN_READTEXT   "rt"
1262
#define FOPEN_WRITETEXT  "wt"
1263
#define FOPEN_APPENDTEXT "at"
1264
#elif defined(__CYGWIN__)
1265
/* Cygwin has specific behavior we need to address when _WIN32 is not defined.
1266
   https://cygwin.com/cygwin-ug-net/using-textbinary.html
1267
   For write we want our output to have line endings of LF and be compatible
1268
   with other Cygwin utilities. For read we want to handle input that may have
1269
   line endings either CRLF or LF so 't' is appropriate. */
1270
#define FOPEN_READTEXT   "rt"
1271
#define FOPEN_WRITETEXT  "w"
1272
#define FOPEN_APPENDTEXT "a"
1273
#else
1274
#define FOPEN_READTEXT   "r"
1275
0
#define FOPEN_WRITETEXT  "w"
1276
#define FOPEN_APPENDTEXT "a"
1277
#endif
1278
1279
/* for systems that do not detect this in configure */
1280
#ifndef CURL_SA_FAMILY_T
1281
#  ifdef USE_WINSOCK
1282
#    define CURL_SA_FAMILY_T ADDRESS_FAMILY
1283
#  elif defined(HAVE_SA_FAMILY_T)
1284
0
#    define CURL_SA_FAMILY_T sa_family_t
1285
#  elif defined(__AMIGA__)
1286
#    define CURL_SA_FAMILY_T unsigned char
1287
#  else
1288
/* use a sensible default */
1289
#    define CURL_SA_FAMILY_T unsigned short
1290
#  endif
1291
#endif
1292
1293
/* Some convenience macros to get the larger/smaller value out of two given.
1294
   We prefix with CURL to prevent name collisions. */
1295
0
#define CURLMAX(x, y) ((x) > (y) ? (x) : (y))
1296
0
#define CURLMIN(x, y) ((x) < (y) ? (x) : (y))
1297
1298
/* Convenience macro to provide the length of a string literal size without
1299
   the null-terminator. Equivalent to strlen() for constant strings. */
1300
8.06k
#define CURL_CSTRLEN(x) (sizeof(x) - 1)
1301
1302
/* A convenience macro to provide both the string literal and the length of
1303
   the string literal in one go, useful for functions that take "string,len"
1304
   as their argument */
1305
8.06k
#define STRCONST(x) x, CURL_CSTRLEN(x)
1306
1307
0
#define CURL_ARRAYSIZE(A) (sizeof(A) / sizeof((A)[0]))
1308
1309
/* Buffer size for error messages retrieved via
1310
   curlx_strerror() and Curl_sspi_strerror() */
1311
#define STRERROR_LEN 256
1312
1313
#ifndef CURL_DID_MEMORY_FUNC_TYPEDEFS /* only if not already done */
1314
/*
1315
 * The following memory function replacement typedef's are COPIED from
1316
 * curl/curl.h and MUST match the originals. We copy them to avoid having to
1317
 * include curl/curl.h here. We avoid that include since it includes stdio.h
1318
 * and other headers that may get messed up with defines done here.
1319
 */
1320
typedef void *(*curl_malloc_callback)(size_t size);
1321
typedef void (*curl_free_callback)(void *ptr);
1322
typedef void *(*curl_realloc_callback)(void *ptr, size_t size);
1323
typedef char *(*curl_strdup_callback)(const char *str);
1324
typedef void *(*curl_calloc_callback)(size_t nmemb, size_t size);
1325
#define CURL_DID_MEMORY_FUNC_TYPEDEFS
1326
#endif
1327
1328
extern curl_malloc_callback Curl_cmalloc;
1329
extern curl_free_callback Curl_cfree;
1330
extern curl_realloc_callback Curl_crealloc;
1331
extern curl_strdup_callback Curl_cstrdup;
1332
extern curl_calloc_callback Curl_ccalloc;
1333
1334
/*
1335
 * curlx_safefree() defined as a macro to allow MemoryTracking feature
1336
 * to log free() calls at same location where curlx_safefree() is used.
1337
 * This macro also assigns NULL to given pointer when free'd.
1338
 */
1339
#define curlx_safefree(ptr) \
1340
6.59k
  do {                      \
1341
6.59k
    curlx_free(ptr);        \
1342
6.59k
    (ptr) = NULL;           \
1343
6.59k
  } while(0)
1344
1345
#include <curl/curl.h> /* for CURL_EXTERN, curl_socket_t, mprintf.h */
1346
1347
#ifdef DEBUGBUILD
1348
#define CURL_MEMDEBUG
1349
#endif
1350
1351
#ifdef CURL_MEMDEBUG
1352
#ifdef __clang__
1353
#  define ALLOC_FUNC         __attribute__((__malloc__))
1354
#  if __clang_major__ >= 4
1355
#  define ALLOC_SIZE(s)      __attribute__((__alloc_size__(s)))
1356
#  define ALLOC_SIZE2(n, s)  __attribute__((__alloc_size__(n, s)))
1357
#  else
1358
#  define ALLOC_SIZE(s)
1359
#  define ALLOC_SIZE2(n, s)
1360
#  endif
1361
#elif defined(__GNUC__) && __GNUC__ >= 3
1362
#  define ALLOC_FUNC         __attribute__((__malloc__))
1363
#  define ALLOC_SIZE(s)      __attribute__((__alloc_size__(s)))
1364
#  define ALLOC_SIZE2(n, s)  __attribute__((__alloc_size__(n, s)))
1365
#elif defined(_MSC_VER)
1366
#  define ALLOC_FUNC         __declspec(restrict)
1367
#  define ALLOC_SIZE(s)
1368
#  define ALLOC_SIZE2(n, s)
1369
#else
1370
#  define ALLOC_FUNC
1371
#  define ALLOC_SIZE(s)
1372
#  define ALLOC_SIZE2(n, s)
1373
#endif
1374
1375
extern FILE *curl_dbg_logfile;
1376
1377
/* memory functions */
1378
CURL_EXTERN void curl_dbg_free(void *ptr, int line, const char *source);
1379
CURL_EXTERN ALLOC_FUNC ALLOC_SIZE(1)
1380
  void *curl_dbg_malloc(size_t size, int line, const char *source);
1381
CURL_EXTERN ALLOC_FUNC ALLOC_SIZE2(1, 2)
1382
  void *curl_dbg_calloc(size_t n, size_t size, int line, const char *source);
1383
CURL_EXTERN ALLOC_SIZE(2)
1384
  void *curl_dbg_realloc(void *ptr, size_t size, int line, const char *source);
1385
CURL_EXTERN ALLOC_FUNC
1386
  char *curl_dbg_strdup(const char *str, int line, const char *src);
1387
#if defined(_WIN32) && defined(UNICODE)
1388
CURL_EXTERN ALLOC_FUNC
1389
  wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, const char *source);
1390
#endif
1391
1392
CURL_EXTERN void curl_dbg_memdebug(const char *logname);
1393
CURL_EXTERN void curl_dbg_memlimit(long limit);
1394
CURL_EXTERN void curl_dbg_log(const char *format, ...) CURL_PRINTF(1, 2);
1395
1396
/* file descriptor manipulators */
1397
CURL_EXTERN curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
1398
                                          int line, const char *source);
1399
CURL_EXTERN void curl_dbg_mark_sclose(curl_socket_t sockfd,
1400
                                      int line, const char *source);
1401
CURL_EXTERN int curl_dbg_sclose(curl_socket_t sockfd,
1402
                                int line, const char *source);
1403
CURL_EXTERN curl_socket_t curl_dbg_accept(curl_socket_t s,
1404
                                          void *saddr, void *saddrlen,
1405
                                          int line, const char *source);
1406
#ifdef HAVE_ACCEPT4
1407
CURL_EXTERN curl_socket_t curl_dbg_accept4(curl_socket_t s, void *saddr,
1408
                                           void *saddrlen, int flags,
1409
                                           int line, const char *source);
1410
#endif
1411
#ifdef HAVE_SOCKETPAIR
1412
CURL_EXTERN int curl_dbg_socketpair(int domain, int type, int protocol,
1413
                                    curl_socket_t socket_vector[2],
1414
                                    int line, const char *source);
1415
#endif
1416
1417
/* FILE functions */
1418
CURL_EXTERN int curl_dbg_fclose(FILE *file, int line, const char *source);
1419
CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fopen(const char *file, const char *mode,
1420
                                            int line, const char *source);
1421
CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_freopen(const char *file,
1422
                                              const char *mode, FILE *fh,
1423
                                              int line, const char *source);
1424
CURL_EXTERN ALLOC_FUNC FILE *curl_dbg_fdopen(int filedes, const char *mode,
1425
                                             int line, const char *source);
1426
1427
0
#define sclose(sockfd) curl_dbg_sclose(sockfd, __LINE__, __FILE__)
1428
#define fake_sclose(sockfd) curl_dbg_mark_sclose(sockfd, __LINE__, __FILE__)
1429
1430
#define CURL_GETADDRINFO(host, serv, hint, res) \
1431
0
  curl_dbg_getaddrinfo(host, serv, hint, res, __LINE__, __FILE__)
1432
#define CURL_FREEADDRINFO(data) \
1433
0
  curl_dbg_freeaddrinfo(data, __LINE__, __FILE__)
1434
#define CURL_SOCKET(domain, type, protocol) \
1435
0
  curl_dbg_socket((int)(domain), type, protocol, __LINE__, __FILE__)
1436
#ifdef HAVE_SOCKETPAIR
1437
#define CURL_SOCKETPAIR(domain, type, protocol, socket_vector) \
1438
  curl_dbg_socketpair((int)(domain), type, protocol, socket_vector, \
1439
                      __LINE__, __FILE__)
1440
#endif
1441
#define CURL_ACCEPT(sock, addr, len) \
1442
  curl_dbg_accept(sock, addr, len, __LINE__, __FILE__)
1443
#ifdef HAVE_ACCEPT4
1444
#define CURL_ACCEPT4(sock, addr, len, flags) \
1445
0
  curl_dbg_accept4(sock, addr, len, flags, __LINE__, __FILE__)
1446
#endif
1447
1448
#else /* !CURL_MEMDEBUG */
1449
1450
#define sclose(x) CURL_SCLOSE(x)
1451
#define fake_sclose(x) Curl_nop_stmt
1452
1453
#define CURL_GETADDRINFO getaddrinfo
1454
#define CURL_FREEADDRINFO freeaddrinfo
1455
#define CURL_SOCKET socket
1456
#ifdef HAVE_SOCKETPAIR
1457
#define CURL_SOCKETPAIR socketpair
1458
#endif
1459
#define CURL_ACCEPT accept
1460
#ifdef HAVE_ACCEPT4
1461
#define CURL_ACCEPT4 accept4
1462
#endif
1463
1464
#endif /* CURL_MEMDEBUG */
1465
1466
/* Allocator macros */
1467
1468
#ifdef _WIN32
1469
#define CURLX_STRDUP_LOW _strdup
1470
#else
1471
0
#define CURLX_STRDUP_LOW strdup
1472
#endif
1473
1474
#ifdef CURL_MEMDEBUG
1475
1476
1.48k
#define curlx_strdup(ptr)          curl_dbg_strdup(ptr, __LINE__, __FILE__)
1477
876
#define curlx_malloc(size)         curl_dbg_malloc(size, __LINE__, __FILE__)
1478
#define curlx_calloc(nbelem, size) \
1479
2.18k
  curl_dbg_calloc(nbelem, size, __LINE__, __FILE__)
1480
#define curlx_realloc(ptr, size) \
1481
3.02k
  curl_dbg_realloc(ptr, size, __LINE__, __FILE__)
1482
54.4k
#define curlx_free(ptr)            curl_dbg_free(ptr, __LINE__, __FILE__)
1483
1484
#ifdef _WIN32
1485
#ifdef UNICODE
1486
#define curlx_tcsdup(ptr)          curl_dbg_wcsdup(ptr, __LINE__, __FILE__)
1487
#else
1488
#define curlx_tcsdup               curlx_strdup
1489
#endif
1490
#endif /* _WIN32 */
1491
1492
#else /* !CURL_MEMDEBUG */
1493
1494
#ifdef BUILDING_LIBCURL
1495
#define curlx_strdup               Curl_cstrdup
1496
#define curlx_malloc               Curl_cmalloc
1497
#define curlx_calloc               Curl_ccalloc
1498
#define curlx_realloc              Curl_crealloc
1499
#define curlx_free                 Curl_cfree
1500
#else /* !BUILDING_LIBCURL */
1501
#define curlx_strdup               CURLX_STRDUP_LOW
1502
#define curlx_malloc               malloc
1503
#define curlx_calloc               calloc
1504
#define curlx_realloc              realloc
1505
#define curlx_free                 free
1506
#endif /* BUILDING_LIBCURL */
1507
1508
#ifdef _WIN32
1509
#ifdef UNICODE
1510
#define curlx_tcsdup               curlx_wcsdup
1511
#else
1512
#define curlx_tcsdup               curlx_strdup
1513
#endif
1514
#endif /* _WIN32 */
1515
1516
#endif /* CURL_MEMDEBUG */
1517
1518
/* Some versions of the Android NDK is missing the declaration */
1519
#if defined(HAVE_GETPWUID_R) && \
1520
  defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
1521
struct passwd;
1522
int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf,
1523
               size_t buflen, struct passwd **result);
1524
#endif
1525
1526
#ifdef UNITTESTS
1527
#define UNITTEST
1528
#else
1529
#define UNITTEST static
1530
#endif
1531
1532
#ifdef USE_NGHTTP2
1533
#define USE_HTTP2
1534
#endif
1535
1536
#if (defined(USE_NGTCP2) && defined(USE_NGHTTP3)) || defined(USE_QUICHE)
1537
1538
#ifdef CURL_WITH_MULTI_SSL
1539
#error "MultiSSL combined with QUIC is not supported"
1540
#endif
1541
1542
#define USE_HTTP3
1543
#endif
1544
1545
/* WebAssembly builds have TCP_NODELAY, but runtime support is missing. */
1546
#ifndef __EMSCRIPTEN__
1547
#define CURL_TCP_NODELAY_SUPPORTED
1548
#endif
1549
1550
/* Certain Windows implementations are not aligned with what curl expects,
1551
   so always use the local one on this platform. E.g. the mingw-w64
1552
   implementation can return wrong results for non-ASCII inputs. */
1553
#if defined(HAVE_BASENAME) && defined(_WIN32)
1554
#undef HAVE_BASENAME
1555
#endif
1556
1557
#if defined(USE_UNIX_SOCKETS) && defined(_WIN32)
1558
/* Offered by mingw-w64 v10+, MS SDK 10.0.16299.0/VS2017 15.4+ */
1559
#if defined(__MINGW32__) && (__MINGW64_VERSION_MAJOR >= 10)
1560
#  include <afunix.h>
1561
#elif !defined(UNIX_PATH_MAX) /* Replicate logic present in afunix.h */
1562
#  define UNIX_PATH_MAX 108
1563
/* !checksrc! disable TYPEDEFSTRUCT 1 */
1564
typedef struct sockaddr_un {
1565
  CURL_SA_FAMILY_T sun_family;
1566
  char sun_path[UNIX_PATH_MAX];
1567
} SOCKADDR_UN, *PSOCKADDR_UN;
1568
#endif
1569
#endif
1570
1571
#ifdef USE_OPENSSL
1572
/* OpenSSL 3 marks these functions deprecated but we have no replacements (yet)
1573
   so tell the compiler to not warn for them:
1574
   - DES_* (for NTLM)
1575
   - EVP_PKEY_get1_RSA, MD5_*, RSA_flags, RSA_free (auto-skipped for OpenSSL
1576
     built with no-deprecated) */
1577
#  define OPENSSL_SUPPRESS_DEPRECATED
1578
#  ifdef _WIN32
1579
/* Silence LibreSSL warnings about wincrypt.h collision. Works in 3.8.2+ */
1580
#    ifndef LIBRESSL_DISABLE_OVERRIDE_WINCRYPT_DEFINES_WARNING
1581
#    define LIBRESSL_DISABLE_OVERRIDE_WINCRYPT_DEFINES_WARNING
1582
#    endif
1583
#  endif
1584
#endif
1585
1586
#ifdef CURL_INLINE
1587
/* 'CURL_INLINE' defined, use as-is */
1588
#elif defined(inline)
1589
#  define CURL_INLINE inline /* 'inline' defined, assumed correct */
1590
#elif defined(__cplusplus)
1591
/* The code is compiled with C++ compiler.
1592
   C++ always supports 'inline'. */
1593
#  define CURL_INLINE inline /* 'inline' keyword supported */
1594
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
1595
/* C99 (and later) supports 'inline' keyword */
1596
#  define CURL_INLINE inline /* 'inline' keyword supported */
1597
#elif defined(__GNUC__) && __GNUC__ >= 3
1598
/* GCC supports '__inline__' as an extension */
1599
#  define CURL_INLINE __inline__
1600
#elif defined(_MSC_VER)
1601
#  define CURL_INLINE __inline
1602
#else
1603
/* Probably 'inline' is not supported by compiler.
1604
   Define to the empty string to be on the safe side. */
1605
#  define CURL_INLINE /* empty */
1606
#endif
1607
1608
/* Detect if compiler supports C99 variadic macros */
1609
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
1610
  defined(_MSC_VER)
1611
#define CURL_HAVE_MACRO_VARARG
1612
#endif
1613
1614
#if !defined(CURL_HAVE_MACRO_VARARG) || \
1615
  (defined(CURL_HAVE_MACRO_VARARG) && !defined(CURL_DISABLE_VERBOSE_STRINGS))
1616
#define CURLVERBOSE
1617
0
#define VERBOSE(x) x
1618
0
#define NOVERBOSE(x) Curl_nop_stmt
1619
#else
1620
#define VERBOSE(x) Curl_nop_stmt
1621
#define NOVERBOSE(x) x
1622
#endif
1623
1624
/* For FreeBSD it is included from curl/curl.h */
1625
#if defined(__DragonFly__) || defined(__OpenBSD__) || defined(__NetBSD__)
1626
#include <sys/param.h>  /* for __DragonFly_version, OpenBSD,
1627
                           __NetBSD_Version__ */
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 /* v5.6+ */) || \
1648
  (defined(__FreeBSD__) && __FreeBSD_version >= 1100037 /* v11.0+ */) || \
1649
  (defined(__OpenBSD__) && OpenBSD >= 201405 /* v5.5+ */)
1650
11
#define curlx_memzero_low(buf, size)  explicit_bzero(buf, size)
1651
#elif defined(__NetBSD__) && __NetBSD_Version__ >= 702000000 /* v7.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 */