initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled

This commit is contained in:
2025-09-16 20:46:46 -04:00
commit 9d30169a8d
13378 changed files with 7050105 additions and 0 deletions

2769
thirdparty/sdl/stdlib/SDL_casefolding.h vendored Normal file

File diff suppressed because it is too large Load Diff

52
thirdparty/sdl/stdlib/SDL_crc16.c vendored Normal file
View File

@@ -0,0 +1,52 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
/* Public domain CRC implementation adapted from:
http://home.thep.lu.se/~bjorn/crc/crc32_simple.c
This algorithm is compatible with the 16-bit CRC described here:
https://www.lammertbies.nl/comm/info/crc-calculation
*/
/* NOTE: DO NOT CHANGE THIS ALGORITHM
There is code that relies on this in the joystick code
*/
static Uint16 crc16_for_byte(Uint8 r)
{
Uint16 crc = 0;
int i;
for (i = 0; i < 8; ++i) {
crc = ((crc ^ r) & 1 ? 0xA001 : 0) ^ crc >> 1;
r >>= 1;
}
return crc;
}
Uint16 SDL_crc16(Uint16 crc, const void *data, size_t len)
{
// As an optimization we can precalculate a 256 entry table for each byte
size_t i;
for (i = 0; i < len; ++i) {
crc = crc16_for_byte((Uint8)crc ^ ((const Uint8 *)data)[i]) ^ crc >> 8;
}
return crc;
}

50
thirdparty/sdl/stdlib/SDL_crc32.c vendored Normal file
View File

@@ -0,0 +1,50 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
/* Public domain CRC implementation adapted from:
http://home.thep.lu.se/~bjorn/crc/crc32_simple.c
This algorithm is compatible with the 32-bit CRC described here:
https://www.lammertbies.nl/comm/info/crc-calculation
*/
/* NOTE: DO NOT CHANGE THIS ALGORITHM
There is code that relies on this in the joystick code
*/
static Uint32 crc32_for_byte(Uint32 r)
{
int i;
for (i = 0; i < 8; ++i) {
r = (r & 1 ? 0 : (Uint32)0xEDB88320L) ^ r >> 1;
}
return r ^ (Uint32)0xFF000000L;
}
Uint32 SDL_crc32(Uint32 crc, const void *data, size_t len)
{
// As an optimization we can precalculate a 256 entry table for each byte
size_t i;
for (i = 0; i < len; ++i) {
crc = crc32_for_byte((Uint8)crc ^ ((const Uint8 *)data)[i]) ^ crc >> 8;
}
return crc;
}

616
thirdparty/sdl/stdlib/SDL_getenv.c vendored Normal file
View File

@@ -0,0 +1,616 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#include "SDL_getenv_c.h"
#if defined(SDL_PLATFORM_WINDOWS)
#include "../core/windows/SDL_windows.h"
#endif
#ifdef SDL_PLATFORM_ANDROID
#include "../core/android/SDL_android.h"
#endif
#if defined(SDL_PLATFORM_WINDOWS)
#define HAVE_WIN32_ENVIRONMENT
#elif defined(HAVE_GETENV) && \
(defined(HAVE_SETENV) || defined(HAVE_PUTENV)) && \
(defined(HAVE_UNSETENV) || defined(HAVE_PUTENV))
#define HAVE_LIBC_ENVIRONMENT
#if defined(SDL_PLATFORM_MACOS)
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#elif defined(SDL_PLATFORM_FREEBSD)
#include <dlfcn.h>
#define environ ((char **)dlsym(RTLD_DEFAULT, "environ"))
#else
extern char **environ;
#endif
#else
#define HAVE_LOCAL_ENVIRONMENT
static char **environ;
#endif
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
#include <stdlib.h>
#endif
struct SDL_Environment
{
SDL_Mutex *lock; // !!! FIXME: reuse SDL_HashTable's lock.
SDL_HashTable *strings;
};
static SDL_Environment *SDL_environment;
SDL_Environment *SDL_GetEnvironment(void)
{
if (!SDL_environment) {
SDL_environment = SDL_CreateEnvironment(true);
}
return SDL_environment;
}
bool SDL_InitEnvironment(void)
{
return (SDL_GetEnvironment() != NULL);
}
void SDL_QuitEnvironment(void)
{
SDL_Environment *env = SDL_environment;
if (env) {
SDL_environment = NULL;
SDL_DestroyEnvironment(env);
}
}
SDL_Environment *SDL_CreateEnvironment(bool populated)
{
SDL_Environment *env = SDL_calloc(1, sizeof(*env));
if (!env) {
return NULL;
}
env->strings = SDL_CreateHashTable(0, false, SDL_HashString, SDL_KeyMatchString, SDL_DestroyHashKey, NULL);
if (!env->strings) {
SDL_free(env);
return NULL;
}
// Don't fail if we can't create a mutex (e.g. on a single-thread environment) // !!! FIXME: single-threaded environments should still return a non-NULL, do-nothing object here. Check for failure!
env->lock = SDL_CreateMutex();
if (populated) {
#ifdef SDL_PLATFORM_WINDOWS
LPWCH strings = GetEnvironmentStringsW();
if (strings) {
for (LPWCH string = strings; *string; string += SDL_wcslen(string) + 1) {
char *variable = WIN_StringToUTF8W(string);
if (!variable) {
continue;
}
char *value = SDL_strchr(variable, '=');
if (!value || value == variable) {
SDL_free(variable);
continue;
}
*value++ = '\0';
SDL_InsertIntoHashTable(env->strings, variable, value, true);
}
FreeEnvironmentStringsW(strings);
}
#else
#ifdef SDL_PLATFORM_ANDROID
// Make sure variables from the application manifest are available
Android_JNI_GetManifestEnvironmentVariables();
#endif
char **strings = environ;
if (strings) {
for (int i = 0; strings[i]; ++i) {
char *variable = SDL_strdup(strings[i]);
if (!variable) {
continue;
}
char *value = SDL_strchr(variable, '=');
if (!value || value == variable) {
SDL_free(variable);
continue;
}
*value++ = '\0';
SDL_InsertIntoHashTable(env->strings, variable, value, true);
}
}
#endif // SDL_PLATFORM_WINDOWS
}
return env;
}
const char *SDL_GetEnvironmentVariable(SDL_Environment *env, const char *name)
{
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
return getenv(name);
#else
const char *result = NULL;
if (!env) {
return NULL;
} else if (!name || *name == '\0') {
return NULL;
}
SDL_LockMutex(env->lock);
{
const char *value;
if (SDL_FindInHashTable(env->strings, name, (const void **)&value)) {
result = SDL_GetPersistentString(value);
}
}
SDL_UnlockMutex(env->lock);
return result;
#endif
}
typedef struct CountEnvStringsData
{
size_t count;
size_t length;
} CountEnvStringsData;
static bool SDLCALL CountEnvStrings(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
{
CountEnvStringsData *data = (CountEnvStringsData *) userdata;
data->length += SDL_strlen((const char *) key) + 1 + SDL_strlen((const char *) value) + 1;
data->count++;
return true; // keep iterating.
}
typedef struct CopyEnvStringsData
{
char **result;
char *string;
size_t count;
} CopyEnvStringsData;
static bool SDLCALL CopyEnvStrings(void *userdata, const SDL_HashTable *table, const void *vkey, const void *vvalue)
{
CopyEnvStringsData *data = (CopyEnvStringsData *) userdata;
const char *key = (const char *) vkey;
const char *value = (const char *) vvalue;
size_t len;
len = SDL_strlen(key);
data->result[data->count] = data->string;
SDL_memcpy(data->string, key, len);
data->string += len;
*(data->string++) = '=';
len = SDL_strlen(value);
SDL_memcpy(data->string, value, len);
data->string += len;
*(data->string++) = '\0';
data->count++;
return true; // keep iterating.
}
char **SDL_GetEnvironmentVariables(SDL_Environment *env)
{
char **result = NULL;
if (!env) {
SDL_InvalidParamError("env");
return NULL;
}
SDL_LockMutex(env->lock);
{
// First pass, get the size we need for all the strings
CountEnvStringsData countdata = { 0, 0 };
SDL_IterateHashTable(env->strings, CountEnvStrings, &countdata);
// Allocate memory for the strings
result = (char **)SDL_malloc((countdata.count + 1) * sizeof(*result) + countdata.length);
if (result) {
// Second pass, copy the strings
char *string = (char *)(result + countdata.count + 1);
CopyEnvStringsData cpydata = { result, string, 0 };
SDL_IterateHashTable(env->strings, CopyEnvStrings, &cpydata);
SDL_assert(countdata.count == cpydata.count);
result[cpydata.count] = NULL;
}
}
SDL_UnlockMutex(env->lock);
return result;
}
bool SDL_SetEnvironmentVariable(SDL_Environment *env, const char *name, const char *value, bool overwrite)
{
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
return setenv(name, value, overwrite);
#else
bool result = false;
if (!env) {
return SDL_InvalidParamError("env");
} else if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return SDL_InvalidParamError("name");
} else if (!value) {
return SDL_InvalidParamError("value");
}
SDL_LockMutex(env->lock);
{
char *string = NULL;
if (SDL_asprintf(&string, "%s=%s", name, value) > 0) {
const size_t len = SDL_strlen(name);
string[len] = '\0';
const char *origname = name;
name = string;
value = string + len + 1;
result = SDL_InsertIntoHashTable(env->strings, name, value, overwrite);
if (!result) {
SDL_free(string);
if (!overwrite) {
const void *existing_value = NULL;
// !!! FIXME: InsertIntoHashTable does this lookup too, maybe we should have a means to report that, to avoid duplicate work?
if (SDL_FindInHashTable(env->strings, origname, &existing_value)) {
result = true; // it already existed, and we refused to overwrite it. Call it success.
}
}
}
}
}
SDL_UnlockMutex(env->lock);
return result;
#endif
}
bool SDL_UnsetEnvironmentVariable(SDL_Environment *env, const char *name)
{
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_FREEBSD)
return unsetenv(name);
#else
bool result = false;
if (!env) {
return SDL_InvalidParamError("env");
} else if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return SDL_InvalidParamError("name");
}
SDL_LockMutex(env->lock);
{
const void *value;
if (SDL_FindInHashTable(env->strings, name, &value)) {
result = SDL_RemoveFromHashTable(env->strings, name);
} else {
result = true;
}
}
SDL_UnlockMutex(env->lock);
return result;
#endif
}
void SDL_DestroyEnvironment(SDL_Environment *env)
{
if (!env || env == SDL_environment) {
return;
}
SDL_DestroyMutex(env->lock);
SDL_DestroyHashTable(env->strings);
SDL_free(env);
}
// Put a variable into the environment
// Note: Name may not contain a '=' character. (Reference: http://www.unix.com/man-page/Linux/3/setenv/)
#ifdef HAVE_LIBC_ENVIRONMENT
#if defined(HAVE_SETENV)
int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
SDL_SetEnvironmentVariable(SDL_GetEnvironment(), name, value, (overwrite != 0));
return setenv(name, value, overwrite);
}
// We have a real environment table, but no real setenv? Fake it w/ putenv.
#else
int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
{
char *new_variable;
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
SDL_SetEnvironmentVariable(SDL_GetEnvironment(), name, value, (overwrite != 0));
if (getenv(name) != NULL) {
if (!overwrite) {
return 0; // leave the existing one there.
}
}
// This leaks. Sorry. Get a better OS so we don't have to do this.
SDL_asprintf(&new_variable, "%s=%s", name, value);
if (!new_variable) {
return -1;
}
return putenv(new_variable);
}
#endif
#elif defined(HAVE_WIN32_ENVIRONMENT)
int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
SDL_SetEnvironmentVariable(SDL_GetEnvironment(), name, value, (overwrite != 0));
if (!overwrite) {
if (GetEnvironmentVariableA(name, NULL, 0) > 0) {
return 0; // asked not to overwrite existing value.
}
}
if (!SetEnvironmentVariableA(name, value)) {
return -1;
}
return 0;
}
#else // roll our own
int SDL_setenv_unsafe(const char *name, const char *value, int overwrite)
{
int added;
size_t len, i;
char **new_env;
char *new_variable;
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
// See if it already exists
if (!overwrite && SDL_getenv_unsafe(name)) {
return 0;
}
SDL_SetEnvironmentVariable(SDL_GetEnvironment(), name, value, (overwrite != 0));
// Allocate memory for the variable
len = SDL_strlen(name) + SDL_strlen(value) + 2;
new_variable = (char *)SDL_malloc(len);
if (!new_variable) {
return -1;
}
SDL_snprintf(new_variable, len, "%s=%s", name, value);
value = new_variable + SDL_strlen(name) + 1;
name = new_variable;
// Actually put it into the environment
added = 0;
i = 0;
if (environ) {
// Check to see if it's already there...
len = (value - name);
for (; environ[i]; ++i) {
if (SDL_strncmp(environ[i], name, len) == 0) {
// If we found it, just replace the entry
SDL_free(environ[i]);
environ[i] = new_variable;
added = 1;
break;
}
}
}
// Didn't find it in the environment, expand and add
if (!added) {
new_env = SDL_realloc(environ, (i + 2) * sizeof(char *));
if (new_env) {
environ = new_env;
environ[i++] = new_variable;
environ[i++] = (char *)0;
added = 1;
} else {
SDL_free(new_variable);
}
}
return added ? 0 : -1;
}
#endif // HAVE_LIBC_ENVIRONMENT
#ifdef HAVE_LIBC_ENVIRONMENT
#if defined(HAVE_UNSETENV)
int SDL_unsetenv_unsafe(const char *name)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return -1;
}
SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), name);
return unsetenv(name);
}
// We have a real environment table, but no unsetenv? Fake it w/ putenv.
#else
int SDL_unsetenv_unsafe(const char *name)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return -1;
}
SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), name);
// Hope this environment uses the non-standard extension of removing the environment variable if it has no '='
return putenv(name);
}
#endif
#elif defined(HAVE_WIN32_ENVIRONMENT)
int SDL_unsetenv_unsafe(const char *name)
{
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return -1;
}
SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), name);
if (!SetEnvironmentVariableA(name, NULL)) {
return -1;
}
return 0;
}
#else
int SDL_unsetenv_unsafe(const char *name)
{
size_t len, i;
// Input validation
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL) {
return -1;
}
SDL_UnsetEnvironmentVariable(SDL_GetEnvironment(), name);
if (environ) {
len = SDL_strlen(name);
for (i = 0; environ[i]; ++i) {
if ((SDL_strncmp(environ[i], name, len) == 0) &&
(environ[i][len] == '=')) {
// Just clear out this entry for now
*environ[i] = '\0';
break;
}
}
}
return 0;
}
#endif // HAVE_LIBC_ENVIRONMENT
// Retrieve a variable named "name" from the environment
#ifdef HAVE_LIBC_ENVIRONMENT
const char *SDL_getenv_unsafe(const char *name)
{
#ifdef SDL_PLATFORM_ANDROID
// Make sure variables from the application manifest are available
Android_JNI_GetManifestEnvironmentVariables();
#endif
// Input validation
if (!name || *name == '\0') {
return NULL;
}
return getenv(name);
}
#elif defined(HAVE_WIN32_ENVIRONMENT)
const char *SDL_getenv_unsafe(const char *name)
{
DWORD length, maxlen = 0;
char *string = NULL;
const char *result = NULL;
// Input validation
if (!name || *name == '\0') {
return NULL;
}
for ( ; ; ) {
SetLastError(ERROR_SUCCESS);
length = GetEnvironmentVariableA(name, string, maxlen);
if (length > maxlen) {
char *temp = (char *)SDL_realloc(string, length);
if (!temp) {
return NULL;
}
string = temp;
maxlen = length;
} else {
if (GetLastError() != ERROR_SUCCESS) {
if (string) {
SDL_free(string);
}
return NULL;
}
break;
}
}
if (string) {
result = SDL_GetPersistentString(string);
SDL_free(string);
}
return result;
}
#else
const char *SDL_getenv_unsafe(const char *name)
{
size_t len, i;
const char *value = NULL;
// Input validation
if (!name || *name == '\0') {
return NULL;
}
if (environ) {
len = SDL_strlen(name);
for (i = 0; environ[i]; ++i) {
if ((SDL_strncmp(environ[i], name, len) == 0) &&
(environ[i][len] == '=')) {
value = &environ[i][len + 1];
break;
}
}
}
return value;
}
#endif // HAVE_LIBC_ENVIRONMENT
const char *SDL_getenv(const char *name)
{
return SDL_GetEnvironmentVariable(SDL_GetEnvironment(), name);
}

