SDL_rwops.c 27 KB

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