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