SDL_rwops.c 20 KB

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