GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/fs/virtfs/rwops.cpp Lines: 15 22 68.2 %
Date: 2018-05-19 03:02:23 Branches: 4 18 22.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2018  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/*
22
 * This code provides a glue layer between PhysicsFS and Simple Directmedia
23
 *  Layer's (SDL) RWops i/o abstraction.
24
 *
25
 * License: this code is public domain. I make no warranty that it is useful,
26
 *  correct, harmless, or environmentally safe.
27
 *
28
 * This particular file may be used however you like, including copying it
29
 *  verbatim into a closed-source project, exploiting it commercially, and
30
 *  removing any trace of my name from the source (although I hope you won't
31
 *  do that). I welcome enhancements and corrections to this file, but I do
32
 *  not require you to send me patches if you make changes. This code has
33
 *  NO WARRANTY.
34
 *
35
 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
36
 *  Please see LICENSE.txt in the root of the source tree.
37
 *
38
 * SDL falls under the LGPL license. You can get SDL at http://www.libsdl.org/
39
 *
40
 *  This file was written by Ryan C. Gordon. ([email protected]).
41
 *
42
 *  Copyright (C) 2012-2018  The ManaPlus Developers
43
 */
44
45
#include "fs/virtfs/rwops.h"
46
47
#include "fs/virtfs/file.h"
48
#include "fs/virtfs/fs.h"
49
#include "fs/virtfs/fsfuncs.h"
50
51
#include "utils/checkutils.h"
52
#include "utils/fuzzer.h"
53
54
PRAGMA48(GCC diagnostic push)
55
PRAGMA48(GCC diagnostic ignored "-Wshadow")
56
#include <SDL_rwops.h>
57
PRAGMA48(GCC diagnostic pop)
58
59
#include "debug.h"
60
61
namespace VirtFs
62
{
63
64
4642
SDL_RWops *create_rwops(File *const file,
65
                        const std::string &restrict fname)
66
{
67
4642
    SDL_RWops *retval = nullptr;
68
69
4642
    if (file == nullptr)
70
    {
71
        reportAlways("VirtFs::rwops_seek: create rwops error: %s",
72
            fname.c_str());
73
    }
74
    else
75
    {
76
4642
        retval = SDL_AllocRW();
77
4642
        if (retval != nullptr)
78
        {
79
#ifdef USE_SDL2
80
4642
            retval->size  = file->funcs->rwops_size;
81
#endif  // USE_SDL2
82
83
4642
            retval->seek  = file->funcs->rwops_seek;
84
4642
            retval->read  = file->funcs->rwops_read;
85
4642
            retval->write = file->funcs->rwops_write;
86
4642
            retval->close = file->funcs->rwops_close;
87
4642
            retval->hidden.unknown.data1 = file;
88
        } /* if */
89
    } /* else */
90
91
4642
    return retval;
92
} /* VirtFs::create_rwops */
93
94
#ifdef __APPLE__
95
static bool checkFilePath(const std::string &restrict fname)
96
{
97
    if (fname.empty())
98
        return false;
99
    if (!exists(fname) || isDirectory(fname))
100
        return false;
101
    return true;
102
}
103
#endif  // __APPLE__
104
105
4642
SDL_RWops *rwopsOpenRead(const std::string &restrict fname)
106
{
107
    BLOCK_START("RWopsopenRead")
108
#ifdef __APPLE__
109
    if (!checkFilePath(fname))
110
        return nullptr;
111
#endif  // __APPLE__
112
#ifdef USE_FUZZER
113
    if (Fuzzer::conditionTerminate(fname))
114
        return nullptr;
115
#endif  // USE_FUZZER
116
#ifdef USE_PROFILER
117
118
    SDL_RWops *const ret = create_rwops(openRead(fname),
119
        fname);
120
121
    BLOCK_END("RWopsopenRead")
122
    return ret;
123
#else  // USE_PROFILER
124
125
9284
    return create_rwops(openRead(fname),
126
9284
        fname);
127
#endif  // USE_PROFILER
128
} /* RWopsopenRead */
129
130
SDL_RWops *rwopsOpenWrite(const std::string &restrict fname)
131
{
132
#ifdef __APPLE__
133
    if (!checkFilePath(fname))
134
        return nullptr;
135
#endif  // __APPLE__
136
137
    return create_rwops(openWrite(fname),
138
        fname);
139
} /* RWopsopenWrite */
140
141
SDL_RWops *rwopsOpenAppend(const std::string &restrict fname)
142
{
143
#ifdef __APPLE__
144
    if (!checkFilePath(fname))
145
        return nullptr;
146
#endif  // __APPLE__
147
148
    return create_rwops(openAppend(fname),
149
        fname);
150
} /* RWopsopenAppend */
151
152
}  // namespace VirtFs
153
154
/* end of virtfsrwops.c ... */