src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Nov 2012 23:44:13 -0800
changeset 6655 6a3e741ea70d
parent 6644 eacc8b228cef
child 6885 700f1b25f77f
child 8340 e470405f3ae9
permissions -rw-r--r--
Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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         #else
   536     	FILE *fp = fopen(file, mode);
   537     	#endif
   538     	if (fp == NULL) {
   539             SDL_SetError("Couldn't open %s", file);
   540         } else {
   541             rwops = SDL_RWFromFP(fp, 1);
   542         }
   543     }
   544 #else
   545     SDL_SetError("SDL not compiled with stdio support");
   546 #endif /* !HAVE_STDIO_H */
   547 
   548     return (rwops);
   549 }
   550 
   551 #ifdef HAVE_STDIO_H
   552 SDL_RWops *
   553 SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
   554 {
   555     SDL_RWops *rwops = NULL;
   556 
   557 #if 0
   558 /*#ifdef __NDS__*/
   559     /* set it up so we can use stdio file function */
   560     fatInitDefault();
   561     printf("called fatInitDefault()");
   562 #endif /* __NDS__ */
   563 
   564     rwops = SDL_AllocRW();
   565     if (rwops != NULL) {
   566         rwops->size = stdio_size;
   567         rwops->seek = stdio_seek;
   568         rwops->read = stdio_read;
   569         rwops->write = stdio_write;
   570         rwops->close = stdio_close;
   571         rwops->hidden.stdio.fp = fp;
   572         rwops->hidden.stdio.autoclose = autoclose;
   573     }
   574     return (rwops);
   575 }
   576 #else
   577 SDL_RWops *
   578 SDL_RWFromFP(void * fp, SDL_bool autoclose)
   579 {
   580     SDL_SetError("SDL not compiled with stdio support");
   581     return NULL;
   582 }
   583 #endif /* HAVE_STDIO_H */
   584 
   585 SDL_RWops *
   586 SDL_RWFromMem(void *mem, int size)
   587 {
   588     SDL_RWops *rwops;
   589 
   590     rwops = SDL_AllocRW();
   591     if (rwops != NULL) {
   592         rwops->size = mem_size;
   593         rwops->seek = mem_seek;
   594         rwops->read = mem_read;
   595         rwops->write = mem_write;
   596         rwops->close = mem_close;
   597         rwops->hidden.mem.base = (Uint8 *) mem;
   598         rwops->hidden.mem.here = rwops->hidden.mem.base;
   599         rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   600     }
   601     return (rwops);
   602 }
   603 
   604 SDL_RWops *
   605 SDL_RWFromConstMem(const void *mem, int size)
   606 {
   607     SDL_RWops *rwops;
   608 
   609     rwops = SDL_AllocRW();
   610     if (rwops != NULL) {
   611         rwops->size = mem_size;
   612         rwops->seek = mem_seek;
   613         rwops->read = mem_read;
   614         rwops->write = mem_writeconst;
   615         rwops->close = mem_close;
   616         rwops->hidden.mem.base = (Uint8 *) mem;
   617         rwops->hidden.mem.here = rwops->hidden.mem.base;
   618         rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   619     }
   620     return (rwops);
   621 }
   622 
   623 SDL_RWops *
   624 SDL_AllocRW(void)
   625 {
   626     SDL_RWops *area;
   627 
   628     area = (SDL_RWops *) SDL_malloc(sizeof *area);
   629     if (area == NULL) {
   630         SDL_OutOfMemory();
   631     }
   632     return (area);
   633 }
   634 
   635 void
   636 SDL_FreeRW(SDL_RWops * area)
   637 {
   638     SDL_free(area);
   639 }
   640 
   641 /* Functions for dynamically reading and writing endian-specific values */
   642 
   643 Uint8
   644 SDL_ReadU8(SDL_RWops * src)
   645 {
   646     Uint8 value = 0;
   647 
   648     SDL_RWread(src, &value, (sizeof value), 1);
   649     return value;
   650 }
   651 
   652 Uint16
   653 SDL_ReadLE16(SDL_RWops * src)
   654 {
   655     Uint16 value = 0;
   656 
   657     SDL_RWread(src, &value, (sizeof value), 1);
   658     return (SDL_SwapLE16(value));
   659 }
   660 
   661 Uint16
   662 SDL_ReadBE16(SDL_RWops * src)
   663 {
   664     Uint16 value = 0;
   665 
   666     SDL_RWread(src, &value, (sizeof value), 1);
   667     return (SDL_SwapBE16(value));
   668 }
   669 
   670 Uint32
   671 SDL_ReadLE32(SDL_RWops * src)
   672 {
   673     Uint32 value = 0;
   674 
   675     SDL_RWread(src, &value, (sizeof value), 1);
   676     return (SDL_SwapLE32(value));
   677 }
   678 
   679 Uint32
   680 SDL_ReadBE32(SDL_RWops * src)
   681 {
   682     Uint32 value = 0;
   683 
   684     SDL_RWread(src, &value, (sizeof value), 1);
   685     return (SDL_SwapBE32(value));
   686 }
   687 
   688 Uint64
   689 SDL_ReadLE64(SDL_RWops * src)
   690 {
   691     Uint64 value = 0;
   692 
   693     SDL_RWread(src, &value, (sizeof value), 1);
   694     return (SDL_SwapLE64(value));
   695 }
   696 
   697 Uint64
   698 SDL_ReadBE64(SDL_RWops * src)
   699 {
   700     Uint64 value = 0;
   701 
   702     SDL_RWread(src, &value, (sizeof value), 1);
   703     return (SDL_SwapBE64(value));
   704 }
   705 
   706 size_t
   707 SDL_WriteU8(SDL_RWops * dst, Uint8 value)
   708 {
   709     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   710 }
   711 
   712 size_t
   713 SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
   714 {
   715     value = SDL_SwapLE16(value);
   716     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   717 }
   718 
   719 size_t
   720 SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
   721 {
   722     value = SDL_SwapBE16(value);
   723     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   724 }
   725 
   726 size_t
   727 SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
   728 {
   729     value = SDL_SwapLE32(value);
   730     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   731 }
   732 
   733 size_t
   734 SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
   735 {
   736     value = SDL_SwapBE32(value);
   737     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   738 }
   739 
   740 size_t
   741 SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
   742 {
   743     value = SDL_SwapLE64(value);
   744     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   745 }
   746 
   747 size_t
   748 SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
   749 {
   750     value = SDL_SwapBE64(value);
   751     return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   752 }
   753 
   754 /* vi: set ts=4 sw=4 expandtab: */