19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This file provides a general interface for SDL to read and write
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   data sources.  It can easily be extended to files, memory, etc.
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_endian.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_rwops.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__WIN32__) && !defined(__SYMBIAN32__)
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions to read/write Win32 API file pointers */
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Will not use it on WinCE because stdio is buffered, it means
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   faster, and all stdio functions anyway are embedded in coredll.dll -
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   the main wince dll*/
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WINDOWS_LEAN_AND_MEAN
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <windows.h>
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef INVALID_SET_FILE_POINTER
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define READAHEAD_BUFFER_SIZE	1024
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL win32_file_open(SDL_RWops *context, const char *filename, const char *mode)
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _WIN32_WCE
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UINT	old_error_mode;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HANDLE	h;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD	r_right, w_right;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD	must_exist, truncate;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int		a_mode;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!context)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1; /* failed (invalid call) */
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.win32io.buffer.data = NULL;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.win32io.buffer.size = 0;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.win32io.buffer.left = 0;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* "r" = reading, file must exist */
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* "w" = writing, truncate existing, file may not exist */
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* "r+"= reading or writing, file must exist            */
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* "a" = writing, append file may not exist             */
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* "a+"= append + read, file may not exist              */
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* "w+" = read, write, truncate. file may not exist    */
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	must_exist = ( SDL_strchr(mode,'r') != NULL ) ? OPEN_EXISTING : 0;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	truncate   = ( SDL_strchr(mode,'w') != NULL ) ? CREATE_ALWAYS : 0;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	r_right    = ( SDL_strchr(mode,'+') != NULL || must_exist ) ? GENERIC_READ : 0;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	a_mode     = ( SDL_strchr(mode,'a') != NULL ) ? OPEN_ALWAYS : 0;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	w_right    = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!r_right && !w_right) /* inconsistent mode */
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1; /* failed (invalid call) */
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!context->hidden.win32io.buffer.data) {
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		size_t size = SDL_strlen(filename)+1;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_stack_free(filenameW);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(context->hidden.win32io.buffer.data);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			context->hidden.win32io.buffer.data = NULL;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unable to convert filename to Unicode");
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return -1;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					   NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_stack_free(filenameW);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* handle Unicode filenames.  We do some tapdancing here to make sure this
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   works on Win9x, which doesn't support anything but 1-byte codepages. */
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const size_t size = SDL_strlen(filename)+1;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static int unicode_support = -1;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (unicode_support == -1) {
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		OSVERSIONINFO osVerInfo;     /* Information about the OS */
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		osVerInfo.dwOSVersionInfoSize = sizeof(osVerInfo);
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (!GetVersionEx(&osVerInfo)) {
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			unicode_support = 0;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if (osVerInfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			unicode_support = 1;  /* Not Win95/98/ME. */
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			unicode_support = 0;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (unicode_support) {  /* everything but Win95/98/ME. */
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_stack_free(filenameW);
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(context->hidden.win32io.buffer.data);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			context->hidden.win32io.buffer.data = NULL;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unable to convert filename to Unicode");
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return -1;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Do not open a dialog box if failure */
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		h = CreateFileW(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					   NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* restore old behaviour */
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetErrorMode(old_error_mode);
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_stack_free(filenameW);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* CP_UTF8 might not be supported (Win95), so use SDL_iconv to get wchars. */
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Use UCS2: no UTF-16 support here. Try again in SDL 1.3.  :) */
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char *utf16 = SDL_iconv_string("UCS2", "UTF8", filename, SDL_strlen(filename) + 1);
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char *filenameA = SDL_stack_alloc(char, size * 6);  /* 6, just in case. */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		BOOL bDefCharUsed = FALSE;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Dither down to a codepage and hope for the best. */
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (!utf16 ||
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			!WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)utf16, -1, filenameA, size*6, 0, &bDefCharUsed) ||
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			bDefCharUsed) {
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_stack_free(filenameA);
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(utf16);
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(context->hidden.win32io.buffer.data);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			context->hidden.win32io.buffer.data = NULL;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unable to convert filename to Unicode");
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return -1;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Do not open a dialog box if failure */
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		h = CreateFile(filenameA, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		               NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* restore old behaviour */
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetErrorMode(old_error_mode);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_stack_free(filenameA);
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(utf16);
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* _WIN32_WCE */
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (h==INVALID_HANDLE_VALUE) {
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(context->hidden.win32io.buffer.data);
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		context->hidden.win32io.buffer.data = NULL;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't open %s",filename);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -2; /* failed (CreateFile) */
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.win32io.h = h;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.win32io.append = a_mode;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0; /* ok */
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD win32whence;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int   file_pos;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("win32_file_seek: invalid context/file not opened");
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* FIXME: We may be able to satisfy the seek within buffered data */
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (whence == RW_SEEK_CUR && context->hidden.win32io.buffer.left) {
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		offset -= context->hidden.win32io.buffer.left;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    context->hidden.win32io.buffer.left = 0;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (whence) {
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case RW_SEEK_SET:
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			win32whence = FILE_BEGIN; break;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case RW_SEEK_CUR:
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			win32whence = FILE_CURRENT; break;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case RW_SEEK_END:
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			win32whence = FILE_END; break;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("win32_file_seek: Unknown value for 'whence'");
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return -1;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	file_pos = SetFilePointer(context->hidden.win32io.h,offset,NULL,win32whence);
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( file_pos != INVALID_SET_FILE_POINTER )
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return file_pos; /* success */
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Error(SDL_EFSEEK);
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1; /* error */
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum)
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int		total_need;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int		total_read = 0;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int     read_ahead;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD	byte_read;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	total_need = size*maxnum;
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE || total_need<=0 || !size)
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (context->hidden.win32io.buffer.left > 0) {
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        void *data = (char *)context->hidden.win32io.buffer.data +
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             context->hidden.win32io.buffer.size -
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             context->hidden.win32io.buffer.left;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        read_ahead = SDL_min(total_need, context->hidden.win32io.buffer.left);
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_memcpy(ptr, data, read_ahead);
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        context->hidden.win32io.buffer.left -= read_ahead;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (read_ahead == total_need) {
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return maxnum;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ptr = (char *)ptr + read_ahead;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        total_need -= read_ahead;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		total_read += read_ahead;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (total_need < READAHEAD_BUFFER_SIZE) {
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (!ReadFile(context->hidden.win32io.h,context->hidden.win32io.buffer.data,READAHEAD_BUFFER_SIZE,&byte_read,NULL)) {
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_Error(SDL_EFREAD);
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return 0;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        read_ahead = SDL_min(total_need, (int)byte_read);
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_memcpy(ptr, context->hidden.win32io.buffer.data, read_ahead);
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        context->hidden.win32io.buffer.size = byte_read;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        context->hidden.win32io.buffer.left = byte_read-read_ahead;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        total_read += read_ahead;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (!ReadFile(context->hidden.win32io.h,ptr,total_need,&byte_read,NULL)) {
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_Error(SDL_EFREAD);
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return 0;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        total_read += byte_read;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (total_read/size);
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL win32_file_write(SDL_RWops *context, const void *ptr, int size, int num)
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int		total_bytes;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD	byte_written,nwritten;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	total_bytes = size*num;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!context || context->hidden.win32io.h==INVALID_HANDLE_VALUE || total_bytes<=0 || !size)
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (context->hidden.win32io.buffer.left) {
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetFilePointer(context->hidden.win32io.h,-context->hidden.win32io.buffer.left,NULL,FILE_CURRENT);
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		context->hidden.win32io.buffer.left = 0;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* if in append mode, we must go to the EOF before write */
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (context->hidden.win32io.append) {
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SetFilePointer(context->hidden.win32io.h,0L,NULL,FILE_END) == INVALID_SET_FILE_POINTER ) {
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Error(SDL_EFWRITE);
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return 0;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!WriteFile(context->hidden.win32io.h,ptr,total_bytes,&byte_written,NULL)) {
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Error(SDL_EFWRITE);
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	nwritten = byte_written/size;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return nwritten;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL win32_file_close(SDL_RWops *context)
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( context ) {
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			CloseHandle(context->hidden.win32io.h);
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (context->hidden.win32io.buffer.data) {
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(context->hidden.win32io.buffer.data);
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			context->hidden.win32io.buffer.data = NULL;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeRW(context);
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* __WIN32__ */
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_STDIO_H
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions to read/write stdio file pointers */
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL stdio_seek(SDL_RWops *context, int offset, int whence)
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(ftell(context->hidden.stdio.fp));
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Error(SDL_EFSEEK);
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t nread;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Error(SDL_EFREAD);
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(nread);
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t nwrote;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Error(SDL_EFWRITE);
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(nwrote);
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL stdio_close(SDL_RWops *context)
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( context ) {
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( context->hidden.stdio.autoclose ) {
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* WARNING:  Check the return value here! */
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fclose(context->hidden.stdio.fp);
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeRW(context);
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !HAVE_STDIO_H */
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions to read/write memory pointers */
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL mem_seek(SDL_RWops *context, int offset, int whence)
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *newpos;
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (whence) {
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case RW_SEEK_SET:
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			newpos = context->hidden.mem.base+offset;
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case RW_SEEK_CUR:
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			newpos = context->hidden.mem.here+offset;
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case RW_SEEK_END:
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			newpos = context->hidden.mem.stop+offset;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unknown value for 'whence'");
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( newpos < context->hidden.mem.base ) {
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		newpos = context->hidden.mem.base;
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( newpos > context->hidden.mem.stop ) {
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		newpos = context->hidden.mem.stop;
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.mem.here = newpos;
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(context->hidden.mem.here-context->hidden.mem.base);
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t total_bytes;
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t mem_available;
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	total_bytes = (maxnum * size);
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != (size_t) size) ) {
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (total_bytes > mem_available) {
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		total_bytes = mem_available;
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.mem.here += total_bytes;
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (total_bytes / size);
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL mem_write(SDL_RWops *context, const void *ptr, int size, int num)
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memcpy(context->hidden.mem.here, ptr, num*size);
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	context->hidden.mem.here += num*size;
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(num);
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("Can't write to read-only memory");
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDLCALL mem_close(SDL_RWops *context)
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( context ) {
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeRW(context);
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions to create SDL_RWops structures from various data sources */
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __MACOS__
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * translate unix-style slash-separated filename to mac-style colon-separated
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * name; return malloced string
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char *unix_to_mac(const char *file)
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int flen = SDL_strlen(file);
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *path = SDL_malloc(flen + 2);
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *src = file;
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *dst = path;
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(*src == '/') {
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* really depends on filesystem layout, hope for the best */
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		src++;
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Check if this is a MacOS path to begin with */
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(*src != ':')
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*dst++ = ':';   /* relative paths begin with ':' */
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while(src < file + flen) {
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const char *end = SDL_strchr(src, '/');
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int len;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(!end)
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			end = file + flen; /* last component */
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		len = end - src;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(len == 0 || (len == 1 && src[0] == '.')) {
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* remove repeated slashes and . */
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(len == 2 && src[0] == '.' && src[1] == '.') {
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* replace .. with the empty string */
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_memcpy(dst, src, len);
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dst += len;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(end < file + flen)
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*dst++ = ':';
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		src = end + 1;
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*dst++ = '\0';
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return path;
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* __MACOS__ */
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWops *rwops = NULL;
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_STDIO_H
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FILE *fp = NULL;
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(void) fp;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !file || !*file || !mode || !*mode ) {
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__WIN32__) && !defined(__SYMBIAN32__)
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rwops = SDL_AllocRW();
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!rwops)
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (win32_file_open(rwops,file,mode) < 0) {
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeRW(rwops);
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rwops->seek  = win32_file_seek;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rwops->read  = win32_file_read;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rwops->write = win32_file_write;
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rwops->close = win32_file_close;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif HAVE_STDIO_H
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __MACOS__
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char *mpath = unix_to_mac(file);
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fp = fopen(mpath, mode);
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(mpath);
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fp = fopen(file, mode);
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( fp == NULL ) {
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't open %s", file);
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops = SDL_RWFromFP(fp, 1);
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("SDL not compiled with stdio support");
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !HAVE_STDIO_H */
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(rwops);
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_STDIO_H
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWops *rwops = NULL;
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rwops = SDL_AllocRW();
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( rwops != NULL ) {
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->seek = stdio_seek;
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->read = stdio_read;
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->write = stdio_write;
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->close = stdio_close;
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->hidden.stdio.fp = fp;
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->hidden.stdio.autoclose = autoclose;
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(rwops);
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_STDIO_H */
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_RWops *SDL_RWFromMem(void *mem, int size)
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWops *rwops;
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rwops = SDL_AllocRW();
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( rwops != NULL ) {
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->seek = mem_seek;
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->read = mem_read;
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->write = mem_write;
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->close = mem_close;
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->hidden.mem.base = (Uint8 *)mem;
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->hidden.mem.here = rwops->hidden.mem.base;
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(rwops);
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWops *rwops;
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rwops = SDL_AllocRW();
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( rwops != NULL ) {
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->seek = mem_seek;
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->read = mem_read;
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->write = mem_writeconst;
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->close = mem_close;
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->hidden.mem.base = (Uint8 *)mem;
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->hidden.mem.here = rwops->hidden.mem.base;
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(rwops);
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_RWops *SDL_AllocRW(void)
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWops *area;
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	area = (SDL_RWops *)SDL_malloc(sizeof *area);
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( area == NULL ) {
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(area);
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_FreeRW(SDL_RWops *area)
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(area);
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions for dynamically reading and writing endian-specific values */
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint16 SDL_ReadLE16 (SDL_RWops *src)
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 value;
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWread(src, &value, (sizeof value), 1);
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SwapLE16(value));
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint16 SDL_ReadBE16 (SDL_RWops *src)
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 value;
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWread(src, &value, (sizeof value), 1);
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SwapBE16(value));
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_ReadLE32 (SDL_RWops *src)
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 value;
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWread(src, &value, (sizeof value), 1);
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SwapLE32(value));
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_ReadBE32 (SDL_RWops *src)
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 value;
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWread(src, &value, (sizeof value), 1);
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SwapBE32(value));
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint64 SDL_ReadLE64 (SDL_RWops *src)
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint64 value;
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWread(src, &value, (sizeof value), 1);
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SwapLE64(value));
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint64 SDL_ReadBE64 (SDL_RWops *src)
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint64 value;
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_RWread(src, &value, (sizeof value), 1);
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SwapBE64(value));
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	value = SDL_SwapLE16(value);
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	value = SDL_SwapBE16(value);
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	value = SDL_SwapLE32(value);
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	value = SDL_SwapBE32(value);
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	value = SDL_SwapLE64(value);
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	value = SDL_SwapBE64(value);
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
674