24
thirdparty/sdl/stdlib/SDL_getenv_c.h vendored Normal file
View File

@@ -0,0 +1,24 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
extern bool SDL_InitEnvironment(void);
extern void SDL_QuitEnvironment(void);

860
thirdparty/sdl/stdlib/SDL_iconv.c vendored Normal file
View File

@@ -0,0 +1,860 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
// This file contains portable iconv functions for SDL
#if defined(HAVE_ICONV) && defined(HAVE_ICONV_H)
#ifndef SDL_USE_LIBICONV
// Define LIBICONV_PLUG to use iconv from the base instead of ports and avoid linker errors.
#define LIBICONV_PLUG 1
#endif
#include <iconv.h>
#include <errno.h>
SDL_COMPILE_TIME_ASSERT(iconv_t, sizeof(iconv_t) <= sizeof(SDL_iconv_t));
SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
{
return (SDL_iconv_t)((uintptr_t)iconv_open(tocode, fromcode));
}
int SDL_iconv_close(SDL_iconv_t cd)
{
if ((size_t)cd == SDL_ICONV_ERROR) {
return -1;
}
return iconv_close((iconv_t)((uintptr_t)cd));
}
size_t SDL_iconv(SDL_iconv_t cd,
const char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
if ((size_t)cd == SDL_ICONV_ERROR) {
return SDL_ICONV_ERROR;
}
/* iconv's second parameter may or may not be `const char const *` depending on the
C runtime's whims. Casting to void * seems to make everyone happy, though. */
const size_t retCode = iconv((iconv_t)((uintptr_t)cd), (void *)inbuf, inbytesleft, outbuf, outbytesleft);
if (retCode == (size_t)-1) {
switch (errno) {
case E2BIG:
return SDL_ICONV_E2BIG;
case EILSEQ:
return SDL_ICONV_EILSEQ;
case EINVAL:
return SDL_ICONV_EINVAL;
default:
return SDL_ICONV_ERROR;
}
}
return retCode;
}
#else
/* Lots of useful information on Unicode at:
http://www.cl.cam.ac.uk/~mgk25/unicode.html
*/
#define UNICODE_BOM 0xFEFF
#define UNKNOWN_ASCII '?'
#define UNKNOWN_UNICODE 0xFFFD
enum
{
ENCODING_UNKNOWN,
ENCODING_ASCII,
ENCODING_LATIN1,
ENCODING_UTF8,
ENCODING_UTF16, // Needs byte order marker
ENCODING_UTF16BE,
ENCODING_UTF16LE,
ENCODING_UTF32, // Needs byte order marker
ENCODING_UTF32BE,
ENCODING_UTF32LE,
ENCODING_UCS2BE,
ENCODING_UCS2LE,
ENCODING_UCS4BE,
ENCODING_UCS4LE,
};
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define ENCODING_UTF16NATIVE ENCODING_UTF16BE
#define ENCODING_UTF32NATIVE ENCODING_UTF32BE
#define ENCODING_UCS2NATIVE ENCODING_UCS2BE
#define ENCODING_UCS4NATIVE ENCODING_UCS4BE
#else
#define ENCODING_UTF16NATIVE ENCODING_UTF16LE
#define ENCODING_UTF32NATIVE ENCODING_UTF32LE
#define ENCODING_UCS2NATIVE ENCODING_UCS2LE
#define ENCODING_UCS4NATIVE ENCODING_UCS4LE
#endif
struct SDL_iconv_data_t
{
int src_fmt;
int dst_fmt;
};
static struct
{
const char *name;
int format;
} encodings[] = {
/* *INDENT-OFF* */ // clang-format off
{ "ASCII", ENCODING_ASCII },
{ "US-ASCII", ENCODING_ASCII },
{ "8859-1", ENCODING_LATIN1 },
{ "ISO-8859-1", ENCODING_LATIN1 },
#if defined(SDL_PLATFORM_WINDOWS) || defined(SDL_PLATFORM_OS2)
{ "WCHAR_T", ENCODING_UTF16LE },
#else
{ "WCHAR_T", ENCODING_UCS4NATIVE },
#endif
{ "UTF8", ENCODING_UTF8 },
{ "UTF-8", ENCODING_UTF8 },
{ "UTF16", ENCODING_UTF16 },
{ "UTF-16", ENCODING_UTF16 },
{ "UTF16BE", ENCODING_UTF16BE },
{ "UTF-16BE", ENCODING_UTF16BE },
{ "UTF16LE", ENCODING_UTF16LE },
{ "UTF-16LE", ENCODING_UTF16LE },
{ "UTF32", ENCODING_UTF32 },
{ "UTF-32", ENCODING_UTF32 },
{ "UTF32BE", ENCODING_UTF32BE },
{ "UTF-32BE", ENCODING_UTF32BE },
{ "UTF32LE", ENCODING_UTF32LE },
{ "UTF-32LE", ENCODING_UTF32LE },
{ "UCS2", ENCODING_UCS2BE },
{ "UCS-2", ENCODING_UCS2BE },
{ "UCS-2LE", ENCODING_UCS2LE },
{ "UCS-2BE", ENCODING_UCS2BE },
{ "UCS-2-INTERNAL", ENCODING_UCS2NATIVE },
{ "UCS4", ENCODING_UCS4BE },
{ "UCS-4", ENCODING_UCS4BE },
{ "UCS-4LE", ENCODING_UCS4LE },
{ "UCS-4BE", ENCODING_UCS4BE },
{ "UCS-4-INTERNAL", ENCODING_UCS4NATIVE },
/* *INDENT-ON* */ // clang-format on
};
static const char *getlocale(char *buffer, size_t bufsize)
{
const char *lang;
char *ptr;
lang = SDL_getenv("LC_ALL");
if (!lang) {
lang = SDL_getenv("LC_CTYPE");
}
if (!lang) {
lang = SDL_getenv("LC_MESSAGES");
}
if (!lang) {
lang = SDL_getenv("LANG");
}
if (!lang || !*lang || SDL_strcmp(lang, "C") == 0) {
lang = "ASCII";
}
// We need to trim down strings like "en_US.UTF-8@blah" to "UTF-8"
ptr = SDL_strchr(lang, '.');
if (ptr) {
lang = ptr + 1;
}
SDL_strlcpy(buffer, lang, bufsize);
ptr = SDL_strchr(buffer, '@');
if (ptr) {
*ptr = '\0'; // chop end of string.
}
return buffer;
}
SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
{
int src_fmt = ENCODING_UNKNOWN;
int dst_fmt = ENCODING_UNKNOWN;
int i;
char fromcode_buffer[64];
char tocode_buffer[64];
if (!fromcode || !*fromcode) {
fromcode = getlocale(fromcode_buffer, sizeof(fromcode_buffer));
}
if (!tocode || !*tocode) {
tocode = getlocale(tocode_buffer, sizeof(tocode_buffer));
}
for (i = 0; i < SDL_arraysize(encodings); ++i) {
if (SDL_strcasecmp(fromcode, encodings[i].name) == 0) {
src_fmt = encodings[i].format;
if (dst_fmt != ENCODING_UNKNOWN) {
break;
}
}
if (SDL_strcasecmp(tocode, encodings[i].name) == 0) {
dst_fmt = encodings[i].format;
if (src_fmt != ENCODING_UNKNOWN) {
break;
}
}
}
if (src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN) {
SDL_iconv_t cd = (SDL_iconv_t)SDL_malloc(sizeof(*cd));
if (cd) {
cd->src_fmt = src_fmt;
cd->dst_fmt = dst_fmt;
return cd;
}
}
return (SDL_iconv_t)-1;
}
size_t SDL_iconv(SDL_iconv_t cd,
const char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
// For simplicity, we'll convert everything to and from UCS-4
const char *src;
char *dst;
size_t srclen, dstlen;
Uint32 ch = 0;
size_t total;
if ((size_t)cd == SDL_ICONV_ERROR) {
return SDL_ICONV_ERROR;
}
if (!inbuf || !*inbuf) {
// Reset the context
return 0;
}
if (!outbuf || !*outbuf || !outbytesleft || !*outbytesleft) {
return SDL_ICONV_E2BIG;
}
src = *inbuf;
srclen = (inbytesleft ? *inbytesleft : 0);
dst = *outbuf;
dstlen = *outbytesleft;
switch (cd->src_fmt) {
case ENCODING_UTF16:
// Scan for a byte order marker
{
Uint8 *p = (Uint8 *)src;
size_t n = srclen / 2;
while (n) {
if (p[0] == 0xFF && p[1] == 0xFE) {
cd->src_fmt = ENCODING_UTF16BE;
break;
} else if (p[0] == 0xFE && p[1] == 0xFF) {
cd->src_fmt = ENCODING_UTF16LE;
break;
}
p += 2;
--n;
}
if (n == 0) {
// We can't tell, default to host order
cd->src_fmt = ENCODING_UTF16NATIVE;
}
}
break;
case ENCODING_UTF32:
// Scan for a byte order marker
{
Uint8 *p = (Uint8 *)src;
size_t n = srclen / 4;
while (n) {
if (p[0] == 0xFF && p[1] == 0xFE &&
p[2] == 0x00 && p[3] == 0x00) {
cd->src_fmt = ENCODING_UTF32BE;
break;
} else if (p[0] == 0x00 && p[1] == 0x00 &&
p[2] == 0xFE && p[3] == 0xFF) {
cd->src_fmt = ENCODING_UTF32LE;
break;
}
p += 4;
--n;
}
if (n == 0) {
// We can't tell, default to host order
cd->src_fmt = ENCODING_UTF32NATIVE;
}
}
break;
}
switch (cd->dst_fmt) {
case ENCODING_UTF16:
// Default to host order, need to add byte order marker
if (dstlen < 2) {
return SDL_ICONV_E2BIG;
}
*(Uint16 *)dst = UNICODE_BOM;
dst += 2;
dstlen -= 2;
cd->dst_fmt = ENCODING_UTF16NATIVE;
break;
case ENCODING_UTF32:
// Default to host order, need to add byte order marker
if (dstlen < 4) {
return SDL_ICONV_E2BIG;
}
*(Uint32 *)dst = UNICODE_BOM;
dst += 4;
dstlen -= 4;
cd->dst_fmt = ENCODING_UTF32NATIVE;
break;
}
total = 0;
while (srclen > 0) {
// Decode a character
switch (cd->src_fmt) {
case ENCODING_ASCII:
{
Uint8 *p = (Uint8 *)src;
ch = (Uint32)(p[0] & 0x7F);
++src;
--srclen;
} break;
case ENCODING_LATIN1:
{
Uint8 *p = (Uint8 *)src;
ch = (Uint32)p[0];
++src;
--srclen;
} break;
case ENCODING_UTF8: // RFC 3629
{
Uint8 *p = (Uint8 *)src;
size_t left = 0;
bool overlong = false;
if (p[0] >= 0xF0) {
if ((p[0] & 0xF8) != 0xF0) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if (p[0] == 0xF0 && srclen > 1 && (p[1] & 0xF0) == 0x80) {
overlong = true;
}
ch = (Uint32)(p[0] & 0x07);
left = 3;
}
} else if (p[0] >= 0xE0) {
if ((p[0] & 0xF0) != 0xE0) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if (p[0] == 0xE0 && srclen > 1 && (p[1] & 0xE0) == 0x80) {
overlong = true;
}
ch = (Uint32)(p[0] & 0x0F);
left = 2;
}
} else if (p[0] >= 0xC0) {
if ((p[0] & 0xE0) != 0xC0) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if ((p[0] & 0xDE) == 0xC0) {
overlong = true;
}
ch = (Uint32)(p[0] & 0x1F);
left = 1;
}
} else {
if (p[0] & 0x80) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
ch = (Uint32)p[0];
}
}
++src;
--srclen;
if (srclen < left) {
return SDL_ICONV_EINVAL;
}
while (left--) {
++p;
if ((p[0] & 0xC0) != 0x80) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
ch <<= 6;
ch |= (p[0] & 0x3F);
++src;
--srclen;
}
if (overlong) {
/* Potential security risk
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
}
if ((ch >= 0xD800 && ch <= 0xDFFF) ||
(ch == 0xFFFE || ch == 0xFFFF) || ch > 0x10FFFF) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
}
} break;
case ENCODING_UTF16BE: // RFC 2781
{
Uint8 *p = (Uint8 *)src;
Uint16 W1, W2;
if (srclen < 2) {
return SDL_ICONV_EINVAL;
}
W1 = ((Uint16)p[0] << 8) | (Uint16)p[1];
src += 2;
srclen -= 2;
if (W1 < 0xD800 || W1 > 0xDFFF) {
ch = (Uint32)W1;
break;
}
if (W1 > 0xDBFF) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
if (srclen < 2) {
return SDL_ICONV_EINVAL;
}
p = (Uint8 *)src;
W2 = ((Uint16)p[0] << 8) | (Uint16)p[1];
src += 2;
srclen -= 2;
if (W2 < 0xDC00 || W2 > 0xDFFF) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
ch = (((Uint32)(W1 & 0x3FF) << 10) |
(Uint32)(W2 & 0x3FF)) +
0x10000;
} break;
case ENCODING_UTF16LE: // RFC 2781
{
Uint8 *p = (Uint8 *)src;
Uint16 W1, W2;
if (srclen < 2) {
return SDL_ICONV_EINVAL;
}
W1 = ((Uint16)p[1] << 8) | (Uint16)p[0];
src += 2;
srclen -= 2;
if (W1 < 0xD800 || W1 > 0xDFFF) {
ch = (Uint32)W1;
break;
}
if (W1 > 0xDBFF) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
if (srclen < 2) {
return SDL_ICONV_EINVAL;
}
p = (Uint8 *)src;
W2 = ((Uint16)p[1] << 8) | (Uint16)p[0];
src += 2;
srclen -= 2;
if (W2 < 0xDC00 || W2 > 0xDFFF) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
ch = (((Uint32)(W1 & 0x3FF) << 10) |
(Uint32)(W2 & 0x3FF)) +
0x10000;
} break;
case ENCODING_UCS2LE:
{
Uint8 *p = (Uint8 *)src;
if (srclen < 2) {
return SDL_ICONV_EINVAL;
}
ch = ((Uint32)p[1] << 8) | (Uint32)p[0];
src += 2;
srclen -= 2;
} break;
case ENCODING_UCS2BE:
{
Uint8 *p = (Uint8 *)src;
if (srclen < 2) {
return SDL_ICONV_EINVAL;
}
ch = ((Uint32)p[0] << 8) | (Uint32)p[1];
src += 2;
srclen -= 2;
} break;
case ENCODING_UCS4BE:
case ENCODING_UTF32BE:
{
Uint8 *p = (Uint8 *)src;
if (srclen < 4) {
return SDL_ICONV_EINVAL;
}
ch = ((Uint32)p[0] << 24) |
((Uint32)p[1] << 16) |
((Uint32)p[2] << 8) | (Uint32)p[3];
src += 4;
srclen -= 4;
} break;
case ENCODING_UCS4LE:
case ENCODING_UTF32LE:
{
Uint8 *p = (Uint8 *)src;
if (srclen < 4) {
return SDL_ICONV_EINVAL;
}
ch = ((Uint32)p[3] << 24) |
((Uint32)p[2] << 16) |
((Uint32)p[1] << 8) | (Uint32)p[0];
src += 4;
srclen -= 4;
} break;
}
// Encode a character
switch (cd->dst_fmt) {
case ENCODING_ASCII:
{
Uint8 *p = (Uint8 *)dst;
if (dstlen < 1) {
return SDL_ICONV_E2BIG;
}
if (ch > 0x7F) {
*p = UNKNOWN_ASCII;
} else {
*p = (Uint8)ch;
}
++dst;
--dstlen;
} break;
case ENCODING_LATIN1:
{
Uint8 *p = (Uint8 *)dst;
if (dstlen < 1) {
return SDL_ICONV_E2BIG;
}
if (ch > 0xFF) {
*p = UNKNOWN_ASCII;
} else {
*p = (Uint8)ch;
}
++dst;
--dstlen;
} break;
case ENCODING_UTF8: // RFC 3629
{
Uint8 *p = (Uint8 *)dst;
if (ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}
if (ch <= 0x7F) {
if (dstlen < 1) {
return SDL_ICONV_E2BIG;
}
*p = (Uint8)ch;
++dst;
--dstlen;
} else if (ch <= 0x7FF) {
if (dstlen < 2) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xC0 | (Uint8)((ch >> 6) & 0x1F);
p[1] = 0x80 | (Uint8)(ch & 0x3F);
dst += 2;
dstlen -= 2;
} else if (ch <= 0xFFFF) {
if (dstlen < 3) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xE0 | (Uint8)((ch >> 12) & 0x0F);
p[1] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
p[2] = 0x80 | (Uint8)(ch & 0x3F);
dst += 3;
dstlen -= 3;
} else {
if (dstlen < 4) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xF0 | (Uint8)((ch >> 18) & 0x07);
p[1] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
p[2] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
p[3] = 0x80 | (Uint8)(ch & 0x3F);
dst += 4;
dstlen -= 4;
}
} break;
case ENCODING_UTF16BE: // RFC 2781
{
Uint8 *p = (Uint8 *)dst;
if (ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}
if (ch < 0x10000) {
if (dstlen < 2) {
return SDL_ICONV_E2BIG;
}
p[0] = (Uint8)(ch >> 8);
p[1] = (Uint8)ch;
dst += 2;
dstlen -= 2;
} else {
Uint16 W1, W2;
if (dstlen < 4) {
return SDL_ICONV_E2BIG;
}
ch = ch - 0x10000;
W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
p[0] = (Uint8)(W1 >> 8);
p[1] = (Uint8)W1;
p[2] = (Uint8)(W2 >> 8);
p[3] = (Uint8)W2;
dst += 4;
dstlen -= 4;
}
} break;
case ENCODING_UTF16LE: // RFC 2781
{
Uint8 *p = (Uint8 *)dst;
if (ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}
if (ch < 0x10000) {
if (dstlen < 2) {
return SDL_ICONV_E2BIG;
}
p[1] = (Uint8)(ch >> 8);
p[0] = (Uint8)ch;
dst += 2;
dstlen -= 2;
} else {
Uint16 W1, W2;
if (dstlen < 4) {
return SDL_ICONV_E2BIG;
}
ch = ch - 0x10000;
W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
p[1] = (Uint8)(W1 >> 8);
p[0] = (Uint8)W1;
p[3] = (Uint8)(W2 >> 8);
p[2] = (Uint8)W2;
dst += 4;
dstlen -= 4;
}
} break;
case ENCODING_UCS2BE:
{
Uint8 *p = (Uint8 *)dst;
if (ch > 0xFFFF) {
ch = UNKNOWN_UNICODE;
}
if (dstlen < 2) {
return SDL_ICONV_E2BIG;
}
p[0] = (Uint8)(ch >> 8);
p[1] = (Uint8)ch;
dst += 2;
dstlen -= 2;
} break;
case ENCODING_UCS2LE:
{
Uint8 *p = (Uint8 *)dst;
if (ch > 0xFFFF) {
ch = UNKNOWN_UNICODE;
}
if (dstlen < 2) {
return SDL_ICONV_E2BIG;
}
p[1] = (Uint8)(ch >> 8);
p[0] = (Uint8)ch;
dst += 2;
dstlen -= 2;
} break;
case ENCODING_UTF32BE:
if (ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}
SDL_FALLTHROUGH;
case ENCODING_UCS4BE:
if (ch > 0x7FFFFFFF) {
ch = UNKNOWN_UNICODE;
}
{
Uint8 *p = (Uint8 *)dst;
if (dstlen < 4) {
return SDL_ICONV_E2BIG;
}
p[0] = (Uint8)(ch >> 24);
p[1] = (Uint8)(ch >> 16);
p[2] = (Uint8)(ch >> 8);
p[3] = (Uint8)ch;
dst += 4;
dstlen -= 4;
}
break;
case ENCODING_UTF32LE:
if (ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}
SDL_FALLTHROUGH;
case ENCODING_UCS4LE:
if (ch > 0x7FFFFFFF) {
ch = UNKNOWN_UNICODE;
}
{
Uint8 *p = (Uint8 *)dst;
if (dstlen < 4) {
return SDL_ICONV_E2BIG;
}
p[3] = (Uint8)(ch >> 24);
p[2] = (Uint8)(ch >> 16);
p[1] = (Uint8)(ch >> 8);
p[0] = (Uint8)ch;
dst += 4;
dstlen -= 4;
}
break;
}
// Update state
*inbuf = src;
*inbytesleft = srclen;
*outbuf = dst;
*outbytesleft = dstlen;
++total;
}
return total;
}
int SDL_iconv_close(SDL_iconv_t cd)
{
if (cd == (SDL_iconv_t)-1) {
return -1;
}
SDL_free(cd);
return 0;
}
#endif // !HAVE_ICONV
char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft)
{
SDL_iconv_t cd;
char *string;
size_t stringsize;
char *outbuf;
size_t outbytesleft;
size_t retCode = 0;
if (!tocode || !*tocode) {
tocode = "UTF-8";
}
if (!fromcode || !*fromcode) {
fromcode = "UTF-8";
}
cd = SDL_iconv_open(tocode, fromcode);
if (cd == (SDL_iconv_t)-1) {
return NULL;
}
stringsize = inbytesleft;
string = (char *)SDL_malloc(stringsize + sizeof(Uint32));
if (!string) {
SDL_iconv_close(cd);
return NULL;
}
outbuf = string;
outbytesleft = stringsize;
SDL_memset(outbuf, 0, sizeof(Uint32));
while (inbytesleft > 0) {
const size_t oldinbytesleft = inbytesleft;
retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
switch (retCode) {
case SDL_ICONV_E2BIG:
{
const ptrdiff_t diff = (ptrdiff_t) (outbuf - string);
char *oldstring = string;
stringsize *= 2;
string = (char *)SDL_realloc(string, stringsize + sizeof(Uint32));
if (!string) {
SDL_free(oldstring);
SDL_iconv_close(cd);
return NULL;
}
outbuf = string + diff;
outbytesleft = stringsize - diff;
SDL_memset(outbuf, 0, sizeof(Uint32));
continue;
}
case SDL_ICONV_EILSEQ:
// Try skipping some input data - not perfect, but...
++inbuf;
--inbytesleft;
break;
case SDL_ICONV_EINVAL:
case SDL_ICONV_ERROR:
// We can't continue...
inbytesleft = 0;
break;
}
// Avoid infinite loops when nothing gets converted
if (oldinbytesleft == inbytesleft) {
break;
}
}
SDL_memset(outbuf, 0, sizeof(Uint32));
SDL_iconv_close(cd);
return string;
}

6507
thirdparty/sdl/stdlib/SDL_malloc.c vendored Normal file

File diff suppressed because it is too large Load Diff

101
thirdparty/sdl/stdlib/SDL_memcpy.c vendored Normal file
View File

@@ -0,0 +1,101 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_memcpy
#undef SDL_memcpy
#endif
#if SDL_DYNAMIC_API
#define SDL_memcpy SDL_memcpy_REAL
#endif
void *SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
{
#if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC)
/* Presumably this is well tuned for speed.
On my machine this is twice as fast as the C code below.
*/
return __builtin_memcpy(dst, src, len);
#elif defined(HAVE_MEMCPY)
return memcpy(dst, src, len);
#elif defined(HAVE_BCOPY)
bcopy(src, dst, len);
return dst;
#else
/* GCC 4.9.0 with -O3 will generate movaps instructions with the loop
using Uint32* pointers, so we need to make sure the pointers are
aligned before we loop using them.
*/
if (((uintptr_t)src & 0x3) || ((uintptr_t)dst & 0x3)) {
// Do an unaligned byte copy
Uint8 *srcp1 = (Uint8 *)src;
Uint8 *dstp1 = (Uint8 *)dst;
while (len--) {
*dstp1++ = *srcp1++;
}
} else {
size_t left = (len % 4);
Uint32 *srcp4, *dstp4;
Uint8 *srcp1, *dstp1;
srcp4 = (Uint32 *)src;
dstp4 = (Uint32 *)dst;
len /= 4;
while (len--) {
*dstp4++ = *srcp4++;
}
srcp1 = (Uint8 *)srcp4;
dstp1 = (Uint8 *)dstp4;
switch (left) {
case 3:
*dstp1++ = *srcp1++;
SDL_FALLTHROUGH;
case 2:
*dstp1++ = *srcp1++;
SDL_FALLTHROUGH;
case 1:
*dstp1++ = *srcp1++;
}
}
return dst;
#endif // HAVE_MEMCPY
}
/* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls.
We will provide our own implementation if we're not building with a C runtime. */
#ifndef HAVE_LIBC
// NOLINTNEXTLINE(readability-redundant-declaration)
extern void *memcpy(void *dst, const void *src, size_t len);
#if defined(_MSC_VER) && !defined(__INTEL_LLVM_COMPILER)
#pragma intrinsic(memcpy)
#endif
#if defined(_MSC_VER) && !defined(__clang__)
#pragma function(memcpy)
#endif
// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name)
void *memcpy(void *dst, const void *src, size_t len)
{
return SDL_memcpy(dst, src, len);
}
#endif // !HAVE_LIBC

