src/file/SDL_rwops.c
author David Ludwig <dludwig@pobox.com>
Sat, 23 Feb 2013 20:12:14 -0500
changeset 8440 29e693ff8706
parent 8439 14d94a8a9fb6
child 8460 f483e8bdae3c
permissions -rw-r--r--
WinRT: build fixes
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 /* Need this so Linux systems define fseek64o, ftell64o and off64_t */
    22 #define _LARGEFILE64_SOURCE
    23 #include "SDL_config.h"
    24 
    25 /* This file provides a general interface for SDL to read and write
    26    data sources.  It can easily be extended to files, memory, etc.
    27 */
    28 
    29 #include "SDL_endian.h"
    30 #include "SDL_rwops.h"
    31 
    32 #ifdef __APPLE__
    33 #include "cocoa/SDL_rwopsbundlesupport.h"
    34 #endif /* __APPLE__ */
    35 
    36 #ifdef ANDROID
    37 #include "../core/android/SDL_android.h"
    38 #endif
    39 
    40 #ifdef __NDS__
    41 /* include libfat headers for fatInitDefault(). */
    42 #include <fat.h>
    43 #endif /* __NDS__ */
    44 
    45 #ifdef __WIN32__
    46 
    47 /* Functions to read/write Win32 API file pointers */
    48 
    49 #include "../core/windows/SDL_windows.h"
    50 
    51 #ifndef INVALID_SET_FILE_POINTER
    52 #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
    53 #endif
    54 
    55 #define READAHEAD_BUFFER_SIZE	1024
    56 
    57 static int SDLCALL
    58 windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
    59 {
    60     UINT old_error_mode;
    61     HANDLE h;
    62     DWORD r_right, w_right;
    63     DWORD must_exist, truncate;
    64     int a_mode;
    65 
    66     if (!context)
    67         return -1;              /* failed (invalid call) */
    68 
    69     context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* mark this as unusable */
    70     context->hidden.windowsio.buffer.data = NULL;
    71     context->hidden.windowsio.buffer.size = 0;
    72     context->hidden.windowsio.buffer.left = 0;
    73 
    74     /* "r" = reading, file must exist */
    75     /* "w" = writing, truncate existing, file may not exist */
    76     /* "r+"= reading or writing, file must exist            */
    77     /* "a" = writing, append file may not exist             */
    78     /* "a+"= append + read, file may not exist              */
    79     /* "w+" = read, write, truncate. file may not exist    */
    80 
    81     must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
    82     truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
    83     r_right = (SDL_strchr(mode, '+') != NULL
    84                || must_exist) ? GENERIC_READ : 0;
    85     a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
    86     w_right = (a_mode || SDL_strchr(mode, '+')
    87                || truncate) ? GENERIC_WRITE : 0;
    88 
    89     if (!r_right && !w_right)   /* inconsistent mode */
    90         return -1;              /* failed (invalid call) */
    91 
    92     context->hidden.windowsio.buffer.data =
    93         (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
    94     if (!context->hidden.windowsio.buffer.data) {
    95         SDL_OutOfMemory();
    96         return -1;
    97     }
    98     /* Do not open a dialog box if failure */
    99     old_error_mode =
   100         SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
   101 
   102     {
   103         LPTSTR tstr = WIN_UTF8ToString(filename);
   104         h = CreateFile(tstr, (w_right | r_right),
   105                        (w_right) ? 0 : FILE_SHARE_READ, NULL,
   106                        (must_exist | truncate | a_mode),
   107                        FILE_ATTRIBUTE_NORMAL, NULL);
   108         SDL_free(tstr);
   109     }
   110 
   111     /* restore old behavior */
   112     SetErrorMode(old_error_mode);
   113 
   114     if (h == INVALID_HANDLE_VALUE) {
   115         SDL_free(context->hidden.windowsio.buffer.data);
   116         context->hidden.windowsio.buffer.data = NULL;
   117         SDL_SetError("Couldn't open %s", filename);
   118         return -2;              /* failed (CreateFile) */
   119     }
   120     context->hidden.windowsio.h = h;
   121     context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
   122 
   123     return 0;                   /* ok */
   124 }
   125 
   126 static Sint64 SDLCALL
   127 windows_file_size(SDL_RWops * context)
   128 {
   129     LARGE_INTEGER size;
   130 
   131     if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
   132         SDL_SetError("windows_file_size: invalid context/file not opened");
   133         return -1;
   134     }
   135 
   136     if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) {
   137         WIN_SetError("windows_file_size");
   138         return -1;
   139     }
   140 
   141     return size.QuadPart;
   142 }
   143 
   144 static Sint64 SDLCALL
   145 windows_file_seek(SDL_RWops * context, Sint64 offset, int whence)
   146 {
   147     DWORD windowswhence;
   148     LARGE_INTEGER windowsoffset;
   149 
   150     if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
   151         SDL_SetError("windows_file_seek: invalid context/file not opened");
   152         return -1;
   153     }
   154 
   155     /* FIXME: We may be able to satisfy the seek within buffered data */
   156     if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
   157         offset -= (long)context->hidden.windowsio.buffer.left;
   158     }
   159     context->hidden.windowsio.buffer.left = 0;
   160 
   161     switch (whence) {
   162     case RW_SEEK_SET:
   163         windowswhence = FILE_BEGIN;
   164         break;
   165     case RW_SEEK_CUR:
   166         windowswhence = FILE_CURRENT;
   167         break;
   168     case RW_SEEK_END:
   169         windowswhence = FILE_END;
   170         break;
   171     default:
   172         SDL_SetError("windows_file_seek: Unknown value for 'whence'");
   173         return -1;
   174     }
   175 
   176     windowsoffset.QuadPart = offset;
   177     if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) {
   178         WIN_SetError("windows_file_seek");
   179         return -1;
   180     }
   181     return windowsoffset.QuadPart;
   182 }
   183 
   184 static size_t SDLCALL
   185 windows_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   186 {
   187     size_t total_need;
   188     size_t total_read = 0;
   189     size_t read_ahead;
   190     DWORD byte_read;
   191 
   192     total_need = size * maxnum;
   193 
   194     if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
   195         || !total_need)
   196         return 0;
   197 
   198     if (context->hidden.windowsio.buffer.left > 0) {
   199         void *data = (char *) context->hidden.windowsio.buffer.data +
   200             context->hidden.windowsio.buffer.size -
   201             context->hidden.windowsio.buffer.left;
   202         read_ahead =
   203             SDL_min(total_need, context->hidden.windowsio.buffer.left);
   204         SDL_memcpy(ptr, data, read_ahead);
   205         context->hidden.windowsio.buffer.left -= read_ahead;
   206 
   207         if (read_ahead == total_need) {
   208             return maxnum;
   209         }
   210         ptr = (char *) ptr + read_ahead;
   211         total_need -= read_ahead;
   212         total_read += read_ahead;
   213     }
   214 
   215     if (total_need < READAHEAD_BUFFER_SIZE) {
   216         if (!ReadFile
   217             (context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
   218              READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
   219             SDL_Error(SDL_EFREAD);
   220             return 0;
   221         }
   222         read_ahead = SDL_min(total_need, (int) byte_read);
   223         SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
   224         context->hidden.windowsio.buffer.size = byte_read;
   225         context->hidden.windowsio.buffer.left = byte_read - read_ahead;
   226         total_read += read_ahead;
   227     } else {
   228         if (!ReadFile
   229             (context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
   230             SDL_Error(SDL_EFREAD);
   231             return 0;
   232         }
   233         total_read += byte_read;
   234     }
   235     return (total_read / size);
   236 }
   237 
   238 static size_t SDLCALL
   239 windows_file_write(SDL_RWops * context, const void *ptr, size_t size,
   240                  size_t num)
   241 {
   242 
   243     size_t total_bytes;
   244     DWORD byte_written;
   245     size_t nwritten;
   246 
   247     total_bytes = size * num;
   248 
   249     if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
   250         || total_bytes <= 0 || !size)
   251         return 0;
   252 
   253     if (context->hidden.windowsio.buffer.left) {
   254         SetFilePointer(context->hidden.windowsio.h,
   255                        -(LONG)context->hidden.windowsio.buffer.left, NULL,
   256                        FILE_CURRENT);
   257         context->hidden.windowsio.buffer.left = 0;
   258     }
   259 
   260     /* if in append mode, we must go to the EOF before write */
   261     if (context->hidden.windowsio.append) {
   262         if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
   263             INVALID_SET_FILE_POINTER) {
   264             SDL_Error(SDL_EFWRITE);
   265             return 0;
   266         }
   267     }
   268 
   269     if (!WriteFile
   270         (context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
   271         SDL_Error(SDL_EFWRITE);
   272         return 0;
   273     }
   274 
   275     nwritten = byte_written / size;
   276     return nwritten;
   277 }
   278 
   279 static int SDLCALL
   280 windows_file_close(SDL_RWops * context)
   281 {
   282 
   283     if (context) {
   284         if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
   285             CloseHandle(context->hidden.windowsio.h);
   286             context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* to be sure */
   287         }
   288         if (context->hidden.windowsio.buffer.data) {
   289             SDL_free(context->hidden.windowsio.buffer.data);
   290             context->hidden.windowsio.buffer.data = NULL;
   291         }
   292         SDL_FreeRW(context);
   293     }
   294     return (0);
   295 }
   296 #endif /* __WIN32__ */
   297 
   298 #ifdef HAVE_STDIO_H
   299 
   300 /* Functions to read/write stdio file pointers */
   301 
   302 static Sint64 SDLCALL
   303 stdio_size(SDL_RWops * context)
   304 {
   305     Sint64 pos, size;
   306 
   307     pos = SDL_RWseek(context, 0, RW_SEEK_CUR);
   308     if (pos < 0) {
   309         return -1;
   310     }
   311     size = SDL_RWseek(context, 0, RW_SEEK_END);
   312 
   313     SDL_RWseek(context, pos, RW_SEEK_SET);
   314     return size;
   315 }
   316 
   317 static Sint64 SDLCALL
   318 stdio_seek(SDL_RWops * context, Sint64 offset, int whence)
   319 {
   320 #ifdef HAVE_FSEEKO64
   321     if (fseeko64(context->hidden.stdio.fp, (off64_t)offset, whence) == 0) {
   322         return ftello64(context->hidden.stdio.fp);
   323     }
   324 #elif defined(HAVE_FSEEKO)
   325     if (fseeko(context->hidden.stdio.fp, (off_t)offset, whence) == 0) {
   326         return ftello(context->hidden.stdio.fp);
   327     }
   328 #elif defined(HAVE__FSEEKI64)
   329     if (_fseeki64(context->hidden.stdio.fp, offset, whence) == 0) {
   330         return _ftelli64(context->hidden.stdio.fp);
   331     }
   332 #else
   333     if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
   334         return (ftell(context->hidden.stdio.fp));
   335     }
   336 #endif
   337     SDL_Error(SDL_EFSEEK);
   338     return (-1);
   339 }
   340 
   341 static size_t SDLCALL
   342 stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   343 {
   344     size_t nread;
   345 
   346     nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
   347     if (nread == 0 && ferror(context->hidden.stdio.fp)) {
   348         SDL_Error(SDL_EFREAD);
   349     }
   350     return (nread);
   351 }
   352 
   353 static size_t SDLCALL
   354 stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   355 {
   356     size_t nwrote;
   357 
   358     nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   359     if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
   360         SDL_Error(SDL_EFWRITE);
   361     }
   362     return (nwrote);
   363 }
   364 
   365 static int SDLCALL
   366 stdio_close(SDL_RWops * context)
   367 {
   368     int status = 0;
   369     if (context) {
   370         if (context->hidden.stdio.autoclose) {
   371             /* WARNING:  Check the return value here! */
   372             if (fclose(context->hidden.stdio.fp) != 0) {
   373                 SDL_Error(SDL_EFWRITE);
   374                 status = -1;
   375             }
   376         }
   377         SDL_FreeRW(context);
   378     }
   379     return status;
   380 }
   381 #endif /* !HAVE_STDIO_H */
   382 
   383 /* Functions to read/write memory pointers */
   384 
   385 static Sint64 SDLCALL
   386 mem_size(SDL_RWops * context)
   387 {
   388     return (Sint64)(context->hidden.mem.stop - context->hidden.mem.base);
   389 }
   390 
   391 static Sint64 SDLCALL
   392 mem_seek(SDL_RWops * context, Sint64 offset, int whence)
   393 {
   394     Uint8 *newpos;
   395 
   396     switch (whence) {
   397     case RW_SEEK_SET:
   398         newpos = context->hidden.mem.base + offset;
   399         break;
   400     case RW_SEEK_CUR:
   401         newpos = context->hidden.mem.here + offset;
   402         break;
   403     case RW_SEEK_END:
   404         newpos = context->hidden.mem.stop + offset;
   405         break;
   406     default:
   407         SDL_SetError("Unknown value for 'whence'");
   408         return (-1);
   409     }
   410     if (newpos < context->hidden.mem.base) {
   411         newpos = context->hidden.mem.base;
   412     }
   413     if (newpos > context->hidden.mem.stop) {
   414         newpos = context->hidden.mem.stop;
   415     }
   416     context->hidden.mem.here = newpos;
   417     return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
   418 }
   419 
   420 static size_t SDLCALL
   421 mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   422 {
   423     size_t total_bytes;
   424     size_t mem_available;
   425 
   426     total_bytes = (maxnum * size);
   427     if ((maxnum <= 0) || (size <= 0)
   428         || ((total_bytes / maxnum) != (size_t) size)) {
   429         return 0;
   430     }
   431 
   432     mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   433     if (total_bytes > mem_available) {
   434         total_bytes = mem_available;
   435     }
   436 
   437     SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   438     context->hidden.mem.here += total_bytes;
   439 
   440     return (total_bytes / size);
   441 }
   442 
   443 static size_t SDLCALL
   444 mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   445 {
   446     if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
   447         num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
   448     }
   449     SDL_memcpy(context->hidden.mem.here, ptr, num * size);
   450     context->hidden.mem.here += num * size;
   451     return (num);
   452 }
   453 
   454 static size_t SDLCALL
   455 mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   456 {
   457     SDL_SetError("Can't write to read-only memory");
   458     return (-1);
   459 }
   460 
   461 static int SDLCALL
   462 mem_close(SDL_RWops * context)
   463 {
   464     if (context) {
   465         SDL_FreeRW(context);
   466     }
   467     return (0);
   468 }
   469 
   470 
   471 /* Functions to create SDL_RWops structures from various data sources */
   472 
   473 SDL_RWops *
   474 SDL_RWFromFile(const char *file, const char *mode)
   475 {
   476     SDL_RWops *rwops = NULL;
   477     if (!file || !*file || !mode || !*mode) {
   478         SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
   479         return NULL;
   480     }
   481 #if defined(ANDROID)
   482 #ifdef HAVE_STDIO_H
   483     /* Try to open the file on the filesystem first */
   484     if (*file == '/') {
   485         FILE *fp = fopen(file, mode);
   486         if (fp) {
   487             return SDL_RWFromFP(fp, 1);
   488         }
   489     } else {
   490         /* Try opening it from internal storage if it's a relative path */
   491         char *path;
   492         FILE *fp;
   493 
   494         path = SDL_stack_alloc(char, PATH_MAX);
   495         if (path) {
   496             SDL_snprintf(path, PATH_MAX, "%s/%s",
   497                          SDL_AndroidGetInternalStoragePath(), file);
   498             fp = fopen(path, mode);
   499             SDL_stack_free(path);
   500             if (fp) {
   501                 return SDL_RWFromFP(fp, 1);
   502             }
   503         }
   504     }
   505 #endif /* HAVE_STDIO_H */
   506 
   507     /* Try to open the file from the asset system */
   508     rwops = SDL_AllocRW();
   509     if (!rwops)
   510         return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   511     if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
   512         SDL_FreeRW(rwops);
   513         return NULL;
   514     }
   515     rwops->size = Android_JNI_FileSize;
   516     rwops->seek = Android_JNI_FileSeek;
   517     rwops->read = Android_JNI_FileRead;
   518     rwops->write = Android_JNI_FileWrite;
   519     rwops->close = Android_JNI_FileClose;
   520 
   521 #elif defined(__WIN32__)
   522     rwops = SDL_AllocRW();
   523     if (!rwops)
   524         return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   525     if (windows_file_open(rwops, file, mode) < 0) {
   526         SDL_FreeRW(rwops);
   527         return NULL;
   528     }
   529     rwops->size = windows_file_size;
   530     rwops->seek = windows_file_seek;
   531     rwops->read = windows_file_read;
   532     rwops->write = windows_file_write;
   533     rwops->close = windows_file_close;
   534 
   535 #elif HAVE_STDIO_H
   536     {
   537     	#ifdef __APPLE__
   538     	FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
   539         #elif __WINRT__
   540         FILE *fp = NULL;
   541         fopen_s(&fp, file, mode);
   542         #else
   543     	FILE *fp = fopen(file, mode);
   544     	#endif
   545     	if (fp == NULL) {
   546             SDL_SetError("Couldn't open %s", file);
   547         } else {
   548             rwops = SDL_RWFromFP(fp, 1);
   549         }
   550     }
   551 #else
   552     SDL_SetError("SDL not compiled with stdio support");
   553 #endif /* !HAVE_STDIO_H */
   554 
   555     return (rwops);
   556 }
   557 
   558 #ifdef HAVE_STDIO_H
   559 SDL_RWops *
   560 SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
   561 {
   562     SDL_RWops *rwops = NULL;
   563 
   564 #if 0
   565 /*#ifdef __NDS__*/
   566     /* set it up so we can use stdio file function */
   567     fatInitDefault();
   568     printf("called fatInitDefault()");
   569 #endif /* __NDS__ */
   570 
   571     rwops = SDL_AllocRW();
   572     if (rwops != NULL) {
   573         rwops->size = stdio_size;
   574         rwops->seek = stdio_seek;
   575         rwops->read = stdio_read;
   576         rwops->write = stdio_write;
   577         rwops->close = stdio_close;
   578         rwops->hidden.stdio.fp = fp;
   579         rwops->hidden.stdio.autoclose = autoclose;
   580     }
   581     return (rwops);
   582 }
   583 #else
   584 SDL_RWops *
   585 SDL_RWFromFP(void * fp, SDL_bool autoclose)
   586 {
   587     SDL_SetError("SDL not compiled with stdio support");
   588     return NULL;
   589 }
   590 #endif /* HAVE_STDIO_H */
   591 
   592 SDL_RWops *
   593 SDL_RWFromMem(void *mem, int size)
   594 {
   595     SDL_RWops *rwops;
   596 
   597     rwops = SDL_AllocRW();
   598     if (rwops != NULL) {
   599         rwops->size = mem_size;
   600         rwops->seek = mem_seek;
   601         rwops->read = mem_read;
   602         rwops->write = mem_write;
   603         rwops->close = mem_close;
   604         rwops->hidden.mem.base = (Uint8 *) mem;
   605         rwops->hidden.mem.here = rwops->hidden.mem.base;
   606         rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   607     }
   608     return (rwops);
   609 }
   610 
   611 SDL_RWops *
   612 SDL_RWFromConstMem(const void *mem, int size)
   613 {
   614     SDL_RWops *rwops;
   615 
   616     rwops = SDL_AllocRW();
   617     if (rwops != NULL) {
   618         rwops->size = mem_size;
   619         rwops->seek = mem_seek;
   620         rwops->read = mem_read;
   621         rwops->write = mem_writeconst;
   622         rwops->close = mem_close;
   623         rwops->hidden.mem.base = (Uint8 *) mem;
   624         rwops->hidden.mem.here = rwops->hidden.mem.base;
   625         rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   626     }
   627     return (rwops);
   628 }
   629 
   630 SDL_RWops *
   631 SDL_AllocRW(void)
   632 {
   633     SDL_RWops *area;
   634 
   635     area = (SDL_RWops *) SDL_malloc(sizeof *area);
   636     if (area == NULL) {
   637         SDL_OutOfMemory();
   638     }
   639     return (area);
   640 }
   641 
   642 void
   643 SDL_FreeRW(SDL_RWops * area)
   644 {
   645     SDL_free(area);
   646 }
   647 
   648 /* Functions for dynamically reading and writing endian-specific values */
   649 
   650 Uint8
   651 SDL_ReadU8(SDL_RWops * src)
   652 {
   653     Uint8 value = 0;
   654 
   655     SDL_RWread(src, &value, (sizeof value), 1);
   656     return value;
   657 }
   658 
   659 Uint16
   660 SDL_ReadLE16(SDL_RWops * src)
   661 {
   662     Uint16 value = 0;
   663 
   664     SDL_RWread(src, &value, (sizeof value), 1);
   665     return (SDL_SwapLE16(value));
   666 }
   667 
   668 Uint16
   669 SDL_ReadBE16(SDL_RWops * src)
   670 {
   671     Uint16 value = 0;
   672 
   673     SDL_RWread(src, &value, (sizeof value), 1);
   674     return (SDL_SwapBE16(value));
   675 }
   676 
   677 Uint32
   678 SDL_ReadLE32(SDL_RWops * src)
   679 {
   680     Uint32 value = 0;
   681 
   682     SDL_RWread(src, &value, (sizeof value), 1);
   683     return (SDL_SwapLE32(value));
   684 }
   685 
   686 Uint32
   687 SDL_ReadBE32(SDL_RWops * src)
   688 {
   689     Uint32 value = 0;
   690 
   691     SDL_RWread(src, &value, (sizeof value), 1);
   692     return (SDL_SwapBE32(value));
   693 }
   694 
   695 Uint64
   696 SDL_ReadLE64(SDL_RWops * src)
   697 {
   698     Uint64 value = 0;
   699 
   700     SDL_RWread(src, &value, (sizeof value), 1);
   701     return (SDL_SwapLE64(value));
   702 }
   703 
   704 Uint64
   705 SDL_ReadBE64(SDL_RWops * src)
   706 {
   707     Uint64 value = 0;
   708 
   709     SDL_RWread(src, &value, (sizeof value), 1);
   710     return (SDL_SwapBE64(value));
   711 }
   712 
   713 size_t
   714 SDL_WriteU8(SDL_RWops * dst, Uint8 value)
   715 {
   716     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   717 }
   718 
   719 size_t
   720 SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
   721 {
   722     value = SDL_SwapLE16(value);
   723     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   724 }
   725 
   726 size_t
   727 SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
   728 {
   729     value = SDL_SwapBE16(value);
   730     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   731 }
   732 
   733 size_t
   734 SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
   735 {
   736     value = SDL_SwapLE32(value);
   737     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   738 }
   739 
   740 size_t
   741 SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
   742 {
   743     value = SDL_SwapBE32(value);
   744     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   745 }
   746 
   747 size_t
   748 SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
   749 {
   750     value = SDL_SwapLE64(value);
   751     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   752 }
   753 
   754 size_t
   755 SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
   756 {
   757     value = SDL_SwapBE64(value);
   758     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   759 }
   760 
   761 /* vi: set ts=4 sw=4 expandtab: */