Merge commit 'dec0d4ec4153bf9fc2b78ae6c2df45b6ea8dde7a' as 'external/sdl/SDL'
This commit is contained in:
52
external/sdl/SDL/src/stdlib/SDL_crc16.c
vendored
Normal file
52
external/sdl/SDL/src/stdlib/SDL_crc16.c
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2023 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
external/sdl/SDL/src/stdlib/SDL_crc32.c
vendored
Normal file
50
external/sdl/SDL/src/stdlib/SDL_crc32.c
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2023 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;
|
||||
}
|
227
external/sdl/SDL/src/stdlib/SDL_getenv.c
vendored
Normal file
227
external/sdl/SDL/src/stdlib/SDL_getenv.c
vendored
Normal file
@ -0,0 +1,227 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2023 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"
|
||||
|
||||
#if defined(__WIN32__) || defined(__WINGDK__)
|
||||
#include "../core/windows/SDL_windows.h"
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include "../core/android/SDL_android.h"
|
||||
#endif
|
||||
|
||||
#if (defined(__WIN32__) || defined(__WINGDK__)) && (!defined(HAVE_SETENV) || !defined(HAVE_GETENV))
|
||||
/* Note this isn't thread-safe! */
|
||||
static char *SDL_envmem = NULL; /* Ugh, memory leak */
|
||||
static size_t SDL_envmemlen = 0;
|
||||
#endif
|
||||
|
||||
/* 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_SETENV
|
||||
int SDL_setenv(const char *name, const char *value, int overwrite)
|
||||
{
|
||||
/* Input validation */
|
||||
if (name == NULL || *name == '\0' || SDL_strchr(name, '=') != NULL || value == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return setenv(name, value, overwrite);
|
||||
}
|
||||
#elif defined(__WIN32__) || defined(__WINGDK__)
|
||||
int SDL_setenv(const char *name, const char *value, int overwrite)
|
||||
{
|
||||
/* Input validation */
|
||||
if (name == NULL || *name == '\0' || SDL_strchr(name, '=') != NULL || value == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!overwrite) {
|
||||
if (GetEnvironmentVariableA(name, NULL, 0) > 0) {
|
||||
return 0; /* asked not to overwrite existing value. */
|
||||
}
|
||||
}
|
||||
if (!SetEnvironmentVariableA(name, *value ? value : NULL)) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/* We have a real environment table, but no real setenv? Fake it w/ putenv. */
|
||||
#elif (defined(HAVE_GETENV) && defined(HAVE_PUTENV) && !defined(HAVE_SETENV))
|
||||
int SDL_setenv(const char *name, const char *value, int overwrite)
|
||||
{
|
||||
size_t len;
|
||||
char *new_variable;
|
||||
|
||||
/* Input validation */
|
||||
if (name == NULL || *name == '\0' || SDL_strchr(name, '=') != NULL || value == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (getenv(name) != NULL) {
|
||||
if (overwrite) {
|
||||
unsetenv(name);
|
||||
} else {
|
||||
return 0; /* leave the existing one there. */
|
||||
}
|
||||
}
|
||||
|
||||
/* This leaks. Sorry. Get a better OS so we don't have to do this. */
|
||||
len = SDL_strlen(name) + SDL_strlen(value) + 2;
|
||||
new_variable = (char *)SDL_malloc(len);
|
||||
if (new_variable == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_snprintf(new_variable, len, "%s=%s", name, value);
|
||||
return putenv(new_variable);
|
||||
}
|
||||
#else /* roll our own */
|
||||
static char **SDL_env = (char **)0;
|
||||
int SDL_setenv(const char *name, const char *value, int overwrite)
|
||||
{
|
||||
int added;
|
||||
size_t len, i;
|
||||
char **new_env;
|
||||
char *new_variable;
|
||||
|
||||
/* Input validation */
|
||||
if (name == NULL || *name == '\0' || SDL_strchr(name, '=') != NULL || value == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* See if it already exists */
|
||||
if (!overwrite && SDL_getenv(name)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Allocate memory for the variable */
|
||||
len = SDL_strlen(name) + SDL_strlen(value) + 2;
|
||||
new_variable = (char *)SDL_malloc(len);
|
||||
if (new_variable == NULL) {
|
||||
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 (SDL_env) {
|
||||
/* Check to see if it's already there... */
|
||||
len = (value - name);
|
||||
for (; SDL_env[i]; ++i) {
|
||||
if (SDL_strncmp(SDL_env[i], name, len) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* If we found it, just replace the entry */
|
||||
if (SDL_env[i]) {
|
||||
SDL_free(SDL_env[i]);
|
||||
SDL_env[i] = new_variable;
|
||||
added = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Didn't find it in the environment, expand and add */
|
||||
if (!added) {
|
||||
new_env = SDL_realloc(SDL_env, (i + 2) * sizeof(char *));
|
||||
if (new_env) {
|
||||
SDL_env = new_env;
|
||||
SDL_env[i++] = new_variable;
|
||||
SDL_env[i++] = (char *)0;
|
||||
added = 1;
|
||||
} else {
|
||||
SDL_free(new_variable);
|
||||
}
|
||||
}
|
||||
return added ? 0 : -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Retrieve a variable named "name" from the environment */
|
||||
#ifdef HAVE_GETENV
|
||||
char *SDL_getenv(const char *name)
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
/* Make sure variables from the application manifest are available */
|
||||
Android_JNI_GetManifestEnvironmentVariables();
|
||||
#endif
|
||||
|
||||
/* Input validation */
|
||||
if (name == NULL || *name == '\0') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return getenv(name);
|
||||
}
|
||||
#elif defined(__WIN32__) || defined(__WINGDK__)
|
||||
char *SDL_getenv(const char *name)
|
||||
{
|
||||
size_t bufferlen;
|
||||
|
||||
/* Input validation */
|
||||
if (name == NULL || *name == '\0') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bufferlen =
|
||||
GetEnvironmentVariableA(name, SDL_envmem, (DWORD)SDL_envmemlen);
|
||||
if (bufferlen == 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (bufferlen > SDL_envmemlen) {
|
||||
char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
|
||||
if (newmem == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
SDL_envmem = newmem;
|
||||
SDL_envmemlen = bufferlen;
|
||||
GetEnvironmentVariableA(name, SDL_envmem, (DWORD)SDL_envmemlen);
|
||||
}
|
||||
return SDL_envmem;
|
||||
}
|
||||
#else
|
||||
char *SDL_getenv(const char *name)
|
||||
{
|
||||
size_t len, i;
|
||||
char *value;
|
||||
|
||||
/* Input validation */
|
||||
if (name == NULL || *name == '\0') {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
value = (char *)0;
|
||||
if (SDL_env) {
|
||||
len = SDL_strlen(name);
|
||||
for (i = 0; SDL_env[i] && value == NULL; ++i) {
|
||||
if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
|
||||
(SDL_env[i][len] == '=')) {
|
||||
value = &SDL_env[i][len + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
#endif
|
853
external/sdl/SDL/src/stdlib/SDL_iconv.c
vendored
Normal file
853
external/sdl/SDL/src/stdlib/SDL_iconv.c
vendored
Normal file
@ -0,0 +1,853 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2023 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)
|
||||
#ifdef __FreeBSD__
|
||||
/* 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
/* 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(__WIN32__) || defined(__OS2__) || defined(__GDK__)
|
||||
{ "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 == NULL) {
|
||||
lang = SDL_getenv("LC_CTYPE");
|
||||
}
|
||||
if (lang == NULL) {
|
||||
lang = SDL_getenv("LC_MESSAGES");
|
||||
}
|
||||
if (lang == NULL) {
|
||||
lang = SDL_getenv("LANG");
|
||||
}
|
||||
if (lang == NULL || !*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 != NULL) {
|
||||
lang = ptr + 1;
|
||||
}
|
||||
|
||||
SDL_strlcpy(buffer, lang, bufsize);
|
||||
ptr = SDL_strchr(buffer, '@');
|
||||
if (ptr != NULL) {
|
||||
*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 == NULL || !*fromcode) {
|
||||
fromcode = getlocale(fromcode_buffer, sizeof(fromcode_buffer));
|
||||
}
|
||||
if (tocode == NULL || !*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 (inbuf == NULL || !*inbuf) {
|
||||
/* Reset the context */
|
||||
return 0;
|
||||
}
|
||||
if (outbuf == NULL || !*outbuf || outbytesleft == NULL || !*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;
|
||||
SDL_bool overlong = SDL_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 = SDL_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 = SDL_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 = SDL_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) {
|
||||
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;
|
||||
|
||||
cd = SDL_iconv_open(tocode, fromcode);
|
||||
if (cd == (SDL_iconv_t)-1) {
|
||||
/* See if we can recover here (fixes iconv on Solaris 11) */
|
||||
if (tocode == NULL || !*tocode) {
|
||||
tocode = "UTF-8";
|
||||
}
|
||||
if (fromcode == NULL || !*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 == NULL) {
|
||||
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:
|
||||
{
|
||||
char *oldstring = string;
|
||||
stringsize *= 2;
|
||||
string = (char *)SDL_realloc(string, stringsize + sizeof(Uint32));
|
||||
if (string == NULL) {
|
||||
SDL_free(oldstring);
|
||||
SDL_iconv_close(cd);
|
||||
return NULL;
|
||||
}
|
||||
outbuf = string + (outbuf - oldstring);
|
||||
outbytesleft = stringsize - (outbuf - string);
|
||||
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;
|
||||
}
|
5333
external/sdl/SDL/src/stdlib/SDL_malloc.c
vendored
Normal file
5333
external/sdl/SDL/src/stdlib/SDL_malloc.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
716
external/sdl/SDL/src/stdlib/SDL_mslibc.c
vendored
Normal file
716
external/sdl/SDL/src/stdlib/SDL_mslibc.c
vendored
Normal file
@ -0,0 +1,716 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2023 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
|
||||
|
||||
/* The optimizer on Visual Studio 2005 and later generates memcpy() and memset() calls.
|
||||
Always provide it for the SDL3 DLL, but skip it when building static lib w/ static runtime. */
|
||||
#if (_MSC_VER >= 1400) && (!defined(_MT) || defined(DLL_EXPORT))
|
||||
/* NOLINTNEXTLINE(readability-redundant-declaration) */
|
||||
extern void *memcpy(void *dst, const void *src, size_t len);
|
||||
#ifndef __INTEL_LLVM_COMPILER
|
||||
#pragma intrinsic(memcpy)
|
||||
#endif
|
||||
|
||||
#ifndef __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);
|
||||
}
|
||||
|
||||
/* NOLINTNEXTLINE(readability-redundant-declaration) */
|
||||
extern void *memset(void *dst, int c, size_t len);
|
||||
#ifndef __INTEL_LLVM_COMPILER
|
||||
#pragma intrinsic(memset)
|
||||
#endif
|
||||
|
||||
#ifndef __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 /* (_MSC_VER >= 1400) && (!defined(_MT) || defined(DLL_EXPORT)) */
|
||||
|
||||
#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* */
|
||||
}
|
||||
|
||||
#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 */
|
560
external/sdl/SDL/src/stdlib/SDL_qsort.c
vendored
Normal file
560
external/sdl/SDL/src/stdlib/SDL_qsort.c
vendored
Normal file
@ -0,0 +1,560 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2023 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 HAVE_QSORT
|
||||
void SDL_qsort(void *base, size_t nmemb, size_t size, int (*compare) (const void *, const void *))
|
||||
{
|
||||
qsort(base, nmemb, size, compare);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#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
|
||||
#ifdef qsortG
|
||||
#undef qsortG
|
||||
#endif
|
||||
#define qsortG SDL_qsort
|
||||
|
||||
/*
|
||||
This code came from Gareth McCaughan, under the zlib license.
|
||||
Specifically this: https://www.mccaughan.org.uk/software/qsort.c-1.15
|
||||
|
||||
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!
|
||||
|
||||
--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-2016 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.
|
||||
*/
|
||||
|
||||
/* BEGIN SDL CHANGE ... commented this out with an #if 0 block. --ryan. */
|
||||
#if 0
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#undef DEBUG_QSORT
|
||||
|
||||
static char _ID[]="<qsort.c gjm 1.15 2016-03-10>";
|
||||
#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|.
|
||||
*/
|
||||
#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 shorter and get to work on the
|
||||
* longer. This guarantees that every time we push
|
||||
* an item onto the stack its size is <= 1/2 of that
|
||||
* of its parent; 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) { pushLeft; doRight } \
|
||||
else if (r>=Trunc) { pushRight; doLeft }\
|
||||
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);\
|
||||
else { \
|
||||
if (compare(first,mid)<0) { \
|
||||
if (compare(mid,last)>0) { \
|
||||
swapper(mid,last); \
|
||||
if (compare(first,mid)>0) swapper(first,mid);\
|
||||
} \
|
||||
} \
|
||||
else { \
|
||||
if (compare(mid,last)>0) swapper(first,last)\
|
||||
else { \
|
||||
swapper(first,mid); \
|
||||
if (compare(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(first,pivot)<0) first+=sz; \
|
||||
while (compare(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(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(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 compare(const void *, const void *)) {
|
||||
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(a,b)<0 ?
|
||||
(compare(b,c)<0 ? b : (compare(a,c)<0 ? c : a))
|
||||
: (compare(a,c)<0 ? a : (compare(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(a,b)<0 ?
|
||||
(compare(b,c)<0 ? b : (compare(a,c)<0 ? c : a))
|
||||
: (compare(a,c)<0 ? a : (compare(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(a,b)<0 ?
|
||||
(compare(b,c)<0 ? b : (compare(a,c)<0 ? c : a))
|
||||
: (compare(a,c)<0 ? a : (compare(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(m1,m2)<0 ?
|
||||
(compare(m2,m3)<0 ? m2 : (compare(m1,m3)<0 ? m3 : m1))
|
||||
: (compare(m1,m3)<0 ? m1 : (compare(m2,m3)<0 ? m3 : m2));
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
static void qsort_nonaligned(void *base, size_t nmemb, size_t size,
|
||||
int (*compare)(const void *, const void *)) {
|
||||
|
||||
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_aligned(void *base, size_t nmemb, size_t size,
|
||||
int (*compare)(const void *, const void *)) {
|
||||
|
||||
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_words(void *base, size_t nmemb,
|
||||
int (*compare)(const void *, const void *)) {
|
||||
|
||||
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(pl,pivot)>0;pr=pl,--pl) {
|
||||
*pr=*pl; }
|
||||
if (pr!=(int*)first) *pr=*(int*)pivot;
|
||||
}
|
||||
free(pivot);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
extern void qsortG(void *base, size_t nmemb, size_t size,
|
||||
int (*compare)(const void *, const void *)) {
|
||||
|
||||
if (nmemb<=1) return;
|
||||
if (((size_t)base|size)&(WORD_BYTES-1))
|
||||
qsort_nonaligned(base,nmemb,size,compare);
|
||||
else if (size!=WORD_BYTES)
|
||||
qsort_aligned(base,nmemb,size,compare);
|
||||
else
|
||||
qsort_words(base,nmemb,compare);
|
||||
}
|
||||
|
||||
#endif /* HAVE_QSORT */
|
||||
|
||||
void *SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compare)(const void *, const void *))
|
||||
{
|
||||
#ifdef HAVE_BSEARCH
|
||||
return bsearch(key, base, nmemb, size, compare);
|
||||
#else
|
||||
/* SDL's replacement: Taken from the Public Domain C Library (PDCLib):
|
||||
Permission is granted to use, modify, and / or redistribute at will.
|
||||
*/
|
||||
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(key, pivot);
|
||||
|
||||
if (rc > 0) {
|
||||
base = (const char *)pivot + size;
|
||||
/* applying correction */
|
||||
nmemb -= (1 - corr);
|
||||
} else if (rc == 0) {
|
||||
return (void *)pivot;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
#endif /* HAVE_BSEARCH */
|
||||
}
|
701
external/sdl/SDL/src/stdlib/SDL_stdlib.c
vendored
Normal file
701
external/sdl/SDL/src/stdlib/SDL_stdlib.c
vendored
Normal file
@ -0,0 +1,701 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2023 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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
#ifdef HAVE_CTYPE_H
|
||||
int SDL_isalpha(int x)
|
||||
{
|
||||
return isalpha(x);
|
||||
}
|
||||
int SDL_isalnum(int x) { return isalnum(x); }
|
||||
int SDL_isdigit(int x) { return isdigit(x); }
|
||||
int SDL_isxdigit(int x) { return isxdigit(x); }
|
||||
int SDL_ispunct(int x) { return ispunct(x); }
|
||||
int SDL_isspace(int x) { return isspace(x); }
|
||||
int SDL_isupper(int x) { return isupper(x); }
|
||||
int SDL_islower(int x) { return islower(x); }
|
||||
int SDL_isprint(int x) { return isprint(x); }
|
||||
int SDL_isgraph(int x) { return isgraph(x); }
|
||||
int SDL_iscntrl(int x) { return iscntrl(x); }
|
||||
int SDL_toupper(int x) { return toupper(x); }
|
||||
int SDL_tolower(int x) { return tolower(x); }
|
||||
#else
|
||||
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); }
|
||||
#endif
|
||||
|
||||
/* This file contains a portable memcpy manipulation function for SDL */
|
||||
|
||||
void *SDL_memcpy(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
|
||||
{
|
||||
#ifdef __GNUC__
|
||||
/* 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++;
|
||||
case 2:
|
||||
*dstp1++ = *srcp1++;
|
||||
case 1:
|
||||
*dstp1++ = *srcp1++;
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
#endif /* __GNUC__ */
|
||||
}
|
||||
|
||||
void *SDL_memset(SDL_OUT_BYTECAP(len) void *dst, int c, size_t len)
|
||||
{
|
||||
#ifdef 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;
|
||||
case 2:
|
||||
*dstp1++ = value1;
|
||||
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;
|
||||
}
|
||||
|
||||
#if defined(HAVE_CTYPE_H) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
||||
int SDL_isblank(int x)
|
||||
{
|
||||
return isblank(x);
|
||||
}
|
||||
#else
|
||||
int SDL_isblank(int x)
|
||||
{
|
||||
return ((x) == ' ') || ((x) == '\t');
|
||||
}
|
||||
#endif
|
||||
|
||||
void *SDL_aligned_alloc(size_t alignment, size_t size)
|
||||
{
|
||||
size_t padding;
|
||||
Uint8 *retval = NULL;
|
||||
|
||||
if (alignment < sizeof(void*)) {
|
||||
alignment = sizeof(void*);
|
||||
}
|
||||
padding = (alignment - (size % alignment));
|
||||
|
||||
if (SDL_size_add_overflow(size, alignment, &size) == 0 &&
|
||||
SDL_size_add_overflow(size, sizeof(void *), &size) == 0 &&
|
||||
SDL_size_add_overflow(size, padding, &size) == 0) {
|
||||
void *original = SDL_malloc(size);
|
||||
if (original) {
|
||||
/* Make sure we have enough space to store the original pointer */
|
||||
retval = (Uint8 *)original + sizeof(original);
|
||||
|
||||
/* Align the pointer we're going to return */
|
||||
retval += alignment - (((size_t)retval) % alignment);
|
||||
|
||||
/* Store the original pointer right before the returned value */
|
||||
SDL_memcpy(retval - sizeof(original), &original, sizeof(original));
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
void SDL_aligned_free(void *mem)
|
||||
{
|
||||
if (mem) {
|
||||
void *original;
|
||||
SDL_memcpy(&original, ((Uint8 *)mem - sizeof(original)), sizeof(original));
|
||||
SDL_free(original);
|
||||
}
|
||||
}
|
2150
external/sdl/SDL/src/stdlib/SDL_string.c
vendored
Normal file
2150
external/sdl/SDL/src/stdlib/SDL_string.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
95
external/sdl/SDL/src/stdlib/SDL_strtokr.c
vendored
Normal file
95
external/sdl/SDL/src/stdlib/SDL_strtokr.c
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2023 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
|
||||
}
|
34
external/sdl/SDL/src/stdlib/SDL_vacopy.h
vendored
Normal file
34
external/sdl/SDL/src/stdlib/SDL_vacopy.h
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2023 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 */
|
||||
#ifdef __NGAGE__
|
||||
#undef va_copy
|
||||
#define va_copy(dst, src) dst = src
|
||||
|
||||
#elif 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
|
Reference in New Issue
Block a user