diff options
author | Michael Pavone <pavone@retrodev.com> | 2018-03-24 22:18:23 -0700 |
---|---|---|
committer | Michael Pavone <pavone@retrodev.com> | 2018-03-24 22:18:23 -0700 |
commit | 371d5418e6b1bfba88b55382b0a1b91d97023ae5 (patch) | |
tree | 26e75e6766ec3f43284b90f3ba5acbf72d78d646 /blastem.c | |
parent | 484e97a4e318d18b867acea7773853dfc7616a30 (diff) | |
parent | 15af9462392967b6adf7ba6ff4f7ff778cf10eb3 (diff) |
Merge
--HG--
branch : nuklear_ui
Diffstat (limited to 'blastem.c')
-rw-r--r-- | blastem.c | 139 |
1 files changed, 115 insertions, 24 deletions
@@ -24,6 +24,7 @@ #include "arena.h" #include "config.h" #include "menu.h" +#include "zip.h" #ifndef DISABLE_NUKLEAR #include "nuklear_ui/blastem_nuklear.h" #endif @@ -50,37 +51,104 @@ tern_node * config; #define SMD_MAGIC3 0xBB #define SMD_BLOCK_SIZE 0x4000 -int load_smd_rom(long filesize, FILE * f, void **buffer) +#ifdef DISABLE_ZLIB +#define ROMFILE FILE* +#define romopen fopen +#define romread fread +#define romseek fseek +#define romgetc fgetc +#define romclose fclose +#else +#include "zlib/zlib.h" +#define ROMFILE gzFile +#define romopen gzopen +#define romread gzfread +#define romseek gzseek +#define romgetc gzgetc +#define romclose gzclose +#endif + +int load_smd_rom(ROMFILE f, void **buffer) { uint8_t block[SMD_BLOCK_SIZE]; - filesize -= SMD_HEADER_SIZE; - fseek(f, SMD_HEADER_SIZE, SEEK_SET); - - uint16_t *dst = *buffer = malloc(nearest_pow2(filesize)); - int rom_size = filesize; - while (filesize > 0) { - fread(block, 1, SMD_BLOCK_SIZE, f); - for (uint8_t *low = block, *high = (block+SMD_BLOCK_SIZE/2), *end = block+SMD_BLOCK_SIZE; high < end; high++, low++) { - *(dst++) = *low << 8 | *high; + romseek(f, SMD_HEADER_SIZE, SEEK_SET); + + size_t filesize = 512 * 1024; + size_t readsize = 0; + uint16_t *dst = malloc(filesize); + + + size_t read; + do { + if ((readsize + SMD_BLOCK_SIZE > filesize)) { + filesize *= 2; + dst = realloc(dst, filesize); + } + read = romread(block, 1, SMD_BLOCK_SIZE, f); + if (read > 0) { + for (uint8_t *low = block, *high = (block+read/2), *end = block+read; high < end; high++, low++) { + *(dst++) = *low << 8 | *high; + } + readsize += read; + } + } while(read > 0); + romclose(f); + + *buffer = dst; + + return readsize; +} + +uint32_t load_rom_zip(char *filename, void **dst) +{ + static const char *valid_exts[] = {"bin", "md", "gen", "sms", "rom"}; + const uint32_t num_exts = sizeof(valid_exts)/sizeof(*valid_exts); + zip_file *z = zip_open(filename); + if (!z) { + return 0; + } + + for (uint32_t i = 0; i < z->num_entries; i++) + { + char *ext = path_extension(z->entries[i].name); + if (!ext) { + continue; } - filesize -= SMD_BLOCK_SIZE; + for (uint32_t j = 0; j < num_exts; j++) + { + if (!strcasecmp(ext, valid_exts[j])) { + size_t out_size = nearest_pow2(z->entries[i].size); + *dst = zip_read(z, i, &out_size); + if (*dst) { + free(ext); + zip_close(z); + return out_size; + } + } + } + free(ext); } - return rom_size; + zip_close(z); + return 0; } uint32_t load_rom(char * filename, void **dst, system_type *stype) { uint8_t header[10]; - FILE * f = fopen(filename, "rb"); + char *ext = path_extension(filename); + if (!strcasecmp(ext, "zip")) { + free(ext); + return load_rom_zip(filename, dst); + } + free(ext); + ROMFILE f = romopen(filename, "rb"); if (!f) { return 0; } - if (sizeof(header) != fread(header, 1, sizeof(header), f)) { + if (sizeof(header) != romread(header, 1, sizeof(header), f)) { fatal_error("Error reading from %s\n", filename); } - fseek(f, 0, SEEK_END); - long filesize = ftell(f); - fseek(f, 0, SEEK_SET); + if (header[1] == SMD_MAGIC1 && header[8] == SMD_MAGIC2 && header[9] == SMD_MAGIC3) { int i; for (i = 3; i < 8; i++) { @@ -95,15 +163,38 @@ uint32_t load_rom(char * filename, void **dst, system_type *stype) if (stype) { *stype = SYSTEM_GENESIS; } - return load_smd_rom(filesize, f, dst); + return load_smd_rom(f, dst); } } - *dst = malloc(nearest_pow2(filesize)); - if (filesize != fread(*dst, 1, filesize, f)) { - fatal_error("Error reading from %s\n", filename); - } - fclose(f); - return filesize; + + size_t filesize = 512 * 1024; + size_t readsize = sizeof(header); + + char *buf = malloc(filesize); + memcpy(buf, header, readsize); + + size_t read; + do { + read = romread(buf + readsize, 1, filesize - readsize, f); + if (read > 0) { + readsize += read; + if (readsize == filesize) { + int one_more = romgetc(f); + if (one_more >= 0) { + filesize *= 2; + buf = realloc(buf, filesize); + buf[readsize++] = one_more; + } else { + read = 0; + } + } + } + } while (read > 0); + + *dst = buf; + + romclose(f); + return readsize; } |