src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Mar 2006 09:43:47 +0000
changeset 1456 84de7511f79f
parent 1455 f487bb150acc
child 1465 8dfa9a6d69a5
permissions -rw-r--r--
Fixed a bunch of 64-bit compatibility problems
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* This file provides a general interface for SDL to read and write
    25    data sources.  It can easily be extended to files, memory, etc.
    26 */
    27 
    28 #include "SDL_endian.h"
    29 #include "SDL_rwops.h"
    30 
    31 
    32 #ifdef __WIN32__
    33 
    34 /* Functions to read/write Win32 API file pointers */
    35 
    36 #define WINDOWS_LEAN_AND_MEAN
    37 #include <windows.h>
    38 
    39 static int win32_file_open(SDL_RWops *context, const char *filename, const char *mode) {
    40 	
    41 	UINT	old_error_mode;
    42 	HANDLE	h;
    43 	DWORD	r_right, w_right;
    44 	DWORD	must_exist, truncate;
    45 	int		a_mode;
    46 
    47 	if (!context)
    48 		return -1;
    49 		
    50 	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
    51 
    52 	/* "r" = reading, file must exist */
    53 	/* "w" = writing, truncate existing, file may not exist */
    54 	/* "r+"= reading or writing, file must exist            */
    55 	/* "a" = writing, append file may not exist             */
    56 	/* "a+"= append + read, file may not exist              */
    57 	/* "w+" = read, write, truncate. file may not exist    */
    58 	
    59 	must_exist = ( SDL_strchr(mode,'r') != NULL ) ? OPEN_EXISTING : 0;
    60 	truncate   = ( SDL_strchr(mode,'w') != NULL ) ? CREATE_ALWAYS : 0;
    61 	r_right    = ( SDL_strchr(mode,'+') != NULL || must_exist ) ? GENERIC_READ : 0;
    62 	a_mode     = ( SDL_strchr(mode,'a') != NULL ) ? OPEN_ALWAYS : 0;
    63 	w_right    = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;
    64 
    65 	if (!r_right && !w_right) /* inconsistent mode */
    66 		return -1; /* failed (invalid call)*/
    67 	
    68 	/* Do not open a dialog box if failure */
    69 	old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);	
    70 	
    71 	h = CreateFile(filename, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
    72 		           NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
    73 	
    74 	/* restore old behaviour */
    75 	SetErrorMode(old_error_mode);
    76 
    77 	if (h==INVALID_HANDLE_VALUE) {
    78 		SDL_SetError("Couldn't open %s",filename);
    79 		return -2; /* failed (CreateFile) */
    80 	}
    81 	context->hidden.win32io.h = h;
    82 	context->hidden.win32io.append = a_mode;
    83 	
    84 	return 0; /* ok */
    85 }
    86 static int win32_file_seek(SDL_RWops *context, int offset, int whence) {
    87 	DWORD win32whence;
    88 	int   file_pos;
    89 	
    90 	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
    91 		SDL_SetError("win32_file_seek: invalid context/file not opened");
    92 		return -1;
    93 	}
    94 	
    95 	switch (whence) {
    96 		case RW_SEEK_SET:		
    97 			win32whence = FILE_BEGIN; break;
    98 		case RW_SEEK_CUR:
    99 			win32whence = FILE_CURRENT; break;
   100 		case RW_SEEK_END:
   101 			win32whence = FILE_END; break;
   102 		default:
   103 			SDL_SetError("win32_file_seek: Unknown value for 'whence'");			
   104 			return -1;
   105 	}
   106 	
   107 	file_pos = SetFilePointer(context->hidden.win32io.h,offset,NULL,win32whence);
   108 
   109 	if ( file_pos != INVALID_SET_FILE_POINTER )
   110 		return file_pos; /* success */
   111 	
   112 	SDL_Error(SDL_EFSEEK);
   113 	return -1; /* error */
   114 }
   115 static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum) {
   116 	
   117 	int		total_bytes; 
   118 	DWORD	byte_read,nread;
   119 	
   120 	total_bytes = size*maxnum;
   121 	
   122 	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE || total_bytes<=0 || !size) 	
   123 		return 0;
   124 	
   125 	if (!ReadFile(context->hidden.win32io.h,ptr,total_bytes,&byte_read,NULL)) {
   126 		SDL_Error(SDL_EFREAD);
   127 		return 0;
   128 	}
   129 	nread = byte_read/size;
   130 	return nread;
   131 }
   132 static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num) {
   133 	
   134 	int		total_bytes; 
   135 	DWORD	byte_written,nwritten;
   136 	
   137 	total_bytes = size*num;
   138 
   139 	if (!context || context->hidden.win32io.h==INVALID_HANDLE_VALUE || total_bytes<=0 || !size) 	
   140 		return 0;
   141 
   142 	/* if in append mode, we must go to the EOF before write */
   143 	if (context->hidden.win32io.append) {
   144 		if ( SetFilePointer(context->hidden.win32io.h,0L,NULL,FILE_END) == INVALID_SET_FILE_POINTER ) {
   145 			SDL_Error(SDL_EFWRITE);
   146 			return 0;
   147 		}
   148 	}
   149 	
   150 	if (!WriteFile(context->hidden.win32io.h,ptr,total_bytes,&byte_written,NULL)) {
   151 		SDL_Error(SDL_EFWRITE);
   152 		return 0;
   153 	}
   154 	
   155 	nwritten = byte_written/size;
   156 	return nwritten;
   157 }
   158 static int win32_file_close(SDL_RWops *context) {
   159 	
   160 	if ( context ) {								
   161 		if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
   162 			CloseHandle(context->hidden.win32io.h);
   163 			context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */
   164 		}
   165 		SDL_FreeRW(context);
   166 	}
   167 	return(0);
   168 }
   169 #endif /* __WIN32__ */
   170 
   171 #ifdef HAVE_STDIO_H
   172 
   173 /* Functions to read/write stdio file pointers */
   174 
   175 static int stdio_seek(SDL_RWops *context, int offset, int whence)
   176 {
   177 	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
   178 		return(ftell(context->hidden.stdio.fp));
   179 	} else {
   180 		SDL_Error(SDL_EFSEEK);
   181 		return(-1);
   182 	}
   183 }
   184 static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   185 {
   186 	size_t nread;
   187 
   188 	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
   189 	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
   190 		SDL_Error(SDL_EFREAD);
   191 	}
   192 	return(nread);
   193 }
   194 static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
   195 {
   196 	size_t nwrote;
   197 
   198 	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   199 	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
   200 		SDL_Error(SDL_EFWRITE);
   201 	}
   202 	return(nwrote);
   203 }
   204 static int stdio_close(SDL_RWops *context)
   205 {
   206 	if ( context ) {
   207 		if ( context->hidden.stdio.autoclose ) {
   208 			/* WARNING:  Check the return value here! */
   209 			fclose(context->hidden.stdio.fp);
   210 		}
   211 		SDL_FreeRW(context);
   212 	}
   213 	return(0);
   214 }
   215 #endif /* !HAVE_STDIO_H */
   216 
   217 /* Functions to read/write memory pointers */
   218 
   219 static int mem_seek(SDL_RWops *context, int offset, int whence)
   220 {
   221 	Uint8 *newpos;
   222 
   223 	switch (whence) {
   224 		case RW_SEEK_SET:
   225 			newpos = context->hidden.mem.base+offset;
   226 			break;
   227 		case RW_SEEK_CUR:
   228 			newpos = context->hidden.mem.here+offset;
   229 			break;
   230 		case RW_SEEK_END:
   231 			newpos = context->hidden.mem.stop+offset;
   232 			break;
   233 		default:
   234 			SDL_SetError("Unknown value for 'whence'");
   235 			return(-1);
   236 	}
   237 	if ( newpos < context->hidden.mem.base ) {
   238 		newpos = context->hidden.mem.base;
   239 	}
   240 	if ( newpos > context->hidden.mem.stop ) {
   241 		newpos = context->hidden.mem.stop;
   242 	}
   243 	context->hidden.mem.here = newpos;
   244 	return(context->hidden.mem.here-context->hidden.mem.base);
   245 }
   246 static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   247 {
   248 	size_t total_bytes;
   249 	size_t mem_available;
   250 
   251 	total_bytes = (maxnum * size);
   252 	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) {
   253 		return 0;
   254 	}
   255 
   256 	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   257 	if (total_bytes > mem_available) {
   258 		total_bytes = mem_available;
   259 	}
   260 
   261 	SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   262 	context->hidden.mem.here += total_bytes;
   263 
   264 	return (total_bytes / size);
   265 }
   266 static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
   267 {
   268 	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
   269 		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
   270 	}
   271 	SDL_memcpy(context->hidden.mem.here, ptr, num*size);
   272 	context->hidden.mem.here += num*size;
   273 	return(num);
   274 }
   275 static int mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
   276 {
   277 	SDL_SetError("Can't write to read-only memory");
   278 	return(-1);
   279 }
   280 static int mem_close(SDL_RWops *context)
   281 {
   282 	if ( context ) {
   283 		SDL_FreeRW(context);
   284 	}
   285 	return(0);
   286 }
   287 
   288 
   289 /* Functions to create SDL_RWops structures from various data sources */
   290 
   291 #ifdef __MACOS__
   292 /*
   293  * translate unix-style slash-separated filename to mac-style colon-separated
   294  * name; return malloced string
   295  */
   296 static char *unix_to_mac(const char *file)
   297 {
   298 	int flen = SDL_strlen(file);
   299 	char *path = SDL_malloc(flen + 2);
   300 	const char *src = file;
   301 	char *dst = path;
   302 	if(*src == '/') {
   303 		/* really depends on filesystem layout, hope for the best */
   304 		src++;
   305 	} else {
   306 		/* Check if this is a MacOS path to begin with */
   307 		if(*src != ':')
   308 			*dst++ = ':';   /* relative paths begin with ':' */
   309 	}
   310 	while(src < file + flen) {
   311 		const char *end = SDL_strchr(src, '/');
   312 		int len;
   313 		if(!end)
   314 			end = file + flen; /* last component */
   315 		len = end - src;
   316 		if(len == 0 || (len == 1 && src[0] == '.')) {
   317 			/* remove repeated slashes and . */
   318 		} else {
   319 			if(len == 2 && src[0] == '.' && src[1] == '.') {
   320 				/* replace .. with the empty string */
   321 			} else {
   322 				SDL_memcpy(dst, src, len);
   323 				dst += len;
   324 			}
   325 			if(end < file + flen)
   326 				*dst++ = ':';
   327 		}
   328 		src = end + 1;
   329 	}
   330 	*dst++ = '\0';
   331 	return path;
   332 }
   333 #endif /* __MACOS__ */
   334 
   335 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   336 {
   337 	SDL_RWops *rwops = NULL;
   338 
   339 	if ( !file || !*file || !mode || !*mode ) {
   340 		SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
   341 		return NULL;
   342 	}
   343 
   344 #ifdef __WIN32__
   345 	rwops = SDL_AllocRW();
   346 	if (!rwops)
   347 		return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
   348 	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   349 	if (win32_file_open(rwops,file,mode)) {
   350 		SDL_FreeRW(rwops);
   351 		return NULL;
   352 	}	
   353 	rwops->seek  = win32_file_seek;
   354 	rwops->read  = win32_file_read;
   355 	rwops->write = win32_file_write;
   356 	rwops->close = win32_file_close;
   357 
   358 #elif HAVE_STDIO_H
   359 	FILE *fp;
   360 
   361 #ifdef __MACOS__
   362 	{
   363 		char *mpath = unix_to_mac(file);
   364 		fp = fopen(mpath, mode);
   365 		SDL_free(mpath);
   366 	}
   367 #else
   368 	fp = fopen(file, mode);
   369 #endif
   370 	if ( fp == NULL ) {
   371 		SDL_SetError("Couldn't open %s", file);
   372 	} else {
   373 		rwops = SDL_RWFromFP(fp, 1);
   374 	}
   375 #else
   376 	SDL_SetError("SDL not compiled with stdio support");
   377 #endif /* !HAVE_STDIO_H */
   378 
   379 	return(rwops);
   380 }
   381 
   382 #ifdef HAVE_STDIO_H
   383 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   384 {
   385 	SDL_RWops *rwops = NULL;
   386 
   387 	rwops = SDL_AllocRW();
   388 	if ( rwops != NULL ) {
   389 		rwops->seek = stdio_seek;
   390 		rwops->read = stdio_read;
   391 		rwops->write = stdio_write;
   392 		rwops->close = stdio_close;
   393 		rwops->hidden.stdio.fp = fp;
   394 		rwops->hidden.stdio.autoclose = autoclose;
   395 	}
   396 	return(rwops);
   397 }
   398 #endif /* HAVE_STDIO_H */
   399 
   400 SDL_RWops *SDL_RWFromMem(void *mem, int size)
   401 {
   402 	SDL_RWops *rwops;
   403 
   404 	rwops = SDL_AllocRW();
   405 	if ( rwops != NULL ) {
   406 		rwops->seek = mem_seek;
   407 		rwops->read = mem_read;
   408 		rwops->write = mem_write;
   409 		rwops->close = mem_close;
   410 		rwops->hidden.mem.base = (Uint8 *)mem;
   411 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   412 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   413 	}
   414 	return(rwops);
   415 }
   416 
   417 SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
   418 {
   419 	SDL_RWops *rwops;
   420 
   421 	rwops = SDL_AllocRW();
   422 	if ( rwops != NULL ) {
   423 		rwops->seek = mem_seek;
   424 		rwops->read = mem_read;
   425 		rwops->write = mem_writeconst;
   426 		rwops->close = mem_close;
   427 		rwops->hidden.mem.base = (Uint8 *)mem;
   428 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   429 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   430 	}
   431 	return(rwops);
   432 }
   433 
   434 SDL_RWops *SDL_AllocRW(void)
   435 {
   436 	SDL_RWops *area;
   437 
   438 	area = (SDL_RWops *)SDL_malloc(sizeof *area);
   439 	if ( area == NULL ) {
   440 		SDL_OutOfMemory();
   441 	}
   442 	return(area);
   443 }
   444 
   445 void SDL_FreeRW(SDL_RWops *area)
   446 {
   447 	SDL_free(area);
   448 }
   449 
   450 /* Functions for dynamically reading and writing endian-specific values */
   451 
   452 Uint16 SDL_ReadLE16 (SDL_RWops *src)
   453 {
   454 	Uint16 value;
   455 
   456 	SDL_RWread(src, &value, (sizeof value), 1);
   457 	return(SDL_SwapLE16(value));
   458 }
   459 Uint16 SDL_ReadBE16 (SDL_RWops *src)
   460 {
   461 	Uint16 value;
   462 
   463 	SDL_RWread(src, &value, (sizeof value), 1);
   464 	return(SDL_SwapBE16(value));
   465 }
   466 Uint32 SDL_ReadLE32 (SDL_RWops *src)
   467 {
   468 	Uint32 value;
   469 
   470 	SDL_RWread(src, &value, (sizeof value), 1);
   471 	return(SDL_SwapLE32(value));
   472 }
   473 Uint32 SDL_ReadBE32 (SDL_RWops *src)
   474 {
   475 	Uint32 value;
   476 
   477 	SDL_RWread(src, &value, (sizeof value), 1);
   478 	return(SDL_SwapBE32(value));
   479 }
   480 Uint64 SDL_ReadLE64 (SDL_RWops *src)
   481 {
   482 	Uint64 value;
   483 
   484 	SDL_RWread(src, &value, (sizeof value), 1);
   485 	return(SDL_SwapLE64(value));
   486 }
   487 Uint64 SDL_ReadBE64 (SDL_RWops *src)
   488 {
   489 	Uint64 value;
   490 
   491 	SDL_RWread(src, &value, (sizeof value), 1);
   492 	return(SDL_SwapBE64(value));
   493 }
   494 
   495 int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
   496 {
   497 	value = SDL_SwapLE16(value);
   498 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   499 }
   500 int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
   501 {
   502 	value = SDL_SwapBE16(value);
   503 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   504 }
   505 int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
   506 {
   507 	value = SDL_SwapLE32(value);
   508 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   509 }
   510 int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
   511 {
   512 	value = SDL_SwapBE32(value);
   513 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   514 }
   515 int SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
   516 {
   517 	value = SDL_SwapLE64(value);
   518 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   519 }
   520 int SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
   521 {
   522 	value = SDL_SwapBE64(value);
   523 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   524 }