1
0

MCADefrag: Fixed bugs, now produces valid MCA files.

This commit is contained in:
madmaxoft 2014-02-13 12:48:22 +01:00
parent 05590fb91d
commit cd658e02e8
2 changed files with 22 additions and 3 deletions

View File

@ -11,6 +11,13 @@
// An array of 4096 zero bytes, used for writing the padding
static const Byte g_Zeroes[4096] = {0};
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {
new cMCLogger(Printf("Defrag_%08x.log", time(NULL))); new cMCLogger(Printf("Defrag_%08x.log", time(NULL)));
@ -275,7 +282,7 @@ bool cMCADefrag::cThread::WriteChunk(cFile & a_File, Byte * a_LocationRaw)
a_LocationRaw[0] = m_CurrentSectorOut >> 16; a_LocationRaw[0] = m_CurrentSectorOut >> 16;
a_LocationRaw[1] = (m_CurrentSectorOut >> 8) & 0xff; a_LocationRaw[1] = (m_CurrentSectorOut >> 8) & 0xff;
a_LocationRaw[2] = m_CurrentSectorOut & 0xff; a_LocationRaw[2] = m_CurrentSectorOut & 0xff;
a_LocationRaw[3] = (m_CompressedChunkDataSize + (4 KiB) - 1) / (4 KiB); a_LocationRaw[3] = (m_CompressedChunkDataSize + (4 KiB) + 3) / (4 KiB); // +3 because the m_CompressedChunkDataSize doesn't include the exact-length
// Write the data length: // Write the data length:
Byte Buf[4]; Byte Buf[4];
@ -295,6 +302,17 @@ bool cMCADefrag::cThread::WriteChunk(cFile & a_File, Byte * a_LocationRaw)
LOGWARNING("Failed to write chunk data!"); LOGWARNING("Failed to write chunk data!");
return false; return false;
} }
// Pad onto the next sector:
int NumPadding = a_LocationRaw[3] * 4096 - (m_CompressedChunkDataSize + 4);
ASSERT(NumPadding >= 0);
if ((NumPadding > 0) && (a_File.Write(g_Zeroes, NumPadding) != NumPadding))
{
LOGWARNING("Failed to write padding");
return false;
}
m_CurrentSectorOut += a_LocationRaw[3];
return true; return true;
} }

View File

@ -47,10 +47,11 @@ protected:
/** The current compressed chunk data. Valid after a successful ReadChunk(). /** The current compressed chunk data. Valid after a successful ReadChunk().
This contains only the compression method byte and the compressed data, This contains only the compression method byte and the compressed data,
but not the length preceding the data in the MCA file. */ but not the exact-length preceding the data in the MCA file. */
unsigned char m_CompressedChunkData[MAX_COMPRESSED_CHUNK_DATA_SIZE]; unsigned char m_CompressedChunkData[MAX_COMPRESSED_CHUNK_DATA_SIZE];
/** Size of the actual current compressed chunk data. */ /** Size of the actual current compressed chunk data, excluding the 4 exact-length bytes.
This is the amount of bytes in m_CompressedChunkData[] that are valid. */
int m_CompressedChunkDataSize; int m_CompressedChunkDataSize;
/** The current raw chunk data. Valid after a successful ReadChunk(), if recompression is active. */ /** The current raw chunk data. Valid after a successful ReadChunk(), if recompression is active. */