| 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"cxx . . $ `<file>/mednafen/mednafen.h`/ |
| 19 | #include <stdarg.h> |
| 20 | #include <string.h> |
| 21 | #include <errno.h> |
| 22 | |
| 23 | #include <boolean.h>cxx . . $ `<file>/boolean.h`/ |
| 24 | |
| 25 | #include "file.h"cxx . . $ `<file>/mednafen/file.h`/ |
| 26 | #include "general.h"cxx . . $ `<file>/mednafen/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 MDFNFILEcxx . . $ MDFNFILE#::MakeMemWrapAndClosecxx . . $ MDFNFILE#MakeMemWrapAndClose(db00c6a08f501612).(void *fp) |
| 31 | { |
| 32 | bool ret = false; |
| 33 | |
| 34 | locationcxx . . $ MDFNFILE#location. = 0; |
| 35 | |
| 36 | ::fseek((FILE *)fp, 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!); |
| 37 | f_sizecxx . . $ MDFNFILE#f_size. = ::ftell((FILE *)fp); |
| 38 | ::fseek((FILE *)fp, 0, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 39 | |
| 40 | if (!(f_datacxx . . $ MDFNFILE#f_data. = (uint8cxx . . $ uint8#*)malloccxx . . $ malloc(9f008b2a9698428b).(f_sizecxx . . $ MDFNFILE#f_size.))) |
| 41 | goto fail; |
| 42 | ::fread(f_datacxx . . $ MDFNFILE#f_data., 1, f_sizecxx . . $ MDFNFILE#f_size., (FILE *)fp); |
| 43 | |
| 44 | ret = true; |
| 45 | fail: |
| 46 | fclose((FILE*)fp); |
| 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | MDFNFILEcxx . . $ MDFNFILE#::MDFNFILEcxx . . $ MDFNFILE#MDFNFILE(49f6e7a06ebc5aa8).() |
| 51 | { |
| 52 | f_datacxx . . $ MDFNFILE#f_data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 53 | f_sizecxx . . $ MDFNFILE#f_size. = 0; |
| 54 | f_extcxx . . $ MDFNFILE#f_ext. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 55 | |
| 56 | locationcxx . . $ MDFNFILE#location. = 0; |
| 57 | } |
| 58 | |
| 59 | MDFNFILEcxx . . $ MDFNFILE#::MDFNFILEcxx . . $ MDFNFILE#MDFNFILE(9c85eaa12893977b).(const char *path, const void *known_ext, const char *purpose) |
| 60 | { |
| 61 | (void)known_ext; |
| 62 | Opencxx . . $ MDFNFILE#Open(eb4f300025480a43).(path, known_ext, purpose, false); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | MDFNFILEcxx . . $ MDFNFILE#::~cxx . . $ MDFNFILE#`~MDFNFILE`(ced63f7c635d850d).MDFNFILEcxx . . $ MDFNFILE#() |
| 67 | { |
| 68 | Closecxx . . $ MDFNFILE#Close(c9857a73d68e59ba).(); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | bool MDFNFILEcxx . . $ MDFNFILE#::Opencxx . . $ MDFNFILE#Open(eb4f300025480a43).(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_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 81 | |
| 82 | if (!MakeMemWrapAndClosecxx . . $ MDFNFILE#MakeMemWrapAndClose(db00c6a08f501612).(fp)) |
| 83 | return false; |
| 84 | |
| 85 | const char *ld = (const char*)strrchr(path, '.'); |
| 86 | f_extcxx . . $ MDFNFILE#f_ext. = strdup(ld ? ld + 1 : ""); |
| 87 | |
| 88 | return(true); |
| 89 | } |
| 90 | |
| 91 | bool MDFNFILEcxx . . $ MDFNFILE#::Closecxx . . $ MDFNFILE#Close(c9857a73d68e59ba).(void) |
| 92 | { |
| 93 | if (f_extcxx . . $ MDFNFILE#f_ext.) |
| 94 | freecxx . . $ free(bd21765a0afc8e3c).(f_extcxx . . $ MDFNFILE#f_ext.); |
| 95 | f_extcxx . . $ MDFNFILE#f_ext. = 0; |
| 96 | |
| 97 | if (f_datacxx . . $ MDFNFILE#f_data.) |
| 98 | freecxx . . $ free(bd21765a0afc8e3c).(f_datacxx . . $ MDFNFILE#f_data.); |
| 99 | f_datacxx . . $ MDFNFILE#f_data. = 0; |
| 100 | |
| 101 | return(1); |
| 102 | } |
| 103 | |
| 104 | uint64cxx . . $ uint64# MDFNFILEcxx . . $ MDFNFILE#::freadcxx . . $ MDFNFILE#fread(e2eaf0fd878a292f).(void *ptr, size_t element_size, size_t nmemb) |
| 105 | { |
| 106 | int64cxx . . $ int64# avail = f_sizecxx . . $ MDFNFILE#f_size. - locationcxx . . $ MDFNFILE#location.; |
| 107 | if (nmemb > avail) |
| 108 | nmemb = avail; |
| 109 | |
| 110 | memcpy((uint8cxx . . $ uint8#*)ptr, f_datacxx . . $ MDFNFILE#f_data. + locationcxx . . $ MDFNFILE#location., nmemb); |
| 111 | locationcxx . . $ MDFNFILE#location. += nmemb; |
| 112 | return nmemb; |
| 113 | } |
| 114 | |
| 115 | int MDFNFILEcxx . . $ MDFNFILE#::fseekcxx . . $ MDFNFILE#fseek(4a3ed3c01b538bac).(int64cxx . . $ int64# offset, int whence) |
| 116 | { |
| 117 | size_t ptr; |
| 118 | |
| 119 | switch(whence) |
| 120 | { |
| 121 | case SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!: |
| 122 | ptr = offset; |
| 123 | break; |
| 124 | case SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!: |
| 125 | ptr = locationcxx . . $ MDFNFILE#location. + offset; |
| 126 | break; |
| 127 | case SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!: |
| 128 | ptr = f_sizecxx . . $ MDFNFILE#f_size. + offset; |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | if (ptr <= f_sizecxx . . $ MDFNFILE#f_size.) |
| 133 | { |
| 134 | locationcxx . . $ MDFNFILE#location. = ptr; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | int MDFNFILEcxx . . $ MDFNFILE#::read16lecxx . . $ MDFNFILE#read16le(910e03eb156beb90).(uint16cxx . . $ uint16# *val) |
| 142 | { |
| 143 | if ((locationcxx . . $ MDFNFILE#location. + 2) > f_sizecxx . . $ MDFNFILE#f_size.) |
| 144 | return 0; |
| 145 | |
| 146 | *val = MDFN_de16lsb(f_datacxx . . $ MDFNFILE#f_data. + locationcxx . . $ MDFNFILE#location.); |
| 147 | |
| 148 | locationcxx . . $ MDFNFILE#location. += 2; |
| 149 | |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | int MDFNFILEcxx . . $ MDFNFILE#::read32lecxx . . $ MDFNFILE#read32le(4ebf403dd87dd80).(uint32cxx . . $ uint32# *val) |
| 154 | { |
| 155 | if ((locationcxx . . $ MDFNFILE#location. + 4) > f_sizecxx . . $ MDFNFILE#f_size.) |
| 156 | return 0; |
| 157 | |
| 158 | *val = MDFN_de32lsb(f_datacxx . . $ MDFNFILE#f_data. + locationcxx . . $ MDFNFILE#location.); |
| 159 | |
| 160 | locationcxx . . $ MDFNFILE#location. += 4; |
| 161 | |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | static INLINEcxx . . $ `mednafen/mednafen-types.h:21:11`! bool MDFN_DumpToFileRealcxx . . $ MDFN_DumpToFileReal(c1363855640ddc00).(const char *filename, int compress, const stdcxx . . $ std/::vectorcxx . . $ std/vector#<PtrLengthPaircxx . . $ 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.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); i++) |
| 173 | { |
| 174 | const void *data = pearpairs[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i].GetDatacxx . . $ PtrLengthPair#GetData(3a8ef3fdc6e817ef).(); |
| 175 | const uint64cxx . . $ uint64# length = pearpairs[cxx . . $ std/vector#`operator[]`(c8255e0fa326ac32).i].GetLengthcxx . . $ PtrLengthPair#GetLength(fe4d2d262d4164a8).(); |
| 176 | |
| 177 | if (fwrite(data, 1, length, fp) != length) |
| 178 | { |
| 179 | fclose(fp); |
| 180 | return 0; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (fclose(fp) == EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!) |
| 185 | return 0; |
| 186 | |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | bool MDFN_DumpToFilecxx . . $ MDFN_DumpToFile(aa08695a99e98aac).(const char *filename, int compress, const void *data, uint64cxx . . $ uint64# length) |
| 191 | { |
| 192 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<PtrLengthPaircxx . . $ PtrLengthPair#> tmp_pairscxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 193 | tmp_pairs.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(PtrLengthPaircxx . . $ PtrLengthPair#PtrLengthPair(8c7266571c1d65cc).(data, length)); |
| 194 | return (MDFN_DumpToFileRealcxx . . $ MDFN_DumpToFileReal(c1363855640ddc00).(filename, compress, tmp_pairs)); |
| 195 | } |