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