Debian Codegraph @

common/misc/physfsrwops.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /*
8 * This code provides a glue layer between PhysicsFS and Simple Directmedia
9 * Layer's (SDL) RWops i/o abstraction.
10 *
11 * License: this code is public domain. I make no warranty that it is useful,
12 * correct, harmless, or environmentally safe.
13 *
14 * This particular file may be used however you like, including copying it
15 * verbatim into a closed-source project, exploiting it commercially, and
16 * removing any trace of my name from the source (although I hope you won't
17 * do that). I welcome enhancements and corrections to this file, but I do
18 * not require you to send me patches if you make changes. This code has
19 * NO WARRANTY.
20 *
21 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
22 * Please see LICENSE in the root of the source tree.
23 *
24 * SDL falls under the LGPL license. You can get SDL at https://www.libsdl.org/
25 *
26 * This file was written by Ryan C. Gordon. ([email protected]).
27 */
28
29 #include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
30 #include "physfsrwops.h"
31 #include "physfsx.h"
32
33 #if SDL_MAJOR_VERSION == 1
34 #define SDL_RWops_callback_seek_position int
35 #define SDL_RWops_callback_read_position int
36 #define SDL_RWops_callback_write_position int
37 #else
38 #define SDL_RWops_callback_seek_position Sint64
39 #define SDL_RWops_callback_read_position size_t
40 #define SDL_RWops_callback_write_position size_t
41 #endif
42
43 namespace {
44
45 static SDL_RWops_callback_seek_position physfsrwops_seek(SDL_RWops *rw, const SDL_RWops_callback_seek_position offset, const int whence)
46 {
47 PHYSFS_File *handle = reinterpret_cast<PHYSFS_File *>(rw->hidden.unknown.data1);
48 SDL_RWops_callback_seek_position pos;
49
50 if (whence == SEEK_SET)
51 {
52 pos = offset;
53 } /* if */
54
55 else if (whence == SEEK_CUR)
56 {
57 PHYSFS_sint64 current = PHYSFS_tell(handle);
58 if (current == -1)
59 {
60 SDL_SetError("Can't find position in file: %s",
61 PHYSFS_getLastError());
62 return(-1);
63 } /* if */
64
65 if (!std::in_range<SDL_RWops_callback_seek_position>(current))
66 {
67 SDL_SetError("Can't fit current file position in an int!");
68 return(-1);
69 } /* if */
70 if (offset == 0) /* this is a "tell" call. We're done. */
71 return current;
72
73 /* When `SDL_RWops_callback_seek_position` is `int`, this assignment
74 * converts to a narrower type, but the call to std::in_range above
75 * rejected any values for which the narrowing would change the
76 * observed value. An assignment which prohibits narrowing would be
77 * ill-formed, since the compile-time check for narrowing is
78 * context-free and assumes the worst case. Therefore, an
79 * initialization that prohibited narrowing would trigger a compile
80 * error.
81 *
82 * When `SDL_RWops_callback_seek_position` is `Sint64`, then on
83 * x86_64-w64-mingw32,
84 * `static_cast<SDL_RWops_callback_seek_position>(v)` triggers
85 * `-Wuseless-cast`.
86 *
87 * When `SDL_RWops_callback_seek_position` is `Sint64`, then on
88 * x86_64-pc-linux-gnu,
89 * `static_cast<SDL_RWops_callback_seek_position>(v)` is accepted
90 * without issue.
91 */
92 const SDL_RWops_callback_seek_position rwcurrent = current;
93 pos = rwcurrent + offset;
94 } /* else if */
95
96 else if (whence == SEEK_END)
97 {
98 PHYSFS_sint64 len = PHYSFS_fileLength(handle);
99 if (len == -1)
100 {
101 SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
102 return(-1);
103 } /* if */
104
105 if (!std::in_range<SDL_RWops_callback_seek_position>(len))
106 {
107 SDL_SetError("Can't fit end-of-file position in an int!");
108 return(-1);
109 } /* if */
110
111 const SDL_RWops_callback_seek_position rwlen = len;
112 pos = rwlen + offset;
113 } /* else if */
114
115 else
116 {
117 SDL_SetError("Invalid 'whence' parameter.");
118 return(-1);
119 } /* else */
120
121 if ( pos < 0 )
122 {
123 SDL_SetError("Attempt to seek past start of file.");
124 return(-1);
125 } /* if */
126
127 if (!PHYSFS_seek(handle, static_cast<PHYSFS_uint64>(pos)))
128 {
129 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
130 return(-1);
131 } /* if */
132
133 return(pos);
134 } /* physfsrwops_seek */
135
136
137 static SDL_RWops_callback_read_position physfsrwops_read(SDL_RWops *const rw, void *const ptr, const SDL_RWops_callback_read_position size, const SDL_RWops_callback_read_position maxnum)
138 {
139 PHYSFS_File *handle = reinterpret_cast<PHYSFS_File *>(rw->hidden.unknown.data1);
140 const auto count{size * maxnum};
141 const auto rc{PHYSFS_readBytes(handle, ptr, count)};
142 if (rc != count)
143 {
144 if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
145 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
146 } /* if */
147 return rc;
148 } /* physfsrwops_read */
149
150
151 static SDL_RWops_callback_write_position physfsrwops_write(SDL_RWops *const rw, const void *const ptr, const SDL_RWops_callback_write_position size, const SDL_RWops_callback_write_position num)
152 {
153 PHYSFS_File *handle = reinterpret_cast<PHYSFS_File *>(rw->hidden.unknown.data1);
154 const auto count{size * num};
155 const auto rc{PHYSFS_writeBytes(handle, reinterpret_cast<const uint8_t *>(ptr), count)};
156 if (rc != count)
157 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
158 return rc;
159 } /* physfsrwops_write */
160
161
162 static int physfsrwops_close(SDL_RWops *rw)
163 {
164 PHYSFS_File *handle = reinterpret_cast<PHYSFS_File *>(rw->hidden.unknown.data1);
165 if (!PHYSFS_close(handle))
166 {
167 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
168 return(-1);
169 } /* if */
170
171 SDL_FreeRW(rw);
172 return(0);
173 } /* physfsrwops_close */
174
175
176 static std::pair<RWops_ptr, PHYSFS_ErrorCode> create_rwops(RAIIPHYSFS_File handle)
177 {
178 if (!handle)
179 {
180 const auto err = PHYSFS_getLastErrorCode();
181 SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(err));
182 return {nullptr, err};
183 }
184 else
185 {
186 RWops_ptr retval{SDL_AllocRW()};
187 if (retval)
188 {
189 retval->seek = physfsrwops_seek;
190 retval->read = physfsrwops_read;
191 retval->write = physfsrwops_write;
192 retval->close = physfsrwops_close;
193 retval->hidden.unknown.data1 = handle.release();
194 } /* if */
195 else
196 return {nullptr, PHYSFS_ERR_OTHER_ERROR};
197 return {std::move(retval), PHYSFS_ERR_OK};
198 } /* else */
199 } /* create_rwops */
200
201 }
202
203 std::pair<RWops_ptr, PHYSFS_ErrorCode> PHYSFSRWOPS_openRead(const char *fname)
204 {
205 return(create_rwops(RAIIPHYSFS_File{PHYSFS_openRead(fname)}));
206 } /* PHYSFSRWOPS_openRead */
207
208 std::pair<RWops_ptr, PHYSFS_ErrorCode> PHYSFSRWOPS_openReadBuffered(const char *fname, const PHYSFS_uint64 bufferSize)
209 {
210 RAIIPHYSFS_File fp{PHYSFS_openRead(fname)};
211 PHYSFS_setBuffer(fp, bufferSize);
212 return create_rwops(std::move(fp));
213 }
214
215 /* end of physfsrwops.c ... */
216