Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input va…
Browse files Browse the repository at this point in the history
…lidation, add test coverage
  • Loading branch information
ferzkopp committed Mar 13, 2013
1 parent 46d44be commit aa6ce54
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 14 deletions.
8 changes: 8 additions & 0 deletions include/SDL_rwops.h
Expand Up @@ -40,6 +40,14 @@ extern "C" {
/* *INDENT-ON* */
#endif

/* RWops Types */
#define SDL_RWOPS_UNKNOWN 0 /* Unknown stream type */
#define SDL_RWOPS_WINFILE 1 /* Win32 file */
#define SDL_RWOPS_STDFILE 2 /* Stdio file */
#define SDL_RWOPS_JNIFILE 3 /* Android asset */
#define SDL_RWOPS_MEMORY 4 /* Memory stream */
#define SDL_RWOPS_MEMORY_RO 5 /* Read-Only memory stream */

/**
* This is the read/write operation structure -- very basic.
*/
Expand Down
26 changes: 24 additions & 2 deletions src/file/SDL_rwops.c
Expand Up @@ -513,6 +513,7 @@ SDL_RWFromFile(const char *file, const char *mode)
rwops->read = Android_JNI_FileRead;
rwops->write = Android_JNI_FileWrite;
rwops->close = Android_JNI_FileClose;
rwops->type = SDL_RWOPS_JNIFILE;

#elif defined(__WIN32__)
rwops = SDL_AllocRW();
Expand All @@ -527,6 +528,7 @@ SDL_RWFromFile(const char *file, const char *mode)
rwops->read = windows_file_read;
rwops->write = windows_file_write;
rwops->close = windows_file_close;
rwops->type = SDL_RWOPS_WINFILE;

#elif HAVE_STDIO_H
{
Expand Down Expand Up @@ -570,6 +572,7 @@ SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
rwops->close = stdio_close;
rwops->hidden.stdio.fp = fp;
rwops->hidden.stdio.autoclose = autoclose;
rwops->type = SDL_RWOPS_STDFILE;
}
return (rwops);
}
Expand All @@ -585,7 +588,15 @@ SDL_RWFromFP(void * fp, SDL_bool autoclose)
SDL_RWops *
SDL_RWFromMem(void *mem, int size)
{
SDL_RWops *rwops;
SDL_RWops *rwops = NULL;
if (!mem) {
SDL_InvalidParamError("mem");
return (rwops);
}
if (!size) {
SDL_InvalidParamError("size");
return (rwops);
}

rwops = SDL_AllocRW();
if (rwops != NULL) {
Expand All @@ -597,14 +608,23 @@ SDL_RWFromMem(void *mem, int size)
rwops->hidden.mem.base = (Uint8 *) mem;
rwops->hidden.mem.here = rwops->hidden.mem.base;
rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
rwops->type = SDL_RWOPS_MEMORY;
}
return (rwops);
}

SDL_RWops *
SDL_RWFromConstMem(const void *mem, int size)
{
SDL_RWops *rwops;
SDL_RWops *rwops = NULL;
if (!mem) {
SDL_InvalidParamError("mem");
return (rwops);
}
if (!size) {
SDL_InvalidParamError("size");
return (rwops);
}

rwops = SDL_AllocRW();
if (rwops != NULL) {
Expand All @@ -616,6 +636,7 @@ SDL_RWFromConstMem(const void *mem, int size)
rwops->hidden.mem.base = (Uint8 *) mem;
rwops->hidden.mem.here = rwops->hidden.mem.base;
rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
rwops->type = SDL_RWOPS_MEMORY_RO;
}
return (rwops);
}
Expand All @@ -629,6 +650,7 @@ SDL_AllocRW(void)
if (area == NULL) {
SDL_OutOfMemory();
}
area->type = SDL_RWOPS_UNKNOWN;
return (area);
}

Expand Down

0 comments on commit aa6ce54

Please sign in to comment.