src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 27 Feb 2006 19:19:22 +0000
changeset 1453 2eb75f8a32ca
parent 1447 515df0086eb7
child 1455 f487bb150acc
permissions -rw-r--r--
Date: Mon, 27 Feb 2006 17:01:43 +0100
From: "William Petiot [exoide]"
Subject: Re: [SDL] cvs version: standard win32 SDL.DLL file test [patch]

I found a bug or two in previous patch, so here is a patch to correct this.
     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 || !filename || !mode)
    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 	int total_bytes;
   249 	int 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 #ifdef __WIN32__
   340 	rwops = SDL_AllocRW();
   341 	if (!rwops)
   342 		return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
   343 	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   344 	if (win32_file_open(rwops,file,mode)) {
   345 		SDL_FreeRW(rwops);
   346 		return NULL;
   347 	}	
   348 	rwops->seek  = win32_file_seek;
   349 	rwops->read  = win32_file_read;
   350 	rwops->write = win32_file_write;
   351 	rwops->close = win32_file_close;
   352 
   353 #elif HAVE_STDIO_H
   354 	FILE *fp;
   355 
   356 #ifdef __MACOS__
   357 	{
   358 		char *mpath = unix_to_mac(file);
   359 		fp = fopen(mpath, mode);
   360 		SDL_free(mpath);
   361 	}
   362 #else
   363 	fp = fopen(file, mode);
   364 #endif
   365 	if ( fp == NULL ) {
   366 		SDL_SetError("Couldn't open %s", file);
   367 	} else {
   368 		rwops = SDL_RWFromFP(fp, 1);
   369 	}
   370 #else
   371 	SDL_SetError("SDL not compiled with stdio support");
   372 #endif /* !HAVE_STDIO_H */
   373 
   374 	return(rwops);
   375 }
   376 
   377 #ifdef HAVE_STDIO_H
   378 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   379 {
   380 	SDL_RWops *rwops = NULL;
   381 
   382 	rwops = SDL_AllocRW();
   383 	if ( rwops != NULL ) {
   384 		rwops->seek = stdio_seek;
   385 		rwops->read = stdio_read;
   386 		rwops->write = stdio_write;
   387 		rwops->close = stdio_close;
   388 		rwops->hidden.stdio.fp = fp;
   389 		rwops->hidden.stdio.autoclose = autoclose;
   390 	}
   391 	return(rwops);
   392 }
   393 #endif /* HAVE_STDIO_H */
   394 
   395 SDL_RWops *SDL_RWFromMem(void *mem, int size)
   396 {
   397 	SDL_RWops *rwops;
   398 
   399 	rwops = SDL_AllocRW();
   400 	if ( rwops != NULL ) {
   401 		rwops->seek = mem_seek;
   402 		rwops->read = mem_read;
   403 		rwops->write = mem_write;
   404 		rwops->close = mem_close;
   405 		rwops->hidden.mem.base = (Uint8 *)mem;
   406 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   407 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   408 	}
   409 	return(rwops);
   410 }
   411 
   412 SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
   413 {
   414 	SDL_RWops *rwops;
   415 
   416 	rwops = SDL_AllocRW();
   417 	if ( rwops != NULL ) {
   418 		rwops->seek = mem_seek;
   419 		rwops->read = mem_read;
   420 		rwops->write = mem_writeconst;
   421 		rwops->close = mem_close;
   422 		rwops->hidden.mem.base = (Uint8 *)mem;
   423 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   424 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   425 	}
   426 	return(rwops);
   427 }
   428 
   429 SDL_RWops *SDL_AllocRW(void)
   430 {
   431 	SDL_RWops *area;
   432 
   433 	area = (SDL_RWops *)SDL_malloc(sizeof *area);
   434 	if ( area == NULL ) {
   435 		SDL_OutOfMemory();
   436 	}
   437 	return(area);
   438 }
   439 
   440 void SDL_FreeRW(SDL_RWops *area)
   441 {
   442 	SDL_free(area);
   443 }
   444 
   445 /* Functions for dynamically reading and writing endian-specific values */
   446 
   447 Uint16 SDL_ReadLE16 (SDL_RWops *src)
   448 {
   449 	Uint16 value;
   450 
   451 	SDL_RWread(src, &value, (sizeof value), 1);
   452 	return(SDL_SwapLE16(value));
   453 }
   454 Uint16 SDL_ReadBE16 (SDL_RWops *src)
   455 {
   456 	Uint16 value;
   457 
   458 	SDL_RWread(src, &value, (sizeof value), 1);
   459 	return(SDL_SwapBE16(value));
   460 }
   461 Uint32 SDL_ReadLE32 (SDL_RWops *src)
   462 {
   463 	Uint32 value;
   464 
   465 	SDL_RWread(src, &value, (sizeof value), 1);
   466 	return(SDL_SwapLE32(value));
   467 }
   468 Uint32 SDL_ReadBE32 (SDL_RWops *src)
   469 {
   470 	Uint32 value;
   471 
   472 	SDL_RWread(src, &value, (sizeof value), 1);
   473 	return(SDL_SwapBE32(value));
   474 }
   475 Uint64 SDL_ReadLE64 (SDL_RWops *src)
   476 {
   477 	Uint64 value;
   478 
   479 	SDL_RWread(src, &value, (sizeof value), 1);
   480 	return(SDL_SwapLE64(value));
   481 }
   482 Uint64 SDL_ReadBE64 (SDL_RWops *src)
   483 {
   484 	Uint64 value;
   485 
   486 	SDL_RWread(src, &value, (sizeof value), 1);
   487 	return(SDL_SwapBE64(value));
   488 }
   489 
   490 int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
   491 {
   492 	value = SDL_SwapLE16(value);
   493 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   494 }
   495 int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
   496 {
   497 	value = SDL_SwapBE16(value);
   498 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   499 }
   500 int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
   501 {
   502 	value = SDL_SwapLE32(value);
   503 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   504 }
   505 int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
   506 {
   507 	value = SDL_SwapBE32(value);
   508 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   509 }
   510 int SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
   511 {
   512 	value = SDL_SwapLE64(value);
   513 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   514 }
   515 int SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
   516 {
   517 	value = SDL_SwapBE64(value);
   518 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   519 }