SDL_rwops.c 23 KB

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