1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/* utils.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
#include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/settings.h>
#include <testsuite/utils.h>
#include <tests/unit.h>
#ifndef NO_FILESYSTEM
#if defined(_MSC_VER)
#include <direct.h>
#elif defined(__WATCOMC__)
#ifdef __LINUX__
#include <unistd.h>
#else
#include <direct.h>
#endif
#endif
#define TMP_DIR_PREFIX "tmpDir-"
/* len is length of tmpDir name, assuming
* len does not include null terminating character */
char* create_tmp_dir(char *tmpDir, int len)
{
if (len < (int)XSTR_SIZEOF(TMP_DIR_PREFIX))
return NULL;
XMEMCPY(tmpDir, TMP_DIR_PREFIX, XSTR_SIZEOF(TMP_DIR_PREFIX));
if (mymktemp(tmpDir, len, len - (int)XSTR_SIZEOF(TMP_DIR_PREFIX)) == NULL)
return NULL;
#ifdef _MSC_VER
if (_mkdir(tmpDir) != 0)
return NULL;
#elif defined(__MINGW32__)
if (mkdir(tmpDir) != 0)
return NULL;
#elif defined(__WATCOMC__) && !defined(__LINUX__)
if (mkdir(tmpDir) != 0)
return NULL;
#else
if (mkdir(tmpDir, 0700) != 0)
return NULL;
#endif
return tmpDir;
}
int rem_dir(const char* dirName)
{
#ifdef _MSC_VER
if (_rmdir(dirName) != 0)
return -1;
#else
if (rmdir(dirName) != 0)
return -1;
#endif
return 0;
}
int rem_file(const char* fileName)
{
#ifdef _MSC_VER
if (_unlink(fileName) != 0)
return -1;
#else
if (unlink(fileName) != 0)
return -1;
#endif
return 0;
}
int copy_file(const char* in, const char* out)
{
byte buf[100];
XFILE inFile = XBADFILE;
XFILE outFile = XBADFILE;
size_t sz;
int ret = -1;
inFile = XFOPEN(in, "rb");
if (inFile == XBADFILE)
goto cleanup;
outFile = XFOPEN(out, "wb");
if (outFile == XBADFILE)
goto cleanup;
while ((sz = XFREAD(buf, 1, sizeof(buf), inFile)) != 0) {
if (XFERROR(inFile))
goto cleanup;
if (XFWRITE(buf, 1, sz, outFile) != sz)
goto cleanup;
if (XFEOF(inFile))
break;
}
ret = 0;
cleanup:
if (inFile != XBADFILE)
XFCLOSE(inFile);
if (outFile != XBADFILE)
XFCLOSE(outFile);
return ret;
}
#if defined(__MACH__) || defined(__FreeBSD__)
int link_file(const char* in, const char* out)
{
return link(in, out);
}
#endif
#endif /* !NO_FILESYSTEM */
#if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
void signal_ready(tcp_ready* ready)
{
THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
ready->ready = 1;
THREAD_CHECK_RET(wolfSSL_CondSignal(&ready->cond));
THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
}
#endif
void wait_tcp_ready(func_args* args)
{
#if !defined(SINGLE_THREADED) && defined(WOLFSSL_COND)
tcp_ready* ready = args->signal;
THREAD_CHECK_RET(wolfSSL_CondStart(&ready->cond));
if (!ready->ready) {
THREAD_CHECK_RET(wolfSSL_CondWait(&ready->cond));
}
ready->ready = 0; /* reset */
THREAD_CHECK_RET(wolfSSL_CondEnd(&ready->cond));
#else
/* no threading wait or single threaded */
(void)args;
#endif
}
#ifndef SINGLE_THREADED
/* Start a thread.
*
* @param [in] fun Function to execute in thread.
* @param [in] args Object to send to function in thread.
* @param [out] thread Handle to thread.
*/
void start_thread(THREAD_CB fun, func_args* args, THREAD_TYPE* thread)
{
THREAD_CHECK_RET(wolfSSL_NewThread(thread, fun, args));
}
/* Join thread to wait for completion.
*
* @param [in] thread Handle to thread.
*/
void join_thread(THREAD_TYPE thread)
{
THREAD_CHECK_RET(wolfSSL_JoinThread(thread));
}
#endif /* SINGLE_THREADED */
|