src/file/SDL_rwops.c
changeset 8582 c3e9a2b93517
parent 8535 e8ee0708ef5c
child 8583 fb2933ca805f
     1.1 --- a/src/file/SDL_rwops.c	Tue Mar 04 19:49:11 2014 -0500
     1.2 +++ b/src/file/SDL_rwops.c	Sun Mar 09 11:06:11 2014 -0700
     1.3 @@ -1,765 +1,765 @@
     1.4 -/*
     1.5 -  Simple DirectMedia Layer
     1.6 -  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 -
     1.8 -  This software is provided 'as-is', without any express or implied
     1.9 -  warranty.  In no event will the authors be held liable for any damages
    1.10 -  arising from the use of this software.
    1.11 -
    1.12 -  Permission is granted to anyone to use this software for any purpose,
    1.13 -  including commercial applications, and to alter it and redistribute it
    1.14 -  freely, subject to the following restrictions:
    1.15 -
    1.16 -  1. The origin of this software must not be misrepresented; you must not
    1.17 -     claim that you wrote the original software. If you use this software
    1.18 -     in a product, an acknowledgment in the product documentation would be
    1.19 -     appreciated but is not required.
    1.20 -  2. Altered source versions must be plainly marked as such, and must not be
    1.21 -     misrepresented as being the original software.
    1.22 -  3. This notice may not be removed or altered from any source distribution.
    1.23 -*/
    1.24 -/* Need this so Linux systems define fseek64o, ftell64o and off64_t */
    1.25 -#define _LARGEFILE64_SOURCE
    1.26 -#include "SDL_config.h"
    1.27 -
    1.28 -#if defined(__WIN32__)
    1.29 -#include "../core/windows/SDL_windows.h"
    1.30 -#endif
    1.31 -
    1.32 -
    1.33 -/* This file provides a general interface for SDL to read and write
    1.34 -   data sources.  It can easily be extended to files, memory, etc.
    1.35 -*/
    1.36 -
    1.37 -#include "SDL_endian.h"
    1.38 -#include "SDL_rwops.h"
    1.39 -
    1.40 -#ifdef __APPLE__
    1.41 -#include "cocoa/SDL_rwopsbundlesupport.h"
    1.42 -#endif /* __APPLE__ */
    1.43 -
    1.44 -#ifdef ANDROID
    1.45 -#include "../core/android/SDL_android.h"
    1.46 -#include "SDL_system.h"
    1.47 -#endif
    1.48 -
    1.49 -#ifdef __WIN32__
    1.50 -
    1.51 -/* Functions to read/write Win32 API file pointers */
    1.52 -
    1.53 -#ifndef INVALID_SET_FILE_POINTER
    1.54 -#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
    1.55 -#endif
    1.56 -
    1.57 -#define READAHEAD_BUFFER_SIZE   1024
    1.58 -
    1.59 -static int SDLCALL
    1.60 -windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
    1.61 -{
    1.62 -    UINT old_error_mode;
    1.63 -    HANDLE h;
    1.64 -    DWORD r_right, w_right;
    1.65 -    DWORD must_exist, truncate;
    1.66 -    int a_mode;
    1.67 -
    1.68 -    if (!context)
    1.69 -        return -1;              /* failed (invalid call) */
    1.70 -
    1.71 -    context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* mark this as unusable */
    1.72 -    context->hidden.windowsio.buffer.data = NULL;
    1.73 -    context->hidden.windowsio.buffer.size = 0;
    1.74 -    context->hidden.windowsio.buffer.left = 0;
    1.75 -
    1.76 -    /* "r" = reading, file must exist */
    1.77 -    /* "w" = writing, truncate existing, file may not exist */
    1.78 -    /* "r+"= reading or writing, file must exist            */
    1.79 -    /* "a" = writing, append file may not exist             */
    1.80 -    /* "a+"= append + read, file may not exist              */
    1.81 -    /* "w+" = read, write, truncate. file may not exist    */
    1.82 -
    1.83 -    must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
    1.84 -    truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
    1.85 -    r_right = (SDL_strchr(mode, '+') != NULL
    1.86 -               || must_exist) ? GENERIC_READ : 0;
    1.87 -    a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
    1.88 -    w_right = (a_mode || SDL_strchr(mode, '+')
    1.89 -               || truncate) ? GENERIC_WRITE : 0;
    1.90 -
    1.91 -    if (!r_right && !w_right)   /* inconsistent mode */
    1.92 -        return -1;              /* failed (invalid call) */
    1.93 -
    1.94 -    context->hidden.windowsio.buffer.data =
    1.95 -        (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
    1.96 -    if (!context->hidden.windowsio.buffer.data) {
    1.97 -        return SDL_OutOfMemory();
    1.98 -    }
    1.99 -    /* Do not open a dialog box if failure */
   1.100 -    old_error_mode =
   1.101 -        SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
   1.102 -
   1.103 -    {
   1.104 -        LPTSTR tstr = WIN_UTF8ToString(filename);
   1.105 -        h = CreateFile(tstr, (w_right | r_right),
   1.106 -                       (w_right) ? 0 : FILE_SHARE_READ, NULL,
   1.107 -                       (must_exist | truncate | a_mode),
   1.108 -                       FILE_ATTRIBUTE_NORMAL, NULL);
   1.109 -        SDL_free(tstr);
   1.110 -    }
   1.111 -
   1.112 -    /* restore old behavior */
   1.113 -    SetErrorMode(old_error_mode);
   1.114 -
   1.115 -    if (h == INVALID_HANDLE_VALUE) {
   1.116 -        SDL_free(context->hidden.windowsio.buffer.data);
   1.117 -        context->hidden.windowsio.buffer.data = NULL;
   1.118 -        SDL_SetError("Couldn't open %s", filename);
   1.119 -        return -2;              /* failed (CreateFile) */
   1.120 -    }
   1.121 -    context->hidden.windowsio.h = h;
   1.122 -    context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
   1.123 -
   1.124 -    return 0;                   /* ok */
   1.125 -}
   1.126 -
   1.127 -static Sint64 SDLCALL
   1.128 -windows_file_size(SDL_RWops * context)
   1.129 -{
   1.130 -    LARGE_INTEGER size;
   1.131 -
   1.132 -    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
   1.133 -        return SDL_SetError("windows_file_size: invalid context/file not opened");
   1.134 -    }
   1.135 -
   1.136 -    if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) {
   1.137 -        return WIN_SetError("windows_file_size");
   1.138 -    }
   1.139 -
   1.140 -    return size.QuadPart;
   1.141 -}
   1.142 -
   1.143 -static Sint64 SDLCALL
   1.144 -windows_file_seek(SDL_RWops * context, Sint64 offset, int whence)
   1.145 -{
   1.146 -    DWORD windowswhence;
   1.147 -    LARGE_INTEGER windowsoffset;
   1.148 -
   1.149 -    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
   1.150 -        return SDL_SetError("windows_file_seek: invalid context/file not opened");
   1.151 -    }
   1.152 -
   1.153 -    /* FIXME: We may be able to satisfy the seek within buffered data */
   1.154 -    if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
   1.155 -        offset -= (long)context->hidden.windowsio.buffer.left;
   1.156 -    }
   1.157 -    context->hidden.windowsio.buffer.left = 0;
   1.158 -
   1.159 -    switch (whence) {
   1.160 -    case RW_SEEK_SET:
   1.161 -        windowswhence = FILE_BEGIN;
   1.162 -        break;
   1.163 -    case RW_SEEK_CUR:
   1.164 -        windowswhence = FILE_CURRENT;
   1.165 -        break;
   1.166 -    case RW_SEEK_END:
   1.167 -        windowswhence = FILE_END;
   1.168 -        break;
   1.169 -    default:
   1.170 -        return SDL_SetError("windows_file_seek: Unknown value for 'whence'");
   1.171 -    }
   1.172 -
   1.173 -    windowsoffset.QuadPart = offset;
   1.174 -    if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) {
   1.175 -        return WIN_SetError("windows_file_seek");
   1.176 -    }
   1.177 -    return windowsoffset.QuadPart;
   1.178 -}
   1.179 -
   1.180 -static size_t SDLCALL
   1.181 -windows_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   1.182 -{
   1.183 -    size_t total_need;
   1.184 -    size_t total_read = 0;
   1.185 -    size_t read_ahead;
   1.186 -    DWORD byte_read;
   1.187 -
   1.188 -    total_need = size * maxnum;
   1.189 -
   1.190 -    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
   1.191 -        || !total_need)
   1.192 -        return 0;
   1.193 -
   1.194 -    if (context->hidden.windowsio.buffer.left > 0) {
   1.195 -        void *data = (char *) context->hidden.windowsio.buffer.data +
   1.196 -            context->hidden.windowsio.buffer.size -
   1.197 -            context->hidden.windowsio.buffer.left;
   1.198 -        read_ahead =
   1.199 -            SDL_min(total_need, context->hidden.windowsio.buffer.left);
   1.200 -        SDL_memcpy(ptr, data, read_ahead);
   1.201 -        context->hidden.windowsio.buffer.left -= read_ahead;
   1.202 -
   1.203 -        if (read_ahead == total_need) {
   1.204 -            return maxnum;
   1.205 -        }
   1.206 -        ptr = (char *) ptr + read_ahead;
   1.207 -        total_need -= read_ahead;
   1.208 -        total_read += read_ahead;
   1.209 -    }
   1.210 -
   1.211 -    if (total_need < READAHEAD_BUFFER_SIZE) {
   1.212 -        if (!ReadFile
   1.213 -            (context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
   1.214 -             READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
   1.215 -            SDL_Error(SDL_EFREAD);
   1.216 -            return 0;
   1.217 -        }
   1.218 -        read_ahead = SDL_min(total_need, (int) byte_read);
   1.219 -        SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
   1.220 -        context->hidden.windowsio.buffer.size = byte_read;
   1.221 -        context->hidden.windowsio.buffer.left = byte_read - read_ahead;
   1.222 -        total_read += read_ahead;
   1.223 -    } else {
   1.224 -        if (!ReadFile
   1.225 -            (context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
   1.226 -            SDL_Error(SDL_EFREAD);
   1.227 -            return 0;
   1.228 -        }
   1.229 -        total_read += byte_read;
   1.230 -    }
   1.231 -    return (total_read / size);
   1.232 -}
   1.233 -
   1.234 -static size_t SDLCALL
   1.235 -windows_file_write(SDL_RWops * context, const void *ptr, size_t size,
   1.236 -                 size_t num)
   1.237 -{
   1.238 -
   1.239 -    size_t total_bytes;
   1.240 -    DWORD byte_written;
   1.241 -    size_t nwritten;
   1.242 -
   1.243 -    total_bytes = size * num;
   1.244 -
   1.245 -    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
   1.246 -        || total_bytes <= 0 || !size)
   1.247 -        return 0;
   1.248 -
   1.249 -    if (context->hidden.windowsio.buffer.left) {
   1.250 -        SetFilePointer(context->hidden.windowsio.h,
   1.251 -                       -(LONG)context->hidden.windowsio.buffer.left, NULL,
   1.252 -                       FILE_CURRENT);
   1.253 -        context->hidden.windowsio.buffer.left = 0;
   1.254 -    }
   1.255 -
   1.256 -    /* if in append mode, we must go to the EOF before write */
   1.257 -    if (context->hidden.windowsio.append) {
   1.258 -        if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
   1.259 -            INVALID_SET_FILE_POINTER) {
   1.260 -            SDL_Error(SDL_EFWRITE);
   1.261 -            return 0;
   1.262 -        }
   1.263 -    }
   1.264 -
   1.265 -    if (!WriteFile
   1.266 -        (context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
   1.267 -        SDL_Error(SDL_EFWRITE);
   1.268 -        return 0;
   1.269 -    }
   1.270 -
   1.271 -    nwritten = byte_written / size;
   1.272 -    return nwritten;
   1.273 -}
   1.274 -
   1.275 -static int SDLCALL
   1.276 -windows_file_close(SDL_RWops * context)
   1.277 -{
   1.278 -
   1.279 -    if (context) {
   1.280 -        if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
   1.281 -            CloseHandle(context->hidden.windowsio.h);
   1.282 -            context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* to be sure */
   1.283 -        }
   1.284 -        SDL_free(context->hidden.windowsio.buffer.data);
   1.285 -        context->hidden.windowsio.buffer.data = NULL;
   1.286 -        SDL_FreeRW(context);
   1.287 -    }
   1.288 -    return (0);
   1.289 -}
   1.290 -#endif /* __WIN32__ */
   1.291 -
   1.292 -#ifdef HAVE_STDIO_H
   1.293 -
   1.294 -/* Functions to read/write stdio file pointers */
   1.295 -
   1.296 -static Sint64 SDLCALL
   1.297 -stdio_size(SDL_RWops * context)
   1.298 -{
   1.299 -    Sint64 pos, size;
   1.300 -
   1.301 -    pos = SDL_RWseek(context, 0, RW_SEEK_CUR);
   1.302 -    if (pos < 0) {
   1.303 -        return -1;
   1.304 -    }
   1.305 -    size = SDL_RWseek(context, 0, RW_SEEK_END);
   1.306 -
   1.307 -    SDL_RWseek(context, pos, RW_SEEK_SET);
   1.308 -    return size;
   1.309 -}
   1.310 -
   1.311 -static Sint64 SDLCALL
   1.312 -stdio_seek(SDL_RWops * context, Sint64 offset, int whence)
   1.313 -{
   1.314 -#ifdef HAVE_FSEEKO64
   1.315 -    if (fseeko64(context->hidden.stdio.fp, (off64_t)offset, whence) == 0) {
   1.316 -        return ftello64(context->hidden.stdio.fp);
   1.317 -    }
   1.318 -#elif defined(HAVE_FSEEKO)
   1.319 -    if (fseeko(context->hidden.stdio.fp, (off_t)offset, whence) == 0) {
   1.320 -        return ftello(context->hidden.stdio.fp);
   1.321 -    }
   1.322 -#elif defined(HAVE__FSEEKI64)
   1.323 -    if (_fseeki64(context->hidden.stdio.fp, offset, whence) == 0) {
   1.324 -        return _ftelli64(context->hidden.stdio.fp);
   1.325 -    }
   1.326 -#else
   1.327 -    if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
   1.328 -        return (ftell(context->hidden.stdio.fp));
   1.329 -    }
   1.330 -#endif
   1.331 -    return SDL_Error(SDL_EFSEEK);
   1.332 -}
   1.333 -
   1.334 -static size_t SDLCALL
   1.335 -stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   1.336 -{
   1.337 -    size_t nread;
   1.338 -
   1.339 -    nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
   1.340 -    if (nread == 0 && ferror(context->hidden.stdio.fp)) {
   1.341 -        SDL_Error(SDL_EFREAD);
   1.342 -    }
   1.343 -    return (nread);
   1.344 -}
   1.345 -
   1.346 -static size_t SDLCALL
   1.347 -stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   1.348 -{
   1.349 -    size_t nwrote;
   1.350 -
   1.351 -    nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   1.352 -    if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
   1.353 -        SDL_Error(SDL_EFWRITE);
   1.354 -    }
   1.355 -    return (nwrote);
   1.356 -}
   1.357 -
   1.358 -static int SDLCALL
   1.359 -stdio_close(SDL_RWops * context)
   1.360 -{
   1.361 -    int status = 0;
   1.362 -    if (context) {
   1.363 -        if (context->hidden.stdio.autoclose) {
   1.364 -            /* WARNING:  Check the return value here! */
   1.365 -            if (fclose(context->hidden.stdio.fp) != 0) {
   1.366 -                status = SDL_Error(SDL_EFWRITE);
   1.367 -            }
   1.368 -        }
   1.369 -        SDL_FreeRW(context);
   1.370 -    }
   1.371 -    return status;
   1.372 -}
   1.373 -#endif /* !HAVE_STDIO_H */
   1.374 -
   1.375 -/* Functions to read/write memory pointers */
   1.376 -
   1.377 -static Sint64 SDLCALL
   1.378 -mem_size(SDL_RWops * context)
   1.379 -{
   1.380 -    return (Sint64)(context->hidden.mem.stop - context->hidden.mem.base);
   1.381 -}
   1.382 -
   1.383 -static Sint64 SDLCALL
   1.384 -mem_seek(SDL_RWops * context, Sint64 offset, int whence)
   1.385 -{
   1.386 -    Uint8 *newpos;
   1.387 -
   1.388 -    switch (whence) {
   1.389 -    case RW_SEEK_SET:
   1.390 -        newpos = context->hidden.mem.base + offset;
   1.391 -        break;
   1.392 -    case RW_SEEK_CUR:
   1.393 -        newpos = context->hidden.mem.here + offset;
   1.394 -        break;
   1.395 -    case RW_SEEK_END:
   1.396 -        newpos = context->hidden.mem.stop + offset;
   1.397 -        break;
   1.398 -    default:
   1.399 -        return SDL_SetError("Unknown value for 'whence'");
   1.400 -    }
   1.401 -    if (newpos < context->hidden.mem.base) {
   1.402 -        newpos = context->hidden.mem.base;
   1.403 -    }
   1.404 -    if (newpos > context->hidden.mem.stop) {
   1.405 -        newpos = context->hidden.mem.stop;
   1.406 -    }
   1.407 -    context->hidden.mem.here = newpos;
   1.408 -    return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
   1.409 -}
   1.410 -
   1.411 -static size_t SDLCALL
   1.412 -mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   1.413 -{
   1.414 -    size_t total_bytes;
   1.415 -    size_t mem_available;
   1.416 -
   1.417 -    total_bytes = (maxnum * size);
   1.418 -    if ((maxnum <= 0) || (size <= 0)
   1.419 -        || ((total_bytes / maxnum) != (size_t) size)) {
   1.420 -        return 0;
   1.421 -    }
   1.422 -
   1.423 -    mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   1.424 -    if (total_bytes > mem_available) {
   1.425 -        total_bytes = mem_available;
   1.426 -    }
   1.427 -
   1.428 -    SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   1.429 -    context->hidden.mem.here += total_bytes;
   1.430 -
   1.431 -    return (total_bytes / size);
   1.432 -}
   1.433 -
   1.434 -static size_t SDLCALL
   1.435 -mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   1.436 -{
   1.437 -    if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
   1.438 -        num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
   1.439 -    }
   1.440 -    SDL_memcpy(context->hidden.mem.here, ptr, num * size);
   1.441 -    context->hidden.mem.here += num * size;
   1.442 -    return (num);
   1.443 -}
   1.444 -
   1.445 -static size_t SDLCALL
   1.446 -mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
   1.447 -{
   1.448 -    SDL_SetError("Can't write to read-only memory");
   1.449 -    return (0);
   1.450 -}
   1.451 -
   1.452 -static int SDLCALL
   1.453 -mem_close(SDL_RWops * context)
   1.454 -{
   1.455 -    if (context) {
   1.456 -        SDL_FreeRW(context);
   1.457 -    }
   1.458 -    return (0);
   1.459 -}
   1.460 -
   1.461 -
   1.462 -/* Functions to create SDL_RWops structures from various data sources */
   1.463 -
   1.464 -SDL_RWops *
   1.465 -SDL_RWFromFile(const char *file, const char *mode)
   1.466 -{
   1.467 -    SDL_RWops *rwops = NULL;
   1.468 -    if (!file || !*file || !mode || !*mode) {
   1.469 -        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
   1.470 -        return NULL;
   1.471 -    }
   1.472 -#if defined(ANDROID)
   1.473 -#ifdef HAVE_STDIO_H
   1.474 -    /* Try to open the file on the filesystem first */
   1.475 -    if (*file == '/') {
   1.476 -        FILE *fp = fopen(file, mode);
   1.477 -        if (fp) {
   1.478 -            return SDL_RWFromFP(fp, 1);
   1.479 -        }
   1.480 -    } else {
   1.481 -        /* Try opening it from internal storage if it's a relative path */
   1.482 -        char *path;
   1.483 -        FILE *fp;
   1.484 -
   1.485 -        path = SDL_stack_alloc(char, PATH_MAX);
   1.486 -        if (path) {
   1.487 -            SDL_snprintf(path, PATH_MAX, "%s/%s",
   1.488 -                         SDL_AndroidGetInternalStoragePath(), file);
   1.489 -            fp = fopen(path, mode);
   1.490 -            SDL_stack_free(path);
   1.491 -            if (fp) {
   1.492 -                return SDL_RWFromFP(fp, 1);
   1.493 -            }
   1.494 -        }
   1.495 -    }
   1.496 -#endif /* HAVE_STDIO_H */
   1.497 -
   1.498 -    /* Try to open the file from the asset system */
   1.499 -    rwops = SDL_AllocRW();
   1.500 -    if (!rwops)
   1.501 -        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   1.502 -    if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
   1.503 -        SDL_FreeRW(rwops);
   1.504 -        return NULL;
   1.505 -    }
   1.506 -    rwops->size = Android_JNI_FileSize;
   1.507 -    rwops->seek = Android_JNI_FileSeek;
   1.508 -    rwops->read = Android_JNI_FileRead;
   1.509 -    rwops->write = Android_JNI_FileWrite;
   1.510 -    rwops->close = Android_JNI_FileClose;
   1.511 -    rwops->type = SDL_RWOPS_JNIFILE;
   1.512 -
   1.513 -#elif defined(__WIN32__)
   1.514 -    rwops = SDL_AllocRW();
   1.515 -    if (!rwops)
   1.516 -        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
   1.517 -    if (windows_file_open(rwops, file, mode) < 0) {
   1.518 -        SDL_FreeRW(rwops);
   1.519 -        return NULL;
   1.520 -    }
   1.521 -    rwops->size = windows_file_size;
   1.522 -    rwops->seek = windows_file_seek;
   1.523 -    rwops->read = windows_file_read;
   1.524 -    rwops->write = windows_file_write;
   1.525 -    rwops->close = windows_file_close;
   1.526 -    rwops->type = SDL_RWOPS_WINFILE;
   1.527 -
   1.528 -#elif HAVE_STDIO_H
   1.529 -    {
   1.530 -        #ifdef __APPLE__
   1.531 -        FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
   1.532 -        #elif __WINRT__
   1.533 -        FILE *fp = NULL;
   1.534 -        fopen_s(&fp, file, mode);
   1.535 -        #else
   1.536 -        FILE *fp = fopen(file, mode);
   1.537 -        #endif
   1.538 -        if (fp == NULL) {
   1.539 -            SDL_SetError("Couldn't open %s", file);
   1.540 -        } else {
   1.541 -            rwops = SDL_RWFromFP(fp, 1);
   1.542 -        }
   1.543 -    }
   1.544 -#else
   1.545 -    SDL_SetError("SDL not compiled with stdio support");
   1.546 -#endif /* !HAVE_STDIO_H */
   1.547 -
   1.548 -    return (rwops);
   1.549 -}
   1.550 -
   1.551 -#ifdef HAVE_STDIO_H
   1.552 -SDL_RWops *
   1.553 -SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
   1.554 -{
   1.555 -    SDL_RWops *rwops = NULL;
   1.556 -
   1.557 -    rwops = SDL_AllocRW();
   1.558 -    if (rwops != NULL) {
   1.559 -        rwops->size = stdio_size;
   1.560 -        rwops->seek = stdio_seek;
   1.561 -        rwops->read = stdio_read;
   1.562 -        rwops->write = stdio_write;
   1.563 -        rwops->close = stdio_close;
   1.564 -        rwops->hidden.stdio.fp = fp;
   1.565 -        rwops->hidden.stdio.autoclose = autoclose;
   1.566 -        rwops->type = SDL_RWOPS_STDFILE;
   1.567 -    }
   1.568 -    return (rwops);
   1.569 -}
   1.570 -#else
   1.571 -SDL_RWops *
   1.572 -SDL_RWFromFP(void * fp, SDL_bool autoclose)
   1.573 -{
   1.574 -    SDL_SetError("SDL not compiled with stdio support");
   1.575 -    return NULL;
   1.576 -}
   1.577 -#endif /* HAVE_STDIO_H */
   1.578 -
   1.579 -SDL_RWops *
   1.580 -SDL_RWFromMem(void *mem, int size)
   1.581 -{
   1.582 -    SDL_RWops *rwops = NULL;
   1.583 -    if (!mem) {
   1.584 -      SDL_InvalidParamError("mem");
   1.585 -      return (rwops);
   1.586 -    }
   1.587 -    if (!size) {
   1.588 -      SDL_InvalidParamError("size");
   1.589 -      return (rwops);
   1.590 -    }
   1.591 -
   1.592 -    rwops = SDL_AllocRW();
   1.593 -    if (rwops != NULL) {
   1.594 -        rwops->size = mem_size;
   1.595 -        rwops->seek = mem_seek;
   1.596 -        rwops->read = mem_read;
   1.597 -        rwops->write = mem_write;
   1.598 -        rwops->close = mem_close;
   1.599 -        rwops->hidden.mem.base = (Uint8 *) mem;
   1.600 -        rwops->hidden.mem.here = rwops->hidden.mem.base;
   1.601 -        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   1.602 -        rwops->type = SDL_RWOPS_MEMORY;
   1.603 -    }
   1.604 -    return (rwops);
   1.605 -}
   1.606 -
   1.607 -SDL_RWops *
   1.608 -SDL_RWFromConstMem(const void *mem, int size)
   1.609 -{
   1.610 -    SDL_RWops *rwops = NULL;
   1.611 -    if (!mem) {
   1.612 -      SDL_InvalidParamError("mem");
   1.613 -      return (rwops);
   1.614 -    }
   1.615 -    if (!size) {
   1.616 -      SDL_InvalidParamError("size");
   1.617 -      return (rwops);
   1.618 -    }
   1.619 -
   1.620 -    rwops = SDL_AllocRW();
   1.621 -    if (rwops != NULL) {
   1.622 -        rwops->size = mem_size;
   1.623 -        rwops->seek = mem_seek;
   1.624 -        rwops->read = mem_read;
   1.625 -        rwops->write = mem_writeconst;
   1.626 -        rwops->close = mem_close;
   1.627 -        rwops->hidden.mem.base = (Uint8 *) mem;
   1.628 -        rwops->hidden.mem.here = rwops->hidden.mem.base;
   1.629 -        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
   1.630 -        rwops->type = SDL_RWOPS_MEMORY_RO;
   1.631 -    }
   1.632 -    return (rwops);
   1.633 -}
   1.634 -
   1.635 -SDL_RWops *
   1.636 -SDL_AllocRW(void)
   1.637 -{
   1.638 -    SDL_RWops *area;
   1.639 -
   1.640 -    area = (SDL_RWops *) SDL_malloc(sizeof *area);
   1.641 -    if (area == NULL) {
   1.642 -        SDL_OutOfMemory();
   1.643 -    } else {
   1.644 -        area->type = SDL_RWOPS_UNKNOWN;
   1.645 -    }
   1.646 -    return (area);
   1.647 -}
   1.648 -
   1.649 -void
   1.650 -SDL_FreeRW(SDL_RWops * area)
   1.651 -{
   1.652 -    SDL_free(area);
   1.653 -}
   1.654 -
   1.655 -/* Functions for dynamically reading and writing endian-specific values */
   1.656 -
   1.657 -Uint8
   1.658 -SDL_ReadU8(SDL_RWops * src)
   1.659 -{
   1.660 -    Uint8 value = 0;
   1.661 -
   1.662 -    SDL_RWread(src, &value, (sizeof value), 1);
   1.663 -    return value;
   1.664 -}
   1.665 -
   1.666 -Uint16
   1.667 -SDL_ReadLE16(SDL_RWops * src)
   1.668 -{
   1.669 -    Uint16 value = 0;
   1.670 -
   1.671 -    SDL_RWread(src, &value, (sizeof value), 1);
   1.672 -    return (SDL_SwapLE16(value));
   1.673 -}
   1.674 -
   1.675 -Uint16
   1.676 -SDL_ReadBE16(SDL_RWops * src)
   1.677 -{
   1.678 -    Uint16 value = 0;
   1.679 -
   1.680 -    SDL_RWread(src, &value, (sizeof value), 1);
   1.681 -    return (SDL_SwapBE16(value));
   1.682 -}
   1.683 -
   1.684 -Uint32
   1.685 -SDL_ReadLE32(SDL_RWops * src)
   1.686 -{
   1.687 -    Uint32 value = 0;
   1.688 -
   1.689 -    SDL_RWread(src, &value, (sizeof value), 1);
   1.690 -    return (SDL_SwapLE32(value));
   1.691 -}
   1.692 -
   1.693 -Uint32
   1.694 -SDL_ReadBE32(SDL_RWops * src)
   1.695 -{
   1.696 -    Uint32 value = 0;
   1.697 -
   1.698 -    SDL_RWread(src, &value, (sizeof value), 1);
   1.699 -    return (SDL_SwapBE32(value));
   1.700 -}
   1.701 -
   1.702 -Uint64
   1.703 -SDL_ReadLE64(SDL_RWops * src)
   1.704 -{
   1.705 -    Uint64 value = 0;
   1.706 -
   1.707 -    SDL_RWread(src, &value, (sizeof value), 1);
   1.708 -    return (SDL_SwapLE64(value));
   1.709 -}
   1.710 -
   1.711 -Uint64
   1.712 -SDL_ReadBE64(SDL_RWops * src)
   1.713 -{
   1.714 -    Uint64 value = 0;
   1.715 -
   1.716 -    SDL_RWread(src, &value, (sizeof value), 1);
   1.717 -    return (SDL_SwapBE64(value));
   1.718 -}
   1.719 -
   1.720 -size_t
   1.721 -SDL_WriteU8(SDL_RWops * dst, Uint8 value)
   1.722 -{
   1.723 -    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.724 -}
   1.725 -
   1.726 -size_t
   1.727 -SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
   1.728 -{
   1.729 -    value = SDL_SwapLE16(value);
   1.730 -    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.731 -}
   1.732 -
   1.733 -size_t
   1.734 -SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
   1.735 -{
   1.736 -    value = SDL_SwapBE16(value);
   1.737 -    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.738 -}
   1.739 -
   1.740 -size_t
   1.741 -SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
   1.742 -{
   1.743 -    value = SDL_SwapLE32(value);
   1.744 -    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.745 -}
   1.746 -
   1.747 -size_t
   1.748 -SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
   1.749 -{
   1.750 -    value = SDL_SwapBE32(value);
   1.751 -    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.752 -}
   1.753 -
   1.754 -size_t
   1.755 -SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
   1.756 -{
   1.757 -    value = SDL_SwapLE64(value);
   1.758 -    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.759 -}
   1.760 -
   1.761 -size_t
   1.762 -SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
   1.763 -{
   1.764 -    value = SDL_SwapBE64(value);
   1.765 -    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
   1.766 -}
   1.767 -
   1.768 -/* vi: set ts=4 sw=4 expandtab: */
   1.769 +/*
   1.770 +  Simple DirectMedia Layer
   1.771 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
   1.772 +
   1.773 +  This software is provided 'as-is', without any express or implied
   1.774 +  warranty.  In no event will the authors be held liable for any damages
   1.775 +  arising from the use of this software.
   1.776 +
   1.777 +  Permission is granted to anyone to use this software for any purpose,
   1.778 +  including commercial applications, and to alter it and redistribute it
   1.779 +  freely, subject to the following restrictions:
   1.780 +
   1.781 +  1. The origin of this software must not be misrepresented; you must not
   1.782 +     claim that you wrote the original software. If you use this software
   1.783 +     in a product, an acknowledgment in the product documentation would be
   1.784 +     appreciated but is not required.
   1.785 +  2. Altered source versions must be plainly marked as such, and must not be
   1.786 +     misrepresented as being the original software.
   1.787 +  3. This notice may not be removed or altered from any source distribution.
   1.788 +*/
   1.789 +/* Need this so Linux systems define fseek64o, ftell64o and off64_t */
   1.790 +#define _LARGEFILE64_SOURCE
   1.791 +#include "SDL_config.h"
   1.792 +
   1.793 +#if defined(__WIN32__)
   1.794 +#include "../core/windows/SDL_windows.h"
   1.795 +#endif
   1.796 +
   1.797 +
   1.798 +/* This file provides a general interface for SDL to read and write
   1.799 +   data sources.  It can easily be extended to files, memory, etc.
   1.800 +*/
   1.801 +
   1.802 +#include "SDL_endian.h"
   1.803 +#include "SDL_rwops.h"
   1.804 +
   1.805 +#ifdef __APPLE__
   1.806 +#include "cocoa/SDL_rwopsbundlesupport.h"
   1.807 +#endif /* __APPLE__ */
   1.808 +
   1.809 +#ifdef ANDROID
   1.810 +#include "../core/android/SDL_android.h"
   1.811 +#include "SDL_system.h"
   1.812 +#endif
   1.813 +
   1.814 +#ifdef __WIN32__
   1.815 +
   1.816 +/* Functions to read/write Win32 API file pointers */
   1.817 +
   1.818 +#ifndef INVALID_SET_FILE_POINTER
   1.819 +#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
   1.820 +#endif
   1.821 +
   1.822 +#define READAHEAD_BUFFER_SIZE   1024
   1.823 +
   1.824 +static int SDLCALL
   1.825 +windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
   1.826 +{
   1.827 +    UINT old_error_mode;
   1.828 +    HANDLE h;
   1.829 +    DWORD r_right, w_right;
   1.830 +    DWORD must_exist, truncate;
   1.831 +    int a_mode;
   1.832 +
   1.833 +    if (!context)
   1.834 +        return -1;              /* failed (invalid call) */
   1.835 +
   1.836 +    context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* mark this as unusable */
   1.837 +    context->hidden.windowsio.buffer.data = NULL;
   1.838 +    context->hidden.windowsio.buffer.size = 0;
   1.839 +    context->hidden.windowsio.buffer.left = 0;
   1.840 +
   1.841 +    /* "r" = reading, file must exist */
   1.842 +    /* "w" = writing, truncate existing, file may not exist */
   1.843 +    /* "r+"= reading or writing, file must exist            */
   1.844 +    /* "a" = writing, append file may not exist             */
   1.845 +    /* "a+"= append + read, file may not exist              */
   1.846 +    /* "w+" = read, write, truncate. file may not exist    */
   1.847 +
   1.848 +    must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
   1.849 +    truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
   1.850 +    r_right = (SDL_strchr(mode, '+') != NULL
   1.851 +               || must_exist) ? GENERIC_READ : 0;
   1.852 +    a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
   1.853 +    w_right = (a_mode || SDL_strchr(mode, '+')
   1.854 +               || truncate) ? GENERIC_WRITE : 0;
   1.855 +
   1.856 +    if (!r_right && !w_right)   /* inconsistent mode */
   1.857 +        return -1;              /* failed (invalid call) */
   1.858 +
   1.859 +    context->hidden.windowsio.buffer.data =
   1.860 +        (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
   1.861 +    if (!context->hidden.windowsio.buffer.data) {
   1.862 +        return SDL_OutOfMemory();
   1.863 +    }
   1.864 +    /* Do not open a dialog box if failure */
   1.865 +    old_error_mode =
   1.866 +        SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
   1.867 +
   1.868 +    {
   1.869 +        LPTSTR tstr = WIN_UTF8ToString(filename);
   1.870 +        h = CreateFile(tstr, (w_right | r_right),
   1.871 +                       (w_right) ? 0 : FILE_SHARE_READ, NULL,
   1.872 +                       (must_exist | truncate | a_mode),
   1.873 +                       FILE_ATTRIBUTE_NORMAL, NULL);
   1.874 +        SDL_free(tstr);
   1.875 +    }
   1.876 +
   1.877 +    /* restore old behavior */
   1.878 +    SetErrorMode(old_error_mode);
   1.879 +
   1.880 +    if (h == INVALID_HANDLE_VALUE) {
   1.881 +        SDL_free(context->hidden.windowsio.buffer.data);
   1.882 +        context->hidden.windowsio.buffer.data = NULL;
   1.883 +        SDL_SetError("Couldn't open %s", filename);
   1.884 +        return -2;              /* failed (CreateFile) */
   1.885 +    }
   1.886 +    context->hidden.windowsio.h = h;
   1.887 +    context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
   1.888 +
   1.889 +    return 0;                   /* ok */
   1.890 +}
   1.891 +
   1.892 +static Sint64 SDLCALL
   1.893 +windows_file_size(SDL_RWops * context)
   1.894 +{
   1.895 +    LARGE_INTEGER size;
   1.896 +
   1.897 +    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
   1.898 +        return SDL_SetError("windows_file_size: invalid context/file not opened");
   1.899 +    }
   1.900 +
   1.901 +    if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) {
   1.902 +        return WIN_SetError("windows_file_size");
   1.903 +    }
   1.904 +
   1.905 +    return size.QuadPart;
   1.906 +}
   1.907 +
   1.908 +static Sint64 SDLCALL
   1.909 +windows_file_seek(SDL_RWops * context, Sint64 offset, int whence)
   1.910 +{
   1.911 +    DWORD windowswhence;
   1.912 +    LARGE_INTEGER windowsoffset;
   1.913 +
   1.914 +    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
   1.915 +        return SDL_SetError("windows_file_seek: invalid context/file not opened");
   1.916 +    }
   1.917 +
   1.918 +    /* FIXME: We may be able to satisfy the seek within buffered data */
   1.919 +    if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
   1.920 +        offset -= (long)context->hidden.windowsio.buffer.left;
   1.921 +    }
   1.922 +    context->hidden.windowsio.buffer.left = 0;
   1.923 +
   1.924 +    switch (whence) {
   1.925 +    case RW_SEEK_SET:
   1.926 +        windowswhence = FILE_BEGIN;
   1.927 +        break;
   1.928 +    case RW_SEEK_CUR:
   1.929 +        windowswhence = FILE_CURRENT;
   1.930 +        break;
   1.931 +    case RW_SEEK_END:
   1.932 +        windowswhence = FILE_END;
   1.933 +        break;
   1.934 +    default:
   1.935 +        return SDL_SetError("windows_file_seek: Unknown value for 'whence'");
   1.936 +    }
   1.937 +
   1.938 +    windowsoffset.QuadPart = offset;
   1.939 +    if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) {
   1.940 +        return WIN_SetError("windows_file_seek");
   1.941 +    }
   1.942 +    return windowsoffset.QuadPart;
   1.943 +}
   1.944 +
   1.945 +static size_t SDLCALL
   1.946 +windows_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
   1.947 +{
   1.948 +    size_t total_need;
   1.949 +    size_t total_read = 0;
   1.950 +    size_t read_ahead;
   1.951 +    DWORD byte_read;
   1.952 +
   1.953 +    total_need = size * maxnum;
   1.954 +
   1.955 +    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
   1.956 +        || !total_need)
   1.957 +        return 0;
   1.958 +
   1.959 +    if (context->hidden.windowsio.buffer.left > 0) {
   1.960 +        void *data = (char *) context->hidden.windowsio.buffer.data +
   1.961 +            context->hidden.windowsio.buffer.size -
   1.962 +            context->hidden.windowsio.buffer.left;
   1.963 +        read_ahead =
   1.964 +            SDL_min(total_need, context->hidden.windowsio.buffer.left);
   1.965 +        SDL_memcpy(ptr, data, read_ahead);
   1.966 +        context->hidden.windowsio.buffer.left -= read_ahead;
   1.967 +
   1.968 +        if (read_ahead == total_need) {
   1.969 +            return maxnum;
   1.970 +        }
   1.971 +        ptr = (char *) ptr + read_ahead;
   1.972 +        total_need -= read_ahead;
   1.973 +        total_read += read_ahead;
   1.974 +    }
   1.975 +
   1.976 +    if (total_need < READAHEAD_BUFFER_SIZE) {
   1.977 +        if (!ReadFile
   1.978 +            (context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
   1.979 +             READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
   1.980 +            SDL_Error(SDL_EFREAD);
   1.981 +            return 0;
   1.982 +        }
   1.983 +        read_ahead = SDL_min(total_need, (int) byte_read);
   1.984 +        SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
   1.985 +        context->hidden.windowsio.buffer.size = byte_read;
   1.986 +        context->hidden.windowsio.buffer.left = byte_read - read_ahead;
   1.987 +        total_read += read_ahead;
   1.988 +    } else {
   1.989 +        if (!ReadFile
   1.990 +            (context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
   1.991 +            SDL_Error(SDL_EFREAD);
   1.992 +            return 0;
   1.993 +        }
   1.994 +        total_read += byte_read;
   1.995 +    }
   1.996 +    return (total_read / size);
   1.997 +}
   1.998 +
   1.999 +static size_t SDLCALL
  1.1000 +windows_file_write(SDL_RWops * context, const void *ptr, size_t size,
  1.1001 +                 size_t num)
  1.1002 +{
  1.1003 +
  1.1004 +    size_t total_bytes;
  1.1005 +    DWORD byte_written;
  1.1006 +    size_t nwritten;
  1.1007 +
  1.1008 +    total_bytes = size * num;
  1.1009 +
  1.1010 +    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
  1.1011 +        || total_bytes <= 0 || !size)
  1.1012 +        return 0;
  1.1013 +
  1.1014 +    if (context->hidden.windowsio.buffer.left) {
  1.1015 +        SetFilePointer(context->hidden.windowsio.h,
  1.1016 +                       -(LONG)context->hidden.windowsio.buffer.left, NULL,
  1.1017 +                       FILE_CURRENT);
  1.1018 +        context->hidden.windowsio.buffer.left = 0;
  1.1019 +    }
  1.1020 +
  1.1021 +    /* if in append mode, we must go to the EOF before write */
  1.1022 +    if (context->hidden.windowsio.append) {
  1.1023 +        if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
  1.1024 +            INVALID_SET_FILE_POINTER) {
  1.1025 +            SDL_Error(SDL_EFWRITE);
  1.1026 +            return 0;
  1.1027 +        }
  1.1028 +    }
  1.1029 +
  1.1030 +    if (!WriteFile
  1.1031 +        (context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
  1.1032 +        SDL_Error(SDL_EFWRITE);
  1.1033 +        return 0;
  1.1034 +    }
  1.1035 +
  1.1036 +    nwritten = byte_written / size;
  1.1037 +    return nwritten;
  1.1038 +}
  1.1039 +
  1.1040 +static int SDLCALL
  1.1041 +windows_file_close(SDL_RWops * context)
  1.1042 +{
  1.1043 +
  1.1044 +    if (context) {
  1.1045 +        if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
  1.1046 +            CloseHandle(context->hidden.windowsio.h);
  1.1047 +            context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* to be sure */
  1.1048 +        }
  1.1049 +        SDL_free(context->hidden.windowsio.buffer.data);
  1.1050 +        context->hidden.windowsio.buffer.data = NULL;
  1.1051 +        SDL_FreeRW(context);
  1.1052 +    }
  1.1053 +    return (0);
  1.1054 +}
  1.1055 +#endif /* __WIN32__ */
  1.1056 +
  1.1057 +#ifdef HAVE_STDIO_H
  1.1058 +
  1.1059 +/* Functions to read/write stdio file pointers */
  1.1060 +
  1.1061 +static Sint64 SDLCALL
  1.1062 +stdio_size(SDL_RWops * context)
  1.1063 +{
  1.1064 +    Sint64 pos, size;
  1.1065 +
  1.1066 +    pos = SDL_RWseek(context, 0, RW_SEEK_CUR);
  1.1067 +    if (pos < 0) {
  1.1068 +        return -1;
  1.1069 +    }
  1.1070 +    size = SDL_RWseek(context, 0, RW_SEEK_END);
  1.1071 +
  1.1072 +    SDL_RWseek(context, pos, RW_SEEK_SET);
  1.1073 +    return size;
  1.1074 +}
  1.1075 +
  1.1076 +static Sint64 SDLCALL
  1.1077 +stdio_seek(SDL_RWops * context, Sint64 offset, int whence)
  1.1078 +{
  1.1079 +#ifdef HAVE_FSEEKO64
  1.1080 +    if (fseeko64(context->hidden.stdio.fp, (off64_t)offset, whence) == 0) {
  1.1081 +        return ftello64(context->hidden.stdio.fp);
  1.1082 +    }
  1.1083 +#elif defined(HAVE_FSEEKO)
  1.1084 +    if (fseeko(context->hidden.stdio.fp, (off_t)offset, whence) == 0) {
  1.1085 +        return ftello(context->hidden.stdio.fp);
  1.1086 +    }
  1.1087 +#elif defined(HAVE__FSEEKI64)
  1.1088 +    if (_fseeki64(context->hidden.stdio.fp, offset, whence) == 0) {
  1.1089 +        return _ftelli64(context->hidden.stdio.fp);
  1.1090 +    }
  1.1091 +#else
  1.1092 +    if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
  1.1093 +        return (ftell(context->hidden.stdio.fp));
  1.1094 +    }
  1.1095 +#endif
  1.1096 +    return SDL_Error(SDL_EFSEEK);
  1.1097 +}
  1.1098 +
  1.1099 +static size_t SDLCALL
  1.1100 +stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
  1.1101 +{
  1.1102 +    size_t nread;
  1.1103 +
  1.1104 +    nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
  1.1105 +    if (nread == 0 && ferror(context->hidden.stdio.fp)) {
  1.1106 +        SDL_Error(SDL_EFREAD);
  1.1107 +    }
  1.1108 +    return (nread);
  1.1109 +}
  1.1110 +
  1.1111 +static size_t SDLCALL
  1.1112 +stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
  1.1113 +{
  1.1114 +    size_t nwrote;
  1.1115 +
  1.1116 +    nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
  1.1117 +    if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
  1.1118 +        SDL_Error(SDL_EFWRITE);
  1.1119 +    }
  1.1120 +    return (nwrote);
  1.1121 +}
  1.1122 +
  1.1123 +static int SDLCALL
  1.1124 +stdio_close(SDL_RWops * context)
  1.1125 +{
  1.1126 +    int status = 0;
  1.1127 +    if (context) {
  1.1128 +        if (context->hidden.stdio.autoclose) {
  1.1129 +            /* WARNING:  Check the return value here! */
  1.1130 +            if (fclose(context->hidden.stdio.fp) != 0) {
  1.1131 +                status = SDL_Error(SDL_EFWRITE);
  1.1132 +            }
  1.1133 +        }
  1.1134 +        SDL_FreeRW(context);
  1.1135 +    }
  1.1136 +    return status;
  1.1137 +}
  1.1138 +#endif /* !HAVE_STDIO_H */
  1.1139 +
  1.1140 +/* Functions to read/write memory pointers */
  1.1141 +
  1.1142 +static Sint64 SDLCALL
  1.1143 +mem_size(SDL_RWops * context)
  1.1144 +{
  1.1145 +    return (Sint64)(context->hidden.mem.stop - context->hidden.mem.base);
  1.1146 +}
  1.1147 +
  1.1148 +static Sint64 SDLCALL
  1.1149 +mem_seek(SDL_RWops * context, Sint64 offset, int whence)
  1.1150 +{
  1.1151 +    Uint8 *newpos;
  1.1152 +
  1.1153 +    switch (whence) {
  1.1154 +    case RW_SEEK_SET:
  1.1155 +        newpos = context->hidden.mem.base + offset;
  1.1156 +        break;
  1.1157 +    case RW_SEEK_CUR:
  1.1158 +        newpos = context->hidden.mem.here + offset;
  1.1159 +        break;
  1.1160 +    case RW_SEEK_END:
  1.1161 +        newpos = context->hidden.mem.stop + offset;
  1.1162 +        break;
  1.1163 +    default:
  1.1164 +        return SDL_SetError("Unknown value for 'whence'");
  1.1165 +    }
  1.1166 +    if (newpos < context->hidden.mem.base) {
  1.1167 +        newpos = context->hidden.mem.base;
  1.1168 +    }
  1.1169 +    if (newpos > context->hidden.mem.stop) {
  1.1170 +        newpos = context->hidden.mem.stop;
  1.1171 +    }
  1.1172 +    context->hidden.mem.here = newpos;
  1.1173 +    return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
  1.1174 +}
  1.1175 +
  1.1176 +static size_t SDLCALL
  1.1177 +mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
  1.1178 +{
  1.1179 +    size_t total_bytes;
  1.1180 +    size_t mem_available;
  1.1181 +
  1.1182 +    total_bytes = (maxnum * size);
  1.1183 +    if ((maxnum <= 0) || (size <= 0)
  1.1184 +        || ((total_bytes / maxnum) != (size_t) size)) {
  1.1185 +        return 0;
  1.1186 +    }
  1.1187 +
  1.1188 +    mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
  1.1189 +    if (total_bytes > mem_available) {
  1.1190 +        total_bytes = mem_available;
  1.1191 +    }
  1.1192 +
  1.1193 +    SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
  1.1194 +    context->hidden.mem.here += total_bytes;
  1.1195 +
  1.1196 +    return (total_bytes / size);
  1.1197 +}
  1.1198 +
  1.1199 +static size_t SDLCALL
  1.1200 +mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
  1.1201 +{
  1.1202 +    if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
  1.1203 +        num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
  1.1204 +    }
  1.1205 +    SDL_memcpy(context->hidden.mem.here, ptr, num * size);
  1.1206 +    context->hidden.mem.here += num * size;
  1.1207 +    return (num);
  1.1208 +}
  1.1209 +
  1.1210 +static size_t SDLCALL
  1.1211 +mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
  1.1212 +{
  1.1213 +    SDL_SetError("Can't write to read-only memory");
  1.1214 +    return (0);
  1.1215 +}
  1.1216 +
  1.1217 +static int SDLCALL
  1.1218 +mem_close(SDL_RWops * context)
  1.1219 +{
  1.1220 +    if (context) {
  1.1221 +        SDL_FreeRW(context);
  1.1222 +    }
  1.1223 +    return (0);
  1.1224 +}
  1.1225 +
  1.1226 +
  1.1227 +/* Functions to create SDL_RWops structures from various data sources */
  1.1228 +
  1.1229 +SDL_RWops *
  1.1230 +SDL_RWFromFile(const char *file, const char *mode)
  1.1231 +{
  1.1232 +    SDL_RWops *rwops = NULL;
  1.1233 +    if (!file || !*file || !mode || !*mode) {
  1.1234 +        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
  1.1235 +        return NULL;
  1.1236 +    }
  1.1237 +#if defined(ANDROID)
  1.1238 +#ifdef HAVE_STDIO_H
  1.1239 +    /* Try to open the file on the filesystem first */
  1.1240 +    if (*file == '/') {
  1.1241 +        FILE *fp = fopen(file, mode);
  1.1242 +        if (fp) {
  1.1243 +            return SDL_RWFromFP(fp, 1);
  1.1244 +        }
  1.1245 +    } else {
  1.1246 +        /* Try opening it from internal storage if it's a relative path */
  1.1247 +        char *path;
  1.1248 +        FILE *fp;
  1.1249 +
  1.1250 +        path = SDL_stack_alloc(char, PATH_MAX);
  1.1251 +        if (path) {
  1.1252 +            SDL_snprintf(path, PATH_MAX, "%s/%s",
  1.1253 +                         SDL_AndroidGetInternalStoragePath(), file);
  1.1254 +            fp = fopen(path, mode);
  1.1255 +            SDL_stack_free(path);
  1.1256 +            if (fp) {
  1.1257 +                return SDL_RWFromFP(fp, 1);
  1.1258 +            }
  1.1259 +        }
  1.1260 +    }
  1.1261 +#endif /* HAVE_STDIO_H */
  1.1262 +
  1.1263 +    /* Try to open the file from the asset system */
  1.1264 +    rwops = SDL_AllocRW();
  1.1265 +    if (!rwops)
  1.1266 +        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
  1.1267 +    if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
  1.1268 +        SDL_FreeRW(rwops);
  1.1269 +        return NULL;
  1.1270 +    }
  1.1271 +    rwops->size = Android_JNI_FileSize;
  1.1272 +    rwops->seek = Android_JNI_FileSeek;
  1.1273 +    rwops->read = Android_JNI_FileRead;
  1.1274 +    rwops->write = Android_JNI_FileWrite;
  1.1275 +    rwops->close = Android_JNI_FileClose;
  1.1276 +    rwops->type = SDL_RWOPS_JNIFILE;
  1.1277 +
  1.1278 +#elif defined(__WIN32__)
  1.1279 +    rwops = SDL_AllocRW();
  1.1280 +    if (!rwops)
  1.1281 +        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
  1.1282 +    if (windows_file_open(rwops, file, mode) < 0) {
  1.1283 +        SDL_FreeRW(rwops);
  1.1284 +        return NULL;
  1.1285 +    }
  1.1286 +    rwops->size = windows_file_size;
  1.1287 +    rwops->seek = windows_file_seek;
  1.1288 +    rwops->read = windows_file_read;
  1.1289 +    rwops->write = windows_file_write;
  1.1290 +    rwops->close = windows_file_close;
  1.1291 +    rwops->type = SDL_RWOPS_WINFILE;
  1.1292 +
  1.1293 +#elif HAVE_STDIO_H
  1.1294 +    {
  1.1295 +        #ifdef __APPLE__
  1.1296 +        FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
  1.1297 +        #elif __WINRT__
  1.1298 +        FILE *fp = NULL;
  1.1299 +        fopen_s(&fp, file, mode);
  1.1300 +        #else
  1.1301 +        FILE *fp = fopen(file, mode);
  1.1302 +        #endif
  1.1303 +        if (fp == NULL) {
  1.1304 +            SDL_SetError("Couldn't open %s", file);
  1.1305 +        } else {
  1.1306 +            rwops = SDL_RWFromFP(fp, 1);
  1.1307 +        }
  1.1308 +    }
  1.1309 +#else
  1.1310 +    SDL_SetError("SDL not compiled with stdio support");
  1.1311 +#endif /* !HAVE_STDIO_H */
  1.1312 +
  1.1313 +    return (rwops);
  1.1314 +}
  1.1315 +
  1.1316 +#ifdef HAVE_STDIO_H
  1.1317 +SDL_RWops *
  1.1318 +SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
  1.1319 +{
  1.1320 +    SDL_RWops *rwops = NULL;
  1.1321 +
  1.1322 +    rwops = SDL_AllocRW();
  1.1323 +    if (rwops != NULL) {
  1.1324 +        rwops->size = stdio_size;
  1.1325 +        rwops->seek = stdio_seek;
  1.1326 +        rwops->read = stdio_read;
  1.1327 +        rwops->write = stdio_write;
  1.1328 +        rwops->close = stdio_close;
  1.1329 +        rwops->hidden.stdio.fp = fp;
  1.1330 +        rwops->hidden.stdio.autoclose = autoclose;
  1.1331 +        rwops->type = SDL_RWOPS_STDFILE;
  1.1332 +    }
  1.1333 +    return (rwops);
  1.1334 +}
  1.1335 +#else
  1.1336 +SDL_RWops *
  1.1337 +SDL_RWFromFP(void * fp, SDL_bool autoclose)
  1.1338 +{
  1.1339 +    SDL_SetError("SDL not compiled with stdio support");
  1.1340 +    return NULL;
  1.1341 +}
  1.1342 +#endif /* HAVE_STDIO_H */
  1.1343 +
  1.1344 +SDL_RWops *
  1.1345 +SDL_RWFromMem(void *mem, int size)
  1.1346 +{
  1.1347 +    SDL_RWops *rwops = NULL;
  1.1348 +    if (!mem) {
  1.1349 +      SDL_InvalidParamError("mem");
  1.1350 +      return (rwops);
  1.1351 +    }
  1.1352 +    if (!size) {
  1.1353 +      SDL_InvalidParamError("size");
  1.1354 +      return (rwops);
  1.1355 +    }
  1.1356 +
  1.1357 +    rwops = SDL_AllocRW();
  1.1358 +    if (rwops != NULL) {
  1.1359 +        rwops->size = mem_size;
  1.1360 +        rwops->seek = mem_seek;
  1.1361 +        rwops->read = mem_read;
  1.1362 +        rwops->write = mem_write;
  1.1363 +        rwops->close = mem_close;
  1.1364 +        rwops->hidden.mem.base = (Uint8 *) mem;
  1.1365 +        rwops->hidden.mem.here = rwops->hidden.mem.base;
  1.1366 +        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
  1.1367 +        rwops->type = SDL_RWOPS_MEMORY;
  1.1368 +    }
  1.1369 +    return (rwops);
  1.1370 +}
  1.1371 +
  1.1372 +SDL_RWops *
  1.1373 +SDL_RWFromConstMem(const void *mem, int size)
  1.1374 +{
  1.1375 +    SDL_RWops *rwops = NULL;
  1.1376 +    if (!mem) {
  1.1377 +      SDL_InvalidParamError("mem");
  1.1378 +      return (rwops);
  1.1379 +    }
  1.1380 +    if (!size) {
  1.1381 +      SDL_InvalidParamError("size");
  1.1382 +      return (rwops);
  1.1383 +    }
  1.1384 +
  1.1385 +    rwops = SDL_AllocRW();
  1.1386 +    if (rwops != NULL) {
  1.1387 +        rwops->size = mem_size;
  1.1388 +        rwops->seek = mem_seek;
  1.1389 +        rwops->read = mem_read;
  1.1390 +        rwops->write = mem_writeconst;
  1.1391 +        rwops->close = mem_close;
  1.1392 +        rwops->hidden.mem.base = (Uint8 *) mem;
  1.1393 +        rwops->hidden.mem.here = rwops->hidden.mem.base;
  1.1394 +        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
  1.1395 +        rwops->type = SDL_RWOPS_MEMORY_RO;
  1.1396 +    }
  1.1397 +    return (rwops);
  1.1398 +}
  1.1399 +
  1.1400 +SDL_RWops *
  1.1401 +SDL_AllocRW(void)
  1.1402 +{
  1.1403 +    SDL_RWops *area;
  1.1404 +
  1.1405 +    area = (SDL_RWops *) SDL_malloc(sizeof *area);
  1.1406 +    if (area == NULL) {
  1.1407 +        SDL_OutOfMemory();
  1.1408 +    } else {
  1.1409 +        area->type = SDL_RWOPS_UNKNOWN;
  1.1410 +    }
  1.1411 +    return (area);
  1.1412 +}
  1.1413 +
  1.1414 +void
  1.1415 +SDL_FreeRW(SDL_RWops * area)
  1.1416 +{
  1.1417 +    SDL_free(area);
  1.1418 +}
  1.1419 +
  1.1420 +/* Functions for dynamically reading and writing endian-specific values */
  1.1421 +
  1.1422 +Uint8
  1.1423 +SDL_ReadU8(SDL_RWops * src)
  1.1424 +{
  1.1425 +    Uint8 value = 0;
  1.1426 +
  1.1427 +    SDL_RWread(src, &value, (sizeof value), 1);
  1.1428 +    return value;
  1.1429 +}
  1.1430 +
  1.1431 +Uint16
  1.1432 +SDL_ReadLE16(SDL_RWops * src)
  1.1433 +{
  1.1434 +    Uint16 value = 0;
  1.1435 +
  1.1436 +    SDL_RWread(src, &value, (sizeof value), 1);
  1.1437 +    return (SDL_SwapLE16(value));
  1.1438 +}
  1.1439 +
  1.1440 +Uint16
  1.1441 +SDL_ReadBE16(SDL_RWops * src)
  1.1442 +{
  1.1443 +    Uint16 value = 0;
  1.1444 +
  1.1445 +    SDL_RWread(src, &value, (sizeof value), 1);
  1.1446 +    return (SDL_SwapBE16(value));
  1.1447 +}
  1.1448 +
  1.1449 +Uint32
  1.1450 +SDL_ReadLE32(SDL_RWops * src)
  1.1451 +{
  1.1452 +    Uint32 value = 0;
  1.1453 +
  1.1454 +    SDL_RWread(src, &value, (sizeof value), 1);
  1.1455 +    return (SDL_SwapLE32(value));
  1.1456 +}
  1.1457 +
  1.1458 +Uint32
  1.1459 +SDL_ReadBE32(SDL_RWops * src)
  1.1460 +{
  1.1461 +    Uint32 value = 0;
  1.1462 +
  1.1463 +    SDL_RWread(src, &value, (sizeof value), 1);
  1.1464 +    return (SDL_SwapBE32(value));
  1.1465 +}
  1.1466 +
  1.1467 +Uint64
  1.1468 +SDL_ReadLE64(SDL_RWops * src)
  1.1469 +{
  1.1470 +    Uint64 value = 0;
  1.1471 +
  1.1472 +    SDL_RWread(src, &value, (sizeof value), 1);
  1.1473 +    return (SDL_SwapLE64(value));
  1.1474 +}
  1.1475 +
  1.1476 +Uint64
  1.1477 +SDL_ReadBE64(SDL_RWops * src)
  1.1478 +{
  1.1479 +    Uint64 value = 0;
  1.1480 +
  1.1481 +    SDL_RWread(src, &value, (sizeof value), 1);
  1.1482 +    return (SDL_SwapBE64(value));
  1.1483 +}
  1.1484 +
  1.1485 +size_t
  1.1486 +SDL_WriteU8(SDL_RWops * dst, Uint8 value)
  1.1487 +{
  1.1488 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
  1.1489 +}
  1.1490 +
  1.1491 +size_t
  1.1492 +SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
  1.1493 +{
  1.1494 +    value = SDL_SwapLE16(value);
  1.1495 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
  1.1496 +}
  1.1497 +
  1.1498 +size_t
  1.1499 +SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
  1.1500 +{
  1.1501 +    value = SDL_SwapBE16(value);
  1.1502 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
  1.1503 +}
  1.1504 +
  1.1505 +size_t
  1.1506 +SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
  1.1507 +{
  1.1508 +    value = SDL_SwapLE32(value);
  1.1509 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
  1.1510 +}
  1.1511 +
  1.1512 +size_t
  1.1513 +SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
  1.1514 +{
  1.1515 +    value = SDL_SwapBE32(value);
  1.1516 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
  1.1517 +}
  1.1518 +
  1.1519 +size_t
  1.1520 +SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
  1.1521 +{
  1.1522 +    value = SDL_SwapLE64(value);
  1.1523 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
  1.1524 +}
  1.1525 +
  1.1526 +size_t
  1.1527 +SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
  1.1528 +{
  1.1529 +    value = SDL_SwapBE64(value);
  1.1530 +    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
  1.1531 +}
  1.1532 +
  1.1533 +/* vi: set ts=4 sw=4 expandtab: */