SDL_rwops.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_WINRT)
  20. #include "../core/windows/SDL_windows.h"
  21. #endif
  22. #ifdef HAVE_STDIO_H
  23. #include <stdio.h>
  24. #include <sys/stat.h>
  25. #endif
  26. #ifdef HAVE_LIMITS_H
  27. #include <limits.h>
  28. #endif
  29. /* This file provides a general interface for SDL to read and write
  30. data sources. It can easily be extended to files, memory, etc.
  31. */
  32. #ifdef SDL_PLATFORM_APPLE
  33. #include "cocoa/SDL_rwopsbundlesupport.h"
  34. #endif /* SDL_PLATFORM_APPLE */
  35. #ifdef SDL_PLATFORM_3DS
  36. #include "n3ds/SDL_rwopsromfs.h"
  37. #endif /* SDL_PLATFORM_3DS */
  38. #ifdef SDL_PLATFORM_ANDROID
  39. #include "../core/android/SDL_android.h"
  40. #endif
  41. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_WINRT)
  42. /* Functions to read/write Win32 API file pointers */
  43. #ifndef INVALID_SET_FILE_POINTER
  44. #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
  45. #endif
  46. #define READAHEAD_BUFFER_SIZE 1024
  47. static int SDLCALL windows_file_open(SDL_RWops *context, const char *filename, const char *mode)
  48. {
  49. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) && !defined(SDL_PLATFORM_WINRT)
  50. UINT old_error_mode;
  51. #endif
  52. HANDLE h;
  53. DWORD r_right, w_right;
  54. DWORD must_exist, truncate;
  55. int a_mode;
  56. context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
  57. context->hidden.windowsio.buffer.data = NULL;
  58. context->hidden.windowsio.buffer.size = 0;
  59. context->hidden.windowsio.buffer.left = 0;
  60. /* "r" = reading, file must exist */
  61. /* "w" = writing, truncate existing, file may not exist */
  62. /* "r+"= reading or writing, file must exist */
  63. /* "a" = writing, append file may not exist */
  64. /* "a+"= append + read, file may not exist */
  65. /* "w+" = read, write, truncate. file may not exist */
  66. must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
  67. truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
  68. r_right = (SDL_strchr(mode, '+') != NULL || must_exist) ? GENERIC_READ : 0;
  69. a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
  70. w_right = (a_mode || SDL_strchr(mode, '+') || truncate) ? GENERIC_WRITE : 0;
  71. if (!r_right && !w_right) {
  72. return -1; /* inconsistent mode */
  73. }
  74. /* failed (invalid call) */
  75. context->hidden.windowsio.buffer.data =
  76. (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
  77. if (!context->hidden.windowsio.buffer.data) {
  78. return -1;
  79. }
  80. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) && !defined(SDL_PLATFORM_WINRT)
  81. /* Do not open a dialog box if failure */
  82. old_error_mode =
  83. SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  84. #endif
  85. {
  86. LPTSTR tstr = WIN_UTF8ToString(filename);
  87. #if defined(SDL_PLATFORM_WINRT)
  88. CREATEFILE2_EXTENDED_PARAMETERS extparams;
  89. SDL_zero(extparams);
  90. extparams.dwSize = sizeof(extparams);
  91. extparams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
  92. h = CreateFile2(tstr,
  93. (w_right | r_right),
  94. (w_right) ? 0 : FILE_SHARE_READ,
  95. (must_exist | truncate | a_mode),
  96. &extparams);
  97. #else
  98. h = CreateFile(tstr,
  99. (w_right | r_right),
  100. (w_right) ? 0 : FILE_SHARE_READ,
  101. NULL,
  102. (must_exist | truncate | a_mode),
  103. FILE_ATTRIBUTE_NORMAL,
  104. NULL);
  105. #endif
  106. SDL_free(tstr);
  107. }
  108. #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) && !defined(SDL_PLATFORM_WINRT)
  109. /* restore old behavior */
  110. SetErrorMode(old_error_mode);
  111. #endif
  112. if (h == INVALID_HANDLE_VALUE) {
  113. SDL_free(context->hidden.windowsio.buffer.data);
  114. context->hidden.windowsio.buffer.data = NULL;
  115. SDL_SetError("Couldn't open %s", filename);
  116. return -2; /* failed (CreateFile) */
  117. }
  118. context->hidden.windowsio.h = h;
  119. context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
  120. return 0; /* ok */
  121. }
  122. static Sint64 SDLCALL windows_file_size(SDL_RWops *context)
  123. {
  124. LARGE_INTEGER size;
  125. if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) {
  126. return WIN_SetError("windows_file_size");
  127. }
  128. return size.QuadPart;
  129. }
  130. static Sint64 SDLCALL windows_file_seek(SDL_RWops *context, Sint64 offset, int whence)
  131. {
  132. DWORD windowswhence;
  133. LARGE_INTEGER windowsoffset;
  134. /* FIXME: We may be able to satisfy the seek within buffered data */
  135. if (whence == SDL_RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
  136. offset -= context->hidden.windowsio.buffer.left;
  137. }
  138. context->hidden.windowsio.buffer.left = 0;
  139. switch (whence) {
  140. case SDL_RW_SEEK_SET:
  141. windowswhence = FILE_BEGIN;
  142. break;
  143. case SDL_RW_SEEK_CUR:
  144. windowswhence = FILE_CURRENT;
  145. break;
  146. case SDL_RW_SEEK_END:
  147. windowswhence = FILE_END;
  148. break;
  149. default:
  150. return SDL_SetError("windows_file_seek: Unknown value for 'whence'");
  151. }
  152. windowsoffset.QuadPart = offset;
  153. if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) {
  154. return WIN_SetError("windows_file_seek");
  155. }
  156. return windowsoffset.QuadPart;
  157. }
  158. static size_t SDLCALL windows_file_read(SDL_RWops *context, void *ptr, size_t size)
  159. {
  160. size_t total_need = size;
  161. size_t total_read = 0;
  162. size_t read_ahead;
  163. DWORD bytes;
  164. if (context->hidden.windowsio.buffer.left > 0) {
  165. void *data = (char *)context->hidden.windowsio.buffer.data +
  166. context->hidden.windowsio.buffer.size -
  167. context->hidden.windowsio.buffer.left;
  168. read_ahead = SDL_min(total_need, context->hidden.windowsio.buffer.left);
  169. SDL_memcpy(ptr, data, read_ahead);
  170. context->hidden.windowsio.buffer.left -= read_ahead;
  171. if (read_ahead == total_need) {
  172. return size;
  173. }
  174. ptr = (char *)ptr + read_ahead;
  175. total_need -= read_ahead;
  176. total_read += read_ahead;
  177. }
  178. if (total_need < READAHEAD_BUFFER_SIZE) {
  179. if (!ReadFile(context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
  180. READAHEAD_BUFFER_SIZE, &bytes, NULL)) {
  181. SDL_Error(SDL_EFREAD);
  182. return 0;
  183. }
  184. read_ahead = SDL_min(total_need, bytes);
  185. SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
  186. context->hidden.windowsio.buffer.size = bytes;
  187. context->hidden.windowsio.buffer.left = bytes - read_ahead;
  188. total_read += read_ahead;
  189. } else {
  190. if (!ReadFile(context->hidden.windowsio.h, ptr, (DWORD)total_need, &bytes, NULL)) {
  191. SDL_Error(SDL_EFREAD);
  192. return 0;
  193. }
  194. total_read += bytes;
  195. }
  196. return total_read;
  197. }
  198. static size_t SDLCALL windows_file_write(SDL_RWops *context, const void *ptr, size_t size)
  199. {
  200. const size_t total_bytes = size;
  201. DWORD bytes;
  202. if (context->hidden.windowsio.buffer.left) {
  203. if (!SetFilePointer(context->hidden.windowsio.h,
  204. -(LONG)context->hidden.windowsio.buffer.left, NULL,
  205. FILE_CURRENT)) {
  206. SDL_Error(SDL_EFSEEK);
  207. return 0;
  208. }
  209. context->hidden.windowsio.buffer.left = 0;
  210. }
  211. /* if in append mode, we must go to the EOF before write */
  212. if (context->hidden.windowsio.append) {
  213. LARGE_INTEGER windowsoffset;
  214. windowsoffset.QuadPart = 0;
  215. if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, FILE_END)) {
  216. SDL_Error(SDL_EFSEEK);
  217. return 0;
  218. }
  219. }
  220. if (!WriteFile(context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &bytes, NULL)) {
  221. SDL_Error(SDL_EFWRITE);
  222. return 0;
  223. }
  224. return bytes;
  225. }
  226. static int SDLCALL windows_file_close(SDL_RWops *context)
  227. {
  228. if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
  229. CloseHandle(context->hidden.windowsio.h);
  230. context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* to be sure */
  231. }
  232. if (context->hidden.windowsio.buffer.data) {
  233. SDL_free(context->hidden.windowsio.buffer.data);
  234. context->hidden.windowsio.buffer.data = NULL;
  235. }
  236. SDL_DestroyRW(context);
  237. return 0;
  238. }
  239. #endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */
  240. #if defined(HAVE_STDIO_H) && !(defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK))
  241. /* Functions to read/write stdio file pointers. Not used for windows. */
  242. #ifdef HAVE_FOPEN64
  243. #define fopen fopen64
  244. #endif
  245. #ifdef HAVE_FSEEKO64
  246. #define fseek_off_t off64_t
  247. #define fseek fseeko64
  248. #define ftell ftello64
  249. #elif defined(HAVE_FSEEKO)
  250. #if defined(OFF_MIN) && defined(OFF_MAX)
  251. #define FSEEK_OFF_MIN OFF_MIN
  252. #define FSEEK_OFF_MAX OFF_MAX
  253. #elif defined(HAVE_LIMITS_H)
  254. /* POSIX doesn't specify the minimum and maximum macros for off_t so
  255. * we have to improvise and dance around implementation-defined
  256. * behavior. This may fail if the off_t type has padding bits or
  257. * is not a two's-complement representation. The compilers will detect
  258. * and eliminate the dead code if off_t has 64 bits.
  259. */
  260. #define FSEEK_OFF_MAX (((((off_t)1 << (sizeof(off_t) * CHAR_BIT - 2)) - 1) << 1) + 1)
  261. #define FSEEK_OFF_MIN (-(FSEEK_OFF_MAX)-1)
  262. #endif
  263. #define fseek_off_t off_t
  264. #define fseek fseeko
  265. #define ftell ftello
  266. #elif defined(HAVE__FSEEKI64)
  267. #define fseek_off_t __int64
  268. #define fseek _fseeki64
  269. #define ftell _ftelli64
  270. #else
  271. #ifdef HAVE_LIMITS_H
  272. #define FSEEK_OFF_MIN LONG_MIN
  273. #define FSEEK_OFF_MAX LONG_MAX
  274. #endif
  275. #define fseek_off_t long
  276. #endif
  277. static Sint64 SDLCALL stdio_seek(SDL_RWops *context, Sint64 offset, int whence)
  278. {
  279. int stdiowhence;
  280. switch (whence) {
  281. case SDL_RW_SEEK_SET:
  282. stdiowhence = SEEK_SET;
  283. break;
  284. case SDL_RW_SEEK_CUR:
  285. stdiowhence = SEEK_CUR;
  286. break;
  287. case SDL_RW_SEEK_END:
  288. stdiowhence = SEEK_END;
  289. break;
  290. default:
  291. return SDL_SetError("Unknown value for 'whence'");
  292. }
  293. #if defined(FSEEK_OFF_MIN) && defined(FSEEK_OFF_MAX)
  294. if (offset < (Sint64)(FSEEK_OFF_MIN) || offset > (Sint64)(FSEEK_OFF_MAX)) {
  295. return SDL_SetError("Seek offset out of range");
  296. }
  297. #endif
  298. if (fseek((FILE *)context->hidden.stdio.fp, (fseek_off_t)offset, stdiowhence) == 0) {
  299. Sint64 pos = ftell((FILE *)context->hidden.stdio.fp);
  300. if (pos < 0) {
  301. return SDL_SetError("Couldn't get stream offset");
  302. }
  303. return pos;
  304. }
  305. return SDL_Error(SDL_EFSEEK);
  306. }
  307. static size_t SDLCALL stdio_read(SDL_RWops *context, void *ptr, size_t size)
  308. {
  309. size_t bytes;
  310. bytes = fread(ptr, 1, size, (FILE *)context->hidden.stdio.fp);
  311. if (bytes == 0 && ferror((FILE *)context->hidden.stdio.fp)) {
  312. SDL_Error(SDL_EFREAD);
  313. }
  314. return bytes;
  315. }
  316. static size_t SDLCALL stdio_write(SDL_RWops *context, const void *ptr, size_t size)
  317. {
  318. size_t bytes;
  319. bytes = fwrite(ptr, 1, size, (FILE *)context->hidden.stdio.fp);
  320. if (bytes == 0 && ferror((FILE *)context->hidden.stdio.fp)) {
  321. SDL_Error(SDL_EFWRITE);
  322. }
  323. return bytes;
  324. }
  325. static int SDLCALL stdio_close(SDL_RWops *context)
  326. {
  327. int status = 0;
  328. if (context->hidden.stdio.autoclose) {
  329. if (fclose((FILE *)context->hidden.stdio.fp) != 0) {
  330. status = SDL_Error(SDL_EFWRITE);
  331. }
  332. }
  333. SDL_DestroyRW(context);
  334. return status;
  335. }
  336. static SDL_RWops *SDL_RWFromFP(void *fp, SDL_bool autoclose)
  337. {
  338. SDL_RWops *rwops = NULL;
  339. rwops = SDL_CreateRW();
  340. if (rwops) {
  341. rwops->seek = stdio_seek;
  342. rwops->read = stdio_read;
  343. rwops->write = stdio_write;
  344. rwops->close = stdio_close;
  345. rwops->hidden.stdio.fp = fp;
  346. rwops->hidden.stdio.autoclose = autoclose;
  347. rwops->type = SDL_RWOPS_STDFILE;
  348. }
  349. return rwops;
  350. }
  351. #endif /* !HAVE_STDIO_H && !(SDL_PLATFORM_WIN32 || SDL_PLATFORM_GDK) */
  352. /* Functions to read/write memory pointers */
  353. static Sint64 SDLCALL mem_size(SDL_RWops *context)
  354. {
  355. return (context->hidden.mem.stop - context->hidden.mem.base);
  356. }
  357. static Sint64 SDLCALL mem_seek(SDL_RWops *context, Sint64 offset, int whence)
  358. {
  359. Uint8 *newpos;
  360. switch (whence) {
  361. case SDL_RW_SEEK_SET:
  362. newpos = context->hidden.mem.base + offset;
  363. break;
  364. case SDL_RW_SEEK_CUR:
  365. newpos = context->hidden.mem.here + offset;
  366. break;
  367. case SDL_RW_SEEK_END:
  368. newpos = context->hidden.mem.stop + offset;
  369. break;
  370. default:
  371. return SDL_SetError("Unknown value for 'whence'");
  372. }
  373. if (newpos < context->hidden.mem.base) {
  374. newpos = context->hidden.mem.base;
  375. }
  376. if (newpos > context->hidden.mem.stop) {
  377. newpos = context->hidden.mem.stop;
  378. }
  379. context->hidden.mem.here = newpos;
  380. return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
  381. }
  382. static size_t mem_io(SDL_RWops *context, void *dst, const void *src, size_t size)
  383. {
  384. const size_t mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
  385. if (size > mem_available) {
  386. size = mem_available;
  387. }
  388. SDL_memcpy(dst, src, size);
  389. context->hidden.mem.here += size;
  390. return size;
  391. }
  392. static size_t SDLCALL mem_read(SDL_RWops *context, void *ptr, size_t size)
  393. {
  394. return mem_io(context, ptr, context->hidden.mem.here, size);
  395. }
  396. static size_t SDLCALL mem_write(SDL_RWops *context, const void *ptr, size_t size)
  397. {
  398. return mem_io(context, context->hidden.mem.here, ptr, size);
  399. }
  400. /* Functions to create SDL_RWops structures from various data sources */
  401. #if defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINDOWS)
  402. static SDL_bool IsRegularFileOrPipe(FILE *f)
  403. {
  404. #ifdef SDL_PLATFORM_WINRT
  405. struct __stat64 st;
  406. if (_fstat64(_fileno(f), &st) < 0 ||
  407. !((st.st_mode & _S_IFMT) == _S_IFREG || (st.st_mode & _S_IFMT) == _S_IFIFO)) {
  408. return SDL_FALSE;
  409. }
  410. #else
  411. struct stat st;
  412. if (fstat(fileno(f), &st) < 0 || !(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) {
  413. return SDL_FALSE;
  414. }
  415. #endif
  416. return SDL_TRUE;
  417. }
  418. #endif
  419. SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
  420. {
  421. SDL_RWops *rwops = NULL;
  422. if (!file || !*file || !mode || !*mode) {
  423. SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
  424. return NULL;
  425. }
  426. #ifdef SDL_PLATFORM_ANDROID
  427. #ifdef HAVE_STDIO_H
  428. /* Try to open the file on the filesystem first */
  429. if (*file == '/') {
  430. FILE *fp = fopen(file, mode);
  431. if (fp) {
  432. if (!IsRegularFileOrPipe(fp)) {
  433. fclose(fp);
  434. SDL_SetError("%s is not a regular file or pipe", file);
  435. return NULL;
  436. }
  437. return SDL_RWFromFP(fp, 1);
  438. }
  439. } else {
  440. /* Try opening it from internal storage if it's a relative path */
  441. char *path;
  442. FILE *fp;
  443. /* !!! FIXME: why not just "char path[PATH_MAX];" ? */
  444. path = SDL_stack_alloc(char, PATH_MAX);
  445. if (path) {
  446. SDL_snprintf(path, PATH_MAX, "%s/%s",
  447. SDL_AndroidGetInternalStoragePath(), file);
  448. fp = fopen(path, mode);
  449. SDL_stack_free(path);
  450. if (fp) {
  451. if (!IsRegularFileOrPipe(fp)) {
  452. fclose(fp);
  453. SDL_SetError("%s is not a regular file or pipe", path);
  454. return NULL;
  455. }
  456. return SDL_RWFromFP(fp, 1);
  457. }
  458. }
  459. }
  460. #endif /* HAVE_STDIO_H */
  461. /* Try to open the file from the asset system */
  462. rwops = SDL_CreateRW();
  463. if (!rwops) {
  464. return NULL; /* SDL_SetError already setup by SDL_CreateRW() */
  465. }
  466. if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
  467. SDL_DestroyRW(rwops);
  468. return NULL;
  469. }
  470. rwops->size = Android_JNI_FileSize;
  471. rwops->seek = Android_JNI_FileSeek;
  472. rwops->read = Android_JNI_FileRead;
  473. rwops->write = Android_JNI_FileWrite;
  474. rwops->close = Android_JNI_FileClose;
  475. rwops->type = SDL_RWOPS_JNIFILE;
  476. #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_WINRT)
  477. rwops = SDL_CreateRW();
  478. if (!rwops) {
  479. return NULL; /* SDL_SetError already setup by SDL_CreateRW() */
  480. }
  481. if (windows_file_open(rwops, file, mode) < 0) {
  482. SDL_DestroyRW(rwops);
  483. return NULL;
  484. }
  485. rwops->size = windows_file_size;
  486. rwops->seek = windows_file_seek;
  487. rwops->read = windows_file_read;
  488. rwops->write = windows_file_write;
  489. rwops->close = windows_file_close;
  490. rwops->type = SDL_RWOPS_WINFILE;
  491. #elif defined(HAVE_STDIO_H)
  492. {
  493. #if defined(SDL_PLATFORM_APPLE)
  494. FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
  495. #elif defined(SDL_PLATFORM_WINRT)
  496. FILE *fp = NULL;
  497. fopen_s(&fp, file, mode);
  498. #elif defined(SDL_PLATFORM_3DS)
  499. FILE *fp = N3DS_FileOpen(file, mode);
  500. #else
  501. FILE *fp = fopen(file, mode);
  502. #endif
  503. if (!fp) {
  504. SDL_SetError("Couldn't open %s", file);
  505. } else if (!IsRegularFileOrPipe(fp)) {
  506. fclose(fp);
  507. fp = NULL;
  508. SDL_SetError("%s is not a regular file or pipe", file);
  509. } else {
  510. rwops = SDL_RWFromFP(fp, SDL_TRUE);
  511. }
  512. }
  513. #else
  514. SDL_SetError("SDL not compiled with stdio support");
  515. #endif /* !HAVE_STDIO_H */
  516. return rwops;
  517. }
  518. SDL_RWops *SDL_RWFromMem(void *mem, size_t size)
  519. {
  520. SDL_RWops *rwops = NULL;
  521. if (!mem) {
  522. SDL_InvalidParamError("mem");
  523. return NULL;
  524. }
  525. if (!size) {
  526. SDL_InvalidParamError("size");
  527. return NULL;
  528. }
  529. rwops = SDL_CreateRW();
  530. if (rwops) {
  531. rwops->size = mem_size;
  532. rwops->seek = mem_seek;
  533. rwops->read = mem_read;
  534. rwops->write = mem_write;
  535. rwops->hidden.mem.base = (Uint8 *)mem;
  536. rwops->hidden.mem.here = rwops->hidden.mem.base;
  537. rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
  538. rwops->type = SDL_RWOPS_MEMORY;
  539. }
  540. return rwops;
  541. }
  542. SDL_RWops *SDL_RWFromConstMem(const void *mem, size_t size)
  543. {
  544. SDL_RWops *rwops = NULL;
  545. if (!mem) {
  546. SDL_InvalidParamError("mem");
  547. return NULL;
  548. }
  549. if (!size) {
  550. SDL_InvalidParamError("size");
  551. return NULL;
  552. }
  553. rwops = SDL_CreateRW();
  554. if (rwops) {
  555. rwops->size = mem_size;
  556. rwops->seek = mem_seek;
  557. rwops->read = mem_read;
  558. rwops->hidden.mem.base = (Uint8 *)mem;
  559. rwops->hidden.mem.here = rwops->hidden.mem.base;
  560. rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
  561. rwops->type = SDL_RWOPS_MEMORY_RO;
  562. }
  563. return rwops;
  564. }
  565. SDL_RWops *SDL_CreateRW(void)
  566. {
  567. SDL_RWops *context;
  568. context = (SDL_RWops *)SDL_calloc(1, sizeof(*context));
  569. if (context) {
  570. context->type = SDL_RWOPS_UNKNOWN;
  571. }
  572. return context;
  573. }
  574. void SDL_DestroyRW(SDL_RWops *context)
  575. {
  576. SDL_DestroyProperties(context->props);
  577. SDL_free(context);
  578. }
  579. /* Load all the data from an SDL data stream */
  580. void *SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, SDL_bool freesrc)
  581. {
  582. const int FILE_CHUNK_SIZE = 1024;
  583. Sint64 size, size_total = 0;
  584. size_t size_read;
  585. char *data = NULL, *newdata;
  586. SDL_bool loading_chunks = SDL_FALSE;
  587. if (!src) {
  588. SDL_InvalidParamError("src");
  589. goto done;
  590. }
  591. size = SDL_RWsize(src);
  592. if (size < 0) {
  593. size = FILE_CHUNK_SIZE;
  594. loading_chunks = SDL_TRUE;
  595. }
  596. if (size >= SDL_SIZE_MAX) {
  597. goto done;
  598. }
  599. data = (char *)SDL_malloc((size_t)(size + 1));
  600. if (!data) {
  601. goto done;
  602. }
  603. size_total = 0;
  604. for (;;) {
  605. if (loading_chunks) {
  606. if ((size_total + FILE_CHUNK_SIZE) > size) {
  607. size = (size_total + FILE_CHUNK_SIZE);
  608. if (size >= SDL_SIZE_MAX) {
  609. newdata = NULL;
  610. } else {
  611. newdata = (char *)SDL_realloc(data, (size_t)(size + 1));
  612. }
  613. if (!newdata) {
  614. SDL_free(data);
  615. data = NULL;
  616. goto done;
  617. }
  618. data = newdata;
  619. }
  620. }
  621. size_read = SDL_RWread(src, data + size_total, (size_t)(size - size_total));
  622. if (size_read > 0) {
  623. size_total += size_read;
  624. continue;
  625. }
  626. /* The stream status will remain set for the caller to check */
  627. break;
  628. }
  629. data[size_total] = '\0';
  630. done:
  631. if (datasize) {
  632. *datasize = (size_t)size_total;
  633. }
  634. if (freesrc && src) {
  635. SDL_RWclose(src);
  636. }
  637. return data;
  638. }
  639. void *SDL_LoadFile(const char *file, size_t *datasize)
  640. {
  641. return SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, SDL_TRUE);
  642. }
  643. SDL_PropertiesID SDL_GetRWProperties(SDL_RWops *context)
  644. {
  645. if (!context) {
  646. SDL_InvalidParamError("context");
  647. return 0;
  648. }
  649. if (context->props == 0) {
  650. context->props = SDL_CreateProperties();
  651. }
  652. return context->props;
  653. }
  654. Sint64 SDL_RWsize(SDL_RWops *context)
  655. {
  656. if (!context) {
  657. return SDL_InvalidParamError("context");
  658. }
  659. if (!context->size) {
  660. Sint64 pos, size;
  661. pos = SDL_RWseek(context, 0, SDL_RW_SEEK_CUR);
  662. if (pos < 0) {
  663. return -1;
  664. }
  665. size = SDL_RWseek(context, 0, SDL_RW_SEEK_END);
  666. SDL_RWseek(context, pos, SDL_RW_SEEK_SET);
  667. return size;
  668. }
  669. return context->size(context);
  670. }
  671. Sint64 SDL_RWseek(SDL_RWops *context, Sint64 offset, int whence)
  672. {
  673. if (!context) {
  674. return SDL_InvalidParamError("context");
  675. }
  676. if (!context->seek) {
  677. return SDL_Unsupported();
  678. }
  679. return context->seek(context, offset, whence);
  680. }
  681. Sint64 SDL_RWtell(SDL_RWops *context)
  682. {
  683. return SDL_RWseek(context, 0, SDL_RW_SEEK_CUR);
  684. }
  685. size_t SDL_RWread(SDL_RWops *context, void *ptr, size_t size)
  686. {
  687. size_t bytes;
  688. if (!context) {
  689. SDL_InvalidParamError("context");
  690. return 0;
  691. }
  692. if (!context->read) {
  693. context->status = SDL_RWOPS_STATUS_WRITEONLY;
  694. SDL_Unsupported();
  695. return 0;
  696. }
  697. context->status = SDL_RWOPS_STATUS_READY;
  698. SDL_ClearError();
  699. if (size == 0) {
  700. return 0;
  701. }
  702. bytes = context->read(context, ptr, size);
  703. if (bytes == 0 && context->status == SDL_RWOPS_STATUS_READY) {
  704. if (*SDL_GetError()) {
  705. context->status = SDL_RWOPS_STATUS_ERROR;
  706. } else {
  707. context->status = SDL_RWOPS_STATUS_EOF;
  708. }
  709. }
  710. return bytes;
  711. }
  712. size_t SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size)
  713. {
  714. size_t bytes;
  715. if (!context) {
  716. SDL_InvalidParamError("context");
  717. return 0;
  718. }
  719. if (!context->write) {
  720. context->status = SDL_RWOPS_STATUS_READONLY;
  721. SDL_Unsupported();
  722. return 0;
  723. }
  724. context->status = SDL_RWOPS_STATUS_READY;
  725. SDL_ClearError();
  726. if (size == 0) {
  727. return 0;
  728. }
  729. bytes = context->write(context, ptr, size);
  730. if (bytes == 0 && context->status == SDL_RWOPS_STATUS_READY) {
  731. context->status = SDL_RWOPS_STATUS_ERROR;
  732. }
  733. return bytes;
  734. }
  735. size_t SDL_RWprintf(SDL_RWops *context, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  736. {
  737. va_list ap;
  738. int size;
  739. char *string;
  740. size_t bytes;
  741. va_start(ap, fmt);
  742. size = SDL_vasprintf(&string, fmt, ap);
  743. va_end(ap);
  744. if (size < 0) {
  745. return 0;
  746. }
  747. bytes = SDL_RWwrite(context, string, (size_t)size);
  748. SDL_free(string);
  749. return bytes;
  750. }
  751. size_t SDL_RWvprintf(SDL_RWops *context, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
  752. {
  753. int size;
  754. char *string;
  755. size_t bytes;
  756. size = SDL_vasprintf(&string, fmt, ap);
  757. if (size < 0) {
  758. return 0;
  759. }
  760. bytes = SDL_RWwrite(context, string, (size_t)size);
  761. SDL_free(string);
  762. return bytes;
  763. }
  764. int SDL_RWclose(SDL_RWops *context)
  765. {
  766. if (!context) {
  767. return SDL_InvalidParamError("context");
  768. }
  769. if (!context->close) {
  770. SDL_DestroyRW(context);
  771. return 0;
  772. }
  773. return context->close(context);
  774. }
  775. /* Functions for dynamically reading and writing endian-specific values */
  776. SDL_bool SDL_ReadU8(SDL_RWops *src, Uint8 *value)
  777. {
  778. Uint8 data = 0;
  779. SDL_bool result = SDL_FALSE;
  780. if (SDL_RWread(src, &data, sizeof(data)) == sizeof(data)) {
  781. result = SDL_TRUE;
  782. }
  783. if (value) {
  784. *value = data;
  785. }
  786. return result;
  787. }
  788. SDL_bool SDL_ReadU16LE(SDL_RWops *src, Uint16 *value)
  789. {
  790. Uint16 data = 0;
  791. SDL_bool result = SDL_FALSE;
  792. if (SDL_RWread(src, &data, sizeof(data)) == sizeof(data)) {
  793. result = SDL_TRUE;
  794. }
  795. if (value) {
  796. *value = SDL_SwapLE16(data);
  797. }
  798. return result;
  799. }
  800. SDL_bool SDL_ReadS16LE(SDL_RWops *src, Sint16 *value)
  801. {
  802. return SDL_ReadU16LE(src, (Uint16 *)value);
  803. }
  804. SDL_bool SDL_ReadU16BE(SDL_RWops *src, Uint16 *value)
  805. {
  806. Uint16 data = 0;
  807. SDL_bool result = SDL_FALSE;
  808. if (SDL_RWread(src, &data, sizeof(data)) == sizeof(data)) {
  809. result = SDL_TRUE;
  810. }
  811. if (value) {
  812. *value = SDL_SwapBE16(data);
  813. }
  814. return result;
  815. }
  816. SDL_bool SDL_ReadS16BE(SDL_RWops *src, Sint16 *value)
  817. {
  818. return SDL_ReadU16BE(src, (Uint16 *)value);
  819. }
  820. SDL_bool SDL_ReadU32LE(SDL_RWops *src, Uint32 *value)
  821. {
  822. Uint32 data = 0;
  823. SDL_bool result = SDL_FALSE;
  824. if (SDL_RWread(src, &data, sizeof(data)) == sizeof(data)) {
  825. result = SDL_TRUE;
  826. }
  827. if (value) {
  828. *value = SDL_SwapLE32(data);
  829. }
  830. return result;
  831. }
  832. SDL_bool SDL_ReadS32LE(SDL_RWops *src, Sint32 *value)
  833. {
  834. return SDL_ReadU32LE(src, (Uint32 *)value);
  835. }
  836. SDL_bool SDL_ReadU32BE(SDL_RWops *src, Uint32 *value)
  837. {
  838. Uint32 data = 0;
  839. SDL_bool result = SDL_FALSE;
  840. if (SDL_RWread(src, &data, sizeof(data)) == sizeof(data)) {
  841. result = SDL_TRUE;
  842. }
  843. if (value) {
  844. *value = SDL_SwapBE32(data);
  845. }
  846. return result;
  847. }
  848. SDL_bool SDL_ReadS32BE(SDL_RWops *src, Sint32 *value)
  849. {
  850. return SDL_ReadU32BE(src, (Uint32 *)value);
  851. }
  852. SDL_bool SDL_ReadU64LE(SDL_RWops *src, Uint64 *value)
  853. {
  854. Uint64 data = 0;
  855. SDL_bool result = SDL_FALSE;
  856. if (SDL_RWread(src, &data, sizeof(data)) == sizeof(data)) {
  857. result = SDL_TRUE;
  858. }
  859. if (value) {
  860. *value = SDL_SwapLE64(data);
  861. }
  862. return result;
  863. }
  864. SDL_bool SDL_ReadS64LE(SDL_RWops *src, Sint64 *value)
  865. {
  866. return SDL_ReadU64LE(src, (Uint64 *)value);
  867. }
  868. SDL_bool SDL_ReadU64BE(SDL_RWops *src, Uint64 *value)
  869. {
  870. Uint64 data = 0;
  871. SDL_bool result = SDL_FALSE;
  872. if (SDL_RWread(src, &data, sizeof(data)) == sizeof(data)) {
  873. result = SDL_TRUE;
  874. }
  875. if (value) {
  876. *value = SDL_SwapBE64(data);
  877. }
  878. return result;
  879. }
  880. SDL_bool SDL_ReadS64BE(SDL_RWops *src, Sint64 *value)
  881. {
  882. return SDL_ReadU64BE(src, (Uint64 *)value);
  883. }
  884. SDL_bool SDL_WriteU8(SDL_RWops *dst, Uint8 value)
  885. {
  886. return (SDL_RWwrite(dst, &value, sizeof(value)) == sizeof(value));
  887. }
  888. SDL_bool SDL_WriteU16LE(SDL_RWops *dst, Uint16 value)
  889. {
  890. const Uint16 swapped = SDL_SwapLE16(value);
  891. return (SDL_RWwrite(dst, &swapped, sizeof(swapped)) == sizeof(swapped));
  892. }
  893. SDL_bool SDL_WriteS16LE(SDL_RWops *dst, Sint16 value)
  894. {
  895. return SDL_WriteU16LE(dst, (Uint16)value);
  896. }
  897. SDL_bool SDL_WriteU16BE(SDL_RWops *dst, Uint16 value)
  898. {
  899. const Uint16 swapped = SDL_SwapBE16(value);
  900. return (SDL_RWwrite(dst, &swapped, sizeof(swapped)) == sizeof(swapped));
  901. }
  902. SDL_bool SDL_WriteS16BE(SDL_RWops *dst, Sint16 value)
  903. {
  904. return SDL_WriteU16BE(dst, (Uint16)value);
  905. }
  906. SDL_bool SDL_WriteU32LE(SDL_RWops *dst, Uint32 value)
  907. {
  908. const Uint32 swapped = SDL_SwapLE32(value);
  909. return (SDL_RWwrite(dst, &swapped, sizeof(swapped)) == sizeof(swapped));
  910. }
  911. SDL_bool SDL_WriteS32LE(SDL_RWops *dst, Sint32 value)
  912. {
  913. return SDL_WriteU32LE(dst, (Uint32)value);
  914. }
  915. SDL_bool SDL_WriteU32BE(SDL_RWops *dst, Uint32 value)
  916. {
  917. const Uint32 swapped = SDL_SwapBE32(value);
  918. return (SDL_RWwrite(dst, &swapped, sizeof(swapped)) == sizeof(swapped));
  919. }
  920. SDL_bool SDL_WriteS32BE(SDL_RWops *dst, Sint32 value)
  921. {
  922. return SDL_WriteU32BE(dst, (Uint32)value);
  923. }
  924. SDL_bool SDL_WriteU64LE(SDL_RWops *dst, Uint64 value)
  925. {
  926. const Uint64 swapped = SDL_SwapLE64(value);
  927. return (SDL_RWwrite(dst, &swapped, sizeof(swapped)) == sizeof(swapped));
  928. }
  929. SDL_bool SDL_WriteS64LE(SDL_RWops *dst, Sint64 value)
  930. {
  931. return SDL_WriteU64LE(dst, (Uint64)value);
  932. }
  933. SDL_bool SDL_WriteU64BE(SDL_RWops *dst, Uint64 value)
  934. {
  935. const Uint64 swapped = SDL_SwapBE64(value);
  936. return (SDL_RWwrite(dst, &swapped, sizeof(swapped)) == sizeof(swapped));
  937. }
  938. SDL_bool SDL_WriteS64BE(SDL_RWops *dst, Sint64 value)
  939. {
  940. return SDL_WriteU64BE(dst, (Uint64)value);
  941. }