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