src/file/SDL_rwops.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 7828 1451063c8ecd
child 8149 681eb46b8ac4
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

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