73
thirdparty/sdl/stdlib/SDL_memmove.c vendored Normal file
View File

@@ -0,0 +1,73 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_memmove
#undef SDL_memmove
#endif
#if SDL_DYNAMIC_API
#define SDL_memmove SDL_memmove_REAL
#endif
void *SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
{
#if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC)
// Presumably this is well tuned for speed.
return __builtin_memmove(dst, src, len);
#elif defined(HAVE_MEMMOVE)
return memmove(dst, src, len);
#else
char *srcp = (char *)src;
char *dstp = (char *)dst;
if (src < dst) {
srcp += len - 1;
dstp += len - 1;
while (len--) {
*dstp-- = *srcp--;
}
} else {
while (len--) {
*dstp++ = *srcp++;
}
}
return dst;
#endif // HAVE_MEMMOVE
}
#ifndef HAVE_LIBC
// NOLINTNEXTLINE(readability-redundant-declaration)
extern void *memmove(void *dst, const void *src, size_t len);
#if defined(_MSC_VER) && !defined(__INTEL_LLVM_COMPILER)
#pragma intrinsic(memmove)
#endif
#if defined(_MSC_VER) && !defined(__clang__)
#pragma function(memmove)
#endif
// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name)
void *memmove(void *dst, const void *src, size_t len)
{
return SDL_memmove(dst, src, len);
}
#endif // !HAVE_LIBC

