gnunet-android

GNUnet for Android
Log | Files | Refs | README

wc_port.h (53395B)


      1 /* wc_port.h
      2  *
      3  * Copyright (C) 2006-2025 wolfSSL Inc.
      4  *
      5  * This file is part of wolfSSL.
      6  *
      7  * wolfSSL is free software; you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License as published by
      9  * the Free Software Foundation; either version 3 of the License, or
     10  * (at your option) any later version.
     11  *
     12  * wolfSSL is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  * GNU General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU General Public License
     18  * along with this program; if not, write to the Free Software
     19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
     20  */
     21 
     22 /*!
     23     \file wolfssl/wolfcrypt/wc_port.h
     24 */
     25 
     26 #ifndef WOLF_CRYPT_PORT_H
     27 #define WOLF_CRYPT_PORT_H
     28 
     29 #include <wolfssl/wolfcrypt/settings.h>
     30 #include <wolfssl/wolfcrypt/visibility.h>
     31 
     32 #ifdef __cplusplus
     33     extern "C" {
     34 #endif
     35 
     36 /* Detect if compiler supports C99. "NO_WOLF_C99" can be defined in
     37  * user_settings.h to disable checking for C99 support. */
     38 #if !defined(WOLF_C99) && defined(__STDC_VERSION__) && \
     39     !defined(WOLFSSL_ARDUINO) && !defined(NO_WOLF_C99)
     40     #if __STDC_VERSION__ >= 199901L
     41         #define WOLF_C99
     42     #endif
     43 #endif
     44 
     45 
     46 /* GENERIC INCLUDE SECTION */
     47 #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
     48     #include <mqx.h>
     49     #if (defined(MQX_USE_IO_OLD) && MQX_USE_IO_OLD) || \
     50         defined(FREESCALE_MQX_5_0)
     51         #include <fio.h>
     52     #else
     53         #include <nio.h>
     54     #endif
     55 #endif
     56 
     57 #if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)
     58     #include <wolfssl/wolfcrypt/port/maxim/max3266x.h>
     59 #endif
     60 
     61 #ifdef WOLFSSL_LINUXKM
     62     #include "../../linuxkm/linuxkm_wc_port.h"
     63 #endif /* WOLFSSL_LINUXKM */
     64 
     65 #ifndef WARN_UNUSED_RESULT
     66     #if defined(WOLFSSL_LINUXKM) && defined(__must_check)
     67         #define WARN_UNUSED_RESULT __must_check
     68     #elif (defined(__GNUC__) && (__GNUC__ >= 4)) || \
     69         (defined(__IAR_SYSTEMS_ICC__) && (__VER__ >= 9040001))
     70         #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
     71     #else
     72         #define WARN_UNUSED_RESULT
     73     #endif
     74 #endif /* !WARN_UNUSED_RESULT */
     75 
     76 #ifndef WC_MAYBE_UNUSED
     77     #if (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__) || \
     78             defined(__IAR_SYSTEMS_ICC__)
     79         #define WC_MAYBE_UNUSED __attribute__((unused))
     80     #else
     81         #define WC_MAYBE_UNUSED
     82     #endif
     83 #endif /* !WC_MAYBE_UNUSED */
     84 
     85 #ifndef WC_DEPRECATED
     86     #ifdef WOLFSSL_ZEPHYR
     87         #define WC_DEPRECATED(msg) /* null expansion */
     88     #elif ((defined(__GNUC__) && \
     89             ((__GNUC__ >= 5) || \
     90             ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))) ||  \
     91           defined(__clang__)
     92         #define WC_DEPRECATED(msg) __attribute__((deprecated(msg)))
     93     #elif defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) || \
     94           defined(_WIN32_WCE) || defined(__WATCOMC__)
     95         #define WC_DEPRECATED(msg) __declspec(deprecated(msg))
     96     #elif (defined(__GNUC__) && (__GNUC__ >= 4)) || \
     97           defined(__IAR_SYSTEMS_ICC__)
     98         #define WC_DEPRECATED(msg) __attribute__((deprecated))
     99     #else
    100         #define WC_DEPRECATED(msg) /* null expansion */
    101     #endif
    102 #endif /* !WC_MAYBE_UNUSED */
    103 
    104 /* use inlining if compiler allows */
    105 #ifndef WC_INLINE
    106 #ifndef NO_INLINE
    107     #ifdef _MSC_VER
    108         #define WC_INLINE __inline
    109     #elif defined(__GNUC__)
    110            #ifdef WOLFSSL_VXWORKS
    111                #define WC_INLINE __inline__
    112            #else
    113                #define WC_INLINE inline
    114            #endif
    115     #elif defined(__IAR_SYSTEMS_ICC__)
    116         #define WC_INLINE inline
    117     #elif defined(THREADX)
    118         #define WC_INLINE _Inline
    119     #elif defined(__ghc__)
    120         #ifndef __cplusplus
    121             #define WC_INLINE __inline
    122         #else
    123             #define WC_INLINE inline
    124         #endif
    125     #elif defined(__CCRX__)
    126         #define WC_INLINE inline
    127     #elif defined(__DCC__)
    128         #ifndef __cplusplus
    129             #define WC_INLINE __inline__
    130         #else
    131             #define WC_INLINE inline
    132         #endif
    133     #else
    134         #define WC_INLINE WC_MAYBE_UNUSED
    135     #endif
    136 #else
    137     #define WC_INLINE WC_MAYBE_UNUSED
    138 #endif
    139 #endif
    140 
    141 #ifndef WC_OMIT_FRAME_POINTER
    142     #if defined(__GNUC__)
    143         #define WC_OMIT_FRAME_POINTER  \
    144             __attribute__((optimize("-fomit-frame-pointer")))
    145     #else
    146         #define WC_OMIT_FRAME_POINTER
    147     #endif
    148 #endif
    149 
    150 /* THREADING/MUTEX SECTION */
    151 #if defined(SINGLE_THREADED) && defined(NO_FILESYSTEM)
    152     /* No system headers required for build. */
    153 #elif defined(__WATCOMC__)
    154     #if defined(SINGLE_THREADED)
    155         #if defined(USE_WINDOWS_API)
    156             #define _WINSOCKAPI_ /* block inclusion of winsock.h header file */
    157             #include <windows.h>
    158             #undef _WINSOCKAPI_ /* undefine it for MINGW winsock2.h header */
    159             #ifndef WOLFSSL_USER_IO
    160                 #include <winsock2.h>
    161                 #include <ws2tcpip.h> /* required for InetPton */
    162             #endif
    163         #elif defined(__OS2__)
    164             #include <os2.h>
    165         #endif
    166     #else
    167         #if defined(USE_WINDOWS_API)
    168             #define _WINSOCKAPI_ /* block inclusion of winsock.h header file */
    169             #include <windows.h>
    170             #undef _WINSOCKAPI_ /* undefine it for MINGW winsock2.h header */
    171             #include <process.h>
    172             #ifndef WOLFSSL_USER_IO
    173                 #include <winsock2.h>
    174                 #include <ws2tcpip.h> /* required for InetPton */
    175             #endif
    176         #elif defined(__OS2__)
    177             #define INCL_DOSSEMAPHORES
    178             #define INCL_DOSPROCESS
    179             #include <os2.h>
    180             #include <process.h>
    181         #else
    182             #ifndef WOLFSSL_USER_MUTEX
    183                 #define WOLFSSL_PTHREADS
    184             #endif
    185             #if defined(WOLFSSL_PTHREADS)
    186                 #include <pthread.h>
    187             #endif
    188         #endif
    189     #endif
    190 #elif defined(USE_WINDOWS_API)
    191     #if defined(WOLFSSL_PTHREADS)
    192         #include <pthread.h>
    193     #endif
    194     #ifdef WOLFSSL_GAME_BUILD
    195         #include "system/xtl.h"
    196     #else
    197         #ifndef WIN32_LEAN_AND_MEAN
    198             #define WIN32_LEAN_AND_MEAN
    199         #endif
    200         #if !defined(WOLFSSL_SGX) && !defined(WOLFSSL_NOT_WINDOWS_API)
    201             #define _WINSOCKAPI_ /* block inclusion of winsock.h header file. */
    202             #include <windows.h>
    203             #undef _WINSOCKAPI_ /* undefine it for MINGW winsock2.h header */
    204             #ifndef WOLFSSL_USER_IO
    205                 #include <winsock2.h>
    206                 #include <ws2tcpip.h> /* required for InetPton */
    207             #endif
    208         #endif /* WOLFSSL_SGX */
    209     #endif
    210     #if !defined(SINGLE_THREADED) && !defined(_WIN32_WCE)
    211         #include <process.h>
    212     #endif
    213 #elif defined(THREADX)
    214     #ifndef SINGLE_THREADED
    215         #ifdef NEED_THREADX_TYPES
    216             #include <types.h>
    217         #endif
    218         #include <tx_api.h>
    219     #endif
    220 #elif defined(WOLFSSL_DEOS)
    221     #include "mutexapi.h"
    222 #elif defined(MICRIUM)
    223     /* do nothing, just don't pick Unix */
    224 #elif defined(FREERTOS) || defined(FREERTOS_TCP) || defined(WOLFSSL_SAFERTOS)
    225     /* do nothing */
    226 #elif defined(RTTHREAD)
    227     /* do nothing */
    228 #elif defined(EBSNET)
    229     /* do nothing */
    230 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
    231     /* do nothing */
    232 #elif defined(FREESCALE_FREE_RTOS)
    233     #include "fsl_os_abstraction.h"
    234 #elif defined(WOLFSSL_VXWORKS)
    235     #include <semLib.h>
    236     #ifdef WOLFSSL_VXWORKS_6_x
    237         #ifndef SEM_ID_NULL
    238             #define SEM_ID_NULL ((SEM_ID)NULL)
    239         #endif
    240     #endif
    241 #elif defined(WOLFSSL_uITRON4)
    242     #include "stddef.h"
    243     #include "kernel.h"
    244 #elif  defined(WOLFSSL_uTKERNEL2)
    245     #include "tk/tkernel.h"
    246 #elif defined(WOLFSSL_CMSIS_RTOS)
    247     #include "cmsis_os.h"
    248 #elif defined(WOLFSSL_CMSIS_RTOSv2)
    249     #include "cmsis_os2.h"
    250 #elif defined(WOLFSSL_MDK_ARM)
    251     #if defined(WOLFSSL_MDK5)
    252         #include "cmsis_os.h"
    253     #else
    254         #include <rtl.h>
    255     #endif
    256 #elif defined(WOLFSSL_CMSIS_RTOS)
    257     #include "cmsis_os.h"
    258 #elif defined(WOLFSSL_TIRTOS)
    259     #include <ti/sysbios/BIOS.h>
    260     #include <ti/sysbios/knl/Task.h>
    261     #include <ti/sysbios/knl/Semaphore.h>
    262 #elif defined(WOLFSSL_FROSTED)
    263     #include <semaphore.h>
    264 #elif defined(INTIME_RTOS)
    265     #include <rt.h>
    266     #include <io.h>
    267 #elif defined(WOLFSSL_NUCLEUS_1_2)
    268     /* NU_DEBUG needed struct access in nucleus_realloc */
    269     #define NU_DEBUG
    270     #include "plus/nucleus.h"
    271     #include "nucleus.h"
    272 #elif defined(WOLFSSL_APACHE_MYNEWT)
    273     /* do nothing */
    274 #elif defined(WOLFSSL_ZEPHYR)
    275     #include <version.h>
    276     #ifndef SINGLE_THREADED
    277         #if !defined(CONFIG_PTHREAD_IPC) && !defined(CONFIG_POSIX_THREADS)
    278             #error "Threading needs CONFIG_PTHREAD_IPC / CONFIG_POSIX_THREADS"
    279         #endif
    280     #ifdef max
    281     #undef max
    282     #endif
    283     #if KERNEL_VERSION_NUMBER >= 0x30100
    284         #include <zephyr/kernel.h>
    285         #include <zephyr/posix/posix_types.h>
    286         #include <zephyr/posix/pthread.h>
    287     #else
    288         #include <kernel.h>
    289         #include <posix/posix_types.h>
    290         #include <posix/pthread.h>
    291     #endif
    292     #define max MAX
    293     #endif
    294 #elif defined(WOLFSSL_TELIT_M2MB)
    295 
    296     /* Telit SDK uses C++ compile option (--cpp), which causes link issue
    297         to API's if wrapped in extern "C" */
    298     #ifdef __cplusplus
    299         }  /* extern "C" */
    300     #endif
    301 
    302     #include "m2mb_types.h"
    303     #include "m2mb_os_types.h"
    304     #include "m2mb_os_api.h"
    305     #include "m2mb_os.h"
    306     #include "m2mb_os_mtx.h"
    307     #ifndef NO_ASN_TIME
    308     #include "m2mb_rtc.h"
    309     #endif
    310     #ifndef NO_FILESYSTEM
    311     #include "m2mb_fs_posix.h"
    312     #endif
    313 
    314     #undef kB /* eliminate conflict in asn.h */
    315 
    316     #ifdef __cplusplus
    317         extern "C" {
    318     #endif
    319 #elif defined(WOLFSSL_EMBOS)
    320     /* do nothing */
    321 #else
    322     #ifndef SINGLE_THREADED
    323         #ifndef WOLFSSL_USER_MUTEX
    324             #ifdef WOLFSSL_LINUXKM
    325                 /* definitions are in linuxkm/linuxkm_wc_port.h */
    326             #else
    327                 #define WOLFSSL_PTHREADS
    328                 #include <pthread.h>
    329             #endif
    330         #endif
    331     #endif
    332     #if (defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)) && \
    333         !defined(NO_FILESYSTEM)
    334         #ifdef FUSION_RTOS
    335             #include <fclunistd.h>
    336         #else
    337             #include <unistd.h>      /* for close of BIO */
    338         #endif
    339     #endif
    340 #endif
    341 
    342 /* For FIPS keep the function names the same */
    343 #ifdef HAVE_FIPS
    344 #define wc_InitMutex   InitMutex
    345 #define wc_FreeMutex   FreeMutex
    346 #define wc_LockMutex   LockMutex
    347 #define wc_UnLockMutex UnLockMutex
    348 #endif /* HAVE_FIPS */
    349 
    350 #ifdef SINGLE_THREADED
    351     typedef int wolfSSL_Mutex;
    352 #else /* MULTI_THREADED */
    353     /* FREERTOS comes first to enable use of FreeRTOS Windows simulator only */
    354     #if defined(FREERTOS)
    355         #if defined(ESP_IDF_VERSION_MAJOR) && (ESP_IDF_VERSION_MAJOR >= 4)
    356             typedef SemaphoreHandle_t wolfSSL_Mutex;
    357         #else
    358             typedef xSemaphoreHandle wolfSSL_Mutex;
    359         #endif
    360     #elif defined(FREERTOS_TCP)
    361         #include "FreeRTOS.h"
    362         #include "semphr.h"
    363         typedef SemaphoreHandle_t  wolfSSL_Mutex;
    364     #elif defined (RTTHREAD)
    365         #include "rtthread.h"
    366         typedef rt_mutex_t wolfSSL_Mutex;
    367     #elif defined(WOLFSSL_SAFERTOS)
    368         typedef struct wolfSSL_Mutex {
    369             signed char mutexBuffer[portQUEUE_OVERHEAD_BYTES];
    370             xSemaphoreHandle mutex;
    371         } wolfSSL_Mutex;
    372     #elif defined(USE_WINDOWS_API) && !defined(WOLFSSL_PTHREADS)
    373         typedef CRITICAL_SECTION wolfSSL_Mutex;
    374     #elif defined(MAXQ10XX_MUTEX)
    375         #include <sys/mman.h>
    376         #include <fcntl.h>
    377         #include <pthread.h>
    378         typedef pthread_mutex_t wolfSSL_Mutex;
    379         int maxq_CryptHwMutexTryLock(void);
    380     #elif defined(WOLFSSL_PTHREADS)
    381         #ifdef WOLFSSL_USE_RWLOCK
    382             typedef pthread_rwlock_t wolfSSL_RwLock;
    383         #endif
    384         typedef pthread_mutex_t wolfSSL_Mutex;
    385         #define WOLFSSL_MUTEX_INITIALIZER(lockname) PTHREAD_MUTEX_INITIALIZER
    386     #elif defined(THREADX)
    387         typedef TX_MUTEX wolfSSL_Mutex;
    388     #elif defined(WOLFSSL_DEOS)
    389         typedef mutex_handle_t wolfSSL_Mutex;
    390     #elif defined(MICRIUM)
    391         typedef OS_MUTEX wolfSSL_Mutex;
    392     #elif defined(EBSNET)
    393         #if (defined(RTPLATFORM) && (RTPLATFORM != 0))
    394             typedef RTP_MUTEX wolfSSL_Mutex;
    395         #else
    396             typedef KS_RTIPSEM wolfSSL_Mutex;
    397         #endif
    398     #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
    399         typedef MUTEX_STRUCT wolfSSL_Mutex;
    400     #elif defined(FREESCALE_FREE_RTOS)
    401         typedef mutex_t wolfSSL_Mutex;
    402     #elif defined(WOLFSSL_VXWORKS)
    403         typedef SEM_ID wolfSSL_Mutex;
    404     #elif defined(WOLFSSL_uITRON4)
    405         typedef struct wolfSSL_Mutex {
    406             T_CSEM sem ;
    407             ID     id ;
    408         } wolfSSL_Mutex;
    409     #elif defined(WOLFSSL_uTKERNEL2)
    410         typedef struct wolfSSL_Mutex {
    411             T_CSEM sem ;
    412             ID     id ;
    413         } wolfSSL_Mutex;
    414     #elif defined(WOLFSSL_MDK_ARM)
    415         #if defined(WOLFSSL_CMSIS_RTOS)
    416             typedef osMutexId wolfSSL_Mutex;
    417         #else
    418             typedef OS_MUT wolfSSL_Mutex;
    419         #endif
    420     #elif defined(WOLFSSL_CMSIS_RTOS)
    421         typedef osMutexId wolfSSL_Mutex;
    422     #elif defined(WOLFSSL_CMSIS_RTOSv2)
    423         typedef osMutexId_t wolfSSL_Mutex;
    424     #elif defined(WOLFSSL_TIRTOS)
    425         typedef ti_sysbios_knl_Semaphore_Handle wolfSSL_Mutex;
    426     #elif defined(WOLFSSL_FROSTED)
    427         typedef mutex_t * wolfSSL_Mutex;
    428     #elif defined(INTIME_RTOS)
    429         typedef RTHANDLE wolfSSL_Mutex;
    430     #elif defined(WOLFSSL_NUCLEUS_1_2)
    431         typedef NU_SEMAPHORE wolfSSL_Mutex;
    432     #elif defined(WOLFSSL_ZEPHYR)
    433         typedef struct k_mutex wolfSSL_Mutex;
    434     #elif defined(WOLFSSL_TELIT_M2MB)
    435         typedef M2MB_OS_MTX_HANDLE wolfSSL_Mutex;
    436     #elif defined(WOLFSSL_EMBOS)
    437         typedef OS_MUTEX wolfSSL_Mutex;
    438     #elif defined(WOLFSSL_USER_MUTEX)
    439         /* typedef User_Mutex wolfSSL_Mutex; */
    440     #elif defined(WOLFSSL_LINUXKM)
    441         /* definitions are in linuxkm/linuxkm_wc_port.h */
    442     #elif defined(__WATCOMC__)
    443         /* OS/2 */
    444         typedef ULONG wolfSSL_Mutex;
    445     #else
    446         #error Need a mutex type in multithreaded mode
    447     #endif /* USE_WINDOWS_API */
    448 
    449 #endif /* SINGLE_THREADED */
    450 
    451 #ifdef WOLFSSL_TEST_NO_MUTEX_INITIALIZER
    452     #undef WOLFSSL_MUTEX_INITIALIZER
    453 #endif
    454 
    455 #ifdef WOLFSSL_MUTEX_INITIALIZER
    456     #define WOLFSSL_MUTEX_INITIALIZER_CLAUSE(lockname) = WOLFSSL_MUTEX_INITIALIZER(lockname)
    457 #else
    458     #define WOLFSSL_MUTEX_INITIALIZER_CLAUSE(lockname) /* null expansion */
    459 #endif
    460 
    461 #if !defined(WOLFSSL_USE_RWLOCK) || defined(SINGLE_THREADED)
    462     typedef wolfSSL_Mutex wolfSSL_RwLock;
    463 #endif
    464 
    465 #ifndef WOLFSSL_NO_ATOMICS
    466 #ifdef SINGLE_THREADED
    467     typedef int wolfSSL_Atomic_Int;
    468     #define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
    469     #define WOLFSSL_ATOMIC_LOAD(x) (x)
    470     #define WOLFSSL_ATOMIC_STORE(x, val) (x) = (val)
    471     #define WOLFSSL_ATOMIC_OPS
    472 #elif defined(HAVE_C___ATOMIC)
    473 #ifdef __cplusplus
    474 #if defined(__GNUC__) && defined(__ATOMIC_RELAXED)
    475     /* C++ using direct calls to compiler built-in functions */
    476     typedef volatile int wolfSSL_Atomic_Int;
    477     #define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
    478     #define WOLFSSL_ATOMIC_LOAD(x) __atomic_load_n(&(x), __ATOMIC_CONSUME)
    479     #define WOLFSSL_ATOMIC_STORE(x, val) __atomic_store_n(&(x), val, __ATOMIC_RELEASE)
    480     #define WOLFSSL_ATOMIC_OPS
    481 #endif
    482 #else
    483     #ifdef WOLFSSL_HAVE_ATOMIC_H
    484     /* Default C Implementation */
    485     #include <stdatomic.h>
    486     typedef atomic_int wolfSSL_Atomic_Int;
    487     #define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
    488     #define WOLFSSL_ATOMIC_LOAD(x) atomic_load(&(x))
    489     #define WOLFSSL_ATOMIC_STORE(x, val) atomic_store(&(x), val)
    490     #define WOLFSSL_ATOMIC_OPS
    491     #endif /* WOLFSSL_HAVE_ATOMIC_H */
    492 #endif
    493 #elif defined(_MSC_VER) && !defined(WOLFSSL_NOT_WINDOWS_API)
    494     /* Use MSVC compiler intrinsics for atomic ops */
    495     #ifdef _WIN32_WCE
    496         #include <armintr.h>
    497     #else
    498         #include <intrin.h>
    499     #endif
    500     typedef volatile long wolfSSL_Atomic_Int;
    501     #define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
    502     #define WOLFSSL_ATOMIC_LOAD(x) (x)
    503     #define WOLFSSL_ATOMIC_STORE(x, val) (x) = (val)
    504     #define WOLFSSL_ATOMIC_OPS
    505 #endif
    506 #endif /* WOLFSSL_NO_ATOMICS */
    507 
    508 #if defined(WOLFSSL_ATOMIC_OPS) && !defined(SINGLE_THREADED)
    509     WOLFSSL_API void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i);
    510     /* Fetch* functions return the value of the counter immediately preceding
    511      * the effects of the function. */
    512     WOLFSSL_API int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i);
    513     WOLFSSL_API int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i);
    514 #else
    515     /* Code using these fallback implementations in non-SINGLE_THREADED builds
    516      * needs to arrange its own explicit fallback to int for wolfSSL_Atomic_Int,
    517      * which is not defined if !defined(WOLFSSL_ATOMIC_OPS) &&
    518      * !defined(SINGLE_THREADED).  This forces local awareness of thread-unsafe
    519      * semantics.
    520      */
    521     #define wolfSSL_Atomic_Int_Init(c, i) (*(c) = (i))
    522     static WC_INLINE int wolfSSL_Atomic_Int_FetchAdd(int *c, int i) {
    523         int ret = *c;
    524         *c += i;
    525         return ret;
    526     }
    527     static WC_INLINE int wolfSSL_Atomic_Int_FetchSub(int *c, int i) {
    528         int ret = *c;
    529         *c -= i;
    530         return ret;
    531     }
    532 #endif
    533 
    534 /* Reference counting. */
    535 typedef struct wolfSSL_RefWithMutex {
    536 #if !defined(SINGLE_THREADED)
    537     wolfSSL_Mutex mutex;
    538 #endif
    539     int count;
    540 } wolfSSL_RefWithMutex;
    541 
    542 #if defined(WOLFSSL_ATOMIC_OPS) && !defined(SINGLE_THREADED)
    543 typedef struct wolfSSL_Ref {
    544     wolfSSL_Atomic_Int count;
    545 } wolfSSL_Ref;
    546 #else
    547 typedef struct wolfSSL_RefWithMutex wolfSSL_Ref;
    548 #endif
    549 
    550 #if defined(SINGLE_THREADED) || defined(WOLFSSL_ATOMIC_OPS)
    551 
    552 #define wolfSSL_RefInit(ref, err)            \
    553     do {                                     \
    554         wolfSSL_Atomic_Int_Init(&(ref)->count, 1); \
    555         *(err) = 0;                          \
    556     } while(0)
    557 #define wolfSSL_RefFree(ref) WC_DO_NOTHING
    558 #define wolfSSL_RefInc(ref, err)             \
    559     do {                                     \
    560         (void)wolfSSL_Atomic_Int_FetchAdd(&(ref)->count, 1); \
    561         *(err) = 0;                          \
    562     } while(0)
    563 #define wolfSSL_RefDec(ref, isZero, err)     \
    564     do {                                     \
    565         int __prev = wolfSSL_Atomic_Int_FetchSub(&(ref)->count, 1); \
    566         /* __prev holds the value of count before subtracting 1 */ \
    567         *(isZero) = (__prev == 1);     \
    568         *(err) = 0;                          \
    569     } while(0)
    570 
    571 #else
    572 
    573 #define WOLFSSL_REFCNT_ERROR_RETURN
    574 
    575 #define wolfSSL_RefInit wolfSSL_RefWithMutexInit
    576 #define wolfSSL_RefFree wolfSSL_RefWithMutexFree
    577 #define wolfSSL_RefInc wolfSSL_RefWithMutexInc
    578 #define wolfSSL_RefDec wolfSSL_RefWithMutexDec
    579 
    580 #endif
    581 
    582 #if defined(SINGLE_THREADED)
    583 
    584 #define wolfSSL_RefWithMutexInit wolfSSL_RefInit
    585 #define wolfSSL_RefWithMutexFree wolfSSL_RefFree
    586 #define wolfSSL_RefWithMutexInc wolfSSL_RefInc
    587 #define wolfSSL_RefWithMutexLock(ref) 0
    588 #define wolfSSL_RefWithMutexUnlock(ref) 0
    589 #define wolfSSL_RefWithMutexDec wolfSSL_RefDec
    590 
    591 #else
    592 
    593 WOLFSSL_LOCAL void wolfSSL_RefWithMutexInit(wolfSSL_RefWithMutex* ref,
    594                                             int* err);
    595 WOLFSSL_LOCAL void wolfSSL_RefWithMutexFree(wolfSSL_RefWithMutex* ref);
    596 WOLFSSL_LOCAL void wolfSSL_RefWithMutexInc(wolfSSL_RefWithMutex* ref,
    597                                             int* err);
    598 WOLFSSL_LOCAL int wolfSSL_RefWithMutexLock(wolfSSL_RefWithMutex* ref);
    599 WOLFSSL_LOCAL int wolfSSL_RefWithMutexUnlock(wolfSSL_RefWithMutex* ref);
    600 WOLFSSL_LOCAL void wolfSSL_RefWithMutexDec(wolfSSL_RefWithMutex* ref,
    601                                             int* isZero, int* err);
    602 
    603 #endif
    604 
    605 
    606 /* Enable crypt HW mutex for Freescale MMCAU, PIC32MZ or STM32 */
    607 #if defined(FREESCALE_MMCAU) || defined(WOLFSSL_MICROCHIP_PIC32MZ) || \
    608     defined(STM32_CRYPTO) || defined(STM32_HASH) || defined(STM32_RNG) || \
    609     defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)
    610     #ifndef WOLFSSL_CRYPT_HW_MUTEX
    611         #define WOLFSSL_CRYPT_HW_MUTEX  1
    612     #endif
    613 #endif /* FREESCALE_MMCAU */
    614 
    615 #ifndef WOLFSSL_CRYPT_HW_MUTEX
    616     #define WOLFSSL_CRYPT_HW_MUTEX  0
    617 #endif
    618 
    619 #if WOLFSSL_CRYPT_HW_MUTEX
    620     /* wolfSSL_CryptHwMutexInit is called on first wolfSSL_CryptHwMutexLock,
    621        however it's recommended to call this directly on Hw init to avoid possible
    622        race condition where two calls to wolfSSL_CryptHwMutexLock are made at
    623        the same time. */
    624     WOLFSSL_LOCAL int wolfSSL_CryptHwMutexInit(void);
    625     WOLFSSL_LOCAL int wolfSSL_CryptHwMutexLock(void);
    626     WOLFSSL_LOCAL int wolfSSL_CryptHwMutexUnLock(void);
    627 #else
    628     /* Define stubs, since HW mutex is disabled */
    629     #define wolfSSL_CryptHwMutexInit()      0 /* Success */
    630     #define wolfSSL_CryptHwMutexLock()      0 /* Success */
    631     #define wolfSSL_CryptHwMutexUnLock()    (void)0 /* Success */
    632 #endif /* WOLFSSL_CRYPT_HW_MUTEX */
    633 
    634 #if defined(WOLFSSL_ALGO_HW_MUTEX) && (defined(NO_RNG_MUTEX) && \
    635         defined(NO_AES_MUTEX) && defined(NO_HASH_MUTEX) && defined(NO_PK_MUTEX))
    636         #error WOLFSSL_ALGO_HW_MUTEX does not support having all mutexes off
    637 #endif
    638 /* To support HW that can do different Crypto in parallel */
    639 #if WOLFSSL_CRYPT_HW_MUTEX && defined(WOLFSSL_ALGO_HW_MUTEX)
    640     typedef enum {
    641         #ifndef NO_RNG_MUTEX
    642         rng_mutex,
    643         #endif
    644         #ifndef NO_AES_MUTEX
    645         aes_mutex,
    646         #endif
    647         #ifndef NO_HASH_MUTEX
    648         hash_mutex,
    649         #endif
    650         #ifndef NO_PK_MUTEX
    651         pk_mutex,
    652         #endif
    653     } hw_mutex_algo;
    654 #endif
    655 
    656 /* If algo mutex is off, or WOLFSSL_ALGO_HW_MUTEX is not define, default */
    657 /* to using the generic wolfSSL_CryptHwMutex */
    658 #if (!defined(NO_RNG_MUTEX) && defined(WOLFSSL_ALGO_HW_MUTEX)) && \
    659     WOLFSSL_CRYPT_HW_MUTEX
    660     WOLFSSL_LOCAL int wolfSSL_HwRngMutexInit(void);
    661     WOLFSSL_LOCAL int wolfSSL_HwRngMutexLock(void);
    662     WOLFSSL_LOCAL int wolfSSL_HwRngMutexUnLock(void);
    663 #else
    664     #define wolfSSL_HwRngMutexInit    wolfSSL_CryptHwMutexInit
    665     #define wolfSSL_HwRngMutexLock    wolfSSL_CryptHwMutexLock
    666     #define wolfSSL_HwRngMutexUnLock  wolfSSL_CryptHwMutexUnLock
    667 #endif /* !defined(NO_RNG_MUTEX) && defined(WOLFSSL_ALGO_HW_MUTEX) */
    668 
    669 #if (!defined(NO_AES_MUTEX) && defined(WOLFSSL_ALGO_HW_MUTEX)) && \
    670     WOLFSSL_CRYPT_HW_MUTEX
    671     WOLFSSL_LOCAL int wolfSSL_HwAesMutexInit(void);
    672     WOLFSSL_LOCAL int wolfSSL_HwAesMutexLock(void);
    673     WOLFSSL_LOCAL int wolfSSL_HwAesMutexUnLock(void);
    674 #else
    675     #define wolfSSL_HwAesMutexInit    wolfSSL_CryptHwMutexInit
    676     #define wolfSSL_HwAesMutexLock    wolfSSL_CryptHwMutexLock
    677     #define wolfSSL_HwAesMutexUnLock  wolfSSL_CryptHwMutexUnLock
    678 #endif /* !defined(NO_AES_MUTEX) && defined(WOLFSSL_ALGO_HW_MUTEX) */
    679 
    680 #if (!defined(NO_HASH_MUTEX) && defined(WOLFSSL_ALGO_HW_MUTEX)) && \
    681     WOLFSSL_CRYPT_HW_MUTEX
    682     WOLFSSL_LOCAL int wolfSSL_HwHashMutexInit(void);
    683     WOLFSSL_LOCAL int wolfSSL_HwHashMutexLock(void);
    684     WOLFSSL_LOCAL int wolfSSL_HwHashMutexUnLock(void);
    685 #else
    686     #define wolfSSL_HwHashMutexInit   wolfSSL_CryptHwMutexInit
    687     #define wolfSSL_HwHashMutexLock   wolfSSL_CryptHwMutexLock
    688     #define wolfSSL_HwHashMutexUnLock wolfSSL_CryptHwMutexUnLock
    689 #endif /* !defined(NO_HASH_MUTEX) && defined(WOLFSSL_ALGO_HW_MUTEX) */
    690 
    691 #if (!defined(NO_PK_MUTEX) && defined(WOLFSSL_ALGO_HW_MUTEX)) && \
    692     WOLFSSL_CRYPT_HW_MUTEX
    693     WOLFSSL_LOCAL int wolfSSL_HwPkMutexInit(void);
    694     WOLFSSL_LOCAL int wolfSSL_HwPkMutexLock(void);
    695     WOLFSSL_LOCAL int wolfSSL_HwPkMutexUnLock(void);
    696 #else
    697     #define wolfSSL_HwPkMutexInit     wolfSSL_CryptHwMutexInit
    698     #define wolfSSL_HwPkMutexLock     wolfSSL_CryptHwMutexLock
    699     #define wolfSSL_HwPkMutexUnLock   wolfSSL_CryptHwMutexUnLock
    700 #endif /* !defined(NO_PK_MUTEX) && defined(WOLFSSL_ALGO_HW_MUTEX) */
    701 
    702 /* Mutex functions */
    703 WOLFSSL_API int wc_InitMutex(wolfSSL_Mutex* m);
    704 WOLFSSL_API wolfSSL_Mutex* wc_InitAndAllocMutex(void);
    705 WOLFSSL_API int wc_FreeMutex(wolfSSL_Mutex* m);
    706 WOLFSSL_API int wc_LockMutex(wolfSSL_Mutex* m);
    707 WOLFSSL_API int wc_UnLockMutex(wolfSSL_Mutex* m);
    708 /* RwLock functions. Fallback to Mutex when not implemented explicitly. */
    709 WOLFSSL_API int wc_InitRwLock(wolfSSL_RwLock* m);
    710 WOLFSSL_API int wc_FreeRwLock(wolfSSL_RwLock* m);
    711 WOLFSSL_API int wc_LockRwLock_Wr(wolfSSL_RwLock* m);
    712 WOLFSSL_API int wc_LockRwLock_Rd(wolfSSL_RwLock* m);
    713 WOLFSSL_API int wc_UnLockRwLock(wolfSSL_RwLock* m);
    714 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
    715 /* dynamically set which mutex to use. unlock / lock is controlled by flag */
    716 typedef void (mutex_cb)(int flag, int type, const char* file, int line);
    717 
    718 WOLFSSL_API int wc_LockMutex_ex(int flag, int type, const char* file, int line);
    719 WOLFSSL_API int wc_SetMutexCb(mutex_cb* cb);
    720 WOLFSSL_API mutex_cb* wc_GetMutexCb(void);
    721 #endif
    722 
    723 /* main crypto initialization function */
    724 WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Init(void);
    725 WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
    726 
    727 #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
    728     WOLFSSL_API long wolfCrypt_heap_peakAllocs_checkpoint(void);
    729     WOLFSSL_API long wolfCrypt_heap_peakBytes_checkpoint(void);
    730 #endif
    731 
    732 
    733 /* FILESYSTEM SECTION */
    734 /* filesystem abstraction layer, used by ssl.c */
    735 #ifndef NO_FILESYSTEM
    736 
    737 #if defined(EBSNET)
    738     #include "vfapi.h"
    739     #include "vfile.h"
    740 
    741     int ebsnet_fseek(int a, long b, int c); /* Not prototyped in vfile.h per
    742                                              * EBSnet feedback */
    743 
    744     #define XFILE                    int
    745     #define XFOPEN(NAME, MODE)       vf_open((const char *)NAME, VO_RDONLY, 0)
    746     #define XFSEEK                   ebsnet_fseek
    747     #define XFTELL                   vf_tell
    748     #define XFREAD(BUF, SZ, AMT, FD) vf_read(FD, BUF, SZ*AMT)
    749     #define XFWRITE(BUF, SZ, AMT, FD) vf_write(FD, BUF, SZ*AMT)
    750     #define XFCLOSE                  vf_close
    751     #define XSEEK_SET                VSEEK_SET
    752     #define XSEEK_END                VSEEK_END
    753     #define XBADFILE                 -1
    754     #define XFGETS(b,s,f)            -2 /* Not ported yet */
    755     #define XSNPRINTF rtp_snprintf
    756     #define XFPRINTF fprintf
    757 
    758 #elif defined(LSR_FS)
    759     #include <fs.h>
    760     #define XFILE                   struct fs_file*
    761     #define XFOPEN(NAME, MODE)      fs_open((char*)NAME)
    762     #define XFSEEK(F, O, W)         (void)F
    763     #define XFTELL(F)               (F)->len
    764     #define XFREAD(BUF, SZ, AMT, F) fs_read(F, (char*)BUF, SZ*AMT)
    765     #define XFWRITE(BUF, SZ, AMT, F) fs_write(F, (char*)BUF, SZ*AMT)
    766     #define XFCLOSE                 fs_close
    767     #define XSEEK_SET               0
    768     #define XSEEK_END               0
    769     #define XBADFILE                NULL
    770     #define XFGETS(b,s,f)           -2 /* Not ported yet */
    771 
    772 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
    773     #define XFILE                   MQX_FILE_PTR
    774     #define XFOPEN                  fopen
    775     #define XFSEEK                  fseek
    776     #define XFTELL                  ftell
    777     #define XFREAD                  fread
    778     #define XFWRITE                 fwrite
    779     #define XFCLOSE                 fclose
    780     #define XSEEK_SET               IO_SEEK_SET
    781     #define XSEEK_END               IO_SEEK_END
    782     #define XBADFILE                NULL
    783     #define XFGETS                  fgets
    784 
    785 #elif defined(WOLFSSL_DEOS)
    786     #define NO_FILESYSTEM
    787     #warning "TODO - DDC-I Certifiable Fast File System for Deos is not integrated"
    788 #elif defined(MICRIUM)
    789     #include <fs_api.h>
    790     #define XFILE      FS_FILE*
    791     #define XFOPEN     fs_fopen
    792     #define XFSEEK     fs_fseek
    793     #define XFTELL     fs_ftell
    794     #define XFREAD     fs_fread
    795     #define XFWRITE    fs_fwrite
    796     #define XFCLOSE    fs_fclose
    797     #define XSEEK_SET  FS_SEEK_SET
    798     #define XSEEK_END  FS_SEEK_END
    799     #define XBADFILE   NULL
    800     #define XFGETS(b,s,f) -2 /* Not ported yet */
    801 
    802 #elif defined(WOLFSSL_NUCLEUS_1_2)
    803     #include "fal/inc/fal.h"
    804     #define XFILE      FILE*
    805     #define XFOPEN     fopen
    806     #define XFSEEK     fseek
    807     #define XFTELL     ftell
    808     #define XFREAD     fread
    809     #define XFWRITE    fwrite
    810     #define XFCLOSE    fclose
    811     #define XSEEK_SET  PSEEK_SET
    812     #define XSEEK_END  PSEEK_END
    813     #define XBADFILE   NULL
    814 
    815 #elif defined(WOLFSSL_APACHE_MYNEWT)
    816     #include <fs/fs.h>
    817     #define XFILE  struct fs_file*
    818 
    819     #define XFOPEN     mynewt_fopen
    820     #define XFSEEK     mynewt_fseek
    821     #define XFTELL     mynewt_ftell
    822     #define XFREAD     mynewt_fread
    823     #define XFWRITE    mynewt_fwrite
    824     #define XFCLOSE    mynewt_fclose
    825     #define XSEEK_SET  0
    826     #define XSEEK_END  2
    827     #define XBADFILE   NULL
    828     #define XFGETS(b,s,f) -2 /* Not ported yet */
    829 
    830 #elif defined(WOLFSSL_ZEPHYR)
    831     #include <zephyr/fs/fs.h>
    832 
    833     #define XFILE      struct fs_file_t*
    834 
    835     /* These are our wrappers for opening and closing files to
    836      * make the API more POSIX like. */
    837     XFILE z_fs_open(const char* filename, const char* mode);
    838     int z_fs_close(XFILE file);
    839     int z_fs_rewind(XFILE file);
    840 
    841     #define XFOPEN              z_fs_open
    842     #define XFCLOSE             z_fs_close
    843     #define XFFLUSH             fs_sync
    844     #define XFSEEK              fs_seek
    845     #define XFTELL              fs_tell
    846     #define XFREWIND            z_fs_rewind
    847     #define XFREAD(P,S,N,F)     fs_read(F, P, S*N)
    848     #define XFWRITE(P,S,N,F)    fs_write(F, P, S*N)
    849     #define XSEEK_SET           FS_SEEK_SET
    850     #define XSEEK_END           FS_SEEK_END
    851     #define XBADFILE            NULL
    852     #define XFGETS(b,s,f)       -2 /* Not ported yet */
    853 
    854     #define XSTAT               fs_stat
    855     #define XSTAT_TYPE          struct fs_dirent
    856     #define XS_ISREG(s)         (s == FS_DIR_ENTRY_FILE)
    857     #define SEPARATOR_CHAR      ':'
    858 
    859 #elif defined(WOLFSSL_TELIT_M2MB)
    860     #define XFILE                    INT32
    861     #define XFOPEN(NAME, MODE)       m2mb_fs_open((NAME), 0, (MODE))
    862     #define XFSEEK(F, O, W)          m2mb_fs_lseek((F), (O), (W))
    863     #define XFTELL(F)                m2mb_fs_lseek((F), 0, M2MB_SEEK_END)
    864     #define XFREAD(BUF, SZ, AMT, F)  m2mb_fs_read((F), (BUF), (SZ)*(AMT))
    865     #define XFWRITE(BUF, SZ, AMT, F) m2mb_fs_write((F), (BUF), (SZ)*(AMT))
    866     #define XFCLOSE                  m2mb_fs_close
    867     #define XSEEK_SET                M2MB_SEEK_SET
    868     #define XSEEK_END                M2MB_SEEK_END
    869     #define XBADFILE                 -1
    870     #define XFGETS(b,s,f)            -2 /* Not ported yet */
    871 
    872 #elif defined (WOLFSSL_XILINX)
    873     #include "xsdps.h"
    874     #include "ff.h"
    875 
    876     /* workaround to declare variable and provide type */
    877     #define XFILE                    FIL curFile; FIL*
    878     #define XFOPEN(NAME, MODE)       ({ FRESULT res; res = f_open(&curFile, (NAME), (FA_OPEN_ALWAYS | FA_WRITE | FA_READ)); (res == FR_OK) ? &curFile : NULL; })
    879     #define XFSEEK(F, O, W)          f_lseek((F), (O))
    880     #define XFTELL(F)                f_tell((F))
    881     #define XFREAD(BUF, SZ, AMT, F)  ({ FRESULT res; UINT br; res = f_read((F), (BUF), (SZ)*(AMT), &br); (void)br; res; })
    882     #define XFWRITE(BUF, SZ, AMT, F) ({ FRESULT res; UINT written; res = f_write((F), (BUF), (SZ)*(AMT), &written); (void)written; res; })
    883     #define XFCLOSE(F)               f_close((F))
    884     #define XSEEK_SET                0
    885     #define XSEEK_END                0
    886     #define XBADFILE                 NULL
    887     #define XFGETS(b,s,f)            f_gets((b), (s), (f))
    888 #elif defined (_WIN32_WCE)
    889     /* stdio, WINCE case */
    890     #include <stdio.h>
    891     #define XFILE      FILE*
    892     #define XFOPEN     fopen
    893     #define XFDOPEN    fdopen
    894     #define XFSEEK     fseek
    895     #define XFTELL     ftell
    896     #define XFREAD     fread
    897     #define XFWRITE    fwrite
    898     #define XFCLOSE    fclose
    899     #define XSEEK_SET  SEEK_SET
    900     #define XSEEK_END  SEEK_END
    901     #define XBADFILE   NULL
    902     #define XFGETS     fgets
    903     #define XVSNPRINTF _vsnprintf
    904 
    905 #elif defined(FUSION_RTOS)
    906     #include <fclstdio.h>
    907     #include <fclunistd.h>
    908     #include <fcldirent.h>
    909     #include <sys/fclstat.h>
    910     #include <fclstring.h>
    911     #include <fcl_os.h>
    912     #define XFILE     FCL_FILE*
    913     #define XFOPEN    FCL_FOPEN
    914     #define XFSEEK    FCL_FSEEK
    915     #define XFTELL    FCL_FTELL
    916     #define XFREAD    FCL_FREAD
    917     #define XFWRITE   FCL_FWRITE
    918     #define XFCLOSE   FCL_FCLOSE
    919     #define XSEEK_SET SEEK_SET
    920     #define XSEEK_END SEEK_END
    921     #define XBADFILE  NULL
    922     #define XFGETS    FCL_FGETS
    923     #define XFPUTS    FCL_FPUTS
    924     #define XFPRINTF  FCL_FPRINTF
    925     #define XVFPRINTF FCL_VFPRINTF
    926     #define XVSNPRINTF  FCL_VSNPRINTF
    927     #define XSNPRINTF  FCL_SNPRINTF
    928     #define XSPRINTF  FCL_SPRINTF
    929     #define DIR       FCL_DIR
    930     #define stat      FCL_STAT
    931     #define opendir   FCL_OPENDIR
    932     #define closedir  FCL_CLOSEDIR
    933     #define readdir   FCL_READDIR
    934     #define dirent    fclDirent
    935     #define strncasecmp FCL_STRNCASECMP
    936 
    937     /* FUSION SPECIFIC ERROR CODE */
    938     #define FUSION_IO_SEND_E FCL_EWOULDBLOCK
    939 
    940 #elif defined(WOLFSSL_USER_FILESYSTEM)
    941     /* To be defined in user_settings.h */
    942 #else
    943     /* stdio, default case */
    944     #include <stdio.h>
    945     #define XFILE      FILE*
    946     #if defined(WOLFSSL_MDK_ARM)
    947         extern FILE * wolfSSL_fopen(const char *name, const char *mode) ;
    948         #define XFOPEN     wolfSSL_fopen
    949     #else
    950         #define XFOPEN     fopen
    951     #endif
    952     #define XFDOPEN    fdopen
    953     #define XFSEEK     fseek
    954     #define XFTELL     ftell
    955     #define XFREAD     fread
    956     #define XFWRITE    fwrite
    957     #define XFCLOSE    fclose
    958     #define XSEEK_SET  SEEK_SET
    959     #define XSEEK_END  SEEK_END
    960     #define XBADFILE   NULL
    961     #define XFGETS     fgets
    962     #define XFPRINTF   fprintf
    963     #define XFFLUSH    fflush
    964     #define XFEOF(fp)  feof(fp)
    965     #define XFERROR(fp) ferror(fp)
    966     #define XCLEARERR(fp) clearerr(fp)
    967 
    968     #if !defined(NO_WOLFSSL_DIR)\
    969         && !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
    970         #if defined(__WATCOMC__)
    971             #include <unistd.h>
    972             #include <sys/stat.h>
    973             #define XWRITE      write
    974             #define XREAD       read
    975             #define XCLOSE      close
    976             #define XSTAT       stat
    977             #define XS_ISREG(s) S_ISREG(s)
    978             #if defined(__UNIX__)
    979                 #include <dirent.h>
    980                 #define SEPARATOR_CHAR ':'
    981             #else
    982                 #include <direct.h>
    983                 #define SEPARATOR_CHAR ';'
    984             #endif
    985             #if defined(__NT__)
    986                 #define XALTHOMEVARNAME "USERPROFILE"
    987             #endif
    988         #elif defined(USE_WINDOWS_API)
    989             #include <io.h>
    990             #include <sys/stat.h>
    991             #ifndef XSTAT
    992                 #define XSTAT       _stat
    993             #endif
    994             #define XS_ISREG(s) (s & _S_IFREG)
    995             #define SEPARATOR_CHAR ';'
    996             #define XWRITE      _write
    997             #define XREAD       _read
    998             #define XALTHOMEVARNAME "USERPROFILE"
    999 
   1000         #elif defined(ARDUINO)
   1001             #ifndef XSTAT
   1002                 #define XSTAT       _stat
   1003             #endif
   1004             #define XS_ISREG(s) (s & _S_IFREG)
   1005             #define SEPARATOR_CHAR ';'
   1006 
   1007         #elif defined(INTIME_RTOS)
   1008             #include <sys/stat.h>
   1009             #ifndef XSTAT
   1010             #define XSTAT _stat64
   1011             #endif
   1012             #define XS_ISREG(s) S_ISREG(s)
   1013             #define SEPARATOR_CHAR ';'
   1014             #define XWRITE      write
   1015             #define XREAD       read
   1016             #define XCLOSE      close
   1017 
   1018         #elif defined(WOLFSSL_TELIT_M2MB)
   1019             #ifndef XSTAT
   1020             #define XSTAT       m2mb_fs_stat
   1021             #endif
   1022             #define XS_ISREG(s) (s & M2MB_S_IFREG)
   1023             #define SEPARATOR_CHAR ':'
   1024 
   1025         #else
   1026             #include <dirent.h>
   1027             #include <unistd.h>
   1028             #include <sys/stat.h>
   1029             #define XWRITE      write
   1030             #define XREAD       read
   1031             #define XCLOSE      close
   1032             #ifndef XSTAT
   1033             #define XSTAT       stat
   1034             #endif
   1035             #define XS_ISREG(s) S_ISREG(s)
   1036             #define SEPARATOR_CHAR ':'
   1037         #endif
   1038 
   1039         #ifndef XSTAT_TYPE
   1040             #define XSTAT_TYPE struct XSTAT
   1041         #endif
   1042     #endif /* !NO_WOLFSSL_DIR !WOLFSSL_NUCLEUS !WOLFSSL_NUCLEUS_1_2 */
   1043 #endif
   1044 
   1045     #ifndef MAX_FILENAME_SZ
   1046         #define MAX_FILENAME_SZ (260 + 1) /* max file name length */
   1047     #endif
   1048     #ifndef MAX_PATH
   1049         #define MAX_PATH (260 + 1)
   1050     #endif
   1051     #ifndef XFEOF
   1052         #define XFEOF(fp)  0
   1053     #endif
   1054     #ifndef XFERROR
   1055         #define XFERROR(fp) 0
   1056     #endif
   1057     #ifndef XCLEARERR
   1058         #define XCLEARERR(fp) WC_DO_NOTHING
   1059     #endif
   1060 
   1061     WOLFSSL_LOCAL int wc_FileLoad(const char* fname, unsigned char** buf,
   1062         size_t* bufLen, void* heap);
   1063 
   1064 #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS) && \
   1065     !defined(WOLFSSL_NUCLEUS_1_2)
   1066     typedef struct ReadDirCtx {
   1067     #ifdef USE_WINDOWS_API
   1068         WIN32_FIND_DATAA FindFileData;
   1069         HANDLE hFind;
   1070         XSTAT_TYPE s;
   1071     #elif defined(WOLFSSL_ZEPHYR)
   1072         struct fs_dirent entry;
   1073         struct fs_dir_t  dir;
   1074         struct fs_dirent s;
   1075         struct fs_dir_t* dirp;
   1076 
   1077     #elif defined(WOLFSSL_TELIT_M2MB)
   1078         M2MB_DIR_T* dir;
   1079         struct M2MB_DIRENT* entry;
   1080         struct M2MB_STAT s;
   1081     #elif defined(INTIME_RTOS)
   1082         struct stat64 s;
   1083         struct _find64 FindFileData;
   1084         #define IntimeFindFirst(name, data) (0 == _findfirst64(name, data))
   1085         #define IntimeFindNext(data)  (0 == _findnext64(data))
   1086         #define IntimeFindClose(data) (0 == _findclose64(data))
   1087         #define IntimeFilename(ctx)   ctx->FindFileData.f_filename
   1088     #elif defined(ARDUINO)
   1089         /* TODO: board specific features */
   1090     #else
   1091         struct dirent* entry;
   1092         DIR*   dir;
   1093         XSTAT_TYPE s;
   1094     #endif
   1095         char name[MAX_FILENAME_SZ];
   1096     } ReadDirCtx;
   1097 
   1098     #define WC_READDIR_NOFILE (-1)
   1099 
   1100     WOLFSSL_API int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name);
   1101     WOLFSSL_API int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name);
   1102     WOLFSSL_API void wc_ReadDirClose(ReadDirCtx* ctx);
   1103 #endif /* !NO_WOLFSSL_DIR */
   1104     #define WC_ISFILEEXIST_NOFILE (-1)
   1105 
   1106     WOLFSSL_API int wc_FileExists(const char* fname);
   1107 
   1108 #endif /* !NO_FILESYSTEM */
   1109 
   1110 /* Defaults, user may over-ride with user_settings.h or in a porting section
   1111  * above
   1112  */
   1113 #ifndef XVFPRINTF
   1114     #define XVFPRINTF  vfprintf
   1115 #endif
   1116 #ifndef XVSNPRINTF
   1117     #define XVSNPRINTF vsnprintf
   1118 #endif
   1119 #ifndef XFPUTS
   1120     #define XFPUTS     fputs
   1121 #endif
   1122 #ifndef XSPRINTF
   1123     #define XSPRINTF   sprintf
   1124 #endif
   1125 
   1126 #ifdef USE_WINDOWS_API
   1127     #ifndef SOCKET_T
   1128         #ifdef __MINGW64__
   1129             typedef size_t SOCKET_T;
   1130         #else
   1131             typedef unsigned int SOCKET_T;
   1132         #endif
   1133     #endif
   1134     #ifndef SOCKET_INVALID
   1135         #define SOCKET_INVALID INVALID_SOCKET
   1136     #endif
   1137 #else
   1138     #ifndef SOCKET_T
   1139         typedef int SOCKET_T;
   1140     #endif
   1141     #ifndef SOCKET_INVALID
   1142         #define SOCKET_INVALID (-1)
   1143     #endif
   1144 #endif
   1145 
   1146 /* MIN/MAX MACRO SECTION */
   1147 /* Windows API defines its own min() macro. */
   1148 #if defined(USE_WINDOWS_API)
   1149     #if defined(min) || defined(WOLFSSL_MYSQL_COMPATIBLE)
   1150         #undef  WOLFSSL_HAVE_MIN
   1151         #define WOLFSSL_HAVE_MIN
   1152     #endif /* min */
   1153     #if defined(max) || defined(WOLFSSL_MYSQL_COMPATIBLE)
   1154         #undef  WOLFSSL_HAVE_MAX
   1155         #define WOLFSSL_HAVE_MAX
   1156     #endif /* max */
   1157 #endif /* USE_WINDOWS_API */
   1158 
   1159 #ifdef __QNXNTO__
   1160     #define WOLFSSL_HAVE_MIN
   1161     #define WOLFSSL_HAVE_MAX
   1162 #endif
   1163 
   1164 /* TIME SECTION */
   1165 /* Time functions */
   1166 #ifndef NO_ASN_TIME
   1167 #if defined(USER_TIME)
   1168     /* Use our gmtime and time_t/struct tm types.
   1169        Only needs seconds since EPOCH using XTIME function.
   1170        time_t XTIME(time_t * timer) {}
   1171     */
   1172     #define WOLFSSL_GMTIME
   1173     #ifndef HAVE_TM_TYPE
   1174         #define USE_WOLF_TM
   1175     #endif
   1176     #ifndef HAVE_TIME_T_TYPE
   1177         #define USE_WOLF_TIME_T
   1178     #endif
   1179 
   1180 #elif defined(TIME_OVERRIDES)
   1181     /* Override XTIME() and XGMTIME() functionality.
   1182        Requires user to provide these functions:
   1183         time_t XTIME(time_t * timer) {}
   1184         struct tm* XGMTIME(const time_t* timer, struct tm* tmp) {}
   1185     */
   1186     #ifndef HAVE_TIME_T_TYPE
   1187         #define USE_WOLF_TIME_T
   1188     #endif
   1189     #ifndef HAVE_TM_TYPE
   1190         #define USE_WOLF_TM
   1191     #endif
   1192     #define NEED_TMP_TIME
   1193 
   1194 #elif defined(WOLFSSL_XILINX)
   1195     #ifndef XTIME
   1196         #define XTIME(t1)       xilinx_time((t1))
   1197     #endif
   1198     #include <time.h>
   1199     time_t xilinx_time(time_t * timer);
   1200 
   1201 #elif defined(HAVE_RTP_SYS)
   1202     #include "os.h"           /* dc_rtc_api needs    */
   1203     #include "dc_rtc_api.h"   /* to get current time */
   1204 
   1205     /* uses partial <time.h> structures */
   1206     #define XTIME(tl)       (0)
   1207     #define XGMTIME(c, t)   rtpsys_gmtime((c))
   1208 
   1209 #elif defined(WOLFSSL_DEOS) || defined(WOLFSSL_DEOS_RTEMS)
   1210     #include <time.h>
   1211         #ifndef XTIME
   1212             extern time_t deos_time(time_t* timer);
   1213             #define XTIME(t1) deos_time((t1))
   1214         #endif
   1215 #elif defined(MICRIUM)
   1216     #include <clk.h>
   1217     #include <time.h>
   1218     #define XTIME(t1)       micrium_time((t1))
   1219     #define WOLFSSL_GMTIME
   1220 
   1221 #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
   1222     #include <time.h>
   1223     extern time_t pic32_time(time_t* timer);
   1224     #define XTIME(t1)       pic32_time((t1))
   1225     #define XGMTIME(c, t)   gmtime((c))
   1226 
   1227 #elif defined(FREESCALE_RTC)
   1228     #include <time.h>
   1229         #include "fsl_rtc.h"
   1230         #ifndef XTIME
   1231         #define XTIME(t1) fsl_time((t1))
   1232     #endif
   1233 #elif defined(FREESCALE_SNVS_RTC)
   1234     #include <time.h>
   1235     #include "fsl_snvs_hp.h"
   1236     time_t fsl_time(time_t* t);
   1237     #ifndef XTIME
   1238         #define XTIME(t1) fsl_time((t1))
   1239     #endif
   1240 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
   1241     #ifdef FREESCALE_MQX_4_0
   1242         #include <time.h>
   1243         extern time_t mqx_time(time_t* timer);
   1244     #else
   1245         #define HAVE_GMTIME_R
   1246     #endif
   1247     #define XTIME(t1)       mqx_time((t1))
   1248 
   1249 #elif defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS)
   1250     #include <time.h>
   1251     #ifndef XTIME
   1252         /*extern time_t ksdk_time(time_t* timer);*/
   1253         #define XTIME(t1)   ksdk_time((t1))
   1254     #endif
   1255     #define XGMTIME(c, t)   gmtime((c))
   1256 
   1257 #elif defined(WOLFSSL_ATMEL) && defined(WOLFSSL_ATMEL_TIME)
   1258     #define XTIME(t1)       atmel_get_curr_time_and_date((t1))
   1259     #define WOLFSSL_GMTIME
   1260     #define USE_WOLF_TM
   1261     #define USE_WOLF_TIME_T
   1262 
   1263 #elif defined(WOLFSSL_WICED)
   1264     #include <time.h>
   1265     time_t wiced_pseudo_unix_epoch_time(time_t * timer);
   1266     #define XTIME(t1)       wiced_pseudo_unix_epoch_time((t1))
   1267     #define HAVE_GMTIME_R
   1268 
   1269 #elif defined(IDIRECT_DEV_TIME)
   1270     /*Gets the timestamp from cloak software owned by VT iDirect
   1271     in place of time() from <time.h> */
   1272     #include <time.h>
   1273     #define XTIME(t1)       idirect_time((t1))
   1274     #define XGMTIME(c, t)   gmtime((c))
   1275 
   1276 #elif defined(_WIN32_WCE)
   1277     #define _WINSOCKAPI_ /* block inclusion of winsock.h header file */
   1278     #include <windows.h>
   1279     #undef _WINSOCKAPI_ /* undefine it for MINGW winsock2.h header file */
   1280     #include <stdlib.h> /* For file system */
   1281 
   1282     time_t windows_time(time_t* timer);
   1283 
   1284     #define FindNextFileA(h, d) FindNextFile(h, (LPWIN32_FIND_DATAW) d)
   1285     #define FindFirstFileA(fn, d) FindFirstFile((LPCWSTR) fn, \
   1286                                                 (LPWIN32_FIND_DATAW) d)
   1287     #define XTIME(t1)       windows_time((t1))
   1288     #define WOLFSSL_GMTIME
   1289 
   1290     /* if struct tm is not defined in WINCE SDK */
   1291     #ifndef _TM_DEFINED
   1292         struct tm {
   1293             int tm_sec;     /* seconds */
   1294             int tm_min;     /* minutes */
   1295             int tm_hour;    /* hours */
   1296             int tm_mday;    /* day of month (month specific) */
   1297             int tm_mon;     /* month */
   1298             int tm_year;    /* year */
   1299             int tm_wday;    /* day of week (out of 1-7)*/
   1300             int tm_yday;    /* day of year (out of 365) */
   1301             int tm_isdst;   /* is it daylight savings */
   1302             };
   1303             #define _TM_DEFINED
   1304     #endif
   1305 
   1306 #elif defined(WOLFSSL_APACHE_MYNEWT)
   1307     #include "os/os_time.h"
   1308     typedef long time_t;
   1309     extern time_t mynewt_time(time_t* timer);
   1310     #define XTIME(t1)       mynewt_time((t1))
   1311     #define WOLFSSL_GMTIME
   1312     #define USE_WOLF_TM
   1313     #define USE_WOLF_TIME_T
   1314 
   1315 #elif defined(WOLFSSL_ZEPHYR)
   1316     #include <version.h>
   1317     #ifndef _POSIX_C_SOURCE
   1318         #if KERNEL_VERSION_NUMBER >= 0x30100
   1319             #include <zephyr/posix/time.h>
   1320         #else
   1321             #include <posix/time.h>
   1322         #endif
   1323     #else
   1324         #include <time.h>
   1325     #endif
   1326 
   1327     #if defined(CONFIG_RTC)
   1328         #if defined(CONFIG_PICOLIBC) || defined(CONFIG_NEWLIB_LIBC)
   1329             #include <zephyr/drivers/rtc.h>
   1330         #else
   1331             #warning "RTC support needs picolibc or newlib (nano)"
   1332         #endif
   1333     #endif
   1334 
   1335     time_t z_time(time_t *timer);
   1336 
   1337     #define XTIME(tl)       z_time((tl))
   1338     #define XGMTIME(c, t)   gmtime((c))
   1339 
   1340 #elif defined(WOLFSSL_TELIT_M2MB)
   1341     typedef long time_t;
   1342     extern time_t m2mb_xtime(time_t * timer);
   1343     #define XTIME(tl)       m2mb_xtime((tl))
   1344     #ifdef WOLFSSL_TLS13
   1345         extern time_t m2mb_xtime_ms(time_t * timer);
   1346         #define XTIME_MS(tl)    m2mb_xtime_ms((tl))
   1347     #endif
   1348     #ifndef NO_CRYPT_BENCHMARK
   1349         extern double m2mb_xtime_bench(int reset);
   1350         #define WOLFSSL_CURRTIME_REMAP m2mb_xtime_bench
   1351     #endif
   1352     #define XGMTIME(c, t)   gmtime((c))
   1353     #define WOLFSSL_GMTIME
   1354     #define USE_WOLF_TM
   1355 
   1356 
   1357 #elif defined(WOLFSSL_LINUXKM)
   1358 
   1359     /* definitions are in linuxkm/linuxkm_wc_port.h */
   1360 
   1361 #elif defined(HAL_RTC_MODULE_ENABLED)
   1362     #include <time.h>
   1363     WOLFSSL_LOCAL time_t stm32_hal_time(time_t* t1);
   1364     #define XTIME(t1) stm32_hal_time(t1)
   1365     #define WOLFSSL_GMTIME
   1366 #else
   1367     /* default */
   1368     /* uses complete <time.h> facility */
   1369     #include <time.h>
   1370     #if defined(HAVE_SYS_TIME_H)
   1371         #include <sys/time.h>
   1372     #endif
   1373 
   1374     /* PowerPC time_t is int */
   1375     #if defined(__PPC__) || defined(__ppc__)
   1376         #define TIME_T_NOT_64BIT
   1377     #endif
   1378 
   1379     #define XMKTIME(tm) mktime(tm)
   1380     #define XDIFFTIME(to, from) difftime(to, from)
   1381 #endif
   1382 
   1383 #ifdef SIZEOF_TIME_T
   1384     /* check if size of time_t from autoconf is less than 8 bytes (64bits) */
   1385     #if SIZEOF_TIME_T < 8
   1386         #undef  TIME_T_NOT_64BIT
   1387         #define TIME_T_NOT_64BIT
   1388     #endif
   1389 #endif
   1390 #ifdef TIME_T_NOT_LONG
   1391     /* one old reference to TIME_T_NOT_LONG in GCC-ARM example README
   1392      * this keeps support for the old macro name */
   1393     #undef  TIME_T_NOT_64BIT
   1394     #define TIME_T_NOT_64BIT
   1395 #endif
   1396 
   1397 /* Map default time functions */
   1398 #if !defined(XTIME) && !defined(TIME_OVERRIDES) && !defined(USER_TIME)
   1399     #ifdef TEST_BEFORE_DATE
   1400     #define XTIME(tl)       (946681200UL) /* Jan 1, 2000 */
   1401     #else
   1402     #define XTIME(tl)       time((tl))
   1403     #endif
   1404 #endif
   1405 
   1406 #if defined(WOLFSSL_GMTIME) && !defined(HAVE_GMTIME_R)
   1407     #define HAVE_GMTIME_R
   1408 #endif
   1409 
   1410 #if !defined(XGMTIME) && !defined(TIME_OVERRIDES)
   1411     /* Always use gmtime_r if available. */
   1412     #if defined(HAVE_GMTIME_S)
   1413         /* reentrant version */
   1414         #define XGMTIME(c, t)   gmtime_s((c), (t))
   1415         #define NEED_TMP_TIME
   1416     #elif defined(HAVE_GMTIME_R)
   1417         #define XGMTIME(c, t)   gmtime_r((c), (t))
   1418         #define NEED_TMP_TIME
   1419     #else
   1420         #define XGMTIME(c, t)   gmtime((c))
   1421     #endif
   1422 #endif
   1423 #if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE)
   1424     #define USE_WOLF_VALIDDATE
   1425     #define XVALIDATE_DATE(d, f, t) wc_ValidateDate((d), (f), (t))
   1426 #endif
   1427 
   1428 /* wolf struct tm and time_t */
   1429 #if defined(USE_WOLF_TM)
   1430     struct tm {
   1431         int  tm_sec;     /* seconds after the minute [0-60] */
   1432         int  tm_min;     /* minutes after the hour [0-59] */
   1433         int  tm_hour;    /* hours since midnight [0-23] */
   1434         int  tm_mday;    /* day of the month [1-31] */
   1435         int  tm_mon;     /* months since January [0-11] */
   1436         int  tm_year;    /* years since 1900 */
   1437         int  tm_wday;    /* days since Sunday [0-6] */
   1438         int  tm_yday;    /* days since January 1 [0-365] */
   1439         int  tm_isdst;   /* Daylight Savings Time flag */
   1440         long tm_gmtoff;  /* offset from CUT in seconds */
   1441         char *tm_zone;   /* timezone abbreviation */
   1442     };
   1443 #endif /* USE_WOLF_TM */
   1444 #if defined(USE_WOLF_TIME_T)
   1445     typedef long time_t;
   1446 #endif
   1447 #if defined(USE_WOLF_SUSECONDS_T)
   1448     typedef long suseconds_t;
   1449 #endif
   1450 #if defined(USE_WOLF_TIMEVAL_T)
   1451     struct timeval
   1452     {
   1453         time_t tv_sec;
   1454         suseconds_t tv_usec;
   1455     };
   1456 #endif
   1457 
   1458     /* forward declarations */
   1459 #if defined(USER_TIME)
   1460     struct tm* gmtime(const time_t* timer);
   1461     extern time_t XTIME(time_t * timer);
   1462 
   1463     #ifdef STACK_TRAP
   1464         /* for stack trap tracking, don't call os gmtime on OS X/linux,
   1465            uses a lot of stack spce */
   1466         extern time_t time(time_t * timer);
   1467         #define XTIME(tl)  time((tl))
   1468     #endif /* STACK_TRAP */
   1469 
   1470 #elif defined(TIME_OVERRIDES)
   1471     extern time_t XTIME(time_t * timer);
   1472     extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp);
   1473 #elif defined(WOLFSSL_GMTIME)
   1474     struct tm* gmtime(const time_t* timer);
   1475     struct tm* gmtime_r(const time_t* timer, struct tm *ret);
   1476 #endif
   1477 #endif /* !NO_ASN_TIME */
   1478 
   1479 
   1480 #if (!defined(WOLFSSL_LEANPSK) && !defined(STRING_USER)) || \
   1481     defined(USE_WOLF_STRNSTR)
   1482     char* mystrnstr(const char* s1, const char* s2, unsigned int n);
   1483 #endif
   1484 
   1485 #ifndef FILE_BUFFER_SIZE
   1486     /* default static file buffer size for input, will use dynamic buffer if
   1487      * not big enough */
   1488     #ifdef WOLFSSL_CERT_EXT
   1489     #define FILE_BUFFER_SIZE (3*1024)
   1490     #else
   1491     #define FILE_BUFFER_SIZE (1*1024)
   1492     #endif
   1493 #endif
   1494 
   1495 #ifdef HAVE_CAVIUM_OCTEON_SYNC
   1496     /* By default, the OCTEON's global variables are all thread local. This
   1497      * tag allows them to be shared between threads. */
   1498     #include "cvmx-platform.h"
   1499     #define WC_THREADSHARED CVMX_SHARED
   1500 #else
   1501     #define WC_THREADSHARED
   1502 #endif
   1503 
   1504 #ifdef WOLFSSL_DSP
   1505     #include "wolfssl_dsp.h"
   1506 
   1507     /* callbacks for setting handle */
   1508     typedef int (*wolfSSL_DSP_Handle_cb)(remote_handle64 *handle, int finished,
   1509                                          void *ctx);
   1510     WOLFSSL_API int wolfSSL_GetHandleCbSet();
   1511     WOLFSSL_API int wolfSSL_SetHandleCb(wolfSSL_DSP_Handle_cb in);
   1512     WOLFSSL_LOCAL int wolfSSL_InitHandle();
   1513     WOLFSSL_LOCAL void wolfSSL_CleanupHandle();
   1514 #endif
   1515 
   1516 #ifdef WOLFSSL_SCE
   1517     #ifndef WOLFSSL_SCE_GSCE_HANDLE
   1518         #define WOLFSSL_SCE_GSCE_HANDLE g_sce
   1519     #endif
   1520 #endif
   1521 
   1522 #ifdef WOLF_C99
   1523     /* use alternate keyword for compatibility with -std=c99 */
   1524     #define XASM_VOLATILE(a) __asm__ volatile(a)
   1525 #elif defined(__IAR_SYSTEMS_ICC__)
   1526     #define XASM_VOLATILE(a) asm volatile(a)
   1527 #elif defined(__KEIL__)
   1528     #define XASM_VOLATILE(a) __asm volatile(a)
   1529 #else
   1530     #define XASM_VOLATILE(a) __asm__ __volatile__(a)
   1531 #endif
   1532 
   1533 #ifndef WOLFSSL_NO_FENCE
   1534     #ifdef XFENCE
   1535         /* use user-supplied XFENCE definition. */
   1536     #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
   1537           !defined(__STDC_NO_ATOMICS__)
   1538         #include <stdatomic.h>
   1539         #define XFENCE() atomic_thread_fence(memory_order_seq_cst)
   1540     #elif defined(__GNUC__) && (__GNUC__ == 4) && \
   1541           defined(__GNUC_MINOR__) && (__GNUC_MINOR__ >= 1)
   1542         #define XFENCE() __sync_synchronize()
   1543     #elif (defined(__GNUC__) && (__GNUC__ >= 5)) || defined (__clang__)
   1544         #define XFENCE() __atomic_thread_fence(__ATOMIC_SEQ_CST)
   1545     #elif defined(WOLFSSL_NO_ASM)
   1546         #define XFENCE() WC_DO_NOTHING
   1547     #elif defined (__i386__) || defined(__x86_64__)
   1548         #define XFENCE() XASM_VOLATILE("lfence")
   1549     #elif defined (__arm__) && (__ARM_ARCH > 6)
   1550         #define XFENCE() XASM_VOLATILE("isb")
   1551     #elif defined(__aarch64__)
   1552         /* Change ".inst 0xd50330ff" to "sb" when compilers support it. */
   1553         #ifdef WOLFSSL_ARMASM_BARRIER_SB
   1554             #define XFENCE() XASM_VOLATILE(".inst 0xd50330ff")
   1555         #elif defined(WOLFSSL_ARMASM_BARRIER_DETECT)
   1556             extern int aarch64_use_sb;
   1557             #define XFENCE()                                \
   1558                 do {                                        \
   1559                     if (aarch64_use_sb)                     \
   1560                         XASM_VOLATILE(".inst 0xd50330ff");  \
   1561                     else                                    \
   1562                         XASM_VOLATILE("isb");               \
   1563                 } while (0)
   1564         #else
   1565             #define XFENCE() XASM_VOLATILE("isb")
   1566         #endif
   1567     #elif defined(__riscv)
   1568         #define XFENCE() XASM_VOLATILE("fence")
   1569     #elif defined(__PPC__) || defined(__POWERPC__)
   1570         #define XFENCE() XASM_VOLATILE("isync; sync")
   1571     #else
   1572         #define XFENCE() WC_DO_NOTHING
   1573     #endif
   1574 #else
   1575     #define XFENCE() WC_DO_NOTHING
   1576 #endif
   1577 
   1578 
   1579     /* AFTER user_settings.h is loaded,
   1580     ** determine if POSIX multi-threaded: HAVE_PTHREAD  */
   1581     #if defined(SINGLE_THREADED) || defined(__MINGW32__)
   1582         /* Never HAVE_PTHREAD in single thread, or non-POSIX mode.
   1583         ** Reminder: MING32 is win32 threads, not POSIX threads */
   1584         #undef HAVE_PTHREAD
   1585     #else
   1586         /* _POSIX_THREADS is defined by unistd.h so this check needs to happen
   1587          * after we include all the platform relevant libs. */
   1588         #ifdef _POSIX_THREADS
   1589             /* HAVE_PTHREAD == POSIX threads capable and enabled. */
   1590             #undef HAVE_PTHREAD
   1591             #define HAVE_PTHREAD 1
   1592         #endif
   1593     #endif
   1594 
   1595 #ifdef __cplusplus
   1596     }  /* extern "C" */
   1597 #endif
   1598 
   1599 #endif /* WOLF_CRYPT_PORT_H */