1
0

UMXTools.cpp 8.6 KB

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