update sdl to 2.32.6

This commit is contained in:
AzaezelX 2025-05-24 13:39:03 -05:00
parent e557f5962b
commit ddc1f8c1e2
1339 changed files with 53966 additions and 19207 deletions

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -47,7 +47,7 @@ static size_t SDL_envmemlen = 0;
int SDL_setenv(const char *name, const char *value, int overwrite)
{
/* Input validation */
if (name == NULL || *name == '\0' || SDL_strchr(name, '=') != NULL || value == NULL) {
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
@ -57,7 +57,7 @@ int SDL_setenv(const char *name, const char *value, int overwrite)
int SDL_setenv(const char *name, const char *value, int overwrite)
{
/* Input validation */
if (name == NULL || *name == '\0' || SDL_strchr(name, '=') != NULL || value == NULL) {
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
@ -79,7 +79,7 @@ int SDL_setenv(const char *name, const char *value, int overwrite)
char *new_variable;
/* Input validation */
if (name == NULL || *name == '\0' || SDL_strchr(name, '=') != NULL || value == NULL) {
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
@ -94,7 +94,7 @@ int SDL_setenv(const char *name, const char *value, int overwrite)
/* 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) {
if (!new_variable) {
return -1;
}
@ -111,7 +111,7 @@ int SDL_setenv(const char *name, const char *value, int overwrite)
char *new_variable;
/* Input validation */
if (name == NULL || *name == '\0' || SDL_strchr(name, '=') != NULL || value == NULL) {
if (!name || *name == '\0' || SDL_strchr(name, '=') != NULL || !value) {
return -1;
}
@ -123,7 +123,7 @@ int SDL_setenv(const char *name, const char *value, int overwrite)
/* Allocate memory for the variable */
len = SDL_strlen(name) + SDL_strlen(value) + 2;
new_variable = (char *)SDL_malloc(len);
if (new_variable == NULL) {
if (!new_variable) {
return -1;
}
@ -176,7 +176,7 @@ char *SDL_getenv(const char *name)
#endif
/* Input validation */
if (name == NULL || *name == '\0') {
if (!name || *name == '\0') {
return NULL;
}
@ -188,7 +188,7 @@ char *SDL_getenv(const char *name)
size_t bufferlen;
/* Input validation */
if (name == NULL || *name == '\0') {
if (!name || *name == '\0') {
return NULL;
}
@ -199,7 +199,7 @@ char *SDL_getenv(const char *name)
}
if (bufferlen > SDL_envmemlen) {
char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
if (newmem == NULL) {
if (!newmem) {
return NULL;
}
SDL_envmem = newmem;
@ -215,14 +215,14 @@ char *SDL_getenv(const char *name)
char *value;
/* Input validation */
if (name == NULL || *name == '\0') {
if (!name || *name == '\0') {
return NULL;
}
value = (char *)0;
if (SDL_env) {
len = SDL_strlen(name);
for (i = 0; SDL_env[i] && value == NULL; ++i) {
for (i = 0; SDL_env[i] && !value; ++i) {
if ((SDL_strncmp(SDL_env[i], name, len) == 0) &&
(SDL_env[i][len] == '=')) {
value = &SDL_env[i][len + 1];

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -31,7 +31,7 @@
#include "SDL_endian.h"
#if defined(HAVE_ICONV) && defined(HAVE_ICONV_H)
#ifdef __FreeBSD__
#ifndef SDL_USE_LIBICONV
/* Define LIBICONV_PLUG to use iconv from the base instead of ports and avoid linker errors. */
#define LIBICONV_PLUG 1
#endif
@ -166,28 +166,28 @@ static const char *getlocale(char *buffer, size_t bufsize)
char *ptr;
lang = SDL_getenv("LC_ALL");
if (lang == NULL) {
if (!lang) {
lang = SDL_getenv("LC_CTYPE");
}
if (lang == NULL) {
if (!lang) {
lang = SDL_getenv("LC_MESSAGES");
}
if (lang == NULL) {
if (!lang) {
lang = SDL_getenv("LANG");
}
if (lang == NULL || !*lang || SDL_strcmp(lang, "C") == 0) {
if (!lang || !*lang || SDL_strcmp(lang, "C") == 0) {
lang = "ASCII";
}
/* We need to trim down strings like "en_US.UTF-8@blah" to "UTF-8" */
ptr = SDL_strchr(lang, '.');
if (ptr != NULL) {
if (ptr) {
lang = ptr + 1;
}
SDL_strlcpy(buffer, lang, bufsize);
ptr = SDL_strchr(buffer, '@');
if (ptr != NULL) {
if (ptr) {
*ptr = '\0'; /* chop end of string. */
}
@ -202,10 +202,10 @@ SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
char fromcode_buffer[64];
char tocode_buffer[64];
if (fromcode == NULL || !*fromcode) {
if (!fromcode || !*fromcode) {
fromcode = getlocale(fromcode_buffer, sizeof(fromcode_buffer));
}
if (tocode == NULL || !*tocode) {
if (!tocode || !*tocode) {
tocode = getlocale(tocode_buffer, sizeof(tocode_buffer));
}
for (i = 0; i < SDL_arraysize(encodings); ++i) {
@ -245,11 +245,11 @@ SDL_iconv(SDL_iconv_t cd,
Uint32 ch = 0;
size_t total;
if (inbuf == NULL || !*inbuf) {
if (!inbuf || !*inbuf) {
/* Reset the context */
return 0;
}
if (outbuf == NULL || !*outbuf || outbytesleft == NULL || !*outbytesleft) {
if (!outbuf || !*outbuf || !outbytesleft || !*outbytesleft) {
return SDL_ICONV_E2BIG;
}
src = *inbuf;
@ -795,10 +795,10 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
size_t outbytesleft;
size_t retCode = 0;
if (tocode == NULL || !*tocode) {
if (!tocode || !*tocode) {
tocode = "UTF-8";
}
if (fromcode == NULL || !*fromcode) {
if (!fromcode || !*fromcode) {
fromcode = "UTF-8";
}
cd = SDL_iconv_open(tocode, fromcode);
@ -806,15 +806,15 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
return NULL;
}
stringsize = inbytesleft > 4 ? inbytesleft : 4;
string = (char *)SDL_malloc(stringsize + 1);
if (string == NULL) {
stringsize = inbytesleft;
string = (char *)SDL_malloc(stringsize + sizeof(Uint32));
if (!string) {
SDL_iconv_close(cd);
return NULL;
}
outbuf = string;
outbytesleft = stringsize;
SDL_memset(outbuf, 0, 4);
SDL_memset(outbuf, 0, sizeof(Uint32));
while (inbytesleft > 0) {
const size_t oldinbytesleft = inbytesleft;
@ -822,17 +822,18 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
switch (retCode) {
case SDL_ICONV_E2BIG:
{
const ptrdiff_t diff = (ptrdiff_t) (outbuf - string);
char *oldstring = string;
stringsize *= 2;
string = (char *)SDL_realloc(string, stringsize + 1);
if (string == NULL) {
string = (char *)SDL_realloc(string, stringsize + sizeof(Uint32));
if (!string) {
SDL_free(oldstring);
SDL_iconv_close(cd);
return NULL;
}
outbuf = string + (outbuf - oldstring);
outbytesleft = stringsize - (outbuf - string);
SDL_memset(outbuf, 0, 4);
outbuf = string + diff;
outbytesleft = stringsize - diff;
SDL_memset(outbuf, 0, sizeof(Uint32));
continue;
}
case SDL_ICONV_EILSEQ:
@ -851,7 +852,7 @@ char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inb
break;
}
}
*outbuf = '\0';
SDL_memset(outbuf, 0, sizeof(Uint32));
SDL_iconv_close(cd);
return string;

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -405,9 +405,9 @@ MALLINFO_FIELD_TYPE default: size_t
size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set
REALLOC_ZERO_BYTES_FREES default: not defined
This should be set if a call to realloc with zero bytes should
be the same as a call to free. Some people think it should. Otherwise,
since this malloc returns a unique pointer for malloc(0), so does
This should be set if a call to realloc with zero bytes should
be the same as a call to free. Some people think it should. Otherwise,
since this malloc returns a unique pointer for malloc(0), so does
realloc(p, 0).
LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H
@ -2263,7 +2263,7 @@ static void reset_on_error(mstate m);
/* -------------------------- Debugging setup ---------------------------- */
#if ! DEBUG
#ifndef DEBUG
#define check_free_chunk(M,P)
#define check_inuse_chunk(M,P)
@ -2627,7 +2627,7 @@ static int change_mparam(int param_number, int value) {
}
}
#if DEBUG
#ifdef DEBUG
/* ------------------------- Debugging Support --------------------------- */
/* Check properties of any chunk, whether free, inuse, mmapped etc */
@ -3583,7 +3583,7 @@ static void* sys_alloc(mstate m, size_t nb) {
m->seg.sflags = mmap_flag;
m->magic = mparams.magic;
init_bins(m);
if (is_global(m))
if (is_global(m))
init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE);
else {
/* Offset top by embedded malloc_state */
@ -3734,7 +3734,7 @@ static int sys_trim(mstate m, size_t pad) {
}
/* Unmap any unused mmapped segments */
if (HAVE_MMAP)
if (HAVE_MMAP)
released += release_unused_segments(m);
/* On failure, disable autotrim to avoid repeated failed future calls */
@ -3942,7 +3942,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) {
while (a < alignment) a <<= 1;
alignment = a;
}
if (bytes >= MAX_REQUEST - alignment) {
if (m != 0) { /* Test isn't needed but avoids compiler warning */
MALLOC_FAILURE_ACTION;
@ -4125,7 +4125,7 @@ static void** ialloc(mstate m,
}
}
#if DEBUG
#ifdef DEBUG
if (marray != chunks) {
/* final element must have exactly exhausted chunk */
if (element_size != 0) {
@ -4472,7 +4472,7 @@ struct mallinfo dlmallinfo(void) {
}
#endif /* NO_MALLINFO */
void dlmalloc_stats() {
void dlmalloc_stats(void) {
internal_malloc_stats(gm);
}
@ -5187,7 +5187,7 @@ History:
Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu)
* Based loosely on libg++-1.2X malloc. (It retains some of the overall
structure of old version, but most details differ.)
*/
#endif /* !HAVE_MALLOC */

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -28,8 +28,11 @@
#include "SDL_stdinc.h"
#if defined(HAVE_QSORT)
void SDL_qsort(void *base, size_t nmemb, size_t size, int (*compare) (const void *, const void *))
void SDL_qsort(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare)
{
if (!base) {
return;
}
qsort(base, nmemb, size, compare);
}
@ -62,7 +65,7 @@ void SDL_qsort(void *base, size_t nmemb, size_t size, int (*compare) (const void
/*
This code came from Gareth McCaughan, under the zlib license.
Specifically this: https://www.mccaughan.org.uk/software/qsort.c-1.15
Specifically this: https://www.mccaughan.org.uk/software/qsort.c-1.16
Everything below this comment until the HAVE_QSORT #endif was from Gareth
(any minor changes will be noted inline).
@ -109,7 +112,7 @@ benefit!
* Gareth McCaughan
*/
/* Copyright (c) 1998-2016 Gareth McCaughan
/* Copyright (c) 1998-2021 Gareth McCaughan
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
@ -145,17 +148,23 @@ benefit!
* (pre-insertion-sort messed up).
* Disable DEBUG_QSORT by default.
* Tweak comments very slightly.
* 2021-02-20 v1.16 Fix bug kindly reported by Ray Gardner
* (error in recursion leading to possible
* stack overflow).
* When checking alignment, avoid casting
* pointer to possibly-smaller integer.
*/
/* BEGIN SDL CHANGE ... commented this out with an #if 0 block. --ryan. */
#if 0
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#undef DEBUG_QSORT
static char _ID[]="<qsort.c gjm 1.15 2016-03-10>";
static char _ID[]="<qsort.c gjm 1.16 2021-02-20>";
#endif
/* END SDL CHANGE ... commented this out with an #if 0 block. --ryan. */
@ -165,7 +174,8 @@ static char _ID[]="<qsort.c gjm 1.15 2016-03-10>";
#define WORD_BYTES sizeof(int)
/* How big does our stack need to be? Answer: one entry per
* bit in a |size_t|.
* bit in a |size_t|. (Actually, a bit less because we don't
* recurse all the way down to size-1 subarrays.)
*/
#define STACK_SIZE (8*sizeof(size_t))
@ -204,11 +214,12 @@ typedef struct { char * first; char * last; } stack_entry;
* 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.
* 2. We always save the longer and get to work on the
* shorter. This guarantees that whenever we push
* a k'th entry onto the stack we are about to get
* working on something of size <= N/2^k where N is
* the original array size; so the stack can't need
* more than log_2(max-array-size) entries.
* 3. We choose a pivot by looking at the first, last
* and middle elements. We arrange them into order
* because it's easy to do that in conjunction with
@ -270,8 +281,8 @@ typedef struct { char * first; char * last; } stack_entry;
if (r>=Trunc) doRight \
else pop \
} \
else if (l<=r) { pushLeft; doRight } \
else if (r>=Trunc) { pushRight; doLeft }\
else if (l<=r) { pushRight; doLeft } \
else if (r>=Trunc) { pushLeft; doRight }\
else doLeft \
}
@ -365,7 +376,7 @@ typedef struct { char * first; char * last; } stack_entry;
/* ---------------------------------------------------------------------- */
static char * pivot_big(char *first, char *mid, char *last, size_t size,
int compare(const void *, const void *)) {
int (SDLCALL * 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));
@ -406,7 +417,7 @@ fprintf(stderr,"-> %d %d %d @ %p %p %p\n",*(int*)m1,*(int*)m2,*(int*)m3, m1,m2,m
/* ---------------------------------------------------------------------- */
static void qsort_nonaligned(void *base, size_t nmemb, size_t size,
int (*compare)(const void *, const void *)) {
int (SDLCALL * compare)(const void *, const void *)) {
stack_entry stack[STACK_SIZE];
int stacktop=0;
@ -437,7 +448,7 @@ static void qsort_nonaligned(void *base, size_t nmemb, size_t size,
}
static void qsort_aligned(void *base, size_t nmemb, size_t size,
int (*compare)(const void *, const void *)) {
int (SDLCALL * compare)(const void *, const void *)) {
stack_entry stack[STACK_SIZE];
int stacktop=0;
@ -468,7 +479,7 @@ static void qsort_aligned(void *base, size_t nmemb, size_t size,
}
static void qsort_words(void *base, size_t nmemb,
int (*compare)(const void *, const void *)) {
int (SDLCALL * compare)(const void *, const void *)) {
stack_entry stack[STACK_SIZE];
int stacktop=0;
@ -519,11 +530,10 @@ fprintf(stderr, "after partitioning first=#%lu last=#%lu\n", (first-(char*)base)
/* ---------------------------------------------------------------------- */
extern void qsortG(void *base, size_t nmemb, size_t size,
int (*compare)(const void *, const void *)) {
extern void qsortG(void *base, size_t nmemb, size_t size, SDL_CompareCallback compare) {
if (nmemb<=1) return;
if (((size_t)base|size)&(WORD_BYTES-1))
if (((uintptr_t)base|size)&(WORD_BYTES-1))
qsort_nonaligned(base,nmemb,size,compare);
else if (size!=WORD_BYTES)
qsort_aligned(base,nmemb,size,compare);
@ -533,7 +543,7 @@ extern void qsortG(void *base, size_t nmemb, size_t size,
#endif /* HAVE_QSORT */
void *SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compare)(const void *, const void *))
void *SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare)
{
#if defined(HAVE_BSEARCH)
return bsearch(key, base, nmemb, size, compare);
@ -566,4 +576,3 @@ void *SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size,
}
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -55,234 +55,235 @@ static size_t UTF8_TrailingBytes(unsigned char c)
}
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
/**
* Parses an unsigned long long and returns the unsigned value and sign bit.
*
* Positive values are clamped to ULLONG_MAX.
* The result `value == 0 && negative` indicates negative overflow
* and might need to be handled differently depending on whether a
* signed or unsigned integer is being parsed.
*/
static size_t SDL_ScanUnsignedLongLongInternal(const char *text, int count, int radix, unsigned long long *valuep, SDL_bool *negativep)
{
const unsigned long long ullong_max = ~0ULL;
const char *text_start = text;
const char *number_start = text_start;
unsigned long long value = 0;
SDL_bool negative = SDL_FALSE;
SDL_bool overflow = SDL_FALSE;
if (radix == 0 || (radix >= 2 && radix <= 36)) {
while (SDL_isspace(*text)) {
++text;
}
if (*text == '-' || *text == '+') {
negative = *text == '-';
++text;
}
if ((radix == 0 || radix == 16) && *text == '0' && (text[1] == 'x' || text[1] == 'X')) {
text += 2;
radix = 16;
} else if (radix == 0 && *text == '0' && (text[1] >= '0' && text[1] <= '9')) {
++text;
radix = 8;
} else if (radix == 0) {
radix = 10;
}
number_start = text;
do {
unsigned long long digit;
if (*text >= '0' && *text <= '9') {
digit = *text - '0';
} else if (radix > 10) {
if (*text >= 'A' && *text < 'A' + (radix - 10)) {
digit = 10 + (*text - 'A');
} else if (*text >= 'a' && *text < 'a' + (radix - 10)) {
digit = 10 + (*text - 'a');
} else {
break;
}
} else {
break;
}
if (value != 0 && radix > ullong_max / value) {
overflow = SDL_TRUE;
} else {
value *= radix;
if (digit > ullong_max - value) {
overflow = SDL_TRUE;
} else {
value += digit;
}
}
++text;
} while (count == 0 || (text - text_start) != count);
}
if (text == number_start) {
if (radix == 16 && text > text_start && (*(text - 1) == 'x' || *(text - 1) == 'X')) {
// the string was "0x"; consume the '0' but not the 'x'
--text;
} else {
// no number was parsed, and thus no characters were consumed
text = text_start;
}
}
if (overflow) {
if (negative) {
value = 0;
} else {
value = ullong_max;
}
} else if (value == 0) {
negative = SDL_FALSE;
}
*valuep = value;
*negativep = negative;
return text - text_start;
}
static size_t SDL_ScanLong(const char *text, int count, int radix, long *valuep)
{
const char *textstart = text;
long value = 0;
SDL_bool negative = SDL_FALSE;
if (*text == '-') {
negative = SDL_TRUE;
++text;
}
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char)*text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
const unsigned long long_max = (~0UL) >> 1;
unsigned long long value;
SDL_bool negative;
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
if (negative) {
const unsigned long abs_long_min = long_max + 1;
if (value == 0 || value > abs_long_min) {
value = 0ULL - abs_long_min;
} else {
break;
}
value *= radix;
value += v;
++text;
if (count > 0 && (text - textstart) == count) {
break;
value = 0ULL - value;
}
} else if (value > long_max) {
value = long_max;
}
if (valuep && text > textstart) {
if (negative && value) {
*valuep = -value;
} else {
*valuep = value;
}
}
return text - textstart;
*valuep = (long)value;
return len;
}
#endif
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
static size_t SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valuep)
{
const char *textstart = text;
unsigned long value = 0;
if (*text == '-') {
return SDL_ScanLong(text, count, radix, (long *)valuep);
}
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char)*text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
const unsigned long ulong_max = ~0UL;
unsigned long long value;
SDL_bool negative;
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
if (negative) {
if (value == 0 || value > ulong_max) {
value = ulong_max;
} else if (value == ulong_max) {
value = 1;
} else {
break;
}
value *= radix;
value += v;
++text;
if (count > 0 && (text - textstart) == count) {
break;
value = 0ULL - value;
}
} else if (value > ulong_max) {
value = ulong_max;
}
if (valuep && text > textstart) {
*valuep = value;
}
return text - textstart;
*valuep = (unsigned long)value;
return len;
}
#endif
#ifndef HAVE_VSSCANF
static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
{
const char *textstart = text;
uintptr_t value = 0;
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char)*text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
const uintptr_t uintptr_max = ~(uintptr_t)0;
unsigned long long value;
SDL_bool negative;
size_t len = SDL_ScanUnsignedLongLongInternal(text, 0, 16, &value, &negative);
if (negative) {
if (value == 0 || value > uintptr_max) {
value = uintptr_max;
} else if (value == uintptr_max) {
value = 1;
} else {
break;
value = 0ULL - value;
}
value *= radix;
value += v;
++text;
} else if (value > uintptr_max) {
value = uintptr_max;
}
if (valuep && text > textstart) {
*valuep = value;
}
return text - textstart;
*valuep = (uintptr_t)value;
return len;
}
#endif
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
static size_t SDL_ScanLongLong(const char *text, int count, int radix, Sint64 *valuep)
{
const char *textstart = text;
Sint64 value = 0;
SDL_bool negative = SDL_FALSE;
if (*text == '-') {
negative = SDL_TRUE;
++text;
}
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char)*text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
const unsigned long long llong_max = (~0ULL) >> 1;
unsigned long long value;
SDL_bool negative;
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
if (negative) {
const unsigned long long abs_llong_min = llong_max + 1;
if (value == 0 || value > abs_llong_min) {
value = 0ULL - abs_llong_min;
} else {
break;
}
value *= radix;
value += v;
++text;
if (count > 0 && (text - textstart) == count) {
break;
value = 0ULL - value;
}
} else if (value > llong_max) {
value = llong_max;
}
if (valuep && text > textstart) {
if (negative && value) {
*valuep = -value;
} else {
*valuep = value;
}
}
return text - textstart;
*valuep = value;
return len;
}
#endif
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
static size_t SDL_ScanUnsignedLongLong(const char *text, int count, int radix, Uint64 *valuep)
{
const char *textstart = text;
Uint64 value = 0;
if (*text == '-') {
return SDL_ScanLongLong(text, count, radix, (Sint64 *)valuep);
}
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
text += 2;
}
for (;;) {
int v;
if (SDL_isdigit((unsigned char)*text)) {
v = *text - '0';
} else if (radix == 16 && SDL_isupperhex(*text)) {
v = 10 + (*text - 'A');
} else if (radix == 16 && SDL_islowerhex(*text)) {
v = 10 + (*text - 'a');
const unsigned long long ullong_max = ~0ULL;
SDL_bool negative;
size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, valuep, &negative);
if (negative) {
if (*valuep == 0) {
*valuep = ullong_max;
} else {
break;
}
value *= radix;
value += v;
++text;
if (count > 0 && (text - textstart) == count) {
break;
*valuep = 0ULL - *valuep;
}
}
if (valuep && text > textstart) {
*valuep = value;
}
return text - textstart;
return len;
}
#endif
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOD)
static size_t SDL_ScanFloat(const char *text, double *valuep)
{
const char *textstart = text;
unsigned long lvalue = 0;
const char *text_start = text;
const char *number_start = text_start;
double value = 0.0;
SDL_bool negative = SDL_FALSE;
if (*text == '-') {
negative = SDL_TRUE;
while (SDL_isspace(*text)) {
++text;
}
text += SDL_ScanUnsignedLong(text, 0, 10, &lvalue);
value += lvalue;
if (*text == '.') {
int mult = 10;
if (*text == '-' || *text == '+') {
negative = *text == '-';
++text;
while (SDL_isdigit((unsigned char)*text)) {
lvalue = *text - '0';
value += (double)lvalue / mult;
mult *= 10;
}
number_start = text;
if (SDL_isdigit(*text)) {
value += SDL_strtoull(text, (char **)(&text), 10);
if (*text == '.') {
double denom = 10;
++text;
while (SDL_isdigit(*text)) {
value += (double)(*text - '0') / denom;
denom *= 10;
++text;
}
}
}
if (valuep && text > textstart) {
if (negative && value) {
*valuep = -value;
} else {
*valuep = value;
}
if (text == number_start) {
// no number was parsed, and thus no characters were consumed
text = text_start;
} else if (negative) {
value = -value;
}
return text - textstart;
*valuep = value;
return text - text_start;
}
#endif
@ -897,18 +898,8 @@ long SDL_strtol(const char *string, char **endp, int base)
#if defined(HAVE_STRTOL)
return strtol(string, endp, base);
#else
size_t len;
long value = 0;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLong(string, 0, base, &value);
size_t len = SDL_ScanLong(string, 0, base, &value);
if (endp) {
*endp = (char *)string + len;
}
@ -922,18 +913,8 @@ SDL_strtoul(const char *string, char **endp, int base)
#if defined(HAVE_STRTOUL)
return strtoul(string, endp, base);
#else
size_t len;
unsigned long value = 0;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLong(string, 0, base, &value);
size_t len = SDL_ScanUnsignedLong(string, 0, base, &value);
if (endp) {
*endp = (char *)string + len;
}
@ -946,18 +927,8 @@ Sint64 SDL_strtoll(const char *string, char **endp, int base)
#if defined(HAVE_STRTOLL)
return strtoll(string, endp, base);
#else
size_t len;
Sint64 value = 0;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanLongLong(string, 0, base, &value);
long long value = 0;
size_t len = SDL_ScanLongLong(string, 0, base, &value);
if (endp) {
*endp = (char *)string + len;
}
@ -970,18 +941,8 @@ Uint64 SDL_strtoull(const char *string, char **endp, int base)
#if defined(HAVE_STRTOULL)
return strtoull(string, endp, base);
#else
size_t len;
Uint64 value = 0;
if (!base) {
if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
}
}
len = SDL_ScanUnsignedLongLong(string, 0, base, &value);
unsigned long long value = 0;
size_t len = SDL_ScanUnsignedLongLong(string, 0, base, &value);
if (endp) {
*endp = (char *)string + len;
}
@ -1114,12 +1075,46 @@ int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
return vsscanf(text, fmt, ap);
}
#else
static SDL_bool CharacterMatchesSet(char c, const char *set, size_t set_len)
{
SDL_bool invert = SDL_FALSE;
SDL_bool result = SDL_FALSE;
if (*set == '^') {
invert = SDL_TRUE;
++set;
--set_len;
}
while (set_len > 0 && !result) {
if (set_len >= 3 && set[1] == '-') {
char low_char = SDL_min(set[0], set[2]);
char high_char = SDL_max(set[0], set[2]);
if (c >= low_char && c <= high_char) {
result = SDL_TRUE;
}
set += 3;
set_len -= 3;
} else {
if (c == *set) {
result = SDL_TRUE;
}
++set;
--set_len;
}
}
if (invert) {
result = result ? SDL_FALSE : SDL_TRUE;
}
return result;
}
/* NOLINTNEXTLINE(readability-non-const-parameter) */
int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
{
const char *start = text;
int retval = 0;
if (text == NULL || !*text) {
if (!text || !*text) {
return -1;
}
@ -1387,6 +1382,74 @@ int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
}
done = SDL_TRUE;
break;
case 'n':
switch (inttype) {
case DO_SHORT:
{
short *valuep = va_arg(ap, short *);
*valuep = (short)(text - start);
} break;
case DO_INT:
{
int *valuep = va_arg(ap, int *);
*valuep = (int)(text - start);
} break;
case DO_LONG:
{
long *valuep = va_arg(ap, long *);
*valuep = (long)(text - start);
} break;
case DO_LONGLONG:
{
long long *valuep = va_arg(ap, long long *);
*valuep = (long long)(text - start);
} break;
case DO_SIZE_T:
{
size_t *valuep = va_arg(ap, size_t *);
*valuep = (size_t)(text - start);
} break;
}
done = SDL_TRUE;
break;
case '[':
{
const char *set = fmt + 1;
while (*fmt && *fmt != ']') {
++fmt;
}
if (*fmt) {
size_t set_len = (fmt - set);
if (suppress) {
while (CharacterMatchesSet(*text, set, set_len)) {
++text;
if (count) {
if (--count == 0) {
break;
}
}
}
} else {
SDL_bool had_match = SDL_FALSE;
char *valuep = va_arg(ap, char *);
while (CharacterMatchesSet(*text, set, set_len)) {
had_match = SDL_TRUE;
*valuep++ = *text++;
if (count) {
if (--count == 0) {
break;
}
}
}
*valuep = '\0';
if (had_match) {
++retval;
}
}
}
}
done = SDL_TRUE;
break;
default:
done = SDL_TRUE;
break;
@ -1458,7 +1521,7 @@ typedef enum
typedef struct
{
SDL_bool left_justify; /* for now: ignored. */
SDL_bool left_justify;
SDL_bool force_sign;
SDL_bool force_type; /* for now: used only by float printer, ignored otherwise. */
SDL_bool pad_zeroes;
@ -1470,33 +1533,41 @@ typedef struct
static size_t SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *string)
{
const char fill = (info && info->pad_zeroes) ? '0' : ' ';
size_t width = 0;
size_t filllen = 0;
size_t length = 0;
size_t slen, sz;
if (string == NULL) {
if (!string) {
string = "(null)";
}
sz = SDL_strlen(string);
if (info && info->width > 0 && (size_t)info->width > sz) {
const char fill = info->pad_zeroes ? '0' : ' ';
size_t width = info->width - sz;
size_t filllen;
width = info->width - sz;
if (info->precision >= 0 && (size_t)info->precision < sz) {
width += sz - (size_t)info->precision;
}
filllen = SDL_min(width, maxlen);
SDL_memset(text, fill, filllen);
text += filllen;
maxlen -= filllen;
length += width;
if (!info->left_justify) {
SDL_memset(text, fill, filllen);
text += filllen;
maxlen -= filllen;
length += width;
filllen = 0;
}
}
SDL_strlcpy(text, string, maxlen);
length += sz;
if (filllen > 0) {
SDL_memset(text + sz, fill, filllen);
length += width;
}
if (info) {
if (info->precision >= 0 && (size_t)info->precision < sz) {
slen = (size_t)info->precision;
@ -1533,7 +1604,7 @@ static void SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *inf
{ /* left-pad num with zeroes. */
size_t sz, pad, have_sign;
if (info == NULL) {
if (!info) {
return;
}
@ -1664,6 +1735,20 @@ static size_t SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, do
return length;
}
static size_t SDL_PrintPointer(char *text, size_t maxlen, SDL_FormatInfo *info, const void *value)
{
char num[130];
size_t length;
if (!value) {
return SDL_PrintString(text, maxlen, info, NULL);
}
SDL_ulltoa((unsigned long long)(uintptr_t)value, num, 16);
length = SDL_PrintString(text, maxlen, info, "0x");
return length + SDL_PrintString(TEXT_AND_LEN_ARGS, info, num);
}
/* NOLINTNEXTLINE(readability-non-const-parameter) */
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
{
@ -1796,6 +1881,10 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *f
done = SDL_TRUE;
break;
case 'p':
info.force_case = SDL_CASE_LOWER;
length += SDL_PrintPointer(TEXT_AND_LEN_ARGS, &info, va_arg(ap, void *));
done = SDL_TRUE;
break;
case 'x':
info.force_case = SDL_CASE_LOWER;
SDL_FALLTHROUGH;
@ -1806,9 +1895,6 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *f
if (info.radix == 10) {
info.radix = 16;
}
if (*fmt == 'p') {
inttype = DO_LONG;
}
SDL_FALLTHROUGH;
case 'o':
if (info.radix == 10) {
@ -1906,7 +1992,7 @@ int SDL_vasprintf(char **strp, const char *fmt, va_list ap)
*strp = NULL;
p = (char *)SDL_malloc(size);
if (p == NULL) {
if (!p) {
return -1;
}
@ -1918,6 +2004,7 @@ int SDL_vasprintf(char **strp, const char *fmt, va_list ap)
/* Check error code */
if (retval < 0) {
SDL_free(p);
return retval;
}
@ -1931,7 +2018,7 @@ int SDL_vasprintf(char **strp, const char *fmt, va_list ap)
size = retval + 1; /* Precisely what is needed */
np = (char *)SDL_realloc(p, size);
if (np == NULL) {
if (!np) {
SDL_free(p);
return -1;
} else {
@ -1939,5 +2026,3 @@ int SDL_vasprintf(char **strp, const char *fmt, va_list ap)
}
}
}
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages

View file

@ -1,6 +1,6 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages