SDL_rwops.c 20 KB

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