| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include "mednafen.h" |
| 19 | #include <stdarg.h> |
| 20 | #include <string.h> |
| 21 | #include <errno.h> |
| 22 | |
| 23 | #include <boolean.h> |
| 24 | |
| 25 | #include "file.h" |
| 26 | #include "general.h" |
| 27 | |
| 28 | // This function should ALWAYS close the system file "descriptor"(gzip library, zip library, or FILE *) it's given, |
| 29 | // even if it errors out. |
| 30 | bool MDFNFILE::MakeMemWrapAndClose(void *fp) |
| 31 | { |
| 32 | bool ret = false; |
| 33 | |
| 34 | location = 0; |
| 35 | |
| 36 | ::fseek((FILE *)fp, 0, SEEK_END); |
| 37 | f_size = ::ftell((FILE *)fp); |
| 38 | ::fseek((FILE *)fp, 0, SEEK_SET); |
| 39 | |
| 40 | if (!(f_data = (uint8*)malloc(f_size))) |
| 41 | goto fail; |
| 42 | ::fread(f_data, 1, f_size, (FILE *)fp); |
| 43 | |
| 44 | ret = true; |
| 45 | fail: |
| 46 | fclose((FILE*)fp); |
| 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | MDFNFILE::MDFNFILE() |
| 51 | { |
| 52 | f_data = NULL; |
| 53 | f_size = 0; |
| 54 | f_ext = NULL; |
| 55 | |
| 56 | location = 0; |
| 57 | } |
| 58 | |
| 59 | MDFNFILE::MDFNFILE(const char *path, const void *known_ext, const char *purpose) |
| 60 | { |
| 61 | (void)known_ext; |
| 62 | Open(path, known_ext, purpose, false); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | MDFNFILE::~MDFNFILE() |
| 67 | { |
| 68 | Close(); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | bool MDFNFILE::Open(const char *path, const void *known_ext, const char *purpose, const bool suppress_notfound_pe) |
| 73 | { |
| 74 | FILE *fp; |
| 75 | (void)known_ext; |
| 76 | |
| 77 | if (!(fp = fopen(path, "rb"))) |
| 78 | return false; |
| 79 | |
| 80 | ::fseek(fp, 0, SEEK_SET); |
| 81 | |
| 82 | if (!MakeMemWrapAndClose(fp)) |
| 83 | return false; |
| 84 | |
| 85 | const char *ld = (const char*)strrchr(path, '.'); |
| 86 | f_ext = strdup(ld ? ld + 1 : ""); |
| 87 | |
| 88 | return(true); |
| 89 | } |
| 90 | |
| 91 | bool MDFNFILE::Close(void) |
| 92 | { |
| 93 | if (f_ext) |
| 94 | free(f_ext); |
| 95 | f_ext = 0; |
| 96 | |
| 97 | if (f_data) |
| 98 | free(f_data); |
| 99 | f_data = 0; |
| 100 | |
| 101 | return(1); |
| 102 | } |
| 103 | |
| 104 | uint64 MDFNFILE::fread(void *ptr, size_t element_size, size_t nmemb) |
| 105 | { |
| 106 | int64 avail = f_size - location; |
| 107 | if (nmemb > avail) |
| 108 | nmemb = avail; |
| 109 | |
| 110 | memcpy((uint8*)ptr, f_data + location, nmemb); |
| 111 | location += nmemb; |
| 112 | return nmemb; |
| 113 | } |
| 114 | |
| 115 | int MDFNFILE::fseek(int64 offset, int whence) |
| 116 | { |
| 117 | size_t ptr; |
| 118 | |
| 119 | switch(whence) |
| 120 | { |
| 121 | case SEEK_SET: |
| 122 | ptr = offset; |
| 123 | break; |
| 124 | case SEEK_CUR: |
| 125 | ptr = location + offset; |
| 126 | break; |
| 127 | case SEEK_END: |
| 128 | ptr = f_size + offset; |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | if (ptr <= f_size) |
| 133 | { |
| 134 | location = ptr; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | int MDFNFILE::read16le(uint16 *val) |
| 142 | { |
| 143 | if ((location + 2) > f_size) |
| 144 | return 0; |
| 145 | |
| 146 | *val = MDFN_de16lsb(f_data + location); |
| 147 | |
| 148 | location += 2; |
| 149 | |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | int MDFNFILE::read32le(uint32 *val) |
| 154 | { |
| 155 | if ((location + 4) > f_size) |
| 156 | return 0; |
| 157 | |
| 158 | *val = MDFN_de32lsb(f_data + location); |
| 159 | |
| 160 | location += 4; |
| 161 | |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | static INLINE bool MDFN_DumpToFileReal(const char *filename, int compress, const std::vector<PtrLengthPair> &pearpairs) |
| 166 | { |
| 167 | FILE *fp = fopen(filename, "wb"); |
| 168 | |
| 169 | if (!fp) |
| 170 | return 0; |
| 171 | |
| 172 | for(unsigned int i = 0; i < pearpairs.size(); i++) |
| 173 | { |
| 174 | const void *data = pearpairs[i].GetData(); |
| 175 | const uint64 length = pearpairs[i].GetLength(); |
| 176 | |
| 177 | if (fwrite(data, 1, length, fp) != length) |
| 178 | { |
| 179 | fclose(fp); |
| 180 | return 0; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (fclose(fp) == EOF) |
| 185 | return 0; |
| 186 | |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | bool MDFN_DumpToFile(const char *filename, int compress, const void *data, uint64 length) |
| 191 | { |
| 192 | std::vector<PtrLengthPair> tmp_pairs; |
| 193 | tmp_pairs.push_back(PtrLengthPair(data, length)); |
| 194 | return (MDFN_DumpToFileReal(filename, compress, tmp_pairs)); |
| 195 | } |