1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (interface_stream.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include <streams/interface_stream.h>
26 #include <streams/file_stream.h>
27 #include <streams/memory_stream.h>
29 #include <streams/chd_stream.h>
31 #if defined(HAVE_ZLIB)
32 #include <streams/rzip_stream.h>
34 #include <encodings/crc32.h>
36 struct intfstream_internal
60 #if defined(HAVE_ZLIB)
66 enum intfstream_type type;
69 int64_t intfstream_get_size(intfstream_internal_t *intf)
77 return filestream_get_size(intf->file.fp);
78 case INTFSTREAM_MEMORY:
79 return intf->memory.buf.size;
82 return chdstream_get_size(intf->chd.fp);
87 #if defined(HAVE_ZLIB)
88 return rzipstream_get_size(intf->rzip.fp);
97 bool intfstream_resize(intfstream_internal_t *intf, intfstream_info_t *info)
104 case INTFSTREAM_FILE:
106 case INTFSTREAM_MEMORY:
107 intf->memory.buf.data = info->memory.buf.data;
108 intf->memory.buf.size = info->memory.buf.size;
110 memstream_set_buffer(intf->memory.buf.data,
111 intf->memory.buf.size);
117 case INTFSTREAM_RZIP:
125 bool intfstream_open(intfstream_internal_t *intf, const char *path,
126 unsigned mode, unsigned hints)
133 case INTFSTREAM_FILE:
134 intf->file.fp = filestream_open(path, mode, hints);
138 case INTFSTREAM_MEMORY:
139 intf->memory.fp = memstream_open(intf->memory.writable);
140 if (!intf->memory.fp)
145 intf->chd.fp = chdstream_open(path, intf->chd.track);
152 case INTFSTREAM_RZIP:
153 #if defined(HAVE_ZLIB)
154 intf->rzip.fp = rzipstream_open(path, mode);
166 int intfstream_flush(intfstream_internal_t *intf)
173 case INTFSTREAM_FILE:
174 return filestream_flush(intf->file.fp);
175 case INTFSTREAM_MEMORY:
177 case INTFSTREAM_RZIP:
178 /* Should we stub this for these interfaces? */
185 int intfstream_close(intfstream_internal_t *intf)
192 case INTFSTREAM_FILE:
194 return filestream_close(intf->file.fp);
196 case INTFSTREAM_MEMORY:
198 memstream_close(intf->memory.fp);
203 chdstream_close(intf->chd.fp);
206 case INTFSTREAM_RZIP:
207 #if defined(HAVE_ZLIB)
209 return rzipstream_close(intf->rzip.fp);
217 void *intfstream_init(intfstream_info_t *info)
219 intfstream_internal_t *intf = NULL;
223 intf = (intfstream_internal_t*)malloc(sizeof(*intf));
228 intf->type = info->type;
229 intf->file.fp = NULL;
230 intf->memory.buf.data = NULL;
231 intf->memory.buf.size = 0;
232 intf->memory.fp = NULL;
233 intf->memory.writable = false;
239 intf->rzip.fp = NULL;
244 case INTFSTREAM_FILE:
246 case INTFSTREAM_MEMORY:
247 intf->memory.writable = info->memory.writable;
248 if (!intfstream_resize(intf, info))
253 intf->chd.track = info->chd.track;
258 case INTFSTREAM_RZIP:
270 int64_t intfstream_seek(
271 intfstream_internal_t *intf, int64_t offset, int whence)
278 case INTFSTREAM_FILE:
280 int seek_position = 0;
284 seek_position = RETRO_VFS_SEEK_POSITION_START;
287 seek_position = RETRO_VFS_SEEK_POSITION_CURRENT;
290 seek_position = RETRO_VFS_SEEK_POSITION_END;
293 return (int64_t)filestream_seek(intf->file.fp, (int64_t)offset,
296 case INTFSTREAM_MEMORY:
297 return (int64_t)memstream_seek(intf->memory.fp, offset, whence);
300 return (int64_t)chdstream_seek(intf->chd.fp, offset, whence);
304 case INTFSTREAM_RZIP:
312 int64_t intfstream_read(intfstream_internal_t *intf, void *s, uint64_t len)
319 case INTFSTREAM_FILE:
320 return filestream_read(intf->file.fp, s, len);
321 case INTFSTREAM_MEMORY:
322 return memstream_read(intf->memory.fp, s, len);
325 return chdstream_read(intf->chd.fp, s, len);
329 case INTFSTREAM_RZIP:
330 #if defined(HAVE_ZLIB)
331 return rzipstream_read(intf->rzip.fp, s, len);
340 int64_t intfstream_write(intfstream_internal_t *intf,
341 const void *s, uint64_t len)
348 case INTFSTREAM_FILE:
349 return filestream_write(intf->file.fp, s, len);
350 case INTFSTREAM_MEMORY:
351 return memstream_write(intf->memory.fp, s, len);
354 case INTFSTREAM_RZIP:
355 #if defined(HAVE_ZLIB)
356 return rzipstream_write(intf->rzip.fp, s, len);
365 int intfstream_printf(intfstream_internal_t *intf,
366 const char* format, ...)
376 case INTFSTREAM_FILE:
377 va_start(vl, format);
378 result = filestream_vprintf(intf->file.fp, format, vl);
381 case INTFSTREAM_MEMORY:
385 case INTFSTREAM_RZIP:
386 #if defined(HAVE_ZLIB)
387 va_start(vl, format);
388 result = rzipstream_vprintf(intf->rzip.fp, format, vl);
399 int64_t intfstream_get_ptr(intfstream_internal_t* intf)
406 case INTFSTREAM_FILE:
408 case INTFSTREAM_MEMORY:
409 return memstream_get_ptr(intf->memory.fp);
412 case INTFSTREAM_RZIP:
419 char *intfstream_gets(intfstream_internal_t *intf,
420 char *buffer, uint64_t len)
427 case INTFSTREAM_FILE:
428 return filestream_gets(intf->file.fp,
429 buffer, (size_t)len);
430 case INTFSTREAM_MEMORY:
431 return memstream_gets(intf->memory.fp,
432 buffer, (size_t)len);
435 return chdstream_gets(intf->chd.fp, buffer, len);
439 case INTFSTREAM_RZIP:
440 #if defined(HAVE_ZLIB)
441 return rzipstream_gets(intf->rzip.fp, buffer, (size_t)len);
450 int intfstream_getc(intfstream_internal_t *intf)
457 case INTFSTREAM_FILE:
458 return filestream_getc(intf->file.fp);
459 case INTFSTREAM_MEMORY:
460 return memstream_getc(intf->memory.fp);
463 return chdstream_getc(intf->chd.fp);
467 case INTFSTREAM_RZIP:
468 #if defined(HAVE_ZLIB)
469 return rzipstream_getc(intf->rzip.fp);
478 int64_t intfstream_tell(intfstream_internal_t *intf)
485 case INTFSTREAM_FILE:
486 return (int64_t)filestream_tell(intf->file.fp);
487 case INTFSTREAM_MEMORY:
488 return (int64_t)memstream_pos(intf->memory.fp);
491 return (int64_t)chdstream_tell(intf->chd.fp);
495 case INTFSTREAM_RZIP:
496 #if defined(HAVE_ZLIB)
497 return (int64_t)rzipstream_tell(intf->rzip.fp);
506 int intfstream_eof(intfstream_internal_t *intf)
513 case INTFSTREAM_FILE:
514 return filestream_eof(intf->file.fp);
515 case INTFSTREAM_MEMORY:
516 /* TODO: Add this functionality to
517 * memory_stream interface */
520 /* TODO: Add this functionality to
521 * chd_stream interface */
523 case INTFSTREAM_RZIP:
524 #if defined(HAVE_ZLIB)
525 return rzipstream_eof(intf->rzip.fp);
534 void intfstream_rewind(intfstream_internal_t *intf)
538 case INTFSTREAM_FILE:
539 filestream_rewind(intf->file.fp);
541 case INTFSTREAM_MEMORY:
542 memstream_rewind(intf->memory.fp);
546 chdstream_rewind(intf->chd.fp);
549 case INTFSTREAM_RZIP:
550 #if defined(HAVE_ZLIB)
551 rzipstream_rewind(intf->rzip.fp);
557 void intfstream_putc(intfstream_internal_t *intf, int c)
564 case INTFSTREAM_FILE:
565 filestream_putc(intf->file.fp, c);
567 case INTFSTREAM_MEMORY:
568 memstream_putc(intf->memory.fp, c);
572 case INTFSTREAM_RZIP:
573 #if defined(HAVE_ZLIB)
574 rzipstream_putc(intf->rzip.fp, c);
581 uint32_t intfstream_get_offset_to_start(intfstream_internal_t *intf)
586 if (intf->type == INTFSTREAM_CHD)
587 return chdstream_get_track_start(intf->chd.fp);
594 uint32_t intfstream_get_frame_size(intfstream_internal_t *intf)
599 if (intf->type == INTFSTREAM_CHD)
600 return chdstream_get_frame_size(intf->chd.fp);
607 uint32_t intfstream_get_first_sector(intfstream_internal_t* intf)
612 if (intf->type == INTFSTREAM_CHD)
613 return chdstream_get_first_track_sector(intf->chd.fp);
620 bool intfstream_is_compressed(intfstream_internal_t *intf)
627 case INTFSTREAM_FILE:
629 case INTFSTREAM_MEMORY:
633 case INTFSTREAM_RZIP:
634 #if defined(HAVE_ZLIB)
635 return rzipstream_is_compressed(intf->rzip.fp);
644 bool intfstream_get_crc(intfstream_internal_t *intf, uint32_t *crc)
646 int64_t data_read = 0;
647 uint32_t accumulator = 0;
648 uint8_t buffer[4096];
653 /* Ensure we start at the beginning of the file */
654 intfstream_rewind(intf);
656 while ((data_read = intfstream_read(intf, buffer, sizeof(buffer))) > 0)
657 accumulator = encoding_crc32(accumulator, buffer, (size_t)data_read);
664 /* Reset file to the beginning */
665 intfstream_rewind(intf);
670 intfstream_t* intfstream_open_file(const char *path,
671 unsigned mode, unsigned hints)
673 intfstream_info_t info;
674 intfstream_t *fd = NULL;
676 info.type = INTFSTREAM_FILE;
677 fd = (intfstream_t*)intfstream_init(&info);
682 if (!intfstream_open(fd, path, mode, hints))
690 intfstream_close(fd);
696 intfstream_t *intfstream_open_memory(void *data,
697 unsigned mode, unsigned hints, uint64_t size)
699 intfstream_info_t info;
700 intfstream_t *fd = NULL;
702 info.type = INTFSTREAM_MEMORY;
703 info.memory.buf.data = (uint8_t*)data;
704 info.memory.buf.size = size;
705 info.memory.writable = false;
707 fd = (intfstream_t*)intfstream_init(&info);
711 if (!intfstream_open(fd, NULL, mode, hints))
719 intfstream_close(fd);
725 intfstream_t *intfstream_open_writable_memory(void *data,
726 unsigned mode, unsigned hints, uint64_t size)
728 intfstream_info_t info;
729 intfstream_t *fd = NULL;
731 info.type = INTFSTREAM_MEMORY;
732 info.memory.buf.data = (uint8_t*)data;
733 info.memory.buf.size = size;
734 info.memory.writable = true;
736 fd = (intfstream_t*)intfstream_init(&info);
740 if (!intfstream_open(fd, NULL, mode, hints))
748 intfstream_close(fd);
754 intfstream_t *intfstream_open_chd_track(const char *path,
755 unsigned mode, unsigned hints, int32_t track)
757 intfstream_info_t info;
758 intfstream_t *fd = NULL;
760 info.type = INTFSTREAM_CHD;
761 info.chd.track = track;
763 fd = (intfstream_t*)intfstream_init(&info);
768 if (!intfstream_open(fd, path, mode, hints))
776 intfstream_close(fd);
782 intfstream_t* intfstream_open_rzip_file(const char *path,
785 intfstream_info_t info;
786 intfstream_t *fd = NULL;
788 info.type = INTFSTREAM_RZIP;
789 fd = (intfstream_t*)intfstream_init(&info);
794 if (!intfstream_open(fd, path, mode, RETRO_VFS_FILE_ACCESS_HINT_NONE))
802 intfstream_close(fd);