Repository navigation
Expand file tree
/
Copy pathSDL_iostream.c
More file actions
1657 lines (1434 loc) · 43.5 KB
/
Copy pathSDL_iostream.c
File metadata and controls
1657 lines (1434 loc) · 43.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#if defined(SDL_PLATFORM_WINDOWS)
#include "../core/windows/SDL_windows.h"
#else
#include <unistd.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef SDL_PLATFORM_APPLE
#include <fcntl.h>
#endif
#include "SDL_iostream_c.h"
/* This file provides a general interface for SDL to read and write
10000
data sources. It can easily be extended to files, memory, etc.
*/
struct SDL_IOStream
{
SDL_IOStreamInterface iface;
void *userdata;
SDL_IOStatus status;
SDL_PropertiesID props;
};
#ifdef SDL_PLATFORM_3DS
#include "n3ds/SDL_iostreamromfs.h"
#endif // SDL_PLATFORM_3DS
#ifdef SDL_PLATFORM_ANDROID
#include <unistd.h>
#include "../core/android/SDL_android.h"
#endif
#if defined(SDL_PLATFORM_WINDOWS)
typedef struct IOStreamWindowsData
{
HANDLE h;
void *data;
size_t size;
size_t left;
bool append;
bool autoclose;
} IOStreamWindowsData;
// Functions to read/write Win32 API file pointers
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
#endif
#define READAHEAD_BUFFER_SIZE 1024
static HANDLE SDLCALL windows_file_open(const char *filename, const char *mode)
{
#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
UINT old_error_mode;
#endif
HANDLE h;
DWORD r_right, w_right;
DWORD must_exist, truncate;
int a_mode;
// "r" = reading, file must exist
// "w" = writing, truncate existing, file may not exist
// "r+"= reading or writing, file must exist
// "a" = writing, append file may not exist
// "a+"= append + read, file may not exist
// "w+" = read, write, truncate. file may not exist
must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
r_right = (SDL_strchr(mode, '+') != NULL || must_exist) ? GENERIC_READ : 0;
a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
w_right = (a_mode || SDL_strchr(mode, '+') || truncate) ? GENERIC_WRITE : 0;
if (!r_right && !w_right) {
return INVALID_HANDLE_VALUE; // inconsistent mode
}
// failed (invalid call)
#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
// Do not open a dialog box if failure
old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
#endif
{
LPWSTR str = WIN_UTF8ToStringW(filename);
h = CreateFileW(str,
(w_right | r_right),
(w_right) ? 0 : FILE_SHARE_READ,
NULL,
(must_exist | truncate | a_mode),
FILE_ATTRIBUTE_NORMAL,
NULL);
SDL_free(str);
}
#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
// restore old behavior
SetErrorMode(old_error_mode);
#endif
if (h == INVALID_HANDLE_VALUE) {
char *error;
if (SDL_asprintf(&error, "Couldn't open %s", filename) > 0) {
WIN_SetError(error);
SDL_free(error);
} else {
SDL_SetError("Couldn't open %s", filename);
}
}
return h;
}
static Sint64 SDLCALL windows_file_size(void *userdata)
{
IOStreamWindowsData *iodata = (IOStreamWindowsData *) userdata;
LARGE_INTEGER size;
if (!GetFileSizeEx(iodata->h, &size)) {
return WIN_SetError("windows_file_size");
}
return size.QuadPart;
}
static Sint64 SDLCALL windows_file_seek(void *userdata, Sint64 offset, SDL_IOWhence whence)
{
IOStreamWindowsData *iodata = (IOStreamWindowsData *) userdata;
DWORD windowswhence;
LARGE_INTEGER windowsoffset;
// FIXME: We may be able to satisfy the seek within buffered data
if ((whence == SDL_IO_SEEK_CUR) && (iodata->left)) {
offset -= iodata->left;
}
iodata->left = 0;
switch (whence) {
case SDL_IO_SEEK_SET:
windowswhence = FILE_BEGIN;
break;
case SDL_IO_SEEK_CUR:
windowswhence = FILE_CURRENT;
break;
case SDL_IO_SEEK_END:
windowswhence = FILE_END;
break;
default:
SDL_SetError("windows_file_seek: Unknown value for 'whence'");
return -1;
}
windowsoffset.QuadPart = offset;
if (!SetFilePointerEx(iodata->h, windowsoffset, &windowsoffset, windowswhence)) {
return WIN_SetError("Error seeking in datastream");
}
return windowsoffset.QuadPart;
}
static size_t SDLCALL windows_file_read(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
{
IOStreamWindowsData *iodata = (IOStreamWindowsData *) userdata;
size_t total_need = size;
size_t total_read = 0;
size_t read_ahead;
DWORD bytes;
if (iodata->left > 0) {
void *data = (char *)iodata->data +
iodata->size -
iodata->left;
read_ahead = SDL_min(total_need, iodata->left);
SDL_memcpy(ptr, data, read_ahead);
iodata->left -= read_ahead;
if (read_ahead == total_need) {
return size;
}
ptr = (char *)ptr + read_ahead;
total_need -= read_ahead;
total_read += read_ahead;
}
if (total_need < READAHEAD_BUFFER_SIZE) {
if (!ReadFile(iodata->h, iodata->data, READAHEAD_BUFFER_SIZE, &bytes, NULL)) {
DWORD error = GetLastError();
switch (error) {
case ERROR_BROKEN_PIPE:
case ERROR_HANDLE_EOF:
break;
case ERROR_NO_DATA:
*status = SDL_IO_STATUS_NOT_READY;
break;
default:
WIN_SetError("Error reading from datastream");
break;
}
return 0;
}
read_ahead = SDL_min(total_need, bytes);
SDL_memcpy(ptr, iodata->data, read_ahead);
iodata->size = bytes;
iodata->left = bytes - read_ahead;
total_read += read_ahead;
} else {
if (!ReadFile(iodata->h, ptr, (DWORD)total_need, &bytes, NULL)) {
DWORD error = GetLastError();
switch (error) {
case ERROR_BROKEN_PIPE:
case ERROR_HANDLE_EOF:
break;
case ERROR_NO_DATA:
*status = SDL_IO_STATUS_NOT_READY;
break;
default:
WIN_SetError("Error reading from datastream");
break;
}
return 0;
}
total_read += bytes;
}
return total_read;
}
static size_t SDLCALL windows_file_write(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status)
{
IOStreamWindowsData *iodata = (IOStreamWindowsData *) userdata;
DWORD bytes;
if (iodata->left) {
if (!SetFilePointer(iodata->h, -(LONG)iodata->left, NULL, FILE_CURRENT)) {
WIN_SetError("Error seeking in datastream");
return 0;
}
iodata->left = 0;
}
// if in append mode, we must go to the EOF before write
if (iodata->append) {
LARGE_INTEGER windowsoffset;
windowsoffset.QuadPart = 0;
if (!SetFilePointerEx(iodata->h, windowsoffset, &windowsoffset, FILE_END)) {
WIN_SetError("Error seeking in datastream");
return 0;
}
}
if (!WriteFile(iodata->h, ptr, (DWORD)size, &bytes, NULL)) {
WIN_SetError("Error writing to datastream");
return 0;
}
if (bytes == 0 && size > 0) {
*status = SDL_IO_STATUS_NOT_READY;
}
return bytes;
}
static bool SDLCALL windows_file_flush(void *userdata, SDL_IOStatus *status)
{
IOStreamWindowsData *iodata = (IOStreamWindowsData *) userdata;
if (!FlushFileBuffers(iodata->h)) {
return WIN_SetError("Error flushing datastream");
}
return true;
}
static bool SDLCALL windows_file_close(void *userdata)
{
IOStreamWindowsData *iodata = (IOStreamWindowsData *) userdata;
if (iodata->h != INVALID_HANDLE_VALUE) {
if (iodata->autoclose) {
CloseHandle(iodata->h);
}
iodata->h = INVALID_HANDLE_VALUE; // to be sure
}
SDL_free(iodata->data);
SDL_free(iodata);
return true;
}
SDL_IOStream *SDL_IOFromHandle(HANDLE handle, const char *mode, bool autoclose)
{
IOStreamWindowsData *iodata = (IOStreamWindowsData *) SDL_calloc(1, sizeof (*iodata));
if (!iodata) {
if (autoclose) {
CloseHandle(handle);
}
return NULL;
}
SDL_IOStreamInterface iface;
SDL_INIT_INTERFACE(&iface);
if (GetFileType(handle) == FILE_TYPE_DISK) {
iface.size = windows_file_size;
iface.seek = windows_file_seek;
}
iface.read = windows_file_read;
iface.write = windows_file_write;
iface.flush = windows_file_flush;
iface.close = windows_file_close;
iodata->h = handle;
iodata->append = (SDL_strchr(mode, 'a') != NULL);
iodata->autoclose = autoclose;
iodata->data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
if (!iodata->data) {
iface.close(iodata);
return NULL;
}
SDL_IOStream *iostr = SDL_OpenIO(&iface, iodata);
if (!iostr) {
iface.close(iodata);
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER, iodata->h);
}
}
return iostr;
}
#endif // defined(SDL_PLATFORM_WINDOWS)
#if !defined(SDL_PLATFORM_WINDOWS)
// Functions to read/write file descriptors. Not used for windows.
typedef struct IOStreamFDData
{
int fd;
bool autoclose;
bool regular_file;
} IOStreamFDData;
static int SDL_fdatasync(int fd)
{
int result = 0;
#if defined(SDL_PLATFORM_APPLE) // Apple doesn't have fdatasync (rather, the symbol exists as an incompatible system call).
result = fcntl(fd, F_FULLFSYNC);
#elif defined(SDL_PLATFORM_HAIKU)
result = fsync(fd);
#elif defined(HAVE_FDATASYNC)
result = fdatasync(fd);
#endif
return result;
}
static Sint64 SDLCALL fd_seek(void *userdata, Sint64 offset, SDL_IOWhence whence)
{
IOStreamFDData *iodata = (IOStreamFDData *) userdata;
int fdwhence;
switch (whence) {
case SDL_IO_SEEK_SET:
fdwhence = SEEK_SET;
break;
case SDL_IO_SEEK_CUR:
fdwhence = SEEK_CUR;
break;
case SDL_IO_SEEK_END:
fdwhence = SEEK_END;
break;
default:
SDL_SetError("Unknown value for 'whence'");
return -1;
}
off_t result = lseek(iodata->fd, (off_t)offset, fdwhence);
if (result < 0) {
SDL_SetError("Couldn't get stream offset: %s", strerror(errno));
}
return result;
}
static size_t SDLCALL fd_read(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
{
IOStreamFDData *iodata = (IOStreamFDData *) userdata;
ssize_t bytes;
do {
bytes = read(iodata->fd, ptr, size);
} while (bytes < 0 && errno == EINTR);
if (bytes < 0) {
if (errno == EAGAIN) {
*status = SDL_IO_STATUS_NOT_READY;
} else {
SDL_SetError("Error reading from datastream: %s", strerror(errno));
}
bytes = 0;
}
return (size_t)bytes;
}
static size_t SDLCALL fd_write(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status)
{
IOStreamFDData *iodata = (IOStreamFDData *) userdata;
ssize_t bytes;
do {
bytes = write(iodata->fd, ptr, size);
} while (bytes < 0 && errno == EINTR);
if (bytes < 0) {
if (errno == EAGAIN) {
*status = SDL_IO_STATUS_NOT_READY;
} else {
SDL_SetError("Error writing to datastream: %s", strerror(errno));
}
bytes = 0;
}
return (size_t)bytes;
}
static bool SDLCALL fd_flush(void *userdata, SDL_IOStatus *status)
{
IOStreamFDData *iodata = (IOStreamFDData *) userdata;
int result;
do {
result = SDL_fdatasync(iodata->fd);
} while (result < 0 && errno == EINTR);
if (result < 0) {
return SDL_SetError("Error flushing datastream: %s", strerror(errno));
}
return true;
}
static bool SDLCALL fd_close(void *userdata)
{
IOStreamFDData *iodata = (IOStreamFDData *) userdata;
bool status = true;
if (iodata->autoclose) {
if (close(iodata->fd) < 0) {
status = SDL_SetError("Error closing datastream: %s", strerror(errno));
}
}
SDL_free(iodata);
return status;
}
SDL_IOStream *SDL_IOFromFD(int fd, bool autoclose)
{
IOStreamFDData *iodata = (IOStreamFDData *) SDL_calloc(1, sizeof (*iodata));
if (!iodata) {
if (autoclose) {
close(fd);
}
return NULL;
}
SDL_IOStreamInterface iface;
SDL_INIT_INTERFACE(&iface);
// There's no fd_size because SDL_GetIOSize emulates it the same way we'd do it for fd anyhow.
iface.seek = fd_seek;
iface.read = fd_read;
iface.write = fd_write;
iface.flush = fd_flush;
iface.close = fd_close;
iodata->fd = fd;
iodata->autoclose = autoclose;
struct stat st;
iodata->regular_file = ((fstat(fd, &st) == 0) && S_ISREG(st.st_mode));
SDL_IOStream *iostr = SDL_OpenIO(&iface, iodata);
if (!iostr) {
iface.close(iodata);
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER, fd);
}
}
return iostr;
}
#endif // !defined(SDL_PLATFORM_WINDOWS)
#if defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINDOWS)
// Functions to read/write stdio file pointers. Not used for windows.
typedef struct IOStreamStdioData
{
FILE *fp;
bool autoclose;
bool regular_file;
} IOStreamStdioData;
#ifdef HAVE_FOPEN64
#define fopen fopen64
#endif
#ifdef HAVE_FSEEKO64
#define fseek_off_t off64_t
#define fseek fseeko64
#define ftell ftello64
#elif defined(HAVE_FSEEKO)
#if defined(OFF_MIN) && defined(OFF_MAX)
#define FSEEK_OFF_MIN OFF_MIN
#define FSEEK_OFF_MAX OFF_MAX
#elif defined(HAVE_LIMITS_H)
/* POSIX doesn't specify the minimum and maximum macros for off_t so
* we have to improvise and dance around implementation-defined
* behavior. This may fail if the off_t type has padding bits or
* is not a two's-complement representation. The compilers will detect
* and eliminate the dead code if off_t has 64 bits.
*/
#define FSEEK_OFF_MAX (((((off_t)1 << (sizeof(off_t) * CHAR_BIT - 2)) - 1) << 1) + 1)
#define FSEEK_OFF_MIN (-(FSEEK_OFF_MAX)-1)
#endif
#define fseek_off_t off_t
#define fseek fseeko
#define ftell ftello
#elif defined(HAVE__FSEEKI64)
#define fseek_off_t __int64
#define fseek _fseeki64
#define ftell _ftelli64
#else
#ifdef HAVE_LIMITS_H
#define FSEEK_OFF_MIN LONG_MIN
#define FSEEK_OFF_MAX LONG_MAX
#endif
#define fseek_off_t long
#endif
static Sint64 SDLCALL stdio_seek(void *userdata, Sint64 offset, SDL_IOWhence whence)
{
IOStreamStdioData *iodata = (IOStreamStdioData *) userdata;
int stdiowhence;
switch (whence) {
case SDL_IO_SEEK_SET:
stdiowhence = SEEK_SET;
break;
case SDL_IO_SEEK_CUR:
stdiowhence = SEEK_CUR;
break;
case SDL_IO_SEEK_END:
stdiowhence = SEEK_END;
break;
defa
10BC8
ult:
SDL_SetError("Unknown value for 'whence'");
return -1;
}
#if defined(FSEEK_OFF_MIN) && defined(FSEEK_OFF_MAX)
if (offset < (Sint64)(FSEEK_OFF_MIN) || offset > (Sint64)(FSEEK_OFF_MAX)) {
SDL_SetError("Seek offset out of range");
return -1;
}
#endif
// don't make a possibly-costly API call for the noop seek from SDL_TellIO
const bool is_noop = (whence == SDL_IO_SEEK_CUR) && (offset == 0);
if (is_noop || fseek(iodata->fp, (fseek_off_t)offset, stdiowhence) == 0) {
const Sint64 pos = ftell(iodata->fp);
if (pos < 0) {
SDL_SetError("Couldn't get stream offset: %s", strerror(errno));
return -1;
}
return pos;
}
SDL_SetError("Error seeking in datastream: %s", strerror(errno));
return -1;
}
static size_t SDLCALL stdio_read(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
{
IOStreamStdioData *iodata = (IOStreamStdioData *) userdata;
const size_t bytes = fread(ptr, 1, size, iodata->fp);
if (bytes == 0 && ferror(iodata->fp)) {
if (errno == EAGAIN) {
*status = SDL_IO_STATUS_NOT_READY;
clearerr(iodata->fp);
} else {
SDL_SetError("Error reading from datastream: %s", strerror(errno));
}
}
return bytes;
}
static size_t SDLCALL stdio_write(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status)
{
IOStreamStdioData *iodata = (IOStreamStdioData *) userdata;
const size_t bytes = fwrite(ptr, 1, size, iodata->fp);
if (bytes == 0 && ferror(iodata->fp)) {
if (errno == EAGAIN) {
*status = SDL_IO_STATUS_NOT_READY;
clearerr(iodata->fp);
} else {
SDL_SetError("Error writing to datastream: %s", strerror(errno));
}
}
return bytes;
}
static bool SDLCALL stdio_flush(void *userdata, SDL_IOStatus *status)
{
IOStreamStdioData *iodata = (IOStreamStdioData *) userdata;
if (fflush(iodata->fp) != 0) {
if (errno == EAGAIN) {
*status = SDL_IO_STATUS_NOT_READY;
return false;
} else {
return SDL_SetError("Error flushing datastream: %s", strerror(errno));
}
}
int result;
int fd = fileno(iodata->fp);
do {
result = SDL_fdatasync(fd);
} while (result < 0 && errno == EINTR);
if (result < 0) {
return SDL_SetError("Error flushing datastream: %s", strerror(errno));
}
return true;
}
static bool SDLCALL stdio_close(void *userdata)
{
IOStreamStdioData *iodata = (IOStreamStdioData *) userdata;
bool status = true;
if (iodata->autoclose) {
if (fclose(iodata->fp) != 0) {
status = SDL_SetError("Error closing datastream: %s", strerror(errno));
}
}
SDL_free(iodata);
return status;
}
SDL_IOStream *SDL_IOFromFP(FILE *fp, bool autoclose)
{
IOStreamStdioData *iodata = (IOStreamStdioData *) SDL_calloc(1, sizeof (*iodata));
if (!iodata) {
if (autoclose) {
fclose(fp);
}
return NULL;
}
SDL_IOStreamInterface iface;
SDL_INIT_INTERFACE(&iface);
// There's no stdio_size because SDL_GetIOSize emulates it the same way we'd do it for stdio anyhow.
iface.seek = stdio_seek;
iface.read = stdio_read;
iface.write = stdio_write;
iface.flush = stdio_flush;
iface.close = stdio_close;
iodata->fp = fp;
iodata->autoclose = autoclose;
struct stat st;
iodata->regular_file = ((fstat(fileno(fp), &st) == 0) && S_ISREG(st.st_mode));
SDL_IOStream *iostr = SDL_OpenIO(&iface, iodata);
if (!iostr) {
iface.close(iodata);
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_STDIO_FILE_POINTER, fp);
SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER, fileno(fp));
}
}
return iostr;
}
#endif // !HAVE_STDIO_H && !defined(SDL_PLATFORM_WINDOWS)
// Functions to read/write memory pointers
typedef struct IOStreamMemData
{
Uint8 *base;
Uint8 *here;
Uint8 *stop;
} IOStreamMemData;
static Sint64 SDLCALL mem_size(void *userdata)
{
const IOStreamMemData *iodata = (IOStreamMemData *) userdata;
return (iodata->stop - iodata->base);
}
static Sint64 SDLCALL mem_seek(void *userdata, Sint64 offset, SDL_IOWhence whence)
{
IOStreamMemData *iodata = (IOStreamMemData *) userdata;
Uint8 *newpos;
switch (whence) {
case SDL_IO_SEEK_SET:
newpos = iodata->base + offset;
break;
case SDL_IO_SEEK_CUR:
newpos = iodata->here + offset;
break;
case SDL_IO_SEEK_END:
newpos = iodata->stop + offset;
break;
default:
SDL_SetError("Unknown value for 'whence'");
return -1;
}
if (newpos < iodata->base) {
newpos = iodata->base;
}
if (newpos > iodata->stop) {
newpos = iodata->stop;
}
iodata->here = newpos;
return (Sint64)(iodata->here - iodata->base);
}
static size_t mem_io(void *userdata, void *dst, const void *src, size_t size)
{
IOStreamMemData *iodata = (IOStreamMemData *) userdata;
const size_t mem_available = (iodata->stop - iodata->here);
if (size > mem_available) {
size = mem_available;
}
SDL_memcpy(dst, src, size);
iodata->here += size;
return size;
}
static size_t SDLCALL mem_read(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
{
IOStreamMemData *iodata = (IOStreamMemData *) userdata;
return mem_io(userdata, ptr, iodata->here, size);
}
static size_t SDLCALL mem_write(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status)
{
IOStreamMemData *iodata = (IOStreamMemData *) userdata;
return mem_io(userdata, iodata->here, ptr, size);
}
static bool SDLCALL mem_close(void *userdata)
{
SDL_free(userdata);
return true;
}
// Functions to create SDL_IOStream structures from various data sources
#if defined(HAVE_STDIO_H) && !defined(SDL_PLATFORM_WINDOWS)
static bool IsRegularFileOrPipe(FILE *f)
{
#ifndef SDL_PLATFORM_EMSCRIPTEN
struct stat st;
if (fstat(fileno(f), &st) < 0 || !(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) {
return false;
}
#endif // !SDL_PLATFORM_EMSCRIPTEN
return true;
}
#endif
SDL_IOStream *SDL_IOFromFile(const char *file, const char *mode)
{
SDL_IOStream *iostr = NULL;
if (!file || !*file) {
SDL_InvalidParamError("file");
return NULL;
}
if (!mode || !*mode) {
SDL_InvalidParamError("mode");
return NULL;
}
#ifdef SDL_PLATFORM_ANDROID
#ifdef HAVE_STDIO_H
// Try to open the file on the filesystem first
if (*file == '/') {
FILE *fp = fopen(file, mode);
if (fp) {
if (!IsRegularFileOrPipe(fp)) {
fclose(fp);
SDL_SetError("%s is not a regular file or pipe", file);
return NULL;
}
return SDL_IOFromFP(fp, true);
}
} else if (SDL_strncmp(file, "content://", 10) == 0) {
// Try opening content:// URI
int fd = Android_JNI_OpenFileDescriptor(file, mode);
if (fd == -1) {
// SDL error is already set.
return NULL;
}
FILE *fp = fdopen(fd, mode);
if (!fp) {
close(fd);
SDL_SetError("Unable to open file descriptor (%d) from URI %s: %s", fd, file, strerror(errno));
return NULL;
}
return SDL_IOFromFP(fp, true);
} else {
// Try opening it from internal storage if it's a relative path
char *path = NULL;
SDL_asprintf(&path, "%s/%s", SDL_GetAndroidInternalStoragePath(), file);
if (path) {
FILE *fp = fopen(path, mode);
SDL_free(path);
if (fp) {
if (!IsRegularFileOrPipe(fp)) {
fclose(fp);
SDL_SetError("%s is not a regular file or pipe", path);
return NULL;
}
return SDL_IOFromFP(fp, true);
}
}
}
#endif // HAVE_STDIO_H
// Try to open the file from the asset system
void *iodata = NULL;
if (!Android_JNI_FileOpen(&iodata, file, mode)) {
return NULL;
}
SDL_IOStreamInterface iface;
SDL_INIT_INTERFACE(&iface);
iface.size = Android_JNI_FileSize;
iface.seek = Android_JNI_FileSeek;
iface.read = Android_JNI_FileRead;
iface.write = Android_JNI_FileWrite;
iface.close = Android_JNI_FileClose;
iostr = SDL_OpenIO(&iface, iodata);
if (!iostr) {
iface.close(iodata);
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER, iodata);
}
}
#elif defined(SDL_PLATFORM_WINDOWS)
HANDLE handle = windows_file_open(file, mode);
if (handle != INVALID_HANDLE_VALUE) {
iostr = SDL_IOFromHandle(handle, mode, true);
}
#elif defined(HAVE_STDIO_H)
{
#if defined(SDL_PLATFORM_3DS)
FILE *fp = N3DS_FileOpen(file, mode);
#else
FILE *fp = fopen(file, mode);
#endif
if (!fp) {
SDL_SetError("Couldn't open %s: %s", file, strerror(errno));
} else if (!IsRegularFileOrPipe(fp)) {
fclose(fp);
fp = NULL;
SDL_SetError("%s is not a regular file or pipe", file);
} else {
iostr = SDL_IOFromFP(fp, true);
}
}
#else
SDL_SetError("SDL not compiled with stdio support");
#endif // !HAVE_STDIO_H
return iostr;
}
SDL_IOStream *SDL_IOFromMem(void *mem, size_t size)
{
if (!mem) {
SDL_InvalidParamError("mem");
return NULL;
} else if (!size) {
SDL_InvalidParamError("size");
return NULL;
}
IOStreamMemData *iodata = (IOStreamMemData *) SDL_calloc(1, sizeof (*iodata));
if (!iodata) {
return NULL;
}
SDL_IOStreamInterface iface;
SDL_INIT_INTERFACE(&iface);
iface.size = mem_size;
iface.seek = mem_seek;
iface.read = mem_read;
iface.write = mem_write;
iface.close = mem_close;
iodata->base = (Uint8 *)mem;
iodata->here = iodata->base;
iodata->stop = iodata->base + size;
SDL_IOStream *iostr = SDL_OpenIO(&iface, iodata);
if (!iostr) {
SDL_free(iodata);
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_MEMORY_POINTER, mem);
SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER, size);
}
}
return iostr;
}
SDL_IOStream *SDL_IOFromConstMem(const void *mem, size_t size)
{
if (!mem) {
SDL_InvalidParamError("mem");
return NULL;
} else if (!size) {
SDL_InvalidParamError("size");
return NULL;
}
IOStreamMemData *iodata = (IOStreamMemData *) SDL_calloc(1, sizeof (*iodata));
if (!iodata) {
return NULL;
}
SDL_IOStreamInterface iface;
SDL_INIT_INTERFACE(&iface);
iface.size = mem_size;
iface.seek = mem_seek;
iface.read = mem_read;
// leave iface.write as NULL.
iface.close = mem_close;
iodata->base = (Uint8 *)mem;
iodata->here = iodata->base;
iodata->stop = iodata->base + size;
SDL_IOStream *iostr = SDL_OpenIO(&iface, iodata);
if (!iostr) {
SDL_free(iodata);
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_MEMORY_POINTER, (void *)mem);
SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER, size);
}
}
return iostr;
}
typedef struct IOStreamDynamicMemData