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