Merge in mingw changes.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3873 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-05-16 22:30:14 +00:00
parent 87224d2bb6
commit 7848b5e560
12 changed files with 188 additions and 50 deletions

View File

@ -2,7 +2,13 @@
#include "ow-crypt.h"
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
extern BOOLEAN NTAPI SystemFunction036(PVOID Buffer, ULONG BufferLength);
#else
#include <sys/random.h>
#endif
JSValue _crypt_hashpw(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
JSValue _crypt_gensalt(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
@ -34,7 +40,11 @@ JSValue _crypt_gensalt(JSContext* context, JSValueConst this_val, int argc, JSVa
int length;
JS_ToInt32(context, &length, argv[0]);
char buffer[16];
#if defined(_WIN32)
ssize_t bytes = SystemFunction036(buffer, sizeof(buffer)) ? sizeof(buffer) : 0;
#else
ssize_t bytes = getrandom(buffer, sizeof(buffer), 0);
#endif
char output[7 + 22 + 1];
char* salt = crypt_gensalt_rn("$2b$", length, buffer, bytes, output, sizeof(output));
JSValue result = JS_NewString(context, salt);

View File

@ -33,6 +33,13 @@ typedef struct file_stat_t {
uv_fs_t _request;
} file_stat_t;
typedef struct fs_req_t {
uv_fs_t fs;
uv_file file;
size_t size;
char buffer[];
} fs_req_t;
void tf_file_register(JSContext* context)
{
JSValue global = JS_GetGlobalObject(context);
@ -59,6 +66,7 @@ static void _file_async_close_callback(uv_fs_t* req)
static void _file_read_read_callback(uv_fs_t* req)
{
uv_fs_req_cleanup(req);
fs_req_t* fsreq = (fs_req_t*)req;
tf_task_t* task = req->loop->data;
JSContext* context = tf_task_get_context(task);
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
@ -78,7 +86,7 @@ static void _file_read_read_callback(uv_fs_t* req)
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(req->result)));
}
int result = uv_fs_close(req->loop, req, req->file, _file_async_close_callback);
int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
if (result < 0)
{
free(req);
@ -116,12 +124,15 @@ static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int ar
promiseid_t promise = -1;
JSValue promise_value = tf_task_allocate_promise(task, &promise);
uv_fs_t* req = malloc(sizeof(uv_fs_t) + k_file_read_max);
*req = (uv_fs_t)
fs_req_t* req = malloc(sizeof(fs_req_t) + k_file_read_max);
*req = (fs_req_t)
{
.data = (void*)(intptr_t)promise,
.fs =
{
.data = (void*)(intptr_t)promise,
},
};
int result = uv_fs_open(tf_task_get_loop(task), req, file_name, UV_FS_O_RDONLY, 0, _file_read_open_callback);
int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_RDONLY, 0, _file_read_open_callback);
if (result < 0)
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(result)));
@ -134,6 +145,7 @@ static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int ar
static void _file_write_write_callback(uv_fs_t* req)
{
uv_fs_req_cleanup(req);
fs_req_t* fsreq = (fs_req_t*)req;
tf_task_t* task = req->loop->data;
JSContext* context = tf_task_get_context(task);
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
@ -145,7 +157,7 @@ static void _file_write_write_callback(uv_fs_t* req)
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(req->result)));
}
uv_fs_close(req->loop, req, req->file, _file_async_close_callback);
uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
}
static void _file_write_open_callback(uv_fs_t* req)
@ -193,19 +205,22 @@ static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int a
promiseid_t promise = -1;
JSValue promise_value = tf_task_allocate_promise(task, &promise);
uv_fs_t* req = malloc(sizeof(uv_fs_t) + sizeof(size_t) + size);
*req = (uv_fs_t)
fs_req_t* req = malloc(sizeof(fs_req_t) + size);
*req = (fs_req_t)
{
.data = (void*)(intptr_t)promise,
.fs =
{
.data = (void*)(intptr_t)promise,
},
.size = size,
};
memcpy(req + 1, &size, sizeof(size_t));
memcpy((char*)(req + 1) + sizeof(size_t), buffer, size);
memcpy(req->buffer, buffer, size);
if (!is_array_buffer)
{
JS_FreeCString(context, (const char*)buffer);
}
int result = uv_fs_open(tf_task_get_loop(task), req, file_name, UV_FS_O_CREAT | UV_FS_O_WRONLY, 0644, _file_write_open_callback);
int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_CREAT | UV_FS_O_WRONLY, 0644, _file_write_open_callback);
if (result < 0)
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(result)));

