SDL_rwops.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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__)
  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 __ANDROID__
  45. #include "../core/android/SDL_android.h"
  46. #include "SDL_system.h"
  47. #endif
  48. #if __NACL__
  49. #include "nacl_io/nacl_io.h"
  50. #endif
  51. #ifdef __VITA__
  52. #include <psp2/io/fcntl.h>
  53. #include <psp2/io/stat.h>
  54. #define READAHEAD_BUFFER_SIZE 1024
  55. static int SDLCALL
  56. vita_file_open(SDL_RWops * context, const char *filename, const char *mode)
  57. {
  58. int h;
  59. int open_flags;
  60. SDL_bool has_r;
  61. SDL_bool has_w;
  62. SDL_bool has_a;
  63. SDL_bool has_plus;
  64. if (!context)
  65. return -1; /* failed (invalid call) */
  66. context->hidden.vitaio.h = -1; /* mark this as unusable */
  67. context->hidden.vitaio.buffer.data = NULL;
  68. context->hidden.vitaio.buffer.size = 0;
  69. context->hidden.vitaio.buffer.left = 0;
  70. open_flags = 0;
  71. /* "r" = reading, file must exist */
  72. /* "w" = writing, truncate existing, file may not exist */
  73. /* "r+"= reading or writing, file must exist */
  74. /* "a" = writing, append file may not exist */
  75. /* "a+"= append + read, file may not exist */
  76. /* "w+" = read, write, truncate. file may not exist */
  77. has_r = SDL_strchr(mode, 'r') != NULL;
  78. has_w = SDL_strchr(mode, 'w') != NULL;
  79. has_a = SDL_strchr(mode, 'a') != NULL;
  80. has_plus = SDL_strchr(mode, '+') != NULL;
  81. if (has_plus)
  82. {
  83. if (has_r || has_w || has_a)
  84. {
  85. open_flags |= SCE_O_RDWR;
  86. }
  87. }
  88. else
  89. {
  90. if (has_r)
  91. {
  92. open_flags |= SCE_O_RDONLY;
  93. }
  94. if (has_w || has_a)
  95. {
  96. open_flags |= SCE_O_WRONLY;
  97. }
  98. }
  99. if (has_w || has_a)
  100. {
  101. open_flags |= SCE_O_CREAT;
  102. }
  103. if (has_w)
  104. {
  105. open_flags |= SCE_O_TRUNC;
  106. }
  107. if (has_a)
  108. {
  109. open_flags |= SCE_O_APPEND;
  110. }
  111. context->hidden.vitaio.buffer.data =
  112. (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
  113. if (!context->hidden.vitaio.buffer.data) {
  114. return SDL_OutOfMemory();
  115. }
  116. /* Try to open the file on the filesystem first */
  117. h = sceIoOpen(filename, open_flags, 0777);
  118. if (h < 0) {
  119. /* Try opening it from app0:/ container if it's a relative path */
  120. char path[4096];
  121. SDL_snprintf(path, 4096, "app0:/%s", filename);
  122. h = sceIoOpen(path, open_flags, 0777);
  123. }
  124. if (h < 0) {
  125. SDL_free(context->hidden.vitaio.buffer.data);
  126. context->hidden.vitaio.buffer.data = NULL;
  127. SDL_SetError("Couldn't open %s", filename);
  128. return -2; /* failed (sceIoOpen) */
  129. }
  130. context->hidden.vitaio.h = h;
  131. return 0; /* ok */
  132. }
  133. static Sint64 SDLCALL
  134. vita_file_size(SDL_RWops * context)
  135. {
  136. SceIoStat st;
  137. if (!context || context->hidden.vitaio.h < 0) {
  138. return SDL_SetError("vita_file_size: invalid context/file not opened");
  139. }
  140. if (sceIoGetstatByFd(context->hidden.vitaio.h, &st) < 0) {
  141. return SDL_SetError("vita_file_size: could not get file size");
  142. }
  143. return st.st_size;
  144. }
  145. static Sint64 SDLCALL
  146. vita_file_seek(SDL_RWops * context, Sint64 offset, int whence)
  147. {
  148. int vitawhence;
  149. if (!context || context->hidden.vitaio.h < 0) {
  150. return SDL_SetError("vita_file_seek: invalid context/file not opened");
  151. }
  152. /* FIXME: We may be able to satisfy the seek within buffered data */
  153. if (whence == RW_SEEK_CUR && context->hidden.vitaio.buffer.left) {
  154. offset -= (long)context->hidden.vitaio.buffer.left;
  155. }
  156. context->hidden.vitaio.buffer.left = 0;
  157. switch (whence) {
  158. case RW_SEEK_SET:
  159. vitawhence = SCE_SEEK_SET;
  160. break;
  161. case RW_SEEK_CUR:
  162. vitawhence = SCE_SEEK_CUR;
  163. break;
  164. case RW_SEEK_END:
  165. vitawhence = SCE_SEEK_END;
  166. break;
  167. default:
  168. return SDL_SetError("vita_file_seek: Unknown value for 'whence'");
  169. }
  170. return sceIoLseek(context->hidden.vitaio.h, offset, vitawhence);
  171. }
  172. static size_t SDLCALL
  173. vita_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
  174. {
  175. size_t total_need;
  176. size_t total_read = 0;
  177. size_t read_ahead;
  178. size_t byte_read;
  179. total_need = size * maxnum;
  180. if (!context || context->hidden.vitaio.h < 0 || !total_need) {
  181. return 0;
  182. }
  183. if (context->hidden.vitaio.buffer.left > 0) {
  184. void *data = (char *) context->hidden.vitaio.buffer.data +
  185. context->hidden.vitaio.buffer.size -
  186. context->hidden.vitaio.buffer.left;
  187. read_ahead =
  188. SDL_min(total_need, context->hidden.vitaio.buffer.left);
  189. SDL_memcpy(ptr, data, read_ahead);
  190. context->hidden.vitaio.buffer.left -= read_ahead;
  191. if (read_ahead == total_need) {
  192. return maxnum;
  193. }
  194. ptr = (char *) ptr + read_ahead;
  195. total_need -= read_ahead;
  196. total_read += read_ahead;
  197. }
  198. if (total_need < READAHEAD_BUFFER_SIZE) {
  199. byte_read = sceIoRead(context->hidden.vitaio.h, context->hidden.vitaio.buffer.data, READAHEAD_BUFFER_SIZE);
  200. read_ahead = SDL_min(total_need, (int) byte_read);
  201. SDL_memcpy(ptr, context->hidden.vitaio.buffer.data, read_ahead);
  202. context->hidden.vitaio.buffer.size = byte_read;
  203. context->hidden.vitaio.buffer.left = byte_read - read_ahead;
  204. total_read += read_ahead;
  205. } else {
  206. byte_read = sceIoRead(context->hidden.vitaio.h, ptr, total_need);
  207. total_read += byte_read;
  208. }
  209. return (total_read / size);
  210. }
  211. static size_t SDLCALL
  212. vita_file_write(SDL_RWops * context, const void *ptr, size_t size,
  213. size_t num)
  214. {
  215. size_t total_bytes;
  216. size_t byte_written;
  217. size_t nwritten;
  218. total_bytes = size * num;
  219. if (!context || context->hidden.vitaio.h < 0 || !size || !total_bytes) {
  220. return 0;
  221. }
  222. if (context->hidden.vitaio.buffer.left) {
  223. sceIoLseek(context->hidden.vitaio.h, -(SceOff)context->hidden.vitaio.buffer.left, SCE_SEEK_CUR);
  224. context->hidden.vitaio.buffer.left = 0;
  225. }
  226. byte_written = sceIoWrite(context->hidden.vitaio.h, ptr, total_bytes);
  227. nwritten = byte_written / size;
  228. return nwritten;
  229. }
  230. static int SDLCALL
  231. vita_file_close(SDL_RWops * context)
  232. {
  233. if (context) {
  234. if (context->hidden.vitaio.h >= 0) {
  235. sceIoClose(context->hidden.vitaio.h);
  236. context->hidden.vitaio.h = -1; /* to be sure */
  237. }
  238. SDL_free(context->hidden.vitaio.buffer.data);
  239. context->hidden.vitaio.buffer.data = NULL;
  240. SDL_FreeRW(context);
  241. }
  242. return 0;
  243. }
  244. #endif
  245. #ifdef __WIN32__
  246. /* Functions to read/write Win32 API file pointers */
  247. #ifndef INVALID_SET_FILE_POINTER
  248. #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
  249. #endif
  250. #define READAHEAD_BUFFER_SIZE 1024
  251. static int SDLCALL
  252. windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
  253. {
  254. UINT old_error_mode;
  255. HANDLE h;
  256. DWORD r_right, w_right;
  257. DWORD must_exist, truncate;
  258. int a_mode;
  259. if (!context)
  260. return -1; /* failed (invalid call) */
  261. context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
  262. context->hidden.windowsio.buffer.data = NULL;
  263. context->hidden.windowsio.buffer.size = 0;
  264. context->hidden.windowsio.buffer.left = 0;
  265. /* "r" = reading, file must exist */
  266. /* "w" = writing, truncate existing, file may not exist */
  267. /* "r+"= reading or writing, file must exist */
  268. /* "a" = writing, append file may not exist */
  269. /* "a+"= append + read, file may not exist */
  270. /* "w+" = read, write, truncate. file may not exist */
  271. must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
  272. truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
  273. r_right = (SDL_strchr(mode, '+') != NULL
  274. || must_exist) ? GENERIC_READ : 0;
  275. a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
  276. w_right = (a_mode || SDL_strchr(mode, '+')
  277. || truncate) ? GENERIC_WRITE : 0;
  278. if (!r_right && !w_right) /* inconsistent mode */
  279. return -1; /* failed (invalid call) */
  280. context->hidden.windowsio.buffer.data =
  281. (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
  282. if (!context->hidden.windowsio.buffer.data) {
  283. return SDL_OutOfMemory();
  284. }
  285. /* Do not open a dialog box if failure */
  286. old_error_mode =
  287. SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  288. {
  289. LPTSTR tstr = WIN_UTF8ToString(filename);
  290. h = CreateFile(tstr, (w_right | r_right),
  291. (w_right) ? 0 : FILE_SHARE_READ, NULL,
  292. (must_exist | truncate | a_mode),
  293. FILE_ATTRIBUTE_NORMAL, NULL);
  294. SDL_free(tstr);
  295. }
  296. /* restore old behavior */
  297. SetErrorMode(old_error_mode);
  298. if (h == INVALID_HANDLE_VALUE) {
  299. SDL_free(context->hidden.windowsio.buffer.data);
  300. context->hidden.windowsio.buffer.data = NULL;
  301. SDL_SetError("Couldn't open %s", filename);
  302. return -2; /* failed (CreateFile) */
  303. }
  304. context->hidden.windowsio.h = h;
  305. context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
  306. return 0; /* ok */
  307. }
  308. static Sint64 SDLCALL
  309. windows_file_size(SDL_RWops * context)
  310. {
  311. LARGE_INTEGER size;
  312. if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
  313. return SDL_SetError("windows_file_size: invalid context/file not opened");
  314. }
  315. if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) {
  316. return WIN_SetError("windows_file_size");
  317. }
  318. return size.QuadPart;
  319. }
  320. static Sint64 SDLCALL
  321. windows_file_seek(SDL_RWops * context, Sint64 offset, int whence)
  322. {
  323. DWORD windowswhence;
  324. LARGE_INTEGER windowsoffset;
  325. if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
  326. return SDL_SetError("windows_file_seek: invalid context/file not opened");
  327. }
  328. /* FIXME: We may be able to satisfy the seek within buffered data */
  329. if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
  330. offset -= (long)context->hidden.windowsio.buffer.left;
  331. }
  332. context->hidden.windowsio.buffer.left = 0;
  333. switch (whence) {
  334. case RW_SEEK_SET:
  335. windowswhence = FILE_BEGIN;
  336. break;
  337. case RW_SEEK_CUR:
  338. windowswhence = FILE_CURRENT;
  339. break;
  340. case RW_SEEK_END:
  341. windowswhence = FILE_END;
  342. break;
  343. default:
  344. return SDL_SetError("windows_file_seek: Unknown value for 'whence'");
  345. }
  346. windowsoffset.QuadPart = offset;
  347. if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) {
  348. return WIN_SetError("windows_file_seek");
  349. }
  350. return windowsoffset.QuadPart;
  351. }
  352. static size_t SDLCALL
  353. windows_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
  354. {
  355. size_t total_need;
  356. size_t total_read = 0;
  357. size_t read_ahead;
  358. DWORD byte_read;
  359. total_need = size * maxnum;
  360. if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE || !total_need) {
  361. return 0;
  362. }
  363. if (context->hidden.windowsio.buffer.left > 0) {
  364. void *data = (char *) context->hidden.windowsio.buffer.data +
  365. context->hidden.windowsio.buffer.size -
  366. context->hidden.windowsio.buffer.left;
  367. read_ahead =
  368. SDL_min(total_need, context->hidden.windowsio.buffer.left);
  369. SDL_memcpy(ptr, data, read_ahead);
  370. context->hidden.windowsio.buffer.left -= read_ahead;
  371. if (read_ahead == total_need) {
  372. return maxnum;
  373. }
  374. ptr = (char *) ptr + read_ahead;
  375. total_need -= read_ahead;
  376. total_read += read_ahead;
  377. }
  378. if (total_need < READAHEAD_BUFFER_SIZE) {
  379. if (!ReadFile
  380. (context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
  381. READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
  382. SDL_Error(SDL_EFREAD);
  383. return 0;
  384. }
  385. read_ahead = SDL_min(total_need, (int) byte_read);
  386. SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
  387. context->hidden.windowsio.buffer.size = byte_read;
  388. context->hidden.windowsio.buffer.left = byte_read - read_ahead;
  389. total_read += read_ahead;
  390. } else {
  391. if (!ReadFile
  392. (context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
  393. SDL_Error(SDL_EFREAD);
  394. return 0;
  395. }
  396. total_read += byte_read;
  397. }
  398. return (total_read / size);
  399. }
  400. static size_t SDLCALL
  401. windows_file_write(SDL_RWops * context, const void *ptr, size_t size,
  402. size_t num)
  403. {
  404. size_t total_bytes;
  405. DWORD byte_written;
  406. size_t nwritten;
  407. total_bytes = size * num;
  408. if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE || !size || !total_bytes) {
  409. return 0;
  410. }
  411. if (context->hidden.windowsio.buffer.left) {
  412. SetFilePointer(context->hidden.windowsio.h,
  413. -(LONG)context->hidden.windowsio.buffer.left, NULL,
  414. FILE_CURRENT);
  415. context->hidden.windowsio.buffer.left = 0;
  416. }
  417. /* if in append mode, we must go to the EOF before write */
  418. if (context->hidden.windowsio.append) {
  419. if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
  420. INVALID_SET_FILE_POINTER) {
  421. SDL_Error(SDL_EFWRITE);
  422. return 0;
  423. }
  424. }
  425. if (!WriteFile
  426. (context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
  427. SDL_Error(SDL_EFWRITE);
  428. return 0;
  429. }
  430. nwritten = byte_written / size;
  431. return nwritten;
  432. }
  433. static int SDLCALL
  434. windows_file_close(SDL_RWops * context)
  435. {
  436. if (context) {
  437. if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
  438. CloseHandle(context->hidden.windowsio.h);
  439. context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* to be sure */
  440. }
  441. SDL_free(context->hidden.windowsio.buffer.data);
  442. context->hidden.windowsio.buffer.data = NULL;
  443. SDL_FreeRW(context);
  444. }
  445. return 0;
  446. }
  447. #endif /* __WIN32__ */
  448. #ifdef HAVE_STDIO_H
  449. #ifdef HAVE_FOPEN64
  450. #define fopen fopen64
  451. #endif
  452. #ifdef HAVE_FSEEKO64
  453. #define fseek_off_t off64_t
  454. #define fseek fseeko64
  455. #define ftell ftello64
  456. #elif defined(HAVE_FSEEKO)
  457. #if defined(OFF_MIN) && defined(OFF_MAX)
  458. #define FSEEK_OFF_MIN OFF_MIN
  459. #define FSEEK_OFF_MAX OFF_MAX
  460. #elif defined(HAVE_LIMITS_H)
  461. /* POSIX doesn't specify the minimum and maximum macros for off_t so
  462. * we have to improvise and dance around implementation-defined
  463. * behavior. This may fail if the off_t type has padding bits or
  464. * is not a two's-complement representation. The compilers will detect
  465. * and eliminate the dead code if off_t has 64 bits.
  466. */
  467. #define FSEEK_OFF_MAX (((((off_t)1 << (sizeof(off_t) * CHAR_BIT - 2)) - 1) << 1) + 1)
  468. #define FSEEK_OFF_MIN (-(FSEEK_OFF_MAX) - 1)
  469. #endif
  470. #define fseek_off_t off_t
  471. #define fseek fseeko
  472. #define ftell ftello
  473. #elif defined(HAVE__FSEEKI64)
  474. #define fseek_off_t __int64
  475. #define fseek _fseeki64
  476. #define ftell _ftelli64
  477. #else
  478. #ifdef HAVE_LIMITS_H
  479. #define FSEEK_OFF_MIN LONG_MIN
  480. #define FSEEK_OFF_MAX LONG_MAX
  481. #endif
  482. #define fseek_off_t long
  483. #endif
  484. /* Functions to read/write stdio file pointers */
  485. static Sint64 SDLCALL
  486. stdio_size(SDL_RWops * context)
  487. {
  488. Sint64 pos, size;
  489. pos = SDL_RWseek(context, 0, RW_SEEK_CUR);
  490. if (pos < 0) {
  491. return -1;
  492. }
  493. size = SDL_RWseek(context, 0, RW_SEEK_END);
  494. SDL_RWseek(context, pos, RW_SEEK_SET);
  495. return size;
  496. }
  497. static Sint64 SDLCALL
  498. stdio_seek(SDL_RWops * context, Sint64 offset, int whence)
  499. {
  500. int stdiowhence;
  501. switch (whence) {
  502. case RW_SEEK_SET:
  503. stdiowhence = SEEK_SET;
  504. break;
  505. case RW_SEEK_CUR:
  506. stdiowhence = SEEK_CUR;
  507. break;
  508. case RW_SEEK_END:
  509. stdiowhence = SEEK_END;
  510. break;
  511. default:
  512. return SDL_SetError("Unknown value for 'whence'");
  513. }
  514. #if defined(FSEEK_OFF_MIN) && defined(FSEEK_OFF_MAX)
  515. if (offset < (Sint64)(FSEEK_OFF_MIN) || offset > (Sint64)(FSEEK_OFF_MAX)) {
  516. return SDL_SetError("Seek offset out of range");
  517. }
  518. #endif
  519. if (fseek(context->hidden.stdio.fp, (fseek_off_t)offset, stdiowhence) == 0) {
  520. Sint64 pos = ftell(context->hidden.stdio.fp);
  521. if (pos < 0) {
  522. return SDL_SetError("Couldn't get stream offset");
  523. }
  524. return pos;
  525. }
  526. return SDL_Error(SDL_EFSEEK);
  527. }
  528. static size_t SDLCALL
  529. stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
  530. {
  531. size_t nread;
  532. nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
  533. if (nread == 0 && ferror(context->hidden.stdio.fp)) {
  534. SDL_Error(SDL_EFREAD);
  535. }
  536. return nread;
  537. }
  538. static size_t SDLCALL
  539. stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
  540. {
  541. size_t nwrote;
  542. nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
  543. if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
  544. SDL_Error(SDL_EFWRITE);
  545. }
  546. return nwrote;
  547. }
  548. static int SDLCALL
  549. stdio_close(SDL_RWops * context)
  550. {
  551. int status = 0;
  552. if (context) {
  553. if (context->hidden.stdio.autoclose) {
  554. /* WARNING: Check the return value here! */
  555. if (fclose(context->hidden.stdio.fp) != 0) {
  556. status = SDL_Error(SDL_EFWRITE);
  557. }
  558. }
  559. SDL_FreeRW(context);
  560. }
  561. return status;
  562. }
  563. #endif /* !HAVE_STDIO_H */
  564. /* Functions to read/write memory pointers */
  565. static Sint64 SDLCALL
  566. mem_size(SDL_RWops * context)
  567. {
  568. return (Sint64)(context->hidden.mem.stop - context->hidden.mem.base);
  569. }
  570. static Sint64 SDLCALL
  571. mem_seek(SDL_RWops * context, Sint64 offset, int whence)
  572. {
  573. Uint8 *newpos;
  574. switch (whence) {
  575. case RW_SEEK_SET:
  576. newpos = context->hidden.mem.base + offset;
  577. break;
  578. case RW_SEEK_CUR:
  579. newpos = context->hidden.mem.here + offset;
  580. break;
  581. case RW_SEEK_END:
  582. newpos = context->hidden.mem.stop + offset;
  583. break;
  584. default:
  585. return SDL_SetError("Unknown value for 'whence'");
  586. }
  587. if (newpos < context->hidden.mem.base) {
  588. newpos = context->hidden.mem.base;
  589. }
  590. if (newpos > context->hidden.mem.stop) {
  591. newpos = context->hidden.mem.stop;
  592. }
  593. context->hidden.mem.here = newpos;
  594. return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
  595. }
  596. static size_t SDLCALL
  597. mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
  598. {
  599. size_t total_bytes;
  600. size_t mem_available;
  601. total_bytes = (maxnum * size);
  602. if (!maxnum || !size || ((total_bytes / maxnum) != size)) {
  603. return 0;
  604. }
  605. mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
  606. if (total_bytes > mem_available) {
  607. total_bytes = mem_available;
  608. }
  609. SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
  610. context->hidden.mem.here += total_bytes;
  611. return (total_bytes / size);
  612. }
  613. static size_t SDLCALL
  614. mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
  615. {
  616. if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
  617. num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
  618. }
  619. SDL_memcpy(context->hidden.mem.here, ptr, num * size);
  620. context->hidden.mem.here += num * size;
  621. return num;
  622. }
  623. static size_t SDLCALL
  624. mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
  625. {
  626. SDL_SetError("Can't write to read-only memory");
  627. return 0;
  628. }
  629. static int SDLCALL
  630. mem_close(SDL_RWops * context)
  631. {
  632. if (context) {
  633. SDL_FreeRW(context);
  634. }
  635. return 0;
  636. }
  637. /* Functions to create SDL_RWops structures from various data sources */
  638. SDL_RWops *
  639. SDL_RWFromFile(const char *file, const char *mode)
  640. {
  641. SDL_RWops *rwops = NULL;
  642. if (!file || !*file || !mode || !*mode) {
  643. SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
  644. return NULL;
  645. }
  646. #if defined(__ANDROID__)
  647. #ifdef HAVE_STDIO_H
  648. /* Try to open the file on the filesystem first */
  649. if (*file == '/') {
  650. FILE *fp = fopen(file, mode);
  651. if (fp) {
  652. return SDL_RWFromFP(fp, 1);
  653. }
  654. } else {
  655. /* Try opening it from internal storage if it's a relative path */
  656. char *path;
  657. FILE *fp;
  658. /* !!! FIXME: why not just "char path[PATH_MAX];" ? */
  659. path = SDL_stack_alloc(char, PATH_MAX);
  660. if (path) {
  661. SDL_snprintf(path, PATH_MAX, "%s/%s",
  662. SDL_AndroidGetInternalStoragePath(), file);
  663. fp = fopen(path, mode);
  664. SDL_stack_free(path);
  665. if (fp) {
  666. return SDL_RWFromFP(fp, 1);
  667. }
  668. }
  669. }
  670. #endif /* HAVE_STDIO_H */
  671. /* Try to open the file from the asset system */
  672. rwops = SDL_AllocRW();
  673. if (!rwops)
  674. return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
  675. if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
  676. SDL_FreeRW(rwops);
  677. return NULL;
  678. }
  679. rwops->size = Android_JNI_FileSize;
  680. rwops->seek = Android_JNI_FileSeek;
  681. rwops->read = Android_JNI_FileRead;
  682. rwops->write = Android_JNI_FileWrite;
  683. rwops->close = Android_JNI_FileClose;
  684. rwops->type = SDL_RWOPS_JNIFILE;
  685. #elif defined(__WIN32__)
  686. rwops = SDL_AllocRW();
  687. if (!rwops)
  688. return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
  689. if (windows_file_open(rwops, file, mode) < 0) {
  690. SDL_FreeRW(rwops);
  691. return NULL;
  692. }
  693. rwops->size = windows_file_size;
  694. rwops->seek = windows_file_seek;
  695. rwops->read = windows_file_read;
  696. rwops->write = windows_file_write;
  697. rwops->close = windows_file_close;
  698. rwops->type = SDL_RWOPS_WINFILE;
  699. #elif defined(__VITA__)
  700. rwops = SDL_AllocRW();
  701. if (!rwops)
  702. return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
  703. if (vita_file_open(rwops, file, mode) < 0) {
  704. SDL_FreeRW(rwops);
  705. return NULL;
  706. }
  707. rwops->size = vita_file_size;
  708. rwops->seek = vita_file_seek;
  709. rwops->read = vita_file_read;
  710. rwops->write = vita_file_write;
  711. rwops->close = vita_file_close;
  712. rwops->type = SDL_RWOPS_VITAFILE;
  713. #elif HAVE_STDIO_H
  714. {
  715. #if __APPLE__ && !SDL_FILE_DISABLED // TODO: add dummy?
  716. FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
  717. #elif __WINRT__
  718. FILE *fp = NULL;
  719. fopen_s(&fp, file, mode);
  720. #else
  721. FILE *fp = fopen(file, mode);
  722. #endif
  723. if (fp == NULL) {
  724. SDL_SetError("Couldn't open %s", file);
  725. } else {
  726. rwops = SDL_RWFromFP(fp, SDL_TRUE);
  727. }
  728. }
  729. #else
  730. SDL_SetError("SDL not compiled with stdio support");
  731. #endif /* !HAVE_STDIO_H */
  732. return rwops;
  733. }
  734. #ifdef HAVE_STDIO_H
  735. SDL_RWops *
  736. SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
  737. {
  738. SDL_RWops *rwops = NULL;
  739. rwops = SDL_AllocRW();
  740. if (rwops != NULL) {
  741. rwops->size = stdio_size;
  742. rwops->seek = stdio_seek;
  743. rwops->read = stdio_read;
  744. rwops->write = stdio_write;
  745. rwops->close = stdio_close;
  746. rwops->hidden.stdio.fp = fp;
  747. rwops->hidden.stdio.autoclose = autoclose;
  748. rwops->type = SDL_RWOPS_STDFILE;
  749. }
  750. return rwops;
  751. }
  752. #else
  753. SDL_RWops *
  754. SDL_RWFromFP(void * fp, SDL_bool autoclose)
  755. {
  756. SDL_SetError("SDL not compiled with stdio support");
  757. return NULL;
  758. }
  759. #endif /* HAVE_STDIO_H */
  760. SDL_RWops *
  761. SDL_RWFromMem(void *mem, int size)
  762. {
  763. SDL_RWops *rwops = NULL;
  764. if (!mem) {
  765. SDL_InvalidParamError("mem");
  766. return rwops;
  767. }
  768. if (!size) {
  769. SDL_InvalidParamError("size");
  770. return rwops;
  771. }
  772. rwops = SDL_AllocRW();
  773. if (rwops != NULL) {
  774. rwops->size = mem_size;
  775. rwops->seek = mem_seek;
  776. rwops->read = mem_read;
  777. rwops->write = mem_write;
  778. rwops->close = mem_close;
  779. rwops->hidden.mem.base = (Uint8 *) mem;
  780. rwops->hidden.mem.here = rwops->hidden.mem.base;
  781. rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
  782. rwops->type = SDL_RWOPS_MEMORY;
  783. }
  784. return rwops;
  785. }
  786. SDL_RWops *
  787. SDL_RWFromConstMem(const void *mem, int size)
  788. {
  789. SDL_RWops *rwops = NULL;
  790. if (!mem) {
  791. SDL_InvalidParamError("mem");
  792. return rwops;
  793. }
  794. if (!size) {
  795. SDL_InvalidParamError("size");
  796. return rwops;
  797. }
  798. rwops = SDL_AllocRW();
  799. if (rwops != NULL) {
  800. rwops->size = mem_size;
  801. rwops->seek = mem_seek;
  802. rwops->read = mem_read;
  803. rwops->write = mem_writeconst;
  804. rwops->close = mem_close;
  805. rwops->hidden.mem.base = (Uint8 *) mem;
  806. rwops->hidden.mem.here = rwops->hidden.mem.base;
  807. rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
  808. rwops->type = SDL_RWOPS_MEMORY_RO;
  809. }
  810. return rwops;
  811. }
  812. SDL_RWops *
  813. SDL_AllocRW(void)
  814. {
  815. SDL_RWops *area;
  816. area = (SDL_RWops *) SDL_malloc(sizeof *area);
  817. if (area == NULL) {
  818. SDL_OutOfMemory();
  819. } else {
  820. area->type = SDL_RWOPS_UNKNOWN;
  821. }
  822. return area;
  823. }
  824. void
  825. SDL_FreeRW(SDL_RWops * area)
  826. {
  827. SDL_free(area);
  828. }
  829. /* Load all the data from an SDL data stream */
  830. void *
  831. SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize, int freesrc)
  832. {
  833. const int FILE_CHUNK_SIZE = 1024;
  834. Sint64 size;
  835. size_t size_read, size_total;
  836. void *data = NULL, *newdata;
  837. if (!src) {
  838. SDL_InvalidParamError("src");
  839. return NULL;
  840. }
  841. size = SDL_RWsize(src);
  842. if (size < 0) {
  843. size = FILE_CHUNK_SIZE;
  844. }
  845. data = SDL_malloc((size_t)(size + 1));
  846. size_total = 0;
  847. for (;;) {
  848. if ((((Sint64)size_total) + FILE_CHUNK_SIZE) > size) {
  849. size = (size_total + FILE_CHUNK_SIZE);
  850. newdata = SDL_realloc(data, (size_t)(size + 1));
  851. if (!newdata) {
  852. SDL_free(data);
  853. data = NULL;
  854. SDL_OutOfMemory();
  855. goto done;
  856. }
  857. data = newdata;
  858. }
  859. size_read = SDL_RWread(src, (char *)data+size_total, 1, (size_t)(size-size_total));
  860. if (size_read == 0) {
  861. break;
  862. }
  863. size_total += size_read;
  864. }
  865. if (datasize) {
  866. *datasize = size_total;
  867. }
  868. ((char *)data)[size_total] = '\0';
  869. done:
  870. if (freesrc && src) {
  871. SDL_RWclose(src);
  872. }
  873. return data;
  874. }
  875. void *
  876. SDL_LoadFile(const char *file, size_t *datasize)
  877. {
  878. return SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1);
  879. }
  880. Sint64
  881. SDL_RWsize(SDL_RWops *context)
  882. {
  883. return context->size(context);
  884. }
  885. Sint64
  886. SDL_RWseek(SDL_RWops *context, Sint64 offset, int whence)
  887. {
  888. return context->seek(context, offset, whence);
  889. }
  890. Sint64
  891. SDL_RWtell(SDL_RWops *context)
  892. {
  893. return context->seek(context, 0, RW_SEEK_CUR);
  894. }
  895. size_t
  896. SDL_RWread(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
  897. {
  898. return context->read(context, ptr, size, maxnum);
  899. }
  900. size_t
  901. SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size, size_t num)
  902. {
  903. return context->write(context, ptr, size, num);
  904. }
  905. int
  906. SDL_RWclose(SDL_RWops *context)
  907. {
  908. return context->close(context);
  909. }
  910. /* Functions for dynamically reading and writing endian-specific values */
  911. Uint8
  912. SDL_ReadU8(SDL_RWops * src)
  913. {
  914. Uint8 value = 0;
  915. SDL_RWread(src, &value, sizeof (value), 1);
  916. return value;
  917. }
  918. Uint16
  919. SDL_ReadLE16(SDL_RWops * src)
  920. {
  921. Uint16 value = 0;
  922. SDL_RWread(src, &value, sizeof (value), 1);
  923. return SDL_SwapLE16(value);
  924. }
  925. Uint16
  926. SDL_ReadBE16(SDL_RWops * src)
  927. {
  928. Uint16 value = 0;
  929. SDL_RWread(src, &value, sizeof (value), 1);
  930. return SDL_SwapBE16(value);
  931. }
  932. Uint32
  933. SDL_ReadLE32(SDL_RWops * src)
  934. {
  935. Uint32 value = 0;
  936. SDL_RWread(src, &value, sizeof (value), 1);
  937. return SDL_SwapLE32(value);
  938. }
  939. Uint32
  940. SDL_ReadBE32(SDL_RWops * src)
  941. {
  942. Uint32 value = 0;
  943. SDL_RWread(src, &value, sizeof (value), 1);
  944. return SDL_SwapBE32(value);
  945. }
  946. Uint64
  947. SDL_ReadLE64(SDL_RWops * src)
  948. {
  949. Uint64 value = 0;
  950. SDL_RWread(src, &value, sizeof (value), 1);
  951. return SDL_SwapLE64(value);
  952. }
  953. Uint64
  954. SDL_ReadBE64(SDL_RWops * src)
  955. {
  956. Uint64 value = 0;
  957. SDL_RWread(src, &value, sizeof (value), 1);
  958. return SDL_SwapBE64(value);
  959. }
  960. size_t
  961. SDL_WriteU8(SDL_RWops * dst, Uint8 value)
  962. {
  963. return SDL_RWwrite(dst, &value, sizeof (value), 1);
  964. }
  965. size_t
  966. SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
  967. {
  968. const Uint16 swapped = SDL_SwapLE16(value);
  969. return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
  970. }
  971. size_t
  972. SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
  973. {
  974. const Uint16 swapped = SDL_SwapBE16(value);
  975. return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
  976. }
  977. size_t
  978. SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
  979. {
  980. const Uint32 swapped = SDL_SwapLE32(value);
  981. return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
  982. }
  983. size_t
  984. SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
  985. {
  986. const Uint32 swapped = SDL_SwapBE32(value);
  987. return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
  988. }
  989. size_t
  990. SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
  991. {
  992. const Uint64 swapped = SDL_SwapLE64(value);
  993. return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
  994. }
  995. size_t
  996. SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
  997. {
  998. const Uint64 swapped = SDL_SwapBE64(value);
  999. return SDL_RWwrite(dst, &swapped, sizeof (swapped), 1);
  1000. }
  1001. /* vi: set ts=4 sw=4 expandtab: */