src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 8162 b6083d2078c0
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

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