View File

@ -14,7 +14,7 @@
#include <string.h>
#if !defined (_WIN32) && !defined (__MACH__)
#if !defined(_WIN32) && !defined(__MACH__)
#include <signal.h>
#include <sys/prctl.h>
#include <sys/resource.h>
@ -80,7 +80,7 @@ const command_t k_commands[] = {
void shedPrivileges()
{
#if !defined (_WIN32)
#if !defined(_WIN32)
struct rlimit zeroLimit;
zeroLimit.rlim_cur = 0;
zeroLimit.rlim_max = 0;
@ -111,7 +111,7 @@ void shedPrivileges()
perror("setrlimit(RLIMIT_NPROC, {0, 0})");
exit(-1);
}
#if !defined (__MACH__)
#if !defined(__MACH__)
if (setrlimit(RLIMIT_LOCKS, &zeroLimit) != 0)
{
perror("setrlimit(RLIMIT_LOCKS, {0, 0})");
@ -288,7 +288,7 @@ static int _tf_command_export(const char* file, int argc, char* argv[])
"/~cory/docs",
"/~cory/ssb",
};
for (int i = 0; i < _countof(k_export); i++)
for (int i = 0; i < (int)_countof(k_export); i++)
{
printf("Exporting %s...\n", k_export[i]);
tf_ssb_export(ssb, k_export[i]);
@ -417,7 +417,7 @@ static int _tf_command_run(const char* file, int argc, char* argv[])
}
int result = 0;
#if !defined (_WIN32) && !defined (__MACH__)
#if !defined(_WIN32) && !defined(__MACH__)
setpgid(0, 0);
#endif
@ -612,7 +612,7 @@ static int _tf_command_usage(const char* file, int argc, char* argv[])
{
printf("Usage: %s command [command-options]\n", file);
printf("commands:\n");
for (int i = 0; i < _countof(k_commands); i++)
for (int i = 0; i < (int)_countof(k_commands); i++)
{
printf(" %s - %s\n", k_commands[i].name, k_commands[i].description);
}
@ -621,11 +621,13 @@ static int _tf_command_usage(const char* file, int argc, char* argv[])
int main(int argc, char* argv[])
{
#if !defined(_WIN32)
prctl(PR_SET_PDEATHSIG, SIGKILL);
#endif
uv_setup_args(argc, argv);
tf_taskstub_startup();
#if !defined (_WIN32)
#if !defined(_WIN32)
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
{
perror("signal");
@ -634,7 +636,7 @@ int main(int argc, char* argv[])
if (argc >= 2)
{
for (int i = 0; i < _countof(k_commands); i++)
for (int i = 0; i < (int)_countof(k_commands); i++)
{
const command_t* command = &k_commands[i];
if (strcmp(argv[1], command->name) == 0)

View File

@ -7,8 +7,8 @@
#include <assert.h>
#include <malloc.h>
#include <base64c.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
//#include <openssl/evp.h>
//#include <openssl/rand.h>
#include <quickjs.h>
#include <sodium/crypto_auth.h>
#include <sodium/crypto_box.h>

View File

@ -79,7 +79,7 @@ static void _tf_ssb_connections_timer(uv_timer_t* timer)
tf_ssb_connections_t* connections = timer->data;
tf_ssb_connection_t* active[4];
int count = tf_ssb_get_connections(connections->ssb, active, _countof(active));
if (count < _countof(active))
if (count < (int)_countof(active))
{
char host[256];
int port;

View File

@ -24,7 +24,11 @@ static void _write_file(const char* path, void* blob, size_t size)
static void _make_dir(const char* path)
{
#if defined(_WIN32)
if (mkdir(path) && errno != EEXIST)
#else
if (mkdir(path, 0755) && errno != EEXIST)
#endif
{
printf("Failed to create directory %s: %s.\n", path, strerror(errno));
}

View File

@ -24,9 +24,7 @@
#include "quickjs.h"
#include "quickjs-libc.h"
#ifdef _WIN32
static const int STDIN_FILENO = 0;
#else
#ifndef _WIN32
#include <unistd.h>
#endif

View File

@ -15,9 +15,6 @@
#include <io.h>
#include <windows.h>
#include <ws2tcpip.h>
static const int STDIN_FILENO = 0;
static const int STDOUT_FILENO = 1;
static const int STDERR_FILENO = 2;
#else
#include <unistd.h>
#endif

View File

@ -9,6 +9,11 @@
#include <string.h>
#include <unistd.h>
#if defined(_WIN32)
#define WIFEXITED(x) 1
#define WEXITSTATUS(x) (x)
#endif
static void _test_nop(const tf_test_options_t* options)
{
FILE* file = fopen("out/test.js", "w");

View File

@ -2,6 +2,7 @@
#if defined(_WIN32)
#define TF_TLS_SCHANNEL
#include <malloc.h>
#elif defined(__MACH__)
#define TF_TLS_APPLE
#else
@ -394,6 +395,7 @@ TlsContext* TlsContext::create()
return new TlsContext_osx();
}
#elif defined (_WIN32)
#if 0
#include <algorithm>
#include <assert.h>
#include <cstring>
@ -1038,6 +1040,7 @@ TlsContext* TlsContext::create()
{
return new TlsContext_sspi();
}
#endif
#else
TlsContext* TlsContext::create()
{
@ -1084,6 +1087,7 @@ bool tf_tls_context_set_certificate(tf_tls_context_t* context, const char* certi
return result == 1;
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return false;
#endif
}
@ -1099,6 +1103,7 @@ bool tf_tls_context_set_private_key(tf_tls_context_t* context, const char* priva
return result == 1;
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return false;
#endif
}
@ -1123,6 +1128,7 @@ bool tf_tls_context_add_trusted_certificate(tf_tls_context_t* context, const cha
return result;
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return false;
#endif
}
@ -1137,6 +1143,7 @@ tf_tls_session_t* tf_tls_context_create_session(tf_tls_context_t* context)
return session;
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return NULL;
#endif
}
@ -1224,8 +1231,8 @@ void tf_tls_session_shutdown(tf_tls_session_t* session)
int tf_tls_session_get_peer_certificate(tf_tls_session_t* session, char* buffer, size_t bytes)
{
#if defined(TF_TLS_OPENSSL)
int result = -1;
#if defined(TF_TLS_OPENSSL)
X509* certificate = SSL_get_peer_certificate(session->ssl);
BIO* bio = BIO_new(BIO_s_mem());
PEM_write_bio_X509(bio, certificate);
@ -1237,10 +1244,10 @@ int tf_tls_session_get_peer_certificate(tf_tls_session_t* session, char* buffer,
result = mem->length;
}
BIO_free(bio);
return result;
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
#endif
return result;
}
#if defined(TF_TLS_OPENSSL)
@ -1380,6 +1387,7 @@ tf_tls_handshake_t tf_tls_session_handshake(tf_tls_session_t* session)
return result;
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return k_tls_handshake_failed;
#endif
}
@ -1413,6 +1421,7 @@ int tf_tls_session_read_plain(tf_tls_session_t* session, char* buffer, size_t by
return result;
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return k_tls_read_failed;
#endif
}
@ -1422,6 +1431,7 @@ int tf_tls_session_write_plain(tf_tls_session_t* session, const char* buffer, si
return SSL_write(session->ssl, buffer, bytes);
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return -1;
#endif
}
@ -1431,6 +1441,7 @@ int tf_tls_session_read_encrypted(tf_tls_session_t* session, char* buffer, size_
return BIO_read(session->bio_out, buffer, bytes);
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return -1;
#endif
}
@ -1440,6 +1451,7 @@ int tf_tls_session_write_encrypted(tf_tls_session_t* session, const char* buffer
return BIO_write(session->bio_in, buffer, bytes);
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return -1;
#endif
}
@ -1454,5 +1466,6 @@ bool tf_tls_session_get_error(tf_tls_session_t* session, char* buffer, size_t by
return error != 0;
#elif defined(TF_TLS_APPLE)
#elif defined(TF_TLS_SCHANNEL)
return false;
#endif
}

View File

@ -2,6 +2,7 @@
#include <assert.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>