diff options
| author | 2026-01-16 18:57:56 -0600 | |
|---|---|---|
| committer | 2026-01-16 18:57:56 -0600 | |
| commit | a887fd3a4ef9bcf673b286398802aa88ecb1592a (patch) | |
| tree | 568019b692e5b2b38a149bbf94c55dbe78237c7a | |
| parent | move include directory (diff) | |
endian functions
| -rw-r--r-- | config.h.in | 25 | ||||
| -rw-r--r-- | include/platform.h | 88 | ||||
| -rw-r--r-- | lib/sha1.c | 42 | ||||
| -rw-r--r-- | meson.build | 85 | ||||
| -rw-r--r-- | meson.options | 3 |
5 files changed, 218 insertions, 25 deletions
diff --git a/config.h.in b/config.h.in new file mode 100644 index 0000000..ec3e783 --- /dev/null +++ b/config.h.in @@ -0,0 +1,25 @@ +/* Do not modify this file directly; it is generated by meson from config.h.in! + * This file will be overwritten when meson runs again. */ + +#ifndef VL_CONFIG_H_INCLUDED +#define VL_CONFIG_H_INCLUDED + +enum { + PLATFORM_WINDOWS, + PLATFORM_LINUX, + PLATFORM_OSX, + PLATFORM_UNKNOWN +}; + +#mesondefine VL_OS_NAME +#mesondefine VL_OS +#mesondefine VL_ARCH_NAME +#mesondefine VL_JRE_ARCH +#mesondefine VL_ENDIAN_LITTLE +#mesondefine VL_ENDIAN_BIG + +#if !(VL_ENDIAN_LITTLE || VL_ENDIAN_BIG) +#error Bad platform endianness. Must be big or little. +#endif + +#endif diff --git a/include/platform.h b/include/platform.h new file mode 100644 index 0000000..837c87d --- /dev/null +++ b/include/platform.h @@ -0,0 +1,88 @@ +#ifndef VL_ENDIAN_H_INCLUDED +#define VL_ENDIAN_H_INCLUDED + +#include "config.h" +#include <stdint.h> +#include <stddef.h> + +#ifdef __GNUC__ +#define VL_SWAP16_EXPR(_u) __builtin_bswap16(_u) +#define VL_SWAP32_EXPR(_u) __builtin_bswap32(_u) +#define VL_SWAP64_EXPR(_u) __builtin_bswap64(_u) +#else + +#define VL_SWAP16_EXPR(_u) (uint16_t)((_u) << 8 | (_u) >> 8) + +#define VL_SWAP32_EXPR(_u) (uint32_t) \ + ((((_u) & 0xFF000000u) >> 24) | \ + (((_u) & 0x00FF0000u) >> 8) | \ + (((_u) & 0x0000FF00u) << 8) | \ + (((_u) & 0x000000FFu) << 24)) + +#define VL_SWAP64_EXPR(_u) (uint64_t) \ + ((((_u) & UINT64_C(0xFF00000000000000)) >> 56) | \ + (((_u) & UINT64_C(0x00FF000000000000)) >> 40) | \ + (((_u) & UINT64_C(0x0000FF0000000000)) >> 24) | \ + (((_u) & UINT64_C(0x000000FF00000000)) >> 8) | \ + (((_u) & UINT64_C(0x00000000FF000000)) << 8) | \ + (((_u) & UINT64_C(0x0000000000FF0000)) << 24) | \ + (((_u) & UINT64_C(0x000000000000FF00)) << 40) | \ + (((_u) & UINT64_C(0x00000000000000FF)) << 56)) + +#endif + +static inline uint16_t vl_swap16(uint16_t in) +{ + return VL_SWAP16_EXPR(in); +} + +static inline uint32_t vl_swap32(uint32_t in) +{ + return VL_SWAP32_EXPR(in); +} + +static inline uint64_t vl_swap64(uint64_t in) +{ + return VL_SWAP64_EXPR(in); +} + +/* avoid leaking non-public macro definitions -- encourage the use of inline functions instead */ +#undef VL_SWAP16_EXPR +#undef VL_SWAP32_EXPR +#undef VL_SWAP64_EXPR + +#if VL_ENDIAN_LITTLE +#define vl_htobe16(_x) vl_swap16(_x) +#define vl_htobe32(_x) vl_swap32(_x) +#define vl_htobe64(_x) vl_swap64(_x) + +#define vl_htole16(_x) (uint16_t)(_x) +#define vl_htole32(_x) (uint32_t)(_x) +#define vl_htole64(_x) (uint64_t)(_x) + +#define vl_betoh16(_x) vl_swap16(_x) +#define vl_betoh32(_x) vl_swap32(_x) +#define vl_betoh64(_x) vl_swap64(_x) + +#define vl_letoh16(_x) (uint16_t)(_x) +#define vl_letoh32(_x) (uint32_t)(_x) +#define vl_letoh64(_x) (uint64_t)(_x) +#else /* VL_ENDIAN_BIG */ +#define vl_htobe16(_x) (uint16_t)(_x) +#define vl_htobe32(_x) (uint32_t)(_x) +#define vl_htobe64(_x) (uint64_t)(_x) + +#define vl_htole16(_x) vl_swap16(_x) +#define vl_htole32(_x) vl_swap32(_x) +#define vl_htole64(_x) vl_swap64(_x) + +#define vl_betoh16(_x) (uint16_t)(_x) +#define vl_betoh32(_x) (uint32_t)(_x) +#define vl_betoh64(_x) (uint64_t)(_x) + +#define vl_letoh16(_x) vl_swap16(_x) +#define vl_letoh32(_x) vl_swap32(_x) +#define vl_letoh64(_x) vl_swap64(_x) +#endif + +#endif @@ -1,8 +1,7 @@ #include "sha1.h" -#include <string.h> +#include "platform.h" -/* for byteswap functions (TODO: avoid GNU extensions) */ -#include <endian.h> +#include <string.h> /* code adapted from https://git.figboot.dev/l2su/tree/src/digest/sha1.c */ @@ -43,7 +42,7 @@ static void sha1_upd(vl_sha1_st *st) memcpy(w, st->vl_chunk, VL_SHA1_CHUNKLEN_BYTES); for (i = 0; i < 16; ++i) { - w[i] = htobe32(w[i]); + w[i] = vl_htobe32(w[i]); } for (i = 16; i < 80; ++i) { @@ -56,19 +55,16 @@ static void sha1_upd(vl_sha1_st *st) d = st->vl_state[3]; e = st->vl_state[4]; -#define UP \ - temp = rol(a, 5) + f + e + k + w[i]; \ - e = d; \ - d = c; \ - c = rol(b, 30); \ - b = a; \ - a = temp; - -#define OP(_start, _end, _k, _e) \ - for (i = _start; i < _end; ++i) { \ - f = _e; \ - k = _k; \ - UP \ +#define OP(_start, _end, _k, _e) \ + for (i = _start; i < _end; ++i) { \ + f = _e; \ + k = _k; \ + temp = rol(a, 5) + f + e + k + w[i]; \ + e = d; \ + d = c; \ + c = rol(b, 30); \ + b = a; \ + a = temp; \ } OP( 0, 20, SHA1_K0, (b & c) ^ ((~b) & d)) @@ -120,7 +116,7 @@ void vl_sha1_finalize(vl_sha1_st *st, vl_sha1 out) } size_t rem = (VL_SHA1_CHUNKLEN_BYTES - st->vl_nchunk) - 8; - uint64_t lenbe = htobe64(st->vl_total * 8); + uint64_t lenbe = vl_htobe64(st->vl_total * 8); memset(st->vl_chunk + st->vl_nchunk, 0, rem); st->vl_nchunk += rem; @@ -128,11 +124,11 @@ void vl_sha1_finalize(vl_sha1_st *st, vl_sha1 out) sha1_upd(st); uint32_t pout_i[VL_SHA1_STATELEN_32]; - pout_i[0] = htobe32(st->vl_state[0]); - pout_i[1] = htobe32(st->vl_state[1]); - pout_i[2] = htobe32(st->vl_state[2]); - pout_i[3] = htobe32(st->vl_state[3]); - pout_i[4] = htobe32(st->vl_state[4]); + pout_i[0] = vl_htobe32(st->vl_state[0]); + pout_i[1] = vl_htobe32(st->vl_state[1]); + pout_i[2] = vl_htobe32(st->vl_state[2]); + pout_i[3] = vl_htobe32(st->vl_state[3]); + pout_i[4] = vl_htobe32(st->vl_state[4]); memcpy(out, pout_i, sizeof(vl_sha1)); } diff --git a/meson.build b/meson.build index f86511c..d34bf7d 100644 --- a/meson.build +++ b/meson.build @@ -1,9 +1,90 @@ -project('vaclaunch', 'c') +project('vaclaunch', 'c', meson_version : '>=1.1') jansson_dep = dependency('jansson', required : true) curl_dep = dependency('libcurl', required : true) -proj_inc = include_directories('include') +proj_inc = include_directories('include', '.') + +platform_enum_names = { + 'windows': 'PLATFORM_WINDOWS', + 'osx': 'PLATFORM_OSX', + 'linux': 'PLATFORM_LINUX', + 'unknown': 'PLATFORM_UNKNOWN' +} + +plat_os = get_option('launcher_os') +if plat_os == 'detect' + if host_machine.system() == 'windows' or host_machine.system() == 'cygwin' + plat_os = 'windows' + elif host_machine.system() == 'osx' + plat_os = 'osx' + elif host_machine.system() == 'linux' + plat_os = 'linux' + else + plat_os = 'unknown' + endif + + message('Auto-detected launcher OS:', plat_os) +else + message('Using launcher OS:', plat_os) +endif + +plat_arch = get_option('launcher_arch') +if plat_arch == '' + plat_arch = host_machine.cpu_family() + if plat_arch == 'x86_64' + # squash faulty arch regexes like 'x86' for detecting 32-bit systems + plat_arch = 'amd64' + endif + + message('Auto-detected launcher architecture:', plat_arch) +else + message('Using launcher architecture:', plat_arch) +endif + +plat_jre_arch = get_option('launcher_jre_arch') +if plat_jre_arch == '' + plat_jre_arch = 'gamecore' + + if plat_os == 'windows' + if plat_arch == 'amd64' + plat_jre_arch = 'windows-x64' + elif plat_arch == 'x86' + plat_jre_arch = 'windows-x86' + elif plat_arch == 'aarch64' + plat_jre_arch = 'windows-arm64' + endif + elif plat_os == 'linux' + if plat_arch == 'amd64' + plat_jre_arch = 'linux' + elif plat_arch == 'x86' + plat_jre_arch = 'i386' + endif + elif plat_os == 'osx' + if plat_arch == 'amd64' + plat_jre_arch = 'mac-os' + elif plat_arch == 'aarch64' + plat_jre_arch = 'mac-os-arm64' + endif + endif + + message('Auto-detected launcher JRE architecture:', plat_jre_arch) +else + message('Using launcher JRE architecture:', plat_jre_arch) +endif + +platform_config = configuration_data() +platform_config.set_quoted('VL_OS_NAME', plat_os) +platform_config.set('VL_OS', platform_enum_names[plat_os]) +platform_config.set_quoted('VL_ARCH_NAME', plat_arch) +platform_config.set_quoted('VL_JRE_ARCH', plat_jre_arch) +platform_config.set10('VL_ENDIAN_LITTLE', host_machine.endian() == 'little') +platform_config.set10('VL_ENDIAN_BIG', host_machine.endian() == 'big') + +configure_file( + configuration : platform_config, + input : 'config.h.in', + output : 'config.h') subdir('lib') diff --git a/meson.options b/meson.options new file mode 100644 index 0000000..36781d8 --- /dev/null +++ b/meson.options @@ -0,0 +1,3 @@ +option('launcher_os', type : 'combo', choices : [ 'detect', 'windows', 'linux', 'osx', 'unknown' ], value : 'detect') +option('launcher_arch', type : 'string') +option('launcher_jre_arch', type : 'string') |
