Trim down the SDL and GLEW libraries to just those files we use.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5720 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Soren Jorvang
2010-06-16 09:11:29 +00:00
parent 9b6ccb4d77
commit 980a2680be
1777 changed files with 0 additions and 336765 deletions

View File

@ -1,247 +0,0 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2009 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_stdinc.h"
#ifndef HAVE_GETENV
#if defined(__WIN32__) && !defined(_WIN32_WCE) && !defined(__SYMBIAN32__)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
/* Note this isn't thread-safe! */
static char *SDL_envmem = NULL; /* Ugh, memory leak */
static size_t SDL_envmemlen = 0;
/* Put a variable of the form "name=value" into the environment */
int SDL_putenv(const char *variable)
{
size_t bufferlen;
char *value;
const char *sep;
sep = SDL_strchr(variable, '=');
if ( sep == NULL ) {
return -1;
}
bufferlen = SDL_strlen(variable)+1;
if ( bufferlen > SDL_envmemlen ) {
char *newmem = (char *)SDL_realloc(SDL_envmem, bufferlen);
if ( newmem == NULL ) {
return -1;
}
SDL_envmem = newmem;
SDL_envmemlen = bufferlen;
}
SDL_strlcpy(SDL_envmem, variable, bufferlen);
value = SDL_envmem + (sep - variable);
*value++ = '\0';
if ( !SetEnvironmentVariable(SDL_envmem, *value ? value : NULL) ) {
return -1;
}
return 0;
}
/* Retrieve a variable named "name" from the environment */
char *SDL_getenv(const char *name)
{
size_t bufferlen;
bufferlen = GetEnvironmentVariable(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;
GetEnvironmentVariable(name, SDL_envmem, (DWORD)SDL_envmemlen);
}
return SDL_envmem;
}
#else /* roll our own */
static char **SDL_env = (char **)0;
/* Put a variable of the form "name=value" into the environment */
int SDL_putenv(const char *variable)
{
const char *name, *value;
int added;
int len, i;
char **new_env;
char *new_variable;
/* A little error checking */
if ( ! variable ) {
return(-1);
}
name = variable;
for ( value=variable; *value && (*value != '='); ++value ) {
/* Keep looking for '=' */ ;
}
if ( *value ) {
++value;
} else {
return(-1);
}
/* Allocate memory for the variable */
new_variable = SDL_strdup(variable);
if ( ! new_variable ) {
return(-1);
}
/* 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);
}
/* Retrieve a variable named "name" from the environment */
char *SDL_getenv(const char *name)
{
int len, i;
char *value;
value = (char *)0;
if ( SDL_env ) {
len = SDL_strlen(name);
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];
}
}
}
return value;
}
#endif /* __WIN32__ */
#endif /* !HAVE_GETENV */
#ifdef TEST_MAIN
#include <stdio.h>
int main(int argc, char *argv[])
{
char *value;
printf("Checking for non-existent variable... ");
fflush(stdout);
if ( ! SDL_getenv("EXISTS") ) {
printf("okay\n");
} else {
printf("failed\n");
}
printf("Setting FIRST=VALUE1 in the environment... ");
fflush(stdout);
if ( SDL_putenv("FIRST=VALUE1") == 0 ) {
printf("okay\n");
} else {
printf("failed\n");
}
printf("Getting FIRST from the environment... ");
fflush(stdout);
value = SDL_getenv("FIRST");
if ( value && (SDL_strcmp(value, "VALUE1") == 0) ) {
printf("okay\n");
} else {
printf("failed\n");
}
printf("Setting SECOND=VALUE2 in the environment... ");
fflush(stdout);
if ( SDL_putenv("SECOND=VALUE2") == 0 ) {
printf("okay\n");
} else {
printf("failed\n");
}
printf("Getting SECOND from the environment... ");
fflush(stdout);
value = SDL_getenv("SECOND");
if ( value && (SDL_strcmp(value, "VALUE2") == 0) ) {
printf("okay\n");
} else {
printf("failed\n");
}
printf("Setting FIRST=NOVALUE in the environment... ");
fflush(stdout);
if ( SDL_putenv("FIRST=NOVALUE") == 0 ) {
printf("okay\n");
} else {
printf("failed\n");
}
printf("Getting FIRST from the environment... ");
fflush(stdout);
value = SDL_getenv("FIRST");
if ( value && (SDL_strcmp(value, "NOVALUE") == 0) ) {
printf("okay\n");
} else {
printf("failed\n");
}
printf("Checking for non-existent variable... ");
fflush(stdout);
if ( ! SDL_getenv("EXISTS") ) {
printf("okay\n");
} else {
printf("failed\n");
}
return(0);
}
#endif /* TEST_MAIN */

