src/file/SDL_rwops.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 08 Sep 2014 01:36:22 -0400
changeset 9126 08f3b56969b1
parent 8850 7e248a233387
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Deal with various .bmp file format variants in SDL_LoadBMP_RW().

This helps when modern versions of The Gimp (and lots of other things)
produces a 32-bit bitmap with an alpha channel, or anything with "BI_BITFIELDS"
format, since that data is now embedded in the bitmap info header instead of
directly following it and we would accidentally skip over embedded versions of
it.

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