139
thirdparty/sdl/stdlib/SDL_memset.c vendored Normal file
View File

@@ -0,0 +1,139 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_memset
#undef SDL_memset
#endif
#if SDL_DYNAMIC_API
#define SDL_memset SDL_memset_REAL
#endif
void *SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len)
{
#if defined(__GNUC__) && (defined(HAVE_LIBC) && HAVE_LIBC)
return __builtin_memset(dst, c, len);
#elif defined(HAVE_MEMSET)
return memset(dst, c, len);
#else
size_t left;
Uint32 *dstp4;
Uint8 *dstp1 = (Uint8 *)dst;
Uint8 value1;
Uint32 value4;
// The value used in memset() is a byte, passed as an int
c &= 0xff;
/* The destination pointer needs to be aligned on a 4-byte boundary to
* execute a 32-bit set. Set first bytes manually if needed until it is
* aligned. */
value1 = (Uint8)c;
while ((uintptr_t)dstp1 & 0x3) {
if (len--) {
*dstp1++ = value1;
} else {
return dst;
}
}
value4 = ((Uint32)c | ((Uint32)c << 8) | ((Uint32)c << 16) | ((Uint32)c << 24));
dstp4 = (Uint32 *)dstp1;
left = (len % 4);
len /= 4;
while (len--) {
*dstp4++ = value4;
}
dstp1 = (Uint8 *)dstp4;
switch (left) {
case 3:
*dstp1++ = value1;
SDL_FALLTHROUGH;
case 2:
*dstp1++ = value1;
SDL_FALLTHROUGH;
case 1:
*dstp1++ = value1;
}
return dst;
#endif // HAVE_MEMSET
}
// Note that memset() is a byte assignment and this is a 32-bit assignment, so they're not directly equivalent.
void *SDL_memset4(void *dst, Uint32 val, size_t dwords)
{
#if defined(__APPLE__) && defined(HAVE_STRING_H)
memset_pattern4(dst, &val, dwords * 4);
#elif defined(__GNUC__) && defined(__i386__)
int u0, u1, u2;
__asm__ __volatile__(
"cld \n\t"
"rep ; stosl \n\t"
: "=&D"(u0), "=&a"(u1), "=&c"(u2)
: "0"(dst), "1"(val), "2"(SDL_static_cast(Uint32, dwords))
: "memory");
#else
size_t _n = (dwords + 3) / 4;
Uint32 *_p = SDL_static_cast(Uint32 *, dst);
Uint32 _val = (val);
if (dwords == 0) {
return dst;
}
switch (dwords % 4) {
case 0:
do {
*_p++ = _val;
SDL_FALLTHROUGH;
case 3:
*_p++ = _val;
SDL_FALLTHROUGH;
case 2:
*_p++ = _val;
SDL_FALLTHROUGH;
case 1:
*_p++ = _val;
} while (--_n);
}
#endif
return dst;
}
/* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls.
We will provide our own implementation if we're not building with a C runtime. */
#ifndef HAVE_LIBC
// NOLINTNEXTLINE(readability-redundant-declaration)
extern void *memset(void *dst, int c, size_t len);
#if defined(_MSC_VER) && !defined(__INTEL_LLVM_COMPILER)
#pragma intrinsic(memset)
#endif
#if defined(_MSC_VER) && !defined(__clang__)
#pragma function(memset)
#endif
// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name)
void *memset(void *dst, int c, size_t len)
{
return SDL_memset(dst, c, len);
}
#endif // !HAVE_LIBC

746
thirdparty/sdl/stdlib/SDL_mslibc.c vendored Normal file
View File

