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