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