From: <man...@us...> - 2014-10-06 10:45:21
|
Revision: 4388 http://sourceforge.net/p/modplug/code/4388 Author: manxorist Date: 2014-10-06 10:45:14 +0000 (Mon, 06 Oct 2014) Log Message: ----------- [Ref] FileReader: Make the dangerous Read(T) member protected, and convert the resulting broken users to ReadStruct (if it only contains 1 byte values) or ReadConvertEndianness (in most other cases). [Ref] FileReader: Add ReadDoubleLE and ReadDoubleBE. [Ref] MT2 loader: Use ReadDoubleLE for loading double values. [Fix] STM loader: STM loader was broken on big-endian. [Fix] GZIP unarchiver: Was slightly incorrent on big-endian (where it's currently not used however) [Fix[ J2B loader: Fix inconsistency between little and big endian (no negative consequences though at the moment). [Fix] DLS loader: Fix a place of brokenness on big-endian (there are still other broken places but it's not used on big-endian at the moment anyway). Modified Paths: -------------- trunk/OpenMPT/soundlib/Dlsbank.cpp trunk/OpenMPT/soundlib/FileReader.h trunk/OpenMPT/soundlib/Load_amf.cpp trunk/OpenMPT/soundlib/Load_ams.cpp trunk/OpenMPT/soundlib/Load_dbm.cpp trunk/OpenMPT/soundlib/Load_dmf.cpp trunk/OpenMPT/soundlib/Load_it.cpp trunk/OpenMPT/soundlib/Load_itp.cpp trunk/OpenMPT/soundlib/Load_mod.cpp trunk/OpenMPT/soundlib/Load_mt2.cpp trunk/OpenMPT/soundlib/Load_psm.cpp trunk/OpenMPT/soundlib/Load_stm.cpp trunk/OpenMPT/soundlib/Load_ult.cpp trunk/OpenMPT/soundlib/load_j2b.cpp trunk/OpenMPT/unarchiver/ungzip.cpp trunk/OpenMPT/unarchiver/ungzip.h Modified: trunk/OpenMPT/soundlib/Dlsbank.cpp =================================================================== --- trunk/OpenMPT/soundlib/Dlsbank.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Dlsbank.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -19,6 +19,7 @@ #include "Wav.h" #include "../common/StringFixer.h" #include "../soundlib/FileReader.h" +#include "../common/Endianness.h" #include "SampleIO.h" #include <math.h> @@ -275,10 +276,17 @@ typedef struct PACKED WSMPSAMPLELOOP { - DWORD cbSize; - DWORD ulLoopType; - DWORD ulLoopStart; - DWORD ulLoopLength; + uint32 cbSize; + uint32 ulLoopType; + uint32 ulLoopStart; + uint32 ulLoopLength; + void ConvertEndianness() + { + SwapBytesLE(cbSize); + SwapBytesLE(ulLoopType); + SwapBytesLE(ulLoopStart); + SwapBytesLE(ulLoopLength); + } } WSMPSAMPLELOOP; STATIC_ASSERT(sizeof(WSMPSAMPLELOOP) == 16); @@ -1611,7 +1619,7 @@ { WSMPSAMPLELOOP loop; wsmpChunk.Skip(8 + wsmp.cbSize); - wsmpChunk.Read(loop); + wsmpChunk.ReadConvertEndianness(loop); if(loop.ulLoopLength > 3) { sample.uFlags.set(CHN_LOOP); Modified: trunk/OpenMPT/soundlib/FileReader.h =================================================================== --- trunk/OpenMPT/soundlib/FileReader.h 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/FileReader.h 2014-10-06 10:45:14 UTC (rev 4388) @@ -566,6 +566,8 @@ streamPos += result; return result; } + +protected: // Read a "T" object from the stream. // If not enough bytes can be read, false is returned. @@ -791,6 +793,34 @@ } } + // Read 64-Bit float in little-endian format. + // If successful, the file cursor is advanced by the size of the float. + double ReadDoubleLE() + { + IEEE754binary64LE target; + if(Read(target)) + { + return target; + } else + { + return 0.0f; + } + } + + // Read 64-Bit float in big-endian format. + // If successful, the file cursor is advanced by the size of the float. + double ReadDoubleBE() + { + IEEE754binary64BE target; + if(Read(target)) + { + return target; + } else + { + return 0.0f; + } + } + // Read a struct. // If successful, the file cursor is advanced by the size of the struct. Otherwise, the target is zeroed. template <typename T> Modified: trunk/OpenMPT/soundlib/Load_amf.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_amf.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_amf.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -109,7 +109,7 @@ file.Rewind(); AsylumFileHeader fileHeader; - if(!file.Read(fileHeader) + if(!file.ReadStruct(fileHeader) || strncmp(fileHeader.signature, "ASYLUM Music Format V1.0", 25) || fileHeader.numSamples > 64 || !file.CanRead(256 + 64 * sizeof(AsylumSampleHeader) + 64 * 4 * 8 * fileHeader.numPatterns)) Modified: trunk/OpenMPT/soundlib/Load_ams.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_ams.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_ams.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -562,7 +562,7 @@ // Read envelope and do partial conversion. void ConvertToMPT(InstrumentEnvelope &mptEnv, FileReader &file) { - file.Read(*this); + file.ReadStruct(*this); // Read envelope points uint8 data[64][3]; Modified: trunk/OpenMPT/soundlib/Load_dbm.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_dbm.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_dbm.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -299,7 +299,7 @@ DBMFileHeader fileHeader; file.Rewind(); - if(!file.Read(fileHeader) + if(!file.ReadStruct(fileHeader) || memcmp(fileHeader.dbm0, "DBM0", 4) || fileHeader.trkVerHi > 3) { Modified: trunk/OpenMPT/soundlib/Load_dmf.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_dmf.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_dmf.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -961,7 +961,7 @@ { DMFFileHeader fileHeader; file.Rewind(); - if(!file.Read(fileHeader) + if(!file.ReadStruct(fileHeader) || memcmp(fileHeader.signature, "DDMF", 4) || !fileHeader.version || fileHeader.version > 10) { Modified: trunk/OpenMPT/soundlib/Load_it.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_it.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_it.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -537,7 +537,7 @@ } // Reading MIDI Output & Macros - if(m_SongFlags[SONG_EMBEDMIDICFG] && file.Read(m_MidiCfg)) + if(m_SongFlags[SONG_EMBEDMIDICFG] && file.ReadStruct<MIDIMacroConfigData>(m_MidiCfg)) { m_MidiCfg.Sanitize(); } Modified: trunk/OpenMPT/soundlib/Load_itp.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_itp.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_itp.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -216,8 +216,9 @@ ModCommand *target = Patterns[pat].GetpModCommand(0, 0); while(numCommands-- != 0) { + STATIC_ASSERT(sizeof(MODCOMMAND_ORIGINAL) == 6); MODCOMMAND_ORIGINAL data; - patternChunk.Read(data); + patternChunk.ReadStruct(data); *(target++) = data; } } Modified: trunk/OpenMPT/soundlib/Load_mod.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_mod.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_mod.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -573,7 +573,7 @@ // Read order information MODFileHeader fileHeader; - file.Read(fileHeader); + file.ReadStruct(fileHeader); file.Skip(4); // Magic bytes (we already parsed these) Order.ReadFromArray(fileHeader.orderList); @@ -862,7 +862,7 @@ } MODFileHeader fileHeader; - file.Read(fileHeader); + file.ReadStruct(fileHeader); // Sanity check: No more than 128 positions. ST's GUI limits tempo to [1, 220]. if(fileHeader.numOrders > 128 || fileHeader.restartPos == 0 || fileHeader.restartPos > 220) Modified: trunk/OpenMPT/soundlib/Load_mt2.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_mt2.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_mt2.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -495,7 +495,7 @@ for(CHANNELINDEX chn = 0; chn < channelsWithoutDrums; chn++, m++) { MT2Command cmd; - chunk.Read(cmd); + chunk.ReadStruct(cmd); ConvertMT2Command(this, *m, cmd); } } @@ -514,10 +514,13 @@ case MAGIC4LE('B','P','M','+'): if(0) { - double d; - if(chunk.Read(d) && fileHeader.samplesPerTick != 0 && d != 0.0) + if(chunk.CanRead(8)) { - m_nDefaultTempo = Util::Round<uint16>(44100.0 * 60.0 / (m_nDefaultSpeed * m_nDefaultRowsPerBeat * fileHeader.samplesPerTick * d)); + double d = chunk.ReadDoubleLE(); + if(fileHeader.samplesPerTick != 0 && d != 0.0) + { + m_nDefaultTempo = Util::Round<uint16>(44100.0 * 60.0 / (m_nDefaultSpeed * m_nDefaultRowsPerBeat * fileHeader.samplesPerTick * d)); + } } } break; Modified: trunk/OpenMPT/soundlib/Load_psm.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_psm.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_psm.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -347,7 +347,7 @@ { ChunkReader chunk(*subsongIter); PSMSongHeader songHeader; - if(!chunk.Read(songHeader)) + if(!chunk.ReadStruct(songHeader)) { return false; } Modified: trunk/OpenMPT/soundlib/Load_stm.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_stm.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_stm.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -94,6 +94,15 @@ uint8 reserved[13]; // More of PSi's internal crap STMSampleHeader samples[31]; // Sample headers uint8 order[128]; // Order list + + // Convert all multi-byte numeric values to current platform's endianness or vice versa. + void ConvertEndianness() + { + for(std::size_t i = 0; i < 32; ++i) + { + samples[i].ConvertEndianness(); + } + } }; STATIC_ASSERT(sizeof(STMFileHeader) == 1168); @@ -134,7 +143,7 @@ file.Rewind(); STMFileHeader fileHeader; - if(!file.Read(fileHeader) + if(!file.ReadConvertEndianness(fileHeader) || fileHeader.filetype != 2 || fileHeader.dosEof != 0x1A || (mpt::strnicmp(fileHeader.trackername, "!SCREAM!", 8) Modified: trunk/OpenMPT/soundlib/Load_ult.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_ult.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/Load_ult.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -383,7 +383,7 @@ UltFileHeader fileHeader; // Tracker ID - if(!file.Read(fileHeader) + if(!file.ReadStruct(fileHeader) || fileHeader.version < '1' || fileHeader.version > '4' || memcmp(fileHeader.signature, "MAS_UTrack_V00", sizeof(fileHeader.signature)) != 0) Modified: trunk/OpenMPT/soundlib/load_j2b.cpp =================================================================== --- trunk/OpenMPT/soundlib/load_j2b.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/soundlib/load_j2b.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -138,6 +138,12 @@ uint8 tempo; uint32 unknown; // 0x16078035 if original file was MOD, 0xC50100FF for everything else? it's 0xFF00FFFF in Carrotus.j2b (AMFF version) uint8 globalvolume; + + // Convert all multi-byte numeric values to current platform's endianness or vice versa. + void ConvertEndianness() + { + SwapBytesLE(unknown); + } }; STATIC_ASSERT(sizeof(AMFFMainChunk) == 73); @@ -766,7 +772,7 @@ FileReader chunk(chunks.GetChunk(isAM ? AMFFRiffChunk::idINIT : AMFFRiffChunk::idMAIN)); AMFFMainChunk mainChunk; if(!chunk.IsValid() - || !chunk.Read(mainChunk) + || !chunk.ReadConvertEndianness(mainChunk) || mainChunk.channels < 1 || !chunk.CanRead(mainChunk.channels)) { Modified: trunk/OpenMPT/unarchiver/ungzip.cpp =================================================================== --- trunk/OpenMPT/unarchiver/ungzip.cpp 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/unarchiver/ungzip.cpp 2014-10-06 10:45:14 UTC (rev 4388) @@ -27,7 +27,7 @@ //-------------------------------------------------------------- { inFile.Rewind(); - inFile.Read(header); + inFile.ReadConvertEndianness(header); // Check header data + file size if(header.magic1 != GZ_HMAGIC1 || header.magic2 != GZ_HMAGIC2 || header.method != GZ_HMDEFLATE || (header.flags & GZ_FRESERVED) != 0 Modified: trunk/OpenMPT/unarchiver/ungzip.h =================================================================== --- trunk/OpenMPT/unarchiver/ungzip.h 2014-10-06 10:42:14 UTC (rev 4387) +++ trunk/OpenMPT/unarchiver/ungzip.h 2014-10-06 10:45:14 UTC (rev 4388) @@ -32,6 +32,11 @@ uint32 mtime; // UNIX time uint8 xflags; // Available for use by specific compression methods. We ignore this. uint8 os; // Which OS was used to compress the file? We also ignore this. + + void ConvertEndianness() + { + SwapBytesLE(mtime); + } }; STATIC_ASSERT(sizeof(GZheader) == 10); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |