123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #include "stdafx.h"
- #include "Loaders.h"
- #include "UMXTools.h"
- OPENMPT_NAMESPACE_BEGIN
- namespace UMX
- {
- bool FileHeader::IsValid() const
- {
- return !std::memcmp(magic, "\xC1\x83\x2A\x9E", 4)
- && nameOffset >= sizeof(FileHeader)
- && exportOffset >= sizeof(FileHeader)
- && importOffset >= sizeof(FileHeader)
- && nameCount > 0 && nameCount <= uint32_max / 5u
- && exportCount > 0 && exportCount <= uint32_max / 8u
- && importCount > 0 && importCount <= uint32_max / 4u
- && uint32_max - nameCount * 5u >= nameOffset
- && uint32_max - exportCount * 8u >= exportOffset
- && uint32_max - importCount * 4u >= importOffset;
- }
- uint32 FileHeader::GetMinimumAdditionalFileSize() const
- {
- return std::max({nameOffset + nameCount * 5u, exportOffset + exportCount * 8u, importOffset + importCount * 4u}) - sizeof(FileHeader);
- }
- CSoundFile::ProbeResult ProbeFileHeader(MemoryFileReader file, const uint64 *pfilesize, const char *requiredType)
- {
- FileHeader fileHeader;
- if(!file.ReadStruct(fileHeader))
- {
- return CSoundFile::ProbeWantMoreData;
- }
- if(!fileHeader.IsValid())
- {
- return CSoundFile::ProbeFailure;
- }
- if(requiredType != nullptr && !FindNameTableEntryMemory(file, fileHeader, requiredType))
- {
- return CSoundFile::ProbeFailure;
- }
- return CSoundFile::ProbeAdditionalSize(file, pfilesize, fileHeader.GetMinimumAdditionalFileSize());
- }
- template <typename Tfile>
- static int32 ReadIndexImpl(Tfile &chunk)
- {
- enum
- {
- signMask = 0x80,
- valueMask1 = 0x3F,
- continueMask1 = 0x40,
- valueMask = 0x7F,
- continueMask = 0x80,
- };
-
- uint8 b = chunk.ReadUint8();
- bool isSigned = (b & signMask) != 0;
- uint32 result = (b & valueMask1);
- int shift = 6;
- if(b & continueMask1)
- {
-
- do
- {
- b = chunk.ReadUint8();
- uint32 data = static_cast<uint32>(b) & valueMask;
- data <<= shift;
- result |= data;
- shift += 7;
- } while((b & continueMask) != 0 && (shift < 32));
- }
- if(isSigned && result <= int32_max)
- return -static_cast<int32>(result);
- else if(isSigned)
- return int32_min;
- else
- return result;
- }
- int32 ReadIndex(FileReader &chunk)
- {
- return ReadIndexImpl(chunk);
- }
- template <typename TFile>
- static bool FindNameTableEntryImpl(TFile &file, const FileHeader &fileHeader, const char *name)
- {
- if(!name)
- {
- return false;
- }
- const std::size_t nameLen = std::strlen(name);
- if(nameLen == 0)
- {
- return false;
- }
- bool result = false;
- const FileReader::off_t oldpos = file.GetPosition();
- if(file.Seek(fileHeader.nameOffset))
- {
- for(uint32 i = 0; i < fileHeader.nameCount && file.CanRead(5); i++)
- {
- if(fileHeader.packageVersion >= 64)
- {
- int32 length = ReadIndexImpl(file);
- if(length <= 0)
- {
- continue;
- }
- }
- bool match = true;
- std::size_t pos = 0;
- char c = 0;
- while((c = file.ReadUint8()) != 0)
- {
- c = mpt::ToLowerCaseAscii(c);
- if(pos < nameLen)
- {
- match = match && (c == name[pos]);
- }
- pos++;
- }
- if(pos != nameLen)
- {
- match = false;
- }
- if(match)
- {
- result = true;
- }
- file.Skip(4);
- }
- }
- file.Seek(oldpos);
- return result;
- }
- bool FindNameTableEntry(FileReader &file, const FileHeader &fileHeader, const char *name)
- {
- return FindNameTableEntryImpl(file, fileHeader, name);
- }
- bool FindNameTableEntryMemory(MemoryFileReader &file, const FileHeader &fileHeader, const char *name)
- {
- return FindNameTableEntryImpl(file, fileHeader, name);
- }
- std::string ReadNameTableEntry(FileReader &chunk, uint16 packageVersion)
- {
- std::string name;
- if(packageVersion >= 64)
- {
-
- int32 length = ReadIndex(chunk);
- if(length <= 0)
- {
- return "";
- }
- name.reserve(std::min(length, mpt::saturate_cast<int32>(chunk.BytesLeft())));
- }
-
- uint8 chr;
- while((chr = chunk.ReadUint8()) != 0)
- {
-
- if(chr >= 'A' && chr <= 'Z')
- {
- chr = chr - 'A' + 'a';
- }
- name.append(1, static_cast<char>(chr));
- }
- chunk.Skip(4);
- return name;
- }
- std::vector<std::string> ReadNameTable(FileReader &file, const FileHeader &fileHeader)
- {
- file.Seek(fileHeader.nameOffset);
- std::vector<std::string> names;
- names.reserve(fileHeader.nameCount);
- for(uint32 i = 0; i < fileHeader.nameCount && file.CanRead(5); i++)
- {
- names.push_back(ReadNameTableEntry(file, fileHeader.packageVersion));
- }
- return names;
- }
- int32 ReadImportTableEntry(FileReader &chunk, uint16 packageVersion)
- {
- ReadIndex(chunk);
- ReadIndex(chunk);
- if(packageVersion >= 60)
- {
- chunk.Skip(4);
- } else
- {
- ReadIndex(chunk);
- }
- return ReadIndex(chunk);
- }
- std::vector<int32> ReadImportTable(FileReader &file, const FileHeader &fileHeader, const std::vector<std::string> &names)
- {
- file.Seek(fileHeader.importOffset);
- std::vector<int32> classes;
- classes.reserve(fileHeader.importCount);
- for(uint32 i = 0; i < fileHeader.importCount && file.CanRead(4); i++)
- {
- int32 objName = ReadImportTableEntry(file, fileHeader.packageVersion);
- if(static_cast<size_t>(objName) < names.size())
- {
- classes.push_back(objName);
- }
- }
- return classes;
- }
- std::pair<FileReader, int32> ReadExportTableEntry(FileReader &file, const FileHeader &fileHeader, const std::vector<int32> &classes, const std::vector<std::string> &names, const char *filterType)
- {
- const uint32 objClass = ~static_cast<uint32>(ReadIndex(file));
- if(objClass >= classes.size())
- return {};
- ReadIndex(file);
- if(fileHeader.packageVersion >= 60)
- {
- file.Skip(4);
- }
- int32 objName = ReadIndex(file);
- file.Skip(4);
- int32 objSize = ReadIndex(file);
- int32 objOffset = ReadIndex(file);
- if(objSize <= 0 || objOffset <= static_cast<int32>(sizeof(FileHeader)))
- return {};
-
- if(filterType != nullptr && names[classes[objClass]] != filterType)
- return {};
- FileReader chunk = file.GetChunkAt(objOffset, objSize);
- if(!chunk.IsValid())
- return {};
- if(fileHeader.packageVersion < 40)
- {
- chunk.Skip(8);
- }
- if(fileHeader.packageVersion < 60)
- {
- chunk.Skip(16);
- }
-
- #if 0
- size_t propertyName = static_cast<size_t>(ReadIndex(chunk));
- if(propertyName >= names.size() || names[propertyName] != "none")
- {
-
-
-
- MPT_ASSERT_NOTREACHED();
- continue;
- }
- #else
- ReadIndex(chunk);
- #endif
- if(fileHeader.packageVersion >= 120)
- {
-
- ReadIndex(chunk);
- chunk.Skip(8);
- } else if(fileHeader.packageVersion >= 100)
- {
-
- chunk.Skip(4);
- ReadIndex(chunk);
- chunk.Skip(4);
- } else if(fileHeader.packageVersion >= 62)
- {
-
-
-
- ReadIndex(chunk);
- chunk.Skip(4);
- } else
- {
-
- ReadIndex(chunk);
- }
- int32 size = ReadIndex(chunk);
- return {chunk.ReadChunk(size), objName};
- }
- }
- OPENMPT_NAMESPACE_END
|