Skip to content

Commit

Permalink
Merge pull request #509 from evo-lua/automated-cdef-synchronization
Browse files Browse the repository at this point in the history
Enable automated synchronization of cdefs for the FFI bindings
  • Loading branch information
rdw-software authored Feb 19, 2024
2 parents 3d270e8 + 7b2f4ee commit 3661c7d
Show file tree
Hide file tree
Showing 45 changed files with 5,286 additions and 3,456 deletions.
22 changes: 11 additions & 11 deletions .cppcheck
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ unusedFunction:Runtime/LuaVirtualMachine.cpp
unusedFunction:Runtime/Bindings/RmlUi_Renderer_WebGPU.cpp

// FFI exports used from Lua, which cppcheck can't see
unusedStructMember:Runtime/Bindings/crypto_ffi.hpp
unusedStructMember:Runtime/Bindings/glfw_ffi.hpp
unusedStructMember:Runtime/Bindings/iconv_ffi.hpp
unusedStructMember:Runtime/Bindings/interop_ffi.hpp
unusedStructMember:Runtime/Bindings/labsound_ffi.hpp
unusedStructMember:Runtime/Bindings/rml_ffi.hpp
unusedStructMember:Runtime/Bindings/runtime_ffi.hpp
unusedStructMember:Runtime/Bindings/stbi_ffi.hpp
unusedStructMember:Runtime/Bindings/stduuid_ffi.hpp
unusedStructMember:Runtime/Bindings/uws_ffi.hpp
unusedStructMember:Runtime/Bindings/webgpu_ffi.hpp
unusedStructMember:Runtime/Bindings/crypto_exports.hpp
unusedStructMember:Runtime/Bindings/glfw_exports.hpp
unusedStructMember:Runtime/Bindings/iconv_exports.hpp
unusedStructMember:Runtime/Bindings/interop_exports.hpp
unusedStructMember:Runtime/Bindings/labsound_exports.hpp
unusedStructMember:Runtime/Bindings/rml_exports.hpp
unusedStructMember:Runtime/Bindings/runtime_exports.hpp
unusedStructMember:Runtime/Bindings/stbi_exports.hpp
unusedStructMember:Runtime/Bindings/stduuid_exports.hpp
unusedStructMember:Runtime/Bindings/uws_exports.hpp
unusedStructMember:Runtime/Bindings/webgpu_exports.hpp