@@ -0,0 +1,746 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
// This file contains SDL replacements for functions in the C library
#if !defined(HAVE_LIBC) && !defined(SDL_STATIC_LIB)
// These are some C runtime intrinsics that need to be defined
#ifdef _MSC_VER
#ifndef __FLTUSED__
#define __FLTUSED__
__declspec(selectany) int _fltused = 1;
#endif
#ifdef _M_IX86
// Float to long
void __declspec(naked) _ftol()
{
/* *INDENT-OFF* */
__asm {
push ebp
mov ebp,esp
sub esp,20h
and esp,0FFFFFFF0h
fld st(0)
fst dword ptr [esp+18h]
fistp qword ptr [esp+10h]
fild qword ptr [esp+10h]
mov edx,dword ptr [esp+18h]
mov eax,dword ptr [esp+10h]
test eax,eax
je integer_QnaN_or_zero
arg_is_not_integer_QnaN:
fsubp st(1),st
test edx,edx
jns positive
fstp dword ptr [esp]
mov ecx,dword ptr [esp]
xor ecx,80000000h
add ecx,7FFFFFFFh
adc eax,0
mov edx,dword ptr [esp+14h]
adc edx,0
jmp localexit
positive:
fstp dword ptr [esp]
mov ecx,dword ptr [esp]
add ecx,7FFFFFFFh
sbb eax,0
mov edx,dword ptr [esp+14h]
sbb edx,0
jmp localexit
integer_QnaN_or_zero:
mov edx,dword ptr [esp+14h]
test edx,7FFFFFFFh
jne arg_is_not_integer_QnaN
fstp dword ptr [esp+18h]
fstp dword ptr [esp+18h]
localexit:
leave
ret
}
/* *INDENT-ON* */
}
void _ftol2_sse()
{
_ftol();
}
void _ftol2()
{
_ftol();
}
// 64-bit math operators for 32-bit systems
void __declspec(naked) _allmul()
{
/* *INDENT-OFF* */
__asm {
mov eax, dword ptr[esp+8]
mov ecx, dword ptr[esp+10h]
or ecx, eax
mov ecx, dword ptr[esp+0Ch]
jne hard
mov eax, dword ptr[esp+4]
mul ecx
ret 10h
hard:
push ebx
mul ecx
mov ebx, eax
mov eax, dword ptr[esp+8]
mul dword ptr[esp+14h]
add ebx, eax
mov eax, dword ptr[esp+8]
mul ecx
add edx, ebx
pop ebx
ret 10h
}
/* *INDENT-ON* */
}
void __declspec(naked) _alldiv()
{
/* *INDENT-OFF* */
__asm {
push edi
push esi
push ebx
xor edi,edi
mov eax,dword ptr [esp+14h]
or eax,eax
jge L1
inc edi
mov edx,dword ptr [esp+10h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+14h],eax
mov dword ptr [esp+10h],edx
L1:
mov eax,dword ptr [esp+1Ch]
or eax,eax
jge L2
inc edi
mov edx,dword ptr [esp+18h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+1Ch],eax
mov dword ptr [esp+18h],edx
L2:
or eax,eax
jne L3
mov ecx,dword ptr [esp+18h]
mov eax,dword ptr [esp+14h]
xor edx,edx
div ecx
mov ebx,eax
mov eax,dword ptr [esp+10h]
div ecx
mov edx,ebx
jmp L4
L3:
mov ebx,eax
mov ecx,dword ptr [esp+18h]
mov edx,dword ptr [esp+14h]
mov eax,dword ptr [esp+10h]
L5:
shr ebx,1
rcr ecx,1
shr edx,1
rcr eax,1
or ebx,ebx
jne L5
div ecx
mov esi,eax
mul dword ptr [esp+1Ch]
mov ecx,eax
mov eax,dword ptr [esp+18h]
mul esi
add edx,ecx
jb L6
cmp edx,dword ptr [esp+14h]
ja L6
jb L7
cmp eax,dword ptr [esp+10h]
jbe L7
L6:
dec esi
L7:
xor edx,edx
mov eax,esi
L4:
dec edi
jne L8
neg edx
neg eax
sbb edx,0
L8:
pop ebx
pop esi
pop edi
ret 10h
}
/* *INDENT-ON* */
}
void __declspec(naked) _aulldiv()
{
/* *INDENT-OFF* */
__asm {
push ebx
push esi
mov eax,dword ptr [esp+18h]
or eax,eax
jne L1
mov ecx,dword ptr [esp+14h]
mov eax,dword ptr [esp+10h]
xor edx,edx
div ecx
mov ebx,eax
mov eax,dword ptr [esp+0Ch]
div ecx
mov edx,ebx
jmp L2
L1:
mov ecx,eax
mov ebx,dword ptr [esp+14h]
mov edx,dword ptr [esp+10h]
mov eax,dword ptr [esp+0Ch]
L3:
shr ecx,1
rcr ebx,1
shr edx,1
rcr eax,1
or ecx,ecx
jne L3
div ebx
mov esi,eax
mul dword ptr [esp+18h]
mov ecx,eax
mov eax,dword ptr [esp+14h]
mul esi
add edx,ecx
jb L4
cmp edx,dword ptr [esp+10h]
ja L4
jb L5
cmp eax,dword ptr [esp+0Ch]
jbe L5
L4:
dec esi
L5:
xor edx,edx
mov eax,esi
L2:
pop esi
pop ebx
ret 10h
}
/* *INDENT-ON* */
}
void __declspec(naked) _allrem()
{
/* *INDENT-OFF* */
__asm {
push ebx
push edi
xor edi,edi
mov eax,dword ptr [esp+10h]
or eax,eax
jge L1
inc edi
mov edx,dword ptr [esp+0Ch]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+10h],eax
mov dword ptr [esp+0Ch],edx
L1:
mov eax,dword ptr [esp+18h]
or eax,eax
jge L2
mov edx,dword ptr [esp+14h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+18h],eax
mov dword ptr [esp+14h],edx
L2:
or eax,eax
jne L3
mov ecx,dword ptr [esp+14h]
mov eax,dword ptr [esp+10h]
xor edx,edx
div ecx
mov eax,dword ptr [esp+0Ch]
div ecx
mov eax,edx
xor edx,edx
dec edi
jns L4
jmp L8
L3:
mov ebx,eax
mov ecx,dword ptr [esp+14h]
mov edx,dword ptr [esp+10h]
mov eax,dword ptr [esp+0Ch]
L5:
shr ebx,1
rcr ecx,1
shr edx,1
rcr eax,1
or ebx,ebx
jne L5
div ecx
mov ecx,eax
mul dword ptr [esp+18h]
xchg eax,ecx
mul dword ptr [esp+14h]
add edx,ecx
jb L6
cmp edx,dword ptr [esp+10h]
ja L6
jb L7
cmp eax,dword ptr [esp+0Ch]
jbe L7
L6:
sub eax,dword ptr [esp+14h]
sbb edx,dword ptr [esp+18h]
L7:
sub eax,dword ptr [esp+0Ch]
sbb edx,dword ptr [esp+10h]
dec edi
jns L8
L4:
neg edx
neg eax
sbb edx,0
L8:
pop edi
pop ebx
ret 10h
}
/* *INDENT-ON* */
}
void __declspec(naked) _aullrem()
{
/* *INDENT-OFF* */
__asm {
push ebx
mov eax,dword ptr [esp+14h]
or eax,eax
jne L1
mov ecx,dword ptr [esp+10h]
mov eax,dword ptr [esp+0Ch]
xor edx,edx
div ecx
mov eax,dword ptr [esp+8]
div ecx
mov eax,edx
xor edx,edx
jmp L2
L1:
mov ecx,eax
mov ebx,dword ptr [esp+10h]
mov edx,dword ptr [esp+0Ch]
mov eax,dword ptr [esp+8]
L3:
shr ecx,1
rcr ebx,1
shr edx,1
rcr eax,1
or ecx,ecx
jne L3
div ebx
mov ecx,eax
mul dword ptr [esp+14h]
xchg eax,ecx
mul dword ptr [esp+10h]
add edx,ecx
jb L4
cmp edx,dword ptr [esp+0Ch]
ja L4
jb L5
cmp eax,dword ptr [esp+8]
jbe L5
L4:
sub eax,dword ptr [esp+10h]
sbb edx,dword ptr [esp+14h]
L5:
sub eax,dword ptr [esp+8]
sbb edx,dword ptr [esp+0Ch]
neg edx
neg eax
sbb edx,0
L2:
pop ebx
ret 10h
}
/* *INDENT-ON* */
}
void __declspec(naked) _alldvrm()
{
/* *INDENT-OFF* */
__asm {
push edi
push esi
push ebp
xor edi,edi
xor ebp,ebp
mov eax,dword ptr [esp+14h]
or eax,eax
jge L1
inc edi
inc ebp
mov edx,dword ptr [esp+10h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+14h],eax
mov dword ptr [esp+10h],edx
L1:
mov eax,dword ptr [esp+1Ch]
or eax,eax
jge L2
inc edi
mov edx,dword ptr [esp+18h]
neg eax
neg edx
sbb eax,0
mov dword ptr [esp+1Ch],eax
mov dword ptr [esp+18h],edx
L2:
or eax,eax
jne L3
mov ecx,dword ptr [esp+18h]
mov eax,dword ptr [esp+14h]
xor edx,edx
div ecx
mov ebx,eax
mov eax,dword ptr [esp+10h]
div ecx
mov esi,eax
mov eax,ebx
mul dword ptr [esp+18h]
mov ecx,eax
mov eax,esi
mul dword ptr [esp+18h]
add edx,ecx
jmp L4
L3:
mov ebx,eax
mov ecx,dword ptr [esp+18h]
mov edx,dword ptr [esp+14h]
mov eax,dword ptr [esp+10h]
L5:
shr ebx,1
rcr ecx,1
shr edx,1
rcr eax,1
or ebx,ebx
jne L5
div ecx
mov esi,eax
mul dword ptr [esp+1Ch]
mov ecx,eax
mov eax,dword ptr [esp+18h]
mul esi
add edx,ecx
jb L6
cmp edx,dword ptr [esp+14h]
ja L6
jb L7
cmp eax,dword ptr [esp+10h]
jbe L7
L6:
dec esi
sub eax,dword ptr [esp+18h]
sbb edx,dword ptr [esp+1Ch]
L7:
xor ebx,ebx
L4:
sub eax,dword ptr [esp+10h]
sbb edx,dword ptr [esp+14h]
dec ebp
jns L9
neg edx
neg eax
sbb edx,0
L9:
mov ecx,edx
mov edx,ebx
mov ebx,ecx
mov ecx,eax
mov eax,esi
dec edi
jne L8
neg edx
neg eax
sbb edx,0
L8:
pop ebp
pop esi
pop edi
ret 10h
}
/* *INDENT-ON* */
}
void __declspec(naked) _aulldvrm()
{
/* *INDENT-OFF* */
__asm {
push esi
mov eax,dword ptr [esp+14h]
or eax,eax
jne L1
mov ecx,dword ptr [esp+10h]
mov eax,dword ptr [esp+0Ch]
xor edx,edx
div ecx
mov ebx,eax
mov eax,dword ptr [esp+8]
div ecx
mov esi,eax
mov eax,ebx
mul dword ptr [esp+10h]
mov ecx,eax
mov eax,esi
mul dword ptr [esp+10h]
add edx,ecx
jmp L2
L1:
mov ecx,eax
mov ebx,dword ptr [esp+10h]
mov edx,dword ptr [esp+0Ch]
mov eax,dword ptr [esp+8]
L3:
shr ecx,1
rcr ebx,1
shr edx,1
rcr eax,1
or ecx,ecx
jne L3
div ebx
mov esi,eax
mul dword ptr [esp+14h]
mov ecx,eax
mov eax,dword ptr [esp+10h]
mul esi
add edx,ecx
jb L4
cmp edx,dword ptr [esp+0Ch]
ja L4
jb L5
cmp eax,dword ptr [esp+8]
jbe L5
L4:
dec esi
sub eax,dword ptr [esp+10h]
sbb edx,dword ptr [esp+14h]
L5:
xor ebx,ebx
L2:
sub eax,dword ptr [esp+8]
sbb edx,dword ptr [esp+0Ch]
neg edx
neg eax
sbb edx,0
mov ecx,edx
mov edx,ebx
mov ebx,ecx
mov ecx,eax
mov eax,esi
pop esi
ret 10h
}
/* *INDENT-ON* */
}
void __declspec(naked) _allshl()
{
/* *INDENT-OFF* */
__asm {
cmp cl,40h
jae RETZERO
cmp cl,20h
jae MORE32
shld edx,eax,cl
shl eax,cl
ret
MORE32:
mov edx,eax
xor eax,eax
and cl,1Fh
shl edx,cl
ret
RETZERO:
xor eax,eax
xor edx,edx
ret
}
/* *INDENT-ON* */
}
void __declspec(naked) _allshr()
{
/* *INDENT-OFF* */
__asm {
cmp cl,3Fh
jae RETSIGN
cmp cl,20h
jae MORE32
shrd eax,edx,cl
sar edx,cl
ret
MORE32:
mov eax,edx
sar edx,1Fh
and cl,1Fh
sar eax,cl
ret
RETSIGN:
sar edx,1Fh
mov eax,edx
ret
}
/* *INDENT-ON* */
}
void __declspec(naked) _aullshr()
{
/* *INDENT-OFF* */
__asm {
cmp cl,40h
jae RETZERO
cmp cl,20h
jae MORE32
shrd eax,edx,cl
shr edx,cl
ret
MORE32:
mov eax,edx
xor edx,edx
and cl,1Fh
shr eax,cl
ret
RETZERO:
xor eax,eax
xor edx,edx
ret
}
/* *INDENT-ON* */
}
void __declspec(naked) _chkstk(void)
{
__asm {
push ecx
mov ecx,esp ; lea ecx,dword ptr [esp]+4
add ecx,4
sub ecx,eax
sbb eax,eax
not eax
and ecx,eax
mov eax,esp
and eax,0xfffff000
L1:
cmp ecx,eax
jb short L2
mov eax,ecx
pop ecx
xchg esp,eax
mov eax,dword ptr [eax]
mov dword ptr [esp],eax
ret
L2:
sub eax,0x1000
test dword ptr [eax],eax
jmp short L1
}
}
void __declspec(naked) _alloca_probe_8(void)
{
/* *INDENT-OFF* */
__asm {
push ecx
mov ecx,esp ; lea ecx,dword ptr [esp]+8
add ecx,8
sub ecx,eax
and ecx,0x7
add eax,ecx
sbb ecx,ecx
or eax,ecx
pop ecx
jmp _chkstk
}
/* *INDENT-ON* */
}
void __declspec(naked) _alloca_probe_16(void)
{
/* *INDENT-OFF* */
__asm {
push ecx
mov ecx,esp ; lea ecx,dword ptr [esp]+8
add ecx,8
sub ecx,eax
and ecx,0xf
add eax,ecx
sbb ecx,ecx
or eax,ecx
pop ecx
jmp _chkstk
}
/* *INDENT-ON* */
}
#endif // _M_IX86
#endif // MSC_VER
#ifdef __ICL
/* The classic Intel compiler generates calls to _intel_fast_memcpy
* and _intel_fast_memset when building an optimized SDL library */
void *_intel_fast_memcpy(void *dst, const void *src, size_t len)
{
return SDL_memcpy(dst, src, len);
}
void *_intel_fast_memset(void *dst, int c, size_t len)
{
return SDL_memset(dst, c, len);
}
#endif
#endif // !HAVE_LIBC && !SDL_STATIC_LIB

87
thirdparty/sdl/stdlib/SDL_murmur3.c vendored Normal file
View File

@@ -0,0 +1,87 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
// Public domain murmur3 32-bit hash algorithm
//
// Adapted from: https://en.wikipedia.org/wiki/MurmurHash
static SDL_INLINE Uint32 murmur_32_scramble(Uint32 k)
{
k *= 0xcc9e2d51;
k = (k << 15) | (k >> 17);
k *= 0x1b873593;
return k;
}
Uint32 SDLCALL SDL_murmur3_32(const void *data, size_t len, Uint32 seed)
{
const Uint8 *bytes = (const Uint8 *)data;
Uint32 hash = seed;
Uint32 k;
// Read in groups of 4.
if ((((uintptr_t)bytes) & 3) == 0) {
// We can do aligned 32-bit reads
for (size_t i = len >> 2; i--; ) {
k = *(const Uint32 *)bytes;
k = SDL_Swap32LE(k);
bytes += sizeof(Uint32);
hash ^= murmur_32_scramble(k);
hash = (hash << 13) | (hash >> 19);
hash = hash * 5 + 0xe6546b64;
}
} else {
for (size_t i = len >> 2; i--; ) {
SDL_memcpy(&k, bytes, sizeof(Uint32));
k = SDL_Swap32LE(k);
bytes += sizeof(Uint32);
hash ^= murmur_32_scramble(k);
hash = (hash << 13) | (hash >> 19);
hash = hash * 5 + 0xe6546b64;
}
}
// Read the rest.
size_t left = (len & 3);
if (left) {
k = 0;
for (size_t i = left; i--; ) {
k <<= 8;
k |= bytes[i];
}
// A swap is *not* necessary here because the preceding loop already
// places the low bytes in the low places according to whatever endianness
// we use. Swaps only apply when the memory is copied in a chunk.
hash ^= murmur_32_scramble(k);
}
/* Finalize. */
hash ^= len;
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash;
}

