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