Skip to content

Commit

Permalink
[#80] Support for HxC Floppy Emulator *.HFE images (Part 1.3: Fixed w…
Browse files Browse the repository at this point in the history
…orking with invalid FloppyInterface value in header)
  • Loading branch information
tomas-nestorovic committed Dec 27, 2023
1 parent ce5a866 commit 9a3166a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
58 changes: 29 additions & 29 deletions Main/src/HFE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@



#define HEADER_SIGNATURE "HXCPICFE"
#define HEADER_SIGNATURE "HXCPICFE"

CHFE::UHeader::UHeader(){
CHFE::UHeader::UHeader(){
// ctor
::ZeroMemory( this, sizeof(*this) );
::lstrcpyA( signature, HEADER_SIGNATURE );
::lstrcpyA( signature, HEADER_SIGNATURE );
trackEncoding=TTrackEncoding::UNKNOWN;
floppyInterface=TFloppyInterface::DISABLED;
cylInfosBegin=1;
Expand All @@ -43,10 +43,10 @@

bool CHFE::UHeader::IsValid() const{
// True <=> this Header follows HFE specification, otherwise False
return !::memcmp( signature, HEADER_SIGNATURE, sizeof(signature) )
&&
formatRevision==0
return !::memcmp( signature, HEADER_SIGNATURE, sizeof(signature) )
&&
formatRevision==0
&&
0<nCylinders // mustn't be zero for 'capsImageInfo.maxcylinder' is inclusive! (and "-1" isn't valid)
&&
0<nHeads && nHeads<=2
Expand All @@ -69,7 +69,7 @@

CHFE::CHFE()
// ctor
: CCapsBase( &Properties, '\0', true, INI_SECTION ) {
: CCapsBase( &Properties, '\0', true, INI_SECTION ) {
Reset();
}

Expand Down Expand Up @@ -153,19 +153,19 @@ formatError: ::SetLastError(ERROR_BAD_FORMAT);
// - construction of InternalTrack
if (!cylInfos[cyl].IsValid()) // maybe an error during Image creation?
return CTrackReaderWriter::Invalid;
f.Seek( cylInfos[cyl].nBlocksOffset*sizeof(TCylinderBlock)+head*sizeof(TTrackBlock), CFile::begin );
BYTE trackBytes[USHRT_MAX+1], *pTrackBytes=trackBytes;
for( WORD nCylBlocks=Utils::RoundDivUp(cylInfos[cyl].nBytesLength,(WORD)sizeof(TCylinderBlock)); nCylBlocks-->0; ){
const auto nBytesRead=f.Read( pTrackBytes, sizeof(TTrackBlock) );
f.Seek( sizeof(TTrackBlock), CFile::current ); // skip unwanted Head
pTrackBytes+=nBytesRead;
}
if (const auto trackLength=std::min<UDWORD>( cylInfos[cyl].nBytesLength, pTrackBytes-trackBytes )){
f.Seek( cylInfos[cyl].nBlocksOffset*sizeof(TCylinderBlock)+head*sizeof(TTrackBlock), CFile::begin );
BYTE trackBytes[USHRT_MAX+1], *pTrackBytes=trackBytes;
for( WORD nCylBlocks=Utils::RoundDivUp(cylInfos[cyl].nBytesLength,(WORD)sizeof(TCylinderBlock)); nCylBlocks-->0; ){
const auto nBytesRead=f.Read( pTrackBytes, sizeof(TTrackBlock) );
f.Seek( sizeof(TTrackBlock), CFile::current ); // skip unwanted Head
pTrackBytes+=nBytesRead;
}
if (const auto trackLength=std::min<UDWORD>( cylInfos[cyl].nBytesLength, pTrackBytes-trackBytes )){
CapsTrackInfoT2 cti={};
cti.trackbuf=trackBytes;
cti.tracklen=trackLength;
while (pTrackBytes-->trackBytes)
*pTrackBytes=Utils::GetReversedByte(*pTrackBytes);
cti.tracklen=trackLength;
while (pTrackBytes-->trackBytes)
*pTrackBytes=Utils::GetReversedByte(*pTrackBytes);
rit = CInternalTrack::CreateFrom( *this, &cti, 1, 0 );
return *rit;
}
Expand All @@ -175,7 +175,7 @@ formatError: ::SetLastError(ERROR_BAD_FORMAT);
TStdWinError CHFE::SetMediumTypeAndGeometry(PCFormat pFormat,PCSide sideMap,TSector firstSectorNumber){
// sets the given MediumType and its geometry; returns Windows standard i/o error
// - must be setting Medium compatible with the FloppyInterface specified in the Header
if (header.floppyInterface!=TFloppyInterface::DISABLED)
if (header.floppyInterface<TFloppyInterface::LAST_KNOWN)
switch (pFormat->mediumType){
case Medium::FLOPPY_DD:{
static constexpr TFloppyInterface Compatibles[]={ TFloppyInterface::IBM_PC_DD, TFloppyInterface::ATARI_ST_DD, TFloppyInterface::AMIGA_DD, TFloppyInterface::CPC_DD, TFloppyInterface::GENERIC_SHUGART_DD, TFloppyInterface::MSX2_DD, TFloppyInterface::C64_DD, TFloppyInterface::EMU_SHUGART, TFloppyInterface::S950_DD };
Expand Down Expand Up @@ -210,13 +210,13 @@ formatError: ::SetLastError(ERROR_BAD_FORMAT);

TStdWinError CHFE::Reset(){
// resets internal representation of the disk (e.g. by disposing all content without warning)
EXCLUSIVELY_LOCK_THIS_IMAGE();
// - base
if (const TStdWinError err=__super::Reset())
return err;
// - reinitializing to an empty Image
header=UHeader();
::ZeroMemory( cylInfos, sizeof(cylInfos) );
::ZeroMemory( &capsImageInfo, sizeof(capsImageInfo) );
return ERROR_SUCCESS;
}
EXCLUSIVELY_LOCK_THIS_IMAGE();
// - base
if (const TStdWinError err=__super::Reset())
return err;
// - reinitializing to an empty Image
header=UHeader();
::ZeroMemory( cylInfos, sizeof(cylInfos) );
::ZeroMemory( &capsImageInfo, sizeof(capsImageInfo) );
return ERROR_SUCCESS;
}
1 change: 1 addition & 0 deletions Main/src/HFE.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
EMU_SHUGART,
S950_DD,
S950_HD,
LAST_KNOWN,
DISABLED =0xfe
};

Expand Down

0 comments on commit 9a3166a

Please sign in to comment.