574
thirdparty/sdl/stdlib/SDL_qsort.c vendored Normal file
View File

@@ -0,0 +1,574 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
// SDL3 always uses its own internal qsort implementation, below, so
// it can guarantee stable sorts across platforms and not have to
// tapdance to support the various qsort_r interfaces, or bridge from
// the C runtime's non-SDLCALL compare functions.
#ifdef assert
#undef assert
#endif
#define assert SDL_assert
#ifdef malloc
#undef malloc
#endif
#define malloc SDL_malloc
#ifdef free
#undef free
#endif
#define free SDL_free
#ifdef memcpy
#undef memcpy
#endif
#define memcpy SDL_memcpy
#ifdef memmove
#undef memmove
#endif
#define memmove SDL_memmove
/*
This code came from Gareth McCaughan, under the zlib license.
Specifically this: https://www.mccaughan.org.uk/software/qsort.c-1.16
Everything below this comment until the HAVE_QSORT #endif was from Gareth
(any minor changes will be noted inline).
Thank you to Gareth for relicensing this code under the zlib license for our
benefit!
Update for SDL3: we have modified this from a qsort function to qsort_r.
--ryan.
*/
/* This is a drop-in replacement for the C library's |qsort()| routine.
*
* It is intended for use where you know or suspect that your
* platform's qsort is bad. If that isn't the case, then you
* should probably use the qsort your system gives you in preference
* to mine -- it will likely have been tested and tuned better.
*
* Features:
* - Median-of-three pivoting (and more)
* - Truncation and final polishing by a single insertion sort
* - Early truncation when no swaps needed in pivoting step
* - Explicit recursion, guaranteed not to overflow
* - A few little wrinkles stolen from the GNU |qsort()|.
* (For the avoidance of doubt, no code was stolen, only
* broad ideas.)
* - separate code for non-aligned / aligned / word-size objects
*
* Earlier releases of this code used an idiosyncratic licence
* I wrote myself, because I'm an idiot. The code is now released
* under the "zlib/libpng licence"; you will find the actual
* terms in the next comment. I request (but do not require)
* that if you make any changes beyond the name of the exported
* routine and reasonable tweaks to the TRUNC_* and
* PIVOT_THRESHOLD values, you modify the _ID string so as
* to make it clear that you have changed the code.
*
* If you find problems with this code, or find ways of
* making it significantly faster, please let me know!
* My e-mail address, valid as of early 2016 and for the
* foreseeable future, is
* gareth.mccaughan@pobox.com
* Thanks!
*
* Gareth McCaughan
*/
/* Copyright (c) 1998-2021 Gareth McCaughan
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
/* Revision history since release:
* 1998-03-19 v1.12 First release I have any records of.
* 2007-09-02 v1.13 Fix bug kindly reported by Dan Bodoh
* (premature termination of recursion).
* Add a few clarifying comments.
* Minor improvements to debug output.
* 2016-02-21 v1.14 Replace licence with 2-clause BSD,
* and clarify a couple of things in
* comments. No code changes.
* 2016-03-10 v1.15 Fix bug kindly reported by Ryan Gordon
* (pre-insertion-sort messed up).
* Disable DEBUG_QSORT by default.
* Tweak comments very slightly.
* 2021-02-20 v1.16 Fix bug kindly reported by Ray Gardner
* (error in recursion leading to possible
* stack overflow).
* When checking alignment, avoid casting
* pointer to possibly-smaller integer.
*/
/* BEGIN SDL CHANGE ... commented this out with an #if 0 block. --ryan. */
#if 0
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#undef DEBUG_QSORT
static char _ID[]="<qsort.c gjm WITH CHANGES FOR SDL3 1.16 2021-02-20>";
#endif
/* END SDL CHANGE ... commented this out with an #if 0 block. --ryan. */
/* How many bytes are there per word? (Must be a power of 2,
* and must in fact equal sizeof(int).)
*/
#define WORD_BYTES sizeof(int)
/* How big does our stack need to be? Answer: one entry per
* bit in a |size_t|. (Actually, a bit less because we don't
* recurse all the way down to size-1 subarrays.)
*/
#define STACK_SIZE (8*sizeof(size_t))
/* Different situations have slightly different requirements,
* and we make life epsilon easier by using different truncation
* points for the three different cases.
* So far, I have tuned TRUNC_words and guessed that the same
* value might work well for the other two cases. Of course
* what works well on my machine might work badly on yours.
*/
#define TRUNC_nonaligned 12
#define TRUNC_aligned 12
#define TRUNC_words 12*WORD_BYTES /* nb different meaning */
/* We use a simple pivoting algorithm for shortish sub-arrays
* and a more complicated one for larger ones. The threshold
* is PIVOT_THRESHOLD.
*/
#define PIVOT_THRESHOLD 40
typedef struct { char * first; char * last; } stack_entry;
#define pushLeft {stack[stacktop].first=ffirst;stack[stacktop++].last=last;}
#define pushRight {stack[stacktop].first=first;stack[stacktop++].last=llast;}
#define doLeft {first=ffirst;llast=last;continue;}
#define doRight {ffirst=first;last=llast;continue;}
#define pop {if (--stacktop<0) break;\
first=ffirst=stack[stacktop].first;\
last=llast=stack[stacktop].last;\
continue;}
/* Some comments on the implementation.
* 1. When we finish partitioning the array into "low"
* and "high", we forget entirely about short subarrays,
* because they'll be done later by insertion sort.
* Doing lots of little insertion sorts might be a win
* on large datasets for locality-of-reference reasons,
* but it makes the code much nastier and increases
* bookkeeping overhead.
* 2. We always save the longer and get to work on the
* shorter. This guarantees that whenever we push
* a k'th entry onto the stack we are about to get
* working on something of size <= N/2^k where N is
* the original array size; so the stack can't need
* more than log_2(max-array-size) entries.
* 3. We choose a pivot by looking at the first, last
* and middle elements. We arrange them into order
* because it's easy to do that in conjunction with
* choosing the pivot, and it makes things a little
* easier in the partitioning step. Anyway, the pivot
* is the middle of these three. It's still possible
* to construct datasets where the algorithm takes
* time of order n^2, but it simply never happens in
* practice.
* 3' Newsflash: On further investigation I find that
* it's easy to construct datasets where median-of-3
* simply isn't good enough. So on large-ish subarrays
* we do a more sophisticated pivoting: we take three
* sets of 3 elements, find their medians, and then
* take the median of those.
* 4. We copy the pivot element to a separate place
* because that way we can always do our comparisons
* directly against a pointer to that separate place,
* and don't have to wonder "did we move the pivot
* element?". This makes the inner loop better.
* 5. It's possible to make the pivoting even more
* reliable by looking at more candidates when n
* is larger. (Taking this to its logical conclusion
* results in a variant of quicksort that doesn't
* have that n^2 worst case.) However, the overhead
* from the extra bookkeeping means that it's just
* not worth while.
* 6. This is pretty clean and portable code. Here are
* all the potential portability pitfalls and problems
* I know of:
* - In one place (the insertion sort) I construct
* a pointer that points just past the end of the
* supplied array, and assume that (a) it won't
* compare equal to any pointer within the array,
* and (b) it will compare equal to a pointer
* obtained by stepping off the end of the array.
* These might fail on some segmented architectures.
* - I assume that there are 8 bits in a |char| when
* computing the size of stack needed. This would
* fail on machines with 9-bit or 16-bit bytes.
* - I assume that if |((int)base&(sizeof(int)-1))==0|
* and |(size&(sizeof(int)-1))==0| then it's safe to
* get at array elements via |int*|s, and that if
* actually |size==sizeof(int)| as well then it's
* safe to treat the elements as |int|s. This might
* fail on systems that convert pointers to integers
* in non-standard ways.
* - I assume that |8*sizeof(size_t)<=INT_MAX|. This
* would be false on a machine with 8-bit |char|s,
* 16-bit |int|s and 4096-bit |size_t|s. :-)
*/
/* The recursion logic is the same in each case.
* We keep chopping up until we reach subarrays of size
* strictly less than Trunc; we leave these unsorted. */
#define Recurse(Trunc) \
{ size_t l=last-ffirst,r=llast-first; \
if (l<Trunc) { \
if (r>=Trunc) doRight \
else pop \
} \
else if (l<=r) { pushRight; doLeft } \
else if (r>=Trunc) { pushLeft; doRight }\
else doLeft \
}
/* and so is the pivoting logic (note: last is inclusive): */
#define Pivot(swapper,sz) \
if ((size_t)(last-first)>PIVOT_THRESHOLD*sz) mid=pivot_big(first,mid,last,sz,compare,userdata);\
else { \
if (compare(userdata,first,mid)<0) { \
if (compare(userdata,mid,last)>0) { \
swapper(mid,last); \
if (compare(userdata,first,mid)>0) swapper(first,mid);\
} \
} \
else { \
if (compare(userdata,mid,last)>0) swapper(first,last)\
else { \
swapper(first,mid); \
if (compare(userdata,mid,last)>0) swapper(mid,last);\
} \
} \
first+=sz; last-=sz; \
}
#ifdef DEBUG_QSORT
#include <stdio.h>
#endif
/* and so is the partitioning logic: */
#define Partition(swapper,sz) { \
do { \
while (compare(userdata,first,pivot)<0) first+=sz; \
while (compare(userdata,pivot,last)<0) last-=sz; \
if (first<last) { \
swapper(first,last); \
first+=sz; last-=sz; } \
else if (first==last) { first+=sz; last-=sz; break; }\
} while (first<=last); \
}
/* and so is the pre-insertion-sort operation of putting
* the smallest element into place as a sentinel.
* Doing this makes the inner loop nicer. I got this
* idea from the GNU implementation of qsort().
* We find the smallest element from the first |nmemb|,
* or the first |limit|, whichever is smaller;
* therefore we must have ensured that the globally smallest
* element is in the first |limit| (because our
* quicksort recursion bottoms out only once we
* reach subarrays smaller than |limit|).
*/
#define PreInsertion(swapper,limit,sz) \
first=base; \
last=first + ((nmemb>limit ? limit : nmemb)-1)*sz;\
while (last!=base) { \
if (compare(userdata,first,last)>0) first=last; \
last-=sz; } \
if (first!=base) swapper(first,(char*)base);
/* and so is the insertion sort, in the first two cases: */
#define Insertion(swapper) \
last=((char*)base)+nmemb*size; \
for (first=((char*)base)+size;first!=last;first+=size) { \
char *test; \
/* Find the right place for |first|. \
* My apologies for var reuse. */ \
for (test=first-size;compare(userdata,test,first)>0;test-=size) ; \
test+=size; \
if (test!=first) { \
/* Shift everything in [test,first) \
* up by one, and place |first| \
* where |test| is. */ \
memcpy(pivot,first,size); \
memmove(test+size,test,first-test); \
memcpy(test,pivot,size); \
} \
}
#define SWAP_nonaligned(a,b) { \
register char *aa=(a),*bb=(b); \
register size_t sz=size; \
do { register char t=*aa; *aa++=*bb; *bb++=t; } while (--sz); }
#define SWAP_aligned(a,b) { \
register int *aa=(int*)(a),*bb=(int*)(b); \
register size_t sz=size; \
do { register int t=*aa;*aa++=*bb; *bb++=t; } while (sz-=WORD_BYTES); }
#define SWAP_words(a,b) { \
register int t=*((int*)a); *((int*)a)=*((int*)b); *((int*)b)=t; }
/* ---------------------------------------------------------------------- */
static char * pivot_big(char *first, char *mid, char *last, size_t size,
int (SDLCALL *compare)(void *, const void *, const void *), void *userdata) {
size_t d=(((last-first)/size)>>3)*size;
#ifdef DEBUG_QSORT
fprintf(stderr, "pivot_big: first=%p last=%p size=%lu n=%lu\n", first, (unsigned long)last, size, (unsigned long)((last-first+1)/size));
#endif
char *m1,*m2,*m3;
{ char *a=first, *b=first+d, *c=first+2*d;
#ifdef DEBUG_QSORT
fprintf(stderr,"< %d %d %d @ %p %p %p\n",*(int*)a,*(int*)b,*(int*)c, a,b,c);
#endif
m1 = compare(userdata,a,b)<0 ?
(compare(userdata,b,c)<0 ? b : (compare(userdata,a,c)<0 ? c : a))
: (compare(userdata,a,c)<0 ? a : (compare(userdata,b,c)<0 ? c : b));
}
{ char *a=mid-d, *b=mid, *c=mid+d;
#ifdef DEBUG_QSORT
fprintf(stderr,". %d %d %d @ %p %p %p\n",*(int*)a,*(int*)b,*(int*)c, a,b,c);
#endif
m2 = compare(userdata,a,b)<0 ?
(compare(userdata,b,c)<0 ? b : (compare(userdata,a,c)<0 ? c : a))
: (compare(userdata,a,c)<0 ? a : (compare(userdata,b,c)<0 ? c : b));
}
{ char *a=last-2*d, *b=last-d, *c=last;
#ifdef DEBUG_QSORT
fprintf(stderr,"> %d %d %d @ %p %p %p\n",*(int*)a,*(int*)b,*(int*)c, a,b,c);
#endif
m3 = compare(userdata,a,b)<0 ?
(compare(userdata,b,c)<0 ? b : (compare(userdata,a,c)<0 ? c : a))
: (compare(userdata,a,c)<0 ? a : (compare(userdata,b,c)<0 ? c : b));
}
#ifdef DEBUG_QSORT
fprintf(stderr,"-> %d %d %d @ %p %p %p\n",*(int*)m1,*(int*)m2,*(int*)m3, m1,m2,m3);
#endif
return compare(userdata,m1,m2)<0 ?
(compare(userdata,m2,m3)<0 ? m2 : (compare(userdata,m1,m3)<0 ? m3 : m1))
: (compare(userdata,m1,m3)<0 ? m1 : (compare(userdata,m2,m3)<0 ? m3 : m2));
}
/* ---------------------------------------------------------------------- */
static void qsort_r_nonaligned(void *base, size_t nmemb, size_t size,
int (SDLCALL *compare)(void *, const void *, const void *), void *userdata) {
stack_entry stack[STACK_SIZE];
int stacktop=0;
char *first,*last;
char *pivot=malloc(size);
size_t trunc=TRUNC_nonaligned*size;
assert(pivot != NULL);
first=(char*)base; last=first+(nmemb-1)*size;
if ((size_t)(last-first)>=trunc) {
char *ffirst=first, *llast=last;
while (1) {
/* Select pivot */
{ char * mid=first+size*((last-first)/size >> 1);
Pivot(SWAP_nonaligned,size);
memcpy(pivot,mid,size);
}
/* Partition. */
Partition(SWAP_nonaligned,size);
/* Prepare to recurse/iterate. */
Recurse(trunc)
}
}
PreInsertion(SWAP_nonaligned,TRUNC_nonaligned,size);
Insertion(SWAP_nonaligned);
free(pivot);
}
static void qsort_r_aligned(void *base, size_t nmemb, size_t size,
int (SDLCALL *compare)(void *,const void *, const void *), void *userdata) {
stack_entry stack[STACK_SIZE];
int stacktop=0;
char *first,*last;
char *pivot=malloc(size);
size_t trunc=TRUNC_aligned*size;
assert(pivot != NULL);
first=(char*)base; last=first+(nmemb-1)*size;
if ((size_t)(last-first)>=trunc) {
char *ffirst=first,*llast=last;
while (1) {
/* Select pivot */
{ char * mid=first+size*((last-first)/size >> 1);
Pivot(SWAP_aligned,size);
memcpy(pivot,mid,size);
}
/* Partition. */
Partition(SWAP_aligned,size);
/* Prepare to recurse/iterate. */
Recurse(trunc)
}
}
PreInsertion(SWAP_aligned,TRUNC_aligned,size);
Insertion(SWAP_aligned);
free(pivot);
}
static void qsort_r_words(void *base, size_t nmemb,
int (SDLCALL *compare)(void *,const void *, const void *), void *userdata) {
stack_entry stack[STACK_SIZE];
int stacktop=0;
char *first,*last;
char *pivot=malloc(WORD_BYTES);
assert(pivot != NULL);
first=(char*)base; last=first+(nmemb-1)*WORD_BYTES;
if (last-first>=TRUNC_words) {
char *ffirst=first, *llast=last;
while (1) {
#ifdef DEBUG_QSORT
fprintf(stderr,"Doing %d:%d: ",
(first-(char*)base)/WORD_BYTES,
(last-(char*)base)/WORD_BYTES);
#endif
/* Select pivot */
{ char * mid=first+WORD_BYTES*((last-first) / (2*WORD_BYTES));
Pivot(SWAP_words,WORD_BYTES);
*(int*)pivot=*(int*)mid;
#ifdef DEBUG_QSORT
fprintf(stderr,"pivot = %p = #%lu = %d\n", mid, (unsigned long)(((int*)mid)-((int*)base)), *(int*)mid);
#endif
}
/* Partition. */
Partition(SWAP_words,WORD_BYTES);
#ifdef DEBUG_QSORT
fprintf(stderr, "after partitioning first=#%lu last=#%lu\n", (first-(char*)base)/4lu, (last-(char*)base)/4lu);
#endif
/* Prepare to recurse/iterate. */
Recurse(TRUNC_words)
}
}
PreInsertion(SWAP_words,TRUNC_words/WORD_BYTES,WORD_BYTES);
/* Now do insertion sort. */
last=((char*)base)+nmemb*WORD_BYTES;
for (first=((char*)base)+WORD_BYTES;first!=last;first+=WORD_BYTES) {
/* Find the right place for |first|. My apologies for var reuse */
int *pl=(int*)(first-WORD_BYTES),*pr=(int*)first;
*(int*)pivot=*(int*)first;
for (;compare(userdata,pl,pivot)>0;pr=pl,--pl) {
*pr=*pl; }
if (pr!=(int*)first) *pr=*(int*)pivot;
}
free(pivot);
}
/* ---------------------------------------------------------------------- */
void SDL_qsort_r(void *base, size_t nmemb, size_t size,
SDL_CompareCallback_r compare, void *userdata) {
if (nmemb<=1) return;
if (((uintptr_t)base|size)&(WORD_BYTES-1))
qsort_r_nonaligned(base,nmemb,size,compare,userdata);
else if (size!=WORD_BYTES)
qsort_r_aligned(base,nmemb,size,compare,userdata);
else
qsort_r_words(base,nmemb,compare,userdata);
}
static int SDLCALL qsort_non_r_bridge(void *userdata, const void *a, const void *b)
{
int (SDLCALL *compare)(const void *, const void *) = (int (SDLCALL *)(const void *, const void *)) userdata;
return compare(a, b);
}
void SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare)
{
SDL_qsort_r(base, nmemb, size, qsort_non_r_bridge, compare);
}
// Don't use the C runtime for such a simple function, since we want to allow SDLCALL callbacks and userdata.
// SDL's replacement: Taken from the Public Domain C Library (PDCLib):
// Permission is granted to use, modify, and / or redistribute at will.
void *SDL_bsearch_r(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback_r compare, void *userdata)
{
const void *pivot;
size_t corr;
int rc;
while (nmemb) {
/* algorithm needs -1 correction if remaining elements are an even number. */
corr = nmemb % 2;
nmemb /= 2;
pivot = (const char *)base + (nmemb * size);
rc = compare(userdata, key, pivot);
if (rc > 0) {
base = (const char *)pivot + size;
/* applying correction */
nmemb -= (1 - corr);
} else if (rc == 0) {
return (void *)pivot;
}
}
return NULL;
}
void *SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare)
{
// qsort_non_r_bridge just happens to match calling conventions, so reuse it.
return SDL_bsearch_r(key, base, nmemb, size, qsort_non_r_bridge, compare);
}

