unrar.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * unrar.cpp
  3. * ---------
  4. * Purpose: Implementation file for extracting modules from .rar archives
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "unrar.h"
  11. #ifdef MPT_WITH_UNRAR
  12. #include "../common/mptFileIO.h"
  13. #if MPT_OS_WINDOWS
  14. #include <windows.h>
  15. #else // !MPT_OS_WINDOWS
  16. #ifdef _UNIX
  17. #define MPT_UNRAR_UNIX_WAS_DEFINED
  18. #else
  19. #define _UNIX
  20. #endif
  21. #endif // MPT_OS_WINDOWS
  22. #include "unrar/dll.hpp"
  23. #if !MPT_OS_WINDOWS
  24. #ifndef MPT_UNRAR_UNIX_WAS_DEFINED
  25. #undef _UNIX
  26. #undef MPT_UNRAR_UNIX_WAS_DEFINED
  27. #endif
  28. #endif // !MPT_OS_WINDOWS
  29. #endif // MPT_WITH_UNRAR
  30. OPENMPT_NAMESPACE_BEGIN
  31. #ifdef MPT_WITH_UNRAR
  32. struct RARHandle // RAII
  33. {
  34. HANDLE rar = nullptr;
  35. explicit RARHandle(HANDLE rar_) : rar(rar_) { return; }
  36. RARHandle(const RARHandle &) = delete;
  37. ~RARHandle() { if(rar) RARCloseArchive(rar); }
  38. operator HANDLE () const { return rar; }
  39. };
  40. static int CALLBACK RARCallback(unsigned int msg, LPARAM userData, LPARAM p1, LPARAM p2)
  41. {
  42. int result = 0;
  43. CRarArchive *that = reinterpret_cast<CRarArchive *>(userData);
  44. switch(msg)
  45. {
  46. case UCM_PROCESSDATA:
  47. // Receive extracted data
  48. that->RARCallbackProcessData(reinterpret_cast<const char *>(p1), p2);
  49. result = 1;
  50. break;
  51. default:
  52. // No support for passwords or volumes
  53. result = -1;
  54. break;
  55. }
  56. return result;
  57. }
  58. void CRarArchive::RARCallbackProcessData(const char * buf, std::size_t size)
  59. {
  60. if(!captureCurrentFile)
  61. {
  62. return;
  63. }
  64. mpt::append(data, buf, buf + size);
  65. }
  66. CRarArchive::CRarArchive(FileReader &file)
  67. : ArchiveBase(file)
  68. {
  69. // NOTE:
  70. // We open the archive twice, once for listing the contents in the
  71. // constructor and once for actual decompression in ExtractFile.
  72. // For solid archives, listing the contents via RAR_OM_LIST is way faster.
  73. // The overhead of opening twice for non-solid archives is negligable if the
  74. // archive does not contain a lot of files (and archives with large amount of
  75. // files are pretty useless for OpenMPT anyway).
  76. // Early reject files with no Rar! magic
  77. // so that we do not have to instantiate OnDiskFileWrapper.
  78. inFile.Rewind();
  79. if(!inFile.ReadMagic("Rar!\x1A"))
  80. {
  81. return;
  82. }
  83. inFile.Rewind();
  84. diskFile = std::make_unique<OnDiskFileWrapper>(inFile);
  85. if(!diskFile->IsValid())
  86. {
  87. return;
  88. }
  89. std::wstring ArcName = diskFile->GetFilename().ToWide();
  90. std::vector<wchar_t> ArcNameBuf(ArcName.c_str(), ArcName.c_str() + ArcName.length() + 1);
  91. std::vector<wchar_t> CmtBuf(65536);
  92. RAROpenArchiveDataEx ArchiveData = {};
  93. ArchiveData.OpenMode = RAR_OM_LIST;
  94. ArchiveData.ArcNameW = ArcNameBuf.data();
  95. ArchiveData.CmtBufW = CmtBuf.data();
  96. ArchiveData.CmtBufSize = static_cast<unsigned int>(CmtBuf.size());
  97. RARHandle rar(RAROpenArchiveEx(&ArchiveData));
  98. if(!rar)
  99. {
  100. Reset();
  101. return;
  102. }
  103. switch(ArchiveData.CmtState)
  104. {
  105. case 1:
  106. if(ArchiveData.CmtSize)
  107. {
  108. comment = mpt::ToUnicode(std::wstring(ArchiveData.CmtBufW, ArchiveData.CmtBufW + ArchiveData.CmtSize - 1));
  109. break;
  110. }
  111. [[fallthrough]];
  112. case 0:
  113. comment = mpt::ustring();
  114. break;
  115. default:
  116. Reset();
  117. return;
  118. break;
  119. }
  120. bool eof = false;
  121. int RARResult = 0;
  122. while(!eof)
  123. {
  124. RARHeaderDataEx HeaderData = {};
  125. RARResult = RARReadHeaderEx(rar, &HeaderData);
  126. switch(RARResult)
  127. {
  128. case ERAR_SUCCESS:
  129. break;
  130. case ERAR_END_ARCHIVE:
  131. eof = true;
  132. continue;
  133. break;
  134. default:
  135. Reset();
  136. return;
  137. break;
  138. }
  139. ArchiveFileInfo fileInfo;
  140. fileInfo.name = mpt::PathString::FromWide(HeaderData.FileNameW);
  141. fileInfo.type = ArchiveFileType::Normal;
  142. fileInfo.size = HeaderData.UnpSize;
  143. contents.push_back(fileInfo);
  144. RARResult = RARProcessFileW(rar, RAR_SKIP, NULL, NULL);
  145. switch(RARResult)
  146. {
  147. case ERAR_SUCCESS:
  148. break;
  149. default:
  150. Reset();
  151. return;
  152. break;
  153. }
  154. }
  155. }
  156. CRarArchive::~CRarArchive()
  157. {
  158. }
  159. bool CRarArchive::ExtractFile(std::size_t index)
  160. {
  161. if(!diskFile || !diskFile->IsValid())
  162. {
  163. return false;
  164. }
  165. if(index >= contents.size())
  166. {
  167. return false;
  168. }
  169. std::wstring ArcName = diskFile->GetFilename().ToWide();
  170. std::vector<wchar_t> ArcNameBuf(ArcName.c_str(), ArcName.c_str() + ArcName.length() + 1);
  171. RAROpenArchiveDataEx ArchiveData = {};
  172. ArchiveData.OpenMode = RAR_OM_EXTRACT;
  173. ArchiveData.ArcNameW = ArcNameBuf.data();
  174. ArchiveData.Callback = RARCallback;
  175. ArchiveData.UserData = reinterpret_cast<LPARAM>(this);
  176. RARHandle rar(RAROpenArchiveEx(&ArchiveData));
  177. if(!rar)
  178. {
  179. ResetFile();
  180. return false;
  181. }
  182. std::size_t i = 0;
  183. int RARResult = 0;
  184. bool eof = false;
  185. while(!eof)
  186. {
  187. RARHeaderDataEx HeaderData = {};
  188. RARResult = RARReadHeaderEx(rar, &HeaderData);
  189. switch(RARResult)
  190. {
  191. case ERAR_SUCCESS:
  192. break;
  193. case ERAR_END_ARCHIVE:
  194. eof = true;
  195. continue;
  196. break;
  197. default:
  198. ResetFile();
  199. return false;
  200. break;
  201. }
  202. captureCurrentFile = (i == index);
  203. RARResult = RARProcessFileW(rar, captureCurrentFile ? RAR_TEST : RAR_SKIP, NULL, NULL);
  204. switch(RARResult)
  205. {
  206. case ERAR_SUCCESS:
  207. break;
  208. default:
  209. ResetFile();
  210. return false;
  211. break;
  212. }
  213. if(captureCurrentFile)
  214. { // done
  215. return true;
  216. }
  217. captureCurrentFile = false;
  218. ++i;
  219. }
  220. return false;
  221. }
  222. void CRarArchive::Reset()
  223. {
  224. captureCurrentFile = false;
  225. comment = mpt::ustring();
  226. contents = std::vector<ArchiveFileInfo>();
  227. data = std::vector<char>();
  228. }
  229. void CRarArchive::ResetFile()
  230. {
  231. captureCurrentFile = false;
  232. data = std::vector<char>();
  233. }
  234. #endif // MPT_WITH_UNRAR
  235. OPENMPT_NAMESPACE_END