SDL_rwops.c 24 KB

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