115
thirdparty/sdl/stdlib/SDL_random.c vendored Normal file
View File

@@ -0,0 +1,115 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
// This file contains portable random functions for SDL
static Uint64 SDL_rand_state;
static bool SDL_rand_initialized = false;
void SDL_srand(Uint64 seed)
{
if (!seed) {
seed = SDL_GetPerformanceCounter();
}
SDL_rand_state = seed;
SDL_rand_initialized = true;
}
Sint32 SDL_rand(Sint32 n)
{
if (!SDL_rand_initialized) {
SDL_srand(0);
}
return SDL_rand_r(&SDL_rand_state, n);
}
float SDL_randf(void)
{
if (!SDL_rand_initialized) {
SDL_srand(0);
}
return SDL_randf_r(&SDL_rand_state);
}
Uint32 SDL_rand_bits(void)
{
if (!SDL_rand_initialized) {
SDL_srand(0);
}
return SDL_rand_bits_r(&SDL_rand_state);
}
Uint32 SDL_rand_bits_r(Uint64 *state)
{
if (!state) {
return 0;
}
// The C and A parameters of this LCG have been chosen based on hundreds
// of core-hours of testing with PractRand and TestU01's Crush.
// Using a 32-bit A improves performance on 32-bit architectures.
// C can be any odd number, but < 256 generates smaller code on ARM32
// These values perform as well as a full 64-bit implementation against
// Crush and PractRand. Plus, their worst-case performance is better
// than common 64-bit constants when tested against PractRand using seeds
// with only a single bit set.
// We tested all 32-bit and 33-bit A with all C < 256 from a v2 of:
// Steele GL, Vigna S. Computationally easy, spectrally good multipliers
// for congruential pseudorandom number generators.
// Softw Pract Exper. 2022;52(2):443-458. doi: 10.1002/spe.3030
// https://arxiv.org/abs/2001.05304v2
*state = *state * 0xff1cd035ul + 0x05;
// Only return top 32 bits because they have a longer period
return (Uint32)(*state >> 32);
}
Sint32 SDL_rand_r(Uint64 *state, Sint32 n)
{
// Algorithm: get 32 bits from SDL_rand_bits() and treat it as a 0.32 bit
// fixed point number. Multiply by the 31.0 bit n to get a 31.32 bit
// result. Shift right by 32 to get the 31 bit integer that we want.
if (n < 0) {
// The algorithm looks like it works for numbers < 0 but it has an
// infinitesimal chance of returning a value out of range.
// Returning -SDL_rand(abs(n)) blows up at INT_MIN instead.
// It's easier to just say no.
return 0;
}
// On 32-bit arch, the compiler will optimize to a single 32-bit multiply
Uint64 val = (Uint64)SDL_rand_bits_r(state) * n;
return (Sint32)(val >> 32);
}
float SDL_randf_r(Uint64 *state)
{
// Note: its using 24 bits because float has 23 bits significand + 1 implicit bit
return (SDL_rand_bits_r(state) >> (32 - 24)) * 0x1p-24f;
}

573
thirdparty/sdl/stdlib/SDL_stdlib.c vendored Normal file
View File