// PerSocketData is actually used, but cppcheck can't see it
unusedStructMember:Runtime/Bindings/WebServer.hpp
Expand Down
75 changes: 38 additions & 37 deletions Runtime/Bindings/crypto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,44 @@ local crypto = {

crypto.cdefs = [[
typedef struct kdf_parameters_t {
const char* kdf;
uint32_t version;
uint32_t kilobytes;
uint32_t threads;
uint32_t lanes;
size_t size;
uint32_t iterations;
} kdf_parameters_t;
typedef struct kdf_input_t {
const char* password;
size_t pw_length;
const char *salt;
size_t salt_length;
} kdf_input_t;
typedef struct kdf_result_t {
bool success;
unsigned char* hash;
char* message;
} kdf_result_t;
struct static_crypto_exports_table {
// OpenSSL (libcrypto) metadata
const char* (*version_text)(void);
long int (*version_number)(void);
// Argon2 MCF utilities
size_t (*openssl_to_base64)(char *dst, size_t dst_len, const char *src, size_t src_len);
size_t (*openssl_from_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*argon2_to_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*argon2_from_base64)(char *dst, size_t dst_len, const char *src);
void (*openssl_kdf_derive)(kdf_input_t inputs, kdf_parameters_t parameters, kdf_result_t* result);
int (*openssl_crypto_memcmp)(const void *a, const void *b, size_t len);
};
typedef struct kdf_parameters_t {
const char* kdf;
uint32_t version;
uint32_t kilobytes;
uint32_t threads;
uint32_t lanes;
size_t size;
uint32_t iterations;
} kdf_parameters_t;
typedef struct kdf_input_t {
const char* password;
size_t pw_length;
const char* salt;
size_t salt_length;
} kdf_input_t;
typedef struct kdf_result_t {
bool success;
unsigned char* hash;
char* message;
} kdf_result_t;
struct static_crypto_exports_table {
// OpenSSL (libcrypto) metadata
const char* (*version_text)(void);
long int (*version_number)(void);
// Argon2 MCF utilities
size_t (*openssl_to_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*openssl_from_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*argon2_to_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*argon2_from_base64)(unsigned char* dst, size_t dst_len, const char* src);
void (*openssl_kdf_derive)(kdf_input_t inputs, kdf_parameters_t parameters, kdf_result_t* result);
int (*openssl_crypto_memcmp)(const void* a, const void* b, size_t len);
};
]]

-- As per https://www.openssl.org/docs/manmaster/man3/ERR_error_string_n.html
Expand Down
37 changes: 37 additions & 0 deletions Runtime/Bindings/crypto_exports.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
typedef struct kdf_parameters_t {
const char* kdf;
uint32_t version;
uint32_t kilobytes;
uint32_t threads;
uint32_t lanes;
size_t size;
uint32_t iterations;
} kdf_parameters_t;

typedef struct kdf_input_t {
const char* password;
size_t pw_length;
const char* salt;
size_t salt_length;
} kdf_input_t;

typedef struct kdf_result_t {
bool success;
unsigned char* hash;
char* message;
} kdf_result_t;

struct static_crypto_exports_table {
// OpenSSL (libcrypto) metadata
const char* (*version_text)(void);
long int (*version_number)(void);

// Argon2 MCF utilities
size_t (*openssl_to_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*openssl_from_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*argon2_to_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*argon2_from_base64)(unsigned char* dst, size_t dst_len, const char* src);
void (*openssl_kdf_derive)(kdf_input_t inputs, kdf_parameters_t parameters, kdf_result_t* result);

int (*openssl_crypto_memcmp)(const void* a, const void* b, size_t len);
};
38 changes: 1 addition & 37 deletions Runtime/Bindings/crypto_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,7 @@
#include <cstddef>
#include <cstdint>

typedef struct kdf_parameters_t {
const char* kdf;
uint32_t version;
uint32_t kilobytes;
uint32_t threads;
uint32_t lanes;
size_t size;
uint32_t iterations;
} kdf_parameters_t;

typedef struct kdf_input_t {
const char* password;
size_t pw_length;
const char* salt;
size_t salt_length;
} kdf_input_t;

typedef struct kdf_result_t {
bool success;
unsigned char* hash;
char* message;
} kdf_result_t;

struct static_crypto_exports_table {
// OpenSSL (libcrypto) metadata
const char* (*version_text)(void);
long int (*version_number)(void);

// Argon2 MCF utilities
size_t (*openssl_to_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*openssl_from_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*argon2_to_base64)(unsigned char* dst, size_t dst_len, const unsigned char* src, size_t src_len);
size_t (*argon2_from_base64)(unsigned char* dst, size_t dst_len, const char* src);
void (*openssl_kdf_derive)(kdf_input_t inputs, kdf_parameters_t parameters, kdf_result_t* result);

int (*openssl_crypto_memcmp)(const void* a, const void* b, size_t len);
};
#include "crypto_exports.h"

namespace crypto_ffi {

Expand Down
128 changes: 63 additions & 65 deletions Runtime/Bindings/glfw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,69 @@ local validateTable = validation.validateTable
local glfw = {}

glfw.cdefs = [[
typedef struct GLFWimage {
int width;
int height;
unsigned char* pixels;
} GLFWimage;
typedef struct GLFWvidmode {
int width;
int height;
int redBits;
int greenBits;
int blueBits;
int refreshRate;
} GLFWvidmode;
// Opaque pointer types don't need to be defined as they're only ever handled by glfw internals
typedef struct GLFWcursor GLFWcursor;
typedef struct GLFWwindow GLFWwindow;
typedef struct GLFWmonitor GLFWmonitor;
typedef void* deferred_event_queue_t;
// These are passed to WebGPU, but the internals aren't exposed
typedef void* WGPUSurface;
typedef void* WGPUInstance;
struct static_glfw_exports_table {
const char* (*glfw_version)(void);
int (*glfw_find_constant)(const char* name);
WGPUSurface (*glfw_get_wgpu_surface)(WGPUInstance instance, GLFWwindow* window);
int (*glfw_init)(void);
void (*glfw_terminate)(void);
void (*glfw_poll_events)(void);
GLFWwindow* (*glfw_create_window)(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share);
void (*glfw_destroy_window)(GLFWwindow* window);
int (*glfw_window_should_close)(GLFWwindow* window);
void (*glfw_window_hint)(int hint, int value);
void (*glfw_set_window_pos)(GLFWwindow* window, int xpos, int ypos);
void (*glfw_get_framebuffer_size)(GLFWwindow* window, int* width, int* height);
void (*glfw_get_window_size)(GLFWwindow* window, int* width, int* height);
void (*glfw_maximize_window)(GLFWwindow* window);
void (*glfw_restore_window)(GLFWwindow* window);
void (*glfw_hide_window)(GLFWwindow* window);
void (*glfw_show_window)(GLFWwindow* window);
int (*glfw_get_window_attrib)(GLFWwindow* window, int attrib);
void (*glfw_set_window_icon)(GLFWwindow* window, int count, const GLFWimage* images);
void (*glfw_register_events)(GLFWwindow* window, deferred_event_queue_t queue);
GLFWmonitor* (*glfw_get_primary_monitor)(void);
GLFWmonitor** (*glfw_get_monitors)(int* count);
GLFWmonitor* (*glfw_get_window_monitor)(GLFWwindow* window);
void (*glfw_set_window_monitor)(GLFWwindow* window, GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
const GLFWvidmode* (*glfw_get_video_mode)(GLFWmonitor* monitor);
void (*glfw_get_cursor_pos)(GLFWwindow* window, double* xpos, double* ypos);
GLFWcursor* (*glfw_create_cursor)(const GLFWimage* image, int xhot, int yhot);
void (*glfw_destroy_cursor)(GLFWcursor* cursor);
void (*glfw_set_cursor)(GLFWwindow* window, GLFWcursor* cursor);
int (*glfw_get_key)(GLFWwindow* window, int key);
int (*glfw_get_mouse_button)(GLFWwindow* window, int button);
};
typedef struct GLFWimage {
int width;
int height;
unsigned char* pixels;
} GLFWimage;
typedef struct GLFWvidmode {
int width;
int height;
int redBits;
int greenBits;
int blueBits;
int refreshRate;
} GLFWvidmode;
typedef struct GLFWcursor GLFWcursor;
typedef struct GLFWwindow GLFWwindow;
typedef struct GLFWmonitor GLFWmonitor;
typedef void* deferred_event_queue_t; // Duplicated in the interop aliases (fix later)
typedef void* WGPUSurface;
typedef void* WGPUInstance;
struct static_glfw_exports_table {
const char* (*glfw_version)(void);
int (*glfw_find_constant)(const char* name);
WGPUSurface (*glfw_get_wgpu_surface)(WGPUInstance instance, GLFWwindow* window);
int (*glfw_init)(void);
void (*glfw_terminate)(void);
void (*glfw_poll_events)(void);
GLFWwindow* (*glfw_create_window)(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share);
void (*glfw_destroy_window)(GLFWwindow* window);
int (*glfw_window_should_close)(GLFWwindow* window);
void (*glfw_window_hint)(int hint, int value);
void (*glfw_set_window_pos)(GLFWwindow* window, int xpos, int ypos);
void (*glfw_get_framebuffer_size)(GLFWwindow* window, int* width, int* height);
void (*glfw_get_window_size)(GLFWwindow* window, int* width, int* height);
void (*glfw_maximize_window)(GLFWwindow* window);
void (*glfw_restore_window)(GLFWwindow* window);
void (*glfw_hide_window)(GLFWwindow* window);
void (*glfw_show_window)(GLFWwindow* window);
int (*glfw_get_window_attrib)(GLFWwindow* window, int attrib);
void (*glfw_set_window_icon)(GLFWwindow* window, int count, const GLFWimage* images);
void (*glfw_register_events)(GLFWwindow* window, deferred_event_queue_t queue);
GLFWmonitor* (*glfw_get_primary_monitor)(void);
GLFWmonitor** (*glfw_get_monitors)(int* count);
GLFWmonitor* (*glfw_get_window_monitor)(GLFWwindow* window);
void (*glfw_set_window_monitor)(GLFWwindow* window, GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
const GLFWvidmode* (*glfw_get_video_mode)(GLFWmonitor* monitor);
void (*glfw_get_cursor_pos)(GLFWwindow* window, double* xpos, double* ypos);
GLFWcursor* (*glfw_create_cursor)(const GLFWimage* image, int xhot, int yhot);
void (*glfw_destroy_cursor)(GLFWcursor* cursor);
void (*glfw_set_cursor)(GLFWwindow* window, GLFWcursor* cursor);
int (*glfw_get_key)(GLFWwindow* window, int key);
int (*glfw_get_mouse_button)(GLFWwindow* window, int button);
};
]]

function glfw.initialize()
Expand Down
22 changes: 22 additions & 0 deletions Runtime/Bindings/glfw_aliases.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
typedef struct GLFWimage {
int width;
int height;
unsigned char* pixels;
} GLFWimage;

typedef struct GLFWvidmode {
int width;
int height;
int redBits;
int greenBits;
int blueBits;
int refreshRate;
} GLFWvidmode;

typedef struct GLFWcursor GLFWcursor;
typedef struct GLFWwindow GLFWwindow;
typedef struct GLFWmonitor GLFWmonitor;
typedef void* deferred_event_queue_t; // Duplicated in the interop aliases (fix later)

typedef void* WGPUSurface;
typedef void* WGPUInstance;
40 changes: 40 additions & 0 deletions Runtime/Bindings/glfw_exports.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
struct static_glfw_exports_table {
const char* (*glfw_version)(void);
int (*glfw_find_constant)(const char* name);

WGPUSurface (*glfw_get_wgpu_surface)(WGPUInstance instance, GLFWwindow* window);

int (*glfw_init)(void);
void (*glfw_terminate)(void);
void (*glfw_poll_events)(void);

GLFWwindow* (*glfw_create_window)(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share);
void (*glfw_destroy_window)(GLFWwindow* window);
int (*glfw_window_should_close)(GLFWwindow* window);
void (*glfw_window_hint)(int hint, int value);
void (*glfw_set_window_pos)(GLFWwindow* window, int xpos, int ypos);
void (*glfw_get_framebuffer_size)(GLFWwindow* window, int* width, int* height);
void (*glfw_get_window_size)(GLFWwindow* window, int* width, int* height);
void (*glfw_maximize_window)(GLFWwindow* window);
void (*glfw_restore_window)(GLFWwindow* window);
void (*glfw_hide_window)(GLFWwindow* window);
void (*glfw_show_window)(GLFWwindow* window);
int (*glfw_get_window_attrib)(GLFWwindow* window, int attrib);
void (*glfw_set_window_icon)(GLFWwindow* window, int count, const GLFWimage* images);

void (*glfw_register_events)(GLFWwindow* window, deferred_event_queue_t queue);

GLFWmonitor* (*glfw_get_primary_monitor)(void);
GLFWmonitor** (*glfw_get_monitors)(int* count);
GLFWmonitor* (*glfw_get_window_monitor)(GLFWwindow* window);
void (*glfw_set_window_monitor)(GLFWwindow* window, GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
const GLFWvidmode* (*glfw_get_video_mode)(GLFWmonitor* monitor);

void (*glfw_get_cursor_pos)(GLFWwindow* window, double* xpos, double* ypos);
GLFWcursor* (*glfw_create_cursor)(const GLFWimage* image, int xhot, int yhot);
void (*glfw_destroy_cursor)(GLFWcursor* cursor);
void (*glfw_set_cursor)(GLFWwindow* window, GLFWcursor* cursor);

int (*glfw_get_key)(GLFWwindow* window, int key);
int (*glfw_get_mouse_button)(GLFWwindow* window, int button);
};
Loading

0 comments on commit 3661c7d

Please sign in to comment.