src/file/SDL_rwops.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 02 Jun 2013 14:13:21 +0200
changeset 7262 3c5f6170df8e
parent 7191 75360622e65f
child 7719 31b5f9ff36ca
child 8477 ad08f0d710f3
permissions -rw-r--r--
Fixed implicit function declaration for SDL_AndroidGetInternalStoragePath().
     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 #include "SDL_system.h"
    39 #endif
    40 
    41 #ifdef __WIN32__
    42 
    43 /* Functions to read/write Win32 API file pointers */
    44 
    45 #include "../core/windows/SDL_windows.h"
    46 
    47 #ifndef INVALID_SET_FILE_POINTER
    48 #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
    49 #endif
    50 
    51 #define READAHEAD_BUFFER_SIZE   1024
    52 
    53 static int SDLCALL
    54 windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
    55 {
    56     UINT old_error_mode;
    57     HANDLE h;
    58     DWORD r_right, w_right;
    59     DWORD must_exist, truncate;
    60     int a_mode;
    61 
    62     if (!context)
    63         return -1;              /* failed (invalid call) */
    64 
    65     context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* mark this as unusable */
    66     context->hidden.windowsio.buffer.data = NULL;
    67     context->hidden.windowsio.buffer.size = 0;
    68     context->hidden.windowsio.buffer.left = 0;
    69 
    70     /* "r" = reading, file must exist */
    71     /* "w" = writing, truncate existing, file may not exist */
    72     /* "r+"= reading or writing, file must exist            */
    73     /* "a" = writing, append file may not exist             */
    74     /* "a+"= append + read, file may not exist              */
    75     /* "w+" = read, write, truncate. file may not exist    */
    76 
    77     must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
    78     truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
    79     r_right = (SDL_strchr(mode, '+') != NULL
    80                || must_exist) ? GENERIC_READ : 0;
    81     a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
    82     w_right = (a_mode || SDL_strchr(mode, '+')
    83                || truncate) ? GENERIC_WRITE : 0;
    84 
    85     if (!r_right && !w_right)   /* inconsistent mode */
    86         return -1;              /* failed (invalid call) */
    87 
    88     context->hidden.windowsio.buffer.data =
    89         (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
    90     if (!context->hidden.windowsio.buffer.data) {
    91         return SDL_OutOfMemory();
    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         return SDL_SetError("windows_file_size: invalid context/file not opened");
   128     }
   129 
   130     if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) {
   131         return WIN_SetError("windows_file_size");
   132     }
   133 
   134     return size.QuadPart;
   135 }
   136 
   137 static Sint64 SDLCALL
   138 windows_file_seek(SDL_RWops * context, Sint64 offset, int whence)
   139 {
   140     DWORD windowswhence;
   141     LARGE_INTEGER windowsoffset;
   142 
   143     if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
   144         return SDL_SetError("windows_file_seek: invalid context/file not opened");
   145     }
   146 
   147     /* FIXME: We may be able to satisfy the seek within buffered data */
   148     if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
   149         offset -= (long)context->hidden.windowsio.buffer.left;
   150     }
   151     context->hidden.windowsio.buffer.left = 0;
   152 
   153     switch (whence) {
   154     case RW_SEEK_SET:
   155         windowswhence = FILE_BEGIN;
   156         break;
   157     case RW_SEEK_CUR:
   158         windowswhence = FILE_CURRENT;
   159         break;
   160     case RW_SEEK_END:
   161         windowswhence = FILE_END;
   162         break;
   163     default:
   164         return SDL_SetError("windows_file_seek: Unknown value for 'whence'");
   165     }
   166 
   167     windowsoffset.QuadPart = offset;
   168     if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) {
   169         return WIN_SetError("windows_file_seek");
   170     }
   171     return windowsoffset.QuadPart;
   172 }
   173 
   174 static size_t SDLCALL
   175 windows_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   176 {
   177     size_t total_need;
   178     size_t total_read = 0;
   179     size_t read_ahead;
   180     DWORD byte_read;
   181 
   182     total_need = size * maxnum;
   183 
   184     if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
   185         || !total_need)
   186         return 0;
   187 
   188     if (context->hidden.windowsio.buffer.left > 0) {
   189         void *data = (char *) context->hidden.windowsio.buffer.data +
   190             context->hidden.windowsio.buffer.size -
   191             context->hidden.windowsio.buffer.left;
   192         read_ahead =
   193             SDL_min(total_need, context->hidden.windowsio.buffer.left);
   194         SDL_memcpy(ptr, data, read_ahead);
   195         context->hidden.windowsio.buffer.left -= read_ahead;
   196 
   197         if (read_ahead == total_need) {
   198             return maxnum;
   199         }
   200         ptr = (char *) ptr + read_ahead;
   201         total_need -= read_ahead;
   202         total_read += read_ahead;
   203     }
   204 
   205     if (total_need < READAHEAD_BUFFER_SIZE) {
   206         if (!ReadFile
   207             (context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
   208              READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
   209             SDL_Error(SDL_EFREAD);
   210             return 0;
   211         }
   212         read_ahead = SDL_min(total_need, (int) byte_read);
   213         SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
   214         context->hidden.windowsio.buffer.size = byte_read;
   215         context->hidden.windowsio.buffer.left = byte_read - read_ahead;
   216         total_read += read_ahead;
   217     } else {
   218         if (!ReadFile
   219             (context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
   220             SDL_Error(SDL_EFREAD);
   221             return 0;
   222         }
   223         total_read += byte_read;
   224     }
   225     return (total_read / size);
   226 }
   227 
   228 static size_t SDLCALL
   229 windows_file_write(SDL_RWops * context, const void *ptr, size_t size,
   230                  size_t num)
   231 {
   232 
   233     size_t total_bytes;
   234     DWORD byte_written;
   235     size_t nwritten;
   236 
   237     total_bytes = size * num;
   238 
   239     if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
   240         || total_bytes <= 0 || !size)
   241         return 0;
   242 
   243     if (context->hidden.windowsio.buffer.left) {
   244         SetFilePointer(context->hidden.windowsio.h,
   245                        -(LONG)context->hidden.windowsio.buffer.left, NULL,
   246                        FILE_CURRENT);
   247         context->hidden.windowsio.buffer.left = 0;
   248     }
   249 
   250     /* if in append mode, we must go to the EOF before write */
   251     if (context->hidden.windowsio.append) {
   252         if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
   253             INVALID_SET_FILE_POINTER) {
   254             SDL_Error(SDL_EFWRITE);
   255             return 0;
   256         }
   257     }
   258 
   259     if (!WriteFile
   260         (context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
   261         SDL_Error(SDL_EFWRITE);
   262         return 0;
   263     }
   264 
   265     nwritten = byte_written / size;
   266     return nwritten;
   267 }
   268 
   269 static int SDLCALL
   270 windows_file_close(SDL_RWops * context)
   271 {
   272 
   273     if (context) {
   274         if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
   275             CloseHandle(context->hidden.windowsio.h);
   276             context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* to be sure */
   277         }
   278         if (context->hidden.windowsio.buffer.data) {
   279             SDL_free(context->hidden.windowsio.buffer.data);
   280             context->hidden.windowsio.buffer.data = NULL;
   281         }
   282         SDL_FreeRW(context);
   283     }
   284     return (0);
   285 }
   286 #endif /* __WIN32__ */
   287 
   288 #ifdef HAVE_STDIO_H
   289 
   290 /* Functions to read/write stdio file pointers */
   291 
   292 static Sint64 SDLCALL
   293 stdio_size(SDL_RWops * context)
   294 {
   295     Sint64 pos, size;
   296 
   297     pos = SDL_RWseek(context, 0, RW_SEEK_CUR);
   298     if (pos < 0) {
   299         return -1;
   300     }
   301     size = SDL_RWseek(context, 0, RW_SEEK_END);
   302 
   303     SDL_RWseek(context, pos, RW_SEEK_SET);
   304     return size;
   305 }
   306 
   307 static Sint64 SDLCALL
   308 stdio_seek(SDL_RWops * context, Sint64 offset, int whence)
   309 {
   310 #ifdef HAVE_FSEEKO64
   311     if (fseeko64(context->hidden.stdio.fp, (off64_t)offset, whence) == 0) {
   312         return ftello64(context->hidden.stdio.fp);
   313     }
   314 #elif defined(HAVE_FSEEKO)
   315     if (fseeko(context->hidden.stdio.fp, (off_t)offset, whence) == 0) {
   316         return ftello(context->hidden.stdio.fp);
   317     }
   318 #else
   319     if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
   320         return (ftell(context->hidden.stdio.fp));
   321     }
   322 #endif
   323     return SDL_Error(SDL_EFSEEK);
   324 }
   325 
   326 static size_t SDLCALL
   327 stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   328 {
   329     size_t nread;
   330 
   331     nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
   332     if (nread == 0 && ferror(context->hidden.stdio.fp)) {
   333         SDL_Error(SDL_EFREAD);
   334     }
   335     return (nread);
   336 }
   337 
   338 static size_t SDLCALL
   339 stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   340 {
   341     size_t nwrote;
   342 
   343     nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   344     if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
   345         SDL_Error(SDL_EFWRITE);
   346     }
   347     return (nwrote);
   348 }
   349 
   350 static int SDLCALL
   351 stdio_close(SDL_RWops * context)
   352 {
   353     int status = 0;
   354     if (context) {
   355         if (context->hidden.stdio.autoclose) {
   356             /* WARNING:  Check the return value here! */
   357             if (fclose(context->hidden.stdio.fp) != 0) {
   358                 status = SDL_Error(SDL_EFWRITE);
   359             }
   360         }
   361         SDL_FreeRW(context);
   362     }
   363     return status;
   364 }
   365 #endif /* !HAVE_STDIO_H */
   366 
   367 /* Functions to read/write memory pointers */
   368 
   369 static Sint64 SDLCALL
   370 mem_size(SDL_RWops * context)
   371 {
   372     return (Sint64)(context->hidden.mem.stop - context->hidden.mem.base);
   373 }
   374 
   375 static Sint64 SDLCALL
   376 mem_seek(SDL_RWops * context, Sint64 offset, int whence)
   377 {
   378     Uint8 *newpos;
   379 
   380     switch (whence) {
   381     case RW_SEEK_SET:
   382         newpos = context->hidden.mem.base + offset;
   383         break;
   384     case RW_SEEK_CUR:
   385         newpos = context->hidden.mem.here + offset;
   386         break;
   387     case RW_SEEK_END:
   388         newpos = context->hidden.mem.stop + offset;
   389         break;
   390     default:
   391         return SDL_SetError("Unknown value for 'whence'");
   392     }
   393     if (newpos < context->hidden.mem.base) {
   394         newpos = context->hidden.mem.base;
   395     }
   396     if (newpos > context->hidden.mem.stop) {
   397         newpos = context->hidden.mem.stop;
   398     }
   399     context->hidden.mem.here = newpos;
   400     return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
   401 }
   402 
   403 static size_t SDLCALL
   404 mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   405 {
   406     size_t total_bytes;
   407     size_t mem_available;
   408 
   409     total_bytes = (maxnum * size);
   410     if ((maxnum <= 0) || (size <= 0)
   411         || ((total_bytes / maxnum) != (size_t) size)) {
   412         return 0;
   413     }
   414 
   415     mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   416     if (total_bytes > mem_available) {
   417         total_bytes = mem_available;
   418     }
   419 
   420     SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   421     context->hidden.mem.here += total_bytes;
   422 
   423     return (total_bytes / size);
   424 }
   425 
   426 static size_t SDLCALL
   427 mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   428 {
   429     if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
   430         num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
   431     }
   432     SDL_memcpy(context->hidden.mem.here, ptr, num * size);
   433     context->hidden.mem.here += num * size;
   434     return (num);
   435 }
   436 
   437 static size_t SDLCALL
   438 mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   439 {
   440     SDL_SetError("Can't write to read-only memory");
   441     return (0);
   442 }
   443 
   444 static int SDLCALL
   445 mem_close(SDL_RWops * context)
   446 {
   447     if (context) {
   448         SDL_FreeRW(context);
   449     }
   450     return (0);
   451 }
   452 
   453 
   454 /* Functions to create SDL_RWops structures from various data sources */
   455 
   456 SDL_RWops *
   457 SDL_RWFromFile(const char *file, const char *mode)
   458 {
   459     SDL_RWops *rwops = NULL;
   460     if (!file || !*file || !mode || !*mode) {
   461         SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
   462         return NULL;
   463     }
   464 #if defined(ANDROID)
   465 #ifdef HAVE_STDIO_H
   466     /* Try to open the file on the filesystem first */
   467     if (*file == '/') {
   468         FILE *fp = fopen(file, mode);
   469         if (fp) {
   470             return SDL_RWFromFP(fp, 1);
   471         }
   472     } else {
   473         /* Try opening it from internal storage if it's a relative path */
   474         char *path;
   475         FILE *fp;
   476 
   477         path = SDL_stack_alloc(char, PATH_MAX);
   478         if (path) {
   479             SDL_snprintf(path, PATH_MAX, "%s/%s",
   480                          SDL_AndroidGetInternalStoragePath(), file);
   481             fp = fopen(path, mode);
   482             SDL_stack_free(path);
   483             if (fp) {
   484                 return SDL_RWFromFP(fp, 1);
   485             }
   486         }
   487     }
   488 #endif /* HAVE_STDIO_H */
   489 
   490     /* Try to open the file from the asset system */
   491     rwops = SDL_AllocRW();
   492     if (!rwops)
   493         return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   494     if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
   495         SDL_FreeRW(rwops);
   496         return NULL;
   497     }
   498     rwops->size = Android_JNI_FileSize;
   499     rwops->seek = Android_JNI_FileSeek;
   500     rwops->read = Android_JNI_FileRead;
   501     rwops->write = Android_JNI_FileWrite;
   502     rwops->close = Android_JNI_FileClose;
   503     rwops->type = SDL_RWOPS_JNIFILE;
   504 
   505 #elif defined(__WIN32__)
   506     rwops = SDL_AllocRW();
   507     if (!rwops)
   508         return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   509     if (windows_file_open(rwops, file, mode) < 0) {
   510         SDL_FreeRW(rwops);
   511         return NULL;
   512     }
   513     rwops->size = windows_file_size;
   514     rwops->seek = windows_file_seek;
   515     rwops->read = windows_file_read;
   516     rwops->write = windows_file_write;
   517     rwops->close = windows_file_close;
   518     rwops->type = SDL_RWOPS_WINFILE;
   519 
   520 #elif HAVE_STDIO_H
   521     {
   522         #ifdef __APPLE__
   523         FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
   524         #else
   525         FILE *fp = fopen(file, mode);
   526         #endif
   527         if (fp == NULL) {
   528             SDL_SetError("Couldn't open %s", file);
   529         } else {
   530             rwops = SDL_RWFromFP(fp, 1);
   531         }
   532     }
   533 #else
   534     SDL_SetError("SDL not compiled with stdio support");
   535 #endif /* !HAVE_STDIO_H */
   536 
   537     return (rwops);
   538 }
   539 
   540 #ifdef HAVE_STDIO_H
   541 SDL_RWops *
   542 SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
   543 {
   544     SDL_RWops *rwops = NULL;
   545 
   546     rwops = SDL_AllocRW();
   547     if (rwops != NULL) {
   548         rwops->size = stdio_size;
   549         rwops->seek = stdio_seek;
   550         rwops->read = stdio_read;
   551         rwops->write = stdio_write;
   552         rwops->close = stdio_close;
   553         rwops->hidden.stdio.fp = fp;
   554         rwops->hidden.stdio.autoclose = autoclose;
   555         rwops->type = SDL_RWOPS_STDFILE;
   556     }
   557     return (rwops);
   558 }
   559 #else
   560 SDL_RWops *
   561 SDL_RWFromFP(void * fp, SDL_bool autoclose)
   562 {
   563     SDL_SetError("SDL not compiled with stdio support");
   564     return NULL;
   565 }
   566 #endif /* HAVE_STDIO_H */
   567 
   568 SDL_RWops *
   569 SDL_RWFromMem(void *mem, int size)
   570 {
   571     SDL_RWops *rwops = NULL;
   572     if (!mem) {
   573       SDL_InvalidParamError("mem");
   574       return (rwops);
   575     }
   576     if (!size) {
   577       SDL_InvalidParamError("size");
   578       return (rwops);
   579     }
   580 
   581     rwops = SDL_AllocRW();
   582     if (rwops != NULL) {
   583         rwops->size = mem_size;
   584         rwops->seek = mem_seek;
   585         rwops->read = mem_read;
   586         rwops->write = mem_write;
   587         rwops->close = mem_close;
   588         rwops->hidden.mem.base = (Uint8 *) mem;
   589         rwops->hidden.mem.here = rwops->hidden.mem.base;
   590         rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   591         rwops->type = SDL_RWOPS_MEMORY;
   592     }
   593     return (rwops);
   594 }
   595 
   596 SDL_RWops *
   597 SDL_RWFromConstMem(const void *mem, int size)
   598 {
   599     SDL_RWops *rwops = NULL;
   600     if (!mem) {
   601       SDL_InvalidParamError("mem");
   602       return (rwops);
   603     }
   604     if (!size) {
   605       SDL_InvalidParamError("size");
   606       return (rwops);
   607     }
   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         rwops->type = SDL_RWOPS_MEMORY_RO;
   620     }
   621     return (rwops);
   622 }
   623 
   624 SDL_RWops *
   625 SDL_AllocRW(void)
   626 {
   627     SDL_RWops *area;
   628 
   629     area = (SDL_RWops *) SDL_malloc(sizeof *area);
   630     if (area == NULL) {
   631         SDL_OutOfMemory();
   632     } else {
   633         area->type = SDL_RWOPS_UNKNOWN;
   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: */