@@ -0,0 +1,573 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
// This file contains portable stdlib functions for SDL
#include "../libm/math_libm.h"
double SDL_atan(double x)
{
#ifdef HAVE_ATAN
return atan(x);
#else
return SDL_uclibc_atan(x);
#endif
}
float SDL_atanf(float x)
{
#ifdef HAVE_ATANF
return atanf(x);
#else
return (float)SDL_atan((double)x);
#endif
}
double SDL_atan2(double y, double x)
{
#ifdef HAVE_ATAN2
return atan2(y, x);
#else
return SDL_uclibc_atan2(y, x);
#endif
}
float SDL_atan2f(float y, float x)
{
#ifdef HAVE_ATAN2F
return atan2f(y, x);
#else
return (float)SDL_atan2((double)y, (double)x);
#endif
}
double SDL_acos(double val)
{
#ifdef HAVE_ACOS
return acos(val);
#else
double result;
if (val == -1.0) {
result = SDL_PI_D;
} else {
result = SDL_atan(SDL_sqrt(1.0 - val * val) / val);
if (result < 0.0) {
result += SDL_PI_D;
}
}
return result;
#endif
}
float SDL_acosf(float val)
{
#ifdef HAVE_ACOSF
return acosf(val);
#else
return (float)SDL_acos((double)val);
#endif
}
double SDL_asin(double val)
{
#ifdef HAVE_ASIN
return asin(val);
#else
double result;
if (val == -1.0) {
result = -(SDL_PI_D / 2.0);
} else {
result = (SDL_PI_D / 2.0) - SDL_acos(val);
}
return result;
#endif
}
float SDL_asinf(float val)
{
#ifdef HAVE_ASINF
return asinf(val);
#else
return (float)SDL_asin((double)val);
#endif
}
double SDL_ceil(double x)
{
#ifdef HAVE_CEIL
return ceil(x);
#else
double integer = SDL_floor(x);
double fraction = x - integer;
if (fraction > 0.0) {
integer += 1.0;
}
return integer;
#endif // HAVE_CEIL
}
float SDL_ceilf(float x)
{
#ifdef HAVE_CEILF
return ceilf(x);
#else
return (float)SDL_ceil((double)x);
#endif
}
double SDL_copysign(double x, double y)
{
#ifdef HAVE_COPYSIGN
return copysign(x, y);
#elif defined(HAVE__COPYSIGN)
return _copysign(x, y);
#elif defined(__WATCOMC__) && defined(__386__)
// this is nasty as hell, but it works..
unsigned int *xi = (unsigned int *)&x,
*yi = (unsigned int *)&y;
xi[1] = (yi[1] & 0x80000000) | (xi[1] & 0x7fffffff);
return x;
#else
return SDL_uclibc_copysign(x, y);
#endif // HAVE_COPYSIGN
}
float SDL_copysignf(float x, float y)
{
#ifdef HAVE_COPYSIGNF
return copysignf(x, y);
#else
return (float)SDL_copysign((double)x, (double)y);
#endif
}
double SDL_cos(double x)
{
#ifdef HAVE_COS
return cos(x);
#else
return SDL_uclibc_cos(x);
#endif
}
float SDL_cosf(float x)
{
#ifdef HAVE_COSF
return cosf(x);
#else
return (float)SDL_cos((double)x);
#endif
}
double SDL_exp(double x)
{
#ifdef HAVE_EXP
return exp(x);
#else
return SDL_uclibc_exp(x);
#endif
}
float SDL_expf(float x)
{
#ifdef HAVE_EXPF
return expf(x);
#else
return (float)SDL_exp((double)x);
#endif
}
double SDL_fabs(double x)
{
#ifdef HAVE_FABS
return fabs(x);
#else
return SDL_uclibc_fabs(x);
#endif
}
float SDL_fabsf(float x)
{
#ifdef HAVE_FABSF
return fabsf(x);
#else
return (float)SDL_fabs((double)x);
#endif
}
double SDL_floor(double x)
{
#ifdef HAVE_FLOOR
return floor(x);
#else
return SDL_uclibc_floor(x);
#endif
}
float SDL_floorf(float x)
{
#ifdef HAVE_FLOORF
return floorf(x);
#else
return (float)SDL_floor((double)x);
#endif
}
double SDL_trunc(double x)
{
#ifdef HAVE_TRUNC
return trunc(x);
#else
if (x >= 0.0f) {
return SDL_floor(x);
} else {
return SDL_ceil(x);
}
#endif
}
float SDL_truncf(float x)
{
#ifdef HAVE_TRUNCF
return truncf(x);
#else
return (float)SDL_trunc((double)x);
#endif
}
double SDL_fmod(double x, double y)
{
#ifdef HAVE_FMOD
return fmod(x, y);
#else
return SDL_uclibc_fmod(x, y);
#endif
}
float SDL_fmodf(float x, float y)
{
#ifdef HAVE_FMODF
return fmodf(x, y);
#else
return (float)SDL_fmod((double)x, (double)y);
#endif
}
int SDL_isinf(double x)
{
#ifdef HAVE_ISINF
return isinf(x);
#else
return SDL_uclibc_isinf(x);
#endif
}
int SDL_isinff(float x)
{
#ifdef HAVE_ISINF_FLOAT_MACRO
return isinf(x);
#elif defined(HAVE_ISINFF)
return isinff(x);
#else
return SDL_uclibc_isinff(x);
#endif
}
int SDL_isnan(double x)
{
#ifdef HAVE_ISNAN
return isnan(x);
#else
return SDL_uclibc_isnan(x);
#endif
}
int SDL_isnanf(float x)
{
#ifdef HAVE_ISNAN_FLOAT_MACRO
return isnan(x);
#elif defined(HAVE_ISNANF)
return isnanf(x);
#else
return SDL_uclibc_isnanf(x);
#endif
}
double SDL_log(double x)
{
#ifdef HAVE_LOG
return log(x);
#else
return SDL_uclibc_log(x);
#endif
}
float SDL_logf(float x)
{
#ifdef HAVE_LOGF
return logf(x);
#else
return (float)SDL_log((double)x);
#endif
}
double SDL_log10(double x)
{
#ifdef HAVE_LOG10
return log10(x);
#else
return SDL_uclibc_log10(x);
#endif
}
float SDL_log10f(float x)
{
#ifdef HAVE_LOG10F
return log10f(x);
#else
return (float)SDL_log10((double)x);
#endif
}
double SDL_modf(double x, double *y)
{
#ifdef HAVE_MODF
return modf(x, y);
#else
return SDL_uclibc_modf(x, y);
#endif
}
float SDL_modff(float x, float *y)
{
#ifdef HAVE_MODFF
return modff(x, y);
#else
double double_result, double_y;
double_result = SDL_modf((double)x, &double_y);
*y = (float)double_y;
return (float)double_result;
#endif
}
double SDL_pow(double x, double y)
{
#ifdef HAVE_POW
return pow(x, y);
#else
return SDL_uclibc_pow(x, y);
#endif
}
float SDL_powf(float x, float y)
{
#ifdef HAVE_POWF
return powf(x, y);
#else
return (float)SDL_pow((double)x, (double)y);
#endif
}
double SDL_round(double arg)
{
#if defined HAVE_ROUND
return round(arg);
#else
if (arg >= 0.0) {
return SDL_floor(arg + 0.5);
} else {
return SDL_ceil(arg - 0.5);
}
#endif
}
float SDL_roundf(float arg)
{
#if defined HAVE_ROUNDF
return roundf(arg);
#else
return (float)SDL_round((double)arg);
#endif
}
long SDL_lround(double arg)
{
#if defined HAVE_LROUND
return lround(arg);
#else
return (long)SDL_round(arg);
#endif
}
long SDL_lroundf(float arg)
{
#if defined HAVE_LROUNDF
return lroundf(arg);
#else
return (long)SDL_round((double)arg);
#endif
}
double SDL_scalbn(double x, int n)
{
#ifdef HAVE_SCALBN
return scalbn(x, n);
#elif defined(HAVE__SCALB)
return _scalb(x, n);
#elif defined(HAVE_LIBC) && defined(HAVE_FLOAT_H) && (FLT_RADIX == 2)
/* from scalbn(3): If FLT_RADIX equals 2 (which is
* usual), then scalbn() is equivalent to ldexp(3). */
return ldexp(x, n);
#else
return SDL_uclibc_scalbn(x, n);
#endif
}
float SDL_scalbnf(float x, int n)
{
#ifdef HAVE_SCALBNF
return scalbnf(x, n);
#else
return (float)SDL_scalbn((double)x, n);
#endif
}
double SDL_sin(double x)
{
#ifdef HAVE_SIN
return sin(x);
#else
return SDL_uclibc_sin(x);
#endif
}
float SDL_sinf(float x)
{
#ifdef HAVE_SINF
return sinf(x);
#else
return (float)SDL_sin((double)x);
#endif
}
double SDL_sqrt(double x)
{
#ifdef HAVE_SQRT
return sqrt(x);
#else
return SDL_uclibc_sqrt(x);
#endif
}
float SDL_sqrtf(float x)
{
#ifdef HAVE_SQRTF
return sqrtf(x);
#else
return (float)SDL_sqrt((double)x);
#endif
}
double SDL_tan(double x)
{
#ifdef HAVE_TAN
return tan(x);
#else
return SDL_uclibc_tan(x);
#endif
}
float SDL_tanf(float x)
{
#ifdef HAVE_TANF
return tanf(x);
#else
return (float)SDL_tan((double)x);
#endif
}
int SDL_abs(int x)
{
#ifdef HAVE_ABS
return abs(x);
#else
return (x < 0) ? -x : x;
#endif
}
int SDL_isalpha(int x) { return (SDL_isupper(x)) || (SDL_islower(x)); }
int SDL_isalnum(int x) { return (SDL_isalpha(x)) || (SDL_isdigit(x)); }
int SDL_isdigit(int x) { return ((x) >= '0') && ((x) <= '9'); }
int SDL_isxdigit(int x) { return (((x) >= 'A') && ((x) <= 'F')) || (((x) >= 'a') && ((x) <= 'f')) || (SDL_isdigit(x)); }
int SDL_ispunct(int x) { return (SDL_isgraph(x)) && (!SDL_isalnum(x)); }
int SDL_isspace(int x) { return ((x) == ' ') || ((x) == '\t') || ((x) == '\r') || ((x) == '\n') || ((x) == '\f') || ((x) == '\v'); }
int SDL_isupper(int x) { return ((x) >= 'A') && ((x) <= 'Z'); }
int SDL_islower(int x) { return ((x) >= 'a') && ((x) <= 'z'); }
int SDL_isprint(int x) { return ((x) >= ' ') && ((x) < '\x7f'); }
int SDL_isgraph(int x) { return (SDL_isprint(x)) && ((x) != ' '); }
int SDL_iscntrl(int x) { return (((x) >= '\0') && ((x) <= '\x1f')) || ((x) == '\x7f'); }
int SDL_toupper(int x) { return ((x) >= 'a') && ((x) <= 'z') ? ('A' + ((x) - 'a')) : (x); }
int SDL_tolower(int x) { return ((x) >= 'A') && ((x) <= 'Z') ? ('a' + ((x) - 'A')) : (x); }
int SDL_isblank(int x) { return ((x) == ' ') || ((x) == '\t'); }
void *SDL_aligned_alloc(size_t alignment, size_t size)
{
size_t padding;
Uint8 *result = NULL;
size_t requested_size = size;
if (alignment < sizeof(void*)) {
alignment = sizeof(void*);
}
padding = (alignment - (size % alignment));
if (SDL_size_add_check_overflow(size, alignment, &size) &&
SDL_size_add_check_overflow(size, sizeof(void *), &size) &&
SDL_size_add_check_overflow(size, padding, &size)) {
void *original = SDL_malloc(size);
if (original) {
// Make sure we have enough space to store the original pointer
result = (Uint8 *)original + sizeof(original);
// Align the pointer we're going to return
result += alignment - (((size_t)result) % alignment);
// Store the original pointer right before the returned value
SDL_memcpy(result - sizeof(original), &original, sizeof(original));
// Initialize the padding to zero
if (padding > 0) {
SDL_memset(result + requested_size, 0, padding);
}
}
}
return result;
}
void SDL_aligned_free(void *mem)
{
if (mem) {
void *original;
SDL_memcpy(&original, ((Uint8 *)mem - sizeof(original)), sizeof(original));
SDL_free(original);
}
}

2511
thirdparty/sdl/stdlib/SDL_string.c vendored Normal file

File diff suppressed because it is too large Load Diff

95
thirdparty/sdl/stdlib/SDL_strtokr.c vendored Normal file
View File

@@ -0,0 +1,95 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
char *SDL_strtok_r(char *s1, const char *s2, char **ptr)
{
#ifdef HAVE_STRTOK_R
return strtok_r(s1, s2, ptr);
#else /* SDL implementation */
/*
* Adapted from _PDCLIB_strtok() of PDClib library at
* https://github.com/DevSolar/pdclib.git
*
* The code was under CC0 license:
* https://creativecommons.org/publicdomain/zero/1.0/legalcode :
*
* No Copyright
*
* The person who associated a work with this deed has dedicated the
* work to the public domain by waiving all of his or her rights to
* the work worldwide under copyright law, including all related and
* neighboring rights, to the extent allowed by law.
*
* You can copy, modify, distribute and perform the work, even for
* commercial purposes, all without asking permission. See Other
* Information below.
*/
const char *p = s2;
if (!s2 || !ptr || (!s1 && !*ptr)) return NULL;
if (s1 != NULL) { /* new string */
*ptr = s1;
} else { /* old string continued */
if (*ptr == NULL) {
/* No old string, no new string, nothing to do */
return NULL;
}
s1 = *ptr;
}
/* skip leading s2 characters */
while (*p && *s1) {
if (*s1 == *p) {
/* found separator; skip and start over */
++s1;
p = s2;
continue;
}
++p;
}
if (! *s1) { /* no more to parse */
*ptr = s1;
return NULL;
}
/* skipping non-s2 characters */
*ptr = s1;
while (**ptr) {
p = s2;
while (*p) {
if (**ptr == *p++) {
/* found separator; overwrite with '\0', position *ptr, return */
*((*ptr)++) = '\0';
return s1;
}
}
++(*ptr);
}
/* parsed to end of string */
return s1;
#endif
}

32
thirdparty/sdl/stdlib/SDL_sysstdlib.h vendored Normal file
View File

@@ -0,0 +1,32 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SDL_sysstdlib_h_
#define SDL_sysstdlib_h_
// most things you might need internally in here are public APIs, this is
// just a few special pieces right now.
// this expects `from` to be a Unicode codepoint, and `to` to point to AT LEAST THREE Uint32s.
int SDL_CaseFoldUnicode(Uint32 from, Uint32 *to);
#endif

30
thirdparty/sdl/stdlib/SDL_vacopy.h vendored Normal file
View File

@@ -0,0 +1,30 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
// Do our best to make sure va_copy is working
#if defined(_MSC_VER) && _MSC_VER <= 1800
// Visual Studio 2013 tries to link with _vacopy in the C runtime. Newer versions do an inline assignment
#undef va_copy
#define va_copy(dst, src) dst = src
#elif defined(__GNUC__) && (__GNUC__ < 3)
#define va_copy(dst, src) __va_copy(dst, src)
#endif