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