src/file/SDL_rwops.c
author David Ludwig <dludwig@pobox.com>
Sat, 23 Feb 2013 20:01:46 -0500
changeset 8439 14d94a8a9fb6
parent 8340 e470405f3ae9
parent 6885 700f1b25f77f
child 8440 29e693ff8706
permissions -rw-r--r--
WinRT: merged with latest, official, SDL 2.x code
     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 #else
   329     if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
   330         return (ftell(context->hidden.stdio.fp));
   331     }
   332 #endif
   333     SDL_Error(SDL_EFSEEK);
   334     return (-1);
   335 }
   336 
   337 static size_t SDLCALL
   338 stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   339 {
   340     size_t nread;
   341 
   342     nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
   343     if (nread == 0 && ferror(context->hidden.stdio.fp)) {
   344         SDL_Error(SDL_EFREAD);
   345     }
   346     return (nread);
   347 }
   348 
   349 static size_t SDLCALL
   350 stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   351 {
   352     size_t nwrote;
   353 
   354     nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   355     if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
   356         SDL_Error(SDL_EFWRITE);
   357     }
   358     return (nwrote);
   359 }
   360 
   361 static int SDLCALL
   362 stdio_close(SDL_RWops * context)
   363 {
   364     int status = 0;
   365     if (context) {
   366         if (context->hidden.stdio.autoclose) {
   367             /* WARNING:  Check the return value here! */
   368             if (fclose(context->hidden.stdio.fp) != 0) {
   369                 SDL_Error(SDL_EFWRITE);
   370                 status = -1;
   371             }
   372         }
   373         SDL_FreeRW(context);
   374     }
   375     return status;
   376 }
   377 #endif /* !HAVE_STDIO_H */
   378 
   379 /* Functions to read/write memory pointers */
   380 
   381 static Sint64 SDLCALL
   382 mem_size(SDL_RWops * context)
   383 {
   384     return (Sint64)(context->hidden.mem.stop - context->hidden.mem.base);
   385 }
   386 
   387 static Sint64 SDLCALL
   388 mem_seek(SDL_RWops * context, Sint64 offset, int whence)
   389 {
   390     Uint8 *newpos;
   391 
   392     switch (whence) {
   393     case RW_SEEK_SET:
   394         newpos = context->hidden.mem.base + offset;
   395         break;
   396     case RW_SEEK_CUR:
   397         newpos = context->hidden.mem.here + offset;
   398         break;
   399     case RW_SEEK_END:
   400         newpos = context->hidden.mem.stop + offset;
   401         break;
   402     default:
   403         SDL_SetError("Unknown value for 'whence'");
   404         return (-1);
   405     }
   406     if (newpos < context->hidden.mem.base) {
   407         newpos = context->hidden.mem.base;
   408     }
   409     if (newpos > context->hidden.mem.stop) {
   410         newpos = context->hidden.mem.stop;
   411     }
   412     context->hidden.mem.here = newpos;
   413     return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
   414 }
   415 
   416 static size_t SDLCALL
   417 mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   418 {
   419     size_t total_bytes;
   420     size_t mem_available;
   421 
   422     total_bytes = (maxnum * size);
   423     if ((maxnum <= 0) || (size <= 0)
   424         || ((total_bytes / maxnum) != (size_t) size)) {
   425         return 0;
   426     }
   427 
   428     mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   429     if (total_bytes > mem_available) {
   430         total_bytes = mem_available;
   431     }
   432 
   433     SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   434     context->hidden.mem.here += total_bytes;
   435 
   436     return (total_bytes / size);
   437 }
   438 
   439 static size_t SDLCALL
   440 mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   441 {
   442     if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
   443         num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
   444     }
   445     SDL_memcpy(context->hidden.mem.here, ptr, num * size);
   446     context->hidden.mem.here += num * size;
   447     return (num);
   448 }
   449 
   450 static size_t SDLCALL
   451 mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   452 {
   453     SDL_SetError("Can't write to read-only memory");
   454     return (-1);
   455 }
   456 
   457 static int SDLCALL
   458 mem_close(SDL_RWops * context)
   459 {
   460     if (context) {
   461         SDL_FreeRW(context);
   462     }
   463     return (0);
   464 }
   465 
   466 
   467 /* Functions to create SDL_RWops structures from various data sources */
   468 
   469 SDL_RWops *
   470 SDL_RWFromFile(const char *file, const char *mode)
   471 {
   472     SDL_RWops *rwops = NULL;
   473     if (!file || !*file || !mode || !*mode) {
   474         SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
   475         return NULL;
   476     }
   477 #if defined(ANDROID)
   478 #ifdef HAVE_STDIO_H
   479     /* Try to open the file on the filesystem first */
   480     if (*file == '/') {
   481         FILE *fp = fopen(file, mode);
   482         if (fp) {
   483             return SDL_RWFromFP(fp, 1);
   484         }
   485     } else {
   486         /* Try opening it from internal storage if it's a relative path */
   487         char *path;
   488         FILE *fp;
   489 
   490         path = SDL_stack_alloc(char, PATH_MAX);
   491         if (path) {
   492             SDL_snprintf(path, PATH_MAX, "%s/%s",
   493                          SDL_AndroidGetInternalStoragePath(), file);
   494             fp = fopen(path, mode);
   495             SDL_stack_free(path);
   496             if (fp) {
   497                 return SDL_RWFromFP(fp, 1);
   498             }
   499         }
   500     }
   501 #endif /* HAVE_STDIO_H */
   502 
   503     /* Try to open the file from the asset system */
   504     rwops = SDL_AllocRW();
   505     if (!rwops)
   506         return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   507     if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
   508         SDL_FreeRW(rwops);
   509         return NULL;
   510     }
   511     rwops->size = Android_JNI_FileSize;
   512     rwops->seek = Android_JNI_FileSeek;
   513     rwops->read = Android_JNI_FileRead;
   514     rwops->write = Android_JNI_FileWrite;
   515     rwops->close = Android_JNI_FileClose;
   516 
   517 #elif defined(__WIN32__)
   518     rwops = SDL_AllocRW();
   519     if (!rwops)
   520         return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   521     if (windows_file_open(rwops, file, mode) < 0) {
   522         SDL_FreeRW(rwops);
   523         return NULL;
   524     }
   525     rwops->size = windows_file_size;
   526     rwops->seek = windows_file_seek;
   527     rwops->read = windows_file_read;
   528     rwops->write = windows_file_write;
   529     rwops->close = windows_file_close;
   530 
   531 #elif HAVE_STDIO_H
   532     {
   533     	#ifdef __APPLE__
   534     	FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
   535         #elif __WINRT__
   536         FILE *fp = NULL;
   537         fopen_s(&fp, file, mode);
   538         #else
   539     	FILE *fp = fopen(file, mode);
   540     	#endif
   541     	if (fp == NULL) {
   542             SDL_SetError("Couldn't open %s", file);
   543         } else {
   544             rwops = SDL_RWFromFP(fp, 1);
   545         }
   546     }
   547 #else
   548     SDL_SetError("SDL not compiled with stdio support");
   549 #endif /* !HAVE_STDIO_H */
   550 
   551     return (rwops);
   552 }
   553 
   554 #ifdef HAVE_STDIO_H
   555 SDL_RWops *
   556 SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
   557 {
   558     SDL_RWops *rwops = NULL;
   559 
   560 #if 0
   561 /*#ifdef __NDS__*/
   562     /* set it up so we can use stdio file function */
   563     fatInitDefault();
   564     printf("called fatInitDefault()");
   565 #endif /* __NDS__ */
   566 
   567     rwops = SDL_AllocRW();
   568     if (rwops != NULL) {
   569         rwops->size = stdio_size;
   570         rwops->seek = stdio_seek;
   571         rwops->read = stdio_read;
   572         rwops->write = stdio_write;
   573         rwops->close = stdio_close;
   574         rwops->hidden.stdio.fp = fp;
   575         rwops->hidden.stdio.autoclose = autoclose;
   576     }
   577     return (rwops);
   578 }
   579 #else
   580 SDL_RWops *
   581 SDL_RWFromFP(void * fp, SDL_bool autoclose)
   582 {
   583     SDL_SetError("SDL not compiled with stdio support");
   584     return NULL;
   585 }
   586 #endif /* HAVE_STDIO_H */
   587 
   588 SDL_RWops *
   589 SDL_RWFromMem(void *mem, int size)
   590 {
   591     SDL_RWops *rwops;
   592 
   593     rwops = SDL_AllocRW();
   594     if (rwops != NULL) {
   595         rwops->size = mem_size;
   596         rwops->seek = mem_seek;
   597         rwops->read = mem_read;
   598         rwops->write = mem_write;
   599         rwops->close = mem_close;
   600         rwops->hidden.mem.base = (Uint8 *) mem;
   601         rwops->hidden.mem.here = rwops->hidden.mem.base;
   602         rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   603     }
   604     return (rwops);
   605 }
   606 
   607 SDL_RWops *
   608 SDL_RWFromConstMem(const void *mem, int size)
   609 {
   610     SDL_RWops *rwops;
   611 
   612     rwops = SDL_AllocRW();
   613     if (rwops != NULL) {
   614         rwops->size = mem_size;
   615         rwops->seek = mem_seek;
   616         rwops->read = mem_read;
   617         rwops->write = mem_writeconst;
   618         rwops->close = mem_close;
   619         rwops->hidden.mem.base = (Uint8 *) mem;
   620         rwops->hidden.mem.here = rwops->hidden.mem.base;
   621         rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   622     }
   623     return (rwops);
   624 }
   625 
   626 SDL_RWops *
   627 SDL_AllocRW(void)
   628 {
   629     SDL_RWops *area;
   630 
   631     area = (SDL_RWops *) SDL_malloc(sizeof *area);
   632     if (area == NULL) {
   633         SDL_OutOfMemory();
   634     }
   635     return (area);
   636 }
   637 
   638 void
   639 SDL_FreeRW(SDL_RWops * area)
   640 {
   641     SDL_free(area);
   642 }
   643 
   644 /* Functions for dynamically reading and writing endian-specific values */
   645 
   646 Uint8
   647 SDL_ReadU8(SDL_RWops * src)
   648 {
   649     Uint8 value = 0;
   650 
   651     SDL_RWread(src, &value, (sizeof value), 1);
   652     return value;
   653 }
   654 
   655 Uint16
   656 SDL_ReadLE16(SDL_RWops * src)
   657 {
   658     Uint16 value = 0;
   659 
   660     SDL_RWread(src, &value, (sizeof value), 1);
   661     return (SDL_SwapLE16(value));
   662 }
   663 
   664 Uint16
   665 SDL_ReadBE16(SDL_RWops * src)
   666 {
   667     Uint16 value = 0;
   668 
   669     SDL_RWread(src, &value, (sizeof value), 1);
   670     return (SDL_SwapBE16(value));
   671 }
   672 
   673 Uint32
   674 SDL_ReadLE32(SDL_RWops * src)
   675 {
   676     Uint32 value = 0;
   677 
   678     SDL_RWread(src, &value, (sizeof value), 1);
   679     return (SDL_SwapLE32(value));
   680 }
   681 
   682 Uint32
   683 SDL_ReadBE32(SDL_RWops * src)
   684 {
   685     Uint32 value = 0;
   686 
   687     SDL_RWread(src, &value, (sizeof value), 1);
   688     return (SDL_SwapBE32(value));
   689 }
   690 
   691 Uint64
   692 SDL_ReadLE64(SDL_RWops * src)
   693 {
   694     Uint64 value = 0;
   695 
   696     SDL_RWread(src, &value, (sizeof value), 1);
   697     return (SDL_SwapLE64(value));
   698 }
   699 
   700 Uint64
   701 SDL_ReadBE64(SDL_RWops * src)
   702 {
   703     Uint64 value = 0;
   704 
   705     SDL_RWread(src, &value, (sizeof value), 1);
   706     return (SDL_SwapBE64(value));
   707 }
   708 
   709 size_t
   710 SDL_WriteU8(SDL_RWops * dst, Uint8 value)
   711 {
   712     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   713 }
   714 
   715 size_t
   716 SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
   717 {
   718     value = SDL_SwapLE16(value);
   719     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   720 }
   721 
   722 size_t
   723 SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
   724 {
   725     value = SDL_SwapBE16(value);
   726     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   727 }
   728 
   729 size_t
   730 SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
   731 {
   732     value = SDL_SwapLE32(value);
   733     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   734 }
   735 
   736 size_t
   737 SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
   738 {
   739     value = SDL_SwapBE32(value);
   740     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   741 }
   742 
   743 size_t
   744 SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
   745 {
   746     value = SDL_SwapLE64(value);
   747     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   748 }
   749 
   750 size_t
   751 SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
   752 {
   753     value = SDL_SwapBE64(value);
   754     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   755 }
   756 
   757 /* vi: set ts=4 sw=4 expandtab: */