diff options
Diffstat (limited to 'src/emu/fileio.cpp')
-rw-r--r-- | src/emu/fileio.cpp | 228 |
1 files changed, 67 insertions, 161 deletions
diff --git a/src/emu/fileio.cpp b/src/emu/fileio.cpp index 79bf746fbfa..812f2a7007f 100644 --- a/src/emu/fileio.cpp +++ b/src/emu/fileio.cpp @@ -10,7 +10,6 @@ #include "emu.h" #include "unzip.h" -#include "un7z.h" #include "fileio.h" @@ -146,8 +145,6 @@ emu_file::emu_file(UINT32 openflags) m_openflags(openflags), m_zipfile(nullptr), m_ziplength(0), - m__7zfile(nullptr), - m__7zlength(0), m_remove_on_close(false), m_restrict_to_mediapath(false) { @@ -164,8 +161,6 @@ emu_file::emu_file(const char *searchpath, UINT32 openflags) m_openflags(openflags), m_zipfile(nullptr), m_ziplength(0), - m__7zfile(nullptr), - m__7zlength(0), m_remove_on_close(false), m_restrict_to_mediapath(false) { @@ -228,12 +223,6 @@ hash_collection &emu_file::hashes(const char *types) return m_hashes; // if we have ZIP data, just hash that directly - if (!m__7zdata.empty()) - { - m_hashes.compute(&m__7zdata[0], m__7zdata.size(), needed.c_str()); - return m_hashes; - } - if (!m_zipdata.empty()) { m_hashes.compute(&m_zipdata[0], m_zipdata.size(), needed.c_str()); @@ -255,7 +244,7 @@ hash_collection &emu_file::hashes(const char *types) // open - open a file by searching paths //------------------------------------------------- -file_error emu_file::open(const char *name) +osd_file::error emu_file::open(const char *name) { // remember the filename and CRC info m_filename = name; @@ -267,28 +256,28 @@ file_error emu_file::open(const char *name) return open_next(); } -file_error emu_file::open(const char *name1, const char *name2) +osd_file::error emu_file::open(const char *name1, const char *name2) { // concatenate the strings and do a standard open std::string name = std::string(name1).append(name2); return open(name.c_str()); } -file_error emu_file::open(const char *name1, const char *name2, const char *name3) +osd_file::error emu_file::open(const char *name1, const char *name2, const char *name3) { // concatenate the strings and do a standard open std::string name = std::string(name1).append(name2).append(name3); return open(name.c_str()); } -file_error emu_file::open(const char *name1, const char *name2, const char *name3, const char *name4) +osd_file::error emu_file::open(const char *name1, const char *name2, const char *name3, const char *name4) { // concatenate the strings and do a standard open std::string name = std::string(name1).append(name2).append(name3).append(name4); return open(name.c_str()); } -file_error emu_file::open(const char *name, UINT32 crc) +osd_file::error emu_file::open(const char *name, UINT32 crc) { // remember the filename and CRC info m_filename = name; @@ -300,21 +289,21 @@ file_error emu_file::open(const char *name, UINT32 crc) return open_next(); } -file_error emu_file::open(const char *name1, const char *name2, UINT32 crc) +osd_file::error emu_file::open(const char *name1, const char *name2, UINT32 crc) { // concatenate the strings and do a standard open std::string name = std::string(name1).append(name2); return open(name.c_str(), crc); } -file_error emu_file::open(const char *name1, const char *name2, const char *name3, UINT32 crc) +osd_file::error emu_file::open(const char *name1, const char *name2, const char *name3, UINT32 crc) { // concatenate the strings and do a standard open std::string name = std::string(name1).append(name2).append(name3); return open(name.c_str(), crc); } -file_error emu_file::open(const char *name1, const char *name2, const char *name3, const char *name4, UINT32 crc) +osd_file::error emu_file::open(const char *name1, const char *name2, const char *name3, const char *name4, UINT32 crc) { // concatenate the strings and do a standard open std::string name = std::string(name1).append(name2).append(name3).append(name4); @@ -327,19 +316,19 @@ file_error emu_file::open(const char *name1, const char *name2, const char *name // the filename by iterating over paths //------------------------------------------------- -file_error emu_file::open_next() +osd_file::error emu_file::open_next() { // if we're open from a previous attempt, close up now if (m_file != nullptr) close(); // loop over paths - file_error filerr = FILERR_NOT_FOUND; + osd_file::error filerr = osd_file::error::NOT_FOUND; while (m_iterator.next(m_fullpath, m_filename.c_str())) { // attempt to open the file directly - filerr = util::core_file::open(m_fullpath.c_str(), m_openflags, m_file); - if (filerr == FILERR_NONE) + filerr = util::core_file::open(m_fullpath, m_openflags, m_file); + if (filerr == osd_file::error::NONE) break; // if we're opening for read-only we have other options @@ -348,13 +337,13 @@ file_error emu_file::open_next() std::string tempfullpath = m_fullpath; filerr = attempt_zipped(); - if (filerr == FILERR_NONE) + if (filerr == osd_file::error::NONE) break; m_fullpath = tempfullpath; filerr = attempt__7zped(); - if (filerr == FILERR_NONE) + if (filerr == osd_file::error::NONE) break; } } @@ -367,7 +356,7 @@ file_error emu_file::open_next() // just an array of data in RAM //------------------------------------------------- -file_error emu_file::open_ram(const void *data, UINT32 length) +osd_file::error emu_file::open_ram(const void *data, UINT32 length) { // set a fake filename and CRC m_filename = "RAM"; @@ -386,21 +375,13 @@ file_error emu_file::open_ram(const void *data, UINT32 length) void emu_file::close() { // close files and free memory - if (m__7zfile != nullptr) - _7z_file_close(m__7zfile); - m__7zfile = nullptr; - - if (m_zipfile != nullptr) - zip_file_close(m_zipfile); - m_zipfile = nullptr; - + m_zipfile.reset(); m_file.reset(); - m__7zdata.clear(); m_zipdata.clear(); if (m_remove_on_close) - osd_rmfile(m_fullpath.c_str()); + osd_file::remove(m_fullpath); m_remove_on_close = false; // reset our hashes and path as well @@ -415,7 +396,7 @@ void emu_file::close() // compression, or up to 9 for max compression //------------------------------------------------- -file_error emu_file::compress(int level) +osd_file::error emu_file::compress(int level) { return m_file->compress(level); } @@ -429,10 +410,7 @@ file_error emu_file::compress(int level) bool emu_file::compressed_file_ready(void) { // load the ZIP file now if we haven't yet - if (m__7zfile != nullptr && load__7zped_file() != FILERR_NONE) - return true; - - if (m_zipfile != nullptr && load_zipped_file() != FILERR_NONE) + if (m_zipfile && (load_zipped_file() != osd_file::error::NONE)) return true; return false; @@ -499,9 +477,6 @@ bool emu_file::eof() UINT64 emu_file::size() { // use the ZIP length if present - if (m__7zfile != nullptr) - return m__7zlength; - if (m_zipfile != nullptr) return m_ziplength; @@ -657,7 +632,7 @@ bool emu_file::part_of_mediapath(std::string path) // attempt_zipped - attempt to open a ZIPped file //------------------------------------------------- -file_error emu_file::attempt_zipped() +osd_file::error emu_file::attempt_zipped() { std::string filename; @@ -667,11 +642,11 @@ file_error emu_file::attempt_zipped() // find the final path separator int dirsep = m_fullpath.find_last_of(PATH_SEPARATOR[0]); if (dirsep == -1) - return FILERR_NOT_FOUND; + return osd_file::error::NOT_FOUND; if (restrict_to_mediapath()) if ( !part_of_mediapath(m_fullpath) ) - return FILERR_NOT_FOUND; + return osd_file::error::NOT_FOUND; // insert the part from the right of the separator into the head of the filename if (filename.length() > 0) @@ -682,117 +657,51 @@ file_error emu_file::attempt_zipped() m_fullpath = m_fullpath.substr(0, dirsep).append(".zip"); // attempt to open the ZIP file - zip_file *zip; - zip_error ziperr = zip_file_open(m_fullpath.c_str(), &zip); + util::archive_file::ptr zip; + util::archive_file::error ziperr = util::archive_file::open_zip(m_fullpath, zip); // chop the .zip back off the filename before continuing m_fullpath = m_fullpath.substr(0, dirsep); // if we failed to open this file, continue scanning - if (ziperr != ZIPERR_NONE) + if (ziperr != util::archive_file::error::NONE) continue; + int header = -1; + // see if we can find a file with the right name and (if available) crc - const zip_file_header *header; - for (header = zip_file_first_file(zip); header != nullptr; header = zip_file_next_file(zip)) - if (zip_filename_match(*header, filename) && (!(m_openflags & OPEN_FLAG_HAS_CRC) || header->crc == m_crc)) - break; + if (m_openflags & OPEN_FLAG_HAS_CRC) header = zip->search(m_crc, filename); // if that failed, look for a file with the right crc, but the wrong filename - if (header == nullptr && (m_openflags & OPEN_FLAG_HAS_CRC)) - for (header = zip_file_first_file(zip); header != nullptr; header = zip_file_next_file(zip)) - if (header->crc == m_crc && !zip_header_is_path(*header)) - break; + if (header < 0 && (m_openflags & OPEN_FLAG_HAS_CRC)) header = zip->search(m_crc); // if that failed, look for a file with the right name; reporting a bad checksum // is more helpful and less confusing than reporting "rom not found" - if (header == nullptr) - for (header = zip_file_first_file(zip); header != nullptr; header = zip_file_next_file(zip)) - if (zip_filename_match(*header, filename)) - break; + if (header < 0) header = zip->search(filename); // if we got it, read the data - if (header != nullptr) + if (header >= 0) { - m_zipfile = zip; - m_ziplength = header->uncompressed_length; + m_zipfile = std::move(zip); + m_ziplength = m_zipfile->current_uncompressed_length(); // build a hash with just the CRC m_hashes.reset(); - m_hashes.add_crc(header->crc); - return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? FILERR_NONE : load_zipped_file(); + m_hashes.add_crc(m_zipfile->current_crc()); + return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? osd_file::error::NONE : load_zipped_file(); } // close up the ZIP file and try the next level - zip_file_close(zip); - } -} - - -//------------------------------------------------- -// load_zipped_file - load a ZIPped file -//------------------------------------------------- - -file_error emu_file::load_zipped_file() -{ - assert(m_file == nullptr); - assert(m_zipdata.empty()); - assert(m_zipfile != nullptr); - - // allocate some memory - m_zipdata.resize(m_ziplength); - - // read the data into our buffer and return - zip_error ziperr = zip_file_decompress(m_zipfile, &m_zipdata[0], m_zipdata.size()); - if (ziperr != ZIPERR_NONE) - { - m_zipdata.clear(); - return FILERR_FAILURE; - } - - // convert to RAM file - file_error filerr = util::core_file::open_ram(&m_zipdata[0], m_zipdata.size(), m_openflags, m_file); - if (filerr != FILERR_NONE) - { - m_zipdata.clear(); - return FILERR_FAILURE; + zip.reset(); } - - // close out the ZIP file - zip_file_close(m_zipfile); - m_zipfile = nullptr; - return FILERR_NONE; -} - - -//------------------------------------------------- -// zip_filename_match - compare zip filename -// to expected filename, ignoring any directory -//------------------------------------------------- - -bool emu_file::zip_filename_match(const zip_file_header &header, const std::string &filename) -{ - const char *zipfile = header.filename + header.filename_length - filename.length(); - return (zipfile >= header.filename && core_stricmp(filename.c_str(),zipfile) == 0 && (zipfile == header.filename || zipfile[-1] == '/')); } //------------------------------------------------- -// zip_header_is_path - check whether filename -// in header is a path -//------------------------------------------------- - -bool emu_file::zip_header_is_path(const zip_file_header &header) -{ - const char *zipfile = header.filename + header.filename_length - 1; - return (zipfile >= header.filename && zipfile[0] == '/'); -} - -//------------------------------------------------- // attempt__7zped - attempt to open a .7z file //------------------------------------------------- -file_error emu_file::attempt__7zped() +osd_file::error emu_file::attempt__7zped() { std::string filename; @@ -802,11 +711,11 @@ file_error emu_file::attempt__7zped() // find the final path separator int dirsep = m_fullpath.find_last_of(PATH_SEPARATOR[0]); if (dirsep == -1) - return FILERR_NOT_FOUND; + return osd_file::error::NOT_FOUND; if (restrict_to_mediapath()) if ( !part_of_mediapath(m_fullpath) ) - return FILERR_NOT_FOUND; + return osd_file::error::NOT_FOUND; // insert the part from the right of the separator into the head of the filename if (filename.length() > 0) @@ -817,78 +726,75 @@ file_error emu_file::attempt__7zped() m_fullpath = m_fullpath.substr(0, dirsep).append(".7z"); // attempt to open the _7Z file - _7z_file *_7z; - _7z_error _7zerr = _7z_file_open(m_fullpath.c_str(), &_7z); + util::archive_file::ptr _7z; + util::archive_file::error _7zerr = util::archive_file::open_7z(m_fullpath, _7z); // chop the ._7z back off the filename before continuing m_fullpath = m_fullpath.substr(0, dirsep); // if we failed to open this file, continue scanning - if (_7zerr != _7ZERR_NONE) + if (_7zerr != util::archive_file::error::NONE) continue; int fileno = -1; // see if we can find a file with the right name and (if available) crc - if (m_openflags & OPEN_FLAG_HAS_CRC) fileno = _7z_search_crc_match(_7z, m_crc, filename.c_str(), filename.length(), true, true); + if (m_openflags & OPEN_FLAG_HAS_CRC) fileno = _7z->search(m_crc, filename); // if that failed, look for a file with the right crc, but the wrong filename - if (fileno==-1) - if (m_openflags & OPEN_FLAG_HAS_CRC) fileno = _7z_search_crc_match(_7z, m_crc, filename.c_str(), filename.length(), true, false); + if ((fileno < 0) && (m_openflags & OPEN_FLAG_HAS_CRC)) fileno = _7z->search(m_crc); // if that failed, look for a file with the right name; reporting a bad checksum // is more helpful and less confusing than reporting "rom not found" - if (fileno==-1) - fileno = _7z_search_crc_match(_7z, m_crc, filename.c_str(), filename.length(), false, true); + if (fileno < 0) fileno = _7z->search(filename); - if (fileno != -1) + if (fileno >= 0) { - m__7zfile = _7z; - m__7zlength = _7z->uncompressed_length; + m_zipfile = std::move(_7z); + m_ziplength = m_zipfile->current_uncompressed_length(); // build a hash with just the CRC m_hashes.reset(); - m_hashes.add_crc(_7z->crc); - return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? FILERR_NONE : load__7zped_file(); + m_hashes.add_crc(m_zipfile->current_crc()); + return (m_openflags & OPEN_FLAG_NO_PRELOAD) ? osd_file::error::NONE : load_zipped_file(); } // close up the _7Z file and try the next level - _7z_file_close(_7z); + _7z.reset(); } } //------------------------------------------------- -// load__7zped_file - load a _7Zped file +// load_zipped_file - load a ZIPped file //------------------------------------------------- -file_error emu_file::load__7zped_file() +osd_file::error emu_file::load_zipped_file() { assert(m_file == nullptr); - assert(m__7zdata.empty()); - assert(m__7zfile != nullptr); + assert(m_zipdata.empty()); + assert(m_zipfile); // allocate some memory - m__7zdata.resize(m__7zlength); + m_zipdata.resize(m_ziplength); // read the data into our buffer and return - _7z_error _7zerr = _7z_file_decompress(m__7zfile, &m__7zdata[0], m__7zdata.size()); - if (_7zerr != _7ZERR_NONE) + auto const ziperr = m_zipfile->decompress(&m_zipdata[0], m_zipdata.size()); + if (ziperr != util::archive_file::error::NONE) { - m__7zdata.clear(); - return FILERR_FAILURE; + m_zipdata.clear(); + return osd_file::error::FAILURE; } // convert to RAM file - file_error filerr = util::core_file::open_ram(&m__7zdata[0], m__7zdata.size(), m_openflags, m_file); - if (filerr != FILERR_NONE) + osd_file::error filerr = util::core_file::open_ram(&m_zipdata[0], m_zipdata.size(), m_openflags, m_file); + if (filerr != osd_file::error::NONE) { - m__7zdata.clear(); - return FILERR_FAILURE; + m_zipdata.clear(); + return osd_file::error::FAILURE; } - // close out the _7Z file - _7z_file_close(m__7zfile); - m__7zfile = nullptr; - return FILERR_NONE; + // close out the ZIP file + m_zipfile.reset(); + return osd_file::error::NONE; } |