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