1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
* UMXTools.h
* ------------
* Purpose: UMX/UAX (Unreal package) helper functions
* Notes : (currently none)
* Authors: OpenMPT Devs (inspired by code from https://wiki.beyondunreal.com/Legacy:Package_File_Format)
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "UMXTools.h"
#include "Loaders.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}) - static_cast<uint32>(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());
}
// Read compressed unreal integers - similar to MIDI integers, but signed values are possible.
template <typename Tfile>
static int32 ReadIndexImpl(Tfile &chunk)
{
enum
{
signMask = 0x80, // Highest bit of first byte indicates if value is signed
valueMask1 = 0x3F, // Low 6 bits of first byte are actual value
continueMask1 = 0x40, // Second-highest bit of first byte indicates if further bytes follow
valueMask = 0x7F, // Low 7 bits of following bytes are actual value
continueMask = 0x80, // Highest bit of following bytes indicates if further bytes follow
};
// Read first byte
uint8 b = chunk.ReadUint8();
bool isSigned = (b & signMask) != 0;
uint32 result = (b & valueMask1);
int shift = 6;
if(b & continueMask1)
{
// Read remaining bytes
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);
}
// Returns true if the given nme exists in the name table.
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::pos_type 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); // Object flags
}
}
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);
}
// Read an entry from the name table.
std::string ReadNameTableEntry(FileReader &chunk, uint16 packageVersion)
{
std::string name;
if(packageVersion >= 64)
{
// String length
int32 length = ReadIndex(chunk);
if(length <= 0)
{
return "";
}
name.reserve(std::min(length, mpt::saturate_cast<int32>(chunk.BytesLeft())));
}
// Simple zero-terminated string
uint8 chr;
while((chr = chunk.ReadUint8()) != 0)
{
// Convert string to lower case
if(chr >= 'A' && chr <= 'Z')
{
chr = chr - 'A' + 'a';
}
name.append(1, static_cast<char>(chr));
}
chunk.Skip(4); // Object flags
return name;
}
// Read complete name table.
std::vector<std::string> ReadNameTable(FileReader &file, const FileHeader &fileHeader)
{
file.Seek(fileHeader.nameOffset); // nameOffset and nameCount were validated when parsing header
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;
}
// Read an entry from the import table.
int32 ReadImportTableEntry(FileReader &chunk, uint16 packageVersion)
{
ReadIndex(chunk); // Class package
ReadIndex(chunk); // Class name
if(packageVersion >= 60)
{
chunk.Skip(4); // Package
} else
{
ReadIndex(chunk); // ??
}
return ReadIndex(chunk); // Object name (offset into the name table)
}
// Read import table.
std::vector<int32> ReadImportTable(FileReader &file, const FileHeader &fileHeader, const std::vector<std::string> &names)
{
file.Seek(fileHeader.importOffset); // importOffset and importCount were validated when parsing header
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;
}
// Read an entry from the export table.
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)); // Object class
if(objClass >= classes.size())
return {};
ReadIndex(file); // Object parent
if(fileHeader.packageVersion >= 60)
{
file.Skip(4); // Internal package / group of the object
}
int32 objName = ReadIndex(file); // Object name (offset into the name table)
file.Skip(4); // Object flags
int32 objSize = ReadIndex(file);
int32 objOffset = ReadIndex(file);
if(objSize <= 0 || objOffset <= static_cast<int32>(sizeof(FileHeader)))
return {};
// If filterType is set, reject any objects not of that type
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); // 00 00 00 00 00 00 00 00
}
if(fileHeader.packageVersion < 60)
{
chunk.Skip(16); // 81 00 00 00 00 00 FF FF FF FF FF FF FF FF 00 00
}
// Read object properties
#if 0
size_t propertyName = static_cast<size_t>(ReadIndex(chunk));
if(propertyName >= names.size() || names[propertyName] != "none")
{
// Can't bother to implement property reading, as no UMX files I've seen so far use properties for the relevant objects,
// and only the UAX files in the Unreal 1997/98 beta seem to use this and still load just fine when ignoring it.
// If it should be necessary to implement this, check CUnProperty.cpp in http://ut-files.com/index.php?dir=Utilities/&file=utcms_source.zip
MPT_ASSERT_NOTREACHED();
continue;
}
#else
ReadIndex(chunk);
#endif
if(fileHeader.packageVersion >= 120)
{
// UT2003 Packages
ReadIndex(chunk);
chunk.Skip(8);
} else if(fileHeader.packageVersion >= 100)
{
// AAO Packages
chunk.Skip(4);
ReadIndex(chunk);
chunk.Skip(4);
} else if(fileHeader.packageVersion >= 62)
{
// UT Packages
// Mech8.umx and a few other UT tunes have packageVersion = 62.
// In CUnSound.cpp, the condition above reads "packageVersion >= 63" but if that is used, those tunes won't load properly.
ReadIndex(chunk);
chunk.Skip(4);
} else
{
// Old Unreal Packagaes
ReadIndex(chunk);
}
int32 size = ReadIndex(chunk);
return {chunk.ReadChunk(size), objName};
}
} // namespace UMX
OPENMPT_NAMESPACE_END
|