View File

@ -1,881 +0,0 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2009 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
/* This file contains portable iconv functions for SDL */
#include "SDL_stdinc.h"
#include "SDL_endian.h"
#ifdef HAVE_ICONV
/* Depending on which standard the iconv() was implemented with,
iconv() may or may not use const char ** for the inbuf param.
If we get this wrong, it's just a warning, so no big deal.
*/
#if defined(_XGP6) || \
defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
#define ICONV_INBUF_NONCONST
#endif
#include <errno.h>
size_t SDL_iconv(SDL_iconv_t cd,
const char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
size_t retCode;
#ifdef ICONV_INBUF_NONCONST
retCode = iconv(cd, (char **)inbuf, inbytesleft, outbuf, outbytesleft);
#else
retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
#endif
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_UCS2, /* Native byte order assumed */
ENCODING_UCS4, /* Native byte order assumed */
};
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define ENCODING_UTF16NATIVE ENCODING_UTF16BE
#define ENCODING_UTF32NATIVE ENCODING_UTF32BE
#else
#define ENCODING_UTF16NATIVE ENCODING_UTF16LE
#define ENCODING_UTF32NATIVE ENCODING_UTF32LE
#endif
struct _SDL_iconv_t
{
int src_fmt;
int dst_fmt;
};
static struct {
const char *name;
int format;
} encodings[] = {
{ "ASCII", ENCODING_ASCII },
{ "US-ASCII", ENCODING_ASCII },
{ "8859-1", ENCODING_LATIN1 },
{ "ISO-8859-1", ENCODING_LATIN1 },
{ "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_UCS2 },
{ "UCS-2", ENCODING_UCS2 },
{ "UCS4", ENCODING_UCS4 },
{ "UCS-4", ENCODING_UCS4 },
};
static const char *getlocale(char *buffer, size_t bufsize)
{
const char *lang;
char *ptr;
lang = SDL_getenv("LC_ALL");
if ( !lang ) {
lang = SDL_getenv("LC_CTYPE");
}
if ( !lang ) {
lang = SDL_getenv("LC_MESSAGES");
}
if ( !lang ) {
lang = SDL_getenv("LANG");
}
if ( !lang || !*lang || SDL_strcmp(lang, "C") == 0 ) {
lang = "ASCII";
}
/* We need to trim down strings like "en_US.UTF-8@blah" to "UTF-8" */
ptr = SDL_strchr(lang, '.');
if (ptr != 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 || !*fromcode ) {
fromcode = getlocale(fromcode_buffer, sizeof(fromcode_buffer));
}
if ( !tocode || !*tocode ) {
tocode = getlocale(tocode_buffer, sizeof(tocode_buffer));
}
for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {
src_fmt = encodings[i].format;
if ( dst_fmt != ENCODING_UNKNOWN ) {
break;
}
}
if ( SDL_strcasecmp(tocode, encodings[i].name) == 0 ) {
dst_fmt = encodings[i].format;
if ( src_fmt != ENCODING_UNKNOWN ) {
break;
}
}
}
if ( src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN ) {
SDL_iconv_t cd = (SDL_iconv_t)SDL_malloc(sizeof(*cd));
if ( cd ) {
cd->src_fmt = src_fmt;
cd->dst_fmt = dst_fmt;
return cd;
}
}
return (SDL_iconv_t)-1;
}
size_t SDL_iconv(SDL_iconv_t cd,
const char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
/* For simplicity, we'll convert everything to and from UCS-4 */
const char *src;
char *dst;
size_t srclen, dstlen;
Uint32 ch = 0;
size_t total;
if ( !inbuf || !*inbuf ) {
/* Reset the context */
return 0;
}
if ( !outbuf || !*outbuf || !outbytesleft || !*outbytesleft ) {
return SDL_ICONV_E2BIG;
}
src = *inbuf;
srclen = (inbytesleft ? *inbytesleft : 0);
dst = *outbuf;
dstlen = *outbytesleft;
switch ( cd->src_fmt ) {
case ENCODING_UTF16:
/* Scan for a byte order marker */
{
Uint8 *p = (Uint8 *)src;
size_t n = srclen / 2;
while ( n ) {
if ( p[0] == 0xFF && p[1] == 0xFE ) {
cd->src_fmt = ENCODING_UTF16BE;
break;
} else if ( p[0] == 0xFE && p[1] == 0xFF ) {
cd->src_fmt = ENCODING_UTF16LE;
break;
}
p += 2;
--n;
}
if ( n == 0 ) {
/* We can't tell, default to host order */
cd->src_fmt = ENCODING_UTF16NATIVE;
}
}
break;
case ENCODING_UTF32:
/* Scan for a byte order marker */
{
Uint8 *p = (Uint8 *)src;
size_t n = srclen / 4;
while ( n ) {
if ( p[0] == 0xFF && p[1] == 0xFE &&
p[2] == 0x00 && p[3] == 0x00 ) {
cd->src_fmt = ENCODING_UTF32BE;
break;
} else if ( p[0] == 0x00 && p[1] == 0x00 &&
p[2] == 0xFE && p[3] == 0xFF ) {
cd->src_fmt = ENCODING_UTF32LE;
break;
}
p += 4;
--n;
}
if ( n == 0 ) {
/* We can't tell, default to host order */
cd->src_fmt = ENCODING_UTF32NATIVE;
}
}
break;
}
switch ( cd->dst_fmt ) {
case ENCODING_UTF16:
/* Default to host order, need to add byte order marker */
if ( dstlen < 2 ) {
return SDL_ICONV_E2BIG;
}
*(Uint16 *)dst = UNICODE_BOM;
dst += 2;
dstlen -= 2;
cd->dst_fmt = ENCODING_UTF16NATIVE;
break;
case ENCODING_UTF32:
/* Default to host order, need to add byte order marker */
if ( dstlen < 4 ) {
return SDL_ICONV_E2BIG;
}
*(Uint32 *)dst = UNICODE_BOM;
dst += 4;
dstlen -= 4;
cd->dst_fmt = ENCODING_UTF32NATIVE;
break;
}
total = 0;
while ( srclen > 0 ) {
/* Decode a character */
switch ( cd->src_fmt ) {
case ENCODING_ASCII:
{
Uint8 *p = (Uint8 *)src;
ch = (Uint32)(p[0] & 0x7F);
++src;
--srclen;
}
break;
case ENCODING_LATIN1:
{
Uint8 *p = (Uint8 *)src;
ch = (Uint32)p[0];
++src;
--srclen;
}
break;
case ENCODING_UTF8: /* RFC 3629 */
{
Uint8 *p = (Uint8 *)src;
size_t left = 0;
SDL_bool overlong = SDL_FALSE;
if ( p[0] >= 0xFC ) {
if ( (p[0] & 0xFE) != 0xFC ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if ( p[0] == 0xFC ) {
overlong = SDL_TRUE;
}
ch = (Uint32)(p[0] & 0x01);
left = 5;
}
} else if ( p[0] >= 0xF8 ) {
if ( (p[0] & 0xFC) != 0xF8 ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if ( p[0] == 0xF8 ) {
overlong = SDL_TRUE;
}
ch = (Uint32)(p[0] & 0x03);
left = 4;
}
} else if ( p[0] >= 0xF0 ) {
if ( (p[0] & 0xF8) != 0xF0 ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if ( p[0] == 0xF0 ) {
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 ) {
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] & 0xCE) == 0xC0 ) {
overlong = SDL_TRUE;
}
ch = (Uint32)(p[0] & 0x1F);
left = 1;
}
} else {
if ( (p[0] & 0x80) != 0x00 ) {
/* 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_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_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;
case ENCODING_UCS2:
{
Uint16 *p = (Uint16 *)src;
if ( srclen < 2 ) {
return SDL_ICONV_EINVAL;
}
ch = *p;
src += 2;
srclen -= 2;
}
break;
case ENCODING_UCS4:
{
Uint32 *p = (Uint32 *)src;
if ( srclen < 4 ) {
return SDL_ICONV_EINVAL;
}
ch = *p;
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 ( ch <= 0x1FFFFF ) {
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;
} else if ( ch <= 0x3FFFFFF ) {
if ( dstlen < 5 ) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xF8 | (Uint8)((ch >> 24) & 0x03);
p[1] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
p[2] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
p[3] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
p[4] = 0x80 | (Uint8)(ch & 0x3F);
dst += 5;
dstlen -= 5;
} else {
if ( dstlen < 6 ) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xFC | (Uint8)((ch >> 30) & 0x01);
p[1] = 0x80 | (Uint8)((ch >> 24) & 0x3F);
p[2] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
p[3] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
p[4] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
p[5] = 0x80 | (Uint8)(ch & 0x3F);
dst += 6;
dstlen -= 6;
}
}
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_UTF32BE:
{
Uint8 *p = (Uint8 *)dst;
if ( ch > 0x10FFFF ) {
ch = UNKNOWN_UNICODE;
}
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:
{
Uint8 *p = (Uint8 *)dst;
if ( ch > 0x10FFFF ) {
ch = UNKNOWN_UNICODE;
}
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;
case ENCODING_UCS2:
{
Uint16 *p = (Uint16 *)dst;
if ( ch > 0xFFFF ) {
ch = UNKNOWN_UNICODE;
}
if ( dstlen < 2 ) {
return SDL_ICONV_E2BIG;
}
*p = (Uint16)ch;
dst += 2;
dstlen -= 2;
}
break;
case ENCODING_UCS4:
{
Uint32 *p = (Uint32 *)dst;
if ( ch > 0x7FFFFFFF ) {
ch = UNKNOWN_UNICODE;
}
if ( dstlen < 4 ) {
return SDL_ICONV_E2BIG;
}
*p = 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 && 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 || !*tocode ) {
tocode = "UTF-8";
}
if ( !fromcode || !*fromcode ) {
fromcode = "UTF-8";
}
cd = SDL_iconv_open(tocode, fromcode);
}
if ( cd == (SDL_iconv_t)-1 ) {
return NULL;
}
stringsize = inbytesleft > 4 ? inbytesleft : 4;
string = SDL_malloc(stringsize);
if ( !string ) {
SDL_iconv_close(cd);
return NULL;
}
outbuf = string;
outbytesleft = stringsize;
SDL_memset(outbuf, 0, 4);
while ( inbytesleft > 0 ) {
retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
switch (retCode) {
case SDL_ICONV_E2BIG:
{
char *oldstring = string;
stringsize *= 2;
string = SDL_realloc(string, stringsize);
if ( !string ) {
SDL_iconv_close(cd);
return NULL;
}
outbuf = string + (outbuf - oldstring);
outbytesleft = stringsize - (outbuf - string);
SDL_memset(outbuf, 0, 4);
}
break;
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;
}
}
SDL_iconv_close(cd);
return string;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,443 +0,0 @@
/* qsort.c
* (c) 1998 Gareth McCaughan
*
* This is a drop-in replacement for the C library's |qsort()| routine.
*
* 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()|.
* - separate code for non-aligned / aligned / word-size objects
*
* This code may be reproduced freely provided
* - this file is retained unaltered apart from minor
* changes for portability and efficiency
* - no changes are made to this comment
* - any changes that *are* made are clearly flagged
* - the _ID string below is altered by inserting, after
* the date, the string " altered" followed at your option
* by other material. (Exceptions: you may change the name
* of the exported routine without changing the ID string.
* You may change the values of the macros TRUNC_* and
* PIVOT_THRESHOLD without changing the ID string, provided
* they remain constants with TRUNC_nonaligned, TRUNC_aligned
* and TRUNC_words/WORD_BYTES between 8 and 24, and
* PIVOT_THRESHOLD between 32 and 200.)
*
* You may use it in anything you like; you may make money
* out of it; you may distribute it in object form or as
* part of an executable without including source code;
* you don't have to credit me. (But it would be nice if
* you did.)
*
* 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 1998 and certainly
* OK for at least the next 18 months, is
* gjm11@dpmms.cam.ac.uk
* Thanks!
*
* Gareth McCaughan Peterhouse Cambridge 1998
*/
#include "SDL_config.h"
/*
#include <assert.h>
#include <stdlib.h>
#include <string.h>
*/
#include "SDL_stdinc.h"
#ifdef assert
#undef assert
#endif
#define assert(X)
#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 qsort
#undef qsort
#endif
#define qsort SDL_qsort
#ifndef HAVE_QSORT
static char _ID[]="<qsort.c gjm 1.12 1998-03-19>";
/* 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: */
#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: */
#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) { \
int swapped=0; \
do { \
while (compare(first,pivot)<0) first+=sz; \
while (compare(pivot,last)<0) last-=sz; \
if (first<last) { \
swapper(first,last); swapped=1; \
first+=sz; last-=sz; } \
else if (first==last) { first+=sz; last-=sz; break; }\
} while (first<=last); \
if (!swapped) pop \
}
/* 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().
*/
#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;
char *m1,*m2,*m3;
{ char *a=first, *b=first+d, *c=first+2*d;
#ifdef DEBUG_QSORT
fprintf(stderr,"< %d %d %d\n",*(int*)a,*(int*)b,*(int*)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\n",*(int*)a,*(int*)b,*(int*)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\n",*(int*)a,*(int*)b,*(int*)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\n",*(int*)m1,*(int*)m2,*(int*)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!=0);
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!=0);
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!=0);
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=%d\n",*(int*)pivot);
#endif
/* Partition. */
Partition(SWAP_words,WORD_BYTES);
/* 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);
}
/* ---------------------------------------------------------------------- */
void qsort(void *base, size_t nmemb, size_t size,
int (*compare)(const void *, const void *)) {
if (nmemb<=1) return;
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);
else
qsort_words(base,nmemb,compare);
}
#endif /* !HAVE_QSORT */

View File

@ -1,620 +0,0 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2009 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
/* This file contains portable stdlib functions for SDL */
#include "SDL_stdinc.h"
#ifndef HAVE_LIBC
/* These are some C runtime intrinsics that need to be defined */
#if defined(_MSC_VER)
#ifndef __FLTUSED__
#define __FLTUSED__
#ifdef __cplusplus
extern "C"
#endif
__declspec(selectany) int _fltused=1;
#endif
/* Float to long */
void __declspec(naked) _ftol()
{
__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
}
}
void __declspec(naked) _ftol2_sse()
{
_ftol();
}
/* 64-bit math operators for 32-bit systems */
void __declspec(naked) _allmul()
{
__asm {
push ebp
mov ebp,esp
push edi
push esi
push ebx
sub esp,0Ch
mov eax,dword ptr [ebp+10h]
mov edi,dword ptr [ebp+8]
mov ebx,eax
mov esi,eax
sar esi,1Fh
mov eax,dword ptr [ebp+8]
mul ebx
imul edi,esi
mov ecx,edx
mov dword ptr [ebp-18h],eax
mov edx,dword ptr [ebp+0Ch]
add ecx,edi
imul ebx,edx
mov eax,dword ptr [ebp-18h]
lea ebx,[ebx+ecx]
mov dword ptr [ebp-14h],ebx
mov edx,dword ptr [ebp-14h]
add esp,0Ch
pop ebx
pop esi
pop edi
pop ebp
ret
}
}
void __declspec(naked) _alldiv()
{
__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
}
}
void __declspec(naked) _aulldiv()
{
__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
}
}
void __declspec(naked) _allrem()
{
__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
}
}
void __declspec(naked) _aullrem()
{
__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
}
}
void __declspec(naked) _alldvrm()
{
__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
}
}
void __declspec(naked) _aulldvrm()
{
__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
}
}
void __declspec(naked) _allshl()
{
__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
}
}
void __declspec(naked) _aullshr()
{
__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
}
}
#endif /* MSC_VER */
#endif /* !HAVE_LIBC */