diff --git a/Storage/Cartridge/Formats/PRG.cpp b/Storage/Cartridge/Formats/PRG.cpp index 93bc6e1fb..de48f9361 100644 --- a/Storage/Cartridge/Formats/PRG.cpp +++ b/Storage/Cartridge/Formats/PRG.cpp @@ -13,7 +13,7 @@ using namespace Storage::Cartridge; PRG::PRG(const std::string &file_name) { - Storage::FileHolder file(file_name.c_str(), FileHolder::FileMode::Read); + Storage::FileHolder file(file_name.c_str(), FileMode::Read); const auto loading_address = file.get_le(); if(loading_address != 0xa000) { diff --git a/Storage/Disk/DiskImage/Formats/2MG.cpp b/Storage/Disk/DiskImage/Formats/2MG.cpp index a70f4edfb..9d6524155 100644 --- a/Storage/Disk/DiskImage/Formats/2MG.cpp +++ b/Storage/Disk/DiskImage/Formats/2MG.cpp @@ -27,7 +27,7 @@ Disk2MG::DiskOrMassStorageDevice Disk2MG::open(const std::string &file_name) { FileHolder file(file_name); // Check the signature. - if(!file.check_signature("2IMG")) throw Error::InvalidFormat; + if(!file.check_signature("2IMG")) throw Error::InvalidFormat; // Grab the creator, potential to fix the data size momentarily. const auto creator = file.read(4); diff --git a/Storage/Disk/DiskImage/Formats/CPCDSK.cpp b/Storage/Disk/DiskImage/Formats/CPCDSK.cpp index 9d7939833..ff2921ee7 100644 --- a/Storage/Disk/DiskImage/Formats/CPCDSK.cpp +++ b/Storage/Disk/DiskImage/Formats/CPCDSK.cpp @@ -23,10 +23,10 @@ CPCDSK::CPCDSK(const std::string &file_name) : FileHolder file(file_name); is_read_only_ = file.is_known_read_only(); - if(!file.check_signature("MV - CPC")) { + if(!file.check_signature("MV - CPC")) { is_extended_ = true; file.seek(0, Whence::SET); - if(!file.check_signature("EXTENDED")) + if(!file.check_signature("EXTENDED")) throw Error::InvalidFormat; } @@ -276,7 +276,7 @@ void CPCDSK::set_tracks(const std::map<::Storage::Disk::Track::Address, std::uni } // Rewrite the entire disk image, in extended form. - Storage::FileHolder output(file_name_, Storage::FileHolder::FileMode::Rewrite); + Storage::FileHolder output(file_name_, Storage::FileMode::Rewrite); output.write(reinterpret_cast("EXTENDED CPC DSK File\r\nDisk-Info\r\n"), 34); output.write(reinterpret_cast("Clock Signal "), 14); output.put(uint8_t(head_position_count_)); diff --git a/Storage/Disk/DiskImage/Formats/G64.cpp b/Storage/Disk/DiskImage/Formats/G64.cpp index 0164caa5f..28e05b164 100644 --- a/Storage/Disk/DiskImage/Formats/G64.cpp +++ b/Storage/Disk/DiskImage/Formats/G64.cpp @@ -19,7 +19,7 @@ using namespace Storage::Disk; G64::G64(const std::string &file_name) : file_(file_name) { // read and check the file signature - if(!file_.check_signature("GCR-1541")) throw Error::InvalidFormat; + if(!file_.check_signature("GCR-1541")) throw Error::InvalidFormat; // check the version number int version = file_.get(); diff --git a/Storage/Disk/DiskImage/Formats/HFE.cpp b/Storage/Disk/DiskImage/Formats/HFE.cpp index cfe4b27c3..d0b660f14 100644 --- a/Storage/Disk/DiskImage/Formats/HFE.cpp +++ b/Storage/Disk/DiskImage/Formats/HFE.cpp @@ -15,7 +15,7 @@ using namespace Storage::Disk; HFE::HFE(const std::string &file_name) : file_(file_name) { - if(!file_.check_signature("HXCPICFE")) throw Error::InvalidFormat; + if(!file_.check_signature("HXCPICFE")) throw Error::InvalidFormat; if(file_.get()) throw Error::UnknownVersion; track_count_ = file_.get(); diff --git a/Storage/Disk/DiskImage/Formats/IMD.cpp b/Storage/Disk/DiskImage/Formats/IMD.cpp index 536132f6e..5e7e9a4ea 100644 --- a/Storage/Disk/DiskImage/Formats/IMD.cpp +++ b/Storage/Disk/DiskImage/Formats/IMD.cpp @@ -21,7 +21,7 @@ using namespace Storage::Disk; IMD::IMD(const std::string &file_name) : file_(file_name) { // Check for signature. - if(!file_.check_signature("IMD")) { + if(!file_.check_signature("IMD")) { throw Error::InvalidFormat; } diff --git a/Storage/Disk/DiskImage/Formats/OricMFMDSK.cpp b/Storage/Disk/DiskImage/Formats/OricMFMDSK.cpp index 683b1684f..fba65f3cb 100644 --- a/Storage/Disk/DiskImage/Formats/OricMFMDSK.cpp +++ b/Storage/Disk/DiskImage/Formats/OricMFMDSK.cpp @@ -18,7 +18,7 @@ using namespace Storage::Disk; OricMFMDSK::OricMFMDSK(const std::string &file_name) : file_(file_name) { - if(!file_.check_signature("MFM_DISK")) + if(!file_.check_signature("MFM_DISK")) throw Error::InvalidFormat; head_count_ = file_.get_le(); diff --git a/Storage/Disk/DiskImage/Formats/STX.cpp b/Storage/Disk/DiskImage/Formats/STX.cpp index 841e93db2..136270a01 100644 --- a/Storage/Disk/DiskImage/Formats/STX.cpp +++ b/Storage/Disk/DiskImage/Formats/STX.cpp @@ -391,7 +391,7 @@ private: STX::STX(const std::string &file_name) : file_(file_name) { // Require that this be a version 3 Pasti. - if(!file_.check_signature("RSY\0")) throw Error::InvalidFormat; + if(!file_.check_signature("RSY")) throw Error::InvalidFormat; if(file_.get_le() != 3) throw Error::InvalidFormat; // Skip: tool used, 2 reserved bytes. diff --git a/Storage/Disk/DiskImage/Formats/WOZ.cpp b/Storage/Disk/DiskImage/Formats/WOZ.cpp index 485878c30..f431ec36d 100644 --- a/Storage/Disk/DiskImage/Formats/WOZ.cpp +++ b/Storage/Disk/DiskImage/Formats/WOZ.cpp @@ -33,9 +33,9 @@ WOZ::WOZ(const std::string &file_name) : char(0xff), 0x0a, 0x0d, 0x0a }; - const bool isWoz1 = file_.check_signature(signature1); + const bool isWoz1 = file_.check_signature(signature1); file_.seek(0, Whence::SET); - const bool isWoz2 = file_.check_signature(signature2); + const bool isWoz2 = file_.check_signature(signature2); if(!isWoz1 && !isWoz2) throw Error::InvalidFormat; type_ = isWoz2 ? Type::WOZ2 : Type::WOZ1; diff --git a/Storage/FileHolder.hpp b/Storage/FileHolder.hpp index 14356375e..5ebfc3c92 100644 --- a/Storage/FileHolder.hpp +++ b/Storage/FileHolder.hpp @@ -25,18 +25,23 @@ enum class Whence: int { END = SEEK_END, }; +enum class SignatureType { + String, + Binary, +}; + +enum class FileMode { + ReadWrite, + Read, + Rewrite +}; + class FileHolder final { public: enum class Error { CantOpen = -1 }; - enum class FileMode { - ReadWrite, - Read, - Rewrite - }; - ~FileHolder(); /*! @@ -70,7 +75,7 @@ public: Optionally limits itself to only @c size bytes. */ template - void put_be(IntT value) { + void put_be(const IntT value) { auto shift = size * 8; while(shift) { shift -= 8; @@ -161,9 +166,10 @@ public: @returns @c true if the bytes read match the signature; @c false otherwise. */ - template + template bool check_signature(const char (&signature)[size]) { - constexpr auto signature_length = size - 1; + // Discard C-style trailing NULL if this is a string compare. + constexpr auto signature_length = size - (type == SignatureType::String ? 1 : 0); std::array stored_signature; if(read(stored_signature) != size) { @@ -215,7 +221,7 @@ private: }; inline std::vector contents_of(const std::string &file_name) { - FileHolder file(file_name, FileHolder::FileMode::Read); + FileHolder file(file_name, FileMode::Read); return file.read(size_t(file.stats().st_size)); } diff --git a/Storage/MassStorage/Formats/VHD.cpp b/Storage/MassStorage/Formats/VHD.cpp index f2c2eb1ce..c45bb545d 100644 --- a/Storage/MassStorage/Formats/VHD.cpp +++ b/Storage/MassStorage/Formats/VHD.cpp @@ -21,7 +21,7 @@ VHD::VHD(const std::string &file_name) : file_(file_name) { default: throw std::exception(); } - if(!file_.check_signature("conectix")) { + if(!file_.check_signature("conectix")) { throw std::exception(); } diff --git a/Storage/State/SZX.cpp b/Storage/State/SZX.cpp index 482a337e1..6ce42631f 100644 --- a/Storage/State/SZX.cpp +++ b/Storage/State/SZX.cpp @@ -37,7 +37,7 @@ std::unique_ptr SZX::load(const std::string &file_name result->state = std::unique_ptr(state); // Check signature and major version number. - if(!file.check_signature("ZXST")) { + if(!file.check_signature("ZXST")) { return nullptr; } const uint8_t major_version = file.get(); diff --git a/Storage/Tape/Formats/CAS.cpp b/Storage/Tape/Formats/CAS.cpp index 52b00e14b..3cbe584f7 100644 --- a/Storage/Tape/Formats/CAS.cpp +++ b/Storage/Tape/Formats/CAS.cpp @@ -60,7 +60,7 @@ const auto ascii_signature = signature<0xea>; } CAS::CAS(const std::string &file_name) { - Storage::FileHolder file(file_name, FileHolder::FileMode::Read); + Storage::FileHolder file(file_name, FileMode::Read); enum class Mode { Seeking, diff --git a/Storage/Tape/Formats/CSW.cpp b/Storage/Tape/Formats/CSW.cpp index e9f4a90df..a779c3a16 100644 --- a/Storage/Tape/Formats/CSW.cpp +++ b/Storage/Tape/Formats/CSW.cpp @@ -15,11 +15,11 @@ using namespace Storage::Tape; CSW::CSW(const std::string &file_name) { - Storage::FileHolder file(file_name, FileHolder::FileMode::Read); + Storage::FileHolder file(file_name, FileMode::Read); if(file.stats().st_size < 0x20) throw ErrorNotCSW; // Check signature. - if(!file.check_signature("Compressed Square Wave")) { + if(!file.check_signature("Compressed Square Wave")) { throw ErrorNotCSW; } diff --git a/Storage/Tape/Formats/CommodoreTAP.cpp b/Storage/Tape/Formats/CommodoreTAP.cpp index 03f4ee92c..3e78c2fdb 100644 --- a/Storage/Tape/Formats/CommodoreTAP.cpp +++ b/Storage/Tape/Formats/CommodoreTAP.cpp @@ -15,9 +15,9 @@ using namespace Storage::Tape; CommodoreTAP::CommodoreTAP(const std::string &file_name) : file_name_(file_name) { Storage::FileHolder file(file_name); - const bool is_c64 = file.check_signature("C64-TAPE-RAW"); + const bool is_c64 = file.check_signature("C64-TAPE-RAW"); file.seek(0, Whence::SET); - const bool is_c16 = file.check_signature("C16-TAPE-RAW"); + const bool is_c16 = file.check_signature("C16-TAPE-RAW"); if(!is_c64 && !is_c16) { throw ErrorNotCommodoreTAP; } @@ -60,7 +60,7 @@ CommodoreTAP::Serialiser::Serialiser( Pulse initial, bool half_waves, bool updated_layout) : - file_(file_name, FileHolder::FileMode::Read), + file_(file_name, FileMode::Read), current_pulse_(initial), half_waves_(half_waves), updated_layout_(updated_layout) diff --git a/Storage/Tape/Formats/OricTAP.cpp b/Storage/Tape/Formats/OricTAP.cpp index e8f7c927b..83b6a34a4 100644 --- a/Storage/Tape/Formats/OricTAP.cpp +++ b/Storage/Tape/Formats/OricTAP.cpp @@ -13,7 +13,7 @@ using namespace Storage::Tape; OricTAP::OricTAP(const std::string &file_name) : file_name_(file_name) { - Storage::FileHolder file(file_name, FileHolder::FileMode::Read); + Storage::FileHolder file(file_name, FileMode::Read); // Check for a sequence of at least three 0x16s followed by a 0x24. while(true) { @@ -34,7 +34,7 @@ std::unique_ptr OricTAP::format_serialiser() const { return std::make_unique(file_name_); } -OricTAP::Serialiser::Serialiser(const std::string &file_name) : file_(file_name, FileHolder::FileMode::Read) { +OricTAP::Serialiser::Serialiser(const std::string &file_name) : file_(file_name, FileMode::Read) { reset(); } diff --git a/Storage/Tape/Formats/TZX.cpp b/Storage/Tape/Formats/TZX.cpp index 625e93a8b..e7048cbe1 100644 --- a/Storage/Tape/Formats/TZX.cpp +++ b/Storage/Tape/Formats/TZX.cpp @@ -22,10 +22,10 @@ Log::Logger logger; } TZX::TZX(const std::string &file_name) : file_name_(file_name) { - Storage::FileHolder file(file_name, FileHolder::FileMode::Read); + Storage::FileHolder file(file_name, FileMode::Read); // Check for signature followed by a 0x1a - if(!file.check_signature("ZXTape!")) throw ErrorNotTZX; + if(!file.check_signature("ZXTape!")) throw ErrorNotTZX; if(file.get() != 0x1a) throw ErrorNotTZX; // Get version number @@ -40,7 +40,7 @@ std::unique_ptr TZX::format_serialiser() const { return std::make_unique(file_name_); } -TZX::Serialiser::Serialiser(const std::string &file_name) : file_(file_name, FileHolder::FileMode::Read) { +TZX::Serialiser::Serialiser(const std::string &file_name) : file_(file_name, FileMode::Read) { reset(); } diff --git a/Storage/Tape/Formats/TapePRG.cpp b/Storage/Tape/Formats/TapePRG.cpp index ef3f83f7a..9a6498a0d 100644 --- a/Storage/Tape/Formats/TapePRG.cpp +++ b/Storage/Tape/Formats/TapePRG.cpp @@ -49,7 +49,7 @@ using namespace Storage::Tape; PRG::PRG(const std::string &file_name) : file_name_(file_name) { - FileHolder file(file_name, FileHolder::FileMode::Read); + FileHolder file(file_name, FileMode::Read); // There's really no way to validate other than that if this file is larger than 64kb, // of if load address + length > 65536 then it's broken. @@ -68,7 +68,7 @@ std::unique_ptr PRG::format_serialiser() const { } PRG::Serialiser::Serialiser(const std::string &file_name, uint16_t load_address, uint16_t length) : - file_(file_name, FileHolder::FileMode::Read), + file_(file_name, FileMode::Read), load_address_(load_address), length_(length), timings_(false) diff --git a/Storage/Tape/Formats/ZX80O81P.cpp b/Storage/Tape/Formats/ZX80O81P.cpp index bd15886a7..c649fc6c1 100644 --- a/Storage/Tape/Formats/ZX80O81P.cpp +++ b/Storage/Tape/Formats/ZX80O81P.cpp @@ -12,7 +12,7 @@ using namespace Storage::Tape; ZX80O81P::ZX80O81P(const std::string &file_name) { - Storage::FileHolder file(file_name, FileHolder::FileMode::Read); + Storage::FileHolder file(file_name, FileMode::Read); // Grab file contents. data_ = file.read(size_t(file.stats().st_size)); diff --git a/Storage/Tape/Formats/ZXSpectrumTAP.cpp b/Storage/Tape/Formats/ZXSpectrumTAP.cpp index c9f826563..4cba30783 100644 --- a/Storage/Tape/Formats/ZXSpectrumTAP.cpp +++ b/Storage/Tape/Formats/ZXSpectrumTAP.cpp @@ -40,7 +40,7 @@ std::unique_ptr ZXSpectrumTAP::format_serialiser() const { return std::make_unique(file_name_); } -ZXSpectrumTAP::Serialiser::Serialiser(const std::string &file_name) : file_(file_name, FileHolder::FileMode::Read) { +ZXSpectrumTAP::Serialiser::Serialiser(const std::string &file_name) : file_(file_name, FileMode::Read) { read_next_block(); }