SDL_rwops.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. /* We won't get fseeko64 on QNX if _LARGEFILE64_SOURCE is defined, but the
  19. configure script knows the C runtime has it and enables it. */
  20. #ifndef __QNXNTO__
  21. /* Need this so Linux systems define fseek64o, ftell64o and off64_t */
  22. #ifndef _LARGEFILE64_SOURCE
  23. #define _LARGEFILE64_SOURCE
  24. #endif
  25. #endif
  26. #include "../SDL_internal.h"
  27. #if defined(__WIN32__) || defined(__GDK__)
  28. #include "../core/windows/SDL_windows.h"
  29. #endif
  30. #ifdef HAVE_STDIO_H
  31. #include <stdio.h>
  32. #endif
  33. #ifdef HAVE_LIMITS_H
  34. #include <limits.h>
  35. #endif
  36. /* This file provides a general interface for SDL to read and write
  37. data sources. It can easily be extended to files, memory, etc.
  38. */
  39. #include "SDL_endian.h"
  40. #include "SDL_rwops.h"
  41. #ifdef __APPLE__
  42. #include "cocoa/SDL_rwopsbundlesupport.h"
  43. #endif /* __APPLE__ */
  44. #ifdef __3DS__
  45. #include "n3ds/SDL_rwopsromfs.h"
  46. #endif /* __3DS__ */
  47. #ifdef __ANDROID__
  48. #include "../core/android/SDL_android.h"
  49. #include "SDL_system.h"
  50. #endif
  51. #if __NACL__
  52. #include "nacl_io/nacl_io.h"
  53. #endif
  54. #if defined(__WIN32__) || defined(__GDK__)
  55. /* Functions to read/write Win32 API file pointers */
  56. #ifndef INVALID_SET_FILE_POINTER
  57. #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
  58. #endif
  59. #define READAHEAD_BUFFER_SIZE 1024
  60. static int SDLCALL windows_file_open(SDL_RWops *context, const char *filename, const char *mode)
  61. {
  62. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  63. UINT old_error_mode;
  64. #endif
  65. HANDLE h;
  66. DWORD r_right, w_right;
  67. DWORD must_exist, truncate;
  68. int a_mode;
  69. if (context == NULL) {
  70. return -1; /* failed (invalid call) */
  71. }
  72. context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
  73. context->hidden.windowsio.buffer.data = NULL;
  74. context->hidden.windowsio.buffer.size = 0;
  75. context->hidden.windowsio.buffer.left = 0;
  76. /* "r" = reading, file must exist */
  77. /* "w" = writing, truncate existing, file may not exist */
  78. /* "r+"= reading or writing, file must exist */
  79. /* "a" = writing, append file may not exist */
  80. /* "a+"= append + read, file may not exist */
  81. /* "w+" = read, write, truncate. file may not exist */
  82. must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
  83. truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
  84. r_right = (SDL_strchr(mode, '+') != NULL || must_exist) ? GENERIC_READ : 0;
  85. a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
  86. w_right = (a_mode || SDL_strchr(mode, '+') || truncate) ? GENERIC_WRITE : 0;
  87. if (!r_right && !w_right) {
  88. return -1; /* inconsistent mode */
  89. }
  90. /* failed (invalid call) */
  91. context->hidden.windowsio.buffer.data =
  92. (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
  93. if (!context->hidden.windowsio.buffer.data) {
  94. return SDL_OutOfMemory();
  95. }
  96. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  97. /* Do not open a dialog box if failure */
  98. old_error_mode =
  99. SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  100. #endif
  101. {
  102. LPTSTR tstr = WIN_UTF8ToString(filename);
  103. h = CreateFile(tstr, (w_right | r_right),
  104. (w_right) ? 0 : FILE_SHARE_READ, NULL,
  105. (must_exist | truncate | a_mode),
  106. FILE_ATTRIBUTE_NORMAL, NULL);
  107. SDL_free(tstr);
  108. }
  109. #if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
  110. /* restore old behavior */
  111. SetErrorMode(old_error_mode);
  112. #endif
  113. if (h == INVALID_HANDLE_VALUE) {
  114. SDL_free(context->hidden.windowsio.buffer.data);
  115. context->hidden.windowsio.buffer.data = NULL;
  116. SDL_SetError("Couldn't open %s", filename);
  117. return -2; /* failed (CreateFile) */
  118. }
  119. context->hidden.windowsio.h = h;
  120. context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
  121. return 0; /* ok */
  122. }
  123. static Sint64 SDLCALL windows_file_size(SDL_RWops *context)
  124. {
  125. LARGE_INTEGER size;
  126. if (context == NULL || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
  127. return SDL_SetError("windows_file_size: invalid context/file not opened");
  128. }
  129. if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) {
  130. return WIN_SetError("windows_file_size");
  131. }
  132. return size.QuadPart;
  133. }
  134. static Sint64 SDLCALL windows_file_seek(SDL_RWops *context, Sint64 offset, int whence)
  135. {
  136. DWORD windowswhence;
  137. LARGE_INTEGER windowsoffset;
  138. if (context == NULL || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
  139. return SDL_SetError("windows_file_seek: invalid context/file not opened");
  140. }
  141. /* FIXME: We may be able to satisfy the seek within buffered data */
  142. if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
  143. offset -= (long)context->hidden.windowsio.buffer.left;
  144. }
  145. context->hidden.windowsio.buffer.left = 0;
  146. switch (whence) {
  147. case RW_SEEK_SET:
  148. windowswhence = FILE_BEGIN;
  149. break;
  150. case RW_SEEK_CUR:
  151. windowswhence = FILE_CURRENT;
  152. break;
  153. case RW_SEEK_END:
  154. windowswhence = FILE_END;
  155. break;
  156. default:
  157. return SDL_SetError("windows_file_seek: Unknown value for 'whence'");
  158. }
  159. windowsoffset.QuadPart = offset;
  160. if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) {
  161. return WIN_SetError("windows_file_seek");
  162. }
  163. return windowsoffset.QuadPart;
  164. }
  165. static size_t SDLCALL
  166. windows_file_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
  167. {
  168. size_t total_need;
  169. size_t total_read = 0;
  170. size_t read_ahead;
  171. DWORD byte_read;
  172. total_need = size * maxnum;
  173. if (context == NULL || context->hidden.windowsio.h == INVALID_HANDLE_VALUE || !total_need) {
  174. return 0;
  175. }
  176. if (context->hidden.windowsio.buffer.left > 0) {
  177. void *data = (char *)context->hidden.windowsio.buffer.data +
  178. context->hidden.windowsio.buffer.size -
  179. context->hidden.windowsio.buffer.left;
  180. read_ahead =
  181. SDL_min(total_need, context->hidden.windowsio.buffer.left);
  182. SDL_memcpy(ptr, data, read_ahead);
  183. context->hidden.windowsio.buffer.left -= read_ahead;
  184. if (read_ahead == total_need) {
  185. return maxnum;
  186. }
  187. ptr = (char *)ptr + read_ahead;
  188. total_need -= read_ahead;
  189. total_read += read_ahead;
  190. }
  191. if (total_need < READAHEAD_BUFFER_SIZE) {
  192. if (!ReadFile(context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
  193. READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
  194. SDL_Error(SDL_EFREAD);
  195. return 0;
  196. }
  197. read_ahead = SDL_min(total_need, (int)byte_read);
  198. SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
  199. context->hidden.windowsio.buffer.size = byte_read;
  200. context->hidden.windowsio.buffer.left = byte_read - read_ahead;
  201. total_read += read_ahead;
  202. } else {
  203. if (!ReadFile(context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
  204. SDL_Error(SDL_EFREAD);
  205. return 0;
  206. }
  207. total_read += byte_read;
  208. }
  209. return total_read / size;
  210. }
  211. static size_t SDLCALL
  212. windows_file_write(SDL_RWops *context, const void *ptr, size_t size,
  213. size_t num)
  214. {
  215. size_t total_bytes;
  216. DWORD byte_written;
  217. size_t nwritten;
  218. total_bytes = size * num;
  219. if (context == NULL || context->hidden.windowsio.h == INVALID_HANDLE_VALUE || !size || !total_bytes) {
  220. return 0;
  221. }
  222. if (context->hidden.windowsio.buffer.left) {
  223. SetFilePointer(context->hidden.windowsio.h,
  224. -(LONG)context->hidden.windowsio.buffer.left, NULL,
  225. FILE_CURRENT);
  226. context->hidden.windowsio.buffer.left = 0;
  227. }
  228. /* if in append mode, we must go to the EOF before write */
  229. if (context->hidden.windowsio.append) {
  230. if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
  231. INVALID_SET_FILE_POINTER) {
  232. SDL_Error(SDL_EFWRITE);
  233. return 0;
  234. }
  235. }
  236. if (!WriteFile(context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
  237. SDL_Error(SDL_EFWRITE);
  238. return 0;
  239. }
  240. nwritten = byte_written / size;
  241. return nwritten;
  242. }
  243. static int SDLCALL windows_file_close(SDL_RWops *context)
  244. {
  245. if (context) {
  246. if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
  247. CloseHandle(context->hidden.windowsio.h);
  248. context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* to be sure */
  249. }
  250. SDL_free(context->hidden.windowsio.buffer.data);
  251. context->hidden.windowsio.buffer.data = NULL;
  252. SDL_FreeRW(context);
  253. }
  254. return 0;
  255. }
  256. #endif /* defined(__WIN32__) || defined(__GDK__) */
  257. #ifdef HAVE_STDIO_H
  258. #ifdef HAVE_FOPEN64
  259. #define fopen fopen64
  260. #endif
  261. #ifdef HAVE_FSEEKO64
  262. #define fseek_off_t off64_t
  263. #define fseek fseeko64
  264. #define ftell ftello64
  265. #elif defined(HAVE_FSEEKO)
  266. #if defined(OFF_MIN) && defined(OFF_MAX)
  267. #define FSEEK_OFF_MIN OFF_MIN
  268. #define FSEEK_OFF_MAX OFF_MAX
  269. #elif defined(HAVE_LIMITS_H)
  270. /* POSIX doesn't specify the minimum and maximum macros for off_t so
  271. * we have to improvise and dance around implementation-defined
  272. * behavior. This may fail if the off_t type has padding bits or
  273. * is not a two's-complement representation. The compilers will detect
  274. * and eliminate the dead code if off_t has 64 bits.
  275. */
  276. #define FSEEK_OFF_MAX (((((off_t)1 << (sizeof(off_t) * CHAR_BIT - 2)) - 1) << 1) + 1)
  277. #define FSEEK_OFF_MIN (-(FSEEK_OFF_MAX)-1)
  278. #endif
  279. #define fseek_off_t off_t
  280. #define fseek fseeko
  281. #define ftell ftello
  282. #elif defined(HAVE__FSEEKI64)
  283. #define fseek_off_t __int64
  284. #define fseek _fseeki64
  285. #define ftell _ftelli64
  286. #else
  287. #ifdef HAVE_LIMITS_H
  288. #define FSEEK_OFF_MIN LONG_MIN
  289. #define FSEEK_OFF_MAX LONG_MAX
  290. #endif
  291. #define fseek_off_t long
  292. #endif
  293. /* Functions to read/write stdio file pointers */
  294. static Sint64 SDLCALL
  295. stdio_size(SDL_RWops * context)
  296. {
  297. Sint64 pos, size;
  298. pos = SDL_RWseek(context, 0, RW_SEEK_CUR);
  299. if (pos < 0) {
  300. return -1;
  301. }
  302. size = SDL_RWseek(context, 0, RW_SEEK_END);
  303. SDL_RWseek(context, pos, RW_SEEK_SET);
  304. return size;
  305. }
  306. static Sint64 SDLCALL stdio_seek(SDL_RWops *context, Sint64 offset, int whence)
  307. {
  308. int stdiowhence;
  309. switch (whence) {
  310. case RW_SEEK_SET:
  311. stdiowhence = SEEK_SET;
  312. break;
  313. case RW_SEEK_CUR:
  314. stdiowhence = SEEK_CUR;
  315. break;
  316. case RW_SEEK_END:
  317. stdiowhence = SEEK_END;
  318. break;
  319. default:
  320. return SDL_SetError("Unknown value for 'whence'");
  321. }
  322. #if defined(FSEEK_OFF_MIN) && defined(FSEEK_OFF_MAX)
  323. if (offset < (Sint64)(FSEEK_OFF_MIN) || offset > (Sint64)(FSEEK_OFF_MAX)) {
  324. return SDL_SetError("Seek offset out of range");
  325. }
  326. #endif
  327. if (fseek(context->hidden.stdio.fp, (fseek_off_t)offset, stdiowhence) == 0) {
  328. Sint64 pos = ftell(context->hidden.stdio.fp);
  329. if (pos < 0) {
  330. return SDL_SetError("Couldn't get stream offset");
  331. }
  332. return pos;
  333. }
  334. return SDL_Error(SDL_EFSEEK);
  335. }
  336. static size_t SDLCALL
  337. stdio_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
  338. {
  339. size_t nread;
  340. nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
  341. if (nread == 0 && ferror(context->hidden.stdio.fp)) {
  342. SDL_Error(SDL_EFREAD);
  343. }
  344. return nread;
  345. }
  346. static size_t SDLCALL
  347. stdio_write(SDL_RWops *context, const void *ptr, size_t size, size_t num)
  348. {
  349. size_t nwrote;
  350. nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
  351. if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
  352. SDL_Error(SDL_EFWRITE);
  353. }
  354. return nwrote;
  355. }
  356. static int SDLCALL stdio_close(SDL_RWops *context)
  357. {
  358. int status = 0;
  359. if (context) {
  360. if (context->hidden.stdio.autoclose) {
  361. /* WARNING: Check the return value here! */
  362. if (fclose(context->hidden.stdio.fp) != 0) {
  363. status = SDL_Error(SDL_EFWRITE);
  364. }
  365. }
  366. SDL_FreeRW(context);
  367. }
  368. return status;
  369. }
  370. #endif /* !HAVE_STDIO_H */
  371. /* Functions to read/write memory pointers */
  372. static Sint64 SDLCALL mem_size(SDL_RWops *context)
  373. {
  374. return (Sint64)(context->hidden.mem.stop - context->hidden.mem.base);
  375. }
  376. static Sint64 SDLCALL mem_seek(SDL_RWops *context, Sint64 offset, int whence)
  377. {
  378. Uint8 *newpos;
  379. switch (whence) {
  380. case RW_SEEK_SET:
  381. newpos = context->hidden.mem.base + offset;
  382. break;
  383. case RW_SEEK_CUR:
  384. newpos = context->hidden.mem.here + offset;
  385. break;
  386. case RW_SEEK_END:
  387. newpos = context->hidden.mem.stop + offset;
  388. break;
  389. default:
  390. return SDL_SetError("Unknown value for 'whence'");
  391. }
  392. if (newpos < context->hidden.mem.base) {
  393. newpos = context->hidden.mem.base;
  394. }
  395. if (newpos > context->hidden.mem.stop) {
  396. newpos = context->hidden.mem.stop;
  397. }
  398. context->hidden.mem.here = newpos;
  399. return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
  400. }
  401. static size_t SDLCALL
  402. mem_read(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
  403. {
  404. size_t total_bytes;
  405. size_t mem_available;
  406. total_bytes = (maxnum * size);
  407. if (!maxnum || !size || ((total_bytes / maxnum) != size)) {
  408. return 0;
  409. }
  410. mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
  411. if (total_bytes > mem_available) {
  412. total_bytes = mem_available;
  413. }
  414. SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
  415. context->hidden.mem.here += total_bytes;
  416. return total_bytes / size;
  417. }
  418. static size_t SDLCALL
  419. mem_write(SDL_RWops *context, const void *ptr, size_t size, size_t num)
  420. {
  421. if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
  422. num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
  423. }
  424. SDL_memcpy(context->hidden.mem.here, ptr, num * size);
  425. context->hidden.mem.here += num * size;
  426. return num;
  427. }
  428. static size_t SDLCALL
  429. mem_writeconst(SDL_RWops *context, const void *ptr, size_t size, size_t num)
  430. {
  431. SDL_SetError("Can't write to read-only memory");
  432. return 0;
  433. }
  434. static int SDLCALL mem_close(SDL_RWops *context)
  435. {
  436. if (context) {
  437. SDL_FreeRW(context);
  438. }
  439. return 0;
  440. }
  441. /* Functions to create SDL_RWops structures from various data sources */
  442. SDL_RWops *
  443. SDL_RWFromFile(const char *file, const char *mode)
  444. {
  445. SDL_RWops *rwops = NULL;
  446. if (file == NULL || !*file || mode == NULL || !*mode) {
  447. SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
  448. return NULL;
  449. }
  450. #if defined(__ANDROID__)
  451. #ifdef HAVE_STDIO_H
  452. /* Try to open the file on the filesystem first */
  453. if (*file == '/') {
  454. FILE *fp = fopen(file, mode);
  455. if (fp) {
  456. return SDL_RWFromFP(fp, 1);
  457. }
  458. } else {
  459. /* Try opening it from internal storage if it's a relative path */
  460. char *path;
  461. FILE *fp;
  462. /* !!! FIXME: why not just "char path[PATH_MAX];" ? */
  463. path = SDL_stack_alloc(char, PATH_MAX);
  464. if (path) {
  465. SDL_snprintf(path, PATH_MAX, "%s/%s",
  466. SDL_AndroidGetInternalStoragePath(), file);
  467. fp = fopen(path, mode);
  468. SDL_stack_free(path);
  469. if (fp) {
  470. return SDL_RWFromFP(fp, 1);
  471. }
  472. }
  473. }
  474. #endif /* HAVE_STDIO_H */
  475. /* Try to open the file from the asset system */
  476. rwops = SDL_AllocRW();
  477. if (rwops == NULL) {
  478. return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
  479. }
  480. if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
  481. SDL_FreeRW(rwops);
  482. return NULL;
  483. }
  484. rwops->size = Android_JNI_FileSize;
  485. rwops->seek = Android_JNI_FileSeek;
  486. rwops->read = Android_JNI_FileRead;
  487. rwops->write = Android_JNI_FileWrite;
  488. rwops->close = Android_JNI_FileClose;
  489. rwops->type = SDL_RWOPS_JNIFILE;
  490. #elif defined(__WIN32__) || defined(__GDK__)
  491. rwops = SDL_AllocRW();
  492. if (rwops == NULL) {
  493. return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
  494. }
  495. if (windows_file_open(rwops, file, mode) < 0) {
  496. SDL_FreeRW(rwops);
  497. return NULL;
  498. }
  499. rwops->size = windows_file_size;
  500. rwops->seek = windows_file_seek;
  501. rwops->read = windows_file_read;
  502. rwops->write = windows_file_write;
  503. rwops->close = windows_file_close;
  504. rwops->type = SDL_RWOPS_WINFILE;
  505. #elif HAVE_STDIO_H
  506. {
  507. #if __APPLE__ && !SDL_FILE_DISABLED // TODO: add dummy?
  508. FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
  509. #elif __WINRT__
  510. FILE *fp = NULL;
  511. fopen_s(&fp, file, mode);
  512. #elif __3DS__
  513. FILE *fp = N3DS_FileOpen(file, mode);
  514. #else
  515. FILE *fp = fopen(file, mode);
  516. #endif
  517. if (fp == NULL) {
  518. SDL_SetError("Couldn't open %s", file);
  519. } else {
  520. rwops = SDL_RWFromFP(fp, SDL_TRUE);
  521. }
  522. }
  523. #else
  524. SDL_SetError("SDL not compiled with stdio support");
  525. #endif /* !HAVE_STDIO_H */
  526. return rwops;
  527. }
  528. #ifdef HAVE_STDIO_H
  529. SDL_RWops *
  530. SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
  531. {
  532. SDL_RWops *rwops = NULL;
  533. rwops = SDL_AllocRW();
  534. if (rwops != NULL) {
  535. rwops->size = stdio_size;
  536. rwops->seek = stdio_seek;
  537. rwops->read = stdio_read;
  538. rwops->write = stdio_write;
  539. rwops->close = stdio_close;
  540. rwops->hidden.stdio.fp = fp;
  541. rwops->hidden.stdio.autoclose = autoclose;
  542. rwops->type = SDL_RWOPS_STDFILE;
  543. }
  544. return rwops;
  545. }
  546. #else
  547. SDL_RWops *
  548. SDL_RWFromFP(void * fp, SDL_bool autoclose)
  549. {
  550. SDL_SetError("SDL not compiled with stdio support");
  551. return NULL;
  552. }
  553. #endif /* HAVE_STDIO_H */
  554. SDL_RWops *
  555. SDL_RWFromMem(void *mem, int size)
  556. {
  557. SDL_RWops *rwops = NULL;
  558. if (mem == NULL) {
  559. SDL_InvalidParamError("mem");
  560. return rwops;
  561. }
  562. if (!size) {
  563. SDL_InvalidParamError("size");
  564. return rwops;
  565. }
  566. rwops = SDL_AllocRW();
  567. if (rwops != NULL) {
  568. rwops->size = mem_size;
  569. rwops->seek = mem_seek;
  570. rwops->read = mem_read;
  571. rwops->write = mem_write;
  572. rwops->close = mem_close;
  573. rwops->hidden.mem.base = (Uint8 *)mem;
  574. rwops->hidden.mem.here = rwops->hidden.mem.base;
  575. rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
  576. rwops->type = SDL_RWOPS_MEMORY;
  577. }
  578. return rwops;
  579. }
  580. SDL_RWops *
  581. SDL_RWFromConstMem(const void *mem, int size)
  582. {
  583. SDL_RWops *rwops = NULL;
  584. if (mem == NULL) {
  585. SDL_InvalidParamError("mem");
  586. return rwops;
  587. }
  588. if (!size) {
  589. SDL_InvalidParamError("size");
  590. return rwops;
  591. }
  592. rwops = SDL_AllocRW();
  593. if (rwops != NULL) {
  594. rwops->size = mem_size;
  595. rwops->seek = mem_seek;
  596. rwops->read = mem_read;
  597. rwops->write = mem_writeconst;
  598. rwops->close = mem_close;
  599. rwops->hidden.mem.base = (Uint8 *)mem;
  600. rwops->hidden.mem.here = rwops->hidden.mem.base;
  601. rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
  602. rwops->type = SDL_RWOPS_MEMORY_RO;
  603. }
  604. return rwops;
  605. }
  606. SDL_RWops *
  607. SDL_AllocRW(void)
  608. {
  609. SDL_RWops *area;
  610. area = (SDL_RWops *)SDL_malloc(sizeof(*area));
  611. if (area == NULL) {
  612. SDL_OutOfMemory();
  613. } else {
  614. area->type = SDL_RWOPS_UNKNOWN;
  615. }
  616. return area;
  617. }
  618. void SDL_FreeRW(SDL_RWops *area)
  619. {
  620. SDL_free(area);
  621. }
  622. /* Load all the data from an SDL data stream */
  623. void *
  624. SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, int freesrc)
  625. {
  626. static const Sint64 FILE_CHUNK_SIZE = 1024;
  627. Sint64 size;
  628. size_t size_read, size_total;
  629. void *data = NULL, *newdata;
  630. if (src == NULL) {
  631. SDL_InvalidParamError("src");
  632. return NULL;
  633. }
  634. size = SDL_RWsize(src);
  635. if (size < 0) {
  636. size = FILE_CHUNK_SIZE;
  637. }
  638. data = SDL_malloc((size_t)(size + 1));
  639. size_total = 0;
  640. for (;;) {
  641. if ((((Sint64)size_total) + FILE_CHUNK_SIZE) > size) {
  642. size = (size_total + FILE_CHUNK_SIZE);
  643. newdata = SDL_realloc(data, (size_t)(size + 1));
  644. if (newdata == NULL) {
  645. SDL_free(data);
  646. data = NULL;
  647. SDL_OutOfMemory();
  648. goto done;
  649. }
  650. data = newdata;
  651. }
  652. size_read = SDL_RWread(src, (char *)data + size_total, 1, (size_t)(size - size_total));
  653. if (size_read == 0) {
  654. break;
  655. }
  656. size_total += size_read;
  657. }
  658. if (datasize) {
  659. *datasize = size_total;
  660. }
  661. ((char *)data)[size_total] = '\0';
  662. done:
  663. if (freesrc && src) {
  664. SDL_RWclose(src);
  665. }
  666. return data;
  667. }
  668. void *
  669. SDL_LoadFile(const char *file, size_t *datasize)
  670. {
  671. return SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1);
  672. }
  673. Sint64
  674. SDL_RWsize(SDL_RWops *context)
  675. {
  676. return context->size(context);
  677. }
  678. Sint64
  679. SDL_RWseek(SDL_RWops *context, Sint64 offset, int whence)
  680. {
  681. return context->seek(context, offset, whence);
  682. }
  683. Sint64
  684. SDL_RWtell(SDL_RWops *context)
  685. {
  686. return context->seek(context, 0, RW_SEEK_CUR);
  687. }
  688. size_t
  689. SDL_RWread(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
  690. {
  691. return context->read(context, ptr, size, maxnum);
  692. }
  693. size_t
  694. SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size, size_t num)
  695. {
  696. return context->write(context, ptr, size, num);
  697. }
  698. int SDL_RWclose(SDL_RWops *context)
  699. {
  700. return context->close(context);
  701. }
  702. /* Functions for dynamically reading and writing endian-specific values */
  703. Uint8 SDL_ReadU8(SDL_RWops *src)
  704. {
  705. Uint8 value = 0;
  706. SDL_RWread(src, &value, sizeof(value), 1);
  707. return value;
  708. }
  709. Uint16
  710. SDL_ReadLE16(SDL_RWops *src)
  711. {
  712. Uint16 value = 0;
  713. SDL_RWread(src, &value, sizeof(value), 1);
  714. return SDL_SwapLE16(value);
  715. }
  716. Uint16
  717. SDL_ReadBE16(SDL_RWops *src)
  718. {
  719. Uint16 value = 0;
  720. SDL_RWread(src, &value, sizeof(value), 1);
  721. return SDL_SwapBE16(value);
  722. }
  723. Uint32
  724. SDL_ReadLE32(SDL_RWops *src)
  725. {
  726. Uint32 value = 0;
  727. SDL_RWread(src, &value, sizeof(value), 1);
  728. return SDL_SwapLE32(value);
  729. }
  730. Uint32
  731. SDL_ReadBE32(SDL_RWops *src)
  732. {
  733. Uint32 value = 0;
  734. SDL_RWread(src, &value, sizeof(value), 1);
  735. return SDL_SwapBE32(value);
  736. }
  737. Uint64
  738. SDL_ReadLE64(SDL_RWops *src)
  739. {
  740. Uint64 value = 0;
  741. SDL_RWread(src, &value, sizeof(value), 1);
  742. return SDL_SwapLE64(value);
  743. }
  744. Uint64
  745. SDL_ReadBE64(SDL_RWops *src)
  746. {
  747. Uint64 value = 0;
  748. SDL_RWread(src, &value, sizeof(value), 1);
  749. return SDL_SwapBE64(value);
  750. }
  751. size_t
  752. SDL_WriteU8(SDL_RWops *dst, Uint8 value)
  753. {
  754. return SDL_RWwrite(dst, &value, sizeof(value), 1);
  755. }
  756. size_t
  757. SDL_WriteLE16(SDL_RWops *dst, Uint16 value)
  758. {
  759. const Uint16 swapped = SDL_SwapLE16(value);
  760. return SDL_RWwrite(dst, &swapped, sizeof(swapped), 1);
  761. }
  762. size_t
  763. SDL_WriteBE16(SDL_RWops *dst, Uint16 value)
  764. {
  765. const Uint16 swapped = SDL_SwapBE16(value);
  766. return SDL_RWwrite(dst, &swapped, sizeof(swapped), 1);
  767. }
  768. size_t
  769. SDL_WriteLE32(SDL_RWops *dst, Uint32 value)
  770. {
  771. const Uint32 swapped = SDL_SwapLE32(value);
  772. return SDL_RWwrite(dst, &swapped, sizeof(swapped), 1);
  773. }
  774. size_t
  775. SDL_WriteBE32(SDL_RWops *dst, Uint32 value)
  776. {
  777. const Uint32 swapped = SDL_SwapBE32(value);
  778. return SDL_RWwrite(dst, &swapped, sizeof(swapped), 1);
  779. }
  780. size_t
  781. SDL_WriteLE64(SDL_RWops *dst, Uint64 value)
  782. {
  783. const Uint64 swapped = SDL_SwapLE64(value);
  784. return SDL_RWwrite(dst, &swapped, sizeof(swapped), 1);
  785. }
  786. size_t
  787. SDL_WriteBE64(SDL_RWops *dst, Uint64 value)
  788. {
  789. const Uint64 swapped = SDL_SwapBE64(value);
  790. return SDL_RWwrite(dst, &swapped, sizeof(swapped), 1);
  791. }
  792. /* vi: set ts=4 sw=4 expandtab: */