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
|
/* Copyright (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (interface_stream.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <streams/interface_stream.h>
#include <streams/file_stream.h>
#include <streams/memory_stream.h>
#ifdef HAVE_CHD
#include <streams/chd_stream.h>
#endif
#if defined(HAVE_ZLIB)
#include <streams/rzip_stream.h>
#endif
#include <encodings/crc32.h>
struct intfstream_internal
{
struct
{
RFILE *fp;
} file;
struct
{
memstream_t *fp;
struct
{
uint8_t *data;
uint64_t size;
} buf;
bool writable;
} memory;
#ifdef HAVE_CHD
struct
{
chdstream_t *fp;
int32_t track;
} chd;
#endif
#if defined(HAVE_ZLIB)
struct
{
rzipstream_t *fp;
} rzip;
#endif
enum intfstream_type type;
};
int64_t intfstream_get_size(intfstream_internal_t *intf)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_get_size(intf->file.fp);
case INTFSTREAM_MEMORY:
return intf->memory.buf.size;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return chdstream_get_size(intf->chd.fp);
#else
break;
#endif
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
return rzipstream_get_size(intf->rzip.fp);
#else
break;
#endif
}
return 0;
}
bool intfstream_resize(intfstream_internal_t *intf, intfstream_info_t *info)
{
if (!intf || !info)
return false;
switch (intf->type)
{
case INTFSTREAM_FILE:
break;
case INTFSTREAM_MEMORY:
intf->memory.buf.data = info->memory.buf.data;
intf->memory.buf.size = info->memory.buf.size;
memstream_set_buffer(intf->memory.buf.data,
intf->memory.buf.size);
break;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
#endif
break;
case INTFSTREAM_RZIP:
/* Unsupported */
return false;
}
return true;
}
bool intfstream_open(intfstream_internal_t *intf, const char *path,
unsigned mode, unsigned hints)
{
if (!intf)
return false;
switch (intf->type)
{
case INTFSTREAM_FILE:
intf->file.fp = filestream_open(path, mode, hints);
if (!intf->file.fp)
return false;
break;
case INTFSTREAM_MEMORY:
intf->memory.fp = memstream_open(intf->memory.writable);
if (!intf->memory.fp)
return false;
break;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
intf->chd.fp = chdstream_open(path, intf->chd.track);
if (!intf->chd.fp)
return false;
break;
#else
return false;
#endif
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
intf->rzip.fp = rzipstream_open(path, mode);
if (!intf->rzip.fp)
return false;
break;
#else
return false;
#endif
}
return true;
}
int intfstream_flush(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_flush(intf->file.fp);
case INTFSTREAM_MEMORY:
case INTFSTREAM_CHD:
case INTFSTREAM_RZIP:
/* Should we stub this for these interfaces? */
break;
}
return 0;
}
int intfstream_close(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
if (intf->file.fp)
return filestream_close(intf->file.fp);
return 0;
case INTFSTREAM_MEMORY:
if (intf->memory.fp)
memstream_close(intf->memory.fp);
return 0;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
if (intf->chd.fp)
chdstream_close(intf->chd.fp);
#endif
return 0;
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
if (intf->rzip.fp)
return rzipstream_close(intf->rzip.fp);
#endif
return 0;
}
return -1;
}
void *intfstream_init(intfstream_info_t *info)
{
intfstream_internal_t *intf = NULL;
if (!info)
goto error;
intf = (intfstream_internal_t*)malloc(sizeof(*intf));
if (!intf)
goto error;
intf->type = info->type;
intf->file.fp = NULL;
intf->memory.buf.data = NULL;
intf->memory.buf.size = 0;
intf->memory.fp = NULL;
intf->memory.writable = false;
#ifdef HAVE_CHD
intf->chd.track = 0;
intf->chd.fp = NULL;
#endif
#ifdef HAVE_ZLIB
intf->rzip.fp = NULL;
#endif
switch (intf->type)
{
case INTFSTREAM_FILE:
break;
case INTFSTREAM_MEMORY:
intf->memory.writable = info->memory.writable;
if (!intfstream_resize(intf, info))
goto error;
break;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
intf->chd.track = info->chd.track;
break;
#else
goto error;
#endif
case INTFSTREAM_RZIP:
break;
}
return intf;
error:
if (intf)
free(intf);
return NULL;
}
int64_t intfstream_seek(
intfstream_internal_t *intf, int64_t offset, int whence)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
{
int seek_position = 0;
switch (whence)
{
case SEEK_SET:
seek_position = RETRO_VFS_SEEK_POSITION_START;
break;
case SEEK_CUR:
seek_position = RETRO_VFS_SEEK_POSITION_CURRENT;
break;
case SEEK_END:
seek_position = RETRO_VFS_SEEK_POSITION_END;
break;
}
return (int64_t)filestream_seek(intf->file.fp, (int64_t)offset,
seek_position);
}
case INTFSTREAM_MEMORY:
return (int64_t)memstream_seek(intf->memory.fp, offset, whence);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return (int64_t)chdstream_seek(intf->chd.fp, offset, whence);
#else
break;
#endif
case INTFSTREAM_RZIP:
/* Unsupported */
break;
}
return -1;
}
int64_t intfstream_truncate(intfstream_internal_t *intf, uint64_t len)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_truncate(intf->file.fp, len);
case INTFSTREAM_MEMORY:
break;
case INTFSTREAM_CHD:
break;
case INTFSTREAM_RZIP:
break;
}
return 0;
}
int64_t intfstream_read(intfstream_internal_t *intf, void *s, uint64_t len)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_read(intf->file.fp, s, len);
case INTFSTREAM_MEMORY:
return memstream_read(intf->memory.fp, s, len);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return chdstream_read(intf->chd.fp, s, len);
#else
break;
#endif
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
return rzipstream_read(intf->rzip.fp, s, len);
#else
break;
#endif
}
return -1;
}
int64_t intfstream_write(intfstream_internal_t *intf,
const void *s, uint64_t len)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_write(intf->file.fp, s, len);
case INTFSTREAM_MEMORY:
return memstream_write(intf->memory.fp, s, len);
case INTFSTREAM_CHD:
return -1;
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
return rzipstream_write(intf->rzip.fp, s, len);
#else
return -1;
#endif
}
return 0;
}
int intfstream_printf(intfstream_internal_t *intf,
const char* format, ...)
{
va_list vl;
int result;
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
va_start(vl, format);
result = filestream_vprintf(intf->file.fp, format, vl);
va_end(vl);
return result;
case INTFSTREAM_MEMORY:
return -1;
case INTFSTREAM_CHD:
return -1;
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
va_start(vl, format);
result = rzipstream_vprintf(intf->rzip.fp, format, vl);
va_end(vl);
return result;
#else
return -1;
#endif
}
return 0;
}
int64_t intfstream_get_ptr(intfstream_internal_t* intf)
{
if (!intf)
return 0;
switch (intf->type)
{
case INTFSTREAM_FILE:
return -1;
case INTFSTREAM_MEMORY:
return memstream_get_ptr(intf->memory.fp);
case INTFSTREAM_CHD:
return -1;
case INTFSTREAM_RZIP:
return -1;
}
return 0;
}
char *intfstream_gets(intfstream_internal_t *intf,
char *buffer, uint64_t len)
{
if (!intf)
return NULL;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_gets(intf->file.fp,
buffer, (size_t)len);
case INTFSTREAM_MEMORY:
return memstream_gets(intf->memory.fp,
buffer, (size_t)len);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return chdstream_gets(intf->chd.fp, buffer, len);
#else
break;
#endif
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
return rzipstream_gets(intf->rzip.fp, buffer, (size_t)len);
#else
break;
#endif
}
return NULL;
}
int intfstream_getc(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_getc(intf->file.fp);
case INTFSTREAM_MEMORY:
return memstream_getc(intf->memory.fp);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return chdstream_getc(intf->chd.fp);
#else
break;
#endif
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
return rzipstream_getc(intf->rzip.fp);
#else
break;
#endif
}
return -1;
}
int64_t intfstream_tell(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return (int64_t)filestream_tell(intf->file.fp);
case INTFSTREAM_MEMORY:
return (int64_t)memstream_pos(intf->memory.fp);
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
return (int64_t)chdstream_tell(intf->chd.fp);
#else
break;
#endif
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
return (int64_t)rzipstream_tell(intf->rzip.fp);
#else
break;
#endif
}
return -1;
}
int intfstream_eof(intfstream_internal_t *intf)
{
if (!intf)
return -1;
switch (intf->type)
{
case INTFSTREAM_FILE:
return filestream_eof(intf->file.fp);
case INTFSTREAM_MEMORY:
/* TODO: Add this functionality to
* memory_stream interface */
break;
case INTFSTREAM_CHD:
/* TODO: Add this functionality to
* chd_stream interface */
break;
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
return rzipstream_eof(intf->rzip.fp);
#else
break;
#endif
}
return -1;
}
void intfstream_rewind(intfstream_internal_t *intf)
{
switch (intf->type)
{
case INTFSTREAM_FILE:
filestream_rewind(intf->file.fp);
break;
case INTFSTREAM_MEMORY:
memstream_rewind(intf->memory.fp);
break;
case INTFSTREAM_CHD:
#ifdef HAVE_CHD
chdstream_rewind(intf->chd.fp);
#endif
break;
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
rzipstream_rewind(intf->rzip.fp);
#endif
break;
}
}
void intfstream_putc(intfstream_internal_t *intf, int c)
{
if (!intf)
return;
switch (intf->type)
{
case INTFSTREAM_FILE:
filestream_putc(intf->file.fp, c);
break;
case INTFSTREAM_MEMORY:
memstream_putc(intf->memory.fp, c);
break;
case INTFSTREAM_CHD:
break;
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
rzipstream_putc(intf->rzip.fp, c);
#else
break;
#endif
}
}
uint32_t intfstream_get_offset_to_start(intfstream_internal_t *intf)
{
if (intf)
{
#ifdef HAVE_CHD
if (intf->type == INTFSTREAM_CHD)
return chdstream_get_track_start(intf->chd.fp);
#endif
}
return 0;
}
uint32_t intfstream_get_frame_size(intfstream_internal_t *intf)
{
if (intf)
{
#ifdef HAVE_CHD
if (intf->type == INTFSTREAM_CHD)
return chdstream_get_frame_size(intf->chd.fp);
#endif
}
return 0;
}
uint32_t intfstream_get_first_sector(intfstream_internal_t* intf)
{
if (intf)
{
#ifdef HAVE_CHD
if (intf->type == INTFSTREAM_CHD)
return chdstream_get_first_track_sector(intf->chd.fp);
#endif
}
return 0;
}
bool intfstream_is_compressed(intfstream_internal_t *intf)
{
if (!intf)
return false;
switch (intf->type)
{
case INTFSTREAM_FILE:
return false;
case INTFSTREAM_MEMORY:
return false;
case INTFSTREAM_CHD:
return true;
case INTFSTREAM_RZIP:
#if defined(HAVE_ZLIB)
return rzipstream_is_compressed(intf->rzip.fp);
#else
break;
#endif
}
return false;
}
bool intfstream_get_crc(intfstream_internal_t *intf, uint32_t *crc)
{
int64_t data_read = 0;
uint32_t accumulator = 0;
uint8_t buffer[4096];
if (!intf || !crc)
return false;
/* Ensure we start at the beginning of the file */
intfstream_rewind(intf);
while ((data_read = intfstream_read(intf, buffer, sizeof(buffer))) > 0)
accumulator = encoding_crc32(accumulator, buffer, (size_t)data_read);
if (data_read < 0)
return false;
*crc = accumulator;
/* Reset file to the beginning */
intfstream_rewind(intf);
return true;
}
intfstream_t* intfstream_open_file(const char *path,
unsigned mode, unsigned hints)
{
intfstream_info_t info;
intfstream_t *fd = NULL;
info.type = INTFSTREAM_FILE;
fd = (intfstream_t*)intfstream_init(&info);
if (!fd)
return NULL;
if (!intfstream_open(fd, path, mode, hints))
goto error;
return fd;
error:
if (fd)
{
intfstream_close(fd);
free(fd);
}
return NULL;
}
intfstream_t *intfstream_open_memory(void *data,
unsigned mode, unsigned hints, uint64_t size)
{
intfstream_info_t info;
intfstream_t *fd = NULL;
info.type = INTFSTREAM_MEMORY;
info.memory.buf.data = (uint8_t*)data;
info.memory.buf.size = size;
info.memory.writable = false;
fd = (intfstream_t*)intfstream_init(&info);
if (!fd)
return NULL;
if (!intfstream_open(fd, NULL, mode, hints))
goto error;
return fd;
error:
if (fd)
{
intfstream_close(fd);
free(fd);
}
return NULL;
}
intfstream_t *intfstream_open_writable_memory(void *data,
unsigned mode, unsigned hints, uint64_t size)
{
intfstream_info_t info;
intfstream_t *fd = NULL;
info.type = INTFSTREAM_MEMORY;
info.memory.buf.data = (uint8_t*)data;
info.memory.buf.size = size;
info.memory.writable = true;
fd = (intfstream_t*)intfstream_init(&info);
if (!fd)
return NULL;
if (!intfstream_open(fd, NULL, mode, hints))
goto error;
return fd;
error:
if (fd)
{
intfstream_close(fd);
free(fd);
}
return NULL;
}
intfstream_t *intfstream_open_chd_track(const char *path,
unsigned mode, unsigned hints, int32_t track)
{
intfstream_info_t info;
intfstream_t *fd = NULL;
info.type = INTFSTREAM_CHD;
info.chd.track = track;
fd = (intfstream_t*)intfstream_init(&info);
if (!fd)
return NULL;
if (!intfstream_open(fd, path, mode, hints))
goto error;
return fd;
error:
if (fd)
{
intfstream_close(fd);
free(fd);
}
return NULL;
}
intfstream_t* intfstream_open_rzip_file(const char *path,
unsigned mode)
{
intfstream_info_t info;
intfstream_t *fd = NULL;
info.type = INTFSTREAM_RZIP;
fd = (intfstream_t*)intfstream_init(&info);
if (!fd)
return NULL;
if (!intfstream_open(fd, path, mode, RETRO_VFS_FILE_ACCESS_HINT_NONE))
goto error;
return fd;
error:
if (fd)
{
intfstream_close(fd);
free(fd);
}
return NULL;
}
|